Compute shader not writing to SSBO

a 夏天 提交于 2019-12-23 02:36:07

问题


I'm writing a simple test compute shader that writes a value of 5.0 to every element in a buffer. The buffer's values are initialized to -1, so that I know whether or not creating the buffer and reading the buffer are the problem.

class ComputeShaderWindow : public QOpenGLWindow {
  public:
    void initializeGL() {
        // Create the opengl functions object
        gl = context()->versionFunctions<QOpenGLFunctions_4_3_Core>();
        m_compute_program = new QOpenGLShaderProgram(this);
        auto compute_shader_s = fs::readFile(
                                    "test_assets/example_compute_shader.comp");
        // Adds the compute shader, then links and binds it
        m_compute_program->addShaderFromSourceCode(QOpenGLShader::Compute,
                compute_shader_s);
        m_compute_program->link();
        m_compute_program->bind();

        // Fills the buffer with -1, so we know whether the problem
        // is the compute shader not being invoked or not reading
        // the buffer correctly afterwards.
        GLfloat* default_values = new GLfloat[NUM_INVOCATIONS];
        std::fill(default_values, default_values + NUM_INVOCATIONS, -1.0);
        GLuint ssbo;
        gl->glGenBuffers(1, &ssbo);
        gl->glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
        gl->glBufferData(GL_SHADER_STORAGE_BUFFER,
                         NUM_INVOCATIONS,
                         default_values,
                         GL_DYNAMIC_DRAW);
        gl->glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
        gl->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0);
        gl->glDispatchCompute(NUM_INVOCATIONS / WORKGROUP_SIZE, 1, 1);
        gl->glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
        gl->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0);
        gl->glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);

        // Now map the buffer so that we can check its values
        GLfloat* read_data = (GLfloat*) gl->glMapBuffer(GL_SHADER_STORAGE_BUFFER,
                            GL_READ_ONLY);
        std::vector<GLfloat> buffer_data(NUM_INVOCATIONS);

        for (int i = 0; i < NUM_INVOCATIONS; i++) {
            buffer_data[i] = read_data[i];
        }

        gl->glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);

        for (int i = 0; i < NUM_INVOCATIONS; i++) {
            DEBUG(buffer_data[i]);
        }
        assert(gl->glGetError() == GL_NO_ERROR);
    }

    void resizeGL(int width, int height) {

    }

    void paintGL() {

    }

    void teardownGL() {

    }

  private:
    QOpenGLFunctions_4_3_Core* gl;
    QOpenGLShaderProgram* m_compute_program;
    static constexpr int NUM_INVOCATIONS = 9000;
    static constexpr int WORKGROUP_SIZE = 128;
};

My compute shader is fairly simple:

#version 430 core

layout(std430, binding = 0) writeonly buffer SSBO {
    float data[];
};

layout(local_size_x = 128) in;

void main() {
    uint ident = gl_GlobalInvocationID.x;
    data[ident] = 5.0f;
}

When I read the buffer, most it is -1, but some of the data is comprised of random float values (-nan, 0, etc). What's going on here?

EDIT: Changing the memory barrier to GL_BUFFER_UPDATE_BARRIER_BIT (or even GL_ALL_BARRIER_BITS) does not fix the problem; I don't understand how this question is a duplicate.


回答1:


This little problem took me far too long to figure out. There's a couple mistakes here.

This line

gl->glBufferData(GL_SHADER_STORAGE_BUFFER,
                     NUM_INVOCATIONS,
                     default_values,
                     GL_DYNAMIC_DRAW);

should be

gl->glBufferData(GL_SHADER_STORAGE_BUFFER,
                     NUM_INVOCATIONS * sizeof(float),
                     default_values,
                     GL_DYNAMIC_DRAW);

so that we copy the correct number of bytes out. And this block should be

gl->glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
gl->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0);
gl->glDispatchCompute(NUM_INVOCATIONS / WORKGROUP_SIZE, 1, 1);
gl->glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
gl->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0);
gl->glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);

should be

gl->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, ssbo);
gl->glDispatchCompute(NUM_INVOCATIONS / WORKGROUP_SIZE, 1, 1);
gl->glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
gl->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, ssbo);

The glBindBufferBase calls take the ssbo object as the third parameter. Don't know why I gave it 0 originally. Also the glBindBuffer calls are unnecessary (for the purposes of the compute shader, we just need to bind to an indexed buffer target).



来源:https://stackoverflow.com/questions/38084241/compute-shader-not-writing-to-ssbo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!