BsPostProcessing.cpp 64 KB

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