Skip to content

Commit 360d3de

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 as GLUniform1i in all cases, process them as uint with bitwise operations when supported, or as int with bitwise emulation otherwise.
1 parent bbb828c commit 360d3de

File tree

10 files changed

+108
-32
lines changed

10 files changed

+108
-32
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

@@ -3571,10 +3576,10 @@ enum class ColorModulate {
35713576
};
35723577

35733578
class u_ColorModulateColorGen :
3574-
GLUniform1ui {
3579+
GLUniform1i {
35753580
public:
35763581
u_ColorModulateColorGen( GLShader* shader ) :
3577-
GLUniform1ui( shader, "u_ColorModulateColorGen" ) {
3582+
GLUniform1i( shader, "u_ColorModulateColorGen" ) {
35783583
}
35793584

35803585
void SetUniform_ColorModulateColorGen( colorGen_t colorGen, alphaGen_t alphaGen, bool vertexOverbright = false,
@@ -3641,7 +3646,11 @@ class u_ColorModulateColorGen :
36413646
This allows to skip the vertex format change */
36423647
colorModulate |= Util::ordinal( ColorModulate::ALPHA_ADD_ONE );
36433648
}
3644-
this->SetValue( colorModulate );
3649+
/* HACK: Store uint32_t as int32_t to be compatible with GLSL 1.20,
3650+
the GLSL code will convert back to uint32_t. */
3651+
int32_t iColorModulate;
3652+
memcpy( &iColorModulate, &colorModulate, sizeof( iColorModulate ) );
3653+
this->SetValue( iColorModulate );
36453654
}
36463655
};
36473656

src/engine/renderer/glsl_source/common.glsl

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,33 +43,96 @@ 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+
return unpackUnorm4x8( color );
52+
#else
53+
int v = color, m = 0;
54+
55+
if ( v < 0 )
56+
{
57+
v = 2147483647 - abs( v ) + 1;
58+
m = 128;
59+
}
60+
61+
int a = v / 16777216;
62+
v -= a * 16777216;
63+
int b = v / 65536;
64+
v -= b * 65536;
65+
int g = v / 256;
66+
v -= g * 256;
67+
int r = v;
68+
a = a + m;
69+
70+
return vec4( r, g, b, a ) / 255;
71+
#endif
72+
}
73+
4674
/* Bit 0: color * 1
4775
Bit 1: color * ( -1 )
4876
Bit 2: color += lightFactor
4977
Bit 3: alpha * 1
5078
Bit 4: alpha * ( -1 )
5179
Bit 5: alpha = 1
52-
Bit 6-9: lightFactor */
80+
Bit 6-9: lightFactor
81+
All bits after lightFactor should be left zeroed. */
5382

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

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

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] );
103+
vec4 ColorModulateToColor( const in int colorMod, const in float lightFactor ) {
104+
#if __VERSION__ > 120
105+
int rgbIndex = colorMod & 3;
106+
int alphaIndex = ( colorMod & 24 ) >> 3;
107+
int hasLight = ( colorMod & 4 ) >> 2;
108+
#else
109+
int rgbBit0 = colorMod % 2;
110+
int rgbBit1 = ( colorMod / 2 ) % 2;
111+
int hasLight = ( colorMod / 4 ) % 2;
112+
int alphaBit0 = ( colorMod / 8 ) % 2;
113+
int alphaBit1 = ( colorMod / 16 ) % 2;
114+
int rgbIndex = rgbBit0 + ( rgbBit1 * 2 );
115+
int alphaIndex = alphaBit0 + ( alphaBit1 * 2 );
116+
#endif
117+
118+
vec4 colorModulate = vec4( colorModArray[ rgbIndex ] + ( hasLight * lightFactor ) );
119+
colorModulate.a = colorModArray[ alphaIndex ];
65120
return colorModulate;
66121
}
67122

68-
float ColorModulateToLightFactor( const in uint colorMod ) {
123+
float ColorModulateToLightFactor( const in int colorMod ) {
124+
#if __VERSION__ > 120
69125
return ( colorMod >> 6 ) & 0xF;
126+
#else
127+
return float( colorMod / 64 );
128+
#endif
70129
}
71130

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

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_ColorGlobal;
32+
uniform int u_ColorGlobal;
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_ColorGlobal );
61+
color = UnpackColor( u_ColorGlobal );
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)