我一直在尝试初始化 SSBO 并将其传递给计算着色器。
int ssbo = glGenBuffers();
FloatBuffer buff = BufferUtils.createFloatBuffer(4);
buff.put(0.1f);
buff.put(0.4f);
buff.put(1.5f);
buff.put(0.2f);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, buff, GL_DYNAMIC_READ);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
int block_index = glGetProgramResourceIndex(programID, GL_SHADER_STORAGE_BLOCK, "shader_data");
System.out.println(block_index);
int ssbo_binding_point_index = 1;
glShaderStorageBlockBinding(programID, block_index, ssbo_binding_point_index);
int binding_point_index = 1;
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, binding_point_index, ssbo);
在着色器中我有:
layout(binding = 1) buffer shader_data {
vec4 sph;
};
当我运行它时,sph 充满了 0-s。我试图从缓冲区中读取数据:
FloatBuffer a = BufferUtils.createFloatBuffer(4);
glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, a);
System.out.println(glGetError());
//and then print a...
这让我收到错误 1281,也就是 0 + a.size() > ssbo 的大小。然后我检查了 SSBO 的实际大小:
IntBuffer b = BufferUtils.createIntBuffer(1);
glGetBufferParameteriv(GL_SHADER_STORAGE_BUFFER, GL_BUFFER_SIZE, b);
System.out.println("buffer size: " + b.get(0));
这给了我 0。我用过这篇文章。我对 OpenGL 很陌生,所以我的代码中可能有一个非常明显的错误,这就是为什么我在这里包含了这么多。
谢谢指教!
编辑:LWJGL 版本是 3.2.1 build 12