renderProbeMgr.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. #pragma once
  23. #ifndef RENDER_PROBE_MGR_H
  24. #define RENDER_PROBE_MGR_H
  25. #ifndef _RENDERBINMANAGER_H_
  26. #include "renderInstance/renderBinManager.h"
  27. #endif
  28. #ifndef _MATINSTANCE_H_
  29. #include "materials/matInstance.h"
  30. #endif
  31. #ifndef _MATTEXTURETARGET_H_
  32. #include "materials/matTextureTarget.h"
  33. #endif
  34. #ifndef _GFXPRIMITIVEBUFFER_H_
  35. #include "gfx/gfxPrimitiveBuffer.h"
  36. #endif
  37. #ifndef _GFXVERTEXBUFFER_H_
  38. #include "gfx/gfxVertexBuffer.h"
  39. #endif
  40. #include "core/util/SystemInterfaceList.h"
  41. #ifndef _MATERIALS_PROCESSEDSHADERMATERIAL_H_
  42. #include "materials/processedShaderMaterial.h"
  43. #endif
  44. #ifndef _POSTEFFECTCOMMON_H_
  45. #include "postFx/postEffectCommon.h"
  46. #endif
  47. #ifndef _REFLECTOR_H_
  48. #include "scene/reflector.h"
  49. #endif
  50. static U32 MAXPROBECOUNT = 50;
  51. class PostEffect;
  52. class ReflectionProbe;
  53. struct ProbeRenderInst : public SystemInterface<ProbeRenderInst>
  54. {
  55. MatrixF mTransform;
  56. F32 mRadius;
  57. bool mDirty;
  58. Box3F mBounds;
  59. Point3F mPosition;
  60. Point3F mProbePosOffset;
  61. GFXCubemapHandle mCubemap;
  62. GFXCubemapHandle mIrradianceCubemap;
  63. //Utilized in dynamic reflections
  64. CubeReflector mCubeReflector;
  65. /// The priority of this light used for
  66. /// light and shadow scoring.
  67. F32 mPriority;
  68. /// A temporary which holds the score used
  69. /// when prioritizing lights for rendering.
  70. F32 mScore;
  71. bool mIsSkylight;
  72. enum ProbeShapeType
  73. {
  74. Sphere = 0, ///< Sphere shaped
  75. Box = 1, ///< Box-based shape
  76. };
  77. ProbeShapeType mProbeShapeType;
  78. public:
  79. ProbeRenderInst();
  80. ~ProbeRenderInst();
  81. // Copies data passed in from light
  82. void set(const ProbeRenderInst *probeInfo);
  83. // Accessors
  84. const MatrixF& getTransform() const { return mTransform; }
  85. void setTransform(const MatrixF &xfm) { mTransform = xfm; }
  86. Point3F getPosition() const { return mPosition; }
  87. void setPosition(const Point3F &pos) { mPosition = pos; }
  88. VectorF getDirection() const { return mTransform.getForwardVector(); }
  89. void setDirection(const VectorF &val);
  90. void setPriority(F32 priority) { mPriority = priority; }
  91. F32 getPriority() const { return mPriority; }
  92. void setScore(F32 score) { mScore = score; }
  93. F32 getScore() const { return mScore; }
  94. void clear();
  95. };
  96. struct ProbeShaderConstants
  97. {
  98. bool mInit;
  99. GFXShaderRef mShader;
  100. GFXShaderConstHandle* mProbeParamsSC;
  101. //Reflection Probes
  102. GFXShaderConstHandle *mProbePositionSC;
  103. GFXShaderConstHandle *mProbeRadiusSC;
  104. GFXShaderConstHandle *mProbeBoxMinSC;
  105. GFXShaderConstHandle *mProbeBoxMaxSC;
  106. GFXShaderConstHandle *mProbeIsSphereSC;
  107. GFXShaderConstHandle *mProbeLocalPosSC;
  108. GFXShaderConstHandle *mProbeCubemapSC;
  109. GFXShaderConstHandle *mProbeCountSC;
  110. ProbeShaderConstants();
  111. ~ProbeShaderConstants();
  112. void init(GFXShader* buffer);
  113. void _onShaderReload();
  114. };
  115. typedef Map<GFXShader*, ProbeShaderConstants*> ProbeConstantMap;
  116. //**************************************************************************
  117. // RenderObjectMgr
  118. //**************************************************************************
  119. class RenderProbeMgr : public RenderBinManager
  120. {
  121. typedef RenderBinManager Parent;
  122. Vector<U32> mRegisteredProbes;
  123. //Array rendering
  124. U32 mEffectiveProbeCount;
  125. S32 mMipCount;
  126. Vector<Point4F> probePositionsData;
  127. Vector<MatrixF> probeWorldToObjData;
  128. Vector<Point4F> probeBBMinData;
  129. Vector<Point4F> probeBBMaxData;
  130. Vector<Point4F> probeConfigData;
  131. Vector<GFXCubemapHandle> cubeMaps;
  132. Vector<GFXCubemapHandle> irradMaps;
  133. AlignedArray<Point4F> mProbePositions;
  134. AlignedArray<Point4F> mProbeBBMin;
  135. AlignedArray<Point4F> mProbeBBMax;
  136. AlignedArray<float> mProbeUseSphereMode;
  137. AlignedArray<float> mProbeRadius;
  138. AlignedArray<float> mProbeAttenuation;
  139. GFXCubemapArrayHandle mCubemapArray;
  140. GFXCubemapArrayHandle mIrradArray;
  141. //Utilized in forward rendering
  142. ProbeConstantMap mConstantLookup;
  143. GFXShaderRef mLastShader;
  144. ProbeShaderConstants* mLastConstants;
  145. //
  146. SimObjectPtr<PostEffect> mProbeArrayEffect;
  147. public:
  148. RenderProbeMgr();
  149. RenderProbeMgr(RenderInstType riType, F32 renderOrder, F32 processAddOrder);
  150. virtual void onRemove();
  151. // ConsoleObject
  152. static void initPersistFields();
  153. DECLARE_CONOBJECT(RenderProbeMgr);
  154. protected:
  155. /// The current active light manager.
  156. static RenderProbeMgr *smProbeManager;
  157. /// This helper function sets the shader constansts
  158. /// for the stock 4 light forward lighting code.
  159. void _update4ProbeConsts(const SceneData &sgData,
  160. MatrixSet &matSet,
  161. GFXShaderConstHandle *probePositionSC,
  162. GFXShaderConstHandle *probeRadiusSC,
  163. GFXShaderConstHandle *probeBoxMinSC,
  164. GFXShaderConstHandle *probeBoxMaxSC,
  165. GFXShaderConstHandle *probeCubemapSC,
  166. GFXShaderConstHandle *probeIsSphereSC,
  167. GFXShaderConstHandle *probeLocalPosSC,
  168. GFXShaderConstBuffer *shaderConsts);
  169. void _setupStaticParameters();
  170. void _setupPerFrameParameters(const SceneRenderState *state);
  171. virtual void addElement(RenderInst *inst);
  172. virtual void render(SceneRenderState * state);
  173. ProbeShaderConstants* getProbeShaderConstants(GFXShaderConstBuffer* buffer);
  174. PostEffect* getProbeArrayEffect();
  175. public:
  176. // RenderBinMgr
  177. void updateProbes();
  178. /// Returns the active LM.
  179. static inline RenderProbeMgr* getProbeManager();
  180. void registerProbe(U32 probeIdx);
  181. void unregisterProbe(U32 probeIdx);
  182. virtual void setProbeInfo(ProcessedMaterial *pmat,
  183. const Material *mat,
  184. const SceneData &sgData,
  185. const SceneRenderState *state,
  186. U32 pass,
  187. GFXShaderConstBuffer *shaderConsts);
  188. /// Debug rendering
  189. static bool smRenderReflectionProbes;
  190. void bakeProbe(ReflectionProbe *probeInfo);
  191. void bakeProbes();
  192. };
  193. RenderProbeMgr* RenderProbeMgr::getProbeManager()
  194. {
  195. if (smProbeManager == nullptr)
  196. {
  197. RenderProbeMgr* probeManager = new RenderProbeMgr();
  198. smProbeManager = probeManager;
  199. }
  200. return smProbeManager;
  201. }
  202. #define PROBEMGR RenderProbeMgr::getProbeManager()
  203. #endif // RENDER_PROBE_MGR_H