From 1fc38d496f3d1c386773c4a50cd08a19033cdfab Mon Sep 17 00:00:00 2001 From: Areloch Date: Fri, 7 Dec 2018 00:30:08 -0600 Subject: [PATCH] Added deletion tracking so when we delete a probe, it'll delete the prefilter/irrad bakes if they exist. WIP of static cubemap bake(not working). --- .../source/T3D/lighting/reflectionProbe.cpp | 109 +++++++++++------- Engine/source/T3D/lighting/reflectionProbe.h | 4 + Engine/source/console/simObject.h | 2 +- Engine/source/lighting/lightManager.cpp | 30 ++--- Engine/source/lighting/probeManager.cpp | 39 +++---- 5 files changed, 106 insertions(+), 78 deletions(-) diff --git a/Engine/source/T3D/lighting/reflectionProbe.cpp b/Engine/source/T3D/lighting/reflectionProbe.cpp index b068849f0..5dc796b88 100644 --- a/Engine/source/T3D/lighting/reflectionProbe.cpp +++ b/Engine/source/T3D/lighting/reflectionProbe.cpp @@ -301,6 +301,26 @@ void ReflectionProbe::onRemove() Parent::onRemove(); } +void ReflectionProbe::deleteObject() +{ + //we're deleting it? + //Then we need to clear out the processed cubemaps(if we have them) + + String prefilPath = getPrefilterMapPath(); + if (Platform::isFile(prefilPath)) + { + Platform::fileDelete(prefilPath); + } + + String irrPath = getIrradianceMapPath(); + if (Platform::isFile(irrPath)) + { + Platform::fileDelete(irrPath); + } + + Parent::deleteObject(); +} + void ReflectionProbe::setTransform(const MatrixF & mat) { // Let SceneObject handle all of the matrix manipulation @@ -424,8 +444,14 @@ void ReflectionProbe::unpackUpdate(NetConnection *conn, BitStream *stream) { mUseCubemap = stream->readFlag(); + String newCubemapName; stream->read(&mCubemapName); + //if (newCubemapName != mCubemapName) + { + processStaticCubemap(); + } + isMaterialDirty = true; } @@ -502,6 +528,42 @@ void ReflectionProbe::updateProbeParams() mProbeInfo->mScore = mMaxDrawDistance; } +void ReflectionProbe::processStaticCubemap() +{ + createClientResources(); + + Sim::findObject(mCubemapName, mStaticCubemap); + + if (!mStaticCubemap) + { + Con::errorf("ReflectionProbe::updateMaterial() - unable to find static cubemap file!"); + return; + } + + if (mStaticCubemap->mCubemap == nullptr) + { + mStaticCubemap->createMap(); + mStaticCubemap->updateFaces(); + } + + String prefilPath = getPrefilterMapPath(); + String irrPath = getIrradianceMapPath(); + + //if (!Platform::isFile(irrPath) || !Platform::isFile(prefilPath)) + { + GFXTextureTargetRef renderTarget = GFX->allocRenderToTextureTarget(false); + + /*IBLUtilities::GenerateIrradianceMap(renderTarget, mStaticCubemap->mCubemap, mIrridianceMap->mCubemap); + IBLUtilities::GeneratePrefilterMap(renderTarget, mStaticCubemap->mCubemap, mPrefilterMipLevels, mPrefilterMap->mCubemap); + + IBLUtilities::SaveCubeMap(getIrradianceMapPath(), mIrridianceMap->mCubemap); + IBLUtilities::SaveCubeMap(getPrefilterMapPath(), mPrefilterMap->mCubemap);*/ + } + + mProbeInfo->mCubemap = &mPrefilterMap->mCubemap; + mProbeInfo->mIrradianceCubemap = &mIrridianceMap->mCubemap; +} + void ReflectionProbe::updateMaterial() { createClientResources(); @@ -518,47 +580,6 @@ void ReflectionProbe::updateMaterial() { mProbeInfo->mIrradianceCubemap = &mIrridianceMap->mCubemap; } - if (mBrdfTexture.isValid()) - { - mProbeInfo->mBRDFTexture = &mBrdfTexture; - } - } - else if (mReflectionModeType == StaticCubemap && !mCubemapName.isEmpty()) - { - Sim::findObject(mCubemapName, mStaticCubemap); - - if (!mStaticCubemap) - { - Con::errorf("ReflectionProbe::updateMaterial() - unable to find static cubemap file!"); - return; - } - - if (mStaticCubemap->mCubemap == nullptr) - { - mStaticCubemap->createMap(); - mStaticCubemap->updateFaces(); - } - - //GFXTextureTargetRef renderTarget = GFX->allocRenderToTextureTarget(false); - - //IBLUtilities::GenerateIrradianceMap(renderTarget, mStaticCubemap->mCubemap, mIrridianceMap->mCubemap); - //IBLUtilities::GeneratePrefilterMap(renderTarget, mStaticCubemap->mCubemap, mPrefilterMipLevels, mPrefilterMap->mCubemap); - - mProbeInfo->mCubemap = &mStaticCubemap->mCubemap; - mProbeInfo->mIrradianceCubemap = &mStaticCubemap->mCubemap; - - /*if (mPrefilterMap != nullptr && mPrefilterMap->mCubemap.isValid()) - { - mProbeInfo->mCubemap = &mPrefilterMap->mCubemap; - } - if (mIrridianceMap != nullptr && mIrridianceMap->mCubemap.isValid()) - { - mProbeInfo->mIrradianceCubemap = &mIrridianceMap->mCubemap; - }*/ - if (mBrdfTexture.isValid()) - { - mProbeInfo->mBRDFTexture = &mBrdfTexture; - } } } else if (mReflectionModeType == DynamicCubemap && !mDynamicCubemap.isNull()) @@ -566,6 +587,11 @@ void ReflectionProbe::updateMaterial() mProbeInfo->mCubemap = &mDynamicCubemap; } + if (mBrdfTexture.isValid()) + { + mProbeInfo->mBRDFTexture = &mBrdfTexture; + } + //Make us ready to render if (mEnabled) mProbeInfo->mIsEnabled = true; @@ -584,7 +610,6 @@ bool ReflectionProbe::createClientResources() mIrridianceMap->createMap(); } - String irrPath = getIrradianceMapPath(); if (Platform::isFile(irrPath)) { diff --git a/Engine/source/T3D/lighting/reflectionProbe.h b/Engine/source/T3D/lighting/reflectionProbe.h index e1d430016..b4f78592f 100644 --- a/Engine/source/T3D/lighting/reflectionProbe.h +++ b/Engine/source/T3D/lighting/reflectionProbe.h @@ -200,6 +200,8 @@ public: bool onAdd(); void onRemove(); + virtual void deleteObject(); + // Override this so that we can dirty the network flag when it is called void setTransform(const MatrixF &mat); @@ -230,6 +232,8 @@ public: bool createClientResources(); void generateTextures(); + void processStaticCubemap(); + // This is the function that allows this object to submit itself for rendering void prepRenderImage(SceneRenderState *state); diff --git a/Engine/source/console/simObject.h b/Engine/source/console/simObject.h index 585944df7..641ddb96f 100644 --- a/Engine/source/console/simObject.h +++ b/Engine/source/console/simObject.h @@ -744,7 +744,7 @@ class SimObject: public ConsoleObject, public TamlCallbacks void unregisterObject(); /// Unregister, mark as deleted, and free the object. - void deleteObject(); + virtual void deleteObject(); /// Performs a safe delayed delete of the object using a sim event. void safeDeleteObject(); diff --git a/Engine/source/lighting/lightManager.cpp b/Engine/source/lighting/lightManager.cpp index 8f99ac0de..7f806a058 100644 --- a/Engine/source/lighting/lightManager.cpp +++ b/Engine/source/lighting/lightManager.cpp @@ -225,18 +225,20 @@ void LightManager::registerGlobalLights( const Frustum *frustum, bool staticLigh else { // Cull the lights using the frustum. - getSceneManager()->getContainer()->findObjectList( *frustum, lightMask, &activeLights ); - if (enableZoneLightCulling) - { - for (U32 i = 0; i < activeLights.size(); ++i) - { - if (!getSceneManager()->mRenderedObjectsList.contains(activeLights[i])) - { - activeLights.erase(i); - --i; - } - } - } + getSceneManager()->getContainer()->findObjectList(*frustum, lightMask, &activeLights); + + if (enableZoneLightCulling) + { + for (U32 i = 0; i < activeLights.size(); ++i) + { + if (!getSceneManager()->mRenderedObjectsList.contains(activeLights[i])) + { + activeLights.erase(i); + --i; + } + } + } + // Store the culling position for sun placement // later... see setSpecialLight. mCullPos = frustum->getPosition(); @@ -246,10 +248,10 @@ void LightManager::registerGlobalLights( const Frustum *frustum, bool staticLigh // the shape bounds and can often get culled. GameConnection *conn = GameConnection::getConnectionToServer(); - if ( conn->getControlObject() ) + if (conn->getControlObject()) { GameBase *conObject = conn->getControlObject(); - activeLights.push_back_unique( conObject ); + activeLights.push_back_unique(conObject); } } diff --git a/Engine/source/lighting/probeManager.cpp b/Engine/source/lighting/probeManager.cpp index f92cebcca..995527db8 100644 --- a/Engine/source/lighting/probeManager.cpp +++ b/Engine/source/lighting/probeManager.cpp @@ -941,34 +941,31 @@ void ProbeManager::ReflectProbeMaterialInfo::setProbeParameters(const ProbeRende GFX->setTexture(0, deferredTexTarget->getTexture()); GFX->setTexture(1, matInfoTexTarget->getTexture()); GFX->setTexture(2, colorTexTarget->getTexture()); - GFX->setCubeTexture(3, probeInfo->mCubemap->getPointer()); - GFX->setCubeTexture(4, probeInfo->mIrradianceCubemap->getPointer()); + + //Add some safety catches in the event the cubemaps aren't fully initialized yet + if (probeInfo->mCubemap == nullptr || probeInfo->mCubemap->isNull()) + { + GFX->setCubeTexture(3, nullptr); + matParams->setSafe(cubeMips, 2.0f); + } + else + { + GFX->setCubeTexture(3, probeInfo->mCubemap->getPointer()); + matParams->setSafe(cubeMips, mPow(probeInfo->mCubemap->getPointer()->getMipMapLevels(), 2.0f)); + } + + if (probeInfo->mIrradianceCubemap == nullptr || probeInfo->mIrradianceCubemap->isNull()) + GFX->setCubeTexture(4, nullptr); + else + GFX->setCubeTexture(4, probeInfo->mIrradianceCubemap->getPointer()); + GFX->setTexture(5, probeInfo->mBRDFTexture->getPointer()); //set material params - matParams->setSafe(cubeMips, mPow(probeInfo->mCubemap->getPointer()->getMipMapLevels(), 2.0f)); matParams->setSafe(eyePosWorld, renderState->getCameraPosition()); matParams->setSafe(bbMin, probeInfo->mBounds.minExtents); matParams->setSafe(bbMax, probeInfo->mBounds.maxExtents); matParams->setSafe(useSphereMode, probeInfo->mProbeShapeType == ProbeRenderInst::Sphere ? 1.0f : 0.0f); - - //SH Terms - //static AlignedArray shTermsArray(9, sizeof(Point3F)); - //dMemset(shTermsArray.getBuffer(), 0, shTermsArray.getBufferSize()); - - /*for (U32 i = 0; i < 9; i++) - { - matParams->setSafe(shTerms[i], probeInfo->mSHTerms[i]); - } - - for (U32 i = 0; i < 5; i++) - { - matParams->setSafe(shConsts[i], probeInfo->mSHConstants[i]); - }*/ - - //const MatrixF worldToObjectXfm = probeInfo->mTransform; - //MaterialParameterHandle *worldToObjMat = matInstance->getMaterialParameterHandle("$worldToObj"); - //matParams->setSafe(worldToObjMat, worldToObjectXfm); } //