renderProbeMgr.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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 "renderProbeMgr.h"
  23. #include "console/consoleTypes.h"
  24. #include "scene/sceneObject.h"
  25. #include "materials/materialManager.h"
  26. #include "scene/sceneRenderState.h"
  27. #include "math/util/sphereMesh.h"
  28. #include "math/util/matrixSet.h"
  29. #include "materials/processedMaterial.h"
  30. #include "renderInstance/renderDeferredMgr.h"
  31. #include "math/mPolyhedron.impl.h"
  32. #include "gfx/gfxTransformSaver.h"
  33. #include "gfx/gfxDebugEvent.h"
  34. #include "shaderGen/shaderGenVars.h"
  35. #include "materials/shaderData.h"
  36. #include "gfx/gfxTextureManager.h"
  37. #include "postFx/postEffect.h"
  38. #include "T3D/lighting/reflectionProbe.h"
  39. #include "T3D/lighting/IBLUtilities.h"
  40. //For our cameraQuery setup
  41. #include "T3D/gameTSCtrl.h"
  42. #define TORQUE_GFX_VISUAL_DEBUG //renderdoc debugging
  43. IMPLEMENT_CONOBJECT(RenderProbeMgr);
  44. ConsoleDocClass( RenderProbeMgr,
  45. "@brief A render bin which uses object callbacks for rendering.\n\n"
  46. "This render bin gathers object render instances and calls its delegate "
  47. "method to perform rendering. It is used infrequently for specialized "
  48. "scene objects which perform custom rendering.\n\n"
  49. "@ingroup RenderBin\n" );
  50. RenderProbeMgr *RenderProbeMgr::smProbeManager = NULL;
  51. bool RenderProbeMgr::smRenderReflectionProbes = true;
  52. S32 QSORT_CALLBACK AscendingReflectProbeInfluence(const void* a, const void* b)
  53. {
  54. // Debug Profiling.
  55. PROFILE_SCOPE(AdvancedLightBinManager_AscendingReflectProbeInfluence);
  56. // Fetch asset definitions.
  57. const ProbeRenderInst* pReflectProbeA = (*(ProbeRenderInst**)a);
  58. const ProbeRenderInst* pReflectProbeB = (*(ProbeRenderInst**)b);
  59. //sort by score
  60. return pReflectProbeA->mScore - pReflectProbeB->mScore;
  61. }
  62. //
  63. //
  64. ProbeRenderInst::ProbeRenderInst() : SystemInterface(),
  65. mTransform(true),
  66. mDirty(false),
  67. mPriority(1.0f),
  68. mScore(0.0f),
  69. mCubemap(NULL),
  70. mIrradianceCubemap(NULL),
  71. mRadius(1.0f),
  72. mProbeRefOffset(0, 0, 0),
  73. mProbeRefScale(1,1,1)
  74. {
  75. }
  76. ProbeRenderInst::~ProbeRenderInst()
  77. {
  78. if (mCubemap && mCubemap.isValid())
  79. {
  80. mCubemap.free();
  81. }
  82. if (mIrradianceCubemap && mIrradianceCubemap.isValid())
  83. {
  84. mIrradianceCubemap.free();
  85. }
  86. }
  87. void ProbeRenderInst::set(const ProbeRenderInst *probeInfo)
  88. {
  89. mTransform = probeInfo->mTransform;
  90. mCubemap = probeInfo->mCubemap;
  91. mIrradianceCubemap = probeInfo->mIrradianceCubemap;
  92. mRadius = probeInfo->mRadius;
  93. mProbeShapeType = probeInfo->mProbeShapeType;
  94. mBounds = probeInfo->mBounds;
  95. mIsSkylight = probeInfo->mIsSkylight;
  96. mScore = probeInfo->mScore;
  97. }
  98. //
  99. //
  100. ProbeShaderConstants::ProbeShaderConstants()
  101. : mInit(false),
  102. mShader(NULL),
  103. mProbeParamsSC(NULL),
  104. mProbePositionSC(NULL),
  105. mProbeRadiusSC(NULL),
  106. mProbeBoxMinSC(NULL),
  107. mProbeBoxMaxSC(NULL),
  108. mProbeIsSphereSC(NULL),
  109. mProbeLocalPosSC(NULL),
  110. mProbeCubemapSC(NULL),
  111. mProbeCountSC(NULL)
  112. {
  113. }
  114. ProbeShaderConstants::~ProbeShaderConstants()
  115. {
  116. if (mShader.isValid())
  117. {
  118. mShader->getReloadSignal().remove(this, &ProbeShaderConstants::_onShaderReload);
  119. mShader = NULL;
  120. }
  121. }
  122. void ProbeShaderConstants::init(GFXShader* shader)
  123. {
  124. if (mShader.getPointer() != shader)
  125. {
  126. if (mShader.isValid())
  127. mShader->getReloadSignal().remove(this, &ProbeShaderConstants::_onShaderReload);
  128. mShader = shader;
  129. mShader->getReloadSignal().notify(this, &ProbeShaderConstants::_onShaderReload);
  130. }
  131. mProbeParamsSC = shader->getShaderConstHandle("$probeParams");
  132. //Reflection Probes
  133. mProbePositionSC = shader->getShaderConstHandle(ShaderGenVars::probePosition);
  134. mProbeRadiusSC = shader->getShaderConstHandle(ShaderGenVars::probeRadius);
  135. mProbeBoxMinSC = shader->getShaderConstHandle(ShaderGenVars::probeBoxMin);
  136. mProbeBoxMaxSC = shader->getShaderConstHandle(ShaderGenVars::probeBoxMax);
  137. mProbeIsSphereSC = shader->getShaderConstHandle(ShaderGenVars::probeIsSphere);
  138. mProbeLocalPosSC = shader->getShaderConstHandle(ShaderGenVars::probeLocalPos);
  139. mProbeCubemapSC = shader->getShaderConstHandle(ShaderGenVars::probeCubemap);
  140. mProbeCountSC = shader->getShaderConstHandle(ShaderGenVars::probeCount);
  141. mInit = true;
  142. }
  143. void ProbeShaderConstants::_onShaderReload()
  144. {
  145. if (mShader.isValid())
  146. init(mShader);
  147. }
  148. //
  149. //
  150. RenderProbeMgr::RenderProbeMgr()
  151. : RenderBinManager(RenderPassManager::RIT_Probes, 1.0f, 1.0f),
  152. mLastShader(nullptr),
  153. mLastConstants(nullptr)
  154. {
  155. mEffectiveProbeCount = 0;
  156. mMipCount = 0;
  157. mProbeArrayEffect = nullptr;
  158. smProbeManager = this;
  159. }
  160. RenderProbeMgr::RenderProbeMgr(RenderInstType riType, F32 renderOrder, F32 processAddOrder)
  161. : RenderBinManager(riType, renderOrder, processAddOrder)
  162. {
  163. }
  164. RenderProbeMgr::~RenderProbeMgr()
  165. {
  166. mLastShader = NULL;
  167. mLastConstants = NULL;
  168. for (ProbeConstantMap::Iterator i = mConstantLookup.begin(); i != mConstantLookup.end(); i++)
  169. {
  170. if (i->value)
  171. SAFE_DELETE(i->value);
  172. }
  173. mConstantLookup.clear();
  174. }
  175. void RenderProbeMgr::onRemove()
  176. {
  177. Parent::onRemove();
  178. }
  179. void RenderProbeMgr::initPersistFields()
  180. {
  181. Parent::initPersistFields();
  182. }
  183. void RenderProbeMgr::addElement(RenderInst *inst)
  184. {
  185. // If this instance is translucent handle it in RenderTranslucentMgr
  186. //if (inst->translucentSort)
  187. return;
  188. //AssertFatal(inst->defaultKey != 0, "RenderMeshMgr::addElement() - Got null sort key... did you forget to set it?");
  189. /*internalAddElement(inst);
  190. ProbeRenderInst* probeInst = static_cast<ProbeRenderInst*>(inst);
  191. if (probeInst->mIsSkylight)
  192. {
  193. addSkylightProbe(probeInst);
  194. }
  195. else
  196. {
  197. if (probeInst->mProbeShapeType == ProbeInfo::Sphere)
  198. addSphereReflectionProbe(probeInst);
  199. else
  200. addConvexReflectionProbe(probeInst);
  201. }*/
  202. }
  203. void RenderProbeMgr::registerProbe(U32 probeIdx)
  204. {
  205. //Mostly for consolidation, but also lets us sanity check or prep any other data we need for rendering this in one place at time of flagging for render
  206. if (probeIdx >= ProbeRenderInst::all.size())
  207. return;
  208. mRegisteredProbes.push_back_unique(probeIdx);
  209. //rebuild our probe data
  210. _setupStaticParameters();
  211. }
  212. void RenderProbeMgr::unregisterProbe(U32 probeIdx)
  213. {
  214. //Mostly for consolidation, but also lets us sanity check or prep any other data we need for rendering this in one place at time of flagging for render
  215. if (probeIdx >= ProbeRenderInst::all.size())
  216. return;
  217. mRegisteredProbes.remove(probeIdx);
  218. //rebuild our probe data
  219. _setupStaticParameters();
  220. }
  221. //
  222. //
  223. PostEffect* RenderProbeMgr::getProbeArrayEffect()
  224. {
  225. if (!mProbeArrayEffect)
  226. {
  227. mProbeArrayEffect = dynamic_cast<PostEffect*>(Sim::findObject("reflectionProbeArrayPostFX"));
  228. if (!mProbeArrayEffect)
  229. return nullptr;
  230. }
  231. return mProbeArrayEffect;
  232. }
  233. //remove
  234. //Con::setIntVariable("lightMetrics::activeReflectionProbes", mReflectProbeBin.size());
  235. //Con::setIntVariable("lightMetrics::culledReflectProbes", 0/*mNumLightsCulled*/);
  236. //
  237. void RenderProbeMgr::updateProbes()
  238. {
  239. _setupStaticParameters();
  240. }
  241. void RenderProbeMgr::_setupStaticParameters()
  242. {
  243. //Array rendering
  244. U32 probeCount = ProbeRenderInst::all.size();
  245. mEffectiveProbeCount = 0;
  246. mMipCount = 0;
  247. if (probePositionsData.size() != MAXPROBECOUNT)
  248. {
  249. probePositionsData.setSize(MAXPROBECOUNT);
  250. probeRefPositionsData.setSize(MAXPROBECOUNT);
  251. probeWorldToObjData.setSize(MAXPROBECOUNT);
  252. probeBBMinData.setSize(MAXPROBECOUNT);
  253. probeBBMaxData.setSize(MAXPROBECOUNT);
  254. probeConfigData.setSize(MAXPROBECOUNT);
  255. }
  256. probePositionsData.fill(Point4F::Zero);
  257. probeRefPositionsData.fill(Point4F::Zero);
  258. probeWorldToObjData.fill(MatrixF::Identity);
  259. probeBBMinData.fill(Point4F::Zero);
  260. probeBBMaxData.fill(Point4F::Zero);
  261. probeConfigData.fill(Point4F::Zero);
  262. cubeMaps.clear();
  263. irradMaps.clear();
  264. //This should probably ultimately be a per-probe value, but for now, global for testing/adjustability
  265. F32 attenuation = Con::getFloatVariable("$pref::ReflectionProbes::AttenuationStrength", 3.5);
  266. for (U32 i = 0; i < probeCount; i++)
  267. {
  268. if (mEffectiveProbeCount >= MAXPROBECOUNT)
  269. break;
  270. const ProbeRenderInst& curEntry = *ProbeRenderInst::all[i];
  271. if (!curEntry.mIsEnabled)
  272. continue;
  273. if (curEntry.mCubemap.isNull() || curEntry.mIrradianceCubemap.isNull())
  274. continue;
  275. if (!curEntry.mCubemap->isInitialised())
  276. continue;
  277. if (!curEntry.mIrradianceCubemap->isInitialised())
  278. continue;
  279. if (curEntry.mIsSkylight)
  280. continue;
  281. mMipCount = curEntry.mCubemap.getPointer()->getMipMapLevels();
  282. //Setup
  283. Point3F probePos = curEntry.getPosition();
  284. Point3F refPos = curEntry.getPosition() +curEntry.mProbeRefOffset;
  285. probePositionsData[mEffectiveProbeCount] = Point4F(probePos.x, probePos.y, probePos.z,0);
  286. probeRefPositionsData[mEffectiveProbeCount] = Point4F(refPos.x, refPos.y, refPos.z, 0);
  287. probeWorldToObjData[mEffectiveProbeCount] = curEntry.getTransform();
  288. Point3F bbMin = refPos - curEntry.mProbeRefScale/2;
  289. Point3F bbMax = refPos + curEntry.mProbeRefScale/2;
  290. probeBBMinData[mEffectiveProbeCount] = Point4F(bbMin.x, bbMin.y, bbMin.z, 0);
  291. probeBBMaxData[mEffectiveProbeCount] = Point4F(bbMax.x, bbMax.y, bbMax.z, 0);
  292. probeConfigData[mEffectiveProbeCount] = Point4F(curEntry.mProbeShapeType == ProbeRenderInst::Sphere ? 1 : 0,
  293. curEntry.mRadius,
  294. attenuation,
  295. 1);
  296. cubeMaps.push_back(curEntry.mCubemap);
  297. irradMaps.push_back(curEntry.mIrradianceCubemap);
  298. mEffectiveProbeCount++;
  299. }
  300. if (mEffectiveProbeCount != 0)
  301. {
  302. mCubemapArray = GFXCubemapArrayHandle(GFX->createCubemapArray());
  303. mIrradArray = GFXCubemapArrayHandle(GFX->createCubemapArray());
  304. mCubemapArray->initStatic(cubeMaps.address(), cubeMaps.size());
  305. mIrradArray->initStatic(irradMaps.address(), irradMaps.size());
  306. }
  307. }
  308. void RenderProbeMgr::_setupPerFrameParameters(const SceneRenderState *state)
  309. {
  310. PROFILE_SCOPE(RenderProbeMgr_SetupPerFrameParameters);
  311. }
  312. ProbeShaderConstants* RenderProbeMgr::getProbeShaderConstants(GFXShaderConstBuffer* buffer)
  313. {
  314. if (!buffer)
  315. return NULL;
  316. PROFILE_SCOPE(ProbeManager_GetProbeShaderConstants);
  317. GFXShader* shader = buffer->getShader();
  318. // Check to see if this is the same shader, we'll get hit repeatedly by
  319. // the same one due to the render bin loops.
  320. if (mLastShader.getPointer() != shader)
  321. {
  322. ProbeConstantMap::Iterator iter = mConstantLookup.find(shader);
  323. if (iter != mConstantLookup.end())
  324. {
  325. mLastConstants = iter->value;
  326. }
  327. else
  328. {
  329. ProbeShaderConstants* psc = new ProbeShaderConstants();
  330. mConstantLookup[shader] = psc;
  331. mLastConstants = psc;
  332. }
  333. // Set our new shader
  334. mLastShader = shader;
  335. }
  336. // Make sure that our current lighting constants are initialized
  337. if (mLastConstants && !mLastConstants->mInit)
  338. mLastConstants->init(shader);
  339. return mLastConstants;
  340. }
  341. void RenderProbeMgr::_update4ProbeConsts(const SceneData &sgData,
  342. MatrixSet &matSet,
  343. GFXShaderConstHandle *probePositionSC,
  344. GFXShaderConstHandle *probeRadiusSC,
  345. GFXShaderConstHandle *probeBoxMinSC,
  346. GFXShaderConstHandle *probeBoxMaxSC,
  347. GFXShaderConstHandle *probeCubemapSC,
  348. GFXShaderConstHandle *probeIsSphereSC,
  349. GFXShaderConstHandle *probeLocalPosSC,
  350. GFXShaderConstBuffer *shaderConsts)
  351. {
  352. PROFILE_SCOPE(ProbeManager_Update4ProbeConsts);
  353. // Skip over gathering lights if we don't have to!
  354. if (probePositionSC->isValid() ||
  355. probeRadiusSC->isValid() ||
  356. probeBoxMinSC->isValid() ||
  357. probeBoxMaxSC->isValid() ||
  358. probeCubemapSC->isValid()/* && (!ProbeRenderInst::all.empty())*/)
  359. {
  360. PROFILE_SCOPE(ProbeManager_Update4ProbeConsts_setProbes);
  361. static AlignedArray<Point3F> probePositions(4, sizeof(Point3F));
  362. static AlignedArray<F32> probeRadius(4, sizeof(F32));
  363. static AlignedArray<Point3F> probeBoxMins(4, sizeof(Point3F));
  364. static AlignedArray<Point3F> probeBoxMaxs(4, sizeof(Point3F));
  365. static AlignedArray<Point3F> probeLocalPositions(4, sizeof(Point3F));
  366. static AlignedArray<F32> probeIsSphere(4, sizeof(F32));
  367. //static AlignedArray<CubemapData> probeCubemap(4, sizeof(CubemapData));
  368. //F32 range;
  369. // Need to clear the buffers so that we don't leak
  370. // lights from previous passes or have NaNs.
  371. dMemset(probePositions.getBuffer(), 0, probePositions.getBufferSize());
  372. dMemset(probeRadius.getBuffer(), 0, probeRadius.getBufferSize());
  373. dMemset(probeBoxMins.getBuffer(), 0, probeBoxMins.getBufferSize());
  374. dMemset(probeBoxMaxs.getBuffer(), 0, probeBoxMaxs.getBufferSize());
  375. dMemset(probeLocalPositions.getBuffer(), 0, probeLocalPositions.getBufferSize());
  376. dMemset(probeIsSphere.getBuffer(), 0, probeRadius.getBufferSize());
  377. //dMemset(probeCubemap.getBuffer(), 0, probeCubemap.getBufferSize());
  378. matSet.restoreSceneViewProjection();
  379. //const MatrixF &worldToCameraXfm = matSet.getWorldToCamera();
  380. // Gather the data for the first 4 probes.
  381. /*const ProbeRenderInst *probe;
  382. for (U32 i = 0; i < 4; i++)
  383. {
  384. if (i >= ProbeRenderInst::all.size())
  385. break;
  386. probe = ProbeRenderInst::all[i];
  387. if (!probe)
  388. continue;
  389. if (!probe->mIsEnabled)
  390. continue;
  391. // The light positions and spot directions are
  392. // in SoA order to make optimal use of the GPU.
  393. const Point3F &probePos = probe->getPosition();
  394. probePositions[i].x = probePos.x;
  395. probePositions[i].y = probePos.y;
  396. probePositions[i].z = probePos.z;
  397. probeRadius[i] = probe->mRadius;
  398. const Point3F &minExt = probe->mBounds.minExtents;
  399. probeBoxMins[i].x = minExt.x;
  400. probeBoxMins[i].y = minExt.y;
  401. probeBoxMins[i].z = minExt.z;
  402. const Point3F &maxExt = probe->mBounds.maxExtents;
  403. probeBoxMaxs[i].x = maxExt.x;
  404. probeBoxMaxs[i].y = maxExt.y;
  405. probeBoxMaxs[i].z = maxExt.z;
  406. probeIsSphere[i] = probe->mProbeShapeType == ProbeRenderInst::Sphere ? 1.0 : 0.0;
  407. Point3F localProbePos;
  408. worldToCameraXfm.mulP(probe->getPosition(), &localProbePos);
  409. probeLocalPositions[i].x = localProbePos.x;
  410. probeLocalPositions[i].y = localProbePos.y;
  411. probeLocalPositions[i].z = localProbePos.z;
  412. if (probe->mCubemap && !probe->mCubemap.isNull())
  413. {
  414. S32 samplerReg = probeCubemapSC->getSamplerRegister();
  415. if (samplerReg != -1)
  416. GFX->setCubeTexture(samplerReg + i, probe->mCubemap.getPointer());
  417. }
  418. }*/
  419. for (U32 i = 0; i < 4; i++)
  420. {
  421. probePositions[i].x = 0;
  422. probePositions[i].y = 0;
  423. probePositions[i].z = 0;
  424. probeRadius[i] = 0;
  425. probeBoxMins[i].x = 0;
  426. probeBoxMins[i].y = 0;
  427. probeBoxMins[i].z = 0;
  428. probeBoxMaxs[i].x = 0;
  429. probeBoxMaxs[i].y = 0;
  430. probeBoxMaxs[i].z = 0;
  431. probeIsSphere[i] = 0;
  432. probeLocalPositions[i].x = 0;
  433. probeLocalPositions[i].y = 0;
  434. probeLocalPositions[i].z = 0;
  435. S32 samplerReg = probeCubemapSC->getSamplerRegister();
  436. GFX->setCubeTexture(samplerReg + i, nullptr);
  437. }
  438. shaderConsts->setSafe(probePositionSC, probePositions);
  439. shaderConsts->setSafe(probeRadiusSC, probeRadius);
  440. shaderConsts->setSafe(probeBoxMinSC, probeBoxMins);
  441. shaderConsts->setSafe(probeBoxMaxSC, probeBoxMaxs);
  442. shaderConsts->setSafe(probeLocalPosSC, probeLocalPositions);
  443. shaderConsts->setSafe(probeIsSphereSC, probeIsSphere);
  444. }
  445. else
  446. {
  447. if (probeCubemapSC->isValid())
  448. {
  449. for (U32 i = 0; i < 4; ++i)
  450. GFX->setCubeTexture(probeCubemapSC->getSamplerRegister() + i, NULL);
  451. }
  452. }
  453. }
  454. void RenderProbeMgr::setProbeInfo(ProcessedMaterial *pmat,
  455. const Material *mat,
  456. const SceneData &sgData,
  457. const SceneRenderState *state,
  458. U32 pass,
  459. GFXShaderConstBuffer *shaderConsts)
  460. {
  461. // Skip this if we're rendering from the deferred bin.
  462. if (sgData.binType == SceneData::DeferredBin)
  463. return;
  464. // if (mRegisteredProbes.empty())
  465. // return;
  466. PROFILE_SCOPE(ProbeManager_setProbeInfo);
  467. ProbeShaderConstants *psc = getProbeShaderConstants(shaderConsts);
  468. // NOTE: If you encounter a crash from this point forward
  469. // while setting a shader constant its probably because the
  470. // mConstantLookup has bad shaders/constants in it.
  471. //
  472. // This is a known crash bug that can occur if materials/shaders
  473. // are reloaded and the light manager is not reset.
  474. //
  475. // We should look to fix this by clearing the table.
  476. MatrixSet matSet = state->getRenderPass()->getMatrixSet();
  477. // Update the forward shading light constants.
  478. _update4ProbeConsts(sgData,
  479. matSet,
  480. psc->mProbePositionSC,
  481. psc->mProbeRadiusSC,
  482. psc->mProbeBoxMinSC,
  483. psc->mProbeBoxMaxSC,
  484. psc->mProbeCubemapSC,
  485. psc->mProbeIsSphereSC,
  486. psc->mProbeLocalPosSC,
  487. shaderConsts);
  488. }
  489. //-----------------------------------------------------------------------------
  490. // render objects
  491. //-----------------------------------------------------------------------------
  492. void RenderProbeMgr::render( SceneRenderState *state )
  493. {
  494. //PROFILE_SCOPE(RenderProbeMgr_render);
  495. if (getProbeArrayEffect() == nullptr)
  496. return;
  497. //updateProbes();
  498. // Early out if nothing to draw.
  499. if (!ProbeRenderInst::all.size() || !RenderProbeMgr::smRenderReflectionProbes || mEffectiveProbeCount == 0
  500. || !state->isDiffusePass() || cubeMaps.empty() || irradMaps.empty())
  501. {
  502. getProbeArrayEffect()->setSkip(true);
  503. return;
  504. }
  505. GFXTransformSaver saver;
  506. GFXDEBUGEVENT_SCOPE(RenderProbeMgr_render, ColorI::WHITE);
  507. // Initialize and set the per-frame parameters after getting
  508. // the vector light material as we use lazy creation.
  509. //_setupPerFrameParameters(state);
  510. //Array rendering
  511. //U32 probeCount = ProbeRenderInst::all.size();
  512. if (mEffectiveProbeCount != 0)
  513. {
  514. mProbeArrayEffect->setCubemapArrayTexture(4, mCubemapArray);
  515. mProbeArrayEffect->setCubemapArrayTexture(5, mIrradArray);
  516. String useDebugAtten = Con::getVariable("$Probes::showAttenuation", "0");
  517. mProbeArrayEffect->setShaderMacro("DEBUGVIZ_ATTENUATION", useDebugAtten);
  518. String useDebugSpecCubemap = Con::getVariable("$Probes::showSpecularCubemaps", "0");
  519. mProbeArrayEffect->setShaderMacro("DEBUGVIZ_SPECCUBEMAP", useDebugSpecCubemap);
  520. String useDebugDiffuseCubemap = Con::getVariable("$Probes::showDiffuseCubemaps", "0");
  521. mProbeArrayEffect->setShaderMacro("DEBUGVIZ_DIFFCUBEMAP", useDebugDiffuseCubemap);
  522. String useDebugContrib = Con::getVariable("$Probes::showProbeContrib", "0");
  523. mProbeArrayEffect->setShaderMacro("DEBUGVIZ_CONTRIB", useDebugContrib);
  524. if (useDebugContrib == String("1"))
  525. {
  526. MRandomLCG RandomGen;
  527. RandomGen.setSeed(mEffectiveProbeCount);
  528. //also set up some colors
  529. Vector<Point4F> contribColors;
  530. contribColors.setSize(MAXPROBECOUNT);
  531. for (U32 i = 0; i < mEffectiveProbeCount; i++)
  532. {
  533. contribColors[i] = Point4F(RandomGen.randF(0, 1), RandomGen.randF(0, 1), RandomGen.randF(0, 1),1);
  534. }
  535. mProbeArrayEffect->setShaderConst("$probeContribColors", contribColors);
  536. }
  537. mProbeArrayEffect->setShaderConst("$cubeMips", (float)mMipCount);
  538. mProbeArrayEffect->setShaderConst("$numProbes", (float)mEffectiveProbeCount);
  539. mProbeArrayEffect->setShaderConst("$inProbePosArray", probePositionsData);
  540. mProbeArrayEffect->setShaderConst("$inRefPosArray", probeRefPositionsData);
  541. mProbeArrayEffect->setShaderConst("$worldToObjArray", probeWorldToObjData);
  542. mProbeArrayEffect->setShaderConst("$bbMinArray", probeBBMinData);
  543. mProbeArrayEffect->setShaderConst("$bbMaxArray", probeBBMaxData);
  544. mProbeArrayEffect->setShaderConst("$probeConfigData", probeConfigData);
  545. }
  546. // Make sure the effect is gonna render.
  547. getProbeArrayEffect()->setSkip(false);
  548. //PROFILE_END();
  549. }
  550. void RenderProbeMgr::bakeProbe(ReflectionProbe *probe)
  551. {
  552. GFXDEBUGEVENT_SCOPE(RenderProbeMgr_Bake, ColorI::WHITE);
  553. Con::warnf("RenderProbeMgr::bakeProbe() - Beginning bake!");
  554. U32 startMSTime = Platform::getRealMilliseconds();
  555. String path = Con::getVariable("$pref::ReflectionProbes::CurrentLevelPath", "levels/");
  556. U32 resolution = Con::getIntVariable("$pref::ReflectionProbes::BakeResolution", 64);
  557. U32 prefilterMipLevels = mLog2(F32(resolution));
  558. bool renderWithProbes = Con::getIntVariable("$pref::ReflectionProbes::RenderWithProbes", false);
  559. ReflectionProbe *clientProbe = static_cast<ReflectionProbe*>(probe->getClientObject());
  560. String probePrefilterPath = clientProbe->getPrefilterMapPath();
  561. String probeIrradPath = clientProbe->getIrradianceMapPath();
  562. if (clientProbe->mReflectionModeType != ReflectionProbe::DynamicCubemap)
  563. {
  564. //Prep our bake path
  565. if (probePrefilterPath.isEmpty() || probeIrradPath.isEmpty())
  566. {
  567. Con::errorf("RenderProbeMgr::bake() - Unable to bake our captures because probe doesn't have a path set");
  568. return;
  569. }
  570. }
  571. // Save the current transforms so we can restore
  572. // it for child control rendering below.
  573. GFXTransformSaver saver;
  574. bool probeRenderState = RenderProbeMgr::smRenderReflectionProbes;
  575. F32 farPlane = 1000.0f;
  576. ReflectorDesc reflDesc;
  577. reflDesc.texSize = resolution;
  578. reflDesc.farDist = farPlane;
  579. reflDesc.detailAdjust = 1;
  580. reflDesc.objectTypeMask = -1;
  581. CubeReflector cubeRefl;
  582. cubeRefl.registerReflector(probe, &reflDesc);
  583. ReflectParams reflParams;
  584. //need to get the query somehow. Likely do some sort of get function to fetch from the guiTSControl that's active
  585. CameraQuery query; //need to get the last cameraQuery
  586. query.fov = 90; //90 degree slices for each of the 6 sides
  587. query.nearPlane = 0.1f;
  588. query.farPlane = farPlane;
  589. query.headMatrix = MatrixF();
  590. query.cameraMatrix = clientProbe->getTransform();
  591. Frustum culler;
  592. culler.set(false,
  593. query.fov,
  594. (F32)resolution / (F32)resolution,
  595. query.nearPlane,
  596. query.farPlane,
  597. query.cameraMatrix);
  598. S32 stereoTarget = GFX->getCurrentStereoTarget();
  599. Point2I maxRes(2048, 2048); //basically a boundary so we don't go over this and break stuff
  600. reflParams.culler = culler;
  601. reflParams.eyeId = stereoTarget;
  602. reflParams.query = &query;
  603. reflParams.startOfUpdateMs = startMSTime;
  604. reflParams.viewportExtent = maxRes;
  605. if (!renderWithProbes)
  606. RenderProbeMgr::smRenderReflectionProbes = false;
  607. cubeRefl.updateReflection(reflParams);
  608. //Now, save out the maps
  609. //create irridiance cubemap
  610. if (cubeRefl.getCubemap())
  611. {
  612. //Just to ensure we're prepped for the generation
  613. clientProbe->createClientResources();
  614. //Prep it with whatever resolution we've dictated for our bake
  615. if (clientProbe->mUseHDRCaptures)
  616. {
  617. clientProbe->mIrridianceMap->mCubemap->initDynamic(resolution, GFXFormatR16G16B16A16F);
  618. clientProbe->mPrefilterMap->mCubemap->initDynamic(resolution, GFXFormatR16G16B16A16F);
  619. }
  620. else
  621. {
  622. clientProbe->mIrridianceMap->mCubemap->initDynamic(resolution, GFXFormatR8G8B8A8);
  623. clientProbe->mPrefilterMap->mCubemap->initDynamic(resolution, GFXFormatR8G8B8A8);
  624. }
  625. GFXTextureTargetRef renderTarget = GFX->allocRenderToTextureTarget(false);
  626. IBLUtilities::GenerateIrradianceMap(renderTarget, cubeRefl.getCubemap(), clientProbe->mIrridianceMap->mCubemap);
  627. IBLUtilities::GeneratePrefilterMap(renderTarget, cubeRefl.getCubemap(), prefilterMipLevels, clientProbe->mPrefilterMap->mCubemap);
  628. IBLUtilities::SaveCubeMap(clientProbe->getIrradianceMapPath(), clientProbe->mIrridianceMap->mCubemap);
  629. IBLUtilities::SaveCubeMap(clientProbe->getPrefilterMapPath(), clientProbe->mPrefilterMap->mCubemap);
  630. }
  631. else
  632. {
  633. Con::errorf("RenderProbeMgr::bake() - Didn't generate a valid scene capture cubemap, unable to generate prefilter and irradiance maps!");
  634. }
  635. if (!renderWithProbes)
  636. RenderProbeMgr::smRenderReflectionProbes = probeRenderState;
  637. cubeRefl.unregisterReflector();
  638. U32 endMSTime = Platform::getRealMilliseconds();
  639. F32 diffTime = F32(endMSTime - startMSTime);
  640. Con::warnf("RenderProbeMgr::bake() - Finished bake! Took %g milliseconds", diffTime);
  641. }
  642. void RenderProbeMgr::bakeProbes()
  643. {
  644. //TODO: make this just find every probe in the current missionGroup and run the bake on it automagically
  645. }
  646. DefineEngineMethod(RenderProbeMgr, bakeProbe, void, (ReflectionProbe* probe), (nullAsType< ReflectionProbe*>()),
  647. "@brief returns true if control object is inside the fog\n\n.")
  648. {
  649. if(probe != nullptr)
  650. object->bakeProbe(probe);
  651. }