From 8155bf04a84a738b68c5d2aa030ab1a6a5bcc1fb Mon Sep 17 00:00:00 2001 From: Pixelflinger Date: Fri, 7 Aug 2020 22:05:07 -0700 Subject: [PATCH] Implement variance clamping variance clamping further shrinks the min/max AABB --- .../filament-android/src/main/cpp/View.cpp | 2 +- filament/src/materials/antiAliasing/taa.mat | 36 ++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/android/filament-android/src/main/cpp/View.cpp b/android/filament-android/src/main/cpp/View.cpp index 58ec085f13..6ed7190a02 100644 --- a/android/filament-android/src/main/cpp/View.cpp +++ b/android/filament-android/src/main/cpp/View.cpp @@ -275,5 +275,5 @@ Java_com_google_android_filament_View_nSetTemporalAntiAliasingOptions(JNIEnv *, jlong nativeView, jfloat feedback, jfloat filterWidth, jboolean enabled) { View* view = (View*) nativeView; view->setTemporalAntiAliasingOptions({ - .feedback = feedback, .filterWidth = filterWidth, .enabled = (bool) enabled}); + .filterWidth = filterWidth, .feedback = feedback, .enabled = (bool) enabled}); } diff --git a/filament/src/materials/antiAliasing/taa.mat b/filament/src/materials/antiAliasing/taa.mat index 81199999f3..3c905b56ec 100644 --- a/filament/src/materials/antiAliasing/taa.mat +++ b/filament/src/materials/antiAliasing/taa.mat @@ -52,6 +52,18 @@ fragment { #define TEXTURE_SPACE_DN -1 #endif +/* Clipping box type */ + +// Neighborhood AABB +#define BOX_TYPE_AABB 0 +// Further clip by Neighborhood Variance +#define BOX_TYPE_VARIANCE 1 + +// High VARIANCE_GAMMA [0.75, 1.25] increases ghosting artefact, lower values increases jittering +#define VARIANCE_GAMMA 1.0 + +/* Clipping algorithm */ + // accurate box clipping #define BOX_CLIPPING_ACCURATE 0 // clamping instead of clipping @@ -59,22 +71,28 @@ fragment { // no clipping (for debugging only) #define BOX_CLIPPING_NONE 2 +/* Configuration mobile/desktop */ + #if defined(TARGET_MOBILE) #define BOX_CLIPPING BOX_CLIPPING_ACCURATE +#define BOX_TYPE BOX_TYPE_VARIANCE #define USE_YCoCg 0 #define PREVENT_FLICKERING 0 #define FILTER_HISTORY 0 #else #define BOX_CLIPPING BOX_CLIPPING_ACCURATE +#define BOX_TYPE BOX_TYPE_VARIANCE #define USE_YCoCg 1 #define PREVENT_FLICKERING 0 // FIXME: thin lines disapear #define FILTER_HISTORY 1 #endif -// disable for debugging only +/* debugging helper */ + #define HISTORY_REPROJECTION 1 #define FILTER_INPUT 1 + float luma(const vec3 color) { #if USE_YCoCg return color.x; @@ -253,6 +271,22 @@ void postProcess(inout PostProcessInputs postProcess) { vec3 boxmin = (box5min + box9min) * 0.5; vec3 boxmax = (box5max + box9max) * 0.5; +#if BOX_TYPE == BOX_TYPE_VARIANCE + // "An Excursion in Temporal Supersampling" by Marco Salvi + vec3 m0 = s[4]; + vec3 m1 = s[4] * s[4]; + // we use only 5 samples instead of all 9 + for (int i = 1 ; i < 9 ; i+=2) { + m0 += s[i]; + m1 += s[i] * s[i]; + } + vec3 a0 = m0 * (1.0 / 5.0); + vec3 a1 = m1 * (1.0 / 5.0); + vec3 sigma = sqrt(a1 - a0 * a0); + boxmin = min(boxmin, a0 - VARIANCE_GAMMA * sigma); + boxmax = max(boxmax, a0 + VARIANCE_GAMMA * sigma); +#endif + // history clamping history = clipToBox(BOX_CLIPPING, boxmin, boxmax, filtered, history);