diff --git a/neo/renderer/GLMatrix.cpp b/neo/renderer/GLMatrix.cpp index 899a00d2..444b2602 100644 --- a/neo/renderer/GLMatrix.cpp +++ b/neo/renderer/GLMatrix.cpp @@ -495,6 +495,19 @@ void R_SetupProjectionMatrix( viewDef_t* viewDef, bool doJitter ) // const float zNear = ( viewDef->renderView.cramZNear ) ? ( r_znear.GetFloat() * 0.25f ) : r_znear.GetFloat(); + const int viewWidth = viewDef->viewport.x2 - viewDef->viewport.x1 + 1; + const int viewHeight = viewDef->viewport.y2 - viewDef->viewport.y1 + 1; + + // TODO integrate jitterx += viewDef->renderView.stereoScreenSeparation; + + // this mimics the logic in the Donut Feature Demo + const float xoffset = -2.0f * jitterx / ( 1.0f * viewWidth ); + const float yoffset = -2.0f * jittery / ( 1.0f * viewHeight ); + + float* projectionMatrix = doJitter ? viewDef->projectionMatrix : viewDef->unjitteredProjectionMatrix; + +#if 1 + float ymax = zNear * tan( viewDef->renderView.fov_y * idMath::PI / 360.0f ); float ymin = -ymax; @@ -504,34 +517,6 @@ void R_SetupProjectionMatrix( viewDef_t* viewDef, bool doJitter ) const float width = xmax - xmin; const float height = ymax - ymin; - const int viewWidth = viewDef->viewport.x2 - viewDef->viewport.x1 + 1; - const int viewHeight = viewDef->viewport.y2 - viewDef->viewport.y1 + 1; - -#if 0 - jitterx = jitterx * width / viewWidth; - jitterx += r_centerX.GetFloat(); - jitterx += viewDef->renderView.stereoScreenSeparation; - xmin += jitterx * width; - xmax += jitterx * width; - const float xoffset = ( xmax + xmin ) / width; // 0 without jitter - - jittery = jittery * height / viewHeight; - jittery += r_centerY.GetFloat(); - ymin += jittery * height; - ymax += jittery * height; - const float yoffset = ( ymax + ymin ) / height; - -#else - // this mimics the logic in the Donut / Feature Demo - const float xoffset = -2.0f * jitterx / ( 1.0f * viewWidth ); - const float yoffset = -2.0f * jittery / ( 1.0f * viewHeight ); -#endif - - // RB: IMPORTANT - the projectionMatrix has a few changes to make it work with Vulkan - // for a detailed explanation see https://matthewwellings.com/blog/the-new-vulkan-coordinate-system/ - - float* projectionMatrix = doJitter ? viewDef->projectionMatrix : viewDef->unjitteredProjectionMatrix; - projectionMatrix[0 * 4 + 0] = 2.0f * zNear / width; projectionMatrix[1 * 4 + 0] = 0.0f; projectionMatrix[2 * 4 + 0] = xoffset; @@ -558,6 +543,43 @@ void R_SetupProjectionMatrix( viewDef_t* viewDef, bool doJitter ) projectionMatrix[2 * 4 + 3] = -1.0f; projectionMatrix[3 * 4 + 3] = 0.0f; +#else + + // alternative Z for better precision in the distance + + float aspect = viewDef->renderView.fov_x / viewDef->renderView.fov_y; + + float yScale = 1.0f / ( tanf( 0.5f * DEG2RAD( viewDef->renderView.fov_y ) ) ); + float xScale = yScale / aspect; + + const float epsilon = 1.9073486328125e-6F; // 2^-19; + const float zFar = 160000; + + //float k = zFar / ( zFar - zNear ); + float k = 1.0f - epsilon; + + projectionMatrix[0 * 4 + 0] = xScale; + projectionMatrix[1 * 4 + 0] = 0.0f; + projectionMatrix[2 * 4 + 0] = xoffset; + projectionMatrix[3 * 4 + 0] = 0.0f; + + projectionMatrix[0 * 4 + 1] = 0.0f; + projectionMatrix[1 * 4 + 1] = yScale; + projectionMatrix[2 * 4 + 1] = yoffset; + projectionMatrix[3 * 4 + 1] = 0.0f; + + projectionMatrix[0 * 4 + 2] = 0.0f; + projectionMatrix[1 * 4 + 2] = 0.0f; + projectionMatrix[2 * 4 + 2] = -k; + projectionMatrix[3 * 4 + 2] = -k * zNear; + + projectionMatrix[0 * 4 + 3] = 0.0f; + projectionMatrix[1 * 4 + 3] = 0.0f; + projectionMatrix[2 * 4 + 3] = -1.0f; + projectionMatrix[3 * 4 + 3] = 0.0f; + +#endif + if( viewDef->renderView.flipProjection ) { projectionMatrix[1 * 4 + 1] = -projectionMatrix[1 * 4 + 1]; diff --git a/neo/renderer/Passes/SsaoPass.cpp b/neo/renderer/Passes/SsaoPass.cpp index 5ec20170..a1a85430 100644 --- a/neo/renderer/Passes/SsaoPass.cpp +++ b/neo/renderer/Passes/SsaoPass.cpp @@ -30,7 +30,7 @@ static idCVar r_ssaoBackgroundViewDepth( "r_ssaoBackgroundViewDepth", "100", CVAR_RENDERER | CVAR_FLOAT, "" ); static idCVar r_ssaoRadiusWorld( "r_ssaoRadiusWorld", "0.5", CVAR_RENDERER | CVAR_FLOAT, "" ); static idCVar r_ssaoSurfaceBias( "r_ssaoSurfaceBias", "0.1", CVAR_RENDERER | CVAR_FLOAT, "" ); -static idCVar r_ssaoPowerExponent( "r_ssaoSurfaceBias", "2", CVAR_RENDERER | CVAR_FLOAT, "" ); +static idCVar r_ssaoPowerExponent( "r_ssaoPowerExponent", "2", CVAR_RENDERER | CVAR_FLOAT, "" ); static idCVar r_ssaoBlurSharpness( "r_ssaoBlurSharpness", "16", CVAR_RENDERER | CVAR_FLOAT, "" ); static idCVar r_ssaoAmount( "r_ssaoAmount", "2", CVAR_RENDERER | CVAR_FLOAT, "" ); diff --git a/neo/shaders/builtin/SSAO/ssao_deinterleave.cs.hlsl b/neo/shaders/builtin/SSAO/ssao_deinterleave.cs.hlsl index e0b6331c..444f71fa 100644 --- a/neo/shaders/builtin/SSAO/ssao_deinterleave.cs.hlsl +++ b/neo/shaders/builtin/SSAO/ssao_deinterleave.cs.hlsl @@ -74,10 +74,22 @@ void main( uint3 globalId : SV_DispatchThreadID ) #if LINEAR_DEPTH float linearDepth = depth; #else - float4 clipPos = float4( 0, 0, depth, 1 ); + //float4 clipPos = float4( 0, 0, depth, 1 ); //float4 clipPos = float4( 0, 0, depth * 2.0 - 1.0, 1 ); + + // adjust depth + depth = depth * 2.0 - 1.0; + float4 clipPos = float4( 0, 0, depth, 1 ); + float4 viewPos = mul( clipPos, g_Ssao.matClipToView ); float linearDepth = viewPos.z / viewPos.w; + + // HACK: adjust linear depth to fit into [0 .. 16000] range + linearDepth += 0.35; + linearDepth = saturate( linearDepth ); + //linearDepth = 1.0 - linearDepth; // reverse depth + //linearDepth *= 4000; // zFar + //linearDepth *= DOOM_TO_METERS; #endif depths[y * 4 + x] = linearDepth; diff --git a/neo/shaders/builtin/gbuffer.ps.hlsl b/neo/shaders/builtin/gbuffer.ps.hlsl index 806bf961..efdd4dc1 100644 --- a/neo/shaders/builtin/gbuffer.ps.hlsl +++ b/neo/shaders/builtin/gbuffer.ps.hlsl @@ -66,7 +66,7 @@ void main( PS_IN fragment, out PS_OUT result ) localNormal.z = sqrt( 1.0f - dot3( localNormal, localNormal ) ); float3 globalNormal; -#if 1 +#if 0 globalNormal.x = dot3( localNormal, fragment.texcoord2 ); globalNormal.y = dot3( localNormal, fragment.texcoord3 ); globalNormal.z = dot3( localNormal, fragment.texcoord4 );