'Instanced rendering using vertex attribute buffer or uniform buffer for the instance data
I have seen instanced rendering using a vertex attribute buffer. So in addition to your usual data such as 'position' and 'normal' you have an extra 'attribute' for the instanced data:
in vec3 position;
in vec3 normal;
in instanceData
{
mat4 worldMatrix;
};
For each vertex the pointer for 'position' and 'normal' is advanced the stride of one vertex, but the pointer for 'in instanceData' is advanced one stride length for each instance drawn. In OpenGL this is set with the glVertexAttribDivisor function, and in Direct3D I think with IndexCountPerInstance.
I was wondering however whether instanced rendering could be done with the 'instanceData' buffer read from a uniform or constant buffer. In OpenGL this would like this:
in vec3 position;
in vec3 normal;
uniform instanceData
{
mat4 worldMatrix[4000]; // Drawing 4000 objects
};
int main()
{
worldMatrix[glInstanceID]; // If we're drawing for example the 4th instance then the instance data
// is read from the uniform buffer using the instance ID at index 4
}
This way your instance data could be fed to a uniform buffer instead having it as a vertex attribute, which would require changing the vertex input layout when you wanted to instanced render. I know in OpenGL uniform buffers are of a limited size and so would you would be restricted to fewer instances than using the vertex attribute method. But in the case you needed to draw that many instances you can always use a Shader Storage Buffer Object, which allow for much bigger buffers (maybe unlimited), so SSBOs can sort of act like uniforms anyway.
I'm just wondering:
Whether using the uniform buffer method is doable/a good idea, and what are the tradeoffs between the two methods? For example, is the uniform buffer method slower? You'd be indexing into it at each shader call, for each vertex.
Is using this method still instanced rendering? I've only seen examples of instanced rendering using the vertex attribute method.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|