Skip to content

Commit

Permalink
Fix shader_material_glsl example after bevyengine#9254
Browse files Browse the repository at this point in the history
  • Loading branch information
superdump committed Jul 30, 2023
1 parent 04ca832 commit fe41929
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
14 changes: 12 additions & 2 deletions assets/shaders/custom_material.vert
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,23 @@ layout(set = 0, binding = 0) uniform CameraViewProj {
float height;
};

layout(set = 2, binding = 0) uniform Mesh {
struct Mesh {
mat4 Model;
mat4 InverseTransposeModel;
uint flags;
};

#ifdef PER_OBJECT_BUFFER_BATCH_SIZE
layout(set = 2, binding = 0) uniform Mesh Meshes[#{PER_OBJECT_BUFFER_BATCH_SIZE}];
#else
layout(set = 2, binding = 0) readonly buffer _Meshes {
Mesh Meshes[];
};
#endif // PER_OBJECT_BUFFER_BATCH_SIZE

void main() {
v_Uv = Vertex_Uv;
gl_Position = ViewProj * Model * vec4(Vertex_Position, 1.0);
gl_Position = ViewProj
* Meshes[gl_BaseInstance + gl_InstanceIndex].Model
* vec4(Vertex_Position, 1.0);
}
25 changes: 25 additions & 0 deletions crates/bevy_pbr/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,20 @@ pub struct MeshPipeline {
pub dummy_white_gpu_image: GpuImage,
pub clustered_forward_buffer_binding_type: BufferBindingType,
pub mesh_layouts: MeshLayouts,
/// `MeshUniform`s are stored in arrays in buffers. If storage buffers are available, they
/// are used and this will be `None`, otherwise uniform buffers will be used with batches
/// of this many `MeshUniform`s, stored at dynamic offsets within the uniform buffer.
/// Use code like this in custom shaders:
/// ```
/// #ifdef PER_OBJECT_BUFFER_BATCH_SIZE
/// @group(2) @binding(0)
/// var<uniform> mesh: array<Mesh, #{PER_OBJECT_BUFFER_BATCH_SIZE}u>;
/// #else
/// @group(2) @binding(0)
/// var<storage> mesh: array<Mesh>;
/// #endif // PER_OBJECT_BUFFER_BATCH_SIZE
/// ```
pub per_object_buffer_batch_size: Option<u32>,
}

impl FromWorld for MeshPipeline {
Expand Down Expand Up @@ -564,6 +578,7 @@ impl FromWorld for MeshPipeline {
clustered_forward_buffer_binding_type,
dummy_white_gpu_image,
mesh_layouts: MeshLayouts::new(&render_device),
per_object_buffer_batch_size: GpuArrayBuffer::<MeshUniform>::batch_size(&render_device),
}
}
}
Expand Down Expand Up @@ -866,6 +881,16 @@ impl SpecializedMeshPipeline for MeshPipeline {
TextureFormat::bevy_default()
};

// This is defined here so that custom shaders that use something other than
// the mesh binding from bevy_pbr::mesh_bindings can easily make use of this
// in their own shaders.
if let Some(per_object_buffer_batch_size) = self.per_object_buffer_batch_size {
shader_defs.push(ShaderDefVal::UInt(
"PER_OBJECT_BUFFER_BATCH_SIZE".into(),
per_object_buffer_batch_size,
));
}

Ok(RenderPipelineDescriptor {
vertex: VertexState {
shader: MESH_SHADER_HANDLE.typed::<Shader>(),
Expand Down

0 comments on commit fe41929

Please sign in to comment.