BsPostProcessing.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 "BsRendererMaterial.h"
  6. #include "BsParamBlocks.h"
  7. #include "BsRenderTexturePool.h"
  8. #include "BsStandardPostProcessSettings.h"
  9. namespace BansheeEngine
  10. {
  11. /** @addtogroup RenderBeast
  12. * @{
  13. */
  14. /** Contains per-camera data used by post process effects. */
  15. struct PostProcessInfo
  16. {
  17. SPtr<StandardPostProcessSettings> settings;
  18. bool settingDirty = true;
  19. SPtr<PooledRenderTexture> downsampledSceneTex;
  20. SPtr<PooledRenderTexture> histogramTex;
  21. SPtr<PooledRenderTexture> histogramReduceTex;
  22. SPtr<PooledRenderTexture> eyeAdaptationTex[2];
  23. SPtr<PooledRenderTexture> colorLUT;
  24. INT32 lastEyeAdaptationTex = 0;
  25. };
  26. BS_PARAM_BLOCK_BEGIN(DownsampleParams)
  27. BS_PARAM_BLOCK_ENTRY(Vector2, gInvTexSize)
  28. BS_PARAM_BLOCK_END
  29. /** Shader that downsamples a texture to half its size. */
  30. class DownsampleMat : public RendererMaterial<DownsampleMat>
  31. {
  32. RMAT_DEF("PPDownsample.bsl");
  33. public:
  34. DownsampleMat();
  35. /** Renders the post-process effect with the provided parameters. */
  36. void execute(const SPtr<RenderTextureCore>& target, PostProcessInfo& ppInfo);
  37. /** Releases the output render target. */
  38. void release(PostProcessInfo& ppInfo);
  39. /** Returns the render texture where the output will be written. */
  40. SPtr<RenderTextureCore> getOutput() const { return mOutput; }
  41. private:
  42. DownsampleParams mParams;
  43. GpuParamTextureCore mInputTexture;
  44. POOLED_RENDER_TEXTURE_DESC mOutputDesc;
  45. SPtr<RenderTextureCore> mOutput;
  46. };
  47. BS_PARAM_BLOCK_BEGIN(EyeAdaptHistogramParams)
  48. BS_PARAM_BLOCK_ENTRY(Vector4I, gPixelOffsetAndSize)
  49. BS_PARAM_BLOCK_ENTRY(Vector2, gHistogramParams)
  50. BS_PARAM_BLOCK_ENTRY(Vector2I, gThreadGroupCount)
  51. BS_PARAM_BLOCK_END
  52. /** Shader that creates a luminance histogram used for eye adaptation. */
  53. class EyeAdaptHistogramMat : public RendererMaterial<EyeAdaptHistogramMat>
  54. {
  55. RMAT_DEF("PPEyeAdaptHistogram.bsl");
  56. public:
  57. EyeAdaptHistogramMat();
  58. /** Executes the post-process effect with the provided parameters. */
  59. void execute(PostProcessInfo& ppInfo);
  60. /** Releases the output render target. */
  61. void release(PostProcessInfo& ppInfo);
  62. /** Returns the render texture where the output was written. */
  63. SPtr<RenderTextureCore> getOutput() const { return mOutput; }
  64. /** Calculates the number of thread groups that need to execute to cover the provided render target. */
  65. static Vector2I getThreadGroupCount(const SPtr<RenderTextureCore>& target);
  66. /**
  67. * Returns a vector containing scale and offset (in that order) that will be applied to luminance values
  68. * to determine their position in the histogram.
  69. */
  70. static Vector2 getHistogramScaleOffset(const PostProcessInfo& ppInfo);
  71. static const UINT32 THREAD_GROUP_SIZE_X = 8;
  72. static const UINT32 THREAD_GROUP_SIZE_Y = 8;
  73. static const UINT32 HISTOGRAM_NUM_TEXELS = (THREAD_GROUP_SIZE_X * THREAD_GROUP_SIZE_Y) / 4;
  74. private:
  75. EyeAdaptHistogramParams mParams;
  76. GpuParamTextureCore mSceneColor;
  77. GpuParamLoadStoreTextureCore mOutputTex;
  78. POOLED_RENDER_TEXTURE_DESC mOutputDesc;
  79. SPtr<RenderTextureCore> mOutput;
  80. static const UINT32 LOOP_COUNT_X = 8;
  81. static const UINT32 LOOP_COUNT_Y = 8;
  82. };
  83. BS_PARAM_BLOCK_BEGIN(EyeAdaptHistogramReduceParams)
  84. BS_PARAM_BLOCK_ENTRY(int, gThreadGroupCount)
  85. BS_PARAM_BLOCK_END
  86. /** Shader that reduces the luminance histograms created by EyeAdaptHistogramMat into a single histogram. */
  87. class EyeAdaptHistogramReduceMat : public RendererMaterial<EyeAdaptHistogramReduceMat>
  88. {
  89. RMAT_DEF("PPEyeAdaptHistogramReduce.bsl");
  90. public:
  91. EyeAdaptHistogramReduceMat();
  92. /** Executes the post-process effect with the provided parameters. */
  93. void execute(PostProcessInfo& ppInfo);
  94. /** Releases the output render target. */
  95. void release(PostProcessInfo& ppInfo);
  96. /** Returns the render texture where the output was written. */
  97. SPtr<RenderTextureCore> getOutput() const { return mOutput; }
  98. private:
  99. EyeAdaptHistogramReduceParams mParams;
  100. GpuParamTextureCore mHistogramTex;
  101. GpuParamTextureCore mEyeAdaptationTex;
  102. POOLED_RENDER_TEXTURE_DESC mOutputDesc;
  103. SPtr<RenderTextureCore> mOutput;
  104. };
  105. BS_PARAM_BLOCK_BEGIN(EyeAdaptationParams)
  106. BS_PARAM_BLOCK_ENTRY_ARRAY(Vector4, gEyeAdaptationParams, 3)
  107. BS_PARAM_BLOCK_END
  108. /** Shader that computes the eye adaptation value based on scene luminance. */
  109. class EyeAdaptationMat : public RendererMaterial<EyeAdaptationMat>
  110. {
  111. RMAT_DEF("PPEyeAdaptation.bsl");
  112. public:
  113. EyeAdaptationMat();
  114. /** Executes the post-process effect with the provided parameters. */
  115. void execute(PostProcessInfo& ppInfo, float frameDelta);
  116. private:
  117. EyeAdaptationParams mParams;
  118. GpuParamTextureCore mReducedHistogramTex;
  119. };
  120. BS_PARAM_BLOCK_BEGIN(CreateTonemapLUTParams)
  121. BS_PARAM_BLOCK_ENTRY_ARRAY(Vector4, gTonemapParams, 2)
  122. BS_PARAM_BLOCK_ENTRY(float, gGammaAdjustment)
  123. BS_PARAM_BLOCK_ENTRY(int, gGammaCorrectionType)
  124. BS_PARAM_BLOCK_ENTRY(Vector3, gSaturation)
  125. BS_PARAM_BLOCK_ENTRY(Vector3, gContrast)
  126. BS_PARAM_BLOCK_ENTRY(Vector3, gGain)
  127. BS_PARAM_BLOCK_ENTRY(Vector3, gOffset)
  128. BS_PARAM_BLOCK_END
  129. BS_PARAM_BLOCK_BEGIN(WhiteBalanceParams)
  130. BS_PARAM_BLOCK_ENTRY(float, gWhiteTemp)
  131. BS_PARAM_BLOCK_ENTRY(float, gWhiteOffset)
  132. BS_PARAM_BLOCK_END
  133. /**
  134. * Shader that creates a 3D lookup texture that is used to apply tonemapping, color grading, white balancing and gamma
  135. * correction.
  136. */
  137. class CreateTonemapLUTMat : public RendererMaterial<CreateTonemapLUTMat>
  138. {
  139. RMAT_DEF("PPCreateTonemapLUT.bsl");
  140. public:
  141. CreateTonemapLUTMat();
  142. /** Executes the post-process effect with the provided parameters. */
  143. void execute(PostProcessInfo& ppInfo);
  144. /** Releases the output render target. */
  145. void release(PostProcessInfo& ppInfo);
  146. /** Size of the 3D color lookup table. */
  147. static const UINT32 LUT_SIZE = 32;
  148. private:
  149. CreateTonemapLUTParams mParams;
  150. WhiteBalanceParams mWhiteBalanceParams;
  151. };
  152. BS_PARAM_BLOCK_BEGIN(TonemappingParams)
  153. BS_PARAM_BLOCK_ENTRY(float, gRawGamma)
  154. BS_PARAM_BLOCK_ENTRY(float, gManualExposureScale)
  155. BS_PARAM_BLOCK_END
  156. /** Shader that applies tonemapping and converts a HDR image into a LDR image. */
  157. template<bool GammaOnly, bool AutoExposure>
  158. class TonemappingMat : public RendererMaterial<TonemappingMat<GammaOnly, AutoExposure>>
  159. {
  160. RMAT_DEF("PPTonemapping.bsl");
  161. public:
  162. TonemappingMat();
  163. /** Executes the post-process effect with the provided parameters. */
  164. void execute(const SPtr<RenderTextureCore>& sceneColor, const SPtr<ViewportCore>& outputViewport,
  165. PostProcessInfo& ppInfo);
  166. private:
  167. TonemappingParams mParams;
  168. GpuParamTextureCore mInputTex;
  169. GpuParamTextureCore mColorLUT;
  170. GpuParamTextureCore mEyeAdaptationTex;
  171. };
  172. /**
  173. * Renders post-processing effects for the provided render target.
  174. *
  175. * @note Core thread only.
  176. */
  177. class BS_BSRND_EXPORT PostProcessing : public Module<PostProcessing>
  178. {
  179. public:
  180. /** Renders post-processing effects for the provided render target. */
  181. void postProcess(const SPtr<RenderTextureCore>& sceneColor, const CameraCore* camera,
  182. PostProcessInfo& ppInfo, float frameDelta);
  183. private:
  184. DownsampleMat mDownsample;
  185. EyeAdaptHistogramMat mEyeAdaptHistogram;
  186. EyeAdaptHistogramReduceMat mEyeAdaptHistogramReduce;
  187. EyeAdaptationMat mEyeAdaptation;
  188. CreateTonemapLUTMat mCreateLUT;
  189. TonemappingMat<false, true> mTonemapping_AE;
  190. TonemappingMat<true, true> mTonemapping_AE_GO;
  191. TonemappingMat<false, false> mTonemapping;
  192. TonemappingMat<true, false> mTonemapping_GO;
  193. };
  194. /** @} */
  195. }