PDA

View Full Version : Shader Problem



glfreak
01-08-2011, 08:11 PM
I've got the following vertex and fragment shaders respectively:



uniform mat4 projMat;
uniform mat4 viewMat;
uniform vec4 modulateColor;
uniform bool isTextureEnabled_vs;

attribute vec4 inVertexPos;
attribute vec4 inVertexColor;
attribute vec2 inTexCoord;

varying vec4 varVertexColor;
varying vec2 varTexCoord;

void main()
{
vec4 pos = inVertexPos;
pos.z = -pos.z;
gl_Position = viewMat * projMat * pos;

varVertexColor = inVertexColor * modulateColor;

if (isTextureEnabled_vs) {
varTexCoord = inTexCoord;
}
}





uniform sampler2D tex0;
uniform bool isTextureEnabled_fs;

varying vec4 varVertexColor;
varying vec2 varTexCoord;

void main()
{
if (isTextureEnabled_fs) {
gl_FragColor = varVertexColor * texture2D(tex0, varTexCoord.st);
} else {
gl_FragColor = varVertexColor;
}
}


When the isTextureEnabled_vs and isTextureEnabled_fs are (true) then everything render fines with correct texture and color. However when set to (false) then the primitives are rendered with incorrect color values which looks random.
In either cases I pass the same attributes and uniform/sampler values and fill up the same vertex buffer with the same data. What could be the problem?

Another question, is it possible to use the same uniform variable in both shaders so that I can have one common name (isTextureEnabled)?

Graphics spec:
OpenGL renderer string: ATI MOBILITY RADEON X1600
OpenGL version string: 2.1.8545 Release

Help appreciated.

Alfonse Reinheart
01-08-2011, 08:35 PM
In either cases I pass the same attributes and uniform/sampler values and fill up the same vertex buffer with the same data. What could be the problem?

I'm pretty sure that you must write to all output values from a vertex shader. If you declare it, you must write to it, even if you don't use it.

Change that and see what happens.


Another question, is it possible to use the same uniform variable in both shaders so that I can have one common name

Yes. That's part of the reason why shaders are linked into programs.

glfreak
01-08-2011, 09:11 PM
I tried always writing to varTexCoord in vertex shader but nothing changed, same problem...

Then I commented out this line:

gl_FragColor = varVertexColor * texture2D(tex0, varTexCoord.st);

in fragment shader and it wokred fine! How come? it should be hit since the condition is false.

Then I remembered that I have line stippling enabled. I disabled it, and everything worked fine.

So my guess the problem was when line stippling enabled...it did some weird stuff to the shader????

Any thoughts?

V-man
01-11-2011, 06:49 AM
I expect line stippling to be a trick rather than implementef in hw. Isn't it deprecated?

skynet
01-11-2011, 07:07 AM
Just a quick correction; this is what the specs (GLSL 4.1, pp 4.3.6) say:

Only output variables that are read by the subsequent pipeline stage need to be written; it is allowed to
have superfluous declarations of output variables