renderProbeMgr.cpp 12 KB

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