过去几天,我一直对此感到困扰。我已经阅读了许多其他SO帖子和教程:SO question Khronos tutorial 这是一个简单的程序,用于在我完成较大的任务之前熟悉计算缓冲区。到目前为止,该程序假设仅运行两个将数字1插入缓冲区数组的第一个元素的线程。
int main() {
int SIZE = 3;
float test[] = {0.f, 0.f, 0.f};
GLuint buf = 0;
// ... compile, setup code
glUseProgram(progHandle);
// some glUniform stuff to modify uniforms in shader
glGenBuffers (1, &buf);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, buf);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(test), &test, GL_DYNAMIC_DRAW);
glDispatchCompute(SIZE/2, 1, 1);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0);
GLfloat *p = NULL;
glBindBuffer(GL_SHADER_STORAGE_BUFFER, buf);
p = (GLfloat *) glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_READ_ONLY);
if (p)
for(int i = 0; i < SIZE; i++)
test[i] = p[i];
else
printf("p is null %d\n", glGetError();
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0);
p = NULL;
for(int i = 0; i < SIZE; i++)
std::cout << test[i] << " ";
}
着色器:
#version 430 core
layout (local_size_x = 2) in;
layout(std430, binding=0) writeonly buffer res {
float result[];
};
// some uniforms (these arn't being used rn)
void main(){
result[0] = 1.0f;
}
输出为:
p is null: 0
0 0 0
我期望1 0 0,但是由于某种原因,p总是返回null,而没有来自glGetError的错误。如果VMem低于我预期的NO_MEMORY错误,但是返回NULL而没有错误的事实令人困惑。更是如此,因为此代码与链接的SO帖子非常相似。
我在整个代码中都进行了错误检查(在链接,编译,绑定期间),但从未提出过。