Skip to content

Commit cd9550f

Browse files
committed
Move ShaderCache shader defs into pipelines
1 parent 2010164 commit cd9550f

File tree

4 files changed

+42
-18
lines changed

4 files changed

+42
-18
lines changed

crates/bevy_pbr/src/prepass/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use bevy_render::{
3434
ShaderType, SpecializedMeshPipeline, SpecializedMeshPipelineError,
3535
SpecializedMeshPipelines, StencilFaceState, StencilState, TextureDescriptor,
3636
TextureDimension, TextureFormat, TextureSampleType, TextureUsages, TextureViewDimension,
37-
VertexState,
37+
VertexState, platform_shader_defs,
3838
},
3939
renderer::RenderDevice,
4040
texture::{FallbackImagesDepth, FallbackImagesMsaa, TextureCache},
@@ -161,6 +161,7 @@ pub struct PrepassPipeline<M: Material> {
161161
pub material_vertex_shader: Option<Handle<Shader>>,
162162
pub material_fragment_shader: Option<Handle<Shader>>,
163163
pub material_pipeline: MaterialPipeline<M>,
164+
pub shader_defs: Vec<ShaderDefVal>,
164165
_marker: PhantomData<M>,
165166
}
166167

@@ -199,6 +200,8 @@ impl<M: Material> FromWorld for PrepassPipeline<M> {
199200

200201
let mesh_pipeline = world.resource::<MeshPipeline>();
201202

203+
let shader_defs = platform_shader_defs(render_device);
204+
202205
PrepassPipeline {
203206
view_layout,
204207
mesh_layout: mesh_pipeline.mesh_layout.clone(),
@@ -215,6 +218,7 @@ impl<M: Material> FromWorld for PrepassPipeline<M> {
215218
},
216219
material_layout: M::bind_group_layout(render_device),
217220
material_pipeline: world.resource::<MaterialPipeline<M>>().clone(),
221+
shader_defs,
218222
_marker: PhantomData,
219223
}
220224
}
@@ -232,7 +236,7 @@ where
232236
layout: &MeshVertexBufferLayout,
233237
) -> Result<RenderPipelineDescriptor, SpecializedMeshPipelineError> {
234238
let mut bind_group_layout = vec![self.view_layout.clone()];
235-
let mut shader_defs = Vec::new();
239+
let mut shader_defs = self.shader_defs.clone();
236240
let mut vertex_attributes = Vec::new();
237241

238242
// NOTE: Eventually, it would be nice to only add this when the shaders are overloaded by the Material.

crates/bevy_pbr/src/render/mesh.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ pub struct MeshPipeline {
273273
// This dummy white texture is to be used in place of optional StandardMaterial textures
274274
pub dummy_white_gpu_image: GpuImage,
275275
pub clustered_forward_buffer_binding_type: BufferBindingType,
276+
pub shader_defs: Vec<ShaderDefVal>,
276277
}
277278

278279
impl FromWorld for MeshPipeline {
@@ -532,13 +533,17 @@ impl FromWorld for MeshPipeline {
532533
}
533534
};
534535

536+
// Shader defs
537+
let shader_defs = platform_shader_defs(&render_device);
538+
535539
MeshPipeline {
536540
view_layout,
537541
view_layout_multisampled,
538542
mesh_layout,
539543
skinned_mesh_layout,
540544
clustered_forward_buffer_binding_type,
541545
dummy_white_gpu_image,
546+
shader_defs,
542547
}
543548
}
544549
}
@@ -654,7 +659,7 @@ impl SpecializedMeshPipeline for MeshPipeline {
654659
key: Self::Key,
655660
layout: &MeshVertexBufferLayout,
656661
) -> Result<RenderPipelineDescriptor, SpecializedMeshPipelineError> {
657-
let mut shader_defs = Vec::new();
662+
let mut shader_defs = self.shader_defs.clone();
658663
let mut vertex_attributes = Vec::new();
659664

660665
if layout.contains(Mesh::ATTRIBUTE_POSITION) {

crates/bevy_render/src/render_resource/pipeline_cache.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ use parking_lot::Mutex;
2121
use std::{hash::Hash, iter::FusedIterator, mem, ops::Deref};
2222
use thiserror::Error;
2323
use wgpu::{
24-
PipelineLayoutDescriptor, PushConstantRange, VertexBufferLayout as RawVertexBufferLayout,
24+
PipelineLayoutDescriptor, PushConstantRange,
25+
VertexBufferLayout as RawVertexBufferLayout,
2526
};
2627

2728
use crate::render_resource::resource_macros::*;
@@ -193,25 +194,13 @@ impl ShaderCache {
193194
let module = match data.processed_shaders.entry(shader_defs.to_vec()) {
194195
Entry::Occupied(entry) => entry.into_mut(),
195196
Entry::Vacant(entry) => {
196-
let mut shader_defs = shader_defs.to_vec();
197-
#[cfg(feature = "webgl")]
198-
{
199-
shader_defs.push("NO_ARRAY_TEXTURES_SUPPORT".into());
200-
shader_defs.push("SIXTEEN_BYTE_ALIGNMENT".into());
201-
}
202-
203-
shader_defs.push(ShaderDefVal::UInt(
204-
String::from("AVAILABLE_STORAGE_BUFFER_BINDINGS"),
205-
render_device.limits().max_storage_buffers_per_shader_stage,
206-
));
207-
208197
debug!(
209198
"processing shader {:?}, with shader defs {:?}",
210199
handle, shader_defs
211200
);
212201
let processed = self.processor.process(
213202
shader,
214-
&shader_defs,
203+
shader_defs,
215204
&self.shaders,
216205
&self.import_path_shaders,
217206
)?;
@@ -310,7 +299,26 @@ impl ShaderCache {
310299
}
311300
}
312301

302+
// Utility function for initializing a set of platform-driven shader defs from a render device
303+
pub fn platform_shader_defs(render_device: &RenderDevice) -> Vec<ShaderDefVal> {
304+
let mut shader_defs = Vec::new();
305+
306+
#[cfg(feature = "webgl")]
307+
{
308+
shader_defs.push("NO_ARRAY_TEXTURES_SUPPORT".into());
309+
shader_defs.push("SIXTEEN_BYTE_ALIGNMENT".into());
310+
}
311+
312+
shader_defs.push(ShaderDefVal::UInt(
313+
String::from("AVAILABLE_STORAGE_BUFFER_BINDINGS"),
314+
render_device.limits().max_storage_buffers_per_shader_stage,
315+
));
316+
317+
shader_defs
318+
}
319+
313320
type LayoutCacheKey = (Vec<BindGroupLayoutId>, Vec<PushConstantRange>);
321+
314322
#[derive(Default)]
315323
struct LayoutCache {
316324
layouts: HashMap<LayoutCacheKey, ErasedPipelineLayout>,

crates/bevy_sprite/src/mesh2d/mesh.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ pub struct Mesh2dPipeline {
164164
pub mesh_layout: BindGroupLayout,
165165
// This dummy white texture is to be used in place of optional textures
166166
pub dummy_white_gpu_image: GpuImage,
167+
pub shader_defs: Vec<ShaderDefVal>,
167168
}
168169

169170
impl FromWorld for Mesh2dPipeline {
@@ -211,6 +212,10 @@ impl FromWorld for Mesh2dPipeline {
211212
}],
212213
label: Some("mesh2d_layout"),
213214
});
215+
216+
// Shader defs
217+
let shader_defs = platform_shader_defs(&render_device);
218+
214219
// A 1x1x1 'all 1.0' texture to use as a dummy texture to use in place of optional StandardMaterial textures
215220
let dummy_white_gpu_image = {
216221
let image = Image::default();
@@ -256,10 +261,12 @@ impl FromWorld for Mesh2dPipeline {
256261
mip_level_count: image.texture_descriptor.mip_level_count,
257262
}
258263
};
264+
259265
Mesh2dPipeline {
260266
view_layout,
261267
mesh_layout,
262268
dummy_white_gpu_image,
269+
shader_defs,
263270
}
264271
}
265272
}
@@ -362,7 +369,7 @@ impl SpecializedMeshPipeline for Mesh2dPipeline {
362369
key: Self::Key,
363370
layout: &MeshVertexBufferLayout,
364371
) -> Result<RenderPipelineDescriptor, SpecializedMeshPipelineError> {
365-
let mut shader_defs = Vec::new();
372+
let mut shader_defs = self.shader_defs.clone();
366373
let mut vertex_attributes = Vec::new();
367374

368375
if layout.contains(Mesh::ATTRIBUTE_POSITION) {

0 commit comments

Comments
 (0)