BsLightProbeVolume.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 "BsCoreObject.h"
  6. #include "BsAABox.h"
  7. #include "BsVector3.h"
  8. #include "BsQuaternion.h"
  9. #include "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. /**
  74. * Allows you to define a volume of light probes that will be used for indirect lighting. Lighting information in the
  75. * scene will be interpolated from nearby probes to calculate the amount of indirect lighting at that position. It is
  76. * up to the caller to place the light probes in areas where the lighting changes in order to yield the best results.
  77. *
  78. * The volume can never have less than 4 probes.
  79. */
  80. class BS_CORE_EXPORT LightProbeVolume : public IReflectable, public CoreObject, public LightProbeVolumeBase
  81. {
  82. /** Internal information about a single light probe. */
  83. struct ProbeInfo
  84. {
  85. ProbeInfo() {}
  86. ProbeInfo(LightProbeFlags flags, const Vector3& position)
  87. :flags(flags), position(position)
  88. { }
  89. LightProbeFlags flags;
  90. Vector3 position;
  91. /** Coefficients are only valid directly after deserialization, or after updateCoefficients() is called. */
  92. LightProbeSHCoefficients coefficients;
  93. };
  94. public:
  95. ~LightProbeVolume();
  96. /** Adds a new probe at the specified position and returns a handle to the probe. */
  97. UINT32 addProbe(const Vector3& position);
  98. /** Updates the position of the probe with the specified handle. */
  99. void setProbePosition(UINT32 handle, const Vector3& position);
  100. /** Retrieves the position of the probe with the specified handle. */
  101. Vector3 getProbePosition(UINT32 handle) const;
  102. /**
  103. * Removes the probe with the specified handle. Note that if this is one of the last four remaining probes in the
  104. * volume it cannot be removed.
  105. */
  106. void removeProbe(UINT32 handle);
  107. /**
  108. * Causes the information for this specific light probe to be updated. You generally want to call this when the
  109. * probe is moved or the scene around the probe changes.
  110. */
  111. void renderProbe(UINT32 handle);
  112. /**
  113. * Causes the information for all lights probes to be updated. You generally want to call this if you move the
  114. * entire light volume or the scene around the volume changes.
  115. */
  116. void renderProbes();
  117. /**
  118. * Resizes the light probe grid and inserts new light probes, if the new size is larger than previous size.
  119. * New probes are inserted in a grid pattern matching the new size and density parameters.
  120. *
  121. * Note that shrinking the volume will not remove light probes. In order to remove probes outside of the new volume
  122. * call clip().
  123. *
  124. * Resize will not change the positions of current light probes. If you wish to reset all probes to the currently
  125. * set grid position, call reset().
  126. * @param[in] volume Axis aligned volume to be covered by the light probes.
  127. * @param[in] cellCount Number of grid cells to split the volume into. Minimum number of 1, in which case each
  128. * corner of the volume is represented by a single probe. Higher values subdivide the
  129. * volume in an uniform way.
  130. */
  131. void resize(const AABox& volume, const Vector3I& cellCount = {1, 1, 1});
  132. /** Removes any probes outside of the current grid volume. */
  133. void clip();
  134. /**
  135. * Resets all probes to match the original grid pattern. This will reset probe positions, as well as add/remove
  136. * probes as necessary, essentially losing any custom changes to the probes.
  137. */
  138. void reset();
  139. /** Retrieves an implementation of the object usable only from the core thread. */
  140. SPtr<ct::LightProbeVolume> getCore() const;
  141. /**
  142. * Creates a new light volume with probes aligned in a grid pattern.
  143. *
  144. * @param[in] volume Axis aligned volume to be covered by the light probes.
  145. * @param[in] cellCount Number of grid cells to split the volume into. Minimum number of 1, in which case each
  146. * corner of the volume is represented by a single probe. Higher values subdivide the
  147. * volume in an uniform way.
  148. */
  149. static SPtr<LightProbeVolume> create(const AABox& volume = AABox::UNIT_BOX, const Vector3I& cellCount = {1, 1, 1});
  150. /** Returns the hash value that can be used to identify if the internal data needs an update. */
  151. UINT32 _getLastModifiedHash() const { return mLastUpdateHash; }
  152. /** Sets the hash value that can be used to identify if the internal data needs an update. */
  153. void _setLastModifiedHash(UINT32 hash) { mLastUpdateHash = hash; }
  154. /** Updates the transfrom from the provided scene object, if the scene object's data is detected to be dirty. */
  155. void _updateTransform(const HSceneObject& so, bool force = false);
  156. protected:
  157. friend class ct::LightProbeVolume;
  158. LightProbeVolume(const AABox& volume, const Vector3I& cellCount);
  159. /** Renders the light probe data on the core thread. */
  160. void runRenderProbeTask();
  161. /**
  162. * Fetches latest SH coefficient data from the core thread. Note this method will block the caller thread until
  163. * the data is fetched from the core thread. It will also force any in-progress light probe updated to finish.
  164. */
  165. void updateCoefficients();
  166. /** @copydoc CoreObject::createCore */
  167. SPtr<ct::CoreObject> createCore() const override;
  168. /** @copydoc LightProbeVolumeBase::_markCoreDirty */
  169. void _markCoreDirty() override;
  170. /** @copydoc CoreObject::syncToCore */
  171. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  172. /** Creates a light volume with without initializing it. Used for serialization. */
  173. static SPtr<LightProbeVolume> createEmpty();
  174. private:
  175. UnorderedMap<UINT32, ProbeInfo> mProbes;
  176. AABox mVolume = AABox::UNIT_BOX;
  177. Vector3I mCellCount;
  178. UINT32 mLastUpdateHash;
  179. UINT32 mNextProbeId = 0;
  180. SPtr<ct::RendererTask> mRendererTask;
  181. /************************************************************************/
  182. /* RTTI */
  183. /************************************************************************/
  184. public:
  185. friend class LightProbeVolumeRTTI;
  186. static RTTITypeBase* getRTTIStatic();
  187. RTTITypeBase* getRTTI() const override;
  188. protected:
  189. LightProbeVolume(); // Serialization only
  190. };
  191. namespace ct
  192. {
  193. /** Information about a single light probe in a light probe volume. */
  194. struct LightProbeInfo
  195. {
  196. /** Unique handle representing the probe. Always remains the same. */
  197. UINT32 handle;
  198. /** Flags representing the current state of the probe. */
  199. LightProbeFlags flags;
  200. /** Index into the GPU buffer where probe coefficients are stored. -1 if not assigned. Transient. */
  201. UINT32 bufferIdx;
  202. };
  203. /** Core thread usable version of bs::LightProbeVolume. */
  204. class BS_CORE_EXPORT LightProbeVolume : public CoreObject, public LightProbeVolumeBase
  205. {
  206. public:
  207. ~LightProbeVolume();
  208. /** Sets an ID that can be used for uniquely identifying this object by the renderer. */
  209. void setRendererId(UINT32 id) { mRendererId = id; }
  210. /** Retrieves an ID that can be used for uniquely identifying this object by the renderer. */
  211. UINT32 getRendererId() const { return mRendererId; }
  212. /** Returns the number of light probes that are active. */
  213. UINT32 getNumActiveProbes() const { return (UINT32)mProbeMap.size(); }
  214. /** Returns a list of positions for all light probes. Only the first getNumActiveProbes() entries are active. */
  215. const Vector<Vector3>& getLightProbePositions() const { return mProbePositions; }
  216. /**
  217. * Returns non-positional information about all light probes. Only the first getNumActiveProbes() entries are
  218. * active.
  219. */
  220. const Vector<LightProbeInfo>& getLightProbeInfos() const { return mProbeInfos; }
  221. /** Populates the vector with SH coefficients for each light probe. Involves reading the GPU buffer. */
  222. void getProbeCoefficients(Vector<LightProbeCoefficientInfo>& output) const;
  223. /** Returns the GPU buffer containing SH coefficients. */
  224. SPtr<GpuBuffer> getCoefficientsBuffer() const { return mCoefficients; }
  225. protected:
  226. friend class bs::LightProbeVolume;
  227. LightProbeVolume(const UnorderedMap<UINT32, bs::LightProbeVolume::ProbeInfo>& probes);
  228. /** @copydoc CoreObject::initialize */
  229. void initialize() override;
  230. /** @copydoc CoreObject::syncToCore */
  231. void syncToCore(const CoreSyncData& data) override;
  232. /**
  233. * Renders dirty probes and updates their SH coefficients in the local GPU buffer.
  234. *
  235. * @param[in] maxProbes Maximum number of probes to render. Set to zero to render all dirty probes. Limiting the
  236. * number of probes allows the rendering to be distributed over multiple frames.
  237. * @return True if there are no more dirty probes to process.
  238. */
  239. bool renderProbes(UINT32 maxProbes);
  240. /**
  241. * Resizes the internal GPU buffer that stores light probe SH coefficients, to the specified size (in the number
  242. * of probes).
  243. */
  244. void resizeCoefficientBuffer(UINT32 count);
  245. UINT32 mRendererId = 0;
  246. UnorderedMap<UINT32, UINT32> mProbeMap; // Map from static indices to compact list of probes
  247. UINT32 mFirstDirtyProbe = 0;
  248. Vector<Vector3> mProbePositions;
  249. Vector<LightProbeInfo> mProbeInfos;
  250. // Contains SH coefficients for the probes
  251. SPtr<GpuBuffer> mCoefficients;
  252. UINT32 mCoeffBufferSize = 0;
  253. // Temporary until initialization
  254. Vector<LightProbeSHCoefficients> mInitCoefficients;
  255. };
  256. }
  257. /** @} */
  258. }