I was hopping someone can help me out here.
I am trying to get imageStore to work from a simple fragment shader but I had no success until now. I am wondering if I am doing something fundamentally wrong.
In the code bellow I am sending 200,000 points to render and I am trying to use their index ( gl_VertexID ) to fill in the image2D (1024x1024) like this:
imageStore(image_out, ivec2(vertexIndex % imageWidth, vertexIndex / imageHeight ) , vec4(1.,1.,1.,1.));

When I read the pixels from the texture using "glGetTexImage" I get what seems to be random results, while I was expecting to see white color for at least the first 190 lines of the image.
Some time the image will show parts of the underlying windows, or different pass renders, so I am sure I am doing something very wrong here.
This is a very simplified version of my real shader. I only tried it for debug and I still could not get it to work . There are no compile or link errors.

Here is the code:

1) create the texture that will binded to the image:

int imageWidth = 1024;
int imageHeight =1024;
glGenTextures(1, &glBakeTexture);
glBindTexture(GL_TEXTURE_2D, glBakeTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, imageWidth , imageHeight , 0 , GL_RGBA, GL_FLOAT , NULL);
glBindTexture(GL_TEXTURE_2D, 0);

2) The following is how I render to image:


shader->setUniformValue( "mvp", mvp );
// the width of the image2D

shader->setUniformValue("imageWidth", imageWidth);
// the height of the image2D
shader->setUniformValue("imageHeight", imageHeight);

//glActiveTexture( GL_TEXTURE0 + 2 ); // tried using glActiveTexture that didn't help much
glBindImageTexture(2, glBakeTexture, 0, false, 0, GL_WRITE_ONLY, GL_RGBA8);

glBindVertexArray( m_vao );

// draw points
glDrawArrays(GL_POINTS, 0, (GLsizei)numberOfPoints); // about 200,000 points



// release textures etc...

3) read the image using "glGetTexImage"

// tried both of the following barriers and also "ALL_BIT"

glBindTexture(GL_TEXTURE_2D, glBakeTexture);
int numBytes = imageWidth * imageHeight * 4 * sizeof(float);
unsigned char *pixels = (unsigned char*)malloc(numBytes); // allocate image data into RAM
glGetTexImage(GL_TEXTURE_2D , 0 , GL_RGBA , GL_FLOAT , pixels); // after this call the pixels seems wrong !


4) The vertex shader:
#version 430

layout (location = 0) in vec3 in_Position;

uniform mat4 mvp;

flat out int vertexIndex;

void main( void )
vec4 worldPos = vec4(in_Position, 1.0);
gl_Position = mvp * worldPos;

vertexIndex = gl_VertexID;

5) Fragment shader :
#version 430

uniform int imageWidth;
uniform int imageHeight;

in vec4 worldPos;

(I made sure vertexIndex variable has correct value by doing some color testing...)

flat in int vertexIndex;

// Output image
layout (binding = 2, rgba8) writeonly uniform image2D image_out;

void main( void )
imageStore(image_out, ivec2(vertexIndex % imageWidth, vertexIndex / imageHeight ) , vec4(1.,1.,1.,1.));

Am I forgetting something or making an obvious mistake here ?

Thanks in advance !