opengl:将数组类型加载到统一缓冲区对象
opengl : loading array types to uniform buffer objects
这是我在片段着色器中的统一块
layout (std140) uniform Material
{
float array[2];
};
这是统一块对象。我正在使用 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);}
}
对于常规的非数组变量,它工作正常,但对于数组,浮点数和每个数组类型值加载不正确。我已经阅读了指定数组中每个元素都具有 16[vec4 大小] 对齐方式的文档,但我不知道如何在我的代码中指定它
我只是使用
更新浮点数组
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()
但是浮点数再次加载不正确。
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 [...]
你说得对,你必须为每个元素添加 3 个浮点数:
BufferUtils.createFloatBuffer(8).put(new float[]{10.5f,0.0f,0.0f,0.0f,3.0f,0.0f,0.0f,0.0f}).flip()
但是,我建议使用单个 vec2
而不是 float array[2]
:
layout (std140) uniform Material
{
vec2 array;
};
注意,在 GLSL 中,可以使用 Swizzling 或索引运算符(例如 array[0]
或 array[1]
)访问矢量数据类型的组件。
这是我在片段着色器中的统一块
layout (std140) uniform Material
{
float array[2];
};
这是统一块对象。我正在使用 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);}
}
对于常规的非数组变量,它工作正常,但对于数组,浮点数和每个数组类型值加载不正确。我已经阅读了指定数组中每个元素都具有 16[vec4 大小] 对齐方式的文档,但我不知道如何在我的代码中指定它
我只是使用
更新浮点数组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()
但是浮点数再次加载不正确。
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 [...]
你说得对,你必须为每个元素添加 3 个浮点数:
BufferUtils.createFloatBuffer(8).put(new float[]{10.5f,0.0f,0.0f,0.0f,3.0f,0.0f,0.0f,0.0f}).flip()
但是,我建议使用单个 vec2
而不是 float array[2]
:
layout (std140) uniform Material
{
vec2 array;
};
注意,在 GLSL 中,可以使用 Swizzling 或索引运算符(例如 array[0]
或 array[1]
)访问矢量数据类型的组件。