Debian Codegraph @

docs/graphics-debugging/debugging-misrenderings-crashes.rst

raw
1
2 Debugging GPU hangs, faults, and misrenderings
3 ==============================================
4
5 Preface
6 -------
7
8 For most GPU issues you encounter, you can do or think about the steps below, especially when you are unsure where to start. Several of these steps intersect when debugging different issues, and many of their ideas can be generalized.
9
10 There are references to certain debug capabilities that a driver can offer; the driver you are debugging may lack some of these. All capabilities mentioned here are extremely useful, so it's worth spending the time to add these features to your driver to improve the debugging experience.
11
12 For details on how to use specific debug features, refer to the driver's wiki pages.
13
14 Tools used for debugging
15 ^^^^^^^^^^^^^^^^^^^^^^^^
16
17
18 * `RenderDoc <https://github.com/baldurk/renderdoc>`_ - a frame-capture-based graphics debugger. Supports Vulkan, D3D11, D3D12, OpenGL, and OpenGL ES.
19 * `GFXReconstruct <https://github.com/LunarG/gfxreconstruct>`_ - provides tools for the capture and replay of graphics API calls. Supports Vulkan and D3D12.
20 * `apitrace <https://github.com/apitrace/apitrace>`_ - provides tools for the capture and replay of graphics API calls. Supports OpenGL, D3D8, D3D9, and D3D11.
21 * `Amber <https://github.com/google/amber>`_ - Google's shader test framework.
22 * `vkrunner <https://gitlab.freedesktop.org/mesa/vkrunner>`_ - a Vulkan shader tester.
23 * `Piglit <https://gitlab.freedesktop.org/mesa/piglit>`_ - Mesa's test suite that includes shader runner.
24 * `Vulkan-ValidationLayers <https://github.com/KhronosGroup/Vulkan-ValidationLayers>`_ - Vulkan layers that verify the correctness of Vulkan usage.
25
26 Preliminary steps
27 -----------------
28
29 Estimate which component most likely causes the issue
30 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31
32
33 * High chance of app issue:
34
35 * Something someone recently wrote (e.g., translation layer update, new tests, etc.).
36 * Some really weird game/app, especially if it directly uses Vulkan.
37
38 * High chance of driver issue:
39
40 * Well-tested on other GPUs game/app.
41 * Goes through DXVK or Zink translation layers.
42 * Most misrenderings.
43
44 If you suspect that it is an app issue, you should use Khronos Validation Layers. Also, don't forget that certain validation features need to be explicitly enabled, like `GPU-assisted validation <https://vulkan.lunarg.com/doc/view/latest/windows/gpu_validation.html>`_.
45
46 You can also run the app with a ``debugoptimized`` build of the driver (build it with ``--buildtype=debugoptimized``\ ) to see if it triggers any asserts.
47
48 .. note::
49 For any hang that is caused by a ``timeout``\ , you should check if increasing the timeout threshold to a higher value resolves the hang. It maybe possible to indetify whether the hang is caused by a timeout by looking at the dmesg output.
50
51 Driver on-CPU crashes and CPU-side undefined behavior
52 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
53
54 Almost any kind of issue may be caused by undefined behavior on CPU. Functionality without much testing, new code, and explicit CPU-side crashes are mostly the cases when you should be suspicious of undefined behavior.
55
56
57 * Use valgrind for memory issues and threading bugs.
58 * Use ubsan (\ ``-Db_sanitize=undefined`` build option) to find undefined behavior. Though there are some parts of Mesa that produce ubsan errors but work correctly (fixes are still welcome).
59 * ``-Db_sanitize=thread`` to check threading issues.
60 * There are other sanitizers, but they are less useful.
61
62 Did it work on an older driver version?
63 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
64
65 As well as the similar question of "Did it work with an older translation layer?"
66
67 If you know that the app was working on an older driver/translation, or you suspect that it was likely working, it can help to bisect the driver or translation layer to narrow down the cause of the issue.
68
69 You can use `git bisect <https://git-scm.com/docs/git-bisect>`_ with a known good commit hash and a bad commit hash to easily bisect the change history and find the problematic commit(s).
70
71 What kind of issue is it?
72 ^^^^^^^^^^^^^^^^^^^^^^^^^
73
74 Is it a GPU memory access fault? Is it a GPU hang? Is it an unrecoverable GPU hang (GPU doesn't recover, the machine may be completely locked or auto-rebooted)? Is it a misrendering?
75
76 E.g., memory access faults may lead to misrenderings, or hangs may be preceded by a memory access fault.
77
78 Is the issue consistently reproducible? Can you make it 100% reproducible?
79 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
80
81 It's hard to work with random issues, so making the issue as reproducible as possible is a high priority.
82
83 For misrenderings, it's possible that on every run the misrendering presents itself differently. You should try to reproduce the issue such that only one specific misrendering is reproduced every time.
84
85 Tips to make the issue reproducible:
86
87
88 * Make a trace: gfxreconstruct, RenderDoc, apitrace, etc.
89 * Dump submission to the GPU (GPU-specific).
90
91 * If you can capture the raw commands submitted to the gpu and replay them, then that eliminates out a whole pile of ways that API trace capture/replay can affect your issue.
92
93 * Use debug options to reduce variation between runs.
94
95 * Remove any "random" runtime heuristics. E.g., enable ``d3d11.reproducibleCommandStream`` in DXVK.
96 * Write garbage values or zeroes into uninitialized memory/registers (e.g., ``NVK_DEBUG=trash_memory`` in NVK, ``TU_DEBUG_STALE_REGS_RANGE`` in Turnip).
97 * Force synchronizations and cache flushes.
98 * Force synchronizations in shaders.
99 * Investigate other driver- or layer-specific ways you can make the issue reproducible.
100
101 If the issue is intermittent, try to increase its occurrence rate or make it occur more consistently.
102
103 The above tips may prevent the issue from occurring, take that as a possible hint for the cause of the issue.
104
105 Make the issue easier to work with (or how to make a capture)
106 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
107
108 This is connected to reproducibility, but slightly different. For example, if you have a hang happening only when running a game for 10 minutes, it may be 100% reproducible but nearly impossible to debug. It is also possible you know the specific place in a game where there is a misrendering, so the next step is to make the issue more easily reproducible.
109
110
111 * Any API trace is much better than none, though for standalone tests (like dEQP) it may not be necessary.
112 * For misrenderings you ideally need a RenderDoc or Apitrace capture (or anything else that allows inspection of individual draws); for hangs it's also better but not crucial.
113 * Smaller captures are easier to work with; consider reducing texture quality and game resolution. For multi-frame traces, also consider reducing FPS to a low number-MangoHud could be used for this.
114 * For GPU hangs, it is great to capture raw submissions that reproduce the hang when re-submitted.
115
116 .. list-table::
117 :header-rows: 1
118
119 * - Driver
120 - How to get raw submission?
121 * - Turnip/Freedreno
122 - ``FD_RD_DUMP=combine,full``\ , can also be used combined with the ``trigger`` option. Alternatively use ``echo 1 > /sys/module/msm/parameters/rd_full`` + ``/sys/kernel/debug/dri/0/hangrd > submission.rd``
123
124
125 Making a trace (especially a RenderDoc one) that reproduces the issue may be tricky.
126
127 Common issues
128 ~~~~~~~~~~~~~
129
130
131 * Unsupported feature; something goes wrong when capturing:
132
133 * Make sure you are using AT LEAST the latest release (look at the tool's git, not at the distro's package) of the debug tool.
134
135 * I cannot even make a trace; the GPU crashes! See the "Can you prevent the issue from happening?" section.
136
137 * If you manage to make a trace, now make sure it reproduces the same GPU crash as the app.
138
139 * Rendering issue disappears when captured by RenderDoc or other API tracing tooling:
140
141 * Mostly happens with RenderDoc. Caused by additional synchronizations.
142 * If it happens with RenderDoc, in most cases it points to synchronization issues between commands on GPU. Try driver-specific debug options that affect synchronization.
143 * In other cases, it can be that replaying the trace doesn't fully utilize the GPU or changes something in undefined behavior that caused the issue.
144
145 * Trace is not replayable on another GPU due to feature mismatch:
146
147 * For Vulkan, you can use Vulkan-Profiles to restrict features to the intersection of GPU features (see ``gen_profiles_file.py`` from Vulkan-Profiles).
148 * For ``gfxreconstruct``\ , you likely want to replay traces with ``--sfa --swapchain virtual -m rebind --remove-unsupported``.
149 * RenderDoc also wants compatible memory requirements for images/buffers. To achieve that, you can hack the driver and increase the alignment/size requirements while making a capture. Generic solution TBD.
150
151 * Making a trace is too slow (for multi-frame ones):
152
153 * For gfxreconstruct, you may want to change the memory tracking mode (see ``GFXRECON_MEMORY_TRACKING_MODE``\ ).
154 * Start tracing closer to the issue.
155 * Reduce game quality, especially textures; reduce resolution.
156 * Consider making the trace on another faster machine; any difference in GPU features can be mitigated via Vulkan-Profiles (you'd need to intersect GPU capabilities).
157
158 RenderDoc
159 ~~~~~~~~~
160
161 Supports D3D11, D3D12, Vulkan, OpenGL.
162
163 D3D11/D3D12:
164
165
166 * Launching the app from the RenderDoc UI and triggering the capture
167 * `Global Process Hook <https://renderdoc.org/docs/window/capture_attach.html#global-process-hook>`_ - in cases when you cannot launch the app via RenderDoc's UI, e.g., when the app starts through some other executable.
168
169 Vulkan:
170
171
172 * Use ``ENABLE_VULKAN_RENDERDOC_CAPTURE=1`` envvar to enable capturing for the app; it doesn't require the RenderDoc GUI to be launched.
173
174 * If you use a custom RenderDoc build or have downloaded it, you should install the layer from it with ``renderdoccmd vulkanlayer --register --system``
175
176 * Specifying capturing options is available only from the GUI.
177 * If the application was started with the RenderDoc layer enabled (you'd see a frame counter at the top left), you can attach to the application from the GUI: "File" -> "Attach to Running Instance".
178
179 Common issues:
180
181
182 * RenderDoc fails to capture the frame (takes too long, OOMs):
183
184 * Reduce game quality, especially textures; reduce resolution.
185 * Make a capture from a multi-frame trace (uses less memory than full game).
186 * Making the capture on another GPU may not be easy due to memory requirements mismatch, which is not mitigated by Vulkan-Profiles.
187
188 * Hard to catch a specific frame with RenderDoc:
189
190 * Make a multi-frame trace with, e.g., gfxreconstruct, then make a RenderDoc capture of the specific frame.
191
192 * RenderDoc cannot capture from gfxreconstruct due to extensions mismatch:
193
194 * Use Vulkan-Profiles to restrict used features while capturing the gfxreconstruct trace. The Vulkan profile can be obtained by running ``ENABLE_VULKAN_RENDERDOC_CAPTURE=1 vulkaninfo -j=0``
195
196 * Capture portability between different GPUs and driver versions:
197
198 * D3D11 and OpenGL captures are mostly portable between GPUs/drivers.
199 * Vulkan captures are not; they require at least Vulkan Profiles and increased memory requirements for images/buffers during the capture.
200 * D3D12 captures are not portable.
201
202 * A capture may have the issue baked in, e.g., if the previous frame wrote some wrong value into a buffer and RenderDoc captured it.
203 * RenderDoc may support fewer features than the underlying driver, so RenderDoc may mask some of them. This means that what you see in the capture may not be what really runs without RenderDoc attached. This is most relevant to Vulkan.
204
205 GFXReconstruct
206 ~~~~~~~~~~~~~~
207
208 Supports Vulkan and D3D12.
209
210
211 * `Tracing Vulkan <https://github.com/LunarG/gfxreconstruct/blob/dev/USAGE_desktop_Vulkan.md>`_
212 * `Tracing D3D12 <https://github.com/LunarG/gfxreconstruct/blob/dev/USAGE_desktop_D3D12.md>`_
213 * `Tracing on Android <https://github.com/LunarG/gfxreconstruct/blob/dev/USAGE_android.md>`_
214
215 Tips:
216
217
218 * The default memory tracking mode may conflict with crash detection libraries; consider ``userfaultfd`` mode in such cases.
219 * ``GFXRECON_CAPTURE_FILE_FLUSH=true`` may be needed to capture a GPU hang.
220 * ``GFXRECON_CAPTURE_TRIGGER=`` can be used to start tracing at the moment you want; the same mechanism is used for trimming the trace.
221 * If the replaying environment is different from the capturing environment, you may want to add the following options when replaying:
222
223 * For Vulkan: ``--sfa -m rebind --remove-unsupported --wsi xlib --deduplicate-device``
224
225 * ``gfxrecon-optimize`` can be used to reduce the trace size.
226
227 apitrace
228 ~~~~~~~~
229
230 Supports D3D8, D3D9, D3D10, D3D11, OpenGL.
231
232
233 * Tracing D3D: https://github.com/doitsujin/dxvk/wiki/Using-Apitrace
234 * Tracing OpenGL: ``apitrace trace --api /path/to/application [args...]``
235
236 * For more complex cases, see https://github.com/apitrace/apitrace/blob/master/docs/USAGE.markdown
237
238 Tips:
239
240
241 * qapitrace allows inspecting individual draw calls, but to a lesser extent than RenderDoc.
242
243 * Allows editing shader sources, disabling API calls, and inspecting state.
244 * Sometimes inspecting the call crashes it.
245
246 * apitrace has some trimming support, but each API has its own limitations, and some trimmers aren't merged.
247
248 Can you prevent the issue from happening?
249 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
250
251 Especially for hangs, it may be important to find a way to prevent them in order to make a capture. Or, in the general case, it is useful to narrow down the issue.
252
253 For hangs, preventing them would make it possible to make a trace.
254
255
256 * Driver debug options are your friends here (search sources for up to date debug options):
257
258 .. list-table::
259 :header-rows: 1
260
261 * - Driver
262 - DBG Options
263 * - RADV
264 - :envvar:`RADV_DEBUG`, :envvar:`ACO_DEBUG`
265 * - RadeonSI
266 - :envvar:`AMD_DEBUG`
267 * - ANV
268 - :envvar:`ANV_DEBUG`, :envvar:`INTEL_DEBUG`
269 * - Iris
270 - :envvar:`INTEL_DEBUG`
271 * - NVK
272 - :envvar:`NVK_DEBUG`, :envvar:`NAK_DEBUG`
273 * - Turnip
274 - :envvar:`TU_DEBUG`, :envvar:`IR3_SHADER_DEBUG`
275 * - Freedreno
276 - :envvar:`FD_MESA_DEBUG`, :envvar:`IR3_SHADER_DEBUG`
277 * - HK
278 - :envvar:`ASAHI_MESA_DEBUG`, :envvar:`AGX_MESA_DEBUG`
279 * - PANVK
280 - ``PANVK_DEBUG``
281 * - V3DV, V3D
282 - ``V3D_DEBUG``
283 * - PVR
284 - :envvar:`PVR_DEBUG`, :envvar:`ROGUE_DEBUG`
285 * - DXVK (non-Mesa)
286 - ``DXVK_CONFIG``
287
288
289
290 * If debug options don't help, modify the driver:
291
292 * Maybe disable some shader optimizations if you suspect they cause the issue.
293 * Maybe try disabling certain draw calls or dispatches.
294
295 How to debug?
296 -------------
297
298 Generic
299 ^^^^^^^
300
301
302 * Bisect if it is a regression.
303 * If you have some guess about the area of the issue:
304
305 * Add more asserts around the place you suspect.
306 * Disable various optimizations to narrow down the issue.
307
308 Best case - bisection helped
309 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
310
311 In many cases it will be apparent what's wrong with the commit; however, it's far from always.
312
313
314 * You can compare decoded submissions **before** and **after** the bisected commit.
315
316 * It may be worth reducing changes in the bisected commit to narrow down the issue.
317
318 * Otherwise, the bisected commit gives you a better idea regarding the cause of the issue, and you'll go faster through the steps described below.
319
320 .. warning::
321 The bisected commit may expose rather than cause the issue; it may just create an environment suitable for an error from another place to appear. For example, improving performance may cause a synchronization issue to become visible.
322
323 Good case - disabling some shader optimization helped
324 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
325
326 If it's NIR optimization, use ``nir_shader_bisect.py`` to bisect the shader that caused the issue. Having a shader and an optimization that broke should be enough to find the cause of the failure.
327
328 If it's a backend optimization, use backend-specific bisection tooling. (For example, ``ir3_shader_bisect.py`` for ir3.)
329
330 Once you have a specific optimization that causes an issue in a specific shader, simply ``diff``\ ing the shader assembly with an without the optimization might give you a clue of what the issue could be. Then using a shader override tool could be used to gradually bring the faulting shader closer to the working one, until the problem disappears.
331
332 .. list-table::
333 :header-rows: 1
334
335 * - Driver
336 - Debug Option
337 * - SPIR-V level replacement
338 - ``MESA_SPIRV_READ_PATH`` - :ref:`spirv_replacement`
339 * - GLSL level replacement
340 - ``MESA_SHADER_DUMP_PATH``, ``MESA_SHADER_READ_PATH`` - :ref:`replacement`
341 * - Turnip, Freedreno
342 - ``IR3_SHADER_OVERRIDE_PATH``
343 * - ANV, Iris
344 - ``INTEL_SHADER_ASM_READ_PATH``
345
346
347 To help with ``diff``\ ing, it can be useful to make sure codegen changes in some part of the shader don't have a huge impact on unrelated parts. This is obviously difficult in general, but if it is possible, it really pays off to make the compiler output as deterministic as possible. For example, IR3 used to have a dependency between register allocation of the preamble and the main shader, causing a change in preamble codegen to often have a huge impact on the (independent) main shader as well. Making sure this doesn't happen helped a lot in diffing.
348
349 Good case - some debug option helped
350 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
351
352
353 * Try to narrow the cases when the debug option is applied with the goal of finding the specific call.
354
355 * For shader debug options, it could be useful to add assembly macros together with shader overrides. For example, ir3 has ``@fullsync{start,end}`` to force full synchronization for a section of shader assembly.
356
357 * If you have a trace of the issue, you can try disabling the debug option before/after specific draw/dispatch/blit and bisect the command that way.
358
359 Good case - stale register values
360 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
361
362 Stale register values, or even stale memory, are not rare issues, and they tend to cause random misrenderings or GPU crashes. A simple way to flush them out is to write predictable values right before they are expected to be changed.
363
364 For registers there are several natural points to do so:
365
366
367 * At the start of a command buffer, since no state is expected to carry on from previous ones.
368 * Before renderpasses, which have a bit of state setup that can go wrong. While the Vulkan spec allows binding a pipeline outside of the renderpass, no one does that, so it should be OK to overwrite all pipeline-related registers just before a renderpass starts.
369 * Before blits and other commands that are expected to be unaffected by other state.
370
371 It's also useful to have a debug option to always invalidate all state before each draw and other commands.
372
373 .. list-table::
374 :header-rows: 1
375
376 * - Driver
377 - Debug Option
378 * - Turnip
379 - ``TU_DEBUG_STALE_REGS_RANGE`` + ``TU_DEBUG_STALE_REGS_FLAGS``
380
381
382 Misrenderings
383 ^^^^^^^^^^^^^
384
385 Ideally you need to find the exact command causing the misrendering. The easiest way to do that is to use a debug tool that allows you to inspect individual draw calls (e.g., RenderDoc, qapitrace).
386
387 Blog posts with step-by-step examples of how to debug misrenderings:
388
389
390 * https://blogs.igalia.com/dpiliaiev/turnips-in-the-wild-part-1/ - debugging TauCeti Benchmark and Genshin Impact
391 * https://blogs.igalia.com/dpiliaiev/turnips-in-the-wild-part-2/ - debugging Genshin Impact
392 * https://blogs.igalia.com/dpiliaiev/turnips-in-the-wild-part-3/ - debugging Psychonauts 2 and Injustice 2
393 * https://www.supergoodcode.com/the-doctor-is-in/ - debugging Portal 2
394 * https://media.ccc.de/v/xdc2021-6-dissecting_and_fixing_vulkan_rendering_issues_in_drivers_with_renderdoc - a presentation with video examples on how to use RenderDoc to investigate rendering issues.
395
396 They are likely to be helpful in contextualizing the steps described below.
397
398 Good case - you can inspect individual draws
399 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
400
401 Trace back from the final image to the exact draw call that caused the issue.
402
403 For games, most of the frame is image post-processing and copying data from one image to another with small tweaks. For RenderDoc, finding the draw call may look like this:
404
405
406 * Go to the last draw and make sure the issue is present.
407 * Go to the "Inputs" tab, find the image with the issue.
408 * Right-click it and go to the commands that last updated this image.
409 * Repeat a few times.
410
411
412 .. image:: rdc-resource-usage-history.jpg
413 :alt: Example of the context menu with all call where resource is used.
414
415
416 After a few loops you'll land on a renderpass that likely caused the issue; then you can go through the draw calls and find which one went wrong. The same applies to dispatches or other commands.
417
418 A faster way to go through draw calls is using RenderDoc's pixel history (https://renderdoc.org/docs/how/how_inspect_pixel.html#pixel-history):
419
420
421 .. image:: rdc-pixel-history.jpg
422 :alt: Example of the pixel history panel.
423
424
425 I'm not sure if what I see is the issue or not?
426 """""""""""""""""""""""""""""""""""""""""""""""
427
428 Sometimes you look at the image and you're not sure if there is an issue; at best it may look like an abstract painting.
429
430
431 .. image:: rdc-abstractionist-rendering.jpg
432 :alt: Example of an abstractionist rendering.
433
434
435 In cases like this it can be valuable to have a capture of the same frame made on another driver that does not have the misrendering, so you can cross-check the resulting image and see if the abstract art is intended.
436
437 I think I found the command?
438 """"""""""""""""""""""""""""
439
440 Ideally you should compare the inputs that go into this command with a capture opened on another driver known to work. For very complex draws or dispatches, it can be nearly impossible to compare inputs between different GPUs.
441
442
443 * If the game doesn't use compute for everything and the inputs into the draw call look correct, you likely found the bad draw call.
444 * If the game heavily relies on compute to fill vertex buffers and many other resources, it may be hard to tell.
445
446 While investigating the rendering command further, watch for the possibility that inputs are wrong.
447
448 Misrendering happens in random draw calls!
449 """"""""""""""""""""""""""""""""""""""""""
450
451 Try everything to make it stable first. If it's still random, there are a few tricks to try.
452
453
454 * You can use a Python script for RenderDoc to find the first flaky call: https://github.com/werman/rdc-flaky-draw-call-finder/
455 * If it's not too random and you're able to find the draw call that fails fairly regularly, inspect it.
456
457 I definitely found the problematic command!
458 """""""""""""""""""""""""""""""""""""""""""
459
460 Great. Now it's time to determine if the issue is in the draw state or in some shader stage (and which one).
461
462 In many cases it can be self-evident, like if there are issues with depth testing-it's worth looking at depth/stencil state.
463
464 Take a look at the "Rasterizer" and "FB" pipeline states in RenderDoc. Do they have some weird combination of states? That can be a clue.
465
466 If it's not self-evident, at least take a brief look at the raw draw state set for this draw.
467
468 Good case - you found the draw call and the issue is in the draw state
469 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
470
471
472 * Many issues with draw state are caused by stale values, so make sure everything is re-submitted for the draw call or renderpass.
473 * Look carefully through draw state for anything standing out.
474 * Try to modify suspicious draw state for that draw call.
475 * For complex cases you may need to make a standalone reproducer of the issue.
476
477 Good case - you found the draw call and the issue is with shaders
478 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
479
480 Here, having a capture of the same frame opened on another GPU that doesn't reproduce the misrendering may help a lot.
481
482 Which stage is it?
483 """"""""""""""""""
484
485
486 * Vertex explosion - the issue is in VS/TCS/TESS/GS stages.
487 * Some pixels rendered wrong - likely FS.
488 * Texture is misplaced - UVs could have gone wrong at any stage.
489
490 To be more certain, open the mesh inspection panel and eyeball the outputs. Do they look reasonable? Compare them to the outputs you see in the same frame opened on another GPU, if available.
491
492 You found the stage!
493 """"""""""""""""""""
494
495 Even if you guessed the stage wrong, no big deal; during the next steps you'll find out anyway.
496
497
498 * Eyeball the decompiled-to-GLSL shader. Does anything look funny?
499 * Eyeball the assembly.
500 * Use shader debug printf, which is supported by RenderDoc, to inspect values. Keep in mind that modifying a shader can modify the compilation result:
501
502 * `Printf in a graphics debugger <https://docs.vulkan.org/samples/latest/samples/extensions/shader_debugprintf/README.html#_printf_in_a_graphics_debugger>`_
503 * `Shader Messages <https://renderdoc.org/docs/window/shader_messages.html>`_
504
505 * Nothing? It's time to bisect the shader. Ideally you'd want to have the same frame opened on another driver/GPU where there is no issue.
506
507 * Works best with fragment shaders, but can be used with others.
508 * The core idea is to remove parts of the shader, using the color attachment (or varyings or printf) to visualize the variable you want to output.
509 * If you have the same capture opened on another device without the issue, you can paste the reduced shader there and compare results.
510 * If you cannot compare with the correct shader, rely more on educated guesses. Try visualizing some coords used for texturing, texturing results, some inputs, etc. Look for clearly wrong values, like NaNs. It's also worth checking UBOs, push constants, and varyings for correctness.
511
512 * Found something but not sure what's going on? For example, the NIR looks fine and the assembly seems fine, but you see that specific instruction(s) cause issues.
513
514 * Use assembly-replacement tooling in your backend (e.g., ``IR3_SHADER_OVERRIDE_PATH``\ , ``INTEL_SHADER_ASM_READ_PATH``\ , etc.) to make changes directly in the assembly and see via color what's wrong with the instruction.
515
516 Still not clear what's going on?
517 """"""""""""""""""""""""""""""""
518
519 You may want to make a standalone reproducer of this draw call. `Amber <https://github.com/google/amber>`_ test framework may be helpful here. It takes a bit of time to set up the draw call, but it's easier than writing Vulkan code by hand. As an alternative there is also `vkrunner <https://gitlab.freedesktop.org/mesa/vkrunner>`_.
520
521 Bad case - you cannot inspect individual draw calls
522 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
523
524
525 * Go back to attempting to bisect the driver or translation layers if things worked before.
526 * Test with debug builds of the driver and translation layers.
527 * Try all debug features from the driver and translation layers.
528 * Disable various optimizations in the shader compiler and packet emission.
529 * Try again with validation layers.
530
531 All these strategies don't need to completely resolve the misrendering. If something you do changes how the misrendering presents itself, that can be a strong signal for the cause.
532
533 Still nothing? Try to narrow the issue to a specific draw call. This strategy only works with draw calls:
534
535
536 * Bisect draw calls via their parameters, e.g., if ``vertexCount > 10000`` then return. This way you may be able to narrow things down to a single draw call and then find it in the command dump.
537 * It's much easier with traces, but also possible in the game itself-just make it possible to change the condition at runtime.
538
539 Still nothing? Are you sure you haven't missed checking anything? Maybe go back and fix issues preventing you from making a single-frame capture, add more debug flags to the driver, or remove more optimizations.
540
541 Terrible case - you cannot even get a replayable trace of the issue
542 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
543
544 Similar to the above case, but more difficult. It may make sense to have things like "disable draw call if ``vertexCount > 10000``\ " configurable at runtime, so you can change the behavior when the misrendering becomes visible. This way you should hopefully be able to identify the draw call inside submission dumps.
545
546 GPU crash
547 ^^^^^^^^^
548
549 As with misrenderings, you'd want to find the exact command that causes a GPU hang. Most tips for debugging misrenderings also apply to debugging hangs, with one big caveat: RenderDoc doesn't handle hangs well.
550
551 What kind of crash is it?
552 ~~~~~~~~~~~~~~~~~~~~~~~~~
553
554 All kernel drivers print some basic info about GPU hangs and faults to ``dmesg``. The verbosity and accuracy may vary. At a very high level, GPU hangs can be categorized as:
555
556
557 * "HW block got invalid state and reported error" - there is a high chance it would be explicitly reported in dmesg;
558 * "HW block got wedged due to a bad state and then got hang-checked";
559 * "HW block just took too long to complete the workload and got hang-checked."
560
561 In many cases, it won't be possible to tell from the reported GPU crash what exactly happened. Most likely, the HW gets wedged and then hang-checked regardless of the underlying cause.
562
563 And from another perspective, there are:
564
565
566 * **"Recoverable GPU crashes"** - after the crash happens, the kernel kills the offending workload, resets the GPU context, and other workloads run as before.
567 * **"GPU is unrecoverable"** - the kernel, for some reason, cannot correctly reset the GPU, so it won't run any new workload.
568 * **"System is unrecoverable"** - not only can the GPU not be recovered, but nothing else works; the system is inaccessible and, in many cases, triggers an auto-reboot. Mostly happens to iGPUs.
569
570 While the GPU shouldn't crash in the first place (the user-space driver should be fixed), GPU recovery in the kernel should function correctly, and if it doesn't, an issue should be opened. In very rare cases, especially in "System is unrecoverable," the GPU crash can happen due to faulty firmware or even HW bugs.
571
572 .. warning::
573 Not all GPU crashes are caused by the driver; applications not adhering to the specification or having undefined behavior can also cause a GPU to crash.
574
575 Get GPU devcoredump of the crash
576 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
577
578 If your kernel driver generates a GPU coredump on GPU crash, get it and decode it. In a perfect world, a GPU devcoredump could tell exactly where the issue happened, but in practice GPUs are highly parallel devices with many queues and HW blocks that can be wedged, so in many cases a coredump would point somewhere after the command that actually crashed the GPU. However, it also may work, or at least give you an idea about the range of commands.
579
580 On the other hand, if you know what you are doing, devcoredumps can give you a lot of info about the issue, but they may require a lot of digging.
581
582 Get several devcoredumps, compare the crash sites, and if it always points to the same sites, that's a strong signal.
583
584 .. list-table::
585 :header-rows: 1
586
587 * - Driver
588 - Docs
589 * - Turnip, Freedreno
590 - :ref:`freedreno-gpu-devcoredump`
591 * - RADV
592 - :ref:`radv-debug-hang`
593
594
595 Maybe it's possible to find the crashing command in RenderDoc?
596 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
597
598 If debug options, disabling optimizations, or disabling specific draws/dispatches/blits helped prevent the GPU crash, it should be possible to make a capture. Then it should be possible to toggle debug options or commands at runtime so that the capture opens without the crash, but afterward the workaround is disabled and it's possible to go from the start of the capture forward to find the command that crashes.
599
600 Even if the above doesn't yield the crashing command, it's still useful to have a capture to create a reproducer later when you find the command via other means.
601
602 Get a replayable raw submission that crashes the GPU
603 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
604
605 This may not be useful for small workloads that always reproduce the issue, like a specific dEQP test. It can be especially useful for a randomly reproduced crash when playing a game.
606
607 All this depends on whether it's possible to dump the raw submission sent to the GPU with all the GPU memory and be able to replay it.
608
609 The dumped submission doesn't or only randomly reproduces the crash
610 """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
611
612 This is highly likely due to synchronization issues either in the command processor or in shaders.
613
614 GPU crash doesn't always happen, but I got a crashing submission
615 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
616
617 If running the original app doesn't always lead to a GPU crash, but the replayed submission always does, it could indicate a few different things:
618
619
620 * The app produces different commands on each run due to different inputs or other randomness.
621 * If the app is expected to produce the same commands (e.g., CTS or a trace), this may mean the driver produces different commands on each run, which can be the result of undefined behavior.
622
623 * Capture submissions when the app doesn't crash the GPU and when it does; decode and compare submissions. If there are differences other than GPU addresses, they are the most likely candidates.
624
625 You may be lucky and have access to a GPU simulator, or some verifier, or debug bus
626 """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
627
628 Using a GPU simulator is the most surefire way to find the GPU crash cause, but it may be prohibitively slow or hard to access. In such a case, it may still be useful to follow the described steps and at least reduce the workload that will run in the simulator.
629
630 Making changes to the submission dump to bisect the cause
631 """""""""""""""""""""""""""""""""""""""""""""""""""""""""
632
633 If there is a capability to replay submission dumps and to decode them, the next logical step is to be able to edit them by decoding dumps into compilable code, including editable shader assembly. This allows you to rapidly test theories on the cause of a GPU crash and to bisect the crashing command.
634
635 There are two caveats with this feature:
636
637
638 * Self-modifying commands may pose a challenge to editing, e.g., if the driver has to patch some GPU addresses into the command buffer by doing a memory write on GPU.
639 * The GPU shader assembly compiler has to be in good shape to be able to compile all the assembly back. (Though even without it, submission editing is useful.)
640
641 .. list-table::
642 :header-rows: 1
643
644 * - Driver
645 - How
646 * - Turnip
647 - :ref:`rddecompiler <freedreno-editing-command-stream>`
648
649
650 How to dump the crashing submission?
651 """"""""""""""""""""""""""""""""""""
652
653 Mesa or the kernel driver may have the ability to dump raw command streams sent to the GPU plus the memory state at that time. The kernel may be able to dump the crashing command stream with memory at the moment of the crash, or it may be able to dump every submission with the memory. Mesa may also be able to dump every submission with the memory.
654
655 Dumping the crashing command stream from the kernel may be the easiest way to get a reproducible GPU crash.
656
657 .. warning::
658 GPU memory in a crash dump reflects the memory at the crash time, which may lead to divergence of command stream execution from the original.
659
660 Dumping every raw command stream takes much more space and time but can give the exact copy of the command stream that crashed the GPU.
661
662 Debugging unrecoverable GPU crashes
663 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
664
665 This section is most useful for the "System is unrecoverable" case but can occasionally be useful for less severe ones.
666
667 Fortunately, on average, it is easier to reproduce than other cases. So try the following things first:
668
669
670 * Try to bisect the workload itself: reduce the test case, exit earlier when replaying a trace, etc.
671 * Do the above inside the driver: exit at a specific draw/dispatch/blit/etc.
672
673 Now, the above advice may not have helped or may take too much time. There is another way to find the crashing command, with assistance from the driver.
674
675 It's a common GPU debugging tactic to sprinkle breadcrumbs in the command buffers to find the last one executed after a GPU crash. Though, in this case, one cannot inspect anything after the GPU crash, so what if the GPU is synchronized with the CPU at each breadcrumb, so that there is no ambiguity about where the GPU execution stopped? (Well, there still will be a bit of ambiguity due to the asynchronous nature of the GPU, but not much.)
676
677 The proposed debugging utility (already implemented in Turnip in ``tu_cs_breadcrumbs.cc``\ ) does the following:
678
679
680 * Outside of the driver: increase the hangcheck timeout to a huge value.
681 * Emit a GPU<->CPU sync point at each significant packet, like draw/dispatch/blit/etc.
682 * Run a thread on the CPU that checks the breadcrumb value in memory; if a new one appears, print it and send it over the network; write back an ack value for the GPU to proceed.
683 * Optionally request user input at each breadcrumb (useful to be configurable; e.g., request explicit confirmation after the 10,000th breadcrumb).
684
685 Together with dumping the submissions, the above allows you to find the closest point to the unrecoverable crash. The option for explicit user acks is useful to get as close to the crash as possible, with one reason being that HW may noticeably crash only after a certain time after the crashing packet (yes, it happened a few times on Adreno HW).
686
687 .. list-table::
688 :header-rows: 1
689
690 * - Driver
691 - Debug Option
692 * - Turnip
693 - ``TU_BREADCRUMBS``
694