From 15a9411161e9bf0f65cba9a5d62660e209bdcd7f Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Thu, 15 Apr 2021 18:06:50 +0200 Subject: [PATCH] Changed lightgrid texture layout --- .../builtin/debug/lightgrid.ps.hlsl | 12 +-- .../lighting/ambient_lightgrid_IBL.ps.hlsl | 12 +-- neo/renderer/RenderProgs_embedded.h | 100 +++++++----------- neo/renderer/RenderWorld_lightgrid.cpp | 22 ++-- 4 files changed, 69 insertions(+), 77 deletions(-) diff --git a/base/renderprogs/builtin/debug/lightgrid.ps.hlsl b/base/renderprogs/builtin/debug/lightgrid.ps.hlsl index 02b61813..61b4bd40 100644 --- a/base/renderprogs/builtin/debug/lightgrid.ps.hlsl +++ b/base/renderprogs/builtin/debug/lightgrid.ps.hlsl @@ -91,11 +91,11 @@ void main( PS_IN fragment, out PS_OUT result ) float3 lightGridSize = rpJitterTexScale.xyz; int3 lightGridBounds = int3( rpJitterTexOffset.x, rpJitterTexOffset.y, rpJitterTexOffset.z ); - float invXY = ( 1.0 / ( lightGridBounds[0] * lightGridBounds[1] ) ); - float invZ = ( 1.0 / lightGridBounds[2] ); + float invXZ = ( 1.0 / ( lightGridBounds[0] * lightGridBounds[2] ) ); + float invY = ( 1.0 / lightGridBounds[1] ); - normalizedOctCoordZeroOne.x *= invXY; - normalizedOctCoordZeroOne.y *= invZ; + normalizedOctCoordZeroOne.x *= invXZ; + normalizedOctCoordZeroOne.y *= invY; int3 gridStep; @@ -105,8 +105,8 @@ void main( PS_IN fragment, out PS_OUT result ) int3 gridCoord = GetBaseGridCoord( globalPosition ); - normalizedOctCoordZeroOne.x += ( gridCoord[0] * gridStep[0] + gridCoord[1] * gridStep[1] ) * invXY; - normalizedOctCoordZeroOne.y += ( gridCoord[2] * invZ ); + normalizedOctCoordZeroOne.x += ( gridCoord[0] * gridStep[0] + gridCoord[2] * gridStep[1] ) * invXZ; + normalizedOctCoordZeroOne.y += ( gridCoord[1] * invY ); float4 envMap = tex2D( samp0, normalizedOctCoordZeroOne ); diff --git a/base/renderprogs/builtin/lighting/ambient_lightgrid_IBL.ps.hlsl b/base/renderprogs/builtin/lighting/ambient_lightgrid_IBL.ps.hlsl index 30932c86..c459e1a1 100644 --- a/base/renderprogs/builtin/lighting/ambient_lightgrid_IBL.ps.hlsl +++ b/base/renderprogs/builtin/lighting/ambient_lightgrid_IBL.ps.hlsl @@ -249,11 +249,11 @@ void main( PS_IN fragment, out PS_OUT result ) float3 lightGridSize = rpJitterTexScale.xyz; int3 lightGridBounds = int3( rpJitterTexOffset.x, rpJitterTexOffset.y, rpJitterTexOffset.z ); - float invXY = ( 1.0 / ( lightGridBounds[0] * lightGridBounds[1] ) ); - float invZ = ( 1.0 / lightGridBounds[2] ); + float invXZ = ( 1.0 / ( lightGridBounds[0] * lightGridBounds[2] ) ); + float invY = ( 1.0 / lightGridBounds[1] ); - normalizedOctCoordZeroOne.x *= invXY; - normalizedOctCoordZeroOne.y *= invZ; + normalizedOctCoordZeroOne.x *= invXZ; + normalizedOctCoordZeroOne.y *= invY; int3 gridCoord; float3 frac; @@ -328,8 +328,8 @@ void main( PS_IN fragment, out PS_OUT result ) float2 atlasOffset; - atlasOffset.x = ( gridCoord2[0] * gridStep[0] + gridCoord2[1] * gridStep[1] ) * invXY; - atlasOffset.y = ( gridCoord2[2] * invZ ); + atlasOffset.x = ( gridCoord2[0] * gridStep[0] + gridCoord2[2] * gridStep[1] ) * invXZ; + atlasOffset.y = ( gridCoord2[1] * invY ); irradiance += tex2D( samp7, normalizedOctCoordZeroOne + atlasOffset ).rgb * factor; diff --git a/neo/renderer/RenderProgs_embedded.h b/neo/renderer/RenderProgs_embedded.h index 644bdc9c..0cd0f58e 100644 --- a/neo/renderer/RenderProgs_embedded.h +++ b/neo/renderer/RenderProgs_embedded.h @@ -2811,12 +2811,12 @@ static const cgShaderDef_t cg_renderprogs[] = "// *INDENT-ON*\n" "\n" "\n" - "float3 lightGridOrigin = float3( -192.0, -128.0, 0 );\n" - "float3 lightGridSize = float3( 64.0, 64.0, 128.0 );\n" - "int3 lightGridBounds = int3( 7, 7, 3 );\n" - "\n" "int3 GetBaseGridCoord( float3 origin )\n" "{\n" + " float3 lightGridOrigin = rpGlobalLightOrigin.xyz;\n" + " float3 lightGridSize = rpJitterTexScale.xyz;\n" + " int3 lightGridBounds = int3( rpJitterTexOffset.x, rpJitterTexOffset.y, rpJitterTexOffset.z );\n" + "\n" " int3 pos;\n" "\n" " float3 lightOrigin = origin - lightGridOrigin;\n" @@ -2852,11 +2852,15 @@ static const cgShaderDef_t cg_renderprogs[] = " float2 normalizedOctCoord = octEncode( globalNormal );\n" " float2 normalizedOctCoordZeroOne = ( normalizedOctCoord + float2( 1.0 ) ) * 0.5;\n" "\n" - " float invXY = ( 1.0 / ( lightGridBounds[0] * lightGridBounds[1] ) );\n" - " float invZ = ( 1.0 / lightGridBounds[2] );\n" + " float3 lightGridOrigin = rpGlobalLightOrigin.xyz;\n" + " float3 lightGridSize = rpJitterTexScale.xyz;\n" + " int3 lightGridBounds = int3( rpJitterTexOffset.x, rpJitterTexOffset.y, rpJitterTexOffset.z );\n" "\n" - " normalizedOctCoordZeroOne.x *= invXY;\n" - " normalizedOctCoordZeroOne.y *= invZ;\n" + " float invXZ = ( 1.0 / ( lightGridBounds[0] * lightGridBounds[2] ) );\n" + " float invY = ( 1.0 / lightGridBounds[1] );\n" + "\n" + " normalizedOctCoordZeroOne.x *= invXZ;\n" + " normalizedOctCoordZeroOne.y *= invY;\n" "\n" " int3 gridStep;\n" "\n" @@ -2866,8 +2870,8 @@ static const cgShaderDef_t cg_renderprogs[] = "\n" " int3 gridCoord = GetBaseGridCoord( globalPosition );\n" "\n" - " normalizedOctCoordZeroOne.x += ( gridCoord[0] * gridStep[0] + gridCoord[1] * gridStep[1] ) * invXY;\n" - " normalizedOctCoordZeroOne.y += ( gridCoord[2] * invZ );\n" + " normalizedOctCoordZeroOne.x += ( gridCoord[0] * gridStep[0] + gridCoord[2] * gridStep[1] ) * invXZ;\n" + " normalizedOctCoordZeroOne.y += ( gridCoord[1] * invY );\n" "\n" " float4 envMap = tex2D( samp0, normalizedOctCoordZeroOne );\n" "\n" @@ -4798,6 +4802,7 @@ static const cgShaderDef_t cg_renderprogs[] = "};\n" "// *INDENT-ON*\n" "\n" + "\n" "// RB: TODO OPTIMIZE\n" "// this is a straight port of idBounds::RayIntersection\n" "bool AABBRayIntersection( float3 b[2], float3 start, float3 dir, out float scale )\n" @@ -4880,15 +4885,14 @@ static const cgShaderDef_t cg_renderprogs[] = "\n" " float3 globalPosition = fragment.texcoord7.xyz;\n" "\n" - " // RB: rpGlobalLightOrigin is global view origin\n" - " float3 globalEye = normalize( rpGlobalLightOrigin.xyz - globalPosition );\n" + " float3 globalView = normalize( rpGlobalEyePos.xyz - globalPosition );\n" "\n" - " float3 reflectionVector = globalNormal * dot3( globalEye, globalNormal );\n" - " reflectionVector = normalize( ( reflectionVector * 2.0f ) - globalEye );\n" + " float3 reflectionVector = globalNormal * dot3( globalView, globalNormal );\n" + " reflectionVector = normalize( ( reflectionVector * 2.0f ) - globalView );\n" "\n" "#if 1\n" " // parallax box correction using portal area bounds\n" - " float hitScale;\n" + " float hitScale = 0.0;\n" " float3 bounds[2];\n" " bounds[0].x = rpWobbleSkyX.x;\n" " bounds[0].y = rpWobbleSkyX.y;\n" @@ -4914,7 +4918,7 @@ static const cgShaderDef_t cg_renderprogs[] = " }\n" "#endif\n" "\n" - " half vDotN = saturate( dot3( globalEye, globalNormal ) );\n" + " half vDotN = saturate( dot3( globalView, globalNormal ) );\n" "\n" "#if defined( USE_PBR )\n" " const half metallic = specMapSRGB.g;\n" @@ -5296,36 +5300,6 @@ static const cgShaderDef_t cg_renderprogs[] = "// *INDENT-ON*\n" "\n" "\n" - "float3 lightGridOrigin = float3( -192.0, -128.0, 0 );\n" - "float3 lightGridSize = float3( 64.0, 64.0, 128.0 );\n" - "int3 lightGridBounds = int3( 7, 7, 3 );\n" - "\n" - "int3 GetBaseGridCoord( float3 origin )\n" - "{\n" - " int3 pos;\n" - "\n" - " float3 lightOrigin = origin - lightGridOrigin;\n" - " for( int i = 0; i < 3; i++ )\n" - " {\n" - " float v;\n" - "\n" - " v = lightOrigin[i] * ( 1.0f / lightGridSize[i] );\n" - " pos[i] = int( floor( v ) );\n" - "\n" - " if( pos[i] < 0 )\n" - " {\n" - " pos[i] = 0;\n" - " }\n" - " else if( pos[i] >= lightGridBounds[i] - 1 )\n" - " {\n" - " pos[i] = lightGridBounds[i] - 1;\n" - " }\n" - " }\n" - "\n" - " return pos;\n" - "}\n" - "\n" - "\n" "// RB: TODO OPTIMIZE\n" "// this is a straight port of idBounds::RayIntersection\n" "bool AABBRayIntersection( float3 b[2], float3 start, float3 dir, out float scale )\n" @@ -5408,15 +5382,14 @@ static const cgShaderDef_t cg_renderprogs[] = "\n" " float3 globalPosition = fragment.texcoord7.xyz;\n" "\n" - " // RB: rpGlobalLightOrigin is global view origin\n" - " float3 globalEye = normalize( rpGlobalLightOrigin.xyz - globalPosition );\n" + " float3 globalView = normalize( rpGlobalEyePos.xyz - globalPosition );\n" "\n" - " float3 reflectionVector = globalNormal * dot3( globalEye, globalNormal );\n" - " reflectionVector = normalize( ( reflectionVector * 2.0f ) - globalEye );\n" + " float3 reflectionVector = globalNormal * dot3( globalView, globalNormal );\n" + " reflectionVector = normalize( ( reflectionVector * 2.0f ) - globalView );\n" "\n" "#if 1\n" " // parallax box correction using portal area bounds\n" - " float hitScale;\n" + " float hitScale = 0.0;\n" " float3 bounds[2];\n" " bounds[0].x = rpWobbleSkyX.x;\n" " bounds[0].y = rpWobbleSkyX.y;\n" @@ -5442,7 +5415,7 @@ static const cgShaderDef_t cg_renderprogs[] = " }\n" "#endif\n" "\n" - " half vDotN = saturate( dot3( globalEye, globalNormal ) );\n" + " half vDotN = saturate( dot3( globalView, globalNormal ) );\n" "\n" "#if defined( USE_PBR )\n" " const half metallic = specMapSRGB.g;\n" @@ -5504,11 +5477,20 @@ static const cgShaderDef_t cg_renderprogs[] = " float2 normalizedOctCoordZeroOne = ( normalizedOctCoord + float2( 1.0 ) ) * 0.5;\n" "\n" "// lightgrid atlas\n" - " float invXY = ( 1.0 / ( lightGridBounds[0] * lightGridBounds[1] ) );\n" - " float invZ = ( 1.0 / lightGridBounds[2] );\n" "\n" - " normalizedOctCoordZeroOne.x *= invXY;\n" - " normalizedOctCoordZeroOne.y *= invZ;\n" + " //float3 lightGridOrigin = float3( -192.0, -128.0, 0 );\n" + " //float3 lightGridSize = float3( 64.0, 64.0, 128.0 );\n" + " //int3 lightGridBounds = int3( 7, 7, 3 );\n" + "\n" + " float3 lightGridOrigin = rpGlobalLightOrigin.xyz;\n" + " float3 lightGridSize = rpJitterTexScale.xyz;\n" + " int3 lightGridBounds = int3( rpJitterTexOffset.x, rpJitterTexOffset.y, rpJitterTexOffset.z );\n" + "\n" + " float invXZ = ( 1.0 / ( lightGridBounds[0] * lightGridBounds[2] ) );\n" + " float invY = ( 1.0 / lightGridBounds[1] );\n" + "\n" + " normalizedOctCoordZeroOne.x *= invXZ;\n" + " normalizedOctCoordZeroOne.y *= invY;\n" "\n" " int3 gridCoord;\n" " float3 frac;\n" @@ -5540,7 +5522,7 @@ static const cgShaderDef_t cg_renderprogs[] = " gridStep[2] = lightGridBounds[0] * lightGridBounds[1];\n" "\n" " float totalFactor = 0.0;\n" - " float3 irradiance;\n" + " float3 irradiance = float3( 0.0, 0.0, 0.0 );\n" "\n" " /*\n" " for( int i = 0; i < 8; i++ )\n" @@ -5583,8 +5565,8 @@ static const cgShaderDef_t cg_renderprogs[] = "\n" " float2 atlasOffset;\n" "\n" - " atlasOffset.x = ( gridCoord2[0] * gridStep[0] + gridCoord2[1] * gridStep[1] ) * invXY;\n" - " atlasOffset.y = ( gridCoord2[2] * invZ );\n" + " atlasOffset.x = ( gridCoord2[0] * gridStep[0] + gridCoord2[2] * gridStep[1] ) * invXZ;\n" + " atlasOffset.y = ( gridCoord2[1] * invY );\n" "\n" " irradiance += tex2D( samp7, normalizedOctCoordZeroOne + atlasOffset ).rgb * factor;\n" "\n" diff --git a/neo/renderer/RenderWorld_lightgrid.cpp b/neo/renderer/RenderWorld_lightgrid.cpp index 20eaa655..bb6ef4d2 100644 --- a/neo/renderer/RenderWorld_lightgrid.cpp +++ b/neo/renderer/RenderWorld_lightgrid.cpp @@ -670,10 +670,20 @@ CONSOLE_COMMAND( generateLightGrid, "Generate light grid data", NULL ) } */ +#if 1 + int a = tr.primaryWorld->PointInArea( tr.primaryView->renderView.vieworg ); + if( a == -1 ) + { + return; + } +#else for( int a = 0; a < tr.primaryWorld->NumAreas(); a++ ) +#endif { portalArea_t* area = &tr.primaryWorld->portalAreas[a]; + idLib::Printf( "Shooting %i grid probes area %i...\n\n", area->lightGrid.lightGridPoints.Num(), a ); + CommandlineProgressBar progressBar( area->lightGrid.lightGridPoints.Num() ); if( !useThreads ) { @@ -789,15 +799,15 @@ CONSOLE_COMMAND( generateLightGrid, "Generate light grid data", NULL ) - int atlasWidth = area->lightGrid.lightGridBounds[0] * area->lightGrid.lightGridBounds[1] * LIGHTGRID_IRRADIANCE_SIZE; - int atlasHeight = area->lightGrid.lightGridBounds[2] * LIGHTGRID_IRRADIANCE_SIZE; + int atlasWidth = area->lightGrid.lightGridBounds[0] * area->lightGrid.lightGridBounds[2] * LIGHTGRID_IRRADIANCE_SIZE; + int atlasHeight = area->lightGrid.lightGridBounds[1] * LIGHTGRID_IRRADIANCE_SIZE; idTempArray irradianceAtlas( atlasWidth * atlasHeight * 3 ); - // fill everything with solid red + // fill everything with solid black for( int i = 0; i < ( atlasWidth * atlasHeight ); i++ ) { - irradianceAtlas[i * 3 + 0] = F32toF16( 1.0f ); + irradianceAtlas[i * 3 + 0] = F32toF16( 0.0f ); irradianceAtlas[i * 3 + 1] = F32toF16( 0.0f ); irradianceAtlas[i * 3 + 2] = F32toF16( 0.0f ); } @@ -815,8 +825,8 @@ CONSOLE_COMMAND( generateLightGrid, "Generate light grid data", NULL ) { // gridPoint = lightGridPoints[ gridCoord[0] * gridStep[0] + gridCoord[1] * gridStep[1] + gridCoord[2] * gridStep[2] ]; - int xx = x + ( job->gridCoord[0] * gridStep[0] + job->gridCoord[1] * gridStep[1] ) * LIGHTGRID_IRRADIANCE_SIZE; - int yy = y + job->gridCoord[2] * LIGHTGRID_IRRADIANCE_SIZE; + int xx = x + ( job->gridCoord[0] * gridStep[0] + job->gridCoord[2] * gridStep[1] ) * LIGHTGRID_IRRADIANCE_SIZE; + int yy = y + job->gridCoord[1] * LIGHTGRID_IRRADIANCE_SIZE; irradianceAtlas[( yy * atlasWidth + xx ) * 3 + 0] = job->outBuffer[( y * LIGHTGRID_IRRADIANCE_SIZE + x ) * 3 + 0]; irradianceAtlas[( yy * atlasWidth + xx ) * 3 + 1] = job->outBuffer[( y * LIGHTGRID_IRRADIANCE_SIZE + x ) * 3 + 1];