| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988 |
- //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
- //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
- #include "BsPostProcessing.h"
- #include "RenderAPI/BsRenderTexture.h"
- #include "BsGpuResourcePool.h"
- #include "Renderer/BsRendererUtility.h"
- #include "Renderer/BsCamera.h"
- #include "Material/BsGpuParamsSet.h"
- #include "BsRendererView.h"
- #include "Image/BsPixelUtil.h"
- #include "Utility/BsBitwise.h"
- #include "BsRenderBeast.h"
- namespace bs { namespace ct
- {
- void setSamplerState(const SPtr<GpuParams>& params, GpuProgramType gpType, const String& name,
- const String& secondaryName, const SPtr<SamplerState>& samplerState, bool optional = false)
- {
- if (params->hasSamplerState(gpType, name))
- params->setSamplerState(gpType, name, samplerState);
- else
- {
- if(optional)
- {
- if (params->hasSamplerState(gpType, secondaryName))
- params->setSamplerState(gpType, secondaryName, samplerState);
- }
- else
- params->setSamplerState(gpType, secondaryName, samplerState);
- }
- }
- DownsampleParamDef gDownsampleParamDef;
- ShaderVariation DownsampleMat::VAR_LowQuality_NoMSAA = ShaderVariation({
- ShaderVariation::Param("QUALITY", 0),
- ShaderVariation::Param("MSAA", 0)
- });
- ShaderVariation DownsampleMat::VAR_LowQuality_MSAA = ShaderVariation({
- ShaderVariation::Param("QUALITY", 0),
- ShaderVariation::Param("MSAA", 1)
- });
- ShaderVariation DownsampleMat::VAR_HighQuality_NoMSAA = ShaderVariation({
- ShaderVariation::Param("QUALITY", 1),
- ShaderVariation::Param("MSAA", 0)
- });
- ShaderVariation DownsampleMat::VAR_HighQuality_MSAA = ShaderVariation({
- ShaderVariation::Param("QUALITY", 1),
- ShaderVariation::Param("MSAA", 1)
- });
- DownsampleMat::DownsampleMat()
- {
- mParamBuffer = gDownsampleParamDef.createBuffer();
- SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
- if(gpuParams->hasParamBlock(GPT_FRAGMENT_PROGRAM, "Input"))
- gpuParams->setParamBlockBuffer("Input", mParamBuffer);
- mParamsSet->getGpuParams()->getTextureParam(GPT_FRAGMENT_PROGRAM, "gInputTex", mInputTexture);
- }
- void DownsampleMat::_initVariations(ShaderVariations& variations)
- {
- variations.add(VAR_LowQuality_NoMSAA);
- variations.add(VAR_LowQuality_MSAA);
- variations.add(VAR_HighQuality_NoMSAA);
- variations.add(VAR_HighQuality_MSAA);
- }
- void DownsampleMat::execute(const SPtr<Texture>& input, const SPtr<RenderTarget>& output)
- {
- // Set parameters
- mInputTexture.set(input);
- const TextureProperties& rtProps = input->getProperties();
- bool MSAA = mVariation.getInt("MSAA") > 0;
- if(MSAA)
- {
- gDownsampleParamDef.gOffsets.set(mParamBuffer, Vector2(-1.0f, -1.0f));
- gDownsampleParamDef.gOffsets.set(mParamBuffer, Vector2(1.0f, -1.0f));
- gDownsampleParamDef.gOffsets.set(mParamBuffer, Vector2(-1.0f, 1.0f));
- gDownsampleParamDef.gOffsets.set(mParamBuffer, Vector2(1.0f, 1.0f));
- }
- else
- {
- Vector2 invTextureSize(1.0f / rtProps.getWidth(), 1.0f / rtProps.getHeight());
- gDownsampleParamDef.gOffsets.set(mParamBuffer, invTextureSize * Vector2(-1.0f, -1.0f));
- gDownsampleParamDef.gOffsets.set(mParamBuffer, invTextureSize * Vector2(1.0f, -1.0f));
- gDownsampleParamDef.gOffsets.set(mParamBuffer, invTextureSize * Vector2(-1.0f, 1.0f));
- gDownsampleParamDef.gOffsets.set(mParamBuffer, invTextureSize * Vector2(1.0f, 1.0f));
- }
- RenderAPI& rapi = RenderAPI::instance();
- rapi.setRenderTarget(output, FBT_DEPTH | FBT_STENCIL);
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- if (MSAA)
- gRendererUtility().drawScreenQuad(Rect2(0.0f, 0.0f, (float)rtProps.getWidth(), (float)rtProps.getHeight()));
- else
- gRendererUtility().drawScreenQuad();
- rapi.setRenderTarget(nullptr);
- }
- POOLED_RENDER_TEXTURE_DESC DownsampleMat::getOutputDesc(const SPtr<Texture>& target)
- {
- const TextureProperties& rtProps = target->getProperties();
-
- UINT32 width = std::max(1, Math::ceilToInt(rtProps.getWidth() * 0.5f));
- UINT32 height = std::max(1, Math::ceilToInt(rtProps.getHeight() * 0.5f));
- return POOLED_RENDER_TEXTURE_DESC::create2D(rtProps.getFormat(), width, height, TU_RENDERTARGET);
- }
- DownsampleMat* DownsampleMat::getVariation(UINT32 quality, bool msaa)
- {
- if(quality == 0)
- {
- if (msaa)
- return get(VAR_LowQuality_MSAA);
- else
- return get(VAR_LowQuality_NoMSAA);
- }
- else
- {
- if (msaa)
- return get(VAR_HighQuality_MSAA);
- else
- return get(VAR_HighQuality_NoMSAA);
- }
- }
- EyeAdaptHistogramParamDef gEyeAdaptHistogramParamDef;
- EyeAdaptHistogramMat::EyeAdaptHistogramMat()
- {
- mParamBuffer = gEyeAdaptHistogramParamDef.createBuffer();
- SPtr<GpuParams> params = mParamsSet->getGpuParams();
- params->setParamBlockBuffer("Input", mParamBuffer);
- params->getTextureParam(GPT_COMPUTE_PROGRAM, "gSceneColorTex", mSceneColor);
- params->getLoadStoreTextureParam(GPT_COMPUTE_PROGRAM, "gOutputTex", mOutputTex);
- }
- void EyeAdaptHistogramMat::_initVariations(ShaderVariations& variations)
- {
- ShaderVariation variation({
- ShaderVariation::Param("THREADGROUP_SIZE_X", THREAD_GROUP_SIZE_X),
- ShaderVariation::Param("THREADGROUP_SIZE_Y", THREAD_GROUP_SIZE_Y),
- ShaderVariation::Param("LOOP_COUNT_X", LOOP_COUNT_X),
- ShaderVariation::Param("LOOP_COUNT_Y", LOOP_COUNT_Y),
- });
- variations.add(variation);
- }
- void EyeAdaptHistogramMat::execute(const SPtr<Texture>& input, const SPtr<Texture>& output,
- const AutoExposureSettings& settings)
- {
- // Set parameters
- mSceneColor.set(input);
- const TextureProperties& props = input->getProperties();
- Vector4I offsetAndSize(0, 0, (INT32)props.getWidth(), (INT32)props.getHeight());
- gEyeAdaptHistogramParamDef.gHistogramParams.set(mParamBuffer, getHistogramScaleOffset(settings));
- gEyeAdaptHistogramParamDef.gPixelOffsetAndSize.set(mParamBuffer, offsetAndSize);
- Vector2I threadGroupCount = getThreadGroupCount(input);
- gEyeAdaptHistogramParamDef.gThreadGroupCount.set(mParamBuffer, threadGroupCount);
- // Dispatch
- mOutputTex.set(output);
- RenderAPI& rapi = RenderAPI::instance();
- gRendererUtility().setComputePass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- rapi.dispatchCompute(threadGroupCount.x, threadGroupCount.y);
- }
- POOLED_RENDER_TEXTURE_DESC EyeAdaptHistogramMat::getOutputDesc(const SPtr<Texture>& target)
- {
- Vector2I threadGroupCount = getThreadGroupCount(target);
- UINT32 numHistograms = threadGroupCount.x * threadGroupCount.y;
- return POOLED_RENDER_TEXTURE_DESC::create2D(PF_RGBA16F, HISTOGRAM_NUM_TEXELS, numHistograms,
- TU_LOADSTORE);
- }
- Vector2I EyeAdaptHistogramMat::getThreadGroupCount(const SPtr<Texture>& target)
- {
- const UINT32 texelsPerThreadGroupX = THREAD_GROUP_SIZE_X * LOOP_COUNT_X;
- const UINT32 texelsPerThreadGroupY = THREAD_GROUP_SIZE_Y * LOOP_COUNT_Y;
- const TextureProperties& props = target->getProperties();
-
- Vector2I threadGroupCount;
- threadGroupCount.x = ((INT32)props.getWidth() + texelsPerThreadGroupX - 1) / texelsPerThreadGroupX;
- threadGroupCount.y = ((INT32)props.getHeight() + texelsPerThreadGroupY - 1) / texelsPerThreadGroupY;
- return threadGroupCount;
- }
- Vector2 EyeAdaptHistogramMat::getHistogramScaleOffset(const AutoExposureSettings& settings)
- {
- float diff = settings.histogramLog2Max - settings.histogramLog2Min;
- float scale = 1.0f / diff;
- float offset = -settings.histogramLog2Min * scale;
- return Vector2(scale, offset);
- }
- EyeAdaptHistogramReduceParamDef gEyeAdaptHistogramReduceParamDef;
- EyeAdaptHistogramReduceMat::EyeAdaptHistogramReduceMat()
- {
- mParamBuffer = gEyeAdaptHistogramReduceParamDef.createBuffer();
- SPtr<GpuParams> params = mParamsSet->getGpuParams();
- params->setParamBlockBuffer("Input", mParamBuffer);
- params->getTextureParam(GPT_FRAGMENT_PROGRAM, "gHistogramTex", mHistogramTex);
- params->getTextureParam(GPT_FRAGMENT_PROGRAM, "gEyeAdaptationTex", mEyeAdaptationTex);
- }
- void EyeAdaptHistogramReduceMat::_initVariations(ShaderVariations& variations)
- {
- // Do nothing
- }
- void EyeAdaptHistogramReduceMat::execute(const SPtr<Texture>& sceneColor, const SPtr<Texture>& histogram,
- const SPtr<Texture>& prevFrame, const SPtr<RenderTarget>& output)
- {
- // Set parameters
- mHistogramTex.set(histogram);
- SPtr<Texture> eyeAdaptationTex;
- if (prevFrame == nullptr) // Could be that this is the first run
- eyeAdaptationTex = Texture::WHITE;
- else
- eyeAdaptationTex = prevFrame;
- mEyeAdaptationTex.set(eyeAdaptationTex);
- Vector2I threadGroupCount = EyeAdaptHistogramMat::getThreadGroupCount(sceneColor);
- UINT32 numHistograms = threadGroupCount.x * threadGroupCount.y;
- gEyeAdaptHistogramReduceParamDef.gThreadGroupCount.set(mParamBuffer, numHistograms);
- RenderAPI& rapi = RenderAPI::instance();
- rapi.setRenderTarget(output, FBT_DEPTH | FBT_STENCIL);
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- Rect2 drawUV(0.0f, 0.0f, (float)EyeAdaptHistogramMat::HISTOGRAM_NUM_TEXELS, 2.0f);
- gRendererUtility().drawScreenQuad(drawUV);
- rapi.setRenderTarget(nullptr);
- }
- POOLED_RENDER_TEXTURE_DESC EyeAdaptHistogramReduceMat::getOutputDesc()
- {
- return POOLED_RENDER_TEXTURE_DESC::create2D(PF_RGBA16F, EyeAdaptHistogramMat::HISTOGRAM_NUM_TEXELS, 2,
- TU_RENDERTARGET);
- }
- EyeAdaptationParamDef gEyeAdaptationParamDef;
- EyeAdaptationMat::EyeAdaptationMat()
- {
- mParamBuffer = gEyeAdaptationParamDef.createBuffer();
- SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
- gpuParams->setParamBlockBuffer("Input", mParamBuffer);
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gHistogramTex", mReducedHistogramTex);
- }
- void EyeAdaptationMat::_initVariations(ShaderVariations& variations)
- {
- ShaderVariation variation({
- ShaderVariation::Param("THREADGROUP_SIZE_X", EyeAdaptHistogramMat::THREAD_GROUP_SIZE_X),
- ShaderVariation::Param("THREADGROUP_SIZE_Y", EyeAdaptHistogramMat::THREAD_GROUP_SIZE_Y)
- });
- variations.add(variation);
- }
- void EyeAdaptationMat::execute(const SPtr<Texture>& reducedHistogram, const SPtr<RenderTarget>& output,
- float frameDelta, const AutoExposureSettings& settings, float exposureScale)
- {
- // Set parameters
- mReducedHistogramTex.set(reducedHistogram);
- Vector2 histogramScaleAndOffset = EyeAdaptHistogramMat::getHistogramScaleOffset(settings);
- Vector4 eyeAdaptationParams[3];
- eyeAdaptationParams[0].x = histogramScaleAndOffset.x;
- eyeAdaptationParams[0].y = histogramScaleAndOffset.y;
- float histogramPctHigh = Math::clamp01(settings.histogramPctHigh);
- eyeAdaptationParams[0].z = std::min(Math::clamp01(settings.histogramPctLow), histogramPctHigh);
- eyeAdaptationParams[0].w = histogramPctHigh;
- eyeAdaptationParams[1].x = std::min(settings.minEyeAdaptation, settings.maxEyeAdaptation);
- eyeAdaptationParams[1].y = settings.maxEyeAdaptation;
- eyeAdaptationParams[1].z = settings.eyeAdaptationSpeedUp;
- eyeAdaptationParams[1].w = settings.eyeAdaptationSpeedDown;
- eyeAdaptationParams[2].x = Math::pow(2.0f, exposureScale);
- eyeAdaptationParams[2].y = frameDelta;
- eyeAdaptationParams[2].z = 0.0f; // Unused
- eyeAdaptationParams[2].w = 0.0f; // Unused
- gEyeAdaptationParamDef.gEyeAdaptationParams.set(mParamBuffer, eyeAdaptationParams[0], 0);
- gEyeAdaptationParamDef.gEyeAdaptationParams.set(mParamBuffer, eyeAdaptationParams[1], 1);
- gEyeAdaptationParamDef.gEyeAdaptationParams.set(mParamBuffer, eyeAdaptationParams[2], 2);
- // Render
- RenderAPI& rapi = RenderAPI::instance();
- rapi.setRenderTarget(output, FBT_DEPTH | FBT_STENCIL);
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- gRendererUtility().drawScreenQuad();
- rapi.setRenderTarget(nullptr);
- }
- POOLED_RENDER_TEXTURE_DESC EyeAdaptationMat::getOutputDesc()
- {
- return POOLED_RENDER_TEXTURE_DESC::create2D(PF_R32F, 1, 1, TU_RENDERTARGET);
- }
- CreateTonemapLUTParamDef gCreateTonemapLUTParamDef;
- WhiteBalanceParamDef gWhiteBalanceParamDef;
- CreateTonemapLUTMat::CreateTonemapLUTMat()
- {
- mParamBuffer = gCreateTonemapLUTParamDef.createBuffer();
- mWhiteBalanceParamBuffer = gWhiteBalanceParamDef.createBuffer();
- SPtr<GpuParams> params = mParamsSet->getGpuParams();
- params->setParamBlockBuffer("Input", mParamBuffer);
- params->setParamBlockBuffer("WhiteBalanceInput", mWhiteBalanceParamBuffer);
- params->getLoadStoreTextureParam(GPT_COMPUTE_PROGRAM, "gOutputTex", mOutputTex);
- }
- void CreateTonemapLUTMat::_initVariations(ShaderVariations& variations)
- {
- ShaderVariation variation({
- ShaderVariation::Param("LUT_SIZE", LUT_SIZE)
- });
- variations.add(variation);
- }
- void CreateTonemapLUTMat::execute(const SPtr<Texture>& output, const RenderSettings& settings)
- {
- // Set parameters
- gCreateTonemapLUTParamDef.gGammaAdjustment.set(mParamBuffer, 2.2f / settings.gamma);
- // Note: Assuming sRGB (PC monitor) for now, change to Rec.709 when running on console (value 1), or to raw 2.2
- // gamma when running on Mac (value 2)
- gCreateTonemapLUTParamDef.gGammaCorrectionType.set(mParamBuffer, 0);
- Vector4 tonemapParams[2];
- tonemapParams[0].x = settings.tonemapping.filmicCurveShoulderStrength;
- tonemapParams[0].y = settings.tonemapping.filmicCurveLinearStrength;
- tonemapParams[0].z = settings.tonemapping.filmicCurveLinearAngle;
- tonemapParams[0].w = settings.tonemapping.filmicCurveToeStrength;
- tonemapParams[1].x = settings.tonemapping.filmicCurveToeNumerator;
- tonemapParams[1].y = settings.tonemapping.filmicCurveToeDenominator;
- tonemapParams[1].z = settings.tonemapping.filmicCurveLinearWhitePoint;
- tonemapParams[1].w = 0.0f; // Unused
- gCreateTonemapLUTParamDef.gTonemapParams.set(mParamBuffer, tonemapParams[0], 0);
- gCreateTonemapLUTParamDef.gTonemapParams.set(mParamBuffer, tonemapParams[1], 1);
- // Set color grading params
- gCreateTonemapLUTParamDef.gSaturation.set(mParamBuffer, settings.colorGrading.saturation);
- gCreateTonemapLUTParamDef.gContrast.set(mParamBuffer, settings.colorGrading.contrast);
- gCreateTonemapLUTParamDef.gGain.set(mParamBuffer, settings.colorGrading.gain);
- gCreateTonemapLUTParamDef.gOffset.set(mParamBuffer, settings.colorGrading.offset);
- // Set white balance params
- gWhiteBalanceParamDef.gWhiteTemp.set(mWhiteBalanceParamBuffer, settings.whiteBalance.temperature);
- gWhiteBalanceParamDef.gWhiteOffset.set(mWhiteBalanceParamBuffer, settings.whiteBalance.tint);
- // Dispatch
- mOutputTex.set(output);
- RenderAPI& rapi = RenderAPI::instance();
-
- gRendererUtility().setComputePass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- rapi.dispatchCompute(LUT_SIZE / 8, LUT_SIZE / 8, LUT_SIZE);
- }
- POOLED_RENDER_TEXTURE_DESC CreateTonemapLUTMat::getOutputDesc()
- {
- return POOLED_RENDER_TEXTURE_DESC::create3D(PF_RGBA8, LUT_SIZE, LUT_SIZE, LUT_SIZE, TU_LOADSTORE);
- }
- TonemappingParamDef gTonemappingParamDef;
- ShaderVariation TonemappingMat::VAR_Gamma_AutoExposure_MSAA = ShaderVariation({
- ShaderVariation::Param("GAMMA_ONLY", true),
- ShaderVariation::Param("AUTO_EXPOSURE", true),
- ShaderVariation::Param("MSAA", true),
- ShaderVariation::Param("LUT_SIZE", CreateTonemapLUTMat::LUT_SIZE),
- });
- ShaderVariation TonemappingMat::VAR_Gamma_AutoExposure_NoMSAA = ShaderVariation({
- ShaderVariation::Param("GAMMA_ONLY", true),
- ShaderVariation::Param("AUTO_EXPOSURE", true),
- ShaderVariation::Param("MSAA", false),
- ShaderVariation::Param("LUT_SIZE", CreateTonemapLUTMat::LUT_SIZE),
- });
- ShaderVariation TonemappingMat::VAR_Gamma_NoAutoExposure_MSAA = ShaderVariation({
- ShaderVariation::Param("GAMMA_ONLY", true),
- ShaderVariation::Param("AUTO_EXPOSURE", false),
- ShaderVariation::Param("MSAA", true),
- ShaderVariation::Param("LUT_SIZE", CreateTonemapLUTMat::LUT_SIZE),
- });
- ShaderVariation TonemappingMat::VAR_Gamma_NoAutoExposure_NoMSAA = ShaderVariation({
- ShaderVariation::Param("GAMMA_ONLY", true),
- ShaderVariation::Param("AUTO_EXPOSURE", false),
- ShaderVariation::Param("MSAA", false),
- ShaderVariation::Param("LUT_SIZE", CreateTonemapLUTMat::LUT_SIZE),
- });
- ShaderVariation TonemappingMat::VAR_NoGamma_AutoExposure_MSAA = ShaderVariation({
- ShaderVariation::Param("GAMMA_ONLY", false),
- ShaderVariation::Param("AUTO_EXPOSURE", true),
- ShaderVariation::Param("MSAA", true),
- ShaderVariation::Param("LUT_SIZE", CreateTonemapLUTMat::LUT_SIZE),
- });
- ShaderVariation TonemappingMat::VAR_NoGamma_AutoExposure_NoMSAA = ShaderVariation({
- ShaderVariation::Param("GAMMA_ONLY", false),
- ShaderVariation::Param("AUTO_EXPOSURE", true),
- ShaderVariation::Param("MSAA", false),
- ShaderVariation::Param("LUT_SIZE", CreateTonemapLUTMat::LUT_SIZE),
- });
- ShaderVariation TonemappingMat::VAR_NoGamma_NoAutoExposure_MSAA = ShaderVariation({
- ShaderVariation::Param("GAMMA_ONLY", false),
- ShaderVariation::Param("AUTO_EXPOSURE", false),
- ShaderVariation::Param("MSAA", true),
- ShaderVariation::Param("LUT_SIZE", CreateTonemapLUTMat::LUT_SIZE),
- });
- ShaderVariation TonemappingMat::VAR_NoGamma_NoAutoExposure_NoMSAA = ShaderVariation({
- ShaderVariation::Param("GAMMA_ONLY", false),
- ShaderVariation::Param("AUTO_EXPOSURE", false),
- ShaderVariation::Param("MSAA", false),
- ShaderVariation::Param("LUT_SIZE", CreateTonemapLUTMat::LUT_SIZE),
- });
- TonemappingMat::TonemappingMat()
- {
- mParamBuffer = gTonemappingParamDef.createBuffer();
- SPtr<GpuParams> params = mParamsSet->getGpuParams();
- params->setParamBlockBuffer("Input", mParamBuffer);
- params->getTextureParam(GPT_VERTEX_PROGRAM, "gEyeAdaptationTex", mEyeAdaptationTex);
- params->getTextureParam(GPT_FRAGMENT_PROGRAM, "gInputTex", mInputTex);
- if(!mVariation.getBool("GAMMA_ONLY"))
- params->getTextureParam(GPT_FRAGMENT_PROGRAM, "gColorLUT", mColorLUT);
- }
- void TonemappingMat::_initVariations(ShaderVariations& variations)
- {
- variations.add(VAR_Gamma_AutoExposure_MSAA);
- variations.add(VAR_Gamma_AutoExposure_NoMSAA);
- variations.add(VAR_Gamma_NoAutoExposure_MSAA);
- variations.add(VAR_Gamma_NoAutoExposure_NoMSAA);
- variations.add(VAR_NoGamma_AutoExposure_MSAA);
- variations.add(VAR_NoGamma_AutoExposure_NoMSAA);
- variations.add(VAR_NoGamma_NoAutoExposure_MSAA);
- variations.add(VAR_NoGamma_NoAutoExposure_NoMSAA);
- }
- void TonemappingMat::execute(const SPtr<Texture>& sceneColor, const SPtr<Texture>& eyeAdaptation,
- const SPtr<Texture>& colorLUT, const SPtr<RenderTarget>& output, const RenderSettings& settings)
- {
- const TextureProperties& texProps = sceneColor->getProperties();
- gTonemappingParamDef.gRawGamma.set(mParamBuffer, 1.0f / settings.gamma);
- gTonemappingParamDef.gManualExposureScale.set(mParamBuffer, Math::pow(2.0f, settings.exposureScale));
- gTonemappingParamDef.gNumSamples.set(mParamBuffer, texProps.getNumSamples());
- // Set parameters
- mInputTex.set(sceneColor);
- mColorLUT.set(colorLUT);
- mEyeAdaptationTex.set(eyeAdaptation);
- // Render
- RenderAPI& rapi = RenderAPI::instance();
- rapi.setRenderTarget(output);
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- if (mVariation.getBool("MSAA"))
- gRendererUtility().drawScreenQuad(Rect2(0.0f, 0.0f, (float)texProps.getWidth(), (float)texProps.getHeight()));
- else
- gRendererUtility().drawScreenQuad();
- }
- TonemappingMat* TonemappingMat::getVariation(bool gammaOnly, bool autoExposure, bool MSAA)
- {
- if (gammaOnly)
- {
- if (autoExposure)
- {
- if (MSAA)
- return get(VAR_Gamma_AutoExposure_MSAA);
- else
- return get(VAR_Gamma_AutoExposure_NoMSAA);
- }
- else
- {
- if (MSAA)
- return get(VAR_Gamma_NoAutoExposure_MSAA);
- else
- return get(VAR_Gamma_NoAutoExposure_NoMSAA);
- }
- }
- else
- {
- if (autoExposure)
- {
- if (MSAA)
- return get(VAR_NoGamma_AutoExposure_MSAA);
- else
- return get(VAR_NoGamma_AutoExposure_NoMSAA);
- }
- else
- {
- if (MSAA)
- return get(VAR_NoGamma_NoAutoExposure_MSAA);
- else
- return get(VAR_NoGamma_NoAutoExposure_NoMSAA);
- }
- }
- }
- GaussianBlurParamDef gGaussianBlurParamDef;
- GaussianBlurMat::GaussianBlurMat()
- {
- mParamBuffer = gGaussianBlurParamDef.createBuffer();
- SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
- gpuParams->setParamBlockBuffer("Input", mParamBuffer);
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gInputTex", mInputTexture);
- }
- void GaussianBlurMat::_initVariations(ShaderVariations& variations)
- {
- ShaderVariation variation({
- ShaderVariation::Param("MAX_NUM_SAMPLES", MAX_BLUR_SAMPLES)
- });
- variations.add(variation);
- }
- void GaussianBlurMat::execute(const SPtr<Texture>& source, float filterSize, const SPtr<RenderTexture>& destination)
- {
- const TextureProperties& srcProps = source->getProperties();
- const RenderTextureProperties& dstProps = destination->getProperties();
- Vector2 invTexSize(1.0f / srcProps.getWidth(), 1.0f / srcProps.getHeight());
- std::array<float, MAX_BLUR_SAMPLES> sampleOffsets;
- std::array<float, MAX_BLUR_SAMPLES> sampleWeights;
- POOLED_RENDER_TEXTURE_DESC tempTextureDesc = POOLED_RENDER_TEXTURE_DESC::create2D(srcProps.getFormat(),
- dstProps.width, dstProps.height, TU_RENDERTARGET);
- SPtr<PooledRenderTexture> tempTexture = GpuResourcePool::instance().get(tempTextureDesc);
- auto updateParamBuffer = [&](Direction direction)
- {
- float kernelRadius = calcKernelRadius(source, filterSize, direction);
- UINT32 numSamples = calcStdDistribution(kernelRadius, sampleWeights, sampleOffsets);
- for(UINT32 i = 0; i < (numSamples + 3) / 4; ++i)
- {
- UINT32 remainder = std::min(4U, numSamples - i * 4);
- Vector4 weights;
- for (UINT32 j = 0; j < remainder; ++j)
- weights[j] = sampleWeights[i * 4 + j];
- gGaussianBlurParamDef.gSampleWeights.set(mParamBuffer, weights, i);
- }
- UINT32 axis0 = direction == DirHorizontal ? 0 : 1;
- UINT32 axis1 = (axis0 + 1) % 2;
- for(UINT32 i = 0; i < (numSamples + 1) / 2; ++i)
- {
- UINT32 remainder = std::min(2U, numSamples - i * 2);
- Vector4 offset;
- offset[axis0] = sampleOffsets[i * 2 + 0] * invTexSize[axis0];
- offset[axis1] = 0.0f;
- if(remainder == 2)
- {
- offset[axis0 + 2] = sampleOffsets[i * 2 + 1] * invTexSize[axis0];
- offset[axis1 + 2] = 0.0f;
- }
- else
- {
- offset[axis0 + 2] = 0.0f;
- offset[axis1 + 2] = 0.0f;
- }
- gGaussianBlurParamDef.gSampleOffsets.set(mParamBuffer, offset, i);
- }
- gGaussianBlurParamDef.gNumSamples.set(mParamBuffer, numSamples);
- };
- // Horizontal pass
- {
- updateParamBuffer(DirHorizontal);
- mInputTexture.set(source);
- RenderAPI& rapi = RenderAPI::instance();
- rapi.setRenderTarget(tempTexture->renderTexture);
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- gRendererUtility().drawScreenQuad();
- }
- // Vertical pass
- {
- updateParamBuffer(DirVertical);
- mInputTexture.set(tempTexture->texture);
- RenderAPI& rapi = RenderAPI::instance();
- rapi.setRenderTarget(destination);
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- gRendererUtility().drawScreenQuad();
- }
- GpuResourcePool::instance().release(tempTexture);
- }
- UINT32 GaussianBlurMat::calcStdDistribution(float filterRadius, std::array<float, MAX_BLUR_SAMPLES>& weights,
- std::array<float, MAX_BLUR_SAMPLES>& offsets)
- {
- filterRadius = Math::clamp(filterRadius, 0.00001f, (float)(MAX_BLUR_SAMPLES - 1));
- INT32 intFilterRadius = std::min(Math::ceilToInt(filterRadius), MAX_BLUR_SAMPLES - 1);
- auto normalDistribution = [](int i, float scale)
- {
- float samplePos = fabs((float)i) * scale;
- return exp(samplePos * samplePos);
- };
- // We make use of the hardware linear filtering, and therefore only generate half the number of samples.
- // The weights and the sampling location needs to be adjusted in order to get the same results as if we
- // perform two samples separately:
- //
- // Original formula is: t1*w1 + t2*w2
- // With hardware filtering it's: (t1 + (t2 - t1) * o) * w3
- // Or expanded: t1*w3 - t1*o*w3 + t2*o*w3 = t1 * (w3 - o*w3) + t2 * (o*w3)
- //
- // These two need to equal, which means this follows:
- // w1 = w3 - o*w3
- // w2 = o*w3
- //
- // From the second equation get the offset o:
- // o = w2/w3
- //
- // From the first equation and o, get w3:
- // w1 = w3 - w2
- // w3 = w1 + w2
- float scale = 1.0f / filterRadius;
- UINT32 numSamples = 0;
- float totalWeight = 0.0f;
- for(int i = -intFilterRadius; i < intFilterRadius; i += 2)
- {
- float w1 = normalDistribution(i, scale);
- float w2 = normalDistribution(i + 1, scale);
- float w3 = w1 + w2;
- float o = w2/w3; // Relative to first sample
- weights[numSamples] = w3;
- offsets[numSamples] = o;
- numSamples++;
- totalWeight += w3;
- }
- // Special case for last weight, as it doesn't have a matching pair
- float w = normalDistribution(intFilterRadius, scale);
- weights[numSamples] = w;
- offsets[numSamples] = 0.0f;
- numSamples++;
- totalWeight += w;
- // Normalize weights
- float invTotalWeight = 1.0f / totalWeight;
- for(UINT32 i = 0; i < numSamples; i++)
- weights[i] *= invTotalWeight;
- return numSamples;
- }
- float GaussianBlurMat::calcKernelRadius(const SPtr<Texture>& source, float scale, Direction filterDir)
- {
- scale = Math::clamp01(scale);
- UINT32 length;
- if (filterDir == DirHorizontal)
- length = source->getProperties().getWidth();
- else
- length = source->getProperties().getHeight();
- // Divide by two because we need the radius
- return std::min(length * scale / 2, (float)MAX_BLUR_SAMPLES - 1);
- }
- GaussianDOFParamDef gGaussianDOFParamDef;
- ShaderVariation GaussianDOFSeparateMat::VAR_Near_Far = ShaderVariation({
- ShaderVariation::Param("NEAR", true),
- ShaderVariation::Param("FAR", true),
- ShaderVariation::Param("NEAR_AND_FAR", true)
- });
- ShaderVariation GaussianDOFSeparateMat::VAR_NoNear_Far = ShaderVariation({
- ShaderVariation::Param("NEAR", false),
- ShaderVariation::Param("FAR", true),
- ShaderVariation::Param("NEAR_AND_FAR", false)
- });
- ShaderVariation GaussianDOFSeparateMat::VAR_Near_NoFar = ShaderVariation({
- ShaderVariation::Param("NEAR", true),
- ShaderVariation::Param("FAR", false),
- ShaderVariation::Param("NEAR_AND_FAR", false)
- });
- GaussianDOFSeparateMat::GaussianDOFSeparateMat()
- {
- mParamBuffer = gGaussianDOFParamDef.createBuffer();
- SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
- gpuParams->setParamBlockBuffer("Input", mParamBuffer);
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gColorTex", mColorTexture);
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gDepthTex", mDepthTexture);
- SAMPLER_STATE_DESC desc;
- desc.minFilter = FO_POINT;
- desc.magFilter = FO_POINT;
- desc.mipFilter = FO_POINT;
- desc.addressMode.u = TAM_CLAMP;
- desc.addressMode.v = TAM_CLAMP;
- desc.addressMode.w = TAM_CLAMP;
- SPtr<SamplerState> samplerState = SamplerState::create(desc);
- setSamplerState(gpuParams, GPT_FRAGMENT_PROGRAM, "gColorSamp", "gColorTex", samplerState);
- }
- void GaussianDOFSeparateMat::_initVariations(ShaderVariations& variations)
- {
- variations.add(VAR_Near_Far);
- variations.add(VAR_Near_NoFar);
- variations.add(VAR_NoNear_Far);
- }
- void GaussianDOFSeparateMat::execute(const SPtr<Texture>& color, const SPtr<Texture>& depth,
- const RendererView& view, const DepthOfFieldSettings& settings)
- {
- const TextureProperties& srcProps = color->getProperties();
- UINT32 outputWidth = std::max(1U, srcProps.getWidth() / 2);
- UINT32 outputHeight = std::max(1U, srcProps.getHeight() / 2);
- POOLED_RENDER_TEXTURE_DESC outputTexDesc = POOLED_RENDER_TEXTURE_DESC::create2D(srcProps.getFormat(),
- outputWidth, outputHeight, TU_RENDERTARGET);
- mOutput0 = GpuResourcePool::instance().get(outputTexDesc);
- bool near = mVariation.getBool("NEAR");
- bool far = mVariation.getBool("FAR");
- SPtr<RenderTexture> rt;
- if (near && far)
- {
- mOutput1 = GpuResourcePool::instance().get(outputTexDesc);
- RENDER_TEXTURE_DESC rtDesc;
- rtDesc.colorSurfaces[0].texture = mOutput0->texture;
- rtDesc.colorSurfaces[1].texture = mOutput1->texture;
- rt = RenderTexture::create(rtDesc);
- }
- else
- rt = mOutput0->renderTexture;
- Vector2 invTexSize(1.0f / srcProps.getWidth(), 1.0f / srcProps.getHeight());
- gGaussianDOFParamDef.gHalfPixelOffset.set(mParamBuffer, invTexSize * 0.5f);
- gGaussianDOFParamDef.gNearBlurPlane.set(mParamBuffer, settings.focalDistance - settings.focalRange * 0.5f);
- gGaussianDOFParamDef.gFarBlurPlane.set(mParamBuffer, settings.focalDistance + settings.focalRange * 0.5f);
- gGaussianDOFParamDef.gInvNearBlurRange.set(mParamBuffer, 1.0f / settings.nearTransitionRange);
- gGaussianDOFParamDef.gInvFarBlurRange.set(mParamBuffer, 1.0f / settings.farTransitionRange);
- mColorTexture.set(color);
- mDepthTexture.set(depth);
- SPtr<GpuParamBlockBuffer> perView = view.getPerViewBuffer();
- mParamsSet->getGpuParams()->setParamBlockBuffer("PerCamera", perView);
- RenderAPI& rapi = RenderAPI::instance();
- rapi.setRenderTarget(rt);
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- gRendererUtility().drawScreenQuad();
- }
- SPtr<PooledRenderTexture> GaussianDOFSeparateMat::getOutput(UINT32 idx)
- {
- if (idx == 0)
- return mOutput0;
- else if (idx == 1)
- return mOutput1;
- return nullptr;
- }
- void GaussianDOFSeparateMat::release()
- {
- if (mOutput0 != nullptr)
- GpuResourcePool::instance().release(mOutput0);
- if (mOutput1 != nullptr)
- GpuResourcePool::instance().release(mOutput1);
- }
- GaussianDOFSeparateMat* GaussianDOFSeparateMat::getVariation(bool near, bool far)
- {
- if (near)
- {
- if (far)
- return get(VAR_Near_Far);
- else
- return get(VAR_Near_NoFar);
- }
- else
- return get(VAR_NoNear_Far);
- }
- ShaderVariation GaussianDOFCombineMat::VAR_Near_Far = ShaderVariation({
- ShaderVariation::Param("NEAR", true),
- ShaderVariation::Param("FAR", true),
- ShaderVariation::Param("NEAR_AND_FAR", true)
- });
- ShaderVariation GaussianDOFCombineMat::VAR_NoNear_Far = ShaderVariation({
- ShaderVariation::Param("NEAR", false),
- ShaderVariation::Param("FAR", true),
- ShaderVariation::Param("NEAR_AND_FAR", false)
- });
- ShaderVariation GaussianDOFCombineMat::VAR_Near_NoFar = ShaderVariation({
- ShaderVariation::Param("NEAR", true),
- ShaderVariation::Param("FAR", false),
- ShaderVariation::Param("NEAR_AND_FAR", false)
- });
- GaussianDOFCombineMat::GaussianDOFCombineMat()
- {
- mParamBuffer = gGaussianDOFParamDef.createBuffer();
- SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
- gpuParams->setParamBlockBuffer("Input", mParamBuffer);
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gFocusedTex", mFocusedTexture);
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gDepthTex", mDepthTexture);
- if(gpuParams->hasTexture(GPT_FRAGMENT_PROGRAM, "gNearTex"))
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gNearTex", mNearTexture);
- if(gpuParams->hasTexture(GPT_FRAGMENT_PROGRAM, "gFarTex"))
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gFarTex", mFarTexture);
- }
- void GaussianDOFCombineMat::_initVariations(ShaderVariations& variations)
- {
- variations.add(VAR_Near_Far);
- variations.add(VAR_Near_NoFar);
- variations.add(VAR_NoNear_Far);
- }
- void GaussianDOFCombineMat::execute(const SPtr<Texture>& focused, const SPtr<Texture>& near,
- const SPtr<Texture>& far, const SPtr<Texture>& depth, const SPtr<RenderTarget>& output,
- const RendererView& view, const DepthOfFieldSettings& settings)
- {
- const TextureProperties& srcProps = focused->getProperties();
- Vector2 invTexSize(1.0f / srcProps.getWidth(), 1.0f / srcProps.getHeight());
- gGaussianDOFParamDef.gHalfPixelOffset.set(mParamBuffer, invTexSize * 0.5f);
- gGaussianDOFParamDef.gNearBlurPlane.set(mParamBuffer, settings.focalDistance - settings.focalRange * 0.5f);
- gGaussianDOFParamDef.gFarBlurPlane.set(mParamBuffer, settings.focalDistance + settings.focalRange * 0.5f);
- gGaussianDOFParamDef.gInvNearBlurRange.set(mParamBuffer, 1.0f / settings.nearTransitionRange);
- gGaussianDOFParamDef.gInvFarBlurRange.set(mParamBuffer, 1.0f / settings.farTransitionRange);
- mFocusedTexture.set(focused);
- mNearTexture.set(near);
- mFarTexture.set(far);
- mDepthTexture.set(depth);
- SPtr<GpuParamBlockBuffer> perView = view.getPerViewBuffer();
- mParamsSet->getGpuParams()->setParamBlockBuffer("PerCamera", perView);
- RenderAPI& rapi = RenderAPI::instance();
- rapi.setRenderTarget(output);
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- gRendererUtility().drawScreenQuad();
- }
- GaussianDOFCombineMat* GaussianDOFCombineMat::getVariation(bool near, bool far)
- {
- if (near)
- {
- if (far)
- return get(VAR_Near_Far);
- else
- return get(VAR_Near_NoFar);
- }
- else
- return get(VAR_NoNear_Far);
- }
- BuildHiZFParamDef gBuildHiZParamDef;
- BuildHiZMat::BuildHiZMat()
- {
- SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gDepthTex", mInputTexture);
- // If no texture view support, we must manually pick a valid mip level in the shader
- const RenderAPIInfo& rapiInfo = RenderAPI::instance().getAPIInfo();
- if(!rapiInfo.isFlagSet(RenderAPIFeatureFlag::TextureViews))
- {
- mParamBuffer = gBuildHiZParamDef.createBuffer();
- SAMPLER_STATE_DESC inputSampDesc;
- inputSampDesc.minFilter = FO_POINT;
- inputSampDesc.magFilter = FO_POINT;
- inputSampDesc.mipFilter = FO_POINT;
- SPtr<SamplerState> inputSampState = SamplerState::create(inputSampDesc);
- if(gpuParams->hasSamplerState(GPT_FRAGMENT_PROGRAM, "gDepthSamp"))
- gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gDepthSamp", inputSampState);
- }
- }
- void BuildHiZMat::_initVariations(ShaderVariations& variations)
- {
- // Do nothing
- }
- void BuildHiZMat::execute(const SPtr<Texture>& source, UINT32 srcMip, const Rect2& srcRect, const Rect2& dstRect,
- const SPtr<RenderTexture>& output)
- {
- RenderAPI& rapi = RenderAPI::instance();
- // If no texture view support, we must manually pick a valid mip level in the shader
- const RenderAPIInfo& rapiInfo = RenderAPI::instance().getAPIInfo();
- if(rapiInfo.isFlagSet(RenderAPIFeatureFlag::TextureViews))
- mInputTexture.set(source, TextureSurface(srcMip));
- else
- {
- mInputTexture.set(source);
- auto& props = source->getProperties();
- float pixelWidth = (float)props.getWidth();
- float pixelHeight = (float)props.getHeight();
- Vector2 halfPixelOffset(0.5f / pixelWidth, 0.5f / pixelHeight);
- gBuildHiZParamDef.gHalfPixelOffset.set(mParamBuffer, halfPixelOffset);
- gBuildHiZParamDef.gMipLevel.set(mParamBuffer, srcMip);
- }
- rapi.setRenderTarget(output);
- rapi.setViewport(dstRect);
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- gRendererUtility().drawScreenQuad(srcRect);
- rapi.setViewport(Rect2(0, 0, 1, 1));
- }
- FXAAParamDef gFXAAParamDef;
- FXAAMat::FXAAMat()
- {
- mParamBuffer = gFXAAParamDef.createBuffer();
- SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
- gpuParams->setParamBlockBuffer("Input", mParamBuffer);
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gInputTex", mInputTexture);
- }
- void FXAAMat::_initVariations(ShaderVariations& variations)
- {
- // Do nothing
- }
- void FXAAMat::execute(const SPtr<Texture>& source, const SPtr<RenderTarget>& destination)
- {
- const TextureProperties& srcProps = source->getProperties();
- Vector2 invTexSize(1.0f / srcProps.getWidth(), 1.0f / srcProps.getHeight());
- gFXAAParamDef.gInvTexSize.set(mParamBuffer, invTexSize);
- mInputTexture.set(source);
- RenderAPI& rapi = RenderAPI::instance();
- rapi.setRenderTarget(destination);
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- gRendererUtility().drawScreenQuad();
- }
- SSAOParamDef gSSAOParamDef;
- #define VARIATION(QUALITY) \
- ShaderVariation SSAOMat::VAR_Upsample_Final_Quality##QUALITY = ShaderVariation({ \
- ShaderVariation::Param("MIX_WITH_UPSAMPLED", true), \
- ShaderVariation::Param("FINAL_AO", true), \
- ShaderVariation::Param("QUALITY", QUALITY) \
- }); \
- ShaderVariation SSAOMat::VAR_Upsample_NoFinal_Quality##QUALITY = ShaderVariation({ \
- ShaderVariation::Param("MIX_WITH_UPSAMPLED", true), \
- ShaderVariation::Param("FINAL_AO", false), \
- ShaderVariation::Param("QUALITY", QUALITY) \
- }); \
- ShaderVariation SSAOMat::VAR_NoUpsample_Final_Quality##QUALITY = ShaderVariation({ \
- ShaderVariation::Param("MIX_WITH_UPSAMPLED", false), \
- ShaderVariation::Param("FINAL_AO", true), \
- ShaderVariation::Param("QUALITY", QUALITY) \
- }); \
- ShaderVariation SSAOMat::VAR_NoUpsample_NoFinal_Quality##QUALITY = ShaderVariation({ \
- ShaderVariation::Param("MIX_WITH_UPSAMPLED", false), \
- ShaderVariation::Param("FINAL_AO", false), \
- ShaderVariation::Param("QUALITY", QUALITY) \
- });
- VARIATION(0)
- VARIATION(1)
- VARIATION(2)
- VARIATION(3)
- VARIATION(4)
- #undef VARIATION
- SSAOMat::SSAOMat()
- {
- bool isFinal = mVariation.getBool("FINAL_AO");
- bool mixWithUpsampled = mVariation.getBool("MIX_WITH_UPSAMPLED");
- mParamBuffer = gSSAOParamDef.createBuffer();
- SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
- gpuParams->setParamBlockBuffer("Input", mParamBuffer);
- if (isFinal)
- {
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gDepthTex", mDepthTexture);
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gNormalsTex", mNormalsTexture);
- }
-
- if(!isFinal || mixWithUpsampled)
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gSetupAO", mSetupAOTexture);
- if(mixWithUpsampled)
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gDownsampledAO", mDownsampledAOTexture);
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gRandomTex", mRandomTexture);
- SAMPLER_STATE_DESC inputSampDesc;
- inputSampDesc.minFilter = FO_POINT;
- inputSampDesc.magFilter = FO_POINT;
- inputSampDesc.mipFilter = FO_POINT;
- inputSampDesc.addressMode.u = TAM_CLAMP;
- inputSampDesc.addressMode.v = TAM_CLAMP;
- inputSampDesc.addressMode.w = TAM_CLAMP;
- SPtr<SamplerState> inputSampState = SamplerState::create(inputSampDesc);
- if(gpuParams->hasSamplerState(GPT_FRAGMENT_PROGRAM, "gInputSamp"))
- gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gInputSamp", inputSampState);
- else
- {
- if (isFinal)
- {
- gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gDepthTex", inputSampState);
- gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gNormalsTex", inputSampState);
- }
-
- if(!isFinal || mixWithUpsampled)
- gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gSetupAO", inputSampState);
- if(mixWithUpsampled)
- gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gDownsampledAO", inputSampState);
- }
- SAMPLER_STATE_DESC randomSampDesc;
- randomSampDesc.minFilter = FO_POINT;
- randomSampDesc.magFilter = FO_POINT;
- randomSampDesc.mipFilter = FO_POINT;
- randomSampDesc.addressMode.u = TAM_WRAP;
- randomSampDesc.addressMode.v = TAM_WRAP;
- randomSampDesc.addressMode.w = TAM_WRAP;
- SPtr<SamplerState> randomSampState = SamplerState::create(randomSampDesc);
- setSamplerState(gpuParams, GPT_FRAGMENT_PROGRAM, "gRandomSamp", "gRandomTex", randomSampState);
- }
- void SSAOMat::_initVariations(ShaderVariations& variations)
- {
- #define VARIATION(QUALITY) \
- variations.add(VAR_Upsample_Final_Quality##QUALITY); \
- variations.add(VAR_Upsample_NoFinal_Quality##QUALITY); \
- variations.add(VAR_NoUpsample_Final_Quality##QUALITY); \
- variations.add(VAR_NoUpsample_NoFinal_Quality##QUALITY); \
- VARIATION(0)
- VARIATION(1)
- VARIATION(2)
- VARIATION(3)
- VARIATION(4)
- #undef VARIATION
- }
- void SSAOMat::execute(const RendererView& view, const SSAOTextureInputs& textures,
- const SPtr<RenderTexture>& destination, const AmbientOcclusionSettings& settings)
- {
- // Scale that can be used to adjust how quickly does AO radius increase with downsampled AO. This yields a very
- // small AO radius at highest level, and very large radius at lowest level
- static const float DOWNSAMPLE_SCALE = 4.0f;
- const RendererViewProperties& viewProps = view.getProperties();
- const RenderTargetProperties& rtProps = destination->getProperties();
- Vector2 tanHalfFOV;
- tanHalfFOV.x = 1.0f / viewProps.projTransform[0][0];
- tanHalfFOV.y = 1.0f / viewProps.projTransform[1][1];
- float cotHalfFOV = viewProps.projTransform[0][0];
- // Downsampled AO uses a larger AO radius (in higher resolutions this would cause too much cache trashing). This
- // means if only full res AO is used, then only AO from nearby geometry will be calculated.
- float viewScale = viewProps.viewRect.width / (float)rtProps.width;
- // Ramp up the radius exponentially. c^log2(x) function chosen arbitrarily, as it ramps up the radius in a nice way
- float scale = pow(DOWNSAMPLE_SCALE, Math::log2(viewScale));
- // Determine maximum radius scale (division by 4 because we don't downsample more than quarter-size)
- float maxScale = pow(DOWNSAMPLE_SCALE, Math::log2(4.0f));
- // Normalize the scale in [0, 1] range
- scale /= maxScale;
- float radius = settings.radius * scale;
- // Factors used for scaling the AO contribution with range
- Vector2 fadeMultiplyAdd;
- fadeMultiplyAdd.x = 1.0f / settings.fadeRange;
- fadeMultiplyAdd.y = -settings.fadeDistance / settings.fadeRange;
- gSSAOParamDef.gSampleRadius.set(mParamBuffer, radius);
- gSSAOParamDef.gCotHalfFOV.set(mParamBuffer, cotHalfFOV);
- gSSAOParamDef.gTanHalfFOV.set(mParamBuffer, tanHalfFOV);
- gSSAOParamDef.gWorldSpaceRadiusMask.set(mParamBuffer, 1.0f);
- gSSAOParamDef.gBias.set(mParamBuffer, (settings.bias * viewScale) / 1000.0f);
- gSSAOParamDef.gFadeMultiplyAdd.set(mParamBuffer, fadeMultiplyAdd);
- gSSAOParamDef.gPower.set(mParamBuffer, settings.power);
- gSSAOParamDef.gIntensity.set(mParamBuffer, settings.intensity);
-
- bool upsample = mVariation.getBool("MIX_WITH_UPSAMPLED");
- if(upsample)
- {
- const TextureProperties& props = textures.aoDownsampled->getProperties();
- Vector2 downsampledPixelSize;
- downsampledPixelSize.x = 1.0f / props.getWidth();
- downsampledPixelSize.y = 1.0f / props.getHeight();
- gSSAOParamDef.gDownsampledPixelSize.set(mParamBuffer, downsampledPixelSize);
- }
- // Generate a scale which we need to use in order to achieve tiling
- const TextureProperties& rndProps = textures.randomRotations->getProperties();
- UINT32 rndWidth = rndProps.getWidth();
- UINT32 rndHeight = rndProps.getHeight();
- //// Multiple of random texture size, rounded up
- UINT32 scaleWidth = (rtProps.width + rndWidth - 1) / rndWidth;
- UINT32 scaleHeight = (rtProps.height + rndHeight - 1) / rndHeight;
- Vector2 randomTileScale((float)scaleWidth, (float)scaleHeight);
- gSSAOParamDef.gRandomTileScale.set(mParamBuffer, randomTileScale);
- mSetupAOTexture.set(textures.aoSetup);
- bool finalPass = mVariation.getBool("FINAL_AO");
- if (finalPass)
- {
- mDepthTexture.set(textures.sceneDepth);
- mNormalsTexture.set(textures.sceneNormals);
- }
- if (upsample)
- mDownsampledAOTexture.set(textures.aoDownsampled);
-
- mRandomTexture.set(textures.randomRotations);
- SPtr<GpuParamBlockBuffer> perView = view.getPerViewBuffer();
- mParamsSet->getGpuParams()->setParamBlockBuffer("PerCamera", perView);
- RenderAPI& rapi = RenderAPI::instance();
- rapi.setRenderTarget(destination);
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- gRendererUtility().drawScreenQuad();
- }
- SSAOMat* SSAOMat::getVariation(bool upsample, bool finalPass, int quality)
- {
- #define PICK_MATERIAL(QUALITY) \
- if(upsample) \
- if(finalPass) \
- return get(VAR_Upsample_Final_Quality##QUALITY); \
- else \
- return get(VAR_Upsample_NoFinal_Quality##QUALITY); \
- else \
- if(finalPass) \
- return get(VAR_NoUpsample_Final_Quality##QUALITY); \
- else \
- return get(VAR_NoUpsample_NoFinal_Quality##QUALITY); \
- switch(quality)
- {
- case 0:
- PICK_MATERIAL(0)
- case 1:
- PICK_MATERIAL(1)
- case 2:
- PICK_MATERIAL(2)
- case 3:
- PICK_MATERIAL(3)
- default:
- case 4:
- PICK_MATERIAL(4)
- }
- #undef PICK_MATERIAL
- }
- SSAODownsampleParamDef gSSAODownsampleParamDef;
- SSAODownsampleMat::SSAODownsampleMat()
- {
- mParamBuffer = gSSAODownsampleParamDef.createBuffer();
- SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
- gpuParams->setParamBlockBuffer("Input", mParamBuffer);
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gDepthTex", mDepthTexture);
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gNormalsTex", mNormalsTexture);
- SAMPLER_STATE_DESC inputSampDesc;
- inputSampDesc.minFilter = FO_LINEAR;
- inputSampDesc.magFilter = FO_LINEAR;
- inputSampDesc.mipFilter = FO_LINEAR;
- inputSampDesc.addressMode.u = TAM_CLAMP;
- inputSampDesc.addressMode.v = TAM_CLAMP;
- inputSampDesc.addressMode.w = TAM_CLAMP;
- SPtr<SamplerState> inputSampState = SamplerState::create(inputSampDesc);
- if(gpuParams->hasSamplerState(GPT_FRAGMENT_PROGRAM, "gInputSamp"))
- gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gInputSamp", inputSampState);
- else
- {
- gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gDepthTex", inputSampState);
- gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gNormalsTex", inputSampState);
- }
- }
- void SSAODownsampleMat::_initVariations(ShaderVariations& variations)
- {
- // Do nothing
- }
- void SSAODownsampleMat::execute(const RendererView& view, const SPtr<Texture>& depth, const SPtr<Texture>& normals,
- const SPtr<RenderTexture>& destination, float depthRange)
- {
- const RendererViewProperties& viewProps = view.getProperties();
- const RenderTargetProperties& rtProps = destination->getProperties();
- Vector2 pixelSize;
- pixelSize.x = 1.0f / rtProps.width;
- pixelSize.y = 1.0f / rtProps.height;
- float scale = viewProps.viewRect.width / (float)rtProps.width;
- gSSAODownsampleParamDef.gPixelSize.set(mParamBuffer, pixelSize);
- gSSAODownsampleParamDef.gInvDepthThreshold.set(mParamBuffer, (1.0f / depthRange) / scale);
- mDepthTexture.set(depth);
- mNormalsTexture.set(normals);
- SPtr<GpuParamBlockBuffer> perView = view.getPerViewBuffer();
- mParamsSet->getGpuParams()->setParamBlockBuffer("PerCamera", perView);
- RenderAPI& rapi = RenderAPI::instance();
- rapi.setRenderTarget(destination);
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- gRendererUtility().drawScreenQuad();
- }
- SSAOBlurParamDef gSSAOBlurParamDef;
- ShaderVariation SSAOBlurMat::VAR_Vertical = ShaderVariation({
- ShaderVariation::Param("DIR_HORZ", false)
- });
- ShaderVariation SSAOBlurMat::VAR_Horizontal = ShaderVariation({
- ShaderVariation::Param("DIR_HORZ", true)
- });
- SSAOBlurMat::SSAOBlurMat()
- {
- mParamBuffer = gSSAOBlurParamDef.createBuffer();
- SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
- gpuParams->setParamBlockBuffer("Input", mParamBuffer);
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gInputTex", mAOTexture);
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gDepthTex", mDepthTexture);
- SAMPLER_STATE_DESC inputSampDesc;
- inputSampDesc.minFilter = FO_POINT;
- inputSampDesc.magFilter = FO_POINT;
- inputSampDesc.mipFilter = FO_POINT;
- inputSampDesc.addressMode.u = TAM_CLAMP;
- inputSampDesc.addressMode.v = TAM_CLAMP;
- inputSampDesc.addressMode.w = TAM_CLAMP;
- SPtr<SamplerState> inputSampState = SamplerState::create(inputSampDesc);
- if(gpuParams->hasSamplerState(GPT_FRAGMENT_PROGRAM, "gInputSamp"))
- gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gInputSamp", inputSampState);
- else
- {
- gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gInputTex", inputSampState);
- gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gDepthTex", inputSampState);
- }
- }
- void SSAOBlurMat::_initVariations(ShaderVariations& variations)
- {
- variations.add(VAR_Horizontal);
- variations.add(VAR_Vertical);
- }
- void SSAOBlurMat::execute(const RendererView& view, const SPtr<Texture>& ao, const SPtr<Texture>& depth,
- const SPtr<RenderTexture>& destination, float depthRange)
- {
- const RendererViewProperties& viewProps = view.getProperties();
- const TextureProperties& texProps = ao->getProperties();
- Vector2 pixelSize;
- pixelSize.x = 1.0f / texProps.getWidth();
- pixelSize.y = 1.0f / texProps.getHeight();
- Vector2 pixelOffset(BsZero);
- if (mVariation.getBool("DIR_HORZ"))
- pixelOffset.x = pixelSize.x;
- else
- pixelOffset.y = pixelSize.y;
- float scale = viewProps.viewRect.width / (float)texProps.getWidth();
- gSSAOBlurParamDef.gPixelSize.set(mParamBuffer, pixelSize);
- gSSAOBlurParamDef.gPixelOffset.set(mParamBuffer, pixelOffset);
- gSSAOBlurParamDef.gInvDepthThreshold.set(mParamBuffer, (1.0f / depthRange) / scale);
- mAOTexture.set(ao);
- mDepthTexture.set(depth);
- SPtr<GpuParamBlockBuffer> perView = view.getPerViewBuffer();
- mParamsSet->getGpuParams()->setParamBlockBuffer("PerCamera", perView);
- RenderAPI& rapi = RenderAPI::instance();
- rapi.setRenderTarget(destination);
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- gRendererUtility().drawScreenQuad();
- }
- SSAOBlurMat* SSAOBlurMat::getVariation(bool horizontal)
- {
- if (horizontal)
- return get(VAR_Horizontal);
-
- return get(VAR_Vertical);
- }
- SSRStencilParamDef gSSRStencilParamDef;
- ShaderVariation SSRStencilMat::VAR_FullMSAA = ShaderVariation({
- ShaderVariation::Param("MSAA_COUNT", 2)
- });
- ShaderVariation SSRStencilMat::VAR_SingleMSAA = ShaderVariation({
- ShaderVariation::Param("MSAA_COUNT", 2),
- ShaderVariation::Param("MSAA_RESOLVE_0TH", true)
- });
- ShaderVariation SSRStencilMat::VAR_NoMSAA = ShaderVariation({
- ShaderVariation::Param("MSAA_COUNT", 1)
- });
- SSRStencilMat::SSRStencilMat()
- :mGBufferParams(mMaterial, mParamsSet)
- {
- mParamBuffer = gSSRStencilParamDef.createBuffer();
- mParamsSet->getGpuParams()->setParamBlockBuffer("Input", mParamBuffer);
- }
- void SSRStencilMat::_initVariations(ShaderVariations& variations)
- {
- variations.add(VAR_FullMSAA);
- variations.add(VAR_SingleMSAA);
- variations.add(VAR_NoMSAA);
- }
- void SSRStencilMat::execute(const RendererView& view, GBufferTextures gbuffer,
- const ScreenSpaceReflectionsSettings& settings)
- {
- mGBufferParams.bind(gbuffer);
- Vector2 roughnessScaleBias = SSRTraceMat::calcRoughnessFadeScaleBias(settings.maxRoughness);
- gSSRStencilParamDef.gRoughnessScaleBias.set(mParamBuffer, roughnessScaleBias);
- SPtr<GpuParamBlockBuffer> perView = view.getPerViewBuffer();
- mParamsSet->getGpuParams()->setParamBlockBuffer("PerCamera", perView);
- const RendererViewProperties& viewProps = view.getProperties();
- const Rect2I& viewRect = viewProps.viewRect;
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- if(viewProps.numSamples > 1)
- gRendererUtility().drawScreenQuad(Rect2(0.0f, 0.0f, (float)viewRect.width, (float)viewRect.height));
- else
- gRendererUtility().drawScreenQuad();
- }
- SSRStencilMat* SSRStencilMat::getVariation(bool msaa, bool singleSampleMSAA)
- {
- if (msaa)
- {
- if (singleSampleMSAA)
- return get(VAR_SingleMSAA);
- return get(VAR_FullMSAA);
- }
- else
- return get(VAR_NoMSAA);
- }
- SSRTraceParamDef gSSRTraceParamDef;
- #define VARIATION(QUALITY) \
- ShaderVariation SSRTraceMat::VAR_NoMSAA_Quality##QUALITY = ShaderVariation({ \
- ShaderVariation::Param("MSAA_COUNT", 1), \
- ShaderVariation::Param("QUALITY", QUALITY) \
- }); \
- ShaderVariation SSRTraceMat::VAR_FullMSAA_Quality##QUALITY = ShaderVariation({ \
- ShaderVariation::Param("MSAA_COUNT", 2), \
- ShaderVariation::Param("QUALITY", QUALITY) \
- }); \
- ShaderVariation SSRTraceMat::VAR_SingleMSAA_Quality##QUALITY = ShaderVariation({ \
- ShaderVariation::Param("MSAA_COUNT", 2), \
- ShaderVariation::Param("MSAA_RESOLVE_0TH", true), \
- ShaderVariation::Param("QUALITY", QUALITY) \
- }); \
- VARIATION(0)
- VARIATION(1)
- VARIATION(2)
- VARIATION(3)
- VARIATION(4)
- #undef VARIATION
- SSRTraceMat::SSRTraceMat()
- :mGBufferParams(mMaterial, mParamsSet)
- {
- mParamBuffer = gSSRTraceParamDef.createBuffer();
- SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gSceneColor", mSceneColorTexture);
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gHiZ", mHiZTexture);
- if(gpuParams->hasParamBlock(GPT_FRAGMENT_PROGRAM, "Input"))
- gpuParams->setParamBlockBuffer(GPT_FRAGMENT_PROGRAM, "Input", mParamBuffer);
- SAMPLER_STATE_DESC desc;
- desc.minFilter = FO_POINT;
- desc.magFilter = FO_POINT;
- desc.mipFilter = FO_POINT;
- desc.addressMode.u = TAM_CLAMP;
- desc.addressMode.v = TAM_CLAMP;
- desc.addressMode.w = TAM_CLAMP;
- SPtr<SamplerState> hiZSamplerState = SamplerState::create(desc);
- if (gpuParams->hasSamplerState(GPT_FRAGMENT_PROGRAM, "gHiZSamp"))
- gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gHiZSamp", hiZSamplerState);
- else if(gpuParams->hasSamplerState(GPT_FRAGMENT_PROGRAM, "gHiZ"))
- gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gHiZ", hiZSamplerState);
- }
- void SSRTraceMat::_initVariations(ShaderVariations& variations)
- {
- #define VARIATION(QUALITY) \
- variations.add(VAR_NoMSAA_Quality##QUALITY); \
- variations.add(VAR_FullMSAA_Quality##QUALITY); \
- variations.add(VAR_SingleMSAA_Quality##QUALITY); \
- VARIATION(0)
- VARIATION(1)
- VARIATION(2)
- VARIATION(3)
- VARIATION(4)
- #undef VARIATION
- }
- void SSRTraceMat::execute(const RendererView& view, GBufferTextures gbuffer, const SPtr<Texture>& sceneColor,
- const SPtr<Texture>& hiZ, const ScreenSpaceReflectionsSettings& settings,
- const SPtr<RenderTarget>& destination)
- {
- const RendererViewProperties& viewProps = view.getProperties();
- const TextureProperties& hiZProps = hiZ->getProperties();
- mGBufferParams.bind(gbuffer);
- mSceneColorTexture.set(sceneColor);
- mHiZTexture.set(hiZ);
-
- Rect2I viewRect = viewProps.viewRect;
- // Maps from NDC to UV [0, 1]
- Vector4 ndcToHiZUV;
- ndcToHiZUV.x = 0.5f;
- ndcToHiZUV.y = -0.5f;
- ndcToHiZUV.z = 0.5f;
- ndcToHiZUV.w = 0.5f;
- // Either of these flips the Y axis, but if they're both true they cancel out
- RenderAPI& rapi = RenderAPI::instance();
- const RenderAPIInfo& rapiInfo = rapi.getAPIInfo();
- if (rapiInfo.isFlagSet(RenderAPIFeatureFlag::UVYAxisUp) ^ rapiInfo.isFlagSet(RenderAPIFeatureFlag::NDCYAxisDown))
- ndcToHiZUV.y = -ndcToHiZUV.y;
-
- // Maps from [0, 1] to area of HiZ where depth is stored in
- ndcToHiZUV.x *= (float)viewRect.width / hiZProps.getWidth();
- ndcToHiZUV.y *= (float)viewRect.height / hiZProps.getHeight();
- ndcToHiZUV.z *= (float)viewRect.width / hiZProps.getWidth();
- ndcToHiZUV.w *= (float)viewRect.height / hiZProps.getHeight();
-
- // Maps from HiZ UV to [0, 1] UV
- Vector2 HiZUVToScreenUV;
- HiZUVToScreenUV.x = hiZProps.getWidth() / (float)viewRect.width;
- HiZUVToScreenUV.y = hiZProps.getHeight() / (float)viewRect.height;
- // Used for roughness fading
- Vector2 roughnessScaleBias = calcRoughnessFadeScaleBias(settings.maxRoughness);
- UINT32 temporalJitter = (viewProps.frameIdx % 8) * 1503;
- Vector2I bufferSize(viewRect.width, viewRect.height);
- gSSRTraceParamDef.gHiZSize.set(mParamBuffer, bufferSize);
- gSSRTraceParamDef.gHiZNumMips.set(mParamBuffer, hiZProps.getNumMipmaps());
- gSSRTraceParamDef.gNDCToHiZUV.set(mParamBuffer, ndcToHiZUV);
- gSSRTraceParamDef.gHiZUVToScreenUV.set(mParamBuffer, HiZUVToScreenUV);
- gSSRTraceParamDef.gIntensity.set(mParamBuffer, settings.intensity);
- gSSRTraceParamDef.gRoughnessScaleBias.set(mParamBuffer, roughnessScaleBias);
- gSSRTraceParamDef.gTemporalJitter.set(mParamBuffer, temporalJitter);
- SPtr<GpuParamBlockBuffer> perView = view.getPerViewBuffer();
- mParamsSet->getGpuParams()->setParamBlockBuffer("PerCamera", perView);
- rapi.setRenderTarget(destination, FBT_DEPTH);
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- if(viewProps.numSamples > 1)
- gRendererUtility().drawScreenQuad(Rect2(0.0f, 0.0f, (float)viewRect.width, (float)viewRect.height));
- else
- gRendererUtility().drawScreenQuad();
- }
- Vector2 SSRTraceMat::calcRoughnessFadeScaleBias(float maxRoughness)
- {
- const static float RANGE_SCALE = 2.0f;
- Vector2 scaleBias;
- scaleBias.x = -RANGE_SCALE / (-1.0f + maxRoughness);
- scaleBias.y = (RANGE_SCALE * maxRoughness) / (-1.0f + maxRoughness);
- return scaleBias;
- }
- SSRTraceMat* SSRTraceMat::getVariation(UINT32 quality, bool msaa, bool singleSampleMSAA)
- {
- #define PICK_MATERIAL(QUALITY) \
- if(msaa) \
- if(singleSampleMSAA) \
- return get(VAR_SingleMSAA_Quality##QUALITY); \
- else \
- return get(VAR_FullMSAA_Quality##QUALITY); \
- else \
- return get(VAR_NoMSAA_Quality##QUALITY); \
- switch(quality)
- {
- case 0:
- PICK_MATERIAL(0)
- case 1:
- PICK_MATERIAL(1)
- case 2:
- PICK_MATERIAL(2)
- case 3:
- PICK_MATERIAL(3)
- default:
- case 4:
- PICK_MATERIAL(4)
- }
- #undef PICK_MATERIAL
- }
- TemporalResolveParamDef gTemporalResolveParamDef;
- SSRResolveParamDef gSSRResolveParamDef;
- ShaderVariation SSRResolveMat::VAR_MSAA = ShaderVariation({
- ShaderVariation::Param("MSAA", true)
- });
- ShaderVariation SSRResolveMat::VAR_NoMSAA = ShaderVariation({
- ShaderVariation::Param("MSAA", false)
- });
- SSRResolveMat::SSRResolveMat()
- {
- mSSRParamBuffer = gSSRResolveParamDef.createBuffer();
- mTemporalParamBuffer = gTemporalResolveParamDef.createBuffer();
- SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gSceneDepth", mSceneDepthTexture);
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gSceneColor", mSceneColorTexture);
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gPrevColor", mPrevColorTexture);
- gpuParams->setParamBlockBuffer(GPT_FRAGMENT_PROGRAM, "Input", mSSRParamBuffer);
- gpuParams->setParamBlockBuffer(GPT_FRAGMENT_PROGRAM, "TemporalInput", mTemporalParamBuffer);
- SAMPLER_STATE_DESC pointSampDesc;
- pointSampDesc.minFilter = FO_POINT;
- pointSampDesc.magFilter = FO_POINT;
- pointSampDesc.mipFilter = FO_POINT;
- pointSampDesc.addressMode.u = TAM_CLAMP;
- pointSampDesc.addressMode.v = TAM_CLAMP;
- pointSampDesc.addressMode.w = TAM_CLAMP;
- SPtr<SamplerState> pointSampState = SamplerState::create(pointSampDesc);
- if(gpuParams->hasSamplerState(GPT_FRAGMENT_PROGRAM, "gPointSampler"))
- gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gPointSampler", pointSampState);
- else
- gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gSceneDepth", pointSampState);
- SAMPLER_STATE_DESC linearSampDesc;
- linearSampDesc.minFilter = FO_POINT;
- linearSampDesc.magFilter = FO_POINT;
- linearSampDesc.mipFilter = FO_POINT;
- linearSampDesc.addressMode.u = TAM_CLAMP;
- linearSampDesc.addressMode.v = TAM_CLAMP;
- linearSampDesc.addressMode.w = TAM_CLAMP;
- SPtr<SamplerState> linearSampState = SamplerState::create(linearSampDesc);
- if(gpuParams->hasSamplerState(GPT_FRAGMENT_PROGRAM, "gLinearSampler"))
- gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gLinearSampler", linearSampState);
- else
- {
- gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gSceneColor", linearSampState);
- gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gPrevColor", linearSampState);
- }
- }
- void SSRResolveMat::_initVariations(ShaderVariations& variations)
- {
- variations.add(VAR_NoMSAA);
- variations.add(VAR_MSAA);
- }
- void SSRResolveMat::execute(const RendererView& view, const SPtr<Texture>& prevFrame,
- const SPtr<Texture>& curFrame, const SPtr<Texture>& sceneDepth, const SPtr<RenderTarget>& destination)
- {
- // Note: This shader should not be called when temporal AA is turned on
- // Note: This shader doesn't have velocity texture enabled and will only account for camera movement (can be easily
- // enabled when velocity texture is added)
- // - WHen added, velocity should use a 16-bit SNORM format
- mPrevColorTexture.set(prevFrame);
- mSceneColorTexture.set(curFrame);
- mSceneDepthTexture.set(sceneDepth);
- auto& colorProps = curFrame->getProperties(); // Assuming prev and current frame are the same size
- auto& depthProps = sceneDepth->getProperties();
- Vector2 colorPixelSize(1.0f / colorProps.getWidth(), 1.0f / colorProps.getHeight());
- Vector2 depthPixelSize(1.0f / depthProps.getWidth(), 1.0f / depthProps.getHeight());
- gSSRResolveParamDef.gSceneColorTexelSize.set(mSSRParamBuffer, colorPixelSize);
- gSSRResolveParamDef.gSceneDepthTexelSize.set(mSSRParamBuffer, depthPixelSize);
- gSSRResolveParamDef.gManualExposure.set(mSSRParamBuffer, 1.0f);
- // Generate samples
- // Note: Move this code to a more general spot where it can be used by other temporal shaders.
-
- float sampleWeights[9];
- float sampleWeightsLowPass[9];
- float totalWeights = 0.0f;
- float totalWeightsLowPass = 0.0f;
- Vector2 jitter(BsZero); // Only relevant for general case, not using this type of jitter for SSR
- // Weights are generated using an exponential fit to Blackman-Harris 3.3
- bool useYCoCg = false; // Only relevant for general case, not using it for SSR
- float sharpness = 1.0f; // Make this a customizable parameter eventually
- if(useYCoCg)
- {
- static const Vector2 sampleOffsets[] =
- {
- { 0.0f, -1.0f },
- { -1.0f, 0.0f },
- { 0.0f, 0.0f },
- { 1.0f, 0.0f },
- { 0.0f, 1.0f },
- };
- for (UINT32 i = 0; i < 5; ++i)
- {
- // Get rid of jitter introduced by the projection matrix
- Vector2 offset = sampleOffsets[i] - jitter;
- offset *= 1.0f + sharpness * 0.5f;
- sampleWeights[i] = exp(-2.29f * offset.dot(offset));
- totalWeights += sampleWeights[i];
- }
- for (UINT32 i = 5; i < 9; ++i)
- sampleWeights[i] = 0.0f;
-
- memset(sampleWeightsLowPass, 0, sizeof(sampleWeightsLowPass));
- totalWeightsLowPass = 1.0f;
- }
- else
- {
- static const Vector2 sampleOffsets[] =
- {
- { -1.0f, -1.0f },
- { 0.0f, -1.0f },
- { 1.0f, -1.0f },
- { -1.0f, 0.0f },
- { 0.0f, 0.0f },
- { 1.0f, 0.0f },
- { -1.0f, 1.0f },
- { 0.0f, 1.0f },
- { 1.0f, 1.0f },
- };
- for (UINT32 i = 0; i < 9; ++i)
- {
- // Get rid of jitter introduced by the projection matrix
- Vector2 offset = sampleOffsets[i] - jitter;
- offset *= 1.0f + sharpness * 0.5f;
- sampleWeights[i] = exp(-2.29f * offset.dot(offset));
- totalWeights += sampleWeights[i];
- // Low pass
- offset *= 0.25f;
- sampleWeightsLowPass[i] = exp(-2.29f * offset.dot(offset));
- totalWeightsLowPass += sampleWeightsLowPass[i];
- }
- }
- for (UINT32 i = 0; i < 9; ++i)
- {
- gTemporalResolveParamDef.gSampleWeights.set(mTemporalParamBuffer, sampleWeights[i] / totalWeights, i);
- gTemporalResolveParamDef.gSampleWeightsLowpass.set(mTemporalParamBuffer, sampleWeightsLowPass[i] / totalWeightsLowPass, i);
- }
-
- SPtr<GpuParamBlockBuffer> perView = view.getPerViewBuffer();
- mParamsSet->getGpuParams()->setParamBlockBuffer("PerCamera", perView);
- RenderAPI& rapi = RenderAPI::instance();
- rapi.setRenderTarget(destination);
- const RendererViewProperties& viewProps = view.getProperties();
- const Rect2I& viewRect = viewProps.viewRect;
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- if(viewProps.numSamples > 1)
- gRendererUtility().drawScreenQuad(Rect2(0.0f, 0.0f, (float)viewRect.width, (float)viewRect.height));
- else
- gRendererUtility().drawScreenQuad();
- }
- SSRResolveMat* SSRResolveMat::getVariation(bool msaa)
- {
- if (msaa)
- return get(VAR_MSAA);
- else
- return get(VAR_NoMSAA);
- }
- EncodeDepthParamDef gEncodeDepthParamDef;
- EncodeDepthMat::EncodeDepthMat()
- {
- mParamBuffer = gEncodeDepthParamDef.createBuffer();
- SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
- gpuParams->setParamBlockBuffer("Params", mParamBuffer);
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gInputTex", mInputTexture);
- SAMPLER_STATE_DESC sampDesc;
- sampDesc.minFilter = FO_POINT;
- sampDesc.magFilter = FO_POINT;
- sampDesc.mipFilter = FO_POINT;
- sampDesc.addressMode.u = TAM_CLAMP;
- sampDesc.addressMode.v = TAM_CLAMP;
- sampDesc.addressMode.w = TAM_CLAMP;
- SPtr<SamplerState> samplerState = SamplerState::create(sampDesc);
- setSamplerState(gpuParams, GPT_FRAGMENT_PROGRAM, "gInputSamp", "gInputTex", samplerState);
- }
- void EncodeDepthMat::_initVariations(ShaderVariations& variations)
- {
- // Do nothing
- }
- void EncodeDepthMat::execute(const SPtr<Texture>& depth, float near, float far, const SPtr<RenderTarget>& output)
- {
- mInputTexture.set(depth);
- gEncodeDepthParamDef.gNear.set(mParamBuffer, near);
- gEncodeDepthParamDef.gFar.set(mParamBuffer, far);
- RenderAPI& rapi = RenderAPI::instance();
- rapi.setRenderTarget(output, 0, RT_COLOR0);
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- gRendererUtility().drawScreenQuad();
- }
- ShaderVariation MSAACoverageMat::VAR_2x = ShaderVariation({
- ShaderVariation::Param("MSAA_COUNT", 2)
- });
- ShaderVariation MSAACoverageMat::VAR_4x = ShaderVariation({
- ShaderVariation::Param("MSAA_COUNT", 4)
- });
- ShaderVariation MSAACoverageMat::VAR_8x = ShaderVariation({
- ShaderVariation::Param("MSAA_COUNT", 8)
- });
- MSAACoverageMat::MSAACoverageMat()
- :mGBufferParams(mMaterial, mParamsSet)
- { }
- void MSAACoverageMat::_initVariations(ShaderVariations& variations)
- {
- variations.add(VAR_2x);
- variations.add(VAR_4x);
- variations.add(VAR_8x);
- }
- void MSAACoverageMat::execute(const RendererView& view, GBufferTextures gbuffer)
- {
- mGBufferParams.bind(gbuffer);
- const Rect2I& viewRect = view.getProperties().viewRect;
- SPtr<GpuParamBlockBuffer> perView = view.getPerViewBuffer();
- mParamsSet->getGpuParams()->setParamBlockBuffer("PerCamera", perView);
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- gRendererUtility().drawScreenQuad(Rect2(0, 0, (float)viewRect.width, (float)viewRect.height));
- }
- MSAACoverageMat* MSAACoverageMat::getVariation(UINT32 msaaCount)
- {
- switch(msaaCount)
- {
- case 2:
- return get(VAR_2x);
- case 4:
- return get(VAR_4x);
- case 8:
- default:
- return get(VAR_8x);
- }
- }
- MSAACoverageStencilMat::MSAACoverageStencilMat()
- {
- SPtr<GpuParams> params = mParamsSet->getGpuParams();
- params->getTextureParam(GPT_FRAGMENT_PROGRAM, "gMSAACoverage", mCoverageTexParam);
- }
- void MSAACoverageStencilMat::_initVariations(ShaderVariations& variations)
- {
- // Do nothing
- }
- void MSAACoverageStencilMat::execute(const RendererView& view, const SPtr<Texture>& coverage)
- {
- const Rect2I& viewRect = view.getProperties().viewRect;
- mCoverageTexParam.set(coverage);
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- gRendererUtility().drawScreenQuad(Rect2(0, 0, (float)viewRect.width, (float)viewRect.height));
- }
- }}
|