wgpu icon indicating copy to clipboard operation
wgpu copied to clipboard

Debug printf in shaders

Open exrook opened this issue 2 years ago • 11 comments

  • [ ] Run cargo clippy.
  • [ ] Run cargo clippy --target wasm32-unknown-unknown if 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

exrook avatar Oct 25 '23 19:10 exrook

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.

exrook avatar Nov 01 '23 12:11 exrook

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.

kvark avatar Nov 09 '23 05:11 kvark

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.

exrook avatar Nov 09 '23 23:11 exrook

Is there anything left to do here besides rebase?

LegNeato avatar Dec 19 '23 02:12 LegNeato

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.

cwfitzgerald avatar Dec 19 '23 05:12 cwfitzgerald

I took the liberty of rebasing the branch on trunk, replacing the merge commit. I hope that's all right.

jimblandy avatar Dec 21 '23 05:12 jimblandy

My rebase seems to have broken something. I'm looking into it.

jimblandy avatar Dec 22 '23 05:12 jimblandy

@exrook friendly poke :)

cwfitzgerald avatar Jan 12 '24 04:01 cwfitzgerald

This looks super helpful!

MiniaczQ avatar Jan 17 '24 00:01 MiniaczQ

I'll update this tonight :)

exrook avatar Jan 24 '24 20:01 exrook

Is this ready for re-review?

MiniaczQ avatar Apr 17 '24 19:04 MiniaczQ