Skip to content

Commit 650bb3f

Browse files
committed
tr_shader: do not collapse custom light stage and glow map
if a custome light stage is found, - do not collapse light stage with diffuse stage - do not collapse glow stage with diffuse stage - ensure diffuse < light < glow
1 parent d03d324 commit 650bb3f

3 files changed

Lines changed: 80 additions & 7 deletions

File tree

src/engine/renderer/tr_local.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,8 @@ static inline void halfToFloat( const f16vec4_t in, vec4_t out )
10881088
bool tcGen_Environment;
10891089
bool tcGen_Lightmap;
10901090

1091+
bool disableImplicitLightmap;
1092+
10911093
Color::Color32Bit constantColor; // for CGEN_CONST and AGEN_CONST
10921094

10931095
uint32_t stateBits; // GLS_xxxx mask

src/engine/renderer/tr_shade.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,14 @@ static void Render_lightMapping( int stage, bool asColorMap, bool normalMapping,
12621262
GL_BindToTMU( 4, tr.blackImage );
12631263
}
12641264

1265+
// do not paintover lightmap
1266+
// as a standalone lightmap stage
1267+
// will do later
1268+
if ( pStage->disableImplicitLightmap )
1269+
{
1270+
whiteLight = true;
1271+
}
1272+
12651273
// bind u_LightMap
12661274
BindLightMap( 3, whiteLight );
12671275

src/engine/renderer/tr_shader.cpp

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4309,18 +4309,81 @@ static void CollapseStages()
43094309
// disable since it's merged
43104310
stages[ materialStage ].active = false;
43114311
}
4312+
// always test for this stage before glow stage
43124313
if ( lightStage != -1 )
43134314
{
4314-
// TODO: investigate how to pass rgbGen/alphaGen to implicit lightmap stage
4315-
// disable to not paint it over implicit lightmap stage and glow map
4316-
stages[ lightStage ].active = false;
4315+
// “blendFunc filter” is same as “blendFunc GL_DST_COLOR GL_ZERO” and the default
4316+
if ( ( stages[ lightStage ].stateBits & GLS_SRCBLEND_BITS ) == GLS_SRCBLEND_DST_COLOR
4317+
&& ( stages[ lightStage ].stateBits & GLS_DSTBLEND_BITS ) == GLS_DSTBLEND_ZERO )
4318+
{
4319+
// common lightmap stage
4320+
// disable to not paint it over implicit light stage and glow map
4321+
stages[ lightStage ].active = false;
4322+
}
4323+
else
4324+
{
4325+
// custom lightmap stage, disable the implicit light stage and keep
4326+
// this one uncollapsed
4327+
Log::Debug("found custom lightmap stage in '%s' shader, not collapsing", shader.name);
4328+
stages[ diffuseStage ].disableImplicitLightmap = true;
4329+
}
43174330
}
4331+
// always test for this stage after light stage
43184332
if ( glowStage != -1 )
43194333
{
4320-
// merge with diffuse stage
4321-
stages[ diffuseStage ].bundle[ TB_GLOWMAP ] = stages[ glowStage ].bundle[ 0 ];
4322-
// disable since it's merged
4323-
stages[ glowStage ].active = false;
4334+
// if there is no custom light stage, collapse to the diffuse stage
4335+
// that will relie on the implicit light stage
4336+
if ( !stages[ diffuseStage ].disableImplicitLightmap )
4337+
{
4338+
// merge with diffuse stage
4339+
stages[ diffuseStage ].bundle[ TB_GLOWMAP ] = stages[ glowStage ].bundle[ 0 ];
4340+
// disable since it's merged
4341+
stages[ glowStage ].active = false;
4342+
}
4343+
// if there is a custom light stage, keep the glow stage uncollapsed
4344+
// and makes sure the diffuse stage precedes the light stage
4345+
// and the light stages precedes the glow stage
4346+
else
4347+
{
4348+
Log::Debug("found glow map with custom lightmap stage in '%s' shader, not collapsing", shader.name);
4349+
stages[ glowStage ].type = stageType_t::ST_COLORMAP;
4350+
stages[ glowStage ].bundle[ TB_COLORMAP ] = stages[ glowStage ].bundle[ 0 ];
4351+
4352+
// we can't be there without collapsing an already found
4353+
// diffuse stage that is then not using -1 index
4354+
// and since we are there because glow stage is found
4355+
// there is no need to secure it more
4356+
if ( glowStage < diffuseStage )
4357+
{
4358+
// swap stages
4359+
shaderStage_t tmpStage = stages [ diffuseStage ];
4360+
stages[ diffuseStage ] = stages [ glowStage ];
4361+
stages [ glowStage ] = tmpStage;
4362+
4363+
// swap stage indexes
4364+
int tmp = diffuseStage;
4365+
diffuseStage = glowStage;
4366+
glowStage = tmp;
4367+
}
4368+
// part of that code is not run if lightStage does not exist
4369+
// since disableImplicitLightmap is set when a custom lightStage
4370+
// exists and non-existent lightStage has -1 index so it can't
4371+
// be smaller than glowStage index, so no need to secure it more
4372+
if ( glowStage < lightStage )
4373+
{
4374+
// swap stages
4375+
shaderStage_t tmpStage = stages [ lightStage ];
4376+
stages[ lightStage ] = stages [ glowStage ];
4377+
stages [ glowStage ] = tmpStage;
4378+
4379+
// no need to swap indexes at this point
4380+
/*
4381+
int tmp = lightStage;
4382+
lightStage = glowStage;
4383+
glowStage = tmp;
4384+
*/
4385+
}
4386+
}
43244387
}
43254388
}
43264389
}

0 commit comments

Comments
 (0)