renderProbeMgr.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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
  54. {
  55. bool mIsEnabled;
  56. MatrixF mTransform;
  57. F32 mRadius;
  58. bool mDirty;
  59. Box3F mBounds;
  60. Point3F mExtents;
  61. Point3F mPosition;
  62. Point3F mProbeRefOffset;
  63. Point3F mProbeRefScale;
  64. F32 mAtten;
  65. GFXCubemapHandle mPrefilterCubemap;
  66. GFXCubemapHandle mIrradianceCubemap;
  67. /// The priority of this light used for
  68. /// light and shadow scoring.
  69. F32 mPriority;
  70. /// A temporary which holds the score used
  71. /// when prioritizing lights for rendering.
  72. F32 mScore;
  73. bool mIsSkylight;
  74. enum ProbeShapeType
  75. {
  76. Box = 0, ///< Sphere shaped
  77. Sphere = 1, ///< Box-based shape
  78. Skylight = 2
  79. };
  80. ProbeShapeType mProbeShapeType;
  81. U32 mCubemapIndex;
  82. U32 mProbeIdx;
  83. public:
  84. ProbeRenderInst();
  85. ~ProbeRenderInst();
  86. // Copies data passed in from light
  87. void set(const ProbeRenderInst *probeInfo);
  88. // Accessors
  89. const MatrixF& getTransform() const { return mTransform; }
  90. void setTransform(const MatrixF &xfm) { mTransform = xfm; }
  91. Point3F getPosition() const { return mPosition; }
  92. void setPosition(const Point3F &pos) { mPosition = pos; }
  93. VectorF getDirection() const { return mTransform.getForwardVector(); }
  94. void setDirection(const VectorF &val);
  95. void setPriority(F32 priority) { mPriority = priority; }
  96. F32 getPriority() const { return mPriority; }
  97. void setScore(F32 score) { mScore = score; }
  98. F32 getScore() const { return mScore; }
  99. void clear();
  100. inline bool ProbeRenderInst::operator ==(const ProbeRenderInst& b) const
  101. {
  102. return mProbeIdx == b.mProbeIdx;
  103. }
  104. };
  105. struct ProbeShaderConstants
  106. {
  107. bool mInit;
  108. GFXShaderRef mShader;
  109. //Reflection Probes
  110. GFXShaderConstHandle *mProbePositionSC;
  111. GFXShaderConstHandle *mProbeRefPosSC;
  112. GFXShaderConstHandle *mProbeBoxMinSC;
  113. GFXShaderConstHandle *mProbeBoxMaxSC;
  114. GFXShaderConstHandle *mWorldToObjArraySC;
  115. GFXShaderConstHandle *mProbeConfigDataSC;
  116. GFXShaderConstHandle *mProbeSpecularCubemapSC;
  117. GFXShaderConstHandle *mProbeIrradianceCubemapSC;
  118. GFXShaderConstHandle *mProbeCountSC;
  119. GFXShaderConstHandle *mBRDFTextureMap;
  120. GFXShaderConstHandle *mSkylightSpecularMap;
  121. GFXShaderConstHandle *mSkylightIrradMap;
  122. GFXShaderConstHandle *mHasSkylight;
  123. ProbeShaderConstants();
  124. ~ProbeShaderConstants();
  125. void init(GFXShader* buffer);
  126. bool isValid();
  127. void _onShaderReload();
  128. };
  129. typedef Map<GFXShader*, ProbeShaderConstants*> ProbeConstantMap;
  130. //**************************************************************************
  131. // RenderObjectMgr
  132. //**************************************************************************
  133. class RenderProbeMgr : public RenderBinManager
  134. {
  135. typedef RenderBinManager Parent;
  136. Vector<ProbeRenderInst> mRegisteredProbes;
  137. bool mProbesDirty;
  138. //maximum number of allowed probes
  139. static const U32 PROBE_MAX_COUNT = 250;
  140. //maximum number of rendered probes per frame adjust as needed
  141. static const U32 PROBE_MAX_FRAME = 8;
  142. //number of slots to allocate at once in the cubemap array
  143. static const U32 PROBE_ARRAY_SLOT_BUFFER_SIZE = 10;
  144. static const U32 PROBE_IRRAD_SIZE = 64;
  145. static const U32 PROBE_PREFILTER_SIZE = 64;
  146. static const GFXFormat PROBE_FORMAT = GFXFormatR16G16B16A16F;// GFXFormatR8G8B8A8;// when hdr fixed GFXFormatR16G16B16A16F; look into bc6h compression
  147. static const U32 INVALID_CUBE_SLOT = U32_MAX;
  148. //Array rendering
  149. U32 mEffectiveProbeCount;
  150. S32 mMipCount;
  151. Vector<Point4F> probePositionsData;
  152. Vector<Point4F> probeRefPositionsData;
  153. Vector<MatrixF> probeWorldToObjData;
  154. Vector<Point4F> probeBBMinData;
  155. Vector<Point4F> probeBBMaxData;
  156. Vector<Point4F> probeConfigData;
  157. bool mHasSkylight;
  158. S32 mSkylightCubemapIdx;
  159. AlignedArray<Point4F> mProbePositions;
  160. AlignedArray<Point4F> mProbeBBMin;
  161. AlignedArray<Point4F> mProbeBBMax;
  162. AlignedArray<float> mProbeUseSphereMode;
  163. AlignedArray<float> mProbeRadius;
  164. AlignedArray<float> mProbeAttenuation;
  165. //number of cubemaps
  166. U32 mCubeMapCount;
  167. //number of cubemap slots allocated
  168. U32 mCubeSlotCount;
  169. //array of cubemap slots, due to the editor these may be mixed around as probes are added and deleted
  170. bool mCubeMapSlots[PROBE_MAX_COUNT];
  171. GFXCubemapArrayHandle mPrefilterArray;
  172. GFXCubemapArrayHandle mIrradianceArray;
  173. //Utilized in forward rendering
  174. ProbeConstantMap mConstantLookup;
  175. GFXShaderRef mLastShader;
  176. ProbeShaderConstants* mLastConstants;
  177. //
  178. SimObjectPtr<PostEffect> mProbeArrayEffect;
  179. //Default skylight, used for shape editors, etc
  180. ProbeRenderInst* mDefaultSkyLight;
  181. GFXTexHandle mBRDFTexture;
  182. public:
  183. RenderProbeMgr();
  184. RenderProbeMgr(RenderInstType riType, F32 renderOrder, F32 processAddOrder);
  185. virtual ~RenderProbeMgr();
  186. virtual bool onAdd();
  187. virtual void onRemove();
  188. // ConsoleObject
  189. static void initPersistFields();
  190. DECLARE_CONOBJECT(RenderProbeMgr);
  191. protected:
  192. /// The current active light manager.
  193. static RenderProbeMgr *smProbeManager;
  194. /// This helper function sets the shader constansts
  195. /// for the stock 4 light forward lighting code.
  196. void _update4ProbeConsts(const SceneData &sgData,
  197. MatrixSet &matSet,
  198. ProbeShaderConstants *probeShaderConsts,
  199. GFXShaderConstBuffer *shaderConsts);
  200. void _setupStaticParameters();
  201. void _setupPerFrameParameters(const SceneRenderState *state);
  202. virtual void addElement(RenderInst *inst);
  203. virtual void render(SceneRenderState * state);
  204. ProbeShaderConstants* getProbeShaderConstants(GFXShaderConstBuffer* buffer);
  205. PostEffect* getProbeArrayEffect();
  206. U32 _findNextEmptyCubeSlot()
  207. {
  208. for (U32 i = 0; i < PROBE_MAX_COUNT; i++)
  209. {
  210. if (!mCubeMapSlots[i])
  211. return i;
  212. }
  213. return INVALID_CUBE_SLOT;
  214. }
  215. public:
  216. // RenderBinMgr
  217. void updateProbes();
  218. /// Returns the active LM.
  219. static inline RenderProbeMgr* getProbeManager();
  220. ProbeRenderInst* registerProbe();
  221. void unregisterProbe(U32 probeIdx);
  222. virtual void setProbeInfo(ProcessedMaterial *pmat,
  223. const Material *mat,
  224. const SceneData &sgData,
  225. const SceneRenderState *state,
  226. U32 pass,
  227. GFXShaderConstBuffer *shaderConsts);
  228. void updateProbeTexture(ProbeRenderInst* probeInfo);
  229. /// Debug rendering
  230. static bool smRenderReflectionProbes;
  231. void bakeProbe(ReflectionProbe *probeInfo);
  232. void bakeProbes();
  233. };
  234. RenderProbeMgr* RenderProbeMgr::getProbeManager()
  235. {
  236. if (smProbeManager == nullptr)
  237. {
  238. RenderProbeMgr* probeManager = new RenderProbeMgr();
  239. smProbeManager = probeManager;
  240. }
  241. return smProbeManager;
  242. }
  243. #define PROBEMGR RenderProbeMgr::getProbeManager()
  244. #endif // RENDER_PROBE_MGR_H