Skip to content

Commit

Permalink
Add bindgroup cache in wgpu
Browse files Browse the repository at this point in the history
avoid recreating them every frame
  • Loading branch information
Uriopass committed Dec 20, 2023
1 parent 6427909 commit d428807
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 36 deletions.
74 changes: 74 additions & 0 deletions crates/yakui-wgpu/src/bindgroup_cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use crate::samplers::Samplers;
use std::collections::HashMap;
use wgpu::{FilterMode, TextureView};
use yakui_core::TextureId;

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

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 update(
&mut self,
device: &wgpu::Device,
entry: TextureBindgroupCacheEntry,
view: &TextureView,
samplers: &Samplers,
) {
self.cache.entry(entry).or_insert_with(|| {
bindgroup(
device,
&self.layout,
samplers,
view,
entry.min_filter,
entry.mag_filter,
)
});
}

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: &TextureView,
min_filter: FilterMode,
mag_filter: FilterMode,
) -> 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)),
},
],
})
}
73 changes: 37 additions & 36 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,23 @@ 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,
);

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 @@ -277,7 +286,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 Down Expand Up @@ -310,45 +324,33 @@ impl YakuiWgpu {
self.vertices.extend(vertices);
self.indices.extend(indices);

let (view, min_filter, mag_filter) = call
let bind_group_entry = call
.texture
.and_then(|id| match id {
TextureId::Managed(managed) => {
let texture = self.managed_textures.get(&managed)?;
Some((&texture.view, texture.min_filter, texture.mag_filter))
Some((id, &texture.view, texture.min_filter, texture.mag_filter))
}
TextureId::User(bits) => {
let index = Index::from_bits(bits)?;
let texture = self.textures.get(index)?;
Some((&texture.view, texture.min_filter, texture.mag_filter))
Some((id, &texture.view, texture.min_filter, texture.mag_filter))
}
})
.unwrap_or((
&self.default_texture.view,
self.default_texture.min_filter,
self.default_texture.mag_filter,
));

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

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)| {
let entry = TextureBindgroupCacheEntry {
id,
min_filter,
mag_filter,
};
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 All @@ -361,10 +363,9 @@ impl YakuiWgpu {
profiling::scope!("update_textures");

for (id, texture) in paint.textures() {
if !self.managed_textures.contains_key(&id) {
self.managed_textures
.insert(id, GpuManagedTexture::new(device, queue, texture));
}
self.managed_textures
.entry(id)
.or_insert_with(|| GpuManagedTexture::new(device, queue, texture));
}

for (id, change) in paint.texture_edits() {
Expand Down Expand Up @@ -392,7 +393,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 d428807

Please sign in to comment.