Problem using Image Load Store EXT

I’m having some problems using the Image Load Store EXT. What I’m trying to do is very simple, its just clear the texels of a texture using imageStore, but when I check the texture values back in opengl, the values haven’t changed at all.

I’ll post the revelant part of the code:

  //create a pointer to texture data, and initialize all texels with 5000.
  _bufferCounter = new cBufferType[_fboHeight*_fboWidth];
  for (int i=0; i<_fboHeight*_fboWidth; ++i) { _bufferCounter[i] = (cBufferType)5000;}

The cBufferType is type unsigned int. (I tried with some other types too, like float).

  //gen a texture and bind it in a image unit
  glGenTextures(1, &_textureId2);
  glBindTexture(GL_TEXTURE_2D, _textureId2);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_R32UI, _fboWidth, _fboHeight, 0,  GL_RED, GL_UNSIGNED_INT, _bufferCounter);
  glBindImageTextureEXT(0, _textureId2, 0, GL_FALSE, 0,  GL_READ_WRITE, GL_R32UI);
  glBindTexture(GL_TEXTURE_2D, 0);

Tried some others combination here too (like GL_R32F, with GL_FLOAT), but is the same.
after bind the texture I send it to the shader in display func.

  //display Func...

  glBindTexture(GL_TEXTURE_2D, _textureId2);   

  //Pass counter buff texture to shader
  glProgramUniform1iEXT(_shaderProgram, glGetUniformLocation(_shaderProgram, "counterBuff"), 0);


  glClearColor(0.f, 0.f, 0.f, 0.f);    

  // draw to fbo color buffer

         glVertex2f(0.0f, 0.0f);
         glVertex2f(1.0f, 0.0f);
         glVertex2f(1.0f, 1.0f);
         glVertex2f(0.0f, 1.0f);

Now the fragment Shader:

#version 400

//-- enable opengl extentions
#extension GL_NV_gpu_shader5 : enable
#extension GL_EXT_shader_image_load_store : enable

//-- Whole number pixel offsets
layout(pixel_center_integer) in vec4 gl_FragCoord;

//-- fragment counter texture
coherent uniform layout(size1x32) uimage2D counterBuff;

void main(void){
   //-- get coords
   ivec2 coords = ivec2(gl_FragCoord.xy);

   if(coords.x>=0 && coords.y>=0 && coords.x<512 && coords.y<512 ){
     //clear texture
     imageStore(counterBuff, coords, ivec4(0));

   //-- nothing is written to the framebuffer

But when I read the texture back in OpenGL (The _bufferCounter), the values haven’t changed at all (still all 5000).
Not 0 as I expected.

I’m doing something wrong or missing something?

Thanks in advance.

First, stop using the EXT version. We have the ARB version; use that. We also have ARB_separate_shader_objects; you don’t have to use ProgramUniform*EXT either.

More importantly, I don’t see the use of glMemoryBarrier in your code. Until that function executes, you can assume nothing about whether anything has modified an image. Your read from the image may read old data, new data, corrupt data, or cause your machine to burst into flames.

Until you use a glMemoryBarrier call to tell OpenGL that you’ve changed an image and want to see the results, you have no idea what you’ll get.

Thanks for the reply.

The problem is solved.

Yes, using memory barrier is important (I will use it), but also the way I was trying to access the values of the texture was wrong.

The right way is to use glGetTexImage to retrieve the values.

So with the following modifications it worked.

  glTexImage2D(GL_TEXTURE_2D, 0, GL_R32UI, _fboWidth, _fboHeight, 0,  GL_RED_INTEGER, GL_UNSIGNED_INT, 0);

Then retrieving the values from memory

  glBindTexture(GL_TEXTURE_2D, _textureId2);
  glGetTexImage( GL_TEXTURE_2D, 0 , GL_RED_INTEGER, GL_UNSIGNED_INT, _bufferCounter );


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