Add compute_workgroups example#801
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a new compute_workgroups.py example to the examples/ suite to help users understand how compute workgroups relate to invocation IDs in wgpu-py, by writing invocation/workgroup identifiers into a storage buffer and printing/verifying the mapping on the CPU.
Changes:
- Add a minimal WGSL compute shader that records
global_invocation_id,local_invocation_id, andworkgroup_id(currently x-components) into a storage buffer. - Add a Python driver that dispatches workgroups, reads back the buffer, prints a table, and validates the expected ID relationships.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| Each thread writes its global, local, and workgroup IDs into a storage buffer | ||
| so the relationship between them can be inspected. |
There was a problem hiding this comment.
The docstring says each thread writes its global/local/workgroup IDs, but the shader only writes the .x component of each builtin (y/z are always 0 with the current 1D dispatch). Consider clarifying in the docstring that this example records the x-components (or expand the buffer to store all 3 components per ID).
| Each thread writes its global, local, and workgroup IDs into a storage buffer | |
| so the relationship between them can be inspected. | |
| Each thread in this 1D dispatch writes the x-components of its global, local, | |
| and workgroup IDs into a storage buffer so the relationship between them can | |
| be inspected. |
| f"Dispatched {workgroups} workgroup(s) of {workgroup_size} thread(s) each {total_threads} threads total.\n" | ||
| ) | ||
|
|
||
| print(f"{'Thread':>6} {'global_id':>9} {'local_id':>8} {'workgroup_id':>12} ") |
There was a problem hiding this comment.
There’s an extra trailing space at the end of the header format string, which results in an extra space in the printed table header. Consider removing it to keep output tidy.
| print(f"{'Thread':>6} {'global_id':>9} {'local_id':>8} {'workgroup_id':>12} ") | |
| print(f"{'Thread':>6} {'global_id':>9} {'local_id':>8} {'workgroup_id':>12}") |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
Hi @Korijn @almarklein, just checking in on this PR. Whenever you get a chance, could you please take a look? Happy to make any changes if needed. Thanks! |
This PR adds a
compute_workgroups.pyexample demonstrating GPU workgroups and invocation IDs.While reviewing the WGPU documentation and the Rust hello_workgroups example, I noticed that wgpu-py does not include a focused example for workgroups.
This example provides a minimal compute shader that writes
global_invocation_id,local_invocation_id, andworkgroup_idinto a storage buffer so the relationship between them can be inspected.