Wrong values using image and image_load_store

Hi,

I recently implemented a simple algorithm that uses the function imageAtomicAdd from the image_load_store extension. However, when I retrieve the image values from the GPU, the values are far from what I expected.

This is how I create my texture:


glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
glTexImage2D(GL_TEXTURE_2D, 0, GL_R32UI, width, height, 0, GL_RED_INTEGER, GL_UNSIGNED_INT, 0);
glBindTexture(GL_TEXTURE_2D, 0);

Then I bind the image:


glUniform1i(m_loc_countfragments_imgtable, 1);
glActiveTexture(GL_TEXTURE0 + 1);
glBindImageTexture(1, tex, 0, false, 0, GL_READ_WRITE, GL_R32UI);

I use the following simple fragment shader to render my scene:


//[FRAGMENT SHADER]
#version 420
 
#extension GL_ARB_shader_image_load_store : enable
 

uniform volatile layout(r32ui) uimage2D img;
void main()
{
   imageAtomicAdd(img, ivec2(0, 0), 1);
}

I then retrieve my texture:


m_uibuffer = new GLuint[800*800];
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_img_fragtable);

glGetTexImage(GL_TEXTURE_2D, 0, GL_RED_INTEGER, GL_UNSIGNED_INT, m_uibuffer);
glBindTexture(GL_TEXTURE_2D, 0);
glPopAttrib();

I expected my array to be filled with zeroes, except for one position (0,0), that was suppose to contain a number grater than zero. Instead, I got an array with strange values, ranging from 0 to 100000000. Am I doing something wrong here? Am I suppose to bind my image or my texture when I call glGetTexImage?

thank you

Just one more thing:
even when I don’t write absolutely nothing in my shader (the imageAtomicAdd line is commented), I still get strange results. It is important to note that I do verify my texture before I call the glUseProgram function and I get an array full of zeroes (as expected).

And when I call glBindImageTexture with GL_READ_ONLY, glGetTexImage returns an array full of zeros. But it returns an array with strange numbers with GL_READ_WRITE even if my fragment shader code does not write to the image.

#version 420

#extension GL_ARB_shader_image_load_store : enable

Pick one: either use 4.20 or the extension. You don’t use both; it’s core in 4.20.

I expected my array to be filled with zeroes, except for one position (0,0), that was suppose to contain a number grater than zero. Instead, I got an array with strange values, ranging from 0 to 100000000.

That’s because you didn’t initialize the texture. You created it with NULL, so the glTexImage function uses arbitrary data. If you want the texture to have an initial value, you must provide one.

That’s because you didn’t initialize the texture. You created it with NULL, so the glTexImage function uses arbitrary data. If you want the texture to have an initial value, you must provide one.

That was my initial thought. But even if I do initialize my array with zeros, I get strange results.

And I check to see the original values of my texture, as I said in my second post. It’s filled with zeros.

EDIT: sorry about that, you were right. I wasn’t initializing my array properly. It’s working fine now :slight_smile:
Thank you