From 50719da96b28d49c11ceeb5795f5b75c89af9be2 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Wed, 18 Nov 2026 13:42:52 -0800 Subject: [PATCH] rt: a constexpr sampler the translator refuses gives back the elements the state's earlier ones took number_samplers released a state's constexpr samplers' elements only when hold_constexpr failed. It returned early without releasing when a later constexpr sampler's device sampler was refused (the translator's Err, or the caller's gap for a use with none). So the elements and device samplers of the constexpr samplers numbered before it stayed held, with their indices, and no state owned them. Each sampler's uses now resolve in one function. Every refusal from them, or from the hold, takes the single release path. The new device test numbers two constexpr samplers, a linear one and the same state with a NaN level bias. The second fails. The test asserts that the arrays' descriptor slots are back at their count before the call, that the reaper frees the linear device sampler (the device sampler gauge returns to its count before the call), and that the grant's indices release. A pipeline cannot reach this through the public API today: constexpr_sampler refuses only what translation for the same profile refused first. The test calls number_samplers directly. --- crates/metal2vulkan-rt/src/pipeline.rs | 47 +++++++++++----- crates/metal2vulkan-rt/src/tests/sampler.rs | 62 ++++++++++++++++++++- 2 files changed, 93 insertions(+), 16 deletions(-) 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}")); + } + } }