BsPostProcessing.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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 "Renderer/BsRendererMaterial.h"
  6. #include "Renderer/BsParamBlocks.h"
  7. #include "BsGpuResourcePool.h"
  8. #include "BsLightRendering.h"
  9. #include "Renderer/BsRenderSettings.h"
  10. namespace bs { namespace ct
  11. {
  12. struct RendererViewTargetData;
  13. /** @addtogroup RenderBeast
  14. * @{
  15. */
  16. BS_PARAM_BLOCK_BEGIN(DownsampleParamDef)
  17. BS_PARAM_BLOCK_ENTRY_ARRAY(Vector2, gOffsets, 4)
  18. BS_PARAM_BLOCK_END
  19. extern DownsampleParamDef gDownsampleParamDef;
  20. /** Shader that downsamples a texture to half its size. */
  21. class DownsampleMat : public RendererMaterial<DownsampleMat>
  22. {
  23. RMAT_DEF("PPDownsample.bsl");
  24. public:
  25. DownsampleMat();
  26. /** Renders the post-process effect with the provided parameters. */
  27. void execute(const SPtr<Texture>& input, const SPtr<RenderTarget>& output);
  28. /** Returns the texture descriptor that can be used for initializing the output render target. */
  29. static POOLED_RENDER_TEXTURE_DESC getOutputDesc(const SPtr<Texture>& target);
  30. /** Returns the downsample material variation matching the provided parameters. */
  31. static DownsampleMat* getVariation(UINT32 quality, bool msaa);
  32. private:
  33. SPtr<GpuParamBlockBuffer> mParamBuffer;
  34. GpuParamTexture mInputTexture;
  35. static ShaderVariation VAR_LowQuality_NoMSAA;
  36. static ShaderVariation VAR_LowQuality_MSAA;
  37. static ShaderVariation VAR_HighQuality_NoMSAA;
  38. static ShaderVariation VAR_HighQuality_MSAA;
  39. };
  40. BS_PARAM_BLOCK_BEGIN(EyeAdaptHistogramParamDef)
  41. BS_PARAM_BLOCK_ENTRY(Vector4I, gPixelOffsetAndSize)
  42. BS_PARAM_BLOCK_ENTRY(Vector2, gHistogramParams)
  43. BS_PARAM_BLOCK_ENTRY(Vector2I, gThreadGroupCount)
  44. BS_PARAM_BLOCK_END
  45. extern EyeAdaptHistogramParamDef gEyeAdaptHistogramParamDef;
  46. /** Shader that creates a luminance histogram used for eye adaptation. */
  47. class EyeAdaptHistogramMat : public RendererMaterial<EyeAdaptHistogramMat>
  48. {
  49. RMAT_DEF("PPEyeAdaptHistogram.bsl");
  50. public:
  51. EyeAdaptHistogramMat();
  52. /** Executes the post-process effect with the provided parameters. */
  53. void execute(const SPtr<Texture>& input, const SPtr<Texture>& output, const AutoExposureSettings& settings);
  54. /** Returns the texture descriptor that can be used for initializing the output render target. */
  55. static POOLED_RENDER_TEXTURE_DESC getOutputDesc(const SPtr<Texture>& target);
  56. /** Calculates the number of thread groups that need to execute to cover the provided texture. */
  57. static Vector2I getThreadGroupCount(const SPtr<Texture>& target);
  58. /**
  59. * Returns a vector containing scale and offset (in that order) that will be applied to luminance values
  60. * to determine their position in the histogram.
  61. */
  62. static Vector2 getHistogramScaleOffset(const AutoExposureSettings& settings);
  63. static const UINT32 THREAD_GROUP_SIZE_X = 8;
  64. static const UINT32 THREAD_GROUP_SIZE_Y = 8;
  65. static const UINT32 HISTOGRAM_NUM_TEXELS = (THREAD_GROUP_SIZE_X * THREAD_GROUP_SIZE_Y) / 4;
  66. private:
  67. SPtr<GpuParamBlockBuffer> mParamBuffer;
  68. GpuParamTexture mSceneColor;
  69. GpuParamLoadStoreTexture mOutputTex;
  70. static const UINT32 LOOP_COUNT_X = 8;
  71. static const UINT32 LOOP_COUNT_Y = 8;
  72. };
  73. BS_PARAM_BLOCK_BEGIN(EyeAdaptHistogramReduceParamDef)
  74. BS_PARAM_BLOCK_ENTRY(int, gThreadGroupCount)
  75. BS_PARAM_BLOCK_END
  76. extern EyeAdaptHistogramReduceParamDef gEyeAdaptHistogramReduceParamDef;
  77. /** Shader that reduces the luminance histograms created by EyeAdaptHistogramMat into a single histogram. */
  78. class EyeAdaptHistogramReduceMat : public RendererMaterial<EyeAdaptHistogramReduceMat>
  79. {
  80. RMAT_DEF("PPEyeAdaptHistogramReduce.bsl");
  81. public:
  82. EyeAdaptHistogramReduceMat();
  83. /** Executes the post-process effect with the provided parameters. */
  84. void execute(const SPtr<Texture>& sceneColor, const SPtr<Texture>& histogram, const SPtr<Texture>& prevFrame,
  85. const SPtr<RenderTarget>& output);
  86. /** Returns the texture descriptor that can be used for initializing the output render target. */
  87. static POOLED_RENDER_TEXTURE_DESC getOutputDesc();
  88. private:
  89. SPtr<GpuParamBlockBuffer> mParamBuffer;
  90. GpuParamTexture mHistogramTex;
  91. GpuParamTexture mEyeAdaptationTex;
  92. };
  93. BS_PARAM_BLOCK_BEGIN(EyeAdaptationParamDef)
  94. BS_PARAM_BLOCK_ENTRY_ARRAY(Vector4, gEyeAdaptationParams, 3)
  95. BS_PARAM_BLOCK_END
  96. extern EyeAdaptationParamDef gEyeAdaptationParamDef;
  97. /** Shader that computes the eye adaptation value based on scene luminance. */
  98. class EyeAdaptationMat : public RendererMaterial<EyeAdaptationMat>
  99. {
  100. RMAT_DEF("PPEyeAdaptation.bsl");
  101. public:
  102. EyeAdaptationMat();
  103. /** Executes the post-process effect with the provided parameters. */
  104. void execute(const SPtr<Texture>& reducedHistogram, const SPtr<RenderTarget>& output, float frameDelta,
  105. const AutoExposureSettings& settings, float exposureScale);
  106. /** Returns the texture descriptor that can be used for initializing the output render target. */
  107. static POOLED_RENDER_TEXTURE_DESC getOutputDesc();
  108. private:
  109. SPtr<GpuParamBlockBuffer> mParamBuffer;
  110. GpuParamTexture mReducedHistogramTex;
  111. };
  112. BS_PARAM_BLOCK_BEGIN(CreateTonemapLUTParamDef)
  113. BS_PARAM_BLOCK_ENTRY_ARRAY(Vector4, gTonemapParams, 2)
  114. BS_PARAM_BLOCK_ENTRY(float, gGammaAdjustment)
  115. BS_PARAM_BLOCK_ENTRY(int, gGammaCorrectionType)
  116. BS_PARAM_BLOCK_ENTRY(Vector3, gSaturation)
  117. BS_PARAM_BLOCK_ENTRY(Vector3, gContrast)
  118. BS_PARAM_BLOCK_ENTRY(Vector3, gGain)
  119. BS_PARAM_BLOCK_ENTRY(Vector3, gOffset)
  120. BS_PARAM_BLOCK_END
  121. extern CreateTonemapLUTParamDef gCreateTonemapLUTParamDef;
  122. BS_PARAM_BLOCK_BEGIN(WhiteBalanceParamDef)
  123. BS_PARAM_BLOCK_ENTRY(float, gWhiteTemp)
  124. BS_PARAM_BLOCK_ENTRY(float, gWhiteOffset)
  125. BS_PARAM_BLOCK_END
  126. extern WhiteBalanceParamDef gWhiteBalanceParamDef;
  127. /**
  128. * Shader that creates a 3D lookup texture that is used to apply tonemapping, color grading, white balancing and gamma
  129. * correction.
  130. */
  131. class CreateTonemapLUTMat : public RendererMaterial<CreateTonemapLUTMat>
  132. {
  133. RMAT_DEF("PPCreateTonemapLUT.bsl");
  134. public:
  135. CreateTonemapLUTMat();
  136. /** Executes the post-process effect with the provided parameters. */
  137. void execute(const SPtr<Texture>& output, const RenderSettings& settings);
  138. /** Returns the texture descriptor that can be used for initializing the output render target. */
  139. static POOLED_RENDER_TEXTURE_DESC getOutputDesc();
  140. /** Size of the 3D color lookup table. */
  141. static const UINT32 LUT_SIZE = 32;
  142. private:
  143. SPtr<GpuParamBlockBuffer> mParamBuffer;
  144. SPtr<GpuParamBlockBuffer> mWhiteBalanceParamBuffer;
  145. GpuParamLoadStoreTexture mOutputTex;
  146. };
  147. BS_PARAM_BLOCK_BEGIN(TonemappingParamDef)
  148. BS_PARAM_BLOCK_ENTRY(float, gRawGamma)
  149. BS_PARAM_BLOCK_ENTRY(float, gManualExposureScale)
  150. BS_PARAM_BLOCK_ENTRY(int, gNumSamples)
  151. BS_PARAM_BLOCK_END
  152. extern TonemappingParamDef gTonemappingParamDef;
  153. /** Shader that applies tonemapping and converts a HDR image into a LDR image. */
  154. class TonemappingMat : public RendererMaterial<TonemappingMat>
  155. {
  156. RMAT_DEF("PPTonemapping.bsl");
  157. public:
  158. TonemappingMat();
  159. /** Executes the post-process effect with the provided parameters. */
  160. void execute(const SPtr<Texture>& sceneColor, const SPtr<Texture>& eyeAdaptation, const SPtr<Texture>& colorLUT,
  161. const SPtr<RenderTarget>& output, const RenderSettings& settings);
  162. /** Returns the material variation matching the provided parameters. */
  163. static TonemappingMat* getVariation(bool gammaOnly, bool autoExposure, bool MSAA);
  164. private:
  165. SPtr<GpuParamBlockBuffer> mParamBuffer;
  166. GpuParamTexture mInputTex;
  167. GpuParamTexture mColorLUT;
  168. GpuParamTexture mEyeAdaptationTex;
  169. static ShaderVariation VAR_Gamma_AutoExposure_MSAA;
  170. static ShaderVariation VAR_Gamma_AutoExposure_NoMSAA;
  171. static ShaderVariation VAR_Gamma_NoAutoExposure_MSAA;
  172. static ShaderVariation VAR_Gamma_NoAutoExposure_NoMSAA;
  173. static ShaderVariation VAR_NoGamma_AutoExposure_MSAA;
  174. static ShaderVariation VAR_NoGamma_AutoExposure_NoMSAA;
  175. static ShaderVariation VAR_NoGamma_NoAutoExposure_MSAA;
  176. static ShaderVariation VAR_NoGamma_NoAutoExposure_NoMSAA;
  177. };
  178. const int MAX_BLUR_SAMPLES = 128;
  179. BS_PARAM_BLOCK_BEGIN(GaussianBlurParamDef)
  180. BS_PARAM_BLOCK_ENTRY_ARRAY(Vector4, gSampleOffsets, (MAX_BLUR_SAMPLES + 1) / 2)
  181. BS_PARAM_BLOCK_ENTRY_ARRAY(Vector4, gSampleWeights, (MAX_BLUR_SAMPLES + 3) / 4)
  182. BS_PARAM_BLOCK_ENTRY(int, gNumSamples)
  183. BS_PARAM_BLOCK_END
  184. extern GaussianBlurParamDef gGaussianBlurParamDef;
  185. /** Shader that performs Gaussian blur filtering on the provided texture. */
  186. class GaussianBlurMat : public RendererMaterial<GaussianBlurMat>
  187. {
  188. // Direction of the Gaussian filter pass
  189. enum Direction
  190. {
  191. DirVertical,
  192. DirHorizontal
  193. };
  194. RMAT_DEF("PPGaussianBlur.bsl");
  195. public:
  196. GaussianBlurMat();
  197. /**
  198. * Renders the post-process effect with the provided parameters.
  199. *
  200. * @param[in] source Input texture to blur.
  201. * @param[in] filterSize Size of the blurring filter, in percent of the source texture. In range [0, 1].
  202. * @param[in] destination Output texture to which to write the blurred image to.
  203. */
  204. void execute(const SPtr<Texture>& source, float filterSize, const SPtr<RenderTexture>& destination);
  205. private:
  206. /** Calculates weights and offsets for the standard distribution of the specified filter size. */
  207. static UINT32 calcStdDistribution(float filterRadius, std::array<float, MAX_BLUR_SAMPLES>& weights,
  208. std::array<float, MAX_BLUR_SAMPLES>& offsets);
  209. /** Calculates the radius of the blur kernel depending on the source texture size and provided scale. */
  210. static float calcKernelRadius(const SPtr<Texture>& source, float scale, Direction filterDir);
  211. SPtr<GpuParamBlockBuffer> mParamBuffer;
  212. GpuParamTexture mInputTexture;
  213. };
  214. BS_PARAM_BLOCK_BEGIN(GaussianDOFParamDef)
  215. BS_PARAM_BLOCK_ENTRY(float, gNearBlurPlane)
  216. BS_PARAM_BLOCK_ENTRY(float, gFarBlurPlane)
  217. BS_PARAM_BLOCK_ENTRY(float, gInvNearBlurRange)
  218. BS_PARAM_BLOCK_ENTRY(float, gInvFarBlurRange)
  219. BS_PARAM_BLOCK_ENTRY(Vector2, gHalfPixelOffset)
  220. BS_PARAM_BLOCK_END
  221. extern GaussianDOFParamDef sGaussianDOFParamDef;
  222. /**
  223. * Shader that masks pixels from the input color texture into one or two output textures. The masking is done by
  224. * determining if the pixel falls into near or far unfocused plane, as determined by depth-of-field parameters. User
  225. * can pick whether to output pixels just on the near plane, just on the far plane, or both.
  226. *
  227. */
  228. class GaussianDOFSeparateMat : public RendererMaterial<GaussianDOFSeparateMat>
  229. {
  230. RMAT_DEF("PPGaussianDOFSeparate.bsl");
  231. public:
  232. GaussianDOFSeparateMat();
  233. /**
  234. * Renders the post-process effect with the provided parameters.
  235. *
  236. * @param[in] color Input color texture to process.
  237. * @param[in] depth Input depth buffer texture that will be used for determining pixel depth.
  238. * @param[in] view View through which the depth of field effect is viewed.
  239. * @param[in] settings Settings used to control depth of field rendering.
  240. */
  241. void execute(const SPtr<Texture>& color, const SPtr<Texture>& depth, const RendererView& view,
  242. const DepthOfFieldSettings& settings);
  243. /**
  244. * Returns the texture generated after the shader was executed. Only valid to call this in-between calls to
  245. * execute() & release(), with @p idx value 0 or 1.
  246. */
  247. SPtr<PooledRenderTexture> getOutput(UINT32 idx);
  248. /**
  249. * Releases the interally allocated output render textures. Must be called after each call to execute(), when the
  250. * caller is done using the textures.
  251. */
  252. void release();
  253. /**
  254. * Returns the material variation matching the provided parameters.
  255. *
  256. * @param near If true, near plane pixels are output to the first render target.
  257. * @param far If true, far plane pixels are output to the first render target. If @p near is also enabled, the
  258. * pixels are output to the second render target instead.
  259. */
  260. static GaussianDOFSeparateMat* getVariation(bool near, bool far);
  261. private:
  262. SPtr<GpuParamBlockBuffer> mParamBuffer;
  263. GpuParamTexture mColorTexture;
  264. GpuParamTexture mDepthTexture;
  265. SPtr<PooledRenderTexture> mOutput0;
  266. SPtr<PooledRenderTexture> mOutput1;
  267. static ShaderVariation VAR_Near_Far;
  268. static ShaderVariation VAR_NoNear_Far;
  269. static ShaderVariation VAR_Near_NoFar;
  270. };
  271. /**
  272. * Shader that combines pixels for near unfocused, focused and far unfocused planes, as calculated by
  273. * GaussianDOFSeparateMat. Outputs final depth-of-field filtered image.
  274. */
  275. class GaussianDOFCombineMat : public RendererMaterial<GaussianDOFCombineMat>
  276. {
  277. RMAT_DEF("PPGaussianDOFCombine.bsl");
  278. public:
  279. GaussianDOFCombineMat();
  280. /**
  281. * Renders the post-process effect with the provided parameters.
  282. *
  283. * @param[in] focused Input texture containing focused (default) scene color.
  284. * @param[in] near Input texture containing filtered (blurred) values for the unfocused foreground area.
  285. * Can be null if no near plane needs to be blended.
  286. * @param[in] far Input texture containing filtered (blurred) values for the unfocused background area.
  287. * Can be null if no far plane needs to be blended.
  288. * @param[in] depth Input depth buffer texture that will be used for determining pixel depth.
  289. * @param[in] output Texture to output the results to.
  290. * @param[in] view View through which the depth of field effect is viewed.
  291. * @param[in] settings Settings used to control depth of field rendering.
  292. */
  293. void execute(const SPtr<Texture>& focused, const SPtr<Texture>& near, const SPtr<Texture>& far,
  294. const SPtr<Texture>& depth, const SPtr<RenderTarget>& output, const RendererView& view,
  295. const DepthOfFieldSettings& settings);
  296. /**
  297. * Returns the material variation matching the provided parameters.
  298. *
  299. * @param near If true, near plane pixels are read from the near plane texture, otherwise near plane is assumed
  300. * not to exist.
  301. * @param far If true, far plane pixels are read from the far plane texture, otherwise far plane is assumed not
  302. * to exist.
  303. */
  304. static GaussianDOFCombineMat* getVariation(bool near, bool far);
  305. private:
  306. SPtr<GpuParamBlockBuffer> mParamBuffer;
  307. GpuParamTexture mFocusedTexture;
  308. GpuParamTexture mNearTexture;
  309. GpuParamTexture mFarTexture;
  310. GpuParamTexture mDepthTexture;
  311. static ShaderVariation VAR_Near_Far;
  312. static ShaderVariation VAR_NoNear_Far;
  313. static ShaderVariation VAR_Near_NoFar;
  314. };
  315. /** Shader that calculates a single level of the hierarchical Z mipmap chain. */
  316. class BuildHiZMat : public RendererMaterial<BuildHiZMat>
  317. {
  318. RMAT_DEF("PPBuildHiZ.bsl");
  319. public:
  320. BuildHiZMat();
  321. /**
  322. * Renders the post-process effect with the provided parameters.
  323. *
  324. * @param[in] source Input depth texture to use as the source.
  325. * @param[in] srcMip Mip level to read from the @p source texture.
  326. * @param[in] srcRect Rectangle in normalized coordinates, describing from which portion of the source
  327. * texture to read the input.
  328. * @param[in] dstRect Destination rectangle to limit the writes to.
  329. * @param[in] output Output target to which to write to results.
  330. */
  331. void execute(const SPtr<Texture>& source, UINT32 srcMip, const Rect2& srcRect, const Rect2& dstRect,
  332. const SPtr<RenderTexture>& output);
  333. private:
  334. GpuParamTexture mInputTexture;
  335. };
  336. BS_PARAM_BLOCK_BEGIN(FXAAParamDef)
  337. BS_PARAM_BLOCK_ENTRY(Vector2, gInvTexSize)
  338. BS_PARAM_BLOCK_END
  339. extern FXAAParamDef gFXAAParamDef;
  340. /** Shader that performs Fast Approximate anti-aliasing. */
  341. class FXAAMat : public RendererMaterial<FXAAMat>
  342. {
  343. RMAT_DEF("PPFXAA.bsl");
  344. public:
  345. FXAAMat();
  346. /**
  347. * Renders the post-process effect with the provided parameters.
  348. *
  349. * @param[in] source Input texture to apply FXAA to.
  350. * @param[in] destination Output target to which to write the antialiased image to.
  351. */
  352. void execute(const SPtr<Texture>& source, const SPtr<RenderTarget>& destination);
  353. private:
  354. SPtr<GpuParamBlockBuffer> mParamBuffer;
  355. GpuParamTexture mInputTexture;
  356. };
  357. BS_PARAM_BLOCK_BEGIN(SSAOParamDef)
  358. BS_PARAM_BLOCK_ENTRY(float, gSampleRadius)
  359. BS_PARAM_BLOCK_ENTRY(float, gWorldSpaceRadiusMask)
  360. BS_PARAM_BLOCK_ENTRY(Vector2, gTanHalfFOV)
  361. BS_PARAM_BLOCK_ENTRY(Vector2, gRandomTileScale)
  362. BS_PARAM_BLOCK_ENTRY(float, gCotHalfFOV)
  363. BS_PARAM_BLOCK_ENTRY(float, gBias)
  364. BS_PARAM_BLOCK_ENTRY(Vector2, gDownsampledPixelSize)
  365. BS_PARAM_BLOCK_ENTRY(Vector2, gFadeMultiplyAdd)
  366. BS_PARAM_BLOCK_ENTRY(float, gPower)
  367. BS_PARAM_BLOCK_ENTRY(float, gIntensity)
  368. BS_PARAM_BLOCK_END
  369. extern SSAOParamDef gSSAOParamDef;
  370. /** Textures used as input when calculating SSAO. */
  371. struct SSAOTextureInputs
  372. {
  373. /** Full resolution scene depth. Only used by final SSAO pass. */
  374. SPtr<Texture> sceneDepth;
  375. /** Full resolution buffer containing scene normals. Only used by final SSAO pass. */
  376. SPtr<Texture> sceneNormals;
  377. /** Precalculated texture containing downsampled normals/depth, to be used for AO input. */
  378. SPtr<Texture> aoSetup;
  379. /** Texture containing AO from the previous pass. Only used if upsampling is enabled. */
  380. SPtr<Texture> aoDownsampled;
  381. /** Tileable texture containing random rotations that will be applied to AO samples. */
  382. SPtr<Texture> randomRotations;
  383. };
  384. /** Shader that computes ambient occlusion using screen based methods. */
  385. class SSAOMat : public RendererMaterial<SSAOMat>
  386. {
  387. RMAT_DEF("PPSSAO.bsl");
  388. public:
  389. SSAOMat();
  390. /**
  391. * Renders the post-process effect with the provided parameters.
  392. *
  393. * @param[in] view Information about the view we're rendering from.
  394. * @param[in] textures Set of textures to be used as input. Which textures are used depends on the
  395. * template parameters of this class.
  396. * @param[in] destination Output texture to which to write the ambient occlusion data to.
  397. * @param[in] settings Settings used to control the ambient occlusion effect.
  398. */
  399. void execute(const RendererView& view, const SSAOTextureInputs& textures, const SPtr<RenderTexture>& destination,
  400. const AmbientOcclusionSettings& settings);
  401. /**
  402. * Returns the material variation matching the provided parameters.
  403. *
  404. * @param upsample If true the shader will blend the calculated AO with AO data from the previous pass.
  405. * @param finalPass If true the shader will use the full screen normal/depth information and perform
  406. * intensity scaling, as well as distance fade. Otherwise the shader will use the
  407. * downsampled AO setup information, with no scaling/fade.
  408. * @param quality Integer in range [0, 4] that controls the quality of SSAO sampling. Higher numbers yield
  409. * better quality at the cost of performance.
  410. */
  411. static SSAOMat* getVariation(bool upsample, bool finalPass, int quality);
  412. private:
  413. SPtr<GpuParamBlockBuffer> mParamBuffer;
  414. GpuParamTexture mDepthTexture;
  415. GpuParamTexture mNormalsTexture;
  416. GpuParamTexture mDownsampledAOTexture;
  417. GpuParamTexture mSetupAOTexture;
  418. GpuParamTexture mRandomTexture;
  419. #define VARIATION(QUALITY) \
  420. static ShaderVariation VAR_Upsample_Final_Quality##QUALITY; \
  421. static ShaderVariation VAR_Upsample_NoFinal_Quality##QUALITY; \
  422. static ShaderVariation VAR_NoUpsample_Final_Quality##QUALITY; \
  423. static ShaderVariation VAR_NoUpsample_NoFinal_Quality##QUALITY; \
  424. VARIATION(0)
  425. VARIATION(1)
  426. VARIATION(2)
  427. VARIATION(3)
  428. VARIATION(4)
  429. #undef VARIATION
  430. };
  431. BS_PARAM_BLOCK_BEGIN(SSAODownsampleParamDef)
  432. BS_PARAM_BLOCK_ENTRY(Vector2, gPixelSize)
  433. BS_PARAM_BLOCK_ENTRY(float, gInvDepthThreshold)
  434. BS_PARAM_BLOCK_END
  435. extern SSAODownsampleParamDef gSSAODownsampleParamDef;
  436. /**
  437. * Shader that downsamples the depth & normal buffer and stores their results in a common texture, to be consumed
  438. * by SSAOMat.
  439. */
  440. class SSAODownsampleMat : public RendererMaterial<SSAODownsampleMat>
  441. {
  442. RMAT_DEF("PPSSAODownsample.bsl");
  443. public:
  444. SSAODownsampleMat();
  445. /**
  446. * Renders the post-process effect with the provided parameters.
  447. *
  448. * @param[in] view Information about the view we're rendering from.
  449. * @param[in] sceneDepth Input texture containing scene depth.
  450. * @param[in] sceneNormals Input texture containing scene world space normals.
  451. * @param[in] destination Output texture to which to write the downsampled data to.
  452. * @param[in] depthRange Valid depth range (in view space) within which nearby samples will be averaged.
  453. */
  454. void execute(const RendererView& view, const SPtr<Texture>& sceneDepth, const SPtr<Texture>& sceneNormals,
  455. const SPtr<RenderTexture>& destination, float depthRange);
  456. private:
  457. SPtr<GpuParamBlockBuffer> mParamBuffer;
  458. GpuParamTexture mDepthTexture;
  459. GpuParamTexture mNormalsTexture;
  460. };
  461. BS_PARAM_BLOCK_BEGIN(SSAOBlurParamDef)
  462. BS_PARAM_BLOCK_ENTRY(Vector2, gPixelSize)
  463. BS_PARAM_BLOCK_ENTRY(Vector2, gPixelOffset)
  464. BS_PARAM_BLOCK_ENTRY(float, gInvDepthThreshold)
  465. BS_PARAM_BLOCK_END
  466. extern SSAOBlurParamDef gSSAOBlurParamDef;
  467. /**
  468. * Shaders that blurs the ambient occlusion output, in order to hide the noise caused by the randomization texture.
  469. */
  470. class SSAOBlurMat : public RendererMaterial<SSAOBlurMat>
  471. {
  472. RMAT_DEF("PPSSAOBlur.bsl");
  473. public:
  474. SSAOBlurMat();
  475. /**
  476. * Renders the post-process effect with the provided parameters.
  477. *
  478. * @param[in] view Information about the view we're rendering from.
  479. * @param[in] ao Input texture containing ambient occlusion data to be blurred.
  480. * @param[in] sceneDepth Input texture containing scene depth.
  481. * @param[in] destination Output texture to which to write the blurred data to.
  482. * @param[in] depthRange Valid depth range (in view space) within which nearby samples will be averaged.
  483. */
  484. void execute(const RendererView& view, const SPtr<Texture>& ao, const SPtr<Texture>& sceneDepth,
  485. const SPtr<RenderTexture>& destination, float depthRange);
  486. /** Returns the material variation matching the provided parameters. */
  487. static SSAOBlurMat* getVariation(bool horizontal);
  488. private:
  489. SPtr<GpuParamBlockBuffer> mParamBuffer;
  490. GpuParamTexture mAOTexture;
  491. GpuParamTexture mDepthTexture;
  492. static ShaderVariation VAR_Vertical;
  493. static ShaderVariation VAR_Horizontal;
  494. };
  495. BS_PARAM_BLOCK_BEGIN(SSRStencilParamDef)
  496. BS_PARAM_BLOCK_ENTRY(Vector2, gRoughnessScaleBias)
  497. BS_PARAM_BLOCK_END
  498. extern SSRStencilParamDef gSSRStencilParamDef;
  499. /** Shader used for marking which parts of the screen require screen space reflections. */
  500. class SSRStencilMat : public RendererMaterial<SSRStencilMat>
  501. {
  502. RMAT_DEF("PPSSRStencil.bsl");
  503. public:
  504. SSRStencilMat();
  505. /**
  506. * Renders the effect with the provided parameters, using the currently bound render target.
  507. *
  508. * @param[in] view Information about the view we're rendering from.
  509. * @param[in] gbuffer GBuffer textures.
  510. * @param[in] settings Parameters used for controling the SSR effect.
  511. */
  512. void execute(const RendererView& view, GBufferTextures gbuffer, const ScreenSpaceReflectionsSettings& settings);
  513. /**
  514. * Returns the material variation matching the provided parameters.
  515. *
  516. * @param[in] msaa True if the shader will operate on a multisampled surface.
  517. * @param[in] singleSampleMSAA Only relevant of @p msaa is true. When enabled only the first sample will be
  518. * evaluated. Otherwise all samples will be evaluated.
  519. * @return Requested variation of the material.
  520. */
  521. static SSRStencilMat* getVariation(bool msaa, bool singleSampleMSAA);
  522. private:
  523. SPtr<GpuParamBlockBuffer> mParamBuffer;
  524. GBufferParams mGBufferParams;
  525. static ShaderVariation VAR_FullMSAA;
  526. static ShaderVariation VAR_SingleMSAA;
  527. static ShaderVariation VAR_NoMSAA;
  528. };
  529. BS_PARAM_BLOCK_BEGIN(SSRTraceParamDef)
  530. BS_PARAM_BLOCK_ENTRY(Vector4, gNDCToHiZUV)
  531. BS_PARAM_BLOCK_ENTRY(Vector2, gHiZUVToScreenUV)
  532. BS_PARAM_BLOCK_ENTRY(Vector2I, gHiZSize)
  533. BS_PARAM_BLOCK_ENTRY(int, gHiZNumMips)
  534. BS_PARAM_BLOCK_ENTRY(float, gIntensity)
  535. BS_PARAM_BLOCK_ENTRY(Vector2, gRoughnessScaleBias)
  536. BS_PARAM_BLOCK_ENTRY(int, gTemporalJitter)
  537. BS_PARAM_BLOCK_END
  538. extern SSRTraceParamDef gSSRTraceParamDef;
  539. /** Shader used for tracing rays for screen space reflections. */
  540. class SSRTraceMat : public RendererMaterial<SSRTraceMat>
  541. {
  542. RMAT_DEF("PPSSRTrace.bsl");
  543. public:
  544. SSRTraceMat();
  545. /**
  546. * Renders the effect with the provided parameters.
  547. *
  548. * @param[in] view Information about the view we're rendering from.
  549. * @param[in] gbuffer GBuffer textures.
  550. * @param[in] sceneColor Scene color texture.
  551. * @param[in] hiZ Hierarchical Z buffer.
  552. * @param[in] settings Parameters used for controling the SSR effect.
  553. * @param[in] destination Render target to which to write the results to.
  554. */
  555. void execute(const RendererView& view, GBufferTextures gbuffer, const SPtr<Texture>& sceneColor,
  556. const SPtr<Texture>& hiZ, const ScreenSpaceReflectionsSettings& settings,
  557. const SPtr<RenderTarget>& destination);
  558. /**
  559. * Calculates a scale & bias that is used for transforming roughness into a fade out value. Anything that is below
  560. * @p maxRoughness will have the fade value of 1. Values above @p maxRoughness is slowly fade out over a range that
  561. * is 1/2 the length of @p maxRoughness.
  562. */
  563. static Vector2 calcRoughnessFadeScaleBias(float maxRoughness);
  564. /**
  565. * Returns the material variation matching the provided parameters.
  566. *
  567. * @param[in] quality Determines how many rays to trace. In range [0, 4].
  568. * @param[in] msaa True if the shader will operate on a multisampled surface.
  569. * @param[in] singleSampleMSAA Only relevant of @p msaa is true. When enabled only the first sample will be
  570. * evaluated. Otherwise all samples will be evaluated.
  571. * @return Requested variation of the material.
  572. */
  573. static SSRTraceMat* getVariation(UINT32 quality, bool msaa, bool singleSampleMSAA = false);
  574. private:
  575. SPtr<GpuParamBlockBuffer> mParamBuffer;
  576. GBufferParams mGBufferParams;
  577. GpuParamTexture mSceneColorTexture;
  578. GpuParamTexture mHiZTexture;
  579. #define VARIATION(QUALITY) \
  580. static ShaderVariation VAR_NoMSAA_Quality##QUALITY; \
  581. static ShaderVariation VAR_FullMSAA_Quality##QUALITY; \
  582. static ShaderVariation VAR_SingleMSAA_Quality##QUALITY; \
  583. VARIATION(0)
  584. VARIATION(1)
  585. VARIATION(2)
  586. VARIATION(3)
  587. VARIATION(4)
  588. #undef VARIATION
  589. };
  590. BS_PARAM_BLOCK_BEGIN(TemporalResolveParamDef)
  591. BS_PARAM_BLOCK_ENTRY_ARRAY(float, gSampleWeights, 9)
  592. BS_PARAM_BLOCK_ENTRY_ARRAY(float, gSampleWeightsLowpass, 9)
  593. BS_PARAM_BLOCK_END
  594. extern TemporalResolveParamDef gTemporalResolveParamDef;
  595. BS_PARAM_BLOCK_BEGIN(SSRResolveParamDef)
  596. BS_PARAM_BLOCK_ENTRY(Vector2, gSceneDepthTexelSize)
  597. BS_PARAM_BLOCK_ENTRY(Vector2, gSceneColorTexelSize)
  598. BS_PARAM_BLOCK_ENTRY(float, gManualExposure)
  599. BS_PARAM_BLOCK_END
  600. extern SSRResolveParamDef gSSRResolveParamDef;
  601. /** Shader used for combining SSR information from the previous frame, in order to yield better quality. */
  602. class SSRResolveMat : public RendererMaterial<SSRResolveMat>
  603. {
  604. RMAT_DEF("PPSSRResolve.bsl");
  605. public:
  606. SSRResolveMat();
  607. /**
  608. * Renders the effect with the provided parameters.
  609. *
  610. * @param[in] view Information about the view we're rendering from.
  611. * @param[in] prevFrame SSR data calculated previous frame.
  612. * @param[in] curFrame SSR data calculated this frame.
  613. * @param[in] sceneDepth Buffer containing scene depth.
  614. * @param[in] destination Render target to which to write the results to.
  615. */
  616. void execute(const RendererView& view, const SPtr<Texture>& prevFrame, const SPtr<Texture>& curFrame,
  617. const SPtr<Texture>& sceneDepth, const SPtr<RenderTarget>& destination);
  618. /**
  619. * Returns the material variation matching the provided parameters.
  620. *
  621. * @param[in] msaa True if the shader will operate on a multisampled surface. Note that previous
  622. * and current frame color textures must be non-MSAA, regardless of this parameter.
  623. * @return Requested variation of the material.
  624. */
  625. static SSRResolveMat* getVariation(bool msaa);
  626. private:
  627. SPtr<GpuParamBlockBuffer> mSSRParamBuffer;
  628. SPtr<GpuParamBlockBuffer> mTemporalParamBuffer;
  629. GpuParamTexture mSceneColorTexture;
  630. GpuParamTexture mPrevColorTexture;
  631. GpuParamTexture mSceneDepthTexture;
  632. GpuParamTexture mEyeAdaptationTexture;
  633. static ShaderVariation VAR_MSAA;
  634. static ShaderVariation VAR_NoMSAA;
  635. };
  636. BS_PARAM_BLOCK_BEGIN(EncodeDepthParamDef)
  637. BS_PARAM_BLOCK_ENTRY(float, gNear)
  638. BS_PARAM_BLOCK_ENTRY(float, gFar)
  639. BS_PARAM_BLOCK_END
  640. extern EncodeDepthParamDef gEncodeDepthParamDef;
  641. /**
  642. * Shader that encodes depth from a specified range into [0, 1] range, and writes the result in the alpha channel
  643. * of the output texture.
  644. */
  645. class EncodeDepthMat : public RendererMaterial<EncodeDepthMat>
  646. {
  647. RMAT_DEF("PPEncodeDepth.bsl");
  648. public:
  649. EncodeDepthMat();
  650. /**
  651. * Renders the post-process effect with the provided parameters.
  652. *
  653. * @param[in] depth Resolved (non-MSAA) depth texture to encode.
  654. * @param[in] near Near range (in view space) to start encoding the depth. Any depth lower than this will
  655. * be encoded to 1.
  656. * @param[in] far Far range (in view space) to end encoding the depth. Any depth higher than this will
  657. * be encoded to 0.
  658. * @param[in] output Output texture to write the results in. Results will be written in the alpha channel.
  659. */
  660. void execute(const SPtr<Texture>& depth, float near, float far, const SPtr<RenderTarget>& output);
  661. private:
  662. SPtr<GpuParamBlockBuffer> mParamBuffer;
  663. GpuParamTexture mInputTexture;
  664. };
  665. /**
  666. * Shader that outputs a texture that determines which pixels require per-sample evaluation. Only relevant when
  667. * rendering with MSAA enabled.
  668. */
  669. class MSAACoverageMat : public RendererMaterial<MSAACoverageMat>
  670. {
  671. RMAT_DEF("MSAACoverage.bsl");
  672. public:
  673. MSAACoverageMat();
  674. /**
  675. * Renders the effect with the provided parameters, using the currently bound render target.
  676. *
  677. * @param[in] view Information about the view we're rendering from.
  678. * @param[in] gbuffer GBuffer textures.
  679. */
  680. void execute(const RendererView& view, GBufferTextures gbuffer);
  681. /** Returns the material variation matching the provided parameters. */
  682. static MSAACoverageMat* getVariation(UINT32 msaaCount);
  683. private:
  684. GBufferParams mGBufferParams;
  685. static ShaderVariation VAR_2x;
  686. static ShaderVariation VAR_4x;
  687. static ShaderVariation VAR_8x;
  688. };
  689. /**
  690. * Converts the coverage texture output by MSAACoverageMat and writes its information in the highest bit of the
  691. * currently bound stencil buffer. This allows coverage information to be used by normal (non-compute) rendering
  692. * shaders.
  693. */
  694. class MSAACoverageStencilMat : public RendererMaterial<MSAACoverageStencilMat>
  695. {
  696. RMAT_DEF("MSAACoverageStencil.bsl");
  697. public:
  698. MSAACoverageStencilMat();
  699. /**
  700. * Renders the effect with the provided parameters, using the currently bound render target.
  701. *
  702. * @param[in] view Information about the view we're rendering from.
  703. * @param[in] coverage Coverage texture as output by MSAACoverageMat.
  704. */
  705. void execute(const RendererView& view, const SPtr<Texture>& coverage);
  706. private:
  707. GpuParamTexture mCoverageTexParam;
  708. };
  709. /** @} */
  710. }}