renderProbeMgr.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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> probeUseSphereModeData;
  131. Vector<Point4F> probeRadiusData;
  132. Vector<Point4F> probeAttenuationData;
  133. Vector<GFXCubemapHandle> cubeMaps;
  134. Vector<GFXCubemapHandle> irradMaps;
  135. AlignedArray<Point4F> mProbePositions;
  136. AlignedArray<Point4F> mProbeBBMin;
  137. AlignedArray<Point4F> mProbeBBMax;
  138. AlignedArray<float> mProbeUseSphereMode;
  139. AlignedArray<float> mProbeRadius;
  140. AlignedArray<float> mProbeAttenuation;
  141. GFXCubemapArrayHandle mCubemapArray;
  142. GFXCubemapArrayHandle mIrradArray;
  143. //Utilized in forward rendering
  144. ProbeConstantMap mConstantLookup;
  145. GFXShaderRef mLastShader;
  146. ProbeShaderConstants* mLastConstants;
  147. //
  148. SimObjectPtr<PostEffect> mProbeArrayEffect;
  149. public:
  150. RenderProbeMgr();
  151. RenderProbeMgr(RenderInstType riType, F32 renderOrder, F32 processAddOrder);
  152. virtual void onRemove();
  153. // ConsoleObject
  154. static void initPersistFields();
  155. DECLARE_CONOBJECT(RenderProbeMgr);
  156. protected:
  157. /// The current active light manager.
  158. static RenderProbeMgr *smProbeManager;
  159. /// This helper function sets the shader constansts
  160. /// for the stock 4 light forward lighting code.
  161. void _update4ProbeConsts(const SceneData &sgData,
  162. MatrixSet &matSet,
  163. GFXShaderConstHandle *probePositionSC,
  164. GFXShaderConstHandle *probeRadiusSC,
  165. GFXShaderConstHandle *probeBoxMinSC,
  166. GFXShaderConstHandle *probeBoxMaxSC,
  167. GFXShaderConstHandle *probeCubemapSC,
  168. GFXShaderConstHandle *probeIsSphereSC,
  169. GFXShaderConstHandle *probeLocalPosSC,
  170. GFXShaderConstBuffer *shaderConsts);
  171. void _setupStaticParameters();
  172. void _setupPerFrameParameters(const SceneRenderState *state);
  173. virtual void addElement(RenderInst *inst);
  174. virtual void render(SceneRenderState * state);
  175. ProbeShaderConstants* getProbeShaderConstants(GFXShaderConstBuffer* buffer);
  176. PostEffect* getProbeArrayEffect();
  177. public:
  178. // RenderBinMgr
  179. void updateProbes();
  180. /// Returns the active LM.
  181. static inline RenderProbeMgr* getProbeManager();
  182. void registerProbe(U32 probeIdx);
  183. void unregisterProbe(U32 probeIdx);
  184. virtual void setProbeInfo(ProcessedMaterial *pmat,
  185. const Material *mat,
  186. const SceneData &sgData,
  187. const SceneRenderState *state,
  188. U32 pass,
  189. GFXShaderConstBuffer *shaderConsts);
  190. /// Debug rendering
  191. static bool smRenderReflectionProbes;
  192. void bakeProbe(ReflectionProbe *probeInfo);
  193. void bakeProbes();
  194. };
  195. RenderProbeMgr* RenderProbeMgr::getProbeManager()
  196. {
  197. if (smProbeManager == nullptr)
  198. {
  199. RenderProbeMgr* probeManager = new RenderProbeMgr();
  200. smProbeManager = probeManager;
  201. }
  202. return smProbeManager;
  203. }
  204. #define PROBEMGR RenderProbeMgr::getProbeManager()
  205. #endif // RENDER_PROBE_MGR_H