renderProbeMgr.h 9.9 KB

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