llreflectionmapmanager.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /**
  2. * @file llreflectionmapmanager.h
  3. * @brief LLReflectionMap, LLReflectionMapManager and LLHeroProbeManager
  4. * classes declaration.
  5. *
  6. * $LicenseInfo:firstyear=2022&license=viewergpl$
  7. *
  8. * Copyright (c) 2022, Linden Research, Inc.
  9. *
  10. * Second Life Viewer Source Code
  11. * The source code in this file ("Source Code") is provided by Linden Lab
  12. * to you under the terms of the GNU General Public License, version 2.0
  13. * ("GPL"), unless you have obtained a separate licensing agreement
  14. * ("Other License"), formally executed by you and Linden Lab. Terms of
  15. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  16. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  17. *
  18. * There are special exceptions to the terms and conditions of the GPL as
  19. * it is applied to this Source Code. View the full text of the exception
  20. * in the file doc/FLOSS-exception.txt in this software distribution, or
  21. * online at
  22. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  23. *
  24. * By copying, modifying or distributing this software, you acknowledge
  25. * that you have read and understood your obligations described above,
  26. * and agree to abide by those obligations.
  27. *
  28. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  29. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  30. * COMPLETENESS OR PERFORMANCE.
  31. * $/LicenseInfo$
  32. */
  33. #ifndef LL_LLREFLECTIONMAPMANAGER_H
  34. #define LL_LLREFLECTIONMAPMANAGER_H
  35. #include <list>
  36. #include <vector>
  37. #include "llcubemap.h"
  38. #include "llcubemaparray.h"
  39. #include "hbfastset.h"
  40. #include "llplane.h"
  41. #include "llrendertarget.h"
  42. #include "llvovolume.h"
  43. class LLSpatialGroup;
  44. // Number of reflection probes to keep in VRAM
  45. #define LL_MAX_REFLECTION_PROBE_COUNT 256
  46. // Maximum number of hero reflection probes to keep in VRAM
  47. #define LL_MAX_HERO_PROBE_COUNT 2
  48. // Reflection probe resolution
  49. #define LL_IRRADIANCE_MAP_RESOLUTION 64
  50. // Reflection probe mininum scale
  51. #define LL_REFLECTION_PROBE_MINIMUM_SCALE 1.f;
  52. // Change to 1 to reset the HDRI sky on reflection maps reset (instead, the
  53. // Cool VL Viewer got a debug setting allowing to disable/enable HDRI preview
  54. // on demand). HB
  55. #define LL_RESET_HDRI_SKY_ON_REFLECTION_MAP_RESET 0
  56. ///////////////////////////////////////////////////////////////////////////////
  57. // LLReflectionMap class
  58. // This used to be in its own llreflectionmap.h/cpp module, but the header was
  59. // only included by llreflectionmapmanager.h, so... HB
  60. ///////////////////////////////////////////////////////////////////////////////
  61. class alignas(16) LLReflectionMap : public LLRefCount
  62. {
  63. protected:
  64. LOG_CLASS(LLReflectionMap);
  65. public:
  66. LL_ALIGNED16_NEW_DELETE
  67. enum DetailLevel : U32
  68. {
  69. DISABLED = 0,
  70. STATIC_ONLY = 1,
  71. STATIC_AND_DYNAMIC = 2,
  72. REALTIME = 3
  73. };
  74. typedef std::vector<LLReflectionMap*> reflmap_vec_t;
  75. // Allocates an environment map of the given resolution
  76. LLReflectionMap();
  77. ~LLReflectionMap();
  78. // Updates this environment map resolution
  79. void update(U32 resolution, U32 face, bool force_dynamic = false,
  80. F32 near_clip = -1.f);
  81. // For volume partition probes, tries to place this probe in the best spot
  82. void autoAdjustOrigin();
  83. // Returns true if given reflection map's influence volume intersects with
  84. // this one's.
  85. bool intersects(LLReflectionMap* otherp);
  86. // Gets the ambiance value to use for this probe
  87. F32 getAmbiance();
  88. // Gets the near clip plane distance to use for this probe
  89. F32 getNearClip();
  90. // Returns true if this probe should include avatars in its reflection map
  91. bool getIsDynamic();
  92. // Gets the encoded bounding box of this probe's influence volume; will
  93. // only return a box if this probe is associated with a LLVOVolume with its
  94. // reflection probe influence volume to to VOLUME_TYPE_BOX. Returns false
  95. // if no bounding box (treat as sphere influence volume).
  96. bool getBox(LLMatrix4& box);
  97. // Returns true if this probe is active for rendering
  98. LL_INLINE bool isActive() { return mCubeIndex != -1; }
  99. // Performs occlusion query/readback
  100. void doOcclusion(const LLVector4a& eye);
  101. // Returns false if this probe is not currently relevant (for example,
  102. // disabled due to graphics preferences).
  103. bool isRelevant();
  104. public:
  105. // Index into array packed by LLReflectionMapManager::getReflectionMaps()
  106. // WARNING: only valid immediately after call to getReflectionMaps().
  107. // Note
  108. S32 mProbeIndex;
  109. // Spatial group this probe is tracking (if any).
  110. LLPointer<LLSpatialGroup> mGroup;
  111. // Point at which environment map was last generated from (in agent space).
  112. // Note: placed 16 bytes (counting the LLRefCount S32) from variable
  113. // members start, to avoid padding issues. HB
  114. LLVector4a mOrigin;
  115. // Viewer object this probe is tracking (if any).
  116. LLPointer<LLViewerObject> mViewerObject;
  117. // Set of any LLReflectionMaps that intersect this map (maintained by
  118. // LLReflectionMapManager).
  119. reflmap_vec_t mNeighbors;
  120. // Cube map used to sample this environment map.
  121. LLPointer<LLCubeMapArray> mCubeArray;
  122. // Index into cube map array or -1 if not currently stored in a cube map.
  123. S32 mCubeIndex;
  124. // Distance from main viewer camera.
  125. F32 mDistance;
  126. // Minimum and maximum depth in current render camera.
  127. F32 mMinDepth;
  128. F32 mMaxDepth;
  129. // Radius of this probe's affected area
  130. F32 mRadius;
  131. // Last time this probe was updated (or when its update timer got reset).
  132. F32 mLastUpdateTime;
  133. // Last time this probe was bound for rendering.
  134. F32 mLastBindTime;
  135. // fade in parameter for this probe
  136. F32 mFadeIn;
  137. // What priority should this probe have (higher is higher priority)
  138. // currently only 0 or 1
  139. // 0 - automatic probe
  140. // 1 - manual probe
  141. U32 mPriority;
  142. // Occlusion culling state
  143. U32 mOcclusionQuery;
  144. U32 mOcclusionPendingFrames;
  145. bool mOccluded;
  146. // true when probe has had at least one full update and is ready to render.
  147. bool mComplete;
  148. };
  149. ///////////////////////////////////////////////////////////////////////////////
  150. // LLReflectionMapManager class
  151. ///////////////////////////////////////////////////////////////////////////////
  152. class alignas(16) LLReflectionMapManager
  153. {
  154. friend class LLHeroProbeManager;
  155. friend class LLPipeline;
  156. protected:
  157. LOG_CLASS(LLReflectionMapManager);
  158. public:
  159. LL_ALIGNED16_NEW_DELETE
  160. // Allocates an environment map of the given resolution
  161. LLReflectionMapManager();
  162. // Releases any GL state
  163. void cleanup();
  164. // Maintains reflection probes
  165. void update();
  166. // Adds a probe for the given spatial group.
  167. LLReflectionMap* addProbe(LLSpatialGroup* groupp = NULL);
  168. // Populate "maps" with the N most relevant reflection maps where N is no
  169. // more than maps.size(). If less than maps.size() reflection maps are
  170. // available, will assign trailing elements to NULL. 'maps' must be an
  171. // adequately sized array of reflection map pointers.
  172. void getReflectionMaps(std::vector<LLReflectionMap*>& maps);
  173. // Called by LLSpatialGroup constructor. If spatial group should receive a
  174. // reflection probe, creates one for the specified spatial group.
  175. LLReflectionMap* registerSpatialGroup(LLSpatialGroup* groupp);
  176. // Presently hacked into LLViewerObject::setTE(). Used by LLViewerObjects
  177. // which are reflection probes. 'vobjp' must not be NULL. Guaranteed to not
  178. // return NULL.
  179. LLReflectionMap* registerViewerObject(LLViewerObject* vobjp);
  180. // Resets all state on the next update
  181. LL_INLINE void reset() { mReset = true; }
  182. // Pauses all updates other than the default probe for 'duration' seconds.
  183. void pause(F32 duration);
  184. // Called on region crossing to "shift" probes into new coordinate frame.
  185. void shift(const LLVector4a& offset);
  186. // Called from LLSpatialPartition when reflection probe debug display is
  187. // active.
  188. void renderDebug();
  189. // Called once at startup to allocate cubemap arrays
  190. void initReflectionMaps();
  191. // Returns true if currently updating a radiance map, false if currently
  192. // updating an irradiance map.
  193. LL_INLINE bool isRadiancePass() { return mRadiancePass; }
  194. // Performs occlusion culling on all active reflection probes
  195. void doOcclusion();
  196. // *HACK: "culls" all reflection probes except the default one. Only call
  197. // this if you do not intend to call updateUniforms directly. Call again
  198. // with false when done.
  199. void forceDefaultProbeAndUpdateUniforms(bool force = true);
  200. typedef std::vector<LLPointer<LLReflectionMap> > prmap_vec_t;
  201. private:
  202. // Initializes mCubeFree array to default values
  203. void initCubeFree();
  204. // Deletes the probe with the given index in mProbes
  205. void deleteProbe(U32 i);
  206. // Gets a free cube index
  207. // returns -1 if allocation failed
  208. S32 allocateCubeIndex();
  209. // Updates the neighbors of the given probe
  210. void updateNeighbors(LLReflectionMap* probep);
  211. // Updates UBO used for rendering (call only once per render pipe flush)
  212. void updateUniforms();
  213. // Binds UBO used for rendering
  214. void setUniforms();
  215. // Performs an update on the currently updating probe
  216. void doProbeUpdate();
  217. // Updates the specified face of the specified probe
  218. void updateProbeFace(LLReflectionMap* probep, U32 face);
  219. private:
  220. // Render target for cube snapshots; used to generate mipmaps without
  221. // doing a copy-to-texture.
  222. LLRenderTarget mRenderTarget;
  223. std::vector<LLRenderTarget> mMipChain;
  224. // List of free cubemap indices
  225. std::list<S32> mCubeFree;
  226. // Storage for reflection probe radiance maps (plus two scratch space
  227. // cubemaps)
  228. LLPointer<LLCubeMapArray> mTexture;
  229. // Vertex buffer for pushing verts to filter shaders
  230. LLPointer<LLVertexBuffer> mVertexBuffer;
  231. // Storage for reflection probe irradiance maps
  232. LLPointer<LLCubeMapArray> mIrradianceMaps;
  233. // Default reflection probe to fall back to for pixels with no probe
  234. // influences (should always be at cube index 0).
  235. LLPointer<LLReflectionMap> mDefaultProbe;
  236. LLReflectionMap* mUpdatingProbe;
  237. // List of maps being used for rendering
  238. typedef std::vector<LLReflectionMap*> rmap_vec_t;
  239. rmap_vec_t mReflectionMaps;
  240. // List of active reflection maps
  241. prmap_vec_t mProbes;
  242. // List of reflection maps to kill
  243. prmap_vec_t mKillList;
  244. // List of reflection maps to create
  245. prmap_vec_t mCreateList;
  246. // Handle to UBO
  247. U32 mUBO;
  248. U32 mUpdatingFace;
  249. // Number of reflection probes to use for rendering.
  250. U32 mReflectionProbeCount;
  251. // Resolution of reflection probes
  252. U32 mProbeResolution;
  253. // Previous resolution of reflection probes (used to detect resolution
  254. // changes and reset render target and mip chains when they happen). HB
  255. U32 mOldProbeResolution;
  256. // Maximum LoD of reflection probes (mip levels - 1)
  257. F32 mMaxProbeLOD;
  258. // Amount to scale local lights during an irradiance map update (set during
  259. // updateProbeFace() and used by LLPipeline).
  260. F32 mLightScale;
  261. F32 mResumeTime;
  262. // If true, we are generating the radiance map for the current probe,
  263. // otherwise we are generating the irradiance map. Update sequence should
  264. // be to generate the irradiance map from render of the world that has no
  265. // irradiance, then generate the radiance map from a render of the world
  266. // that includes irradiance. This should avoid feedback loops and ensure
  267. // that the colors in the radiance maps match the colors in the
  268. // environment.
  269. bool mRadiancePass;
  270. // Same as above, but for the realtime probe. Realtime probes should update
  271. // all six sides of the irradiance map on "odd" frames and all six sides of
  272. // the radiance map on "even" frames.
  273. bool mRealtimeRadiancePass;
  274. // If true, reset all probe render state on the next update (for teleports
  275. // and sky changes).
  276. bool mReset;
  277. // If true, only update the default probe
  278. bool mPaused;
  279. };
  280. ///////////////////////////////////////////////////////////////////////////////
  281. // LLHeroProbeManager class
  282. ///////////////////////////////////////////////////////////////////////////////
  283. struct HeroProbeData
  284. {
  285. LLMatrix4 heroBox;
  286. LLVector4 heroSphere;
  287. GLint heroShape;
  288. GLint heroMipCount;
  289. GLint heroProbeCount;
  290. };
  291. class alignas(16) LLHeroProbeManager
  292. {
  293. friend class LLPipeline;
  294. friend class LLReflectionMapManager;
  295. protected:
  296. LOG_CLASS(LLHeroProbeManager);
  297. public:
  298. LL_ALIGNED16_NEW_DELETE
  299. LLHeroProbeManager();
  300. ~LLHeroProbeManager();
  301. // Releases any GL state
  302. void cleanup();
  303. void update();
  304. void renderProbes();
  305. // Called from LLSpatialPartition when reflection probe debug display is
  306. // active.
  307. void renderDebug();
  308. // Called once at startup to allocate cubemap arrays
  309. void initReflectionMaps();
  310. // Performs occlusion culling on all active reflection probes
  311. void doOcclusion();
  312. LL_INLINE void reset() { mReset = true; }
  313. bool registerViewerObject(LLVOVolume* volp);
  314. void unregisterViewerObject(LLVOVolume* volp);
  315. LL_INLINE bool isMirrorPass() const { return mRenderingMirror; }
  316. private:
  317. // Updates UBO used for rendering (call only once per render pipe flush)
  318. void updateUniforms();
  319. // Updates the specified face of the specified probe
  320. void updateProbeFace(LLReflectionMap* probep, U32 face, bool is_dynamic,
  321. F32 near_clip);
  322. void generateRadiance(LLReflectionMap* probep);
  323. private:
  324. // Aligned member
  325. LLPlane mCurrentClipPlane;
  326. LLRenderTarget mRenderTarget;
  327. std::vector<LLRenderTarget> mMipChain;
  328. // Storage for reflection probe radiance maps (plus two scratch space
  329. // cubemaps)
  330. LLPointer<LLCubeMapArray> mTexture;
  331. // Vertex buffer for pushing verts to filter shaders
  332. LLPointer<LLVertexBuffer> mVertexBuffer;
  333. // List of active reflection maps
  334. typedef std::vector<LLPointer<LLReflectionMap> > prmap_vec_t;
  335. prmap_vec_t mProbes;
  336. // Default reflection probe to fall back to for pixels with no probe
  337. // influences (should always be at cube index 0).
  338. LLPointer<LLReflectionMap> mDefaultProbe;
  339. std::map<U32, S32> mFaceUpdateList;
  340. typedef fast_hset<LLPointer<LLVOVolume> > volp_set_t;
  341. volp_set_t mHeroVOList;
  342. LLPointer<LLVOVolume> mNearestHero;
  343. // Number of reflection probes to use for rendering
  344. U32 mReflectionProbeCount;
  345. U32 mProbeResolution;
  346. F32 mNearestProbeDist;
  347. // Maximum LoD of reflection probes (mip levels - 1)
  348. F32 mMaxProbeLOD;
  349. F32 mHeroProbeStrength;
  350. // Last time this probe was updated (or when its update timer got reset).
  351. F32 mLastUpdateTime;
  352. U32 mCurrentProbeUpdateFrame;
  353. bool mIsInTransition;
  354. // If true, reset all probe render state on the next update (for teleports
  355. // and sky changes).
  356. bool mReset;
  357. bool mRenderingMirror;
  358. public:
  359. LLVector3 mMirrorPosition;
  360. LLVector3 mMirrorNormal;
  361. HeroProbeData mHeroData;
  362. };
  363. #endif // LL_LLREFLECTIONMAPMANAGER_H