Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printf in GLSL?

Tags:

glsl

In C, I can debug code like:

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

in GLSL ... is there anyway 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 GLSL shader normally
  • press key 'd' = 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).

Thanks!

like image 454
anon Avatar asked Aug 06 '10 02:08

anon


People also ask

How do I print from shaders?

In general, you cannot print from shaders, because they are executed on the graphic card, and even if you could, the output would be so flooded that you would be unable to see anything relevant (that thing gets run for every vertice and every pixel, you know^^).

What is gl_Position in GLSL?

gl_Position is a special variable that holds the position of the vertex in clip space. Since a vertex shader's main output is the position in clip space, it must always set gl_Position. This vertex shader just transforms each vertex position (by the VP matrix).

What is HLSL GLSL?

You port your OpenGL Shader Language (GLSL) code to Microsoft High Level Shader Language (HLSL) code when you port your graphics architecture from OpenGL ES 2.0 to Direct3D 11 to create a game for Universal Windows Platform (UWP).


2 Answers

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.

like image 84
UncleZeiv Avatar answered Sep 28 '22 04:09

UncleZeiv


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.

like image 24
John Flatness Avatar answered Sep 28 '22 03:09

John Flatness