I'm trying to use my tools in shader storage buffer objects (aka Buffer Blocks), and there are a few things that I do not fully understand. What I'm trying to do is to store (simplified) data of an undetermined number of lights nin them, so my shader can iterate over them and perform calculations.
Let me start by getting the correct results and no errors from OpenGL. However, it bothers me not to know why it works.
So, in my shader, I got the following:
struct PointLight {
vec3 pos;
float intensity;
};
layout (std430, binding = 0) buffer PointLights {
PointLight pointLights[];
};
void main() {
PointLight light;
for (int i = 0; i < pointLights.length(); i++) {
light = pointLights[i];
// etc
}
}
and in my application:
struct PointLightData {
glm::vec3 pos;
float intensity;
};
class PointLight {
PointLightData data;
};
std::vector<PointLight*> pointLights;
glGenBuffers(1, &BBO);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, BBO);
glNamedBufferStorage(BBO, n * sizeof(PointLightData), NULL, GL_DYNAMIC_STORAGE_BIT);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, BBO);
...
for (unsigned int i = 0; i < pointLights.size(); i++) {
glNamedBufferSubData(BBO, i * sizeof(PointLightData), sizeof(PointLightData), &(pointLights[i]->data));
}
In this last cycle, I save the structure PointLightDatawith an offset equal to its size times the number of them that I already saved (so the offset is 0for the first).
, , . , .. .
. std430 - , std140, . ? , , std430 PointLights, , PointLightData struct ( , ). , ?
, , , , opengl glGetUniformIndices glGetActiveUniformsiv ( GL_UNIFORM_SIZE GL_UNIFORM_OFFSET), , Uniform Blocks, Buffer Blocks, . , OpenGL, , 1281 (- 3432898282 - ):
const char * names[2] = {
"pos", "intensity"
};
GLuint indices[2];
GLint size[2];
GLint offset[2];
glGetUniformIndices(shaderProgram->id, 2, names, indices);
glGetActiveUniformsiv(shaderProgram->id, 2, indices, GL_UNIFORM_SIZE, size);
glGetActiveUniformsiv(shaderProgram->id, 2, indices, GL_UNIFORM_OFFSET, offset);
, glGetUniformIndices glGetActiveUniformsiv ?
, , , ? H , . OpenGL / , , , (, ), .