BsPostProcessing.cpp 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988
  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 "BsRenderBeast.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. BuildHiZFParamDef gBuildHiZParamDef;
  772. BuildHiZMat::BuildHiZMat()
  773. {
  774. SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
  775. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gDepthTex", mInputTexture);
  776. // If no texture view support, we must manually pick a valid mip level in the shader
  777. const RenderAPIInfo& rapiInfo = RenderAPI::instance().getAPIInfo();
  778. if(!rapiInfo.isFlagSet(RenderAPIFeatureFlag::TextureViews))
  779. {
  780. mParamBuffer = gBuildHiZParamDef.createBuffer();
  781. SAMPLER_STATE_DESC inputSampDesc;
  782. inputSampDesc.minFilter = FO_POINT;
  783. inputSampDesc.magFilter = FO_POINT;
  784. inputSampDesc.mipFilter = FO_POINT;
  785. SPtr<SamplerState> inputSampState = SamplerState::create(inputSampDesc);
  786. if(gpuParams->hasSamplerState(GPT_FRAGMENT_PROGRAM, "gDepthSamp"))
  787. gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gDepthSamp", inputSampState);
  788. }
  789. }
  790. void BuildHiZMat::_initVariations(ShaderVariations& variations)
  791. {
  792. // Do nothing
  793. }
  794. void BuildHiZMat::execute(const SPtr<Texture>& source, UINT32 srcMip, const Rect2& srcRect, const Rect2& dstRect,
  795. const SPtr<RenderTexture>& output)
  796. {
  797. RenderAPI& rapi = RenderAPI::instance();
  798. // If no texture view support, we must manually pick a valid mip level in the shader
  799. const RenderAPIInfo& rapiInfo = RenderAPI::instance().getAPIInfo();
  800. if(rapiInfo.isFlagSet(RenderAPIFeatureFlag::TextureViews))
  801. mInputTexture.set(source, TextureSurface(srcMip));
  802. else
  803. {
  804. mInputTexture.set(source);
  805. auto& props = source->getProperties();
  806. float pixelWidth = (float)props.getWidth();
  807. float pixelHeight = (float)props.getHeight();
  808. Vector2 halfPixelOffset(0.5f / pixelWidth, 0.5f / pixelHeight);
  809. gBuildHiZParamDef.gHalfPixelOffset.set(mParamBuffer, halfPixelOffset);
  810. gBuildHiZParamDef.gMipLevel.set(mParamBuffer, srcMip);
  811. }
  812. rapi.setRenderTarget(output);
  813. rapi.setViewport(dstRect);
  814. gRendererUtility().setPass(mMaterial);
  815. gRendererUtility().setPassParams(mParamsSet);
  816. gRendererUtility().drawScreenQuad(srcRect);
  817. rapi.setViewport(Rect2(0, 0, 1, 1));
  818. }
  819. FXAAParamDef gFXAAParamDef;
  820. FXAAMat::FXAAMat()
  821. {
  822. mParamBuffer = gFXAAParamDef.createBuffer();
  823. SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
  824. gpuParams->setParamBlockBuffer("Input", mParamBuffer);
  825. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gInputTex", mInputTexture);
  826. }
  827. void FXAAMat::_initVariations(ShaderVariations& variations)
  828. {
  829. // Do nothing
  830. }
  831. void FXAAMat::execute(const SPtr<Texture>& source, const SPtr<RenderTarget>& destination)
  832. {
  833. const TextureProperties& srcProps = source->getProperties();
  834. Vector2 invTexSize(1.0f / srcProps.getWidth(), 1.0f / srcProps.getHeight());
  835. gFXAAParamDef.gInvTexSize.set(mParamBuffer, invTexSize);
  836. mInputTexture.set(source);
  837. RenderAPI& rapi = RenderAPI::instance();
  838. rapi.setRenderTarget(destination);
  839. gRendererUtility().setPass(mMaterial);
  840. gRendererUtility().setPassParams(mParamsSet);
  841. gRendererUtility().drawScreenQuad();
  842. }
  843. SSAOParamDef gSSAOParamDef;
  844. #define VARIATION(QUALITY) \
  845. ShaderVariation SSAOMat::VAR_Upsample_Final_Quality##QUALITY = ShaderVariation({ \
  846. ShaderVariation::Param("MIX_WITH_UPSAMPLED", true), \
  847. ShaderVariation::Param("FINAL_AO", true), \
  848. ShaderVariation::Param("QUALITY", QUALITY) \
  849. }); \
  850. ShaderVariation SSAOMat::VAR_Upsample_NoFinal_Quality##QUALITY = ShaderVariation({ \
  851. ShaderVariation::Param("MIX_WITH_UPSAMPLED", true), \
  852. ShaderVariation::Param("FINAL_AO", false), \
  853. ShaderVariation::Param("QUALITY", QUALITY) \
  854. }); \
  855. ShaderVariation SSAOMat::VAR_NoUpsample_Final_Quality##QUALITY = ShaderVariation({ \
  856. ShaderVariation::Param("MIX_WITH_UPSAMPLED", false), \
  857. ShaderVariation::Param("FINAL_AO", true), \
  858. ShaderVariation::Param("QUALITY", QUALITY) \
  859. }); \
  860. ShaderVariation SSAOMat::VAR_NoUpsample_NoFinal_Quality##QUALITY = ShaderVariation({ \
  861. ShaderVariation::Param("MIX_WITH_UPSAMPLED", false), \
  862. ShaderVariation::Param("FINAL_AO", false), \
  863. ShaderVariation::Param("QUALITY", QUALITY) \
  864. });
  865. VARIATION(0)
  866. VARIATION(1)
  867. VARIATION(2)
  868. VARIATION(3)
  869. VARIATION(4)
  870. #undef VARIATION
  871. SSAOMat::SSAOMat()
  872. {
  873. bool isFinal = mVariation.getBool("FINAL_AO");
  874. bool mixWithUpsampled = mVariation.getBool("MIX_WITH_UPSAMPLED");
  875. mParamBuffer = gSSAOParamDef.createBuffer();
  876. SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
  877. gpuParams->setParamBlockBuffer("Input", mParamBuffer);
  878. if (isFinal)
  879. {
  880. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gDepthTex", mDepthTexture);
  881. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gNormalsTex", mNormalsTexture);
  882. }
  883. if(!isFinal || mixWithUpsampled)
  884. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gSetupAO", mSetupAOTexture);
  885. if(mixWithUpsampled)
  886. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gDownsampledAO", mDownsampledAOTexture);
  887. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gRandomTex", mRandomTexture);
  888. SAMPLER_STATE_DESC inputSampDesc;
  889. inputSampDesc.minFilter = FO_POINT;
  890. inputSampDesc.magFilter = FO_POINT;
  891. inputSampDesc.mipFilter = FO_POINT;
  892. inputSampDesc.addressMode.u = TAM_CLAMP;
  893. inputSampDesc.addressMode.v = TAM_CLAMP;
  894. inputSampDesc.addressMode.w = TAM_CLAMP;
  895. SPtr<SamplerState> inputSampState = SamplerState::create(inputSampDesc);
  896. if(gpuParams->hasSamplerState(GPT_FRAGMENT_PROGRAM, "gInputSamp"))
  897. gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gInputSamp", inputSampState);
  898. else
  899. {
  900. if (isFinal)
  901. {
  902. gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gDepthTex", inputSampState);
  903. gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gNormalsTex", inputSampState);
  904. }
  905. if(!isFinal || mixWithUpsampled)
  906. gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gSetupAO", inputSampState);
  907. if(mixWithUpsampled)
  908. gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gDownsampledAO", inputSampState);
  909. }
  910. SAMPLER_STATE_DESC randomSampDesc;
  911. randomSampDesc.minFilter = FO_POINT;
  912. randomSampDesc.magFilter = FO_POINT;
  913. randomSampDesc.mipFilter = FO_POINT;
  914. randomSampDesc.addressMode.u = TAM_WRAP;
  915. randomSampDesc.addressMode.v = TAM_WRAP;
  916. randomSampDesc.addressMode.w = TAM_WRAP;
  917. SPtr<SamplerState> randomSampState = SamplerState::create(randomSampDesc);
  918. setSamplerState(gpuParams, GPT_FRAGMENT_PROGRAM, "gRandomSamp", "gRandomTex", randomSampState);
  919. }
  920. void SSAOMat::_initVariations(ShaderVariations& variations)
  921. {
  922. #define VARIATION(QUALITY) \
  923. variations.add(VAR_Upsample_Final_Quality##QUALITY); \
  924. variations.add(VAR_Upsample_NoFinal_Quality##QUALITY); \
  925. variations.add(VAR_NoUpsample_Final_Quality##QUALITY); \
  926. variations.add(VAR_NoUpsample_NoFinal_Quality##QUALITY); \
  927. VARIATION(0)
  928. VARIATION(1)
  929. VARIATION(2)
  930. VARIATION(3)
  931. VARIATION(4)
  932. #undef VARIATION
  933. }
  934. void SSAOMat::execute(const RendererView& view, const SSAOTextureInputs& textures,
  935. const SPtr<RenderTexture>& destination, const AmbientOcclusionSettings& settings)
  936. {
  937. // Scale that can be used to adjust how quickly does AO radius increase with downsampled AO. This yields a very
  938. // small AO radius at highest level, and very large radius at lowest level
  939. static const float DOWNSAMPLE_SCALE = 4.0f;
  940. const RendererViewProperties& viewProps = view.getProperties();
  941. const RenderTargetProperties& rtProps = destination->getProperties();
  942. Vector2 tanHalfFOV;
  943. tanHalfFOV.x = 1.0f / viewProps.projTransform[0][0];
  944. tanHalfFOV.y = 1.0f / viewProps.projTransform[1][1];
  945. float cotHalfFOV = viewProps.projTransform[0][0];
  946. // Downsampled AO uses a larger AO radius (in higher resolutions this would cause too much cache trashing). This
  947. // means if only full res AO is used, then only AO from nearby geometry will be calculated.
  948. float viewScale = viewProps.viewRect.width / (float)rtProps.width;
  949. // Ramp up the radius exponentially. c^log2(x) function chosen arbitrarily, as it ramps up the radius in a nice way
  950. float scale = pow(DOWNSAMPLE_SCALE, Math::log2(viewScale));
  951. // Determine maximum radius scale (division by 4 because we don't downsample more than quarter-size)
  952. float maxScale = pow(DOWNSAMPLE_SCALE, Math::log2(4.0f));
  953. // Normalize the scale in [0, 1] range
  954. scale /= maxScale;
  955. float radius = settings.radius * scale;
  956. // Factors used for scaling the AO contribution with range
  957. Vector2 fadeMultiplyAdd;
  958. fadeMultiplyAdd.x = 1.0f / settings.fadeRange;
  959. fadeMultiplyAdd.y = -settings.fadeDistance / settings.fadeRange;
  960. gSSAOParamDef.gSampleRadius.set(mParamBuffer, radius);
  961. gSSAOParamDef.gCotHalfFOV.set(mParamBuffer, cotHalfFOV);
  962. gSSAOParamDef.gTanHalfFOV.set(mParamBuffer, tanHalfFOV);
  963. gSSAOParamDef.gWorldSpaceRadiusMask.set(mParamBuffer, 1.0f);
  964. gSSAOParamDef.gBias.set(mParamBuffer, (settings.bias * viewScale) / 1000.0f);
  965. gSSAOParamDef.gFadeMultiplyAdd.set(mParamBuffer, fadeMultiplyAdd);
  966. gSSAOParamDef.gPower.set(mParamBuffer, settings.power);
  967. gSSAOParamDef.gIntensity.set(mParamBuffer, settings.intensity);
  968. bool upsample = mVariation.getBool("MIX_WITH_UPSAMPLED");
  969. if(upsample)
  970. {
  971. const TextureProperties& props = textures.aoDownsampled->getProperties();
  972. Vector2 downsampledPixelSize;
  973. downsampledPixelSize.x = 1.0f / props.getWidth();
  974. downsampledPixelSize.y = 1.0f / props.getHeight();
  975. gSSAOParamDef.gDownsampledPixelSize.set(mParamBuffer, downsampledPixelSize);
  976. }
  977. // Generate a scale which we need to use in order to achieve tiling
  978. const TextureProperties& rndProps = textures.randomRotations->getProperties();
  979. UINT32 rndWidth = rndProps.getWidth();
  980. UINT32 rndHeight = rndProps.getHeight();
  981. //// Multiple of random texture size, rounded up
  982. UINT32 scaleWidth = (rtProps.width + rndWidth - 1) / rndWidth;
  983. UINT32 scaleHeight = (rtProps.height + rndHeight - 1) / rndHeight;
  984. Vector2 randomTileScale((float)scaleWidth, (float)scaleHeight);
  985. gSSAOParamDef.gRandomTileScale.set(mParamBuffer, randomTileScale);
  986. mSetupAOTexture.set(textures.aoSetup);
  987. bool finalPass = mVariation.getBool("FINAL_AO");
  988. if (finalPass)
  989. {
  990. mDepthTexture.set(textures.sceneDepth);
  991. mNormalsTexture.set(textures.sceneNormals);
  992. }
  993. if (upsample)
  994. mDownsampledAOTexture.set(textures.aoDownsampled);
  995. mRandomTexture.set(textures.randomRotations);
  996. SPtr<GpuParamBlockBuffer> perView = view.getPerViewBuffer();
  997. mParamsSet->getGpuParams()->setParamBlockBuffer("PerCamera", perView);
  998. RenderAPI& rapi = RenderAPI::instance();
  999. rapi.setRenderTarget(destination);
  1000. gRendererUtility().setPass(mMaterial);
  1001. gRendererUtility().setPassParams(mParamsSet);
  1002. gRendererUtility().drawScreenQuad();
  1003. }
  1004. SSAOMat* SSAOMat::getVariation(bool upsample, bool finalPass, int quality)
  1005. {
  1006. #define PICK_MATERIAL(QUALITY) \
  1007. if(upsample) \
  1008. if(finalPass) \
  1009. return get(VAR_Upsample_Final_Quality##QUALITY); \
  1010. else \
  1011. return get(VAR_Upsample_NoFinal_Quality##QUALITY); \
  1012. else \
  1013. if(finalPass) \
  1014. return get(VAR_NoUpsample_Final_Quality##QUALITY); \
  1015. else \
  1016. return get(VAR_NoUpsample_NoFinal_Quality##QUALITY); \
  1017. switch(quality)
  1018. {
  1019. case 0:
  1020. PICK_MATERIAL(0)
  1021. case 1:
  1022. PICK_MATERIAL(1)
  1023. case 2:
  1024. PICK_MATERIAL(2)
  1025. case 3:
  1026. PICK_MATERIAL(3)
  1027. default:
  1028. case 4:
  1029. PICK_MATERIAL(4)
  1030. }
  1031. #undef PICK_MATERIAL
  1032. }
  1033. SSAODownsampleParamDef gSSAODownsampleParamDef;
  1034. SSAODownsampleMat::SSAODownsampleMat()
  1035. {
  1036. mParamBuffer = gSSAODownsampleParamDef.createBuffer();
  1037. SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
  1038. gpuParams->setParamBlockBuffer("Input", mParamBuffer);
  1039. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gDepthTex", mDepthTexture);
  1040. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gNormalsTex", mNormalsTexture);
  1041. SAMPLER_STATE_DESC inputSampDesc;
  1042. inputSampDesc.minFilter = FO_LINEAR;
  1043. inputSampDesc.magFilter = FO_LINEAR;
  1044. inputSampDesc.mipFilter = FO_LINEAR;
  1045. inputSampDesc.addressMode.u = TAM_CLAMP;
  1046. inputSampDesc.addressMode.v = TAM_CLAMP;
  1047. inputSampDesc.addressMode.w = TAM_CLAMP;
  1048. SPtr<SamplerState> inputSampState = SamplerState::create(inputSampDesc);
  1049. if(gpuParams->hasSamplerState(GPT_FRAGMENT_PROGRAM, "gInputSamp"))
  1050. gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gInputSamp", inputSampState);
  1051. else
  1052. {
  1053. gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gDepthTex", inputSampState);
  1054. gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gNormalsTex", inputSampState);
  1055. }
  1056. }
  1057. void SSAODownsampleMat::_initVariations(ShaderVariations& variations)
  1058. {
  1059. // Do nothing
  1060. }
  1061. void SSAODownsampleMat::execute(const RendererView& view, const SPtr<Texture>& depth, const SPtr<Texture>& normals,
  1062. const SPtr<RenderTexture>& destination, float depthRange)
  1063. {
  1064. const RendererViewProperties& viewProps = view.getProperties();
  1065. const RenderTargetProperties& rtProps = destination->getProperties();
  1066. Vector2 pixelSize;
  1067. pixelSize.x = 1.0f / rtProps.width;
  1068. pixelSize.y = 1.0f / rtProps.height;
  1069. float scale = viewProps.viewRect.width / (float)rtProps.width;
  1070. gSSAODownsampleParamDef.gPixelSize.set(mParamBuffer, pixelSize);
  1071. gSSAODownsampleParamDef.gInvDepthThreshold.set(mParamBuffer, (1.0f / depthRange) / scale);
  1072. mDepthTexture.set(depth);
  1073. mNormalsTexture.set(normals);
  1074. SPtr<GpuParamBlockBuffer> perView = view.getPerViewBuffer();
  1075. mParamsSet->getGpuParams()->setParamBlockBuffer("PerCamera", perView);
  1076. RenderAPI& rapi = RenderAPI::instance();
  1077. rapi.setRenderTarget(destination);
  1078. gRendererUtility().setPass(mMaterial);
  1079. gRendererUtility().setPassParams(mParamsSet);
  1080. gRendererUtility().drawScreenQuad();
  1081. }
  1082. SSAOBlurParamDef gSSAOBlurParamDef;
  1083. ShaderVariation SSAOBlurMat::VAR_Vertical = ShaderVariation({
  1084. ShaderVariation::Param("DIR_HORZ", false)
  1085. });
  1086. ShaderVariation SSAOBlurMat::VAR_Horizontal = ShaderVariation({
  1087. ShaderVariation::Param("DIR_HORZ", true)
  1088. });
  1089. SSAOBlurMat::SSAOBlurMat()
  1090. {
  1091. mParamBuffer = gSSAOBlurParamDef.createBuffer();
  1092. SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
  1093. gpuParams->setParamBlockBuffer("Input", mParamBuffer);
  1094. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gInputTex", mAOTexture);
  1095. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gDepthTex", mDepthTexture);
  1096. SAMPLER_STATE_DESC inputSampDesc;
  1097. inputSampDesc.minFilter = FO_POINT;
  1098. inputSampDesc.magFilter = FO_POINT;
  1099. inputSampDesc.mipFilter = FO_POINT;
  1100. inputSampDesc.addressMode.u = TAM_CLAMP;
  1101. inputSampDesc.addressMode.v = TAM_CLAMP;
  1102. inputSampDesc.addressMode.w = TAM_CLAMP;
  1103. SPtr<SamplerState> inputSampState = SamplerState::create(inputSampDesc);
  1104. if(gpuParams->hasSamplerState(GPT_FRAGMENT_PROGRAM, "gInputSamp"))
  1105. gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gInputSamp", inputSampState);
  1106. else
  1107. {
  1108. gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gInputTex", inputSampState);
  1109. gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gDepthTex", inputSampState);
  1110. }
  1111. }
  1112. void SSAOBlurMat::_initVariations(ShaderVariations& variations)
  1113. {
  1114. variations.add(VAR_Horizontal);
  1115. variations.add(VAR_Vertical);
  1116. }
  1117. void SSAOBlurMat::execute(const RendererView& view, const SPtr<Texture>& ao, const SPtr<Texture>& depth,
  1118. const SPtr<RenderTexture>& destination, float depthRange)
  1119. {
  1120. const RendererViewProperties& viewProps = view.getProperties();
  1121. const TextureProperties& texProps = ao->getProperties();
  1122. Vector2 pixelSize;
  1123. pixelSize.x = 1.0f / texProps.getWidth();
  1124. pixelSize.y = 1.0f / texProps.getHeight();
  1125. Vector2 pixelOffset(BsZero);
  1126. if (mVariation.getBool("DIR_HORZ"))
  1127. pixelOffset.x = pixelSize.x;
  1128. else
  1129. pixelOffset.y = pixelSize.y;
  1130. float scale = viewProps.viewRect.width / (float)texProps.getWidth();
  1131. gSSAOBlurParamDef.gPixelSize.set(mParamBuffer, pixelSize);
  1132. gSSAOBlurParamDef.gPixelOffset.set(mParamBuffer, pixelOffset);
  1133. gSSAOBlurParamDef.gInvDepthThreshold.set(mParamBuffer, (1.0f / depthRange) / scale);
  1134. mAOTexture.set(ao);
  1135. mDepthTexture.set(depth);
  1136. SPtr<GpuParamBlockBuffer> perView = view.getPerViewBuffer();
  1137. mParamsSet->getGpuParams()->setParamBlockBuffer("PerCamera", perView);
  1138. RenderAPI& rapi = RenderAPI::instance();
  1139. rapi.setRenderTarget(destination);
  1140. gRendererUtility().setPass(mMaterial);
  1141. gRendererUtility().setPassParams(mParamsSet);
  1142. gRendererUtility().drawScreenQuad();
  1143. }
  1144. SSAOBlurMat* SSAOBlurMat::getVariation(bool horizontal)
  1145. {
  1146. if (horizontal)
  1147. return get(VAR_Horizontal);
  1148. return get(VAR_Vertical);
  1149. }
  1150. SSRStencilParamDef gSSRStencilParamDef;
  1151. ShaderVariation SSRStencilMat::VAR_FullMSAA = ShaderVariation({
  1152. ShaderVariation::Param("MSAA_COUNT", 2)
  1153. });
  1154. ShaderVariation SSRStencilMat::VAR_SingleMSAA = ShaderVariation({
  1155. ShaderVariation::Param("MSAA_COUNT", 2),
  1156. ShaderVariation::Param("MSAA_RESOLVE_0TH", true)
  1157. });
  1158. ShaderVariation SSRStencilMat::VAR_NoMSAA = ShaderVariation({
  1159. ShaderVariation::Param("MSAA_COUNT", 1)
  1160. });
  1161. SSRStencilMat::SSRStencilMat()
  1162. :mGBufferParams(mMaterial, mParamsSet)
  1163. {
  1164. mParamBuffer = gSSRStencilParamDef.createBuffer();
  1165. mParamsSet->getGpuParams()->setParamBlockBuffer("Input", mParamBuffer);
  1166. }
  1167. void SSRStencilMat::_initVariations(ShaderVariations& variations)
  1168. {
  1169. variations.add(VAR_FullMSAA);
  1170. variations.add(VAR_SingleMSAA);
  1171. variations.add(VAR_NoMSAA);
  1172. }
  1173. void SSRStencilMat::execute(const RendererView& view, GBufferTextures gbuffer,
  1174. const ScreenSpaceReflectionsSettings& settings)
  1175. {
  1176. mGBufferParams.bind(gbuffer);
  1177. Vector2 roughnessScaleBias = SSRTraceMat::calcRoughnessFadeScaleBias(settings.maxRoughness);
  1178. gSSRStencilParamDef.gRoughnessScaleBias.set(mParamBuffer, roughnessScaleBias);
  1179. SPtr<GpuParamBlockBuffer> perView = view.getPerViewBuffer();
  1180. mParamsSet->getGpuParams()->setParamBlockBuffer("PerCamera", perView);
  1181. const RendererViewProperties& viewProps = view.getProperties();
  1182. const Rect2I& viewRect = viewProps.viewRect;
  1183. gRendererUtility().setPass(mMaterial);
  1184. gRendererUtility().setPassParams(mParamsSet);
  1185. if(viewProps.numSamples > 1)
  1186. gRendererUtility().drawScreenQuad(Rect2(0.0f, 0.0f, (float)viewRect.width, (float)viewRect.height));
  1187. else
  1188. gRendererUtility().drawScreenQuad();
  1189. }
  1190. SSRStencilMat* SSRStencilMat::getVariation(bool msaa, bool singleSampleMSAA)
  1191. {
  1192. if (msaa)
  1193. {
  1194. if (singleSampleMSAA)
  1195. return get(VAR_SingleMSAA);
  1196. return get(VAR_FullMSAA);
  1197. }
  1198. else
  1199. return get(VAR_NoMSAA);
  1200. }
  1201. SSRTraceParamDef gSSRTraceParamDef;
  1202. #define VARIATION(QUALITY) \
  1203. ShaderVariation SSRTraceMat::VAR_NoMSAA_Quality##QUALITY = ShaderVariation({ \
  1204. ShaderVariation::Param("MSAA_COUNT", 1), \
  1205. ShaderVariation::Param("QUALITY", QUALITY) \
  1206. }); \
  1207. ShaderVariation SSRTraceMat::VAR_FullMSAA_Quality##QUALITY = ShaderVariation({ \
  1208. ShaderVariation::Param("MSAA_COUNT", 2), \
  1209. ShaderVariation::Param("QUALITY", QUALITY) \
  1210. }); \
  1211. ShaderVariation SSRTraceMat::VAR_SingleMSAA_Quality##QUALITY = ShaderVariation({ \
  1212. ShaderVariation::Param("MSAA_COUNT", 2), \
  1213. ShaderVariation::Param("MSAA_RESOLVE_0TH", true), \
  1214. ShaderVariation::Param("QUALITY", QUALITY) \
  1215. }); \
  1216. VARIATION(0)
  1217. VARIATION(1)
  1218. VARIATION(2)
  1219. VARIATION(3)
  1220. VARIATION(4)
  1221. #undef VARIATION
  1222. SSRTraceMat::SSRTraceMat()
  1223. :mGBufferParams(mMaterial, mParamsSet)
  1224. {
  1225. mParamBuffer = gSSRTraceParamDef.createBuffer();
  1226. SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
  1227. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gSceneColor", mSceneColorTexture);
  1228. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gHiZ", mHiZTexture);
  1229. if(gpuParams->hasParamBlock(GPT_FRAGMENT_PROGRAM, "Input"))
  1230. gpuParams->setParamBlockBuffer(GPT_FRAGMENT_PROGRAM, "Input", mParamBuffer);
  1231. SAMPLER_STATE_DESC desc;
  1232. desc.minFilter = FO_POINT;
  1233. desc.magFilter = FO_POINT;
  1234. desc.mipFilter = FO_POINT;
  1235. desc.addressMode.u = TAM_CLAMP;
  1236. desc.addressMode.v = TAM_CLAMP;
  1237. desc.addressMode.w = TAM_CLAMP;
  1238. SPtr<SamplerState> hiZSamplerState = SamplerState::create(desc);
  1239. if (gpuParams->hasSamplerState(GPT_FRAGMENT_PROGRAM, "gHiZSamp"))
  1240. gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gHiZSamp", hiZSamplerState);
  1241. else if(gpuParams->hasSamplerState(GPT_FRAGMENT_PROGRAM, "gHiZ"))
  1242. gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gHiZ", hiZSamplerState);
  1243. }
  1244. void SSRTraceMat::_initVariations(ShaderVariations& variations)
  1245. {
  1246. #define VARIATION(QUALITY) \
  1247. variations.add(VAR_NoMSAA_Quality##QUALITY); \
  1248. variations.add(VAR_FullMSAA_Quality##QUALITY); \
  1249. variations.add(VAR_SingleMSAA_Quality##QUALITY); \
  1250. VARIATION(0)
  1251. VARIATION(1)
  1252. VARIATION(2)
  1253. VARIATION(3)
  1254. VARIATION(4)
  1255. #undef VARIATION
  1256. }
  1257. void SSRTraceMat::execute(const RendererView& view, GBufferTextures gbuffer, const SPtr<Texture>& sceneColor,
  1258. const SPtr<Texture>& hiZ, const ScreenSpaceReflectionsSettings& settings,
  1259. const SPtr<RenderTarget>& destination)
  1260. {
  1261. const RendererViewProperties& viewProps = view.getProperties();
  1262. const TextureProperties& hiZProps = hiZ->getProperties();
  1263. mGBufferParams.bind(gbuffer);
  1264. mSceneColorTexture.set(sceneColor);
  1265. mHiZTexture.set(hiZ);
  1266. Rect2I viewRect = viewProps.viewRect;
  1267. // Maps from NDC to UV [0, 1]
  1268. Vector4 ndcToHiZUV;
  1269. ndcToHiZUV.x = 0.5f;
  1270. ndcToHiZUV.y = -0.5f;
  1271. ndcToHiZUV.z = 0.5f;
  1272. ndcToHiZUV.w = 0.5f;
  1273. // Either of these flips the Y axis, but if they're both true they cancel out
  1274. RenderAPI& rapi = RenderAPI::instance();
  1275. const RenderAPIInfo& rapiInfo = rapi.getAPIInfo();
  1276. if (rapiInfo.isFlagSet(RenderAPIFeatureFlag::UVYAxisUp) ^ rapiInfo.isFlagSet(RenderAPIFeatureFlag::NDCYAxisDown))
  1277. ndcToHiZUV.y = -ndcToHiZUV.y;
  1278. // Maps from [0, 1] to area of HiZ where depth is stored in
  1279. ndcToHiZUV.x *= (float)viewRect.width / hiZProps.getWidth();
  1280. ndcToHiZUV.y *= (float)viewRect.height / hiZProps.getHeight();
  1281. ndcToHiZUV.z *= (float)viewRect.width / hiZProps.getWidth();
  1282. ndcToHiZUV.w *= (float)viewRect.height / hiZProps.getHeight();
  1283. // Maps from HiZ UV to [0, 1] UV
  1284. Vector2 HiZUVToScreenUV;
  1285. HiZUVToScreenUV.x = hiZProps.getWidth() / (float)viewRect.width;
  1286. HiZUVToScreenUV.y = hiZProps.getHeight() / (float)viewRect.height;
  1287. // Used for roughness fading
  1288. Vector2 roughnessScaleBias = calcRoughnessFadeScaleBias(settings.maxRoughness);
  1289. UINT32 temporalJitter = (viewProps.frameIdx % 8) * 1503;
  1290. Vector2I bufferSize(viewRect.width, viewRect.height);
  1291. gSSRTraceParamDef.gHiZSize.set(mParamBuffer, bufferSize);
  1292. gSSRTraceParamDef.gHiZNumMips.set(mParamBuffer, hiZProps.getNumMipmaps());
  1293. gSSRTraceParamDef.gNDCToHiZUV.set(mParamBuffer, ndcToHiZUV);
  1294. gSSRTraceParamDef.gHiZUVToScreenUV.set(mParamBuffer, HiZUVToScreenUV);
  1295. gSSRTraceParamDef.gIntensity.set(mParamBuffer, settings.intensity);
  1296. gSSRTraceParamDef.gRoughnessScaleBias.set(mParamBuffer, roughnessScaleBias);
  1297. gSSRTraceParamDef.gTemporalJitter.set(mParamBuffer, temporalJitter);
  1298. SPtr<GpuParamBlockBuffer> perView = view.getPerViewBuffer();
  1299. mParamsSet->getGpuParams()->setParamBlockBuffer("PerCamera", perView);
  1300. rapi.setRenderTarget(destination, FBT_DEPTH);
  1301. gRendererUtility().setPass(mMaterial);
  1302. gRendererUtility().setPassParams(mParamsSet);
  1303. if(viewProps.numSamples > 1)
  1304. gRendererUtility().drawScreenQuad(Rect2(0.0f, 0.0f, (float)viewRect.width, (float)viewRect.height));
  1305. else
  1306. gRendererUtility().drawScreenQuad();
  1307. }
  1308. Vector2 SSRTraceMat::calcRoughnessFadeScaleBias(float maxRoughness)
  1309. {
  1310. const static float RANGE_SCALE = 2.0f;
  1311. Vector2 scaleBias;
  1312. scaleBias.x = -RANGE_SCALE / (-1.0f + maxRoughness);
  1313. scaleBias.y = (RANGE_SCALE * maxRoughness) / (-1.0f + maxRoughness);
  1314. return scaleBias;
  1315. }
  1316. SSRTraceMat* SSRTraceMat::getVariation(UINT32 quality, bool msaa, bool singleSampleMSAA)
  1317. {
  1318. #define PICK_MATERIAL(QUALITY) \
  1319. if(msaa) \
  1320. if(singleSampleMSAA) \
  1321. return get(VAR_SingleMSAA_Quality##QUALITY); \
  1322. else \
  1323. return get(VAR_FullMSAA_Quality##QUALITY); \
  1324. else \
  1325. return get(VAR_NoMSAA_Quality##QUALITY); \
  1326. switch(quality)
  1327. {
  1328. case 0:
  1329. PICK_MATERIAL(0)
  1330. case 1:
  1331. PICK_MATERIAL(1)
  1332. case 2:
  1333. PICK_MATERIAL(2)
  1334. case 3:
  1335. PICK_MATERIAL(3)
  1336. default:
  1337. case 4:
  1338. PICK_MATERIAL(4)
  1339. }
  1340. #undef PICK_MATERIAL
  1341. }
  1342. TemporalResolveParamDef gTemporalResolveParamDef;
  1343. SSRResolveParamDef gSSRResolveParamDef;
  1344. ShaderVariation SSRResolveMat::VAR_MSAA = ShaderVariation({
  1345. ShaderVariation::Param("MSAA", true)
  1346. });
  1347. ShaderVariation SSRResolveMat::VAR_NoMSAA = ShaderVariation({
  1348. ShaderVariation::Param("MSAA", false)
  1349. });
  1350. SSRResolveMat::SSRResolveMat()
  1351. {
  1352. mSSRParamBuffer = gSSRResolveParamDef.createBuffer();
  1353. mTemporalParamBuffer = gTemporalResolveParamDef.createBuffer();
  1354. SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
  1355. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gSceneDepth", mSceneDepthTexture);
  1356. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gSceneColor", mSceneColorTexture);
  1357. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gPrevColor", mPrevColorTexture);
  1358. gpuParams->setParamBlockBuffer(GPT_FRAGMENT_PROGRAM, "Input", mSSRParamBuffer);
  1359. gpuParams->setParamBlockBuffer(GPT_FRAGMENT_PROGRAM, "TemporalInput", mTemporalParamBuffer);
  1360. SAMPLER_STATE_DESC pointSampDesc;
  1361. pointSampDesc.minFilter = FO_POINT;
  1362. pointSampDesc.magFilter = FO_POINT;
  1363. pointSampDesc.mipFilter = FO_POINT;
  1364. pointSampDesc.addressMode.u = TAM_CLAMP;
  1365. pointSampDesc.addressMode.v = TAM_CLAMP;
  1366. pointSampDesc.addressMode.w = TAM_CLAMP;
  1367. SPtr<SamplerState> pointSampState = SamplerState::create(pointSampDesc);
  1368. if(gpuParams->hasSamplerState(GPT_FRAGMENT_PROGRAM, "gPointSampler"))
  1369. gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gPointSampler", pointSampState);
  1370. else
  1371. gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gSceneDepth", pointSampState);
  1372. SAMPLER_STATE_DESC linearSampDesc;
  1373. linearSampDesc.minFilter = FO_POINT;
  1374. linearSampDesc.magFilter = FO_POINT;
  1375. linearSampDesc.mipFilter = FO_POINT;
  1376. linearSampDesc.addressMode.u = TAM_CLAMP;
  1377. linearSampDesc.addressMode.v = TAM_CLAMP;
  1378. linearSampDesc.addressMode.w = TAM_CLAMP;
  1379. SPtr<SamplerState> linearSampState = SamplerState::create(linearSampDesc);
  1380. if(gpuParams->hasSamplerState(GPT_FRAGMENT_PROGRAM, "gLinearSampler"))
  1381. gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gLinearSampler", linearSampState);
  1382. else
  1383. {
  1384. gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gSceneColor", linearSampState);
  1385. gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gPrevColor", linearSampState);
  1386. }
  1387. }
  1388. void SSRResolveMat::_initVariations(ShaderVariations& variations)
  1389. {
  1390. variations.add(VAR_NoMSAA);
  1391. variations.add(VAR_MSAA);
  1392. }
  1393. void SSRResolveMat::execute(const RendererView& view, const SPtr<Texture>& prevFrame,
  1394. const SPtr<Texture>& curFrame, const SPtr<Texture>& sceneDepth, const SPtr<RenderTarget>& destination)
  1395. {
  1396. // Note: This shader should not be called when temporal AA is turned on
  1397. // Note: This shader doesn't have velocity texture enabled and will only account for camera movement (can be easily
  1398. // enabled when velocity texture is added)
  1399. // - WHen added, velocity should use a 16-bit SNORM format
  1400. mPrevColorTexture.set(prevFrame);
  1401. mSceneColorTexture.set(curFrame);
  1402. mSceneDepthTexture.set(sceneDepth);
  1403. auto& colorProps = curFrame->getProperties(); // Assuming prev and current frame are the same size
  1404. auto& depthProps = sceneDepth->getProperties();
  1405. Vector2 colorPixelSize(1.0f / colorProps.getWidth(), 1.0f / colorProps.getHeight());
  1406. Vector2 depthPixelSize(1.0f / depthProps.getWidth(), 1.0f / depthProps.getHeight());
  1407. gSSRResolveParamDef.gSceneColorTexelSize.set(mSSRParamBuffer, colorPixelSize);
  1408. gSSRResolveParamDef.gSceneDepthTexelSize.set(mSSRParamBuffer, depthPixelSize);
  1409. gSSRResolveParamDef.gManualExposure.set(mSSRParamBuffer, 1.0f);
  1410. // Generate samples
  1411. // Note: Move this code to a more general spot where it can be used by other temporal shaders.
  1412. float sampleWeights[9];
  1413. float sampleWeightsLowPass[9];
  1414. float totalWeights = 0.0f;
  1415. float totalWeightsLowPass = 0.0f;
  1416. Vector2 jitter(BsZero); // Only relevant for general case, not using this type of jitter for SSR
  1417. // Weights are generated using an exponential fit to Blackman-Harris 3.3
  1418. bool useYCoCg = false; // Only relevant for general case, not using it for SSR
  1419. float sharpness = 1.0f; // Make this a customizable parameter eventually
  1420. if(useYCoCg)
  1421. {
  1422. static const Vector2 sampleOffsets[] =
  1423. {
  1424. { 0.0f, -1.0f },
  1425. { -1.0f, 0.0f },
  1426. { 0.0f, 0.0f },
  1427. { 1.0f, 0.0f },
  1428. { 0.0f, 1.0f },
  1429. };
  1430. for (UINT32 i = 0; i < 5; ++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. }
  1438. for (UINT32 i = 5; i < 9; ++i)
  1439. sampleWeights[i] = 0.0f;
  1440. memset(sampleWeightsLowPass, 0, sizeof(sampleWeightsLowPass));
  1441. totalWeightsLowPass = 1.0f;
  1442. }
  1443. else
  1444. {
  1445. static const Vector2 sampleOffsets[] =
  1446. {
  1447. { -1.0f, -1.0f },
  1448. { 0.0f, -1.0f },
  1449. { 1.0f, -1.0f },
  1450. { -1.0f, 0.0f },
  1451. { 0.0f, 0.0f },
  1452. { 1.0f, 0.0f },
  1453. { -1.0f, 1.0f },
  1454. { 0.0f, 1.0f },
  1455. { 1.0f, 1.0f },
  1456. };
  1457. for (UINT32 i = 0; i < 9; ++i)
  1458. {
  1459. // Get rid of jitter introduced by the projection matrix
  1460. Vector2 offset = sampleOffsets[i] - jitter;
  1461. offset *= 1.0f + sharpness * 0.5f;
  1462. sampleWeights[i] = exp(-2.29f * offset.dot(offset));
  1463. totalWeights += sampleWeights[i];
  1464. // Low pass
  1465. offset *= 0.25f;
  1466. sampleWeightsLowPass[i] = exp(-2.29f * offset.dot(offset));
  1467. totalWeightsLowPass += sampleWeightsLowPass[i];
  1468. }
  1469. }
  1470. for (UINT32 i = 0; i < 9; ++i)
  1471. {
  1472. gTemporalResolveParamDef.gSampleWeights.set(mTemporalParamBuffer, sampleWeights[i] / totalWeights, i);
  1473. gTemporalResolveParamDef.gSampleWeightsLowpass.set(mTemporalParamBuffer, sampleWeightsLowPass[i] / totalWeightsLowPass, i);
  1474. }
  1475. SPtr<GpuParamBlockBuffer> perView = view.getPerViewBuffer();
  1476. mParamsSet->getGpuParams()->setParamBlockBuffer("PerCamera", perView);
  1477. RenderAPI& rapi = RenderAPI::instance();
  1478. rapi.setRenderTarget(destination);
  1479. const RendererViewProperties& viewProps = view.getProperties();
  1480. const Rect2I& viewRect = viewProps.viewRect;
  1481. gRendererUtility().setPass(mMaterial);
  1482. gRendererUtility().setPassParams(mParamsSet);
  1483. if(viewProps.numSamples > 1)
  1484. gRendererUtility().drawScreenQuad(Rect2(0.0f, 0.0f, (float)viewRect.width, (float)viewRect.height));
  1485. else
  1486. gRendererUtility().drawScreenQuad();
  1487. }
  1488. SSRResolveMat* SSRResolveMat::getVariation(bool msaa)
  1489. {
  1490. if (msaa)
  1491. return get(VAR_MSAA);
  1492. else
  1493. return get(VAR_NoMSAA);
  1494. }
  1495. EncodeDepthParamDef gEncodeDepthParamDef;
  1496. EncodeDepthMat::EncodeDepthMat()
  1497. {
  1498. mParamBuffer = gEncodeDepthParamDef.createBuffer();
  1499. SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
  1500. gpuParams->setParamBlockBuffer("Params", mParamBuffer);
  1501. gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gInputTex", mInputTexture);
  1502. SAMPLER_STATE_DESC sampDesc;
  1503. sampDesc.minFilter = FO_POINT;
  1504. sampDesc.magFilter = FO_POINT;
  1505. sampDesc.mipFilter = FO_POINT;
  1506. sampDesc.addressMode.u = TAM_CLAMP;
  1507. sampDesc.addressMode.v = TAM_CLAMP;
  1508. sampDesc.addressMode.w = TAM_CLAMP;
  1509. SPtr<SamplerState> samplerState = SamplerState::create(sampDesc);
  1510. setSamplerState(gpuParams, GPT_FRAGMENT_PROGRAM, "gInputSamp", "gInputTex", samplerState);
  1511. }
  1512. void EncodeDepthMat::_initVariations(ShaderVariations& variations)
  1513. {
  1514. // Do nothing
  1515. }
  1516. void EncodeDepthMat::execute(const SPtr<Texture>& depth, float near, float far, const SPtr<RenderTarget>& output)
  1517. {
  1518. mInputTexture.set(depth);
  1519. gEncodeDepthParamDef.gNear.set(mParamBuffer, near);
  1520. gEncodeDepthParamDef.gFar.set(mParamBuffer, far);
  1521. RenderAPI& rapi = RenderAPI::instance();
  1522. rapi.setRenderTarget(output, 0, RT_COLOR0);
  1523. gRendererUtility().setPass(mMaterial);
  1524. gRendererUtility().setPassParams(mParamsSet);
  1525. gRendererUtility().drawScreenQuad();
  1526. }
  1527. ShaderVariation MSAACoverageMat::VAR_2x = ShaderVariation({
  1528. ShaderVariation::Param("MSAA_COUNT", 2)
  1529. });
  1530. ShaderVariation MSAACoverageMat::VAR_4x = ShaderVariation({
  1531. ShaderVariation::Param("MSAA_COUNT", 4)
  1532. });
  1533. ShaderVariation MSAACoverageMat::VAR_8x = ShaderVariation({
  1534. ShaderVariation::Param("MSAA_COUNT", 8)
  1535. });
  1536. MSAACoverageMat::MSAACoverageMat()
  1537. :mGBufferParams(mMaterial, mParamsSet)
  1538. { }
  1539. void MSAACoverageMat::_initVariations(ShaderVariations& variations)
  1540. {
  1541. variations.add(VAR_2x);
  1542. variations.add(VAR_4x);
  1543. variations.add(VAR_8x);
  1544. }
  1545. void MSAACoverageMat::execute(const RendererView& view, GBufferTextures gbuffer)
  1546. {
  1547. mGBufferParams.bind(gbuffer);
  1548. const Rect2I& viewRect = view.getProperties().viewRect;
  1549. SPtr<GpuParamBlockBuffer> perView = view.getPerViewBuffer();
  1550. mParamsSet->getGpuParams()->setParamBlockBuffer("PerCamera", perView);
  1551. gRendererUtility().setPass(mMaterial);
  1552. gRendererUtility().setPassParams(mParamsSet);
  1553. gRendererUtility().drawScreenQuad(Rect2(0, 0, (float)viewRect.width, (float)viewRect.height));
  1554. }
  1555. MSAACoverageMat* MSAACoverageMat::getVariation(UINT32 msaaCount)
  1556. {
  1557. switch(msaaCount)
  1558. {
  1559. case 2:
  1560. return get(VAR_2x);
  1561. case 4:
  1562. return get(VAR_4x);
  1563. case 8:
  1564. default:
  1565. return get(VAR_8x);
  1566. }
  1567. }
  1568. MSAACoverageStencilMat::MSAACoverageStencilMat()
  1569. {
  1570. SPtr<GpuParams> params = mParamsSet->getGpuParams();
  1571. params->getTextureParam(GPT_FRAGMENT_PROGRAM, "gMSAACoverage", mCoverageTexParam);
  1572. }
  1573. void MSAACoverageStencilMat::_initVariations(ShaderVariations& variations)
  1574. {
  1575. // Do nothing
  1576. }
  1577. void MSAACoverageStencilMat::execute(const RendererView& view, const SPtr<Texture>& coverage)
  1578. {
  1579. const Rect2I& viewRect = view.getProperties().viewRect;
  1580. mCoverageTexParam.set(coverage);
  1581. gRendererUtility().setPass(mMaterial);
  1582. gRendererUtility().setPassParams(mParamsSet);
  1583. gRendererUtility().drawScreenQuad(Rect2(0, 0, (float)viewRect.width, (float)viewRect.height));
  1584. }
  1585. }}