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; pos = gbufferModelView * pos;
return pos.xyz; 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){ vec2 SSRT_Shadows(vec3 viewPos, bool depthCheck, vec3 lightDir, float noise, bool isSSS, bool hand){
float handSwitch = hand ? 1.0 : 0.0; float handSwitch = hand ? 1.0 : 0.0;
float steps = 16.0; float steps = 16.0;
@ -379,6 +388,8 @@ vec2 SSRT_Shadows(vec3 viewPos, bool depthCheck, vec3 lightDir, float noise, boo
_far = dhFarPlane; _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); vec3 clipPosition = toClipSpace3_DH(viewPos, depthCheck);
//prevents the ray from going behind the camera //prevents the ray from going behind the camera
@ -391,11 +402,13 @@ 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); 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; vec3 screenPos = clipPosition * vec3(RENDER_SCALE,1.0) + rayDir * noise;
for (int i = 0; i < int(steps); i++) { float minZ = screenPos.z;
float maxZ = screenPos.z;
screenPos += rayDir; for (int i = 0; i < int(steps); i++) {
float samplePos = convertHandDepth_2(texture2D(depthtex1, screenPos.xy).x, hand); float samplePos = convertHandDepth_2(texture2D(depthtex1, screenPos.xy).x, hand);
@ -403,13 +416,20 @@ vec2 SSRT_Shadows(vec3 viewPos, bool depthCheck, vec3 lightDir, float noise, boo
if(depthCheck) samplePos = texture2D(dhDepthTex1, screenPos.xy).x; if(depthCheck) samplePos = texture2D(dhDepthTex1, screenPos.xy).x;
#endif #endif
if(samplePos < screenPos.z) { if(samplePos < screenPos.z && (samplePos <= max(minZ,maxZ) && samplePos >= min(minZ,maxZ))){
vec2 linearZ = vec2(linearizeDepthFast(screenPos.z, _near, _far), linearizeDepthFast(samplePos, _near, _far));
vec2 linearZ = vec2(swapperlinZ(screenPos.z, _near, _far), swapperlinZ(samplePos, _near, _far));
float calcthreshold = abs(linearZ.x - linearZ.y) / linearZ.x; float calcthreshold = abs(linearZ.x - linearZ.y) / linearZ.x;
if (calcthreshold < 0.035) Shadow = 0.0; 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); return vec2(Shadow, SSS);
} }
@ -1227,9 +1247,11 @@ void main() {
// combine shadowmap with a minumum shadow determined by the screenspace shadows. // combine shadowmap with a minumum shadow determined by the screenspace shadows.
Shadows = min(Shadows, SS_directLight.r); 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 // 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 = mix(SS_directLight.g, ShadowBlockerDepth, shadowMapFalloff2);
// ShadowBlockerDepth = max( SS_directLight.g,0.0);
#endif #endif

View File

@ -338,8 +338,13 @@ vec4 TAA_hq(bool hand){
vec2 adjTC = texcoord; vec2 adjTC = texcoord;
#endif #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 //use velocity from the nearest texel from camera in a 3x3 box in order to improve edge quality in motion
#ifdef CLOSEST_VELOCITY #ifdef CLOSEST_VELOCITY
#ifdef DISTANT_HORIZONS #ifdef DISTANT_HORIZONS
@ -370,11 +375,11 @@ vec4 TAA_hq(bool hand){
//reject history if off-screen and early exit //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) 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 #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 // Interpolating neighboorhood clampling boundaries between pixels
vec3 cMax = texture2D(colortex0, adjTC).rgb; vec3 cMax = texture2D(colortex0, adjTC).rgb;
vec3 cMin = texture2D(colortex6, adjTC).rgb; vec3 cMin = texture2D(colortex6, adjTC).rgb;
@ -388,10 +393,12 @@ vec4 TAA_hq(bool hand){
vec3 albedoCurrent6 = texture2D(colortex3, adjTC + vec2(0.0,-texelSize.y)).rgb; vec3 albedoCurrent6 = texture2D(colortex3, adjTC + vec2(0.0,-texelSize.y)).rgb;
vec3 albedoCurrent7 = texture2D(colortex3, adjTC + vec2(-texelSize.x,0.0)).rgb; vec3 albedoCurrent7 = texture2D(colortex3, adjTC + vec2(-texelSize.x,0.0)).rgb;
vec3 albedoCurrent8 = 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 //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 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)))))); 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 #endif
#ifndef SCREENSHOT_MODE #ifndef SCREENSHOT_MODE
@ -441,7 +448,7 @@ void main() {
vec2 taauTC = clamp(texcoord*RENDER_SCALE, vec2(0.0), RENDER_SCALE - texelSize*2.0); 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; bool hand = abs(dataUnpacked-0.75) < 0.01 && texture2D(depthtex1,taauTC).x < 1.0;
vec4 color = TAA_hq(hand); 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; 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 //get at which length the ray intersects with the edge of the screen
vec3 maxLengths = (step(0.,direction)-clipPosition) / direction; vec3 maxLengths = (step(0.,direction)-clipPosition) / direction;
float mult = min(min(maxLengths.x,maxLengths.y),maxLengths.z)*2000.0; float mult = min(min(maxLengths.x,maxLengths.y),maxLengths.z)*2000.0;
vec3 stepv = direction/len; 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) #define saturate(x) clamp(x,0.0,1.0)