renderProbeMgr.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 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 *mRefBoxMinSC;
  113. GFXShaderConstHandle *mRefBoxMaxSC;
  114. GFXShaderConstHandle *mWorldToObjArraySC;
  115. GFXShaderConstHandle *mProbeConfigDataSC;
  116. GFXShaderConstHandle *mProbeSpecularCubemapSC;
  117. GFXShaderConstHandle *mProbeIrradianceCubemapSC;
  118. GFXShaderConstHandle *mProbeCountSC;
  119. GFXShaderConstHandle *mBRDFTextureMap;
  120. GFXShaderConstHandle *mSkylightCubemapIdxSC;
  121. ProbeShaderConstants();
  122. ~ProbeShaderConstants();
  123. void init(GFXShader* buffer);
  124. bool isValid();
  125. void _onShaderReload();
  126. };
  127. typedef Map<GFXShader*, ProbeShaderConstants*> ProbeConstantMap;
  128. struct ProbeDataSet
  129. {
  130. AlignedArray<Point4F> probePositionArray;
  131. AlignedArray<Point4F> refBoxMinArray;
  132. AlignedArray<Point4F> refBoxMaxArray;
  133. AlignedArray<Point4F> probeRefPositionArray;
  134. AlignedArray<Point4F> probeConfigArray;
  135. Vector<MatrixF> probeWorldToObjArray;
  136. S32 skyLightIdx;
  137. U32 effectiveProbeCount;
  138. U32 MAX_PROBE_COUNT;
  139. ProbeDataSet(U32 maxProbeCount)
  140. {
  141. MAX_PROBE_COUNT = maxProbeCount;
  142. probePositionArray = AlignedArray<Point4F>(maxProbeCount, sizeof(Point4F));
  143. refBoxMinArray = AlignedArray<Point4F>(maxProbeCount, sizeof(Point4F));
  144. refBoxMaxArray = AlignedArray<Point4F>(maxProbeCount, sizeof(Point4F));
  145. probeRefPositionArray = AlignedArray<Point4F>(maxProbeCount, sizeof(Point4F));
  146. probeConfigArray = AlignedArray<Point4F>(maxProbeCount, sizeof(Point4F));
  147. probeWorldToObjArray.setSize(maxProbeCount);
  148. // Need to clear the buffers so that we don't leak
  149. // lights from previous passes or have NaNs.
  150. dMemset(probePositionArray.getBuffer(), 0, probePositionArray.getBufferSize());
  151. dMemset(refBoxMinArray.getBuffer(), 0, refBoxMinArray.getBufferSize());
  152. dMemset(refBoxMaxArray.getBuffer(), 0, refBoxMaxArray.getBufferSize());
  153. dMemset(probeRefPositionArray.getBuffer(), 0, probeRefPositionArray.getBufferSize());
  154. dMemset(probeConfigArray.getBuffer(), 0, probeConfigArray.getBufferSize());
  155. skyLightIdx = -1;
  156. effectiveProbeCount = 0;
  157. }
  158. };
  159. struct ProbeTextureArrayData
  160. {
  161. GFXTexHandle BRDFTexture;
  162. GFXCubemapArrayHandle prefilterArray;
  163. GFXCubemapArrayHandle irradianceArray;
  164. };
  165. //**************************************************************************
  166. // RenderObjectMgr
  167. //**************************************************************************
  168. class RenderProbeMgr : public RenderBinManager
  169. {
  170. typedef RenderBinManager Parent;
  171. Vector<ProbeRenderInst> mRegisteredProbes;
  172. bool mProbesDirty;
  173. public:
  174. //maximum number of allowed probes
  175. static const U32 PROBE_MAX_COUNT = 250;
  176. //maximum number of rendered probes per frame adjust as needed
  177. static const U32 PROBE_MAX_FRAME = 8;
  178. //number of slots to allocate at once in the cubemap array
  179. static const U32 PROBE_ARRAY_SLOT_BUFFER_SIZE = 10;
  180. static const U32 PROBE_IRRAD_SIZE = 64;
  181. static const U32 PROBE_PREFILTER_SIZE = 64;
  182. static const GFXFormat PROBE_FORMAT = GFXFormatR16G16B16A16F;// GFXFormatR8G8B8A8;// when hdr fixed GFXFormatR16G16B16A16F; look into bc6h compression
  183. static const U32 INVALID_CUBE_SLOT = U32_MAX;
  184. private:
  185. //Array rendering
  186. U32 mEffectiveProbeCount;
  187. S32 mMipCount;
  188. Vector<Point4F> probePositionsData;
  189. Vector<Point4F> probeRefPositionsData;
  190. Vector<MatrixF> probeWorldToObjData;
  191. Vector<Point4F> refBoxMinData;
  192. Vector<Point4F> refBoxMaxData;
  193. Vector<Point4F> probeConfigData;
  194. bool mHasSkylight;
  195. S32 mSkylightCubemapIdx;
  196. AlignedArray<Point4F> mProbePositions;
  197. AlignedArray<Point4F> mRefBoxMin;
  198. AlignedArray<Point4F> mRefBoxMax;
  199. AlignedArray<float> mProbeUseSphereMode;
  200. AlignedArray<float> mProbeRadius;
  201. AlignedArray<float> mProbeAttenuation;
  202. //number of cubemaps
  203. U32 mCubeMapCount;
  204. //number of cubemap slots allocated
  205. U32 mCubeSlotCount;
  206. //array of cubemap slots, due to the editor these may be mixed around as probes are added and deleted
  207. bool mCubeMapSlots[PROBE_MAX_COUNT];
  208. GFXCubemapArrayHandle mPrefilterArray;
  209. GFXCubemapArrayHandle mIrradianceArray;
  210. //Utilized in forward rendering
  211. ProbeConstantMap mConstantLookup;
  212. GFXShaderRef mLastShader;
  213. ProbeShaderConstants* mLastConstants;
  214. //
  215. SimObjectPtr<PostEffect> mProbeArrayEffect;
  216. //Default skylight, used for shape editors, etc
  217. ProbeRenderInst* mDefaultSkyLight;
  218. GFXTexHandle mBRDFTexture;
  219. public:
  220. RenderProbeMgr();
  221. RenderProbeMgr(RenderInstType riType, F32 renderOrder, F32 processAddOrder);
  222. virtual ~RenderProbeMgr();
  223. virtual bool onAdd();
  224. virtual void onRemove();
  225. // ConsoleObject
  226. static void initPersistFields();
  227. DECLARE_CONOBJECT(RenderProbeMgr);
  228. protected:
  229. /// The current active light manager.
  230. static RenderProbeMgr *smProbeManager;
  231. /// This helper function sets the shader constansts
  232. /// for the stock 4 light forward lighting code.
  233. void _update4ProbeConsts(const SceneData &sgData,
  234. MatrixSet &matSet,
  235. ProbeShaderConstants *probeShaderConsts,
  236. GFXShaderConstBuffer *shaderConsts);
  237. void _setupStaticParameters();
  238. void _setupPerFrameParameters(const SceneRenderState *state);
  239. virtual void addElement(RenderInst *inst);
  240. virtual void render(SceneRenderState * state);
  241. ProbeShaderConstants* getProbeShaderConstants(GFXShaderConstBuffer* buffer);
  242. PostEffect* getProbeArrayEffect();
  243. U32 _findNextEmptyCubeSlot()
  244. {
  245. for (U32 i = 0; i < PROBE_MAX_COUNT; i++)
  246. {
  247. if (!mCubeMapSlots[i])
  248. return i;
  249. }
  250. return INVALID_CUBE_SLOT;
  251. }
  252. public:
  253. // RenderBinMgr
  254. void updateProbes();
  255. /// Returns the active LM.
  256. static inline RenderProbeMgr* getProbeManager();
  257. ProbeRenderInst* registerProbe();
  258. void unregisterProbe(U32 probeIdx);
  259. virtual void setProbeInfo(ProcessedMaterial *pmat,
  260. const Material *mat,
  261. const SceneData &sgData,
  262. const SceneRenderState *state,
  263. U32 pass,
  264. GFXShaderConstBuffer *shaderConsts);
  265. void updateProbeTexture(ProbeRenderInst* probeInfo);
  266. void reloadTextures();
  267. /// Debug rendering
  268. static bool smRenderReflectionProbes;
  269. void bakeProbe(ReflectionProbe *probeInfo, bool writeFile = true);
  270. void bakeProbes();
  271. void getProbeTextureData(ProbeTextureArrayData* probeTextureSet);
  272. S32 getSkylightIndex() { return mSkylightCubemapIdx; }
  273. //accumulates the best fit of probes given the object position
  274. void getBestProbes(const Point3F& objPosition, ProbeDataSet* probeDataSet);
  275. };
  276. RenderProbeMgr* RenderProbeMgr::getProbeManager()
  277. {
  278. if (smProbeManager == nullptr)
  279. {
  280. RenderProbeMgr* probeManager = new RenderProbeMgr();
  281. smProbeManager = probeManager;
  282. }
  283. return smProbeManager;
  284. }
  285. #define PROBEMGR RenderProbeMgr::getProbeManager()
  286. #endif // RENDER_PROBE_MGR_H