From 3d2afd0d85a7f94a15a0fdee566d258b9abbff72 Mon Sep 17 00:00:00 2001 From: turanszkij Date: Thu, 24 Aug 2017 22:06:09 +0200 Subject: [PATCH 1/2] improved temporal AA --- WickedEngine/postProcessHF.hlsli | 15 ------ WickedEngine/ssao.hlsl | 4 +- WickedEngine/temporalAAResolvePS.hlsl | 67 ++++++++++++++++++++++----- WickedEngine/wiVersion.cpp | 2 +- 4 files changed, 59 insertions(+), 29 deletions(-) diff --git a/WickedEngine/postProcessHF.hlsli b/WickedEngine/postProcessHF.hlsli index 408a5a45e..74824e7c9 100644 --- a/WickedEngine/postProcessHF.hlsli +++ b/WickedEngine/postProcessHF.hlsli @@ -3,9 +3,6 @@ #include "imageHF.hlsli" #include "packHF.hlsli" - - - #include "depthConvertHF.hlsli" /*inline float getDepth(float4 c) @@ -22,18 +19,6 @@ float loadDepth(float2 texCoord) texture_lineardepth.GetDimensions(dim.x, dim.y); return texture_lineardepth.Load(int3(dim*texCoord, 0)).r; } -float4 loadNormal(float2 texCoord) -{ - float2 dim; - texture_gbuffer1.GetDimensions(dim.x, dim.y); - return texture_gbuffer1.Load(int3(dim*texCoord, 0)); -} -float4 loadVelocity(float2 texCoord) -{ - float2 dim; - texture_gbuffer2.GetDimensions(dim.x, dim.y); - return texture_gbuffer2.Load(int3(dim*texCoord, 0)); -} float4 loadMask(float2 texCoord) { float2 dim; diff --git a/WickedEngine/ssao.hlsl b/WickedEngine/ssao.hlsl index f7ee119a3..c9e3c6435 100644 --- a/WickedEngine/ssao.hlsl +++ b/WickedEngine/ssao.hlsl @@ -46,7 +46,7 @@ static const float3 AO_SAMPLES[ NUM_SAMPLES ] = float4 main(VertexToPixelPostProcess input ):SV_Target { - float3 normal = decode(loadNormal(input.tex.xy).xy); + float3 normal = decode(texture_gbuffer1.SampleLevel(sampler_linear_clamp,input.tex.xy,0).xy); float3 fres = normalize(xMaskTex.Load(int3((64 * input.tex.xy * 400) % 64, 0)).xyz * 2.0 - 1.0); @@ -70,7 +70,7 @@ float4 main(VertexToPixelPostProcess input ):SV_Target float2 newTex = ep.xy + ray.xy; //occFrag = xNormalMap.SampleLevel(Sampler, newTex,0).xyz; - occFrag = decode(loadNormal(newTex).xy); + occFrag = decode(texture_gbuffer1.SampleLevel(sampler_linear_clamp, newTex, 0).xy); if(!occFrag.x && !occFrag.y && !occFrag.z) break; diff --git a/WickedEngine/temporalAAResolvePS.hlsl b/WickedEngine/temporalAAResolvePS.hlsl index 86747c7d8..ac572cb34 100644 --- a/WickedEngine/temporalAAResolvePS.hlsl +++ b/WickedEngine/temporalAAResolvePS.hlsl @@ -2,23 +2,56 @@ #include "reconstructPositionHF.hlsli" #include "tonemapHF.hlsli" +// This hack can improve bright areas: #define HDR_CORRECTION +// This can retrieve better velocity vectors so moving objects could be better anti aliased: +#define DILATE_VELOCITY + + +float2 GetVelocity(in int2 pixel) +{ +#ifdef DILATE_VELOCITY + float bestDepth = g_xFrame_MainCamera_ZFarP; + int2 bestPixel = int2(0, 0); + + [unroll] + for (int i = -1; i <= 1; ++i) + { + [unroll] + for (int j = -1; j <= 1; ++j) + { + float depth = texture_lineardepth[pixel]; + [flatten] + if (depth < bestDepth) + { + bestDepth = depth; + bestPixel = pixel + int2(i, j); + } + } + } + + return texture_gbuffer1[bestPixel].zw; +#else + return texture_gbuffer1[pixel].zw; +#endif // DILATE_VELOCITY +} + float4 main(VertexToPixelPostProcess PSIn) : SV_TARGET { - float2 velocity = texture_gbuffer1.Load(uint3(PSIn.pos.xy,0)).zw; + float2 velocity = GetVelocity(PSIn.pos.xy); float2 prevTC = PSIn.tex + velocity; float4 neighborhood[9]; - neighborhood[0] = xTexture.Load(uint3(PSIn.pos.xy + float2(-1, -1), 0)); - neighborhood[1] = xTexture.Load(uint3(PSIn.pos.xy + float2(0, -1), 0)); - neighborhood[2] = xTexture.Load(uint3(PSIn.pos.xy + float2(1, -1), 0)); - neighborhood[3] = xTexture.Load(uint3(PSIn.pos.xy + float2(-1, 0), 0)); - neighborhood[4] = xTexture.Load(uint3(PSIn.pos.xy + float2(0, 0), 0)); // center - neighborhood[5] = xTexture.Load(uint3(PSIn.pos.xy + float2(1, 0), 0)); - neighborhood[6] = xTexture.Load(uint3(PSIn.pos.xy + float2(-1, 1), 0)); - neighborhood[7] = xTexture.Load(uint3(PSIn.pos.xy + float2(0, 1), 0)); - neighborhood[8] = xTexture.Load(uint3(PSIn.pos.xy + float2(1, 1), 0)); + neighborhood[0] = xTexture[PSIn.pos.xy + float2(-1, -1)]; + neighborhood[1] = xTexture[PSIn.pos.xy + float2(0, -1)]; + neighborhood[2] = xTexture[PSIn.pos.xy + float2(1, -1)]; + neighborhood[3] = xTexture[PSIn.pos.xy + float2(-1, 0)]; + neighborhood[4] = xTexture[PSIn.pos.xy + float2(0, 0)]; // center + neighborhood[5] = xTexture[PSIn.pos.xy + float2(1, 0)]; + neighborhood[6] = xTexture[PSIn.pos.xy + float2(-1, 1)]; + neighborhood[7] = xTexture[PSIn.pos.xy + float2(0, 1)]; + neighborhood[8] = xTexture[PSIn.pos.xy + float2(1, 1)]; float4 neighborhoodMin = neighborhood[0]; float4 neighborhoodMax = neighborhood[0]; [unroll] @@ -28,18 +61,30 @@ float4 main(VertexToPixelPostProcess PSIn) : SV_TARGET neighborhoodMax = max(neighborhoodMax, neighborhood[i]); } + // we cannot avoid the linear filter here because point sampling could sample irrelevant pixels but we try to correct it later: float4 history = xMaskTex.SampleLevel(sampler_linear_clamp, prevTC, 0); + + // simple correction of image signal incoherency (eg. moving shadows or lighting changes): history = clamp(history, neighborhoodMin, neighborhoodMax); + // our currently rendered frame sample: float4 current = neighborhood[4]; - float blendfactor = saturate(lerp(0.05f, 1.0f, length(velocity) * 10 * length(neighborhoodMax - neighborhoodMin))); // todo + // the linear filtering can cause blurry image, try to account for that: + float subpixelCorrection = frac(max(abs(velocity.x)*g_xWorld_InternalResolution.x, abs(velocity.y)*g_xWorld_InternalResolution.y)) * 0.5f; + + // compute a nice blend factor: + float blendfactor = saturate(lerp(0.05f, 0.8f, subpixelCorrection)); + + // if information can not be found on the screen, revert to aliased image: + blendfactor = any(prevTC - saturate(prevTC)) ? 1.0f : blendfactor; #ifdef HDR_CORRECTION history.rgb = tonemap(history.rgb); current.rgb = tonemap(current.rgb); #endif + // do the temporal super sampling by linearly accumulating previous samples with the current one: float4 resolved = float4(lerp(history.rgb, current.rgb, blendfactor), 1); #ifdef HDR_CORRECTION diff --git a/WickedEngine/wiVersion.cpp b/WickedEngine/wiVersion.cpp index 4672b43fe..45849c893 100644 --- a/WickedEngine/wiVersion.cpp +++ b/WickedEngine/wiVersion.cpp @@ -9,7 +9,7 @@ namespace wiVersion // minor features, major updates const int minor = 12; // minor bug fixes, alterations, refactors, updates - const int revision = 6; + const int revision = 7; long GetVersion() From 4e26e376f4dd2f1b785936dc2b0ad314ff1afabd Mon Sep 17 00:00:00 2001 From: turanszkij Date: Mon, 28 Aug 2017 20:32:28 +0200 Subject: [PATCH 2/2] updated motion blur --- WickedEngine/motionBlurPS.hlsl | 15 ++++++------ WickedEngine/postProcessHF.hlsli | 33 ++++++++++++++++++++----- WickedEngine/temporalAAResolvePS.hlsl | 35 +++------------------------ WickedEngine/wiVersion.cpp | 2 +- 4 files changed, 39 insertions(+), 46 deletions(-) diff --git a/WickedEngine/motionBlurPS.hlsl b/WickedEngine/motionBlurPS.hlsl index 9591ff97e..f86800458 100644 --- a/WickedEngine/motionBlurPS.hlsl +++ b/WickedEngine/motionBlurPS.hlsl @@ -1,21 +1,22 @@ +#define DILATE_VELOCITY + #include "postProcessHF.hlsli" #include "reconstructPositionHF.hlsli" + float4 main(VertexToPixelPostProcess PSIn) : SV_TARGET { - float3 color = float3(0,0,0); - float numSampling = 0.0f; + float3 color = 0; + float numSampling = 1.0f; - float2 vel = texture_gbuffer1.Load(uint3(PSIn.pos.xy, 0)).zw; + float2 vel = GetVelocity(PSIn.pos.xy); vel *= 0.025f; - numSampling++; - [unroll] for(float i=-7.5f;i<=7.5f;i+=1.0f){ - color.rgb += xTexture.SampleLevel(Sampler,saturate(PSIn.tex+vel*i*0.5f),0).rgb; + color.rgb += xTexture.SampleLevel(sampler_linear_clamp,saturate(PSIn.tex+vel*i*0.5f),0).rgb; numSampling++; } - return float4(color/numSampling,1); + return float4(color / numSampling, 1); } \ No newline at end of file diff --git a/WickedEngine/postProcessHF.hlsli b/WickedEngine/postProcessHF.hlsli index 74824e7c9..2fecf13a0 100644 --- a/WickedEngine/postProcessHF.hlsli +++ b/WickedEngine/postProcessHF.hlsli @@ -5,13 +5,34 @@ #include "packHF.hlsli" #include "depthConvertHF.hlsli" -/*inline float getDepth(float4 c) + +float2 GetVelocity(in int2 pixel) { - float z_b = c.x; - float z_n = 2.0 * z_b - 1.0; - float lin = 2.0 * zNearP * zFarP / (zFarP + zNearP - z_n * (zFarP - zNearP)); - return lin*0.01f; -}*/ +#ifdef DILATE_VELOCITY + float bestDepth = g_xFrame_MainCamera_ZFarP; + int2 bestPixel = int2(0, 0); + + [unroll] + for (int i = -1; i <= 1; ++i) + { + [unroll] + for (int j = -1; j <= 1; ++j) + { + float depth = texture_lineardepth[pixel]; + [flatten] + if (depth < bestDepth) + { + bestDepth = depth; + bestPixel = pixel + int2(i, j); + } + } + } + + return texture_gbuffer1[bestPixel].zw; +#else + return texture_gbuffer1[pixel].zw; +#endif // DILATE_VELOCITY +} float loadDepth(float2 texCoord) { diff --git a/WickedEngine/temporalAAResolvePS.hlsl b/WickedEngine/temporalAAResolvePS.hlsl index ac572cb34..b09ea98a0 100644 --- a/WickedEngine/temporalAAResolvePS.hlsl +++ b/WickedEngine/temporalAAResolvePS.hlsl @@ -1,3 +1,6 @@ +// This can retrieve better velocity vectors so moving objects could be better anti aliased: +#define DILATE_VELOCITY + #include "postProcessHF.hlsli" #include "reconstructPositionHF.hlsli" #include "tonemapHF.hlsli" @@ -5,38 +8,6 @@ // This hack can improve bright areas: #define HDR_CORRECTION -// This can retrieve better velocity vectors so moving objects could be better anti aliased: -#define DILATE_VELOCITY - - -float2 GetVelocity(in int2 pixel) -{ -#ifdef DILATE_VELOCITY - float bestDepth = g_xFrame_MainCamera_ZFarP; - int2 bestPixel = int2(0, 0); - - [unroll] - for (int i = -1; i <= 1; ++i) - { - [unroll] - for (int j = -1; j <= 1; ++j) - { - float depth = texture_lineardepth[pixel]; - [flatten] - if (depth < bestDepth) - { - bestDepth = depth; - bestPixel = pixel + int2(i, j); - } - } - } - - return texture_gbuffer1[bestPixel].zw; -#else - return texture_gbuffer1[pixel].zw; -#endif // DILATE_VELOCITY -} - float4 main(VertexToPixelPostProcess PSIn) : SV_TARGET { float2 velocity = GetVelocity(PSIn.pos.xy); diff --git a/WickedEngine/wiVersion.cpp b/WickedEngine/wiVersion.cpp index 45849c893..d7d778425 100644 --- a/WickedEngine/wiVersion.cpp +++ b/WickedEngine/wiVersion.cpp @@ -9,7 +9,7 @@ namespace wiVersion // minor features, major updates const int minor = 12; // minor bug fixes, alterations, refactors, updates - const int revision = 7; + const int revision = 8; long GetVersion()