| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812 |
- //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
- //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
- #include "BsPostProcessing.h"
- #include "BsRenderTexture.h"
- #include "BsGpuResourcePool.h"
- #include "BsRendererUtility.h"
- #include "BsTextureManager.h"
- #include "BsCamera.h"
- #include "BsGpuParamsSet.h"
- #include "BsRendererView.h"
- #include "BsRenderTargets.h"
- #include "BsPixelUtil.h"
- #include "BsBitwise.h"
- namespace bs { namespace ct
- {
- DownsampleParamDef gDownsampleParamDef;
- template<int Quality, bool MSAA>
- DownsampleMat<Quality, MSAA>::DownsampleMat()
- {
- mParamBuffer = gDownsampleParamDef.createBuffer();
- SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
- if(gpuParams->hasParamBlock(GPT_FRAGMENT_PROGRAM, "Input"))
- mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
- mParamsSet->getGpuParams()->getTextureParam(GPT_FRAGMENT_PROGRAM, "gInputTex", mInputTexture);
- }
- template<int Quality, bool MSAA>
- void DownsampleMat<Quality, MSAA>::_initDefines(ShaderDefines& defines)
- {
- defines.set("QUALITY", Quality);
- defines.set("MSAA", MSAA ? 1 : 0);
- }
- template<int Quality, bool MSAA>
- void DownsampleMat<Quality, MSAA>::execute(const SPtr<Texture>& target, PostProcessInfo& ppInfo)
- {
- // Set parameters
- mInputTexture.set(target);
- const TextureProperties& rtProps = target->getProperties();
- 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));
- }
- // Set output
- UINT32 width = std::max(1, Math::ceilToInt(rtProps.getWidth() * 0.5f));
- UINT32 height = std::max(1, Math::ceilToInt(rtProps.getHeight() * 0.5f));
- mOutputDesc = POOLED_RENDER_TEXTURE_DESC::create2D(rtProps.getFormat(), width, height, TU_RENDERTARGET);
- // Render
- ppInfo.downsampledSceneTex = GpuResourcePool::instance().get(mOutputDesc);
- RenderAPI& rapi = RenderAPI::instance();
- rapi.setRenderTarget(ppInfo.downsampledSceneTex->renderTexture, 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);
- mOutput = ppInfo.downsampledSceneTex->renderTexture;
- }
- template<int Quality, bool MSAA>
- void DownsampleMat<Quality, MSAA>::release(PostProcessInfo& ppInfo)
- {
- GpuResourcePool::instance().release(ppInfo.downsampledSceneTex);
- mOutput = nullptr;
- }
- void DownsampleMaterials::execute(UINT32 quality, bool msaa, const SPtr<Texture>& target, PostProcessInfo& ppInfo)
- {
- if(quality == 0)
- {
- if(msaa)
- m0_MSAA.execute(target, ppInfo);
- else
- m0_NoMSAA.execute(target, ppInfo);
- }
- else
- {
- if (msaa)
- m1_MSAA.execute(target, ppInfo);
- else
- m1_NoMSAA.execute(target, ppInfo);
- }
- }
- void DownsampleMaterials::release(UINT32 quality, bool msaa, PostProcessInfo& ppInfo)
- {
- if(quality == 0)
- {
- if(msaa)
- m0_MSAA.release(ppInfo);
- else
- m0_NoMSAA.release(ppInfo);
- }
- else
- {
- if (msaa)
- m1_MSAA.release(ppInfo);
- else
- m1_NoMSAA.release(ppInfo);
- }
- }
- template class DownsampleMat<0, false>;
- template class DownsampleMat<0, true>;
- template class DownsampleMat<1, false>;
- template class DownsampleMat<1, true>;
- EyeAdaptHistogramParamDef gEyeAdaptHistogramParamDef;
- EyeAdaptHistogramMat::EyeAdaptHistogramMat()
- {
- mParamBuffer = gEyeAdaptHistogramParamDef.createBuffer();
- mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
- SPtr<GpuParams> params = mParamsSet->getGpuParams();
- params->getTextureParam(GPT_COMPUTE_PROGRAM, "gSceneColorTex", mSceneColor);
- params->getLoadStoreTextureParam(GPT_COMPUTE_PROGRAM, "gOutputTex", mOutputTex);
- }
- void EyeAdaptHistogramMat::_initDefines(ShaderDefines& defines)
- {
- defines.set("THREADGROUP_SIZE_X", THREAD_GROUP_SIZE_X);
- defines.set("THREADGROUP_SIZE_Y", THREAD_GROUP_SIZE_Y);
- defines.set("LOOP_COUNT_X", LOOP_COUNT_X);
- defines.set("LOOP_COUNT_Y", LOOP_COUNT_Y);
- }
- void EyeAdaptHistogramMat::execute(PostProcessInfo& ppInfo)
- {
- // Set parameters
- SPtr<RenderTexture> target = ppInfo.downsampledSceneTex->renderTexture;
- mSceneColor.set(ppInfo.downsampledSceneTex->texture);
- const RenderTextureProperties& props = target->getProperties();
- int offsetAndSize[4] = { 0, 0, (INT32)props.getWidth(), (INT32)props.getHeight() };
- gEyeAdaptHistogramParamDef.gHistogramParams.set(mParamBuffer, getHistogramScaleOffset(ppInfo));
- gEyeAdaptHistogramParamDef.gPixelOffsetAndSize.set(mParamBuffer, Vector4I(offsetAndSize));
- Vector2I threadGroupCount = getThreadGroupCount(target);
- gEyeAdaptHistogramParamDef.gThreadGroupCount.set(mParamBuffer, threadGroupCount);
- // Set output
- UINT32 numHistograms = threadGroupCount.x * threadGroupCount.y;
- mOutputDesc = POOLED_RENDER_TEXTURE_DESC::create2D(PF_FLOAT16_RGBA, HISTOGRAM_NUM_TEXELS, numHistograms,
- TU_LOADSTORE);
- // Dispatch
- ppInfo.histogramTex = GpuResourcePool::instance().get(mOutputDesc);
- mOutputTex.set(ppInfo.histogramTex->texture);
- RenderAPI& rapi = RenderAPI::instance();
- gRendererUtility().setComputePass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- rapi.dispatchCompute(threadGroupCount.x, threadGroupCount.y);
- mOutput = ppInfo.histogramTex->renderTexture;
- }
- void EyeAdaptHistogramMat::release(PostProcessInfo& ppInfo)
- {
- GpuResourcePool::instance().release(ppInfo.histogramTex);
- mOutput = nullptr;
- }
- Vector2I EyeAdaptHistogramMat::getThreadGroupCount(const SPtr<RenderTexture>& target)
- {
- const UINT32 texelsPerThreadGroupX = THREAD_GROUP_SIZE_X * LOOP_COUNT_X;
- const UINT32 texelsPerThreadGroupY = THREAD_GROUP_SIZE_Y * LOOP_COUNT_Y;
- const RenderTextureProperties& 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 PostProcessInfo& ppInfo)
- {
- const StandardPostProcessSettings& settings = *ppInfo.settings;
- float diff = settings.autoExposure.histogramLog2Max - settings.autoExposure.histogramLog2Min;
- float scale = 1.0f / diff;
- float offset = -settings.autoExposure.histogramLog2Min * scale;
- return Vector2(scale, offset);
- }
- EyeAdaptHistogramReduceParamDef gEyeAdaptHistogramReduceParamDef;
- EyeAdaptHistogramReduceMat::EyeAdaptHistogramReduceMat()
- {
- mParamBuffer = gEyeAdaptHistogramReduceParamDef.createBuffer();
- mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
- SPtr<GpuParams> params = mParamsSet->getGpuParams();
- params->getTextureParam(GPT_FRAGMENT_PROGRAM, "gHistogramTex", mHistogramTex);
- params->getTextureParam(GPT_FRAGMENT_PROGRAM, "gEyeAdaptationTex", mEyeAdaptationTex);
- }
- void EyeAdaptHistogramReduceMat::_initDefines(ShaderDefines& defines)
- {
- // Do nothing
- }
- void EyeAdaptHistogramReduceMat::execute(PostProcessInfo& ppInfo)
- {
- // Set parameters
- mHistogramTex.set(ppInfo.histogramTex->texture);
- SPtr<PooledRenderTexture> eyeAdaptationRT = ppInfo.eyeAdaptationTex[ppInfo.lastEyeAdaptationTex];
- SPtr<Texture> eyeAdaptationTex;
- if (eyeAdaptationRT != nullptr) // Could be that this is the first run
- eyeAdaptationTex = eyeAdaptationRT->texture;
- else
- eyeAdaptationTex = Texture::WHITE;
- mEyeAdaptationTex.set(eyeAdaptationTex);
- Vector2I threadGroupCount = EyeAdaptHistogramMat::getThreadGroupCount(ppInfo.downsampledSceneTex->renderTexture);
- UINT32 numHistograms = threadGroupCount.x * threadGroupCount.y;
- gEyeAdaptHistogramReduceParamDef.gThreadGroupCount.set(mParamBuffer, numHistograms);
- // Set output
- mOutputDesc = POOLED_RENDER_TEXTURE_DESC::create2D(PF_FLOAT16_RGBA, EyeAdaptHistogramMat::HISTOGRAM_NUM_TEXELS, 2,
- TU_RENDERTARGET);
- // Render
- ppInfo.histogramReduceTex = GpuResourcePool::instance().get(mOutputDesc);
- RenderAPI& rapi = RenderAPI::instance();
- rapi.setRenderTarget(ppInfo.histogramReduceTex->renderTexture, 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);
- mOutput = ppInfo.histogramReduceTex->renderTexture;
- }
- void EyeAdaptHistogramReduceMat::release(PostProcessInfo& ppInfo)
- {
- GpuResourcePool::instance().release(ppInfo.histogramReduceTex);
- mOutput = nullptr;
- }
- EyeAdaptationParamDef gEyeAdaptationParamDef;
- EyeAdaptationMat::EyeAdaptationMat()
- {
- mParamBuffer = gEyeAdaptationParamDef.createBuffer();
- mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
- mParamsSet->getGpuParams()->getTextureParam(GPT_FRAGMENT_PROGRAM, "gHistogramTex", mReducedHistogramTex);
- }
- void EyeAdaptationMat::_initDefines(ShaderDefines& defines)
- {
- defines.set("THREADGROUP_SIZE_X", EyeAdaptHistogramMat::THREAD_GROUP_SIZE_X);
- defines.set("THREADGROUP_SIZE_Y", EyeAdaptHistogramMat::THREAD_GROUP_SIZE_Y);
- }
- void EyeAdaptationMat::execute(PostProcessInfo& ppInfo, float frameDelta)
- {
- bool texturesInitialized = ppInfo.eyeAdaptationTex[0] != nullptr && ppInfo.eyeAdaptationTex[1] != nullptr;
- if(!texturesInitialized)
- {
- POOLED_RENDER_TEXTURE_DESC outputDesc = POOLED_RENDER_TEXTURE_DESC::create2D(PF_FLOAT32_R, 1, 1, TU_RENDERTARGET);
- ppInfo.eyeAdaptationTex[0] = GpuResourcePool::instance().get(outputDesc);
- ppInfo.eyeAdaptationTex[1] = GpuResourcePool::instance().get(outputDesc);
- }
- ppInfo.lastEyeAdaptationTex = (ppInfo.lastEyeAdaptationTex + 1) % 2; // TODO - Do I really need two targets?
- // Set parameters
- mReducedHistogramTex.set(ppInfo.histogramReduceTex->texture);
- Vector2 histogramScaleAndOffset = EyeAdaptHistogramMat::getHistogramScaleOffset(ppInfo);
- const StandardPostProcessSettings& settings = *ppInfo.settings;
- Vector4 eyeAdaptationParams[3];
- eyeAdaptationParams[0].x = histogramScaleAndOffset.x;
- eyeAdaptationParams[0].y = histogramScaleAndOffset.y;
- float histogramPctHigh = Math::clamp01(settings.autoExposure.histogramPctHigh);
- eyeAdaptationParams[0].z = std::min(Math::clamp01(settings.autoExposure.histogramPctLow), histogramPctHigh);
- eyeAdaptationParams[0].w = histogramPctHigh;
- eyeAdaptationParams[1].x = std::min(settings.autoExposure.minEyeAdaptation, settings.autoExposure.maxEyeAdaptation);
- eyeAdaptationParams[1].y = settings.autoExposure.maxEyeAdaptation;
- eyeAdaptationParams[1].z = settings.autoExposure.eyeAdaptationSpeedUp;
- eyeAdaptationParams[1].w = settings.autoExposure.eyeAdaptationSpeedDown;
- eyeAdaptationParams[2].x = Math::pow(2.0f, settings.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
- SPtr<PooledRenderTexture> eyeAdaptationRT = ppInfo.eyeAdaptationTex[ppInfo.lastEyeAdaptationTex];
- RenderAPI& rapi = RenderAPI::instance();
- rapi.setRenderTarget(eyeAdaptationRT->renderTexture, FBT_DEPTH | FBT_STENCIL);
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- gRendererUtility().drawScreenQuad();
- rapi.setRenderTarget(nullptr);
- }
- CreateTonemapLUTParamDef gCreateTonemapLUTParamDef;
- WhiteBalanceParamDef gWhiteBalanceParamDef;
- CreateTonemapLUTMat::CreateTonemapLUTMat()
- {
- mParamBuffer = gCreateTonemapLUTParamDef.createBuffer();
- mWhiteBalanceParamBuffer = gWhiteBalanceParamDef.createBuffer();
- mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
- mParamsSet->setParamBlockBuffer("WhiteBalanceInput", mWhiteBalanceParamBuffer);
- SPtr<GpuParams> params = mParamsSet->getGpuParams();
- params->getLoadStoreTextureParam(GPT_COMPUTE_PROGRAM, "gOutputTex", mOutputTex);
- }
- void CreateTonemapLUTMat::_initDefines(ShaderDefines& defines)
- {
- defines.set("LUT_SIZE", LUT_SIZE);
- }
- void CreateTonemapLUTMat::execute(PostProcessInfo& ppInfo)
- {
- const StandardPostProcessSettings& settings = *ppInfo.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);
- // Set output
- POOLED_RENDER_TEXTURE_DESC outputDesc = POOLED_RENDER_TEXTURE_DESC::create3D(PF_R8G8B8A8,
- LUT_SIZE, LUT_SIZE, LUT_SIZE, TU_LOADSTORE);
- // Dispatch
- ppInfo.colorLUT = GpuResourcePool::instance().get(outputDesc);
- mOutputTex.set(ppInfo.colorLUT->texture);
- RenderAPI& rapi = RenderAPI::instance();
-
- gRendererUtility().setComputePass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- rapi.dispatchCompute(LUT_SIZE / 8, LUT_SIZE / 8, LUT_SIZE);
- }
- void CreateTonemapLUTMat::release(PostProcessInfo& ppInfo)
- {
- GpuResourcePool::instance().release(ppInfo.colorLUT);
- }
- TonemappingParamDef gTonemappingParamDef;
- template<bool GammaOnly, bool AutoExposure, bool MSAA>
- TonemappingMat<GammaOnly, AutoExposure, MSAA>::TonemappingMat()
- {
- mParamBuffer = gTonemappingParamDef.createBuffer();
- mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
- SPtr<GpuParams> params = mParamsSet->getGpuParams();
- params->getTextureParam(GPT_VERTEX_PROGRAM, "gEyeAdaptationTex", mEyeAdaptationTex);
- params->getTextureParam(GPT_FRAGMENT_PROGRAM, "gInputTex", mInputTex);
- if(!GammaOnly)
- params->getTextureParam(GPT_FRAGMENT_PROGRAM, "gColorLUT", mColorLUT);
- }
- template<bool GammaOnly, bool AutoExposure, bool MSAA>
- void TonemappingMat<GammaOnly, AutoExposure, MSAA>::_initDefines(ShaderDefines& defines)
- {
- if(GammaOnly)
- defines.set("GAMMA_ONLY", 1);
- if (AutoExposure)
- defines.set("AUTO_EXPOSURE", 1);
- defines.set("MSAA", MSAA ? 1 : 0);
- defines.set("LUT_SIZE", CreateTonemapLUTMat::LUT_SIZE);
- }
- template<bool GammaOnly, bool AutoExposure, bool MSAA>
- void TonemappingMat<GammaOnly, AutoExposure, MSAA>::execute(const SPtr<Texture>& sceneColor,
- const SPtr<RenderTarget>& outputRT, const Rect2& outputRect, PostProcessInfo& ppInfo)
- {
- const TextureProperties& texProps = sceneColor->getProperties();
- gTonemappingParamDef.gRawGamma.set(mParamBuffer, 1.0f / ppInfo.settings->gamma);
- gTonemappingParamDef.gManualExposureScale.set(mParamBuffer, Math::pow(2.0f, ppInfo.settings->exposureScale));
- gTonemappingParamDef.gNumSamples.set(mParamBuffer, texProps.getNumSamples());
- // Set parameters
- mInputTex.set(sceneColor);
- SPtr<Texture> colorLUT;
- if(ppInfo.colorLUT != nullptr)
- colorLUT = ppInfo.colorLUT->texture;
- mColorLUT.set(colorLUT);
- SPtr<Texture> eyeAdaptationTexture;
- if(ppInfo.eyeAdaptationTex[ppInfo.lastEyeAdaptationTex] != nullptr)
- eyeAdaptationTexture = ppInfo.eyeAdaptationTex[ppInfo.lastEyeAdaptationTex]->texture;
- mEyeAdaptationTex.set(eyeAdaptationTexture);
- // Render
- RenderAPI& rapi = RenderAPI::instance();
- rapi.setRenderTarget(outputRT);
- rapi.setViewport(outputRect);
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- if (MSAA)
- gRendererUtility().drawScreenQuad(Rect2(0.0f, 0.0f, (float)texProps.getWidth(), (float)texProps.getHeight()));
- else
- gRendererUtility().drawScreenQuad();
- }
- template class TonemappingMat<false, false, false>;
- template class TonemappingMat<false, false, true>;
- template class TonemappingMat<false, true, false>;
- template class TonemappingMat<false, true, true>;
- template class TonemappingMat<true, false, false>;
- template class TonemappingMat<true, false, true>;
- template class TonemappingMat<true, true, false>;
- template class TonemappingMat<true, true, true>;
- void TonemappingMaterials::execute(bool gammaOnly, bool autoExposure, bool MSAA, const SPtr<Texture>& sceneColor,
- const SPtr<RenderTarget>& outputRT, const Rect2& outputRect, PostProcessInfo& ppInfo)
- {
- if (gammaOnly)
- {
- if (autoExposure)
- {
- if (MSAA)
- mTTT.execute(sceneColor, outputRT, outputRect, ppInfo);
- else
- mTTF.execute(sceneColor, outputRT, outputRect, ppInfo);
- }
- else
- {
- if (MSAA)
- mTFT.execute(sceneColor, outputRT, outputRect, ppInfo);
- else
- mTFF.execute(sceneColor, outputRT, outputRect, ppInfo);
- }
- }
- else
- {
- if (autoExposure)
- {
- if (MSAA)
- mFTT.execute(sceneColor, outputRT, outputRect, ppInfo);
- else
- mFTF.execute(sceneColor, outputRT, outputRect, ppInfo);
- }
- else
- {
- if (MSAA)
- mFFT.execute(sceneColor, outputRT, outputRect, ppInfo);
- else
- mFFF.execute(sceneColor, outputRT, outputRect, ppInfo);
- }
- }
- }
- GaussianBlurParamDef gGaussianBlurParamDef;
- GaussianBlurMat::GaussianBlurMat()
- {
- mParamBuffer = gGaussianBlurParamDef.createBuffer();
- mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
- mParamsSet->getGpuParams()->getTextureParam(GPT_FRAGMENT_PROGRAM, "gInputTex", mInputTexture);
- }
- void GaussianBlurMat::_initDefines(ShaderDefines& defines)
- {
- defines.set("MAX_NUM_SAMPLES", MAX_BLUR_SAMPLES);
- }
- 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.getWidth(), dstProps.getHeight(), 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;
- template<bool Near, bool Far>
- GaussianDOFSeparateMat<Near, Far>::GaussianDOFSeparateMat()
- {
- mParamBuffer = gGaussianDOFParamDef.createBuffer();
- mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
- mParamsSet->getGpuParams()->getTextureParam(GPT_FRAGMENT_PROGRAM, "gColorTex", mColorTexture);
- mParamsSet->getGpuParams()->getTextureParam(GPT_FRAGMENT_PROGRAM, "gDepthTex", mDepthTexture);
- GpuParamSampState colorSampState;
- mParamsSet->getGpuParams()->getSamplerStateParam(GPT_FRAGMENT_PROGRAM, "gColorSamp", colorSampState);
- 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);
- colorSampState.set(samplerState);
- }
- template<bool Near, bool Far>
- void GaussianDOFSeparateMat<Near, Far>::_initDefines(ShaderDefines& defines)
- {
- defines.set("NEAR", Near ? 1 : 0);
- defines.set("FAR", Far ? 1 : 0);
- defines.set("NEAR_AND_FAR", (Near && Far) ? 1 : 0);
- }
- template<bool Near, bool Far>
- void GaussianDOFSeparateMat<Near, Far>::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);
- 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->setParamBlockBuffer("PerCamera", perView);
- RenderAPI& rapi = RenderAPI::instance();
- rapi.setRenderTarget(rt);
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- gRendererUtility().drawScreenQuad();
- }
- template <bool Near, bool Far>
- SPtr<PooledRenderTexture> GaussianDOFSeparateMat<Near, Far>::getOutput(UINT32 idx)
- {
- if (idx == 0)
- return mOutput0;
- else if (idx == 1)
- return mOutput1;
- return nullptr;
- }
- template<bool Near, bool Far>
- void GaussianDOFSeparateMat<Near, Far>::release()
- {
- if (mOutput0 != nullptr)
- GpuResourcePool::instance().release(mOutput0);
- if (mOutput1 != nullptr)
- GpuResourcePool::instance().release(mOutput1);
- }
- template class GaussianDOFSeparateMat<true, true>;
- template class GaussianDOFSeparateMat<true, false>;
- template class GaussianDOFSeparateMat<false, true>;
- template<bool Near, bool Far>
- GaussianDOFCombineMat<Near, Far>::GaussianDOFCombineMat()
- {
- mParamBuffer = gGaussianDOFParamDef.createBuffer();
- mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
- SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
- 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);
- }
- template<bool Near, bool Far>
- void GaussianDOFCombineMat<Near, Far>::_initDefines(ShaderDefines& defines)
- {
- defines.set("NEAR", Near ? 1 : 0);
- defines.set("FAR", Far ? 1 : 0);
- defines.set("NEAR_AND_FAR", (Near && Far) ? 1 : 0);
- }
- template<bool Near, bool Far>
- void GaussianDOFCombineMat<Near, Far>::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->setParamBlockBuffer("PerCamera", perView);
- RenderAPI& rapi = RenderAPI::instance();
- rapi.setRenderTarget(output);
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- gRendererUtility().drawScreenQuad();
- }
- template class GaussianDOFCombineMat<true, true>;
- template class GaussianDOFCombineMat<true, false>;
- template class GaussianDOFCombineMat<false, true>;
- void GaussianDOF::execute(const SPtr<Texture>& sceneColor, const SPtr<Texture>& sceneDepth,
- const SPtr<RenderTarget>& output, const RendererView& view, const DepthOfFieldSettings& settings)
- {
- bool near = settings.nearBlurAmount > 0.0f;
- bool far = settings.farBlurAmount > 0.0f;
- // This shouldn't have been called if both are false
- assert(near || far);
- IGaussianDOFSeparateMat* separateMat;
- IGaussianDOFCombineMat* combineMat;
- if (near && far)
- {
- separateMat = &mSeparateNF;
- combineMat = &mCombineNF;
- }
- else
- {
- if(near)
- {
- separateMat = &mSeparateN;
- combineMat = &mCombineN;
- }
- else
- {
- separateMat = &mSeparateF;
- combineMat = &mCombineF;
- }
- }
- separateMat->execute(sceneColor, sceneDepth, view, settings);
- SPtr<PooledRenderTexture> nearTex, farTex;
- if(near && far)
- {
- nearTex = separateMat->getOutput(0);
- farTex = separateMat->getOutput(1);
- }
- else
- {
- if (near)
- nearTex = separateMat->getOutput(0);
- else
- farTex = separateMat->getOutput(0);
- }
- // Blur the out of focus pixels
- // Note: Perhaps set up stencil so I can avoid performing blur on unused parts of the textures?
- const TextureProperties& texProps = nearTex ? nearTex->texture->getProperties() : farTex->texture->getProperties();
- POOLED_RENDER_TEXTURE_DESC tempTexDesc = POOLED_RENDER_TEXTURE_DESC::create2D(texProps.getFormat(),
- texProps.getWidth(), texProps.getHeight(), TU_RENDERTARGET);
- SPtr<PooledRenderTexture> tempTexture = GpuResourcePool::instance().get(tempTexDesc);
- SPtr<Texture> blurredNearTex;
- if(nearTex)
- {
- mBlur.execute(nearTex->texture, settings.nearBlurAmount, tempTexture->renderTexture);
- blurredNearTex = tempTexture->texture;
- }
- SPtr<Texture> blurredFarTex;
- if(farTex)
- {
- // If temporary texture is used up, re-use the original near texture for the blurred result
- if(blurredNearTex)
- {
- mBlur.execute(farTex->texture, settings.farBlurAmount, nearTex->renderTexture);
- blurredFarTex = nearTex->texture;
- }
- else // Otherwise just use the temporary
- {
- mBlur.execute(farTex->texture, settings.farBlurAmount, tempTexture->renderTexture);
- blurredFarTex = tempTexture->texture;
- }
- }
-
- combineMat->execute(sceneColor, blurredNearTex, blurredFarTex, sceneDepth, output, view, settings);
- separateMat->release();
- GpuResourcePool::instance().release(tempTexture);
- }
- bool GaussianDOF::requiresDOF(const DepthOfFieldSettings& settings)
- {
- bool near = settings.nearBlurAmount > 0.0f;
- bool far = settings.farBlurAmount > 0.0f;
- return settings.enabled && (near || far);
- }
-
- BuildHiZMat::BuildHiZMat()
- {
- SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gDepthTex", mInputTexture);
- }
- void BuildHiZMat::_initDefines(ShaderDefines& defines)
- {
- // 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();
- const TextureProperties& srcProps = source->getProperties();
- mInputTexture.set(source, TextureSurface(srcMip));
- rapi.setRenderTarget(output);
- rapi.setViewport(dstRect);
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- gRendererUtility().drawScreenQuad(srcRect);
- rapi.setViewport(Rect2(0, 0, 1, 1));
- }
- void BuildHiZ::execute(const RendererViewTargetData& viewInfo, const SPtr<Texture>& source, const SPtr<Texture>& output)
- {
- GpuResourcePool& pool = GpuResourcePool::instance();
- // First resolve if MSAA if required
- SPtr<PooledRenderTexture> resolvedDepth;
- Rect2 srcRect = viewInfo.nrmViewRect;
- // Generate first mip
- RENDER_TEXTURE_DESC rtDesc;
- rtDesc.colorSurfaces[0].texture = output;
- rtDesc.colorSurfaces[0].mipLevel = 0;
- // Make sure that 1 pixel in HiZ maps to a 2x2 block in source
- const TextureProperties& outProps = output->getProperties();
- Rect2 destRect(0, 0,
- Math::ceilToInt(viewInfo.viewRect.width / 2.0f) / (float)outProps.getWidth(),
- Math::ceilToInt(viewInfo.viewRect.height / 2.0f) / (float)outProps.getHeight());
- // If viewport size is odd, adjust UV
- srcRect.width += (viewInfo.viewRect.width % 2) * (1.0f / viewInfo.viewRect.width);
- srcRect.height += (viewInfo.viewRect.height % 2) * (1.0f / viewInfo.viewRect.height);
- SPtr<RenderTexture> rt = RenderTexture::create(rtDesc);
- mHiZMat.execute(source, 0, srcRect, destRect, rt);
- // Generate remaining mip levels
- for(UINT32 i = 1; i <= outProps.getNumMipmaps(); i++)
- {
- rtDesc.colorSurfaces[0].mipLevel = i;
- rt = RenderTexture::create(rtDesc);
- mHiZMat.execute(output, i - 1, destRect, destRect, rt);
- }
- if (resolvedDepth)
- pool.release(resolvedDepth);
- }
- POOLED_RENDER_TEXTURE_DESC BuildHiZ::getHiZTextureDesc(UINT32 viewWidth, UINT32 viewHeight)
- {
- UINT32 size = Bitwise::nextPow2(std::max(viewWidth, viewHeight));
- UINT32 numMips = PixelUtil::getMaxMipmaps(size, size, 1, PF_FLOAT32_R);
- size = 1 << numMips;
- // Note: Use the 32-bit buffer here as 16-bit causes too much banding (most of the scene gets assigned 4-5 different
- // depth values).
- // - When I add UNORM 16-bit format I should be able to switch to that
- return POOLED_RENDER_TEXTURE_DESC::create2D(PF_FLOAT32_R, size, size, TU_RENDERTARGET, 1, false, 1, numMips);
- }
- FXAAParamDef gFXAAParamDef;
- FXAAMat::FXAAMat()
- {
- mParamBuffer = gFXAAParamDef.createBuffer();
- mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
- mParamsSet->getGpuParams()->getTextureParam(GPT_FRAGMENT_PROGRAM, "gInputTex", mInputTexture);
- }
- void FXAAMat::_initDefines(ShaderDefines& defines)
- {
- // 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;
- template<bool UPSAMPLE, bool FINAL_PASS, int QUALITY>
- SSAOMat<UPSAMPLE, FINAL_PASS, QUALITY>::SSAOMat()
- {
- mParamBuffer = gSSAOParamDef.createBuffer();
- mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
- SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gDepthTex", mDepthTexture);
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gNormalsTex", mNormalsTexture);
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gDownsampledAO", mDownsampledAOTexture);
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gSetupAO", mSetupAOTexture);
- 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);
- gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gInputSamp", 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);
- gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gRandomSamp", randomSampState);
- }
- template<bool UPSAMPLE, bool FINAL_PASS, int QUALITY>
- void SSAOMat<UPSAMPLE, FINAL_PASS, QUALITY>::_initDefines(ShaderDefines& defines)
- {
- defines.set("MIX_WITH_UPSAMPLED", UPSAMPLE ? 1 : 0);
- defines.set("FINAL_AO", FINAL_PASS ? 1 : 0);
- defines.set("QUALITY", QUALITY);
- }
- template <bool UPSAMPLE, bool FINAL_PASS, int QUALITY>
- void SSAOMat<UPSAMPLE, FINAL_PASS, QUALITY>::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.getWidth();
- // 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);
-
- 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.getWidth() + rndWidth - 1) / rndWidth;
- UINT32 scaleHeight = (rtProps.getHeight() + rndHeight - 1) / rndHeight;
- Vector2 randomTileScale((float)scaleWidth, (float)scaleHeight);
- gSSAOParamDef.gRandomTileScale.set(mParamBuffer, randomTileScale);
- mSetupAOTexture.set(textures.aoSetup);
- if (FINAL_PASS)
- {
- mDepthTexture.set(textures.sceneDepth);
- mNormalsTexture.set(textures.sceneNormals);
- }
- if (UPSAMPLE)
- mDownsampledAOTexture.set(textures.aoDownsampled);
-
- mRandomTexture.set(textures.randomRotations);
- SPtr<GpuParamBlockBuffer> perView = view.getPerViewBuffer();
- mParamsSet->setParamBlockBuffer("PerCamera", perView);
- RenderAPI& rapi = RenderAPI::instance();
- rapi.setRenderTarget(destination);
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- gRendererUtility().drawScreenQuad();
- }
- SSAODownsampleParamDef gSSAODownsampleParamDef;
- SSAODownsampleMat::SSAODownsampleMat()
- {
- mParamBuffer = gSSAODownsampleParamDef.createBuffer();
- mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
- SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
- 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);
- gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gInputSamp", inputSampState);
- }
- void SSAODownsampleMat::_initDefines(ShaderDefines& defines)
- {
- // 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.getWidth();
- pixelSize.y = 1.0f / rtProps.getHeight();
- float scale = viewProps.viewRect.width / (float)rtProps.getWidth();
- 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->setParamBlockBuffer("PerCamera", perView);
- RenderAPI& rapi = RenderAPI::instance();
- rapi.setRenderTarget(destination);
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- gRendererUtility().drawScreenQuad();
- }
- SSAOBlurParamDef gSSAOBlurParamDef;
- template<bool HORIZONTAL>
- SSAOBlurMat<HORIZONTAL>::SSAOBlurMat()
- {
- mParamBuffer = gSSAOBlurParamDef.createBuffer();
- mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
- SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
- 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);
- gpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gInputSamp", inputSampState);
- }
- template<bool HORIZONTAL>
- void SSAOBlurMat<HORIZONTAL>::_initDefines(ShaderDefines& defines)
- {
- defines.set("DIR_HORZ", HORIZONTAL ? 1 : 0);
- }
- template<bool HORIZONTAL>
- void SSAOBlurMat<HORIZONTAL>::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 (HORIZONTAL)
- 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->setParamBlockBuffer("PerCamera", perView);
- RenderAPI& rapi = RenderAPI::instance();
- rapi.setRenderTarget(destination);
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- gRendererUtility().drawScreenQuad();
- }
- SSAO::SSAO()
- {
- mSSAORandomizationTex = generate4x4RandomizationTexture();
- }
- void SSAO::execute(const RendererView& view, const SPtr<RenderTexture>& destination,
- const AmbientOcclusionSettings& settings)
- {
- /** Maximum valid depth range within samples in a sample set. In meters. */
- static const float DEPTH_RANGE = 1.0f;
- const RendererViewProperties& viewProps = view.getProperties();
- SPtr<RenderTargets> renderTargets = view.getRenderTargets();
- SPtr<Texture> sceneDepth = renderTargets->get(RTT_ResolvedDepth);
- SPtr<Texture> sceneNormals = renderTargets->get(RTT_GBuffer, RT_COLOR1);
- const TextureProperties& normalsProps = sceneNormals->getProperties();
- SPtr<PooledRenderTexture> resolvedNormals;
- if(sceneNormals->getProperties().getNumSamples() > 1)
- {
- POOLED_RENDER_TEXTURE_DESC desc = POOLED_RENDER_TEXTURE_DESC::create2D(normalsProps.getFormat(),
- normalsProps.getWidth(), normalsProps.getHeight(), TU_RENDERTARGET);
- resolvedNormals = GpuResourcePool::instance().get(desc);
- RenderAPI::instance().setRenderTarget(resolvedNormals->renderTexture);
- gRendererUtility().blit(sceneNormals);
- sceneNormals = resolvedNormals->texture;
- }
- // Multiple downsampled AO levels are used to minimize cache trashing. Downsampled AO targets use larger radius,
- // whose contents are then blended with the higher level.
- UINT32 quality = settings.quality;
- UINT32 numDownsampleLevels = 0;
- if (quality > 1)
- numDownsampleLevels = 1;
- else if (quality > 2)
- numDownsampleLevels = 2;
- SPtr<PooledRenderTexture> setupTex0;
- if(numDownsampleLevels > 0)
- {
- Vector2I downsampledSize(
- std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.width, 2)),
- std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.height, 2))
- );
- POOLED_RENDER_TEXTURE_DESC desc = POOLED_RENDER_TEXTURE_DESC::create2D(PF_FLOAT16_RGBA, downsampledSize.x,
- downsampledSize.y, TU_RENDERTARGET);
- setupTex0 = GpuResourcePool::instance().get(desc);
- mDownsample.execute(view, sceneDepth, sceneNormals, setupTex0->renderTexture, DEPTH_RANGE);
- }
- SPtr<PooledRenderTexture> setupTex1;
- if(numDownsampleLevels > 1)
- {
- Vector2I downsampledSize(
- std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.width, 4)),
- std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.height, 4))
- );
- POOLED_RENDER_TEXTURE_DESC desc = POOLED_RENDER_TEXTURE_DESC::create2D(PF_FLOAT16_RGBA, downsampledSize.x,
- downsampledSize.y, TU_RENDERTARGET);
- setupTex1 = GpuResourcePool::instance().get(desc);
- mDownsample.execute(view, sceneDepth, sceneNormals, setupTex1->renderTexture, DEPTH_RANGE);
- }
- SSAOTextureInputs textures;
- textures.sceneDepth = sceneDepth;
- textures.sceneNormals = sceneNormals;
- textures.randomRotations = mSSAORandomizationTex;
- SPtr<PooledRenderTexture> downAOTex1;
- if(numDownsampleLevels > 1)
- {
- textures.aoSetup = setupTex1->texture;
- Vector2I downsampledSize(
- std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.width, 4)),
- std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.height, 4))
- );
- POOLED_RENDER_TEXTURE_DESC desc = POOLED_RENDER_TEXTURE_DESC::create2D(PF_R8, downsampledSize.x,
- downsampledSize.y, TU_RENDERTARGET);
- downAOTex1 = GpuResourcePool::instance().get(desc);
- executeSSAOMat(false, false, quality, view, textures, downAOTex1->renderTexture, settings);
- GpuResourcePool::instance().release(setupTex1);
- setupTex1 = nullptr;
- }
- SPtr<PooledRenderTexture> downAOTex0;
- if(numDownsampleLevels > 0)
- {
- textures.aoSetup = setupTex0->texture;
- if(downAOTex1)
- textures.aoDownsampled = downAOTex1->texture;
- Vector2I downsampledSize(
- std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.width, 2)),
- std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.height, 2))
- );
- POOLED_RENDER_TEXTURE_DESC desc = POOLED_RENDER_TEXTURE_DESC::create2D(PF_R8, downsampledSize.x,
- downsampledSize.y, TU_RENDERTARGET);
- downAOTex0 = GpuResourcePool::instance().get(desc);
- bool upsample = numDownsampleLevels > 1;
- executeSSAOMat(upsample, false, quality, view, textures, downAOTex0->renderTexture, settings);
- if(upsample)
- {
- GpuResourcePool::instance().release(downAOTex1);
- downAOTex1 = nullptr;
- }
- }
- {
- if(setupTex0)
- textures.aoSetup = setupTex0->texture;
- if(downAOTex0)
- textures.aoDownsampled = downAOTex0->texture;
- bool upsample = numDownsampleLevels > 0;
- executeSSAOMat(upsample, true, quality, view, textures, destination, settings);
- }
- if(resolvedNormals)
- {
- GpuResourcePool::instance().release(resolvedNormals);
- resolvedNormals = nullptr;
- }
- if(numDownsampleLevels > 0)
- {
- GpuResourcePool::instance().release(setupTex0);
- GpuResourcePool::instance().release(downAOTex0);
- }
- // Blur the output
- // Note: If I implement temporal AA then this can probably be avoided. I can instead jitter the sample offsets
- // each frame, and averaging them out should yield blurred AO.
- if(quality > 1) // On level 0 we don't blur at all, on level 1 we use the ad-hoc blur in shader
- {
- const RenderTargetProperties& rtProps = destination->getProperties();
- POOLED_RENDER_TEXTURE_DESC desc = POOLED_RENDER_TEXTURE_DESC::create2D(PF_R8, rtProps.getWidth(),
- rtProps.getHeight(), TU_RENDERTARGET);
- SPtr<PooledRenderTexture> blurIntermediateTex = GpuResourcePool::instance().get(desc);
- mBlurHorz.execute(view, destination->getColorTexture(0), sceneDepth, blurIntermediateTex->renderTexture,
- DEPTH_RANGE);
- mBlurVert.execute(view, blurIntermediateTex->texture, sceneDepth, destination, DEPTH_RANGE);
- GpuResourcePool::instance().release(blurIntermediateTex);
- }
- }
- void SSAO::executeSSAOMat(bool upsample, bool final, int quality, const RendererView& view,
- const SSAOTextureInputs& textures, const SPtr<RenderTexture>& destination, const AmbientOcclusionSettings& settings)
- {
- #define PICK_MATERIAL(QUALITY) \
- if(upsample) \
- if(final) \
- mSSAO_TT_##QUALITY.execute(view, textures, destination, settings); \
- else \
- mSSAO_TF_##QUALITY.execute(view, textures, destination, settings); \
- else \
- if(final) \
- mSSAO_FT_##QUALITY.execute(view, textures, destination, settings); \
- else \
- mSSAO_FF_##QUALITY.execute(view, textures, destination, settings); \
- switch(quality)
- {
- case 0:
- PICK_MATERIAL(0)
- break;
- case 1:
- PICK_MATERIAL(1)
- break;
- case 2:
- PICK_MATERIAL(2)
- break;
- case 3:
- PICK_MATERIAL(3)
- break;
- default:
- case 4:
- PICK_MATERIAL(4)
- break;
- }
- #undef PICK_MATERIAL
- }
- SPtr<Texture> SSAO::generate4x4RandomizationTexture() const
- {
- UINT32 mapping[16] = { 13, 5, 1, 9, 14, 3, 7, 11, 15, 2, 6, 12, 4, 8, 0, 10 };
- Vector2 bases[16];
- for (UINT32 i = 0; i < 16; ++i)
- {
- float angle = (mapping[i] / 16.0f) * Math::PI;
- bases[i].x = cos(angle);
- bases[i].y = sin(angle);
- }
- SPtr<PixelData> pixelData = PixelData::create(4, 4, 1, PF_R8G8);
- for(UINT32 y = 0; y < 4; ++y)
- for(UINT32 x = 0; x < 4; ++x)
- {
- UINT32 base = (y * 4) + x;
- Color color;
- color.r = bases[base].x * 0.5f + 0.5f;
- color.g = bases[base].y * 0.5f + 0.5f;
- pixelData->setColorAt(color, x, y);
- }
- return Texture::create(pixelData);
- }
- SSRStencilParamDef gSSRStencilParamDef;
- SSRStencilMat::SSRStencilMat()
- :mGBufferParams(mMaterial, mParamsSet)
- {
- mParamBuffer = gSSRStencilParamDef.createBuffer();
- mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
- }
- void SSRStencilMat::_initDefines(ShaderDefines& defines)
- {
- // Do nothing
- }
- void SSRStencilMat::execute(const RendererView& view, const ScreenSpaceReflectionsSettings& settings)
- {
- const RendererViewProperties& viewProps = view.getProperties();
- RenderTargets& renderTargets = *view.getRenderTargets();
- mGBufferParams.bind(renderTargets);
- Vector2 roughnessScaleBias = SSRTraceMat::calcRoughnessFadeScaleBias(settings.maxRoughness);
- gSSRStencilParamDef.gRoughnessScaleBias.set(mParamBuffer, roughnessScaleBias);
- SPtr<GpuParamBlockBuffer> perView = view.getPerViewBuffer();
- mParamsSet->setParamBlockBuffer("PerCamera", perView);
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- gRendererUtility().drawScreenQuad();
- }
- SSRTraceParamDef gSSRTraceParamDef;
- SSRTraceMat::SSRTraceMat()
- :mGBufferParams(mMaterial, mParamsSet)
- {
- mParamBuffer = gSSRTraceParamDef.createBuffer();
- mParamsSet->setParamBlockBuffer("Input", mParamBuffer);
- SPtr<GpuParams> gpuParams = mParamsSet->getGpuParams();
- gpuParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gSceneColor", mSceneColorTexture);
- }
- void SSRTraceMat::_initDefines(ShaderDefines& defines)
- {
- // Do nothing
- }
- void SSRTraceMat::execute(const RendererView& view, const ScreenSpaceReflectionsSettings& settings,
- const SPtr<RenderTexture>& destination)
- {
- const RendererViewProperties& viewProps = view.getProperties();
- RenderTargets& renderTargets = *view.getRenderTargets();
- mGBufferParams.bind(renderTargets);
- mSceneColorTexture.set(renderTargets.get(RTT_ResolvedSceneColor));
- SPtr<Texture> hiZ = renderTargets.get(RTT_HiZ);
- const TextureProperties& hiZProps = hiZ->getProperties();
-
- Rect2I viewRect = viewProps.viewRect;
- // Maps from NDC to UV [0, 1]
- Vector4 ndcToUVMapping;
- ndcToUVMapping.x = 0.5f;
- ndcToUVMapping.y = -0.5f;
- ndcToUVMapping.z = 0.5f;
- ndcToUVMapping.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))
- ndcToUVMapping.y = -ndcToUVMapping.y;
-
- // Maps from [0, 1] to are of HiZ where depth is stored in
- ndcToUVMapping.x *= (float)viewRect.width / hiZProps.getWidth();
- ndcToUVMapping.y *= (float)viewRect.height / hiZProps.getHeight();
-
- Vector2I bufferSize(hiZProps.getWidth(), hiZProps.getHeight());
- gSSRTraceParamDef.gHiZSize.set(mParamBuffer, bufferSize);
- gSSRTraceParamDef.gHiZNumMips.set(mParamBuffer, hiZProps.getNumMipmaps());
- gSSRTraceParamDef.gHiZUVMapping.set(mParamBuffer, ndcToUVMapping);
- SPtr<GpuParamBlockBuffer> perView = view.getPerViewBuffer();
- mParamsSet->setParamBlockBuffer("PerCamera", perView);
- rapi.setRenderTarget(destination);
- gRendererUtility().setPass(mMaterial);
- gRendererUtility().setPassParams(mParamsSet);
- 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;
- }
- void PostProcessing::postProcess(RendererView* viewInfo, const SPtr<RenderTargets>& renderTargets, float frameDelta)
- {
- auto& viewProps = viewInfo->getProperties();
- PostProcessInfo& ppInfo = viewInfo->getPPInfo();
- const StandardPostProcessSettings& settings = *ppInfo.settings;
- SPtr<Texture> sceneColor = renderTargets->get(RTT_SceneColor);
- Rect2 viewportRect = viewProps.nrmViewRect;
- bool hdr = viewProps.isHDR;
- bool msaa = viewProps.numSamples > 1;
- if(hdr && settings.enableAutoExposure)
- {
- mDownsample.execute(1, msaa, sceneColor, ppInfo);
- mEyeAdaptHistogram.execute(ppInfo);
- mDownsample.release(1, msaa, ppInfo);
- mEyeAdaptHistogramReduce.execute(ppInfo);
- mEyeAdaptHistogram.release(ppInfo);
- mEyeAdaptation.execute(ppInfo, frameDelta);
- mEyeAdaptHistogramReduce.release(ppInfo);
- }
- bool gammaOnly;
- bool autoExposure;
- if (hdr)
- {
- if (settings.enableTonemapping)
- {
- if (ppInfo.settingDirty) // Rebuild LUT if PP settings changed
- mCreateLUT.execute(ppInfo);
- gammaOnly = false;
- }
- else
- gammaOnly = true;
- autoExposure = settings.enableAutoExposure;
- }
- else
- {
- gammaOnly = true;
- autoExposure = false;
- }
- bool performDOF = GaussianDOF::requiresDOF(settings.depthOfField);
- SPtr<RenderTarget> tonemapTarget;
- if (!performDOF && !settings.enableFXAA)
- tonemapTarget = viewProps.target;
- else
- {
- renderTargets->allocate(RTT_ResolvedSceneColorSecondary);
- tonemapTarget = renderTargets->getRT(RTT_ResolvedSceneColorSecondary);
- }
- mTonemapping.execute(gammaOnly, autoExposure, msaa, sceneColor, tonemapTarget, viewportRect, ppInfo);
- if(performDOF)
- {
- SPtr<RenderTarget> dofTarget;
- // If DOF is the final effect, output to final target, otherwise use a temporary
- if (settings.enableFXAA)
- {
- renderTargets->allocate(RTT_ResolvedSceneColor);
- dofTarget = renderTargets->getRT(RTT_ResolvedSceneColor);
- }
- else
- dofTarget = viewProps.target;
- SPtr<Texture> sceneDepth = renderTargets->get(RTT_ResolvedDepth);
- mGaussianDOF.execute(renderTargets->get(RTT_ResolvedSceneColorSecondary), sceneDepth, dofTarget, *viewInfo,
- settings.depthOfField);
- renderTargets->release(RTT_ResolvedSceneColorSecondary);
- }
- if(settings.enableFXAA)
- {
- SPtr<Texture> fxaaSource;
- if (performDOF)
- fxaaSource = renderTargets->get(RTT_ResolvedSceneColor);
- else
- fxaaSource = renderTargets->get(RTT_ResolvedSceneColorSecondary);
- // Note: I could skip executing FXAA over DOF and motion blurred pixels
- mFXAA.execute(fxaaSource, viewProps.target);
- if (performDOF)
- renderTargets->release(RTT_ResolvedSceneColor);
- else
- renderTargets->release(RTT_ResolvedSceneColorSecondary);
- }
- if (ppInfo.settingDirty)
- ppInfo.settingDirty = false;
- }
- void PostProcessing::buildSSAO(const RendererView& view)
- {
- const SPtr<RenderTargets> renderTargets = view.getRenderTargets();
- const PostProcessInfo& ppInfo = view.getPPInfo();
- mSSAO.execute(view, renderTargets->getRT(RTT_AmbientOcclusion), ppInfo.settings->ambientOcclusion);
- }
- }}
|