-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Unpack bins belonging to multidrawable batch sets on the GPU instead of on the CPU. #23481
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5f7d576
Unpack bins belonging to multidrawable batch sets on the GPU instead of
pcwalton cfc6b34
Doc check police
pcwalton e6f1579
Doc check police
pcwalton d2f2028
Doc check police again
pcwalton faf0f03
Typo in comment
alice-i-cecile 7a83c2a
Merge remote-tracking branch 'origin/main' into bin-slabs-2
pcwalton 285c064
Address review comments and fix some naming
pcwalton c063b78
Merge remote-tracking branch 'pcwalton/bin-slabs-2' into HEAD
pcwalton 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,85 @@ | ||
| // A compute shader that unpacks bins. | ||
| // | ||
| // This shader runs before mesh preprocessing in order to generate | ||
| // `PreprocessWorkItem`s from cached bins. A single dispatch of this shader | ||
| // corresponds to a single batch set, and one invocation of this shader | ||
| // corresponds to one binned entity. Each shader invocation builds the work item | ||
| // for its entity by copying over the mesh input uniform index and calculating | ||
| // the position of the command in the indirect parameters buffer that will draw | ||
| // that entity. | ||
|
|
||
| #import bevy_pbr::mesh_preprocess_types::PreprocessWorkItem | ||
|
|
||
| // Information needed to unpack bins belonging to a single batch set. | ||
| struct BinUnpackingMetadata { | ||
| // The index of the first `PreprocessWorkItem` that this compute shader | ||
| // dispatch is to write to. | ||
| base_output_work_item_index: u32, | ||
| // The index of the first GPU indirect parameters command for this batch | ||
| // set. | ||
| base_indirect_parameters_index: u32, | ||
| // The number of binned mesh instances in the `binned_mesh_instances` | ||
| // array. | ||
| binned_mesh_instance_count: u32, | ||
| // Padding. | ||
| pad_a: u32, | ||
| // Padding. | ||
| pad_b: array<vec4<u32>, 15>, | ||
| }; | ||
|
|
||
| // One mesh instance in a bin. | ||
| // | ||
| // This corresponds to the CPU-side `GpuRenderBinnedMeshInstance` structure. | ||
| // | ||
| // Note that this structure isn't sorted within the | ||
| // `binned_mesh_instances` buffer. Instances from the same bin aren't | ||
| // guaranteed to be adjacent to one another. | ||
| struct BinnedMeshInstance { | ||
| // The index of the `MeshInputUniform` corresponding to this mesh instance | ||
| // in the `MeshInputUniform` buffer. | ||
| input_uniform_index: u32, | ||
| // The index of the bin that this mesh instance belongs to. | ||
| bin_index: u32, | ||
| }; | ||
|
|
||
| // Metadata for the entire batch set. | ||
| @group(0) @binding(0) var<uniform> bin_unpacking_metadata: BinUnpackingMetadata; | ||
|
|
||
| // The input array of `BinnedMeshInstance`s. | ||
| // | ||
| // Note that this array isn't sorted. | ||
| @group(0) @binding(1) var<storage> binned_mesh_instances: array<BinnedMeshInstance>; | ||
|
|
||
| // The output list of `PreprocessWorkItem`s. | ||
| @group(0) @binding(2) var<storage, read_write> preprocess_work_items: array<PreprocessWorkItem>; | ||
|
|
||
| // A mapping from each `bin_index` to the index of the GPU indirect parameters | ||
| // for this bin, relative to the start of the indirect parameters for this batch | ||
| // set. | ||
| @group(0) @binding(3) var<storage> bin_index_to_indirect_parameters_offset: array<u32>; | ||
|
|
||
| @compute | ||
| @workgroup_size(64) | ||
| fn main(@builtin(global_invocation_id) global_invocation_id: vec3<u32>) { | ||
| // Figure out which instance we're looking at. | ||
| let global_id = global_invocation_id.x; | ||
| if (global_id >= bin_unpacking_metadata.binned_mesh_instance_count) { | ||
| return; | ||
| } | ||
|
|
||
| // Unpack the `BinnedMeshInstance`. | ||
| let input_uniform_index = binned_mesh_instances[global_id].input_uniform_index; | ||
| let bin_index = binned_mesh_instances[global_id].bin_index; | ||
|
|
||
| // Look up the indirect parameters index for this bin, relative to the first | ||
| // indirect parameters offset for this batch set. | ||
| let indirect_parameters_offset = bin_index_to_indirect_parameters_offset[bin_index]; | ||
|
|
||
| // Determine the location we should write the work item to. | ||
| let output_index = bin_unpacking_metadata.base_output_work_item_index + global_id; | ||
|
|
||
| // Write out the resulting work item. | ||
| preprocess_work_items[output_index].input_index = input_uniform_index; | ||
| preprocess_work_items[output_index].output_or_indirect_parameters_index = | ||
| bin_unpacking_metadata.base_indirect_parameters_index + indirect_parameters_offset; | ||
| } | ||
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
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.
why not just
?
Uh oh!
There was an error while loading. Please reload this page.
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.
I originally tried that, but it caused a UBO alignment error. It seems that
wgpuwants UBO types to have 16 byte alignment.