Skip to content

Commit db8bb5a

Browse files
committed
renderer: add non-bitwise alternative for u_Color and u_ColorModulateColorGen on GLSL 1.20
Add non-bitwise alternative for u_Color and u_ColorModulateColorGen on GLSL 1.20. Store the related bitfields in GLUniform1i in all cases, process them as uint with bitwise operations when supported, or as int with bitwise emulation otherwise.
1 parent 73fe129 commit db8bb5a

File tree

10 files changed

+108
-31
lines changed

10 files changed

+108
-31
lines changed

src/engine/renderer/gl_shader.h

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3043,17 +3043,22 @@ class u_CloudHeight :
30433043
};
30443044

30453045
class u_Color :
3046-
GLUniform1ui
3046+
GLUniform1i
30473047
{
30483048
public:
30493049
u_Color( GLShader *shader ) :
3050-
GLUniform1ui( shader, "u_Color" )
3050+
GLUniform1i( shader, "u_Color" )
30513051
{
30523052
}
30533053

30543054
void SetUniform_Color( const Color::Color& color )
30553055
{
3056-
this->SetValue( packUnorm4x8( color.ToArray() ) );
3056+
/* HACK: Store uint32_t as int32_t to be compatible with GLSL 1.20,
3057+
the GLSL code will convert back to uint32_t. */
3058+
uint32_t uColor = packUnorm4x8( color.ToArray() );
3059+
int32_t iColor;
3060+
memcpy( &iColor, &uColor, sizeof( iColor ) );
3061+
this->SetValue( iColor );
30573062
}
30583063
};
30593064

@@ -3559,10 +3564,10 @@ enum class ColorModulate {
35593564
};
35603565

