-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
compute-shader mesh generation example #22296
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
ChristopherBiscardi
wants to merge
16
commits into
bevyengine:main
Choose a base branch
from
ChristopherBiscardi:compute-shader-mesh
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.
+452
−0
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0bc8f1f
compute-shader mesh generation example
ChristopherBiscardi c1ad3bf
mesh_allocator storage buffers
ChristopherBiscardi d1d7d28
fix offsets
ChristopherBiscardi dacedb2
more comments
ChristopherBiscardi 41bfb30
cleanup imports
ChristopherBiscardi 339d8ab
example page
ChristopherBiscardi b06c3be
markdown lint
ChristopherBiscardi 1990f7a
comments
ChristopherBiscardi 44fb8e1
change cube size, move upward
ChristopherBiscardi 3786e2e
move to advanced section
ChristopherBiscardi ed34521
update readme
ChristopherBiscardi 516c76d
dont update every frame
ChristopherBiscardi eb61f0d
add node edge
ChristopherBiscardi 334181c
wait for pipeline to be ready before considering meshes "processed"
ChristopherBiscardi f6916e9
ci clippy
ChristopherBiscardi 3c5acc4
wgsl comment fix
ChristopherBiscardi 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // This shader is used for the compute_mesh example | ||
| // The actual work it does is not important for the example and | ||
| // has been hardcoded to return a cube mesh | ||
|
|
||
| // `vertex_start` is the starting offset of the mesh data in the *vertex_data* storage buffer | ||
| // `index_start` is the starting offset of the index data in the *index_data* storage buffer | ||
| struct DataRanges { | ||
| vertex_start: u32, | ||
| vertex_end: u32, | ||
| index_start: u32, | ||
| index_end: u32, | ||
| } | ||
|
|
||
| @group(0) @binding(0) var<uniform> data_range: DataRanges; | ||
| @group(0) @binding(1) var<storage, read_write> vertex_data: array<f32>; | ||
| @group(0) @binding(2) var<storage, read_write> index_data: array<u32>; | ||
|
|
||
| @compute @workgroup_size(1) | ||
| fn main(@builtin(global_invocation_id) global_id: vec3<u32>) { | ||
| // this loop is iterating over the full list of (position, normal, uv) | ||
| // data what we have in `vertices`. | ||
| // `192` is used because arrayLength on const arrays doesn't work | ||
| for (var i = 0u; i < 192; i++) { | ||
| // The vertex_data buffer is bigger than just the mesh we're | ||
| // processing because Bevy stores meshes in the mesh_allocator | ||
| // which allocates slabs that each can contain multiple meshes. | ||
| // This buffer is one slab, and data_range.vertex_start is the | ||
| // starting offset for the mesh we care about. | ||
| // So the 0 starting value in the for loop is added to | ||
| // data_range.vertex_start which means we start writing at the | ||
| // correct offset. | ||
| // | ||
| // The "end" of the available space to write into is known by us | ||
| // ahead of time in this example, so we know this has enough space, | ||
| // but you may wish to also check to make sure you are not writing | ||
| // past the end of the range *because you should not write past the | ||
| // end of the range ever*. Doing this can overwrite a different | ||
| // mesh's data. | ||
| vertex_data[i + data_range.vertex_start] = vertices[i]; | ||
| } | ||
| // `36` is the length of the `indices` array | ||
| for (var i = 0u; i < 36; i++) { | ||
| // This is doing the same as the vertex_data offset described above | ||
| index_data[i + data_range.index_start] = u32(indices[i]); | ||
| } | ||
| } | ||
|
|
||
| // hardcoded compute shader data. | ||
| // half_size is half the size of the cube | ||
| const half_size = vec3(1.5); | ||
| const min = -half_size; | ||
| const max = half_size; | ||
|
|
||
| // Suppose Y-up right hand, and camera look from +Z to -Z | ||
| const vertices = array( | ||
| // xyz, normal.xyz, uv.xy | ||
| // Front | ||
| min.x, min.y, max.z, 0.0, 0.0, 1.0, 0.0, 0.0, | ||
| max.x, min.y, max.z, 0.0, 0.0, 1.0, 1.0, 0.0, | ||
| max.x, max.y, max.z, 0.0, 0.0, 1.0, 1.0, 1.0, | ||
| min.x, max.y, max.z, 0.0, 0.0, 1.0, 0.0, 1.0, | ||
| // Back | ||
| min.x, max.y, min.z, 0.0, 0.0, -1.0, 1.0, 0.0, | ||
| max.x, max.y, min.z, 0.0, 0.0, -1.0, 0.0, 0.0, | ||
| max.x, min.y, min.z, 0.0, 0.0, -1.0, 0.0, 1.0, | ||
| min.x, min.y, min.z, 0.0, 0.0, -1.0, 1.0, 1.0, | ||
| // Right | ||
| max.x, min.y, min.z, 1.0, 0.0, 0.0, 0.0, 0.0, | ||
| max.x, max.y, min.z, 1.0, 0.0, 0.0, 1.0, 0.0, | ||
| max.x, max.y, max.z, 1.0, 0.0, 0.0, 1.0, 1.0, | ||
| max.x, min.y, max.z, 1.0, 0.0, 0.0, 0.0, 1.0, | ||
| // Left | ||
| min.x, min.y, max.z, -1.0, 0.0, 0.0, 1.0, 0.0, | ||
| min.x, max.y, max.z, -1.0, 0.0, 0.0, 0.0, 0.0, | ||
| min.x, max.y, min.z, -1.0, 0.0, 0.0, 0.0, 1.0, | ||
| min.x, min.y, min.z, -1.0, 0.0, 0.0, 1.0, 1.0, | ||
| // Top | ||
| max.x, max.y, min.z, 0.0, 1.0, 0.0, 1.0, 0.0, | ||
| min.x, max.y, min.z, 0.0, 1.0, 0.0, 0.0, 0.0, | ||
| min.x, max.y, max.z, 0.0, 1.0, 0.0, 0.0, 1.0, | ||
| max.x, max.y, max.z, 0.0, 1.0, 0.0, 1.0, 1.0, | ||
| // Bottom | ||
| max.x, min.y, max.z, 0.0, -1.0, 0.0, 0.0, 0.0, | ||
| min.x, min.y, max.z, 0.0, -1.0, 0.0, 1.0, 0.0, | ||
| min.x, min.y, min.z, 0.0, -1.0, 0.0, 1.0, 1.0, | ||
| max.x, min.y, min.z, 0.0, -1.0, 0.0, 0.0, 1.0 | ||
| ); | ||
|
|
||
| const indices = array( | ||
| 0, 1, 2, 2, 3, 0, // front | ||
| 4, 5, 6, 6, 7, 4, // back | ||
| 8, 9, 10, 10, 11, 8, // right | ||
| 12, 13, 14, 14, 15, 12, // left | ||
| 16, 17, 18, 18, 19, 16, // top | ||
| 20, 21, 22, 22, 23, 20, // bottom | ||
| ); |
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.
would this one work in WebGPU but not WebGL2?
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.
yes, it can't work in webgl2 because it uses a compute shader which isn't available in webgl2.