wgpu: Cache texture views along side their owned textures in texture pool

This commit is contained in:
Nathan Adams 2023-01-08 20:17:54 +01:00
parent fe49f7b872
commit b07a01da57
4 changed files with 63 additions and 54 deletions

View File

@ -10,7 +10,7 @@ type Constructor<T> = Box<dyn Fn(&Descriptors) -> T>;
#[derive(Debug)]
pub struct TexturePool {
pools: FnvHashMap<TextureKey, BufferPool<wgpu::Texture>>,
pools: FnvHashMap<TextureKey, BufferPool<(wgpu::Texture, wgpu::TextureView)>>,
globals_cache: FnvHashMap<GlobalsKey, Weak<Globals>>,
}
@ -29,7 +29,7 @@ impl TexturePool {
usage: wgpu::TextureUsages,
format: wgpu::TextureFormat,
sample_count: u32,
) -> PoolEntry<wgpu::Texture> {
) -> PoolEntry<(wgpu::Texture, wgpu::TextureView)> {
let key = TextureKey {
size,
usage,
@ -46,7 +46,7 @@ impl TexturePool {
None
};
BufferPool::new(Box::new(move |descriptors| {
descriptors.device.create_texture(&wgpu::TextureDescriptor {
let texture = descriptors.device.create_texture(&wgpu::TextureDescriptor {
label: label.as_deref(),
size,
mip_level_count: 1,
@ -54,7 +54,9 @@ impl TexturePool {
dimension: wgpu::TextureDimension::D2,
format,
usage,
})
});
let view = texture.create_view(&Default::default());
(texture, view)
}))
});
pool.take(&descriptors)

View File

@ -258,7 +258,6 @@ impl Surface {
let parent_blend_buffer =
parent.update_blend_buffer(&descriptors, texture_pool, draw_encoder);
let texture_view = texture.create_view(&Default::default());
let blend_bind_group =
descriptors
.device
@ -283,7 +282,7 @@ impl Surface {
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::TextureView(&texture_view),
resource: wgpu::BindingResource::TextureView(&texture.1),
},
wgpu::BindGroupEntry {
binding: 2,

View File

@ -1,7 +1,7 @@
use crate::blend::TrivialBlend;
use crate::blend::{BlendType, ComplexBlend};
use crate::buffer_pool::{PoolEntry, TexturePool};
use crate::mesh::{BitmapBinds, DrawType, Mesh};
use crate::mesh::{DrawType, Mesh};
use crate::surface::target::CommandTarget;
use crate::surface::Surface;
use crate::{
@ -368,7 +368,11 @@ impl<'pass, 'frame: 'pass, 'global: 'frame> CommandRenderer<'pass, 'frame, 'glob
pub enum Chunk {
Draw(Vec<DrawCommand>, bool),
Blend(PoolEntry<wgpu::Texture>, ComplexBlend, bool),
Blend(
PoolEntry<(wgpu::Texture, wgpu::TextureView)>,
ComplexBlend,
bool,
),
}
#[derive(Debug)]
@ -380,7 +384,7 @@ pub enum DrawCommand {
blend_mode: TrivialBlend,
},
RenderTexture {
_texture: PoolEntry<wgpu::Texture>,
_texture: PoolEntry<(wgpu::Texture, wgpu::TextureView)>,
binds: wgpu::BindGroup,
transform: Transform,
blend_mode: TrivialBlend,
@ -457,17 +461,39 @@ pub fn chunk_blends<'a>(
color_transform: Default::default(),
};
let texture = target.take_color_texture();
let binds = BitmapBinds::new(
&descriptors.device,
&descriptors.bind_layouts.bitmap,
descriptors.bitmap_samplers.get_sampler(false, false),
&descriptors.quad.texture_transforms,
texture.create_view(&Default::default()),
None,
);
let bind_group =
descriptors
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &&descriptors.bind_layouts.bitmap,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: descriptors
.quad
.texture_transforms
.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::TextureView(
&texture.1,
),
},
wgpu::BindGroupEntry {
binding: 2,
resource: wgpu::BindingResource::Sampler(
descriptors
.bitmap_samplers
.get_sampler(false, false),
),
},
],
label: None,
});
current.push(DrawCommand::RenderTexture {
_texture: texture,
binds: binds.bind_group,
binds: bind_group,
transform,
blend_mode,
})

