mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-01-27 03:35:27 +00:00
stop excessive allocations with lightmanager callback
This commit is contained in:
parent
69654b6697
commit
2cea4f3172
@ -476,16 +476,14 @@ namespace SceneUtil
|
|||||||
{
|
{
|
||||||
osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet;
|
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", mLightManager->getMaxLights());
|
||||||
osg::ref_ptr<osg::Uniform> indicesUni = new osg::Uniform(osg::Uniform::Type::INT, "PointLightIndex", indices->size());
|
|
||||||
int pointCount = 0;
|
int pointCount = 0;
|
||||||
|
|
||||||
for (size_t i = 0; i < lightList.size(); ++i)
|
for (size_t i = 0; i < lightList.size(); ++i)
|
||||||
{
|
{
|
||||||
int bufIndex = mLightManager->getLightIndexMap(frameNum)[lightList[i]->mLightSource->getId()];
|
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(indicesUni);
|
||||||
stateset->addUniform(new osg::Uniform("PointLightCount", pointCount));
|
stateset->addUniform(new osg::Uniform("PointLightCount", pointCount));
|
||||||
|
|
||||||
@ -608,20 +606,29 @@ namespace SceneUtil
|
|||||||
class LightManagerCullCallback : public SceneUtil::NodeCallback<LightManagerCullCallback, LightManager*, osgUtil::CullVisitor*>
|
class LightManagerCullCallback : public SceneUtil::NodeCallback<LightManagerCullCallback, LightManager*, osgUtil::CullVisitor*>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void operator()(LightManager* node, osgUtil::CullVisitor* cv)
|
LightManagerCullCallback(LightManager* lightManager)
|
||||||
{
|
{
|
||||||
osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet;
|
for (size_t i = 0; i < mStateSet.size(); ++i)
|
||||||
|
|
||||||
if (node->getLightingMethod() == LightingMethod::SingleUBO)
|
|
||||||
{
|
{
|
||||||
auto buffer = node->getUBOManager()->getLightBuffer(cv->getTraversalNumber());
|
auto& buffer = lightManager->getUBOManager()->getLightBuffer(i);
|
||||||
|
|
||||||
#if OSG_VERSION_GREATER_OR_EQUAL(3,5,7)
|
#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());
|
osg::ref_ptr<osg::UniformBufferBinding> ubb = new osg::UniformBufferBinding(static_cast<int>(Resource::SceneManager::UBOBinding::LightBuffer), buffer->getData(), 0, buffer->getData()->getTotalDataSize());
|
||||||
#else
|
#else
|
||||||
osg::ref_ptr<osg::UniformBufferBinding> ubb = new osg::UniformBufferBinding(static_cast<int>(Resource::SceneManager::UBOBinding::LightBuffer), buffer->getData()->getBufferObject(), 0, buffer->getData()->getTotalDataSize());
|
osg::ref_ptr<osg::UniformBufferBinding> ubb = new osg::UniformBufferBinding(static_cast<int>(Resource::SceneManager::UBOBinding::LightBuffer), buffer->getData()->getBufferObject(), 0, buffer->getData()->getTotalDataSize());
|
||||||
#endif
|
#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())
|
if (auto sun = node->getSunlight())
|
||||||
{
|
{
|
||||||
@ -652,6 +659,8 @@ namespace SceneUtil
|
|||||||
if (node->getPPLightsBuffer() && cv->getCurrentCamera()->getName() == Constants::SceneCamera)
|
if (node->getPPLightsBuffer() && cv->getCurrentCamera()->getName() == Constants::SceneCamera)
|
||||||
node->getPPLightsBuffer()->updateCount(cv->getTraversalNumber());
|
node->getPPLightsBuffer()->updateCount(cv->getTraversalNumber());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::array<osg::ref_ptr<osg::StateSet>, 2> mStateSet = { new osg::StateSet, new osg::StateSet };
|
||||||
};
|
};
|
||||||
|
|
||||||
UBOManager::UBOManager(int lightCount)
|
UBOManager::UBOManager(int lightCount)
|
||||||
@ -838,7 +847,7 @@ namespace SceneUtil
|
|||||||
|
|
||||||
getOrCreateStateSet()->addUniform(new osg::Uniform("PointLightCount", 0));
|
getOrCreateStateSet()->addUniform(new osg::Uniform("PointLightCount", 0));
|
||||||
|
|
||||||
addCullCallback(new LightManagerCullCallback);
|
addCullCallback(new LightManagerCullCallback(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
LightManager::LightManager(const LightManager ©, const osg::CopyOp ©op)
|
LightManager::LightManager(const LightManager ©, const osg::CopyOp ©op)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user