35613566
class u_ColorModulateColorGen :
3562-
GLUniform1ui {
3567+
GLUniform1i {
35633568
public:
35643569
u_ColorModulateColorGen( GLShader* shader ) :
3565-
GLUniform1ui( shader, "u_ColorModulateColorGen" ) {
3570+
GLUniform1i( shader, "u_ColorModulateColorGen" ) {
35663571
}
35673572

35683573
void SetUniform_ColorModulateColorGen( colorGen_t colorGen, alphaGen_t alphaGen, bool vertexOverbright = false,
@@ -3629,7 +3634,11 @@ class u_ColorModulateColorGen :
36293634
This allows to skip the vertex format change */
36303635
colorModulate |= Util::ordinal( ColorModulate::ALPHA_ADD_ONE );
36313636
}
3632-
this->SetValue( colorModulate );
3637+
/* HACK: Store uint32_t as int32_t to be compatible with GLSL 1.20,
3638+
the GLSL code will convert back to uint32_t. */
3639+
int32_t iColorModulate;
3640+
memcpy( &iColorModulate, &colorModulate, sizeof( iColorModulate ) );
3641+
this->SetValue( iColorModulate );
36333642
}
36343643
};
36353644

src/engine/renderer/glsl_source/common.glsl

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,33 +43,97 @@ array must be in the form of uvec4 array[] */
4343
#define UINT_FROM_UVEC4_ARRAY( array, id ) ( ( array )[( id ) / 4][( id ) % 4] )
4444
#define UVEC2_FROM_UVEC4_ARRAY( array, id ) ( ( id ) % 2 == 0 ? ( array )[( id ) / 2].xy : ( array )[( id ) / 2].zw )
4545

46+
// Common functions
47+
48+
vec4 UnpackColor( const in int color )
49+
{
50+
#if __VERSION__ > 120
51+
uint uColor = color;
52+
return unpackUnorm4x8( uColor );
53+
#else
54+
int v = color, m = 0;
55+
56+
if ( v < 0 )
57+
{
58+
v = 2147483647 - abs( v ) + 1;
59+
m = 128;
60+
}
61+
62+
int a = v / 16777216;
63+
v -= a * 16777216;
64+
int b = v / 65536;
65+
v -= b * 65536;
66+
int g = v / 256;
67+
v -= g * 256;
68+
int r = v;
69+
a = a + m;
70+
71+
return vec4( r, g, b, a ) / 255;
72+
#endif
73+
}
74+
4675
/* Bit 0: color * 1
4776
Bit 1: color * ( -1 )
4877
Bit 2: color += lightFactor
4978
Bit 3: alpha * 1
5079
Bit 4: alpha * ( -1 )
5180
Bit 5: alpha = 1
81+
// There should be no bit above.
5282
Bit 6-9: lightFactor */
5383

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

56-
vec4 ColorModulateToColor( const in uint colorMod ) {
57-
vec4 colorModulate = vec4( colorModArray[colorMod & 3] );
58-
colorModulate.a = ( colorModArray[( colorMod & 24 ) >> 3] );
86+
vec4 ColorModulateToColor( const in int colorMod ) {
87+
#if __VERSION__ > 120
88+
int rgbIndex = colorMod & 3;
89+
int alphaIndex = ( colorMod & 24 ) >> 3;
90+
#else
91+
int rgbBit0 = colorMod % 2;
92+
int rgbBit1 = ( colorMod / 2 ) % 2;
93+
int alphaBit0 = ( colorMod / 8 ) % 2;
94+
int alphaBit1 = ( colorMod / 16 ) % 2;
95+
int rgbIndex = rgbBit0 + ( rgbBit1 * 2 );
96+
int alphaIndex = alphaBit0 + ( alphaBit1 * 2 );
97+
#endif
98+
99+
vec4 colorModulate = vec4( colorModArray[ rgbIndex ] );
100+
colorModulate.a = colorModArray[ alphaIndex ];
59101
return colorModulate;
60102
}
61103

62-
vec4 ColorModulateToColor( const in uint colorMod, const in float lightFactor ) {
63-
vec4 colorModulate = vec4( colorModArray[colorMod & 3] + ( ( colorMod & 4 ) >> 2 ) * lightFactor );
64-
colorModulate.a = ( colorModArray[( colorMod & 24 ) >> 3] );
104+
vec4 ColorModulateToColor( const in int colorMod, const in float lightFactor ) {
105+
#if __VERSION__ > 120
106+
int rgbIndex = colorMod & 3;
107+
int alphaIndex = ( colorMod & 24 ) >> 3;
108+
int hasLight = ( colorMod & 4 ) >> 2;
109+
#else
110+
int rgbBit0 = colorMod % 2;
111+
int rgbBit1 = ( colorMod / 2 ) % 2;
112+
int hasLight = ( colorMod / 4 ) % 2;
113+
int alphaBit0 = ( colorMod / 8 ) % 2;
114+
int alphaBit1 = ( colorMod / 16 ) % 2;
115+
int rgbIndex = rgbBit0 + ( rgbBit1 * 2 );
116+
int alphaIndex = alphaBit0 + ( alphaBit1 * 2 );
117+
#endif
118+
119+
vec4 colorModulate = vec4( colorModArray[ rgbIndex ] + ( hasLight * lightFactor ) );
120+
colorModulate.a = colorModArray[ alphaIndex ];
65121
return colorModulate;
66122
}
67123

68-
float ColorModulateToLightFactor( const in uint colorMod ) {
124+
float ColorModulateToLightFactor( const in int colorMod ) {
125+
#if __VERSION__ > 120
69126
return ( colorMod >> 6 ) & 0xF;
127+
#else
128+
return float( colorMod / 64 );
129+
#endif
70130
}
71131

72132
// This is used to skip vertex colours if the colorMod doesn't need them
73-
bool ColorModulateToVertexColor( const in uint colorMod ) {
133+
bool ColorModulateToVertexColor( const in int colorMod ) {
134+
#if __VERSION__ > 120
74135
return ( colorMod & 32 ) == 32;
136+
#else
137+
return ( colorMod / 32 ) % 2 == 1;
138+
#endif
75139
}

src/engine/renderer/glsl_source/fogGlobal_fp.glsl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2222

2323
/* fogGlobal_fp.glsl */
2424

25+
#insert common
26+
2527
uniform sampler2D u_ColorMap; // fog texture
2628
uniform sampler2D u_DepthMap;
2729

2830
uniform vec4 u_FogDistanceVector;
29-
uniform uint u_Color;
31+
uniform int u_Color;
3032
uniform mat4 u_UnprojectMatrix;
3133

3234
#if __VERSION__ > 120
@@ -52,5 +54,5 @@ void main()
5254

5355
vec4 color = texture2D(u_ColorMap, st);
5456

55-
outputColor = unpackUnorm4x8( u_Color ) * color;
57+
outputColor = UnpackColor( u_Color ) * color;
5658
}

src/engine/renderer/glsl_source/fogQuake3_vp.glsl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2222

2323
/* fogQuake3_vp.glsl */
2424

25+
#insert common
2526
#insert vertexSimple_vp
2627
#insert vertexSkinning_vp
2728
#insert vertexAnimation_vp
2829

2930
uniform float u_Time;
3031

31-
uniform uint u_Color;
32+
uniform int u_Color;
3233
uniform mat4 u_ModelMatrix;
3334
uniform mat4 u_ModelViewProjectionMatrix;
3435

@@ -57,7 +58,7 @@ void main()
5758

5859
VertexFetch( position, LB, color, texCoord, lmCoord );
5960

60-
color = unpackUnorm4x8( u_Color );
61+
color = UnpackColor( u_Color );
6162

6263
DeformVertex( position,
6364
LB.normal,

src/engine/renderer/glsl_source/forwardLighting_vp.glsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ uniform mat4 u_LightAttenuationMatrix;
3232
uniform mat4 u_ModelMatrix;
3333
uniform mat4 u_ModelViewProjectionMatrix;
3434

35-
uniform uint u_ColorModulateColorGen;
36-
uniform uint u_Color;
35+
uniform int u_ColorModulateColorGen;
36+
uniform int u_Color;
3737

3838
uniform float u_Time;
3939

@@ -64,7 +64,7 @@ void main()
6464
VertexFetch( position, LB, color, texCoord, lmCoord);
6565

6666
// assign color
67-
color = color * ColorModulateToColor( u_ColorModulateColorGen ) + unpackUnorm4x8( u_Color );
67+
color = color * ColorModulateToColor( u_ColorModulateColorGen ) + UnpackColor( u_Color );
6868

6969
DeformVertex( position,
7070
LB.normal,

src/engine/renderer/glsl_source/generic_vp.glsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ uniform vec3 u_ViewOrigin;
3636

3737
uniform float u_Time;
3838

39-
uniform uint u_ColorModulateColorGen;
40-
uniform uint u_Color;
39+
uniform int u_ColorModulateColorGen;
40+
uniform int u_Color;
4141
#if defined(USE_TCGEN_ENVIRONMENT)
4242
uniform mat4 u_ModelMatrix;
4343
#endif
@@ -70,7 +70,7 @@ void main()
7070
float lightFactor = ColorModulateToLightFactor( u_ColorModulateColorGen );
7171
color.a = ColorModulateToVertexColor( u_ColorModulateColorGen ) ? 1.0 : color.a;
7272
color = color * ColorModulateToColor( u_ColorModulateColorGen, lightFactor )
73-
+ unpackUnorm4x8( u_Color ) * vec4( lightFactor, lightFactor, lightFactor, 1.0 );
73+
+ UnpackColor( u_Color ) * vec4( lightFactor, lightFactor, lightFactor, 1.0 );
7474

7575
DeformVertex( position,
7676
LB.normal,

src/engine/renderer/glsl_source/lightMapping_fp.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ uniform sampler2D u_GlowMap;
3535
uniform float u_AlphaThreshold;
3636
uniform vec3 u_ViewOrigin;
3737

38-
uniform uint u_ColorModulateColorGen;
38+
uniform int u_ColorModulateColorGen;
3939

4040
IN(smooth) vec3 var_Position;
4141
IN(smooth) vec2 var_TexCoords;

src/engine/renderer/glsl_source/lightMapping_vp.glsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ uniform mat4 u_ModelViewProjectionMatrix;
4848

4949
uniform float u_Time;
5050

51-
uniform uint u_ColorModulateColorGen;
52-
uniform uint u_Color;
51+
uniform int u_ColorModulateColorGen;
52+
uniform int u_Color;
5353

5454
OUT(smooth) vec3 var_Position;
5555
OUT(smooth) vec2 var_TexCoords;
@@ -76,7 +76,7 @@ void main()
7676

7777
VertexFetch(position, LB, color, texCoord, lmCoord);
7878

79-
color = color * ColorModulateToColor( u_ColorModulateColorGen ) + unpackUnorm4x8( u_Color );
79+
color = color * ColorModulateToColor( u_ColorModulateColorGen ) + UnpackColor( u_Color );
8080

8181
DeformVertex(position, LB.normal, texCoord, color, u_Time);
8282

src/engine/renderer/glsl_source/liquid_fp.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ uniform sampler2D u_DepthMap;
3434

3535
uniform vec3 u_ViewOrigin;
3636

37-
uniform uint u_ColorModulateColorGen;
37+
uniform int u_ColorModulateColorGen;
3838

3939
uniform float u_FogDensity;
4040
uniform vec3 u_FogColor;

src/engine/renderer/glsl_source/shadowFill_vp.glsl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2222

2323
/* shadowFill_vp.glsl */
2424

25+
#insert common
2526
#insert vertexSimple_vp
2627
#insert vertexSkinning_vp
2728
#insert vertexAnimation_vp
2829

29-
uniform uint u_Color;
30+
uniform int u_Color;
3031

3132
uniform mat3x2 u_TextureMatrix;
3233
uniform mat4 u_ModelMatrix;
@@ -73,5 +74,5 @@ void main()
7374
var_TexCoords = (u_TextureMatrix * vec3(texCoord, 1.0)).st;
7475

7576
// assign color
76-
var_Color = unpackUnorm4x8( u_Color );
77+
var_Color = UnpackColor( u_Color );
7778
}

0 commit comments

Comments
 (0)