BsPostProcessing.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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. struct RendererViewTargetData;
  12. /** @addtogroup RenderBeast
  13. * @{
  14. */
  15. /** Contains per-camera data used by post process effects. */
  16. struct PostProcessInfo
  17. {
  18. SPtr<StandardPostProcessSettings> settings;
  19. bool settingDirty = true;
  20. SPtr<PooledRenderTexture> downsampledSceneTex;
  21. SPtr<PooledRenderTexture> histogramTex;
  22. SPtr<PooledRenderTexture> histogramReduceTex;
  23. SPtr<PooledRenderTexture> eyeAdaptationTex[2];
  24. SPtr<PooledRenderTexture> colorLUT;
  25. INT32 lastEyeAdaptationTex = 0;
  26. };
  27. BS_PARAM_BLOCK_BEGIN(DownsampleParamDef)
  28. BS_PARAM_BLOCK_ENTRY_ARRAY(Vector2, gOffsets, 4)
  29. BS_PARAM_BLOCK_END
  30. extern DownsampleParamDef gDownsampleParamDef;
  31. /**
  32. * Shader that downsamples a texture to half its size.
  33. *
  34. * @tparam Quality 0 for a 2x2 filtered sample, 1 or higher for 4x4 filtered sample
  35. * @tparam MSAA True if the input texture is multi-sampled. Only first sample will be used, the rest will be
  36. * discarded.
  37. */
  38. template<int Quality, bool MSAA>
  39. class DownsampleMat : public RendererMaterial<DownsampleMat<Quality, MSAA>>
  40. {
  41. RMAT_DEF("PPDownsample.bsl");
  42. public:
  43. DownsampleMat();
  44. /** Renders the post-process effect with the provided parameters. */
  45. void execute(const SPtr<Texture>& target, PostProcessInfo& ppInfo);
  46. /** Releases the output render target. */
  47. void release(PostProcessInfo& ppInfo);
  48. /** Returns the render texture where the output will be written. */
  49. SPtr<RenderTexture> getOutput() const { return mOutput; }
  50. private:
  51. SPtr<GpuParamBlockBuffer> mParamBuffer;
  52. GpuParamTexture mInputTexture;
  53. POOLED_RENDER_TEXTURE_DESC mOutputDesc;
  54. SPtr<RenderTexture> mOutput;
  55. };
  56. /** Contains all variations of the DownsampleMat material. */
  57. class DownsampleMaterials
  58. {
  59. public:
  60. /**
  61. * Executes the appropriate downsampling material.
  62. *
  63. * @param[in] quality Determines quality of the downsampling filer. Specify 0 to use a 2x2 filter block, and
  64. * 1 or higher for a 4x4 filter block.
  65. * @param[in] msaa If true the input texture is assumed to have multiple samples. The downsampling shader
  66. * will discard all samples except the first one.
  67. * @param[in] target Texture to downsample.
  68. * @param[in] ppInfo Information about the current post processing pass.
  69. */
  70. void execute(UINT32 quality, bool msaa, const SPtr<Texture>& target, PostProcessInfo& ppInfo);
  71. /**
  72. * Releases any resources allocated by execute(). Must be called using the same @p quality and @p msaa parameters as
  73. * the corresponding execute() call. @see execute().
  74. */
  75. void release(UINT32 quality, bool msaa, PostProcessInfo& ppInfo);
  76. private:
  77. DownsampleMat<0, false> m0_NoMSAA;
  78. DownsampleMat<0, true> m0_MSAA;
  79. DownsampleMat<1, false> m1_NoMSAA;
  80. DownsampleMat<1, true> m1_MSAA;
  81. };
  82. BS_PARAM_BLOCK_BEGIN(EyeAdaptHistogramParamDef)
  83. BS_PARAM_BLOCK_ENTRY(Vector4I, gPixelOffsetAndSize)
  84. BS_PARAM_BLOCK_ENTRY(Vector2, gHistogramParams)
  85. BS_PARAM_BLOCK_ENTRY(Vector2I, gThreadGroupCount)
  86. BS_PARAM_BLOCK_END
  87. extern EyeAdaptHistogramParamDef gEyeAdaptHistogramParamDef;
  88. /** Shader that creates a luminance histogram used for eye adaptation. */
  89. class EyeAdaptHistogramMat : public RendererMaterial<EyeAdaptHistogramMat>
  90. {
  91. RMAT_DEF("PPEyeAdaptHistogram.bsl");
  92. public:
  93. EyeAdaptHistogramMat();
  94. /** Executes the post-process effect with the provided parameters. */
  95. void execute(PostProcessInfo& ppInfo);
  96. /** Releases the output render target. */
  97. void release(PostProcessInfo& ppInfo);
  98. /** Returns the render texture where the output was written. */
  99. SPtr<RenderTexture> getOutput() const { return mOutput; }
  100. /** Calculates the number of thread groups that need to execute to cover the provided render target. */
  101. static Vector2I getThreadGroupCount(const SPtr<RenderTexture>& target);
  102. /**
  103. * Returns a vector containing scale and offset (in that order) that will be applied to luminance values
  104. * to determine their position in the histogram.
  105. */
  106. static Vector2 getHistogramScaleOffset(const PostProcessInfo& ppInfo);
  107. static const UINT32 THREAD_GROUP_SIZE_X = 8;
  108. static const UINT32 THREAD_GROUP_SIZE_Y = 8;
  109. static const UINT32 HISTOGRAM_NUM_TEXELS = (THREAD_GROUP_SIZE_X * THREAD_GROUP_SIZE_Y) / 4;
  110. private:
  111. SPtr<GpuParamBlockBuffer> mParamBuffer;
  112. GpuParamTexture mSceneColor;
  113. GpuParamLoadStoreTexture mOutputTex;
  114. POOLED_RENDER_TEXTURE_DESC mOutputDesc;
  115. SPtr<RenderTexture> mOutput;
  116. static const UINT32 LOOP_COUNT_X = 8;
  117. static const UINT32 LOOP_COUNT_Y = 8;
  118. };
  119. BS_PARAM_BLOCK_BEGIN(EyeAdaptHistogramReduceParamDef)
  120. BS_PARAM_BLOCK_ENTRY(int, gThreadGroupCount)
  121. BS_PARAM_BLOCK_END
  122. extern EyeAdaptHistogramReduceParamDef gEyeAdaptHistogramReduceParamDef;
  123. /** Shader that reduces the luminance histograms created by EyeAdaptHistogramMat into a single histogram. */
  124. class EyeAdaptHistogramReduceMat : public RendererMaterial<EyeAdaptHistogramReduceMat>
  125. {
  126. RMAT_DEF("PPEyeAdaptHistogramReduce.bsl");
  127. public:
  128. EyeAdaptHistogramReduceMat();
  129. /** Executes the post-process effect with the provided parameters. */
  130. void execute(PostProcessInfo& ppInfo);
  131. /** Releases the output render target. */
  132. void release(PostProcessInfo& ppInfo);
  133. /** Returns the render texture where the output was written. */
  134. SPtr<RenderTexture> getOutput() const { return mOutput; }
  135. private:
  136. SPtr<GpuParamBlockBuffer> mParamBuffer;
  137. GpuParamTexture mHistogramTex;
  138. GpuParamTexture mEyeAdaptationTex;
  139. POOLED_RENDER_TEXTURE_DESC mOutputDesc;
  140. SPtr<RenderTexture> mOutput;
  141. };
  142. BS_PARAM_BLOCK_BEGIN(EyeAdaptationParamDef)
  143. BS_PARAM_BLOCK_ENTRY_ARRAY(Vector4, gEyeAdaptationParams, 3)
  144. BS_PARAM_BLOCK_END
  145. extern EyeAdaptationParamDef gEyeAdaptationParamDef;
  146. /** Shader that computes the eye adaptation value based on scene luminance. */
  147. class EyeAdaptationMat : public RendererMaterial<EyeAdaptationMat>
  148. {
  149. RMAT_DEF("PPEyeAdaptation.bsl");
  150. public:
  151. EyeAdaptationMat();
  152. /** Executes the post-process effect with the provided parameters. */
  153. void execute(PostProcessInfo& ppInfo, float frameDelta);
  154. private:
  155. SPtr<GpuParamBlockBuffer> mParamBuffer;
  156. GpuParamTexture mReducedHistogramTex;
  157. };
  158. BS_PARAM_BLOCK_BEGIN(CreateTonemapLUTParamDef)
  159. BS_PARAM_BLOCK_ENTRY_ARRAY(Vector4, gTonemapParams, 2)
  160. BS_PARAM_BLOCK_ENTRY(float, gGammaAdjustment)
  161. BS_PARAM_BLOCK_ENTRY(int, gGammaCorrectionType)
  162. BS_PARAM_BLOCK_ENTRY(Vector3, gSaturation)
  163. BS_PARAM_BLOCK_ENTRY(Vector3, gContrast)
  164. BS_PARAM_BLOCK_ENTRY(Vector3, gGain)
  165. BS_PARAM_BLOCK_ENTRY(Vector3, gOffset)
  166. BS_PARAM_BLOCK_END
  167. extern CreateTonemapLUTParamDef gCreateTonemapLUTParamDef;
  168. BS_PARAM_BLOCK_BEGIN(WhiteBalanceParamDef)
  169. BS_PARAM_BLOCK_ENTRY(float, gWhiteTemp)
  170. BS_PARAM_BLOCK_ENTRY(float, gWhiteOffset)
  171. BS_PARAM_BLOCK_END
  172. extern WhiteBalanceParamDef gWhiteBalanceParamDef;
  173. /**
  174. * Shader that creates a 3D lookup texture that is used to apply tonemapping, color grading, white balancing and gamma
  175. * correction.
  176. */
  177. class CreateTonemapLUTMat : public RendererMaterial<CreateTonemapLUTMat>
  178. {
  179. RMAT_DEF("PPCreateTonemapLUT.bsl");
  180. public:
  181. CreateTonemapLUTMat();
  182. /** Executes the post-process effect with the provided parameters. */
  183. void execute(PostProcessInfo& ppInfo);
  184. /** Releases the output render target. */
  185. void release(PostProcessInfo& ppInfo);
  186. /** Size of the 3D color lookup table. */
  187. static const UINT32 LUT_SIZE = 32;
  188. private:
  189. SPtr<GpuParamBlockBuffer> mParamBuffer;
  190. SPtr<GpuParamBlockBuffer> mWhiteBalanceParamBuffer;
  191. GpuParamLoadStoreTexture mOutputTex;
  192. };
  193. BS_PARAM_BLOCK_BEGIN(TonemappingParamDef)
  194. BS_PARAM_BLOCK_ENTRY(float, gRawGamma)
  195. BS_PARAM_BLOCK_ENTRY(float, gManualExposureScale)
  196. BS_PARAM_BLOCK_ENTRY(int, gNumSamples)
  197. BS_PARAM_BLOCK_END
  198. extern TonemappingParamDef gTonemappingParamDef;
  199. /** Shader that applies tonemapping and converts a HDR image into a LDR image. */
  200. template<bool GammaOnly, bool AutoExposure, bool MSAA>
  201. class TonemappingMat : public RendererMaterial<TonemappingMat<GammaOnly, AutoExposure, MSAA>>
  202. {
  203. RMAT_DEF("PPTonemapping.bsl");
  204. public:
  205. TonemappingMat();
  206. /** Executes the post-process effect with the provided parameters. */
  207. void execute(const SPtr<Texture>& sceneColor, const SPtr<RenderTarget>& outputRT, const Rect2& outputRect,
  208. PostProcessInfo& ppInfo);
  209. private:
  210. SPtr<GpuParamBlockBuffer> mParamBuffer;
  211. GpuParamTexture mInputTex;
  212. GpuParamTexture mColorLUT;
  213. GpuParamTexture mEyeAdaptationTex;
  214. };
  215. /** Container for all variations of the TonemappingMat material. */
  216. class TonemappingMaterials
  217. {
  218. public:
  219. /**
  220. * Finds a valid tonemapping material according to the requested parameters and executes it.
  221. *
  222. * @param[in] gammaOnly If true no color correction, white balancing or curve tonemapping will take place.
  223. * Instead the image will only be scaled by the exposure value and gamma corrected.
  224. * @param[in] autoExposure If true, the automatically calculated eye-adapatation exposure value will be used
  225. * as the exposure scale. If false, the user provided value will be used instead.
  226. * @param[in] MSAA True if the input texture has multiple samples. This will ensure that the samples
  227. * are resolved properly into a non-MSAA output texture.
  228. * @param[in] sceneColor Texture to apply tonemapping to.
  229. * @param[in] outputRT Render target to write the results to.
  230. * @param[in] outputRect Normalized rectangle determining to which part of the output texture to write to.
  231. * @param[in] ppInfo Information about the current post processing pass.
  232. */
  233. void execute(bool gammaOnly, bool autoExposure, bool MSAA, const SPtr<Texture>& sceneColor,
  234. const SPtr<RenderTarget>& outputRT, const Rect2& outputRect, PostProcessInfo& ppInfo);
  235. private:
  236. TonemappingMat<false, false, false> mFFF;
  237. TonemappingMat<false, false, true> mFFT;
  238. TonemappingMat<false, true, false> mFTF;
  239. TonemappingMat<false, true, true> mFTT;
  240. TonemappingMat<true, false, false> mTFF;
  241. TonemappingMat<true, false, true> mTFT;
  242. TonemappingMat<true, true, false> mTTF;
  243. TonemappingMat<true, true, true> mTTT;
  244. };
  245. const int MAX_BLUR_SAMPLES = 128;
  246. BS_PARAM_BLOCK_BEGIN(GaussianBlurParamDef)
  247. BS_PARAM_BLOCK_ENTRY_ARRAY(Vector4, gSampleOffsets, (MAX_BLUR_SAMPLES + 1) / 2)
  248. BS_PARAM_BLOCK_ENTRY_ARRAY(Vector4, gSampleWeights, (MAX_BLUR_SAMPLES + 3) / 4)
  249. BS_PARAM_BLOCK_ENTRY(int, gNumSamples)
  250. BS_PARAM_BLOCK_END
  251. extern GaussianBlurParamDef gGaussianBlurParamDef;
  252. /** Shader that performs Gaussian blur filtering on the provided texture. */
  253. class GaussianBlurMat : public RendererMaterial<GaussianBlurMat>
  254. {
  255. // Direction of the Gaussian filter pass
  256. enum Direction
  257. {
  258. DirVertical,
  259. DirHorizontal
  260. };
  261. RMAT_DEF("PPGaussianBlur.bsl");
  262. public:
  263. GaussianBlurMat();
  264. /**
  265. * Renders the post-process effect with the provided parameters.
  266. *
  267. * @param[in] source Input texture to blur.
  268. * @param[in] filterSize Size of the blurring filter, in percent of the source texture. In range [0, 1].
  269. * @param[in] destination Output texture to which to write the blurred image to.
  270. */
  271. void execute(const SPtr<Texture>& source, float filterSize, const SPtr<RenderTexture>& destination);
  272. private:
  273. /** Calculates weights and offsets for the standard distribution of the specified filter size. */
  274. static UINT32 calcStdDistribution(float filterRadius, std::array<float, MAX_BLUR_SAMPLES>& weights,
  275. std::array<float, MAX_BLUR_SAMPLES>& offsets);
  276. /** Calculates the radius of the blur kernel depending on the source texture size and provided scale. */
  277. static float calcKernelRadius(const SPtr<Texture>& source, float scale, Direction filterDir);
  278. SPtr<GpuParamBlockBuffer> mParamBuffer;
  279. GpuParamTexture mInputTexture;
  280. };
  281. BS_PARAM_BLOCK_BEGIN(GaussianDOFParamDef)
  282. BS_PARAM_BLOCK_ENTRY(float, gNearBlurPlane)
  283. BS_PARAM_BLOCK_ENTRY(float, gFarBlurPlane)
  284. BS_PARAM_BLOCK_ENTRY(float, gInvNearBlurRange)
  285. BS_PARAM_BLOCK_ENTRY(float, gInvFarBlurRange)
  286. BS_PARAM_BLOCK_ENTRY(Vector2, gHalfPixelOffset)
  287. BS_PARAM_BLOCK_END
  288. extern GaussianDOFParamDef sGaussianDOFParamDef;
  289. /** Common interface for all variations of GaussianDOFSeparateMat. */
  290. class IGaussianDOFSeparateMat
  291. {
  292. public:
  293. virtual ~IGaussianDOFSeparateMat() { }
  294. /**
  295. * Renders the post-process effect with the provided parameters.
  296. *
  297. * @param[in] color Input color texture to process.
  298. * @param[in] depth Input depth buffer texture that will be used for determining pixel depth.
  299. * @param[in] view View through which the depth of field effect is viewed.
  300. * @param[in] settings Settings used to control depth of field rendering.
  301. */
  302. virtual void execute(const SPtr<Texture>& color, const SPtr<Texture>& depth, const RendererView& view,
  303. const DepthOfFieldSettings& settings) = 0;
  304. /**
  305. * Returns the texture generated after the shader was executed. Only valid to call this in-between calls to
  306. * execute() & release(), with @p idx value 0 or 1.
  307. */
  308. virtual SPtr<PooledRenderTexture> getOutput(UINT32 idx) = 0;
  309. /**
  310. * Releases the interally allocated output render textures. Must be called after each call to execute(), when the
  311. * caller is done using the textures.
  312. */
  313. virtual void release() = 0;
  314. };
  315. /**
  316. * Shader that masks pixels from the input color texture into one or two output textures. The masking is done by
  317. * determining if the pixel falls into near or far unfocused plane, as determined by depth-of-field parameters. User
  318. * can pick whether to output pixels just on the near plane, just on the far plane, or both.
  319. *
  320. * @tparam Near If true, near plane pixels are output to the first render target.
  321. * @tparam Far If true, far plane pixels are output to the first render target. If @p Near is also enabled, the
  322. * pixels are output to the second render target instead.
  323. */
  324. template<bool Near, bool Far>
  325. class GaussianDOFSeparateMat : public IGaussianDOFSeparateMat, public RendererMaterial<GaussianDOFSeparateMat<Near, Far>>
  326. {
  327. RMAT_DEF("PPGaussianDOFSeparate.bsl");
  328. public:
  329. GaussianDOFSeparateMat();
  330. /** @copydoc IGaussianDOFSeparateMat::execute() */
  331. void execute(const SPtr<Texture>& color, const SPtr<Texture>& depth, const RendererView& view,
  332. const DepthOfFieldSettings& settings) override;
  333. /** @copydoc IGaussianDOFSeparateMat::getOutput() */
  334. SPtr<PooledRenderTexture> getOutput(UINT32 idx) override;
  335. /** @copydoc IGaussianDOFSeparateMat::release() */
  336. void release() override;
  337. private:
  338. SPtr<GpuParamBlockBuffer> mParamBuffer;
  339. GpuParamTexture mColorTexture;
  340. GpuParamTexture mDepthTexture;
  341. SPtr<PooledRenderTexture> mOutput0;
  342. SPtr<PooledRenderTexture> mOutput1;
  343. };
  344. /** Common interface for all variations of GaussianDOFCombineMat. */
  345. class IGaussianDOFCombineMat
  346. {
  347. public:
  348. virtual ~IGaussianDOFCombineMat() { }
  349. /**
  350. * Renders the post-process effect with the provided parameters.
  351. *
  352. * @param[in] focused Input texture containing focused (default) scene color.
  353. * @param[in] near Input texture containing filtered (blurred) values for the unfocused foreground area.
  354. * Can be null if no near plane needs to be blended.
  355. * @param[in] far Input texture containing filtered (blurred) values for the unfocused background area.
  356. * Can be null if no far plane needs to be blended.
  357. * @param[in] depth Input depth buffer texture that will be used for determining pixel depth.
  358. * @param[in} output Texture to output the results to.
  359. * @param[in] view View through which the depth of field effect is viewed.
  360. * @param[in] settings Settings used to control depth of field rendering.
  361. */
  362. virtual void execute(const SPtr<Texture>& focused, const SPtr<Texture>& near, const SPtr<Texture>& far,
  363. const SPtr<Texture>& depth, const SPtr<RenderTarget>& output, const RendererView& view,
  364. const DepthOfFieldSettings& settings) = 0;
  365. };
  366. /**
  367. * Shader that combines pixels for near unfocused, focused and far unfocused planes, as calculated by
  368. * GaussianDOFSeparateMat. Outputs final depth-of-field filtered image.
  369. *
  370. * @tparam Near If true, near plane pixels are read from the near plane texture, otherwise near plane is assumed
  371. * not to exist.
  372. * @tparam Far If true, far plane pixels are read from the far plane texture, otherwise far plane is assumed not
  373. * to exist.
  374. */
  375. template<bool Near, bool Far>
  376. class GaussianDOFCombineMat : public IGaussianDOFCombineMat, public RendererMaterial<GaussianDOFCombineMat<Near, Far>>
  377. {
  378. RMAT_DEF("PPGaussianDOFCombine.bsl");
  379. public:
  380. GaussianDOFCombineMat();
  381. void execute(const SPtr<Texture>& focused, const SPtr<Texture>& near, const SPtr<Texture>& far,
  382. const SPtr<Texture>& depth, const SPtr<RenderTarget>& output, const RendererView& view,
  383. const DepthOfFieldSettings& settings) override;
  384. private:
  385. SPtr<GpuParamBlockBuffer> mParamBuffer;
  386. GpuParamTexture mFocusedTexture;
  387. GpuParamTexture mNearTexture;
  388. GpuParamTexture mFarTexture;
  389. GpuParamTexture mDepthTexture;
  390. };
  391. /** Performs Gaussian depth of field effect with the help of various related shaders. */
  392. class GaussianDOF
  393. {
  394. public:
  395. /**
  396. * Executes the depth of field effect on the provided scene color texture.
  397. *
  398. * @param[in] sceneColor Input texture containing scene color.
  399. * @param[in] sceneDepth Input depth buffer texture that will be used for determining pixel depth.
  400. * @param[in} output Texture to output the results to.
  401. * @param[in] view View through which the depth of field effect is viewed.
  402. * @param[in] settings Settings used to control depth of field rendering.
  403. */
  404. void execute(const SPtr<Texture>& sceneColor, const SPtr<Texture>& sceneDepth, const SPtr<RenderTarget>& output,
  405. const RendererView& view, const DepthOfFieldSettings& settings);
  406. /** Checks does the depth of field effect need to execute. */
  407. static bool requiresDOF(const DepthOfFieldSettings& settings);
  408. private:
  409. GaussianDOFSeparateMat<true, true> mSeparateNF;
  410. GaussianDOFSeparateMat<false, true> mSeparateF;
  411. GaussianDOFSeparateMat<true, false> mSeparateN;
  412. GaussianDOFCombineMat<true, true> mCombineNF;
  413. GaussianDOFCombineMat<false, true> mCombineF;
  414. GaussianDOFCombineMat<true, false> mCombineN;
  415. GaussianBlurMat mBlur;
  416. };
  417. /** Shader that calculates a single level of the hierarchical Z mipmap chain. */
  418. class BuildHiZMat : public RendererMaterial<BuildHiZMat>
  419. {
  420. RMAT_DEF("PPBuildHiZ.bsl");
  421. public:
  422. BuildHiZMat();
  423. /**
  424. * Renders the post-process effect with the provided parameters.
  425. *
  426. * @param[in] source Input depth texture to use as the source.
  427. * @param[in] srcMip Mip level to read from the @p source texture.
  428. * @param[in] srcRect Rectangle in normalized coordinates, describing from which portion of the source
  429. * texture to read the input.
  430. * @param[in] dstRect Destination rectangle to limit the writes to.
  431. * @param[in] output Output target to which to write to results.
  432. */
  433. void execute(const SPtr<Texture>& source, UINT32 srcMip, const Rect2& srcRect, const Rect2& dstRect,
  434. const SPtr<RenderTexture>& output);
  435. private:
  436. GpuParamTexture mInputTexture;
  437. };
  438. /** Builds a hierarchical Z mipmap chain from the source depth texture. */
  439. class BuildHiZ
  440. {
  441. public:
  442. /**
  443. * Renders the post-process effect with the provided parameters.
  444. *
  445. * @param[in] viewInfo Information about the view we're rendering from.
  446. * @param[in] source Input depth texture to use as the source.
  447. * @param[in] output Output target to which to write to results. This texture should be created using the
  448. * descriptor returned by getHiZTextureDesc().
  449. */
  450. void execute(const RendererViewTargetData& viewInfo, const SPtr<Texture>& source, const SPtr<Texture>& output);
  451. /** Generates a descriptor that can be used for creating a texture to contain the HiZ mipmap chain. */
  452. static POOLED_RENDER_TEXTURE_DESC getHiZTextureDesc(UINT32 viewWidth, UINT32 viewHeight);
  453. private:
  454. BuildHiZMat mHiZMat;
  455. };
  456. BS_PARAM_BLOCK_BEGIN(FXAAParamDef)
  457. BS_PARAM_BLOCK_ENTRY(Vector2, gInvTexSize)
  458. BS_PARAM_BLOCK_END
  459. extern FXAAParamDef gFXAAParamDef;
  460. /** Shader that performs Fast Approximate anti-aliasing. */
  461. class FXAAMat : public RendererMaterial<FXAAMat>
  462. {
  463. RMAT_DEF("PPFXAA.bsl");
  464. public:
  465. FXAAMat();
  466. /**
  467. * Renders the post-process effect with the provided parameters.
  468. *
  469. * @param[in] source Input texture to apply FXAA to.
  470. * @param[in] destination Output target to which to write the antialiased image to.
  471. */
  472. void execute(const SPtr<Texture>& source, const SPtr<RenderTarget>& destination);
  473. private:
  474. SPtr<GpuParamBlockBuffer> mParamBuffer;
  475. GpuParamTexture mInputTexture;
  476. };
  477. BS_PARAM_BLOCK_BEGIN(SSAOParamDef)
  478. BS_PARAM_BLOCK_ENTRY(float, gSampleRadius)
  479. BS_PARAM_BLOCK_ENTRY(float, gWorldSpaceRadiusMask)
  480. BS_PARAM_BLOCK_ENTRY(Vector2, gTanHalfFOV)
  481. BS_PARAM_BLOCK_ENTRY(Vector2, gRandomTileScale)
  482. BS_PARAM_BLOCK_ENTRY(float, gCotHalfFOV)
  483. BS_PARAM_BLOCK_ENTRY(float, gBias)
  484. BS_PARAM_BLOCK_ENTRY(Vector2, gDownsampledPixelSize)
  485. BS_PARAM_BLOCK_ENTRY(Vector2, gFadeMultiplyAdd)
  486. BS_PARAM_BLOCK_ENTRY(float, gPower)
  487. BS_PARAM_BLOCK_ENTRY(float, gIntensity)
  488. BS_PARAM_BLOCK_END
  489. extern SSAOParamDef gSSAOParamDef;
  490. /** Textures used as input when calculating SSAO. */
  491. struct SSAOTextureInputs
  492. {
  493. /** Full resolution scene depth. Only used by final SSAO pass. */
  494. SPtr<Texture> sceneDepth;
  495. /** Full resolution buffer containing scene normals. Only used by final SSAO pass. */
  496. SPtr<Texture> sceneNormals;
  497. /** Precalculated texture containing downsampled normals/depth, to be used for AO input. */
  498. SPtr<Texture> aoSetup;
  499. /** Texture containing AO from the previous pass. Only used if upsampling is enabled. */
  500. SPtr<Texture> aoDownsampled;
  501. /** Tileable texture containing random rotations that will be applied to AO samples. */
  502. SPtr<Texture> randomRotations;
  503. };
  504. /**
  505. * Shader that computes ambient occlusion using screen based methods.
  506. *
  507. * @tparam UPSAMPLE If true the shader will blend the calculated AO with AO data from the previous pass.
  508. * @tparam FINAL_PASS If true the shader will use the full screen normal/depth information and perform
  509. * intensity scaling, as well as distance fade. Otherwise the shader will use the
  510. * downsampled AO setup information, with no scaling/fade.
  511. * @tparam QUALITY Integer in range [0, 4] that controls the quality of SSAO sampling. Higher numbers yield
  512. * better quality at the cost of performance.
  513. */
  514. template<bool UPSAMPLE, bool FINAL_PASS, int QUALITY>
  515. class SSAOMat : public RendererMaterial<SSAOMat<UPSAMPLE, FINAL_PASS, QUALITY>>
  516. {
  517. RMAT_DEF("PPSSAO.bsl");
  518. public:
  519. SSAOMat();
  520. /**
  521. * Renders the post-process effect with the provided parameters.
  522. *
  523. * @param[in] view Information about the view we're rendering from.
  524. * @param[in] textures Set of textures to be used as input. Which textures are used depends on the
  525. * template parameters of this class.
  526. * @param[in] destination Output texture to which to write the ambient occlusion data to.
  527. * @param[in] settings Settings used to control the ambient occlusion effect.
  528. */
  529. void execute(const RendererView& view, const SSAOTextureInputs& textures, const SPtr<RenderTexture>& destination,
  530. const AmbientOcclusionSettings& settings);
  531. private:
  532. SPtr<GpuParamBlockBuffer> mParamBuffer;
  533. GpuParamTexture mDepthTexture;
  534. GpuParamTexture mNormalsTexture;
  535. GpuParamTexture mDownsampledAOTexture;
  536. GpuParamTexture mSetupAOTexture;
  537. GpuParamTexture mRandomTexture;
  538. };
  539. BS_PARAM_BLOCK_BEGIN(SSAODownsampleParamDef)
  540. BS_PARAM_BLOCK_ENTRY(Vector2, gPixelSize)
  541. BS_PARAM_BLOCK_ENTRY(float, gInvDepthThreshold)
  542. BS_PARAM_BLOCK_END
  543. extern SSAODownsampleParamDef gSSAODownsampleParamDef;
  544. /**
  545. * Shader that downsamples the depth & normal buffer and stores their results in a common texture, to be consumed
  546. * by SSAOMat.
  547. */
  548. class SSAODownsampleMat : public RendererMaterial<SSAODownsampleMat>
  549. {
  550. RMAT_DEF("PPSSAODownsample.bsl");
  551. public:
  552. SSAODownsampleMat();
  553. /**
  554. * Renders the post-process effect with the provided parameters.
  555. *
  556. * @param[in] view Information about the view we're rendering from.
  557. * @param[in] sceneDepth Input texture containing scene depth.
  558. * @param[in] sceneNormals Input texture containing scene world space normals.
  559. * @param[in] destination Output texture to which to write the downsampled data to.
  560. * @param[in] depthRange Valid depth range (in view space) within which nearby samples will be averaged.
  561. */
  562. void execute(const RendererView& view, const SPtr<Texture>& sceneDepth, const SPtr<Texture>& sceneNormals,
  563. const SPtr<RenderTexture>& destination, float depthRange);
  564. private:
  565. SPtr<GpuParamBlockBuffer> mParamBuffer;
  566. GpuParamTexture mDepthTexture;
  567. GpuParamTexture mNormalsTexture;
  568. };
  569. BS_PARAM_BLOCK_BEGIN(SSAOBlurParamDef)
  570. BS_PARAM_BLOCK_ENTRY(Vector2, gPixelSize)
  571. BS_PARAM_BLOCK_ENTRY(Vector2, gPixelOffset)
  572. BS_PARAM_BLOCK_ENTRY(float, gInvDepthThreshold)
  573. BS_PARAM_BLOCK_END
  574. extern SSAOBlurParamDef gSSAOBlurParamDef;
  575. /**
  576. * Shaders that blurs the ambient occlusion output, in order to hide the noise caused by the randomization texture.
  577. */
  578. template<bool HORIZONTAL>
  579. class SSAOBlurMat : public RendererMaterial<SSAOBlurMat<HORIZONTAL>>
  580. {
  581. RMAT_DEF("PPSSAOBlur.bsl");
  582. public:
  583. SSAOBlurMat();
  584. /**
  585. * Renders the post-process effect with the provided parameters.
  586. *
  587. * @param[in] view Information about the view we're rendering from.
  588. * @param[in] ao Input texture containing ambient occlusion data to be blurred.
  589. * @param[in] sceneDepth Input texture containing scene depth.
  590. * @param[in] destination Output texture to which to write the blurred data to.
  591. * @param[in] depthRange Valid depth range (in view space) within which nearby samples will be averaged.
  592. */
  593. void execute(const RendererView& view, const SPtr<Texture>& ao, const SPtr<Texture>& sceneDepth,
  594. const SPtr<RenderTexture>& destination, float depthRange);
  595. private:
  596. SPtr<GpuParamBlockBuffer> mParamBuffer;
  597. GpuParamTexture mAOTexture;
  598. GpuParamTexture mDepthTexture;
  599. };
  600. /** Helper class that is used for calculating the SSAO information. */
  601. class SSAO
  602. {
  603. public:
  604. SSAO();
  605. /**
  606. * Calculates SSAO for the specified view.
  607. *
  608. * @param[in] view Information about the view we're rendering from.
  609. * @param[in] destination Output texture to which to write the SSAO data to.
  610. * @param[in] settings Settings that control how is SSAO calculated.
  611. */
  612. void execute(const RendererView& view, const SPtr<RenderTexture>& destination,
  613. const AmbientOcclusionSettings& settings);
  614. /**
  615. * Generates a texture that is used for randomizing sample locations during SSAO calculation. The texture contains
  616. * 16 different rotations in a 4x4 tile.
  617. */
  618. SPtr<Texture> generate4x4RandomizationTexture() const;
  619. private:
  620. /** @copydoc SSAOMat::execute() */
  621. void executeSSAOMat(bool upsample, bool final, int quality, const RendererView& view,
  622. const SSAOTextureInputs& textures, const SPtr<RenderTexture>& destination,
  623. const AmbientOcclusionSettings& settings);
  624. SSAODownsampleMat mDownsample;
  625. SSAOBlurMat<true> mBlurHorz;
  626. SSAOBlurMat<false> mBlurVert;
  627. SPtr<Texture> mSSAORandomizationTex;
  628. #define DEFINE_MATERIAL(QUALITY) \
  629. SSAOMat<false, false, QUALITY> mSSAO_FF_##QUALITY; \
  630. SSAOMat<true, false, QUALITY> mSSAO_TF_##QUALITY; \
  631. SSAOMat<false, true, QUALITY> mSSAO_FT_##QUALITY; \
  632. SSAOMat<true, true, QUALITY> mSSAO_TT_##QUALITY; \
  633. DEFINE_MATERIAL(0)
  634. DEFINE_MATERIAL(1)
  635. DEFINE_MATERIAL(2)
  636. DEFINE_MATERIAL(3)
  637. DEFINE_MATERIAL(4)
  638. #undef DEFINE_MATERIAL
  639. };
  640. /**
  641. * Renders post-processing effects for the provided render target.
  642. *
  643. * @note Core thread only.
  644. */
  645. class PostProcessing : public Module<PostProcessing>
  646. {
  647. public:
  648. /**
  649. * Renders post-processing effects for the provided render target. Resolves provided scene color texture into the
  650. * view's final output render target. Once the method exits, final render target is guaranteed to be currently
  651. * bound for rendering.
  652. */
  653. void postProcess(RendererView* viewInfo, const SPtr<RenderTargets>& renderTargets, float frameDelta);
  654. /**
  655. * Populates the ambient occlusion texture of the specified view with screen-space ambient occlusion information.
  656. * Ambient occlusion texture must be allocated on the view's render targets before calling this method.
  657. */
  658. void buildSSAO(const RendererView& view);
  659. private:
  660. DownsampleMaterials mDownsample;
  661. EyeAdaptHistogramMat mEyeAdaptHistogram;
  662. EyeAdaptHistogramReduceMat mEyeAdaptHistogramReduce;
  663. EyeAdaptationMat mEyeAdaptation;
  664. CreateTonemapLUTMat mCreateLUT;
  665. TonemappingMaterials mTonemapping;
  666. GaussianDOF mGaussianDOF;
  667. FXAAMat mFXAA;
  668. SSAO mSSAO;
  669. };
  670. /** @} */
  671. }}