probeManager.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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. #include "math/util/sphereMesh.h"
  40. Signal<void(const char*,bool)> ProbeManager::smActivateSignal;
  41. ProbeManager *ProbeManager::smProbeManager = NULL;
  42. bool ProbeManager::smRenderReflectionProbes = true;
  43. //
  44. //
  45. ProbeRenderInst::ProbeRenderInst() : SystemInterface(),
  46. mTransform(true),
  47. mDirty(false),
  48. mAmbient(0.0f, 0.0f, 0.0f, 1.0f),
  49. mPriority(1.0f),
  50. mScore(0.0f),
  51. mDebugRender(false),
  52. mCubemap(NULL),
  53. mIrradianceCubemap(NULL),
  54. mBRDFTexture(NULL),
  55. mRadius(1.0f),
  56. mIntensity(1.0f),
  57. mProbePosOffset(0,0,0),
  58. numPrims(0)
  59. {
  60. for (U32 i = 0; i < 5; ++i)
  61. {
  62. mSHConstants[i] = 0;
  63. }
  64. }
  65. ProbeRenderInst::~ProbeRenderInst()
  66. {
  67. if (mCubemap && !mCubemap->isNull())
  68. {
  69. mCubemap->getPointer()->destroySelf();
  70. mCubemap->free();
  71. }
  72. if (mIrradianceCubemap && !mIrradianceCubemap->isNull())
  73. {
  74. mIrradianceCubemap->getPointer()->destroySelf();
  75. mIrradianceCubemap->free();
  76. }
  77. if (mBRDFTexture && !mBRDFTexture->isNull())
  78. {
  79. mBRDFTexture->getPointer()->destroySelf();
  80. mBRDFTexture->free();
  81. }
  82. }
  83. void ProbeRenderInst::set(const ProbeRenderInst *probeInfo)
  84. {
  85. mTransform = probeInfo->mTransform;
  86. mAmbient = probeInfo->mAmbient;
  87. mCubemap = probeInfo->mCubemap;
  88. mIrradianceCubemap = probeInfo->mIrradianceCubemap;
  89. mBRDFTexture = probeInfo->mBRDFTexture;
  90. mRadius = probeInfo->mRadius;
  91. mIntensity = probeInfo->mIntensity;
  92. mProbeShapeType = probeInfo->mProbeShapeType;
  93. numPrims = probeInfo->numPrims;
  94. numVerts = probeInfo->numVerts;
  95. numIndicesForPoly = probeInfo->numIndicesForPoly;
  96. mBounds = probeInfo->mBounds;
  97. for (U32 i = 0; i < 9; i++)
  98. {
  99. mSHTerms[i] = probeInfo->mSHTerms[i];
  100. }
  101. for (U32 i = 0; i < 5; i++)
  102. {
  103. mSHConstants[i] = probeInfo->mSHConstants[i];
  104. }
  105. }
  106. void ProbeRenderInst::getWorldToLightProj(MatrixF *outMatrix) const
  107. {
  108. *outMatrix = getTransform();
  109. outMatrix->inverse();
  110. }
  111. ProbeShaderConstants::ProbeShaderConstants()
  112. : mInit(false),
  113. mShader(NULL),
  114. mProbeParamsSC(NULL),
  115. mProbePositionSC(NULL),
  116. mProbeRadiusSC(NULL),
  117. mProbeBoxMinSC(NULL),
  118. mProbeBoxMaxSC(NULL),
  119. mProbeIsSphereSC(NULL),
  120. mProbeLocalPosSC(NULL),
  121. mProbeCubemapSC(NULL)
  122. {
  123. }
  124. ProbeShaderConstants::~ProbeShaderConstants()
  125. {
  126. if (mShader.isValid())
  127. {
  128. mShader->getReloadSignal().remove(this, &ProbeShaderConstants::_onShaderReload);
  129. mShader = NULL;
  130. }
  131. }
  132. void ProbeShaderConstants::init(GFXShader* shader)
  133. {
  134. if (mShader.getPointer() != shader)
  135. {
  136. if (mShader.isValid())
  137. mShader->getReloadSignal().remove(this, &ProbeShaderConstants::_onShaderReload);
  138. mShader = shader;
  139. mShader->getReloadSignal().notify(this, &ProbeShaderConstants::_onShaderReload);
  140. }
  141. mProbeParamsSC = shader->getShaderConstHandle("$probeParams");
  142. //Reflection Probes
  143. mProbePositionSC = shader->getShaderConstHandle(ShaderGenVars::probePosition);
  144. mProbeRadiusSC = shader->getShaderConstHandle(ShaderGenVars::probeRadius);
  145. mProbeBoxMinSC = shader->getShaderConstHandle(ShaderGenVars::probeBoxMin);
  146. mProbeBoxMaxSC = shader->getShaderConstHandle(ShaderGenVars::probeBoxMax);
  147. mProbeIsSphereSC = shader->getShaderConstHandle(ShaderGenVars::probeIsSphere);
  148. mProbeLocalPosSC = shader->getShaderConstHandle(ShaderGenVars::probeLocalPos);
  149. mProbeCubemapSC = shader->getShaderConstHandle(ShaderGenVars::probeCubemap);
  150. mInit = true;
  151. }
  152. void ProbeShaderConstants::_onShaderReload()
  153. {
  154. if (mShader.isValid())
  155. init(mShader);
  156. }
  157. ProbeManager::ProbeManager()
  158. : mSceneManager( NULL ),
  159. mCullPos( Point3F::Zero )
  160. {
  161. mLastShader = NULL;
  162. mLastConstants = NULL;
  163. mSkylightMaterial = nullptr;
  164. mReflectProbeMaterial = nullptr;
  165. }
  166. ProbeManager::~ProbeManager()
  167. {
  168. }
  169. ProbeRenderInst* ProbeManager::createProbeInfo(ProbeRenderInst* probe /* = NULL */)
  170. {
  171. ProbeRenderInst *outProbe = (probe != NULL) ? probe : new ProbeRenderInst;
  172. /*ProbeManagerMap &ProbeManagers = _getProbeManagers();
  173. ProbeManagerMap::Iterator iter = ProbeManagers.begin();
  174. for ( ; iter != ProbeManagers.end(); iter++ )
  175. {
  176. ProbeManager *lm = iter->value;
  177. lm->_addLightInfoEx( outLight );
  178. }*/
  179. return outProbe;
  180. }
  181. /*void ProbeManager::initLightFields()
  182. {
  183. ProbeManagerMap &ProbeManagers = _getProbeManagers();
  184. ProbeManagerMap::Iterator iter = ProbeManagers.begin();
  185. for ( ; iter != ProbeManagers.end(); iter++ )
  186. {
  187. ProbeManager *lm = iter->value;
  188. lm->_initLightFields();
  189. }
  190. }*/
  191. IMPLEMENT_GLOBAL_CALLBACK( onProbeManagerActivate, void, ( const char *name ), ( name ),
  192. "A callback called by the engine when a light manager is activated.\n"
  193. "@param name The name of the light manager being activated.\n"
  194. "@ingroup Lighting\n" );
  195. void ProbeManager::activate( SceneManager *sceneManager )
  196. {
  197. AssertFatal( sceneManager, "ProbeManager::activate() - Got null scene manager!" );
  198. //AssertFatal( mIsActive == false, "ProbeManager::activate() - Already activated!" );
  199. AssertFatal(smProbeManager == NULL, "ProbeManager::activate() - A previous ProbeManager is still active!" );
  200. mSceneManager = sceneManager;
  201. smProbeManager = this;
  202. }
  203. IMPLEMENT_GLOBAL_CALLBACK( onProbeManagerDeactivate, void, ( const char *name ), ( name ),
  204. "A callback called by the engine when a light manager is deactivated.\n"
  205. "@param name The name of the light manager being deactivated.\n"
  206. "@ingroup Lighting\n" );
  207. void ProbeManager::deactivate()
  208. {
  209. //AssertFatal( mIsActive == true, "ProbeManager::deactivate() - Already deactivated!" );
  210. AssertFatal( smProbeManager == this, "ProbeManager::activate() - This isn't the active light manager!" );
  211. //if( Sim::getRootGroup() ) // To protect against shutdown.
  212. // onProbeManagerDeactivate_callback( getName() );
  213. //mIsActive = false;
  214. mSceneManager = NULL;
  215. smProbeManager = NULL;
  216. }
  217. ProbeShaderConstants* ProbeManager::getProbeShaderConstants(GFXShaderConstBuffer* buffer)
  218. {
  219. if (!buffer)
  220. return NULL;
  221. PROFILE_SCOPE(ProbeManager_GetProbeShaderConstants);
  222. GFXShader* shader = buffer->getShader();
  223. // Check to see if this is the same shader, we'll get hit repeatedly by
  224. // the same one due to the render bin loops.
  225. if (mLastShader.getPointer() != shader)
  226. {
  227. ProbeConstantMap::Iterator iter = mConstantLookup.find(shader);
  228. if (iter != mConstantLookup.end())
  229. {
  230. mLastConstants = iter->value;
  231. }
  232. else
  233. {
  234. ProbeShaderConstants* psc = new ProbeShaderConstants();
  235. mConstantLookup[shader] = psc;
  236. mLastConstants = psc;
  237. }
  238. // Set our new shader
  239. mLastShader = shader;
  240. }
  241. mLastConstants = new ProbeShaderConstants();
  242. // Make sure that our current lighting constants are initialized
  243. if (!mLastConstants->mInit)
  244. mLastConstants->init(shader);
  245. return mLastConstants;
  246. }
  247. void ProbeManager::_update4ProbeConsts( const SceneData &sgData,
  248. MatrixSet &matSet,
  249. GFXShaderConstHandle *probePositionSC,
  250. GFXShaderConstHandle *probeRadiusSC,
  251. GFXShaderConstHandle *probeBoxMinSC,
  252. GFXShaderConstHandle *probeBoxMaxSC,
  253. GFXShaderConstHandle *probeCubemapSC,
  254. GFXShaderConstHandle *probeIsSphereSC,
  255. GFXShaderConstHandle *probeLocalPosSC,
  256. GFXShaderConstBuffer *shaderConsts )
  257. {
  258. PROFILE_SCOPE( ProbeManager_Update4ProbeConsts );
  259. // Skip over gathering lights if we don't have to!
  260. if (probePositionSC->isValid() ||
  261. probeRadiusSC->isValid() ||
  262. probeBoxMinSC->isValid() ||
  263. probeBoxMaxSC->isValid() ||
  264. probeCubemapSC->isValid() && (!ProbeRenderInst::all.empty()))
  265. {
  266. PROFILE_SCOPE(ProbeManager_Update4ProbeConsts_setProbes);
  267. static AlignedArray<Point3F> probePositions(4, sizeof(Point3F));
  268. static AlignedArray<F32> probeRadius(4, sizeof(F32));
  269. static AlignedArray<Point3F> probeBoxMins(4, sizeof(Point3F));
  270. static AlignedArray<Point3F> probeBoxMaxs(4, sizeof(Point3F));
  271. static AlignedArray<Point3F> probeLocalPositions(4, sizeof(Point3F));
  272. static AlignedArray<F32> probeIsSphere(4, sizeof(F32));
  273. //static AlignedArray<CubemapData> probeCubemap(4, sizeof(CubemapData));
  274. F32 range;
  275. // Need to clear the buffers so that we don't leak
  276. // lights from previous passes or have NaNs.
  277. dMemset(probePositions.getBuffer(), 0, probePositions.getBufferSize());
  278. dMemset(probeRadius.getBuffer(), 0, probeRadius.getBufferSize());
  279. dMemset(probeBoxMins.getBuffer(), 0, probeBoxMins.getBufferSize());
  280. dMemset(probeBoxMaxs.getBuffer(), 0, probeBoxMaxs.getBufferSize());
  281. dMemset(probeLocalPositions.getBuffer(), 0, probeLocalPositions.getBufferSize());
  282. dMemset(probeIsSphere.getBuffer(), 0, probeRadius.getBufferSize());
  283. //dMemset(probeCubemap.getBuffer(), 0, probeCubemap.getBufferSize());
  284. matSet.restoreSceneViewProjection();
  285. const MatrixF &worldToCameraXfm = matSet.getWorldToCamera();
  286. // Gather the data for the first 4 probes.
  287. const ProbeRenderInst *probe;
  288. for (U32 i = 0; i < 4; i++)
  289. {
  290. if (i >= ProbeRenderInst::all.size())
  291. break;
  292. probe = ProbeRenderInst::all[i];
  293. if (!probe)
  294. continue;
  295. if (!probe->mIsEnabled)
  296. continue;
  297. // The light positions and spot directions are
  298. // in SoA order to make optimal use of the GPU.
  299. const Point3F &probePos = probe->getPosition();
  300. probePositions[i].x = probePos.x;
  301. probePositions[i].y = probePos.y;
  302. probePositions[i].z = probePos.z;
  303. probeRadius[i] = probe->mRadius;
  304. const Point3F &minExt = probe->mBounds.minExtents;
  305. probeBoxMins[i].x = minExt.x;
  306. probeBoxMins[i].y = minExt.y;
  307. probeBoxMins[i].z = minExt.z;
  308. const Point3F &maxExt = probe->mBounds.maxExtents;
  309. probeBoxMaxs[i].x = maxExt.x;
  310. probeBoxMaxs[i].y = maxExt.y;
  311. probeBoxMaxs[i].z = maxExt.z;
  312. probeIsSphere[i] = probe->mProbeShapeType == ProbeRenderInst::Sphere ? 1.0 : 0.0;
  313. Point3F localProbePos;
  314. worldToCameraXfm.mulP(probe->getPosition(), &localProbePos);
  315. probeLocalPositions[i].x = localProbePos.x;
  316. probeLocalPositions[i].y = localProbePos.y;
  317. probeLocalPositions[i].z = localProbePos.z;
  318. if (probe->mCubemap && !probe->mCubemap->isNull())
  319. {
  320. S32 samplerReg = probeCubemapSC->getSamplerRegister();
  321. if(samplerReg != -1)
  322. GFX->setCubeTexture(samplerReg + i, probe->mCubemap->getPointer());
  323. }
  324. }
  325. shaderConsts->setSafe(probePositionSC, probePositions);
  326. shaderConsts->setSafe(probeRadiusSC, probeRadius);
  327. shaderConsts->setSafe(probeBoxMinSC, probeBoxMins);
  328. shaderConsts->setSafe(probeBoxMaxSC, probeBoxMaxs);
  329. shaderConsts->setSafe(probeLocalPosSC, probeLocalPositions);
  330. shaderConsts->setSafe(probeIsSphereSC, probeIsSphere);
  331. //
  332. //shaderConsts->setSafe(lightSpotAngleSC, lightSpotAngle);
  333. //shaderConsts->setSafe(lightSpotFalloffSC, lightSpotFalloff);
  334. }
  335. else
  336. {
  337. /*if (probe->mCubemap && !probe->mCubemap->isNull())
  338. {
  339. GFX->setCubeTexture(1, probe->mCubemap->getPointer());
  340. }*/
  341. if (probeCubemapSC->isValid())
  342. {
  343. for(U32 i=0; i < 4; ++i)
  344. GFX->setCubeTexture(probeCubemapSC->getSamplerRegister() + i, NULL);
  345. }
  346. }
  347. }
  348. void ProbeManager::setProbeInfo(ProcessedMaterial *pmat,
  349. const Material *mat,
  350. const SceneData &sgData,
  351. const SceneRenderState *state,
  352. U32 pass,
  353. GFXShaderConstBuffer *shaderConsts)
  354. {
  355. // Skip this if we're rendering from the deferred bin.
  356. if ( sgData.binType == SceneData::DeferredBin )
  357. return;
  358. // if (mRegisteredProbes.empty())
  359. // return;
  360. PROFILE_SCOPE(ProbeManager_setProbeInfo);
  361. ProbeShaderConstants *psc = getProbeShaderConstants(shaderConsts);
  362. //ProbeInfo *probe;
  363. //probe = mRegisteredProbes[0];
  364. // NOTE: If you encounter a crash from this point forward
  365. // while setting a shader constant its probably because the
  366. // mConstantLookup has bad shaders/constants in it.
  367. //
  368. // This is a known crash bug that can occur if materials/shaders
  369. // are reloaded and the light manager is not reset.
  370. //
  371. // We should look to fix this by clearing the table.
  372. MatrixSet matSet = state->getRenderPass()->getMatrixSet();
  373. // Update the forward shading light constants.
  374. _update4ProbeConsts( sgData,
  375. matSet,
  376. psc->mProbePositionSC,
  377. psc->mProbeRadiusSC,
  378. psc->mProbeBoxMinSC,
  379. psc->mProbeBoxMaxSC,
  380. psc->mProbeCubemapSC,
  381. psc->mProbeIsSphereSC,
  382. psc->mProbeLocalPosSC,
  383. shaderConsts );
  384. // Static
  385. /*if (lsm && light->getCastShadows())
  386. {
  387. if (psc->mWorldToLightProjSC->isValid())
  388. shaderConsts->set(psc->mWorldToLightProjSC,
  389. lsm->getWorldToLightProj(),
  390. psc->mWorldToLightProjSC->getType());
  391. if (psc->mViewToLightProjSC->isValid())
  392. {
  393. // TODO: Should probably cache these results and
  394. // not do this mul here on every material that needs
  395. // this transform.
  396. shaderConsts->set(psc->mViewToLightProjSC,
  397. lsm->getWorldToLightProj() * state->getCameraTransform(),
  398. psc->mViewToLightProjSC->getType());
  399. }
  400. shaderConsts->setSafe(psc->mShadowMapSizeSC, 1.0f / (F32)lsm->getTexSize());
  401. // Do this last so that overrides can properly override parameters previously set
  402. lsm->setShaderParameters(shaderConsts, psc);
  403. }
  404. else
  405. {
  406. if (psc->mViewToLightProjSC->isValid())
  407. {
  408. // TODO: Should probably cache these results and
  409. // not do this mul here on every material that needs
  410. // this transform.
  411. MatrixF proj;
  412. light->getWorldToLightProj(&proj);
  413. shaderConsts->set(psc->mViewToLightProjSC,
  414. proj * state->getCameraTransform(),
  415. psc->mViewToLightProjSC->getType());
  416. }
  417. }
  418. // Dynamic
  419. if (dynamicShadowMap)
  420. {
  421. if (psc->mDynamicWorldToLightProjSC->isValid())
  422. shaderConsts->set(psc->mDynamicWorldToLightProjSC,
  423. dynamicShadowMap->getWorldToLightProj(),
  424. psc->mDynamicWorldToLightProjSC->getType());
  425. if (psc->mDynamicViewToLightProjSC->isValid())
  426. {
  427. // TODO: Should probably cache these results and
  428. // not do this mul here on every material that needs
  429. // this transform.
  430. shaderConsts->set(psc->mDynamicViewToLightProjSC,
  431. dynamicShadowMap->getWorldToLightProj() * state->getCameraTransform(),
  432. psc->mDynamicViewToLightProjSC->getType());
  433. }
  434. shaderConsts->setSafe(psc->mShadowMapSizeSC, 1.0f / (F32)dynamicShadowMap->getTexSize());
  435. // Do this last so that overrides can properly override parameters previously set
  436. dynamicShadowMap->setShaderParameters(shaderConsts, psc);
  437. }
  438. else
  439. {
  440. if (psc->mDynamicViewToLightProjSC->isValid())
  441. {
  442. // TODO: Should probably cache these results and
  443. // not do this mul here on every material that needs
  444. // this transform.
  445. MatrixF proj;
  446. light->getWorldToLightProj(&proj);
  447. shaderConsts->set(psc->mDynamicViewToLightProjSC,
  448. proj * state->getCameraTransform(),
  449. psc->mDynamicViewToLightProjSC->getType());
  450. }
  451. }*/
  452. }
  453. /// Allows us to set textures during the Material::setTextureStage call, return true if we've done work.
  454. bool ProbeManager::setTextureStage(const SceneData &sgData,
  455. const U32 currTexFlag,
  456. const U32 textureSlot,
  457. GFXShaderConstBuffer *shaderConsts,
  458. ShaderConstHandles *handles)
  459. {
  460. return false;
  461. }
  462. AvailableSLInterfaces* ProbeManager::getSceneLightingInterface()
  463. {
  464. //if ( !mAvailableSLInterfaces )
  465. // mAvailableSLInterfaces = new AvailableSLInterfaces();
  466. return NULL;
  467. }
  468. void ProbeManager::updateDirtyProbes()
  469. {
  470. for (U32 i = 0; i < ProbeRenderInst::all.size(); i++)
  471. {
  472. ProbeRenderInst* probe = ProbeRenderInst::all[i];
  473. if (probe->mDirty)
  474. {
  475. //make sure we have a fill-out on our primitives, materials, etc
  476. //so we don't have to always force an update when it's not needed
  477. if (probe->mIsSkylight)
  478. {
  479. setupSkylightProbe(probe);
  480. }
  481. else
  482. {
  483. if (probe->mProbeShapeType == ProbeRenderInst::Sphere)
  484. {
  485. setupSphereReflectionProbe(probe);
  486. }
  487. else if(probe->mProbeShapeType == ProbeRenderInst::Box)
  488. {
  489. setupConvexReflectionProbe(probe);
  490. }
  491. }
  492. probe->mDirty = false;
  493. }
  494. }
  495. }
  496. ProbeManager::SkylightMaterialInfo* ProbeManager::getSkylightMaterial()
  497. {
  498. PROFILE_SCOPE(AdvancedLightBinManager_getSkylightMaterial);
  499. //ReflectProbeMaterialInfo *info = NULL;
  500. if (!mSkylightMaterial)
  501. // Now create the material info object.
  502. mSkylightMaterial = new SkylightMaterialInfo("SklyightMaterial",
  503. getGFXVertexFormat<GFXVertexPC>());
  504. return mSkylightMaterial;
  505. }
  506. ProbeManager::ReflectProbeMaterialInfo* ProbeManager::getReflectProbeMaterial()
  507. {
  508. PROFILE_SCOPE(AdvancedLightBinManager_getReflectProbeMaterial);
  509. //ReflectProbeMaterialInfo *info = NULL;
  510. if (!mReflectProbeMaterial)
  511. // Now create the material info object.
  512. mReflectProbeMaterial = new ReflectProbeMaterialInfo("ReflectionProbeMaterial",
  513. getGFXVertexFormat<GFXVertexPC>());
  514. return mReflectProbeMaterial;
  515. }
  516. void ProbeManager::setupSkylightProbe(ProbeRenderInst *probeInfo)
  517. {
  518. probeInfo->vertBuffer = getSphereMesh(probeInfo->numPrims, probeInfo->primBuffer);
  519. if (!mSkylightMaterial)
  520. mSkylightMaterial = getSkylightMaterial();
  521. }
  522. void ProbeManager::setupSphereReflectionProbe(ProbeRenderInst *probeInfo)
  523. {
  524. probeInfo->vertBuffer = getSphereMesh(probeInfo->numPrims, probeInfo->primBuffer);
  525. if (!mReflectProbeMaterial)
  526. mReflectProbeMaterial = getReflectProbeMaterial();
  527. }
  528. void ProbeManager::setupConvexReflectionProbe(ProbeRenderInst *probeInfo)
  529. {
  530. static const Point3F cubePoints[8] =
  531. {
  532. Point3F(1, -1, -1), Point3F(1, -1, 1), Point3F(1, 1, -1), Point3F(1, 1, 1),
  533. Point3F(-1, -1, -1), Point3F(-1, 1, -1), Point3F(-1, -1, 1), Point3F(-1, 1, 1)
  534. };
  535. /*static const Point3F cubeNormals[6] =
  536. {
  537. Point3F(1, 0, 0), Point3F(-1, 0, 0), Point3F(0, 1, 0),
  538. Point3F(0, -1, 0), Point3F(0, 0, 1), Point3F(0, 0, -1)
  539. };*/
  540. /*static const Point2F cubeTexCoords[4] =
  541. {
  542. Point2F(0, 0), Point2F(0, -1),
  543. Point2F(1, 0), Point2F(1, -1)
  544. };*/
  545. static const U32 cubeFaces[36][3] =
  546. {
  547. { 3, 0, 3 },{ 0, 0, 0 },{ 1, 0, 1 },
  548. { 2, 0, 2 },{ 0, 0, 0 },{ 3, 0, 3 },
  549. { 7, 1, 1 },{ 4, 1, 2 },{ 5, 1, 0 },
  550. { 6, 1, 3 },{ 4, 1, 2 },{ 7, 1, 1 },
  551. { 3, 2, 1 },{ 5, 2, 2 },{ 2, 2, 0 },
  552. { 7, 2, 3 },{ 5, 2, 2 },{ 3, 2, 1 },
  553. { 1, 3, 3 },{ 4, 3, 0 },{ 6, 3, 1 },
  554. { 0, 3, 2 },{ 4, 3, 0 },{ 1, 3, 3 },
  555. { 3, 4, 3 },{ 6, 4, 0 },{ 7, 4, 1 },
  556. { 1, 4, 2 },{ 6, 4, 0 },{ 3, 4, 3 },
  557. { 2, 5, 1 },{ 4, 5, 2 },{ 0, 5, 0 },
  558. { 5, 5, 3 },{ 4, 5, 2 },{ 2, 5, 1 }
  559. };
  560. // Fill the vertex buffer
  561. GFXVertexPC *pVert = NULL;
  562. probeInfo->numVerts = 36;
  563. probeInfo->vertBuffer.set(GFX, 36, GFXBufferTypeStatic);
  564. pVert = probeInfo->vertBuffer.lock();
  565. Point3F halfSize = Point3F(probeInfo->mRadius, probeInfo->mRadius, probeInfo->mRadius);
  566. for (U32 i = 0; i < 36; i++)
  567. {
  568. const U32& vdx = cubeFaces[i][0];
  569. pVert[i].point = cubePoints[vdx] * halfSize;
  570. }
  571. probeInfo->vertBuffer.unlock();
  572. // Fill the primitive buffer
  573. U16 *pIdx = NULL;
  574. probeInfo->primBuffer.set(GFX, 36, 12, GFXBufferTypeStatic);
  575. probeInfo->primBuffer.lock(&pIdx);
  576. for (U16 i = 0; i < 36; i++)
  577. pIdx[i] = i;
  578. probeInfo->primBuffer.unlock();
  579. probeInfo->numPrims = 12;
  580. if (!mReflectProbeMaterial)
  581. mReflectProbeMaterial = getReflectProbeMaterial();
  582. //
  583. // mReflectProbeBin.push_back(pEntry);
  584. }
  585. GFXVertexBufferHandle<GFXVertexPC> ProbeManager::getSphereMesh(U32 &outNumPrimitives, GFXPrimitiveBufferHandle &outPrimitives)
  586. {
  587. static SphereMesh sSphereMesh;
  588. if (mSphereGeometry.isNull())
  589. {
  590. const SphereMesh::TriangleMesh * sphereMesh = sSphereMesh.getMesh(3);
  591. S32 numPoly = sphereMesh->numPoly;
  592. mSpherePrimitiveCount = 0;
  593. mSphereGeometry.set(GFX, numPoly * 3, GFXBufferTypeStatic);
  594. mSphereGeometry.lock();
  595. S32 vertexIndex = 0;
  596. for (S32 i = 0; i<numPoly; i++)
  597. {
  598. mSpherePrimitiveCount++;
  599. mSphereGeometry[vertexIndex].point = sphereMesh->poly[i].pnt[0];
  600. mSphereGeometry[vertexIndex].color = ColorI::WHITE;
  601. vertexIndex++;
  602. mSphereGeometry[vertexIndex].point = sphereMesh->poly[i].pnt[1];
  603. mSphereGeometry[vertexIndex].color = ColorI::WHITE;
  604. vertexIndex++;
  605. mSphereGeometry[vertexIndex].point = sphereMesh->poly[i].pnt[2];
  606. mSphereGeometry[vertexIndex].color = ColorI::WHITE;
  607. vertexIndex++;
  608. }
  609. mSphereGeometry.unlock();
  610. }
  611. outNumPrimitives = mSpherePrimitiveCount;
  612. outPrimitives = NULL; // For now
  613. return mSphereGeometry;
  614. }
  615. //
  616. //
  617. bool ReflectProbeMatInstance::init(const FeatureSet &features, const GFXVertexFormat *vertexFormat)
  618. {
  619. bool success = Parent::init(features, vertexFormat);
  620. // If the initialization failed don't continue.
  621. if (!success || !mProcessedMaterial || mProcessedMaterial->getNumPasses() == 0)
  622. return false;
  623. return true;
  624. }
  625. bool ReflectProbeMatInstance::setupPass(SceneRenderState *state, const SceneData &sgData)
  626. {
  627. // Go no further if the material failed to initialize properly.
  628. if (!mProcessedMaterial ||
  629. mProcessedMaterial->getNumPasses() == 0)
  630. return false;
  631. bool bRetVal = Parent::setupPass(state, sgData);;
  632. AssertFatal(mProcessedMaterial->getNumPasses() > 0, "No passes created! Ohnoes");
  633. const RenderPassData *rpd = mProcessedMaterial->getPass(0);
  634. AssertFatal(rpd, "No render pass data!");
  635. AssertFatal(rpd->mRenderStates[0], "No render state 0!");
  636. if (!mProjectionState)
  637. {
  638. GFXStateBlockDesc desc;
  639. desc.setZReadWrite(false);
  640. desc.zWriteEnable = false;
  641. desc.setCullMode(GFXCullNone);
  642. desc.setBlend(true, GFXBlendOne, GFXBlendOne);
  643. mProjectionState = GFX->createStateBlock(desc);
  644. }
  645. // Now override stateblock with our own
  646. GFX->setStateBlock(mProjectionState);
  647. return bRetVal;
  648. }
  649. //
  650. //
  651. ProbeManager::ReflectProbeMaterialInfo::ReflectProbeMaterialInfo(const String &matName,
  652. const GFXVertexFormat *vertexFormat)
  653. : matInstance(NULL),
  654. zNearFarInvNearFar(NULL),
  655. farPlane(NULL),
  656. vsFarPlane(NULL),
  657. negFarPlaneDotEye(NULL),
  658. probeWSPos(NULL),
  659. attenuation(NULL),
  660. radius(NULL),
  661. invViewMat(NULL),
  662. cubeMips(NULL)
  663. {
  664. Material *mat = MATMGR->getMaterialDefinitionByName(matName);
  665. if (!mat)
  666. return;
  667. matInstance = new ReflectProbeMatInstance(*mat);
  668. const Vector<GFXShaderMacro> &macros = Vector<GFXShaderMacro>();
  669. for (U32 i = 0; i < macros.size(); i++)
  670. matInstance->addShaderMacro(macros[i].name, macros[i].value);
  671. matInstance->init(MATMGR->getDefaultFeatures(), vertexFormat);
  672. attenuation = matInstance->getMaterialParameterHandle("$attenuation");
  673. radius = matInstance->getMaterialParameterHandle("$radius");
  674. probeLSPos = matInstance->getMaterialParameterHandle("$probeLSPos");
  675. probeWSPos = matInstance->getMaterialParameterHandle("$probeWSPos");
  676. farPlane = matInstance->getMaterialParameterHandle("$farPlane");
  677. vsFarPlane = matInstance->getMaterialParameterHandle("$vsFarPlane");
  678. negFarPlaneDotEye = matInstance->getMaterialParameterHandle("$negFarPlaneDotEye");
  679. zNearFarInvNearFar = matInstance->getMaterialParameterHandle("$zNearFarInvNearFar");
  680. invViewMat = matInstance->getMaterialParameterHandle("$invViewMat");
  681. useCubemap = matInstance->getMaterialParameterHandle("$useCubemap");
  682. cubemap = matInstance->getMaterialParameterHandle("$cubeMap");
  683. cubeMips = matInstance->getMaterialParameterHandle("$cubeMips");
  684. eyePosWorld = matInstance->getMaterialParameterHandle("$eyePosWorld");
  685. bbMin = matInstance->getMaterialParameterHandle("$bbMin");
  686. bbMax = matInstance->getMaterialParameterHandle("$bbMax");
  687. useSphereMode = matInstance->getMaterialParameterHandle("$useSphereMode");
  688. for (U32 i = 0; i < 9; i++)
  689. shTerms[i] = matInstance->getMaterialParameterHandle(String::ToString("$SHTerms%d", i));
  690. for (U32 i = 0; i < 5; i++)
  691. shConsts[i] = matInstance->getMaterialParameterHandle(String::ToString("$SHConsts%d", i));
  692. }
  693. ProbeManager::ReflectProbeMaterialInfo::~ReflectProbeMaterialInfo()
  694. {
  695. SAFE_DELETE(matInstance);
  696. }
  697. void ProbeManager::ReflectProbeMaterialInfo::setViewParameters(const F32 _zNear,
  698. const F32 _zFar,
  699. const Point3F &_eyePos,
  700. const PlaneF &_farPlane,
  701. const PlaneF &_vsFarPlane, const MatrixF &_inverseViewMatrix)
  702. {
  703. MaterialParameters *matParams = matInstance->getMaterialParameters();
  704. matParams->setSafe(farPlane, *((const Point4F *)&_farPlane));
  705. matParams->setSafe(vsFarPlane, *((const Point4F *)&_vsFarPlane));
  706. if (negFarPlaneDotEye->isValid())
  707. {
  708. // -dot( farPlane, eyePos )
  709. const F32 negFarPlaneDotEyeVal = -(mDot(*((const Point3F *)&_farPlane), _eyePos) + _farPlane.d);
  710. matParams->set(negFarPlaneDotEye, negFarPlaneDotEyeVal);
  711. }
  712. matParams->setSafe(zNearFarInvNearFar, Point4F(_zNear, _zFar, 1.0f / _zNear, 1.0f / _zFar));
  713. matParams->setSafe(invViewMat, _inverseViewMatrix);
  714. Point4F frPlane = *((const Point4F *)&_farPlane);
  715. Point4F vsFrPlane = *((const Point4F *)&_vsFarPlane);
  716. Point4F nearFarInvNearFar = Point4F(_zNear, _zFar, 1.0f / _zNear, 1.0f / _zFar);
  717. const F32 negFarPlaneDotEyeVal = -(mDot(*((const Point3F *)&_farPlane), _eyePos) + _farPlane.d);
  718. }
  719. void ProbeManager::ReflectProbeMaterialInfo::setProbeParameters(const ProbeRenderInst *probeInfo, const SceneRenderState* renderState, const MatrixF &worldViewOnly)
  720. {
  721. //Set up the params
  722. MaterialParameters *matParams = matInstance->getMaterialParameters();
  723. matParams->setSafe(radius, probeInfo->mRadius);
  724. Point3F probePos = probeInfo->getPosition() + probeInfo->mProbePosOffset;
  725. //worldViewOnly.mulP(probeInfo->getPosition(), &probePos);
  726. matParams->setSafe(probeWSPos, probePos);
  727. worldViewOnly.mulP(probeInfo->getPosition(), &probePos);
  728. matParams->setSafe(probeLSPos, probePos);
  729. // Get the attenuation falloff ratio and normalize it.
  730. Point3F attenRatio = Point3F(0.0f, 1.0f, 1.0f);
  731. F32 total = attenRatio.x + attenRatio.y + attenRatio.z;
  732. if (total > 0.0f)
  733. attenRatio /= total;
  734. F32 radius = probeInfo->mRadius;
  735. Point2F attenParams((1.0f / radius) * attenRatio.y,
  736. (1.0f / (radius * radius)) * attenRatio.z);
  737. matParams->setSafe(attenuation, attenParams);
  738. NamedTexTarget* deferredTexTarget = NamedTexTarget::find("deferred");
  739. GFXTextureObject *deferredTexObject = deferredTexTarget->getTexture();
  740. if (!deferredTexObject) return;
  741. GFX->setTexture(0, deferredTexObject);
  742. NamedTexTarget* matInfoTexTarget = NamedTexTarget::find("matinfo");
  743. GFXTextureObject *matInfoTexObject = matInfoTexTarget->getTexture();
  744. if (!matInfoTexObject) return;
  745. GFX->setTexture(1, matInfoTexObject);
  746. if (probeInfo->mCubemap && !probeInfo->mCubemap->isNull())
  747. {
  748. GFX->setCubeTexture(2, probeInfo->mCubemap->getPointer());
  749. }
  750. else
  751. {
  752. GFX->setCubeTexture(2, NULL);
  753. }
  754. if (probeInfo->mIrradianceCubemap && !probeInfo->mIrradianceCubemap->isNull())
  755. {
  756. GFX->setCubeTexture(3, probeInfo->mIrradianceCubemap->getPointer());
  757. }
  758. else
  759. {
  760. GFX->setCubeTexture(3, NULL);
  761. }
  762. if (probeInfo->mBRDFTexture && !probeInfo->mBRDFTexture->isNull())
  763. {
  764. GFX->setTexture(4, probeInfo->mBRDFTexture->getPointer());
  765. }
  766. else
  767. {
  768. GFX->setTexture(4, NULL);
  769. }
  770. if (probeInfo->mCubemap->isValid())
  771. matParams->setSafe(cubeMips, mPow(probeInfo->mCubemap->getPointer()->getMipMapLevels(), 2.0f));
  772. else
  773. matParams->setSafe(cubeMips, F32(0.0));
  774. matParams->setSafe(eyePosWorld, renderState->getCameraPosition());
  775. matParams->setSafe(bbMin, probeInfo->mBounds.minExtents);
  776. matParams->setSafe(bbMax, probeInfo->mBounds.maxExtents);
  777. matParams->setSafe(useSphereMode, probeInfo->mProbeShapeType == ProbeRenderInst::Sphere ? 1.0f : 0.0f);
  778. //SH Terms
  779. //static AlignedArray<Point3F> shTermsArray(9, sizeof(Point3F));
  780. //dMemset(shTermsArray.getBuffer(), 0, shTermsArray.getBufferSize());
  781. for (U32 i = 0; i < 9; i++)
  782. {
  783. matParams->setSafe(shTerms[i], probeInfo->mSHTerms[i]);
  784. }
  785. for (U32 i = 0; i < 5; i++)
  786. {
  787. matParams->setSafe(shConsts[i], probeInfo->mSHConstants[i]);
  788. }
  789. const MatrixF worldToObjectXfm = probeInfo->mTransform;
  790. MaterialParameterHandle *worldToObjMat = matInstance->getMaterialParameterHandle("$worldToObj");
  791. matParams->setSafe(worldToObjMat, worldToObjectXfm);
  792. }
  793. //
  794. //
  795. //
  796. ProbeManager::SkylightMaterialInfo::SkylightMaterialInfo(const String &matName,
  797. const GFXVertexFormat *vertexFormat)
  798. : ReflectProbeMaterialInfo(matName, vertexFormat)
  799. {
  800. Material *mat = MATMGR->getMaterialDefinitionByName(matName);
  801. if (!mat)
  802. return;
  803. matInstance = new SkylightMatInstance(*mat);
  804. const Vector<GFXShaderMacro> &macros = Vector<GFXShaderMacro>();
  805. for (U32 i = 0; i < macros.size(); i++)
  806. matInstance->addShaderMacro(macros[i].name, macros[i].value);
  807. matInstance->init(MATMGR->getDefaultFeatures(), vertexFormat);
  808. farPlane = matInstance->getMaterialParameterHandle("$farPlane");
  809. vsFarPlane = matInstance->getMaterialParameterHandle("$vsFarPlane");
  810. negFarPlaneDotEye = matInstance->getMaterialParameterHandle("$negFarPlaneDotEye");
  811. zNearFarInvNearFar = matInstance->getMaterialParameterHandle("$zNearFarInvNearFar");
  812. invViewMat = matInstance->getMaterialParameterHandle("$invViewMat");
  813. useCubemap = matInstance->getMaterialParameterHandle("$useCubemap");
  814. cubemap = matInstance->getMaterialParameterHandle("$cubeMap");
  815. eyePosWorld = matInstance->getMaterialParameterHandle("$eyePosWorld");
  816. for (U32 i = 0; i < 9; i++)
  817. shTerms[i] = matInstance->getMaterialParameterHandle(String::ToString("$SHTerms%d", i));
  818. for (U32 i = 0; i < 5; i++)
  819. shConsts[i] = matInstance->getMaterialParameterHandle(String::ToString("$SHConsts%d", i));
  820. }
  821. ProbeManager::SkylightMaterialInfo::~SkylightMaterialInfo()
  822. {
  823. SAFE_DELETE(matInstance);
  824. }
  825. /*bool ProbeManager::lightScene( const char* callback, const char* param )
  826. {
  827. BitSet32 flags = 0;
  828. if ( param )
  829. {
  830. if ( !dStricmp( param, "forceAlways" ) )
  831. flags.set( SceneLighting::ForceAlways );
  832. else if ( !dStricmp(param, "forceWritable" ) )
  833. flags.set( SceneLighting::ForceWritable );
  834. else if ( !dStricmp(param, "loadOnly" ) )
  835. flags.set( SceneLighting::LoadOnly );
  836. }
  837. // The SceneLighting object will delete itself
  838. // once the lighting process is complete.
  839. SceneLighting* sl = new SceneLighting( getSceneLightingInterface() );
  840. return sl->lightScene( callback, flags );
  841. }*/
  842. /*RenderDeferredMgr* ProbeManager::_findDeferredRenderBin()
  843. {
  844. RenderPassManager* rpm = getSceneManager()->getDefaultRenderPass();
  845. for( U32 i = 0; i < rpm->getManagerCount(); i++ )
  846. {
  847. RenderBinManager *bin = rpm->getManager( i );
  848. if( bin->getRenderInstType() == RenderDeferredMgr::RIT_Deferred )
  849. {
  850. return ( RenderDeferredMgr* ) bin;
  851. }
  852. }
  853. return NULL;
  854. }*/
  855. DefineEngineFunction( CreateProbeManager, bool, (),,
  856. "Finds and activates the named light manager.\n"
  857. "@return Returns true if the light manager is found and activated.\n"
  858. "@ingroup Lighting\n" )
  859. {
  860. ProbeManager* probeManager = new ProbeManager();
  861. if (probeManager != nullptr && gClientSceneGraph != nullptr)
  862. {
  863. probeManager->activate(gClientSceneGraph);
  864. return true;
  865. }
  866. return false;
  867. }
  868. DefineEngineFunction( resetProbeManager, void, (),,
  869. "@brief Deactivates and then activates the currently active light manager."
  870. "This causes most shaders to be regenerated and is often used when global "
  871. "rendering changes have occured.\n"
  872. "@ingroup Lighting\n" )
  873. {
  874. ProbeManager *pm = PROBEMGR;
  875. if ( !pm)
  876. return;
  877. /*SceneManager *sm = lm->getSceneManager();
  878. lm->deactivate();
  879. lm->activate( sm );*/
  880. }