Skip to content

Commit 53f5c73

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 0741011 commit 53f5c73

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
@@ -1087,12 +1087,6 @@ void ProcessMaterialGeneric3D( Material* material, shaderStage_t* pStage, drawSu
10871087
material->tcGen_Lightmap = pStage->tcGen_Lightmap;
10881088
material->deformIndex = pStage->deformIndex;
10891089

1090-
colorGen_t rgbGen = SetRgbGen( pStage );
1091-
alphaGen_t alphaGen = SetAlphaGen( pStage );
1092-
1093-
material->useAttrColor = rgbGen == colorGen_t::CGEN_VERTEX || rgbGen == colorGen_t::CGEN_ONE_MINUS_VERTEX
1094-
|| alphaGen == alphaGen_t::AGEN_VERTEX || alphaGen == alphaGen_t::AGEN_ONE_MINUS_VERTEX;
1095-
10961090
gl_genericShaderMaterial->SetTCGenEnvironment( pStage->tcGen_Environment );
10971091
gl_genericShaderMaterial->SetTCGenLightmap( pStage->tcGen_Lightmap );
10981092

@@ -1118,14 +1112,6 @@ void ProcessMaterialLightMapping( Material* material, shaderStage_t* pStage, dra
11181112

11191113
DAEMON_ASSERT( !( enableDeluxeMapping && enableGridDeluxeMapping ) );
11201114

1121-
// useAttrColor has no effect since the lightMapping shader has ATTR_COLOR forced to be always
1122-
// on (_requiredVertexAttribs). If we removed ATTR_COLOR from there, we would need to detect
1123-
// implicit vertex lighting as well, not only rgbgen (see SetLightDeluxeMode).
1124-
/* colorGen_t rgbGen = SetRgbGen( pStage );
1125-
alphaGen_t alphaGen = SetAlphaGen( pStage );
1126-
material->useAttrColor = rgbGen == colorGen_t::CGEN_VERTEX || rgbGen == colorGen_t::CGEN_ONE_MINUS_VERTEX
1127-
|| alphaGen == alphaGen_t::AGEN_VERTEX || alphaGen == alphaGen_t::AGEN_ONE_MINUS_VERTEX; */
1128-
11291115
material->enableDeluxeMapping = enableDeluxeMapping;
11301116
material->enableGridLighting = enableGridLighting;
11311117
material->enableGridDeluxeMapping = enableGridDeluxeMapping;
@@ -2150,12 +2136,6 @@ void MaterialSystem::RenderMaterial( Material& material, const uint32_t viewID )
21502136

21512137
backEnd.currentEntity = &tr.worldEntity;
21522138

2153-
if ( material.useAttrColor ) {
2154-
material.shader->AddVertexAttribBit( ATTR_COLOR );
2155-
} else {
2156-
material.shader->DelVertexAttribBit( ATTR_COLOR );
2157-
}
2158-
21592139
GL_State( stateBits );
21602140
if ( material.usePolygonOffset ) {
21612141
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
@@ -2252,7 +2252,7 @@ void GLShader::WriteUniformsToBuffer( uint32_t* buffer ) {
22522252
}
22532253

22542254
GLShader_generic::GLShader_generic( GLShaderManager *manager ) :
2255-
GLShader( "generic", ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT, manager ),
2255+
GLShader( "generic", ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT | ATTR_COLOR, manager ),
22562256
u_ColorMap( this ),
22572257
u_DepthMap( this ),
22582258
u_TextureMatrix( this ),
@@ -2284,7 +2284,7 @@ void GLShader_generic::SetShaderProgramUniforms( shaderProgram_t *shaderProgram
22842284
}
22852285

22862286
GLShader_genericMaterial::GLShader_genericMaterial( GLShaderManager* manager ) :
2287-
GLShader( "genericMaterial", "generic", true, ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT, manager ),
2287+
GLShader( "genericMaterial", "generic", true, ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT | ATTR_COLOR, manager ),
22882288
u_ColorMap( this ),
22892289
u_DepthMap( this ),
22902290
u_TextureMatrix( this ),

src/engine/renderer/gl_shader.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3494,7 +3494,8 @@ enum class ColorModulate {
34943494
COLOR_MINUS_ONE = BIT( 1 ),
34953495
COLOR_LIGHTFACTOR = BIT( 2 ),
34963496
ALPHA_ONE = BIT( 3 ),
3497-
ALPHA_MINUS_ONE = BIT( 4 )
3497+
ALPHA_MINUS_ONE = BIT( 4 ),
3498+
ALPHA_ADD_ONE = BIT( 5 )
34983499
};
34993500

35003501
class u_ColorModulateColorGen :
@@ -3524,7 +3525,7 @@ class u_ColorModulateColorGen :
35243525
// vertexOverbright is only needed for non-lightmapped cases. When there is a
35253526
// lightmap, this is done by multiplying with the overbright-scaled white image
35263527
colorModulate |= Util::ordinal( ColorModulate::COLOR_LIGHTFACTOR );
3527-
lightFactor = uint32_t( tr.mapLightFactor ) << 5;
3528+
lightFactor = uint32_t( tr.mapLightFactor ) << 6;
35283529
} else {
35293530
colorModulate |= Util::ordinal( ColorModulate::COLOR_ONE );
35303531
}
@@ -3541,10 +3542,10 @@ class u_ColorModulateColorGen :
35413542

35423543
if ( useMapLightFactor ) {
35433544
ASSERT_EQ( vertexOverbright, false );
3544-
lightFactor = uint32_t( tr.mapLightFactor ) << 5;
3545+
lightFactor = uint32_t( tr.mapLightFactor ) << 6;
35453546
}
35463547

3547-
colorModulate |= lightFactor ? lightFactor : 1 << 5;
3548+
colorModulate |= lightFactor ? lightFactor : 1 << 6;
35483549

35493550
switch ( alphaGen ) {
35503551
case alphaGen_t::AGEN_VERTEX:
@@ -3561,10 +3562,12 @@ class u_ColorModulateColorGen :
35613562
break;
35623563
}
35633564

3564-
if ( needAttrib ) {
3565-
_shader->AddVertexAttribBit( ATTR_COLOR );
3566-
} else {
3567-
_shader->DelVertexAttribBit( ATTR_COLOR );
3565+
if ( !needAttrib ) {
3566+
/* Originally, this controlled whether the vertex color array was used,
3567+
now it does the equivalent by setting the color in the shader in such way as if it was using
3568+
the default OpenGL values for the disabled arrays (0.0, 0.0, 0.0, 1.0)
3569+
This allows to skip the vertex format change */
3570+
colorModulate |= Util::ordinal( ColorModulate::ALPHA_ADD_ONE );
35683571
}
35693572
this->SetValue( colorModulate );
35703573
}

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
@@ -68,6 +68,7 @@ void main()
6868

6969
VertexFetch( position, LB, color, texCoord, lmCoord );
7070
float lightFactor = ColorModulateToLightFactor( u_ColorModulateColorGen );
71+
color.a = ColorModulateToVertexColor( u_ColorModulateColorGen ) ? 1.0 : color.a;
7172
color = color * ColorModulateToColor( u_ColorModulateColorGen, lightFactor )
7273
+ unpackUnorm4x8( u_Color ) * vec4( lightFactor, lightFactor, lightFactor, 1.0 );
7374

0 commit comments

Comments
 (0)