Skip to content

Commit

Permalink
Cleanup R_BuildCubeMaps()
Browse files Browse the repository at this point in the history
  • Loading branch information
VReaperV committed Sep 22, 2024
1 parent 65f4443 commit f0fe22c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 116 deletions.
144 changes: 29 additions & 115 deletions src/engine/renderer/tr_bsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6501,18 +6501,6 @@ void R_FindTwoNearestCubeMaps( const vec3_t position, cubemapProbe_t **cubeProbe

void R_BuildCubeMaps()
{
int i;
int ii, jj;
bool flipx;
bool flipy;
int x, y, xy, xy2;

byte *dest;

int startTime, endTime;
size_t tics = 0;
size_t nextTicCount = 0;

// Early abort if a BSP is not loaded yet since
// the buildcubemaps command can be called from
// everywhere including the main menu.
Expand All @@ -6527,12 +6515,11 @@ void R_BuildCubeMaps()
return;
}

startTime = ri.Milliseconds();
const int startTime = ri.Milliseconds();

refdef_t rf{};

byte* temp = ( byte* ) Z_Malloc( cubeMapSize * cubeMapSize * 4 );
for ( i = 0; i < 6; i++ )
for ( int i = 0; i < 6; i++ )
{
tr.cubeTemp[ i ] = (byte*) Z_Malloc( ( size_t ) cubeMapSize * cubeMapSize * 4 );
}
Expand All @@ -6544,7 +6531,7 @@ void R_BuildCubeMaps()
{
bspNode_t *node;

for ( i = 0; i < tr.world->numnodes; i++ )
for ( int i = 0; i < tr.world->numnodes; i++ )
{
node = &tr.world->nodes[ i ];

Expand All @@ -6566,7 +6553,7 @@ void R_BuildCubeMaps()

if ( FindVertexInHashTable( tr.cubeHashTable, origin, 256 ) == nullptr )
{
auto *cubeProbe = (cubemapProbe_t*) ri.Hunk_Alloc( sizeof( cubemapProbe_t ), ha_pref::h_high );
cubemapProbe_t* cubeProbe = (cubemapProbe_t*) ri.Hunk_Alloc( sizeof( cubemapProbe_t ), ha_pref::h_high );
tr.cubeProbes.push_back( cubeProbe );

VectorCopy( origin, cubeProbe->origin );
Expand All @@ -6585,43 +6572,14 @@ void R_BuildCubeMaps()
VectorClear( cubeProbe->origin );
}

Log::Notice("...pre-rendering %d cubemaps", tr.cubeProbes.size() );
Log::Notice("0%% 10 20 30 40 50 60 70 80 90 100%%" );
Log::Notice("|----|----|----|----|----|----|----|----|----|----|" );
Log::Notice( "...pre-rendering %d cubemaps", tr.cubeProbes.size() );

const bool gpuOcclusionCulling = r_gpuOcclusionCulling.Get();
r_gpuOcclusionCulling.Set( false );

for ( size_t j = 0; j < tr.cubeProbes.size(); j++ )
for ( size_t i = 0; i < tr.cubeProbes.size(); i++ )
{
cubemapProbe_t *cubeProbe = tr.cubeProbes[ j ];

//Log::Notice("rendering cubemap at (%i %i %i)", (int)cubeProbe->origin[0], (int)cubeProbe->origin[1],
// (int)cubeProbe->origin[2]);

if ( ( j + 1 ) >= nextTicCount )
{
size_t ticsNeeded = ( size_t )( ( ( double )( j + 1 ) / tr.cubeProbes.size() ) * 50.0 );

do
{
Log::Notice("*");
Cmd::ExecuteCommand("updatescreen");
}
while ( ++tics < ticsNeeded );

nextTicCount = ( size_t )( ( tics / 50.0 ) * tr.cubeProbes.size() );

if ( ( j + 1 ) == tr.cubeProbes.size() )
{
if ( tics < 51 )
{
Log::Notice("*");
}

Log::Notice("");
}
}
cubemapProbe_t *cubeProbe = tr.cubeProbes[ i ];

VectorCopy( cubeProbe->origin, rf.vieworg );

Expand All @@ -6642,12 +6600,10 @@ void R_BuildCubeMaps()

rf.rdflags = RDF_NOCUBEMAP | RDF_NOBLOOM;

for ( i = 0; i < 6; i++ )
for ( int j = 0; j < 6; j++ )
{
flipx = false;
flipy = false;

switch ( i )
switch ( j )
{
case 0:
{
Expand All @@ -6661,7 +6617,6 @@ void R_BuildCubeMaps()
rf.viewaxis[ 1 ][ 2 ] = 1;

CrossProduct( rf.viewaxis[ 0 ], rf.viewaxis[ 1 ], rf.viewaxis[ 2 ] );
//flipx=true;
break;
}

Expand All @@ -6677,7 +6632,6 @@ void R_BuildCubeMaps()
rf.viewaxis[ 1 ][ 2 ] = -1;

CrossProduct( rf.viewaxis[ 0 ], rf.viewaxis[ 1 ], rf.viewaxis[ 2 ] );
//flipx=true;
break;
}

Expand All @@ -6693,7 +6647,6 @@ void R_BuildCubeMaps()
rf.viewaxis[ 1 ][ 2 ] = 0;

CrossProduct( rf.viewaxis[ 0 ], rf.viewaxis[ 1 ], rf.viewaxis[ 2 ] );
//flipx=true;
break;
}

Expand All @@ -6704,12 +6657,11 @@ void R_BuildCubeMaps()
rf.viewaxis[ 0 ][ 1 ] = -1;
rf.viewaxis[ 0 ][ 2 ] = 0;

rf.viewaxis[ 1 ][ 0 ] = -1; //-1
rf.viewaxis[ 1 ][ 0 ] = -1;
rf.viewaxis[ 1 ][ 1 ] = 0;
rf.viewaxis[ 1 ][ 2 ] = 0;

CrossProduct( rf.viewaxis[ 0 ], rf.viewaxis[ 1 ], rf.viewaxis[ 2 ] );
//flipx=true;
break;
}

Expand All @@ -6725,7 +6677,6 @@ void R_BuildCubeMaps()
rf.viewaxis[ 1 ][ 2 ] = 0;

CrossProduct( rf.viewaxis[ 0 ], rf.viewaxis[ 1 ], rf.viewaxis[ 2 ] );
// flipx=true;
break;
}

Expand All @@ -6741,82 +6692,45 @@ void R_BuildCubeMaps()
rf.viewaxis[ 1 ][ 2 ] = 0;

CrossProduct( rf.viewaxis[ 0 ], rf.viewaxis[ 1 ], rf.viewaxis[ 2 ] );
//flipx=true;
break;
}
}

