PDA

View Full Version : Can't use uniform variable in fragment shader



bsdlover
12-13-2004, 02:20 AM
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 [0]=2.0
tdata2 [1]=3.0.
(tdata2 [2],tdata[2 [3] depend on the xy location of the textures)

But the obtained result shows tdata2 [1]=0.0.

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("\n");
for(i=0;i<w;i++){
for(j=0;j<h;j++){
if(i%50==0 &amp;&amp; j%50==0)
printf("%d %d %2.10f %2.10f %2.10f %2.10f\n",i,j,
tdata[ic+0],tdata[ic+1],tdata[ic+2],tdata[ic+3]);
ic += 4 ;
}
}
printf("\n");
}

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, &amp;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, &amp;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);

}

vince
12-13-2004, 10:21 AM
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.

bsdlover
12-15-2004, 03:37 AM
Hi,vince.

Thank you for your information.




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

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