Skip to content

Commit

Permalink
Add bindgroup cache in wgpu (#130)
Browse files Browse the repository at this point in the history
avoid recreating them every frame

Co-authored-by: Lucien Greathouse <[email protected]>
  • Loading branch information
Uriopass and LPGhatguy committed Jul 25, 2024
1 parent 2536770 commit d60f37f
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 33 deletions.
88 changes: 88 additions & 0 deletions crates/yakui-wgpu/src/bindgroup_cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use crate::samplers::Samplers;
use std::collections::HashMap;
use yakui_core::TextureId;

#[derive(Copy, Clone, Hash, PartialEq, Eq)]
pub(crate) struct TextureBindgroupCacheEntry {
pub id: TextureId,
pub min_filter: wgpu::FilterMode,
pub mag_filter: wgpu::FilterMode,
pub mipmap_filter: wgpu::FilterMode,
pub address_mode: wgpu::AddressMode,
}

pub(crate) struct TextureBindgroupCache {
cache: HashMap<TextureBindgroupCacheEntry, wgpu::BindGroup>,
layout: wgpu::BindGroupLayout,
pub default: wgpu::BindGroup,
}

impl TextureBindgroupCache {
pub fn new(layout: wgpu::BindGroupLayout, default: wgpu::BindGroup) -> Self {
Self {
cache: HashMap::new(),
layout,
default,
}
}

pub fn clear(&mut self) {
self.cache.clear();
}

pub fn update(
&mut self,
device: &wgpu::Device,
entry: TextureBindgroupCacheEntry,
view: &wgpu::TextureView,
samplers: &Samplers,
) {
self.cache.entry(entry).or_insert_with(|| {
bindgroup(
device,
&self.layout,
samplers,
view,
entry.min_filter,
entry.mag_filter,
entry.mipmap_filter,
entry.address_mode,
)
});
}

pub fn get(&self, entry: &TextureBindgroupCacheEntry) -> &wgpu::BindGroup {
self.cache.get(entry).unwrap_or(&self.default)
}
}

pub fn bindgroup(
device: &wgpu::Device,
layout: &wgpu::BindGroupLayout,
samplers: &Samplers,
view: &wgpu::TextureView,
min_filter: wgpu::FilterMode,
mag_filter: wgpu::FilterMode,
mipmap_filter: wgpu::FilterMode,
address_mode: wgpu::AddressMode,
) -> wgpu::BindGroup {
device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("yakui Bind Group"),
layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(view),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::Sampler(samplers.get(
min_filter,
mag_filter,
mipmap_filter,
address_mode,
)),
},
],
})
}
77 changes: 44 additions & 33 deletions crates/yakui-wgpu/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(clippy::new_without_default)]

mod bindgroup_cache;
mod buffer;
mod pipeline_cache;
mod samplers;
Expand All @@ -18,18 +19,19 @@ use yakui_core::geometry::{Rect, Vec2, Vec4};
use yakui_core::paint::{PaintDom, Pipeline, Texture, TextureChange, TextureFormat};
use yakui_core::{ManagedTextureId, TextureId};

use self::bindgroup_cache::TextureBindgroupCache;
use self::bindgroup_cache::TextureBindgroupCacheEntry;
use self::pipeline_cache::PipelineCache;
use self::samplers::Samplers;
use self::texture::{GpuManagedTexture, GpuTexture};

pub struct YakuiWgpu {
main_pipeline: PipelineCache,
text_pipeline: PipelineCache,
layout: wgpu::BindGroupLayout,
default_texture: GpuManagedTexture,
samplers: Samplers,
textures: Arena<GpuTexture>,
managed_textures: HashMap<ManagedTextureId, GpuManagedTexture>,
texture_bindgroup_cache: TextureBindgroupCache,

vertices: Buffer,
indices: Buffer,
Expand Down Expand Up @@ -109,16 +111,25 @@ impl YakuiWgpu {
let default_texture_data =
Texture::new(TextureFormat::Rgba8Srgb, UVec2::new(1, 1), vec![255; 4]);
let default_texture = GpuManagedTexture::new(device, queue, &default_texture_data);
let default_bindgroup = bindgroup_cache::bindgroup(
device,
&layout,
&samplers,
&default_texture.view,
default_texture.min_filter,
default_texture.mag_filter,
wgpu::FilterMode::Nearest,
wgpu::AddressMode::ClampToEdge,
);

Self {
main_pipeline,
text_pipeline,
layout,
default_texture,
samplers,
textures: Arena::new(),
managed_textures: HashMap::new(),

texture_bindgroup_cache: TextureBindgroupCache::new(layout, default_bindgroup),
vertices: Buffer::new(wgpu::BufferUsages::VERTEX),
indices: Buffer::new(wgpu::BufferUsages::INDEX),
commands: Vec::new(),
Expand Down Expand Up @@ -281,7 +292,12 @@ impl YakuiWgpu {
}
}

render_pass.set_bind_group(0, &command.bind_group, &[]);
let bindgroup = command
.bind_group_entry
.map(|entry| self.texture_bindgroup_cache.get(&entry))
.unwrap_or(&self.texture_bindgroup_cache.default);

render_pass.set_bind_group(0, bindgroup, &[]);
render_pass.draw_indexed(command.index_range.clone(), 0, 0..1);
}
}
Expand All @@ -293,6 +309,7 @@ impl YakuiWgpu {
self.vertices.clear();
self.indices.clear();
self.commands.clear();
self.texture_bindgroup_cache.clear();

let commands = paint
.layers()
Expand All @@ -314,12 +331,13 @@ impl YakuiWgpu {
self.vertices.extend(vertices);
self.indices.extend(indices);

let (view, min_filter, mag_filter, mipmap_filter, address_mode) = call
let bind_group_entry = call
.texture
.and_then(|id| match id {
TextureId::Managed(managed) => {
let texture = self.managed_textures.get(&managed)?;
Some((
id,
&texture.view,
texture.min_filter,
texture.mag_filter,
Expand All @@ -331,6 +349,7 @@ impl YakuiWgpu {
let index = Index::from_bits(bits)?;
let texture = self.textures.get(index)?;
Some((
id,
&texture.view,
texture.min_filter,
texture.mag_filter,
Expand All @@ -339,36 +358,28 @@ impl YakuiWgpu {
))
}
})
.unwrap_or((
&self.default_texture.view,
self.default_texture.min_filter,
self.default_texture.mag_filter,
wgpu::FilterMode::Nearest,
self.default_texture.address_mode,
));

let sampler =
self.samplers
.get(min_filter, mag_filter, mipmap_filter, address_mode);

let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("yakui Bind Group"),
layout: &self.layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(view),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::Sampler(sampler),
.map(
|(id, view, min_filter, mag_filter, mipmap_filter, address_mode)| {
let entry = TextureBindgroupCacheEntry {
id,
min_filter,
mag_filter,
mipmap_filter,
address_mode,
};
self.texture_bindgroup_cache.update(
device,
entry,
view,
&self.samplers,
);
entry
},
],
});
);

DrawCommand {
index_range: start..end,
bind_group,
bind_group_entry,
pipeline: call.pipeline,
clip: call.clip,
}
Expand Down Expand Up @@ -411,7 +422,7 @@ impl YakuiWgpu {

struct DrawCommand {
index_range: Range<u32>,
bind_group: wgpu::BindGroup,
bind_group_entry: Option<TextureBindgroupCacheEntry>,
pipeline: Pipeline,
clip: Option<Rect>,
}
Expand Down

0 comments on commit d60f37f

Please sign in to comment.