renderProbeMgr.h 16 KB

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