Debugging Metal compute shaders with Xcode

2.2k views Asked by At

In Apple's 2018 WWDC session "Metal Shader Debugging and Profiling," the speakers detail what was then a new Metal debug workflow. However, they didn't go very far with compute shaders in the demo, only briefly mentioning the options that would appear when debugging compute kernels and focusing on vertex and fragment shader debugging.

How do we debug compute shaders when we send work to the GPU only once as opposed to every frame? Please direct me to other WWDC sessions you recommend covering this topic and use of MTLCaptureManager.

1

There are 1 answers

0
ebarti On BEST ANSWER

To debug your Metal Compute Kernel functions, you need to create a CaptureScope.

You do not allocate the scope yourself, but rather retrieve it from the MTLCaptureManager:

let sharedCapturer = MTLCaptureManager.shared()
let customScope = sharedCapturer.makeCaptureScope(device: device)
// Add a label if you want to capture it from XCode's debug bar
customScope.label = "Pls debug me"
// If you want to set this scope as the default debug scope, assign it to MTLCaptureManager's defaultCaptureScope
sharedCapturer.defaultCaptureScope = customScope

Critical steps to get the custom scope to work:

  • Create the scope outside of your compute loop.
  • While the scope is active, you need to maintain a strong reference to it.

What I said above, but in code:

customScope?.begin()
let commandBuffer = commandQueue.makeCommandBuffer()!
// Perform your metal computing here
commandBuffer.commit()
customScope?.end()

Some related WWDCs:

WWDC 2019 - Delivering Optimized Metal Apps and Games

More Info:

Developer Guide - Frame Capture Debugging Tools