BsLightProbeVolume.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. namespace bs
  10. {
  11. namespace ct
  12. {
  13. class RendererTask;
  14. }
  15. /** @addtogroup Implementation
  16. * @{
  17. */
  18. /** Potential states the light probe can be in. */
  19. enum class LightProbeFlags
  20. {
  21. Empty, Clean, Dirty, Removed
  22. };
  23. /** Base class for both sim and core thread LightProbeVolume implementations. */
  24. class BS_CORE_EXPORT LightProbeVolumeBase
  25. {
  26. public:
  27. LightProbeVolumeBase();
  28. virtual ~LightProbeVolumeBase() { }
  29. /** Returns the position of the volume, in world space. */
  30. Vector3 getPosition() const { return mPosition; }
  31. /** Sets the position of the volume, in world space. */
  32. void setPosition(const Vector3& position) { mPosition = position; _markCoreDirty(); }
  33. /** Returns the rotation of the volume, in world space. */
  34. Quaternion getRotation() const { return mRotation; }
  35. /** Sets the rotation of the light, in world space. */
  36. void setRotation(const Quaternion& rotation) { mRotation = rotation; _markCoreDirty(); }
  37. /** Checks whether the light volume should be used during rendering or not. */
  38. bool getIsActive() const { return mIsActive; }
  39. /** Sets whether the light volume should be used during rendering or not. */
  40. void setIsActive(bool active) { mIsActive = active; _markCoreDirty(); }
  41. /**
  42. * Marks the simulation thread object as dirty and notifies the system its data should be synced with its core
  43. * thread counterpart.
  44. */
  45. virtual void _markCoreDirty() { }
  46. protected:
  47. Vector3 mPosition; /**< World space position. */
  48. Quaternion mRotation; /**< World space rotation. */
  49. bool mIsActive; /**< Whether the light volume should be used during rendering or not. */
  50. };
  51. /** @} */
  52. /** @addtogroup Renderer-Internal
  53. * @{
  54. */
  55. namespace ct { class LightProbeVolume; }
  56. /** Vector representing spherical harmonic coefficients for a light probe. */
  57. struct LightProbeSHCoefficients
  58. {
  59. float coeffsR[9];
  60. float coeffsG[9];
  61. float coeffsB[9];
  62. };
  63. /** SH coefficients for a specific light probe, and its handle. */
  64. struct LightProbeCoefficientInfo
  65. {
  66. UINT32 handle;
  67. LightProbeSHCoefficients coefficients;
  68. };
  69. /**
  70. * Allows you to define a volume of light probes that will be used for indirect lighting. Lighting information in the
  71. * scene will be interpolated from nearby probes to calculate the amount of indirect lighting at that position. It is
  72. * up to the caller to place the light probes in areas where the lighting changes in order to yield the best results.
  73. *
  74. * The volume can never have less than 4 probes.
  75. */
  76. class BS_CORE_EXPORT LightProbeVolume : public IReflectable, public CoreObject, public LightProbeVolumeBase
  77. {
  78. /** Internal information about a single light probe. */
  79. struct ProbeInfo
  80. {
  81. ProbeInfo() {}
  82. ProbeInfo(LightProbeFlags flags, const Vector3& position)
  83. :flags(flags), position(position)
  84. { }
  85. LightProbeFlags flags;
  86. Vector3 position;
  87. /** Coefficients are only valid directly after deserialization, or after updateCoefficients() is called. */
  88. LightProbeSHCoefficients coefficients;
  89. };
  90. public:
  91. ~LightProbeVolume();
  92. /** Adds a new probe at the specified position and returns a handle to the probe. */
  93. UINT32 addProbe(const Vector3& position);
  94. /** Updates the position of the probe with the specified handle. */
  95. void setProbePosition(UINT32 handle, const Vector3& position);
  96. /** Retrieves the position of the probe with the specified handle. */
  97. Vector3 getProbePosition(UINT32 handle) const;
  98. /**
  99. * Removes the probe with the specified handle. Note that if this is one of the last four remaining probes in the
  100. * volume it cannot be removed.
  101. */
  102. void removeProbe(UINT32 handle);
  103. /**
  104. * Causes the information for this specific light probe to be updated. You generally want to call this when the
  105. * probe is moved or the scene around the probe changes.
  106. */
  107. void renderProbe(UINT32 handle);
  108. /**
  109. * Causes the information for all lights probes to be updated. You generally want to call this if you move the
  110. * entire light volume or the scene around the volume changes.
  111. */
  112. void renderProbes();
  113. /** Retrieves an implementation of the object usable only from the core thread. */
  114. SPtr<ct::LightProbeVolume> getCore() const;
  115. /**
  116. * Creates a new light volume with probes aligned in a grid pattern.
  117. *
  118. * @param[in] volume Axis aligned volume to be covered by the light probes.
  119. * @param[in] density Density of light probes in each direction. Starting on one side of the volume, a new
  120. * probe will be added every 1/density meters (per-axis). Note that one probe will be
  121. * placed at the start and at the end of the volume, regardless of density. This means the
  122. * smallest volume will have 8 probes.
  123. */
  124. static SPtr<LightProbeVolume> create(const AABox& volume = AABox::UNIT_BOX, const Vector3& density = Vector3::ONE);
  125. protected:
  126. friend class ct::LightProbeVolume;
  127. LightProbeVolume(const AABox& volume, const Vector3& density);
  128. /** Renders the light probe data on the core thread. */
  129. void runRenderProbeTask();
  130. /**
  131. * Fetches latest SH coefficient data from the core thread. Note this method will block the caller thread until
  132. * the data is fetched from the core thread. It will also force any in-progress light probe updated to finish.
  133. */
  134. void updateCoefficients();
  135. /** @copydoc CoreObject::createCore */
  136. SPtr<ct::CoreObject> createCore() const override;
  137. /** @copydoc LightProbeVolumeBase::_markCoreDirty */
  138. void _markCoreDirty() override;
  139. /** @copydoc CoreObject::syncToCore */
  140. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  141. /** Creates a light volume with without initializing it. Used for serialization. */
  142. static SPtr<LightProbeVolume> createEmpty();
  143. private:
  144. UnorderedMap<UINT32, ProbeInfo> mProbes;
  145. UINT32 mNextProbeId = 0;
  146. SPtr<ct::RendererTask> mRendererTask;
  147. /************************************************************************/
  148. /* RTTI */
  149. /************************************************************************/
  150. public:
  151. friend class LightProbeVolumeRTTI;
  152. static RTTITypeBase* getRTTIStatic();
  153. RTTITypeBase* getRTTI() const override;
  154. protected:
  155. LightProbeVolume(); // Serialization only
  156. };
  157. namespace ct
  158. {
  159. /** Information about a single light probe in a light probe volume. */
  160. struct LightProbeInfo
  161. {
  162. /** Unique handle representing the probe. Always remains the same. */
  163. UINT32 handle;
  164. /** Flags representing the current state of the probe. */
  165. LightProbeFlags flags;
  166. /** Index into the GPU buffer where probe coefficients are stored. -1 if not assigned. Transient. */
  167. UINT32 bufferIdx;
  168. };
  169. /** Core thread usable version of bs::LightProbeVolume. */
  170. class BS_CORE_EXPORT LightProbeVolume : public CoreObject, public LightProbeVolumeBase
  171. {
  172. public:
  173. ~LightProbeVolume();
  174. /** Sets an ID that can be used for uniquely identifying this object by the renderer. */
  175. void setRendererId(UINT32 id) { mRendererId = id; }
  176. /** Retrieves an ID that can be used for uniquely identifying this object by the renderer. */
  177. UINT32 getRendererId() const { return mRendererId; }
  178. /** Returns the number of light probes that are active. */
  179. UINT32 getNumActiveProbes() const { return (UINT32)mProbeMap.size(); }
  180. /** Returns a list of positions for all light probes. Only the first getNumActiveProbes() entries are active. */
  181. const Vector<Vector3>& getLightProbePositions() const { return mProbePositions; }
  182. /**
  183. * Returns non-positional information about all light probes. Only the first getNumActiveProbes() entries are
  184. * active.
  185. */
  186. const Vector<LightProbeInfo>& getLightProbeInfos() const { return mProbeInfos; }
  187. /** Populates the vector with SH coefficients for each light probe. Involves reading the GPU buffer. */
  188. void getProbeCoefficients(Vector<LightProbeCoefficientInfo>& output) const;
  189. /** Returns the GPU buffer containing SH coefficients. */
  190. SPtr<GpuBuffer> getCoefficientsBuffer() const { return mCoefficients; }
  191. protected:
  192. friend class bs::LightProbeVolume;
  193. LightProbeVolume(const UnorderedMap<UINT32, bs::LightProbeVolume::ProbeInfo>& probes);
  194. /** @copydoc CoreObject::initialize */
  195. void initialize() override;
  196. /** @copydoc CoreObject::syncToCore */
  197. void syncToCore(const CoreSyncData& data) override;
  198. /**
  199. * Renders dirty probes and updates their SH coefficients in the local GPU buffer.
  200. *
  201. * @param[in] maxProbes Maximum number of probes to render. Set to zero to render all dirty probes. Limiting the
  202. * number of probes allows the rendering to be distributed over multiple frames.
  203. * @return True if there are no more dirty probes to process.
  204. */
  205. bool renderProbes(UINT32 maxProbes);
  206. /**
  207. * Resizes the internal GPU buffer that stores light probe SH coefficients, to the specified size (in the number
  208. * of probes).
  209. */
  210. void resizeCoefficientBuffer(UINT32 count);
  211. UINT32 mRendererId = 0;
  212. UnorderedMap<UINT32, UINT32> mProbeMap; // Map from static indices to compact list of probes
  213. UINT32 mFirstDirtyProbe = 0;
  214. Vector<Vector3> mProbePositions;
  215. Vector<LightProbeInfo> mProbeInfos;
  216. // Contains SH coefficients for the probes
  217. SPtr<GpuBuffer> mCoefficients;
  218. UINT32 mCoeffBufferSize = 0;
  219. // Temporary until initialization
  220. Vector<LightProbeSHCoefficients> mInitCoefficients;
  221. };
  222. }
  223. /** @} */
  224. }