@@ -792,7 +792,7 @@ void MaterialSystem::GenerateWorldMaterialsBuffer() {
792792 uint32_t * materialsData = materialsSSBO.MapBufferRange ( offset );
793793 memset ( materialsData, 0 , offset * sizeof ( uint32_t ) );
794794
795- for ( uint32_t materialPackID = 0 ; materialPackID < 3 ; materialPackID++ ) {
795+ for ( uint32_t materialPackID = 0 ; materialPackID < 4 ; materialPackID++ ) {
796796 for ( Material& material : materialPacks[materialPackID].materials ) {
797797
798798 for ( drawSurf_t* drawSurf : material.drawSurfs ) {
@@ -960,7 +960,7 @@ void MaterialSystem::GenerateWorldCommandBuffer() {
960960 }
961961
962962 shader = shader->remappedShader ? shader->remappedShader : shader;
963- if ( shader->isSky || shader-> isPortal ) {
963+ if ( shader->isPortal ) {
964964 continue ;
965965 }
966966
@@ -1332,6 +1332,9 @@ void MaterialSystem::ProcessStage( drawSurf_t* drawSurf, shaderStage_t* pStage,
13321332 } else {
13331333 materialPack = 2 ;
13341334 }
1335+ if ( shader->isSky ) {
1336+ materialPack = 3 ;
1337+ }
13351338 uint32_t id = packIDs[materialPack];
13361339
13371340 // In surfaces with multiple stages each consecutive stage must be drawn after the previous stage,
@@ -1358,6 +1361,14 @@ void MaterialSystem::ProcessStage( drawSurf_t* drawSurf, shaderStage_t* pStage,
13581361 drawSurf->texturesDynamic [stage] = true ;
13591362 }
13601363
1364+ if ( shader->isSky ) {
1365+ if ( std::find ( skyShaders.begin (), skyShaders.end (), shader ) == skyShaders.end () ) {
1366+ skyShaders.emplace_back ( shader );
1367+ }
1368+
1369+ material.skyShader = shader;
1370+ }
1371+
13611372 pStage->materialProcessor ( &material, pStage, drawSurf );
13621373
13631374 std::vector<Material>& materials = materialPacks[materialPack].materials ;
@@ -1433,8 +1444,7 @@ void MaterialSystem::GenerateWorldMaterials() {
14331444
14341445 drawSurf_t* drawSurf;
14351446 totalDrawSurfs = 0 ;
1436-
1437- uint32_t packIDs[3 ] = { 0 , 0 , 0 };
1447+ uint32_t packIDs[4 ] = { 0 , 0 , 0 , 0 };
14381448
14391449 for ( int i = 0 ; i < tr.refdef .numDrawSurfs ; i++ ) {
14401450 drawSurf = &tr.refdef .drawSurfs [i];
@@ -1448,7 +1458,7 @@ void MaterialSystem::GenerateWorldMaterials() {
14481458 }
14491459
14501460 shader = shader->remappedShader ? shader->remappedShader : shader;
1451- if ( shader->isSky || shader-> isPortal ) {
1461+ if ( shader->isPortal ) {
14521462 continue ;
14531463 }
14541464
@@ -1981,9 +1991,67 @@ void MaterialSystem::RenderMaterials( const shaderSort_t fromSort, const shaderS
19811991 continue ;
19821992 }
19831993
1994+ // Use stencil buffer to avoid rendering stuff over the skybox that would normally be culled by the BSP
1995+ if ( backEnd.viewParms .portalLevel == 0 ) {
1996+ glEnable ( GL_STENCIL_TEST );
1997+ glStencilMask ( 0xff );
1998+ }
1999+
2000+ glStencilFunc ( GL_EQUAL, backEnd.viewParms .portalLevel , 0xff );
2001+ glStencilOp ( GL_KEEP, GL_KEEP, GL_INCR );
2002+
2003+ glState.glStateBitsMask = 0 ;
2004+
2005+ for ( Material& material : materialPacks[3 ].materials ) {
2006+ if ( material.skyShader == skyShader ) {
2007+ RenderMaterial ( material, viewID );
2008+ renderedMaterials.emplace_back ( &material );
2009+ }
2010+ }
2011+
2012+ glStencilFunc ( GL_EQUAL, backEnd.viewParms .portalLevel + 1 , 0xff );
2013+ glStencilOp ( GL_KEEP, GL_KEEP, GL_KEEP );
2014+
2015+ GL_State ( GLS_DEPTHMASK_TRUE | GLS_DEPTHFUNC_ALWAYS );
2016+ glState.glStateBitsMask = GLS_COLORMASK_BITS | GLS_DEPTHMASK_TRUE | GLS_DEPTHFUNC_ALWAYS;
2017+ glDepthRange ( 1.0 , 1.0 );
2018+
2019+ for ( Material& material : materialPacks[3 ].materials ) {
2020+ if ( material.skyShader == skyShader ) {
2021+ RenderMaterial ( material, viewID );
2022+ renderedMaterials.emplace_back ( &material );
2023+ }
2024+ }
2025+
2026+ // Actually draw the skybox
19842027 tr.drawingSky = true ;
19852028 Tess_Begin ( Tess_StageIteratorSky, skyShader, nullptr , false , -1 , 0 , false );
19862029 Tess_End ();
2030+
2031+ glStencilOp ( GL_KEEP, GL_KEEP, GL_DECR );
2032+
2033+ glState.glStateBitsMask = 0 ;
2034+ GL_State ( GLS_COLORMASK_BITS | GLS_DEPTHMASK_TRUE | GLS_DEPTHFUNC_ALWAYS );
2035+ glState.glStateBitsMask = GLS_COLORMASK_BITS | GLS_DEPTHMASK_TRUE | GLS_DEPTHFUNC_ALWAYS;
2036+ glDepthRange ( 1.0 , 1.0 );
2037+
2038+ for ( Material& material : materialPacks[3 ].materials ) {
2039+ if ( material.skyShader == skyShader ) {
2040+ RenderMaterial ( material, viewID );
2041+ renderedMaterials.emplace_back ( &material );
2042+ }
2043+ }
2044+
2045+ glStencilFunc ( GL_EQUAL, backEnd.viewParms .portalLevel , 0xff );
2046+ glStencilOp ( GL_KEEP, GL_KEEP, GL_KEEP );
2047+
2048+ glState.glStateBitsMask = 0 ;
2049+ GL_State ( GLS_DEFAULT );
2050+ glDepthRange ( 0.0 , 1.0 );
2051+
2052+ if ( backEnd.viewParms .portalLevel == 0 ) {
2053+ glDisable ( GL_STENCIL_TEST );
2054+ }
19872055 }
19882056 }
19892057}
0 commit comments