fix race condition with sun uniform

pull/3065/head
glassmancody.info 4 years ago
parent b7adb9d088
commit 8ce65232ff

@ -644,7 +644,7 @@ namespace SceneUtil
configureAmbient(lightMat, sun->getAmbient());
configureDiffuse(lightMat, sun->getDiffuse());
configureSpecular(lightMat, sun->getSpecular());
mLightManager->getStateSet()->getUniform("LightBuffer")->setElement(0, lightMat);
mLightManager->setSunlightBuffer(lightMat, mLastFrameNumber);
}
else
{
@ -767,6 +767,36 @@ namespace SceneUtil
osg::ref_ptr<osg::Program> mDummyProgram;
};
class LightManagerStateAttributePerObjectUniform : public osg::StateAttribute
{
public:
LightManagerStateAttributePerObjectUniform()
: mLightManager(nullptr) {}
LightManagerStateAttributePerObjectUniform(LightManager* lightManager)
: mLightManager(lightManager)
{
}
LightManagerStateAttributePerObjectUniform(const LightManagerStateAttributePerObjectUniform& copy, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY)
: osg::StateAttribute(copy,copyop), mLightManager(copy.mLightManager) {}
int compare(const StateAttribute &sa) const override
{
throw std::runtime_error("LightManagerStateAttributePerObjectUniform::compare: unimplemented");
}
META_StateAttribute(NifOsg, LightManagerStateAttributePerObjectUniform, osg::StateAttribute::LIGHT)
void apply(osg::State& state) const override
{
mLightManager->getStateSet()->getUniform("LightBuffer")->setElement(0, mLightManager->getSunlightBuffer(state.getFrameStamp()->getFrameNumber()));
}
private:
LightManager* mLightManager;
};
const std::unordered_map<std::string, LightingMethod> LightManager::mLightingMethodSettingMap = {
{"legacy", LightingMethod::FFP}
,{"shaders compatibility", LightingMethod::PerObjectUniform}
@ -845,6 +875,7 @@ namespace SceneUtil
initSingleUBO(targetLights);
getOrCreateStateSet()->addUniform(new osg::Uniform("PointLightCount", 0));
getOrCreateStateSet()->setAttributeAndModes(new LightManagerStateAttributePerObjectUniform(this), osg::StateAttribute::ON);
addCullCallback(new LightManagerCullCallback(this));
}

@ -179,6 +179,9 @@ namespace SceneUtil
auto& getLightBuffer(size_t frameNum) { return mLightBuffers[frameNum%2]; }
osg::Matrixf getSunlightBuffer(size_t frameNum) const { return mSunlightBuffers[frameNum%2]; }
void setSunlightBuffer(const osg::Matrixf& buffer, size_t frameNum) { mSunlightBuffers[frameNum%2] = buffer; }
std::map<std::string, std::string> getLightDefines() const;
void processChangedSettings(const Settings::CategorySettingVector& changed);
@ -217,6 +220,8 @@ namespace SceneUtil
osg::ref_ptr<LightBuffer> mLightBuffers[2];
osg::Matrixf mSunlightBuffers[2];
// < Light ID , Buffer Index >
using LightIndexMap = std::unordered_map<int, int>;
LightIndexMap mLightIndexMaps[2];

Loading…
Cancel
Save