renderProbeMgr.h 10 KB

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