BsLightProbeVolume.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. /** @addtogroup Implementation
  12. * @{
  13. */
  14. /** Potential states the light probe can be in. */
  15. enum class LightProbeFlags
  16. {
  17. Empty, Clean, Dirty, Removed
  18. };
  19. /** Base class for both sim and core thread LightProbeVolume implementations. */
  20. class BS_CORE_EXPORT LightProbeVolumeBase
  21. {
  22. public:
  23. LightProbeVolumeBase();
  24. virtual ~LightProbeVolumeBase() { }
  25. /** Returns the position of the volume, in world space. */
  26. Vector3 getPosition() const { return mPosition; }
  27. /** Sets the position of the volume, in world space. */
  28. void setPosition(const Vector3& position) { mPosition = position; _markCoreDirty(); }
  29. /** Returns the rotation of the volume, in world space. */
  30. Quaternion getRotation() const { return mRotation; }
  31. /** Sets the rotation of the light, in world space. */
  32. void setRotation(const Quaternion& rotation) { mRotation = rotation; _markCoreDirty(); }
  33. /** Checks whether the light volume should be used during rendering or not. */
  34. bool getIsActive() const { return mIsActive; }
  35. /** Sets whether the light volume should be used during rendering or not. */
  36. void setIsActive(bool active) { mIsActive = active; _markCoreDirty(); }
  37. /**
  38. * Marks the simulation thread object as dirty and notifies the system its data should be synced with its core
  39. * thread counterpart.
  40. */
  41. virtual void _markCoreDirty() { }
  42. protected:
  43. Vector3 mPosition; /**< World space position. */
  44. Quaternion mRotation; /**< World space rotation. */
  45. bool mIsActive; /**< Whether the light volume should be used during rendering or not. */
  46. };
  47. /** @} */
  48. /** @addtogroup Renderer-Engine-Internal
  49. * @{
  50. */
  51. namespace ct { class LightProbeVolume; }
  52. /**
  53. * Allows you to define a volume of light probes that will be used for indirect lighting. Lighting information in the
  54. * scene will be interpolated from nearby probes to calculate the amount of indirect lighting at that position. It is
  55. * up to the caller to place the light probes in areas where the lighting changes in order to yield the best results.
  56. *
  57. * The volume can never have less than 4 probes.
  58. */
  59. class BS_CORE_EXPORT LightProbeVolume : public IReflectable, public CoreObject, public LightProbeVolumeBase
  60. {
  61. /** Internal information about a single light probe. */
  62. struct ProbeInfo
  63. {
  64. ProbeInfo() {}
  65. ProbeInfo(LightProbeFlags flags, const Vector3& position)
  66. :flags(flags), position(position)
  67. { }
  68. LightProbeFlags flags;
  69. Vector3 position;
  70. };
  71. public:
  72. /** Adds a new probe at the specified position and returns a handle to the probe. */
  73. UINT32 addProbe(const Vector3& position);
  74. /** Updates the position of the probe with the specified handle. */
  75. void setProbePosition(UINT32 handle, const Vector3& position);
  76. /** Retrieves the position of the probe with the specified handle. */
  77. Vector3 getProbePosition(UINT32 handle) const;
  78. /**
  79. * Removes the probe with the specified handle. Note that if this is one of the last four remaining probes in the
  80. * volume it cannot be removed.
  81. */
  82. void removeProbe(UINT32 handle);
  83. /** Retrieves an implementation of the object usable only from the core thread. */
  84. SPtr<ct::LightProbeVolume> getCore() const;
  85. /**
  86. * Creates a new light volume with probes aligned in a grid pattern.
  87. *
  88. * @param[in] volume Axis aligned volume to be covered by the light probes.
  89. * @param[in] density Density of light probes in each direction. Starting on one side of the volume, a new
  90. * probe will be added every 1/density meters (per-axis). Note that one probe will be
  91. * placed at the start and at the end of the volume, regardless of density. This means the
  92. * smallest volume will have 8 probes.
  93. */
  94. static SPtr<LightProbeVolume> create(const AABox& volume = AABox::UNIT_BOX, const Vector3& density = Vector3::ONE);
  95. protected:
  96. friend class ct::LightProbeVolume;
  97. LightProbeVolume(const AABox& volume, const Vector3& density);
  98. /** @copydoc CoreObject::createCore */
  99. SPtr<ct::CoreObject> createCore() const override;
  100. /** @copydoc LightProbeVolumeBase::_markCoreDirty */
  101. void _markCoreDirty() override;
  102. /** @copydoc CoreObject::syncToCore */
  103. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  104. /** Creates a light volume with without initializing it. Used for serialization. */
  105. static SPtr<LightProbeVolume> createEmpty();
  106. private:
  107. UnorderedMap<UINT32, ProbeInfo> mProbes;
  108. UINT32 mNextProbeId = 0;
  109. /************************************************************************/
  110. /* RTTI */
  111. /************************************************************************/
  112. public:
  113. friend class LightProbeVolumeRTTI;
  114. static RTTITypeBase* getRTTIStatic();
  115. RTTITypeBase* getRTTI() const override;
  116. protected:
  117. LightProbeVolume(); // Serialization only
  118. };
  119. namespace ct
  120. {
  121. /** Information about a single light probe in a light probe volume. */
  122. struct LightProbeInfo
  123. {
  124. /** Unique handle representing the probe. Always remains the same. */
  125. UINT32 handle;
  126. /** Flags representing the current state of the probe. */
  127. LightProbeFlags flags;
  128. /** Index into the GPU buffer where probe coefficients are stored. -1 if not assigned. Transient. */
  129. UINT32 bufferIdx;
  130. };
  131. /** Core thread usable version of bs::LightProbeVolume. */
  132. class BS_CORE_EXPORT LightProbeVolume : public CoreObject, public LightProbeVolumeBase
  133. {
  134. public:
  135. ~LightProbeVolume();
  136. /** Sets an ID that can be used for uniquely identifying this object by the renderer. */
  137. void setRendererId(UINT32 id) { mRendererId = id; }
  138. /** Retrieves an ID that can be used for uniquely identifying this object by the renderer. */
  139. UINT32 getRendererId() const { return mRendererId; }
  140. /**
  141. * Parses the list of probes and reorganizes it by removing gaps so that all probes are sequential.
  142. *
  143. * @param[out] freedEntries A list of entries mapping to the GPU buffer where probe SH coefficients are stored.
  144. * These are the entries that have been freed since the last call to prune().
  145. * @param[in] freeAll If true, all probes held by this volume will be marked as freed.
  146. */
  147. void prune(Vector<UINT32>& freedEntries, bool freeAll = false);
  148. /** Returns information about all light probes. */
  149. Vector<LightProbeInfo>& getLightProbeInfos() { return mProbeInfos; }
  150. /** Returns a list of positions for all light probes. */
  151. Vector<Vector3>& getLightProbePositions() { return mProbePositions; }
  152. protected:
  153. friend class bs::LightProbeVolume;
  154. LightProbeVolume(const UnorderedMap<UINT32, bs::LightProbeVolume::ProbeInfo>& probes);
  155. /** @copydoc CoreObject::initialize */
  156. void initialize() override;
  157. /** @copydoc CoreObject::syncToCore */
  158. void syncToCore(const CoreSyncData& data) override;
  159. UINT32 mRendererId = 0;
  160. UnorderedMap<UINT32, UINT32> mProbeMap; // Map from static indices to compact list of probes
  161. Vector<Vector3> mProbePositions;
  162. Vector<LightProbeInfo> mProbeInfos;
  163. };
  164. }
  165. /** @} */
  166. }