Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use colorModulateColorGen instead of disabling vertex color #1514

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions src/engine/renderer/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1107,12 +1107,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 @@ -1138,14 +1132,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 @@ -2138,12 +2124,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 @@ -100,8 +100,6 @@ struct Material {
bool enableSpecularMapping;
bool enablePhysicalMapping;

bool useAttrColor = false;

cullType_t cullType;

uint32_t sort;
Expand All @@ -120,8 +118,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 @@ -2220,7 +2220,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 @@ -2252,7 +2252,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 @@ -3664,7 +3664,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 @@ -3694,7 +3695,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 @@ -3711,10 +3712,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 @@ -3731,10 +3732,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 */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that related to "Vertex shader in program 11 is being recompiled based on GL state." warnings?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be. On my end I got this warn for the deformVertexes shader, which does use data taken from attr_Color. Perhaps the driver was optimising all attr_Color and derivative usages out when the attrib was disabled, then had to recompile when it got enabled.

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-9: 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 ) & 0xF;
}

// 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 @@ -68,6 +68,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