BsLightProbeVolume.h 11 KB

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