BsPostProcessing.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. namespace BansheeEngine
  9. {
  10. /** @addtogroup RenderBeast
  11. * @{
  12. */
  13. /** Settings that control the post-process operation. */
  14. struct PostProcessSettings
  15. {
  16. /**
  17. * Determines minimum luminance value in the eye adaptation histogram. In log2 units (-8 = 1/256). In the range
  18. * [-16, 0].
  19. */
  20. float histogramLog2Min = -8.0f;
  21. /**
  22. * Determines maximum luminance value in the eye adaptation histogram. In log2 units (4 = 16). In the range
  23. * [0, 16].
  24. */
  25. float histogramLog2Max = 4.0f;
  26. /** Percentage below which to ignore values in the eye adaptation histogram. In range [0.0f, 1.0f]. */
  27. float histogramPctLow = 0.8f;
  28. /** Percentage above which to ignore values in the eye adaptation histogram. In range [0.0f, 1.0f]. */
  29. float histogramPctHigh = 0.983f;
  30. /** Clamps the minimum eye adaptation scale (pre-exposure scale) to this value. In range [0.0f, 10.0f]. */
  31. float minEyeAdaptation = 0.03f;
  32. /** Clamps the maximum eye adaptation scale (pre-exposure scale) to this value. In range [0.0f, 10.0f]. */
  33. float maxEyeAdaptation = 2.0f;
  34. /**
  35. * Log2 value to scale the eye adaptation by. Smaller values yield darker image, while larger yield brighter image.
  36. * In range [-8, 8].
  37. */
  38. float exposureScale = 0.0f;
  39. /** Determines how quickly does the eye adaptation adjust to larger values. In range [0.01f, 20.0f]. */
  40. float eyeAdaptationSpeedUp = 3.0f;
  41. /** Determines how quickly does the eye adaptation adjust to smaller values. In range [0.01f, 20.0f]. */
  42. float eyeAdaptationSpeedDown = 3.0f;
  43. };
  44. /** Contains per-camera data used by post process effects. */
  45. struct PostProcessInfo
  46. {
  47. PostProcessSettings settings;
  48. SPtr<PooledRenderTexture> downsampledSceneTex;
  49. SPtr<PooledRenderTexture> histogramTex;
  50. SPtr<PooledRenderTexture> histogramReduceTex;
  51. SPtr<PooledRenderTexture> eyeAdaptationTex[2];
  52. INT32 lastEyeAdaptationTex = 0;
  53. };
  54. BS_PARAM_BLOCK_BEGIN(DownsampleParams)
  55. BS_PARAM_BLOCK_ENTRY(Vector2, gInvTexSize)
  56. BS_PARAM_BLOCK_END
  57. /** Shader that downsamples a texture to half its size. */
  58. class DownsampleMat : public RendererMaterial<DownsampleMat>
  59. {
  60. RMAT_DEF("PPDownsample.bsl");
  61. public:
  62. DownsampleMat();
  63. /** Renders the post-process effect with the provided parameters. */
  64. void execute(const SPtr<RenderTextureCore>& target, PostProcessInfo& ppInfo);
  65. /** Releases the output render target. */
  66. void release(PostProcessInfo& ppInfo);
  67. /** Returns the render texture where the output will be written. */
  68. SPtr<RenderTextureCore> getOutput() const { return mOutput; }
  69. private:
  70. DownsampleParams mParams;
  71. MaterialParamVec2Core mInvTexSize;
  72. MaterialParamTextureCore mInputTexture;
  73. POOLED_RENDER_TEXTURE_DESC mOutputDesc;
  74. SPtr<RenderTextureCore> mOutput;
  75. };
  76. BS_PARAM_BLOCK_BEGIN(EyeAdaptHistogramParams)
  77. BS_PARAM_BLOCK_ENTRY(Vector4I, gPixelOffsetAndSize)
  78. BS_PARAM_BLOCK_ENTRY(Vector2, gHistogramParams)
  79. BS_PARAM_BLOCK_ENTRY(Vector2I, gThreadGroupCount)
  80. BS_PARAM_BLOCK_END
  81. /** Shader that creates a luminance histogram used for eye adaptation. */
  82. class EyeAdaptHistogramMat : public RendererMaterial<EyeAdaptHistogramMat>
  83. {
  84. RMAT_DEF("PPEyeAdaptHistogram.bsl");
  85. public:
  86. EyeAdaptHistogramMat();
  87. /** Executes the post-process effect with the provided parameters. */
  88. void execute(PostProcessInfo& ppInfo);
  89. /** Releases the output render target. */
  90. void release(PostProcessInfo& ppInfo);
  91. /** Returns the render texture where the output was written. */
  92. SPtr<RenderTextureCore> getOutput() const { return mOutput; }
  93. /** Calculates the number of thread groups that need to execute to cover the provided render target. */
  94. static Vector2I getThreadGroupCount(const SPtr<RenderTextureCore>& target);
  95. /**
  96. * Returns a vector containing scale and offset (in that order) that will be applied to luminance values
  97. * to determine their position in the histogram.
  98. */
  99. static Vector2 getHistogramScaleOffset(const PostProcessInfo& ppInfo);
  100. static const UINT32 THREAD_GROUP_SIZE_X = 4;
  101. static const UINT32 THREAD_GROUP_SIZE_Y = 4;
  102. static const UINT32 HISTOGRAM_NUM_TEXELS = (THREAD_GROUP_SIZE_X * THREAD_GROUP_SIZE_Y) / 4;
  103. private:
  104. EyeAdaptHistogramParams mParams;
  105. MaterialParamTextureCore mSceneColor;
  106. MaterialParamLoadStoreTextureCore mOutputTex;
  107. POOLED_RENDER_TEXTURE_DESC mOutputDesc;
  108. SPtr<RenderTextureCore> mOutput;
  109. static const UINT32 LOOP_COUNT_X = 8;
  110. static const UINT32 LOOP_COUNT_Y = 8;
  111. };
  112. BS_PARAM_BLOCK_BEGIN(EyeAdaptHistogramReduceParams)
  113. BS_PARAM_BLOCK_ENTRY(Vector2I, gThreadGroupCount)
  114. BS_PARAM_BLOCK_END
  115. /** Shader that reduces the luminance histograms created by EyeAdaptHistogramMat into a single histogram. */
  116. class EyeAdaptHistogramReduceMat : public RendererMaterial<EyeAdaptHistogramReduceMat>
  117. {
  118. RMAT_DEF("PPEyeAdaptHistogramReduce.bsl");
  119. public:
  120. EyeAdaptHistogramReduceMat();
  121. /** Executes the post-process effect with the provided parameters. */
  122. void execute(PostProcessInfo& ppInfo);
  123. /** Releases the output render target. */
  124. void release(PostProcessInfo& ppInfo);
  125. /** Returns the render texture where the output was written. */
  126. SPtr<RenderTextureCore> getOutput() const { return mOutput; }
  127. private:
  128. EyeAdaptHistogramReduceParams mParams;
  129. MaterialParamTextureCore mHistogramTex;
  130. MaterialParamTextureCore mEyeAdaptationTex;
  131. POOLED_RENDER_TEXTURE_DESC mOutputDesc;
  132. SPtr<RenderTextureCore> mOutput;
  133. };
  134. BS_PARAM_BLOCK_BEGIN(EyeAdaptationParams)
  135. BS_PARAM_BLOCK_ENTRY_ARRAY(Vector4, gEyeAdaptationParams, 3)
  136. BS_PARAM_BLOCK_END
  137. /** Shader that computes the eye adaptation value based on scene luminance. */
  138. class EyeAdaptationMat : public RendererMaterial<EyeAdaptationMat>
  139. {
  140. RMAT_DEF("PPEyeAdaptation.bsl");
  141. public:
  142. EyeAdaptationMat();
  143. /** Executes the post-process effect with the provided parameters. */
  144. void execute(PostProcessInfo& ppInfo, float frameDelta);
  145. private:
  146. EyeAdaptationParams mParams;
  147. MaterialParamTextureCore mReducedHistogramTex;
  148. };
  149. /**
  150. * Renders post-processing effects for the provided render target.
  151. *
  152. * @note Core thread only.
  153. */
  154. class BS_BSRND_EXPORT PostProcessing : public Module<PostProcessing>
  155. {
  156. public:
  157. /** Renders post-processing effects for the provided render target. */
  158. void postProcess(const SPtr<RenderTextureCore>& target, PostProcessInfo& ppInfo, float frameDelta);
  159. private:
  160. DownsampleMat mDownsample;
  161. EyeAdaptHistogramMat mEyeAdaptHistogram;
  162. EyeAdaptHistogramReduceMat mEyeAdaptHistogramReduce;
  163. EyeAdaptationMat mEyeAdaptation;
  164. };
  165. /** @} */
  166. }