BsLightProbes.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsRenderBeastPrerequisites.h"
  5. #include "BsTriangulation.h"
  6. #include "BsMatrix4.h"
  7. #include "BsMatrixNxM.h"
  8. namespace bs { namespace ct
  9. {
  10. struct FrameInfo;
  11. class LightProbeVolume;
  12. struct VisibleLightProbeData;
  13. /** @addtogroup RenderBeast
  14. * @{
  15. */
  16. /** Handles any pre-processing for light (irradiance) probe lighting. */
  17. class LightProbes
  18. {
  19. /** Internal information about a single light probe volume. */
  20. struct VolumeInfo
  21. {
  22. /** Volume containing the information about the probes. */
  23. SPtr<LightProbeVolume> volume;
  24. /** Remains true as long as there are dirty probes in the volume. */
  25. bool isDirty;
  26. /** Keeps track of which dirty probe was last updated, so we can perform the update over multiple frames. */
  27. UINT32 lastUpdatedProbe;
  28. };
  29. /**
  30. * Information about a single tetrahedron, including neighbor information. Neighbor 4th index will be set to -1
  31. * if the tetrahedron represents an outer face (which is not actually a tetrahedron, but a triangle, but is stored
  32. * in the same array for convenience).
  33. */
  34. struct TetrahedronData
  35. {
  36. Tetrahedron volume;
  37. Matrix4 transform;
  38. };
  39. public:
  40. LightProbes();
  41. /** Notifies sthe manager that the provided light probe volume has been added. */
  42. void notifyAdded(const SPtr<LightProbeVolume>& volume);
  43. /** Notifies the manager that the provided light probe volume has some dirty light probes. */
  44. void notifyDirty(const SPtr<LightProbeVolume>& volume);
  45. /** Notifies the manager that all the probes in the provided volume have been removed. */
  46. void notifyRemoved(const SPtr<LightProbeVolume>& volume);
  47. /**
  48. * Updates any dirty light probes by rendering the scene from their perspective and generating their SH
  49. * coefficients.
  50. *
  51. * @param[in] frameInfo Information about the current frame.
  52. * @param[in] maxProbes Places a limit of how many probes can be updated in a single call to this method.
  53. * Any probes that weren't updated will be updated when the method is called next
  54. * (up to the @p maxProbes limit), as so on.
  55. *
  56. * This limit is provided to ensure there are no massive framerate spikes caused up
  57. * updating many probes in a single frame - instead this method allows the updates to
  58. * be distributed over multiple frames.
  59. *
  60. * Provide a limit of 0 to force all probes to be updated.
  61. */
  62. void updateProbes(const FrameInfo& frameInfo, UINT32 maxProbes = 3);
  63. /** Generates GPU buffers that contain a list of probe tetrahedrons visible from the provided view. */
  64. void updateVisibleProbes(const RendererView& view, VisibleLightProbeData& output);
  65. private:
  66. /**
  67. * Perform tetrahedrization of the provided point list, and outputs a list of tetrahedrons and outer faces of the
  68. * volume. Each entry contains connections to nearby tetrahedrons/faces, as well as a matrix that can be used for
  69. * calculating barycentric coordinates within the tetrahedron (or projected triangle barycentric coordinates for
  70. * faces).
  71. */
  72. void generateTetrahedronData(const Vector<Vector3>& positions, Vector<TetrahedronData>& output,
  73. bool includeOuterFaces = false);
  74. /** Resizes the GPU buffers used for holding tetrahedron data, to the specified size (in number of tetraheda). */
  75. void resizeTetrahedronBuffers(VisibleLightProbeData& data, UINT32 count);
  76. /**
  77. * Resized the GPU buffer that stores light probe SH coefficients, to the specified size (in the number of probes).
  78. */
  79. void resizeCoefficientBuffer(UINT32 count);
  80. Vector<VolumeInfo> mVolumes;
  81. bool mTetrahedronVolumeDirty;
  82. UINT32 mNumAllocatedEntries;
  83. UINT32 mNumUsedEntries;
  84. Vector<UINT32> mEmptyEntries;
  85. Vector<AABox> mTetrahedronBounds;
  86. Vector<TetrahedronData> mTetrahedronInfos;
  87. SPtr<GpuBuffer> mProbeCoefficientsGPU;
  88. // Temporary buffers
  89. Vector<Vector3> mTempTetrahedronPositions;
  90. Vector<UINT32> mTempTetrahedronVisibility;
  91. };
  92. /** Storage of tetrahedron AA box, for use on the GPU. */
  93. struct TetrahedronBoundsGPU
  94. {
  95. Vector4 center;
  96. Vector4 extents;
  97. };
  98. /** Information about a single tetrahedron, for use on the GPU. */
  99. struct TetrahedronDataGPU
  100. {
  101. UINT32 indices[4];
  102. Matrix3x4 transform;
  103. };
  104. /** Contains information about light probes visible from a particular RendererView. */
  105. struct VisibleLightProbeData
  106. {
  107. /** Current number of visible tetrahedrons in the GPU buffers. */
  108. UINT32 numEntries;
  109. /** Maximum number of tetrahedrons that fit in the GPU buffers, before the buffers need to be resized. */
  110. UINT32 maxNumEntries;
  111. /** GPU buffer containing tetrahedron bounds in form of TetrahedronBoundsGPU structure. */
  112. SPtr<GpuBuffer> tetrahedronBounds;
  113. /** GPU buffer containing tetrahedron information in form of TetrahedronDataGPU structure. */
  114. SPtr<GpuBuffer> tetrahedronInfos;
  115. };
  116. /** @} */
  117. }}