Debian Codegraph @

docs/spirv/index.rst

raw
1 SPIR-V Debugging
2 ================
3
4 SPIR-V is a binary shader intermediate representation used extensively by
5 Vulkan applications. Mesa translates SPIR-V shaders into its internal NIR
6 representation using the `spirv_to_nir` function.
7
8 .. _spirv_capture:
9
10 SPIR-V Shader Capture
11 ~~~~~~~~~~~~~~~~~~~~~
12 Set the environment variable `MESA_SPIRV_DUMP_PATH` to a directory where
13 the SPIR-V shaders will be captured. This is useful for debugging purposes,
14 allowing developers to inspect the SPIR-V shaders provided by applications.
15
16 Shader capture filenames are a hexadecimal representation of the shader's
17 BLAKE3 hash. The file extension corresponds to the shader stage, e.g.
18 `.vert` for vertex shaders or `.frag` for fragment shaders.
19
20 It is usually necessary to disable the shader cache to activate the dump logic.
21 This can be done by setting the `MESA_SHADER_CACHE_DISABLE` environment variable to `1`.
22
23 .. code-block:: sh
24
25 MESA_SPIRV_DUMP_PATH=$(pwd) MESA_SHADER_CACHE_DISABLE=1 vkcube
26
27 .. _spirv_replacement:
28
29 SPIR-V Shader Replacement
30 ~~~~~~~~~~~~~~~~~~~~~~~~~
31 SPIR-V shaders can be replaced with alternative SPIR-V shaders
32 during runtime. This is useful for debugging or testing purposes, allowing
33 developers to swap out shaders without modifying the application.
34
35 To enable shader replacement, set the environment variable
36 `MESA_SPIRV_READ_PATH` to the directory containing the replacement SPIR-V shaders.
37 The filenames of the replacement shaders must match the original shader's BLAKE3
38 hash and the extensions must match the original shader stages (e.g., `.vert`, `.frag`).
39
40 It is usually necessary to disable the shader cache to ensure that
41 the replacement shaders are used. This can be done by setting the
42 `MESA_SHADER_CACHE_DISABLE` environment variable to `1`.
43
44 When a shader is replaced, Mesa will log the replacement action, including
45 the original shader's BLAKE3 hash and the filename of the replacement shader.
46 Set `MESA_SPIRV_LOG_LEVEL=info` to see these log messages printed to `stderr`.
47
48 .. code-block:: sh
49
50 MESA_SPIRV_READ_PATH=$(pwd)/replace MESA_SPIRV_LOG_LEVEL=info MESA_SHADER_CACHE_DISABLE=1 vkcube
51
52 To replace a shader with a modified version of a captured shader, you can
53 convert the captured SPIR-V shader to GLSL, edit as needed, and then
54 recompile it back to SPIR-V.
55
56 .. code-block:: sh
57
58 # Capture a SPIR-V shader
59 MESA_SPIRV_DUMP_PATH=$(pwd) MESA_SHADER_CACHE_DISABLE=1 vkcube
60
61 # Convert a captured SPIR-V shader to Vulkan-flavored GLSL
62 # spriv-cross is bundled with the Vulkan SDK and also generally available via package managers
63 spirv-cross -V 0x36c56e0f736238.frag --output 0x36c56e0f736238.frag.glsl
64
65 # <Edit the GLSL file as needed>
66
67 # Recompile the GLSL shader to SPIR-V
68 # Some shaders may require additional flags, such as "--auto-map-locations"
69 glslang -V 0x36c56e0f736238.frag.glsl -o replace/0x36c56e0f736238.frag
70
71 # run the application with the replacement shader
72 MESA_SPIRV_READ_PATH=$(pwd)/replace MESA_SPIRV_LOG_LEVEL=info MESA_SHADER_CACHE_DISABLE=1 vkcube
73