BsPostProcessing.cpp 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsPostProcessing.h"
  4. #include "BsRenderTexture.h"
  5. #include "BsGpuResourcePool.h"
  6. #include "BsRendererUtility.h"
  7. #include "BsTextureManager.h"
  8. #include "BsCamera.h"
  9. #include "BsGpuParamsSet.h"
  10. #include "BsRendererView.h"
  11. #include "BsRenderTargets.h"
  12. #include "BsPixelUtil.h"
  13. #include "BsBitwise.h"
  14. #include "BsBuiltinResourcesHelper.h"
  15. namespace bs { namespace ct
  16. {
  17. DownsampleParamDef gDownsampleParamDef;
  18. ShaderVariation DownsampleMat::VAR_LowQuality_NoMSAA = ShaderVariation({
  19. ShaderVariation::Param("QUALITY", 0),
  20. ShaderVariation::Param("MSAA", 0)
  21. });
  22. ShaderVariation DownsampleMat::VAR_LowQuality_MSAA = ShaderVariation({
  23. ShaderVariation::Param("QUALITY", 0),
  24. ShaderVariation::Param("MSAA", 1)
  25. });
  26. ShaderVariation DownsampleMat::VAR_HighQuality_NoMSAA = ShaderVariation({
  27. ShaderVariation::Param("QUALITY", 1),
  28. ShaderVariation::Param("MSAA", 0)
  29. });
  30. ShaderVariation DownsampleMat::VAR_HighQuality_MSAA = ShaderVariation({
  31. ShaderVariation::Param("QUALITY", 1),
  32. ShaderVariation::Param("MSAA", 1)
  33. });
  34. DownsampleMat::DownsampleMat()
  35. {
  36. mParamBuffer = gDownsampleParamDef.createBuffer();
  37. SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
  38. if(gpuParams->hasParamBlock(GPT_FRAGMENT_PROGRAM, "Input"))
  39. mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
  40. mParamsSet->getGpuParams()->getTextureParam(GPT_FRAGMENT_PROGRAM, "gInputTex", mInputTexture);
  41. }
  42. void DownsampleMat::_initVariations(ShaderVariations& variations)
  43. {
  44. variations.add(VAR_LowQuality_NoMSAA);
  45. variations.add(VAR_LowQuality_MSAA);
  46. variations.add(VAR_HighQuality_NoMSAA);
  47. variations.add(VAR_HighQuality_MSAA);
  48. }
  49. void DownsampleMat::execute(const SPtr<Texture>& input, const SPtr<RenderTarget>& output)
  50. {
  51. // Set parameters
  52. mInputTexture.set(input);
  53. const TextureProperties& rtProps = input->getProperties();
  54. bool MSAA = mVariation.getInt("MSAA") > 0;
  55. if(MSAA)
  56. {
  57. gDownsampleParamDef.gOffsets.set(mParamBuffer, Vector2(-1.0f, -1.0f));
  58. gDownsampleParamDef.gOffsets.set(mParamBuffer, Vector2(1.0f, -1.0f));
  59. gDownsampleParamDef.gOffsets.set(mParamBuffer, Vector2(-1.0f, 1.0f));
  60. gDownsampleParamDef.gOffsets.set(mParamBuffer, Vector2(1.0f, 1.0f));
  61. }
  62. else
  63. {
  64. Vector2 invTextureSize(1.0f / rtProps.getWidth(), 1.0f / rtProps.getHeight());
  65. gDownsampleParamDef.gOffsets.set(mParamBuffer, invTextureSize * Vector2(-1.0f, -1.0f));
  66. gDownsampleParamDef.gOffsets.set(mParamBuffer, invTextureSize * Vector2(1.0f, -1.0f));
  67. gDownsampleParamDef.gOffsets.set(mParamBuffer, invTextureSize * Vector2(-1.0f, 1.0f));
  68. gDownsampleParamDef.gOffsets.set(mParamBuffer, invTextureSize * Vector2(1.0f, 1.0f));
  69. }
  70. RenderAPI& rapi = RenderAPI::instance();
  71. rapi.setRenderTarget(output, FBT_DEPTH | FBT_STENCIL);
  72. gRendererUtility().setPass(mMaterial);
  73. gRendererUtility().setPassParams(mParamsSet);
  74. if (MSAA)
  75. gRendererUtility().drawScreenQuad(Rect2(0.0f, 0.0f, (float)rtProps.getWidth(), (float)rtProps.getHeight()));
  76. else
  77. gRendererUtility().drawScreenQuad();
  78. rapi.setRenderTarget(nullptr);
  79. }
  80. POOLED_RENDER_TEXTURE_DESC DownsampleMat::getOutputDesc(const SPtr<Texture>& target)
  81. {
  82. const TextureProperties& rtProps = target->getProperties();
  83. UINT32 width = std::max(1, Math::ceilToInt(rtProps.getWidth() * 0.5f));
  84. UINT32 height = std::max(1, Math::ceilToInt(rtProps.getHeight() * 0.5f));
  85. return POOLED_RENDER_TEXTURE_DESC::create2D(rtProps.getFormat(), width, height, TU_RENDERTARGET);
  86. }
  87. DownsampleMat* DownsampleMat::getVariation(UINT32 quality, bool msaa)
  88. {
  89. if(quality == 0)
  90. {
  91. if (msaa)
  92. return get(VAR_LowQuality_MSAA);
  93. else
  94. return get(VAR_LowQuality_NoMSAA);
  95. }
  96. else
  97. {
  98. if (msaa)
  99. return get(VAR_HighQuality_MSAA);
  100. else
  101. return get(VAR_HighQuality_NoMSAA);
  102. }
  103. }
  104. EyeAdaptHistogramParamDef gEyeAdaptHistogramParamDef;
  105. EyeAdaptHistogramMat::EyeAdaptHistogramMat()
  106. {
  107. mParamBuffer = gEyeAdaptHistogramParamDef.createBuffer();
  108. mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
  109. SPtr<GpuParams> params = mParamsSet->getGpuParams();
  110. params->getTextureParam(GPT_COMPUTE_PROGRAM, "gSceneColorTex", mSceneColor);
  111. params->getLoadStoreTextureParam(GPT_COMPUTE_PROGRAM, "gOutputTex", mOutputTex);
  112. }
  113. void EyeAdaptHistogramMat::_initVariations(ShaderVariations& variations)
  114. {
  115. ShaderVariation variation({
  116. ShaderVariation::Param("THREADGROUP_SIZE_X", THREAD_GROUP_SIZE_X),
  117. ShaderVariation::Param("THREADGROUP_SIZE_Y", THREAD_GROUP_SIZE_Y),
  118. ShaderVariation::Param("LOOP_COUNT_X", LOOP_COUNT_X),
  119. ShaderVariation::Param("LOOP_COUNT_Y", LOOP_COUNT_Y),
  120. });
  121. variations.add(variation);
  122. }
  123. void EyeAdaptHistogramMat::execute(const SPtr<Texture>& input, const SPtr<Texture>& output,
  124. const AutoExposureSettings& settings)
  125. {
  126. // Set parameters
  127. mSceneColor.set(input);
  128. const TextureProperties& props = input->getProperties();
  129. int offsetAndSize[4] = { 0, 0, (INT32)props.getWidth(), (INT32)props.getHeight() };
  130. gEyeAdaptHistogramParamDef.gHistogramParams.set(mParamBuffer, getHistogramScaleOffset(settings));
  131. gEyeAdaptHistogramParamDef.gPixelOffsetAndSize.set(mParamBuffer, Vector4I(offsetAndSize));
  132. Vector2I threadGroupCount = getThreadGroupCount(input);
  133. gEyeAdaptHistogramParamDef.gThreadGroupCount.set(mParamBuffer, threadGroupCount);
  134. // Dispatch
  135. mOutputTex.set(output);
  136. RenderAPI& rapi = RenderAPI::instance();
  137. gRendererUtility().setComputePass(mMaterial);
  138. gRendererUtility().setPassParams(mParamsSet);
  139. rapi.dispatchCompute(threadGroupCount.x, threadGroupCount.y);
  140. }
  141. POOLED_RENDER_TEXTURE_DESC EyeAdaptHistogramMat::getOutputDesc(const SPtr<Texture>& target)
  142. {
  143. Vector2I threadGroupCount = getThreadGroupCount(target);
  144. UINT32 numHistograms = threadGroupCount.x * threadGroupCount.y;
  145. return POOLED_RENDER_TEXTURE_DESC::create2D(PF_FLOAT16_RGBA, HISTOGRAM_NUM_TEXELS, numHistograms,
  146. TU_LOADSTORE);
  147. }
  148. Vector2I EyeAdaptHistogramMat::getThreadGroupCount(const SPtr<Texture>& target)
  149. {
  150. const UINT32 texelsPerThreadGroupX = THREAD_GROUP_SIZE_X * LOOP_COUNT_X;
  151. const UINT32 texelsPerThreadGroupY = THREAD_GROUP_SIZE_Y * LOOP_COUNT_Y;
  152. const TextureProperties& props = target->getProperties();
  153. Vector2I threadGroupCount;
  154. threadGroupCount.x = ((INT32)props.getWidth() + texelsPerThreadGroupX - 1) / texelsPerThreadGroupX;
  155. threadGroupCount.y = ((INT32)props.getHeight() + texelsPerThreadGroupY - 1) / texelsPerThreadGroupY;
  156. return threadGroupCount;
  157. }
  158. Vector2 EyeAdaptHistogramMat::getHistogramScaleOffset(const AutoExposureSettings& settings)
  159. {
  160. float diff = settings.histogramLog2Max - settings.histogramLog2Min;
  161. float scale = 1.0f / diff;
  162. float offset = -settings.histogramLog2Min * scale;
  163. return Vector2(scale, offset);
  164. }
  165. EyeAdaptHistogramReduceParamDef gEyeAdaptHistogramReduceParamDef;
  166. EyeAdaptHistogramReduceMat::EyeAdaptHistogramReduceMat()
  167. {
  168. mParamBuffer = gEyeAdaptHistogramReduceParamDef.createBuffer();
  169. mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
  170. SPtr<GpuParams> params = mParamsSet->getGpuParams();
  171. params->getTextureParam(GPT_FRAGMENT_PROGRAM, "gHistogramTex", mHistogramTex);
  172. params->getTextureParam(GPT_FRAGMENT_PROGRAM, "gEyeAdaptationTex", mEyeAdaptationTex);
  173. }
  174. void EyeAdaptHistogramReduceMat::_initVariations(ShaderVariations& variations)
  175. {
  176. // Do nothing
  177. }
  178. void EyeAdaptHistogramReduceMat::execute(const SPtr<Texture>& sceneColor, const SPtr<Texture>& histogram,
  179. const SPtr<Texture>& prevFrame, const SPtr<RenderTarget>& output)
  180. {
  181. // Set parameters
  182. mHistogramTex.set(histogram);
  183. SPtr<Texture> eyeAdaptationTex;
  184. if (prevFrame == nullptr) // Could be that this is the first run
  185. eyeAdaptationTex = Texture::WHITE;
  186. else
  187. eyeAdaptationTex = prevFrame;
  188. mEyeAdaptationTex.set(eyeAdaptationTex);
  189. Vector2I threadGroupCount = EyeAdaptHistogramMat::getThreadGroupCount(sceneColor);
  190. UINT32 numHistograms = threadGroupCount.x * threadGroupCount.y;
  191. gEyeAdaptHistogramReduceParamDef.gThreadGroupCount.set(mParamBuffer, numHistograms);
  192. RenderAPI& rapi = RenderAPI::instance();
  193. rapi.setRenderTarget(output, FBT_DEPTH | FBT_STENCIL);
  194. gRendererUtility().setPass(mMaterial);
  195. gRendererUtility().setPassParams(mParamsSet);
  196. Rect2 drawUV(0.0f, 0.0f, (float)EyeAdaptHistogramMat::HISTOGRAM_NUM_TEXELS, 2.0f);
  197. gRendererUtility().drawScreenQuad(drawUV);
  198. rapi.setRenderTarget(nullptr);
  199. }
  200. POOLED_RENDER_TEXTURE_DESC EyeAdaptHistogramReduceMat::getOutputDesc()
  201. {
  202. return POOLED_RENDER_TEXTURE_DESC::create2D(PF_FLOAT16_RGBA, EyeAdaptHistogramMat::HISTOGRAM_NUM_TEXELS, 2,
  203. TU_RENDERTARGET);
  204. }
  205. EyeAdaptationParamDef gEyeAdaptationParamDef;
  206. EyeAdaptationMat::EyeAdaptationMat()
  207. {
  208. mParamBuffer = gEyeAdaptationParamDef.createBuffer();
  209. mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
  210. mParamsSet->getGpuParams()->getTextureParam(GPT_FRAGMENT_PROGRAM, "gHistogramTex", mReducedHistogramTex);
  211. }
  212. void EyeAdaptationMat::_initVariations(ShaderVariations& variations)
  213. {
  214. ShaderVariation variation({
  215. ShaderVariation::Param("THREADGROUP_SIZE_X", EyeAdaptHistogramMat::THREAD_GROUP_SIZE_X),
  216. ShaderVariation::Param("THREADGROUP_SIZE_Y", EyeAdaptHistogramMat::THREAD_GROUP_SIZE_Y)
  217. });
  218. variations.add(variation);
  219. }
  220. void EyeAdaptationMat::execute(const SPtr<Texture>& reducedHistogram, const SPtr<RenderTarget>& output,
  221. float frameDelta, const AutoExposureSettings& settings, float exposureScale)
  222. {
  223. // Set parameters
  224. mReducedHistogramTex.set(reducedHistogram);
  225. Vector2 histogramScaleAndOffset = EyeAdaptHistogramMat::getHistogramScaleOffset(settings);
  226. Vector4 eyeAdaptationParams[3];
  227. eyeAdaptationParams[0].x = histogramScaleAndOffset.x;
  228. eyeAdaptationParams[0].y = histogramScaleAndOffset.y;
  229. float histogramPctHigh = Math::clamp01(settings.histogramPctHigh);
  230. eyeAdaptationParams[0].z = std::min(Math::clamp01(settings.histogramPctLow), histogramPctHigh);
  231. eyeAdaptationParams[0].w = histogramPctHigh;
  232. eyeAdaptationParams[1].x = std::min(settings.minEyeAdaptation, settings.maxEyeAdaptation);
  233. eyeAdaptationParams[1].y = settings.maxEyeAdaptation;
  234. eyeAdaptationParams[1].z = settings.eyeAdaptationSpeedUp;
  235. eyeAdaptationParams[1].w = settings.eyeAdaptationSpeedDown;
  236. eyeAdaptationParams[2].x = Math::pow(2.0f, exposureScale);
  237. eyeAdaptationParams[2].y = frameDelta;
  238. eyeAdaptationParams[2].z = 0.0f; // Unused
  239. eyeAdaptationParams[2].w = 0.0f; // Unused
  240. gEyeAdaptationParamDef.gEyeAdaptationParams.set(mParamBuffer, eyeAdaptationParams[0], 0);
  241. gEyeAdaptationParamDef.gEyeAdaptationParams.set(mParamBuffer, eyeAdaptationParams[1], 1);
  242. gEyeAdaptationParamDef.gEyeAdaptationParams.set(mParamBuffer, eyeAdaptationParams[2], 2);
  243. // Render
  244. RenderAPI& rapi = RenderAPI::instance();
  245. rapi.setRenderTarget(output, FBT_DEPTH | FBT_STENCIL);
  246. gRendererUtility().setPass(mMaterial);
  247. gRendererUtility().setPassParams(mParamsSet);
  248. gRendererUtility().drawScreenQuad();
  249. rapi.setRenderTarget(nullptr);
  250. }
  251. POOLED_RENDER_TEXTURE_DESC EyeAdaptationMat::getOutputDesc()
  252. {
  253. return POOLED_RENDER_TEXTURE_DESC::create2D(PF_FLOAT32_R, 1, 1, TU_RENDERTARGET);
  254. }
  255. CreateTonemapLUTParamDef gCreateTonemapLUTParamDef;
  256. WhiteBalanceParamDef gWhiteBalanceParamDef;
  257. CreateTonemapLUTMat::CreateTonemapLUTMat()
  258. {
  259. mParamBuffer = gCreateTonemapLUTParamDef.createBuffer();
  260. mWhiteBalanceParamBuffer = gWhiteBalanceParamDef.createBuffer();
  261. mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
  262. mParamsSet->setParamBlockBuffer("WhiteBalanceInput", mWhiteBalanceParamBuffer);
  263. SPtr<GpuParams> params = mParamsSet->getGpuParams();
  264. params->getLoadStoreTextureParam(GPT_COMPUTE_PROGRAM, "gOutputTex", mOutputTex);
  265. }
  266. void CreateTonemapLUTMat::_initVariations(ShaderVariations& variations)
  267. {
  268. ShaderVariation variation({
  269. ShaderVariation::Param("LUT_SIZE", LUT_SIZE)
  270. });
  271. variations.add(variation);
  272. }
  273. void CreateTonemapLUTMat::execute(const SPtr<Texture>& output, const StandardPostProcessSettings& settings)
  274. {
  275. // Set parameters
  276. gCreateTonemapLUTParamDef.gGammaAdjustment.set(mParamBuffer, 2.2f / settings.gamma);
  277. // Note: Assuming sRGB (PC monitor) for now, change to Rec.709 when running on console (value 1), or to raw 2.2
  278. // gamma when running on Mac (value 2)
  279. gCreateTonemapLUTParamDef.gGammaCorrectionType.set(mParamBuffer, 0);
  280. Vector4 tonemapParams[2];
  281. tonemapParams[0].x = settings.tonemapping.filmicCurveShoulderStrength;
  282. tonemapParams[0].y = settings.tonemapping.filmicCurveLinearStrength;
  283. tonemapParams[0].z = settings.tonemapping.filmicCurveLinearAngle;
  284. tonemapParams[0].w = settings.tonemapping.filmicCurveToeStrength;
  285. tonemapParams[1].x = settings.tonemapping.filmicCurveToeNumerator;
  286. tonemapParams[1].y = settings.tonemapping.filmicCurveToeDenominator;
  287. tonemapParams[1].z = settings.tonemapping.filmicCurveLinearWhitePoint;
  288. tonemapParams[1].w = 0.0f; // Unused
  289. gCreateTonemapLUTParamDef.gTonemapParams.set(mParamBuffer, tonemapParams[0], 0);
  290. gCreateTonemapLUTParamDef.gTonemapParams.set(mParamBuffer, tonemapParams[1], 1);
  291. // Set color grading params
  292. gCreateTonemapLUTParamDef.gSaturation.set(mParamBuffer, settings.colorGrading.saturation);
  293. gCreateTonemapLUTParamDef.gContrast.set(mParamBuffer, settings.colorGrading.contrast);
  294. gCreateTonemapLUTParamDef.gGain.set(mParamBuffer, settings.colorGrading.gain);
  295. gCreateTonemapLUTParamDef.gOffset.set(mParamBuffer, settings.colorGrading.offset);
  296. // Set white balance params
  297. gWhiteBalanceParamDef.gWhiteTemp.set(mWhiteBalanceParamBuffer, settings.whiteBalance.temperature);
  298. gWhiteBalanceParamDef.gWhiteOffset.set(mWhiteBalanceParamBuffer, settings.whiteBalance.tint);
  299. // Dispatch
  300. mOutputTex.set(output);
  301. RenderAPI& rapi = RenderAPI::instance();
  302. gRendererUtility().setComputePass(mMaterial);
  303. gRendererUtility().setPassParams(mParamsSet);
  304. rapi.dispatchCompute(LUT_SIZE / 8, LUT_SIZE / 8, LUT_SIZE);
  305. }
  306. POOLED_RENDER_TEXTURE_DESC CreateTonemapLUTMat::getOutputDesc()
  307. {
  308. return POOLED_RENDER_TEXTURE_DESC::create3D(PF_R8G8B8A8, LUT_SIZE, LUT_SIZE, LUT_SIZE, TU_LOADSTORE);
  309. }
  310. TonemappingParamDef gTonemappingParamDef;
  311. ShaderVariation TonemappingMat::VAR_Gamma_AutoExposure_MSAA = ShaderVariation({
  312. ShaderVariation::Param("GAMMA_ONLY", true),
  313. ShaderVariation::Param("AUTO_EXPOSURE", true),
  314. ShaderVariation::Param("MSAA", true),
  315. ShaderVariation::Param("LUT_SIZE", CreateTonemapLUTMat::LUT_SIZE),
  316. });
  317. ShaderVariation TonemappingMat::VAR_Gamma_AutoExposure_NoMSAA = ShaderVariation({
  318. ShaderVariation::Param("GAMMA_ONLY", true),
  319. ShaderVariation::Param("AUTO_EXPOSURE", true),
  320. ShaderVariation::Param("MSAA", false),
  321. ShaderVariation::Param("LUT_SIZE", CreateTonemapLUTMat::LUT_SIZE),
  322. });
  323. ShaderVariation TonemappingMat::VAR_Gamma_NoAutoExposure_MSAA = ShaderVariation({
  324. ShaderVariation::Param("GAMMA_ONLY", true),
  325. ShaderVariation::Param("AUTO_EXPOSURE", false),
  326. ShaderVariation::Param("MSAA", true),
  327. ShaderVariation::Param("LUT_SIZE", CreateTonemapLUTMat::LUT_SIZE),
  328. });
  329. ShaderVariation TonemappingMat::VAR_Gamma_NoAutoExposure_NoMSAA = ShaderVariation({
  330. ShaderVariation::Param("GAMMA_ONLY", true),
  331. ShaderVariation::Param("AUTO_EXPOSURE", false),
  332. ShaderVariation::Param("MSAA", false),
  333. ShaderVariation::Param("LUT_SIZE", CreateTonemapLUTMat::LUT_SIZE),
  334. });
  335. ShaderVariation TonemappingMat::VAR_NoGamma_AutoExposure_MSAA = ShaderVariation({
  336. ShaderVariation::Param("GAMMA_ONLY", false),
  337. ShaderVariation::Param("AUTO_EXPOSURE", true),
  338. ShaderVariation::Param("MSAA", true),
  339. ShaderVariation::Param("LUT_SIZE", CreateTonemapLUTMat::LUT_SIZE),
  340. });
  341. ShaderVariation TonemappingMat::VAR_NoGamma_AutoExposure_NoMSAA = ShaderVariation({
  342. ShaderVariation::Param("GAMMA_ONLY", false),
  343. ShaderVariation::Param("AUTO_EXPOSURE", true),
  344. ShaderVariation::Param("MSAA", false),
  345. ShaderVariation::Param("LUT_SIZE", CreateTonemapLUTMat::LUT_SIZE),
  346. });
  347. ShaderVariation TonemappingMat::VAR_NoGamma_NoAutoExposure_MSAA = ShaderVariation({
  348. ShaderVariation::Param("GAMMA_ONLY", false),
  349. ShaderVariation::Param("AUTO_EXPOSURE", false),
  350. ShaderVariation::Param("MSAA", true),
  351. ShaderVariation::Param("LUT_SIZE", CreateTonemapLUTMat::LUT_SIZE),
  352. });
  353. ShaderVariation TonemappingMat::VAR_NoGamma_NoAutoExposure_NoMSAA = ShaderVariation({
  354. ShaderVariation::Param("GAMMA_ONLY", false),
  355. ShaderVariation::Param("AUTO_EXPOSURE", false),
  356. ShaderVariation::Param("MSAA", false),
  357. ShaderVariation::Param("LUT_SIZE", CreateTonemapLUTMat::LUT_SIZE),
  358. });
  359. TonemappingMat::TonemappingMat()
  360. {
  361. mParamBuffer = gTonemappingParamDef.createBuffer();
  362. mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
  363. SPtr<GpuParams> params = mParamsSet->getGpuParams();
  364. params->getTextureParam(GPT_VERTEX_PROGRAM, "gEyeAdaptationTex", mEyeAdaptationTex);
  365. params->getTextureParam(GPT_FRAGMENT_PROGRAM, "gInputTex", mInputTex);
  366. if(!mVariation.getBool("GAMMA_ONLY"))
  367. params->getTextureParam(GPT_FRAGMENT_PROGRAM, "gColorLUT", mColorLUT);
  368. }
  369. void TonemappingMat::_initVariations(ShaderVariations& variations)
  370. {
  371. variations.add(VAR_Gamma_AutoExposure_MSAA);
  372. variations.add(VAR_Gamma_AutoExposure_NoMSAA);
  373. variations.add(VAR_Gamma_NoAutoExposure_MSAA);
  374. variations.add(VAR_Gamma_NoAutoExposure_NoMSAA);
  375. variations.add(VAR_NoGamma_AutoExposure_MSAA);
  376. variations.add(VAR_NoGamma_AutoExposure_NoMSAA);
  377. variations.add(VAR_NoGamma_NoAutoExposure_MSAA);
  378. variations.add(VAR_NoGamma_NoAutoExposure_NoMSAA);
  379. }
  380. void TonemappingMat::execute(const SPtr<Texture>& sceneColor, const SPtr<Texture>& eyeAdaptation,
  381. const SPtr<Texture>& colorLUT, const SPtr<RenderTarget>& output, const StandardPostProcessSettings& settings)
  382. {
  383. const TextureProperties& texProps = sceneColor->getProperties();
  384. gTonemappingParamDef.gRawGamma.set(mParamBuffer, 1.0f / settings.gamma);
  385. gTonemappingParamDef.gManualExposureScale.set(mParamBuffer, Math::pow(2.0f, settings.exposureScale));
  386. gTonemappingParamDef.gNumSamples.set(mParamBuffer, texProps.getNumSamples());
  387. // Set parameters
  388. mInputTex.set(sceneColor);
  389. mColorLUT.set(colorLUT);
  390. mEyeAdaptationTex.set(eyeAdaptation);
  391. // Render
  392. RenderAPI& rapi = RenderAPI::instance();
  393. rapi.setRenderTarget(output);
  394. gRendererUtility().setPass(mMaterial);
  395. gRendererUtility().setPassParams(mParamsSet);
  396. if (mVariation.getBool("MSAA"))
  397. gRendererUtility().drawScreenQuad(Rect2(0.0f, 0.0f, (float)texProps.getWidth(), (float)texProps.getHeight()));
  398. else
  399. gRendererUtility().drawScreenQuad();
  400. }
  401. TonemappingMat* TonemappingMat::getVariation(bool gammaOnly, bool autoExposure, bool MSAA)
  402. {
  403. if (gammaOnly)
  404. {
  405. if (autoExposure)
  406. {
  407. if (MSAA)
  408. return get(VAR_Gamma_AutoExposure_MSAA);
  409. else
  410. return get(VAR_Gamma_AutoExposure_NoMSAA);
  411. }
  412. else
  413. {
  414. if (MSAA)
  415. return get(VAR_Gamma_NoAutoExposure_MSAA);
  416. else
  417. return get(VAR_Gamma_NoAutoExposure_NoMSAA);
  418. }
  419. }
  420. else
  421. {
  422. if (autoExposure)
  423. {
  424. if (MSAA)
  425. return get(VAR_NoGamma_AutoExposure_MSAA);
  426. else
  427. return get(VAR_NoGamma_AutoExposure_NoMSAA);
  428. }
  429. else
  430. {
  431. if (MSAA)
  432. return get(VAR_NoGamma_NoAutoExposure_MSAA);
  433. else
  434. return get(VAR_NoGamma_NoAutoExposure_NoMSAA);
  435. }
  436. }
  437. }
  438. GaussianBlurParamDef gGaussianBlurParamDef;
  439. GaussianBlurMat::GaussianBlurMat()
  440. {
  441. mParamBuffer = gGaussianBlurParamDef.createBuffer();
  442. mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
  443. mParamsSet->getGpuParams()->getTextureParam(GPT_FRAGMENT_PROGRAM, "gInputTex", mInputTexture);
  444. }
  445. void GaussianBlurMat::_initVariations(ShaderVariations& variations)
  446. {
  447. ShaderVariation variation({
  448. ShaderVariation::Param("MAX_NUM_SAMPLES", MAX_BLUR_SAMPLES)
  449. });
  450. variations.add(variation);
  451. }
  452. void GaussianBlurMat::execute(const SPtr<Texture>& source, float filterSize, const SPtr<RenderTexture>& destination)
  453. {
  454. const TextureProperties& srcProps = source->getProperties();
  455. const RenderTextureProperties& dstProps = destination->getProperties();
  456. Vector2 invTexSize(1.0f / srcProps.getWidth(), 1.0f / srcProps.getHeight());
  457. std::array<float, MAX_BLUR_SAMPLES> sampleOffsets;
  458. std::array<float, MAX_BLUR_SAMPLES> sampleWeights;
  459. POOLED_RENDER_TEXTURE_DESC tempTextureDesc = POOLED_RENDER_TEXTURE_DESC::create2D(srcProps.getFormat(),
  460. dstProps.getWidth(), dstProps.getHeight(), TU_RENDERTARGET);
  461. SPtr<PooledRenderTexture> tempTexture = GpuResourcePool::instance().get(tempTextureDesc);
  462. auto updateParamBuffer = [&](Direction direction)
  463. {
  464. float kernelRadius = calcKernelRadius(source, filterSize, direction);
  465. UINT32 numSamples = calcStdDistribution(kernelRadius, sampleWeights, sampleOffsets);
  466. for(UINT32 i = 0; i < (numSamples + 3) / 4; ++i)
  467. {
  468. UINT32 remainder = std::min(4U, numSamples - i * 4);
  469. Vector4 weights;
  470. for (UINT32 j = 0; j < remainder; ++j)
  471. weights[j] = sampleWeights[i * 4 + j];
  472. gGaussianBlurParamDef.gSampleWeights.set(mParamBuffer, weights, i);
  473. }
  474. UINT32 axis0 = direction == DirHorizontal ? 0 : 1;
  475. UINT32 axis1 = (axis0 + 1) % 2;
  476. for(UINT32 i = 0; i < (numSamples + 1) / 2; ++i)
  477. {
  478. UINT32 remainder = std::min(2U, numSamples - i * 2);
  479. Vector4 offset;
  480. offset[axis0] = sampleOffsets[i * 2 + 0] * invTexSize[axis0];
  481. offset[axis1] = 0.0f;
  482. if(remainder == 2)
  483. {
  484. offset[axis0 + 2] = sampleOffsets[i * 2 + 1] * invTexSize[axis0];
  485. offset[axis1 + 2] = 0.0f;
  486. }
  487. else
  488. {
  489. offset[axis0 + 2] = 0.0f;
  490. offset[axis1 + 2] = 0.0f;
  491. }
  492. gGaussianBlurParamDef.gSampleOffsets.set(mParamBuffer, offset, i);
  493. }
  494. gGaussianBlurParamDef.gNumSamples.set(mParamBuffer, numSamples);
  495. };
  496. // Horizontal pass
  497. {
  498. updateParamBuffer(DirHorizontal);
  499. mInputTexture.set(source);
  500. RenderAPI& rapi = RenderAPI::instance();
  501. rapi.setRenderTarget(tempTexture->renderTexture);
  502. gRendererUtility().setPass(mMaterial);
  503. gRendererUtility().setPassParams(mParamsSet);
  504. gRendererUtility().drawScreenQuad();
  505. }
  506. // Vertical pass
  507. {
  508. updateParamBuffer(DirVertical);
  509. mInputTexture.set(tempTexture->texture);
  510. RenderAPI& rapi = RenderAPI::instance();
  511. rapi.setRenderTarget(destination);
  512. gRendererUtility().setPass(mMaterial);
  513. gRendererUtility().setPassParams(mParamsSet);
  514. gRendererUtility().drawScreenQuad();
  515. }
  516. GpuResourcePool::instance().release(tempTexture);
  517. }
  518. UINT32 GaussianBlurMat::calcStdDistribution(float filterRadius, std::array<float, MAX_BLUR_SAMPLES>& weights,
  519. std::array<float, MAX_BLUR_SAMPLES>& offsets)
  520. {
  521. filterRadius = Math::clamp(filterRadius, 0.00001f, (float)(MAX_BLUR_SAMPLES - 1));
  522. INT32 intFilterRadius = std::min(Math::ceilToInt(filterRadius), MAX_BLUR_SAMPLES - 1);
  523. auto normalDistribution = [](int i, float scale)
  524. {
  525. float samplePos = fabs((float)i) * scale;
  526. return exp(samplePos * samplePos);
  527. };
  528. // We make use of the hardware linear filtering, and therefore only generate half the number of samples.
  529. // The weights and the sampling location needs to be adjusted in order to get the same results as if we
  530. // perform two samples separately:
  531. //
  532. // Original formula is: t1*w1 + t2*w2
  533. // With hardware filtering it's: (t1 + (t2 - t1) * o) * w3
  534. // Or expanded: t1*w3 - t1*o*w3 + t2*o*w3 = t1 * (w3 - o*w3) + t2 * (o*w3)
  535. //
  536. // These two need to equal, which means this follows:
  537. // w1 = w3 - o*w3
  538. // w2 = o*w3
  539. //
  540. // From the second equation get the offset o:
  541. // o = w2/w3
  542. //
  543. // From the first equation and o, get w3:
  544. // w1 = w3 - w2
  545. // w3 = w1 + w2
  546. float scale = 1.0f / filterRadius;
  547. UINT32 numSamples = 0;
  548. float totalWeight = 0.0f;
  549. for(int i = -intFilterRadius; i < intFilterRadius; i += 2)
  550. {
  551. float w1 = normalDistribution(i, scale);
  552. float w2 = normalDistribution(i + 1, scale);
  553. float w3 = w1 + w2;
  554. float o = w2/w3; // Relative to first sample
  555. weights[numSamples] = w3;
  556. offsets[numSamples] = o;
  557. numSamples++;
  558. totalWeight += w3;
  559. }
  560. // Special case for last weight, as it doesn't have a matching pair
  561. float w = normalDistribution(intFilterRadius, scale);
  562. weights[numSamples] = w;
  563. offsets[numSamples] = 0.0f;
  564. numSamples++;
  565. totalWeight += w;
  566. // Normalize weights
  567. float invTotalWeight = 1.0f / totalWeight;
  568. for(UINT32 i = 0; i < numSamples; i++)
  569. weights[i] *= invTotalWeight;
  570. return numSamples;
  571. }
  572. float GaussianBlurMat::calcKernelRadius(const SPtr<Texture>& source, float scale, Direction filterDir)
  573. {
  574. scale = Math::clamp01(scale);
  575. UINT32 length;
  576. if (filterDir == DirHorizontal)
  577. length = source->getProperties().getWidth();
  578. else
  579. length = source->getProperties().getHeight();
  580. // Divide by two because we need the radius
  581. return std::min(length * scale / 2, (float)MAX_BLUR_SAMPLES - 1);
  582. }
  583. GaussianDOFParamDef gGaussianDOFParamDef;
  584. ShaderVariation GaussianDOFSeparateMat::VAR_Near_Far = ShaderVariation({
  585. ShaderVariation::Param("NEAR", true),
  586. ShaderVariation::Param("FAR", true),
  587. ShaderVariation::Param("NEAR_AND_FAR", true)
  588. });
  589. ShaderVariation GaussianDOFSeparateMat::VAR_NoNear_Far = ShaderVariation({
  590. ShaderVariation::Param("NEAR", false),
  591. ShaderVariation::Param("FAR", true),
  592. ShaderVariation::Param("NEAR_AND_FAR", false)
  593. });
  594. ShaderVariation GaussianDOFSeparateMat::VAR_Near_NoFar = ShaderVariation({
  595. ShaderVariation::Param("NEAR", true),
  596. ShaderVariation::Param("FAR", false),
  597. ShaderVariation::Param("NEAR_AND_FAR", false)
  598. });
  599. GaussianDOFSeparateMat::GaussianDOFSeparateMat()
  600. {
  601. mParamBuffer = gGaussianDOFParamDef.createBuffer();
  602. mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
  603. mParamsSet->getGpuParams()->getTextureParam(GPT_FRAGMENT_PROGRAM, "gColorTex", mColorTexture);
  604. mParamsSet->getGpuParams()->getTextureParam(GPT_FRAGMENT_PROGRAM, "gDepthTex", mDepthTexture);
  605. GpuParamSampState colorSampState;
  606. mParamsSet->getGpuParams()->getSamplerStateParam(GPT_FRAGMENT_PROGRAM, "gColorSamp", colorSampState);
  607. SAMPLER_STATE_DESC desc;
  608. desc.minFilter = FO_POINT;
  609. desc.magFilter = FO_POINT;
  610. desc.mipFilter = FO_POINT;
  611. desc.addressMode.u = TAM_CLAMP;
  612. desc.addressMode.v = TAM_CLAMP;
  613. desc.addressMode.w = TAM_CLAMP;
  614. SPtr<SamplerState> samplerState = SamplerState::create(desc);
  615. colorSampState.set(samplerState);
  616. }
  617. void GaussianDOFSeparateMat::_initVariations(ShaderVariations& variations)
  618. {
  619. variations.add(VAR_Near_Far);
  620. variations.add(VAR_Near_NoFar);
  621. variations.add(VAR_NoNear_Far);
  622. }
  623. void GaussianDOFSeparateMat::execute(const SPtr<Texture>& color, const SPtr<Texture>& depth,
  624. const RendererView& view, const DepthOfFieldSettings& settings)
  625. {
  626. const TextureProperties& srcProps = color->getProperties();
  627. UINT32 outputWidth = std::max(1U, srcProps.getWidth() / 2);
  628. UINT32 outputHeight = std::max(1U, srcProps.getHeight() / 2);
  629. POOLED_RENDER_TEXTURE_DESC outputTexDesc = POOLED_RENDER_TEXTURE_DESC::create2D(srcProps.getFormat(),
  630. outputWidth, outputHeight, TU_RENDERTARGET);
  631. mOutput0 = GpuResourcePool::instance().get(outputTexDesc);
  632. bool near = mVariation.getBool("NEAR");
  633. bool far = mVariation.getBool("FAR");
  634. SPtr<RenderTexture> rt;
  635. if (near && far)
  636. {
  637. mOutput1 = GpuResourcePool::instance().get(outputTexDesc);
  638. RENDER_TEXTURE_DESC rtDesc;
  639. rtDesc.colorSurfaces[0].texture = mOutput0->texture;
  640. rtDesc.colorSurfaces[1].texture = mOutput1->texture;
  641. rt = RenderTexture::create(rtDesc);
  642. }
  643. else
  644. rt = mOutput0->renderTexture;
  645. Vector2 invTexSize(1.0f / srcProps.getWidth(), 1.0f / srcProps.getHeight());
  646. gGaussianDOFParamDef.gHalfPixelOffset.set(mParamBuffer, invTexSize * 0.5f);
  647. gGaussianDOFParamDef.gNearBlurPlane.set(mParamBuffer, settings.focalDistance - settings.focalRange * 0.5f);
  648. gGaussianDOFParamDef.gFarBlurPlane.set(mParamBuffer, settings.focalDistance + settings.focalRange * 0.5f);
  649. gGaussianDOFParamDef.gInvNearBlurRange.set(mParamBuffer, 1.0f / settings.nearTransitionRange);
  650. gGaussianDOFParamDef.gInvFarBlurRange.set(mParamBuffer, 1.0f / settings.farTransitionRange);
  651. mColorTexture.set(color);
  652. mDepthTexture.set(depth);
  653. SPtr<GpuParamBlockBuffer> perView = view.getPerViewBuffer();
  654. mParamsSet->setParamBlockBuffer("PerCamera", perView);
  655. RenderAPI& rapi = RenderAPI::instance();
  656. rapi.setRenderTarget(rt);
  657. gRendererUtility().setPass(mMaterial);
  658. gRendererUtility().setPassParams(mParamsSet);
  659. gRendererUtility().drawScreenQuad();
  660. }
  661. SPtr<PooledRenderTexture> GaussianDOFSeparateMat::getOutput(UINT32 idx)
  662. {
  663. if (idx == 0)
  664. return mOutput0;
  665. else if (idx == 1)
  666. return mOutput1;
  667. return nullptr;
  668. }
  669. void GaussianDOFSeparateMat::release()
  670. {
  671. if (mOutput0 != nullptr)
  672. GpuResourcePool::instance().release(mOutput0);
  673. if (mOutput1 != nullptr)
  674. GpuResourcePool::instance().release(mOutput1);
  675. }
  676. GaussianDOFSeparateMat* GaussianDOFSeparateMat::getVariation(bool near, bool far)
  677. {
  678. if (near)
  679. {
  680. if (far)
  681. return get(VAR_Near_Far);
  682. else
  683. return get(VAR_Near_NoFar);
  684. }
  685. else
  686. return get(VAR_NoNear_Far);
  687. }
  688. ShaderVariation GaussianDOFCombineMat::VAR_Near_Far = ShaderVariation({
  689. ShaderVariation::Param("NEAR", true),
  690. ShaderVariation::Param("FAR", true),
  691. ShaderVariation::Param("NEAR_AND_FAR", true)
  692. });
  693. ShaderVariation GaussianDOFCombineMat::VAR_NoNear_Far = ShaderVariation({
  694. ShaderVariation::Param("NEAR", false),
  695. ShaderVariation::Param("FAR", true),
  696. ShaderVariation::Param("NEAR_AND_FAR", false)
  697. });
  698. ShaderVariation GaussianDOFCombineMat::VAR_Near_NoFar = ShaderVariation({
  699. ShaderVariation::Param("NEAR", true),
  700. ShaderVariation::Param("FAR", false),
  701. ShaderVariation::Param("NEAR_AND_FAR", false)
  702. });
  703. GaussianDOFCombineMat::GaussianDOFCombineMat()
  704. {
  705. mParamBuffer = gGaussianDOFParamDef.createBuffer();
  706. mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
  707. SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
  708. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gFocusedTex", mFocusedTexture);
  709. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gDepthTex", mDepthTexture);
  710. if(gpuParams->hasTexture(GPT_FRAGMENT_PROGRAM, "gNearTex"))
  711. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gNearTex", mNearTexture);
  712. if(gpuParams->hasTexture(GPT_FRAGMENT_PROGRAM, "gFarTex"))
  713. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gFarTex", mFarTexture);
  714. }
  715. void GaussianDOFCombineMat::_initVariations(ShaderVariations& variations)
  716. {
  717. variations.add(VAR_Near_Far);
  718. variations.add(VAR_Near_NoFar);
  719. variations.add(VAR_NoNear_Far);
  720. }
  721. void GaussianDOFCombineMat::execute(const SPtr<Texture>& focused, const SPtr<Texture>& near,
  722. const SPtr<Texture>& far, const SPtr<Texture>& depth, const SPtr<RenderTarget>& output,
  723. const RendererView& view, const DepthOfFieldSettings& settings)
  724. {
  725. const TextureProperties& srcProps = focused->getProperties();
  726. Vector2 invTexSize(1.0f / srcProps.getWidth(), 1.0f / srcProps.getHeight());
  727. gGaussianDOFParamDef.gHalfPixelOffset.set(mParamBuffer, invTexSize * 0.5f);
  728. gGaussianDOFParamDef.gNearBlurPlane.set(mParamBuffer, settings.focalDistance - settings.focalRange * 0.5f);
  729. gGaussianDOFParamDef.gFarBlurPlane.set(mParamBuffer, settings.focalDistance + settings.focalRange * 0.5f);
  730. gGaussianDOFParamDef.gInvNearBlurRange.set(mParamBuffer, 1.0f / settings.nearTransitionRange);
  731. gGaussianDOFParamDef.gInvFarBlurRange.set(mParamBuffer, 1.0f / settings.farTransitionRange);
  732. mFocusedTexture.set(focused);
  733. mNearTexture.set(near);
  734. mFarTexture.set(far);
  735. mDepthTexture.set(depth);
  736. SPtr<GpuParamBlockBuffer> perView = view.getPerViewBuffer();
  737. mParamsSet->setParamBlockBuffer("PerCamera", perView);
  738. RenderAPI& rapi = RenderAPI::instance();
  739. rapi.setRenderTarget(output);
  740. gRendererUtility().setPass(mMaterial);
  741. gRendererUtility().setPassParams(mParamsSet);
  742. gRendererUtility().drawScreenQuad();
  743. }
  744. GaussianDOFCombineMat* GaussianDOFCombineMat::getVariation(bool near, bool far)
  745. {
  746. if (near)
  747. {
  748. if (far)
  749. return get(VAR_Near_Far);
  750. else
  751. return get(VAR_Near_NoFar);
  752. }
  753. else
  754. return get(VAR_NoNear_Far);
  755. }
  756. BuildHiZMat::BuildHiZMat()
  757. {
  758. SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
  759. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gDepthTex", mInputTexture);
  760. }
  761. void BuildHiZMat::_initVariations(ShaderVariations& variations)
  762. {
  763. // Do nothing
  764. }
  765. void BuildHiZMat::execute(const SPtr<Texture>& source, UINT32 srcMip, const Rect2& srcRect, const Rect2& dstRect,
  766. const SPtr<RenderTexture>& output)
  767. {
  768. RenderAPI& rapi = RenderAPI::instance();
  769. const TextureProperties& srcProps = source->getProperties();
  770. mInputTexture.set(source, TextureSurface(srcMip));
  771. rapi.setRenderTarget(output);
  772. rapi.setViewport(dstRect);
  773. gRendererUtility().setPass(mMaterial);
  774. gRendererUtility().setPassParams(mParamsSet);
  775. gRendererUtility().drawScreenQuad(srcRect);
  776. rapi.setViewport(Rect2(0, 0, 1, 1));
  777. }
  778. void BuildHiZ::execute(const RendererViewTargetData& viewInfo, const SPtr<Texture>& source, const SPtr<Texture>& output)
  779. {
  780. Rect2 srcRect = viewInfo.nrmViewRect;
  781. // If viewport size is odd, adjust UV
  782. srcRect.width += (viewInfo.viewRect.width % 2) * (1.0f / viewInfo.viewRect.width);
  783. srcRect.height += (viewInfo.viewRect.height % 2) * (1.0f / viewInfo.viewRect.height);
  784. // Generate first mip
  785. RENDER_TEXTURE_DESC rtDesc;
  786. rtDesc.colorSurfaces[0].texture = output;
  787. rtDesc.colorSurfaces[0].mipLevel = 0;
  788. SPtr<RenderTexture> rt = RenderTexture::create(rtDesc);
  789. const TextureProperties& outProps = output->getProperties();
  790. Rect2 destRect;
  791. bool downsampledFirstMip = false; // Not used currently
  792. if (downsampledFirstMip)
  793. {
  794. // Make sure that 1 pixel in HiZ maps to a 2x2 block in source
  795. destRect = Rect2(0, 0,
  796. Math::ceilToInt(viewInfo.viewRect.width / 2.0f) / (float)outProps.getWidth(),
  797. Math::ceilToInt(viewInfo.viewRect.height / 2.0f) / (float)outProps.getHeight());
  798. BuildHiZMat* material = BuildHiZMat::get();
  799. material->execute(source, 0, srcRect, destRect, rt);
  800. }
  801. else // First level is just a copy of the depth buffer
  802. {
  803. destRect = Rect2(0, 0,
  804. viewInfo.viewRect.width / (float)outProps.getWidth(),
  805. viewInfo.viewRect.height / (float)outProps.getHeight());
  806. RenderAPI& rapi = RenderAPI::instance();
  807. rapi.setRenderTarget(rt);
  808. rapi.setViewport(destRect);
  809. Rect2I srcAreaInt;
  810. srcAreaInt.x = (INT32)(srcRect.x * viewInfo.viewRect.width);
  811. srcAreaInt.y = (INT32)(srcRect.y * viewInfo.viewRect.height);
  812. srcAreaInt.width = (UINT32)(srcRect.width * viewInfo.viewRect.width);
  813. srcAreaInt.height = (UINT32)(srcRect.height * viewInfo.viewRect.height);
  814. gRendererUtility().blit(source, srcAreaInt);
  815. rapi.setViewport(Rect2(0, 0, 1, 1));
  816. }
  817. // Generate remaining mip levels
  818. for(UINT32 i = 1; i <= outProps.getNumMipmaps(); i++)
  819. {
  820. rtDesc.colorSurfaces[0].mipLevel = i;
  821. rt = RenderTexture::create(rtDesc);
  822. BuildHiZMat* material = BuildHiZMat::get();
  823. material->execute(output, i - 1, destRect, destRect, rt);
  824. }
  825. }
  826. POOLED_RENDER_TEXTURE_DESC BuildHiZ::getHiZTextureDesc(UINT32 viewWidth, UINT32 viewHeight)
  827. {
  828. UINT32 size = Bitwise::nextPow2(std::max(viewWidth, viewHeight));
  829. UINT32 numMips = PixelUtil::getMaxMipmaps(size, size, 1, PF_FLOAT32_R);
  830. size = 1 << numMips;
  831. // Note: Use the 32-bit buffer here as 16-bit causes too much banding (most of the scene gets assigned 4-5 different
  832. // depth values).
  833. // - When I add UNORM 16-bit format I should be able to switch to that
  834. return POOLED_RENDER_TEXTURE_DESC::create2D(PF_FLOAT32_R, size, size, TU_RENDERTARGET, 1, false, 1, numMips);
  835. }
  836. FXAAParamDef gFXAAParamDef;
  837. FXAAMat::FXAAMat()
  838. {
  839. mParamBuffer = gFXAAParamDef.createBuffer();
  840. mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
  841. mParamsSet->getGpuParams()->getTextureParam(GPT_FRAGMENT_PROGRAM, "gInputTex", mInputTexture);
  842. }
  843. void FXAAMat::_initVariations(ShaderVariations& variations)
  844. {
  845. // Do nothing
  846. }
  847. void FXAAMat::execute(const SPtr<Texture>& source, const SPtr<RenderTarget>& destination)
  848. {
  849. const TextureProperties& srcProps = source->getProperties();
  850. Vector2 invTexSize(1.0f / srcProps.getWidth(), 1.0f / srcProps.getHeight());
  851. gFXAAParamDef.gInvTexSize.set(mParamBuffer, invTexSize);
  852. mInputTexture.set(source);
  853. RenderAPI& rapi = RenderAPI::instance();
  854. rapi.setRenderTarget(destination);
  855. gRendererUtility().setPass(mMaterial);
  856. gRendererUtility().setPassParams(mParamsSet);
  857. gRendererUtility().drawScreenQuad();
  858. }
  859. SSAOParamDef gSSAOParamDef;
  860. #define VARIATION(QUALITY) \
  861. ShaderVariation SSAOMat::VAR_Upsample_Final_Quality##QUALITY = ShaderVariation({ \
  862. ShaderVariation::Param("MIX_WITH_UPSAMPLED", true), \
  863. ShaderVariation::Param("FINAL_AO", true), \
  864. ShaderVariation::Param("QUALITY", QUALITY) \
  865. }); \
  866. ShaderVariation SSAOMat::VAR_Upsample_NoFinal_Quality##QUALITY = ShaderVariation({ \
  867. ShaderVariation::Param("MIX_WITH_UPSAMPLED", true), \
  868. ShaderVariation::Param("FINAL_AO", false), \
  869. ShaderVariation::Param("QUALITY", QUALITY) \
  870. }); \
  871. ShaderVariation SSAOMat::VAR_NoUpsample_Final_Quality##QUALITY = ShaderVariation({ \
  872. ShaderVariation::Param("MIX_WITH_UPSAMPLED", false), \
  873. ShaderVariation::Param("FINAL_AO", true), \
  874. ShaderVariation::Param("QUALITY", QUALITY) \
  875. }); \
  876. ShaderVariation SSAOMat::VAR_NoUpsample_NoFinal_Quality##QUALITY = ShaderVariation({ \
  877. ShaderVariation::Param("MIX_WITH_UPSAMPLED", false), \
  878. ShaderVariation::Param("FINAL_AO", false), \
  879. ShaderVariation::Param("QUALITY", QUALITY) \
  880. });
  881. VARIATION(0)
  882. VARIATION(1)
  883. VARIATION(2)
  884. VARIATION(3)
  885. VARIATION(4)
  886. #undef VARIATION
  887. SSAOMat::SSAOMat()
  888. {
  889. mParamBuffer = gSSAOParamDef.createBuffer();
  890. mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
  891. SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
  892. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gDepthTex", mDepthTexture);
  893. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gNormalsTex", mNormalsTexture);
  894. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gDownsampledAO", mDownsampledAOTexture);
  895. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gSetupAO", mSetupAOTexture);
  896. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gRandomTex", mRandomTexture);
  897. SAMPLER_STATE_DESC inputSampDesc;
  898. inputSampDesc.minFilter = FO_POINT;
  899. inputSampDesc.magFilter = FO_POINT;
  900. inputSampDesc.mipFilter = FO_POINT;
  901. inputSampDesc.addressMode.u = TAM_CLAMP;
  902. inputSampDesc.addressMode.v = TAM_CLAMP;
  903. inputSampDesc.addressMode.w = TAM_CLAMP;
  904. SPtr<SamplerState> inputSampState = SamplerState::create(inputSampDesc);
  905. gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gInputSamp", inputSampState);
  906. SAMPLER_STATE_DESC randomSampDesc;
  907. randomSampDesc.minFilter = FO_POINT;
  908. randomSampDesc.magFilter = FO_POINT;
  909. randomSampDesc.mipFilter = FO_POINT;
  910. randomSampDesc.addressMode.u = TAM_WRAP;
  911. randomSampDesc.addressMode.v = TAM_WRAP;
  912. randomSampDesc.addressMode.w = TAM_WRAP;
  913. SPtr<SamplerState> randomSampState = SamplerState::create(randomSampDesc);
  914. gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gRandomSamp", randomSampState);
  915. }
  916. void SSAOMat::_initVariations(ShaderVariations& variations)
  917. {
  918. #define VARIATION(QUALITY) \
  919. variations.add(VAR_Upsample_Final_Quality##QUALITY); \
  920. variations.add(VAR_Upsample_NoFinal_Quality##QUALITY); \
  921. variations.add(VAR_NoUpsample_Final_Quality##QUALITY); \
  922. variations.add(VAR_NoUpsample_NoFinal_Quality##QUALITY); \
  923. VARIATION(0)
  924. VARIATION(1)
  925. VARIATION(2)
  926. VARIATION(3)
  927. VARIATION(4)
  928. #undef VARIATION
  929. }
  930. void SSAOMat::execute(const RendererView& view, const SSAOTextureInputs& textures,
  931. const SPtr<RenderTexture>& destination, const AmbientOcclusionSettings& settings)
  932. {
  933. // Scale that can be used to adjust how quickly does AO radius increase with downsampled AO. This yields a very
  934. // small AO radius at highest level, and very large radius at lowest level
  935. static const float DOWNSAMPLE_SCALE = 4.0f;
  936. const RendererViewProperties& viewProps = view.getProperties();
  937. const RenderTargetProperties& rtProps = destination->getProperties();
  938. Vector2 tanHalfFOV;
  939. tanHalfFOV.x = 1.0f / viewProps.projTransform[0][0];
  940. tanHalfFOV.y = 1.0f / viewProps.projTransform[1][1];
  941. float cotHalfFOV = viewProps.projTransform[0][0];
  942. // Downsampled AO uses a larger AO radius (in higher resolutions this would cause too much cache trashing). This
  943. // means if only full res AO is used, then only AO from nearby geometry will be calculated.
  944. float viewScale = viewProps.viewRect.width / (float)rtProps.getWidth();
  945. // Ramp up the radius exponentially. c^log2(x) function chosen arbitrarily, as it ramps up the radius in a nice way
  946. float scale = pow(DOWNSAMPLE_SCALE, Math::log2(viewScale));
  947. // Determine maximum radius scale (division by 4 because we don't downsample more than quarter-size)
  948. float maxScale = pow(DOWNSAMPLE_SCALE, Math::log2(4.0f));
  949. // Normalize the scale in [0, 1] range
  950. scale /= maxScale;
  951. float radius = settings.radius * scale;
  952. // Factors used for scaling the AO contribution with range
  953. Vector2 fadeMultiplyAdd;
  954. fadeMultiplyAdd.x = 1.0f / settings.fadeRange;
  955. fadeMultiplyAdd.y = -settings.fadeDistance / settings.fadeRange;
  956. gSSAOParamDef.gSampleRadius.set(mParamBuffer, radius);
  957. gSSAOParamDef.gCotHalfFOV.set(mParamBuffer, cotHalfFOV);
  958. gSSAOParamDef.gTanHalfFOV.set(mParamBuffer, tanHalfFOV);
  959. gSSAOParamDef.gWorldSpaceRadiusMask.set(mParamBuffer, 1.0f);
  960. gSSAOParamDef.gBias.set(mParamBuffer, (settings.bias * viewScale) / 1000.0f);
  961. gSSAOParamDef.gFadeMultiplyAdd.set(mParamBuffer, fadeMultiplyAdd);
  962. gSSAOParamDef.gPower.set(mParamBuffer, settings.power);
  963. gSSAOParamDef.gIntensity.set(mParamBuffer, settings.intensity);
  964. bool upsample = mVariation.getBool("MIX_WITH_UPSAMPLED");
  965. if(upsample)
  966. {
  967. const TextureProperties& props = textures.aoDownsampled->getProperties();
  968. Vector2 downsampledPixelSize;
  969. downsampledPixelSize.x = 1.0f / props.getWidth();
  970. downsampledPixelSize.y = 1.0f / props.getHeight();
  971. gSSAOParamDef.gDownsampledPixelSize.set(mParamBuffer, downsampledPixelSize);
  972. }
  973. // Generate a scale which we need to use in order to achieve tiling
  974. const TextureProperties& rndProps = textures.randomRotations->getProperties();
  975. UINT32 rndWidth = rndProps.getWidth();
  976. UINT32 rndHeight = rndProps.getHeight();
  977. //// Multiple of random texture size, rounded up
  978. UINT32 scaleWidth = (rtProps.getWidth() + rndWidth - 1) / rndWidth;
  979. UINT32 scaleHeight = (rtProps.getHeight() + rndHeight - 1) / rndHeight;
  980. Vector2 randomTileScale((float)scaleWidth, (float)scaleHeight);
  981. gSSAOParamDef.gRandomTileScale.set(mParamBuffer, randomTileScale);
  982. mSetupAOTexture.set(textures.aoSetup);
  983. bool finalPass = mVariation.getBool("FINAL_AO");
  984. if (finalPass)
  985. {
  986. mDepthTexture.set(textures.sceneDepth);
  987. mNormalsTexture.set(textures.sceneNormals);
  988. }
  989. if (upsample)
  990. mDownsampledAOTexture.set(textures.aoDownsampled);
  991. mRandomTexture.set(textures.randomRotations);
  992. SPtr<GpuParamBlockBuffer> perView = view.getPerViewBuffer();
  993. mParamsSet->setParamBlockBuffer("PerCamera", perView);
  994. RenderAPI& rapi = RenderAPI::instance();
  995. rapi.setRenderTarget(destination);
  996. gRendererUtility().setPass(mMaterial);
  997. gRendererUtility().setPassParams(mParamsSet);
  998. gRendererUtility().drawScreenQuad();
  999. }
  1000. SSAOMat* SSAOMat::getVariation(bool upsample, bool finalPass, int quality)
  1001. {
  1002. #define PICK_MATERIAL(QUALITY) \
  1003. if(upsample) \
  1004. if(finalPass) \
  1005. return get(VAR_Upsample_Final_Quality##QUALITY); \
  1006. else \
  1007. return get(VAR_Upsample_NoFinal_Quality##QUALITY); \
  1008. else \
  1009. if(finalPass) \
  1010. return get(VAR_NoUpsample_Final_Quality##QUALITY); \
  1011. else \
  1012. return get(VAR_NoUpsample_NoFinal_Quality##QUALITY); \
  1013. switch(quality)
  1014. {
  1015. case 0:
  1016. PICK_MATERIAL(0)
  1017. case 1:
  1018. PICK_MATERIAL(1)
  1019. case 2:
  1020. PICK_MATERIAL(2)
  1021. case 3:
  1022. PICK_MATERIAL(3)
  1023. default:
  1024. case 4:
  1025. PICK_MATERIAL(4)
  1026. }
  1027. #undef PICK_MATERIAL
  1028. }
  1029. SSAODownsampleParamDef gSSAODownsampleParamDef;
  1030. SSAODownsampleMat::SSAODownsampleMat()
  1031. {
  1032. mParamBuffer = gSSAODownsampleParamDef.createBuffer();
  1033. mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
  1034. SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
  1035. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gDepthTex", mDepthTexture);
  1036. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gNormalsTex", mNormalsTexture);
  1037. SAMPLER_STATE_DESC inputSampDesc;
  1038. inputSampDesc.minFilter = FO_LINEAR;
  1039. inputSampDesc.magFilter = FO_LINEAR;
  1040. inputSampDesc.mipFilter = FO_LINEAR;
  1041. inputSampDesc.addressMode.u = TAM_CLAMP;
  1042. inputSampDesc.addressMode.v = TAM_CLAMP;
  1043. inputSampDesc.addressMode.w = TAM_CLAMP;
  1044. SPtr<SamplerState> inputSampState = SamplerState::create(inputSampDesc);
  1045. gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gInputSamp", inputSampState);
  1046. }
  1047. void SSAODownsampleMat::_initVariations(ShaderVariations& variations)
  1048. {
  1049. // Do nothing
  1050. }
  1051. void SSAODownsampleMat::execute(const RendererView& view, const SPtr<Texture>& depth, const SPtr<Texture>& normals,
  1052. const SPtr<RenderTexture>& destination, float depthRange)
  1053. {
  1054. const RendererViewProperties& viewProps = view.getProperties();
  1055. const RenderTargetProperties& rtProps = destination->getProperties();
  1056. Vector2 pixelSize;
  1057. pixelSize.x = 1.0f / rtProps.getWidth();
  1058. pixelSize.y = 1.0f / rtProps.getHeight();
  1059. float scale = viewProps.viewRect.width / (float)rtProps.getWidth();
  1060. gSSAODownsampleParamDef.gPixelSize.set(mParamBuffer, pixelSize);
  1061. gSSAODownsampleParamDef.gInvDepthThreshold.set(mParamBuffer, (1.0f / depthRange) / scale);
  1062. mDepthTexture.set(depth);
  1063. mNormalsTexture.set(normals);
  1064. SPtr<GpuParamBlockBuffer> perView = view.getPerViewBuffer();
  1065. mParamsSet->setParamBlockBuffer("PerCamera", perView);
  1066. RenderAPI& rapi = RenderAPI::instance();
  1067. rapi.setRenderTarget(destination);
  1068. gRendererUtility().setPass(mMaterial);
  1069. gRendererUtility().setPassParams(mParamsSet);
  1070. gRendererUtility().drawScreenQuad();
  1071. }
  1072. SSAOBlurParamDef gSSAOBlurParamDef;
  1073. ShaderVariation SSAOBlurMat::VAR_Vertical = ShaderVariation({
  1074. ShaderVariation::Param("DIR_HORZ", false)
  1075. });
  1076. ShaderVariation SSAOBlurMat::VAR_Horizontal = ShaderVariation({
  1077. ShaderVariation::Param("DIR_HORZ", true)
  1078. });
  1079. SSAOBlurMat::SSAOBlurMat()
  1080. {
  1081. mParamBuffer = gSSAOBlurParamDef.createBuffer();
  1082. mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
  1083. SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
  1084. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gInputTex", mAOTexture);
  1085. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gDepthTex", mDepthTexture);
  1086. SAMPLER_STATE_DESC inputSampDesc;
  1087. inputSampDesc.minFilter = FO_POINT;
  1088. inputSampDesc.magFilter = FO_POINT;
  1089. inputSampDesc.mipFilter = FO_POINT;
  1090. inputSampDesc.addressMode.u = TAM_CLAMP;
  1091. inputSampDesc.addressMode.v = TAM_CLAMP;
  1092. inputSampDesc.addressMode.w = TAM_CLAMP;
  1093. SPtr<SamplerState> inputSampState = SamplerState::create(inputSampDesc);
  1094. gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gInputSamp", inputSampState);
  1095. }
  1096. void SSAOBlurMat::_initVariations(ShaderVariations& variations)
  1097. {
  1098. variations.add(VAR_Horizontal);
  1099. variations.add(VAR_Vertical);
  1100. }
  1101. void SSAOBlurMat::execute(const RendererView& view, const SPtr<Texture>& ao, const SPtr<Texture>& depth,
  1102. const SPtr<RenderTexture>& destination, float depthRange)
  1103. {
  1104. const RendererViewProperties& viewProps = view.getProperties();
  1105. const TextureProperties& texProps = ao->getProperties();
  1106. Vector2 pixelSize;
  1107. pixelSize.x = 1.0f / texProps.getWidth();
  1108. pixelSize.y = 1.0f / texProps.getHeight();
  1109. Vector2 pixelOffset(BsZero);
  1110. if (mVariation.getBool("DIR_HORZ"))
  1111. pixelOffset.x = pixelSize.x;
  1112. else
  1113. pixelOffset.y = pixelSize.y;
  1114. float scale = viewProps.viewRect.width / (float)texProps.getWidth();
  1115. gSSAOBlurParamDef.gPixelSize.set(mParamBuffer, pixelSize);
  1116. gSSAOBlurParamDef.gPixelOffset.set(mParamBuffer, pixelOffset);
  1117. gSSAOBlurParamDef.gInvDepthThreshold.set(mParamBuffer, (1.0f / depthRange) / scale);
  1118. mAOTexture.set(ao);
  1119. mDepthTexture.set(depth);
  1120. SPtr<GpuParamBlockBuffer> perView = view.getPerViewBuffer();
  1121. mParamsSet->setParamBlockBuffer("PerCamera", perView);
  1122. RenderAPI& rapi = RenderAPI::instance();
  1123. rapi.setRenderTarget(destination);
  1124. gRendererUtility().setPass(mMaterial);
  1125. gRendererUtility().setPassParams(mParamsSet);
  1126. gRendererUtility().drawScreenQuad();
  1127. }
  1128. SSAOBlurMat* SSAOBlurMat::getVariation(bool horizontal)
  1129. {
  1130. if (horizontal)
  1131. return get(VAR_Horizontal);
  1132. return get(VAR_Vertical);
  1133. }
  1134. SSAO::SSAO()
  1135. {
  1136. mSSAORandomizationTex = generate4x4RandomizationTexture();
  1137. }
  1138. void SSAO::execute(const RendererView& view, const SPtr<RenderTexture>& destination,
  1139. const AmbientOcclusionSettings& settings)
  1140. {
  1141. /** Maximum valid depth range within samples in a sample set. In meters. */
  1142. static const float DEPTH_RANGE = 1.0f;
  1143. const RendererViewProperties& viewProps = view.getProperties();
  1144. SPtr<RenderTargets> renderTargets = view.getRenderTargets();
  1145. SPtr<Texture> sceneDepth = renderTargets->get(RTT_ResolvedDepth);
  1146. SPtr<Texture> sceneNormals = renderTargets->get(RTT_GBuffer, RT_COLOR1);
  1147. const TextureProperties& normalsProps = sceneNormals->getProperties();
  1148. SPtr<PooledRenderTexture> resolvedNormals;
  1149. if(sceneNormals->getProperties().getNumSamples() > 1)
  1150. {
  1151. POOLED_RENDER_TEXTURE_DESC desc = POOLED_RENDER_TEXTURE_DESC::create2D(normalsProps.getFormat(),
  1152. normalsProps.getWidth(), normalsProps.getHeight(), TU_RENDERTARGET);
  1153. resolvedNormals = GpuResourcePool::instance().get(desc);
  1154. RenderAPI::instance().setRenderTarget(resolvedNormals->renderTexture);
  1155. gRendererUtility().blit(sceneNormals);
  1156. sceneNormals = resolvedNormals->texture;
  1157. }
  1158. // Multiple downsampled AO levels are used to minimize cache trashing. Downsampled AO targets use larger radius,
  1159. // whose contents are then blended with the higher level.
  1160. UINT32 quality = settings.quality;
  1161. UINT32 numDownsampleLevels = 0;
  1162. if (quality > 1)
  1163. numDownsampleLevels = 1;
  1164. else if (quality > 2)
  1165. numDownsampleLevels = 2;
  1166. SSAODownsampleMat* downsample = SSAODownsampleMat::get();
  1167. SPtr<PooledRenderTexture> setupTex0;
  1168. if(numDownsampleLevels > 0)
  1169. {
  1170. Vector2I downsampledSize(
  1171. std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.width, 2)),
  1172. std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.height, 2))
  1173. );
  1174. POOLED_RENDER_TEXTURE_DESC desc = POOLED_RENDER_TEXTURE_DESC::create2D(PF_FLOAT16_RGBA, downsampledSize.x,
  1175. downsampledSize.y, TU_RENDERTARGET);
  1176. setupTex0 = GpuResourcePool::instance().get(desc);
  1177. downsample->execute(view, sceneDepth, sceneNormals, setupTex0->renderTexture, DEPTH_RANGE);
  1178. }
  1179. SPtr<PooledRenderTexture> setupTex1;
  1180. if(numDownsampleLevels > 1)
  1181. {
  1182. Vector2I downsampledSize(
  1183. std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.width, 4)),
  1184. std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.height, 4))
  1185. );
  1186. POOLED_RENDER_TEXTURE_DESC desc = POOLED_RENDER_TEXTURE_DESC::create2D(PF_FLOAT16_RGBA, downsampledSize.x,
  1187. downsampledSize.y, TU_RENDERTARGET);
  1188. setupTex1 = GpuResourcePool::instance().get(desc);
  1189. downsample->execute(view, sceneDepth, sceneNormals, setupTex1->renderTexture, DEPTH_RANGE);
  1190. }
  1191. SSAOTextureInputs textures;
  1192. textures.sceneDepth = sceneDepth;
  1193. textures.sceneNormals = sceneNormals;
  1194. textures.randomRotations = mSSAORandomizationTex;
  1195. SPtr<PooledRenderTexture> downAOTex1;
  1196. if(numDownsampleLevels > 1)
  1197. {
  1198. textures.aoSetup = setupTex1->texture;
  1199. Vector2I downsampledSize(
  1200. std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.width, 4)),
  1201. std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.height, 4))
  1202. );
  1203. POOLED_RENDER_TEXTURE_DESC desc = POOLED_RENDER_TEXTURE_DESC::create2D(PF_R8, downsampledSize.x,
  1204. downsampledSize.y, TU_RENDERTARGET);
  1205. downAOTex1 = GpuResourcePool::instance().get(desc);
  1206. SSAOMat* ssaoMat = SSAOMat::getVariation(false, false, quality);
  1207. ssaoMat->execute(view, textures, downAOTex1->renderTexture, settings);
  1208. GpuResourcePool::instance().release(setupTex1);
  1209. setupTex1 = nullptr;
  1210. }
  1211. SPtr<PooledRenderTexture> downAOTex0;
  1212. if(numDownsampleLevels > 0)
  1213. {
  1214. textures.aoSetup = setupTex0->texture;
  1215. if(downAOTex1)
  1216. textures.aoDownsampled = downAOTex1->texture;
  1217. Vector2I downsampledSize(
  1218. std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.width, 2)),
  1219. std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.height, 2))
  1220. );
  1221. POOLED_RENDER_TEXTURE_DESC desc = POOLED_RENDER_TEXTURE_DESC::create2D(PF_R8, downsampledSize.x,
  1222. downsampledSize.y, TU_RENDERTARGET);
  1223. downAOTex0 = GpuResourcePool::instance().get(desc);
  1224. bool upsample = numDownsampleLevels > 1;
  1225. SSAOMat* ssaoMat = SSAOMat::getVariation(upsample, false, quality);
  1226. ssaoMat->execute(view, textures, downAOTex0->renderTexture, settings);
  1227. if(upsample)
  1228. {
  1229. GpuResourcePool::instance().release(downAOTex1);
  1230. downAOTex1 = nullptr;
  1231. }
  1232. }
  1233. {
  1234. if(setupTex0)
  1235. textures.aoSetup = setupTex0->texture;
  1236. if(downAOTex0)
  1237. textures.aoDownsampled = downAOTex0->texture;
  1238. bool upsample = numDownsampleLevels > 0;
  1239. SSAOMat* ssaoMat = SSAOMat::getVariation(upsample, true, quality);
  1240. ssaoMat->execute(view, textures, destination, settings);
  1241. }
  1242. if(resolvedNormals)
  1243. {
  1244. GpuResourcePool::instance().release(resolvedNormals);
  1245. resolvedNormals = nullptr;
  1246. }
  1247. if(numDownsampleLevels > 0)
  1248. {
  1249. GpuResourcePool::instance().release(setupTex0);
  1250. GpuResourcePool::instance().release(downAOTex0);
  1251. }
  1252. // Blur the output
  1253. // Note: If I implement temporal AA then this can probably be avoided. I can instead jitter the sample offsets
  1254. // each frame, and averaging them out should yield blurred AO.
  1255. if(quality > 1) // On level 0 we don't blur at all, on level 1 we use the ad-hoc blur in shader
  1256. {
  1257. const RenderTargetProperties& rtProps = destination->getProperties();
  1258. POOLED_RENDER_TEXTURE_DESC desc = POOLED_RENDER_TEXTURE_DESC::create2D(PF_R8, rtProps.getWidth(),
  1259. rtProps.getHeight(), TU_RENDERTARGET);
  1260. SPtr<PooledRenderTexture> blurIntermediateTex = GpuResourcePool::instance().get(desc);
  1261. SSAOBlurMat* blurHorz = SSAOBlurMat::getVariation(true);
  1262. SSAOBlurMat* blurVert = SSAOBlurMat::getVariation(false);
  1263. blurHorz->execute(view, destination->getColorTexture(0), sceneDepth, blurIntermediateTex->renderTexture,
  1264. DEPTH_RANGE);
  1265. blurVert->execute(view, blurIntermediateTex->texture, sceneDepth, destination, DEPTH_RANGE);
  1266. GpuResourcePool::instance().release(blurIntermediateTex);
  1267. }
  1268. }
  1269. SPtr<Texture> SSAO::generate4x4RandomizationTexture() const
  1270. {
  1271. UINT32 mapping[16] = { 13, 5, 1, 9, 14, 3, 7, 11, 15, 2, 6, 12, 4, 8, 0, 10 };
  1272. Vector2 bases[16];
  1273. for (UINT32 i = 0; i < 16; ++i)
  1274. {
  1275. float angle = (mapping[i] / 16.0f) * Math::PI;
  1276. bases[i].x = cos(angle);
  1277. bases[i].y = sin(angle);
  1278. }
  1279. SPtr<PixelData> pixelData = PixelData::create(4, 4, 1, PF_R8G8);
  1280. for(UINT32 y = 0; y < 4; ++y)
  1281. for(UINT32 x = 0; x < 4; ++x)
  1282. {
  1283. UINT32 base = (y * 4) + x;
  1284. Color color;
  1285. color.r = bases[base].x * 0.5f + 0.5f;
  1286. color.g = bases[base].y * 0.5f + 0.5f;
  1287. pixelData->setColorAt(color, x, y);
  1288. }
  1289. return Texture::create(pixelData);
  1290. }
  1291. SSRStencilParamDef gSSRStencilParamDef;
  1292. SSRStencilMat::SSRStencilMat()
  1293. :mGBufferParams(mMaterial, mParamsSet)
  1294. {
  1295. mParamBuffer = gSSRStencilParamDef.createBuffer();
  1296. mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
  1297. }
  1298. void SSRStencilMat::_initVariations(ShaderVariations& variations)
  1299. {
  1300. // Do nothing
  1301. }
  1302. void SSRStencilMat::execute(const RendererView& view, const ScreenSpaceReflectionsSettings& settings)
  1303. {
  1304. const RendererViewProperties& viewProps = view.getProperties();
  1305. RenderTargets& renderTargets = *view.getRenderTargets();
  1306. mGBufferParams.bind(renderTargets);
  1307. Vector2 roughnessScaleBias = SSRTraceMat::calcRoughnessFadeScaleBias(settings.maxRoughness);
  1308. gSSRStencilParamDef.gRoughnessScaleBias.set(mParamBuffer, roughnessScaleBias);
  1309. SPtr<GpuParamBlockBuffer> perView = view.getPerViewBuffer();
  1310. mParamsSet->setParamBlockBuffer("PerCamera", perView);
  1311. gRendererUtility().setPass(mMaterial);
  1312. gRendererUtility().setPassParams(mParamsSet);
  1313. gRendererUtility().drawScreenQuad();
  1314. }
  1315. SSRTraceParamDef gSSRTraceParamDef;
  1316. SSRTraceMat::SSRTraceMat()
  1317. :mGBufferParams(mMaterial, mParamsSet)
  1318. {
  1319. mParamBuffer = gSSRTraceParamDef.createBuffer();
  1320. SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
  1321. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gSceneColor", mSceneColorTexture);
  1322. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gHiZ", mHiZTexture);
  1323. if(gpuParams->hasParamBlock(GPT_FRAGMENT_PROGRAM, "Input"))
  1324. gpuParams->setParamBlockBuffer(GPT_FRAGMENT_PROGRAM, "Input", mParamBuffer);
  1325. }
  1326. void SSRTraceMat::_initVariations(ShaderVariations& variations)
  1327. {
  1328. // Do nothing
  1329. }
  1330. void SSRTraceMat::execute(const RendererView& view, const ScreenSpaceReflectionsSettings& settings,
  1331. const SPtr<RenderTarget>& destination)
  1332. {
  1333. const RendererViewProperties& viewProps = view.getProperties();
  1334. RenderTargets& renderTargets = *view.getRenderTargets();
  1335. SPtr<Texture> hiZ = renderTargets.get(RTT_HiZ);
  1336. const TextureProperties& hiZProps = hiZ->getProperties();
  1337. mGBufferParams.bind(renderTargets);
  1338. mSceneColorTexture.set(renderTargets.get(RTT_ResolvedSceneColor));
  1339. mHiZTexture.set(hiZ);
  1340. Rect2I viewRect = viewProps.viewRect;
  1341. // Maps from NDC to UV [0, 1]
  1342. Vector4 ndcToHiZUV;
  1343. ndcToHiZUV.x = 0.5f;
  1344. ndcToHiZUV.y = -0.5f;
  1345. ndcToHiZUV.z = 0.5f;
  1346. ndcToHiZUV.w = 0.5f;
  1347. // Either of these flips the Y axis, but if they're both true they cancel out
  1348. RenderAPI& rapi = RenderAPI::instance();
  1349. const RenderAPIInfo& rapiInfo = rapi.getAPIInfo();
  1350. if (rapiInfo.isFlagSet(RenderAPIFeatureFlag::UVYAxisUp) ^ rapiInfo.isFlagSet(RenderAPIFeatureFlag::NDCYAxisDown))
  1351. ndcToHiZUV.y = -ndcToHiZUV.y;
  1352. // Maps from [0, 1] to area of HiZ where depth is stored in
  1353. ndcToHiZUV.x *= (float)viewRect.width / hiZProps.getWidth();
  1354. ndcToHiZUV.y *= (float)viewRect.height / hiZProps.getHeight();
  1355. ndcToHiZUV.z *= (float)viewRect.width / hiZProps.getWidth();
  1356. ndcToHiZUV.w *= (float)viewRect.height / hiZProps.getHeight();
  1357. // Maps from HiZ UV to [0, 1] UV
  1358. Vector2 HiZUVToScreenUV;
  1359. HiZUVToScreenUV.x = hiZProps.getWidth() / (float)viewRect.width;
  1360. HiZUVToScreenUV.y = hiZProps.getHeight() / (float)viewRect.height;
  1361. // Used for roughness fading
  1362. Vector2 roughnessScaleBias = calcRoughnessFadeScaleBias(settings.maxRoughness);
  1363. Vector2I bufferSize(viewRect.width, viewRect.height);
  1364. gSSRTraceParamDef.gHiZSize.set(mParamBuffer, bufferSize);
  1365. gSSRTraceParamDef.gHiZNumMips.set(mParamBuffer, hiZProps.getNumMipmaps());
  1366. gSSRTraceParamDef.gNDCToHiZUV.set(mParamBuffer, ndcToHiZUV);
  1367. gSSRTraceParamDef.gHiZUVToScreenUV.set(mParamBuffer, HiZUVToScreenUV);
  1368. gSSRTraceParamDef.gIntensity.set(mParamBuffer, settings.intensity);
  1369. gSSRTraceParamDef.gRoughnessScaleBias.set(mParamBuffer, roughnessScaleBias);
  1370. SPtr<GpuParamBlockBuffer> perView = view.getPerViewBuffer();
  1371. mParamsSet->setParamBlockBuffer("PerCamera", perView);
  1372. rapi.setRenderTarget(destination);
  1373. gRendererUtility().setPass(mMaterial);
  1374. gRendererUtility().setPassParams(mParamsSet);
  1375. gRendererUtility().drawScreenQuad();
  1376. }
  1377. Vector2 SSRTraceMat::calcRoughnessFadeScaleBias(float maxRoughness)
  1378. {
  1379. const static float RANGE_SCALE = 2.0f;
  1380. Vector2 scaleBias;
  1381. scaleBias.x = -RANGE_SCALE / (-1.0f + maxRoughness);
  1382. scaleBias.y = (RANGE_SCALE * maxRoughness) / (-1.0f + maxRoughness);
  1383. return scaleBias;
  1384. }
  1385. TemporalResolveParamDef gTemporalResolveParamDef;
  1386. SSRResolveParamDef gSSRResolveParamDef;
  1387. ShaderVariation SSRResolveMat::VAR_EyeAdaptation = ShaderVariation({
  1388. ShaderVariation::Param("EYE_ADAPTATION", true),
  1389. ShaderVariation::Param("MSAA", false)
  1390. });
  1391. ShaderVariation SSRResolveMat::VAR_NoEyeAdaptation = ShaderVariation({
  1392. ShaderVariation::Param("EYE_ADAPTATION", false),
  1393. ShaderVariation::Param("MSAA", false)
  1394. });
  1395. SSRResolveMat::SSRResolveMat()
  1396. {
  1397. mSSRParamBuffer = gSSRResolveParamDef.createBuffer();
  1398. mTemporalParamBuffer = gTemporalResolveParamDef.createBuffer();
  1399. SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
  1400. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gSceneDepth", mSceneDepthTexture);
  1401. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gSceneColor", mSceneColorTexture);
  1402. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gPrevColor", mPrevColorTexture);
  1403. gpuParams->setParamBlockBuffer(GPT_FRAGMENT_PROGRAM, "Input", mSSRParamBuffer);
  1404. gpuParams->setParamBlockBuffer(GPT_FRAGMENT_PROGRAM, "TemporalInput", mTemporalParamBuffer);
  1405. SAMPLER_STATE_DESC pointSampDesc;
  1406. pointSampDesc.minFilter = FO_POINT;
  1407. pointSampDesc.magFilter = FO_POINT;
  1408. pointSampDesc.mipFilter = FO_POINT;
  1409. pointSampDesc.addressMode.u = TAM_CLAMP;
  1410. pointSampDesc.addressMode.v = TAM_CLAMP;
  1411. pointSampDesc.addressMode.w = TAM_CLAMP;
  1412. SPtr<SamplerState> pointSampState = SamplerState::create(pointSampDesc);
  1413. gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gPointSampler", pointSampState);
  1414. SAMPLER_STATE_DESC linearSampDesc;
  1415. linearSampDesc.minFilter = FO_POINT;
  1416. linearSampDesc.magFilter = FO_POINT;
  1417. linearSampDesc.mipFilter = FO_POINT;
  1418. linearSampDesc.addressMode.u = TAM_CLAMP;
  1419. linearSampDesc.addressMode.v = TAM_CLAMP;
  1420. linearSampDesc.addressMode.w = TAM_CLAMP;
  1421. SPtr<SamplerState> linearSampState = SamplerState::create(linearSampDesc);
  1422. gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gLinearSampler", linearSampState);
  1423. }
  1424. void SSRResolveMat::_initVariations(ShaderVariations& variations)
  1425. {
  1426. variations.add(VAR_EyeAdaptation);
  1427. variations.add(VAR_NoEyeAdaptation);
  1428. }
  1429. void SSRResolveMat::execute(const RendererView& view, const SPtr<Texture>& prevFrame,
  1430. const SPtr<Texture>& curFrame, const SPtr<Texture>& sceneDepth, const SPtr<RenderTarget>& destination)
  1431. {
  1432. // TODO - MSAA not supported (remember UV must be in pixels)
  1433. // Note: This shader should not be called when temporal AA is turned on
  1434. // Note: This shader doesn't have velocity texture enabled and will only account for camera movement (can be easily
  1435. // enabled when velocity texture is added)
  1436. // - WHen added, velocity should use a 16-bit SNORM format
  1437. mPrevColorTexture.set(prevFrame);
  1438. mSceneColorTexture.set(curFrame);
  1439. mSceneDepthTexture.set(sceneDepth);
  1440. if(mVariation.getBool("EYE_ADAPTATION"))
  1441. {
  1442. // TODO - Set eye adaptation texture
  1443. }
  1444. auto& colorProps = curFrame->getProperties(); // Assuming prev and current frame are the same size
  1445. auto& depthProps = sceneDepth->getProperties();
  1446. Vector2 colorPixelSize(1.0f / colorProps.getWidth(), 1.0f / colorProps.getHeight());
  1447. Vector2 depthPixelSize(1.0f / depthProps.getWidth(), 1.0f / depthProps.getHeight());
  1448. gSSRResolveParamDef.gSceneColorTexelSize.set(mSSRParamBuffer, colorPixelSize);
  1449. gSSRResolveParamDef.gSceneDepthTexelSize.set(mSSRParamBuffer, depthPixelSize);
  1450. // TODO - Set manual exposure value
  1451. // Generate samples
  1452. // Note: Move this code to a more general spot where it can be used by other temporal shaders.
  1453. float sampleWeights[9];
  1454. float sampleWeightsLowPass[9];
  1455. float totalWeights = 0.0f;
  1456. float totalWeightsLowPass = 0.0f;
  1457. Vector2 jitter(BsZero); // Only relevant for general case, not using this type of jitter for SSR
  1458. // Weights are generated using an exponential fit to Blackman-Harris 3.3
  1459. bool useYCoCg = false; // Only relevant for general case, not using it for SSR
  1460. float sharpness = 1.0f; // Make this a customizable parameter eventually
  1461. if(useYCoCg)
  1462. {
  1463. static const Vector2 sampleOffsets[] =
  1464. {
  1465. { 0.0f, -1.0f },
  1466. { -1.0f, 0.0f },
  1467. { 0.0f, 0.0f },
  1468. { 1.0f, 0.0f },
  1469. { 0.0f, 1.0f },
  1470. };
  1471. for (UINT32 i = 0; i < 5; ++i)
  1472. {
  1473. // Get rid of jitter introduced by the projection matrix
  1474. Vector2 offset = sampleOffsets[i] - jitter;
  1475. offset *= 1.0f + sharpness * 0.5f;
  1476. sampleWeights[i] = exp(-2.29f * offset.dot(offset));
  1477. totalWeights += sampleWeights[i];
  1478. }
  1479. for (UINT32 i = 5; i < 9; ++i)
  1480. sampleWeights[i] = 0.0f;
  1481. memset(sampleWeightsLowPass, 0, sizeof(sampleWeightsLowPass));
  1482. totalWeightsLowPass = 1.0f;
  1483. }
  1484. else
  1485. {
  1486. static const Vector2 sampleOffsets[] =
  1487. {
  1488. { -1.0f, -1.0f },
  1489. { 0.0f, -1.0f },
  1490. { 1.0f, -1.0f },
  1491. { -1.0f, 0.0f },
  1492. { 0.0f, 0.0f },
  1493. { 1.0f, 0.0f },
  1494. { -1.0f, 1.0f },
  1495. { 0.0f, 1.0f },
  1496. { 1.0f, 1.0f },
  1497. };
  1498. for (UINT32 i = 0; i < 9; ++i)
  1499. {
  1500. // Get rid of jitter introduced by the projection matrix
  1501. Vector2 offset = sampleOffsets[i] - jitter;
  1502. offset *= 1.0f + sharpness * 0.5f;
  1503. sampleWeights[i] = exp(-2.29f * offset.dot(offset));
  1504. totalWeights += sampleWeights[i];
  1505. // Low pass
  1506. offset *= 0.25f;
  1507. sampleWeightsLowPass[i] = exp(-2.29f * offset.dot(offset));
  1508. totalWeightsLowPass += sampleWeightsLowPass[i];
  1509. }
  1510. }
  1511. for (UINT32 i = 0; i < 9; ++i)
  1512. {
  1513. gTemporalResolveParamDef.gSampleWeights.set(mTemporalParamBuffer, sampleWeights[i] / totalWeights);
  1514. gTemporalResolveParamDef.gSampleWeightsLowpass.set(mTemporalParamBuffer, sampleWeightsLowPass[i] / totalWeightsLowPass);
  1515. }
  1516. SPtr<GpuParamBlockBuffer> perView = view.getPerViewBuffer();
  1517. mParamsSet->setParamBlockBuffer("PerCamera", perView);
  1518. RenderAPI& rapi = RenderAPI::instance();
  1519. rapi.setRenderTarget(destination);
  1520. gRendererUtility().setPass(mMaterial);
  1521. gRendererUtility().setPassParams(mParamsSet);
  1522. gRendererUtility().drawScreenQuad();
  1523. }
  1524. void PostProcessing::postProcess(RendererView* viewInfo, const SPtr<RenderTargets>& renderTargets, float frameDelta)
  1525. {
  1526. auto& viewProps = viewInfo->getProperties();
  1527. PostProcessInfo& ppInfo = viewInfo->getPPInfo();
  1528. const StandardPostProcessSettings& settings = *ppInfo.settings;
  1529. // DEBUG ONLY
  1530. //SSRTraceMat ssrTrace;
  1531. //renderTargets->allocate(RTT_ResolvedSceneColorSecondary);
  1532. //SPtr<RenderTarget> target = renderTargets->getRT(RTT_ResolvedSceneColorSecondary);
  1533. //ssrTrace.execute(*viewInfo, ppInfo.settings->screenSpaceReflections, target);
  1534. //RenderAPI::instance().setRenderTarget(renderTargets->getRT(RTT_ResolvedSceneColor));
  1535. //gRendererUtility().blit(renderTargets->get(RTT_ResolvedSceneColorSecondary));
  1536. }
  1537. void PostProcessing::buildSSAO(const RendererView& view)
  1538. {
  1539. const SPtr<RenderTargets> renderTargets = view.getRenderTargets();
  1540. const PostProcessInfo& ppInfo = view.getPPInfo();
  1541. mSSAO.execute(view, renderTargets->getRT(RTT_AmbientOcclusion), ppInfo.settings->ambientOcclusion);
  1542. }
  1543. }}