tr.refdef.pixelTarget = tr.cubeTemp[ i ];
memset( tr.cubeTemp[ i ], 255, ( size_t ) cubeMapSize * cubeMapSize * 4 );
tr.refdef.pixelTarget = tr.cubeTemp[ j ];
memset( tr.cubeTemp[ j ], 255, ( size_t ) cubeMapSize * cubeMapSize * 4 );
tr.refdef.pixelTargetWidth = cubeMapSize;
tr.refdef.pixelTargetHeight = cubeMapSize;

int msecUnused1;
int msecUnused2;
if ( glConfig2.materialSystemAvailable ) {
tr.refdef.pixelTarget = nullptr;

RE_BeginFrame();
RE_RenderScene( &rf );
RE_EndFrame( &ii, &jj );
RE_EndFrame( &msecUnused1, &msecUnused2 );

tr.refdef.pixelTarget = tr.cubeTemp[i];
tr.refdef.pixelTarget = tr.cubeTemp[j];
}

RE_BeginFrame();
RE_RenderScene( &rf );
RE_EndFrame( &ii, &jj );

if ( flipx )
{
dest = tr.cubeTemp[ i ];
memcpy( temp, dest, cubeMapSize * cubeMapSize * 4 );

for ( y = 0; y < cubeMapSize; y++ )
{
for ( x = 0; x < cubeMapSize; x++ )
{
xy = ( ( y * cubeMapSize ) + x ) * 4;
xy2 = ( ( y * cubeMapSize ) + ( ( cubeMapSize - 1 ) - x ) ) * 4;
dest[ xy2 + 0 ] = temp[ xy + 0 ];
dest[ xy2 + 1 ] = temp[ xy + 1 ];
dest[ xy2 + 2 ] = temp[ xy + 2 ];
dest[ xy2 + 3 ] = temp[ xy + 3 ];
}
}
}

