BsPostProcessing.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 { namespace ct
  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<RenderTexture>& 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<RenderTexture> getOutput() const { return mOutput; }
  42. private:
  43. SPtr<GpuParamBlockBuffer> mParamBuffer;
  44. GpuParamTexture mInputTexture;
  45. POOLED_RENDER_TEXTURE_DESC mOutputDesc;
  46. SPtr<RenderTexture> 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<RenderTexture> 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<RenderTexture>& 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<GpuParamBlockBuffer> mParamBuffer;
  78. GpuParamTexture mSceneColor;
  79. GpuParamLoadStoreTexture mOutputTex;
  80. POOLED_RENDER_TEXTURE_DESC mOutputDesc;
  81. SPtr<RenderTexture> 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<RenderTexture> getOutput() const { return mOutput; }
  101. private:
  102. SPtr<GpuParamBlockBuffer> mParamBuffer;
  103. GpuParamTexture mHistogramTex;
  104. GpuParamTexture mEyeAdaptationTex;
  105. POOLED_RENDER_TEXTURE_DESC mOutputDesc;
  106. SPtr<RenderTexture> 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<GpuParamBlockBuffer> mParamBuffer;
  122. GpuParamTexture 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<GpuParamBlockBuffer> mParamBuffer;
  156. SPtr<GpuParamBlockBuffer> mWhiteBalanceParamBuffer;
  157. GpuParamLoadStoreTexture mOutputTex;
  158. };
  159. BS_PARAM_BLOCK_BEGIN(TonemappingParamDef)
  160. BS_PARAM_BLOCK_ENTRY(float, gRawGamma)
  161. BS_PARAM_BLOCK_ENTRY(float, gManualExposureScale)
  162. BS_PARAM_BLOCK_END
  163. extern TonemappingParamDef gTonemappingParamDef;
  164. /** Shader that applies tonemapping and converts a HDR image into a LDR image. */
  165. template<bool GammaOnly, bool AutoExposure>
  166. class TonemappingMat : public RendererMaterial<TonemappingMat<GammaOnly, AutoExposure>>
  167. {
  168. RMAT_DEF("PPTonemapping.bsl");
  169. public:
  170. TonemappingMat();
  171. /** Executes the post-process effect with the provided parameters. */
  172. void execute(const SPtr<RenderTexture>& sceneColor, const SPtr<Viewport>& outputViewport,
  173. PostProcessInfo& ppInfo);
  174. private:
  175. SPtr<GpuParamBlockBuffer> mParamBuffer;
  176. GpuParamTexture mInputTex;
  177. GpuParamTexture mColorLUT;
  178. GpuParamTexture mEyeAdaptationTex;
  179. };
  180. /**
  181. * Renders post-processing effects for the provided render target.
  182. *
  183. * @note Core thread only.
  184. */
  185. class BS_BSRND_EXPORT PostProcessing : public Module<PostProcessing>
  186. {
  187. public:
  188. /** Renders post-processing effects for the provided render target. */
  189. void postProcess(const SPtr<RenderTexture>& sceneColor, const Camera* camera,
  190. PostProcessInfo& ppInfo, float frameDelta);
  191. private:
  192. DownsampleMat mDownsample;
  193. EyeAdaptHistogramMat mEyeAdaptHistogram;
  194. EyeAdaptHistogramReduceMat mEyeAdaptHistogramReduce;
  195. EyeAdaptationMat mEyeAdaptation;
  196. CreateTonemapLUTMat mCreateLUT;
  197. TonemappingMat<false, true> mTonemapping_AE;
  198. TonemappingMat<true, true> mTonemapping_AE_GO;
  199. TonemappingMat<false, false> mTonemapping;
  200. TonemappingMat<true, false> mTonemapping_GO;
  201. };
  202. /** @} */
  203. }}