BsPostProcessing.h 7.7 KB

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