xref: /aosp_15_r20/external/swiftshader/docs/VulkanShaderDebugging.md (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
1*03ce13f7SAndroid Build Coastguard Worker# Vulkan Shader Debugging
2*03ce13f7SAndroid Build Coastguard Worker
3*03ce13f7SAndroid Build Coastguard WorkerSwiftShader implements a Vulkan shader debugger that uses the [Debug Adapter Protocol](https://microsoft.github.io/debug-adapter-protocol).
4*03ce13f7SAndroid Build Coastguard Worker
5*03ce13f7SAndroid Build Coastguard WorkerThis debugger is still actively being developed. Please see the [Known Issues](#Known-Issues).
6*03ce13f7SAndroid Build Coastguard Worker
7*03ce13f7SAndroid Build Coastguard Worker# Enabling
8*03ce13f7SAndroid Build Coastguard Worker
9*03ce13f7SAndroid Build Coastguard WorkerTo enable the debugger functionality, SwiftShader needs to be built using the CMake `SWIFTSHADER_ENABLE_VULKAN_DEBUGGER` flag (`-DSWIFTSHADER_ENABLE_VULKAN_DEBUGGER=1`):
10*03ce13f7SAndroid Build Coastguard Worker
11*03ce13f7SAndroid Build Coastguard WorkerOnce SwiftShader is built with the debugger functionality, there are two environment flags that control the runtime behavior:
12*03ce13f7SAndroid Build Coastguard Worker
13*03ce13f7SAndroid Build Coastguard Worker* `VK_DEBUGGER_PORT` - set to an unused port number that will be used to create the DAP localhost socket. If this environment variable is not set, then the debugger functionality will not be enabled.
14*03ce13f7SAndroid Build Coastguard Worker* `VK_WAIT_FOR_DEBUGGER` - if defined, the debugger will block on `vkCreateDevice()` until a debugger connection is established, before allowing `vkCreateDevice()` to return. This allows breakpoints to be set before execution continues.
15*03ce13f7SAndroid Build Coastguard Worker
16*03ce13f7SAndroid Build Coastguard Worker# Connecting using Visual Studio Code
17*03ce13f7SAndroid Build Coastguard Worker
18*03ce13f7SAndroid Build Coastguard WorkerOnce you have built SwiftShader with the debugger functionality enabled, and the `VK_DEBUGGER_PORT` environment variable set, you can connect to the debugger using the following Visual Studio Code `"debugServer"` [Launch Configuration](https://code.visualstudio.com/docs/editor/debugging#_launch-configurations):
19*03ce13f7SAndroid Build Coastguard Worker
20*03ce13f7SAndroid Build Coastguard Worker```json
21*03ce13f7SAndroid Build Coastguard Worker    {
22*03ce13f7SAndroid Build Coastguard Worker        "name": "Vulkan Shader Debugger",
23*03ce13f7SAndroid Build Coastguard Worker        "type": "node",
24*03ce13f7SAndroid Build Coastguard Worker        "request": "launch",
25*03ce13f7SAndroid Build Coastguard Worker        "debugServer": 19020,
26*03ce13f7SAndroid Build Coastguard Worker    }
27*03ce13f7SAndroid Build Coastguard Worker```
28*03ce13f7SAndroid Build Coastguard Worker
29*03ce13f7SAndroid Build Coastguard WorkerNote that the `"type": "node"` field is unused, but is required.
30*03ce13f7SAndroid Build Coastguard Worker
31*03ce13f7SAndroid Build Coastguard Worker[TODO](https://issuetracker.google.com/issues/148373102): Create a Visual Studio Code extension that provides a pre-built SwiftShader driver and debugger type.
32*03ce13f7SAndroid Build Coastguard Worker
33*03ce13f7SAndroid Build Coastguard Worker# Shader entry breakpoints
34*03ce13f7SAndroid Build Coastguard Worker
35*03ce13f7SAndroid Build Coastguard WorkerYou can use the following function breakpoint names to set a breakpoint on the entry to all shaders of the corresponding shader type:
36*03ce13f7SAndroid Build Coastguard Worker* `"VertexShader"`
37*03ce13f7SAndroid Build Coastguard Worker* `"FragmentShader"`
38*03ce13f7SAndroid Build Coastguard Worker* `"ComputeShader"`
39*03ce13f7SAndroid Build Coastguard Worker
40*03ce13f7SAndroid Build Coastguard Worker# High-level Shader debugging
41*03ce13f7SAndroid Build Coastguard Worker
42*03ce13f7SAndroid Build Coastguard WorkerThe debugger, will by default, automatically disassemble the SPIR-V shader code, and provide this as the source for the shader program.
43*03ce13f7SAndroid Build Coastguard Worker
44*03ce13f7SAndroid Build Coastguard WorkerHowever, if the shader program contains [`OpenCL.DebugInfo.100`](https://www.khronos.org/registry/spir-v/specs/unified1/OpenCL.DebugInfo.100.mobile.html) debug info instructions, then the debugger will allow you to debug the high-level shader source (please see [Known Issues](#Known-Issues)).
45*03ce13f7SAndroid Build Coastguard Worker
46*03ce13f7SAndroid Build Coastguard Worker
47*03ce13f7SAndroid Build Coastguard Worker# Known Issues
48*03ce13f7SAndroid Build Coastguard Worker
49*03ce13f7SAndroid Build Coastguard Worker* Currently enabling the debugger dramatically affects performance for all shader invocations. We may want to just-in-time recompile shaders that are actively being debugged to keep the invocations of non-debugged shaders performant. [Tracker bug](https://issuetracker.google.com/issues/148372410)
50*03ce13f7SAndroid Build Coastguard Worker* Support for [`OpenCL.DebugInfo.100`](https://www.khronos.org/registry/spir-v/specs/unified1/OpenCL.DebugInfo.100.mobile.html) is still in early, but active development. Many features are still incomplete.
51*03ce13f7SAndroid Build Coastguard Worker* Shader subgroup invocations are currently presented as a single thread, with each invocation presented as `Lane N` groups in the watch window(s). This approach is still being evaluated, and may be reworked.