stop excessive allocations with lightmanager callback

cleanup
glassmancody.info 2 years ago
parent 69654b6697
commit 2cea4f3172

@ -476,16 +476,14 @@ namespace SceneUtil
{
osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet;
osg::ref_ptr<osg::IntArray> indices = new osg::IntArray(mLightManager->getMaxLights());
osg::ref_ptr<osg::Uniform> indicesUni = new osg::Uniform(osg::Uniform::Type::INT, "PointLightIndex", indices->size());
osg::ref_ptr<osg::Uniform> indicesUni = new osg::Uniform(osg::Uniform::Type::INT, "PointLightIndex", mLightManager->getMaxLights());
int pointCount = 0;
for (size_t i = 0; i < lightList.size(); ++i)
{
int bufIndex = mLightManager->getLightIndexMap(frameNum)[lightList[i]->mLightSource->getId()];
indices->at(pointCount++) = bufIndex;
indicesUni->setElement(pointCount++, bufIndex);
}
indicesUni->setArray(indices);
stateset->addUniform(indicesUni);
stateset->addUniform(new osg::Uniform("PointLightCount", pointCount));
@ -608,20 +606,29 @@ namespace SceneUtil
class LightManagerCullCallback : public SceneUtil::NodeCallback<LightManagerCullCallback, LightManager*, osgUtil::CullVisitor*>
{
public:
void operator()(LightManager* node, osgUtil::CullVisitor* cv)
LightManagerCullCallback(LightManager* lightManager)
{
osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet;
if (node->getLightingMethod() == LightingMethod::SingleUBO)
for (size_t i = 0; i < mStateSet.size(); ++i)
{
auto buffer = node->getUBOManager()->getLightBuffer(cv->getTraversalNumber());
auto& buffer = lightManager->getUBOManager()->getLightBuffer(i);
#if OSG_VERSION_GREATER_OR_EQUAL(3,5,7)
osg::ref_ptr<osg::UniformBufferBinding> ubb = new osg::UniformBufferBinding(static_cast<int>(Resource::SceneManager::UBOBinding::LightBuffer), buffer->getData(), 0, buffer->getData()->getTotalDataSize());
#else
osg::ref_ptr<osg::UniformBufferBinding> ubb = new osg::UniformBufferBinding(static_cast<int>(Resource::SceneManager::UBOBinding::LightBuffer), buffer->getData()->getBufferObject(), 0, buffer->getData()->getTotalDataSize());
#endif
stateset->setAttributeAndModes(ubb, osg::StateAttribute::ON);
mStateSet[i]->setAttributeAndModes(ubb, osg::StateAttribute::ON);
}
}
void operator()(LightManager* node, osgUtil::CullVisitor* cv)
{
const size_t frameId = cv->getTraversalNumber() % 2;
auto& stateset = mStateSet[frameId];
if (node->getLightingMethod() == LightingMethod::SingleUBO)
{
auto& buffer = node->getUBOManager()->getLightBuffer(cv->getTraversalNumber());
if (auto sun = node->getSunlight())
{
@ -652,6 +659,8 @@ namespace SceneUtil
if (node->getPPLightsBuffer() && cv->getCurrentCamera()->getName() == Constants::SceneCamera)
node->getPPLightsBuffer()->updateCount(cv->getTraversalNumber());
}
std::array<osg::ref_ptr<osg::StateSet>, 2> mStateSet = { new osg::StateSet, new osg::StateSet };
};
UBOManager::UBOManager(int lightCount)
@ -838,7 +847,7 @@ namespace SceneUtil
getOrCreateStateSet()->addUniform(new osg::Uniform("PointLightCount", 0));
addCullCallback(new LightManagerCullCallback);
addCullCallback(new LightManagerCullCallback(this));
}
LightManager::LightManager(const LightManager &copy, const osg::CopyOp &copyop)

Loading…
Cancel
Save