Skip to content

Commit

Permalink
wgpu: Cache texture views along side their owned textures in texture …
Browse files Browse the repository at this point in the history
…pool
  • Loading branch information
Dinnerbone committed Jan 9, 2023
1 parent fe49f7b commit b07a01d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 54 deletions.
10 changes: 6 additions & 4 deletions render/wgpu/src/buffer_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>,
}

Expand All @@ -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,
Expand All @@ -46,15 +46,17 @@ 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,
sample_count,
dimension: wgpu::TextureDimension::D2,
format,
usage,
})
});
let view = texture.create_view(&Default::default());
(texture, view)
}))
});
pool.take(&descriptors)
Expand Down
3 changes: 1 addition & 2 deletions render/wgpu/src/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
50 changes: 38 additions & 12 deletions render/wgpu/src/surface/commands.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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)]
Expand All @@ -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,
Expand Down Expand Up @@ -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,
})
Expand Down
54 changes: 18 additions & 36 deletions render/wgpu/src/surface/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
}

Expand All @@ -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
}

Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}
}

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

0 comments on commit b07a01d

Please sign in to comment.