Shader Storage Buffer Object是GLSL着色器的buffer,不同于uniform buffer在着色器不可修改,shader storage buffer是可读可写的。修改的内容给其他着色器调用或者应用程序本身。
Buffer Block
layout (std430, binding=1) buffer shader_storage_block_data
{
vec3 uLightDirectionE;
vec3 uMaterialAmbient;
vec3 uMaterialDiffuse;
vec3 uLightAmbient;
vec3 uLightDiffuse;
};
采用内存布局方式为430,绑定点为1。
初始化
m_ShaderStorageBlockData.uLightDirectionE = glm::vec4(1, 1, 1, 0);
m_ShaderStorageBlockData.uMaterialAmbient = glm::vec4(0.3, 0.3, 0.3, 0);
m_ShaderStorageBlockData.uMaterialDiffuse = glm::vec4(0.9, 0.9, 0.9, 0);
m_ShaderStorageBlockData.uLightAmbient = glm::vec4(0.6, 0.6, 0.6, 0);
m_ShaderStorageBlockData.uLightDiffuse = glm::vec4(0.9, 0.9, 0.9, 0);
m_ShaderStorageBlockIndex = glGetProgramResourceIndex(m_pEffect->getProgramID(0), GL_SHADER_STORAGE_BLOCK, "shader_storage_block_data");
GLint SSBOBinding=0, BlockDataSize = 0;
glGetIntegerv(GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS, &SSBOBinding);
glGetIntegerv(GL_MAX_SHADER_STORAGE_BLOCK_SIZE, &BlockDataSize);
glGenBuffers(1, &m_SSBO);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_SSBO);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(shader_storage_block_data), &m_ShaderStorageBlockData, GL_DYNAMIC_DRAW);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
建立连接
建立buffer block和shader storage buffer的连接,也是通过绑定点完成。
GLuint BindingPointIndex = 1;
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, BindingPointIndex, m_SSBO);
glShaderStorageBlockBinding(m_pEffect->getProgramID(0), m_ShaderStorageBlockIndex, BindingPointIndex);
m_pMesh->render();
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
其实SSBO和UBO用法类似,所以参照Uniform Buffer Object即可。
参考资料
【1】https://www.opengl.org/wiki/Shader_Storage_Buffer_Object
【2】http://www.geeks3d.com/20140704/tutorial-introduction-to-opengl-4-3-shader-storage-buffers-objects-ssbo-demo/