Skip to content

Commit

Permalink
Use colorModulateColorGen instead of disabling vertex color
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
VReaperV committed Jan 26, 2025
1 parent 3265f2d commit 57bcbde
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 36 deletions.
20 changes: 0 additions & 20 deletions src/engine/renderer/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1257,12 +1257,6 @@ void ProcessMaterialGeneric3D( Material* material, shaderStage_t* pStage, drawSu
material->tcGen_Lightmap = pStage->tcGen_Lightmap;
material->deformIndex = pStage->deformIndex;

colorGen_t rgbGen = SetRgbGen( pStage );
alphaGen_t alphaGen = SetAlphaGen( pStage );

material->useAttrColor = rgbGen == colorGen_t::CGEN_VERTEX || rgbGen == colorGen_t::CGEN_ONE_MINUS_VERTEX
|| alphaGen == alphaGen_t::AGEN_VERTEX || alphaGen == alphaGen_t::AGEN_ONE_MINUS_VERTEX;

gl_genericShaderMaterial->SetTCGenEnvironment( pStage->tcGen_Environment );
gl_genericShaderMaterial->SetTCGenLightmap( pStage->tcGen_Lightmap );

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

DAEMON_ASSERT( !( enableDeluxeMapping && enableGridDeluxeMapping ) );

// useAttrColor has no effect since the lightMapping shader has ATTR_COLOR forced to be always
// on (_requiredVertexAttribs). If we removed ATTR_COLOR from there, we would need to detect
// implicit vertex lighting as well, not only rgbgen (see SetLightDeluxeMode).
/* colorGen_t rgbGen = SetRgbGen( pStage );
alphaGen_t alphaGen = SetAlphaGen( pStage );
material->useAttrColor = rgbGen == colorGen_t::CGEN_VERTEX || rgbGen == colorGen_t::CGEN_ONE_MINUS_VERTEX
|| alphaGen == alphaGen_t::AGEN_VERTEX || alphaGen == alphaGen_t::AGEN_ONE_MINUS_VERTEX; */

material->enableDeluxeMapping = enableDeluxeMapping;
material->enableGridLighting = enableGridLighting;
material->enableGridDeluxeMapping = enableGridDeluxeMapping;
Expand Down Expand Up @@ -2144,12 +2130,6 @@ void MaterialSystem::RenderMaterial( Material& material, const uint32_t viewID )

backEnd.currentEntity = &tr.worldEntity;

if ( material.useAttrColor ) {
material.shader->AddVertexAttribBit( ATTR_COLOR );
} else {
material.shader->DelVertexAttribBit( ATTR_COLOR );
}

GL_State( stateBits );
if ( material.usePolygonOffset ) {
glEnable( GL_POLYGON_OFFSET_FILL );
Expand Down
5 changes: 1 addition & 4 deletions src/engine/renderer/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ struct Material {
bool enableSpecularMapping;
bool enablePhysicalMapping;

bool useAttrColor = false;

cullType_t cullType;

uint32_t sort;
Expand All @@ -128,8 +126,7 @@ struct Material {

bool operator==( const Material& other ) {
return program == other.program && stateBits == other.stateBits && vbo == other.vbo && ibo == other.ibo
&& fog == other.fog && cullType == other.cullType && usePolygonOffset == other.usePolygonOffset
&& useAttrColor == other.useAttrColor;
&& fog == other.fog && cullType == other.cullType && usePolygonOffset == other.usePolygonOffset;
}

void AddTexture( Texture* texture ) {
Expand Down
4 changes: 2 additions & 2 deletions src/engine/renderer/gl_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2185,7 +2185,7 @@ void GLShader::WriteUniformsToBuffer( uint32_t* buffer ) {
}

GLShader_generic::GLShader_generic( GLShaderManager *manager ) :
GLShader( "generic", ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT, manager ),
GLShader( "generic", ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT | ATTR_COLOR, manager ),
u_ColorMap( this ),
u_DepthMap( this ),
u_TextureMatrix( this ),
Expand Down Expand Up @@ -2217,7 +2217,7 @@ void GLShader_generic::SetShaderProgramUniforms( shaderProgram_t *shaderProgram
}

GLShader_genericMaterial::GLShader_genericMaterial( GLShaderManager* manager ) :
GLShader( "genericMaterial", "generic", true, ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT, manager ),
GLShader( "genericMaterial", "generic", true, ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT | ATTR_COLOR, manager ),
u_ColorMap( this ),
u_DepthMap( this ),
u_TextureMatrix( this ),
Expand Down
19 changes: 11 additions & 8 deletions src/engine/renderer/gl_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -3595,7 +3595,8 @@ enum class ColorModulate {
COLOR_MINUS_ONE = BIT( 1 ),
COLOR_LIGHTFACTOR = BIT( 2 ),
ALPHA_ONE = BIT( 3 ),
ALPHA_MINUS_ONE = BIT( 4 )
ALPHA_MINUS_ONE = BIT( 4 ),
ALPHA_ADD_ONE = BIT( 5 )
};

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

if ( useMapLightFactor ) {
ASSERT_EQ( vertexOverbright, false );
lightFactor = uint32_t( tr.mapLightFactor ) << 5;
lightFactor = uint32_t( tr.mapLightFactor ) << 6;
}

colorModulate |= lightFactor ? lightFactor : 1 << 5;
colorModulate |= lightFactor ? lightFactor : 1 << 6;

switch ( alphaGen ) {
case alphaGen_t::AGEN_VERTEX:
Expand All @@ -3662,10 +3663,12 @@ class u_ColorModulateColorGen :
break;
}

if ( needAttrib ) {
_shader->AddVertexAttribBit( ATTR_COLOR );
} else {
_shader->DelVertexAttribBit( ATTR_COLOR );
if ( !needAttrib ) {
/* Originally, this controlled whether the vertex color array was used,
now it does the equivalent by setting the color in the shader in such way as if it was using
the default OpenGL values for the disabled arrays (0.0, 0.0, 0.0, 1.0)
This allows to skip the vertex format change */
colorModulate |= Util::ordinal( ColorModulate::ALPHA_ADD_ONE );
}
this->SetValue( colorModulate );
}
Expand Down
10 changes: 8 additions & 2 deletions src/engine/renderer/glsl_source/common.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ Bit 1: color * ( -1 )
Bit 2: color += lightFactor
Bit 3: alpha * 1
Bit 4: alpha * ( -1 )
Bit 5-7: lightFactor */
Bit 5: alpha = 1
Bit 6-8: lightFactor */

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

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

float ColorModulateToLightFactor( const in uint colorMod ) {
return ( colorMod >> 5 ) & 0x7;
return ( colorMod >> 6 ) & 0x7;
}

// This is used to skip vertex colours if the colorMod doesn't need them
bool ColorModulateToVertexColor( const in uint colorMod ) {
return ( colorMod & 0xFF ) == 0xFF;
}
1 change: 1 addition & 0 deletions src/engine/renderer/glsl_source/generic_vp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ void main()

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

Expand Down

0 comments on commit 57bcbde

Please sign in to comment.