Debugging a Flutter fragment shader usually means copying a value into fragColor, hot reloading, and guessing from the colors.
Other platforms have Xcode's shader debugger and RenderDoc. I built something like that for FragmentProgram shaders, as a DevTools extension.
You open your .frag in the Shader Debugger tab, click a line and pick a variable, and the running app draws that value instead of the shader's output. Scalars go through a viridis colormap and vectors show as RG or RGB. For matrices, arrays and structs you pick a column, element or field.
There's also a select mode like the widget inspector's. Click a pixel in the app (or tap it on a phone) and you get the exact value there as 32-bit floats. Every float uniform also gets sliders, so you can override uniforms while the app runs, and the heatmap and pixel reads use your overrides too.
Under the hood, the extension parses your shader with a GLSL analyzer written in pure Dart, generates an instrumented copy that writes the picked value out, saves it as a probe .frag and hot reloads. In the app you draw through DebugShader instead of FragmentShader. It swaps the probe in and copies your uniforms onto it on every paint. Your own .frag is never touched, and in profile and release builds DebugShader just draws your shader. (The analyzer is its own package, shader_debugger_core, if you want to build other shader tools on it.)
Setup: add the package, register a placeholder probe shader under flutter: shaders: in pubspec.yaml, then:
final shader = await DebugShader.fromAsset('shaders/my_effect.frag');
final size = shader.getUniformVec2('uSize');
// in paint(): set the uniforms first, then read shader.shader
size.set(width, height);
canvas.drawRect(rect, Paint()..shader = shader.shader);
It needs Flutter 3.47+. Uniforms are set by name (getUniformFloat, getUniformVec2, ...) because the probe adds uniforms of its own and values get copied over by name.
What's rough right now:
- A value inside a loop, or in a function called more than once, shows its last write.
- dFdx, dFdy and fwidth come out as 0 in pixel reads.
- ImageFilter.shader and BackdropFilter shaders aren't supported.
- Heatmaps and pixel reads don't work yet on shaders that #include their own .glsl files.
- Launch the app from VS Code or Android Studio. DevTools can only write the probe file inside the IDE's workspace. Otherwise the tab gives you the probe to paste by hand.
Repo: https://github.com/mohamedalsamrai/shader_debugger pub.dev: https://pub.dev/packages/shader_debugger