Can't use uniform variable in fragment shader

I am new to GPU programming.

I have a problem in using uniform variables.

The following code is my code for testing floating point textures + pbuffers + fragment shader.

I think the finally obtained array “tdata2” should be
tdata2[li][0]=2.0 [/li]tdata2[li][1]=3.0.[/li](tdata2[li][2],tdata[2[*][3] depend on the xy location of the textures)[/li]
But the obtained result shows tdata2[li][1]=0.0.[/li]
I think the uniform variable “mtValue” is not working.

I cannot resolve this problem.

My test environment is :
OS: FreeBSD 4.10R (I have also tested this code on redhat linux with minor modifications about typedefs. The results were same as those obtained on the FreeBSD.)
GPU:nvidia GeForce5700 256MB VRAM
Driver:nvidia 61.13 for FreeBSD

Thanks in advance.

 

#include <GL/gl.h>
#include <GL/glext.h>
#include <GL/glx.h>
#include <GL/glu.h>
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

#define GL_FRAGMENT_SHADER_ARB 0x8B30
#define GL_OBJECT_LINK_STATUS_ARB 0x8B82
#define GL_FLOAT_RGBA32_NV 0x888B

typedef char GLcharARB;
typedef int GLhandleARB;

void set_texdata1(
        float *tdata,
        int w,
        int h,
        int nelem)
{
        int i,j,k,ic=0;
        for(i=0;i<w;i++)
                for(j=0;j<h;j++)
                        for(k=0;k<4;k++){
                                tdata[ic+0] = 0.01*i + 0.001*j + 0.00001*k ;
                                ic++ ;
                        }
}

void show_texdata(
        float *tdata,
        int w,
        int h,
        int nelem)
{
        int i,j,k,ic=0;
        printf("
");
        for(i=0;i<w;i++){
                for(j=0;j<h;j++){
                        if(i%50==0 && j%50==0)
                                printf("%d %d %2.10f %2.10f %2.10f %2.10f
",i,j,
                                        tdata[ic+0],tdata[ic+1],tdata[ic+2],tdata[ic+3]);
                        ic += 4 ;
                }
        }
        printf("
");
}

static const char *edgeFragSource = {
"const float ctValue=2.0;"
"uniform float mtValue;"
"uniform sampler2DRect texUnit1;"
"uniform sampler2DRect texUnit2;"
"void main(void)" 
"{"
"   vec2 texCoord = gl_TexCoord[0].xy;"
"   vec4 c  = texture2DRect(texUnit1, texCoord);"
"   vec4 d  = texture2DRect(texUnit2, texCoord);"
"   gl_FragColor.r = ctValue ;"
"   gl_FragColor.g = mtValue ;"
"   gl_FragColor.b = c.b + d.b ;"
"   gl_FragColor.a = c.a + d.a ;"
"}"
};

int main()
{
        Display *dpy;
        GLXFBConfig *fbconfig;
        int i,j,k,screen, w=64, h=64, nelem=4, fbconfigCount, vp[4];
        float *tdata0,*tdata1,*tdata2,mvalue;
        GLXContext cx,cx1;
        GLXPbuffer pbuffer;
        int attribList[]={
                GLX_RENDER_TYPE, GLX_RGBA_BIT,
                GLX_MAX_PBUFFER_WIDTH, w,
                GLX_MAX_PBUFFER_HEIGHT, h,
                GLX_RED_SIZE, 32,
                GLX_GREEN_SIZE, 32,
                GLX_BLUE_SIZE, 32,
                GLX_ALPHA_SIZE, 32,
                GLX_DRAWABLE_TYPE,GLX_PBUFFER_BIT,
                None 
                };
        int attrib_2[]={
                GLX_PBUFFER_WIDTH, w,
                GLX_PBUFFER_HEIGHT, h,
                GLX_NONE
                };
        FILE *file;
        unsigned char *pixels;   

    unsigned int  _iTexture[2];
    GLhandleARB   _programObject, _fragmentShader;
    GLint _texUnit1, _texUnit2, _mtValue, progLinkSuccess;
                         
        tdata0 = (float *)malloc(w*h*nelem*sizeof(float));
        tdata1 = (float *)malloc(w*h*nelem*sizeof(float));
        tdata2 = (float *)malloc(w*h*nelem*sizeof(float));
        set_texdata1(tdata0,w,h,nelem);
        set_texdata1(tdata1,w,h,nelem);
        
        dpy = XOpenDisplay(0);
        screen = DefaultScreen(dpy);
        fbconfig = glXChooseFBConfig(dpy, screen, attribList, &fbconfigCount);
        pbuffer = glXCreatePbuffer(dpy, fbconfig[0], attrib_2);
        cx = glXCreateNewContext(dpy, fbconfig[0], GLX_RGBA_TYPE, NULL, GL_TRUE);
        glXMakeContextCurrent(dpy, pbuffer, pbuffer, cx);
        
        glGenTextures(2, _iTexture);
        glBindTexture(GL_TEXTURE_RECTANGLE_NV, _iTexture[0]);
        glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_S, GL_CLAMP);
        glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_T, GL_CLAMP);
        glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_FLOAT_RGBA32_NV, w, h,
                     0, GL_RGBA, GL_FLOAT, tdata0);

        glBindTexture(GL_TEXTURE_RECTANGLE_NV, _iTexture[1]);
        glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_S, GL_CLAMP);
        glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_T, GL_CLAMP);
        glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_FLOAT_RGBA32_NV, w, h,
                     0, GL_RGBA, GL_FLOAT, tdata1);

        glBindTexture(GL_TEXTURE_RECTANGLE_NV, _iTexture[0]);
        glGetTexImage(GL_TEXTURE_RECTANGLE_NV,0,GL_RGBA,GL_FLOAT,tdata2);

        glBindTexture(GL_TEXTURE_RECTANGLE_NV, _iTexture[1]);
        glGetTexImage(GL_TEXTURE_RECTANGLE_NV,0,GL_RGBA,GL_FLOAT,tdata2);
        
        _programObject = glCreateProgramObjectARB();
        _fragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
        glShaderSourceARB(_fragmentShader, 1, &edgeFragSource, NULL);
        glCompileShaderARB(_fragmentShader);
        glAttachObjectARB(_programObject, _fragmentShader);
        glLinkProgramARB(_programObject);
        glUseProgramObjectARB(_programObject);
        
        _mtValue  = glGetUniformLocationARB(_programObject, "mtValue");
        _texUnit1 = glGetUniformLocationARB(_programObject, "texUnit1");
        _texUnit2 = glGetUniformLocationARB(_programObject, "texUnit2");

        glUniform1fARB(_mtValue, 3.0);
        glUniform1iARB(_texUnit1, 0);
        glUniform1iARB(_texUnit2, 1);

        glViewport(0,0,w,h);
        gluOrtho2D(0,w,0,h);        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_RECTANGLE_NV, _iTexture[0]);
        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_RECTANGLE_NV, _iTexture[1]);
        
        glBegin(GL_QUADS);
        {
            glTexCoord2i(0, 0); glVertex2i(0, 0);
            glTexCoord2i(w, 0); glVertex2i(w, 0);
            glTexCoord2i(w, h); glVertex2i(w, h);
            glTexCoord2i(0, h); glVertex2i(0, h);
        }
        glEnd();
        glUseProgramObjectARB(0);
        
        glBindTexture(GL_TEXTURE_RECTANGLE_NV, _iTexture[1]);
        glCopyTexSubImage2D(GL_TEXTURE_RECTANGLE_NV, 0, 0, 0, 0, 0, w, h);

        glGetTexImage(GL_TEXTURE_RECTANGLE_NV,0,GL_RGBA,GL_FLOAT,tdata2);

        show_texdata(tdata2,w,h,nelem);

}


 

I really can’t explain why, but last week I was also fighting with with a uniform that wouldn’t work in my fragment shader.

Today I replaced my 5900 with a 6800 and it’s working fine.

There must some limitation int the FX 5xxx serie.

Hi,vince.

Thank you for your information.

[b]
Today I replaced my 5900 with a 6800 and it’s working fine.

There must some limitation int the FX 5xxx serie.[/b]
Hmm…
I hope this UNDOCUMENTED limitation will be fixed by the new version of the video driver.

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