41

In C, I can debug code like:

fprintf(stderr, "blah: %f", some_var);

In GLSL, is there a way for me to just dump out a value in a Vertex or Fragment shader? I don't care if it's slow; I just want to dump out the value. Ideally, I want a setup like the following:

  • normal state = run the GLSL shader normally
  • press key D = the next frame is generated in ULTRA slow mode, where the "printfs" in the Vertex/Fragment shader are executed and dumped out.

Is this feasible? (I don't care about performance; I just want to do this for one frame).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
anon
  • 41,035
  • 53
  • 197
  • 293

4 Answers4

26

Unfortunately it's not possible directly. One possible solution though, that I end up using a lot (but I'm sure it's pretty common among GLSL developers) is to "print" values as colors, in place of your intended final result.

Of course this has many limitations; for one, you have to make sure that your value maps in a (0,1.0) range. Functions as mod, fract etc. turn out useful in these cases. But, in general, this is what I see as the "printf" equivalent in GLSL.

UncleZeiv
  • 18,272
  • 7
  • 49
  • 77
  • 6
    Yes. And computers lend themselves very well to this. For example, many vectors (e.g., normal, eye, light) are three component. These can be outputted into the red, green, and blue channels. With a surprisingly little amount of practice, "seeing" vectors this way is intuitive. You can't visualize vectors this way--even with a debugger! – geometrian Aug 15 '12 at 09:26
21

Instead of printing values, have you thought of trying a GLSL debugger?

For example, glslDevil will let you step through your shader's execution and examine the variables at each step.

John Flatness
  • 32,469
  • 5
  • 79
  • 81
4

Check out AMD CodeXL. It will let you step frame by frame to inspect OpenGL state values, shader code, and texture memory.

http://developer.amd.com/tools-and-sdks/heterogeneous-computing/codexl/

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aaron Hagan
  • 586
  • 2
  • 4
-5

You can see the variable you want to check by copying its value in a uniform and then get that uniform with glGetUniformfv

linello
  • 8,451
  • 18
  • 63
  • 109
  • 9
    According to the specifications, uniform variables are read-only and have the same value among all processed vertices/fragments. You can *not* change them within your shader and actually expect glGetUniform to retrieve the newly assigned value! – Greg S Jun 28 '13 at 19:29
  • 7
    This answer is incorrect. From shader perspective uniforms are read only values. Usually implemented by HW vendors by reading constant registers (default block uniforms) or constant buffers (uniform blocks).However, latest OpenGL versions (ES3.1/GL4.2,4.3) introduce shader image load&stores, shader atomics and shader storage buffers. These are shader writable. Also, after issuing proper barrier, their current storage is visible from CPU. – sacygan Jul 30 '14 at 23:03