Calculate shader texture entry

I implemented a CPU code that copies the projected texture into a large texture onto a 3d object, a “baking decal” if you want, but now I need to implement it on the GPU. For this, I hope to use compute shader, since it is quite difficult to add FBO to my current setup.

Sample image from my current implementation

This question relates to the use of Compute shaders, but for everyone who is interested, the idea is based on the answer received from the user jozxyqk, here: https://stackoverflow.com/a/2129178/

The text that is written-in is in my code called _texture , and the projected one is _textureProj

Simple Computing 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 can see, I just want the texture to be 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 updateTex() outside my main program object, I see a null effect, whereas if I call it in my scope like this:

  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 after visualization, I see the following:

iSrQGcq.jpg

Question: I understand that setting the update method within the main area of ​​the program object is not the right way to do this, however it is the only way to get any visual results. It seems to me that what happens is that it pretty much eliminates fragmentshader and draws screens ...

What can I do to make this work properly? (my main focus is being able to write texture and update something)

Please let me know if more code is required to publish.

+5
source share
1 answer

I believe that in this case the FBO will be simpler and faster, and would recommend it instead. But the question itself is still quite fair.

I am surprised to see the sphere, given that you are writing a blue texture (minus any boundary bits if the texture size is not a multiple of 16). I think this is from code elsewhere.

In any case, it seems your main problem is the ability to write a texture from a computational shader outside of the setup code for normal rendering. I suspect this is due to the way you attach the destTex image . I'm not sure what your TexUnit and activate methods TexUnit , but to associate the GL texture with the image unit do the following:

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

cm

Finally, since you are using image2D , therefore GL_SHADER_IMAGE_ACCESS_BARRIER_BIT is a barrier to use. GL_SHADER_STORAGE_BARRIER_BIT for storage buffer objects .

+1
source

Source: https://habr.com/ru/post/1209176/


All Articles