BsLightProbes.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #include "BsRendererMaterial.h"
  9. #include "BsGpuResourcePool.h"
  10. #include "BsParamBlocks.h"
  11. namespace bs { namespace ct
  12. {
  13. struct FrameInfo;
  14. class LightProbeVolume;
  15. /** @addtogroup RenderBeast
  16. * @{
  17. */
  18. BS_PARAM_BLOCK_BEGIN(TetrahedraRenderParamDef)
  19. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatViewProj)
  20. BS_PARAM_BLOCK_ENTRY(Vector4, gNDCToUV)
  21. BS_PARAM_BLOCK_ENTRY(Vector2, gNDCToDeviceZ)
  22. BS_PARAM_BLOCK_END
  23. extern TetrahedraRenderParamDef gTetrahedraRenderParamDef;
  24. /**
  25. * Shader that renders the tetrahedra used for light probe evaluation. Tetrahedra depth is compare with current scene
  26. * depth, and for each scene pixel the matching tetrahedron index is written to the output target.
  27. */
  28. class TetrahedraRenderMat : public RendererMaterial<TetrahedraRenderMat>
  29. {
  30. RMAT_DEF("TetrahedraRender.bsl");
  31. public:
  32. TetrahedraRenderMat();
  33. /**
  34. * Executes the material using the provided parameters.
  35. *
  36. * @param[in] view View that is currently being rendered.
  37. * @param[in] sceneDepth Depth of scene objects that should be lit.
  38. * @param[in] mesh Mesh to render.
  39. * @param[in] output Output texture created using the descriptor returned by getOutputDesc().
  40. */
  41. void execute(const RendererView& view, const SPtr<Texture>& sceneDepth, const SPtr<Mesh>& mesh,
  42. const SPtr<RenderTexture>& output);
  43. /**
  44. * Returns the descriptors that can be used for creating the output render texture for this material. The render
  45. * texture is expected to have a single color attachment, and a depth attachment.
  46. */
  47. static void getOutputDesc(const RendererView& view, POOLED_RENDER_TEXTURE_DESC& colorDesc,
  48. POOLED_RENDER_TEXTURE_DESC& depthDesc);
  49. /** Returns the material variation matching the provided parameters. */
  50. static TetrahedraRenderMat* getVariation(bool msaa);
  51. private:
  52. SPtr<GpuParamBlockBuffer> mParamBuffer;
  53. GpuParamTexture mDepthBufferTex;
  54. static ShaderVariation VAR_NoMSAA;
  55. static ShaderVariation VAR_MSAA;
  56. };
  57. /** Handles any pre-processing for light (irradiance) probe lighting. */
  58. class LightProbes
  59. {
  60. /** Internal information about a single light probe volume. */
  61. struct VolumeInfo
  62. {
  63. /** Volume containing the information about the probes. */
  64. SPtr<LightProbeVolume> volume;
  65. /** Remains true as long as there are dirty probes in the volume. */
  66. bool isDirty;
  67. };
  68. /**
  69. * Information about a single tetrahedron, including neighbor information. Neighbor 4th index will be set to -1
  70. * if the tetrahedron represents an outer face (which is not actually a tetrahedron, but a triangle, but is stored
  71. * in the same array for convenience).
  72. */
  73. struct TetrahedronData
  74. {
  75. Tetrahedron volume;
  76. Matrix4 transform;
  77. };
  78. public:
  79. LightProbes();
  80. /** Notifies sthe manager that the provided light probe volume has been added. */
  81. void notifyAdded(const SPtr<LightProbeVolume>& volume);
  82. /** Notifies the manager that the provided light probe volume has some dirty light probes. */
  83. void notifyDirty(const SPtr<LightProbeVolume>& volume);
  84. /** Notifies the manager that all the probes in the provided volume have been removed. */
  85. void notifyRemoved(const SPtr<LightProbeVolume>& volume);
  86. /** Updates light probe tetrahedron data after probes changed (added/removed/moved). */
  87. void updateProbes();
  88. private:
  89. /**
  90. * Perform tetrahedrization of the provided point list, and outputs a list of tetrahedrons and outer faces of the
  91. * volume. Each entry contains connections to nearby tetrahedrons/faces, as well as a matrix that can be used for
  92. * calculating barycentric coordinates within the tetrahedron (or projected triangle barycentric coordinates for
  93. * faces).
  94. *
  95. * @param[in,out] positions A set of positions to generate the tetrahedra from. If
  96. * @p generateExtrapolationVolume is enabled then this array will be
  97. * appended with new vertices forming that volume.
  98. * @param[out] output A list of generated tetrahedra and relevant data.
  99. * @param[in] generateExtrapolationVolume If true, the tetrahedron volume will be surrounded with points
  100. * at "infinity" (technically just far away).
  101. */
  102. void generateTetrahedronData(Vector<Vector3>& positions, Vector<TetrahedronData>& output,
  103. bool generateExtrapolationVolume = false);
  104. /** Resizes the GPU buffer used for holding tetrahedron data, to the specified size (in number of tetraheda). */
  105. void resizeTetrahedronBuffer(UINT32 count);
  106. /**
  107. * Resized the GPU buffer that stores light probe SH coefficients, to the specified size (in the number of probes).
  108. */
  109. void resizeCoefficientBuffer(UINT32 count);
  110. Vector<VolumeInfo> mVolumes;
  111. bool mTetrahedronVolumeDirty;
  112. UINT32 mMaxCoefficients;
  113. UINT32 mMaxTetrahedra;
  114. Vector<TetrahedronData> mTetrahedronInfos;
  115. SPtr<GpuBuffer> mProbeCoefficientsGPU;
  116. SPtr<GpuBuffer> mTetrahedronInfosGPU;
  117. SPtr<Mesh> mVolumeMesh;
  118. // Temporary buffers
  119. Vector<Vector3> mTempTetrahedronPositions;
  120. Vector<UINT32> mTempTetrahedronBufferIndices;
  121. };
  122. /** Storage of tetrahedron AA box, for use on the GPU. */
  123. struct TetrahedronBoundsGPU
  124. {
  125. Vector4 center;
  126. Vector4 extents;
  127. };
  128. /** Information about a single tetrahedron, for use on the GPU. */
  129. struct TetrahedronDataGPU
  130. {
  131. UINT32 indices[4];
  132. Matrix3x4 transform;
  133. };
  134. /** @} */
  135. }}