• Advertisement
Sign in to follow this  

GL_RGBA4 and FBO problem

This topic is 4264 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I think i've found a problem using FBO's and GL_RGBA4 textures. The problem lies in wrong color conversion when writing a value into FBO. For example, when I write the color 0.167969, I get the 0.2 value inside the buffer. But when I write 0.171875 (=0.167969+1.0/256), the buffer is updated with a value of 0.1333333, which is smaller than 0.2! Writtig the value 0.175781 (=0.171875+1.0/256) updates the buffer with a value of 0.2 again. So, basically, when I write 256 values from 0.0 to 1.0 in +1.0/256 steps, the resulting readback from the FBO is not in an ascending order! Following is the source that shows the problem I mentioned. I modified the source found on gpgpu.org, so that the texture internal format is GL_RGBA4.
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glut.h>

static texSize = 8;
static GLenum textarget = GL_TEXTURE_2D;
static GLenum texinternalformat = GL_RGBA4;

int main(int argc, char **argv) {
    // declare texture size, the actual data will be a vector 
    // of size texSize*texSize*4
    // create test data and fill arbitrarily
    float* data = (float*)malloc(4*texSize*texSize*sizeof(float));
    float* result = (float*)malloc(4*texSize*texSize*sizeof(float));

	// Make a 256-element texture. 
	//Each element is +1.0/256 greater than the previous one. 
	for (int i=0; i<4*texSize*texSize; i++)
		data = ((float)i)/256.0;

	// set up glut to get valid GL context and 
    // get extension entry points
    glutInit (&argc, argv);
    glutCreateWindow("TEST1");
    glewInit();
    // viewport transform for 1:1 pixel=texel=data mapping
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0,texSize,0.0,texSize);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glViewport(0,0,texSize,texSize);
    // create FBO and bind it (that is, use offscreen render target)
    GLuint fb;
    glGenFramebuffersEXT(1,&fb); 
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,fb);
    // create texture
    GLuint tex;
    glGenTextures (1, &tex);
    glBindTexture(textarget,tex);
    // set texture parameters
    glTexParameteri(textarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(textarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(textarget, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(textarget, GL_TEXTURE_WRAP_T, GL_CLAMP);
    // define texture with floating point format
    glTexImage2D(textarget,0,texinternalformat,texSize,texSize,0,GL_RGBA,GL_FLOAT,0);
    // attach texture
    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, textarget,tex,0);
    glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);

	GLenum status = glCheckFramebufferStatusEXT( GL_FRAMEBUFFER_EXT );
	switch( status )
	{
		case GL_FRAMEBUFFER_COMPLETE_EXT:
			printf("FBO complete!\n");
			break;
		default:
			printf("FBO wrong!\n");
			exit(0);
	}
	
	// transfer data to texture
	glRasterPos2i(0,0);
	glDrawPixels(texSize,texSize,GL_RGBA,GL_FLOAT,data);
	
	glReadPixels(0, 0, texSize, texSize,GL_RGBA,GL_FLOAT,result);
    // print out results
    for (int i=0; i<texSize*texSize*4; i++)
		printf("%d:\t %f quantized to %f\n",i, data, result);
    free(data);
    free(result);
    glDeleteFramebuffersEXT (1,&fb);
    glDeleteTextures (1,&tex);
    return 0;
}
A few lines from the program output:
54:      0.210938 quantized to 0.200000
55:      0.214844 quantized to 0.200000
56:      0.218750 quantized to 0.266667
57:      0.222656 quantized to 0.266667
58:      0.226563 quantized to 0.266667
59:      0.230469 quantized to 0.266667
60:      0.234375 quantized to 0.200000
61:      0.238281 quantized to 0.200000
62:      0.242188 quantized to 0.266667
63:      0.246094 quantized to 0.266667
64:      0.250000 quantized to 0.200000
65:      0.253906 quantized to 0.266667
66:      0.257813 quantized to 0.266667
The problem does not show when the format is GL_RGBA. The buffer testing in booth cases says that the buffer is complete. The quantization errors might be small, but I have a huge problem with this. I use the RGBA4 to render each scene polygon using different color, which is used for visibility check in a realtime radiosity GPGPU project. There a few pixels on the scene that fail the visibility check even if they are visible, just because of this problem. I'm using an ATIx1800xl and 6.5 driver on WinXP. Can someone check if there is a similar problem on NV cards?

Share this post


Link to post
Share on other sites
Advertisement
I get a "FBO wrong!" message...
Geforce 6800GT
ForceWare version 84.21

Share this post


Link to post
Share on other sites
Quote:
Original post by Kambiz
I get a "FBO wrong!" message...
Geforce 6800GT
ForceWare version 84.21


Thanks.
Looks like NVidia doesn't support GL_RGBA4 attached to a FBO :( ...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement