renderProbeMgr.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 = 340;
  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 = 5;
  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. private:
  155. /// <summary>
  156. /// List of registered probes. These are not necessarily rendered in a given frame
  157. /// but the Probe Manager is aware of them and they have cubemap array slots allocated
  158. /// </summary>
  159. Vector<ProbeRenderInst> mRegisteredProbes;
  160. /// <summary>
  161. /// List of active probes. These are ones that are not only registered, but submitted by the probe itself as
  162. /// ready to be rendered. Likely to be rendered in the current frame, settings-dependent.
  163. /// </summary>
  164. Vector<ProbeRenderInst> mActiveProbes;
  165. /// <summary>
  166. /// The PostEffect used to actually rendered the probes into the frame when in deferred mode
  167. /// </summary>
  168. SimObjectPtr<PostEffect> mProbeArrayEffect;
  169. /// <summary>
  170. /// Do we have a active skylight probe
  171. /// </summary>
  172. bool mHasSkylight;
  173. /// <summary>
  174. /// If we have a skylight, what's the array pair index for it?
  175. /// </summary>
  176. S32 mSkylightCubemapIdx;
  177. bool mSkylightDamp;
  178. /// <summary>
  179. /// The 'effective' probe count. This tracks the number of probes that are actually going to be rendered
  180. /// </summary>
  181. U32 mEffectiveProbeCount;
  182. //
  183. //Array rendering
  184. /// <summary>
  185. /// The number of mips the cubemap array has. Mips are used in the PBR calcs for handling roughness
  186. /// </summary>
  187. S32 mMipCount;
  188. /// <summary>
  189. /// The number of cubemaps registered in our array pair
  190. /// </summary>
  191. U32 mCubeMapCount;
  192. /// <summary>
  193. /// The number of allocated slots for the array pair. Rather than adding slots one at a time to the arrays
  194. /// We allocate in chunks so we don't have to resize/rebuild the arrays as often
  195. /// </summary>
  196. U32 mCubeSlotCount;
  197. /// <summary>
  198. /// List indicating if a given allocated slot is actually in use.
  199. /// Due to the editor these may be mixed around as probes are added and deleted
  200. /// </summary>
  201. /// <returns></returns>
  202. bool mCubeMapSlots[PROBE_MAX_COUNT];
  203. /// <summary>
  204. /// The prefilter cubemap array
  205. /// </summary>
  206. GFXTexHandle mPrefilterArray;
  207. /// <summary>
  208. /// The irradiance cubemap array
  209. /// </summary>
  210. GFXTexHandle mIrradianceArray;
  211. //Utilized in forward rendering
  212. /// <summary>
  213. /// This is used to look up already-made ProbeShaderConsts for a given shader
  214. /// This allows us to avoid having to rebuild the consts each frame if it's a shader
  215. /// we've already handled before.
  216. /// </summary>
  217. ProbeConstantMap mConstantLookup;
  218. /// <summary>
  219. /// The last shader we rendered(in forward mode). With this, we can shortcut the constant
  220. /// lookup if the shader being processed and the last one are the same.
  221. /// </summary>
  222. GFXShaderRef mLastShader;
  223. /// <summary>
  224. /// THe previous shader constants. When used in conjunction with the mLastShader, we can skip
  225. /// having to do a lookup to find an existing ProbeShaderConstants, saving overhead on batched
  226. /// rendering
  227. /// </summary>
  228. ProbeShaderConstants* mLastConstants;
  229. /// <summary>
  230. /// The BRDF texture used in PBR math calculations
  231. /// </summary>
  232. GFXTexHandle mBRDFTexture;
  233. GFXTexHandle mWetnessTexture;
  234. /// <summary>
  235. /// Processed best probe selection list of the current frame when rendering in deferred mode.
  236. /// </summary>
  237. ProbeDataSet mProbeData;
  238. /// <summary>
  239. /// Allows us the full HDR range on the in-memory cubemap captures
  240. /// </summary>
  241. bool mUseHDRCaptures;
  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. bool onAdd() override;
  284. void onRemove() override;
  285. // ConsoleObject
  286. static void initPersistFields();
  287. static void consoleInit();
  288. void addElement(RenderInst* inst) override {};
  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. static bool smBakeReflectionProbes;
  295. //=============================================================================
  296. // Utility functions for processing and setting up the probes for rendering
  297. //=============================================================================
  298. /// <summary>
  299. /// Sorts probes based on their score values. These scores are calculated by the probes themselves based on size, distance from camera, etc
  300. /// </summary>
  301. static S32 QSORT_CALLBACK _probeScoreCmp(const ProbeRenderInst* a, const ProbeRenderInst* b);
  302. /// <summary>
  303. /// Builds a dataset of the best probes to be rendered this frame.
  304. /// </summary>
  305. /// <param name="objPosition"></param>
  306. /// <param name="probeDataSet"></param>
  307. void getBestProbes(const Point3F& objPosition, ProbeDataSet* probeDataSet);
  308. /// <summary>
  309. /// This function adds a ReflectionProbe to the registered list and also allocates
  310. /// a slot in the cubemap array pair for its use
  311. /// </summary>
  312. /// <param name="probeInfo">The probe info to be registered to the bin</param>
  313. void registerProbe(ReflectionProbe::ProbeInfo* probeInfo);
  314. /// <summary>
  315. /// This function removes the ReflectionProbe from the registered list, and marks it's cubemap
  316. /// array slots as unused, allowing them to be freed.
  317. /// </summary>
  318. /// <param name="probeInfo">The probe info to be un-registered to the bin</param>
  319. void unregisterProbe(ReflectionProbe::ProbeInfo* probeInfo);
  320. /// <summary>
  321. /// This function is for registering a ReflectionProbe's probe info
  322. /// as being rendered in the current frame. This is distinct from
  323. /// registered probes in that registered probes are any 'real' probe
  324. /// in the scene, but they may not necessarily render
  325. /// Active(submmitted) probes are intended to actual be rendered this frame
  326. /// </summary>
  327. /// <param name="probe">The ProbeInfo being submitted to be rendered</param>
  328. void submitProbe(ReflectionProbe::ProbeInfo* probe);
  329. /// <summary>
  330. /// Gets the PostEffect used by the bin for rendering the probe array in deferred
  331. /// </summary>
  332. /// <returns>the PostEffect object</returns>
  333. PostEffect* getProbeArrayEffect();
  334. U32 getProbeTexSize();
  335. /// <summary>
  336. /// Finds the associated cubemap array slot for the incoming ProbeInfo and updates the array's texture(s) from it
  337. /// </summary>
  338. /// <param name="probeInfo"></param>
  339. void updateProbeTexture(ReflectionProbe::ProbeInfo* probeInfo);
  340. /// <summary>
  341. /// Forces an update for all registered probes' cubemaps
  342. /// </summary>
  343. void reloadTextures();
  344. /// <summary>
  345. /// Takes a reflection probe and runs the cubemap bake process on it, outputting the resulting files to disk
  346. /// </summary>
  347. void bakeProbe(ReflectionProbe* probe);
  348. void preBake();
  349. void postBake();
  350. /// <summary>
  351. /// Runs the cubemap bake on all probes in the current scene
  352. /// </summary>
  353. void bakeProbes();
  354. /// <summary>
  355. /// Returns the active Probe Manager.
  356. /// </summary>
  357. static inline RenderProbeMgr* getProbeManager();
  358. //=============================================================================
  359. // Forward Rendering functions
  360. //=============================================================================
  361. /// <summary>
  362. /// This function returns or builds a ProbeShaderConsts containing needed data for
  363. /// rendering probes in forward mode
  364. /// </summary>
  365. /// <param name="buffer">The GFXShaderConstBuffer used to build or fetch the Probe Consts</param>
  366. ProbeShaderConstants* getProbeShaderConstants(GFXShaderConstBuffer* buffer);
  367. /// <summary>
  368. /// Sets up the probe data required for doing a render in forward mode.
  369. /// </summary>
  370. virtual void setProbeInfo(ProcessedMaterial* pmat,
  371. const Material* mat,
  372. const SceneData& sgData,
  373. const SceneRenderState* state,
  374. U32 pass,
  375. GFXShaderConstBuffer* shaderConsts);
  376. /// <summary>
  377. /// Invoked as part of the setup in preperation to render an object in forward mode. Used to ensure the probes are
  378. /// sorted ahead of render.
  379. /// </summary>
  380. /// <param name="state"></param>
  381. void setupSGData(SceneData& data, const SceneRenderState* state, LightInfo* light);
  382. /// <summary>
  383. /// Sets up and binds all the shader const data required for rendering probes/IBL for a forward-rendered material.
  384. /// </summary>
  385. /// <returns></returns>
  386. void _update4ProbeConsts(const SceneData& sgData,
  387. MatrixSet& matSet,
  388. ProbeShaderConstants* probeShaderConsts,
  389. GFXShaderConstBuffer* shaderConsts);
  390. //=============================================================================
  391. // Deferred Rendering Functions
  392. //=============================================================================
  393. /// <summary>
  394. /// Ensures the probes are properly sorted before we render them in deferred mode
  395. /// </summary>
  396. void _setupPerFrameParameters(const SceneRenderState *state);
  397. /// <summary>
  398. /// Renders the sorted probes list via a PostEffect to draw them into the buffer data in deferred mode.
  399. /// </summary>
  400. void render(SceneRenderState * state) override;
  401. void clear() override { mActiveProbes.clear(); Parent::clear(); }
  402. };
  403. RenderProbeMgr* RenderProbeMgr::getProbeManager()
  404. {
  405. if (smProbeManager == nullptr)
  406. {
  407. RenderProbeMgr* probeManager = new RenderProbeMgr();
  408. smProbeManager = probeManager;
  409. }
  410. return smProbeManager;
  411. }
  412. #define PROBEMGR RenderProbeMgr::getProbeManager()
  413. #endif // RENDER_PROBE_MGR_H