Skip to content

Commit a27d279

Browse files
committed
Move alphaGen portal support to "generic" shader
For alphaGen portal, a "portal" shader was used, which was like the "generic" shader but with most features removed. Instead of that, move alphaGen portal support to the "generic" shader so that it can be combined with other features. This fixes the ancient-remains foliage which uses alphaGen portal to fade out when the camera is close, but also needs alpha testing which was not supported in the "portal" shader. (Also it's vertex-lit so it should use overbright.) I added back the capability for the "generic" shader to do vertex lighting, but the implementation is simpler than before.
1 parent e80ec08 commit a27d279

8 files changed

Lines changed: 101 additions & 10 deletions

File tree

src/engine/renderer/Material.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ void UpdateSurfaceDataNOP( uint32_t*, shaderStage_t*, bool, bool ) {
142142
}
143143

144144
void UpdateSurfaceDataGeneric3D( uint32_t* materials, shaderStage_t* pStage, bool, bool ) {
145-
// shader_t* shader = pStage->shader;
145+
shader_t* shader = pStage->shader;
146146

147147
materials += pStage->bufferOffset;
148148

@@ -154,11 +154,13 @@ void UpdateSurfaceDataGeneric3D( uint32_t* materials, shaderStage_t* pStage, boo
154154
alphaGen_t alphaGen = SetAlphaGen( pStage );
155155

156156
const bool styleLightMap = pStage->type == stageType_t::ST_STYLELIGHTMAP || pStage->type == stageType_t::ST_STYLECOLORMAP;
157-
gl_genericShaderMaterial->SetUniform_ColorModulateColorGen_Uint( rgbGen, alphaGen, styleLightMap );
157+
gl_genericShaderMaterial->SetUniform_ColorModulateColorGen_Uint( rgbGen, alphaGen, styleLightMap || pStage->forceVertexLighting );
158158

159159
Tess_ComputeColor( pStage );
160160
gl_genericShaderMaterial->SetUniform_Color_Uint( tess.svars.color );
161161

162+
gl_genericShaderMaterial->SetUniform_InversePortalRange( 1.0f / shader->portalRange );
163+
162164
gl_genericShaderMaterial->SetUniform_DepthScale( pStage->depthFadeValue );
163165

164166
gl_genericShaderMaterial->WriteUniformsToBuffer( materials, GLShader::MATERIAL );
@@ -879,6 +881,7 @@ void BindShaderGeneric3D( Material* material ) {
879881
// Select shader permutation.
880882
gl_genericShaderMaterial->SetTCGenEnvironment( material->tcGenEnvironment );
881883
gl_genericShaderMaterial->SetTCGenLightmap( material->tcGen_Lightmap );
884+
gl_genericShaderMaterial->SetAlphaGenPortal( material->alphaGenPortal );
882885
gl_genericShaderMaterial->SetDepthFade( material->hasDepthFade );
883886
gl_genericShaderMaterial->SetDeform( material->deformIndex );
884887

@@ -892,6 +895,14 @@ void BindShaderGeneric3D( Material* material ) {
892895

893896
gl_genericShaderMaterial->SetUniform_ModelMatrix( backEnd.orientation.transformMatrix );
894897
gl_genericShaderMaterial->SetUniform_ModelViewProjectionMatrix( glState.modelViewProjectionMatrix[glState.stackIndex] );
898+
{
899+
matrix_t unprojectMatrix;
900+
MatrixCopy( backEnd.viewParms.projectionMatrix, unprojectMatrix );
901+
MatrixInverse( unprojectMatrix );
902+
MatrixMultiplyTranslation( unprojectMatrix, -1.0f, -1.0f, -1.0f );
903+
MatrixMultiplyScale( unprojectMatrix, 2.0f / windowConfig.vidWidth, 2.0f / windowConfig.vidHeight, 2.0f );
904+
gl_genericShaderMaterial->SetUniform_UnprojectMatrix( unprojectMatrix );
905+
}
895906

896907
gl_genericShaderMaterial->SetUniform_DepthMapBindless( GL_BindToTMU( 1, tr.depthSamplerImage ) );
897908

@@ -1097,6 +1108,10 @@ void ProcessMaterialGeneric3D( Material* material, shaderStage_t* pStage, Materi
10971108
gl_genericShaderMaterial->SetTCGenEnvironment( pStage->tcGen_Environment );
10981109
gl_genericShaderMaterial->SetTCGenLightmap( pStage->tcGen_Lightmap );
10991110

1111+
bool alphaGenPortal = pStage->alphaGen == alphaGen_t::AGEN_PORTAL;
1112+
material->alphaGenPortal = alphaGenPortal;
1113+
gl_genericShaderMaterial->SetAlphaGenPortal( alphaGenPortal );
1114+
11001115
bool hasDepthFade = pStage->hasDepthFade;
11011116
material->hasDepthFade = hasDepthFade;
11021117
gl_genericShaderMaterial->SetDepthFade( hasDepthFade );
@@ -1299,7 +1314,8 @@ void MaterialSystem::AddStage( MaterialSurface* surface, shaderStage_t* pStage,
12991314
}
13001315

13011316
if ( pStage->shader->reliefDepthScale != pStage2->shader->reliefDepthScale
1302-
|| pStage->shader->reliefOffsetBias != pStage2->shader->reliefOffsetBias )
1317+
|| pStage->shader->reliefOffsetBias != pStage2->shader->reliefOffsetBias
1318+
|| pStage->shader->portalRange != pStage2->shader->portalRange )
13031319
{
13041320
continue;
13051321
}

src/engine/renderer/Material.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ struct Material {
129129
int deformIndex;
130130
bool tcGenEnvironment;
131131
bool tcGen_Lightmap;
132+
bool alphaGenPortal;
132133
bool hasDepthFade;
133134

134135
bool bspSurface;

src/engine/renderer/gl_shader.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2446,12 +2446,14 @@ GLShader_generic::GLShader_generic() :
24462446
u_AlphaThreshold( this ),
24472447
u_ModelMatrix( this ),
24482448
u_ModelViewProjectionMatrix( this ),
2449+
u_UnprojectMatrix( this ),
24492450
u_ColorModulateColorGen_Float( this ),
24502451
u_ColorModulateColorGen_Uint( this ),
24512452
u_Color_Float( this ),
24522453
u_Color_Uint( this ),
24532454
u_Bones( this ),
24542455
u_VertexInterpolation( this ),
2456+
u_InversePortalRange( this ),
24552457
u_DepthScale( this ),
24562458
u_ProfilerZero( this ),
24572459
u_ProfilerRenderSubGroups( this ),
@@ -2460,6 +2462,7 @@ GLShader_generic::GLShader_generic() :
24602462
GLCompileMacro_USE_VERTEX_ANIMATION( this ),
24612463
GLCompileMacro_USE_TCGEN_ENVIRONMENT( this ),
24622464
GLCompileMacro_USE_TCGEN_LIGHTMAP( this ),
2465+
GLCompileMacro_USE_ALPHAGEN_PORTAL( this ),
24632466
GLCompileMacro_USE_DEPTH_FADE( this )
24642467
{
24652468
}
@@ -2480,8 +2483,10 @@ GLShader_genericMaterial::GLShader_genericMaterial() :
24802483
u_AlphaThreshold( this ),
24812484
u_ModelMatrix( this ),
24822485
u_ModelViewProjectionMatrix( this ),
2486+
u_UnprojectMatrix( this ),
24832487
u_ColorModulateColorGen_Uint( this ),
24842488
u_Color_Uint( this ),
2489+
u_InversePortalRange( this ),
24852490
u_DepthScale( this ),
24862491
u_ShowTris( this ),
24872492
u_MaterialColour( this ),
@@ -2490,6 +2495,7 @@ GLShader_genericMaterial::GLShader_genericMaterial() :
24902495
GLDeformStage( this ),
24912496
GLCompileMacro_USE_TCGEN_ENVIRONMENT( this ),
24922497
GLCompileMacro_USE_TCGEN_LIGHTMAP( this ),
2498+
GLCompileMacro_USE_ALPHAGEN_PORTAL( this ),
24932499
GLCompileMacro_USE_DEPTH_FADE( this ) {
24942500
}
24952501

src/engine/renderer/gl_shader.h

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,7 @@ class GLCompileMacro
939939
USE_REFLECTIVE_SPECULAR,
940940
LIGHT_DIRECTIONAL,
941941
USE_DEPTH_FADE,
942+
USE_ALPHAGEN_PORTAL,
942943
USE_PHYSICAL_MAPPING,
943944
};
944945

@@ -1425,6 +1426,35 @@ class GLCompileMacro_USE_DEPTH_FADE :
14251426
}
14261427
};
14271428

1429+
class GLCompileMacro_USE_ALPHAGEN_PORTAL :
1430+
GLCompileMacro
1431+
{
1432+
public:
1433+
GLCompileMacro_USE_ALPHAGEN_PORTAL( GLShader *shader ) :
1434+
GLCompileMacro( shader )
1435+
{
1436+
}
1437+
1438+
const char *GetName() const override
1439+
{
1440+
return "USE_ALPHAGEN_PORTAL";
1441+
}
1442+
1443+
EGLCompileMacro GetType() const override
1444+
{
1445+
return EGLCompileMacro::USE_ALPHAGEN_PORTAL;
1446+
}
1447+
1448+
int GetShaderTypes() const override {
1449+
return ShaderType::FRAGMENT;
1450+
}
1451+
1452+
void SetAlphaGenPortal( bool enable )
1453+
{
1454+
SetMacro( enable );
1455+
}
1456+
};
1457+
14281458
class GLCompileMacro_USE_PHYSICAL_MAPPING :
14291459
GLCompileMacro
14301460
{
@@ -2401,7 +2431,7 @@ class u_InversePortalRange :
24012431
{
24022432
public:
24032433
u_InversePortalRange( GLShader *shader ) :
2404-
GLUniform1f( shader, "u_InversePortalRange", PUSH )
2434+
GLUniform1f( shader, "u_InversePortalRange", MATERIAL_OR_PUSH )
24052435
{
24062436
}
24072437

@@ -2939,12 +2969,14 @@ class GLShader_generic :
29392969
public u_AlphaThreshold,
29402970
public u_ModelMatrix,
29412971
public u_ModelViewProjectionMatrix,
2972+
public u_UnprojectMatrix,
29422973
public u_ColorModulateColorGen_Float,
29432974
public u_ColorModulateColorGen_Uint,
29442975
public u_Color_Float,
29452976
public u_Color_Uint,
29462977
public u_Bones,
29472978
public u_VertexInterpolation,
2979+
public u_InversePortalRange,
29482980
public u_DepthScale,
29492981
public u_ProfilerZero,
29502982
public u_ProfilerRenderSubGroups,
@@ -2953,6 +2985,7 @@ class GLShader_generic :
29532985
public GLCompileMacro_USE_VERTEX_ANIMATION,
29542986
public GLCompileMacro_USE_TCGEN_ENVIRONMENT,
29552987
public GLCompileMacro_USE_TCGEN_LIGHTMAP,
2988+
public GLCompileMacro_USE_ALPHAGEN_PORTAL,
29562989
public GLCompileMacro_USE_DEPTH_FADE
29572990
{
29582991
public:
@@ -2969,8 +3002,10 @@ class GLShader_genericMaterial :
29693002
public u_AlphaThreshold,
29703003
public u_ModelMatrix,
29713004
public u_ModelViewProjectionMatrix,
3005+
public u_UnprojectMatrix,
29723006
public u_ColorModulateColorGen_Uint,
29733007
public u_Color_Uint,
3008+
public u_InversePortalRange,
29743009
public u_DepthScale,
29753010
public u_ShowTris,
29763011
public u_MaterialColour,
@@ -2979,6 +3014,7 @@ class GLShader_genericMaterial :
29793014
public GLDeformStage,
29803015
public GLCompileMacro_USE_TCGEN_ENVIRONMENT,
29813016
public GLCompileMacro_USE_TCGEN_LIGHTMAP,
3017+
public GLCompileMacro_USE_ALPHAGEN_PORTAL,
29823018
public GLCompileMacro_USE_DEPTH_FADE {
29833019
public:
29843020
GLShader_genericMaterial();

src/engine/renderer/glsl_source/common.glsl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ void ColorModulateColor_lightFactor(
135135

136136
vec4 unpackedColor = UnpackColor( packedColor );
137137

138-
unpackedColor.rgb *= lightFactor;
139-
140138
ModulateColor( colorModulation, unpackedColor, color );
139+
color.rgb *= lightFactor;
141140
}

src/engine/renderer/glsl_source/generic_fp.glsl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ IN(smooth) float var_FadeDepth;
4242
uniform sampler2D u_DepthMap;
4343
#endif
4444

45+
#if defined(USE_ALPHAGEN_PORTAL)
46+
uniform float u_InversePortalRange;
47+
uniform mat4 u_UnprojectMatrix;
48+
#endif
49+
4550
#insert shaderProfiler_fp
4651

4752
DECLARE_OUTPUT(vec4)
@@ -67,6 +72,16 @@ void main()
6772
return;
6873
}
6974

75+
// Normally alpha modulation is supposed to be done before the alpha test, but this looks
76+
// better for the ancient-remains foliage combining alphagen portal with an alpha-tested
77+
// plant image: fades out to 0 smoothly instead of cutting off at 0.5.
78+
#if defined(USE_ALPHAGEN_PORTAL)
79+
vec4 position = u_UnprojectMatrix * vec4(gl_FragCoord.xyz, 1.0);
80+
float portalAlpha = length(position.xyz / position.w) * u_InversePortalRange;
81+
portalAlpha = length(position.xyz / position.w) * (1.0/256.0);
82+
color.a *= clamp(portalAlpha, 0.0, 1.0);
83+
#endif
84+
7085
#if defined(USE_DEPTH_FADE)
7186
float depth = texture2D(u_DepthMap, gl_FragCoord.xy / r_FBufSize).x;
7287

src/engine/renderer/tr_shade.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,7 @@ void ProcessShaderGeneric3D( const shaderStage_t* pStage ) {
814814
gl_genericShader->SetDeform( pStage->deformIndex );
815815
gl_genericShader->SetTCGenEnvironment( pStage->tcGen_Environment );
816816
gl_genericShader->SetTCGenLightmap( pStage->tcGen_Lightmap );
817+
gl_genericShader->SetAlphaGenPortal( pStage->alphaGen == alphaGen_t::AGEN_PORTAL );
817818
gl_genericShader->SetDepthFade( pStage->hasDepthFade );
818819
}
819820

@@ -926,7 +927,7 @@ void Render_generic3D( shaderStage_t *pStage )
926927
alphaGen_t alphaGen = SetAlphaGen( pStage );
927928

928929
const bool styleLightMap = pStage->type == stageType_t::ST_STYLELIGHTMAP || pStage->type == stageType_t::ST_STYLECOLORMAP;
929-
SetUniform_ColorModulateColorGen( gl_genericShader, rgbGen, alphaGen, styleLightMap );
930+
SetUniform_ColorModulateColorGen( gl_genericShader, rgbGen, alphaGen, styleLightMap || pStage->forceVertexLighting );
930931

931932
// u_Color
932933
SetUniform_Color( gl_genericShader, tess.svars.color );
@@ -963,6 +964,18 @@ void Render_generic3D( shaderStage_t *pStage )
963964

964965
gl_genericShader->SetUniform_TextureMatrix( tess.svars.texMatrices[ TB_COLORMAP ] );
965966

967+
if ( pStage->alphaGen == alphaGen_t::AGEN_PORTAL )
968+
{
969+
matrix_t unprojectMatrix;
970+
MatrixCopy( backEnd.viewParms.projectionMatrix, unprojectMatrix );
971+
MatrixInverse( unprojectMatrix );
972+
MatrixMultiplyTranslation( unprojectMatrix, -1.0f, -1.0f, -1.0f );
973+
MatrixMultiplyScale( unprojectMatrix, 2.0f / windowConfig.vidWidth, 2.0f / windowConfig.vidHeight, 2.0f );
974+
975+
gl_genericShader->SetUniform_InversePortalRange( 1.0f / tess.surfaceShader->portalRange );
976+
gl_genericShader->SetUniform_UnprojectMatrix( unprojectMatrix );
977+
}
978+
966979
if ( hasDepthFade )
967980
{
968981
gl_genericShader->SetUniform_DepthScale( pStage->depthFadeValue );

src/engine/renderer/tr_shader.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2928,7 +2928,6 @@ static bool ParseStage( shaderStage_t *stage, const char **text )
29282928
}
29292929
else if ( !Q_stricmp( token, "portal" ) )
29302930
{
2931-
stage->type = stageType_t::ST_PORTALMAP;
29322931
stage->alphaGen = alphaGen_t::AGEN_PORTAL;
29332932

29342933
token = COM_ParseExt2( text, false );
@@ -5431,9 +5430,15 @@ static void FinishStages()
54315430
case stageType_t::ST_COLORMAP:
54325431
if ( stage->rgbGen == colorGen_t::CGEN_VERTEX && shader.registerFlags & RSF_BSP )
54335432
{
5434-
// vertex colors used as lighting detected. Enable overbright and realtime lights
5435-
stage->type = stageType_t::ST_DIFFUSEMAP;
5433+
// Vertex colors used as lighting detected. Enable overbright
5434+
54365435
stage->forceVertexLighting = true; // use vertex lighting even if there is a lightmap
5436+
5437+
if ( stage->alphaGen != alphaGen_t::AGEN_PORTAL ) // AGEN_PORTAL not supported in lightMapping
5438+
{
5439+
// use lightMapping shader to get realtime lights
5440+
stage->type = stageType_t::ST_DIFFUSEMAP;
5441+
}
54375442
}
54385443

54395444
default:

0 commit comments

Comments
 (0)