diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index 57044c7cf..32484aaa2 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -4749,8 +4749,12 @@ cache is answered compile-required on both. front-facing winding is Vulkan's front face of the same name. A viewport of no width or height, or with a NaN origin x, draws nothing, as Metal's. Metal clips a draw to the pixels its viewport covers, so the scissor is Metal's rectangle cut to them; a rectangle past Metal's render area, - which Metal's validation stops, is cut to it, as plain Metal cuts it. A stencil reference - saturates at 255, as Metal stores it. + which Metal's validation stops, is cut to it, as plain Metal cuts it. A viewport Vulkan takes no + form of (a negative width, a depth range past `[0, 1]`, or an extent or corner past the + device's `maxViewportDimensions` and `viewportBoundsRange`) that covers no pixel of Metal's + render area, such as one whose origin lies far past the area, draws nothing, as Metal's: the + draw records a viewport of no extent at the area's origin behind an empty scissor. A stencil + reference saturates at 255, as Metal stores it. - `set_viewports` and `set_scissor_rects` are `setViewports:count:` and `setScissorRects:count:`, 1 to 16 each, recorded with their count (`vkCmdSetViewportWithCount`, `vkCmdSetScissorWithCount`, so no pipeline is made for a count). A vertex function's @@ -4887,8 +4891,13 @@ cache is answered compile-required on both. (`set_line_width`, which keeps every width) is not 1 (`LineWidth`): Metal draws such a line as a parallelogram that width wide, and Vulkan's wider lines are a device feature (`wideLines`) the floor does not require; a - viewport Vulkan's is not (a - negative width, a depth range past `[0, 1]`, bounds past the device's viewport limits), a + viewport Vulkan's is not that covers a pixel of Metal's render area (`set_viewport`, + `set_viewports`), as no Vulkan viewport maps clip space as it does: a negative width, which + Metal mirrors; a depth range past `[0, 1]`; or an extent or corner past the device's + `maxViewportDimensions` and `viewportBoundsRange`, which, as Vulkan's bounds reach at least + twice the largest extent past either side of the origin, is a viewport wider or taller than + `maxViewportDimensions` over the area, or one whose edge lies inside the last pixel of an area + of that largest size; a pipeline state whose formats are not the pass's attachments' (but for colours past the pass's last colour attachment, above), a draw that binds a texture whose image the pass renders, where neither the feedback loop nor a copy reads it: on a device diff --git a/crates/metal2vulkan-runtime/src/error.rs b/crates/metal2vulkan-runtime/src/error.rs index 4fce426de..944051212 100644 --- a/crates/metal2vulkan-runtime/src/error.rs +++ b/crates/metal2vulkan-runtime/src/error.rs @@ -1749,8 +1749,10 @@ pub enum DrawKind { /// No viewport or scissor rectangle, or more than Metal's 16 (`setViewports:count:`, /// `setScissorRects:count:`), which Metal's validation stops. Viewports, - /// A viewport Vulkan's is not: a negative width (Metal mirrors the draw), a depth range past - /// `[0, 1]`, or bounds past the device's viewport limits. + /// A viewport Vulkan's is not that covers a pixel of the render area: a negative width (Metal + /// mirrors the draw), a depth range past `[0, 1]`, or bounds past the device's viewport + /// limits (`maxViewportDimensions`, `viewportBoundsRange`); no Vulkan viewport maps clip space + /// as it does. One that covers no pixel draws nothing, as Metal's. Viewport, /// A pipeline state whose colour, depth or stencil formats are not its pass's attachments', /// or whose `rasterSampleCount` is not their sample count: Metal's validation stops such diff --git a/crates/metal2vulkan-runtime/src/render_encoder.rs b/crates/metal2vulkan-runtime/src/render_encoder.rs index 6c2665ce4..c3ccf27af 100644 --- a/crates/metal2vulkan-runtime/src/render_encoder.rs +++ b/crates/metal2vulkan-runtime/src/render_encoder.rs @@ -1417,10 +1417,13 @@ impl RenderCommandEncoder { /// `setViewport:`. Metal's default covers Metal's render area with the depth range `[0, 1]`. A /// viewport of no width or height draws nothing, as Metal's; so does one whose origin's x is /// NaN, as plain Metal draws nothing through it. A negative height flips the draw, as Metal's. + /// A viewport Vulkan takes no form of (a negative width, a depth range past `[0, 1]`, or + /// bounds past the device's `maxViewportDimensions` and `viewportBoundsRange`) that covers no + /// pixel of Metal's render area draws nothing, as Metal draws nothing through it. /// /// Err: [`Refusal::ViewportNotFinite`] for any other field NaN or infinite; [`Gap::Draw`] of - /// [`DrawKind::Viewport`] for a negative width, a depth range past `[0, 1]`, or bounds past - /// the device's `maxViewportDimensions` and `viewportBoundsRange`. + /// [`DrawKind::Viewport`] for a viewport Vulkan takes no form of that covers a pixel of the + /// render area. pub fn set_viewport(&mut self, viewport: Viewport) -> Result<(), Error> { let v = self.checked(viewport)?; self.viewports[0] = v; @@ -1429,6 +1432,12 @@ impl RenderCommandEncoder { } /// `viewport` as a draw takes it, checked as [`set_viewport`](Self::set_viewport) says. + /// + /// A viewport Vulkan takes as it is stays itself. No other Vulkan viewport maps clip space as + /// one past Vulkan's rules does. One that covers no pixel of the render area, through which + /// Metal draws nothing, becomes a viewport of no width or height at the area's origin with + /// its depths cut to `[0, 1]`: Vulkan takes it, it covers no pixel either + /// (`viewport_pixels`), and a draw through it records an empty scissor. fn checked(&self, viewport: Viewport) -> Result { let v = match viewport.origin_x.is_nan() { true => Viewport { @@ -1447,6 +1456,10 @@ impl RenderCommandEncoder { let vk = vk_viewport(&v); let [x, y, width, height] = [vk.x, vk.y, vk.width, vk.height].map(f64::from); let unit = 0.0..=1.0; + // `VkViewport`'s valid usage: a positive width, depths within `[0, 1]` (the runtime + // enables no `VK_EXT_depth_range_unrestricted`), and the extent and corners within + // `maxViewportDimensions` and `viewportBoundsRange` (VUID-VkViewport-width-01771, + // -height-01773, -x-01774, -x-01232, -y-01775, -y-01776, -y-01777 and -y-01233). let fits = v.width >= 0.0 && unit.contains(&v.znear) && unit.contains(&v.zfar) @@ -1455,12 +1468,23 @@ impl RenderCommandEncoder { && x >= low && x + width <= high && [y, y + height].iter().all(|y| (low..=high).contains(y)); - if !fits { - return Err(Error::Unsupported(Gap::Draw { + if fits { + return Ok(v); + } + let [columns, rows] = viewport_pixels(&v, self.pass.area); + match columns[0] == columns[1] || rows[0] == rows[1] { + true => Ok(Viewport { + origin_x: 0.0, + origin_y: 0.0, + width: 0.0, + height: 0.0, + znear: v.znear.clamp(0.0, 1.0), + zfar: v.zfar.clamp(0.0, 1.0), + }), + false => Err(Error::Unsupported(Gap::Draw { kind: DrawKind::Viewport, - })); + })), } - Ok(v) } /// The first `count` viewports are set: each later draw records them, and one whose vertex diff --git a/crates/metal2vulkan-runtime/src/tests/draw.rs b/crates/metal2vulkan-runtime/src/tests/draw.rs index bb395037b..40b108032 100644 --- a/crates/metal2vulkan-runtime/src/tests/draw.rs +++ b/crates/metal2vulkan-runtime/src/tests/draw.rs @@ -12,9 +12,10 @@ //! pipeline and copies no byte of the runtime's own; a memory barrier after draws that wrote //! no memory records nothing. //! - viewports past the device's `viewportBoundsRange` or `maxViewportDimensions`, on one side -//! each and over no pixel of the target, draw nothing, set alone or among several, and a -//! draw after them through a viewport that fits lands where Metal puts it; a viewport wider -//! than the device's largest, over the target, is refused. +//! each, and a mirrored one and one with depths past `[0, 1]`, all over no pixel of the +//! target, draw nothing, set alone or among several, and a draw after them through a +//! viewport that fits lands where Metal puts it; a viewport wider than the device's largest, +//! over the target, is refused. //! - 2,000 draws in one encoder move none of the device's counters while it is open; its end //! adds the pass, the push and the 1,999 pushes held. //! - on a GPU, the lock budget (`crate::tests::locks`): 2,000 draws more in a recording, and @@ -71,7 +72,8 @@ //! is recorded, and 16-bit indices there draw. //! - what Metal refuses is refused, and what a draw does not build is a gap: no pipeline state, //! a pipeline state of other formats than the pass's, a depth test, a stencil test without a -//! stencil attachment, a misaligned index offset, a non-finite viewport and a mirrored one. +//! stencil attachment, a misaligned index offset, a non-finite viewport and a mirrored one over +//! the target. //! - a pass with a visibility result buffer draws, its command buffer keeps the buffer until it //! retires, and its bytes stay as they were; `Disabled` at any offset records nothing, and the //! pass takes the submission, barrier and pass one without the buffer takes (`Boolean` and @@ -427,11 +429,12 @@ pub(crate) mod on_device { }); } - /// A viewport past the device's `viewportBoundsRange` or `maxViewportDimensions` that covers - /// no pixel of the render area draws nothing, as Metal draws nothing through it, and a draw - /// after it through a viewport that fits lands where Metal puts it; one past them that covers - /// a pixel is refused. The layer checks each viewport recorded against the device's limits - /// (`VUID-VkViewport-x-01232` and the rest of `VkViewport`'s). + /// A viewport past the device's `viewportBoundsRange` or `maxViewportDimensions`, or with a + /// negative width or a depth range past `[0, 1]`, that covers no pixel of the render area + /// draws nothing, as Metal draws nothing through it, and a draw after it through a viewport + /// that fits lands where Metal puts it; one past the device's limits that covers a pixel is + /// refused. The layer checks each viewport recorded against `VkViewport`'s valid usage + /// (`VUID-VkViewport-x-01232` and the rest). #[test] fn every_device_draws_nothing_through_a_viewport_past_its_bounds() { let Some(library) = examples() else { @@ -471,7 +474,9 @@ pub(crate) mod on_device { // Each past one of the device's limits, over no pixel of the target: an origin whose // x plus the width passes the range's top (a WebGL page's far viewport), an origin // below the range's bottom, the same two in y, a flipped one whose origin passes the - // top, and one wider, and one taller, than the device's largest viewport. + // top, and one wider, and one taller, than the device's largest viewport; then the + // other two Vulkan takes in no form, over no pixel either: a negative width and a + // depth range past `[0, 1]`. let past = [ ( "x plus width past the range", @@ -495,6 +500,17 @@ pub(crate) mod on_device { "taller than the largest", at(0.0, -(tall + 2.0), side, tall + 1.0), ), + ( + "a mirrored one left of the target", + at(0.0, 0.0, -side, side), + ), + ( + "depths past [0, 1] left of the target", + Viewport { + zfar: 2.0, + ..at(-2.0 * side, 0.0, side, side) + }, + ), ]; for (case, viewport) in past { (encoder.set_viewport(viewport)) @@ -3498,6 +3514,7 @@ define internal void @_GLOBAL__sub_I_() section "air.static_init" { ); same( encoder.set_viewport(Viewport { + origin_x: 4.0, width: -4.0, ..viewport }),