Integer texture read+write problem

I am trying to write some integer texture data into the framebuffer. Here’s the API code:

int* data = new int[ 4 * texWidth * texHeight ];
for ( int i = 0; i < ( 4 * texWidth * texHeight ); i++ )
    data[ i ] = i;

GLuint texID;
glGenTextures( 1, &texID );
glBindTexture( GL_TEXTURE_2D, texID );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA32I, texWidth, texHeight, 0, GL_RGBA_INTEGER, GL_INT, data );
check_gl_error();
	
glBindTexture( GL_TEXTURE_2D, texID );
glUniform1i( glGetUniformLocation( program.getID(), "indexSampler" ), 0 );
check_gl_error();

GLuint fb = m_allocater.createFramebuffer( texWidth, texHeight, GL_RGBA32I, GL_RGBA_INTEGER, GL_INT );
check_gl_error();

/* Dispath the drawcall */
float timeMicroS = apiclib::gl_util::renderFullScreenQuad_t( texWidth, texHeight, fb, drawCalls );
check_gl_error();

int *fbData = new int[ texWidth * texHeight * 4 ];
glBindFramebuffer( GL_FRAMEBUFFER, fb );
glReadPixels( 0, 0, texWidth, texHeight, GL_RGBA_INTEGER, GL_INT, fbData );
check_gl_error();

It renders a full-screen quad and runs the following fragment shader (Vertex Shader is passthrough) :

#version 300 es
layout(location = 0) out ivec4 color;
uniform highp isampler2D indexSampler;

void main()
{
    ivec2 samplePos = ivec2( gl_FragCoord.xy );
    color = texelFetch( indexSampler, samplePos, 0 );
}

So basically the above OpenGL code is trying to write some integer data from a texture into a framebuffer. They both have the same formats - 4 channel integer. The problem is that I always get 0s in the framebuffer. I can write gl_FragCoord values to the framebuffer and get correct values but not the texture data. I have tried using this too without any success:

color = texture( indexSampler, gl_FragCoord.xy );

The strange thing is that when I use the default framebuffer i.e with 0 id, though it’s RGBA8, it still returns correct values although scaled to 8-bit. So writing an integer value greater than 1 will be changed to 255. So if I have something like this : data[ i ] = i % 2 then I see output as 255, 0, 255, 0, 255, 0, 255, 0, … . But when I use a texture as a framebuffer then I get all 0s.

I am not sure what I am doing wrong here. :confused:

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