Proper fix for the SMP

Proper fix for the SMP

diff --git a/src/engine/GPURenderer/r_backend.cpp b/src/engine/GPURenderer/r_backend.cpp
index 2ac2101..054ce57 100644
--- a/src/engine/GPURenderer/r_backend.cpp
+++ b/src/engine/GPURenderer/r_backend.cpp
@@ -30,7 +30,7 @@
 
 #include <framework/precompiled.h>
 
-backEndData_t* backEndData[SMP_FRAMES];
+backEndData_t* backEndData;
 backEndState_t	backEnd;
 
 static float32	s_flipMatrix[16] =
@@ -959,12 +959,6 @@ static const void* RB_PrefilterEnvMap( const void* data )
         return ( const void* )( cmd + 1 );
     }
     
-    sint cubeMipSize = cubemap->image->width;
-    sint numMips = 0;
-    
-    sint width = cubemap->image->width;
-    sint height = cubemap->image->height;
-    
     vec4_t quadVerts[4];
     vec2_t texCoords[4];
     
@@ -982,39 +976,30 @@ static const void* RB_PrefilterEnvMap( const void* data )
     texCoords[3][0] = 0;
     texCoords[3][1] = 1;
     
-    while( cubeMipSize )
-    {
-        cubeMipSize >>= 1;
-        numMips++;
-    }
-    numMips = MAX( 1, numMips - 2 );
-    
     FBO_Bind( tr.preFilterEnvMapFbo );
     GL_BindToTMU( cubemap->image, TB_CUBEMAP );
     
     GLSL_BindProgram( &tr.prefilterEnvMapShader );
     
-    for( sint level = 1; level <= numMips; level++ )
+    sint width = cubemap->image->width;
+    sint height = cubemap->image->height;
+    
+    for( sint level = 1; level <= r_cubemapSize->integer; level++ )
     {
         width = width / 2.0;
         height = height / 2.0;
-        
         qglViewport( 0, 0, width, height );
         qglScissor( 0, 0, width, height );
-        
         for( sint cubemapSide = 0; cubemapSide < 6; cubemapSide++ )
         {
             vec4_t viewInfo;
-            
-            VectorSet4( viewInfo, cubemapSide, level, numMips, 0.0 );
+            VectorSet4( viewInfo, cubemapSide, level, r_cubemapSize->integer, ( level / ( float32 )r_cubemapSize->integer ) );
             GLSL_SetUniformVec4( &tr.prefilterEnvMapShader, UNIFORM_VIEWINFO, viewInfo );
             RB_InstantQuad2( quadVerts, texCoords );
             qglCopyTexSubImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X + cubemapSide, level, 0, 0, 0, 0, width, height );
         }
     }
     
-    //qglActiveTexture( GL_TEXTURE0 );
-    
     return ( const void* )( cmd + 1 );
 }
 
@@ -2008,15 +1993,6 @@ void RB_ExecuteRenderCommands( const void* data )
     
     t1 = CL_ScaledMilliseconds();
     
