probeManager.cpp 25 KB

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