BsLightProbeVolume.h 10 KB

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