计算着色器不写入SSBO



我正在编写一个简单的测试计算着色器,将值5.0写入缓冲区中的每个元素。缓冲区的值被初始化为-1,这样我就知道创建缓冲区和读取缓冲区是否有问题。

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;
};

我的计算着色器相当简单:

#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;
}

当我读取缓冲区时,大多数是-1,但有些数据由随机浮点值(-nan, 0等)组成。这是怎么回事?

EDIT:将内存屏障更改为GL_BUFFER_UPDATE_BARRIER_BIT(甚至GL_ALL_BARRIER_BITS)并不能解决问题;我不明白为什么这个问题是重复的

这个小问题花了我很长时间才弄明白。这里有几个错误。

这条线

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

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

以便我们复制出正确的字节数。这个block应该是

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);
应该

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);

glBindBufferBase调用将ssbo对象作为第三个参数。不知道为什么我一开始给它0。此外,glBindBuffer调用是不必要的(为了计算着色器的目的,我们只需要绑定到一个索引缓冲区目标)。

相关内容

  • 没有找到相关文章

最新更新