-    if( !r_smp->integer || data == backEndData[0]->commands.cmds )
-    {
-        backEnd.smpFrame = 0;
-    }
-    else
-    {
-        backEnd.smpFrame = 1;
-    }
-    
     while( 1 )
     {
         data = PADP( data, sizeof( void* ) );
diff --git a/src/engine/GPURenderer/r_cmds.cpp b/src/engine/GPURenderer/r_cmds.cpp
index 360c39d..c0fe54e 100644
--- a/src/engine/GPURenderer/r_cmds.cpp
+++ b/src/engine/GPURenderer/r_cmds.cpp
@@ -106,7 +106,7 @@ R_IssueRenderCommands
 void R_IssueRenderCommands( bool runPerformanceCounters )
 {
     static sint c_blockedOnRender, c_blockedOnMain;
-    renderCommandList_t* cmdList = &backEndData[tr.smpFrame]->commands;
+    renderCommandList_t* cmdList = &backEndData->commands[tr.smpFrame];
     assert( cmdList );
     
     // add an end-of-list command
@@ -196,7 +196,7 @@ void* R_GetCommandBufferReserved( sint bytes, sint reservedBytes )
 {
     renderCommandList_t*	cmdList;
     
-    cmdList = &backEndData[tr.smpFrame]->commands;
+    cmdList = &backEndData->commands[tr.smpFrame];
     bytes = PAD( bytes, sizeof( void* ) );
     
     // always leave room for the end of list command
@@ -751,6 +751,11 @@ void idRenderSystemLocal::EndFrame( sint* frontEndMsec, sint* backEndMsec )
     
     R_IssueRenderCommands( true );
     
+    if( glConfig.smpActive )
+    {
+        GLimp_FrontEndSleep();
+    }
+    
     R_InitNextFrame();
     
     if( frontEndMsec )
diff --git a/src/engine/GPURenderer/r_glimp.cpp b/src/engine/GPURenderer/r_glimp.cpp
index 44859a6..101b8ae 100644
--- a/src/engine/GPURenderer/r_glimp.cpp
+++ b/src/engine/GPURenderer/r_glimp.cpp
@@ -1301,6 +1301,8 @@ static SDL_cond* renderCommandsEvent = nullptr;
 static SDL_cond* renderCompletedEvent = nullptr;
 static void ( *glimpRenderThread )( void ) = nullptr;
 static SDL_Thread* renderThread = nullptr;
+static volatile void* smpData = nullptr;
+static volatile bool smpDataReady;
 
 /*
 ===============
@@ -1414,12 +1416,17 @@ bool GLimp_SpawnRenderThread( void ( *function )( void ) )
         return false;
     }
     
+    SDL_LockMutex( smpMutex );
+    while( smpData )
+    {
+        SDL_CondWait( renderCompletedEvent, smpMutex );
+    }
+    
+    SDL_UnlockMutex( smpMutex );
+    
     return true;
 }
 
-static volatile void* smpData = nullptr;
-static volatile bool smpDataReady;
-
 /*
 ===============
 GLimp_RendererSleep
diff --git a/src/engine/GPURenderer/r_glsl.cpp b/src/engine/GPURenderer/r_glsl.cpp
index 66dff51..c202c7d 100644
--- a/src/engine/GPURenderer/r_glsl.cpp
+++ b/src/engine/GPURenderer/r_glsl.cpp
@@ -362,6 +362,8 @@ static void GLSL_GetShaderHeader( uint shaderType, pointer extra, valueType* des
         Q_strcat( dest, size, va( "#define ROUGHNESS_MIPS float(%d)\n", numRoughnessMips ) );
     }
     
+    Q_strcat( dest, size, va( "#define CUBEMAP_RESOLUTION float(%i)\n", r_cubemapSize->integer ) );
+    
     if( r_horizonFade->integer )
     {
         float32 fade = 1 + ( 0.1 * r_horizonFade->integer );
diff --git a/src/engine/GPURenderer/r_init.cpp b/src/engine/GPURenderer/r_init.cpp
index aef7348..01026fa 100644
--- a/src/engine/GPURenderer/r_init.cpp
+++ b/src/engine/GPURenderer/r_init.cpp
@@ -1555,23 +1555,10 @@ void R_Init( void )
     if( max_polyverts < MAX_POLYVERTS )
         max_polyverts = MAX_POLYVERTS;
         
-    ptr = ( uchar8* )( Hunk_Alloc( sizeof( *backEndData[0] ) + sizeof( srfPoly_t ) * max_polys + sizeof( polyVert_t ) * max_polyverts, h_low ) );
-    backEndData[0] = ( backEndData_t* )ptr;
-    backEndData[0]->polys = ( srfPoly_t* )( ( valueType* )( ptr ) + sizeof( *backEndData[0] ) );
-    backEndData[0]->polyVerts = ( polyVert_t* )( ( valueType* )( ptr ) + sizeof( *backEndData[0] ) + sizeof( srfPoly_t ) * max_polys );
-    
-    if( r_smp->integer )
-    {
-        ptr = ( uchar8* )( Hunk_Alloc( sizeof( *backEndData[1] ) + sizeof( srfPoly_t ) * max_polys + sizeof( polyVert_t ) * max_polyverts, h_low ) );
-        backEndData[1] = ( backEndData_t* )ptr;
-        backEndData[1]->polys = ( srfPoly_t* )( ( valueType* )( ptr ) + sizeof( *backEndData[1] ) );
-        backEndData[1]->polyVerts = ( polyVert_t* )( ( valueType* )( ptr ) + sizeof( *backEndData[1] ) + sizeof( srfPoly_t ) * max_polys );
-    }
-    else
-    {
-        backEndData[1] = nullptr;
-    }
-    
+    ptr = ( uchar8* )( Hunk_Alloc( sizeof( *backEndData ) + sizeof( srfPoly_t ) * max_polys + sizeof( polyVert_t ) * max_polyverts, h_low ) );
+    backEndData = ( backEndData_t* )ptr;
+    backEndData->polys = ( srfPoly_t* )( ( valueType* )( ptr ) + sizeof( *backEndData ) );
+    backEndData->polyVerts = ( polyVert_t* )( ( valueType* )( ptr ) + sizeof( *backEndData ) + sizeof( srfPoly_t ) * max_polys );
     
     R_InitNextFrame();
     
diff --git a/src/engine/GPURenderer/r_light.cpp b/src/engine/GPURenderer/r_light.cpp
index c09f6ec..9e3bee8 100644
--- a/src/engine/GPURenderer/r_light.cpp
+++ b/src/engine/GPURenderer/r_light.cpp
@@ -117,7 +117,7 @@ void R_DlightBmodel( bmodel_t* bmodel )
             case SF_FACE:
             case SF_GRID:
             case SF_TRIANGLES:
-                ( ( srfBspSurface_t* )surf->data )->dlightBits[tr.smpFrame] = mask;
+                ( ( srfBspSurface_t* )surf->data )->dlightBits = mask;
                 break;
                 
             default:
diff --git a/src/engine/GPURenderer/r_local.h b/src/engine/GPURenderer/r_local.h
index 6850789..34e804e 100644
--- a/src/engine/GPURenderer/r_local.h
+++ b/src/engine/GPURenderer/r_local.h
@@ -1005,7 +1005,7 @@ typedef struct srfBspSurface_s
     surfaceType_t   surfaceType;
     

[... diff too long, it was truncated ...]

GitHub
sha: a529664c