View File

@ -9,8 +9,7 @@ use std::sync::Arc;
#[derive(Debug)]
pub struct ResolveBuffer {
texture: PoolEntry<wgpu::Texture>,
view: wgpu::TextureView,
texture: PoolEntry<(wgpu::Texture, wgpu::TextureView)>,
}
impl ResolveBuffer {
@ -22,29 +21,25 @@ impl ResolveBuffer {
pool: &mut TexturePool,
) -> Self {
let texture = pool.get_texture(descriptors, size, usage, format, 1);
let view = texture.create_view(&Default::default());
Self { texture, view }
Self { texture }
}
pub fn view(&self) -> &wgpu::TextureView {
&self.view
&self.texture.1
}
pub fn texture(&self) -> &wgpu::Texture {
&self.texture
&self.texture.0
}
pub fn take_texture(self) -> PoolEntry<wgpu::Texture> {
pub fn take_texture(self) -> PoolEntry<(wgpu::Texture, wgpu::TextureView)> {
self.texture
}
}
#[derive(Debug)]
pub struct FrameBuffer {
texture: PoolEntry<wgpu::Texture>,
view: wgpu::TextureView,
texture: PoolEntry<(wgpu::Texture, wgpu::TextureView)>,
size: wgpu::Extent3d,
}
@ -59,24 +54,18 @@ impl FrameBuffer {
) -> Self {
let texture = pool.get_texture(descriptors, size, usage, format, sample_count);
let view = texture.create_view(&Default::default());
Self {
texture,
view,
size,
}
Self { texture, size }
}
pub fn view(&self) -> &wgpu::TextureView {
&self.view
&self.texture.1
}
pub fn texture(&self) -> &wgpu::Texture {
&self.texture
&self.texture.0
}
pub fn take_texture(self) -> PoolEntry<wgpu::Texture> {
pub fn take_texture(self) -> PoolEntry<(wgpu::Texture, wgpu::TextureView)> {
self.texture
}
@ -87,8 +76,7 @@ impl FrameBuffer {
#[derive(Debug)]
pub struct BlendBuffer {
texture: PoolEntry<wgpu::Texture>,
view: wgpu::TextureView,
texture: PoolEntry<(wgpu::Texture, wgpu::TextureView)>,
}
impl BlendBuffer {
@ -100,24 +88,22 @@ impl BlendBuffer {
pool: &mut TexturePool,
) -> Self {
let texture = pool.get_texture(descriptors, size, usage, format, 1);
let view = texture.create_view(&Default::default());
Self { texture, view }
Self { texture }
}
pub fn view(&self) -> &wgpu::TextureView {
&self.view
&self.texture.1
}
pub fn texture(&self) -> &wgpu::Texture {
&self.texture
&self.texture.0
}
}
#[derive(Debug)]
pub struct DepthBuffer {
_texture: PoolEntry<wgpu::Texture>,
view: wgpu::TextureView,
texture: PoolEntry<(wgpu::Texture, wgpu::TextureView)>,
}
impl DepthBuffer {
@ -135,15 +121,11 @@ impl DepthBuffer {
msaa_sample_count,
);
let view = texture.create_view(&Default::default());
Self {
_texture: texture,
view,
}
Self { texture }
}
pub fn view(&self) -> &wgpu::TextureView {
&self.view
&self.texture.1
}
}
@ -236,7 +218,7 @@ impl CommandTarget {
});
}
pub fn take_color_texture(self) -> PoolEntry<wgpu::Texture> {
pub fn take_color_texture(self) -> PoolEntry<(wgpu::Texture, wgpu::TextureView)> {
self.resolve_buffer
.map(|b| b.take_texture())
.unwrap_or_else(|| self.frame_buffer.take_texture())