diff --git a/crates/metal2vulkan-rt/src/pipeline.rs b/crates/metal2vulkan-rt/src/pipeline.rs index 0a390fd47..cd8be5f5d 100644 --- a/crates/metal2vulkan-rt/src/pipeline.rs +++ b/crates/metal2vulkan-rt/src/pipeline.rs @@ -180,26 +180,43 @@ pub(crate) fn number_samplers( let NumberedSampler::Constexpr(words) = *sampler else { continue; }; - let mut held = [None; 3]; - for (_, sampler) in uses.iter().filter(|(place, _)| *place == at) { - let desc = sampler - .constexpr_sampler(profile) - .map_err(Error::Translation)? - .ok_or_else(gap)?; - held[sampler_array(sampler)] = Some(canonical(desc)); + // Any refusal gives back the elements the constexpr samplers before this one took. + let held = constexpr_uses(profile, uses, at, gap).and_then(|held| { + if held.iter().all(Option::is_none) { + return Ok(false); + } + hold_constexpr(core, numbered.ids[at], words, held).map(|()| true) + }); + match held { + Ok(true) => numbered.held.push(at), + Ok(false) => {} + Err(error) => { + numbered.release(core); + return Err(error); + } } - if held.iter().all(Option::is_none) { - continue; - } - if let Err(error) = hold_constexpr(core, numbered.ids[at], words, held) { - numbered.release(core); - return Err(error); - } - numbered.held.push(at); } Ok(numbered) } +/// The device sampler of each sampler array the uses of `uses` at place `at` sample through. +fn constexpr_uses( + profile: &metal2vulkan::device::DeviceProfile, + uses: &[(usize, CombinedSampler)], + at: usize, + gap: fn() -> Error, +) -> Result<[Option; 3], Error> { + let mut held = [None; 3]; + for (_, sampler) in uses.iter().filter(|(place, _)| *place == at) { + let desc = sampler + .constexpr_sampler(profile) + .map_err(Error::Translation)? + .ok_or_else(gap)?; + held[sampler_array(sampler)] = Some(canonical(desc)); + } + Ok(held) +} + impl Numbered { /// Gives up the constexpr samplers' elements the state holds. pub(crate) fn release(&mut self, core: &Core) { diff --git a/crates/metal2vulkan-rt/src/tests/sampler.rs b/crates/metal2vulkan-rt/src/tests/sampler.rs index d6a99b0d9..1471df4d1 100644 --- a/crates/metal2vulkan-rt/src/tests/sampler.rs +++ b/crates/metal2vulkan-rt/src/tests/sampler.rs @@ -10,7 +10,8 @@ //! an anisotropic state is made. A cubic filter is `Gap::CubicSampler`. A state made at an index //! another holds takes it, and the earlier state's going leaves it; the grant waits for the //! index's owner, and the index is free once that state is gone. The device's samplers come -//! back to zero with the device. +//! back to zero with the device. A state's constexpr samplers numbered up to one the +//! translator refuses give back what the earlier ones took. use crate::error::{Error, Limit}; use crate::sampler::SamplerTable; @@ -151,4 +152,63 @@ mod on_device { ); } } + + /// A state's constexpr samplers, numbered up to one whose device sampler the translator does + /// not state: the refusal gives back what the earlier one took, its arrays' elements at once + /// and its device sampler through the reaper, and leaves its index free. + #[test] + fn a_refused_constexpr_sampler_gives_back_the_ones_numbered_before_it() { + use metal2vulkan::pipeline::{CombinedSampler, NumberedSampler, SamplerRef}; + // Linear on both filters, clamped to the edge on every axis, levels 0 to 65504: no + // default sampler shares its create info. + let linear = [ + 1 | (1 << 3) | (1 << 6) | (1 << 9) | (1 << 11) | (0x7bff << 40), + 0, + ]; + // The same state with a NaN level bias, of which the translator makes no device sampler. + let nan_bias = [linear[0], 0x7e00]; + let instance = crate::tests::validation::instance(&[]); + let devices = devices_of_the_cfgs_kind(&instance); + assert!( + !devices.is_empty(), + "the loader lists no device of this cfg's kind" + ); + for (name, physical) in devices { + let device = Device::new(&physical, DeviceOptions::default()) + .unwrap_or_else(|error| panic!("{name}: {error}")); + let counters = device.counters(); + let grant = device + .grant_indices(IndexClass::Sampler, 1, 2) + .unwrap_or_else(|error| panic!("{name}: {error}")); + let before = counters.gauges(); + let samplers = vec![ + NumberedSampler::Constexpr(linear), + NumberedSampler::Constexpr(nan_bias), + ]; + let uses = [linear, nan_bias] + .map(|words| CombinedSampler::new(SamplerRef::Constexpr(words), false, false)); + let numbered = crate::pipeline::number_samplers( + device.core(), + samplers, + &[(0, uses[0]), (1, uses[1])], + |_| Ok(vec![1, 2]), + || Error::Unsupported(Gap::KernelInput), + ); + assert!( + matches!(numbered, Err(Error::Translation(_))), + "{name}: the NaN bias is the translator's refusal" + ); + assert_eq!( + counters.gauges().descriptor_slots, + before.descriptor_slots, + "{name}: the linear sampler's elements are given back" + ); + crate::tests::command::on_device::within("the reaper frees the device sampler", || { + counters.gauges().device_samplers == before.device_samplers + }); + device + .release_indices(grant) + .unwrap_or_else(|error| panic!("{name}: index 1 is free: {error}")); + } + } }