@@ -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