From ddb598203e61f2c28d9da62d76771c4e8efea212 Mon Sep 17 00:00:00 2001 From: samcake Date: Thu, 31 Aug 2017 18:27:47 -0700 Subject: [PATCH] COmparing against fxaa --- .../render-utils/src/AntialiasingEffect.cpp | 4 +- .../render-utils/src/AntialiasingEffect.h | 27 +++--- libraries/render-utils/src/taa.slf | 9 ++ libraries/render-utils/src/taa.slh | 91 ++++++++++++++++-- libraries/render-utils/src/taa_blend.slf | 18 ++-- .../lib/styles-uit/HifiConstants.qmlc | Bin 51304 -> 51304 bytes .../utilities/render/antialiasing.qml | 10 +- 7 files changed, 132 insertions(+), 27 deletions(-) diff --git a/libraries/render-utils/src/AntialiasingEffect.cpp b/libraries/render-utils/src/AntialiasingEffect.cpp index b18d7f20ce..c1d622b8de 100644 --- a/libraries/render-utils/src/AntialiasingEffect.cpp +++ b/libraries/render-utils/src/AntialiasingEffect.cpp @@ -291,7 +291,9 @@ void Antialiasing::configure(const Config& config) { _params.edit().debugShowVelocityThreshold = config.debugShowVelocityThreshold; - _params.edit().debugX = config.debugX; + _params.edit().regionInfo.x = config.debugX; + _params.edit().regionInfo.z = config.debugFXAAX; + _params.edit().setDebug(config.debug); _params.edit().setShowDebugCursor(config.showCursorPixel); _params.edit().setDebugCursor(config.debugCursorTexcoord); diff --git a/libraries/render-utils/src/AntialiasingEffect.h b/libraries/render-utils/src/AntialiasingEffect.h index ab25642880..b981704602 100644 --- a/libraries/render-utils/src/AntialiasingEffect.h +++ b/libraries/render-utils/src/AntialiasingEffect.h @@ -88,6 +88,7 @@ class AntialiasingConfig : public render::Job::Config { Q_PROPERTY(bool debug MEMBER debug NOTIFY dirty) Q_PROPERTY(float debugX MEMBER debugX NOTIFY dirty) + Q_PROPERTY(float debugFXAAX MEMBER debugFXAAX NOTIFY dirty) Q_PROPERTY(float debugShowVelocityThreshold MEMBER debugShowVelocityThreshold NOTIFY dirty) Q_PROPERTY(bool showCursorPixel MEMBER showCursorPixel NOTIFY dirty) Q_PROPERTY(glm::vec2 debugCursorTexcoord MEMBER debugCursorTexcoord NOTIFY dirty) @@ -103,6 +104,7 @@ public: float velocityScale{ 1.0f }; float debugX{ 0.0f }; + float debugFXAAX{ 1.0f }; float debugShowVelocityThreshold{ 1.0f }; glm::vec2 debugCursorTexcoord{ 0.5f, 0.5f }; float debugOrbZoom{ 2.0f }; @@ -123,25 +125,26 @@ signals: #define GET_BIT(bitfield, bitIndex) ((bitfield) & (1 << (bitIndex))) struct TAAParams { - float debugX{ 0.0f }; + float nope{ 0.0f }; float blend{ 0.1f }; float velocityScale{ 1.0f }; float debugShowVelocityThreshold{ 1.0f }; - glm::ivec4 debug{ 0 }; + glm::ivec4 flags{ 0 }; glm::vec4 pixelInfo{ 0.5f, 0.5f, 2.0f, 0.0f }; + glm::vec4 regionInfo{ 0.0f, 0.0f, 1.0f, 0.0f }; - void setUnjitter(bool enabled) { SET_BIT(debug.y, 0, enabled); } - bool isUnjitter() const { return (bool)GET_BIT(debug.y, 0); } + void setUnjitter(bool enabled) { SET_BIT(flags.y, 0, enabled); } + bool isUnjitter() const { return (bool)GET_BIT(flags.y, 0); } - void setConstrainColor(bool enabled) { SET_BIT(debug.y, 1, enabled); } - bool isConstrainColor() const { return (bool)GET_BIT(debug.y, 1); } + void setConstrainColor(bool enabled) { SET_BIT(flags.y, 1, enabled); } + bool isConstrainColor() const { return (bool)GET_BIT(flags.y, 1); } - void setDebug(bool enabled) { SET_BIT(debug.x, 0, enabled); } - bool isDebug() const { return (bool) GET_BIT(debug.x, 0); } + void setDebug(bool enabled) { SET_BIT(flags.x, 0, enabled); } + bool isDebug() const { return (bool) GET_BIT(flags.x, 0); } - void setShowDebugCursor(bool enabled) { SET_BIT(debug.x, 1, enabled); } - bool showDebugCursor() const { return (bool)GET_BIT(debug.x, 1); } + void setShowDebugCursor(bool enabled) { SET_BIT(flags.x, 1, enabled); } + bool showDebugCursor() const { return (bool)GET_BIT(flags.x, 1); } void setDebugCursor(glm::vec2 debugCursor) { pixelInfo.x = debugCursor.x; pixelInfo.y = debugCursor.y; } glm::vec2 getDebugCursor() const { return glm::vec2(pixelInfo.x, pixelInfo.y); } @@ -149,8 +152,8 @@ struct TAAParams { void setDebugOrbZoom(float orbZoom) { pixelInfo.z = orbZoom; } float getDebugOrbZoom() const { return pixelInfo.z; } - void setShowJitterSequence(bool enabled) { SET_BIT(debug.x, 2, enabled); } - void setShowClosestFragment(bool enabled) { SET_BIT(debug.x, 3, enabled); } + void setShowJitterSequence(bool enabled) { SET_BIT(flags.x, 2, enabled); } + void setShowClosestFragment(bool enabled) { SET_BIT(flags.x, 3, enabled); } }; diff --git a/libraries/render-utils/src/taa.slf b/libraries/render-utils/src/taa.slf index 23c6b5e8cc..01c8942aee 100644 --- a/libraries/render-utils/src/taa.slf +++ b/libraries/render-utils/src/taa.slf @@ -20,6 +20,8 @@ layout(location = 0) out vec4 outFragColor; void main() { vec2 fragUV = varTexCoord0; + + vec2 texelSize = getInvWidthHeight(); vec2 fragJitterPix = taa_getCurrentJitterSample(); @@ -27,6 +29,13 @@ void main() { fragUV -= fragJitterPix * texelSize; } + // Debug region before debug or fxaa region X + float distToRegionFXAA = fragUV.x - taa_getRegionFXAA().x; + if (distToRegionFXAA > 0.0) { + outFragColor = vec4(taa_evalTXAA(fragUV), 1.0); + return; + } + vec3 nearFragUV = taa_findClosestFragment3x3(fragUV); vec2 fragVel = taa_fetchVelocityMap(nearFragUV.xy); diff --git a/libraries/render-utils/src/taa.slh b/libraries/render-utils/src/taa.slh index fff5ef15c5..fd4bcde377 100644 --- a/libraries/render-utils/src/taa.slh +++ b/libraries/render-utils/src/taa.slh @@ -47,12 +47,13 @@ uniform sampler2D nextMap; struct TAAParams { - float debugX; + float none; float blend; float motionScale; float debugShowVelocityThreshold; - ivec4 debug; + ivec4 flags; vec4 pixelInfo_orbZoom; + vec4 regionInfo; }; layout(std140) uniform taaParamsBuffer { @@ -62,23 +63,23 @@ layout(std140) uniform taaParamsBuffer { #define GET_BIT(bitfield, bitIndex) bool((bitfield) & (1 << (bitIndex))) bool taa_showDebugCursor() { - return GET_BIT(params.debug.x, 1); + return GET_BIT(params.flags.x, 1); } bool taa_showJitterSequence() { - return GET_BIT(params.debug.x, 2); + return GET_BIT(params.flags.x, 2); } bool taa_showClosestFragment() { - return GET_BIT(params.debug.x, 3); + return GET_BIT(params.flags.x, 3); } bool taa_unjitter() { - return GET_BIT(params.debug.y, 0); + return GET_BIT(params.flags.y, 0); } bool taa_constrainColor() { - return GET_BIT(params.debug.y, 1); + return GET_BIT(params.flags.y, 1); } vec2 taa_getDebugCursorTexcoord() { @@ -89,6 +90,14 @@ float taa_getDebugOrbZoom() { return params.pixelInfo_orbZoom.z; } +vec2 taa_getRegionDebug() { + return params.regionInfo.xy; +} + +vec2 taa_getRegionFXAA() { + return params.regionInfo.zw; +} + vec4 taa_fetchColor(sampler2D map, vec2 uv) { #if USE_YCOCG vec4 c = texture(map, uv); @@ -274,3 +283,71 @@ vec3 taa_getVelocityColorRelative(float velocityPixLength) { vec3 taa_getVelocityColorAboveThreshold(float velocityPixLength) { return colorRamp((velocityPixLength - params.debugShowVelocityThreshold)/params.debugShowVelocityThreshold); } + + +vec3 taa_evalTXAA(vec2 fragUV) { + + vec2 texelSize = getInvWidthHeight(); + + // filter width limit for dependent "two-tap" texture samples + float FXAA_SPAN_MAX = 8.0; + + // local contrast multiplier for performing AA + // higher = sharper, but setting this value too high will cause near-vertical and near-horizontal edges to fail + // see "fxaaQualityEdgeThreshold" + float FXAA_REDUCE_MUL = 1.0 / 8.0; + + // luminance threshold for processing dark colors + // see "fxaaQualityEdgeThresholdMin" + float FXAA_REDUCE_MIN = 1.0 / 128.0; + + // fetch raw RGB values for nearby locations + // sampling pattern is "five on a die" (each diagonal direction and the center) + // computing the coordinates for these texture reads could be moved to the vertex shader for speed if needed + vec3 rgbNW = texture(sourceMap, fragUV + (vec2(-1.0, -1.0) * texelSize)).xyz; + vec3 rgbNE = texture(sourceMap, fragUV + (vec2(+1.0, -1.0) * texelSize)).xyz; + vec3 rgbSW = texture(sourceMap, fragUV + (vec2(-1.0, +1.0) * texelSize)).xyz; + vec3 rgbSE = texture(sourceMap, fragUV + (vec2(+1.0, +1.0) * texelSize)).xyz; + vec3 rgbM = texture(sourceMap, fragUV).xyz; + + // convert RGB values to luminance + vec3 luma = vec3(0.299, 0.587, 0.114); + float lumaNW = dot(rgbNW, luma); + float lumaNE = dot(rgbNE, luma); + float lumaSW = dot(rgbSW, luma); + float lumaSE = dot(rgbSE, luma); + float lumaM = dot( rgbM, luma); + + // luma range of local neighborhood + float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE))); + float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE))); + + // direction perpendicular to local luma gradient + vec2 dir; + dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE)); + dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE)); + + // compute clamped direction offset for additional "two-tap" samples + // longer vector = blurry, shorter vector = sharp + float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) * (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN); + float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce); + dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX), + max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), dir * rcpDirMin)) * texelSize; + + // perform additional texture sampling perpendicular to gradient + vec3 rgbA = (1.0 / 2.0) * ( + texture(sourceMap, fragUV + dir * (1.0 / 3.0 - 0.5)).xyz + + texture(sourceMap, fragUV + dir * (2.0 / 3.0 - 0.5)).xyz); + vec3 rgbB = rgbA * (1.0 / 2.0) + (1.0 / 4.0) * ( + texture(sourceMap, fragUV + dir * (0.0 / 3.0 - 0.5)).xyz + + texture(sourceMap, fragUV + dir * (3.0 / 3.0 - 0.5)).xyz); + float lumaB = dot(rgbB, luma); + + // compare luma of new samples to the luma range of the original neighborhood + // if the new samples exceed this range, just use the first two samples instead of all four + if (lumaB < lumaMin || lumaB > lumaMax) { + return rgbA; + } else { + return rgbB; + } +} \ No newline at end of file diff --git a/libraries/render-utils/src/taa_blend.slf b/libraries/render-utils/src/taa_blend.slf index cd046506d0..ac8e18036a 100644 --- a/libraries/render-utils/src/taa_blend.slf +++ b/libraries/render-utils/src/taa_blend.slf @@ -125,7 +125,8 @@ void main(void) { float niceDotR2 = 4.0; - int sequenceLength = taa_getJitterSequenceLength(); + //int sequenceLength = taa_getJitterSequenceLength(); + int sequenceLength = SEQUENCE_LENGTH; for (int s = 0; s < sequenceLength; s++) { vec2 pixToSampleVec = jitterRegionPos - (vec2(0.5) + taa_getJitterSample(s)) * jitterRegionSize; float radius2 = (s == taa_getCurrentJitterIndex() ? 4.0 * niceDotR2 : niceDotR2); @@ -139,18 +140,23 @@ void main(void) { } } - // Debug region before debugX - if (varTexCoord0.x > params.debugX) { + // Debug region before debug or fxaa region X + float distToRegionDebug = varTexCoord0.x - taa_getRegionDebug().x; + float distToRegionFXAA = varTexCoord0.x - taa_getRegionFXAA().x; + if ((distToRegionFXAA < 0.0) && (distToRegionDebug > 0.0)) { return; } - // draw a region splitter - if (abs(varTexCoord0.x - params.debugX) < getInvWidthHeight().x) { + // draw region splitter + if ((abs(distToRegionDebug) < getInvWidthHeight().x) || (abs(distToRegionFXAA) < getInvWidthHeight().x)) { outFragColor.rgb = vec3(1.0, 1.0, 0.0); return; } - + if (distToRegionFXAA > 0.0) { + outFragColor.rgb = taa_evalTXAA(); + return; + } if (taa_showClosestFragment()) { vec3 fragUV = taa_findClosestFragment3x3(varTexCoord0); diff --git a/scripts/developer/utilities/lib/styles-uit/HifiConstants.qmlc b/scripts/developer/utilities/lib/styles-uit/HifiConstants.qmlc index 369d4444878f60a092189d56d24fd586c7a0c747..a11d8cf029dcb2a16ad80b66aa3ac76475f0233e 100644 GIT binary patch delta 104 zcmaDcf%(M*W}U(^ljM}dl0-oU1_nk>R))Potmk4G85nG;85nAuW^T0qCvEb_ZFTsZ z_rd2k>inow4Q60qNMp!k$YDrjuwu{$!(=eaogtHR)%vaax!s@3=B5a3=DU@Bo!xd$^}mDUF9x# zN&L=6ogbB&p$rTRX$+YRISi=`Rt)-Jm<(olFl2&df*4X63K;Sydsek?zEU;C7XT#3 B9RmOW diff --git a/scripts/developer/utilities/render/antialiasing.qml b/scripts/developer/utilities/render/antialiasing.qml index 8f9c02b8be..b4b20d48bd 100644 --- a/scripts/developer/utilities/render/antialiasing.qml +++ b/scripts/developer/utilities/render/antialiasing.qml @@ -93,13 +93,21 @@ Rectangle { } } ConfigSlider { - label: qsTr("Debug X") + label: qsTr("Debug Region <") integral: false config: Render.getConfig("RenderMainView.Antialiasing") property: "debugX" max: 1.0 min: 0.0 } + ConfigSlider { + label: qsTr("FXAA Region >") + integral: false + config: Render.getConfig("RenderMainView.Antialiasing") + property: "debugFXAAX" + max: 1.0 + min: 0.0 + } Row { CheckBox { text: "Jitter Sequence"