From 9f75d34bb87be08ac57b6eee49e030f836bc7b3a Mon Sep 17 00:00:00 2001 From: Ben Doherty Date: Wed, 8 Dec 2021 13:35:54 -0800 Subject: [PATCH] Rework color history in preparation for SSR (#4932) --- filament/src/PostProcessManager.cpp | 9 ++----- filament/src/PostProcessManager.h | 1 + filament/src/Renderer.cpp | 40 ++++++++++++++++++++++++++++- filament/src/details/Renderer.h | 3 +++ 4 files changed, 45 insertions(+), 8 deletions(-) diff --git a/filament/src/PostProcessManager.cpp b/filament/src/PostProcessManager.cpp index 026706b575..4fbef7c253 100644 --- a/filament/src/PostProcessManager.cpp +++ b/filament/src/PostProcessManager.cpp @@ -2177,19 +2177,17 @@ void PostProcessManager::prepareTaa(FrameHistory& frameHistory, FrameGraphId PostProcessManager::taa(FrameGraph& fg, FrameGraphId input, FrameHistory& frameHistory, + FrameGraphId colorHistory, TemporalAntiAliasingOptions const& taaOptions, ColorGradingConfig colorGradingConfig) noexcept { FrameHistoryEntry const& entry = frameHistory[0]; - FrameGraphId colorHistory; mat4f const* historyProjection = nullptr; - if (UTILS_UNLIKELY(!entry.color.handle)) { + if (UTILS_UNLIKELY(!colorHistory)) { // if we don't have a history yet, just use the current color buffer as history colorHistory = input; historyProjection = &frameHistory.getCurrent().projection; } else { - colorHistory = fg.import("TAA history", entry.colorDesc, - FrameGraphTexture::Usage::SAMPLEABLE, entry.color); historyProjection = &entry.projection; } @@ -2296,9 +2294,6 @@ FrameGraphId PostProcessManager::taa(FrameGraph& fg, colorGradingSubpass(driver, colorGradingConfig); } driver.endRenderPass(); - - // perform TAA here using colorHistory + input -> output - resources.detach(data.output, ¤t.color, ¤t.colorDesc); }); return colorGradingConfig.asSubpass ? taa->tonemappedOutput : taa->output; } diff --git a/filament/src/PostProcessManager.h b/filament/src/PostProcessManager.h index 111d235416..74c626cc9a 100644 --- a/filament/src/PostProcessManager.h +++ b/filament/src/PostProcessManager.h @@ -126,6 +126,7 @@ public: FrameGraphId taa(FrameGraph& fg, FrameGraphId input, FrameHistory& frameHistory, + FrameGraphId colorHistory, TemporalAntiAliasingOptions const& taaOptions, ColorGradingConfig colorGradingConfig) noexcept; diff --git a/filament/src/Renderer.cpp b/filament/src/Renderer.cpp index b03ea974f1..52d5628b2e 100644 --- a/filament/src/Renderer.cpp +++ b/filament/src/Renderer.cpp @@ -589,7 +589,25 @@ void FRenderer::renderJob(ArenaScope& arena, FView& view) { // TAA for color pass if (taaOptions.enabled) { - input = ppm.taa(fg, input, view.getFrameHistory(), taaOptions, colorGradingConfig); + input = ppm.taa(fg, input, view.getFrameHistory(), + getColorHistory(fg, view.getFrameHistory()), taaOptions, colorGradingConfig); + } + + struct ExportColorHistoryData { + FrameGraphId color; + }; + if (taaOptions.enabled) { + fg.addPass("Export color history", [&](FrameGraph::Builder& builder, + auto& data) { + // We need to use sideEffect here to ensure this pass won't be culled. The "output" of + // this pass is going to be used during the next frame as an "import". + builder.sideEffect(); + data.color = builder.sample(input); + }, [&view](FrameGraphResources const& resources, auto const& data, backend::DriverApi&) { + FrameHistory& frameHistory = view.getFrameHistory(); + FrameHistoryEntry& current = frameHistory.getCurrent(); + resources.detach(data.color, ¤t.color, ¤t.colorDesc); + }); } // -------------------------------------------------------------------------------------------- @@ -1248,6 +1266,26 @@ void FRenderer::initializeClearFlags() { | TargetBufferFlags::DEPTH_AND_STENCIL; } +FrameGraphId FRenderer::getColorHistory(FrameGraph& fg, + FrameHistory const& frameHistory) noexcept { + // Here we import the previous frame's color output and cache it in the blackboard. This ensures + // we only import it once per frame even across multiple calls to getColorHistory. + Blackboard& blackboard = fg.getBlackboard(); + + if (auto history = blackboard.get("colorHistory")) { + return history; + } + + FrameHistoryEntry const& entry = frameHistory[0]; + if (UTILS_UNLIKELY(!entry.color.handle)) { + return {}; + } + FrameGraphId colorHistory = fg.import("Color history", entry.colorDesc, + FrameGraphTexture::Usage::SAMPLEABLE, entry.color); + blackboard["colorHistory"] = colorHistory; + return colorHistory; +} + // ------------------------------------------------------------------------------------------------ // Trampoline calling into private implementation // ------------------------------------------------------------------------------------------------ diff --git a/filament/src/details/Renderer.h b/filament/src/details/Renderer.h index 13626266f4..026915e8ad 100644 --- a/filament/src/details/Renderer.h +++ b/filament/src/details/Renderer.h @@ -187,6 +187,9 @@ private: return clock::now() - getUserEpoch(); } + static FrameGraphId getColorHistory(FrameGraph& fg, + FrameHistory const& frameHistory) noexcept; + // keep a reference to our engine FEngine& mEngine; FrameSkipper mFrameSkipper;