Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log values of variables inside the vertex shader

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;
}
like image 971
Pupillam Avatar asked Dec 25 '12 10:12

Pupillam


People also ask

What happens in the vertex shader?

A vertex shader receives a single vertex from the vertex stream and generates a single vertex to the output vertex stream. There must be a 1:1 mapping from input vertices to output vertices. Vertex shaders typically perform transformations to post-projection space, for consumption by the Vertex Post-Processing stage.

What are the inputs and outputs of a vertex shader?

The Vertex-shader Each vertex shader input vertex can be comprised of up to 16 32-bit vectors (up to 4 components each) and each output vertex can be comprised of as many as 16 32-bit 4-component vectors. All vertex-shaders must have a minimum of one input and one output, which can be as little as one scalar value.

What is the variable in the vertex shader that must be assigned the position of the vertex?

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 vUV in vertex shader?

The vUV model-texture mapping variable that was passed by the vertex shader to the pixel shader for the current portion of the 3D mesh is used to apply the originally intended 2D texture color to the 3D mesh with the line.


1 Answers

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!

like image 128
Ricardo RendonCepeda Avatar answered Sep 28 '22 06:09

Ricardo RendonCepeda