Hello, I want to use compute shader to speed up calculation of every voxel in the volume, which is stored as a 3d-texture. But something weird happened.
- VS 2019
- GLFW 3.3.1
- OpenGL Core 4.6
I’ve created some functions in the compute shader as follow:
vec2 evaluateValuePerTex(layout(rgba32f) readonly image3D kernelTex, ivec3 texCoord, uvec3 p)
{
ivec3 coordMean = texCoord;
ivec3 coordVar = texCoord + ivec3(0, 0, 2 * 3 * 4);
vec4 means = imageLoad(kernelTex, coordMean);
vec4 vars = imageLoad(kernelTex, coordVar);
float binWeight = means.w;
float kernelValue = vars.w * gaussian3D(means.xyz, vars.xyz, p);
return vec2(binWeight, kernelValue);
}
float evaluateProb(uvec3 p, ivec3 texCoord)
{
vec2 V1 = evaluateValuePerTex(tex_GMMKernel1, texCoord, p);
vec2 V2 = evaluateValuePerTex(tex_GMMKernel2, texCoord, p);
vec2 V3 = evaluateValuePerTex(tex_GMMKernel3, texCoord, p);
vec2 V4 = evaluateValuePerTex(tex_GMMKernel4, texCoord, p);
return V1.x * V1.y + V2.x * V2.y + V3.x * V3.y + V4.x * V4.y;
}
It returns the value reconstructed by four texture images tex_GMMKernel1, tex_GMMKernel2, tex_GMMKernel3 and tex_GMMKernel4, and I’m sure that single texture access value is correct, and nothing wrong with image binding.
And I call glDispatchCompute as follow:
CompShader reconCompShader("../resources/shaders/reconstruct_iter_comp.glsl");
for (int i = 0; i < KernelNum; ++i)
glBindImageTexture(i, texSet[i]->getID(), 0, GL_TRUE, 0, GL_READ_ONLY, GL_RGBA32F);
glBindImageTexture(KernelNum + 0, texVolume.getID(), 0, GL_TRUE, 0, GL_READ_WRITE, GL_RG32F);
glBindImageTexture(KernelNum + 1, texCount.getID(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R16UI);
reconCompShader.use();
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_SHADER_STORAGE_BARRIER_BIT);
for (int i = 0; i < 256; ++i)
{
std::cout << "channel " << i << "\r";
glDispatchCompute(resolution.x, resolution.y, resolution.z);
}
std::cout << std::endl << "Calculate finished..." << std::endl;
but the result doesn’t match what the code should get using pure C++. Please notice the function
float evaluateProb(uvec3 p, ivec3 texCoord)
the four similar lines of function call get V1, V2, V3 and V4, but when the function returns
V1.x*(V1.y + V2.y + V3.y + V4.y)
it acts like return the value V1.x * V4.y, and also following results happened but I don’t know why:
V1.x*(V1.y + V2.y + V3.y + V4.y) // similar to V1.x * V4.y
V1.x*(V1.y + V2.y + V3.y) // similar to V1.x * V3.y
V1.x*(V1.y + V2.y) // similar to V1.x * V2.y
so I want to know why, I am certainly sure there’re something wrong when I access textures frequently in function evaluateProb
. Maybe there’re something else while accessing textures? I’d be honored to get your help, thanks!