BsLightProbeVolume.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "CoreThread/BsCoreObject.h"
  6. #include "Math/BsAABox.h"
  7. #include "Math/BsVector3.h"
  8. #include "Math/BsQuaternion.h"
  9. #include "Math/BsVectorNI.h"
  10. namespace bs
  11. {
  12. namespace ct
  13. {
  14. class RendererTask;
  15. }
  16. /** @addtogroup Implementation
  17. * @{
  18. */
  19. /** Potential states the light probe can be in. */
  20. enum class LightProbeFlags
  21. {
  22. Empty, Clean, Dirty, Removed
  23. };
  24. /** Base class for both sim and core thread LightProbeVolume implementations. */
  25. class BS_CORE_EXPORT LightProbeVolumeBase
  26. {
  27. public:
  28. LightProbeVolumeBase();
  29. virtual ~LightProbeVolumeBase() { }
  30. /** Returns the position of the volume, in world space. */
  31. Vector3 getPosition() const { return mPosition; }
  32. /** Sets the position of the volume, in world space. */
  33. void setPosition(const Vector3& position) { mPosition = position; _markCoreDirty(); }
  34. /** Returns the rotation of the volume, in world space. */
  35. Quaternion getRotation() const { return mRotation; }
  36. /** Sets the rotation of the light, in world space. */
  37. void setRotation(const Quaternion& rotation) { mRotation = rotation; _markCoreDirty(); }
  38. /** Checks whether the light volume should be used during rendering or not. */
  39. bool getIsActive() const { return mIsActive; }
  40. /** Sets whether the light volume should be used during rendering or not. */
  41. void setIsActive(bool active) { mIsActive = active; _markCoreDirty(); }
  42. /**
  43. * Marks the simulation thread object as dirty and notifies the system its data should be synced with its core
  44. * thread counterpart.
  45. */
  46. virtual void _markCoreDirty() { }
  47. protected:
  48. Vector3 mPosition; /**< World space position. */
  49. Quaternion mRotation; /**< World space rotation. */
  50. bool mIsActive; /**< Whether the light volume should be used during rendering or not. */
  51. };
  52. /** @} */
  53. /** @addtogroup Renderer-Internal
  54. * @{
  55. */
  56. namespace ct { class LightProbeVolume; }
  57. /** Vector representing spherical harmonic coefficients for a light probe. */
  58. struct LightProbeSHCoefficients
  59. {
  60. LightProbeSHCoefficients()
  61. :coeffsR(), coeffsG(), coeffsB()
  62. { }
  63. float coeffsR[9];
  64. float coeffsG[9];
  65. float coeffsB[9];
  66. };
  67. /** SH coefficients for a specific light probe, and its handle. */
  68. struct LightProbeCoefficientInfo
  69. {
  70. UINT32 handle;
  71. LightProbeSHCoefficients coefficients;
  72. };
  73. /** Information about a single probe in the light probe volume. */
  74. struct LightProbeInfo
  75. {
  76. UINT32 handle;
  77. Vector3 position;
  78. LightProbeSHCoefficients shCoefficients;
  79. };
  80. /**
  81. * Allows you to define a volume of light probes that will be used for indirect lighting. Lighting information in the
  82. * scene will be interpolated from nearby probes to calculate the amount of indirect lighting at that position. It is
  83. * up to the caller to place the light probes in areas where the lighting changes in order to yield the best results.
  84. *
  85. * The volume can never have less than 4 probes.
  86. */
  87. class BS_CORE_EXPORT LightProbeVolume : public IReflectable, public CoreObject, public LightProbeVolumeBase
  88. {
  89. /** Internal information about a single light probe. */
  90. struct ProbeInfo
  91. {
  92. ProbeInfo() {}
  93. ProbeInfo(LightProbeFlags flags, const Vector3& position)
  94. :flags(flags), position(position)
  95. { }
  96. LightProbeFlags flags;
  97. Vector3 position;
  98. /** Coefficients are only valid directly after deserialization, or after updateCoefficients() is called. */
  99. LightProbeSHCoefficients coefficients;
  100. };
  101. public:
  102. ~LightProbeVolume();
  103. /** Adds a new probe at the specified position and returns a handle to the probe. */
  104. UINT32 addProbe(const Vector3& position);
  105. /** Updates the position of the probe with the specified handle. */
  106. void setProbePosition(UINT32 handle, const Vector3& position);
  107. /** Retrieves the position of the probe with the specified handle. */
  108. Vector3 getProbePosition(UINT32 handle) const;
  109. /**
  110. * Removes the probe with the specified handle. Note that if this is one of the last four remaining probes in the
  111. * volume it cannot be removed.
  112. */
  113. void removeProbe(UINT32 handle);
  114. /** Returns a list of positions of all light probes in the volume. */
  115. Vector<LightProbeInfo> getProbes() const;
  116. /**
  117. * Causes the information for this specific light probe to be updated. You generally want to call this when the
  118. * probe is moved or the scene around the probe changes.
  119. */
  120. void renderProbe(UINT32 handle);
  121. /**
  122. * Causes the information for all lights probes to be updated. You generally want to call this if you move the
  123. * entire light volume or the scene around the volume changes.
  124. */
  125. void renderProbes();
  126. /**
  127. * Resizes the light probe grid and inserts new light probes, if the new size is larger than previous size.
  128. * New probes are inserted in a grid pattern matching the new size and density parameters.
  129. *
  130. * Note that shrinking the volume will not remove light probes. In order to remove probes outside of the new volume
  131. * call clip().
  132. *
  133. * Resize will not change the positions of current light probes. If you wish to reset all probes to the currently
  134. * set grid position, call reset().
  135. * @param[in] volume Axis aligned volume to be covered by the light probes.
  136. * @param[in] cellCount Number of grid cells to split the volume into. Minimum number of 1, in which case each
  137. * corner of the volume is represented by a single probe. Higher values subdivide the
  138. * volume in an uniform way.
  139. */
  140. void resize(const AABox& volume, const Vector3I& cellCount = {1, 1, 1});
  141. /** Removes any probes outside of the current grid volume. */
  142. void clip();
  143. /**
  144. * Resets all probes to match the original grid pattern. This will reset probe positions, as well as add/remove
  145. * probes as necessary, essentially losing any custom changes to the probes.
  146. */
  147. void reset();
  148. /** Retrieves an implementation of the object usable only from the core thread. */
  149. SPtr<ct::LightProbeVolume> getCore() const;
  150. /**
  151. * Creates a new light volume with probes aligned in a grid pattern.
  152. *
  153. * @param[in] volume Axis aligned volume to be covered by the light probes.
  154. * @param[in] cellCount Number of grid cells to split the volume into. Minimum number of 1, in which case each
  155. * corner of the volume is represented by a single probe. Higher values subdivide the
  156. * volume in an uniform way.
  157. */
  158. static SPtr<LightProbeVolume> create(const AABox& volume = AABox::UNIT_BOX, const Vector3I& cellCount = {1, 1, 1});
  159. /** Returns the hash value that can be used to identify if the internal data needs an update. */
  160. UINT32 _getLastModifiedHash() const { return mLastUpdateHash; }
  161. /** Sets the hash value that can be used to identify if the internal data needs an update. */
  162. void _setLastModifiedHash(UINT32 hash) { mLastUpdateHash = hash; }
  163. /** Updates the transfrom from the provided scene object, if the scene object's data is detected to be dirty. */
  164. void _updateTransform(const HSceneObject& so, bool force = false);
  165. protected:
  166. friend class ct::LightProbeVolume;
  167. LightProbeVolume(const AABox& volume, const Vector3I& cellCount);
  168. /** Renders the light probe data on the core thread. */
  169. void runRenderProbeTask();
  170. /**
  171. * Fetches latest SH coefficient data from the core thread. Note this method will block the caller thread until
  172. * the data is fetched from the core thread. It will also force any in-progress light probe updates to finish.
  173. */
  174. void updateCoefficients();
  175. /** @copydoc CoreObject::createCore */
  176. SPtr<ct::CoreObject> createCore() const override;
  177. /** @copydoc LightProbeVolumeBase::_markCoreDirty */
  178. void _markCoreDirty() override;
  179. /** @copydoc CoreObject::syncToCore */
  180. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  181. /** Creates a light volume with without initializing it. Used for serialization. */
  182. static SPtr<LightProbeVolume> createEmpty();
  183. private:
  184. UnorderedMap<UINT32, ProbeInfo> mProbes;
  185. AABox mVolume = AABox::UNIT_BOX;
  186. Vector3I mCellCount;
  187. UINT32 mLastUpdateHash;
  188. UINT32 mNextProbeId = 0;
  189. SPtr<ct::RendererTask> mRendererTask;
  190. /************************************************************************/
  191. /* RTTI */
  192. /************************************************************************/
  193. public:
  194. friend class LightProbeVolumeRTTI;
  195. static RTTITypeBase* getRTTIStatic();
  196. RTTITypeBase* getRTTI() const override;
  197. protected:
  198. LightProbeVolume(); // Serialization only
  199. };
  200. namespace ct
  201. {
  202. /** Information about a single light probe in a light probe volume. */
  203. struct LightProbeInfo
  204. {
  205. /** Unique handle representing the probe. Always remains the same. */
  206. UINT32 handle;
  207. /** Flags representing the current state of the probe. */
  208. LightProbeFlags flags;
  209. /** Index into the GPU buffer where probe coefficients are stored. -1 if not assigned. Transient. */
  210. UINT32 bufferIdx;
  211. };
  212. /** Core thread usable version of bs::LightProbeVolume. */
  213. class BS_CORE_EXPORT LightProbeVolume : public CoreObject, public LightProbeVolumeBase
  214. {
  215. public:
  216. ~LightProbeVolume();
  217. /** Sets an ID that can be used for uniquely identifying this object by the renderer. */
  218. void setRendererId(UINT32 id) { mRendererId = id; }
  219. /** Retrieves an ID that can be used for uniquely identifying this object by the renderer. */
  220. UINT32 getRendererId() const { return mRendererId; }
  221. /** Returns the number of light probes that are active. */
  222. UINT32 getNumActiveProbes() const { return (UINT32)mProbeMap.size(); }
  223. /** Returns a list of positions for all light probes. Only the first getNumActiveProbes() entries are active. */
  224. const Vector<Vector3>& getLightProbePositions() const { return mProbePositions; }
  225. /**
  226. * Returns non-positional information about all light probes. Only the first getNumActiveProbes() entries are
  227. * active.
  228. */
  229. const Vector<LightProbeInfo>& getLightProbeInfos() const { return mProbeInfos; }
  230. /** Populates the vector with SH coefficients for each light probe. Involves reading the GPU buffer. */
  231. void getProbeCoefficients(Vector<LightProbeCoefficientInfo>& output) const;
  232. /** Returns the GPU buffer containing SH coefficients. */
  233. SPtr<GpuBuffer> getCoefficientsBuffer() const { return mCoefficients; }
  234. protected:
  235. friend class bs::LightProbeVolume;
  236. LightProbeVolume(const UnorderedMap<UINT32, bs::LightProbeVolume::ProbeInfo>& probes);
  237. /** @copydoc CoreObject::initialize */
  238. void initialize() override;
  239. /** @copydoc CoreObject::syncToCore */
  240. void syncToCore(const CoreSyncData& data) override;
  241. /**
  242. * Renders dirty probes and updates their SH coefficients in the local GPU buffer.
  243. *
  244. * @param[in] maxProbes Maximum number of probes to render. Set to zero to render all dirty probes. Limiting the
  245. * number of probes allows the rendering to be distributed over multiple frames.
  246. * @return True if there are no more dirty probes to process.
  247. */
  248. bool renderProbes(UINT32 maxProbes);
  249. /**
  250. * Resizes the internal GPU buffer that stores light probe SH coefficients, to the specified size (in the number
  251. * of probes).
  252. */
  253. void resizeCoefficientBuffer(UINT32 count);
  254. UINT32 mRendererId = 0;
  255. UnorderedMap<UINT32, UINT32> mProbeMap; // Map from static indices to compact list of probes
  256. UINT32 mFirstDirtyProbe = 0;
  257. Vector<Vector3> mProbePositions;
  258. Vector<LightProbeInfo> mProbeInfos;
  259. // Contains SH coefficients for the probes
  260. SPtr<GpuBuffer> mCoefficients;
  261. UINT32 mCoeffBufferSize = 0;
  262. // Temporary until initialization
  263. Vector<LightProbeSHCoefficients> mInitCoefficients;
  264. };
  265. }
  266. /** @} */
  267. }