| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
- //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
- #include "BsPostProcessing.h"
- #include "BsRenderTexture.h"
- #include "BsRenderTexturePool.h"
- #include "BsRendererUtility.h"
- #include "BsTextureManager.h"
- #include "BsCamera.h"
- namespace BansheeEngine
- {
- DownsampleMat::DownsampleMat()
- {
- mMaterial->setParamBlockBuffer("Input", mParams.getBuffer());
- mInputTexture = mMaterial->getParamTexture("gInputTex");
- mInvTexSize = mMaterial->getParamVec2("gInvTexSize");
- }
- void DownsampleMat::_initDefines(ShaderDefines& defines)
- {
- // Do nothing
- }
- void DownsampleMat::execute(const SPtr<RenderTextureCore>& target, PostProcessInfo& ppInfo)
- {
- // Set parameters
- SPtr<TextureCore> colorTexture = target->getBindableColorTexture();
- mInputTexture.set(colorTexture);
- const RenderTextureProperties& rtProps = target->getProperties();
- Vector2 invTextureSize(1.0f / rtProps.getWidth(), 1.0f / rtProps.getHeight());
- mParams.gInvTexSize.set(invTextureSize);
- // Set output
- const TextureProperties& colorProps = colorTexture->getProperties();
- UINT32 width = std::max(1, Math::ceilToInt(colorProps.getWidth() * 0.5f));
- UINT32 height = std::max(1, Math::ceilToInt(colorProps.getHeight() * 0.5f));
- mOutputDesc = POOLED_RENDER_TEXTURE_DESC::create2D(colorProps.getFormat(), width, height, TU_RENDERTARGET);
- // Render
- ppInfo.downsampledSceneTex = RenderTexturePool::instance().get(mOutputDesc);
- RenderAPICore& rapi = RenderAPICore::instance();
- rapi.setRenderTarget(ppInfo.downsampledSceneTex->renderTexture, true);
- gRendererUtility().setPass(mMaterial, 0);
- gRendererUtility().drawScreenQuad();
- rapi.setRenderTarget(nullptr);
- mOutput = ppInfo.downsampledSceneTex->renderTexture;
- }
- void DownsampleMat::release(PostProcessInfo& ppInfo)
- {
- RenderTexturePool::instance().release(ppInfo.downsampledSceneTex);
- mOutput = nullptr;
- }
- EyeAdaptHistogramMat::EyeAdaptHistogramMat()
- {
- mMaterial->setParamBlockBuffer("Input", mParams.getBuffer());
- mSceneColor = mMaterial->getParamTexture("gSceneColorTex");
- mOutputTex = mMaterial->getParamLoadStoreTexture("gOutputTex");
- }
- 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<RenderTextureCore> 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() };
- mParams.gHistogramParams.set(getHistogramScaleOffset(ppInfo));
- mParams.gPixelOffsetAndSize.set(Vector4I(offsetAndSize));
- Vector2I threadGroupCount = getThreadGroupCount(target);
- mParams.gThreadGroupCount.set(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 = RenderTexturePool::instance().get(mOutputDesc);
- mOutputTex.set(ppInfo.histogramTex->texture);
- RenderAPICore& rapi = RenderAPICore::instance();
- gRendererUtility().setComputePass(mMaterial);
- rapi.dispatchCompute(threadGroupCount.x, threadGroupCount.y);
- // Note: This is ugly, add a better way to clear load/store textures?
- TextureSurface blankSurface;
- rapi.setLoadStoreTexture(GPT_COMPUTE_PROGRAM, 0, false, nullptr, blankSurface);
- mOutput = ppInfo.histogramTex->renderTexture;
- }
- void EyeAdaptHistogramMat::release(PostProcessInfo& ppInfo)
- {
- RenderTexturePool::instance().release(ppInfo.histogramTex);
- mOutput = nullptr;
- }
- Vector2I EyeAdaptHistogramMat::getThreadGroupCount(const SPtr<RenderTextureCore>& 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 PostProcessSettings& 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);
- }
- EyeAdaptHistogramReduceMat::EyeAdaptHistogramReduceMat()
- {
- mMaterial->setParamBlockBuffer("Input", mParams.getBuffer());
- mHistogramTex = mMaterial->getParamTexture("gHistogramTex");
- mEyeAdaptationTex = mMaterial->getParamTexture("gEyeAdaptationTex");
- }
- 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<TextureCore> eyeAdaptationTex;
- if (eyeAdaptationRT != nullptr) // Could be that this is the first run
- eyeAdaptationTex = eyeAdaptationRT->texture;
- else
- eyeAdaptationTex = TextureCore::WHITE;
- mEyeAdaptationTex.set(eyeAdaptationTex);
- Vector2I threadGroupCount = EyeAdaptHistogramMat::getThreadGroupCount(ppInfo.downsampledSceneTex->renderTexture);
- UINT32 numHistograms = threadGroupCount.x * threadGroupCount.y;
- mParams.gThreadGroupCount.set(numHistograms);
- // Set output
- mOutputDesc = POOLED_RENDER_TEXTURE_DESC::create2D(PF_FLOAT16_RGBA, EyeAdaptHistogramMat::HISTOGRAM_NUM_TEXELS, 2,
- TU_RENDERTARGET);
- // Render
- ppInfo.histogramReduceTex = RenderTexturePool::instance().get(mOutputDesc);
- RenderAPICore& rapi = RenderAPICore::instance();
- rapi.setRenderTarget(ppInfo.histogramReduceTex->renderTexture, true);
- gRendererUtility().setPass(mMaterial, 0);
- 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)
- {
- RenderTexturePool::instance().release(ppInfo.histogramReduceTex);
- mOutput = nullptr;
- }
- EyeAdaptationMat::EyeAdaptationMat()
- {
- mMaterial->setParamBlockBuffer("Input", mParams.getBuffer());
- mReducedHistogramTex = mMaterial->getParamTexture("gHistogramTex");
- }
- 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] = RenderTexturePool::instance().get(outputDesc);
- ppInfo.eyeAdaptationTex[1] = RenderTexturePool::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 PostProcessSettings& 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;
- mParams.gEyeAdaptationParams.set(eyeAdaptationParams[0], 0);
- mParams.gEyeAdaptationParams.set(eyeAdaptationParams[1], 1);
- mParams.gEyeAdaptationParams.set(eyeAdaptationParams[2], 2);
- // Render
- SPtr<PooledRenderTexture> eyeAdaptationRT = ppInfo.eyeAdaptationTex[ppInfo.lastEyeAdaptationTex];
- RenderAPICore& rapi = RenderAPICore::instance();
- rapi.setRenderTarget(eyeAdaptationRT->renderTexture, true);
- gRendererUtility().setPass(mMaterial, 0);
- gRendererUtility().drawScreenQuad();
- rapi.setRenderTarget(nullptr);
- }
- CreateTonemapLUTMat::CreateTonemapLUTMat()
- {
- mMaterial->setParamBlockBuffer("Input", mParams.getBuffer());
- mMaterial->setParamBlockBuffer("WhiteBalanceInput", mWhiteBalanceParams.getBuffer());
- }
- void CreateTonemapLUTMat::_initDefines(ShaderDefines& defines)
- {
- defines.set("LUT_SIZE", LUT_SIZE);
- }
- void CreateTonemapLUTMat::execute(PostProcessInfo& ppInfo)
- {
- // Set parameters
- mParams.gGammaAdjustment.set(2.2f / ppInfo.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)
- mParams.gGammaCorrectionType.set(0);
- Vector4 tonemapParams[2];
- tonemapParams[0].x = ppInfo.settings.tonemapping.filmicCurveShoulderStrength;
- tonemapParams[0].y = ppInfo.settings.tonemapping.filmicCurveLinearStrength;
- tonemapParams[0].z = ppInfo.settings.tonemapping.filmicCurveLinearAngle;
- tonemapParams[0].w = ppInfo.settings.tonemapping.filmicCurveToeStrength;
- tonemapParams[1].x = ppInfo.settings.tonemapping.filmicCurveToeNumerator;
- tonemapParams[1].y = ppInfo.settings.tonemapping.filmicCurveToeDenominator;
- tonemapParams[1].z = ppInfo.settings.tonemapping.filmicCurveLinearWhitePoint;
- tonemapParams[1].w = 0.0f; // Unused
- mParams.gTonemapParams.set(tonemapParams[0], 0);
- mParams.gTonemapParams.set(tonemapParams[1], 1);
- // Set color grading params
- mParams.gSaturation.set(ppInfo.settings.colorGrading.saturation);
- mParams.gContrast.set(ppInfo.settings.colorGrading.contrast);
- mParams.gGain.set(ppInfo.settings.colorGrading.gain);
- mParams.gOffset.set(ppInfo.settings.colorGrading.offset);
- // Set white balance params
- mWhiteBalanceParams.gWhiteTemp.set(ppInfo.settings.whiteBalance.temperature);
- mWhiteBalanceParams.gWhiteOffset.set(ppInfo.settings.whiteBalance.tint);
- // Set output
- POOLED_RENDER_TEXTURE_DESC outputDesc = POOLED_RENDER_TEXTURE_DESC::create3D(PF_B8G8R8X8,
- LUT_SIZE, LUT_SIZE, LUT_SIZE, TU_RENDERTARGET);
- // Render
- ppInfo.colorLUT = RenderTexturePool::instance().get(outputDesc);
- RenderAPICore& rapi = RenderAPICore::instance();
- rapi.setRenderTarget(ppInfo.colorLUT->renderTexture);
- gRendererUtility().setPass(mMaterial, 0);
- gRendererUtility().drawScreenQuad(LUT_SIZE);
- }
- void CreateTonemapLUTMat::release(PostProcessInfo& ppInfo)
- {
- RenderTexturePool::instance().release(ppInfo.colorLUT);
- }
- template<bool GammaOnly, bool AutoExposure>
- TonemappingMat<GammaOnly, AutoExposure>::TonemappingMat()
- {
- mMaterial->setParamBlockBuffer("Input", mParams.getBuffer());
- mInputTex = mMaterial->getParamTexture("gInputTex");
- mColorLUT = mMaterial->getParamTexture("gColorLUT");
- mEyeAdaptationTex = mMaterial->getParamTexture("gEyeAdaptationTex");
- }
- template<bool GammaOnly, bool AutoExposure>
- void TonemappingMat<GammaOnly, AutoExposure>::_initDefines(ShaderDefines& defines)
- {
- if(GammaOnly)
- defines.set("GAMMA_ONLY", 1);
- if (AutoExposure)
- defines.set("AUTO_EXPOSURE", 1);
- defines.set("LUT_SIZE", CreateTonemapLUTMat::LUT_SIZE);
- }
- template<bool GammaOnly, bool AutoExposure>
- void TonemappingMat<GammaOnly, AutoExposure>::execute(const SPtr<RenderTextureCore>& sceneColor, const SPtr<ViewportCore>& outputViewport,
- PostProcessInfo& ppInfo)
- {
- mParams.gRawGamma.set(1.0f / ppInfo.settings.gamma);
- mParams.gManualExposureScale.set(Math::pow(2.0f, ppInfo.settings.exposureScale));
- // Set parameters
- SPtr<TextureCore> colorTexture = sceneColor->getBindableColorTexture();
- mInputTex.set(colorTexture);
- SPtr<TextureCore> colorLUT;
- if(ppInfo.colorLUT != nullptr)
- colorLUT = ppInfo.colorLUT->texture;
- mColorLUT.set(colorLUT);
- SPtr<TextureCore> eyeAdaptationTexture;
- if(ppInfo.eyeAdaptationTex[ppInfo.lastEyeAdaptationTex] != nullptr)
- eyeAdaptationTexture = ppInfo.eyeAdaptationTex[ppInfo.lastEyeAdaptationTex]->texture;
- mEyeAdaptationTex.set(eyeAdaptationTexture);
- // Render
- RenderAPICore& rapi = RenderAPICore::instance();
- SPtr<RenderTargetCore> target = outputViewport->getTarget();
- rapi.setRenderTarget(target);
- rapi.setViewport(outputViewport->getNormArea());
- gRendererUtility().setPass(mMaterial, 0);
- gRendererUtility().drawScreenQuad();
- }
- template class TonemappingMat<true, true>;
- template class TonemappingMat<false, true>;
- template class TonemappingMat<true, false>;
- template class TonemappingMat<false, false>;
- void PostProcessing::postProcess(const SPtr<RenderTextureCore>& sceneColor, const CameraCore* camera,
- PostProcessInfo& ppInfo, float frameDelta)
- {
- SPtr<ViewportCore> outputViewport = camera->getViewport();
- bool hdr = camera->getFlags().isSet(CameraFlag::HDR);
- if(hdr && ppInfo.settings.enableAutoExposure)
- {
- mDownsample.execute(sceneColor, ppInfo);
- mEyeAdaptHistogram.execute(ppInfo);
- mDownsample.release(ppInfo);
- mEyeAdaptHistogramReduce.execute(ppInfo);
- mEyeAdaptHistogram.release(ppInfo);
- mEyeAdaptation.execute(ppInfo, frameDelta);
- mEyeAdaptHistogramReduce.release(ppInfo);
- }
- if (hdr && ppInfo.settings.enableTonemapping)
- {
- if (ppInfo.settingDirty) // Rebuild LUT if PP settings changed
- mCreateLUT.execute(ppInfo);
- if (ppInfo.settings.enableAutoExposure)
- mTonemapping_AE.execute(sceneColor, outputViewport, ppInfo);
- else
- mTonemapping.execute(sceneColor, outputViewport, ppInfo);
- }
- else
- {
- if (hdr && ppInfo.settings.enableAutoExposure)
- mTonemapping_AE_GO.execute(sceneColor, outputViewport, ppInfo);
- else
- mTonemapping_GO.execute(sceneColor, outputViewport, ppInfo);
- }
- if (ppInfo.settingDirty)
- ppInfo.settingDirty = false;
- // TODO - External code depends on the main RT being bound when this exits, make this clearer
- }
- }
|