-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Meshlet BVH Culling #19318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
atlv24
wants to merge
25
commits into
bevyengine:main
Choose a base branch
from
atlv24:bvh-cull-fixed
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Meshlet BVH Culling #19318
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
83483ce
build bvh
SparkyPotato 5911c2c
uploaded meshes and instances
SparkyPotato 66fe2d5
most scaffolding
SparkyPotato a8a84cb
fix shaders somewhat
SparkyPotato df04bc5
something on the screen
SparkyPotato 135202f
some more bugfixes
SparkyPotato 0d87d40
Fix never clearing instance_aabbs, formatting
JMS55 6a59614
fix flickering and bad bvh cull traversal
SparkyPotato 5fd808c
fix frustum culling
SparkyPotato 9113ef6
try to render only lod 0
SparkyPotato b7bba4b
fix meshlet cull not considering all meshlets
SparkyPotato 40b4a65
test monotonicity in builder
SparkyPotato 330a215
merge 2 spheres at a time
SparkyPotato da00224
fix build
SparkyPotato 568c5e8
add occlusion culling
SparkyPotato 3ed3ea0
fix occlusion culling
SparkyPotato cbcf716
feat(meshlet): hot reloading
atlv24 c867182
fix(meshlet): use correct near plane calculation
atlv24 275d655
fix(meshlet): fix occlusion culling and orthographic error bias
atlv24 5370e14
clippy
atlv24 0e143c6
clippy
atlv24 16e83c7
readd sw raster
SparkyPotato 6d1c71f
better orthographic error metric
SparkyPotato 148a319
Merge pull request #1 from SparkyPotato/bvh-cull-fixed
atlv24 829ba6c
feedback
atlv24 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#import bevy_pbr::meshlet_bindings::{ | ||
InstancedOffset, | ||
get_aabb, | ||
get_aabb_error, | ||
get_aabb_child_offset, | ||
constants, | ||
meshlet_bvh_nodes, | ||
meshlet_bvh_cull_count_read, | ||
meshlet_bvh_cull_count_write, | ||
meshlet_bvh_cull_dispatch, | ||
meshlet_bvh_cull_queue, | ||
meshlet_meshlet_cull_count_early, | ||
meshlet_meshlet_cull_count_late, | ||
meshlet_meshlet_cull_dispatch_early, | ||
meshlet_meshlet_cull_dispatch_late, | ||
meshlet_meshlet_cull_queue, | ||
meshlet_second_pass_bvh_count, | ||
meshlet_second_pass_bvh_dispatch, | ||
meshlet_second_pass_bvh_queue, | ||
} | ||
#import bevy_pbr::meshlet_cull_shared::{ | ||
lod_error_is_imperceptible, | ||
aabb_in_frustum, | ||
should_occlusion_cull_aabb, | ||
} | ||
|
||
@compute | ||
@workgroup_size(128, 1, 1) // 8 threads per node | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1 node is handled by 8 threads, so 16 nodes per WG? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup. |
||
fn cull_bvh(@builtin(global_invocation_id) global_invocation_id: vec3<u32>) { | ||
// Calculate the queue ID for this thread | ||
let dispatch_id = global_invocation_id.x; | ||
var node = dispatch_id >> 3u; | ||
let subnode = dispatch_id & 7u; | ||
if node >= meshlet_bvh_cull_count_read { return; } | ||
|
||
node = select(node, constants.rightmost_slot - node, constants.read_from_front == 0u); | ||
let instanced_offset = meshlet_bvh_cull_queue[node]; | ||
let instance_id = instanced_offset.instance_id; | ||
let bvh_node = &meshlet_bvh_nodes[instanced_offset.offset]; | ||
|
||
var aabb_error_offset = (*bvh_node).aabbs[subnode]; | ||
let aabb = get_aabb(&aabb_error_offset); | ||
let parent_error = get_aabb_error(&aabb_error_offset); | ||
let lod_sphere = (*bvh_node).lod_bounds[subnode]; | ||
|
||
let parent_is_imperceptible = lod_error_is_imperceptible(lod_sphere, parent_error, instance_id); | ||
// Error and frustum cull, in both passes | ||
if parent_is_imperceptible || !aabb_in_frustum(aabb, instance_id) { return; } | ||
|
||
let child_offset = get_aabb_child_offset(&aabb_error_offset); | ||
let index = subnode >> 2u; | ||
let bit_offset = subnode & 3u; | ||
let packed_child_count = (*bvh_node).child_counts[index]; | ||
let child_count = extractBits(packed_child_count, bit_offset * 8u, 8u); | ||
var value = InstancedOffset(instance_id, child_offset); | ||
|
||
// If we pass, try occlusion culling | ||
// If this node was occluded, push it's children to the second pass to check against this frame's HZB | ||
if should_occlusion_cull_aabb(aabb, instance_id) { | ||
#ifdef MESHLET_FIRST_CULLING_PASS | ||
if child_count == 255u { | ||
let id = atomicAdd(&meshlet_second_pass_bvh_count, 1u); | ||
meshlet_second_pass_bvh_queue[id] = value; | ||
if ((id & 15u) == 0u) { | ||
atomicAdd(&meshlet_second_pass_bvh_dispatch.x, 1u); | ||
} | ||
} else { | ||
let base = atomicAdd(&meshlet_meshlet_cull_count_late, child_count); | ||
let start = constants.rightmost_slot - base; | ||
for (var i = start; i < start - child_count; i--) { | ||
meshlet_meshlet_cull_queue[i] = value; | ||
value.offset += 1u; | ||
} | ||
let req = (base + child_count + 127u) >> 7u; | ||
atomicMax(&meshlet_meshlet_cull_dispatch_late.x, req); | ||
} | ||
#endif | ||
return; | ||
} | ||
|
||
// If we pass, push the children to the next BVH cull | ||
if child_count == 255u { | ||
let id = atomicAdd(&meshlet_bvh_cull_count_write, 1u); | ||
let index = select(constants.rightmost_slot - id, id, constants.read_from_front == 0u); | ||
meshlet_bvh_cull_queue[index] = value; | ||
if ((id & 15u) == 0u) { | ||
atomicAdd(&meshlet_bvh_cull_dispatch.x, 1u); | ||
} | ||
} else { | ||
#ifdef MESHLET_FIRST_CULLING_PASS | ||
let base = atomicAdd(&meshlet_meshlet_cull_count_early, child_count); | ||
let end = base + child_count; | ||
for (var i = base; i < end; i++) { | ||
meshlet_meshlet_cull_queue[i] = value; | ||
value.offset += 1u; | ||
} | ||
let req = (end + 127u) >> 7u; | ||
atomicMax(&meshlet_meshlet_cull_dispatch_early.x, req); | ||
#else | ||
let base = atomicAdd(&meshlet_meshlet_cull_count_late, child_count); | ||
let start = constants.rightmost_slot - base; | ||
for (var i = start; i < start - child_count; i--) { | ||
meshlet_meshlet_cull_queue[i] = value; | ||
value.offset += 1u; | ||
} | ||
let req = (base + child_count + 127u) >> 7u; | ||
atomicMax(&meshlet_meshlet_cull_dispatch_late.x, req); | ||
#endif | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move the instance abbb + bvh_depth to before the FrameEncoder?