diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index 6bc6a7e76..9484c7805 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -2869,6 +2869,12 @@ or does not check is rebuilt from the generations' names, with young counted ful 26 streams' libraries calls a SIMD-group, quad or SIMD-group matrix function (each library whose bytes name one disassembled with `llvm-dis`, jobs m3-max-52530, 52531, 52532, 52557 and 52604). Their libraries that call quad functions, 2 to 5 a stream, call them from no fragment function. +- On a device without `shaderResourceMinLod` (Min-LOD clamps, under Floor), a function that samples + with a `min_lod_clamp` other than 0 is refused at creation, an `Error::Translation` naming + `MinLodClamp` and `shaderResourceMinLod`. Of the venues dumped, the UHD 630's Windows driver + alone does not report it (`vulkaninfo`, job m3-max-48736); RADV on the RX 7900 XTX, the 780Ms + and the RX 550, NVIDIA's driver on the RTX 2080, RTX 3090 and RTX 5080 Laptop, ANV on the Iris + Xe, lavapipe and KosmicKrisp on the M3 Max report it (m3-max-48727 to 48731, 48737 and 61508). - On a device without `fragmentShaderBarycentric` (Barycentrics, under Floor), a fragment function that reads `[[barycentric_coord]]` is refused at creation, an `Error::Translation` naming `Barycentrics`: the translator's other form, weights a generated geometry stage writes, needs @@ -3597,6 +3603,17 @@ changes. |---|---|---| | `vertexAttributeInstanceRateDivisor` | `VkPhysicalDeviceVertexAttributeDivisorFeaturesKHR` | a vertex binding's divisor other than 1 | +### Min-LOD clamps + +A sample's `min_lod_clamp` other than 0 has one form, SPIR-V's `MinLod` image operand. Where the +device reports the feature below, device creation enables it, and the translator builds the +sample; elsewhere it refuses the function at creation, an `Error::Translation` naming +`MinLodClamp` and `shaderResourceMinLod` (Known gaps of render pipeline states). + +| Min-LOD feature | Structure | Used for | +|---|---|---| +| `shaderResourceMinLod` | `VkPhysicalDeviceFeatures` | a sample's `min_lod_clamp`, the device's own level-of-detail clamp | + ### Quad control A fragment function whose group operations read, combine or count other lanes has one form: its diff --git a/crates/metal2vulkan-runtime/src/floor.rs b/crates/metal2vulkan-runtime/src/floor.rs index 3691f828f..e972a79ef 100644 --- a/crates/metal2vulkan-runtime/src/floor.rs +++ b/crates/metal2vulkan-runtime/src/floor.rs @@ -526,6 +526,13 @@ pub(crate) const STEP_RATES: &[Feature] = &[feature!( divisor.vertex_attribute_instance_rate_divisor => VertexAttributeInstanceRateDivisor )]; +/// The feature device creation enables where the device reports it, for a sample's +/// `min_lod_clamp` other than 0, which the translator builds in one form, SPIR-V's `MinLod` image +/// operand, and refuses on a device without it (`Feature::MinLodClamp`). No answer chooses a +/// second form. +pub(crate) const MIN_LOD_CLAMPS: &[Feature] = + &[feature!(core.shader_resource_min_lod => ShaderResourceMinLod)]; + /// The feature of `VK_KHR_shader_quad_control` (one of [`OPTIONAL_EXTENSIONS`]), which device /// creation enables where the device lists the extension and reports it: a fragment function whose /// group operations read, combine or count other lanes runs its quads whole @@ -582,6 +589,7 @@ pub(crate) fn shader_enables(capabilities: &Capabilities) -> EnableSet { for feature in (REQUIRED.iter().chain(OPTIONAL)) .chain(PORTABILITY) .chain(STEP_RATES) + .chain(MIN_LOD_CLAMPS) .chain(QUAD_CONTROL) .chain(PIXEL_INTERLOCK) .chain(BARYCENTRICS) @@ -1047,6 +1055,7 @@ pub(crate) fn decide(device: &Queried) -> Result { return Err(BelowFloor { missing }); }; for feature in (OPTIONAL.iter().chain(STEP_RATES)) + .chain(MIN_LOD_CLAMPS) .chain(QUAD_CONTROL) .chain(PIXEL_INTERLOCK) { diff --git a/crates/metal2vulkan-runtime/src/tests/floor.rs b/crates/metal2vulkan-runtime/src/tests/floor.rs index b7e6d564a..04a420be5 100644 --- a/crates/metal2vulkan-runtime/src/tests/floor.rs +++ b/crates/metal2vulkan-runtime/src/tests/floor.rs @@ -6,7 +6,7 @@ use crate::floor::{ choose, decide, flag_names, limits, vulkan_name, BelowFloor, Features, MemoryTypes, Missing, Queried, ATTACHMENT_READS, ATTACHMENT_READ_EXTENSIONS, BARYCENTRICS, BARYCENTRIC_EXTENSIONS, BORDER_SWIZZLE, BORDER_SWIZZLE_EXTENSIONS, DEVICE_EXTENSIONS, INSTANCE_EXTENSIONS, - LINEAR_ATTACHMENTS, MEMORY_FLAG_NAMES, OPTIONAL, OPTIONAL_EXTENSIONS, + LINEAR_ATTACHMENTS, MEMORY_FLAG_NAMES, MIN_LOD_CLAMPS, OPTIONAL, OPTIONAL_EXTENSIONS, OPTIONAL_INSTANCE_EXTENSIONS, PIXEL_INTERLOCK, PORTABILITY, PRIVATE_MEMORY, QUAD_CONTROL, REQUIRED, RING_MEMORY, SHARED_MEMORY, STEP_RATES, TIMESTAMP_BITS, TIME_DOMAINS, VERSION, }; @@ -331,6 +331,7 @@ fn a_device_at_the_floor_is_above_it() { ); } for optional in (OPTIONAL.iter().chain(STEP_RATES)) + .chain(MIN_LOD_CLAMPS) .chain(QUAD_CONTROL) .chain(PIXEL_INTERLOCK) .chain(BARYCENTRICS) @@ -1191,6 +1192,27 @@ fn the_pixel_interlock_is_enabled_where_reported() { .contains(&VkExtension::ExtFragmentShaderInterlock)); } +/// A device that reports `shaderResourceMinLod` is given it, and the translator sees it; one that +/// does not is given neither. +#[test] +fn the_min_lod_clamp_is_enabled_where_reported() { + use crate::floor::shader_enables; + use metal2vulkan::device::VkFeature; + let mut device = at_the_floor(); + let unreported = decide(&device).unwrap_or_else(|below| panic!("{below}")); + assert_eq!(unreported.features.core.shader_resource_min_lod, vk::FALSE); + assert!(!shader_enables(&unreported) + .features + .contains(&VkFeature::ShaderResourceMinLod)); + + device.features.core.shader_resource_min_lod = vk::TRUE; + let capabilities = decide(&device).unwrap_or_else(|below| panic!("{below}")); + assert_eq!(capabilities.features.core.shader_resource_min_lod, vk::TRUE); + assert!(shader_enables(&capabilities) + .features + .contains(&VkFeature::ShaderResourceMinLod)); +} + /// A device that lists `VK_KHR_shader_quad_control` and reports `shaderQuadControl` is given both, /// and the translator sees both; one that lists the extension without the feature is given the /// extension alone. @@ -1589,6 +1611,7 @@ fn the_readme_states_the_floor() { ATTACHMENT_READS, ), (["Step-rate feature", "Structure", "Used for"], STEP_RATES), + (["Min-LOD feature", "Structure", "Used for"], MIN_LOD_CLAMPS), ( ["Quad-control feature", "Structure", "Used for"], QUAD_CONTROL, diff --git a/crates/metal2vulkan-runtime/src/tests/min_lod.rs b/crates/metal2vulkan-runtime/src/tests/min_lod.rs new file mode 100644 index 000000000..2e976a432 --- /dev/null +++ b/crates/metal2vulkan-runtime/src/tests/min_lod.rs @@ -0,0 +1,149 @@ +//! A fragment function that samples with a `min_lod_clamp` (`floor`'s `MIN_LOD_CLAMPS`), under a +//! device cfg on every device of the cfg's kind: where device creation enabled +//! `shaderResourceMinLod`, the translator's profile has it, the state is made and each drawn pixel +//! reads the levels the clamp selects; elsewhere creation refuses the state, naming `MinLodClamp`. +//! +//! `sample_min_lod` (engine `sampling`) samples a 2D texture through the bound sampler at a +//! sixteenth of the pixel's position, clamped to level of detail 1.5. Over a texture 16 texels a +//! side the position's derivatives make the level of detail 0, so the clamp alone moves it: under a +//! linear mip filter, every pixel reads level 1 and level 2 half and half, where an unclamped +//! sample reads level 0. +//! +//! `llvm-as` assembles the functions; without it the test skips visibly. + +#[cfg(any(m2v_gpu, m2v_cpu_icd))] +mod on_device { + use crate::tests::command::on_device::each_device; + use crate::tests::draw::on_device::{ + descriptor, examples, floats, library, pass, read, run, target, Buffers, SIDE, + }; + use crate::{ + Error, FunctionDescriptor, HostBytes, IndexClass, PixelFormat, PrimitiveType, Region, + SamplerDescriptor, SamplerMipFilter, StageSource, StorageMode, TextureDescriptor, + TextureType, TextureUsage, + }; + use metal2vulkan::device::VkFeature; + use metal2vulkan::error::Feature; + use metal2vulkan_devtools::engine_dir; + + /// The sampled texture's side, and its levels (16 texels down to 1). + const TEXELS: u32 = 16; + const LEVELS: u32 = 5; + /// Each level's one colour: level 0 red, 1 green, 2 blue, then white. + const COLOURS: [[u8; 4]; LEVELS as usize] = [ + [255, 0, 0, 255], + [0, 255, 0, 255], + [0, 0, 255, 255], + [255, 255, 255, 255], + [255, 255, 255, 255], + ]; + + /// Where Vulkan offers `shaderResourceMinLod`, a sample's `min_lod_clamp` raises its level of + /// detail as Metal's does; elsewhere the state is refused naming the clamp. + #[test] + fn every_device_clamps_a_sample_s_level_where_it_offers_min_lod() { + let (Some(examples), Some(sampling)) = + (examples(), library(&engine_dir().join("sampling.ll"))) + else { + return; + }; + let mut d = descriptor(&examples, "colored_fragment", PixelFormat::RGBA8Unorm); + d.fragment = Some(StageSource::Function(FunctionDescriptor::new( + sampling.function("sample_min_lod").expect("the function"), + ))); + each_device("min_lod", |name, _, device| { + let core = device.core(); + let on = (core.translator.profile().enabled().features) + .contains(&VkFeature::ShaderResourceMinLod); + let state = + match device.new_render_pipeline_state(&d, crate::tests::no_constexpr_samplers) { + Ok(state) => state, + Err(Error::Translation(metal2vulkan::error::Error::Unsupported(u))) if !on => { + assert_eq!(u.feature, Feature::MinLodClamp, "{name}: {u:?}"); + println!("min_lod: {name}: no shaderResourceMinLod; the state is refused"); + return; + } + Err(error) => panic!("{name}: {error}"), + }; + assert!(on, "{name}: a state made without shaderResourceMinLod"); + let _grants = [IndexClass::Texture, IndexClass::Sampler].map(|class| { + (device.grant_indices(class, 1, 1)) + .unwrap_or_else(|error| panic!("{name}: {error}")) + }); + let mut td = TextureDescriptor::new( + TextureType::Type2D, + PixelFormat::RGBA8Unorm, + TEXELS, + TEXELS, + ); + td.mipmap_level_count = LEVELS; + td.usage = TextureUsage::SHADER_READ; + td.storage_mode = StorageMode::Shared; + let texture = (device.new_texture(&td, Some(1))) + .unwrap_or_else(|error| panic!("{name}: {error}")); + for (level, colour) in (0..LEVELS).zip(COLOURS) { + let side = TEXELS >> level; + let bytes = colour.repeat((side * side) as usize); + let whole = Region { + origin: [0; 3], + size: [side, side, 1], + }; + let row = u64::from(side) * 4; + (texture.replace_region( + whole, + level, + 0, + HostBytes::from(&bytes[..]), + row, + row * u64::from(side), + )) + .unwrap_or_else(|error| panic!("{name}: level {level}: {error}")); + } + let mut sd = SamplerDescriptor::new(); + sd.mip_filter = SamplerMipFilter::Linear; + let sampler = (device.new_sampler_state(&sd, 1)) + .unwrap_or_else(|error| panic!("{name}: {error}")); + let mut buffers = Buffers::new(&device); + // `colored_vertex`'s attributes: a triangle that covers the target, and its colours. + let positions = buffers.of(&device, &floats(&[-1.0, -1.0, 3.0, -1.0, -1.0, 3.0])); + let colors = buffers.of(&device, &floats(&[0.0; 12])); + let color = target( + &device, + PixelFormat::RGBA8Unorm, + TextureUsage::RENDER_TARGET, + ); + let queue = device.new_command_queue(4).expect("a queue"); + let command_buffer = queue.command_buffer().expect("a command buffer"); + let ok = |result: Result<(), Error>| { + result.unwrap_or_else(|error| panic!("{name}: {error}")) + }; + let mut encoder = (command_buffer.render_command_encoder(&pass(&color, None))) + .unwrap_or_else(|error| panic!("{name}: {error}")); + ok(encoder.set_render_pipeline_state(&state)); + ok(encoder.set_vertex_buffer(Some(&positions), 0, 0)); + ok(encoder.set_vertex_buffer(Some(&colors), 0, 1)); + ok(encoder.set_fragment_texture(Some(&texture), 0)); + ok(encoder.set_fragment_sampler_state(Some(&sampler), 0)); + ok(encoder.draw_primitives(PrimitiveType::Triangle, 0, 3, 1, 0)); + encoder.end_encoding(); + run(name, &command_buffer); + // Level 1 and level 2 half and half: green and blue each 127.5, within the device's + // filtering of two codes. + let texels = read(&color, 4); + for (at, texel) in texels.as_chunks::<4>().0.iter().enumerate() { + let [r, g, b, a] = *texel; + let near = |code: u8| code.abs_diff(128) <= 2; + assert!( + r == 0 && near(g) && near(b) && a == 255, + "{name}: pixel {at}: {texel:?}, where levels 1 and 2 half and half are \ + [0, 128, 128, 255]" + ); + } + println!( + "min_lod: {name}: shaderResourceMinLod enabled; {} pixels read levels 1 and 2 \ + half and half under a clamp of 1.5", + SIDE * SIDE + ); + }); + } +} diff --git a/crates/metal2vulkan-runtime/src/tests/mod.rs b/crates/metal2vulkan-runtime/src/tests/mod.rs index 82d51003d..74342a16e 100644 --- a/crates/metal2vulkan-runtime/src/tests/mod.rs +++ b/crates/metal2vulkan-runtime/src/tests/mod.rs @@ -52,6 +52,7 @@ mod lowered; mod maker_yield; mod managed; mod memory; +mod min_lod; mod mipmaps; mod narrow; mod nil_slots;