Stabilize VisibilityRange buffer indices to fix stale HLOD culling in the GPU-driven path#24381
Open
stuartparmenter wants to merge 1 commit into
Open
Stabilize VisibilityRange buffer indices to fix stale HLOD culling in the GPU-driven path#24381stuartparmenter wants to merge 1 commit into
stuartparmenter wants to merge 1 commit into
Conversation
… the GPU-driven path
Contributor
|
Welcome, new contributor! Please make sure you've read our contributing guide, as well as our policy regarding AI usage, and we look forward to reviewing your pull request shortly ✨ |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Objective
Continuously-visible meshes using VisibilityRange (HLOD) intermittently vanish or render with the wrong LOD/crossfade when other VisibilityRange entities spawn or despawn, on the GPU-driven mesh path. Forcing NoIndirectDrawing, calling set_changed on the affected Mesh3d, or moving the camera all make it go away.
The cause is a stale index. RenderVisibilityRanges assigns each distinct VisibilityRange value a buffer_index into the GPU visibility_ranges buffer. That index is baked into each mesh's MeshInputUniform flags at extraction (extract_meshes_for_gpu_building), and the GPU preprocessing pass reads visibility_ranges[index] to do the range cull/crossfade. But extract_visibility_ranges calls clear() and rebuilds the whole table — reassigning indices by first-encounter order — whenever any VisibilityRange changes or is removed. Meanwhile the per-mesh flags are only refreshed when that mesh is re-extracted (the Changed<…> filter). So a mesh that didn't change keeps an index that, after a rebuild reorders the buffer, now points at a different range — it gets wrongly culled (vanishes) or wrongly crossfaded. The CPU/direct path rebuilds the flags for all visible meshes every frame, which is why NoIndirectDrawing masks it.
Solution
Make a given VisibilityRange value keep the same buffer index for the lifetime of the app, so a baked index never goes stale:
Indices are dense and never reused, so the buffer grows only with the number of distinct range values ever seen (bounded in practice by the small set of LOD configurations an app uses). No change to the public API or the shader.
Testing
Reproduced in an app that streams chunked terrain (3-tier HLOD via VisibilityRange) in and out during movement on the default GPU-driven path: unchanged, in-frustum chunks would drop out or pop to the wrong LOD as neighbors streamed. With this change the corruption no longer occurs, with indirect drawing left enabled. Confirmed NoIndirectDrawing was the only prior workaround and is no longer needed.