BsPostProcessing.cpp 67 KB

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