probeManager.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "lighting/probeManager.h"
  24. #include "console/console.h"
  25. #include "console/consoleTypes.h"
  26. #include "core/util/safeDelete.h"
  27. #include "console/sim.h"
  28. #include "console/simSet.h"
  29. #include "scene/sceneManager.h"
  30. #include "materials/materialManager.h"
  31. #include "materials/sceneData.h"
  32. #include "lighting/lightInfo.h"
  33. #include "lighting/lightingInterfaces.h"
  34. #include "T3D/gameBase/gameConnection.h"
  35. #include "gfx/gfxStringEnumTranslate.h"
  36. #include "console/engineAPI.h"
  37. #include "renderInstance/renderDeferredMgr.h"
  38. #include "shaderGen/shaderGenVars.h"
  39. Signal<void(const char*,bool)> ProbeManager::smActivateSignal;
  40. ProbeManager *ProbeManager::smProbeManager = NULL;
  41. //
  42. //
  43. ProbeInfo::ProbeInfo()
  44. : mTransform(true),
  45. mAmbient(0.0f, 0.0f, 0.0f, 1.0f),
  46. mPriority(1.0f),
  47. mScore(0.0f),
  48. mDebugRender(false),
  49. mCubemap(NULL),
  50. mIrradianceCubemap(NULL),
  51. mBRDFTexture(NULL),
  52. mRadius(1.0f),
  53. mIntensity(1.0f),
  54. mProbePosOffset(0,0,0)
  55. {
  56. for (U32 i = 0; i < 5; ++i)
  57. {
  58. mSHConstants[i] = 0;
  59. }
  60. }
  61. ProbeInfo::~ProbeInfo()
  62. {
  63. SAFE_DELETE(mCubemap);
  64. }
  65. void ProbeInfo::set(const ProbeInfo *probeInfo)
  66. {
  67. mTransform = probeInfo->mTransform;
  68. mAmbient = probeInfo->mAmbient;
  69. mCubemap = probeInfo->mCubemap;
  70. mIrradianceCubemap = probeInfo->mIrradianceCubemap;
  71. mBRDFTexture = probeInfo->mBRDFTexture;
  72. mRadius = probeInfo->mRadius;
  73. mIntensity = probeInfo->mIntensity;
  74. mProbeShapeType = probeInfo->mProbeShapeType;
  75. numPrims = probeInfo->numPrims;
  76. numVerts = probeInfo->numVerts;
  77. numIndicesForPoly = probeInfo->numIndicesForPoly;
  78. mBounds = probeInfo->mBounds;
  79. for (U32 i = 0; i < 9; i++)
  80. {
  81. mSHTerms[i] = probeInfo->mSHTerms[i];
  82. }
  83. for (U32 i = 0; i < 5; i++)
  84. {
  85. mSHConstants[i] = probeInfo->mSHConstants[i];
  86. }
  87. }
  88. void ProbeInfo::getWorldToLightProj(MatrixF *outMatrix) const
  89. {
  90. *outMatrix = getTransform();
  91. outMatrix->inverse();
  92. }
  93. void ProbeInfoList::registerProbe(ProbeInfo *light)
  94. {
  95. if (!light)
  96. return;
  97. // just add the light, we'll try to scan for dupes later...
  98. push_back(light);
  99. }
  100. void ProbeInfoList::unregisterProbe(ProbeInfo *light)
  101. {
  102. // remove all of them...
  103. ProbeInfoList &list = *this;
  104. for (U32 i = 0; i<list.size(); i++)
  105. {
  106. if (list[i] != light)
  107. continue;
  108. // this moves last to i, which allows
  109. // the search to continue forward...
  110. list.erase_fast(i);
  111. // want to check this location again...
  112. i--;
  113. }
  114. }
  115. ProbeShaderConstants::ProbeShaderConstants()
  116. : mInit(false),
  117. mShader(NULL),
  118. mProbeParamsSC(NULL),
  119. mProbePositionSC(NULL),
  120. mProbeRadiusSC(NULL),
  121. mProbeBoxMinSC(NULL),
  122. mProbeBoxMaxSC(NULL),
  123. mProbeIsSphereSC(NULL),
  124. mProbeLocalPosSC(NULL),
  125. mProbeCubemapSC(NULL)
  126. {
  127. }
  128. ProbeShaderConstants::~ProbeShaderConstants()
  129. {
  130. if (mShader.isValid())
  131. {
  132. mShader->getReloadSignal().remove(this, &ProbeShaderConstants::_onShaderReload);
  133. mShader = NULL;
  134. }
  135. }
  136. void ProbeShaderConstants::init(GFXShader* shader)
  137. {
  138. if (mShader.getPointer() != shader)
  139. {
  140. if (mShader.isValid())
  141. mShader->getReloadSignal().remove(this, &ProbeShaderConstants::_onShaderReload);
  142. mShader = shader;
  143. mShader->getReloadSignal().notify(this, &ProbeShaderConstants::_onShaderReload);
  144. }
  145. mProbeParamsSC = shader->getShaderConstHandle("$probeParams");
  146. //Reflection Probes
  147. mProbePositionSC = shader->getShaderConstHandle(ShaderGenVars::probePosition);
  148. mProbeRadiusSC = shader->getShaderConstHandle(ShaderGenVars::probeRadius);
  149. mProbeBoxMinSC = shader->getShaderConstHandle(ShaderGenVars::probeBoxMin);
  150. mProbeBoxMaxSC = shader->getShaderConstHandle(ShaderGenVars::probeBoxMax);
  151. mProbeIsSphereSC = shader->getShaderConstHandle(ShaderGenVars::probeIsSphere);
  152. mProbeLocalPosSC = shader->getShaderConstHandle(ShaderGenVars::probeLocalPos);
  153. mProbeCubemapSC = shader->getShaderConstHandle(ShaderGenVars::probeCubemap);
  154. mInit = true;
  155. }
  156. void ProbeShaderConstants::_onShaderReload()
  157. {
  158. if (mShader.isValid())
  159. init(mShader);
  160. }
  161. ProbeManager::ProbeManager()
  162. : mDefaultProbe( NULL ),
  163. mSceneManager( NULL ),
  164. mCullPos( Point3F::Zero )
  165. {
  166. dMemset( &mSpecialProbes, 0, sizeof(mSpecialProbes) );
  167. mLastShader = NULL;
  168. mLastConstants = NULL;
  169. }
  170. ProbeManager::~ProbeManager()
  171. {
  172. }
  173. ProbeInfo* ProbeManager::createProbeInfo(ProbeInfo* probe /* = NULL */)
  174. {
  175. ProbeInfo *outProbe = (probe != NULL) ? probe : new ProbeInfo;
  176. /*ProbeManagerMap &ProbeManagers = _getProbeManagers();
  177. ProbeManagerMap::Iterator iter = ProbeManagers.begin();
  178. for ( ; iter != ProbeManagers.end(); iter++ )
  179. {
  180. ProbeManager *lm = iter->value;
  181. lm->_addLightInfoEx( outLight );
  182. }*/
  183. return outProbe;
  184. }
  185. /*void ProbeManager::initLightFields()
  186. {
  187. ProbeManagerMap &ProbeManagers = _getProbeManagers();
  188. ProbeManagerMap::Iterator iter = ProbeManagers.begin();
  189. for ( ; iter != ProbeManagers.end(); iter++ )
  190. {
  191. ProbeManager *lm = iter->value;
  192. lm->_initLightFields();
  193. }
  194. }*/
  195. IMPLEMENT_GLOBAL_CALLBACK( onProbeManagerActivate, void, ( const char *name ), ( name ),
  196. "A callback called by the engine when a light manager is activated.\n"
  197. "@param name The name of the light manager being activated.\n"
  198. "@ingroup Lighting\n" );
  199. void ProbeManager::activate( SceneManager *sceneManager )
  200. {
  201. AssertFatal( sceneManager, "ProbeManager::activate() - Got null scene manager!" );
  202. //AssertFatal( mIsActive == false, "ProbeManager::activate() - Already activated!" );
  203. AssertFatal(smProbeManager == NULL, "ProbeManager::activate() - A previous ProbeManager is still active!" );
  204. mSceneManager = sceneManager;
  205. smProbeManager = this;
  206. }
  207. IMPLEMENT_GLOBAL_CALLBACK( onProbeManagerDeactivate, void, ( const char *name ), ( name ),
  208. "A callback called by the engine when a light manager is deactivated.\n"
  209. "@param name The name of the light manager being deactivated.\n"
  210. "@ingroup Lighting\n" );
  211. void ProbeManager::deactivate()
  212. {
  213. //AssertFatal( mIsActive == true, "ProbeManager::deactivate() - Already deactivated!" );
  214. AssertFatal( smProbeManager == this, "ProbeManager::activate() - This isn't the active light manager!" );
  215. //if( Sim::getRootGroup() ) // To protect against shutdown.
  216. // onProbeManagerDeactivate_callback( getName() );
  217. //mIsActive = false;
  218. mSceneManager = NULL;
  219. smProbeManager = NULL;
  220. // Just in case... make sure we're all clear.
  221. unregisterAllProbes();
  222. }
  223. ProbeInfo* ProbeManager::getDefaultLight()
  224. {
  225. // The sun is always our default light when
  226. // when its registered.
  227. if ( mSpecialProbes[ ProbeManager::SkylightProbeType ] )
  228. return mSpecialProbes[ ProbeManager::SkylightProbeType ];
  229. // Else return a dummy special light.
  230. //if ( !mDefaultLight )
  231. // mDefaultLight = createLightInfo();
  232. return NULL;
  233. }
  234. ProbeInfo* ProbeManager::getSpecialProbe( ProbeManager::SpecialProbeTypesEnum type, bool useDefault )
  235. {
  236. //if ( mSpecialLights[type] )
  237. // return mSpecialLights[type];
  238. if ( useDefault )
  239. return getDefaultLight();
  240. return NULL;
  241. }
  242. void ProbeManager::setSpecialProbe( ProbeManager::SpecialProbeTypesEnum type, ProbeInfo *probe )
  243. {
  244. if (probe && type == SkylightProbeType )
  245. {
  246. // The sun must be specially positioned and ranged
  247. // so that it can be processed like a point light
  248. // in the stock light shader used by Basic Lighting.
  249. probe->setPosition( mCullPos - (probe->getDirection() * 10000.0f ) );
  250. probe->mRadius = 2000000.0f;
  251. }
  252. mSpecialProbes[type] = probe;
  253. registerProbe(probe, NULL );
  254. }
  255. void ProbeManager::registerProbes( const Frustum *frustum, bool staticLighting )
  256. {
  257. PROFILE_SCOPE( ProbeManager_RegisterProbes );
  258. // TODO: We need to work this out...
  259. //
  260. // 1. Why do we register and unregister lights on every
  261. // render when they don't often change... shouldn't we
  262. // just register once and keep them?
  263. //
  264. // 2. If we do culling of lights should this happen as part
  265. // of registration or somewhere else?
  266. //
  267. // Grab the lights to process.
  268. Vector<SceneObject*> activeLights;
  269. const U32 lightMask = LightObjectType;
  270. if ( staticLighting || !frustum )
  271. {
  272. // We're processing static lighting or want all the lights
  273. // in the container registerd... so no culling.
  274. getSceneManager()->getContainer()->findObjectList( lightMask, &activeLights );
  275. }
  276. else
  277. {
  278. // Cull the lights using the frustum.
  279. getSceneManager()->getContainer()->findObjectList( *frustum, lightMask, &activeLights );
  280. /* supress light culling filter until we can sort out why that's misbehaving with dynamic cube mapping
  281. for (U32 i = 0; i < activeLights.size(); ++i)
  282. {
  283. if (!getSceneManager()->mRenderedObjectsList.contains(activeLights[i]))
  284. {
  285. activeLights.erase(i);
  286. --i;
  287. }
  288. }
  289. */
  290. // Store the culling position for sun placement
  291. // later... see setSpecialLight.
  292. mCullPos = frustum->getPosition();
  293. // HACK: Make sure the control object always gets
  294. // processed as lights mounted to it don't change
  295. // the shape bounds and can often get culled.
  296. GameConnection *conn = GameConnection::getConnectionToServer();
  297. if ( conn->getControlObject() )
  298. {
  299. GameBase *conObject = conn->getControlObject();
  300. activeLights.push_back_unique( conObject );
  301. }
  302. }
  303. // Let the lights register themselves.
  304. /*for ( U32 i = 0; i < activeLights.size(); i++ )
  305. {
  306. ISceneLight *lightInterface = dynamic_cast<ISceneLight*>( activeLights[i] );
  307. if ( lightInterface )
  308. lightInterface->submitLights( this, staticLighting );
  309. }*/
  310. }
  311. void ProbeManager::registerSkylight(ProbeInfo *probe, SimObject *obj)
  312. {
  313. mSkylight = probe;
  314. if (String("Advanced Lighting").equal(LIGHTMGR->getName(), String::NoCase))
  315. {
  316. SceneRenderState* state = mSceneManager->getCurrentRenderState();
  317. RenderPassManager *renderPass = state->getRenderPass();
  318. // Allocate an MeshRenderInst so that we can submit it to the RenderPassManager
  319. ProbeRenderInst *probeInst = renderPass->allocInst<ProbeRenderInst>();
  320. probeInst->set(probe);
  321. probeInst->type = RenderPassManager::RIT_Probes;
  322. // Submit our RenderInst to the RenderPassManager
  323. state->getRenderPass()->addInst(probeInst);
  324. }
  325. }
  326. void ProbeManager::registerProbe(ProbeInfo *probe, SimObject *obj )
  327. {
  328. // AssertFatal( !mRegisteredProbes.contains(probe),
  329. //"ProbeManager::registerGlobalLight - This light is already registered!" );
  330. if (!mRegisteredProbes.contains(probe))
  331. mRegisteredProbes.push_back(probe);
  332. if (String("Advanced Lighting").equal(LIGHTMGR->getName(), String::NoCase))
  333. {
  334. SceneRenderState* state = mSceneManager->getCurrentRenderState();
  335. RenderPassManager *renderPass = state->getRenderPass();
  336. // Allocate an MeshRenderInst so that we can submit it to the RenderPassManager
  337. ProbeRenderInst *probeInst = renderPass->allocInst<ProbeRenderInst>();
  338. probeInst->set(probe);
  339. probeInst->type = RenderPassManager::RIT_Probes;
  340. // Submit our RenderInst to the RenderPassManager
  341. state->getRenderPass()->addInst(probeInst);
  342. }
  343. }
  344. void ProbeManager::unregisterProbe(ProbeInfo *probe )
  345. {
  346. mRegisteredProbes.unregisterProbe(probe);
  347. // If this is the sun... clear the special light too.
  348. if (probe == mSpecialProbes[SkylightProbeType] )
  349. dMemset(mSpecialProbes, 0, sizeof(mSpecialProbes) );
  350. }
  351. void ProbeManager::unregisterAllProbes()
  352. {
  353. //dMemset(mSpecialProbes, 0, sizeof(mSpecialProbes) );
  354. mRegisteredProbes.clear();
  355. mSkylight = nullptr;
  356. }
  357. void ProbeManager::getAllUnsortedProbes( Vector<ProbeInfo*> *list ) const
  358. {
  359. list->merge( mRegisteredProbes );
  360. }
  361. ProbeShaderConstants* ProbeManager::getProbeShaderConstants(GFXShaderConstBuffer* buffer)
  362. {
  363. if (!buffer)
  364. return NULL;
  365. PROFILE_SCOPE(ProbeManager_GetProbeShaderConstants);
  366. GFXShader* shader = buffer->getShader();
  367. // Check to see if this is the same shader, we'll get hit repeatedly by
  368. // the same one due to the render bin loops.
  369. if (mLastShader.getPointer() != shader)
  370. {
  371. ProbeConstantMap::Iterator iter = mConstantLookup.find(shader);
  372. if (iter != mConstantLookup.end())
  373. {
  374. mLastConstants = iter->value;
  375. }
  376. else
  377. {
  378. ProbeShaderConstants* psc = new ProbeShaderConstants();
  379. mConstantLookup[shader] = psc;
  380. mLastConstants = psc;
  381. }
  382. // Set our new shader
  383. mLastShader = shader;
  384. }
  385. mLastConstants = new ProbeShaderConstants();
  386. // Make sure that our current lighting constants are initialized
  387. if (!mLastConstants->mInit)
  388. mLastConstants->init(shader);
  389. return mLastConstants;
  390. }
  391. void ProbeManager::_update4ProbeConsts( const SceneData &sgData,
  392. MatrixSet &matSet,
  393. GFXShaderConstHandle *probePositionSC,
  394. GFXShaderConstHandle *probeRadiusSC,
  395. GFXShaderConstHandle *probeBoxMinSC,
  396. GFXShaderConstHandle *probeBoxMaxSC,
  397. GFXShaderConstHandle *probeCubemapSC,
  398. GFXShaderConstHandle *probeIsSphereSC,
  399. GFXShaderConstHandle *probeLocalPosSC,
  400. GFXShaderConstBuffer *shaderConsts )
  401. {
  402. PROFILE_SCOPE( ProbeManager_Update4ProbeConsts );
  403. // Skip over gathering lights if we don't have to!
  404. if (probePositionSC->isValid() ||
  405. probeRadiusSC->isValid() ||
  406. probeBoxMinSC->isValid() ||
  407. probeBoxMaxSC->isValid() ||
  408. probeCubemapSC->isValid() && (!mRegisteredProbes.empty() || mSkylight))
  409. {
  410. PROFILE_SCOPE(ProbeManager_Update4ProbeConsts_setProbes);
  411. static AlignedArray<Point3F> probePositions(4, sizeof(Point3F));
  412. static AlignedArray<F32> probeRadius(4, sizeof(F32));
  413. static AlignedArray<Point3F> probeBoxMins(4, sizeof(Point3F));
  414. static AlignedArray<Point3F> probeBoxMaxs(4, sizeof(Point3F));
  415. static AlignedArray<Point3F> probeLocalPositions(4, sizeof(Point3F));
  416. static AlignedArray<F32> probeIsSphere(4, sizeof(F32));
  417. //static AlignedArray<CubemapData> probeCubemap(4, sizeof(CubemapData));
  418. F32 range;
  419. // Need to clear the buffers so that we don't leak
  420. // lights from previous passes or have NaNs.
  421. dMemset(probePositions.getBuffer(), 0, probePositions.getBufferSize());
  422. dMemset(probeRadius.getBuffer(), 0, probeRadius.getBufferSize());
  423. dMemset(probeBoxMins.getBuffer(), 0, probeBoxMins.getBufferSize());
  424. dMemset(probeBoxMaxs.getBuffer(), 0, probeBoxMaxs.getBufferSize());
  425. dMemset(probeLocalPositions.getBuffer(), 0, probeLocalPositions.getBufferSize());
  426. dMemset(probeIsSphere.getBuffer(), 0, probeRadius.getBufferSize());
  427. //dMemset(probeCubemap.getBuffer(), 0, probeCubemap.getBufferSize());
  428. matSet.restoreSceneViewProjection();
  429. const MatrixF &worldToCameraXfm = matSet.getWorldToCamera();
  430. // Gather the data for the first 4 probes.
  431. const ProbeInfo *probe;
  432. for (U32 i = 0; i < 4; i++)
  433. {
  434. if (i >= mRegisteredProbes.size())
  435. break;
  436. if (i == 0 && mSkylight)
  437. {
  438. //quickly try and see if we have a skylight, and set that to always be probe 0
  439. probe = mSkylight;
  440. }
  441. else
  442. {
  443. probe = mRegisteredProbes[i];
  444. }
  445. if (!probe)
  446. continue;
  447. // The light positions and spot directions are
  448. // in SoA order to make optimal use of the GPU.
  449. const Point3F &probePos = probe->getPosition();
  450. probePositions[i].x = probePos.x;
  451. probePositions[i].y = probePos.y;
  452. probePositions[i].z = probePos.z;
  453. probeRadius[i] = probe->mRadius;
  454. const Point3F &minExt = probe->mBounds.minExtents;
  455. probeBoxMins[i].x = minExt.x;
  456. probeBoxMins[i].y = minExt.y;
  457. probeBoxMins[i].z = minExt.z;
  458. const Point3F &maxExt = probe->mBounds.maxExtents;
  459. probeBoxMaxs[i].x = maxExt.x;
  460. probeBoxMaxs[i].y = maxExt.y;
  461. probeBoxMaxs[i].z = maxExt.z;
  462. probeIsSphere[i] = probe->mProbeShapeType == ProbeInfo::Sphere ? 1.0 : 0.0;
  463. Point3F localProbePos;
  464. worldToCameraXfm.mulP(probe->getPosition(), &localProbePos);
  465. probeLocalPositions[i].x = localProbePos.x;
  466. probeLocalPositions[i].y = localProbePos.y;
  467. probeLocalPositions[i].z = localProbePos.z;
  468. if (probe->mCubemap && !probe->mCubemap->isNull())
  469. {
  470. S32 samplerReg = probeCubemapSC->getSamplerRegister();
  471. if(samplerReg != -1)
  472. GFX->setCubeTexture(samplerReg + i, probe->mCubemap->getPointer());
  473. }
  474. }
  475. shaderConsts->setSafe(probePositionSC, probePositions);
  476. shaderConsts->setSafe(probeRadiusSC, probeRadius);
  477. shaderConsts->setSafe(probeBoxMinSC, probeBoxMins);
  478. shaderConsts->setSafe(probeBoxMaxSC, probeBoxMaxs);
  479. shaderConsts->setSafe(probeLocalPosSC, probeLocalPositions);
  480. shaderConsts->setSafe(probeIsSphereSC, probeIsSphere);
  481. //
  482. //shaderConsts->setSafe(lightSpotAngleSC, lightSpotAngle);
  483. //shaderConsts->setSafe(lightSpotFalloffSC, lightSpotFalloff);
  484. }
  485. else
  486. {
  487. /*if (probe->mCubemap && !probe->mCubemap->isNull())
  488. {
  489. GFX->setCubeTexture(1, probe->mCubemap->getPointer());
  490. }*/
  491. if (probeCubemapSC->isValid())
  492. {
  493. for(U32 i=0; i < 4; ++i)
  494. GFX->setCubeTexture(probeCubemapSC->getSamplerRegister() + i, NULL);
  495. }
  496. }
  497. }
  498. void ProbeManager::setProbeInfo(ProcessedMaterial *pmat,
  499. const Material *mat,
  500. const SceneData &sgData,
  501. const SceneRenderState *state,
  502. U32 pass,
  503. GFXShaderConstBuffer *shaderConsts)
  504. {
  505. // Skip this if we're rendering from the deferred bin.
  506. if ( sgData.binType == SceneData::DeferredBin )
  507. return;
  508. // if (mRegisteredProbes.empty())
  509. // return;
  510. PROFILE_SCOPE(ProbeManager_setProbeInfo);
  511. ProbeShaderConstants *psc = getProbeShaderConstants(shaderConsts);
  512. //ProbeInfo *probe;
  513. //probe = mRegisteredProbes[0];
  514. // NOTE: If you encounter a crash from this point forward
  515. // while setting a shader constant its probably because the
  516. // mConstantLookup has bad shaders/constants in it.
  517. //
  518. // This is a known crash bug that can occur if materials/shaders
  519. // are reloaded and the light manager is not reset.
  520. //
  521. // We should look to fix this by clearing the table.
  522. MatrixSet matSet = state->getRenderPass()->getMatrixSet();
  523. // Update the forward shading light constants.
  524. _update4ProbeConsts( sgData,
  525. matSet,
  526. psc->mProbePositionSC,
  527. psc->mProbeRadiusSC,
  528. psc->mProbeBoxMinSC,
  529. psc->mProbeBoxMaxSC,
  530. psc->mProbeCubemapSC,
  531. psc->mProbeIsSphereSC,
  532. psc->mProbeLocalPosSC,
  533. shaderConsts );
  534. // Static
  535. /*if (lsm && light->getCastShadows())
  536. {
  537. if (psc->mWorldToLightProjSC->isValid())
  538. shaderConsts->set(psc->mWorldToLightProjSC,
  539. lsm->getWorldToLightProj(),
  540. psc->mWorldToLightProjSC->getType());
  541. if (psc->mViewToLightProjSC->isValid())
  542. {
  543. // TODO: Should probably cache these results and
  544. // not do this mul here on every material that needs
  545. // this transform.
  546. shaderConsts->set(psc->mViewToLightProjSC,
  547. lsm->getWorldToLightProj() * state->getCameraTransform(),
  548. psc->mViewToLightProjSC->getType());
  549. }
  550. shaderConsts->setSafe(psc->mShadowMapSizeSC, 1.0f / (F32)lsm->getTexSize());
  551. // Do this last so that overrides can properly override parameters previously set
  552. lsm->setShaderParameters(shaderConsts, psc);
  553. }
  554. else
  555. {
  556. if (psc->mViewToLightProjSC->isValid())
  557. {
  558. // TODO: Should probably cache these results and
  559. // not do this mul here on every material that needs
  560. // this transform.
  561. MatrixF proj;
  562. light->getWorldToLightProj(&proj);
  563. shaderConsts->set(psc->mViewToLightProjSC,
  564. proj * state->getCameraTransform(),
  565. psc->mViewToLightProjSC->getType());
  566. }
  567. }
  568. // Dynamic
  569. if (dynamicShadowMap)
  570. {
  571. if (psc->mDynamicWorldToLightProjSC->isValid())
  572. shaderConsts->set(psc->mDynamicWorldToLightProjSC,
  573. dynamicShadowMap->getWorldToLightProj(),
  574. psc->mDynamicWorldToLightProjSC->getType());
  575. if (psc->mDynamicViewToLightProjSC->isValid())
  576. {
  577. // TODO: Should probably cache these results and
  578. // not do this mul here on every material that needs
  579. // this transform.
  580. shaderConsts->set(psc->mDynamicViewToLightProjSC,
  581. dynamicShadowMap->getWorldToLightProj() * state->getCameraTransform(),
  582. psc->mDynamicViewToLightProjSC->getType());
  583. }
  584. shaderConsts->setSafe(psc->mShadowMapSizeSC, 1.0f / (F32)dynamicShadowMap->getTexSize());
  585. // Do this last so that overrides can properly override parameters previously set
  586. dynamicShadowMap->setShaderParameters(shaderConsts, psc);
  587. }
  588. else
  589. {
  590. if (psc->mDynamicViewToLightProjSC->isValid())
  591. {
  592. // TODO: Should probably cache these results and
  593. // not do this mul here on every material that needs
  594. // this transform.
  595. MatrixF proj;
  596. light->getWorldToLightProj(&proj);
  597. shaderConsts->set(psc->mDynamicViewToLightProjSC,
  598. proj * state->getCameraTransform(),
  599. psc->mDynamicViewToLightProjSC->getType());
  600. }
  601. }*/
  602. }
  603. /// Allows us to set textures during the Material::setTextureStage call, return true if we've done work.
  604. bool ProbeManager::setTextureStage(const SceneData &sgData,
  605. const U32 currTexFlag,
  606. const U32 textureSlot,
  607. GFXShaderConstBuffer *shaderConsts,
  608. ShaderConstHandles *handles)
  609. {
  610. return false;
  611. }
  612. AvailableSLInterfaces* ProbeManager::getSceneLightingInterface()
  613. {
  614. //if ( !mAvailableSLInterfaces )
  615. // mAvailableSLInterfaces = new AvailableSLInterfaces();
  616. return NULL;
  617. }
  618. /*bool ProbeManager::lightScene( const char* callback, const char* param )
  619. {
  620. BitSet32 flags = 0;
  621. if ( param )
  622. {
  623. if ( !dStricmp( param, "forceAlways" ) )
  624. flags.set( SceneLighting::ForceAlways );
  625. else if ( !dStricmp(param, "forceWritable" ) )
  626. flags.set( SceneLighting::ForceWritable );
  627. else if ( !dStricmp(param, "loadOnly" ) )
  628. flags.set( SceneLighting::LoadOnly );
  629. }
  630. // The SceneLighting object will delete itself
  631. // once the lighting process is complete.
  632. SceneLighting* sl = new SceneLighting( getSceneLightingInterface() );
  633. return sl->lightScene( callback, flags );
  634. }*/
  635. /*RenderDeferredMgr* ProbeManager::_findDeferredRenderBin()
  636. {
  637. RenderPassManager* rpm = getSceneManager()->getDefaultRenderPass();
  638. for( U32 i = 0; i < rpm->getManagerCount(); i++ )
  639. {
  640. RenderBinManager *bin = rpm->getManager( i );
  641. if( bin->getRenderInstType() == RenderDeferredMgr::RIT_Deferred )
  642. {
  643. return ( RenderDeferredMgr* ) bin;
  644. }
  645. }
  646. return NULL;
  647. }*/
  648. DefineEngineFunction( CreateProbeManager, bool, (),,
  649. "Finds and activates the named light manager.\n"
  650. "@return Returns true if the light manager is found and activated.\n"
  651. "@ingroup Lighting\n" )
  652. {
  653. ProbeManager* probeManager = new ProbeManager();
  654. if (probeManager != nullptr && gClientSceneGraph != nullptr)
  655. {
  656. probeManager->activate(gClientSceneGraph);
  657. return true;
  658. }
  659. return false;
  660. }
  661. DefineEngineFunction( resetProbeManager, void, (),,
  662. "@brief Deactivates and then activates the currently active light manager."
  663. "This causes most shaders to be regenerated and is often used when global "
  664. "rendering changes have occured.\n"
  665. "@ingroup Lighting\n" )
  666. {
  667. ProbeManager *pm = PROBEMGR;
  668. if ( !pm)
  669. return;
  670. /*SceneManager *sm = lm->getSceneManager();
  671. lm->deactivate();
  672. lm->activate( sm );*/
  673. }