BsPostProcessing.cpp 55 KB

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