BsLightProbeVolume.h 9.8 KB

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