I am using Windows 10 and OpenGL 4.
I want to manually create a mipmap texture using a custom reduction method, but I failed to write anything into the level #1 of the mipmap texture.
I first create the mipmap:
int w = 1024;
int h = 1024;
int levels = 12;
std::vector<float> value;
value.resize(w * h, 1e38f);
glGenTextures(1, &mipmap);
glBindTexture(GL_TEXTURE_2D, mipmap);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, w, h, 0, GL_RED, GL_FLOAT, value.data());
for (int level = 1; level < levels; ++level) {
w = w / 2;
h = h / 2;
glTexImage2D(GL_TEXTURE_2D, level, GL_R32F, w, h, 0, GL_RED, GL_FLOAT, NULL);
}
Then I create a compute shader and compute program from the following GLSL:
#version 450
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
layout(binding = 0, r32f) uniform writeonly image2D outImage;
layout(binding = 1) uniform sampler2D inImage;
uniform vec2 imageSize;
void main(void) {
uvec2 pos = gl_GlobalInvocationID.xy;
float depth = texture(inImage, (vec2(pos) + vec2(0.5)) / imageSize).x;
depth *= 0.5;
imageStore(outImage, ivec2(pos), vec4(depth));
}
Finally I run the compute shader:
glUseProgram(computeProgram);
int w = 1024;
int h = 1024;
int levels = 12;
for (int i = 1; i < levels; ++i) {
glActiveTexture(GL_TEXTURE0);
glActiveTexture(GL_TEXTURE1);
glBindImageTexture(1, mipmap, i - 1, GL_FALSE, 0, GL_READ_ONLY, GL_R32F);
glBindImageTexture(0, mipmap, i, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32F);
w = w / 2;
h = h / 2;
GLfloat size[2] = { w, h };
GLuint constantLocation = glGetUniformLocation(computeProgram, "imageSize");
glUniform2f(constantLocation, size[0], size[1]);
glDispatchCompute(w, h, 1);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
}
The codes compiled smoothly.
I ran the program and dumped the resulting mipmap to files and found that only the level #0 has non-zero values.
I checked everywhere using glGetError, but it did not report a single error.
I also debugged using RenderDoc and it shows the inImage has no resource. So I removed the inImage and always assign 0.5 to depth
. But the resulting level #1 is still filled with 0 but not 0.5.
How to fix this? Thanks!