Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I write to a texture buffer object?

Tags:

image

load

glsl

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

like image 608
user1822451 Avatar asked Sep 15 '25 22:09

user1822451


1 Answers

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.

like image 131
jozxyqk Avatar answered Sep 17 '25 20:09

jozxyqk