Debug printf in shaders
- [ ] Run
cargo clippy. - [ ] Run
cargo clippy --target wasm32-unknown-unknownif applicable. - [ ] Add change to CHANGELOG.md. See simple instructions inside file.
Connections Migrated from https://github.com/gfx-rs/naga/pull/2563
Description
Exposes the vulkan validation layers shader printf feature to wgsl shaders through a new debugPrintf builtin function.
example:
@compute @workgroup_size(8,1,1)
fn main(@builtin(local_invocation_index) idx: u32) {
debugPrintf("Hello from %d", idx);
}
output with validation layers enabled and configured properly:
UNASSIGNED-DEBUG-PRINTF(INFO / SPEC): msgNum: -1841738615 - Validation Information: [ UNASSIGNED-DEBUG-PRINTF ] | MessageID = 0x92394c89 | Hello from thread 0!
Objects: 0
UNASSIGNED-DEBUG-PRINTF(INFO / SPEC): msgNum: -1841738615 - Validation Information: [ UNASSIGNED-DEBUG-PRINTF ] | MessageID = 0x92394c89 | Hello from thread 1!
Objects: 0
UNASSIGNED-DEBUG-PRINTF(INFO / SPEC): msgNum: -1841738615 - Validation Information: [ UNASSIGNED-DEBUG-PRINTF ] | MessageID = 0x92394c89 | Hello from thread 2!
Objects: 0
UNASSIGNED-DEBUG-PRINTF(INFO / SPEC): msgNum: -1841738615 - Validation Information: [ UNASSIGNED-DEBUG-PRINTF ] | MessageID = 0x92394c89 | Hello from thread 3!
Objects: 0
UNASSIGNED-DEBUG-PRINTF(INFO / SPEC): msgNum: -1841738615 - Validation Information: [ UNASSIGNED-DEBUG-PRINTF ] | MessageID = 0x92394c89 | Hello from thread 4!
Objects: 0
UNASSIGNED-DEBUG-PRINTF(INFO / SPEC): msgNum: -1841738615 - Validation Information: [ UNASSIGNED-DEBUG-PRINTF ] | MessageID = 0x92394c89 | Hello from thread 5!
Objects: 0
UNASSIGNED-DEBUG-PRINTF(INFO / SPEC): msgNum: -1841738615 - Validation Information: [ UNASSIGNED-DEBUG-PRINTF ] | MessageID = 0x92394c89 | Hello from thread 6!
Objects: 0
UNASSIGNED-DEBUG-PRINTF(INFO / SPEC): msgNum: -1841738615 - Validation Information: [ UNASSIGNED-DEBUG-PRINTF ] | MessageID = 0x92394c89 | Hello from thread 7!
Objects: 0
This feature is also supported on HLSL/DirectX though only when using FXC. DXC does not fully support printf, see https://github.com/microsoft/DirectXShaderCompiler/issues/357.
Additionally supports converting SPIR-V shaders containing NonSemantic.DebugPrintf to wgsl, glsl, hlsl.
Testing TBD
Looking into how the validation layers implement this, there's no reason we couldn't universally support a feature like this in a similar way with a pass on the naga IR + transparently adding a bind group to shader dispatches.
That being said I think this basic validation layer based impl would still be nice to have immediately for users on vulkan platforms.
I believe the minimal functionality in WGSL you'd need in order to support this in user space is just a string type. As soon as you have a string literal to work with, you can have WGSL preprocessor recognizing a construct and writing the format string as well as arguments into some debug buffer.
I believe the minimal functionality in WGSL you'd need in order to support this in user space is just a string type. As soon as you have a string literal to work with, you can have WGSL preprocessor recognizing a construct and writing the format string as well as arguments into some debug buffer.
The intent behind this PR was to make debugging shaders easier for wgpu users, with this PR if I want to inspect a value all I have to do is enable the validation layers and wgpu feature, and add a single line to my shader source.
Even without string support in the language a user can already implement printf-like behavior in their project, as done here for example, but I think it's a much better experience if we can provide built in debugging utilities, so I can debug my shaders without having to make any changes to my project.
Is there anything left to do here besides rebase?
Needs a review from the Naga team, will poke them in our next meeting, but holidays are approaching, so it may be a little bit.
I took the liberty of rebasing the branch on trunk, replacing the merge commit. I hope that's all right.
My rebase seems to have broken something. I'm looking into it.
@exrook friendly poke :)
This looks super helpful!
I'll update this tonight :)
Is this ready for re-review?