Skip to content

Commit

Permalink
Try to fix occlusion culling (does not seem to be working still)
Browse files Browse the repository at this point in the history
  • Loading branch information
JMS55 committed Jan 18, 2024
1 parent d2903ac commit 2a6e022
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
13 changes: 7 additions & 6 deletions crates/bevy_pbr/src/meshlet/cull_meshlets.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#import bevy_render::maths::affine_to_square

/// Culls individual meshlets (1 per thread) in two passes (two pass occlusion culling), and outputs a bitmask of which meshlets survived.
/// 1. The first pass is only frustum culling, on all meshlets that were visible last frame.
/// 2. The second pass performs both frustum and occlusion culling (using the depth buffer generated from the first pass), on all meshlets that were _not_ visible last frame.
/// 1. The first pass is only frustum culling, on only the meshlets that were visible last frame.
/// 2. The second pass performs both frustum and occlusion culling (using the depth buffer generated from the first pass), on all meshlets.

@compute
@workgroup_size(128, 1, 1)
Expand All @@ -30,12 +30,13 @@ fn cull_meshlets(@builtin(global_invocation_id) thread_id: vec3<u32>) {
let bounding_sphere_center = model * vec4(bounding_sphere.center, 1.0);
let bounding_sphere_radius = model_scale * bounding_sphere.radius;

// In the first pass, operate only on the meshlets visible last frame. In the second pass, operate on the opposite set.
var meshlet_visible = get_meshlet_previous_occlusion(thread_id.x);
// In the first pass, operate only on the meshlets visible last frame. In the second pass, operate on all meshlets.
#ifdef MESHLET_SECOND_CULLING_PASS
meshlet_visible = !meshlet_visible;
#endif
var meshlet_visible = true;
#else
var meshlet_visible = get_meshlet_previous_occlusion(thread_id.x);
if !meshlet_visible { return; }
#endif

// Frustum culling
// TODO: Faster method from https://vkguide.dev/docs/gpudriven/compute_culling/#frustum-culling-function
Expand Down
10 changes: 10 additions & 0 deletions crates/bevy_pbr/src/meshlet/visibility_buffer_raster_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ impl Node for MeshletVisibilityBufferRasterPassNode {
write_index_buffer_pipeline,
write_index_buffer_workgroups,
);
render_context.command_encoder().clear_buffer(
&meshlet_view_resources.occlusion_buffer,
0,
None,
);
raster_pass(
true,
render_context,
Expand Down Expand Up @@ -214,6 +219,11 @@ impl Node for MeshletVisibilityBufferRasterPassNode {
write_index_buffer_pipeline,
write_index_buffer_workgroups,
);
render_context.command_encoder().clear_buffer(
&meshlet_view_resources.occlusion_buffer,
0,
None,
);
raster_pass(
true,
render_context,
Expand Down

0 comments on commit 2a6e022

Please sign in to comment.