<div dir="ltr"><div class="gmail_extra"><div class="gmail_signature"><div dir="ltr"><div>Hello,</div><div> I've seen that OSG has some basic primitives for uniform buffers, but they seem designed for homogeneous data. Most modern engines use uniform buffers to represent various levels of shader constants based on frequency of state changing, so you might have a per frame UBO/per pass UBO, a material UBO, and a per draw call UBO. (one-off object changes might be the done with the simpler uniform system.) </div><div><br></div><div>Typically that data is not homogeneous, but represented by a C structure that has to be somewhat carefully laid out. </div><div>For example I've switched to a UBO for OSG's transform stack:</div><div>struct osg_TransformStack</div><div dir="ltr">{</div><div dir="ltr"> osg::Matrixf ModelViewMatrixInverse;</div><div dir="ltr"> osg::Matrixf ModelViewMatrix;</div><div dir="ltr"> osg::Matrixf ModelViewProjectionMatrix;</div><div dir="ltr"> osg::Matrixf ProjectionMatrix;</div><div dir="ltr"> osg::Matrix3 NormalMatrix;</div><div dir="ltr">};</div><div dir="ltr"><br></div><div>That can get mapped to a UBO via </div><div><div> _extensions->glBufferData(GL_UNIFORM_BUFFER, sizeof(osg_TransformStack), &osg_transformstack, GL_DYNAMIC_DRAW);</div></div><div><br></div><div>And the UBO in glsl would look like:</div><div dir="ltr"><br></div><div dir="ltr">struct osg_TransformStack<br></div><div dir="ltr">{</div><div dir="ltr"> mat4 ModelViewMatrixInverse;</div><div dir="ltr"> mat4 ModelViewMatrix;</div><div dir="ltr"> mat4 ModelViewProjectionMatrix;</div><div dir="ltr"> mat4 ProjectionMatrix;</div><div dir="ltr"> mat3 NormalMatrix; // Mat3's don't actually map great to std140 layouts</div><div dir="ltr">};</div><div dir="ltr"><br></div><div dir="ltr">layout(std140) uniform osg_transform_stack_block<br></div><div dir="ltr">{</div><div dir="ltr"> osg_TransformStack osg;</div><div dir="ltr">};</div><div><br></div><div>Replacing the individual uniforms that OSG adds to replace the legacy gl_*Matrix calls in State::convertVertexShaderSourceToOsgBuiltIns() with a UBO saves me a few ms a frame, but I'm doing all the uniform buffer management by hand and I've had to significantly modify the state class. (I think some of the performance gain is from avoiding OSG's uniform management abstractions as well.) I've had to do something similar with materials, otherwise I have significant performance degradation when switching off the fixed function pipeline.</div><div> Has anyone else experimented with UBO's in this manor? Is there a cleaner OSG way to do that's still high performance?</div><div><br></div><div>Gedalia</div></div></div>
</div></div>