我一直无法将数据发送到SSBO以供计算着色器使用。不幸的是,我和khronos docs say "TODO"无法使它们的示例代码正常工作,并且人们似乎在做些细微的事情Example 1 Example 2 Example 3-有人可以帮忙吗?
(我摘录了我认为不相关的代码的其他部分-but the entire codebase is here。这是到目前为止的内容:
SSBO用一些数据初始化

std::vector<glm::vec4> data = { glm::vec4(1.0, 0.0, 0.0, 1.0), glm::vec4(1.0, 0.0, 0.0, 1.0) };

GLuint SSBO;
glGenBuffers(1, &SSBO);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO);
glBufferData(GL_SHADER_STORAGE_BUFFER, data.size() * sizeof(glm::vec4), &data[0], GL_DYNAMIC_DRAW);

//the khronos docs put this line in
//glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, SSBO);

glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
更新循环
s_Data.compute_shader.use();

-- snip: bind a texture --

int ssbo_binding = 1;
int block_index = glGetProgramResourceIndex(s_Data.compute_shader.ID, GL_SHADER_STORAGE_BLOCK, "bufferData");
glShaderStorageBlockBinding(s_Data.compute_shader.ID, block_index, ssbo_binding );
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, ssbo_binding, SSBO);

glDispatchCompute( X workers, Y workers, 1);

//Synchronize all writes to the framebuffer image
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);

// Reset bindings
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, ssbo_binding, 0);
glBindImageTexture(0, 0, 0, false, 0, GL_READ_WRITE, GL_RGBA32F);
glUseProgram(0);

-- snip: render output texture to screen --
计算着色器
#version 430 core

layout(binding = 0, rgba16f) uniform image2D outTexture;

layout(std430, binding = 1 ) readonly buffer bufferData
{
    vec4 data[];
};

layout (local_size_x = 16, local_size_y = 8) in;
void main(void) {
    ivec2 px = ivec2(gl_GlobalInvocationID.xy);
    ivec2 size = imageSize(outTexture);

    vec3 color;
    if(data.length() > 0)
    {
        //green = data
        color = vec3(0.2, 0.6, 0.2);
    } else
    {
        //red = bad
        color = vec3(0.6, 0.2, 0.2);
    }

    imageStore(outTexture, px, vec4(color, 1.0));
}
目前,我的屏幕显示为红色,表示没有数据通过SSBO发送。
编辑:
找到了问题。计算着色器中的.length()方法不起作用。

最佳答案

我在计算着色器中发现了问题。
.length()返回错误的值。我查询了data [0]和data [1],它们在计算着色器中返回了正确设置的值-因此这就是问题所在(但我不一定有解决方案)

关于c++ - OpenGL为计算着色器设置SSBO,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63209756/

10-09 00:32