我正在编写一个简单的测试计算着色器,它将值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等)组成。这里发生了什么?
编辑:将内存屏障更改为GL_BUFFER_UPDATE_BARRIER_BIT
(甚至GL_ALL_BARRIER_BITS
)并不能解决问题;我不明白这个问题是如何重复的。
答案 0 :(得分: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).