BsLightProbeVolume.h 10 KB

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