Skip to content

Commit 4166989

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 e9c9324 commit 4166989

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
@@ -1107,12 +1107,6 @@ void ProcessMaterialGeneric3D( Material* material, shaderStage_t* pStage, drawSu
11071107
material->tcGen_Lightmap = pStage->tcGen_Lightmap;
11081108
material->deformIndex = pStage->deformIndex;
11091109

1110-
colorGen_t rgbGen = SetRgbGen( pStage );
1111-
alphaGen_t alphaGen = SetAlphaGen( pStage );
1112-
1113-
material->useAttrColor = rgbGen == colorGen_t::CGEN_VERTEX || rgbGen == colorGen_t::CGEN_ONE_MINUS_VERTEX
1114-
|| alphaGen == alphaGen_t::AGEN_VERTEX || alphaGen == alphaGen_t::AGEN_ONE_MINUS_VERTEX;
1115-
11161110
gl_genericShaderMaterial->SetTCGenEnvironment( pStage->tcGen_Environment );
11171111
gl_genericShaderMaterial->SetTCGenLightmap( pStage->tcGen_Lightmap );
11181112

@@ -1138,14 +1132,6 @@ void ProcessMaterialLightMapping( Material* material, shaderStage_t* pStage, dra
11381132

11391133
DAEMON_ASSERT( !( enableDeluxeMapping && enableGridDeluxeMapping ) );
11401134

1141-
// useAttrColor has no effect since the lightMapping shader has ATTR_COLOR forced to be always
1142-
// on (_requiredVertexAttribs). If we removed ATTR_COLOR from there, we would need to detect
1143-
// implicit vertex lighting as well, not only rgbgen (see SetLightDeluxeMode).
1144-
/* colorGen_t rgbGen = SetRgbGen( pStage );
1145-
alphaGen_t alphaGen = SetAlphaGen( pStage );
1146-
material->useAttrColor = rgbGen == colorGen_t::CGEN_VERTEX || rgbGen == colorGen_t::CGEN_ONE_MINUS_VERTEX
1147-
|| alphaGen == alphaGen_t::AGEN_VERTEX || alphaGen == alphaGen_t::AGEN_ONE_MINUS_VERTEX; */
1148-
11491135
material->enableDeluxeMapping = enableDeluxeMapping;
11501136
material->enableGridLighting = enableGridLighting;
11511137
material->enableGridDeluxeMapping = enableGridDeluxeMapping;
@@ -2138,12 +2124,6 @@ void MaterialSystem::RenderMaterial( Material& material, const uint32_t viewID )
21382124

21392125
backEnd.currentEntity = &tr.worldEntity;
21402126

2141-
if ( material.useAttrColor ) {
2142-
material.shader->AddVertexAttribBit( ATTR_COLOR );
2143-
} else {
2144-
material.shader->DelVertexAttribBit( ATTR_COLOR );
2145-
}
2146-
21472127
GL_State( stateBits );
21482128
if ( material.usePolygonOffset ) {
21492129
glEnable( GL_POLYGON_OFFSET_FILL );

src/engine/renderer/Material.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,6 @@ struct Material {
100100
bool enableSpecularMapping;
101101
bool enablePhysicalMapping;
102102

103-
bool useAttrColor = false;
104-
105103
cullType_t cullType;
106104

107105
uint32_t sort;
@@ -120,8 +118,7 @@ struct Material {
120118

121119
bool operator==( const Material& other ) {
122120
return program == other.program && stateBits == other.stateBits && vbo == other.vbo && ibo == other.ibo
123-
&& fog == other.fog && cullType == other.cullType && usePolygonOffset == other.usePolygonOffset
124-
&& useAttrColor == other.useAttrColor;
121+
&& fog == other.fog && cullType == other.cullType && usePolygonOffset == other.usePolygonOffset;
125122
}
126123

127124
void AddTexture( Texture* texture ) {

src/engine/renderer/gl_shader.cpp

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

22222222
GLShader_generic::GLShader_generic( GLShaderManager *manager ) :
2223-
GLShader( "generic", ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT, manager ),
2223+
GLShader( "generic", ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT | ATTR_COLOR, manager ),
22242224
u_ColorMap( this ),
22252225
u_DepthMap( this ),
22262226
u_TextureMatrix( this ),
@@ -2252,7 +2252,7 @@ void GLShader_generic::SetShaderProgramUniforms( shaderProgram_t *shaderProgram
22522252
}
22532253

22542254
GLShader_genericMaterial::GLShader_genericMaterial( GLShaderManager* manager ) :
2255-
GLShader( "genericMaterial", "generic", true, ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT, manager ),
2255+
GLShader( "genericMaterial", "generic", true, ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT | ATTR_COLOR, manager ),
22562256
u_ColorMap( this ),
22572257
u_DepthMap( this ),
22582258
u_TextureMatrix( this ),

src/engine/renderer/gl_shader.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3664,7 +3664,8 @@ enum class ColorModulate {
36643664
COLOR_MINUS_ONE = BIT( 1 ),
36653665
COLOR_LIGHTFACTOR = BIT( 2 ),
36663666
ALPHA_ONE = BIT( 3 ),
3667-
ALPHA_MINUS_ONE = BIT( 4 )
3667+
ALPHA_MINUS_ONE = BIT( 4 ),
3668+
ALPHA_ADD_ONE = BIT( 5 )
36683669
};
36693670

36703671
class u_ColorModulateColorGen :
@@ -3694,7 +3695,7 @@ class u_ColorModulateColorGen :
36943695
// vertexOverbright is only needed for non-lightmapped cases. When there is a
36953696
// lightmap, this is done by multiplying with the overbright-scaled white image
36963697
colorModulate |= Util::ordinal( ColorModulate::COLOR_LIGHTFACTOR );
3697-
lightFactor = uint32_t( tr.mapLightFactor ) << 5;
3698+
lightFactor = uint32_t( tr.mapLightFactor ) << 6;
36983699
} else {
36993700
colorModulate |= Util::ordinal( ColorModulate::COLOR_ONE );
37003701
}
@@ -3711,10 +3712,10 @@ class u_ColorModulateColorGen :
37113712

37123713
if ( useMapLightFactor ) {
37133714
ASSERT_EQ( vertexOverbright, false );
3714-
lightFactor = uint32_t( tr.mapLightFactor ) << 5;
3715+
lightFactor = uint32_t( tr.mapLightFactor ) << 6;
37153716
}
37163717

3717-
colorModulate |= lightFactor ? lightFactor : 1 << 5;
3718+
colorModulate |= lightFactor ? lightFactor : 1 << 6;
37183719

37193720
switch ( alphaGen ) {
37203721
case alphaGen_t::AGEN_VERTEX:
@@ -3731,10 +3732,12 @@ class u_ColorModulateColorGen :
37313732
break;
37323733
}
37333734

3734-
if ( needAttrib ) {
3735-
_shader->AddVertexAttribBit( ATTR_COLOR );
3736-
} else {
3737-
_shader->DelVertexAttribBit( ATTR_COLOR );
3735+
if ( !needAttrib ) {
3736+
/* Originally, this controlled whether the vertex color array was used,
3737+
now it does the equivalent by setting the color in the shader in such way as if it was using
3738+
the default OpenGL values for the disabled arrays (0.0, 0.0, 0.0, 1.0)
3739+
This allows to skip the vertex format change */
3740+
colorModulate |= Util::ordinal( ColorModulate::ALPHA_ADD_ONE );
37383741
}
37393742
this->SetValue( colorModulate );
37403743
}

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)