BsPostProcessing.h 8.1 KB

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