renderProbeMgr.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. #ifndef REFLECTIONPROBE_H
  51. #include "T3D/lighting/reflectionProbe.h"
  52. #endif
  53. class PostEffect;
  54. class ReflectionProbe;
  55. /// <summary>
  56. /// A simple container for a ReflectionProbe's ProbeInfo and index for it's associated
  57. /// cubemaps in the cubemap array pair
  58. /// </summary>
  59. struct ProbeRenderInst
  60. {
  61. ReflectionProbe::ProbeInfo* mProbeInfo;
  62. U32 mCubemapIndex;
  63. public:
  64. ProbeRenderInst();
  65. ~ProbeRenderInst();
  66. // Copies data passed in from light
  67. void set(const ProbeRenderInst *probeInfo);
  68. };
  69. /// <summary>
  70. /// A container for all the shader consts needed for rendering probes in forward mode
  71. /// </summary>
  72. struct ProbeShaderConstants
  73. {
  74. bool mInit;
  75. GFXShaderRef mShader;
  76. //Reflection Probes
  77. GFXShaderConstHandle *mProbePositionArraySC;
  78. GFXShaderConstHandle *mProbeRefPosArraySC;
  79. GFXShaderConstHandle *mRefScaleArraySC;
  80. GFXShaderConstHandle *mWorldToObjArraySC;
  81. GFXShaderConstHandle *mProbeConfigDataArraySC;
  82. GFXShaderConstHandle *mProbeSpecularCubemapArraySC;
  83. GFXShaderConstHandle *mProbeIrradianceCubemapArraySC;
  84. GFXShaderConstHandle *mProbeCountSC;
  85. GFXShaderConstHandle *mBRDFTextureMap;
  86. GFXShaderConstHandle* mWetnessTextureMap;
  87. GFXShaderConstHandle *mSkylightCubemapIdxSC;
  88. GFXShaderConstHandle* mSkylightDampSC;
  89. GFXShaderConstHandle* mMaxProbeDrawDistanceSC;
  90. ProbeShaderConstants();
  91. ~ProbeShaderConstants();
  92. void init(GFXShader* buffer);
  93. bool isValid();
  94. void _onShaderReload();
  95. };
  96. typedef Map<GFXShader*, ProbeShaderConstants*> ProbeConstantMap;
  97. /// <summary>
  98. /// A container for processed and packed probe data. This is made when we get the frame's
  99. /// best probes, and is passed to the shader for actual rendering.
  100. /// </summary>
  101. struct ProbeDataSet
  102. {
  103. Vector<Point4F> probePositionArray;
  104. Vector<Point4F> refScaleArray;
  105. Vector<Point4F> probeRefPositionArray;
  106. Vector<Point4F> probeConfigArray;
  107. Vector<MatrixF> probeWorldToObjArray;
  108. S32 skyLightIdx;
  109. bool skyLightDamp;
  110. U32 effectiveProbeCount;
  111. U32 maxProbeCount;
  112. ProbeDataSet()
  113. {
  114. probePositionArray.setSize(0);
  115. refScaleArray.setSize(0);
  116. probeRefPositionArray.setSize(0);
  117. probeConfigArray.setSize(0);
  118. probeWorldToObjArray.setSize(0);
  119. skyLightIdx = -1;
  120. effectiveProbeCount = 0;
  121. maxProbeCount = 0;
  122. skyLightDamp = true;
  123. }
  124. ProbeDataSet(U32 _maxProbeCount)
  125. {
  126. maxProbeCount = _maxProbeCount;
  127. probePositionArray.setSize(maxProbeCount);
  128. refScaleArray.setSize(maxProbeCount);
  129. probeRefPositionArray.setSize(maxProbeCount);
  130. probeConfigArray.setSize(maxProbeCount);
  131. probeWorldToObjArray.setSize(maxProbeCount);
  132. effectiveProbeCount = 0;
  133. }
  134. };
  135. //**************************************************************************
  136. // RenderObjectMgr
  137. //**************************************************************************
  138. class RenderProbeMgr : public RenderBinManager
  139. {
  140. typedef RenderBinManager Parent;
  141. public:
  142. //maximum number of allowed probes
  143. static const U32 PROBE_MAX_COUNT = 250;
  144. //maximum number of rendered probes per frame adjust as needed
  145. static const U32 PROBE_MAX_FRAME = 8;
  146. //number of slots to allocate at once in the cubemap array
  147. static const U32 PROBE_ARRAY_SLOT_BUFFER_SIZE = 10;
  148. //These dictate the default resolution size for the probe arrays
  149. static const GFXFormat PROBE_FORMAT = GFXFormatR16G16B16A16F;// GFXFormatR8G8B8A8;// when hdr fixed GFXFormatR16G16B16A16F; look into bc6h compression
  150. static const U32 INVALID_CUBE_SLOT = U32_MAX;
  151. static F32 smMaxProbeDrawDistance;
  152. static S32 smMaxProbesPerFrame;
  153. static S32 smProbeBakeResolution;
  154. SceneRenderState *mState;
  155. private:
  156. /// <summary>
  157. /// List of registered probes. These are not necessarily rendered in a given frame
  158. /// but the Probe Manager is aware of them and they have cubemap array slots allocated
  159. /// </summary>
  160. Vector<ProbeRenderInst> mRegisteredProbes;
  161. /// <summary>
  162. /// List of active probes. These are ones that are not only registered, but submitted by the probe itself as
  163. /// ready to be rendered. Likely to be rendered in the current frame, settings-dependent.
  164. /// </summary>
  165. Vector<ProbeRenderInst> mActiveProbes;
  166. /// <summary>
  167. /// The PostEffect used to actually rendered the probes into the frame when in deferred mode
  168. /// </summary>
  169. SimObjectPtr<PostEffect> mProbeArrayEffect;
  170. /// <summary>
  171. /// Do we have a active skylight probe
  172. /// </summary>
  173. bool mHasSkylight;
  174. /// <summary>
  175. /// If we have a skylight, what's the array pair index for it?
  176. /// </summary>
  177. S32 mSkylightCubemapIdx;
  178. bool mSkylightDamp;
  179. /// <summary>
  180. /// The 'effective' probe count. This tracks the number of probes that are actually going to be rendered
  181. /// </summary>
  182. U32 mEffectiveProbeCount;
  183. //
  184. //Array rendering
  185. /// <summary>
  186. /// The number of mips the cubemap array has. Mips are used in the PBR calcs for handling roughness
  187. /// </summary>
  188. S32 mMipCount;
  189. /// <summary>
  190. /// The number of cubemaps registered in our array pair
  191. /// </summary>
  192. U32 mCubeMapCount;
  193. /// <summary>
  194. /// The number of allocated slots for the array pair. Rather than adding slots one at a time to the arrays
  195. /// We allocate in chunks so we don't have to resize/rebuild the arrays as often
  196. /// </summary>
  197. U32 mCubeSlotCount;
  198. /// <summary>
  199. /// List indicating if a given allocated slot is actually in use.
  200. /// Due to the editor these may be mixed around as probes are added and deleted
  201. /// </summary>
  202. /// <returns></returns>
  203. bool mCubeMapSlots[PROBE_MAX_COUNT];
  204. /// <summary>
  205. /// The prefilter cubemap array
  206. /// </summary>
  207. GFXCubemapArrayHandle mPrefilterArray;
  208. /// <summary>
  209. /// The irradiance cubemap array
  210. /// </summary>
  211. GFXCubemapArrayHandle mIrradianceArray;
  212. //Utilized in forward rendering
  213. /// <summary>
  214. /// This is used to look up already-made ProbeShaderConsts for a given shader
  215. /// This allows us to avoid having to rebuild the consts each frame if it's a shader
  216. /// we've already handled before.
  217. /// </summary>
  218. ProbeConstantMap mConstantLookup;
  219. /// <summary>
  220. /// The last shader we rendered(in forward mode). With this, we can shortcut the constant
  221. /// lookup if the shader being processed and the last one are the same.
  222. /// </summary>
  223. GFXShaderRef mLastShader;
  224. /// <summary>
  225. /// THe previous shader constants. When used in conjunction with the mLastShader, we can skip
  226. /// having to do a lookup to find an existing ProbeShaderConstants, saving overhead on batched
  227. /// rendering
  228. /// </summary>
  229. ProbeShaderConstants* mLastConstants;
  230. /// <summary>
  231. /// The BRDF texture used in PBR math calculations
  232. /// </summary>
  233. GFXTexHandle mBRDFTexture;
  234. GFXTexHandle mWetnessTexture;
  235. /// <summary>
  236. /// Processed best probe selection list of the current frame when rendering in deferred mode.
  237. /// </summary>
  238. ProbeDataSet mProbeData;
  239. /// <summary>
  240. /// Allows us the full HDR range on the in-memory cubemap captures
  241. /// </summary>
  242. bool mUseHDRCaptures;
  243. /// <summary>
  244. /// holds the normal render state for light fade so we can capture them before and restore them after baking
  245. /// </summary>
  246. S32 mRenderMaximumNumOfLights;
  247. bool mRenderUseLightFade;
  248. protected:
  249. /// The current active light manager.
  250. static RenderProbeMgr* smProbeManager;
  251. //=============================================================================
  252. // Internal Management/Utility Functions
  253. //=============================================================================
  254. /// <summary>
  255. /// Simple utility function that finds the next free cubemap slot for the cubemap array
  256. /// </summary>
  257. /// <returns>U32 index of next available slot</returns>
  258. U32 _findNextEmptyCubeSlot()
  259. {
  260. for (U32 i = 0; i < PROBE_MAX_COUNT; i++)
  261. {
  262. if (!mCubeMapSlots[i])
  263. return i;
  264. }
  265. return INVALID_CUBE_SLOT;
  266. }
  267. /// <summary>
  268. /// Utility function to quickly find a ProbeRenderInst in association to a
  269. /// ReflectionProbe's ProbeInfo
  270. /// </summary>
  271. /// <param name="probeInfo"></param>
  272. /// <returns>Associated ProbeRederInst to param's probeInfo. Null if no matches found</returns>
  273. ProbeRenderInst* findProbeInst(ReflectionProbe::ProbeInfo* probeInfo)
  274. {
  275. for (U32 i = 0; i < mRegisteredProbes.size(); i++)
  276. {
  277. auto asd = mRegisteredProbes[i];
  278. if (mRegisteredProbes[i].mProbeInfo == probeInfo)
  279. {
  280. return &mRegisteredProbes[i];
  281. }
  282. }
  283. return nullptr;
  284. }
  285. public:
  286. RenderProbeMgr();
  287. RenderProbeMgr(RenderInstType riType, F32 renderOrder, F32 processAddOrder);
  288. virtual ~RenderProbeMgr();
  289. virtual bool onAdd();
  290. virtual void onRemove();
  291. // ConsoleObject
  292. static void initPersistFields();
  293. static void consoleInit();
  294. virtual void addElement(RenderInst* inst) {};
  295. DECLARE_CONOBJECT(RenderProbeMgr);
  296. /// <summary>
  297. /// Static flag used to indicate if probes should be rendered at all. Used for debugging
  298. /// </summary>
  299. static bool smRenderReflectionProbes;
  300. //=============================================================================
  301. // Utility functions for processing and setting up the probes for rendering
  302. //=============================================================================
  303. /// <summary>
  304. /// Sorts probes based on their score values. These scores are calculated by the probes themselves based on size, distance from camera, etc
  305. /// </summary>
  306. static S32 QSORT_CALLBACK _probeScoreCmp(const ProbeRenderInst* a, const ProbeRenderInst* b);
  307. /// <summary>
  308. /// Builds a dataset of the best probes to be rendered this frame.
  309. /// </summary>
  310. /// <param name="objPosition"></param>
  311. /// <param name="probeDataSet"></param>
  312. void getBestProbes(const Point3F& objPosition, ProbeDataSet* probeDataSet);
  313. /// <summary>
  314. /// This function adds a ReflectionProbe to the registered list and also allocates
  315. /// a slot in the cubemap array pair for its use
  316. /// </summary>
  317. /// <param name="probeInfo">The probe info to be registered to the bin</param>
  318. void registerProbe(ReflectionProbe::ProbeInfo* probeInfo);
  319. /// <summary>
  320. /// This function removes the ReflectionProbe from the registered list, and marks it's cubemap
  321. /// array slots as unused, allowing them to be freed.
  322. /// </summary>
  323. /// <param name="probeInfo">The probe info to be un-registered to the bin</param>
  324. void unregisterProbe(ReflectionProbe::ProbeInfo* probeInfo);
  325. /// <summary>
  326. /// This function is for registering a ReflectionProbe's probe info
  327. /// as being rendered in the current frame. This is distinct from
  328. /// registered probes in that registered probes are any 'real' probe
  329. /// in the scene, but they may not necessarily render
  330. /// Active(submmitted) probes are intended to actual be rendered this frame
  331. /// </summary>
  332. /// <param name="probe">The ProbeInfo being submitted to be rendered</param>
  333. void submitProbe(ReflectionProbe::ProbeInfo* probe);
  334. /// <summary>
  335. /// Gets the PostEffect used by the bin for rendering the probe array in deferred
  336. /// </summary>
  337. /// <returns>the PostEffect object</returns>
  338. PostEffect* getProbeArrayEffect();
  339. U32 getProbeTexSize();
  340. /// <summary>
  341. /// Finds the associated cubemap array slot for the incoming ProbeInfo and updates the array's texture(s) from it
  342. /// </summary>
  343. /// <param name="probeInfo"></param>
  344. void updateProbeTexture(ReflectionProbe::ProbeInfo* probeInfo);
  345. /// <summary>
  346. /// Forces an update for all registered probes' cubemaps
  347. /// </summary>
  348. void reloadTextures();
  349. /// <summary>
  350. /// Takes a reflection probe and runs the cubemap bake process on it, outputting the resulting files to disk
  351. /// </summary>
  352. void bakeProbe(ReflectionProbe* probe);
  353. void preBake();
  354. void postBake();
  355. /// <summary>
  356. /// Runs the cubemap bake on all probes in the current scene
  357. /// </summary>
  358. void bakeProbes();
  359. /// <summary>
  360. /// Returns the active Probe Manager.
  361. /// </summary>
  362. static inline RenderProbeMgr* getProbeManager();
  363. //=============================================================================
  364. // Forward Rendering functions
  365. //=============================================================================
  366. /// <summary>
  367. /// This function returns or builds a ProbeShaderConsts containing needed data for
  368. /// rendering probes in forward mode
  369. /// </summary>
  370. /// <param name="buffer">The GFXShaderConstBuffer used to build or fetch the Probe Consts</param>
  371. ProbeShaderConstants* getProbeShaderConstants(GFXShaderConstBuffer* buffer);
  372. /// <summary>
  373. /// Sets up the probe data required for doing a render in forward mode.
  374. /// </summary>
  375. virtual void setProbeInfo(ProcessedMaterial* pmat,
  376. const Material* mat,
  377. const SceneData& sgData,
  378. const SceneRenderState* state,
  379. U32 pass,
  380. GFXShaderConstBuffer* shaderConsts);
  381. /// <summary>
  382. /// Invoked as part of the setup in preperation to render an object in forward mode. Used to ensure the probes are
  383. /// sorted ahead of render.
  384. /// </summary>
  385. /// <param name="state"></param>
  386. void setupSGData(SceneData& data, const SceneRenderState* state, LightInfo* light);
  387. /// <summary>
  388. /// Sets up and binds all the shader const data required for rendering probes/IBL for a forward-rendered material.
  389. /// </summary>
  390. /// <returns></returns>
  391. void _update4ProbeConsts(const SceneData& sgData,
  392. MatrixSet& matSet,
  393. ProbeShaderConstants* probeShaderConsts,
  394. GFXShaderConstBuffer* shaderConsts);
  395. //=============================================================================
  396. // Deferred Rendering Functions
  397. //=============================================================================
  398. /// <summary>
  399. /// Ensures the probes are properly sorted before we render them in deferred mode
  400. /// </summary>
  401. void _setupPerFrameParameters(const SceneRenderState *state);
  402. /// <summary>
  403. /// Renders the sorted probes list via a PostEffect to draw them into the buffer data in deferred mode.
  404. /// </summary>
  405. virtual void render(SceneRenderState * state);
  406. virtual void clear() { mActiveProbes.clear(); Parent::clear(); }
  407. };
  408. RenderProbeMgr* RenderProbeMgr::getProbeManager()
  409. {
  410. if (smProbeManager == nullptr)
  411. {
  412. RenderProbeMgr* probeManager = new RenderProbeMgr();
  413. smProbeManager = probeManager;
  414. }
  415. return smProbeManager;
  416. }
  417. #define PROBEMGR RenderProbeMgr::getProbeManager()
  418. #endif // RENDER_PROBE_MGR_H