diff --git a/crates/metal2vulkan-validation/src/candidate/engine.rs b/crates/metal2vulkan-validation/src/candidate/engine.rs index bccd048b6..367d06c97 100644 --- a/crates/metal2vulkan-validation/src/candidate/engine.rs +++ b/crates/metal2vulkan-validation/src/candidate/engine.rs @@ -65,7 +65,7 @@ use crate::gpu::wait::wait_timeout; use crate::gpu::DeviceType; use crate::hash::sha256_bytes; use crate::library_module::ResolvedLinkedFunctions; -use crate::literal::{LiteralResources, LiteralTexture}; +use crate::literal::{LiteralDepthStencil, LiteralResources, LiteralTexture}; use crate::metal::ORACLE_ABI; use crate::observation::{ Backend, CandidateDependencies, CandidateObservation, CandidateStatus, CaseTiming, @@ -123,7 +123,7 @@ use tables::{function_tables, table_buffers, FunctionTable, TableBuffers}; /// how it decides a match. A change no recorded row can observe (a case kind no earlier executor /// ran, or one it refused) needs no bump; its commit says why. The commit that bumps it says what /// changed; this comment keeps no history. -pub const ENGINE_EXECUTOR_ABI: &str = "vulkan-engine-v194"; +pub const ENGINE_EXECUTOR_ABI: &str = "vulkan-engine-v195"; /// One kernel case in the terms the binder takes: what this executor supports of an /// [`AuthoredCase`] (buffers, device buffer arrays, argument-buffer buffers and textures, command @@ -1395,10 +1395,11 @@ pub struct RuntimeKernel<'a> { /// (`platform::execute_runtime_render`) in place of the executor's own context: a vertex or /// fragment case of one sample, its own stage's buffers, bound textures the runtime makes /// (`runtime_texture`) and bound samplers, function constants and linked functions, its vertex -/// buffers at static strides, its color targets (a fragment case renders into at least one: the -/// runtime makes no pass without an attachment, `Gap::RenderPass`), the encoder's depth-stencil -/// state, cull mode and winding, and one direct or indexed draw of primitives, observing a buffer, -/// a texture, a color target or nothing, whose out-of-bounds targets, if any, are attribute +/// buffers at static strides, its color targets and a depth/stencil attachment of one sample and +/// one aspect (a fragment case renders into at least one attachment: the runtime makes no pass +/// without one, `Gap::RenderPass`), the encoder's depth-stencil state, cull mode and winding, and +/// one direct or indexed draw of primitives, observing a buffer, a texture, a color target, the +/// depth/stencil attachment's aspect or nothing, whose out-of-bounds targets, if any, are attribute /// buffers (`runtime_bounds::drawn` reads their facts). Decided by the case's structure alone: /// every field the mapping fills is named here, so a new one is decided when it is added. pub fn runs_render_through_runtime(authored: &AuthoredCase, case: &graphics::RenderCase) -> bool { @@ -1441,12 +1442,17 @@ pub fn runs_render_through_runtime(authored: &AuthoredCase, case: &graphics::Ren && textures.values().all(runtime_texture) && function_tables.is_empty() }; + // The runtime renders into no separate depth and stencil textures in one pass + // (`Gap::RenderPass` of `DepthStencilViews`), which the oracle makes of an attachment of both. + let one_aspect = |attachment: &LiteralDepthStencil| { + attachment.samples == 1 && (attachment.depth.is_none() || attachment.stencil.is_none()) + }; bound(vertex) && fragment.as_ref().is_none_or(bound) - && (fragment.is_none() || !color_targets.is_empty()) + && (fragment.is_none() || !color_targets.is_empty() || depth_stencil.is_some()) && vertex_buffers.iter().all(|buffer| !buffer.dynamic) && color_targets.values().all(|target| target.samples == 1) - && depth_stencil.is_none() + && depth_stencil.as_ref().is_none_or(one_aspect) && *render_target_array_length == 0 && matches!(assembly, graphics::Assembly::Primitives(_)) && matches!(draw, DrawCall::Primitives { .. } | DrawCall::Indexed { .. }) @@ -1460,6 +1466,8 @@ pub fn runs_render_through_runtime(authored: &AuthoredCase, case: &graphics::Ren | graphics::RenderOutput::Buffer { .. } | graphics::RenderOutput::Texture { .. } | graphics::RenderOutput::RenderTarget { .. } + | graphics::RenderOutput::Depth { .. } + | graphics::RenderOutput::Stencil { .. } ) && rate_map_pass.is_none() && raster_sample_count.is_none() diff --git a/crates/metal2vulkan-validation/src/candidate/engine/tests.rs b/crates/metal2vulkan-validation/src/candidate/engine/tests.rs index dfeec9ecc..bab92ead1 100644 --- a/crates/metal2vulkan-validation/src/candidate/engine/tests.rs +++ b/crates/metal2vulkan-validation/src/candidate/engine/tests.rs @@ -2704,11 +2704,12 @@ fn a_plain_kernel_case_and_only_one_runs_through_the_runtime() { /// The runtime draws a vertex or fragment case of one sample, its own stage's buffers, textures /// and samplers, static vertex strides, color targets, and one direct or indexed draw of /// primitives, observing a buffer, a texture, a color target or nothing; and a -/// rasterization-disabled vertex case, which renders into the oracle's sink; and an attribute -/// buffer's out-of-bounds target. A case with anything else (a depth/stencil attachment, more -/// samples, layered rendering, a dynamic vertex stride, an argument-buffer member, an explicit -/// imageblock, a depth observation, a fragment pass with no color target, which the runtime makes -/// no pass of, or an index buffer's out-of-bounds target) stays on the executor's own context. +/// rasterization-disabled vertex case, which renders into the oracle's sink; a depth/stencil +/// attachment of one sample and one aspect, observed or not; and an attribute buffer's +/// out-of-bounds target. A case with anything else (a depth/stencil attachment of both aspects or +/// more samples, more samples, layered rendering, a dynamic vertex stride, an argument-buffer +/// member, an explicit imageblock, a fragment pass with no attachment, which the runtime makes no +/// pass of, or an index buffer's out-of-bounds target) stays on the executor's own context. #[test] fn a_plain_draw_and_only_one_runs_through_the_runtime() { use super::graphics::tests::two_stage_case; @@ -2793,13 +2794,54 @@ fn a_plain_draw_and_only_one_runs_through_the_runtime() { .argument_buffer_buffers .insert((0, 0), vec![0; 16]); } + // A depth/stencil attachment of one sample and one aspect, observed or not, with or without a + // color target; one of both aspects or of more samples stays on the executor's own context. + let depth = LiteralDepthStencil { + dimensions: [2, 2], + samples: 1, + depth: Some(vec![0; 16]), + stencil: None, + }; + let stencil = LiteralDepthStencil { + depth: None, + stencil: Some(vec![0; 4]), + ..depth.clone() + }; + for attachment in [&depth, &stencil] { + let attached = RenderCase { + depth_stencil: Some(attachment.clone()), + ..plain.clone() + }; + assert!(runs_render_through_runtime(&authored, &attached)); + let observed = RenderCase { + color_targets: BTreeMap::new(), + output: match attachment.depth { + Some(_) => RenderOutput::Depth { + origin: [0, 0], + dimensions: [1, 1], + }, + None => RenderOutput::Stencil { + origin: [0, 0], + dimensions: [1, 1], + }, + }, + ..attached + }; + assert!(runs_render_through_runtime(&authored, &observed)); + } let others = [ RenderCase { depth_stencil: Some(LiteralDepthStencil { - dimensions: [2, 2], - samples: 1, - depth: Some(vec![0; 16]), - stencil: None, + stencil: Some(vec![0; 4]), + ..depth.clone() + }), + ..plain.clone() + }, + RenderCase { + depth_stencil: Some(LiteralDepthStencil { + samples: 4, + depth: Some(vec![0; 64]), + ..depth.clone() }), ..plain.clone() }, @@ -2832,13 +2874,6 @@ fn a_plain_draw_and_only_one_runs_through_the_runtime() { }), ..plain.clone() }, - RenderCase { - output: RenderOutput::Depth { - origin: [0, 0], - dimensions: [1, 1], - }, - ..plain.clone() - }, RenderCase { color_targets: BTreeMap::new(), output: RenderOutput::None, diff --git a/crates/metal2vulkan-validation/src/candidate/platform/runtime_render.rs b/crates/metal2vulkan-validation/src/candidate/platform/runtime_render.rs index fd8694149..37322144a 100644 --- a/crates/metal2vulkan-validation/src/candidate/platform/runtime_render.rs +++ b/crates/metal2vulkan-validation/src/candidate/platform/runtime_render.rs @@ -9,7 +9,10 @@ //! buffer; each bound texture and sampler as the Metal oracle makes them (`runtime_textures`); each //! color attachment a Shared 2D texture of its format and extent, written from the case's bytes //! and loaded and stored by the pass, and the oracle's 1x1 `R8Unorm` attachment where -//! rasterization is disabled, which no draw writes; the oracle's depth-stencil state, stencil +//! rasterization is disabled, which no draw writes; the depth/stencil attachment's one aspect as +//! the oracle makes it, a Shared `Depth32Float` filled by a draw of the case's depths +//! (`runtime_depth`, as the runtime takes no upload of one) or a Shared `Stencil8` written from its +//! bytes, loaded and stored by the pass; the oracle's depth-stencil state, stencil //! references, cull mode and winding; one draw, direct or indexed, in one command buffer, waited //! for under the hang protocol, then read back. //! @@ -19,6 +22,7 @@ use super::runtime::{self, runtime_error, Identity, Placed}; use super::runtime_bounds; +use super::runtime_depth; use super::runtime_textures::{self, Bound, Indices, Wanted}; use crate::candidate::engine::graphics::{ self, Assembly, ColorTarget, RenderCase, RenderFinals, RenderOutput, @@ -28,6 +32,7 @@ use crate::case::Stage; use crate::check::admission::{ExecutorError, NotSupported}; use crate::coverage::ModuleCensus; use crate::gpu::DeviceType; +use crate::literal::LiteralDepthStencil; use metal2vulkan::bind::DrawCall; use metal2vulkan::library::Library; use metal2vulkan::pipeline::{ @@ -112,7 +117,7 @@ fn run( .as_deref() .map(|bytes| Placed::new(device, "the index buffer", bytes)) .transpose()?; - let targets = Targets::make(device, case, &mut indices)?; + let targets = Targets::make(device, device_type, case, &mut indices)?; let depth_stencil = device.new_depth_stencil_state(&case.depth_stencil_state.descriptor); let label = crate::gpu::wait::SubmissionLabel { case_id: case_id.into(), @@ -176,6 +181,7 @@ fn run( let finals = RenderFinals { buffers: stages.finals(), targets: targets.finals(case)?, + depth_stencil: targets.depth_stencil_finals()?, ..RenderFinals::default() }; graphics::select_render_output(output, &finals).map_err(ExecutorError::Failed)? @@ -365,12 +371,23 @@ impl Stages { } } -/// The pass's color attachments, by Metal index. +/// The pass's color attachments, by Metal index, and its depth/stencil attachment's aspect. struct Targets { textures: BTreeMap, + depth_stencil: Option, _grant: Option, } +/// A depth/stencil attachment of one sample and one aspect, as the oracle makes it (Metal's +/// separate `Depth32Float` and `Stencil8`): the runtime renders into no separate depth and stencil +/// textures in one pass (`Gap::RenderPass` of `DepthStencilViews`), so a case of both stays on the +/// executor's own context (`engine::runs_render_through_runtime`). +struct DepthStencil { + texture: rt::Texture, + depth: bool, + dimensions: [u32; 2], +} + impl Targets { /// Each color target of `case` as a Shared 2D texture of its format and extent, made for /// rendering at the next of `indices` and written from its bytes; a rasterization-disabled @@ -378,6 +395,7 @@ impl Targets { /// (a case of that draw authors no color target, `RasterizationDisabledRenderTarget`). fn make( device: &rt::Device, + device_type: DeviceType, case: &RenderCase, indices: &mut Indices, ) -> Result { @@ -387,15 +405,29 @@ impl Targets { let sink = attachment(device, SINK, [1, 1], (0, at.start))?; return Ok(Targets { textures: BTreeMap::from([(0, sink)]), + depth_stencil: None, _grant: grant, }); } - let (grant, at) = runtime_textures::texture_indices( + let aspects = usize::from(case.depth_stencil.is_some()); + let (grant, mut at) = runtime_textures::texture_indices( device, indices, - case.color_targets.len(), + case.color_targets.len() + aspects, &runtime_error, )?; + let depth_stencil = case + .depth_stencil + .as_ref() + .map(|authored| { + let index = at.next().ok_or_else(|| { + ExecutorError::Failed( + "no texture index for the depth/stencil attachment".into(), + ) + })?; + DepthStencil::make(device, device_type, authored, index) + }) + .transpose()?; let mut textures = BTreeMap::new(); for ((&index, target), at) in case.color_targets.iter().zip(at) { let format = resources::pixel_format(target.format); @@ -416,6 +448,7 @@ impl Targets { } Ok(Targets { textures, + depth_stencil, _grant: grant, }) } @@ -429,9 +462,55 @@ impl Targets { attachment.load_action = rt::LoadAction::Load; attachment.store_action = rt::StoreAction::Store; } + match &self.depth_stencil { + Some(made) if made.depth => { + let attachment = &mut pass.depth_attachment; + attachment.texture = Some(made.texture.clone()); + attachment.load_action = rt::LoadAction::Load; + attachment.store_action = rt::StoreAction::Store; + } + Some(made) => { + let attachment = &mut pass.stencil_attachment; + attachment.texture = Some(made.texture.clone()); + attachment.load_action = rt::LoadAction::Load; + attachment.store_action = rt::StoreAction::Store; + } + None => {} + } pass } + /// The depth/stencil attachment's aspect with its bytes now, where the pass has one. + fn depth_stencil_finals(&self) -> Result, ExecutorError> { + let Some(made) = &self.depth_stencil else { + return Ok(None); + }; + let [width, height] = made.dimensions; + let row = width as usize * if made.depth { 4 } else { 1 }; + let image = row * height as usize; + let mut bytes = vec![0u8; image]; + made.texture + .get_bytes( + &mut bytes, + row as u64, + image as u64, + whole([width, height]), + 0, + 0, + ) + .map_err(|error| runtime_error("the depth/stencil attachment's read-back", error))?; + let (depth, stencil) = match made.depth { + true => (Some(bytes), None), + false => (None, Some(bytes)), + }; + Ok(Some(LiteralDepthStencil { + dimensions: made.dimensions, + samples: 1, + depth, + stencil, + })) + } + /// Each authored color target with its bytes now, by Metal index. fn finals(&self, case: &RenderCase) -> Result, ExecutorError> { case.color_targets @@ -468,6 +547,75 @@ impl Targets { } } +impl DepthStencil { + /// `authored`'s one aspect at texture index `at` of the run's grant: a `Depth32Float` filled + /// with its depths by a draw (`runtime_depth`), or a `Stencil8` written from its bytes. + fn make( + device: &rt::Device, + device_type: DeviceType, + authored: &LiteralDepthStencil, + at: u32, + ) -> Result { + let [width, height] = authored.dimensions; + let what = "the depth/stencil attachment"; + match (&authored.depth, &authored.stencil) { + (Some(depths), None) => { + let texture = aspect(device, rt::PixelFormat::Depth32Float, [width, height], at)?; + let fill = runtime_depth::Fill { + texture: texture.clone(), + level: 0, + slice: 0, + width, + depths: depths.clone(), + }; + runtime_depth::filled(device, device_type, &[fill], &runtime_error)?; + Ok(DepthStencil { + texture, + depth: true, + dimensions: authored.dimensions, + }) + } + (None, Some(stencil)) => { + let texture = aspect(device, rt::PixelFormat::Stencil8, [width, height], at)?; + texture + .replace_region( + whole([width, height]), + 0, + 0, + stencil.as_slice().into(), + u64::from(width), + u64::from(width) * u64::from(height), + ) + .map_err(|error| runtime_error(what, error))?; + Ok(DepthStencil { + texture, + depth: false, + dimensions: authored.dimensions, + }) + } + _ => Err(ExecutorError::Failed(format!( + "{what} through the runtime holds one aspect" + ))), + } + } +} + +/// A Shared 2D depth or stencil attachment of `format` and `extent` at texture index `at` of the +/// run's grant. +fn aspect( + device: &rt::Device, + format: rt::PixelFormat, + [width, height]: [u32; 2], + at: u32, +) -> Result { + let mut descriptor = rt::TextureDescriptor::new(rt::TextureType::Type2D, format, width, height); + descriptor.usage = rt::TextureUsage::RENDER_TARGET; + descriptor.storage_mode = rt::StorageMode::Shared; + device + .new_texture(&descriptor, Some(at)) + .map_err(|error| runtime_error("the depth/stencil attachment", error)) +} + /// A Shared 2D color attachment of `format` and `extent`, color attachment `index`'s, at texture /// index `at` of the run's grant. fn attachment( diff --git a/crates/metal2vulkan-validation/src/candidate/tests/engine_render.rs b/crates/metal2vulkan-validation/src/candidate/tests/engine_render.rs index a70b4788e..5f855b68f 100644 --- a/crates/metal2vulkan-validation/src/candidate/tests/engine_render.rs +++ b/crates/metal2vulkan-validation/src/candidate/tests/engine_render.rs @@ -285,7 +285,7 @@ fn a_fragment_case_draws_through_its_generated_passthrough_vertex() { /// A fragment case with a depth/stencil attachment: `fragment_depth_stencil` writes depth 0.5 under /// `depth(any)` and stencil 7, and the oracle's depth-stencil state (always-pass depth test with /// writes, always-pass stencil test that replaces) stores both over the attachment's authored 1.0 -/// and 0xab, as the oracle does for case 685a8d84. +/// and 0xab, as the oracle does for case 685a8d84; through the runtime, each aspect alone. #[cfg(any(target_os = "linux", target_os = "macos"))] #[test] fn a_fragment_case_writes_its_depth_and_stencil_attachment() { @@ -334,6 +334,43 @@ fn a_fragment_case_writes_its_depth_and_stencil_attachment() { Some(metal2vulkan::bind::StatusReport::default()) ); } + // Through the runtime, each aspect alone: an attachment of both is the oracle's two + // textures, which the runtime renders into in no one pass. + #[cfg(target_os = "linux")] + { + for (depth, output, expected) in [ + ( + true, + OutputSelection::Depth { + origin: [0, 0], + dimensions: [1, 1], + }, + 0.5f32.to_le_bytes().to_vec(), + ), + ( + false, + OutputSelection::Stencil { + origin: [0, 0], + dimensions: [1, 1], + }, + vec![7], + ), + ] { + authored.depth_stencil = Some(crate::case::DepthStencilResource { + dimensions: [1, 1], + initial_depth_b64: depth.then(|| b64(&1.0f32.to_le_bytes())), + initial_stencil_b64: (!depth).then(|| b64(&[0xab])), + state: None, + }); + authored.output = output; + let (case, companion) = with_companion(&authored, &fragment); + assert!(crate::candidate::engine::runs_render_through_runtime( + &authored, &case + )); + let through = run_on_runtime(&case, &fragment, Some(&companion)).unwrap(); + assert_eq!(through.output, expected, "{:?}", authored.output); + } + } } /// A vertex case observed through a varying and through its position, each drawn into its diff --git a/docs/VALIDATION.md b/docs/VALIDATION.md index a949dec93..e018db67f 100644 --- a/docs/VALIDATION.md +++ b/docs/VALIDATION.md @@ -484,17 +484,22 @@ cargo run -p metal2vulkan-validation --release --bin corpus-moltenvk -- \ sample draws through the runtime the same way (`engine::runs_render_through_runtime`): its own stage's buffers, bound textures and samplers, function constants and linked functions, its generated companion, its vertex buffers at static strides, its color targets (Shared 2D - textures, loaded and stored by the pass), the oracle's depth-stencil state, cull mode and - winding, and one direct or indexed draw of primitives, observing a buffer, a texture, a color - target or nothing, its out-of-bounds targets, if any, attribute buffers. Their facts are read + textures, loaded and stored by the pass), a depth/stencil attachment of one aspect as the oracle + makes it (a Shared `Depth32Float` filled by a draw of its depths, as a 32-bit float depth texture + is, or a Shared `Stencil8` written from its bytes, loaded and stored by the pass), the oracle's + depth-stencil state, cull mode and winding, and one direct or indexed draw of primitives, + observing a buffer, a texture, a color target, the attachment's aspect or nothing, its + out-of-bounds targets, if any, attribute buffers. Their facts are read before the state is made, from the translator's compile of the state's descriptor on the runtime device's own translator: the runtime's floor enables no buffer robustness, so a fixed-function vertex fetch past its buffer is checked nowhere and the case is refused as `not_supported{uncontained:unchecked vertex_input}`. A rasterization-disabled vertex case renders - into the oracle's 1x1 `R8Unorm` attachment. A depth/stencil attachment, more samples, layered rendering, a dynamic vertex - stride, an argument-buffer member, tessellation, an imageblock, a rasterization-rate map, an - out-of-bounds target other than an attribute buffer or a fragment pass with no color target (the - runtime makes no pass without an attachment) keeps the case on the executor's own context. An + into the oracle's 1x1 `R8Unorm` attachment. A depth/stencil attachment of both aspects (the + oracle's two textures, which the runtime renders into in no one pass: `Gap::RenderPass` of + `DepthStencilViews`), more samples, layered rendering, a dynamic vertex stride, an + argument-buffer member, tessellation, an imageblock, a rasterization-rate map, an out-of-bounds + target other than an attribute buffer or a fragment pass with no attachment (the runtime makes + no pass without one) keeps the case on the executor's own context. An indexed draw past its index buffer, which the runtime refuses as Metal's validation does (`Refusal::IndexBufferOffset`), carries `index_buffer_past_end`, the runtime's limit, and runs on Metal alone. Its device worker enables the