mirror of
https://github.com/OpenMW/openmw.git
synced 2025-01-21 08:23:53 +00:00
Merge branch 'ubb_fix' into 'master'
Remove excessive allocations in lightmanager See merge request OpenMW/openmw!2251
This commit is contained in:
commit
611180a5d8
1 changed files with 23 additions and 11 deletions
|
@ -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,32 @@ 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 (!lightManager->getUBOManager())
|
||||
return;
|
||||
|
||||
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 +662,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 +850,7 @@ namespace SceneUtil
|
|||
|
||||
getOrCreateStateSet()->addUniform(new osg::Uniform("PointLightCount", 0));
|
||||
|
||||
addCullCallback(new LightManagerCullCallback);
|
||||
addCullCallback(new LightManagerCullCallback(this));
|
||||
}
|
||||
|
||||
LightManager::LightManager(const LightManager ©, const osg::CopyOp ©op)
|
||||
|
|
Loading…
Reference in a new issue