use std::collections::HashMap;
pub struct RenderTargetEntry {
pub texture: wgpu::Texture,
pub view: wgpu::TextureView,
pub width: u32,
pub height: u32,
}
pub struct RenderTargetStore {
pub targets: HashMap<u32, RenderTargetEntry>,
}
impl RenderTargetStore {
pub fn new() -> Self {
Self {
targets: HashMap::new(),
}
}
pub fn create(
&mut self,
device: &wgpu::Device,
id: u32,
width: u32,
height: u32,
surface_format: wgpu::TextureFormat,
) {
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some(&format!("render_target_{id}")),
size: wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: surface_format,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT
| wgpu::TextureUsages::TEXTURE_BINDING,
view_formats: &[],
});
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
self.targets.insert(
id,
RenderTargetEntry {
texture,
view,
width,
height,
},
);
}
pub fn get_view(&self, id: u32) -> Option<&wgpu::TextureView> {
self.targets.get(&id).map(|e| &e.view)
}
pub fn get_dims(&self, id: u32) -> Option<(u32, u32)> {
self.targets.get(&id).map(|e| (e.width, e.height))
}
pub fn destroy(&mut self, id: u32) {
self.targets.remove(&id);
}
}