GLSL and RenderMonkey

Hi, I m a newbie to both GLSL and this forum :slight_smile:
Anyway, I’m trying to implement screen space effect, and ATI’s RenderMonkey help me to concentrate on GLSL itself. My question is this, when I tried to implement screen space effect with GLSL, I’ve got into strange problems.

In vertex shader, I used the quad vertices(which has coordinate range from -1 to 1) directly without applying any matrix transformation. That is,

 
uniform sampler2D   Texture0;
varying vec2 TexCoord;

void main(void)
{
   gl_Position.xy = sign(gl_Vertex.xy);
   gl_Position.w=1.0;
   gl_Position.z=0.0;
   TexCoord.x=(gl_Vertex.x+1.0) * 0.5;
   TexCoord.y=(gl_Vertex.y+1.0) * 0.5;
}
 

For a screen quad mesh, each component of gl_Vertex always range from -1 to 1, so I can directly assign gl_Vertex to gl_Position. TexCoord is used later in the fragment program.

Fragment program is even simpler. That is,

 
uniform sampler2D   Texture0;
varying vec2 TexCoord;

void main(void)
{
   vec4 c0= texture2D(Texture0, TexCoord);
   
   gl_FragColor = c0;

}
 

I thouth these two shader would display the texture named “Texture0” directly on screen, but it only display a full black rectangle without any colored pixel. That makes me crazy, because the program itself is so simple that nothing can be wrong. To find out the problem, I tried the following vertex shader ,

 uniform sampler2D   Texture0;
varying vec2 TexCoord;

void main(void)
{
//   gl_Position.xy = sign(gl_Vertex.xy);
//   gl_Position.w=1.0;
//   gl_Position.z=0.0;

   TexCoord.x=(gl_Vertex.x+1.0) * 0.5;
   TexCoord.y=(gl_Vertex.y+1.0) * 0.5;
   gl_Position=gl_ModelViewProjectionMatrix * gl_Vertex;
}
 

With the same fragment shader, it displays OK. But what I intended is not this vertex shader(not a fully screen aligned quad).

Whew, thanks for reading boring wrong thread. Is there anything wrong with the first two shaders? Any reply or suggestins would be appreciated.

Originally posted by Mr.Twinsen:
[b]

 
uniform sampler2D   Texture0;
varying vec2 TexCoord;

void main(void)
{
gl_Position.xy = sign(gl_Vertex.xy);
gl_Position.w=1.0;
gl_Position.z=0.0;
TexCoord.x=(gl_Vertex.x+1.0) * 0.5;
TexCoord.y=(gl_Vertex.y+1.0) * 0.5;
}

[/b]

You found a bug, where any shader pair with only texture sampler uniforms would fail to bind the texture to the texture unit and fail to load the uniform sampler2D. (Almost all shaders bind to either built-in uniforms OR user-defined uniforms.)

Workaround by moving an in-lined constant into a user-defined uniform, such as:

 
uniform float Scale;  // 0.5
uniform float Bias;   // 0.5
//
varying vec2 TexCoord;
//
void main(void)
{
   gl_Position.xy = sign(gl_Vertex.xy);
   gl_Position.w=1.0;
   gl_Position.z=0.0;

   // Note Scale first Bias second is a MAD - more efficent than...
   // Your Bias first then Scale is a ADD MUL - less efficient.
   // Also, do both x and y components together instead of two scalar expressions
   //

   TexCoord = gl_Position.xy * Scale + Bias;

}

varying vec2 TexCoord;
//
uniform sampler2D   Tex0;
//
void main( void )
{
   gl_FragColor = texture2D( Tex0, TexCoord );
}
 

-mr. bill

Hi, Bill. Thx for your useful response. I fixed the shader as you mentioned. I really appreciate it, but your shader mentioned doesn’t work neither. I found one more curious bug like problem. After some test, I found no additional uniform variables are needed.

//uniform float   Scale;
//uniform float   Bias;
varying vec2 TexCoord;

void main(void)
{
   gl_Position.xy = sign(gl_Vertex.xy);
   gl_Position.w=1.0;
   gl_Position.z=0.0;
   
   TexCoord=gl_Position.xy * 0.5 + 0.5;
}

This surely doesnt work. But,

//uniform float   Scale;
//uniform float   Bias;
varying vec2 TexCoord;

void main(void)
{
   gl_Position= ftransform();
   gl_Position.xy = sign(gl_Vertex.xy);
   gl_Position.w=1.0;
   gl_Position.z=0.0;
   
   TexCoord=gl_Position.xy * 0.5 + 0.5;
}

This does work. Crazy, isn’t it? I dont know what’s the difference. Same fragment program is used. Thx for the kind reply :slight_smile:

You are binding to a built-in uniform. Implicitly, your vertex shader has the active uniform gl_ModelViewProjectionMatrix. (Yes, it’s still active even though you write gl_Position immediately after. ftransform() has the side-effect of writing to gl_ClipVertex.)

I think my vertex shader works IF you changed the name of the fragment shader sampler to “Tex0”, right?

(This bug is fixed in a future driver, btw.)

-mr. bill

Hi, thx for your quick reply.
The sampler name has no relation with this topic. You may find the difference of the sampler name, but I’m smart enough to check if the correct name is bound :slight_smile: Anyway, the real problem is the differernce of the following two, ClipVertex is not important in this problem:

void main(void)
{
   //gl_Position=gl_ModelViewProjectionMatrix * gl_Vertex;
   gl_Position.xy = sign(gl_Vertex.xy);
   gl_Position.w=1.0;
   gl_Position.z=0.0;
   
   TexCoord=gl_Position.xy * 0.5 + 0.5;
}

Yeah, this doesn’t work.
and,

void main(void)
{
   gl_Position=gl_ModelViewProjectionMatrix * gl_Vertex;
   gl_Position.xy = sign(gl_Vertex.xy);
   gl_Position.w=1.0;
   gl_Position.z=0.0;
   
   TexCoord=gl_Position.xy * 0.5 + 0.5;
}

gl_Position is re-written immediately, so the difference shouldn’t be, if the shader compiler optimization worked properly. Result? The second one works regardless of sampler name, and the first one fails. Anyway that’s bug, I guesss. Thx for the reply.
ps. Tested on RADEON 9700 cat 4.7

I have do tests of your shaders on nvidia fx5900xt and it works. Even more, shader from your first post works too. Tested on forceware 61.71.

yooyo

Originally posted by Mr.Twinsen:
Hi, thx for your quick reply.
The sampler name has no relation with this topic.

The sampler name has a direct relationship with the bug.

Try:

uniform sampler2D myReallYReallyReallyLongSamplerName;
uniform sampler2D Tex0;

The value returned by a get for
GL_OBJECT_ACTIVE_UNIFORM_MAX_LENGTH_ARB was
incorrectly set to the longest non-sampler uniform name length.

You can work around that bug by keeping your sampler names shorter than the longest name of a non-sampler uniform.

-mr. bill

Yeah, Bill, I didn’t test with the long name, but, I agree with your point. (Although, “Texture0” is surely not that long.)

Thx for the reply, YooYoo. It does work on your nvidia’s, but not work on my radeon, it’s just a radeon-specific strange driver bug(4.7 cat), I think.

Thx for all the replies. :slight_smile: Now the glow shader works perfectly except that bug.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.