Skip to content

Commit 21ac5dc

Browse files
committed
Use colorModulateColorGen instead of disabling vertex color
Originally, vertex color array was disabled if colorGen CGEN_VERTEX or CGEN_ONE_MINUS_VERTEX or alphaGen CGEN_VERTEX or CGEN_ONE_MINUS_VERTEX were used, resulting in the shader receiving the default OpenGL values for the disabled arrays (0.0, 0.0, 0.0, 1.0). Now it will instead be set via setting a bit in `u_ColorModulateColorGen`, which allows skipping the vertex format change. It will also be necessary for the geometry cache. Also fixes incorrect lighting with `tr.mapOverBrightBits = 3`.
1 parent a8a5211 commit 21ac5dc

File tree

6 files changed

+23
-36
lines changed

6 files changed

+23
-36
lines changed

src/engine/renderer/Material.cpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,12 +1257,6 @@ void ProcessMaterialGeneric3D( Material* material, shaderStage_t* pStage, drawSu
12571257
material->tcGen_Lightmap = pStage->tcGen_Lightmap;
12581258
material->deformIndex = pStage->deformIndex;
12591259

1260-
colorGen_t rgbGen = SetRgbGen( pStage );
1261-
alphaGen_t alphaGen = SetAlphaGen( pStage );
1262-
1263-
material->useAttrColor = rgbGen == colorGen_t::CGEN_VERTEX || rgbGen == colorGen_t::CGEN_ONE_MINUS_VERTEX
1264-
|| alphaGen == alphaGen_t::AGEN_VERTEX || alphaGen == alphaGen_t::AGEN_ONE_MINUS_VERTEX;
1265-
12661260
gl_genericShaderMaterial->SetTCGenEnvironment( pStage->tcGen_Environment );
12671261
gl_genericShaderMaterial->SetTCGenLightmap( pStage->tcGen_Lightmap );
12681262

@@ -1288,14 +1282,6 @@ void ProcessMaterialLightMapping( Material* material, shaderStage_t* pStage, dra
12881282

12891283
DAEMON_ASSERT( !( enableDeluxeMapping && enableGridDeluxeMapping ) );
12901284

1291-
// useAttrColor has no effect since the lightMapping shader has ATTR_COLOR forced to be always
1292-
// on (_requiredVertexAttribs). If we removed ATTR_COLOR from there, we would need to detect
1293-
// implicit vertex lighting as well, not only rgbgen (see SetLightDeluxeMode).
1294-
/* colorGen_t rgbGen = SetRgbGen( pStage );
1295-
alphaGen_t alphaGen = SetAlphaGen( pStage );
1296-
material->useAttrColor = rgbGen == colorGen_t::CGEN_VERTEX || rgbGen == colorGen_t::CGEN_ONE_MINUS_VERTEX
1297-
|| alphaGen == alphaGen_t::AGEN_VERTEX || alphaGen == alphaGen_t::AGEN_ONE_MINUS_VERTEX; */
1298-
12991285
material->enableDeluxeMapping = enableDeluxeMapping;
13001286
material->enableGridLighting = enableGridLighting;
13011287
material->enableGridDeluxeMapping = enableGridDeluxeMapping;
@@ -2144,12 +2130,6 @@ void MaterialSystem::RenderMaterial( Material& material, const uint32_t viewID )
21442130

21452131
backEnd.currentEntity = &tr.worldEntity;
21462132

2147-
if ( material.useAttrColor ) {
2148-
material.shader->AddVertexAttribBit( ATTR_COLOR );
2149-
} else {
2150-
material.shader->DelVertexAttribBit( ATTR_COLOR );
2151-
}
2152-
21532133
GL_State( stateBits );
21542134
if ( material.usePolygonOffset ) {
21552135
glEnable( GL_POLYGON_OFFSET_FILL );

src/engine/renderer/Material.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,6 @@ struct Material {
108108
bool enableSpecularMapping;
109109
bool enablePhysicalMapping;
110110

111-
bool useAttrColor = false;
112-
113111
cullType_t cullType;
114112

115113
uint32_t sort;
@@ -128,8 +126,7 @@ struct Material {
128126

129127
bool operator==( const Material& other ) {
130128
return program == other.program && stateBits == other.stateBits && vbo == other.vbo && ibo == other.ibo
131-
&& fog == other.fog && cullType == other.cullType && usePolygonOffset == other.usePolygonOffset
132-
&& useAttrColor == other.useAttrColor;
129+
&& fog == other.fog && cullType == other.cullType && usePolygonOffset == other.usePolygonOffset;
133130
}
134131

135132
void AddTexture( Texture* texture ) {

src/engine/renderer/gl_shader.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,7 +2185,7 @@ void GLShader::WriteUniformsToBuffer( uint32_t* buffer ) {
21852185
}
21862186

21872187
GLShader_generic::GLShader_generic( GLShaderManager *manager ) :
2188-
GLShader( "generic", ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT, manager ),
2188+
GLShader( "generic", ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT | ATTR_COLOR, manager ),
21892189
u_ColorMap( this ),
21902190
u_DepthMap( this ),
21912191
u_TextureMatrix( this ),
@@ -2217,7 +2217,7 @@ void GLShader_generic::SetShaderProgramUniforms( shaderProgram_t *shaderProgram
22172217
}
22182218

22192219
GLShader_genericMaterial::GLShader_genericMaterial( GLShaderManager* manager ) :
2220-
GLShader( "genericMaterial", "generic", true, ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT, manager ),
2220+
GLShader( "genericMaterial", "generic", true, ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT | ATTR_COLOR, manager ),
22212221
u_ColorMap( this ),
22222222
u_DepthMap( this ),
22232223
u_TextureMatrix( this ),

src/engine/renderer/gl_shader.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3595,7 +3595,8 @@ enum class ColorModulate {
35953595
COLOR_MINUS_ONE = BIT( 1 ),
35963596
COLOR_LIGHTFACTOR = BIT( 2 ),
35973597
ALPHA_ONE = BIT( 3 ),
3598-
ALPHA_MINUS_ONE = BIT( 4 )
3598+
ALPHA_MINUS_ONE = BIT( 4 ),
3599+
ALPHA_ADD_ONE = BIT( 5 )
35993600
};
36003601

36013602
class u_ColorModulateColorGen :
@@ -3625,7 +3626,7 @@ class u_ColorModulateColorGen :
36253626
// vertexOverbright is only needed for non-lightmapped cases. When there is a
36263627
// lightmap, this is done by multiplying with the overbright-scaled white image
36273628
colorModulate |= Util::ordinal( ColorModulate::COLOR_LIGHTFACTOR );
3628-
lightFactor = uint32_t( tr.mapLightFactor ) << 5;
3629+
lightFactor = uint32_t( tr.mapLightFactor ) << 6;
36293630
} else {
36303631
colorModulate |= Util::ordinal( ColorModulate::COLOR_ONE );
36313632
}
@@ -3642,10 +3643,10 @@ class u_ColorModulateColorGen :
36423643

36433644
if ( useMapLightFactor ) {
36443645
ASSERT_EQ( vertexOverbright, false );
3645-
lightFactor = uint32_t( tr.mapLightFactor ) << 5;
3646+
lightFactor = uint32_t( tr.mapLightFactor ) << 6;
36463647
}
36473648

3648-
colorModulate |= lightFactor ? lightFactor : 1 << 5;
3649+
colorModulate |= lightFactor ? lightFactor : 1 << 6;
36493650

36503651
switch ( alphaGen ) {
36513652
case alphaGen_t::AGEN_VERTEX:
@@ -3662,10 +3663,12 @@ class u_ColorModulateColorGen :
36623663
break;
36633664
}
36643665

3665-
if ( needAttrib ) {
3666-
_shader->AddVertexAttribBit( ATTR_COLOR );
3667-
} else {
3668-
_shader->DelVertexAttribBit( ATTR_COLOR );
3666+
if ( !needAttrib ) {
3667+
/* Originally, this controlled whether the vertex color array was used,
3668+
now it does the equivalent by setting the color in the shader in such way as if it was using
3669+
the default OpenGL values for the disabled arrays (0.0, 0.0, 0.0, 1.0)
3670+
This allows to skip the vertex format change */
3671+
colorModulate |= Util::ordinal( ColorModulate::ALPHA_ADD_ONE );
36693672
}
36703673
this->SetValue( colorModulate );
36713674
}

src/engine/renderer/glsl_source/common.glsl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ Bit 1: color * ( -1 )
4848
Bit 2: color += lightFactor
4949
Bit 3: alpha * 1
5050
Bit 4: alpha * ( -1 )
51-
Bit 5-7: lightFactor */
51+
Bit 5: alpha = 1
52+
Bit 6-9: lightFactor */
5253

5354
float colorModArray[3] = float[3] ( 0.0f, 1.0f, -1.0f );
5455

@@ -65,5 +66,10 @@ vec4 ColorModulateToColor( const in uint colorMod, const in float lightFactor )
6566
}
6667

6768
float ColorModulateToLightFactor( const in uint colorMod ) {
68-
return ( colorMod >> 5 ) & 0x7;
69+
return ( colorMod >> 6 ) & 0xF;
70+
}
71+
72+
// This is used to skip vertex colours if the colorMod doesn't need them
73+
bool ColorModulateToVertexColor( const in uint colorMod ) {
74+
return ( colorMod & 0xFF ) == 0xFF;
6975
}

src/engine/renderer/glsl_source/generic_vp.glsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ void main()
6565

6666
VertexFetch( position, LB, color, texCoord, lmCoord );
6767
float lightFactor = ColorModulateToLightFactor( u_ColorModulateColorGen );
68+
color.a = ColorModulateToVertexColor( u_ColorModulateColorGen ) ? 1.0 : color.a;
6869
color = color * ColorModulateToColor( u_ColorModulateColorGen, lightFactor )
6970
+ unpackUnorm4x8( u_Color ) * vec4( lightFactor, lightFactor, lightFactor, 1.0 );
7071

0 commit comments

Comments
 (0)