Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions crates/bevy_render/src/render_resource/pipeline_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use bevy_material::descriptor::{
BindGroupLayoutDescriptor, CachedComputePipelineId, CachedRenderPipelineId,
ComputePipelineDescriptor, PipelineDescriptor, RenderPipelineDescriptor,
};
use smallvec::SmallVec;

use crate::{
render_resource::*,
Expand All @@ -16,7 +17,7 @@ use bevy_ecs::{
system::{Res, ResMut},
};
use bevy_log::error;
use bevy_platform::collections::{HashMap, HashSet};
use bevy_platform::collections::{hash_map::RawEntryMut, HashMap, HashSet};
use bevy_shader::{
CachedPipelineId, Shader, ShaderCache, ShaderCacheError, ShaderCacheSource, ShaderDefVal,
ValidateShader,
Expand Down Expand Up @@ -79,8 +80,15 @@ impl CachedPipelineState {
}
}

// The default webgpu `max_bind_groups` is 4. Most desktop GPUs support 8.
// Since the element size we used below is small, we inline 8 on the stack.
const BIND_GROUP_LAYOUTS_INLINE_CAPACITY: usize = 8;

type ImmediateSize = u32;
type LayoutCacheKey = (Vec<BindGroupLayoutId>, ImmediateSize);
type LayoutCacheKey = (
SmallVec<[BindGroupLayoutId; BIND_GROUP_LAYOUTS_INLINE_CAPACITY]>,
ImmediateSize,
);
#[derive(Default)]
struct LayoutCache {
layouts: HashMap<LayoutCacheKey, Arc<WgpuWrapper<PipelineLayout>>>,
Expand All @@ -101,7 +109,7 @@ impl LayoutCache {
.iter()
.map(BindGroupLayout::value)
.map(Some)
.collect::<Vec<_>>();
.collect::<SmallVec<[_; BIND_GROUP_LAYOUTS_INLINE_CAPACITY]>>();
Arc::new(WgpuWrapper::new(render_device.create_pipeline_layout(
&PipelineLayoutDescriptor {
bind_group_layouts: &bind_group_layouts,
Expand Down Expand Up @@ -175,15 +183,17 @@ impl BindGroupLayoutCache {
fn get(
&mut self,
render_device: &RenderDevice,
descriptor: BindGroupLayoutDescriptor,
descriptor: &BindGroupLayoutDescriptor,
) -> BindGroupLayout {
self.bgls
.entry(descriptor)
.or_insert_with_key(|descriptor| {
render_device
.create_bind_group_layout(descriptor.label.as_ref(), &descriptor.entries)
})
.clone()
match self.bgls.raw_entry_mut().from_key(descriptor) {
RawEntryMut::Occupied(entry) => entry.into_mut(),
RawEntryMut::Vacant(slot) => {
let created = render_device
.create_bind_group_layout(descriptor.label.as_ref(), &descriptor.entries);
slot.insert(descriptor.clone(), created).1
}
}
.clone()
}
}

Expand Down Expand Up @@ -442,7 +452,7 @@ impl PipelineCache {
self.bindgroup_layout_cache
.lock()
.unwrap()
.get(&self.device, bind_group_layout_descriptor.clone())
.get(&self.device, bind_group_layout_descriptor)
}

/// Inserts a [`Shader`] into this cache with the provided [`AssetId`].
Expand Down Expand Up @@ -478,9 +488,9 @@ impl PipelineCache {
.layout
.iter()
.map(|bind_group_layout_descriptor| {
bindgroup_layout_cache.get(&self.device, bind_group_layout_descriptor.clone())
bindgroup_layout_cache.get(&self.device, bind_group_layout_descriptor)
})
.collect::<Vec<_>>();
.collect::<SmallVec<[_; BIND_GROUP_LAYOUTS_INLINE_CAPACITY]>>();

create_pipeline_task(
async move {
Expand Down Expand Up @@ -586,9 +596,9 @@ impl PipelineCache {
.layout
.iter()
.map(|bind_group_layout_descriptor| {
bindgroup_layout_cache.get(&self.device, bind_group_layout_descriptor.clone())
bindgroup_layout_cache.get(&self.device, bind_group_layout_descriptor)
})
.collect::<Vec<_>>();
.collect::<SmallVec<[_; BIND_GROUP_LAYOUTS_INLINE_CAPACITY]>>();

create_pipeline_task(
async move {
Expand Down
Loading