Compute Shader write to texture

mike picture mike · Dec 16, 2014 · Viewed 7.8k times · Source

I have implemented CPU code that copies a projected texture to a larger texture on a 3d object, 'decal baking' if you will, but now I need to implement it on the GPU. To do this I hope to use compute shader as its quite difficult to add an FBO in my current setup.

Example image from my current implementation

This question is more about how to use Compute shaders but for anyone interested, the idea is based on an answer I got from user jozxyqk, seen here: https://stackoverflow.com/a/27124029/2579996

The texture that is written-to is in my code called _texture, whilst the one projected is _textureProj

Simple compute shader

const char *csSrc[] = {
    "#version 440\n",
    "layout (binding = 0, rgba32f) uniform image2D destTex;\
     layout (local_size_x = 16, local_size_y = 16) in;\
     void main() {\
           ivec2 storePos = ivec2(gl_GlobalInvocationID.xy);\
           imageStore(destTex, storePos, vec4(0.0,0.0,1.0,1.0));\
    }"
};

As you see I currently only want to have the texture updated to some arbitrary (blue) color.

Update function

void updateTex(){ 
    glUseProgram(_computeShader);
    const GLint location = glGetUniformLocation(_computeShader, "destTex"); 
    if (location == -1){
        printf("Could not locate uniform location for texture in CS");
    }
    // bind texture
    glUniform1i(location, 0); 
    glBindImageTexture(0, *_texture, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
    // ^second param returns the GLint id - that im sure of. 

    glDispatchCompute(_texture->width() / 16, _texture->height() / 16, 1); 
    glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);

    glUseProgram(0);
    printOpenGLError(); // reports no errors.
}

Problem If i call the updateTex() outside of my main program object i see zero effect, whereas if I call it within its scope like so:

 glUseProgram(_id); // vert, frag shader pipe
  updateTex();
  // Pass uniforms to shader 
  // bind _textureProj & _texture (latter is the one im trying to update) 
 glUseProgram(0);

Then upon rendering I see this:

QUESTION: I realise that setting the update method within the main program object scope is not the proper way of doing it, however its the only way to get any visual results. It seems to me that what happens is that it pretty much eliminates the fragmentshader and draws to screenspace...

What can I do to get this working properly? (my main focus is to be able to write anything to the texture & update)

Please let me know if more code needs posting.

Answer

jozxyqk picture jozxyqk · Dec 18, 2014

I believe in this case an FBO would be easier and faster, and would recommend that instead. But the question itself is still quite valid.

I'm surprised to see a sphere, given you're writing blue to the entire texture (minus any edge bits if the texture size is not a multiple of 16). I guess this is from code elsewhere.

Anyway, it seems your main problem is being able to write to the texture from a compute shader outside the setup code for regular rendering. I suspect this is related to how you bind your destTex image. I'm not sure what your TexUnit and activate methods do, but to bind a GL texture to an image unit, do this:

int imageUnitIndex = 0; //something unique
int uniformLocation = glGetUniformLocation(...);
glUniform1i(uniformLocation, imageUnitIndex); //program must be active
glBindImageTexture(imageUnitIndex, textureHandle, ...);

see:

Lastly, as you're using image2D so GL_SHADER_IMAGE_ACCESS_BARRIER_BIT is the barrier to use. GL_SHADER_STORAGE_BARRIER_BIT is for storage buffer objects.