简体   繁体   English

计算着色器无法获取结果图像

[英]Compute shader can't get result image

I am using the compute shader to get a column of colors to match the conditions and I want to get the results for further processing.我正在使用计算着色器获取 colors 的列以匹配条件,我想获取结果以进行进一步处理。 I want to get the output data in image1D imgOutput, but get_img can't get anything, did I do something wrong?我想获取image1D imgOutput中的output数据,但是get_img什么也获取不到,是我做错了吗? the texture in the first block has been generated before.第一个块中的纹理之前已经生成。

Shader spriteRowProgram, spriteColumnProgram;
unsigned int resultTexture = 0;
const std::string path = found::utils::Environment::GetRunPath();
const std::string sprite_row_path = path + "shaders/glfw_sprite_row.comp";
spriteRowProgram = Shader(sprite_row_path);
std::vector<GLfloat> get_img(qMax(TEX_WIDTH, TEX_HEIGHT) * 4);
glGenTextures(1, &resultTexture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_1D, resultTexture);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA32F, qMax(TEX_WIDTH, TEX_HEIGHT), 0, GL_RGBA, GL_FLOAT, get_img.data());

glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, texture);

spriteRowProgram.use();
glUniform1i(1, 1);
glBindImageTexture(1, texture, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA32F);
glUniform1i(0, 0);
glBindImageTexture(0, resultTexture, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
glClear(GL_COLOR_BUFFER_BIT);

spriteRowProgram.setInt("input_width", TEX_WIDTH);
spriteRowProgram.setInt("input_height", TEX_HEIGHT);
glDispatchCompute(TEX_WIDTH, 1, 1);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
#version 430 core
layout (local_size_x = 1, local_size_y = 1) in;
layout(rgba32f, location = 0, binding = 0) uniform writeonly image1D imgOutput;

// texture samplers
layout(rgba32f, location = 1, binding = 1) uniform readonly image2D inputImage;
uniform int input_width;
uniform int input_height;

void main()
{
    vec4 baseColor = imageLoad(inputImage, ivec2(0, 0));
    bool alpha = baseColor.w == 1;
    bool success = true;
    for (int i = 0; i <= input_height; ++i)
    {
        vec4 current = imageLoad(inputImage, ivec2(gl_GlobalInvocationID.x, i));
        if (alpha) {
            if (current.w > 0.99) continue;
            else {
                success = false;
                break;
            }
        } else {
            vec4 difference = baseColor - current;
            success = abs(difference.x) < 0.01 && abs(difference.y) < 0.01 && abs(difference.z) < 0.01;
            if (success) continue;
            else {
                break;
            }
        }
    }
    if (success) baseColor = ivec4(0.1, 0.1, 0.1, 0.1);
    else baseColor = ivec4(1.0, 1.0, 1.0, 1.0);
    imageStore(imgOutput, int(gl_GlobalInvocationID.x), baseColor);
}

One issue might be that if you want to access images after the compute shader, the argument for glMemoryBarrier should be GL_SHADER_IMAGE_ACCESS_BARRIER_BIT and not GL_SHADER_STORAGE_BARRIER_BIT .一个问题可能是,如果您想在计算着色器之后访问图像,则glMemoryBarrier的参数应该是GL_SHADER_IMAGE_ACCESS_BARRIER_BIT而不是GL_SHADER_STORAGE_BARRIER_BIT

So所以

spriteRowProgram.setInt("input_width", TEX_WIDTH);
spriteRowProgram.setInt("input_height", TEX_HEIGHT);
glDispatchCompute(TEX_WIDTH, 1, 1);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);

should really be真的应该

spriteRowProgram.setInt("input_width", TEX_WIDTH);
spriteRowProgram.setInt("input_height", TEX_HEIGHT);
glDispatchCompute(TEX_WIDTH, 1, 1);
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM