From 4cdbefc440453f99a07cc11fbca5479918d38fd3 Mon Sep 17 00:00:00 2001 From: samcake Date: Tue, 22 Aug 2017 17:44:14 -0700 Subject: [PATCH] MOre debugging, adding the find the closest fragment 3x3 --- .../render-utils/src/AntialiasingEffect.cpp | 34 +++++++---- .../render-utils/src/AntialiasingEffect.h | 4 +- libraries/render-utils/src/taa.slf | 14 +++-- libraries/render-utils/src/taa.slh | 59 +++++++++++++++++-- libraries/render-utils/src/taa_blend.slf | 45 ++++++++++---- .../src/velocityBuffer_cameraMotion.slf | 3 +- .../utilities/render/antialiasing.qml | 14 +++-- 7 files changed, 129 insertions(+), 44 deletions(-) diff --git a/libraries/render-utils/src/AntialiasingEffect.cpp b/libraries/render-utils/src/AntialiasingEffect.cpp index 95c66a3ca9..fd576f89d0 100644 --- a/libraries/render-utils/src/AntialiasingEffect.cpp +++ b/libraries/render-utils/src/AntialiasingEffect.cpp @@ -182,7 +182,8 @@ const int AntialiasingPass_FrameTransformSlot = 1; const int AntialiasingPass_HistoryMapSlot = 0; const int AntialiasingPass_SourceMapSlot = 1; const int AntialiasingPass_VelocityMapSlot = 2; -const int AntialiasingPass_CurrentMapSlot = 3; +const int AntialiasingPass_NextMapSlot = 3; +const int AntialiasingPass_DepthMapSlot = 3; Antialiasing::Antialiasing() { @@ -209,8 +210,10 @@ const gpu::PipelinePointer& Antialiasing::getAntialiasingPipeline() { slotBindings.insert(gpu::Shader::Binding(std::string("deferredFrameTransformBuffer"), AntialiasingPass_FrameTransformSlot)); slotBindings.insert(gpu::Shader::Binding(std::string("historyMap"), AntialiasingPass_HistoryMapSlot)); - slotBindings.insert(gpu::Shader::Binding(std::string("colorMap"), AntialiasingPass_SourceMapSlot)); + slotBindings.insert(gpu::Shader::Binding(std::string("sourceMap"), AntialiasingPass_SourceMapSlot)); slotBindings.insert(gpu::Shader::Binding(std::string("velocityMap"), AntialiasingPass_VelocityMapSlot)); + slotBindings.insert(gpu::Shader::Binding(std::string("depthMap"), AntialiasingPass_DepthMapSlot)); + gpu::Shader::makeProgram(*program, slotBindings); @@ -232,7 +235,7 @@ const gpu::PipelinePointer& Antialiasing::getBlendPipeline() { gpu::ShaderPointer program = gpu::Shader::createProgram(vs, ps); gpu::Shader::BindingSet slotBindings; - slotBindings.insert(gpu::Shader::Binding(std::string("colorTexture"), AntialiasingPass_CurrentMapSlot)); + slotBindings.insert(gpu::Shader::Binding(std::string("colorTexture"), AntialiasingPass_NextMapSlot)); gpu::Shader::makeProgram(*program, slotBindings); @@ -256,10 +259,11 @@ const gpu::PipelinePointer& Antialiasing::getDebugBlendPipeline() { slotBindings.insert(gpu::Shader::Binding(std::string("taaParamsBuffer"), AntialiasingPass_ParamsSlot)); slotBindings.insert(gpu::Shader::Binding(std::string("deferredFrameTransformBuffer"), AntialiasingPass_FrameTransformSlot)); - slotBindings.insert(gpu::Shader::Binding(std::string("currentMap"), AntialiasingPass_CurrentMapSlot)); + slotBindings.insert(gpu::Shader::Binding(std::string("nextMap"), AntialiasingPass_NextMapSlot)); slotBindings.insert(gpu::Shader::Binding(std::string("historyMap"), AntialiasingPass_HistoryMapSlot)); - slotBindings.insert(gpu::Shader::Binding(std::string("colorMap"), AntialiasingPass_SourceMapSlot)); + slotBindings.insert(gpu::Shader::Binding(std::string("sourceMap"), AntialiasingPass_SourceMapSlot)); slotBindings.insert(gpu::Shader::Binding(std::string("velocityMap"), AntialiasingPass_VelocityMapSlot)); + slotBindings.insert(gpu::Shader::Binding(std::string("depthMap"), AntialiasingPass_DepthMapSlot)); gpu::Shader::makeProgram(*program, slotBindings); @@ -282,9 +286,10 @@ void Antialiasing::configure(const Config& config) { _params.edit().debugCursor.x = config.showCursorPixel; + auto orbZoom = (_params->pixelInfo.z); auto cursorPos = glm::vec2(_params->pixelInfo); - if (cursorPos != config.debugCursorTexcoord) { - _params.edit().pixelInfo = glm::vec4(config.debugCursorTexcoord, 0.0f, 0.0f); + if (cursorPos != config.debugCursorTexcoord || (orbZoom != config.debugOrbZoom)) { + _params.edit().pixelInfo = glm::vec4(config.debugCursorTexcoord, config.debugOrbZoom, 0.0f); } } @@ -322,8 +327,8 @@ void Antialiasing::run(const render::RenderContextPointer& renderContext, const _antialiasingBuffer[i]->setRenderBuffer(0, _antialiasingTexture[i]); } } - int currentFrame = (_currentFrame++) % 2; - int prevFrame = (currentFrame + 1) % 2; + int nextFrame = (_currentFrame++) % 2; + int prevFrame = (nextFrame + 1) % 2; gpu::doInBatch(args->_context, [&](gpu::Batch& batch) { batch.enableStereo(false); @@ -334,31 +339,34 @@ void Antialiasing::run(const render::RenderContextPointer& renderContext, const batch.setResourceTexture(AntialiasingPass_HistoryMapSlot, _antialiasingTexture[prevFrame]); batch.setResourceTexture(AntialiasingPass_SourceMapSlot, sourceBuffer->getRenderBuffer(0)); batch.setResourceTexture(AntialiasingPass_VelocityMapSlot, velocityBuffer->getVelocityTexture()); + batch.setResourceTexture(AntialiasingPass_DepthMapSlot, sourceBuffer->getDepthStencilBuffer()); batch.setUniformBuffer(AntialiasingPass_ParamsSlot, _params._buffer); batch.setUniformBuffer(AntialiasingPass_FrameTransformSlot, deferredFrameTransform->getFrameTransformBuffer()); - batch.setFramebuffer(_antialiasingBuffer[currentFrame]); + batch.setFramebuffer(_antialiasingBuffer[nextFrame]); batch.setPipeline(getAntialiasingPipeline()); batch.draw(gpu::TRIANGLE_STRIP, 4); // Blend step + batch.setResourceTexture(AntialiasingPass_SourceMapSlot, nullptr); + batch.setFramebuffer(sourceBuffer); if (_params->debugX <= 0.0) { batch.setPipeline(getBlendPipeline()); } else { batch.setPipeline(getDebugBlendPipeline()); } - batch.setResourceTexture(AntialiasingPass_CurrentMapSlot, _antialiasingTexture[currentFrame]); + batch.setResourceTexture(AntialiasingPass_NextMapSlot, _antialiasingTexture[nextFrame]); batch.draw(gpu::TRIANGLE_STRIP, 4); batch.setUniformBuffer(AntialiasingPass_ParamsSlot, nullptr); batch.setUniformBuffer(AntialiasingPass_FrameTransformSlot, nullptr); + batch.setResourceTexture(AntialiasingPass_DepthMapSlot, nullptr); batch.setResourceTexture(AntialiasingPass_HistoryMapSlot, nullptr); - batch.setResourceTexture(AntialiasingPass_SourceMapSlot, nullptr); batch.setResourceTexture(AntialiasingPass_VelocityMapSlot, nullptr); - batch.setResourceTexture(AntialiasingPass_CurrentMapSlot, nullptr); + batch.setResourceTexture(AntialiasingPass_NextMapSlot, nullptr); }); } diff --git a/libraries/render-utils/src/AntialiasingEffect.h b/libraries/render-utils/src/AntialiasingEffect.h index 99af206843..1fd3fb6584 100644 --- a/libraries/render-utils/src/AntialiasingEffect.h +++ b/libraries/render-utils/src/AntialiasingEffect.h @@ -26,6 +26,7 @@ class AntialiasingConfig : public render::Job::Config { 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) + Q_PROPERTY(float debugOrbZoom MEMBER debugOrbZoom NOTIFY dirty) public: AntialiasingConfig() : render::Job::Config(true) {} @@ -36,6 +37,7 @@ public: bool showCursorPixel{ false }; glm::vec2 debugCursorTexcoord{ 0.5f, 0.5f }; + float debugOrbZoom{ 2.0f }; signals: void dirty(); @@ -49,7 +51,7 @@ struct TAAParams { float debugShowVelocityThreshold{ 1.0f }; glm::vec4 debugCursor{ 0.0f }; - glm::vec4 pixelInfo{ 0.5f, 0.5f, 0.0f, 0.0f }; + glm::vec4 pixelInfo{ 0.5f, 0.5f, 2.0f, 0.0f }; }; using TAAParamsBuffer = gpu::StructBuffer; diff --git a/libraries/render-utils/src/taa.slf b/libraries/render-utils/src/taa.slf index f0cd66b368..a3c6313b8e 100644 --- a/libraries/render-utils/src/taa.slf +++ b/libraries/render-utils/src/taa.slf @@ -19,19 +19,21 @@ in vec2 varTexCoord0; layout(location = 0) out vec4 outFragColor; void main() { - vec3 currentColor = texture(colorMap, varTexCoord0).xyz; - vec2 pixVelocity = texture(velocityMap, varTexCoord0).xy; + vec2 fragUV = find_closest_fragment_3x3(varTexCoord0); + vec3 sourceColor = texture(sourceMap, fragUV).xyz; + + vec2 pixVelocity = texture(velocityMap, fragUV).xy; vec2 velocity = params.motionScale * pixVelocity;// *getInvWidthHeight(); vec2 prevTexCoord = varTexCoord0 - velocity; - vec3 prevColor = currentColor; + vec3 historyColor = sourceColor; if (!(any(lessThan(prevTexCoord, vec2(0.0))) || any(greaterThan(prevTexCoord, vec2(1.0))))) { - prevColor = texture(historyMap, prevTexCoord).xyz; + historyColor = texture(historyMap, prevTexCoord).xyz; } - vec3 newColor = mix(prevColor, currentColor, params.blend); + vec3 nextColor = mix(historyColor, sourceColor, params.blend); - outFragColor = vec4(newColor, 1.0); + outFragColor = vec4(nextColor, 1.0); } diff --git a/libraries/render-utils/src/taa.slh b/libraries/render-utils/src/taa.slh index 153bed90e9..c2dc47707e 100644 --- a/libraries/render-utils/src/taa.slh +++ b/libraries/render-utils/src/taa.slh @@ -13,10 +13,11 @@ <@include DeferredTransform.slh@> <$declareDeferredFrameTransform()$> -uniform sampler2D currentMap; -uniform sampler2D colorMap; +uniform sampler2D depthMap; +uniform sampler2D sourceMap; uniform sampler2D historyMap; uniform sampler2D velocityMap; +uniform sampler2D nextMap; struct TAAParams { @@ -25,7 +26,7 @@ struct TAAParams float motionScale; float debugShowVelocityThreshold; vec4 debugCursor; - vec4 pixelInfo; + vec4 pixelInfo_orbZoom; }; layout(std140) uniform taaParamsBuffer { @@ -33,9 +34,57 @@ layout(std140) uniform taaParamsBuffer { }; vec2 getDebugCursorTexcoord() { - return params.pixelInfo.xy; + return params.pixelInfo_orbZoom.xy; } +float getOrbZoom() { + return params.pixelInfo_orbZoom.z; +} + +float fetchDepth(vec2 uv) { + return texture(depthMap, vec2(uv), 0).x; +} + +float resolveDepthLinear(float depth) { + return Zeye = -evalZeyeFromZdb(Zdb); + } + +#define ZCMP_GT(a, b) (a < b) + +vec3 find_closest_fragment_3x3(vec2 uv) +{ + vec2 dd = abs(getInvWidthHeight()); + vec2 du = vec2(dd.x, 0.0); + vec2 dv = vec2(0.0, dd.y); + + vec3 dtl = vec3(-1, -1, fetchDepth(uv - dv - du); + vec3 dtc = vec3( 0, -1, fetchDepth(uv - dv).x); + vec3 dtr = vec3( 1, -1, fetchDepth(uv - dv + du); + + vec3 dml = vec3(-1, 0, fetchDepth(CameraDepthTexture, uv - du); + vec3 dmc = vec3( 0, 0, fetchDepth(CameraDepthTexture, uv); + vec3 dmr = vec3( 1, 0, fetchDepth(CameraDepthTexture, uv + du); + + vec3 dbl = vec3(-1, 1, fetchDepth(CameraDepthTexture, uv + dv - du); + vec3 dbc = vec3( 0, 1, fetchDepth(CameraDepthTexture, uv + dv); + vec3 dbr = vec3( 1, 1, fetchDepth(CameraDepthTexture, uv + dv + du); + + vec3 dmin = dtl; + if (ZCMP_GT(dmin.z, dtc.z)) dmin = dtc; + if (ZCMP_GT(dmin.z, dtr.z)) dmin = dtr; + + if (ZCMP_GT(dmin.z, dml.z)) dmin = dml; + if (ZCMP_GT(dmin.z, dmc.z)) dmin = dmc; + if (ZCMP_GT(dmin.z, dmr.z)) dmin = dmr; + + if (ZCMP_GT(dmin.z, dbl.z)) dmin = dbl; + if (ZCMP_GT(dmin.z, dbc.z)) dmin = dbc; + if (ZCMP_GT(dmin.z, dbr.z)) dmin = dbr; + + return vec3(uv + dd.xy * dmin.xy, dmin.z); +} + + <@include gpu/Color.slh@> <$declareColorWheel()$> @@ -45,4 +94,4 @@ vec3 getVelocityColorRelative(float velocityPixLength) { vec3 getVelocityColorAboveThreshold(float velocityPixLength) { return colorRamp((velocityPixLength - params.debugShowVelocityThreshold)/params.debugShowVelocityThreshold); -} \ 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 a633b61821..29b32f0412 100644 --- a/libraries/render-utils/src/taa_blend.slf +++ b/libraries/render-utils/src/taa_blend.slf @@ -18,11 +18,11 @@ in vec2 varTexCoord0; layout(location = 0) out vec4 outFragColor; void main(void) { - vec3 newColor = texture(currentMap, varTexCoord0).xyz; - outFragColor = vec4(newColor, 1.0); + vec3 nextColor = texture(nextMap, varTexCoord0).xyz; + outFragColor = vec4(nextColor, 1.0); // Pixel being shaded - vec3 sourceColor = texture(colorMap, varTexCoord0).xyz; + vec3 sourceColor = texture(sourceMap, varTexCoord0).xyz; vec2 imageSize = getWidthHeight(0); @@ -34,12 +34,14 @@ void main(void) { vec2 prevPix = prevTexCoord * imageSize; // Pixel Debugged - vec2 cursorUV = getDebugCursorTexcoord(); + vec3 cursorFrag = find_closest_fragment_3x3(getDebugCursorTexcoord()); + vec2 cursorUV = cursorFrag.xy; vec2 cursorPixelPos = cursorUV * imageSize; vec2 cursorVelocity = texture(velocityMap, cursorUV).xy; vec2 cursorPrevUV = cursorUV - cursorVelocity; cursorVelocity *= imageSize; float cursorVelocityLength = length(cursorVelocity); + vec2 cursorVelocityDir = cursorVelocity / cursorVelocityLength; vec2 cursorToFragVec = pixPos - cursorPixelPos; float cursorToFragLength = length(cursorToFragVec); @@ -60,25 +62,44 @@ void main(void) { float tenPercentHeight = 0.1 * imageSize.y; float centerWidth = imageSize.x * 0.5; - vec2 prevOrbPos = vec2(centerWidth - tenPercentHeight, imageSize.y - tenPercentHeight); + vec2 nextOrbPos = vec2(centerWidth, imageSize.y - 3 * tenPercentHeight); + vec2 nextOrbPosToPix = pixPos - nextOrbPos; + + vec2 prevOrbPos = nextOrbPos + cursorVelocityDir * 2.0 * tenPercentHeight; vec2 prevOrbPosToPix = pixPos - prevOrbPos; + if (dot(prevOrbPosToPix, prevOrbPosToPix) < tenPercentHeight * tenPercentHeight) { - vec2 prevOrbPosToPix_uv = cursorPrevUV + prevOrbPosToPix * getInvWidthHeight() * 0.5; + vec2 prevOrbPosToPix_uv = cursorPrevUV + prevOrbPosToPix * getInvWidthHeight() / getOrbZoom(); vec3 preOrbColor = vec3(0.0); if (!(any(lessThan(prevOrbPosToPix_uv, vec2(0.0))) || any(greaterThan(prevOrbPosToPix_uv, vec2(1.0))))) { preOrbColor = texture(historyMap, prevOrbPosToPix_uv).xyz; } + float distanceToPrev = length(prevOrbPosToPix); + if (distanceToPrev < 2.0) { + preOrbColor = vec3(1.0, 0.0, 1.0); + } + float distanceToNext = length(imageSize * (cursorUV - prevOrbPosToPix_uv)); + if (distanceToNext < 2.0) { + preOrbColor = vec3(1.0, 0.5, 0.0); + } outFragColor = vec4(preOrbColor, 1.0); return; } - vec2 nextOrbPos = vec2(centerWidth + tenPercentHeight, imageSize.y - tenPercentHeight); - vec2 nextOrbPosToPix = pixPos - nextOrbPos; if (dot(nextOrbPosToPix, nextOrbPosToPix) < tenPercentHeight * tenPercentHeight) { - vec2 nextOrbPosToPix_uv = cursorUV + nextOrbPosToPix * getInvWidthHeight() * 0.5; + vec2 nextOrbPosToPix_uv = cursorUV + nextOrbPosToPix * getInvWidthHeight() / getOrbZoom(); vec3 nextOrbColor = vec3(0.0); if (!(any(lessThan(nextOrbPosToPix_uv, vec2(0.0))) || any(greaterThan(nextOrbPosToPix_uv, vec2(1.0))))) { - nextOrbColor = texture(colorMap, nextOrbPosToPix_uv).xyz; + nextOrbColor = texture(nextMap, nextOrbPosToPix_uv).xyz; } + float distanceToPrev = length(imageSize * (cursorPrevUV - nextOrbPosToPix_uv)); + if (distanceToPrev < 2.0) { + nextOrbColor = vec3(1.0, 0.0, 1.0); + } + float distanceToNext = length(nextOrbPosToPix); + if (distanceToNext < 2.0) { + nextOrbColor = vec3(1.0, 0.5, 0.0); + } + outFragColor = vec4(nextOrbColor, 1.0); return; } @@ -94,9 +115,9 @@ void main(void) { return; } - outFragColor = vec4(sourceColor, 1.0); + outFragColor = vec4(nextColor, 1.0); - vec3 prevColor = sourceColor; + vec3 prevColor = nextColor; if (!(any(lessThan(prevTexCoord, vec2(0.0))) || any(greaterThan(prevTexCoord, vec2(1.0))))) { prevColor = texture(historyMap, prevTexCoord).xyz; diff --git a/libraries/render-utils/src/velocityBuffer_cameraMotion.slf b/libraries/render-utils/src/velocityBuffer_cameraMotion.slf index 5a83501b4c..cef6afea84 100644 --- a/libraries/render-utils/src/velocityBuffer_cameraMotion.slf +++ b/libraries/render-utils/src/velocityBuffer_cameraMotion.slf @@ -40,6 +40,7 @@ void main(void) { vec4 prevClipPos = (frameTransform._projection[stereoSide.x] * vec4(prevEyePos, 1.0)); vec2 prevUV = 0.5 * (prevClipPos.xy / prevClipPos.w) + vec2(0.5); - vec2 imageSize = vec2(1.0, 1.0); // getWidthHeight(0); + //vec2 imageSize = getWidthHeight(0); + vec2 imageSize = vec2(1.0, 1.0); outFragColor = vec4( ((texcoordPos - prevUV) * imageSize), 0.0, 0.0); } diff --git a/scripts/developer/utilities/render/antialiasing.qml b/scripts/developer/utilities/render/antialiasing.qml index c09b5293bd..9076e9eee4 100644 --- a/scripts/developer/utilities/render/antialiasing.qml +++ b/scripts/developer/utilities/render/antialiasing.qml @@ -56,12 +56,14 @@ Column { max: 50 min: 0.0 } - CheckBox { - text: "Freeze " - checked: Render.getConfig("RenderMainView.JitterCam")["freeze"] - onCheckedChanged: { Render.getConfig("RenderMainView.JitterCam")["freeze"] = checked } - } - + ConfigSlider { + label: qsTr("Debug Orb Zoom") + integral: false + config: Render.getConfig("RenderMainView.Antialiasing") + property: "debugOrbZoom" + max: 8.0 + min: 0.0 + } } } }