use super::*;
impl WgpuTextureManager {
pub fn new() -> Self {
Self {
textures: HashMap::new(),
next_id: 1, custom_samplers: HashMap::new(),
custom_sampler_by_texture: HashMap::new(),
common_bind_groups: HashMap::new(),
next_sampler_id: 1, }
}
pub fn register_texture(&mut self, texture: WgpuTexture) -> TextureId {
let id = TextureId::new(self.next_id);
self.next_id += 1;
self.textures.insert(id, texture);
id
}
pub fn get_texture(&self, id: TextureId) -> Option<&WgpuTexture> {
self.textures.get(&id)
}
pub fn remove_texture(&mut self, id: TextureId) -> Option<WgpuTexture> {
self.textures.remove(&id)
}
pub fn contains_texture(&self, id: TextureId) -> bool {
self.textures.contains_key(&id)
}
pub fn insert_texture_with_id(&mut self, id: TextureId, texture: WgpuTexture) {
self.textures.insert(id, texture);
if id.id() >= self.next_id {
self.next_id = id.id().saturating_add(1);
}
}
pub(crate) fn set_custom_sampler_for_texture(
&mut self,
texture_id: TextureId,
sampler: Sampler,
) -> u64 {
let sampler_id = self.next_sampler_id;
self.next_sampler_id += 1;
self.custom_samplers.insert(sampler_id, sampler);
self.custom_sampler_by_texture
.insert(texture_id, sampler_id);
self.common_bind_groups.remove(&sampler_id);
sampler_id
}
pub(crate) fn update_custom_sampler_for_texture(
&mut self,
texture_id: TextureId,
sampler: Sampler,
) -> bool {
if !self.textures.contains_key(&texture_id) {
return false;
}
if let Some(sampler_id) = self.custom_sampler_by_texture.get(&texture_id).copied() {
self.custom_samplers.insert(sampler_id, sampler);
self.common_bind_groups.remove(&sampler_id);
} else {
self.set_custom_sampler_for_texture(texture_id, sampler);
}
true
}
pub(crate) fn custom_sampler_id_for_texture(&self, texture_id: TextureId) -> Option<u64> {
self.custom_sampler_by_texture.get(&texture_id).copied()
}
pub(crate) fn clear_custom_sampler_for_texture(&mut self, texture_id: TextureId) {
if let Some(sampler_id) = self.custom_sampler_by_texture.remove(&texture_id) {
self.common_bind_groups.remove(&sampler_id);
}
}
pub(crate) fn get_or_create_common_bind_group_for_sampler(
&mut self,
device: &Device,
common_layout: &BindGroupLayout,
uniform_buffer: &Buffer,
sampler_id: u64,
) -> Option<BindGroup> {
if let Some(bg) = self.common_bind_groups.get(&sampler_id) {
return Some(bg.clone());
}
let sampler = self.custom_samplers.get(&sampler_id)?;
let bg = device.create_bind_group(&BindGroupDescriptor {
label: Some("Dear ImGui Common Bind Group (custom sampler)"),
layout: common_layout,
entries: &[
BindGroupEntry {
binding: 0,
resource: uniform_buffer.as_entire_binding(),
},
BindGroupEntry {
binding: 1,
resource: BindingResource::Sampler(sampler),
},
],
});
self.common_bind_groups.insert(sampler_id, bg.clone());
Some(bg)
}
pub fn texture_count(&self) -> usize {
self.textures.len()
}
pub fn clear(&mut self) {
self.textures.clear();
self.next_id = 1;
self.custom_sampler_by_texture.clear();
self.common_bind_groups.clear();
self.custom_samplers.clear();
self.next_sampler_id = 1;
}
}