Search across packages, versions, files, declarations, and comments. Use multiple terms to narrow results (AND), and add @vertex, @fragment, or @compute to filter by shader stage.
1#import bevy_pbr::{
2 meshlet_bindings::{
3 meshlet_cluster_meshlet_ids,
4 meshlets,
5 meshlet_cluster_instance_ids,
6 meshlet_instance_uniforms,
7 meshlet_raster_clusters,
8 meshlet_visibility_buffer,
9 view,
10 get_meshlet_triangle_count,
11 get_meshlet_vertex_id,
12 get_meshlet_vertex_position,
13 },
14 mesh_functions::mesh_position_local_to_world,
16#import bevy_render::maths::affine3_to_square
17var<push_constant> meshlet_raster_cluster_rightmost_slot: u32;
19/// Vertex/fragment shader for rasterizing large clusters into a visibility buffer.
21struct VertexOutput {
22 @builtin(position) position: vec4<f32>,
23#ifdef MESHLET_VISIBILITY_BUFFER_RASTER_PASS_OUTPUT
24 @location(0) @interpolate(flat) packed_ids: u32,
25#endif
28@vertex
29fn vertex(@builtin(instance_index) instance_index: u32, @builtin(vertex_index) vertex_index: u32) -> VertexOutput {
30 let cluster_id = meshlet_raster_clusters[meshlet_raster_cluster_rightmost_slot - instance_index];
31 let meshlet_id = meshlet_cluster_meshlet_ids[cluster_id];
32 var meshlet = meshlets[meshlet_id];
34 let triangle_id = vertex_index / 3u;
35 if triangle_id >= get_meshlet_triangle_count(&meshlet) { return dummy_vertex(); }
36 let index_id = (triangle_id * 3u) + (vertex_index % 3u);
37 let vertex_id = get_meshlet_vertex_id(meshlet.start_index_id + index_id);
39 let instance_id = meshlet_cluster_instance_ids[cluster_id];
40 let instance_uniform = meshlet_instance_uniforms[instance_id];
42 let vertex_position = get_meshlet_vertex_position(&meshlet, vertex_id);
43 let world_from_local = affine3_to_square(instance_uniform.world_from_local);
44 let world_position = mesh_position_local_to_world(world_from_local, vec4(vertex_position, 1.0));
45 let clip_position = view.clip_from_world * vec4(world_position.xyz, 1.0);
47 return VertexOutput(
48 clip_position,
49#ifdef MESHLET_VISIBILITY_BUFFER_RASTER_PASS_OUTPUT
50 (cluster_id << 7u) | triangle_id,
51#endif
52 );
55@fragment
56fn fragment(vertex_output: VertexOutput) {
57 let depth = bitcast<u32>(vertex_output.position.z);
58#ifdef MESHLET_VISIBILITY_BUFFER_RASTER_PASS_OUTPUT
59 let visibility = (u64(depth) << 32u) | u64(vertex_output.packed_ids);
60#else
61 let visibility = depth;
62#endif
63 textureAtomicMax(meshlet_visibility_buffer, vec2<u32>(vertex_output.position.xy), visibility);
66fn dummy_vertex() -> VertexOutput {
67 return VertexOutput(
68 vec4(divide(0.0, 0.0)), // NaN vertex position
69#ifdef MESHLET_VISIBILITY_BUFFER_RASTER_PASS_OUTPUT
70 0u,
71#endif
72 );
75// Naga doesn't allow divide by zero literals, but this lets us work around it
76fn divide(a: f32, b: f32) -> f32 {
77 return a / b;
Contents