if ( flipy )
{
dest = tr.cubeTemp[ i ];
memcpy( temp, dest, cubeMapSize * cubeMapSize * 4 );

for ( y = 0; y < cubeMapSize; y++ )
{
for ( x = 0; x < cubeMapSize; x++ )
{
xy = ( ( y * cubeMapSize ) + x ) * 4;
xy2 = ( ( ( ( cubeMapSize - 1 ) - y ) * cubeMapSize ) + x ) * 4;
dest[ xy2 + 0 ] = temp[ xy + 0 ];
dest[ xy2 + 1 ] = temp[ xy + 1 ];
dest[ xy2 + 2 ] = temp[ xy + 2 ];
dest[ xy2 + 3 ] = temp[ xy + 3 ];
}
}
}
RE_EndFrame( &msecUnused1, &msecUnused2 );

// encode the pixel intensity into the alpha channel, saves work in the shader
byte r, g, b, best;
byte best;

dest = tr.cubeTemp[ i ];
byte* dest = tr.cubeTemp[ j ];

for ( y = 0; y < cubeMapSize; y++ )
for ( int y = 0; y < cubeMapSize; y++ )
{
for ( x = 0; x < cubeMapSize; x++ )
for ( int x = 0; x < cubeMapSize; x++ )
{
xy = ( ( y * cubeMapSize ) + x ) * 4;
int xy = ( ( y * cubeMapSize ) + x ) * 4;

r = dest[ xy + 0 ];
g = dest[ xy + 1 ];
b = dest[ xy + 2 ];
const byte r = dest[ xy + 0 ];
const byte g = dest[ xy + 1 ];
const byte b = dest[ xy + 2 ];

if ( ( r > g ) && ( r > b ) )
{
Expand All @@ -6837,7 +6751,7 @@ void R_BuildCubeMaps()
}

// build the cubemap
cubeProbe->cubemap = R_AllocImage( Str::Format( "_autoCube%d", j ).c_str(), false);
cubeProbe->cubemap = R_AllocImage( Str::Format( "_autoCube%d", i ).c_str(), false );

if ( !cubeProbe->cubemap )
{
Expand All @@ -6860,14 +6774,14 @@ void R_BuildCubeMaps()

r_gpuOcclusionCulling.Set( gpuOcclusionCulling );

Log::Notice("");
Log::Notice( "" );

// turn pixel targets off
tr.refdef.pixelTarget = nullptr;

// assign the surfs a cubemap
endTime = ri.Milliseconds();
Log::Notice("cubemap probes pre-rendering time of %i cubes = %5.2f seconds", tr.cubeProbes.size(),
const int endTime = ri.Milliseconds();
Log::Notice( "Cubemap probes pre-rendering time of %d cubes = %5.2f seconds", tr.cubeProbes.size(),
( endTime - startTime ) / 1000.0 );
}

Expand Down
1 change: 0 additions & 1 deletion src/engine/renderer/tr_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,6 @@ enum class realtimeLightingRenderer_t { LEGACY, TILED };

#define REF_CUBEMAP_SIZE 32
#define REF_CUBEMAP_STORE_SIZE 1024
#define REF_CUBEMAP_STORE_SIDE ( REF_CUBEMAP_STORE_SIZE / REF_CUBEMAP_SIZE )

#define REF_COLORGRADE_SLOTS 4
#define REF_COLORGRADEMAP_SIZE 16
Expand Down

0 comments on commit f0fe22c

Please sign in to comment.