renderProbeMgr.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. IMPLEMENT_CONOBJECT(RenderProbeMgr);
  35. ConsoleDocClass( RenderProbeMgr,
  36. "@brief A render bin which uses object callbacks for rendering.\n\n"
  37. "This render bin gathers object render instances and calls its delegate "
  38. "method to perform rendering. It is used infrequently for specialized "
  39. "scene objects which perform custom rendering.\n\n"
  40. "@ingroup RenderBin\n" );
  41. S32 QSORT_CALLBACK AscendingReflectProbeInfluence(const void* a, const void* b)
  42. {
  43. // Debug Profiling.
  44. PROFILE_SCOPE(AdvancedLightBinManager_AscendingReflectProbeInfluence);
  45. // Fetch asset definitions.
  46. /*const ProbeRenderInst* pReflectProbeA = static_cast<ProbeRenderInst*>(((RenderBinManager::MainSortElem*)(a))->inst);
  47. const ProbeRenderInst* pReflectProbeB = static_cast<ProbeRenderInst*>(((RenderBinManager::MainSortElem*)(b))->inst);
  48. // Sort.
  49. //First, immediate check on if either is a skylight. Skylight always gets the highest priority
  50. //if (pReflectProbeA->mIsSkylight)
  51. // return 1;
  52. //else if (pReflectProbeB->mIsSkylight)
  53. // return -1;
  54. //No? then sort by score
  55. if (pReflectProbeA->mScore > pReflectProbeB->mScore)
  56. return 1;
  57. else if (pReflectProbeA->mScore < pReflectProbeB->mScore)
  58. return -1;*/
  59. return 0;
  60. }
  61. RenderProbeMgr::RenderProbeMgr()
  62. : RenderBinManager(RenderPassManager::RIT_Probes, 1.0f, 1.0f)
  63. {
  64. }
  65. RenderProbeMgr::RenderProbeMgr(RenderInstType riType, F32 renderOrder, F32 processAddOrder)
  66. : RenderBinManager(riType, renderOrder, processAddOrder)
  67. {
  68. }
  69. void RenderProbeMgr::initPersistFields()
  70. {
  71. Parent::initPersistFields();
  72. }
  73. void RenderProbeMgr::addElement(RenderInst *inst)
  74. {
  75. // If this instance is translucent handle it in RenderTranslucentMgr
  76. //if (inst->translucentSort)
  77. return;
  78. //AssertFatal(inst->defaultKey != 0, "RenderMeshMgr::addElement() - Got null sort key... did you forget to set it?");
  79. /*internalAddElement(inst);
  80. ProbeRenderInst* probeInst = static_cast<ProbeRenderInst*>(inst);
  81. if (probeInst->mIsSkylight)
  82. {
  83. addSkylightProbe(probeInst);
  84. }
  85. else
  86. {
  87. if (probeInst->mProbeShapeType == ProbeInfo::Sphere)
  88. addSphereReflectionProbe(probeInst);
  89. else
  90. addConvexReflectionProbe(probeInst);
  91. }*/
  92. }
  93. //remove
  94. //Con::setIntVariable("lightMetrics::activeReflectionProbes", mReflectProbeBin.size());
  95. //Con::setIntVariable("lightMetrics::culledReflectProbes", 0/*mNumLightsCulled*/);
  96. //
  97. void RenderProbeMgr::_setupPerFrameParameters(const SceneRenderState *state)
  98. {
  99. PROFILE_SCOPE(RenderProbeMgr_SetupPerFrameParameters);
  100. const Frustum &frustum = state->getCameraFrustum();
  101. MatrixF invCam(frustum.getTransform());
  102. invCam.inverse();
  103. const Point3F *wsFrustumPoints = frustum.getPoints();
  104. const Point3F& cameraPos = frustum.getPosition();
  105. // Perform a camera offset. We need to manually perform this offset on the sun (or vector) light's
  106. // polygon, which is at the far plane.
  107. Point3F cameraOffsetPos = cameraPos;
  108. // Now build the quad for drawing full-screen vector light
  109. // passes.... this is a volatile VB and updates every frame.
  110. FarFrustumQuadVert verts[4];
  111. {
  112. verts[0].point.set(wsFrustumPoints[Frustum::FarTopLeft] - cameraPos);
  113. invCam.mulP(wsFrustumPoints[Frustum::FarTopLeft], &verts[0].normal);
  114. verts[0].texCoord.set(-1.0, 1.0);
  115. verts[0].tangent.set(wsFrustumPoints[Frustum::FarTopLeft] - cameraOffsetPos);
  116. verts[1].point.set(wsFrustumPoints[Frustum::FarTopRight] - cameraPos);
  117. invCam.mulP(wsFrustumPoints[Frustum::FarTopRight], &verts[1].normal);
  118. verts[1].texCoord.set(1.0, 1.0);
  119. verts[1].tangent.set(wsFrustumPoints[Frustum::FarTopRight] - cameraOffsetPos);
  120. verts[2].point.set(wsFrustumPoints[Frustum::FarBottomLeft] - cameraPos);
  121. invCam.mulP(wsFrustumPoints[Frustum::FarBottomLeft], &verts[2].normal);
  122. verts[2].texCoord.set(-1.0, -1.0);
  123. verts[2].tangent.set(wsFrustumPoints[Frustum::FarBottomLeft] - cameraOffsetPos);
  124. verts[3].point.set(wsFrustumPoints[Frustum::FarBottomRight] - cameraPos);
  125. invCam.mulP(wsFrustumPoints[Frustum::FarBottomRight], &verts[3].normal);
  126. verts[3].texCoord.set(1.0, -1.0);
  127. verts[3].tangent.set(wsFrustumPoints[Frustum::FarBottomRight] - cameraOffsetPos);
  128. }
  129. mFarFrustumQuadVerts.set(GFX, 4);
  130. dMemcpy(mFarFrustumQuadVerts.lock(), verts, sizeof(verts));
  131. mFarFrustumQuadVerts.unlock();
  132. PlaneF farPlane(wsFrustumPoints[Frustum::FarBottomLeft], wsFrustumPoints[Frustum::FarTopLeft], wsFrustumPoints[Frustum::FarTopRight]);
  133. PlaneF vsFarPlane(verts[0].normal, verts[1].normal, verts[2].normal);
  134. MatrixSet &matrixSet = getRenderPass()->getMatrixSet();
  135. matrixSet.restoreSceneViewProjection();
  136. const MatrixF &worldToCameraXfm = matrixSet.getCameraToWorld();
  137. MatrixF inverseViewMatrix = worldToCameraXfm;
  138. // Parameters calculated, assign them to the materials
  139. ProbeManager::SkylightMaterialInfo* skylightMat = PROBEMGR->getSkylightMaterial();
  140. if (skylightMat != nullptr && skylightMat->matInstance != nullptr)
  141. {
  142. skylightMat->setViewParameters(frustum.getNearDist(),
  143. frustum.getFarDist(),
  144. frustum.getPosition(),
  145. farPlane,
  146. vsFarPlane);
  147. }
  148. ProbeManager::ReflectProbeMaterialInfo* reflProbeMat = PROBEMGR->getReflectProbeMaterial();
  149. if (reflProbeMat != nullptr && reflProbeMat->matInstance != nullptr)
  150. {
  151. reflProbeMat->setViewParameters(frustum.getNearDist(),
  152. frustum.getFarDist(),
  153. frustum.getPosition(),
  154. farPlane,
  155. vsFarPlane);
  156. }
  157. }
  158. //-----------------------------------------------------------------------------
  159. // render objects
  160. //-----------------------------------------------------------------------------
  161. void RenderProbeMgr::render( SceneRenderState *state )
  162. {
  163. PROFILE_SCOPE(RenderProbeMgr_render);
  164. // Early out if nothing to draw.
  165. if (!ProbeRenderInst::all.size())
  166. return;
  167. if (PROBEMGR->mRegisteredProbes.empty())
  168. return;
  169. if (!ProbeManager::smRenderReflectionProbes)
  170. return;
  171. GFXTransformSaver saver;
  172. GFXDEBUGEVENT_SCOPE(RenderProbeMgr_render, ColorI::WHITE);
  173. NamedTexTargetRef diffuseLightingTarget = NamedTexTarget::find("diffuseLighting");
  174. if (diffuseLightingTarget.isNull())
  175. return;
  176. NamedTexTargetRef specularLightingTarget = NamedTexTarget::find("specularLighting");
  177. if (specularLightingTarget.isNull())
  178. return;
  179. GFXTextureTargetRef probeLightingTargetRef = GFX->allocRenderToTextureTarget();
  180. if (probeLightingTargetRef.isNull())
  181. return;
  182. //Do a quick pass to update our probes if they're dirty
  183. PROBEMGR->updateDirtyProbes();
  184. probeLightingTargetRef->attachTexture(GFXTextureTarget::Color0, specularLightingTarget->getTexture());
  185. probeLightingTargetRef->attachTexture(GFXTextureTarget::Color1, diffuseLightingTarget->getTexture());
  186. GFX->pushActiveRenderTarget();
  187. GFX->setActiveRenderTarget(probeLightingTargetRef);
  188. GFX->setViewport(specularLightingTarget->getViewport());
  189. //GFX->setViewport(specularLightingTarget->getViewport());
  190. // Restore transforms
  191. MatrixSet &matrixSet = getRenderPass()->getMatrixSet();
  192. matrixSet.restoreSceneViewProjection();
  193. const MatrixF &worldToCameraXfm = matrixSet.getWorldToCamera();
  194. // Set up the SG Data
  195. SceneData sgData;
  196. sgData.init(state);
  197. // Initialize and set the per-frame parameters after getting
  198. // the vector light material as we use lazy creation.
  199. _setupPerFrameParameters(state);
  200. //Order the probes by size, biggest to smallest
  201. //dQsort(mElementList.address(), mElementList.size(), sizeof(const MainSortElem), AscendingReflectProbeInfluence);
  202. //Specular
  203. PROFILE_START(RenderProbeManager_ReflectProbeRender);
  204. ProbeManager::SkylightMaterialInfo* skylightMat = PROBEMGR->getSkylightMaterial();
  205. ProbeManager::ReflectProbeMaterialInfo* reflProbeMat = PROBEMGR->getReflectProbeMaterial();
  206. for (U32 i = 0; i < PROBEMGR->mRegisteredProbes.size(); i++)
  207. {
  208. ProbeRenderInst* curEntry = ProbeRenderInst::all[PROBEMGR->mRegisteredProbes[i]];
  209. if (!curEntry->mIsEnabled)
  210. continue;
  211. if (curEntry->numPrims == 0)
  212. continue;
  213. if (curEntry->mIsSkylight && (!skylightMat || !skylightMat->matInstance))
  214. continue;
  215. if (!curEntry->mIsSkylight && (!reflProbeMat || !reflProbeMat->matInstance))
  216. break;
  217. //Setup
  218. MatrixF probeTrans = curEntry->getTransform();
  219. if (!curEntry->mIsSkylight)
  220. {
  221. if (curEntry->mProbeShapeType == ProbeRenderInst::Sphere)
  222. probeTrans.scale(curEntry->mRadius * 1.01f);
  223. }
  224. else
  225. {
  226. probeTrans.scale(10); //force it to be big enough to surround the camera
  227. }
  228. sgData.objTrans = &probeTrans;
  229. if(curEntry->mIsSkylight)
  230. skylightMat->setProbeParameters(curEntry, state, worldToCameraXfm);
  231. else
  232. reflProbeMat->setProbeParameters(curEntry, state, worldToCameraXfm);
  233. // Set geometry
  234. GFX->setVertexBuffer(curEntry->vertBuffer);
  235. GFX->setPrimitiveBuffer(curEntry->primBuffer);
  236. if (curEntry->mIsSkylight)
  237. {
  238. while (skylightMat->matInstance->setupPass(state, sgData))
  239. {
  240. // Set transforms
  241. matrixSet.setWorld(*sgData.objTrans);
  242. skylightMat->matInstance->setTransforms(matrixSet, state);
  243. skylightMat->matInstance->setSceneInfo(state, sgData);
  244. GFX->drawPrimitive(GFXTriangleList, 0, curEntry->numPrims);
  245. }
  246. }
  247. else
  248. {
  249. while (reflProbeMat->matInstance->setupPass(state, sgData))
  250. {
  251. // Set transforms
  252. matrixSet.setWorld(*sgData.objTrans);
  253. reflProbeMat->matInstance->setTransforms(matrixSet, state);
  254. reflProbeMat->matInstance->setSceneInfo(state, sgData);
  255. GFX->drawPrimitive(GFXTriangleList, 0, curEntry->numPrims);
  256. }
  257. }
  258. }
  259. //And clean us up
  260. PROBEMGR->mRegisteredProbes.clear();
  261. probeLightingTargetRef->resolve();
  262. GFX->popActiveRenderTarget();
  263. //PROBEMGR->unregisterAllProbes();
  264. PROFILE_END();
  265. GFX->setVertexBuffer(NULL);
  266. GFX->setPrimitiveBuffer(NULL);
  267. // Fire off a signal to let others know that light-bin rendering is ending now
  268. //getRenderSignal().trigger(state, this);
  269. }