BsPostProcessing.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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. BS_PARAM_BLOCK_BEGIN(BuildHiZFParamDef)
  316. BS_PARAM_BLOCK_ENTRY(Vector2, gHalfPixelOffset)
  317. BS_PARAM_BLOCK_ENTRY(int, gMipLevel)
  318. BS_PARAM_BLOCK_END
  319. extern BuildHiZFParamDef sBuildHiZFParamDef;
  320. /** Shader that calculates a single level of the hierarchical Z mipmap chain. */
  321. class BuildHiZMat : public RendererMaterial<BuildHiZMat>
  322. {
  323. RMAT_DEF("PPBuildHiZ.bsl");
  324. public:
  325. BuildHiZMat();
  326. /**
  327. * Renders the post-process effect with the provided parameters.
  328. *
  329. * @param[in] source Input depth texture to use as the source.
  330. * @param[in] srcMip Mip level to read from the @p source texture.
  331. * @param[in] srcRect Rectangle in normalized coordinates, describing from which portion of the source
  332. * texture to read the input.
  333. * @param[in] dstRect Destination rectangle to limit the writes to.
  334. * @param[in] output Output target to which to write to results.
  335. */
  336. void execute(const SPtr<Texture>& source, UINT32 srcMip, const Rect2& srcRect, const Rect2& dstRect,
  337. const SPtr<RenderTexture>& output);
  338. private:
  339. GpuParamTexture mInputTexture;
  340. SPtr<GpuParamBlockBuffer> mParamBuffer;
  341. };
  342. BS_PARAM_BLOCK_BEGIN(FXAAParamDef)
  343. BS_PARAM_BLOCK_ENTRY(Vector2, gInvTexSize)
  344. BS_PARAM_BLOCK_END
  345. extern FXAAParamDef gFXAAParamDef;
  346. /** Shader that performs Fast Approximate anti-aliasing. */
  347. class FXAAMat : public RendererMaterial<FXAAMat>
  348. {
  349. RMAT_DEF("PPFXAA.bsl");
  350. public:
  351. FXAAMat();
  352. /**
  353. * Renders the post-process effect with the provided parameters.
  354. *
  355. * @param[in] source Input texture to apply FXAA to.
  356. * @param[in] destination Output target to which to write the antialiased image to.
  357. */
  358. void execute(const SPtr<Texture>& source, const SPtr<RenderTarget>& destination);
  359. private:
  360. SPtr<GpuParamBlockBuffer> mParamBuffer;
  361. GpuParamTexture mInputTexture;
  362. };
  363. BS_PARAM_BLOCK_BEGIN(SSAOParamDef)
  364. BS_PARAM_BLOCK_ENTRY(float, gSampleRadius)
  365. BS_PARAM_BLOCK_ENTRY(float, gWorldSpaceRadiusMask)
  366. BS_PARAM_BLOCK_ENTRY(Vector2, gTanHalfFOV)
  367. BS_PARAM_BLOCK_ENTRY(Vector2, gRandomTileScale)
  368. BS_PARAM_BLOCK_ENTRY(float, gCotHalfFOV)
  369. BS_PARAM_BLOCK_ENTRY(float, gBias)
  370. BS_PARAM_BLOCK_ENTRY(Vector2, gDownsampledPixelSize)
  371. BS_PARAM_BLOCK_ENTRY(Vector2, gFadeMultiplyAdd)
  372. BS_PARAM_BLOCK_ENTRY(float, gPower)
  373. BS_PARAM_BLOCK_ENTRY(float, gIntensity)
  374. BS_PARAM_BLOCK_END
  375. extern SSAOParamDef gSSAOParamDef;
  376. /** Textures used as input when calculating SSAO. */
  377. struct SSAOTextureInputs
  378. {
  379. /** Full resolution scene depth. Only used by final SSAO pass. */
  380. SPtr<Texture> sceneDepth;
  381. /** Full resolution buffer containing scene normals. Only used by final SSAO pass. */
  382. SPtr<Texture> sceneNormals;
  383. /** Precalculated texture containing downsampled normals/depth, to be used for AO input. */
  384. SPtr<Texture> aoSetup;
  385. /** Texture containing AO from the previous pass. Only used if upsampling is enabled. */
  386. SPtr<Texture> aoDownsampled;
  387. /** Tileable texture containing random rotations that will be applied to AO samples. */
  388. SPtr<Texture> randomRotations;
  389. };
  390. /** Shader that computes ambient occlusion using screen based methods. */
  391. class SSAOMat : public RendererMaterial<SSAOMat>
  392. {
  393. RMAT_DEF("PPSSAO.bsl");
  394. public:
  395. SSAOMat();
  396. /**
  397. * Renders the post-process effect with the provided parameters.
  398. *
  399. * @param[in] view Information about the view we're rendering from.
  400. * @param[in] textures Set of textures to be used as input. Which textures are used depends on the
  401. * template parameters of this class.
  402. * @param[in] destination Output texture to which to write the ambient occlusion data to.
  403. * @param[in] settings Settings used to control the ambient occlusion effect.
  404. */
  405. void execute(const RendererView& view, const SSAOTextureInputs& textures, const SPtr<RenderTexture>& destination,
  406. const AmbientOcclusionSettings& settings);
  407. /**
  408. * Returns the material variation matching the provided parameters.
  409. *
  410. * @param upsample If true the shader will blend the calculated AO with AO data from the previous pass.
  411. * @param finalPass If true the shader will use the full screen normal/depth information and perform
  412. * intensity scaling, as well as distance fade. Otherwise the shader will use the
  413. * downsampled AO setup information, with no scaling/fade.
  414. * @param quality Integer in range [0, 4] that controls the quality of SSAO sampling. Higher numbers yield
  415. * better quality at the cost of performance.
  416. */
  417. static SSAOMat* getVariation(bool upsample, bool finalPass, int quality);
  418. private:
  419. SPtr<GpuParamBlockBuffer> mParamBuffer;
  420. GpuParamTexture mDepthTexture;
  421. GpuParamTexture mNormalsTexture;
  422. GpuParamTexture mDownsampledAOTexture;
  423. GpuParamTexture mSetupAOTexture;
  424. GpuParamTexture mRandomTexture;
  425. #define VARIATION(QUALITY) \
  426. static ShaderVariation VAR_Upsample_Final_Quality##QUALITY; \
  427. static ShaderVariation VAR_Upsample_NoFinal_Quality##QUALITY; \
  428. static ShaderVariation VAR_NoUpsample_Final_Quality##QUALITY; \
  429. static ShaderVariation VAR_NoUpsample_NoFinal_Quality##QUALITY; \
  430. VARIATION(0)
  431. VARIATION(1)
  432. VARIATION(2)
  433. VARIATION(3)
  434. VARIATION(4)
  435. #undef VARIATION
  436. };
  437. BS_PARAM_BLOCK_BEGIN(SSAODownsampleParamDef)
  438. BS_PARAM_BLOCK_ENTRY(Vector2, gPixelSize)
  439. BS_PARAM_BLOCK_ENTRY(float, gInvDepthThreshold)
  440. BS_PARAM_BLOCK_END
  441. extern SSAODownsampleParamDef gSSAODownsampleParamDef;
  442. /**
  443. * Shader that downsamples the depth & normal buffer and stores their results in a common texture, to be consumed
  444. * by SSAOMat.
  445. */
  446. class SSAODownsampleMat : public RendererMaterial<SSAODownsampleMat>
  447. {
  448. RMAT_DEF("PPSSAODownsample.bsl");
  449. public:
  450. SSAODownsampleMat();
  451. /**
  452. * Renders the post-process effect with the provided parameters.
  453. *
  454. * @param[in] view Information about the view we're rendering from.
  455. * @param[in] sceneDepth Input texture containing scene depth.
  456. * @param[in] sceneNormals Input texture containing scene world space normals.
  457. * @param[in] destination Output texture to which to write the downsampled data to.
  458. * @param[in] depthRange Valid depth range (in view space) within which nearby samples will be averaged.
  459. */
  460. void execute(const RendererView& view, const SPtr<Texture>& sceneDepth, const SPtr<Texture>& sceneNormals,
  461. const SPtr<RenderTexture>& destination, float depthRange);
  462. private:
  463. SPtr<GpuParamBlockBuffer> mParamBuffer;
  464. GpuParamTexture mDepthTexture;
  465. GpuParamTexture mNormalsTexture;
  466. };
  467. BS_PARAM_BLOCK_BEGIN(SSAOBlurParamDef)
  468. BS_PARAM_BLOCK_ENTRY(Vector2, gPixelSize)
  469. BS_PARAM_BLOCK_ENTRY(Vector2, gPixelOffset)
  470. BS_PARAM_BLOCK_ENTRY(float, gInvDepthThreshold)
  471. BS_PARAM_BLOCK_END
  472. extern SSAOBlurParamDef gSSAOBlurParamDef;
  473. /**
  474. * Shaders that blurs the ambient occlusion output, in order to hide the noise caused by the randomization texture.
  475. */
  476. class SSAOBlurMat : public RendererMaterial<SSAOBlurMat>
  477. {
  478. RMAT_DEF("PPSSAOBlur.bsl");
  479. public:
  480. SSAOBlurMat();
  481. /**
  482. * Renders the post-process effect with the provided parameters.
  483. *
  484. * @param[in] view Information about the view we're rendering from.
  485. * @param[in] ao Input texture containing ambient occlusion data to be blurred.
  486. * @param[in] sceneDepth Input texture containing scene depth.
  487. * @param[in] destination Output texture to which to write the blurred data to.
  488. * @param[in] depthRange Valid depth range (in view space) within which nearby samples will be averaged.
  489. */
  490. void execute(const RendererView& view, const SPtr<Texture>& ao, const SPtr<Texture>& sceneDepth,
  491. const SPtr<RenderTexture>& destination, float depthRange);
  492. /** Returns the material variation matching the provided parameters. */
  493. static SSAOBlurMat* getVariation(bool horizontal);
  494. private:
  495. SPtr<GpuParamBlockBuffer> mParamBuffer;
  496. GpuParamTexture mAOTexture;
  497. GpuParamTexture mDepthTexture;
  498. static ShaderVariation VAR_Vertical;
  499. static ShaderVariation VAR_Horizontal;
  500. };
  501. BS_PARAM_BLOCK_BEGIN(SSRStencilParamDef)
  502. BS_PARAM_BLOCK_ENTRY(Vector2, gRoughnessScaleBias)
  503. BS_PARAM_BLOCK_END
  504. extern SSRStencilParamDef gSSRStencilParamDef;
  505. /** Shader used for marking which parts of the screen require screen space reflections. */
  506. class SSRStencilMat : public RendererMaterial<SSRStencilMat>
  507. {
  508. RMAT_DEF("PPSSRStencil.bsl");
  509. public:
  510. SSRStencilMat();
  511. /**
  512. * Renders the effect with the provided parameters, using the currently bound render target.
  513. *
  514. * @param[in] view Information about the view we're rendering from.
  515. * @param[in] gbuffer GBuffer textures.
  516. * @param[in] settings Parameters used for controling the SSR effect.
  517. */
  518. void execute(const RendererView& view, GBufferTextures gbuffer, const ScreenSpaceReflectionsSettings& settings);
  519. /**
  520. * Returns the material variation matching the provided parameters.
  521. *
  522. * @param[in] msaa True if the shader will operate on a multisampled surface.
  523. * @param[in] singleSampleMSAA Only relevant of @p msaa is true. When enabled only the first sample will be
  524. * evaluated. Otherwise all samples will be evaluated.
  525. * @return Requested variation of the material.
  526. */
  527. static SSRStencilMat* getVariation(bool msaa, bool singleSampleMSAA);
  528. private:
  529. SPtr<GpuParamBlockBuffer> mParamBuffer;
  530. GBufferParams mGBufferParams;
  531. static ShaderVariation VAR_FullMSAA;
  532. static ShaderVariation VAR_SingleMSAA;
  533. static ShaderVariation VAR_NoMSAA;
  534. };
  535. BS_PARAM_BLOCK_BEGIN(SSRTraceParamDef)
  536. BS_PARAM_BLOCK_ENTRY(Vector4, gNDCToHiZUV)
  537. BS_PARAM_BLOCK_ENTRY(Vector2, gHiZUVToScreenUV)
  538. BS_PARAM_BLOCK_ENTRY(Vector2I, gHiZSize)
  539. BS_PARAM_BLOCK_ENTRY(int, gHiZNumMips)
  540. BS_PARAM_BLOCK_ENTRY(float, gIntensity)
  541. BS_PARAM_BLOCK_ENTRY(Vector2, gRoughnessScaleBias)
  542. BS_PARAM_BLOCK_ENTRY(int, gTemporalJitter)
  543. BS_PARAM_BLOCK_END
  544. extern SSRTraceParamDef gSSRTraceParamDef;
  545. /** Shader used for tracing rays for screen space reflections. */
  546. class SSRTraceMat : public RendererMaterial<SSRTraceMat>
  547. {
  548. RMAT_DEF("PPSSRTrace.bsl");
  549. public:
  550. SSRTraceMat();
  551. /**
  552. * Renders the effect with the provided parameters.
  553. *
  554. * @param[in] view Information about the view we're rendering from.
  555. * @param[in] gbuffer GBuffer textures.
  556. * @param[in] sceneColor Scene color texture.
  557. * @param[in] hiZ Hierarchical Z buffer.
  558. * @param[in] settings Parameters used for controling the SSR effect.
  559. * @param[in] destination Render target to which to write the results to.
  560. */
  561. void execute(const RendererView& view, GBufferTextures gbuffer, const SPtr<Texture>& sceneColor,
  562. const SPtr<Texture>& hiZ, const ScreenSpaceReflectionsSettings& settings,
  563. const SPtr<RenderTarget>& destination);
  564. /**
  565. * Calculates a scale & bias that is used for transforming roughness into a fade out value. Anything that is below
  566. * @p maxRoughness will have the fade value of 1. Values above @p maxRoughness is slowly fade out over a range that
  567. * is 1/2 the length of @p maxRoughness.
  568. */
  569. static Vector2 calcRoughnessFadeScaleBias(float maxRoughness);
  570. /**
  571. * Returns the material variation matching the provided parameters.
  572. *
  573. * @param[in] quality Determines how many rays to trace. In range [0, 4].
  574. * @param[in] msaa True if the shader will operate on a multisampled surface.
  575. * @param[in] singleSampleMSAA Only relevant of @p msaa is true. When enabled only the first sample will be
  576. * evaluated. Otherwise all samples will be evaluated.
  577. * @return Requested variation of the material.
  578. */
  579. static SSRTraceMat* getVariation(UINT32 quality, bool msaa, bool singleSampleMSAA = false);
  580. private:
  581. SPtr<GpuParamBlockBuffer> mParamBuffer;
  582. GBufferParams mGBufferParams;
  583. GpuParamTexture mSceneColorTexture;
  584. GpuParamTexture mHiZTexture;
  585. #define VARIATION(QUALITY) \
  586. static ShaderVariation VAR_NoMSAA_Quality##QUALITY; \
  587. static ShaderVariation VAR_FullMSAA_Quality##QUALITY; \
  588. static ShaderVariation VAR_SingleMSAA_Quality##QUALITY; \
  589. VARIATION(0)
  590. VARIATION(1)
  591. VARIATION(2)
  592. VARIATION(3)
  593. VARIATION(4)
  594. #undef VARIATION
  595. };
  596. BS_PARAM_BLOCK_BEGIN(TemporalResolveParamDef)
  597. BS_PARAM_BLOCK_ENTRY_ARRAY(float, gSampleWeights, 9)
  598. BS_PARAM_BLOCK_ENTRY_ARRAY(float, gSampleWeightsLowpass, 9)
  599. BS_PARAM_BLOCK_END
  600. extern TemporalResolveParamDef gTemporalResolveParamDef;
  601. BS_PARAM_BLOCK_BEGIN(SSRResolveParamDef)
  602. BS_PARAM_BLOCK_ENTRY(Vector2, gSceneDepthTexelSize)
  603. BS_PARAM_BLOCK_ENTRY(Vector2, gSceneColorTexelSize)
  604. BS_PARAM_BLOCK_ENTRY(float, gManualExposure)
  605. BS_PARAM_BLOCK_END
  606. extern SSRResolveParamDef gSSRResolveParamDef;
  607. /** Shader used for combining SSR information from the previous frame, in order to yield better quality. */
  608. class SSRResolveMat : public RendererMaterial<SSRResolveMat>
  609. {
  610. RMAT_DEF("PPSSRResolve.bsl");
  611. public:
  612. SSRResolveMat();
  613. /**
  614. * Renders the effect with the provided parameters.
  615. *
  616. * @param[in] view Information about the view we're rendering from.
  617. * @param[in] prevFrame SSR data calculated previous frame.
  618. * @param[in] curFrame SSR data calculated this frame.
  619. * @param[in] sceneDepth Buffer containing scene depth.
  620. * @param[in] destination Render target to which to write the results to.
  621. */
  622. void execute(const RendererView& view, const SPtr<Texture>& prevFrame, const SPtr<Texture>& curFrame,
  623. const SPtr<Texture>& sceneDepth, const SPtr<RenderTarget>& destination);
  624. /**
  625. * Returns the material variation matching the provided parameters.
  626. *
  627. * @param[in] msaa True if the shader will operate on a multisampled surface. Note that previous
  628. * and current frame color textures must be non-MSAA, regardless of this parameter.
  629. * @return Requested variation of the material.
  630. */
  631. static SSRResolveMat* getVariation(bool msaa);
  632. private:
  633. SPtr<GpuParamBlockBuffer> mSSRParamBuffer;
  634. SPtr<GpuParamBlockBuffer> mTemporalParamBuffer;
  635. GpuParamTexture mSceneColorTexture;
  636. GpuParamTexture mPrevColorTexture;
  637. GpuParamTexture mSceneDepthTexture;
  638. GpuParamTexture mEyeAdaptationTexture;
  639. static ShaderVariation VAR_MSAA;
  640. static ShaderVariation VAR_NoMSAA;
  641. };
  642. BS_PARAM_BLOCK_BEGIN(EncodeDepthParamDef)
  643. BS_PARAM_BLOCK_ENTRY(float, gNear)
  644. BS_PARAM_BLOCK_ENTRY(float, gFar)
  645. BS_PARAM_BLOCK_END
  646. extern EncodeDepthParamDef gEncodeDepthParamDef;
  647. /**
  648. * Shader that encodes depth from a specified range into [0, 1] range, and writes the result in the alpha channel
  649. * of the output texture.
  650. */
  651. class EncodeDepthMat : public RendererMaterial<EncodeDepthMat>
  652. {
  653. RMAT_DEF("PPEncodeDepth.bsl");
  654. public:
  655. EncodeDepthMat();
  656. /**
  657. * Renders the post-process effect with the provided parameters.
  658. *
  659. * @param[in] depth Resolved (non-MSAA) depth texture to encode.
  660. * @param[in] near Near range (in view space) to start encoding the depth. Any depth lower than this will
  661. * be encoded to 1.
  662. * @param[in] far Far range (in view space) to end encoding the depth. Any depth higher than this will
  663. * be encoded to 0.
  664. * @param[in] output Output texture to write the results in. Results will be written in the alpha channel.
  665. */
  666. void execute(const SPtr<Texture>& depth, float near, float far, const SPtr<RenderTarget>& output);
  667. private:
  668. SPtr<GpuParamBlockBuffer> mParamBuffer;
  669. GpuParamTexture mInputTexture;
  670. };
  671. /**
  672. * Shader that outputs a texture that determines which pixels require per-sample evaluation. Only relevant when
  673. * rendering with MSAA enabled.
  674. */
  675. class MSAACoverageMat : public RendererMaterial<MSAACoverageMat>
  676. {
  677. RMAT_DEF("MSAACoverage.bsl");
  678. public:
  679. MSAACoverageMat();
  680. /**
  681. * Renders the effect with the provided parameters, using the currently bound render target.
  682. *
  683. * @param[in] view Information about the view we're rendering from.
  684. * @param[in] gbuffer GBuffer textures.
  685. */
  686. void execute(const RendererView& view, GBufferTextures gbuffer);
  687. /** Returns the material variation matching the provided parameters. */
  688. static MSAACoverageMat* getVariation(UINT32 msaaCount);
  689. private:
  690. GBufferParams mGBufferParams;
  691. static ShaderVariation VAR_2x;
  692. static ShaderVariation VAR_4x;
  693. static ShaderVariation VAR_8x;
  694. };
  695. /**
  696. * Converts the coverage texture output by MSAACoverageMat and writes its information in the highest bit of the
  697. * currently bound stencil buffer. This allows coverage information to be used by normal (non-compute) rendering
  698. * shaders.
  699. */
  700. class MSAACoverageStencilMat : public RendererMaterial<MSAACoverageStencilMat>
  701. {
  702. RMAT_DEF("MSAACoverageStencil.bsl");
  703. public:
  704. MSAACoverageStencilMat();
  705. /**
  706. * Renders the effect with the provided parameters, using the currently bound render target.
  707. *
  708. * @param[in] view Information about the view we're rendering from.
  709. * @param[in] coverage Coverage texture as output by MSAACoverageMat.
  710. */
  711. void execute(const RendererView& view, const SPtr<Texture>& coverage);
  712. private:
  713. GpuParamTexture mCoverageTexParam;
  714. };
  715. /** @} */
  716. }}