4

I'm using a texture buffer object like that:

glGenBuffers(1, &tbo);
glBindBuffer(GL_TEXTURE_BUFFER, tbo);
glBufferData(GL_TEXTURE_BUFFER, maxSize*sizeof(float), faceNormals.data(), GL_STATIC_DRAW);
glGenTextures(1, &tbo_tex);
glBindBuffer(GL_TEXTURE_BUFFER, 0);

and I can read it inside my compute shader using texelFetch(u_tbo_tex, index), but how can I update this value?

thank you! luiz

user1822451
  • 101
  • 1
  • 5
  • Why bother doing this in the first place? If you're using a compute shader, use `imageLoad` and `imageStore` instead. That is pretty much the only way you are going to write to a texture from a compute shader. You are going to have to consider memory coherency if you do this, but that's something you have to deal with at some point in compute shaders anyway. – Andon M. Coleman Feb 25 '15 at 09:39

1 Answers1

6

Binding it as a samplerBuffer, as I assume you're doing for texelFetch() will give you read only access. Not sure if this caches better, but imo can be a tiny bit faster than imageLoad().

To write to texture buffers from a compute shader, use image_load_store.

  • Declare the image in the shader such as:

    layout(rgba32f) uniform imageBuffer mybuffer;
    
  • Bind your texture object (which wraps the buffer object):

    void glBindImageTexture(GLuint unit,
        GLuint texture,
        GLint level,
        GLboolean layered,
        GLint layer,
        GLenum access,
        GLenum format);
    

    The unit can be anything but of course must be unique. The value of the uniform must be set to this index. I.e. glUniform1i(mybufferlocation, unit) (or hard coded in layout(), but I've never done this).

  • Then you can...

    imageStore(mybuffer, atsomeindex, vec4(123.0));
    
  • Make sure to use glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT) between shader passes that write/read. Also if there's ever potential for race conditions within the shader invocation, look into memoryBarrier/memoryBarrierImage and the coherent qualifier.

It's also worth mentioning SSBOs (what's the difference? here & here).

Transform feedback can also write directly to buffer objects from vertex shader output and perform stream compaction from geometry shaders, but this doesn't apply to your compute shaders.

Community
  • 1
  • 1
jozxyqk
  • 16,424
  • 12
  • 91
  • 180