diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index e0da5a458..f04a58005 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -5472,11 +5472,22 @@ cache is answered compile-required on both. 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 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. + form of (a negative width, 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. +- A viewport depth range past `[0, 1]` (OpenGL's `[-1, 1]`) is set, as Metal and its API + validation take it. Metal maps clip space's z through it unclamped into a fragment function's + `[[position]].z`, and a depth attachment stores that, and a `[[depth]]` output, clamped to + `[0, 1]` (an Apple M3 Max, macOS 27.0.1: the conformance crate's + `probes/viewport_depth_past_unit.m`). The runtime enables no `VK_EXT_depth_range_unrestricted`, + so it records the range cut to `[0, 1]`, which shows nowhere in a draw whose pass has no depth + attachment and whose fragment function reads no `[[position]]` z or w, since clipping is in + clip space; such a draw draws as Metal's. Every other draw while such a range is set is a known + gap, refused at the draw (`DrawKind::Viewport`). Whether a range is past `[0, 1]` is found when + the viewports are set, and whether a draw is refused for it when they or the pipeline state + are set, so a draw reads one field for 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 @@ -5656,11 +5667,13 @@ cache is answered compile-required on both. 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 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 + width, which Metal mirrors; 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 + that largest size; at a draw, a viewport depth range past `[0, 1]` where the draw's pass has a + depth attachment or its fragment function may read `[[position]]`'s z or w (above); 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 without the attachment reads, from the vertex function at the rendered level and layer, written, of a depth or stencil diff --git a/crates/metal2vulkan-runtime/src/depth_stencil.rs b/crates/metal2vulkan-runtime/src/depth_stencil.rs index 4f6832503..de097b7a9 100644 --- a/crates/metal2vulkan-runtime/src/depth_stencil.rs +++ b/crates/metal2vulkan-runtime/src/depth_stencil.rs @@ -235,8 +235,9 @@ impl DepthStencilState { /// /// A negative or NaN depth, which Metal's test compares by its magnitude, takes no note: the /// runtime enables no `VK_EXT_depth_range_unrestricted` and refuses a clear depth outside - /// `[0, 1]` (`RenderPassKind::ClearDepth`) and a viewport's depth range past it - /// (`DrawKind::Viewport`), so a depth attachment holds such a value only where Vulkan leaves a + /// `[0, 1]` (`RenderPassKind::ClearDepth`) and a draw into a depth attachment through a + /// viewport's depth range past it (`DrawKind::Viewport`), so a depth attachment holds such a + /// value only where Vulkan leaves a /// depth past the viewport's range undefined, or where an upload put one there, which is the /// device's under the upload's own note /// ([`Texture::FLOAT_DEPTH_UPLOAD_NOTE`](crate::Texture::FLOAT_DEPTH_UPLOAD_NOTE)). diff --git a/crates/metal2vulkan-runtime/src/error.rs b/crates/metal2vulkan-runtime/src/error.rs index 9ea7312b9..076a191ea 100644 --- a/crates/metal2vulkan-runtime/src/error.rs +++ b/crates/metal2vulkan-runtime/src/error.rs @@ -2092,9 +2092,11 @@ pub enum DrawKind { /// `setScissorRects:count:`), which Metal's validation stops. Viewports, /// 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. + /// mirrors the draw), or bounds past the device's viewport limits (`maxViewportDimensions`, + /// `viewportBoundsRange`), refused where it is set; or, at a draw, a depth range past + /// `[0, 1]` where the draw's pass has a depth attachment or its fragment function may read + /// `[[position]]`'s z or w. 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.rs b/crates/metal2vulkan-runtime/src/render.rs index 19a206dad..1c9b4efc8 100644 --- a/crates/metal2vulkan-runtime/src/render.rs +++ b/crates/metal2vulkan-runtime/src/render.rs @@ -418,6 +418,10 @@ pub(crate) struct RenderCompiled { /// take in a pipeline that rasterizes at custom sample locations /// (`VUID-VkGraphicsPipelineCreateInfo-sampleLocationsEnable-01524`). interpolates_at_sample: bool, + /// Whether the fragment function may read `[[position]]`'s z or w + /// (`metal2vulkan::library::CompiledFunction::reads_position_depth`), where a viewport's depth + /// range shows; `false` without a fragment function. + reads_position_depth: bool, /// The translator's notes of where the pipeline's answers may differ from Metal's, and its /// functions' constexpr samplers' (`constexpr_notes`). notes: Vec, @@ -1589,6 +1593,12 @@ impl RenderCompiled { rasterizes_points: pipeline.graphics.writes_point_size || lowered.input_primitive_topology == PrimitiveTopologyClass::Point, interpolates_at_sample: pipeline.graphics.interpolates_at_sample, + // Lowered, the fragment function is compiled (`lower_stages`, `recorded`). + reads_position_depth: match &lowered.fragment { + None => false, + Some(StageSource::Compiled(function)) => function.reads_position_depth(), + Some(StageSource::Function(_)) => true, + }, notes, digest: pipeline.content_digest(), allocated_size: (pipeline.stages.iter()) @@ -1644,6 +1654,13 @@ impl RenderCompiled { } } + /// Whether the fragment function may read `[[position]]`'s z or w (`reads_position_depth`), + /// where a viewport's depth range past `[0, 1]` shows + /// ([`RenderCommandEncoder::set_viewport`](crate::RenderCommandEncoder::set_viewport)). + pub(crate) fn reads_position_depth(&self) -> bool { + self.reads_position_depth + } + /// Whether a draw of points rasterizes them (`rasterizes_points`): else the encoder draws them /// behind an empty scissor, its vertex function run. pub(crate) fn rasterizes_points(&self) -> bool { diff --git a/crates/metal2vulkan-runtime/src/render_encoder.rs b/crates/metal2vulkan-runtime/src/render_encoder.rs index edfa8ec47..257874ab0 100644 --- a/crates/metal2vulkan-runtime/src/render_encoder.rs +++ b/crates/metal2vulkan-runtime/src/render_encoder.rs @@ -812,6 +812,14 @@ pub struct RenderCommandEncoder { patch_indices: [u64; 2], /// Whether the pipeline state renders into the pass's attachments' formats. renders_into: bool, + /// Whether a viewport set has a depth range past `[0, 1]` (`set_viewport`), found when the + /// viewports are set. + depth_past: bool, + /// What every draw is refused for while the pipeline state and the viewports are what they + /// are ([`draws_refused`](Self::draws_refused)), found when either is set, so a draw reads + /// one field: `AttachmentFormats` where the state does not render into the pass, else + /// `Viewport` where a depth range past `[0, 1]` would show. + refused: Option, /// The depth-stencil state's tests, Metal's default state's where none is set. tests: Tests, /// The tests the encoder last recorded, none before its first draw. @@ -928,6 +936,8 @@ impl CommandBuffer { factors: None, patch_indices: [0; 2], renders_into: false, + depth_past: false, + refused: Some(DrawKind::AttachmentFormats), tests: Tests::of(&DepthStencilDescriptor::default()), viewports: [Viewport { origin_x: 0.0, @@ -1206,9 +1216,30 @@ impl RenderCommandEncoder { self.base = Some(state); self.words = 0; self.srgb = [0; 2]; + self.refused = self.draws_refused(); self.changed(); } + /// What every draw is refused for under the pipeline state and the viewports set + /// ([`refused`](Self::refused)): [`DrawKind::AttachmentFormats`] where the state does not + /// render into the pass's formats; else [`DrawKind::Viewport`] where a viewport's depth range + /// is past `[0, 1]` ([`set_viewport`](Self::set_viewport)) and the pass has a depth attachment + /// or the fragment function may read `[[position]]`'s z or w, where that range shows. The + /// contents a state draws through (`bind_content`) share its fragment function, so the state + /// set answers for them. + fn draws_refused(&self) -> Option { + if !self.renders_into { + return Some(DrawKind::AttachmentFormats); + } + let reads = || { + (self.base.as_ref()).is_some_and(|state| state.inner.compiled.reads_position_depth()) + }; + match self.depth_past && (self.pass.depth.is_some() || reads()) { + true => Some(DrawKind::Viewport), + false => None, + } + } + /// Where the buffers and textures bound change what a draw draws through: `base`, or the /// state's content for them ([`RenderPipelineState::for_draws`]), found in the device's map of /// contents or made now: where a buffer argument of a function the state lowers whose @@ -1583,9 +1614,18 @@ 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. + /// A viewport Vulkan takes no form of (a negative width, 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. + /// + /// A depth range past `[0, 1]` is set, as Metal and its API validation take it. Metal maps + /// clip space's z through it unclamped into a fragment function's `[[position]].z`, and a + /// depth attachment stores that, and a `[[depth]]` output, clamped to `[0, 1]` (an Apple M3 + /// Max, macOS 27.0.1). Vulkan takes no such range here (the runtime enables no + /// `VK_EXT_depth_range_unrestricted`), so the viewport is recorded with its range cut to + /// `[0, 1]`, which a draw shows nowhere where its pass has no depth attachment and its + /// fragment function reads no `[[position]]` z or w: clipping is in clip space. Any other draw + /// while such a range is set is refused ([`Gap::Draw`] of [`DrawKind::Viewport`]). /// /// Err: [`Refusal::ViewportNotFinite`] for any other field NaN or infinite; [`Gap::Draw`] of /// [`DrawKind::Viewport`] for a viewport Vulkan takes no form of that covers a pixel of the @@ -1621,14 +1661,13 @@ impl RenderCommandEncoder { let [low, high] = limits.viewport_bounds.map(f64::from); 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 + // `VkViewport`'s valid usage: a positive width, 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). + // -height-01773, -x-01774, -x-01232, -y-01775, -y-01776, -y-01777 and -y-01233). Its + // depths within `[0, 1]` (the runtime enables no `VK_EXT_depth_range_unrestricted`) are + // `vk_viewport`'s, which cuts them there: a range past it is kept, and the draws it would + // show in are refused (`draws_refused`). let fits = v.width >= 0.0 - && unit.contains(&v.znear) - && unit.contains(&v.zfar) && width <= f64::from(limits.max_viewport[0]) && height.abs() <= f64::from(limits.max_viewport[1]) && x >= low @@ -1660,6 +1699,11 @@ impl RenderCommandEncoder { self.viewport_count = count; self.push_filled = false; } + let unit = 0.0..=1.0; + self.depth_past = (self.viewports[..count]) + .iter() + .any(|v| !unit.contains(&v.znear) || !unit.contains(&v.zfar)); + self.refused = self.draws_refused(); self.dirty |= VIEWPORT; } @@ -2837,8 +2881,9 @@ impl RenderCommandEncoder { _ => [None; 2], }; let patch_buffers = patch_reads.map(|read| read.map(|(buffer, _)| buffer)); - if !self.renders_into { - return gap(DrawKind::AttachmentFormats); + // Found when the state or the viewports were set (`draws_refused`). + if let Some(kind) = self.refused { + return gap(kind); } if self.tests.stencil && self.pass.stencil.is_none() { return Err(Error::Refused(Refusal::StencilAttachment)); @@ -4691,7 +4736,9 @@ fn slot(index: u32) -> Result { } /// The Vulkan viewport of `v`: its height negated, from `y + height`, so the draw follows Metal's -/// clip space; a viewport of no width or height takes one pixel, behind an empty scissor. +/// clip space; a viewport of no width or height takes one pixel, behind an empty scissor. Its +/// depths are cut to `[0, 1]`, as Vulkan takes them: a range past it is drawn through only where +/// nothing reads the depth (`RenderCommandEncoder::draws_refused`). pub(crate) fn vk_viewport(v: &Viewport) -> vk::Viewport { let width = if v.width == 0.0 { 1.0 } else { v.width }; let height = if v.height == 0.0 { 1.0 } else { v.height }; @@ -4700,8 +4747,8 @@ pub(crate) fn vk_viewport(v: &Viewport) -> vk::Viewport { y: (v.origin_y + height) as f32, width: width as f32, height: (-height) as f32, - min_depth: v.znear as f32, - max_depth: v.zfar as f32, + min_depth: v.znear.clamp(0.0, 1.0) as f32, + max_depth: v.zfar.clamp(0.0, 1.0) as f32, } } diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 75861c449..b7ae31f02 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -93,7 +93,7 @@ pub const ARTIFACT_FORMAT_VERSION: u32 = 609; /// changes no behaviour, and then updates that hash alone with the commit saying why, or changes /// behaviour and also bumps this version. The commit that bumps it says what changed; this comment /// keeps no history. -pub const RUNTIME_VERSION: u32 = 2570; +pub const RUNTIME_VERSION: u32 = 2571; /// Writes a digest as lowercase hex. pub(crate) fn write_hex(f: &mut fmt::Formatter<'_>, name: &str, hash: &Hash256) -> fmt::Result { diff --git a/crates/metal2vulkan/src/library/compiled.rs b/crates/metal2vulkan/src/library/compiled.rs index 2b06ec69e..f0d49045b 100644 --- a/crates/metal2vulkan/src/library/compiled.rs +++ b/crates/metal2vulkan/src/library/compiled.rs @@ -236,6 +236,16 @@ impl CompiledFunction { self.0.interface.writes_point_size() } + /// Whether the function is a fragment function that may read `[[position]]`'s z or w, read + /// from its stage's built-in inputs and the lanes its module reads of them: where a fragment + /// function observes the viewport's depth range, which Metal maps its position's z through + /// unclamped. `false` for any other function. + pub fn reads_position_depth(&self) -> bool { + let io = &self.0.interface.io; + io.position_depth + && (io.builtins).contains(&crate::pipeline::stage_io::StageBuiltin::FragCoord) + } + /// The samplers a pipeline state of the function numbers for it, in Metal's order /// ([`NumberedSampler`](crate::pipeline::NumberedSampler)): Metal's pipeline creation gives /// each a plain sampler resource ID unless an equal one of the device holds one. diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 017460b6d..410a3aad4 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -6468,6 +6468,7 @@ method metal2vulkan::library::CompiledFunction::module_stats pub fn module_stats method metal2vulkan::library::CompiledFunction::numbered_samplers pub fn numbered_samplers(&self) -> &[crate::pipeline::NumberedSampler] method metal2vulkan::library::CompiledFunction::observes_threadgroup_partition pub fn observes_threadgroup_partition(&self) -> bool method metal2vulkan::library::CompiledFunction::precision_notes pub fn precision_notes(&self) -> &[PrecisionNote] +method metal2vulkan::library::CompiledFunction::reads_position_depth pub fn reads_position_depth(&self) -> bool method metal2vulkan::library::CompiledFunction::reconvergence pub fn reconvergence(&self) -> Reconvergence method metal2vulkan::library::CompiledFunction::required_subgroup_size pub fn required_subgroup_size(&self) -> Option method metal2vulkan::library::CompiledFunction::requirements pub fn requirements(&self) -> &Requirements