diff --git a/Cargo.toml b/Cargo.toml index e812891f64c..c16b1b000d4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -109,6 +109,7 @@ noise = { version = "0.8", git = "https://github.com/Razaekel/noise-rs.git", rev nv-flip = "0.1" obj = "0.10" once_cell = "1.20.2" +# Firefox has 3.4.0 vendored, so we allow that version in our dependencies ordered-float = ">=3,<=4.6" parking_lot = "0.12.1" pico-args = { version = "0.5.0", features = [ diff --git a/wgpu-hal/src/vulkan/device.rs b/wgpu-hal/src/vulkan/device.rs index 017fedbabdf..1c8d36a3734 100644 --- a/wgpu-hal/src/vulkan/device.rs +++ b/wgpu-hal/src/vulkan/device.rs @@ -1331,7 +1331,9 @@ impl crate::Device for super::Device { .lock() .create_sampler(&self.shared.raw, create_info)?; - // TODO: Cached samplers will just continually overwrite the label + // Note: Cached samplers will just continually overwrite the label + // + // https://github.com/gfx-rs/wgpu/issues/6867 if let Some(label) = desc.label { unsafe { self.shared.set_object_name(raw, label) }; } diff --git a/wgpu-hal/src/vulkan/sampler.rs b/wgpu-hal/src/vulkan/sampler.rs index 9eb4630c6e0..11030226f08 100644 --- a/wgpu-hal/src/vulkan/sampler.rs +++ b/wgpu-hal/src/vulkan/sampler.rs @@ -7,6 +7,9 @@ use std::collections::{hash_map::Entry, HashMap}; use ash::vk; use ordered_float::OrderedFloat; +/// If the allowed sampler count is above this value, the sampler cache is disabled. +const ENABLE_SAMPLER_CACHE_CUTOFF: u32 = 1 << 20; + /// [`vk::SamplerCreateInfo`] is not hashable, so we wrap it in a newtype that is. /// /// We use [`OrderedFloat`] to allow for floating point values to be compared and @@ -73,13 +76,17 @@ pub(crate) struct SamplerCache { samplers: HashMap, /// Maximum number of unique samplers that can be created. total_capacity: u32, + /// If true, the sampler cache is disabled and all samplers are created on demand. + passthrough: bool, } impl SamplerCache { pub fn new(total_capacity: u32) -> Self { - SamplerCache { + let passthrough = total_capacity >= ENABLE_SAMPLER_CACHE_CUTOFF; + Self { samplers: HashMap::new(), total_capacity, + passthrough, } } @@ -95,6 +102,11 @@ impl SamplerCache { device: &ash::Device, create_info: vk::SamplerCreateInfo<'static>, ) -> Result { + if self.passthrough { + return unsafe { device.create_sampler(&create_info, None) } + .map_err(super::map_host_device_oom_and_ioca_err); + }; + // Get the number of used samplers. Needs to be done before to appease the borrow checker. let used_samplers = self.samplers.len(); @@ -138,6 +150,11 @@ impl SamplerCache { create_info: vk::SamplerCreateInfo<'static>, provided_sampler: vk::Sampler, ) { + if self.passthrough { + unsafe { device.destroy_sampler(provided_sampler, None) }; + return; + }; + let Entry::Occupied(mut hash_map_entry) = self.samplers.entry(HashableSamplerCreateInfo(create_info)) else {