Skip to content

Commit 4c8fba0

Browse files
committed
Use a module-associated shader def for PER_OBJECT_BUFFER_BATCH_SIZE
1 parent 8c50fdb commit 4c8fba0

File tree

2 files changed

+23
-31
lines changed

2 files changed

+23
-31
lines changed

crates/bevy_pbr/src/prepass/mod.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ use bevy_render::{
3131
BindGroupLayoutEntry, BindingResource, BindingType, BlendState, BufferBindingType,
3232
ColorTargetState, ColorWrites, CompareFunction, DepthBiasState, DepthStencilState,
3333
DynamicUniformBuffer, FragmentState, FrontFace, GpuArrayBufferIndex, MultisampleState,
34-
PipelineCache, PolygonMode, PrimitiveState, RenderPipelineDescriptor, Shader, ShaderDefVal,
35-
ShaderRef, ShaderStages, ShaderType, SpecializedMeshPipeline, SpecializedMeshPipelineError,
34+
PipelineCache, PolygonMode, PrimitiveState, RenderPipelineDescriptor, Shader, ShaderRef,
35+
ShaderStages, ShaderType, SpecializedMeshPipeline, SpecializedMeshPipelineError,
3636
SpecializedMeshPipelines, StencilFaceState, StencilState, TextureSampleType,
3737
TextureViewDimension, VertexState,
3838
},
@@ -226,7 +226,6 @@ pub struct PrepassPipeline<M: Material> {
226226
pub view_layout_motion_vectors: BindGroupLayout,
227227
pub view_layout_no_motion_vectors: BindGroupLayout,
228228
pub mesh_layouts: MeshLayouts,
229-
pub mesh_buffer_batch_size: Option<u32>,
230229
pub material_layout: BindGroupLayout,
231230
pub material_vertex_shader: Option<Handle<Shader>>,
232231
pub material_fragment_shader: Option<Handle<Shader>>,
@@ -314,7 +313,6 @@ impl<M: Material> FromWorld for PrepassPipeline<M> {
314313
view_layout_motion_vectors,
315314
view_layout_no_motion_vectors,
316315
mesh_layouts: mesh_pipeline.mesh_layouts.clone(),
317-
mesh_buffer_batch_size: mesh_pipeline.per_object_buffer_batch_size,
318316
material_vertex_shader: match M::prepass_vertex_shader() {
319317
ShaderRef::Default => None,
320318
ShaderRef::Handle(handle) => Some(handle),
@@ -354,13 +352,6 @@ where
354352
let mut shader_defs = Vec::new();
355353
let mut vertex_attributes = Vec::new();
356354

357-
if let Some(batch_size) = self.mesh_buffer_batch_size {
358-
shader_defs.push(ShaderDefVal::UInt(
359-
"PER_OBJECT_BUFFER_BATCH_SIZE".into(),
360-
batch_size,
361-
));
362-
}
363-
364355
// NOTE: Eventually, it would be nice to only add this when the shaders are overloaded by the Material.
365356
// The main limitation right now is that bind group order is hardcoded in shaders.
366357
bind_group_layouts.insert(1, self.material_layout.clone());

crates/bevy_pbr/src/render/mesh.rs

+21-20
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,6 @@ impl Plugin for MeshRenderPlugin {
106106
Shader::from_wgsl
107107
);
108108
load_internal_asset!(app, MESH_TYPES_HANDLE, "mesh_types.wgsl", Shader::from_wgsl);
109-
load_internal_asset!(
110-
app,
111-
MESH_BINDINGS_HANDLE,
112-
"mesh_bindings.wgsl",
113-
Shader::from_wgsl
114-
);
115109
load_internal_asset!(
116110
app,
117111
MESH_FUNCTIONS_HANDLE,
@@ -146,9 +140,30 @@ impl Plugin for MeshRenderPlugin {
146140
}
147141

148142
fn finish(&self, app: &mut bevy_app::App) {
143+
let mut mesh_bindings_shader_defs = Vec::with_capacity(1);
144+
149145
if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {
146+
if let Some(per_object_buffer_batch_size) = GpuArrayBuffer::<MeshUniform>::batch_size(
147+
&render_app.world.resource::<RenderDevice>(),
148+
) {
149+
mesh_bindings_shader_defs.push(ShaderDefVal::UInt(
150+
"PER_OBJECT_BUFFER_BATCH_SIZE".into(),
151+
per_object_buffer_batch_size,
152+
));
153+
}
154+
150155
render_app.init_resource::<MeshPipeline>();
151156
}
157+
158+
// Load the mesh_bindings shader module here as it depends on runtime information about
159+
// whether storage buffers are supported, or the maximum uniform buffer binding size.
160+
load_internal_asset!(
161+
app,
162+
MESH_BINDINGS_HANDLE,
163+
"mesh_bindings.wgsl",
164+
Shader::from_wgsl_with_defs,
165+
mesh_bindings_shader_defs
166+
);
152167
}
153168
}
154169

@@ -311,11 +326,6 @@ pub struct MeshPipeline {
311326
pub dummy_white_gpu_image: GpuImage,
312327
pub clustered_forward_buffer_binding_type: BufferBindingType,
313328
pub mesh_layouts: MeshLayouts,
314-
// This defines the batch size of the per-object buffer, for example when on WebGL2 and a
315-
// uniform buffer is used within `GpuArrayBuffer` with a fixed-size array of `MeshUniform`
316-
// in batches. Use `ShaderDefVal::UInt("PER_OBJECT_BUFFER_BATCH_SIZE", <value>)` to configure
317-
// this in shaders.
318-
pub per_object_buffer_batch_size: Option<u32>,
319329
}
320330

321331
impl FromWorld for MeshPipeline {
@@ -555,9 +565,6 @@ impl FromWorld for MeshPipeline {
555565
clustered_forward_buffer_binding_type,
556566
dummy_white_gpu_image,
557567
mesh_layouts: MeshLayouts::new(&render_device),
558-
per_object_buffer_batch_size: GpuArrayBuffer::<MeshUniform>::batch_size(
559-
render_device.as_ref(),
560-
),
561568
}
562569
}
563570
}
@@ -718,12 +725,6 @@ impl SpecializedMeshPipeline for MeshPipeline {
718725
let mut vertex_attributes = Vec::new();
719726

720727
shader_defs.push("VERTEX_OUTPUT_INSTANCE_INDEX".into());
721-
if let Some(batch_size) = self.per_object_buffer_batch_size {
722-
shader_defs.push(ShaderDefVal::UInt(
723-
"PER_OBJECT_BUFFER_BATCH_SIZE".into(),
724-
batch_size,
725-
));
726-
}
727728

728729
if layout.contains(Mesh::ATTRIBUTE_POSITION) {
729730
shader_defs.push("VERTEX_POSITIONS".into());

0 commit comments

Comments
 (0)