ComputeShader和SSBO读/写问题

时间:2018-08-16 14:00:31

标签: opengl glsl shader compute-shader

我目前正在尝试使此代码段正常工作。我试图在ComputeShader上进行计算,并在裂开时读出来。这是Computeshader:

#version 430 core

layout(local_size_x = 2) in;
layout(std430, binding = 0) writeonly buffer Picture{
  float picture[];
};

void main() {
  picture[gl_GlobalInvocationID.x] = 1.0f;
}

,这是main(

// ... Creating Compiling and Linking Shader Program ...

std::vector<GLfloat> picture;
picture.reserve(1920*1080*3);

glUseProgram(g_calc_program);
GLuint SSBO;
glGenBuffers(1, &SSBO);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, SSBO);
glBufferData(GL_SHADER_STORAGE_BUFFER, 1920*1080*3 * sizeof(GLfloat), &picture, GL_DYNAMIC_DRAW);

glDispatchCompute( 1920*1080*3/2, 1, 1);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0);

glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO);


GLfloat *ptr;
ptr = (GLfloat *) glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_READ_ONLY);

for(int i = 0; i < 1920*1080*3; i++){
  std::cout << ptr[i] << ", ";
}

glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);

cout是一堆零,这些零是通过保留初始向量中的空间而创建的,而不是ComputeShader正在编写的1.0。我究竟做错了什么?

0 个答案:

没有答案