Skip to content

Commit f1b18fa

Browse files
committed
Move ShaderCache-sourced shader defs into a resource
1 parent 4f16d6e commit f1b18fa

File tree

6 files changed

+49
-16
lines changed

6 files changed

+49
-16
lines changed

crates/bevy_gizmos/src/pipeline_3d.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl SpecializedMeshPipeline for GizmoPipeline {
4646
(depth_test, key): Self::Key,
4747
layout: &MeshVertexBufferLayout,
4848
) -> Result<RenderPipelineDescriptor, SpecializedMeshPipelineError> {
49-
let mut shader_defs = Vec::new();
49+
let mut shader_defs = self.mesh_pipeline.shader_defs.clone();
5050
shader_defs.push("GIZMO_LINES_3D".into());
5151
shader_defs.push(ShaderDefVal::Int(
5252
"MAX_DIRECTIONAL_LIGHTS".to_string(),

crates/bevy_pbr/src/prepass/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

@@ -215,6 +216,7 @@ impl<M: Material> FromWorld for PrepassPipeline<M> {
215216
},
216217
material_layout: M::bind_group_layout(render_device),
217218
material_pipeline: world.resource::<MaterialPipeline<M>>().clone(),
219+
shader_defs: mesh_pipeline.shader_defs.clone(),
218220
_marker: PhantomData,
219221
}
220222
}
@@ -232,7 +234,7 @@ where
232234
layout: &MeshVertexBufferLayout,
233235
) -> Result<RenderPipelineDescriptor, SpecializedMeshPipelineError> {
234236
let mut bind_group_layout = vec![self.view_layout.clone()];
235-
let mut shader_defs = Vec::new();
237+
let mut shader_defs = self.shader_defs.clone();
236238
let mut vertex_attributes = Vec::new();
237239

238240
// 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: 5 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,16 @@ impl FromWorld for MeshPipeline {
532533
}
533534
};
534535

536+
let shader_defs = world.resource::<BaseShaderDefs>().to_vec();
537+
535538
MeshPipeline {
536539
view_layout,
537540
view_layout_multisampled,
538541
mesh_layout,
539542
skinned_mesh_layout,
540543
clustered_forward_buffer_binding_type,
541544
dummy_white_gpu_image,
545+
shader_defs,
542546
}
543547
}
544548
}
@@ -654,7 +658,7 @@ impl SpecializedMeshPipeline for MeshPipeline {
654658
key: Self::Key,
655659
layout: &MeshVertexBufferLayout,
656660
) -> Result<RenderPipelineDescriptor, SpecializedMeshPipelineError> {
657-
let mut shader_defs = Vec::new();
661+
let mut shader_defs = self.shader_defs.clone();
658662
let mut vertex_attributes = Vec::new();
659663

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

crates/bevy_render/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub use once_cell;
4646
use crate::{
4747
camera::CameraPlugin,
4848
mesh::MeshPlugin,
49-
render_resource::{PipelineCache, Shader, ShaderLoader},
49+
render_resource::{BaseShaderDefs, PipelineCache, Shader, ShaderLoader},
5050
renderer::{render_system, RenderInstance},
5151
settings::WgpuSettings,
5252
view::{ViewPlugin, WindowRenderPlugin},
@@ -242,6 +242,7 @@ impl Plugin for RenderPlugin {
242242
.init_resource::<render_graph::RenderGraph>()
243243
.insert_resource(RenderInstance(instance))
244244
.insert_resource(PipelineCache::new(device.clone()))
245+
.insert_resource(BaseShaderDefs::new(&device))
245246
.insert_resource(device)
246247
.insert_resource(queue)
247248
.insert_resource(render_adapter)

crates/bevy_render/src/render_resource/pipeline_cache.rs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::{
1010
Extract,
1111
};
1212
use bevy_asset::{AssetEvent, Assets, Handle};
13+
use bevy_derive::{Deref, DerefMut};
1314
use bevy_ecs::system::{Res, ResMut};
1415
use bevy_ecs::{event::EventReader, system::Resource};
1516
use bevy_utils::{
@@ -161,6 +162,35 @@ impl ShaderDefVal {
161162
}
162163
}
163164

165+
/// Provides globally-constant shader definitions to pipeline implementors.
166+
///
167+
/// Contains the following definitions:
168+
///
169+
/// - `NO_ARRAY_TEXTURES_SUPPORT`
170+
/// - `SIXTEEN_BYTE_ALIGNMENT`
171+
/// - `AVAILABLE_STORAGE_BUFFER_BINDINGS`
172+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Deref, DerefMut, Resource)]
173+
pub struct BaseShaderDefs(pub Vec<ShaderDefVal>);
174+
175+
impl BaseShaderDefs {
176+
pub fn new(device: &RenderDevice) -> Self {
177+
let mut shader_defs = vec![];
178+
179+
#[cfg(feature = "webgl")]
180+
{
181+
shader_defs.push("NO_ARRAY_TEXTURES_SUPPORT".into());
182+
shader_defs.push("SIXTEEN_BYTE_ALIGNMENT".into());
183+
}
184+
185+
shader_defs.push(ShaderDefVal::UInt(
186+
String::from("AVAILABLE_STORAGE_BUFFER_BINDINGS"),
187+
device.limits().max_storage_buffers_per_shader_stage,
188+
));
189+
190+
BaseShaderDefs(shader_defs)
191+
}
192+
}
193+
164194
impl ShaderCache {
165195
fn get(
166196
&mut self,
@@ -193,17 +223,7 @@ impl ShaderCache {
193223
let module = match data.processed_shaders.entry(shader_defs.to_vec()) {
194224
Entry::Occupied(entry) => entry.into_mut(),
195225
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-
));
226+
let shader_defs = shader_defs.to_vec();
207227

208228
debug!(
209229
"processing shader {:?}, with shader defs {:?}",
@@ -697,6 +717,7 @@ impl PipelineCache {
697717
// shader could not be processed ... retrying won't help
698718
PipelineCacheError::ProcessShaderError(err) => {
699719
error!("failed to process shader: {}", err);
720+
error!("{:#?}", pipeline.descriptor);
700721
continue;
701722
}
702723
PipelineCacheError::AsModuleDescriptorError(err, source) => {

crates/bevy_sprite/src/mesh2d/mesh.rs

Lines changed: 6 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 {
@@ -256,10 +257,14 @@ impl FromWorld for Mesh2dPipeline {
256257
mip_level_count: image.texture_descriptor.mip_level_count,
257258
}
258259
};
260+
261+
let shader_defs = world.resource::<BaseShaderDefs>().to_vec();
262+
259263
Mesh2dPipeline {
260264
view_layout,
261265
mesh_layout,
262266
dummy_white_gpu_image,
267+
shader_defs,
263268
}
264269
}
265270
}
@@ -362,7 +367,7 @@ impl SpecializedMeshPipeline for Mesh2dPipeline {
362367
key: Self::Key,
363368
layout: &MeshVertexBufferLayout,
364369
) -> Result<RenderPipelineDescriptor, SpecializedMeshPipelineError> {
365-
let mut shader_defs = Vec::new();
370+
let mut shader_defs = self.shader_defs.clone();
366371
let mut vertex_attributes = Vec::new();
367372

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

0 commit comments

Comments
 (0)