mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-01-26 18:35:20 +00:00
Add LightStateCache to avoid redundantly setting the same gl_Light
Normally, osg::State would do this for us (via lastAppliedAttribute), but since we're using a custom StateAttribute to apply all lights at once, we have to track ourselves. Further reduction of GL calls in a typical scene by ~2%
This commit is contained in:
parent
4d4dc1b9c1
commit
c00532d82d
@ -13,6 +13,20 @@
|
||||
namespace SceneUtil
|
||||
{
|
||||
|
||||
class LightStateCache
|
||||
{
|
||||
public:
|
||||
osg::Light* lastAppliedLight[8];
|
||||
};
|
||||
|
||||
LightStateCache* getLightStateCache(unsigned int contextid)
|
||||
{
|
||||
static std::vector<LightStateCache> cacheVector;
|
||||
if (cacheVector.size() < contextid+1)
|
||||
cacheVector.resize(contextid+1);
|
||||
return &cacheVector[contextid];
|
||||
}
|
||||
|
||||
// Resets the modelview matrix to just the view matrix before applying lights.
|
||||
class LightStateAttribute : public osg::StateAttribute
|
||||
{
|
||||
@ -50,8 +64,17 @@ namespace SceneUtil
|
||||
|
||||
state.applyModelViewMatrix(state.getInitialViewMatrix());
|
||||
|
||||
LightStateCache* cache = getLightStateCache(state.getContextID());
|
||||
|
||||
for (unsigned int i=0; i<mLights.size(); ++i)
|
||||
applyLight((GLenum)((int)GL_LIGHT0 + i + mIndex), mLights[i].get());
|
||||
{
|
||||
osg::Light* current = cache->lastAppliedLight[i+mIndex];
|
||||
if (current != mLights[i].get())
|
||||
{
|
||||
applyLight((GLenum)((int)GL_LIGHT0 + i + mIndex), mLights[i].get());
|
||||
cache->lastAppliedLight[i+mIndex] = mLights[i].get();
|
||||
}
|
||||
}
|
||||
|
||||
state.applyModelViewMatrix(modelViewMatrix);
|
||||
}
|
||||
@ -289,12 +312,15 @@ namespace SceneUtil
|
||||
|
||||
META_StateAttribute(SceneUtil, DisableLight, osg::StateAttribute::LIGHT)
|
||||
|
||||
virtual void apply(osg::State&) const
|
||||
virtual void apply(osg::State& state) const
|
||||
{
|
||||
int lightNum = GL_LIGHT0 + mIndex;
|
||||
glLightfv( lightNum, GL_AMBIENT, mNull.ptr() );
|
||||
glLightfv( lightNum, GL_DIFFUSE, mNull.ptr() );
|
||||
glLightfv( lightNum, GL_SPECULAR, mNull.ptr() );
|
||||
|
||||
LightStateCache* cache = getLightStateCache(state.getContextID());
|
||||
cache->lastAppliedLight[mIndex] = NULL;
|
||||
}
|
||||
|
||||
private:
|
||||
|
Loading…
x
Reference in New Issue
Block a user