1

I am trying to use a compute shader to write into a buffer.

Setting up the buffer:

glCreateBuffers(1, &m_ssbo);
glNamedBufferStorage(m_ssbo, 1920 * 1080 * 4 * sizeof(GLfloat), nullptr, GL_MAP_WRITE_BIT | GL_MAP_READ_BIT | GL_DYNAMIC_STORAGE_BIT);

Compute Shader:

#version 450 core
layout (local_size_x = 1) in;
layout(std430, binding = 0) restrict writeonly buffer SSBO {
    vec4 color[];
};
void main() {
    color[0] = vec4(1., 0., 0., 1.);
}

Dispatch and Reading:

m_comp_prog.use();
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_ssbo);
glDispatchCompute(1, 1, 0);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);

{
    auto * ptr = m_ssbo.map(0, static_cast<unsigned int>(sizeof(GLfloat)) * 4, GL_MAP_READ_BIT);
    GLfloat * fptr = static_cast<GLfloat *>(ptr);
    std::cout << fptr[0] << std::endl;
    m_ssbo.unmap();
}

The output however is 0 and not 1.

1 Answer 1

3

It looks as if you are never executing the shader:

glDispatchCompute(1, 1, 0);

Although not mentioned in the reference, this page indicatest that parsing 0 to one of the arguments is not allowed. This can be explained since num_group_x * num_group_y * num_group_z workgroups are created, which means that non are created with this parameters (1*1*0 = 0).

To fix the problem, try to call

glDispatchCompute(1, 1, 1);
                        ^

instead, which will create exactly one workgroup.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.