BsPostProcessing.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 "BsGpuResourcePool.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_ARRAY(Vector2, gOffsets, 4)
  28. BS_PARAM_BLOCK_END
  29. extern DownsampleParamDef gDownsampleParamDef;
  30. /**
  31. * Shader that downsamples a texture to half its size.
  32. *
  33. * @tparam Quality 0 for a 2x2 filtered sample, 1 or higher for 4x4 filtered sample
  34. * @tparam MSAA True if the input texture is multi-sampled. Only first sample will be used, the rest will be
  35. * discarded.
  36. */
  37. template<int Quality, bool MSAA>
  38. class DownsampleMat : public RendererMaterial<DownsampleMat<Quality, MSAA>>
  39. {
  40. RMAT_DEF("PPDownsample.bsl");
  41. public:
  42. DownsampleMat();
  43. /** Renders the post-process effect with the provided parameters. */
  44. void execute(const SPtr<Texture>& target, PostProcessInfo& ppInfo);
  45. /** Releases the output render target. */
  46. void release(PostProcessInfo& ppInfo);
  47. /** Returns the render texture where the output will be written. */
  48. SPtr<RenderTexture> getOutput() const { return mOutput; }
  49. private:
  50. SPtr<GpuParamBlockBuffer> mParamBuffer;
  51. GpuParamTexture mInputTexture;
  52. POOLED_RENDER_TEXTURE_DESC mOutputDesc;
  53. SPtr<RenderTexture> mOutput;
  54. };
  55. /** Contains all variations of the DownsampleMat material. */
  56. class DownsampleMaterials
  57. {
  58. public:
  59. /**
  60. * Executes the appropriate downsampling material.
  61. *
  62. * @param[in] quality Determines quality of the downsampling filer. Specify 0 to use a 2x2 filter block, and
  63. * 1 or higher for a 4x4 filter block.
  64. * @param[in] msaa If true the input texture is assumed to have multiple samples. The downsampling shader
  65. * will discard all samples except the first one.
  66. * @param[in] target Texture to downsample.
  67. * @param[in] ppInfo Information about the current post processing pass.
  68. */
  69. void execute(UINT32 quality, bool msaa, const SPtr<Texture>& target, PostProcessInfo& ppInfo);
  70. /**
  71. * Releases any resources allocated by execute(). Must be called using the same @p quality and @p msaa parameters as
  72. * the corresponding execute() call. @see execute().
  73. */
  74. void release(UINT32 quality, bool msaa, PostProcessInfo& ppInfo);
  75. private:
  76. DownsampleMat<0, false> m0_NoMSAA;
  77. DownsampleMat<0, true> m0_MSAA;
  78. DownsampleMat<1, false> m1_NoMSAA;
  79. DownsampleMat<1, true> m1_MSAA;
  80. };
  81. BS_PARAM_BLOCK_BEGIN(EyeAdaptHistogramParamDef)
  82. BS_PARAM_BLOCK_ENTRY(Vector4I, gPixelOffsetAndSize)
  83. BS_PARAM_BLOCK_ENTRY(Vector2, gHistogramParams)
  84. BS_PARAM_BLOCK_ENTRY(Vector2I, gThreadGroupCount)
  85. BS_PARAM_BLOCK_END
  86. extern EyeAdaptHistogramParamDef gEyeAdaptHistogramParamDef;
  87. /** Shader that creates a luminance histogram used for eye adaptation. */
  88. class EyeAdaptHistogramMat : public RendererMaterial<EyeAdaptHistogramMat>
  89. {
  90. RMAT_DEF("PPEyeAdaptHistogram.bsl");
  91. public:
  92. EyeAdaptHistogramMat();
  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<RenderTexture> getOutput() const { return mOutput; }
  99. /** Calculates the number of thread groups that need to execute to cover the provided render target. */
  100. static Vector2I getThreadGroupCount(const SPtr<RenderTexture>& target);
  101. /**
  102. * Returns a vector containing scale and offset (in that order) that will be applied to luminance values
  103. * to determine their position in the histogram.
  104. */
  105. static Vector2 getHistogramScaleOffset(const PostProcessInfo& ppInfo);
  106. static const UINT32 THREAD_GROUP_SIZE_X = 8;
  107. static const UINT32 THREAD_GROUP_SIZE_Y = 8;
  108. static const UINT32 HISTOGRAM_NUM_TEXELS = (THREAD_GROUP_SIZE_X * THREAD_GROUP_SIZE_Y) / 4;
  109. private:
  110. SPtr<GpuParamBlockBuffer> mParamBuffer;
  111. GpuParamTexture mSceneColor;
  112. GpuParamLoadStoreTexture mOutputTex;
  113. POOLED_RENDER_TEXTURE_DESC mOutputDesc;
  114. SPtr<RenderTexture> mOutput;
  115. static const UINT32 LOOP_COUNT_X = 8;
  116. static const UINT32 LOOP_COUNT_Y = 8;
  117. };
  118. BS_PARAM_BLOCK_BEGIN(EyeAdaptHistogramReduceParamDef)
  119. BS_PARAM_BLOCK_ENTRY(int, gThreadGroupCount)
  120. BS_PARAM_BLOCK_END
  121. extern EyeAdaptHistogramReduceParamDef gEyeAdaptHistogramReduceParamDef;
  122. /** Shader that reduces the luminance histograms created by EyeAdaptHistogramMat into a single histogram. */
  123. class EyeAdaptHistogramReduceMat : public RendererMaterial<EyeAdaptHistogramReduceMat>
  124. {
  125. RMAT_DEF("PPEyeAdaptHistogramReduce.bsl");
  126. public:
  127. EyeAdaptHistogramReduceMat();
  128. /** Executes the post-process effect with the provided parameters. */
  129. void execute(PostProcessInfo& ppInfo);
  130. /** Releases the output render target. */
  131. void release(PostProcessInfo& ppInfo);
  132. /** Returns the render texture where the output was written. */
  133. SPtr<RenderTexture> getOutput() const { return mOutput; }
  134. private:
  135. SPtr<GpuParamBlockBuffer> mParamBuffer;
  136. GpuParamTexture mHistogramTex;
  137. GpuParamTexture mEyeAdaptationTex;
  138. POOLED_RENDER_TEXTURE_DESC mOutputDesc;
  139. SPtr<RenderTexture> mOutput;
  140. };
  141. BS_PARAM_BLOCK_BEGIN(EyeAdaptationParamDef)
  142. BS_PARAM_BLOCK_ENTRY_ARRAY(Vector4, gEyeAdaptationParams, 3)
  143. BS_PARAM_BLOCK_END
  144. extern EyeAdaptationParamDef gEyeAdaptationParamDef;
  145. /** Shader that computes the eye adaptation value based on scene luminance. */
  146. class EyeAdaptationMat : public RendererMaterial<EyeAdaptationMat>
  147. {
  148. RMAT_DEF("PPEyeAdaptation.bsl");
  149. public:
  150. EyeAdaptationMat();
  151. /** Executes the post-process effect with the provided parameters. */
  152. void execute(PostProcessInfo& ppInfo, float frameDelta);
  153. private:
  154. SPtr<GpuParamBlockBuffer> mParamBuffer;
  155. GpuParamTexture mReducedHistogramTex;
  156. };
  157. BS_PARAM_BLOCK_BEGIN(CreateTonemapLUTParamDef)
  158. BS_PARAM_BLOCK_ENTRY_ARRAY(Vector4, gTonemapParams, 2)
  159. BS_PARAM_BLOCK_ENTRY(float, gGammaAdjustment)
  160. BS_PARAM_BLOCK_ENTRY(int, gGammaCorrectionType)
  161. BS_PARAM_BLOCK_ENTRY(Vector3, gSaturation)
  162. BS_PARAM_BLOCK_ENTRY(Vector3, gContrast)
  163. BS_PARAM_BLOCK_ENTRY(Vector3, gGain)
  164. BS_PARAM_BLOCK_ENTRY(Vector3, gOffset)
  165. BS_PARAM_BLOCK_END
  166. extern CreateTonemapLUTParamDef gCreateTonemapLUTParamDef;
  167. BS_PARAM_BLOCK_BEGIN(WhiteBalanceParamDef)
  168. BS_PARAM_BLOCK_ENTRY(float, gWhiteTemp)
  169. BS_PARAM_BLOCK_ENTRY(float, gWhiteOffset)
  170. BS_PARAM_BLOCK_END
  171. extern WhiteBalanceParamDef gWhiteBalanceParamDef;
  172. /**
  173. * Shader that creates a 3D lookup texture that is used to apply tonemapping, color grading, white balancing and gamma
  174. * correction.
  175. */
  176. class CreateTonemapLUTMat : public RendererMaterial<CreateTonemapLUTMat>
  177. {
  178. RMAT_DEF("PPCreateTonemapLUT.bsl");
  179. public:
  180. CreateTonemapLUTMat();
  181. /** Executes the post-process effect with the provided parameters. */
  182. void execute(PostProcessInfo& ppInfo);
  183. /** Releases the output render target. */
  184. void release(PostProcessInfo& ppInfo);
  185. /** Size of the 3D color lookup table. */
  186. static const UINT32 LUT_SIZE = 32;
  187. private:
  188. SPtr<GpuParamBlockBuffer> mParamBuffer;
  189. SPtr<GpuParamBlockBuffer> mWhiteBalanceParamBuffer;
  190. GpuParamLoadStoreTexture mOutputTex;
  191. };
  192. BS_PARAM_BLOCK_BEGIN(TonemappingParamDef)
  193. BS_PARAM_BLOCK_ENTRY(float, gRawGamma)
  194. BS_PARAM_BLOCK_ENTRY(float, gManualExposureScale)
  195. BS_PARAM_BLOCK_ENTRY(int, gNumSamples)
  196. BS_PARAM_BLOCK_END
  197. extern TonemappingParamDef gTonemappingParamDef;
  198. /** Shader that applies tonemapping and converts a HDR image into a LDR image. */
  199. template<bool GammaOnly, bool AutoExposure, bool MSAA>
  200. class TonemappingMat : public RendererMaterial<TonemappingMat<GammaOnly, AutoExposure, MSAA>>
  201. {
  202. RMAT_DEF("PPTonemapping.bsl");
  203. public:
  204. TonemappingMat();
  205. /** Executes the post-process effect with the provided parameters. */
  206. void execute(const SPtr<Texture>& sceneColor, const SPtr<RenderTarget>& outputRT, const Rect2& outputRect,
  207. PostProcessInfo& ppInfo);
  208. private:
  209. SPtr<GpuParamBlockBuffer> mParamBuffer;
  210. GpuParamTexture mInputTex;
  211. GpuParamTexture mColorLUT;
  212. GpuParamTexture mEyeAdaptationTex;
  213. };
  214. /** Container for all variations of the TonemappingMat material. */
  215. class TonemappingMaterials
  216. {
  217. public:
  218. /**
  219. * Finds a valid tonemapping material according to the requested parameters and executes it.
  220. *
  221. * @param[in] gammaOnly If true no color correction, white balancing or curve tonemapping will take place.
  222. * Instead the image will only be scaled by the exposure value and gamma corrected.
  223. * @param[in] autoExposure If true, the automatically calculated eye-adapatation exposure value will be used
  224. * as the exposure scale. If false, the user provided value will be used instead.
  225. * @param[in] MSAA True if the input texture has multiple samples. This will ensure that the samples
  226. * are resolved properly into a non-MSAA output texture.
  227. * @param[in] sceneColor Texture to apply tonemapping to.
  228. * @param[in] outputRT Render target to write the results to.
  229. * @param[in] outputRect Normalized rectangle determining to which part of the output texture to write to.
  230. * @param[in] ppInfo Information about the current post processing pass.
  231. */
  232. void execute(bool gammaOnly, bool autoExposure, bool MSAA, const SPtr<Texture>& sceneColor,
  233. const SPtr<RenderTarget>& outputRT, const Rect2& outputRect, PostProcessInfo& ppInfo);
  234. private:
  235. TonemappingMat<false, false, false> mFFF;
  236. TonemappingMat<false, false, true> mFFT;
  237. TonemappingMat<false, true, false> mFTF;
  238. TonemappingMat<false, true, true> mFTT;
  239. TonemappingMat<true, false, false> mTFF;
  240. TonemappingMat<true, false, true> mTFT;
  241. TonemappingMat<true, true, false> mTTF;
  242. TonemappingMat<true, true, true> mTTT;
  243. };
  244. const int MAX_BLUR_SAMPLES = 128;
  245. BS_PARAM_BLOCK_BEGIN(GaussianBlurParamDef)
  246. BS_PARAM_BLOCK_ENTRY_ARRAY(Vector2, gSampleOffsets, MAX_BLUR_SAMPLES)
  247. BS_PARAM_BLOCK_ENTRY_ARRAY(float, gSampleWeights, MAX_BLUR_SAMPLES)
  248. BS_PARAM_BLOCK_ENTRY(int, gNumSamples)
  249. BS_PARAM_BLOCK_END
  250. extern GaussianBlurParamDef gGaussianBlurParamDef;
  251. /** Shader that perform Gaussian blur filtering on the provided texture. */
  252. class GaussianBlurMat : public RendererMaterial<GaussianBlurMat>
  253. {
  254. // Direction of the Gaussian filter pass
  255. enum Direction
  256. {
  257. DirVertical,
  258. DirHorizontal
  259. };
  260. RMAT_DEF("PPGaussianBlur.bsl");
  261. public:
  262. GaussianBlurMat();
  263. /**
  264. * Renders the post-process effect with the provided parameters.
  265. *
  266. * @param[in] source Input texture to blur.
  267. * @param[in] filterSize Size of the blurring filter, in percent of the source texture. In range [0, 1].
  268. * @param[in] destination Output texture to which to write the blurred image to.
  269. */
  270. void execute(const SPtr<Texture>& source, float filterSize, const SPtr<RenderTexture>& destination);
  271. private:
  272. /** Calculates weights and offsets for the standard distribution of the specified filter size. */
  273. static UINT32 calcStdDistribution(float filterRadius, std::array<float, MAX_BLUR_SAMPLES>& weights,
  274. std::array<float, MAX_BLUR_SAMPLES>& offsets);
  275. /** Calculates the radius of the blur kernel depending on the source texture size and provided scale. */
  276. static float calcKernelRadius(const SPtr<Texture>& source, float scale, Direction filterDir);
  277. SPtr<GpuParamBlockBuffer> mParamBuffer;
  278. GpuParamTexture mInputTexture;
  279. };
  280. /**
  281. * Renders post-processing effects for the provided render target.
  282. *
  283. * @note Core thread only.
  284. */
  285. class PostProcessing : public Module<PostProcessing>
  286. {
  287. public:
  288. /**
  289. * Renders post-processing effects for the provided render target. Resolves provided scene color texture into the
  290. * view's final output render target. Once the method exits, final render target is guaranteed to be currently
  291. * bound for rendering.
  292. */
  293. void postProcess(RendererView* viewInfo, const SPtr<RenderTargets>& renderTargets, float frameDelta);
  294. private:
  295. DownsampleMaterials mDownsample;
  296. EyeAdaptHistogramMat mEyeAdaptHistogram;
  297. EyeAdaptHistogramReduceMat mEyeAdaptHistogramReduce;
  298. EyeAdaptationMat mEyeAdaptation;
  299. CreateTonemapLUTMat mCreateLUT;
  300. TonemappingMaterials mTonemapping;
  301. };
  302. /** @} */
  303. }}