Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 8 of 8

Thread: GLSL and RenderMonkey

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2004
    Posts
    8

    GLSL and RenderMonkey

    Hi, I m a newbie to both GLSL and this forum :-)
    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,
    Code :
    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,
    Code :
    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 ,

    Code :
     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.

  2. #2
    Intern Contributor
    Join Date
    Sep 2001
    Location
    Marlboro MA
    Posts
    96

    Re: GLSL and RenderMonkey

    Originally posted by Mr.Twinsen:

    Code :
    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;
    }
    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:
    Code :
    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

  3. #3
    Junior Member Newbie
    Join Date
    Jul 2004
    Posts
    8

    Re: GLSL and RenderMonkey

    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.

    Code :
    //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,

    Code :
    //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 :-)

  4. #4
    Intern Contributor
    Join Date
    Sep 2001
    Location
    Marlboro MA
    Posts
    96

    Re: GLSL and RenderMonkey

    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

  5. #5
    Junior Member Newbie
    Join Date
    Jul 2004
    Posts
    8

    Re: GLSL and RenderMonkey

    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 :-) Anyway, the real problem is the differernce of the following two, ClipVertex is not important in this problem:
    Code :
    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,
    Code :
    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

  6. #6
    Advanced Member Frequent Contributor yooyo's Avatar
    Join Date
    Apr 2003
    Location
    Belgrade, Serbia
    Posts
    883

    Re: GLSL and RenderMonkey

    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

  7. #7
    Intern Contributor
    Join Date
    Sep 2001
    Location
    Marlboro MA
    Posts
    96

    Re: GLSL and RenderMonkey

    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:
    Code :
    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

  8. #8
    Junior Member Newbie
    Join Date
    Jul 2004
    Posts
    8

    Re: GLSL and RenderMonkey

    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. :-) Now the glow shader works perfectly except that bug.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •