gfx-rs / wgpu

A cross-platform, safe, pure-Rust graphics API.
https://wgpu.rs
Apache License 2.0
12.17k stars 893 forks source link

Debuggable Shader #4561

Open wicast opened 1 year ago

wicast commented 1 year ago

SPV

The inital debuggable spv-out is already merged, which can be used with renderdoc. Works fine with WGSL as source code.

But some of the local variable names are missing when compiles a complex shader: debug-symbol-terrain.wgsl and debug-symbol-terrain.spvasm. This needs more investigations.

See spec for more information: https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpLine https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpString

TODOs:

Metal

.metallibsym

.metallibsym is the Metal Companion Symbol file according to Apple doc.

But with the help of https://github.com/YuAo/MetalLibraryArchive

I found that the .metallibsym is acturally a .metallib with debug info + source code archive. They have the same structure. MetalLibraryArchive can unpack .metallibsym directly.

debug symbol

the full metal shader symbol should generate by -gline-tables-only and -frecord-sources as the Xcode shows:

截屏2023-06-30 21 06 16

-gline-tables-only is a clang's parameter and the good news is metal is using llvm's debug info directly.

39791cb6f2ca40dbb25163102de3ac57 6c3d2efee2424cfc87993a6683656913

BTW: The metal shader compiler support lots of clang parameters like -g, -O0, etc.

source code

source code is compressed with Bzip2 and embedded in the .metallib

MetalLibraryArchive can unpack the source code data from .metallib.

截屏2023-06-30 23 53 39

Other metallib reverse engine resource I found: https://worthdoingbadly.com/metalbitcode/

What's missing for generating debuggable .metallib

DX/HLSL ecosystem

TODO Not investigated yet.

teoxoy commented 1 year ago

This is great stuff!


YuAo/MetalLibraryArchive looks a lot more complete than our Wiki page and mtl-info (a cli tool in rust I found a while back).


I bumped into https://github.com/woodruffw/mollusc a few days ago while I was looking for pure rust LLVM-IR crates.


But some of the local variable names are missing when compiles a complex shader: debug-symbol-terrain.wgsl and debug-symbol-terrain.spvasm. This needs more investigations.

The reason for this is that let declarations become 'named expressions' and right now, we only add debug info to statements.


DXIL also makes use of LLVM's debug info (see SourceLevelDebuggingHLSL)