From dca7b7aabaf683120d85395bb53536f3a70d3291 Mon Sep 17 00:00:00 2001 From: Olivier Prat Date: Wed, 21 Feb 2018 17:34:51 +0100 Subject: [PATCH] TAA is working on HMD! Still some issues when game rate is low --- .../display-plugins/hmd/HmdDisplayPlugin.cpp | 3 +- .../render-utils/src/DeferredTransform.slh | 6 +++- libraries/render-utils/src/taa.slf | 5 +--- libraries/render-utils/src/taa.slh | 29 +++++++++++++++++-- libraries/render-utils/src/taa_blend.slf | 10 +++---- .../src/velocityBuffer_cameraMotion.slf | 9 ++---- 6 files changed, 42 insertions(+), 20 deletions(-) diff --git a/libraries/display-plugins/src/display-plugins/hmd/HmdDisplayPlugin.cpp b/libraries/display-plugins/src/display-plugins/hmd/HmdDisplayPlugin.cpp index 22dd7823f6..405ee33a86 100644 --- a/libraries/display-plugins/src/display-plugins/hmd/HmdDisplayPlugin.cpp +++ b/libraries/display-plugins/src/display-plugins/hmd/HmdDisplayPlugin.cpp @@ -330,7 +330,6 @@ void HmdDisplayPlugin::updateFrameData() { } } if (newFrameIndex != INVALID_FRAME) { - _previousPresentFrameInfo = _currentPresentFrameInfo; _currentPresentFrameInfo = _frameInfos[newFrameIndex]; } }); @@ -344,6 +343,8 @@ void HmdDisplayPlugin::updateFrameData() { auto correction = invBatchPose * _currentPresentFrameInfo.presentPose; auto prevCorrection = invPrevBatchPose * _previousPresentFrameInfo.presentPose; getGLBackend()->setCameraCorrection(correction, prevCorrection); + + _previousPresentFrameInfo = _currentPresentFrameInfo; } } diff --git a/libraries/render-utils/src/DeferredTransform.slh b/libraries/render-utils/src/DeferredTransform.slh index f1765978eb..57e67c086b 100644 --- a/libraries/render-utils/src/DeferredTransform.slh +++ b/libraries/render-utils/src/DeferredTransform.slh @@ -89,7 +89,11 @@ mat4 getViewInverse() { } mat4 getView() { - return frameTransform._view * cameraCorrection._correctionInverse; + return cameraCorrection._correctionInverse * frameTransform._view; +} + +mat4 getPreviousView() { + return cameraCorrection._prevCorrectionInverse * frameTransform._prevView; } bool isStereo() { diff --git a/libraries/render-utils/src/taa.slf b/libraries/render-utils/src/taa.slf index 57334e8ab5..a5dce214c6 100644 --- a/libraries/render-utils/src/taa.slf +++ b/libraries/render-utils/src/taa.slf @@ -28,9 +28,6 @@ void main() { return; } - // vec3 nearFragUV = taa_findClosestFragment3x3(fragUV); - // vec2 fragVel = taa_fetchVelocityMap(nearFragUV.xy); - vec3 fragVelAndZ = taa_fetchVelocityMapBest(fragUV); vec2 fragVel = fragVelAndZ.xy; float fragDepth = fragVelAndZ.z; @@ -41,7 +38,7 @@ void main() { vec3 nextColor = sourceColor; - if (taa_constrainColor()) { + if (taa_constrainColor()) { // clamp history to neighbourhood of current sample historyColor = taa_evalConstrainColor(sourceColor, fragUV, fragVel, fragDepth, historyColor); } diff --git a/libraries/render-utils/src/taa.slh b/libraries/render-utils/src/taa.slh index 232d1e9f70..3b3e275da1 100644 --- a/libraries/render-utils/src/taa.slh +++ b/libraries/render-utils/src/taa.slh @@ -225,12 +225,37 @@ vec3 taa_fetchVelocityMapBest(vec2 uv) { return vec3(best.xy, taa_fetchDepth(uv)); } +vec2 taa_fromFragUVToEyeUVAndSide(vec2 fragUV, out int stereoSide) { + vec2 eyeUV = fragUV; + stereoSide = 0; + if (isStereo()) { + if (eyeUV.x > 0.5) { + eyeUV.x -= 0.5; + stereoSide = 1; + } + eyeUV.x *= 2.0; + } + return eyeUV; +} + +vec2 taa_fromEyeUVToFragUV(vec2 eyeUV, int stereoSide) { + vec2 fragUV = eyeUV; + if (isStereo()) { + fragUV.x *= 0.5; + fragUV.x += stereoSide*0.5; + } + return fragUV; +} + vec2 taa_fetchSourceAndHistory(vec2 fragUV, vec2 fragVelocity, out vec3 sourceColor, out vec3 historyColor) { sourceColor = taa_fetchSourceMap(fragUV).xyz; + int stereoSide = 0; + vec2 eyeUV = taa_fromFragUVToEyeUVAndSide(fragUV, stereoSide); + vec2 prevEyeUV = eyeUV - fragVelocity; - vec2 prevFragUV = fragUV - fragVelocity; historyColor = sourceColor; - if (!(any(lessThan(prevFragUV, vec2(0.0))) || any(greaterThan(prevFragUV, vec2(1.0))))) { + vec2 prevFragUV = taa_fromEyeUVToFragUV(prevEyeUV, stereoSide); + if (!(any(lessThan(prevEyeUV, vec2(0.0))) || any(greaterThan(prevEyeUV, vec2(1.0))))) { historyColor = taa_fetchHistoryMap(prevFragUV).xyz; } return prevFragUV; diff --git a/libraries/render-utils/src/taa_blend.slf b/libraries/render-utils/src/taa_blend.slf index 8b32ee2fb0..bb81f4989f 100644 --- a/libraries/render-utils/src/taa_blend.slf +++ b/libraries/render-utils/src/taa_blend.slf @@ -32,7 +32,9 @@ void main(void) { vec2 pixVelocity = imageSize * texture(velocityMap, varTexCoord0).xy; float pixVelocityLength = length(pixVelocity); vec2 velocity = pixVelocity * texelSize; - vec2 prevTexCoord = varTexCoord0 - velocity; + int stereoSide = 0; + vec2 prevTexCoord = taa_fromFragUVToEyeUVAndSide(varTexCoord0, stereoSide) - velocity; + prevTexCoord = taa_fromEyeUVToFragUV(prevTexCoord, stereoSide); vec2 prevPix = prevTexCoord * imageSize; // Pixel Debugged @@ -43,7 +45,7 @@ void main(void) { vec2 cursorUV = cursorUVRaw; vec2 cursorPos = cursorUV * imageSize; vec2 cursorVelocity = texture(velocityMap, cursorUV).xy; - vec2 cursorPrevUV = cursorUV - cursorVelocity; + vec2 cursorPrevUV = taa_fromFragUVToEyeUVAndSide(cursorUV, stereoSide) - cursorVelocity; cursorVelocity *= imageSize; float cursorVelocityLength = length(cursorVelocity); vec2 cursorVelocityDir = cursorVelocity / cursorVelocityLength; @@ -143,13 +145,11 @@ void main(void) { if (!(any(lessThan(prevTexCoord, vec2(0.0))) || any(greaterThan(prevTexCoord, vec2(1.0))))) { prevColor = texture(historyMap, prevTexCoord).xyz; } - outFragColor.xyz = prevColor; + outFragColor.xyz = mix(prevColor, vec3(1,0,1), clamp(distance(prevColor, nextColor) - 0.01, 0, 1)); if (pixVelocityLength > params.debugShowVelocityThreshold) { vec3 speedColor = taa_getVelocityColorAboveThreshold(pixVelocityLength); outFragColor = vec4(0.0, 1.0, 1.0, 1.0); } - - } diff --git a/libraries/render-utils/src/velocityBuffer_cameraMotion.slf b/libraries/render-utils/src/velocityBuffer_cameraMotion.slf index d908ea24da..22a95b55d1 100644 --- a/libraries/render-utils/src/velocityBuffer_cameraMotion.slf +++ b/libraries/render-utils/src/velocityBuffer_cameraMotion.slf @@ -26,17 +26,12 @@ void main(void) { ivec2 framePixelPos = getPixelPosTexcoordPosAndSide(gl_FragCoord.xy, pixelPos, texcoordPos, stereoSide); float Zdb = texelFetch(depthMap, ivec2(gl_FragCoord.xy), 0).x; -/* float Zeye = evalZeyeFromZdb(Zdb); - if (Zeye <= -getPosLinearDepthFar()) { - outFragColor = vec4(0.5, 0.5, 0.0, 0.0); - return; - }*/ // The position of the pixel fragment in Eye space then in world space vec3 eyePos = evalEyePositionFromZdb(stereoSide.x, Zdb, texcoordPos); - vec3 worldPos = (frameTransform._viewInverse * cameraCorrection._correction * vec4(eyePos, 1.0)).xyz; + vec3 worldPos = (getViewInverse() * vec4(eyePos, 1.0)).xyz; - vec3 prevEyePos = (cameraCorrection._prevCorrectionInverse * frameTransform._prevView * vec4(worldPos, 1.0)).xyz; + vec3 prevEyePos = (getPreviousView() * vec4(worldPos, 1.0)).xyz; vec4 prevClipPos = (frameTransform._projection[stereoSide.x] * vec4(prevEyePos, 1.0)); vec2 prevUV = 0.5 * (prevClipPos.xy / prevClipPos.w) + vec2(0.5);