Here is my uniform block in the fragment shader
layout (std140) uniform Material
{
float array[2];
};
Here is the Uniform Block Object . I am using LWJGL 2.9.3
private UBO(int programID,String blockName)
{
blockID=GL31.glGetUniformBlockIndex(programID,blockName); //Get index of uniform block
IntBuffer indices=BufferUtils.createIntBuffer(16);
GL31.glGetActiveUniformBlock(programID,blockID,GL31.GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES,indices); //query all the indices of every variable in the uniform block
indices.flip().limit(GL31.glGetActiveUniformBlocki(programID,blockID,GL31.GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS)); //the above method requires an int buffer of 16 even if i dont have 16 variables, but after retriving indices limit the buffer to read only actual number of variables
while(indices.hasRemaining())
{
int variableIndex=indices.get();
String name=GL31.glGetActiveUniformName(programID
,variableIndex
,GL31.glGetActiveUniformsi(programID,variableIndex,GL31.GL_UNIFORM_NAME_LENGTH)); //get the name of the variable
int offset=GL31.glGetActiveUniformsi(programID,variableIndex,GL31.GL_UNIFORM_OFFSET); //query the offset[will use it in buffer sub data]
offsets.put(name,offset);
}
GL15.glBindBuffer(GL31.GL_UNIFORM_BUFFER,bufferID=GL15.glGenBuffers());
GL15.glBufferData(GL31.GL_UNIFORM_BUFFER
,GL31.glGetActiveUniformBlocki(programID,blockID,GL31.GL_UNIFORM_BLOCK_DATA_SIZE)
,GL15.GL_DYNAMIC_DRAW); //allocate buffer to the actual size of the uniform block
GL15.glBindBuffer(GL31.GL_UNIFORM_BUFFER,0);
}
private void bufferData(String name,Buffer value)
{
int offset=offsets.get(name); //update float or int arrays
if(value instanceof FloatBuffer){GL15.glBufferSubData(GL31.GL_UNIFORM_BUFFER,offset,(FloatBuffer)value);}
else{GL15.glBufferSubData(GL31.GL_UNIFORM_BUFFER,offset,(IntBuffer)value);}
}
With regular non array variables it works fine but with arrays the float and every array type values are loaded incorrectly. I have read the docs specifying that every element in the array has an 16[vec4 size] alignment but i don't know how to specify that in my code
to update the float array i simply use
bufferData("array[0]",BufferUtils.createFloatBuffer(2).put(new float[]{10.5f,3.0f}).flip());
//the array[0] dosen't actually refer to only the first element of the array it is the name of the array returned by GL31.glGetActiveUniformName()
but again the floats are all loaded incorrectly.