creating a 3D texture buffer with glTexImage3D in opengl clear another unrelated buffer [closed]

1 day ago 1
ARTICLE AD BOX

I'm doing a small volume rendering application with opengl in c++ and I have a problem with 3D textures.

I have two 3D textures : one for the volume data (of type GL_R8, id = 9) and one for storing a local ambient occlusion "LAO" (ideally of type GL_R32F but that's the problem, and id = 10). The textures parameters are {GL_LINEAR, GL_REPEAT}.

The thing is, once I created the volume data texture, creating the other one (without even using it, just creating it) somehow seams to clear the buffer of the first one ? (or at least, the image resulting from the raymarching of the volume texture is empty)

But this only appends under two specific condition :

if i first instantiate the volume tex, and then the LAO tex (or any other 3D texture) if the second texture has a different type than the volume texture or if the second texture has a different size than the volume texture

If any of these two condition are not met (instantiate other texture first or same size and same type), the buffer will not be touched and everything will work correctly. But swapping the instantiation doesn't seem like a good solution because it will only move the problem to the other texture.

I tried to switch texture ids, put other random texture ids, and instantiate 2D texture buffer in-between the two 3D buffer instantiation, but it didn't solve anything.

Here is how i instantiate them :

// Volume texture glBindTexture(GL_TEXTURE_2D, 9); glTexImage3D(GL_TEXTURE_3D, 0, GL_R8, width, height, depth, 0, GL_RED, GL_UNSIGNED_BYTE, volume.data()); glBindTexture(GL_TEXTURE_2D, 0); // LAO texture glBindTexture(GL_TEXTURE_2D, 10); glTexImage3D(GL_TEXTURE_3D, 0, GL_R16, width, height, depth, 0, GL_RED, GL_UNSIGNED_SHORT, nullptr); glBindTexture(GL_TEXTURE_2D, 0);

Then bind the volume texture to my raymarch compute shader :

// bind volume texture glActiveTexture(GL_TEXTURE0 + 1); glBindTexture(GL_TEXTURE_2D, 10); // bind output image glBindImageTexture(0, outImgTex->id(), 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F); // call compute shader glDispatchCompute(texWidth / 16, texHeight / 16, 1);

And how I get the binded texture in my compute shader (i know for sure the raymarch algorithm is correct) :

layout(binding = 1) uniform sampler3D volume_in;

I also tried to reproduce this bug with 2D textures only but couldn't.

Read Entire Article