I have already posted this question over on stackoverflow (I cannot put a link here, I’m sorry)
I tried porting my OpenCL raycaster to GLSL compute shaders. Only I’m having trouble with shader storage buffers. How I initiaize the buffer (constructor of class BufferGL):
GLBuffer::GLBuffer(GLint type, GLint usage, void * data, GLuint size)
{
this->type = type;
glGenBuffers(1, &buffer);
this->bind();
glBufferData(type, size, data, usage);
this->unbind();
}
this is how (try to) attach the buffer to the shader:
void Shader::connectShaderStorageBuffer(GLBuffer & buffer, GLuint binding_point)
{
this->bind();
buffer.bind();
buffer.bindBase(GL_SHADER_STORAGE_BUFFER, binding_point);
buffer.unbind();
this->unbind();
}
Putting the two together:
GLfloat f[3] = { 1, 2, 3 };
testBuffer = GLBuffer(GL_SHADER_STORAGE_BUFFER, GL_STATIC_COPY, &f, sizeof(f));
shader->connectShaderStorageBuffer(testBuffer, 2);
GLSL shader:
layout(std430, binding = 2) buffer floats
{
float f[3];
};
void main()
{
ivec2 coords = ivec2(gl_GlobalInvocationID.xy);
//vec2 glcoords = vec2(coords.x/500, coords.y/500);
//Ray r = camToRay(cam[0], glcoords);
if(f[0] == 1)
{
imageStore(screen, coords, vec4(1,0,0,1));
}else
{
imageStore(screen, coords, vec4(0,1,1,1));
}
}
The shader works (without the “if(f[0] == 1)”-clause), however when reading from f I get a black screen and around 6fps (the shader doesn’t contain more code than shown). Any comment/suggesttion and of course answer is appreciated!