Calculate a shader without changing the 3d texture

I want to transfer the initialization of my 3D texture from the CPU to the GPU. As a test, I wrote a shader to set all voxels to a constant value, but the texture does not change at all. How can I make it work?

Calculate Shader:

#version 430 layout(local_size_x=1, local_size_y=1, local_size_z=1) in; layout(r8, location = 0) uniform image3D volume; void main() { imageStore(volume, ivec3(gl_WorkGroupID), vec4(0)); } 

Call:

 glEnable(GL_TEXTURE_3D); glActiveTexture(GL_TEXTURE0); glGenTextures(1, &volume_tid); glBindTexture(GL_TEXTURE_3D, volume_tid); glTexImage3D(GL_TEXTURE_3D, 0, GL_R8, volume_dims[0], volume_dims[1], volume_dims[2], 0, GL_RED, GL_UNSIGNED_BYTE, voxels); ShaderProgram computeVolumeShader; computeVolumeShader.loadShader(GL_COMPUTE_SHADER, "compute_volume.glsl"); computeVolumeShader.link(); computeVolumeShader.use(); computeVolumeShader.uniform("volume", 0); glBindImageTexture(0, volume_tid, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R8); glDispatchCompute(volume_dims[0], volume_dims[1], volume_dims[2]); glBindImageTexture(0, 0, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R8); computeVolumeShader.unUse(); glMemoryBarrier(GL_ALL_BARRIER_BITS); 

Note: voxels loaded in glTexImage3D contains initialized CPU data.

+6
source share
1 answer

Ugh. Thus, apparently, the three-dimensional texture should be tied as multilevel, otherwise the shader will not be able to change any uvw coordinate using w> 0.

 glBindImageTexture(0, volume_tid, 0, /*layered=*/GL_TRUE, 0, GL_READ_WRITE, GL_R8); 

does the trick.

+13
source

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


All Articles