Skip to content

Commit

Permalink
Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cwfitzgerald committed Jan 6, 2025
1 parent 667314f commit 89e727d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
4 changes: 3 additions & 1 deletion wgpu-hal/src/vulkan/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) };
}
Expand Down
19 changes: 18 additions & 1 deletion wgpu-hal/src/vulkan/sampler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -73,13 +76,17 @@ pub(crate) struct SamplerCache {
samplers: HashMap<HashableSamplerCreateInfo, CacheEntry>,
/// 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,
}
}

Expand All @@ -95,6 +102,11 @@ impl SamplerCache {
device: &ash::Device,
create_info: vk::SamplerCreateInfo<'static>,
) -> Result<vk::Sampler, crate::DeviceError> {
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();

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 89e727d

Please sign in to comment.