BsRenderBeastIBLUtility.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsPrerequisites.h"
  5. #include "Renderer/BsIBLUtility.h"
  6. #include "Renderer/BsRendererMaterial.h"
  7. #include "Renderer/BsParamBlocks.h"
  8. namespace bs { namespace ct
  9. {
  10. /** @addtogroup RenderBeast
  11. * @{
  12. */
  13. BS_PARAM_BLOCK_BEGIN(ReflectionCubeDownsampleParamDef)
  14. BS_PARAM_BLOCK_ENTRY(int, gCubeFace)
  15. BS_PARAM_BLOCK_END
  16. extern ReflectionCubeDownsampleParamDef gReflectionCubeDownsampleParamDef;
  17. /** Performs filtering on cubemap faces in order to prepare them for importance sampling. */
  18. class ReflectionCubeDownsampleMat : public RendererMaterial<ReflectionCubeDownsampleMat>
  19. {
  20. RMAT_DEF("ReflectionCubeDownsample.bsl")
  21. public:
  22. ReflectionCubeDownsampleMat();
  23. /** Downsamples the provided texture face and outputs it to the provided target. */
  24. void execute(const SPtr<Texture>& source, UINT32 face, const TextureSurface& surface,
  25. const SPtr<RenderTarget>& target);
  26. private:
  27. SPtr<GpuParamBlockBuffer> mParamBuffer;
  28. GpuParamTexture mInputTexture;
  29. };
  30. BS_PARAM_BLOCK_BEGIN(ReflectionCubeImportanceSampleParamDef)
  31. BS_PARAM_BLOCK_ENTRY(int, gCubeFace)
  32. BS_PARAM_BLOCK_ENTRY(int, gMipLevel)
  33. BS_PARAM_BLOCK_ENTRY(int, gNumMips)
  34. BS_PARAM_BLOCK_ENTRY(float, gPrecomputedMipFactor)
  35. BS_PARAM_BLOCK_END
  36. extern ReflectionCubeImportanceSampleParamDef gReflectionCubeImportanceSampleParamDef;
  37. /** Performs importance sampling on cubemap faces in order for make them suitable for specular evaluation. */
  38. class ReflectionCubeImportanceSampleMat : public RendererMaterial<ReflectionCubeImportanceSampleMat>
  39. {
  40. RMAT_DEF("ReflectionCubeImportanceSample.bsl")
  41. public:
  42. ReflectionCubeImportanceSampleMat();
  43. /** Importance samples the provided texture face and outputs it to the provided target. */
  44. void execute(const SPtr<Texture>& source, UINT32 face, UINT32 mip, const SPtr<RenderTarget>& target);
  45. private:
  46. static const UINT32 NUM_SAMPLES;
  47. SPtr<GpuParamBlockBuffer> mParamBuffer;
  48. GpuParamTexture mInputTexture;
  49. };
  50. /** Vector representing spherical harmonic coefficients for 5 bands. */
  51. struct SHVector5
  52. {
  53. SHVector5()
  54. :coeffs()
  55. { }
  56. float coeffs[25];
  57. };
  58. /** Vector representing spherical coefficients for 5 bands, separate for red, green and blue components. */
  59. struct SHVector5RGB
  60. {
  61. SHVector5 R, G, B;
  62. };
  63. /** Vector representing spherical harmonic coefficients for 3 bands. */
  64. struct SHVector3
  65. {
  66. float coeffs[9];
  67. };
  68. /** Vector representing spherical coefficients for 3 bands, separate for red, green and blue components. */
  69. struct SHVector3RGB
  70. {
  71. SHVector3 R, G, B;
  72. };
  73. /** Intermediate structure used for spherical coefficient calculation. Contains RGB coefficients and weight. */
  74. struct SHCoeffsAndWeight5
  75. {
  76. SHVector5RGB coeffs;
  77. float weight;
  78. };
  79. /** Intermediate structure used for spherical coefficient calculation. Contains RGB coefficients and weight. */
  80. struct SHCoeffsAndWeight3
  81. {
  82. SHVector3RGB coeffs;
  83. float weight;
  84. };
  85. BS_PARAM_BLOCK_BEGIN(IrradianceComputeSHParamDef)
  86. BS_PARAM_BLOCK_ENTRY(int, gCubeFace)
  87. BS_PARAM_BLOCK_ENTRY(int, gFaceSize)
  88. BS_PARAM_BLOCK_ENTRY(Vector2I, gDispatchSize)
  89. BS_PARAM_BLOCK_END
  90. extern IrradianceComputeSHParamDef gIrradianceComputeSHParamDef;
  91. /** Computes spherical harmonic coefficients from a radiance cubemap. */
  92. class IrradianceComputeSHMat : public RendererMaterial<IrradianceComputeSHMat>
  93. {
  94. RMAT_DEF("IrradianceComputeSH.bsl")
  95. public:
  96. IrradianceComputeSHMat();
  97. /**
  98. * Computes spherical harmonic coefficients from a radiance texture and outputs a buffer containing a list of
  99. * coefficient sets (one set of coefficients for each thread group). Coefficients must be reduced and normalized
  100. * by IrradianceReduceSHMat before use. Output buffer should be created by calling createOutputBuffer().
  101. */
  102. void execute(const SPtr<Texture>& source, UINT32 face, const SPtr<GpuBuffer>& output);
  103. /** Creates a buffer of adequate size to be used as output for this material. */
  104. SPtr<GpuBuffer> createOutputBuffer(const SPtr<Texture>& source, UINT32& numCoeffSets);
  105. /**
  106. * Returns the material variation matching the provided parameters.
  107. *
  108. * @param order SH order, which defines the number of coefficients and quality. Only values of 3 and 5 are
  109. * supported.
  110. */
  111. static IrradianceComputeSHMat* getVariation(int order = 5);
  112. private:
  113. SPtr<GpuParamBlockBuffer> mParamBuffer;
  114. GpuParamTexture mInputTexture;
  115. GpuParamBuffer mOutputBuffer;
  116. static ShaderVariation VAR_Order3;
  117. static ShaderVariation VAR_Order5;
  118. };
  119. BS_PARAM_BLOCK_BEGIN(IrradianceReduceSHParamDef)
  120. BS_PARAM_BLOCK_ENTRY(int, gNumEntries)
  121. BS_PARAM_BLOCK_ENTRY(int, gOutputIdx)
  122. BS_PARAM_BLOCK_END
  123. extern IrradianceReduceSHParamDef gIrradianceReduceSHParamDef;
  124. /**
  125. * Sums spherical harmonic coefficients calculated by each thread group of IrradianceComputeSHMat and outputs a single
  126. * set of normalized coefficients.
  127. */
  128. class IrradianceReduceSHMat : public RendererMaterial<IrradianceReduceSHMat>
  129. {
  130. RMAT_DEF("IrradianceReduceSH.bsl")
  131. public:
  132. IrradianceReduceSHMat();
  133. /**
  134. * Sums spherical harmonic coefficients calculated by each thread group of IrradianceComputeSHMat and outputs a
  135. * single set of normalized coefficients. Output buffer should be created by calling createOutputBuffer(). The
  136. * value will be recorded at the @p outputIdx position in the buffer.
  137. */
  138. void execute(const SPtr<GpuBuffer>& source, UINT32 numCoeffSets, const SPtr<GpuBuffer>& output, UINT32 outputIdx);
  139. /** Creates a buffer of adequate size to be used as output for this material. */
  140. SPtr<GpuBuffer> createOutputBuffer(UINT32 numEntries);
  141. /**
  142. * Returns the material variation matching the provided parameters.
  143. *
  144. * @param order SH order, which defines the number of coefficients and quality. Only values of 3 and 5 are
  145. * supported.
  146. */
  147. static IrradianceReduceSHMat* getVariation(int order = 5);
  148. private:
  149. SPtr<GpuParamBlockBuffer> mParamBuffer;
  150. GpuParamBuffer mInputBuffer;
  151. GpuParamBuffer mOutputBuffer;
  152. static ShaderVariation VAR_Order3;
  153. static ShaderVariation VAR_Order5;
  154. };
  155. BS_PARAM_BLOCK_BEGIN(IrradianceProjectSHParamDef)
  156. BS_PARAM_BLOCK_ENTRY(int, gCubeFace)
  157. BS_PARAM_BLOCK_END
  158. extern IrradianceProjectSHParamDef gIrradianceProjectSHParamDef;
  159. /**
  160. * Projects spherical harmonic coefficients calculated by IrradianceReduceSHMat and projects them onto faces of
  161. * a cubemap.
  162. */
  163. class IrradianceProjectSHMat : public RendererMaterial<IrradianceProjectSHMat>
  164. {
  165. RMAT_DEF("IrradianceProjectSH.bsl")
  166. public:
  167. IrradianceProjectSHMat();
  168. /**
  169. * Projects spherical harmonic coefficients calculated by IrradianceReduceSHMat and projects them onto faces of
  170. * a cubemap.
  171. */
  172. void execute(const SPtr<GpuBuffer>& shCoeffs, UINT32 face, const SPtr<RenderTarget>& target);
  173. private:
  174. SPtr<GpuParamBlockBuffer> mParamBuffer;
  175. GpuParamBuffer mInputBuffer;
  176. };
  177. /** Render beast implementation of IBLUtility. */
  178. class RenderBeastIBLUtility : public IBLUtility
  179. {
  180. public:
  181. /** @copydoc IBLUtility::filterCubemapForSpecular */
  182. void filterCubemapForSpecular(const SPtr<Texture>& cubemap, const SPtr<Texture>& scratch) const override;
  183. /** @copydoc IBLUtility::filterCubemapForIrradiance(const SPtr<Texture>&, const SPtr<Texture>&) */
  184. void filterCubemapForIrradiance(const SPtr<Texture>& cubemap, const SPtr<Texture>& output) const override;
  185. /** @copydoc IBLUtility::filterCubemapForIrradiance(const SPtr<Texture>&, const SPtr<GpuBuffer>&, UINT32) */
  186. void filterCubemapForIrradiance(const SPtr<Texture>& cubemap, const SPtr<GpuBuffer>& output,
  187. UINT32 outputIdx) const override;
  188. /** @copydoc IBLUtility::scaleCubemap */
  189. void scaleCubemap(const SPtr<Texture>& src, UINT32 srcMip, const SPtr<Texture>& dst, UINT32 dstMip) const override;
  190. private:
  191. /**
  192. * Downsamples a cubemap using hardware bilinear filtering.
  193. *
  194. * @param[in] src Cubemap to downsample.
  195. * @param[in] srcMip Determines which mip level of the source texture to downsample.
  196. * @param[in] dst Desination texture to output the scaled data to. Must be usable as a render target.
  197. * @param[in] dstMip Determines which mip level of the destination texture to scale.
  198. */
  199. static void downsampleCubemap(const SPtr<Texture>& src, UINT32 srcMip, const SPtr<Texture>& dst, UINT32 dstMip);
  200. };
  201. /** @} */
  202. }}