fix temporal upscaling noisy edges visual issue. slightly improve screenspace shdows.

This commit is contained in:
Xonk 2024-06-26 19:25:04 -04:00
parent 9a6ffa360c
commit 9ab0412f72
4 changed files with 47 additions and 19 deletions

View File

@ -363,9 +363,18 @@ vec3 worldToView(vec3 worldPos) {
pos = gbufferModelView * pos;
return pos.xyz;
}
float swapperlinZ(float depth, float _near, float _far) {
return (2.0 * _near) / (_far + _near - depth * (_far - _near));
// l = (2*n)/(f+n-d(f-n))
// f+n-d(f-n) = 2n/l
// -d(f-n) = ((2n/l)-f-n)
// d = -((2n/l)-f-n)/(f-n)
}
vec2 SSRT_Shadows(vec3 viewPos, bool depthCheck, vec3 lightDir, float noise, bool isSSS, bool hand){
float handSwitch = hand ? 1.0 : 0.0;
float steps = 16.0;
@ -379,6 +388,8 @@ vec2 SSRT_Shadows(vec3 viewPos, bool depthCheck, vec3 lightDir, float noise, boo
_far = dhFarPlane;
}
vec3 worldpos = mat3(gbufferModelViewInverse) * viewPos;
float dist = 1.0 + length(worldpos)/(_far/2.0); // step length as distance increases
vec3 clipPosition = toClipSpace3_DH(viewPos, depthCheck);
//prevents the ray from going behind the camera
@ -390,26 +401,35 @@ vec2 SSRT_Shadows(vec3 viewPos, bool depthCheck, vec3 lightDir, float noise, boo
float Stepmult = depthCheck ? (isSSS ? 1.0 : 6.0) : (isSSS ? 1.0 : 3.0);
vec3 rayDir = direction * Stepmult * vec3(RENDER_SCALE,1.0) ;
vec3 rayDir = direction * Stepmult * vec3(RENDER_SCALE,1.0) ;
vec3 screenPos = clipPosition * vec3(RENDER_SCALE,1.0) + rayDir * noise;
float minZ = screenPos.z;
float maxZ = screenPos.z;
for (int i = 0; i < int(steps); i++) {
screenPos += rayDir;
float samplePos = convertHandDepth_2(texture2D(depthtex1, screenPos.xy).x, hand);
#ifdef DISTANT_HORIZONS
if(depthCheck) samplePos = texture2D(dhDepthTex1, screenPos.xy).x;
#endif
if(samplePos < screenPos.z) {
vec2 linearZ = vec2(linearizeDepthFast(screenPos.z, _near, _far), linearizeDepthFast(samplePos, _near, _far));
if(samplePos < screenPos.z && (samplePos <= max(minZ,maxZ) && samplePos >= min(minZ,maxZ))){
vec2 linearZ = vec2(swapperlinZ(screenPos.z, _near, _far), swapperlinZ(samplePos, _near, _far));
float calcthreshold = abs(linearZ.x - linearZ.y) / linearZ.x;
if (calcthreshold < 0.035) Shadow = 0.0;
if (calcthreshold < (depthCheck ? 1.0 : 0.035)) SSS = i/steps;
}
SSS += 1.0/steps;
}
minZ = maxZ - (isSSS ? 1.0 : 0.0001) / swapperlinZ(samplePos, _near, _far);
maxZ += rayDir.z;
screenPos += rayDir;
}
return vec2(Shadow, SSS);
}
@ -1227,9 +1247,11 @@ void main() {
// combine shadowmap with a minumum shadow determined by the screenspace shadows.
Shadows = min(Shadows, SS_directLight.r);
// Shadows = SS_directLight.r;
// combine shadowmap blocker depth with a minumum determined by the screenspace shadows, starting after the shadowmap ends
ShadowBlockerDepth = mix(SS_directLight.g, ShadowBlockerDepth, shadowMapFalloff2);
// ShadowBlockerDepth = max( SS_directLight.g,0.0);
#endif

View File

@ -115,7 +115,7 @@ vec4 smoothfilter(in sampler2D tex, in vec2 uv)
uv = (uv - 0.5)/textureResolution;
return texture2D( tex, uv);
return texture2D(tex, uv);
}
//approximation from SMAA presentation from siggraph 2016
vec3 FastCatmulRom(sampler2D colorTex, vec2 texcoord, vec4 rtMetrics, float sharpenAmount)
@ -337,20 +337,25 @@ vec4 TAA_hq(bool hand){
#else
vec2 adjTC = texcoord;
#endif
bool depthCheck = texture2D(depthtex0,adjTC).x >= 1.0;
vec2 offsets = offsets[framemod8]*texelSize*0.5;
#ifdef DISTANT_HORIZONS
bool depthCheck = texture2D(depthtex0,adjTC).x >= 1.0;
#else
bool depthCheck = false;
#endif
//use velocity from the nearest texel from camera in a 3x3 box in order to improve edge quality in motion
#ifdef CLOSEST_VELOCITY
#ifdef DISTANT_HORIZONS
vec3 closestToCamera = closestToCamera5taps_DH(adjTC, depthtex0, dhDepthTex, depthCheck, hand);
#else
vec3 closestToCamera = closestToCamera5taps(adjTC,depthtex0, hand);
vec3 closestToCamera = closestToCamera5taps(adjTC, depthtex0, hand);
#endif
#endif
#ifndef CLOSEST_VELOCITY
vec3 closestToCamera = vec3(texcoord, texture2D(depthtex1,adjTC).x);
vec3 closestToCamera = vec3(texcoord, texture2D(depthtex1, adjTC).x);
#endif
//reproject previous frame
@ -370,11 +375,11 @@ vec4 TAA_hq(bool hand){
//reject history if off-screen and early exit
if (previousPosition.x < 0.0 || previousPosition.y < 0.0 || previousPosition.x > 1.0 || previousPosition.y > 1.0)
return vec4(smoothfilter(colortex3, adjTC + offsets[framemod8]*texelSize*0.5).xyz,1.0);
return vec4(smoothfilter(colortex3, adjTC + offsets).xyz,1.0);
#ifdef TAA_UPSCALING
vec3 albedoCurrent0 = smoothfilter(colortex3, adjTC + offsets[framemod8]*texelSize*0.5).xyz;
vec3 albedoCurrent0 = smoothfilter(colortex3, adjTC + offsets).xyz;
// Interpolating neighboorhood clampling boundaries between pixels
vec3 cMax = texture2D(colortex0, adjTC).rgb;
vec3 cMin = texture2D(colortex6, adjTC).rgb;
@ -388,15 +393,17 @@ vec4 TAA_hq(bool hand){
vec3 albedoCurrent6 = texture2D(colortex3, adjTC + vec2(0.0,-texelSize.y)).rgb;
vec3 albedoCurrent7 = texture2D(colortex3, adjTC + vec2(-texelSize.x,0.0)).rgb;
vec3 albedoCurrent8 = texture2D(colortex3, adjTC + vec2(texelSize.x,0.0)).rgb;
//Assuming the history color is a blend of the 3x3 neighborhood, we clamp the history to the min and max of each channel in the 3x3 neighborhood
vec3 cMax = max(max(max(albedoCurrent0,albedoCurrent1),albedoCurrent2),max(albedoCurrent3,max(albedoCurrent4,max(albedoCurrent5,max(albedoCurrent6,max(albedoCurrent7,albedoCurrent8))))));
vec3 cMin = min(min(min(albedoCurrent0,albedoCurrent1),albedoCurrent2),min(albedoCurrent3,min(albedoCurrent4,min(albedoCurrent5,min(albedoCurrent6,min(albedoCurrent7,albedoCurrent8))))));
albedoCurrent0 = smoothfilter(colortex3, adjTC + offsets[framemod8]*texelSize*0.5).rgb;
albedoCurrent0 = smoothfilter(colortex3, adjTC + offsets).rgb;
#endif
#ifndef SCREENSHOT_MODE
vec3 albedoPrev = max(FastCatmulRom(colortex5, previousPosition.xy,vec4(texelSize, 1.0/texelSize), 0.75).xyz, 0.0);
vec3 albedoPrev = max(FastCatmulRom(colortex5, previousPosition.xy, vec4(texelSize, 1.0/texelSize), 0.75).xyz, 0.0);
vec3 finalcAcc = clamp(albedoPrev, cMin, cMax);
//Increases blending factor when far from AABB and in motion, reduces ghosting
@ -441,7 +448,7 @@ void main() {
vec2 taauTC = clamp(texcoord*RENDER_SCALE, vec2(0.0), RENDER_SCALE - texelSize*2.0);
float dataUnpacked = decodeVec2(texture2D(colortex1,taauTC).w).y;
float dataUnpacked = decodeVec2(texelFetch2D(colortex1,ivec2(gl_FragCoord.xy*RENDER_SCALE),0).w).y;
bool hand = abs(dataUnpacked-0.75) < 0.01 && texture2D(depthtex1,taauTC).x < 1.0;
vec4 color = TAA_hq(hand);

View File

@ -318,7 +318,6 @@ vec3 RT_alternate(vec3 dir, vec3 position, float noise, float stepsizes, bool ha
float len = max(abs(direction.x)/texelSize.x,abs(direction.y)/texelSize.y)/stepSize;
//get at which length the ray intersects with the edge of the screen
vec3 maxLengths = (step(0.,direction)-clipPosition) / direction;
float mult = min(min(maxLengths.x,maxLengths.y),maxLengths.z)*2000.0;
vec3 stepv = direction/len;

View File

@ -1,4 +1,4 @@
#define SHADER_VERSION_LABEL 466 // [466]
#define SHADER_VERSION_LABEL 467 // [467]
#define saturate(x) clamp(x,0.0,1.0)