5

I am new to OpenGL ES programming and I am trying to debug my shader programming and I wonder if there is any way to log the value of a particular variable. For example, in the vertex shader program below, I would like to test the value returned by normal, basically, I was looking for something similar to NSLog...

attribute vec4 position;
attribute vec3 normal;
attribute vec2 texture;
varying vec2 v_texCoord;
varying float LightIntensity;

uniform mat4 modelViewProjectionMatrix;
uniform mat3 normalMatrix;

void main()
{
    vec3 eyeNormal = normalize(normalMatrix * normal);
    vec3 lightPosition = vec3(-1.0, 0.0, 3.0);

    float nDotVP = max(0.0, dot(eyeNormal, normalize(lightPosition)));
    LightIntensity = nDotVP;

    v_texCoord = texture;

    gl_Position = modelViewProjectionMatrix * position;
}
Ricardo RendonCepeda
  • 3,271
  • 4
  • 23
  • 30
Pupillam
  • 631
  • 6
  • 26

1 Answers1

5

I'm going to cite my source before my answer: The Developer Technology Engineers at Imagination Technologies, the company responsible for the PowerVR GPUs on iOS devices.

Basically, GLSL ES shaders can't be debugged properly just yet and there is no equivalent to NSLog or printf. If you really want to step inside your GPU code, you need to use OpenCL which is rather difficult to understand and implement, specially on an iOS device. A common way to debug shaders usually occurs in the fragment shader, where known-to-be-incorrect values are usually colored in a way they stand out (e.g. bright red). The vertex shader is harder to debug, but at least it occurs before the fragment shader and mostly depends on your attributes and uniforms.

You should already know your values for normal and normalMatrix in your main, non-GPU program. One should be an array of 3-dimensional vectors and the other a 3x3 matrix. You can multiply these, normalize them, and print the result to NSLog within a loop inside any method of your Objective-C program. This will give you the same value computed by your shader and stored in eyeNormal. In fact, I don't see any part of your vertex shader code that couldn't be calculated on the CPU - it's the fragment shader that will really cause you trouble!

Ricardo RendonCepeda
  • 3,271
  • 4
  • 23
  • 30