BsPostProcessing.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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. #include "BsLightRendering.h"
  10. namespace bs { namespace ct
  11. {
  12. struct RendererViewTargetData;
  13. /** @addtogroup RenderBeast
  14. * @{
  15. */
  16. /** Contains per-camera data used by post process effects. */
  17. struct PostProcessInfo
  18. {
  19. SPtr<StandardPostProcessSettings> settings;
  20. bool settingDirty = true;
  21. };
  22. BS_PARAM_BLOCK_BEGIN(DownsampleParamDef)
  23. BS_PARAM_BLOCK_ENTRY_ARRAY(Vector2, gOffsets, 4)
  24. BS_PARAM_BLOCK_END
  25. extern DownsampleParamDef gDownsampleParamDef;
  26. /** Shader that downsamples a texture to half its size. */
  27. class DownsampleMat : public RendererMaterial<DownsampleMat>
  28. {
  29. RMAT_DEF("PPDownsample.bsl");
  30. public:
  31. DownsampleMat();
  32. /** Renders the post-process effect with the provided parameters. */
  33. void execute(const SPtr<Texture>& input, const SPtr<RenderTarget>& output);
  34. /** Returns the texture descriptor that can be used for initializing the output render target. */
  35. static POOLED_RENDER_TEXTURE_DESC getOutputDesc(const SPtr<Texture>& target);
  36. /** Returns the downsample material variation matching the provided parameters. */
  37. static DownsampleMat* getVariation(UINT32 quality, bool msaa);
  38. private:
  39. SPtr<GpuParamBlockBuffer> mParamBuffer;
  40. GpuParamTexture mInputTexture;
  41. static ShaderVariation VAR_LowQuality_NoMSAA;
  42. static ShaderVariation VAR_LowQuality_MSAA;
  43. static ShaderVariation VAR_HighQuality_NoMSAA;
  44. static ShaderVariation VAR_HighQuality_MSAA;
  45. };
  46. BS_PARAM_BLOCK_BEGIN(EyeAdaptHistogramParamDef)
  47. BS_PARAM_BLOCK_ENTRY(Vector4I, gPixelOffsetAndSize)
  48. BS_PARAM_BLOCK_ENTRY(Vector2, gHistogramParams)
  49. BS_PARAM_BLOCK_ENTRY(Vector2I, gThreadGroupCount)
  50. BS_PARAM_BLOCK_END
  51. extern EyeAdaptHistogramParamDef gEyeAdaptHistogramParamDef;
  52. /** Shader that creates a luminance histogram used for eye adaptation. */
  53. class EyeAdaptHistogramMat : public RendererMaterial<EyeAdaptHistogramMat>
  54. {
  55. RMAT_DEF("PPEyeAdaptHistogram.bsl");
  56. public:
  57. EyeAdaptHistogramMat();
  58. /** Executes the post-process effect with the provided parameters. */
  59. void execute(const SPtr<Texture>& input, const SPtr<Texture>& output, const AutoExposureSettings& settings);
  60. /** Returns the texture descriptor that can be used for initializing the output render target. */
  61. static POOLED_RENDER_TEXTURE_DESC getOutputDesc(const SPtr<Texture>& target);
  62. /** Calculates the number of thread groups that need to execute to cover the provided texture. */
  63. static Vector2I getThreadGroupCount(const SPtr<Texture>& target);
  64. /**
  65. * Returns a vector containing scale and offset (in that order) that will be applied to luminance values
  66. * to determine their position in the histogram.
  67. */
  68. static Vector2 getHistogramScaleOffset(const AutoExposureSettings& settings);
  69. static const UINT32 THREAD_GROUP_SIZE_X = 8;
  70. static const UINT32 THREAD_GROUP_SIZE_Y = 8;
  71. static const UINT32 HISTOGRAM_NUM_TEXELS = (THREAD_GROUP_SIZE_X * THREAD_GROUP_SIZE_Y) / 4;
  72. private:
  73. SPtr<GpuParamBlockBuffer> mParamBuffer;
  74. GpuParamTexture mSceneColor;
  75. GpuParamLoadStoreTexture mOutputTex;
  76. static const UINT32 LOOP_COUNT_X = 8;
  77. static const UINT32 LOOP_COUNT_Y = 8;
  78. };
  79. BS_PARAM_BLOCK_BEGIN(EyeAdaptHistogramReduceParamDef)
  80. BS_PARAM_BLOCK_ENTRY(int, gThreadGroupCount)
  81. BS_PARAM_BLOCK_END
  82. extern EyeAdaptHistogramReduceParamDef gEyeAdaptHistogramReduceParamDef;
  83. /** Shader that reduces the luminance histograms created by EyeAdaptHistogramMat into a single histogram. */
  84. class EyeAdaptHistogramReduceMat : public RendererMaterial<EyeAdaptHistogramReduceMat>
  85. {
  86. RMAT_DEF("PPEyeAdaptHistogramReduce.bsl");
  87. public:
  88. EyeAdaptHistogramReduceMat();
  89. /** Executes the post-process effect with the provided parameters. */
  90. void execute(const SPtr<Texture>& sceneColor, const SPtr<Texture>& histogram, const SPtr<Texture>& prevFrame,
  91. const SPtr<RenderTarget>& output);
  92. /** Returns the texture descriptor that can be used for initializing the output render target. */
  93. static POOLED_RENDER_TEXTURE_DESC getOutputDesc();
  94. private:
  95. SPtr<GpuParamBlockBuffer> mParamBuffer;
  96. GpuParamTexture mHistogramTex;
  97. GpuParamTexture mEyeAdaptationTex;
  98. };
  99. BS_PARAM_BLOCK_BEGIN(EyeAdaptationParamDef)
  100. BS_PARAM_BLOCK_ENTRY_ARRAY(Vector4, gEyeAdaptationParams, 3)
  101. BS_PARAM_BLOCK_END
  102. extern EyeAdaptationParamDef gEyeAdaptationParamDef;
  103. /** Shader that computes the eye adaptation value based on scene luminance. */
  104. class EyeAdaptationMat : public RendererMaterial<EyeAdaptationMat>
  105. {
  106. RMAT_DEF("PPEyeAdaptation.bsl");
  107. public:
  108. EyeAdaptationMat();
  109. /** Executes the post-process effect with the provided parameters. */
  110. void execute(const SPtr<Texture>& reducedHistogram, const SPtr<RenderTarget>& output, float frameDelta,
  111. const AutoExposureSettings& settings, float exposureScale);
  112. /** Returns the texture descriptor that can be used for initializing the output render target. */
  113. static POOLED_RENDER_TEXTURE_DESC getOutputDesc();
  114. private:
  115. SPtr<GpuParamBlockBuffer> mParamBuffer;
  116. GpuParamTexture mReducedHistogramTex;
  117. };
  118. BS_PARAM_BLOCK_BEGIN(CreateTonemapLUTParamDef)
  119. BS_PARAM_BLOCK_ENTRY_ARRAY(Vector4, gTonemapParams, 2)
  120. BS_PARAM_BLOCK_ENTRY(float, gGammaAdjustment)
  121. BS_PARAM_BLOCK_ENTRY(int, gGammaCorrectionType)
  122. BS_PARAM_BLOCK_ENTRY(Vector3, gSaturation)
  123. BS_PARAM_BLOCK_ENTRY(Vector3, gContrast)
  124. BS_PARAM_BLOCK_ENTRY(Vector3, gGain)
  125. BS_PARAM_BLOCK_ENTRY(Vector3, gOffset)
  126. BS_PARAM_BLOCK_END
  127. extern CreateTonemapLUTParamDef gCreateTonemapLUTParamDef;
  128. BS_PARAM_BLOCK_BEGIN(WhiteBalanceParamDef)
  129. BS_PARAM_BLOCK_ENTRY(float, gWhiteTemp)
  130. BS_PARAM_BLOCK_ENTRY(float, gWhiteOffset)
  131. BS_PARAM_BLOCK_END
  132. extern WhiteBalanceParamDef gWhiteBalanceParamDef;
  133. /**
  134. * Shader that creates a 3D lookup texture that is used to apply tonemapping, color grading, white balancing and gamma
  135. * correction.
  136. */
  137. class CreateTonemapLUTMat : public RendererMaterial<CreateTonemapLUTMat>
  138. {
  139. RMAT_DEF("PPCreateTonemapLUT.bsl");
  140. public:
  141. CreateTonemapLUTMat();
  142. /** Executes the post-process effect with the provided parameters. */
  143. void execute(const SPtr<Texture>& output, const StandardPostProcessSettings& settings);
  144. /** Returns the texture descriptor that can be used for initializing the output render target. */
  145. static POOLED_RENDER_TEXTURE_DESC getOutputDesc();
  146. /** Size of the 3D color lookup table. */
  147. static const UINT32 LUT_SIZE = 32;
  148. private:
  149. SPtr<GpuParamBlockBuffer> mParamBuffer;
  150. SPtr<GpuParamBlockBuffer> mWhiteBalanceParamBuffer;
  151. GpuParamLoadStoreTexture mOutputTex;
  152. };
  153. BS_PARAM_BLOCK_BEGIN(TonemappingParamDef)
  154. BS_PARAM_BLOCK_ENTRY(float, gRawGamma)
  155. BS_PARAM_BLOCK_ENTRY(float, gManualExposureScale)
  156. BS_PARAM_BLOCK_ENTRY(int, gNumSamples)
  157. BS_PARAM_BLOCK_END
  158. extern TonemappingParamDef gTonemappingParamDef;
  159. /** Shader that applies tonemapping and converts a HDR image into a LDR image. */
  160. class TonemappingMat : public RendererMaterial<TonemappingMat>
  161. {
  162. RMAT_DEF("PPTonemapping.bsl");
  163. public:
  164. TonemappingMat();
  165. /** Executes the post-process effect with the provided parameters. */
  166. void execute(const SPtr<Texture>& sceneColor, const SPtr<Texture>& eyeAdaptation, const SPtr<Texture>& colorLUT,
  167. const SPtr<RenderTarget>& output, const StandardPostProcessSettings& settings);
  168. /** Returns the material variation matching the provided parameters. */
  169. static TonemappingMat* getVariation(bool gammaOnly, bool autoExposure, bool MSAA);
  170. private:
  171. SPtr<GpuParamBlockBuffer> mParamBuffer;
  172. GpuParamTexture mInputTex;
  173. GpuParamTexture mColorLUT;
  174. GpuParamTexture mEyeAdaptationTex;
  175. static ShaderVariation VAR_Gamma_AutoExposure_MSAA;
  176. static ShaderVariation VAR_Gamma_AutoExposure_NoMSAA;
  177. static ShaderVariation VAR_Gamma_NoAutoExposure_MSAA;
  178. static ShaderVariation VAR_Gamma_NoAutoExposure_NoMSAA;
  179. static ShaderVariation VAR_NoGamma_AutoExposure_MSAA;
  180. static ShaderVariation VAR_NoGamma_AutoExposure_NoMSAA;
  181. static ShaderVariation VAR_NoGamma_NoAutoExposure_MSAA;
  182. static ShaderVariation VAR_NoGamma_NoAutoExposure_NoMSAA;
  183. };
  184. const int MAX_BLUR_SAMPLES = 128;
  185. BS_PARAM_BLOCK_BEGIN(GaussianBlurParamDef)
  186. BS_PARAM_BLOCK_ENTRY_ARRAY(Vector4, gSampleOffsets, (MAX_BLUR_SAMPLES + 1) / 2)
  187. BS_PARAM_BLOCK_ENTRY_ARRAY(Vector4, gSampleWeights, (MAX_BLUR_SAMPLES + 3) / 4)
  188. BS_PARAM_BLOCK_ENTRY(int, gNumSamples)
  189. BS_PARAM_BLOCK_END
  190. extern GaussianBlurParamDef gGaussianBlurParamDef;
  191. /** Shader that performs Gaussian blur filtering on the provided texture. */
  192. class GaussianBlurMat : public RendererMaterial<GaussianBlurMat>
  193. {
  194. // Direction of the Gaussian filter pass
  195. enum Direction
  196. {
  197. DirVertical,
  198. DirHorizontal
  199. };
  200. RMAT_DEF("PPGaussianBlur.bsl");
  201. public:
  202. GaussianBlurMat();
  203. /**
  204. * Renders the post-process effect with the provided parameters.
  205. *
  206. * @param[in] source Input texture to blur.
  207. * @param[in] filterSize Size of the blurring filter, in percent of the source texture. In range [0, 1].
  208. * @param[in] destination Output texture to which to write the blurred image to.
  209. */
  210. void execute(const SPtr<Texture>& source, float filterSize, const SPtr<RenderTexture>& destination);
  211. private:
  212. /** Calculates weights and offsets for the standard distribution of the specified filter size. */
  213. static UINT32 calcStdDistribution(float filterRadius, std::array<float, MAX_BLUR_SAMPLES>& weights,
  214. std::array<float, MAX_BLUR_SAMPLES>& offsets);
  215. /** Calculates the radius of the blur kernel depending on the source texture size and provided scale. */
  216. static float calcKernelRadius(const SPtr<Texture>& source, float scale, Direction filterDir);
  217. SPtr<GpuParamBlockBuffer> mParamBuffer;
  218. GpuParamTexture mInputTexture;
  219. };
  220. BS_PARAM_BLOCK_BEGIN(GaussianDOFParamDef)
  221. BS_PARAM_BLOCK_ENTRY(float, gNearBlurPlane)
  222. BS_PARAM_BLOCK_ENTRY(float, gFarBlurPlane)
  223. BS_PARAM_BLOCK_ENTRY(float, gInvNearBlurRange)
  224. BS_PARAM_BLOCK_ENTRY(float, gInvFarBlurRange)
  225. BS_PARAM_BLOCK_ENTRY(Vector2, gHalfPixelOffset)
  226. BS_PARAM_BLOCK_END
  227. extern GaussianDOFParamDef sGaussianDOFParamDef;
  228. /**
  229. * Shader that masks pixels from the input color texture into one or two output textures. The masking is done by
  230. * determining if the pixel falls into near or far unfocused plane, as determined by depth-of-field parameters. User
  231. * can pick whether to output pixels just on the near plane, just on the far plane, or both.
  232. *
  233. */
  234. class GaussianDOFSeparateMat : public RendererMaterial<GaussianDOFSeparateMat>
  235. {
  236. RMAT_DEF("PPGaussianDOFSeparate.bsl");
  237. public:
  238. GaussianDOFSeparateMat();
  239. /**
  240. * Renders the post-process effect with the provided parameters.
  241. *
  242. * @param[in] color Input color texture to process.
  243. * @param[in] depth Input depth buffer texture that will be used for determining pixel depth.
  244. * @param[in] view View through which the depth of field effect is viewed.
  245. * @param[in] settings Settings used to control depth of field rendering.
  246. */
  247. void execute(const SPtr<Texture>& color, const SPtr<Texture>& depth, const RendererView& view,
  248. const DepthOfFieldSettings& settings);
  249. /**
  250. * Returns the texture generated after the shader was executed. Only valid to call this in-between calls to
  251. * execute() & release(), with @p idx value 0 or 1.
  252. */
  253. SPtr<PooledRenderTexture> getOutput(UINT32 idx);
  254. /**
  255. * Releases the interally allocated output render textures. Must be called after each call to execute(), when the
  256. * caller is done using the textures.
  257. */
  258. void release();
  259. /**
  260. * Returns the material variation matching the provided parameters.
  261. *
  262. * @param near If true, near plane pixels are output to the first render target.
  263. * @param far If true, far plane pixels are output to the first render target. If @p near is also enabled, the
  264. * pixels are output to the second render target instead.
  265. */
  266. static GaussianDOFSeparateMat* getVariation(bool near, bool far);
  267. private:
  268. SPtr<GpuParamBlockBuffer> mParamBuffer;
  269. GpuParamTexture mColorTexture;
  270. GpuParamTexture mDepthTexture;
  271. SPtr<PooledRenderTexture> mOutput0;
  272. SPtr<PooledRenderTexture> mOutput1;
  273. static ShaderVariation VAR_Near_Far;
  274. static ShaderVariation VAR_NoNear_Far;
  275. static ShaderVariation VAR_Near_NoFar;
  276. };
  277. /**
  278. * Shader that combines pixels for near unfocused, focused and far unfocused planes, as calculated by
  279. * GaussianDOFSeparateMat. Outputs final depth-of-field filtered image.
  280. */
  281. class GaussianDOFCombineMat : public RendererMaterial<GaussianDOFCombineMat>
  282. {
  283. RMAT_DEF("PPGaussianDOFCombine.bsl");
  284. public:
  285. GaussianDOFCombineMat();
  286. /**
  287. * Renders the post-process effect with the provided parameters.
  288. *
  289. * @param[in] focused Input texture containing focused (default) scene color.
  290. * @param[in] near Input texture containing filtered (blurred) values for the unfocused foreground area.
  291. * Can be null if no near plane needs to be blended.
  292. * @param[in] far Input texture containing filtered (blurred) values for the unfocused background area.
  293. * Can be null if no far plane needs to be blended.
  294. * @param[in] depth Input depth buffer texture that will be used for determining pixel depth.
  295. * @param[in] output Texture to output the results to.
  296. * @param[in] view View through which the depth of field effect is viewed.
  297. * @param[in] settings Settings used to control depth of field rendering.
  298. */
  299. void execute(const SPtr<Texture>& focused, const SPtr<Texture>& near, const SPtr<Texture>& far,
  300. const SPtr<Texture>& depth, const SPtr<RenderTarget>& output, const RendererView& view,
  301. const DepthOfFieldSettings& settings);
  302. /**
  303. * Returns the material variation matching the provided parameters.
  304. *
  305. * @param near If true, near plane pixels are read from the near plane texture, otherwise near plane is assumed
  306. * not to exist.
  307. * @param far If true, far plane pixels are read from the far plane texture, otherwise far plane is assumed not
  308. * to exist.
  309. */
  310. static GaussianDOFCombineMat* getVariation(bool near, bool far);
  311. private:
  312. SPtr<GpuParamBlockBuffer> mParamBuffer;
  313. GpuParamTexture mFocusedTexture;
  314. GpuParamTexture mNearTexture;
  315. GpuParamTexture mFarTexture;
  316. GpuParamTexture mDepthTexture;
  317. static ShaderVariation VAR_Near_Far;
  318. static ShaderVariation VAR_NoNear_Far;
  319. static ShaderVariation VAR_Near_NoFar;
  320. };
  321. /** Shader that calculates a single level of the hierarchical Z mipmap chain. */
  322. class BuildHiZMat : public RendererMaterial<BuildHiZMat>
  323. {
  324. RMAT_DEF("PPBuildHiZ.bsl");
  325. public:
  326. BuildHiZMat();
  327. /**
  328. * Renders the post-process effect with the provided parameters.
  329. *
  330. * @param[in] source Input depth texture to use as the source.
  331. * @param[in] srcMip Mip level to read from the @p source texture.
  332. * @param[in] srcRect Rectangle in normalized coordinates, describing from which portion of the source
  333. * texture to read the input.
  334. * @param[in] dstRect Destination rectangle to limit the writes to.
  335. * @param[in] output Output target to which to write to results.
  336. */
  337. void execute(const SPtr<Texture>& source, UINT32 srcMip, const Rect2& srcRect, const Rect2& dstRect,
  338. const SPtr<RenderTexture>& output);
  339. private:
  340. GpuParamTexture mInputTexture;
  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, GBufferInput gbuffer, const ScreenSpaceReflectionsSettings& settings);
  519. private:
  520. SPtr<GpuParamBlockBuffer> mParamBuffer;
  521. GBufferParams mGBufferParams;
  522. };
  523. BS_PARAM_BLOCK_BEGIN(SSRTraceParamDef)
  524. BS_PARAM_BLOCK_ENTRY(Vector4, gNDCToHiZUV)
  525. BS_PARAM_BLOCK_ENTRY(Vector2, gHiZUVToScreenUV)
  526. BS_PARAM_BLOCK_ENTRY(Vector2I, gHiZSize)
  527. BS_PARAM_BLOCK_ENTRY(int, gHiZNumMips)
  528. BS_PARAM_BLOCK_ENTRY(float, gIntensity)
  529. BS_PARAM_BLOCK_ENTRY(Vector2, gRoughnessScaleBias)
  530. BS_PARAM_BLOCK_END
  531. extern SSRTraceParamDef gSSRTraceParamDef;
  532. /** Shader used for tracing rays for screen space reflections. */
  533. class SSRTraceMat : public RendererMaterial<SSRTraceMat>
  534. {
  535. RMAT_DEF("PPSSRTrace.bsl");
  536. public:
  537. SSRTraceMat();
  538. /**
  539. * Renders the effect with the provided parameters.
  540. *
  541. * @param[in] view Information about the view we're rendering from.
  542. * @param[in] gbuffer GBuffer textures.
  543. * @param[in] sceneColor Scene color texture.
  544. * @param[in] hiZ Hierarchical Z buffer.
  545. * @param[in] settings Parameters used for controling the SSR effect.
  546. * @param[in] destination Render target to which to write the results to.
  547. */
  548. void execute(const RendererView& view, GBufferInput gbuffer, const SPtr<Texture>& sceneColor,
  549. const SPtr<Texture>& hiZ, const ScreenSpaceReflectionsSettings& settings,
  550. const SPtr<RenderTarget>& destination);
  551. /**
  552. * Calculates a scale & bias that is used for transforming roughness into a fade out value. Anything that is below
  553. * @p maxRoughness will have the fade value of 1. Values above @p maxRoughness is slowly fade out over a range that
  554. * is 1/2 the length of @p maxRoughness.
  555. */
  556. static Vector2 calcRoughnessFadeScaleBias(float maxRoughness);
  557. private:
  558. SPtr<GpuParamBlockBuffer> mParamBuffer;
  559. GBufferParams mGBufferParams;
  560. GpuParamTexture mSceneColorTexture;
  561. GpuParamTexture mHiZTexture;
  562. };
  563. BS_PARAM_BLOCK_BEGIN(TemporalResolveParamDef)
  564. BS_PARAM_BLOCK_ENTRY_ARRAY(float, gSampleWeights, 9)
  565. BS_PARAM_BLOCK_ENTRY_ARRAY(float, gSampleWeightsLowpass, 9)
  566. BS_PARAM_BLOCK_END
  567. extern TemporalResolveParamDef gTemporalResolveParamDef;
  568. BS_PARAM_BLOCK_BEGIN(SSRResolveParamDef)
  569. BS_PARAM_BLOCK_ENTRY(Vector2, gSceneDepthTexelSize)
  570. BS_PARAM_BLOCK_ENTRY(Vector2, gSceneColorTexelSize)
  571. BS_PARAM_BLOCK_ENTRY(float, gManualExposure)
  572. BS_PARAM_BLOCK_END
  573. extern SSRResolveParamDef gSSRResolveParamDef;
  574. /** Shader used for combining SSR information from the previous frame, in order to yield better quality. */
  575. class SSRResolveMat : public RendererMaterial<SSRResolveMat>
  576. {
  577. RMAT_DEF("PPSSRResolve.bsl");
  578. public:
  579. SSRResolveMat();
  580. /**
  581. * Renders the effect with the provided parameters.
  582. *
  583. * @param[in] view Information about the view we're rendering from.
  584. * @param[in] prevFrame SSR data calculated previous frame.
  585. * @param[in] curFrame SSR data calculated this frame.
  586. * @param[in] sceneDepth Buffer containing scene depth.
  587. * @param[in] destination Render target to which to write the results to.
  588. */
  589. void execute(const RendererView& view, const SPtr<Texture>& prevFrame, const SPtr<Texture>& curFrame,
  590. const SPtr<Texture>& sceneDepth, const SPtr<RenderTarget>& destination);
  591. /**
  592. * Returns the material variation matching the provided parameters.
  593. *
  594. * @param eyeAdaptation When true the shader will expect a texture containing an exposure value calculated by
  595. * the eye adaptation shader. Otherwise the manually provided exposure value is used
  596. * instead.
  597. */
  598. SSRResolveMat* getVariation(bool eyeAdaptation);
  599. private:
  600. SPtr<GpuParamBlockBuffer> mSSRParamBuffer;
  601. SPtr<GpuParamBlockBuffer> mTemporalParamBuffer;
  602. GpuParamTexture mSceneColorTexture;
  603. GpuParamTexture mPrevColorTexture;
  604. GpuParamTexture mSceneDepthTexture;
  605. GpuParamTexture mEyeAdaptationTexture;
  606. static ShaderVariation VAR_EyeAdaptation;
  607. static ShaderVariation VAR_NoEyeAdaptation;
  608. };
  609. /** @} */
  610. }}