Skip to content

Commit

Permalink
Use uniform buffers for global uniforms and bone transforms
Browse files Browse the repository at this point in the history
* Increased the maximum number of bones from 100 to 256.
* Correctly rotate the environment map when the up axis is not Z.
* Experimental and very limited Starfield skinning.
  • Loading branch information
fo76utils committed Jan 23, 2025
1 parent 86fa969 commit 6cb8bf4
Show file tree
Hide file tree
Showing 43 changed files with 372 additions and 369 deletions.
16 changes: 3 additions & 13 deletions res/shaders/bonetransform.glsl
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
layout ( location = 5 ) in vec4 boneWeights0;
layout ( location = 6 ) in vec4 boneWeights1;

uniform int numBones;
uniform mat3x4 boneTransforms[MAX_NUM_BONES];
layout ( location = 5 ) in float boneWeights[8];

#ifdef BT_POSITION_ONLY
void boneTransform( inout vec4 v )
Expand All @@ -22,16 +18,10 @@ void boneTransform( inout vec4 v, inout vec3 n, inout vec3 t, inout vec3 b )
#endif
float wSum = 0.0;
for ( int i = 0; i < 8; i++ ) {
float bw;
if ( i < 4 )
bw = boneWeights0[i];
else
bw = boneWeights1[i & 3];
float bw = boneWeights[i];
if ( !( bw > 0.0 ) )
break;
int bone = int( bw );
if ( bone >= numBones )
continue;
int bone = int( bw ) & 0xFF;
float w = fract( bw );
mat3x4 m = boneTransforms[bone];
vTmp += v * m * w;
Expand Down
5 changes: 3 additions & 2 deletions res/shaders/default.frag
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ in vec2 texCoords[9];
in vec4 A;
in vec4 C;
in vec4 D;
in float glowScale;

in vec3 N;

Expand Down Expand Up @@ -79,7 +80,7 @@ void main()
vec3 emissive = frontMaterialEmission.rgb * frontMaterialEmission.a;
if ( ( vertexColorFlags & 0x10 ) != 0 )
emissive = C.rgb * C.a;
color.rgb += emissive;
color.rgb += emissive * glowScale;

color.a *= alpha;

Expand Down Expand Up @@ -119,7 +120,7 @@ void main()
}
if ( textures[4].textureUnit > 0 ) {
// glow
color.rgb += getTexture( 4 ).rgb;
color.rgb += getTexture( 4 ).rgb * glowScale;
}

// Specular
Expand Down
17 changes: 8 additions & 9 deletions res/shaders/default.vert
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ out vec2 texCoords[9];
out vec4 A;
out vec4 C;
out vec4 D;
out float glowScale;

out vec3 N;

uniform mat3 viewMatrix;
uniform mat3 normalMatrix;
#include "uniforms.glsl"

uniform mat3 normalMatrix; // in row-major order
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
uniform vec4 lightSourcePosition[3]; // W0 = environment map rotation (-1.0 to 1.0), W1, W2 = viewport X, Y
uniform vec4 lightSourceDiffuse[3]; // A0 = overall brightness, A1, A2 = viewport width, height
uniform vec4 lightSourceAmbient; // A = tone mapping control (1.0 = full tone mapping)

uniform vec4 vertexColorOverride; // components greater than zero replace the vertex color

Expand All @@ -42,7 +40,7 @@ void main()
vec4 v = vec4( vertexPosition, 1.0 );
vec3 n = normalVector;

if ( numBones > 0 )
if ( boneWeights[0] > 0.0 && renderOptions1.x != 0 )
boneTransform( v, n );

v = modelViewMatrix * v;
Expand All @@ -65,7 +63,8 @@ void main()
ViewDir = -v.xyz;
LightDir = lightSourcePosition[0].xyz;

A = vec4( sqrt(lightSourceAmbient.rgb) * 0.375, lightSourceAmbient.a );
A = vec4( sqrt(lightSourceAmbient.rgb) * 0.375, lightingControls.x );
C = mix( vertexColor, vertexColorOverride, greaterThan( vertexColorOverride, vec4( 0.0 ) ) );
D = vec4( sqrt(lightSourceDiffuse[0].rgb), lightSourceDiffuse[0].a );
D = sqrt( vec4(lightSourceDiffuse[0].rgb, lightingControls.y) );
glowScale = sqrt( lightingControls.z );
}
7 changes: 2 additions & 5 deletions res/shaders/drawline.glsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
uniform vec4 lightSourcePosition[3]; // W0 = environment map rotation (-1.0 to 1.0), W1, W2 = viewport X, Y
uniform vec4 lightSourceDiffuse[3]; // A0 = overall brightness, A1, A2 = viewport width, height

uniform float lineWidth;

void drawLine( vec4 p0, vec4 p1 )
Expand All @@ -19,8 +16,8 @@ void drawLine( vec4 p0, vec4 p1 )
vec3 p0_ndc = p0.xyz / p0.w;
vec3 p1_ndc = p1.xyz / p1.w;

vec2 vpScale = vec2( lightSourceDiffuse[1].a, lightSourceDiffuse[2].a ) * 0.5;
vec2 vpOffs = vec2( lightSourcePosition[1].w, lightSourcePosition[2].w ) + vpScale;
vec2 vpScale = vec2( viewportDimensions.zw ) * 0.5;
vec2 vpOffs = vec2( viewportDimensions.xy ) + vpScale;

vec2 p0_ss = p0_ndc.xy * vpScale;
vec2 p1_ss = p1_ndc.xy * vpScale;
Expand Down
16 changes: 8 additions & 8 deletions res/shaders/f76_default.frag
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#version 410 core

#include "uniforms.glsl"

uniform sampler2D BaseMap;
uniform sampler2D NormalMap;
uniform sampler2D GlowMap;
Expand Down Expand Up @@ -48,12 +50,10 @@ in vec3 ViewDir;

in vec2 texCoord;

in vec4 A;
in vec4 C;
in vec4 D;

in mat3 btnMatrix;
in mat3 reflMatrix;
flat in mat3 reflMatrix;

out vec4 fragColor;

Expand Down Expand Up @@ -163,13 +163,13 @@ void main()
} else if ( hasGlowMap ) {
emissive += glowMap.rgb * glowMult;
}
emissive *= lightingMap.a;
emissive *= lightingMap.a * lightingControls.z;

vec3 f0 = max(reflMap.rgb, vec3(0.02));

// Specular
float roughness = 1.0 - lightingMap.r;
vec3 spec = LightingFuncGGX_REF(NdotL0, NdotH, NdotV, max(roughness, 0.02)) * D.rgb;
vec3 spec = LightingFuncGGX_REF(NdotL0, NdotH, NdotV, max(roughness, 0.02)) * lightSourceDiffuse[0].rgb;

// Diffuse
vec3 diffuse = vec3(NdotL0);
Expand All @@ -183,7 +183,7 @@ void main()

// Environment
vec3 refl = vec3(0.0);
vec3 ambient = A.rgb;
vec3 ambient = lightSourceAmbient.rgb;
if ( hasCubeMap ) {
float m = roughness * (roughness * -4.0 + 10.0);
refl = textureLod(CubeMap, reflectedWS, max(m, 0.0)).rgb;
Expand Down Expand Up @@ -221,7 +221,7 @@ void main()
//}

// Diffuse
color.rgb = diffuse * albedo * D.rgb;
color.rgb = diffuse * albedo * lightSourceDiffuse[0].rgb;
// Ambient
color.rgb += ambient * albedo * ao;
// Specular
Expand All @@ -231,7 +231,7 @@ void main()
// Emissive
color.rgb += emissive;

color.rgb = tonemap(color.rgb * D.a, A.a);
color.rgb = tonemap(color.rgb * lightingControls.y, lightingControls.x);

fragColor = color;
}
31 changes: 9 additions & 22 deletions res/shaders/f76_default.vert
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,14 @@ out vec2 texCoord;

out mat3 btnMatrix;

out vec4 A;
out vec4 C;
out vec4 D;

out mat3 reflMatrix;
flat out mat3 reflMatrix;

uniform mat3 viewMatrix;
uniform mat3 normalMatrix;
#include "uniforms.glsl"

uniform mat3 normalMatrix; // in row-major order
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
uniform vec4 lightSourcePosition[3]; // W0 = environment map rotation (-1.0 to 1.0), W1, W2 = viewport X, Y
uniform vec4 lightSourceDiffuse[3]; // A0 = overall brightness, A1, A2 = viewport width, height
uniform vec4 lightSourceAmbient; // A = tone mapping control (1.0 = full tone mapping)

uniform vec4 vertexColorOverride; // components greater than zero replace the vertex color

Expand All @@ -32,23 +27,14 @@ layout ( location = 7 ) in vec2 multiTexCoord0;

#include "bonetransform.glsl"

mat3 rotateEnv( mat3 m, float rz )
{
float rz_c = cos(rz);
float rz_s = -sin(rz);
return mat3(vec3(m[0][0] * rz_c - m[0][1] * rz_s, m[0][0] * rz_s + m[0][1] * rz_c, m[0][2] * -1.0),
vec3(m[1][0] * rz_c - m[1][1] * rz_s, m[1][0] * rz_s + m[1][1] * rz_c, m[1][2] * -1.0),
vec3(m[2][0] * rz_c - m[2][1] * rz_s, m[2][0] * rz_s + m[2][1] * rz_c, m[2][2] * -1.0));
}

void main()
{
vec4 v = vec4( vertexPosition, 1.0 );
vec3 n = normalVector;
vec3 t = tangentVector;
vec3 b = bitangentVector;

if ( numBones > 0 )
if ( boneWeights[0] > 0.0 && renderOptions1.x != 0 )
boneTransform( v, n, t, b );

v = modelViewMatrix * v;
Expand All @@ -59,15 +45,16 @@ void main()
btnMatrix[1] = normalize( t * normalMatrix );
btnMatrix[0] = normalize( b * normalMatrix );

reflMatrix = rotateEnv( viewMatrix, lightSourcePosition[0].w * 3.14159265 );
reflMatrix = envMapRotation;
reflMatrix[0][2] *= -1.0;
reflMatrix[1][2] *= -1.0;
reflMatrix[2][2] *= -1.0;

if ( projectionMatrix[3][3] == 1.0 )
ViewDir = vec3(0.0, 0.0, 1.0); // orthographic view
else
ViewDir = -v.xyz;
LightDir = lightSourcePosition[0].xyz;

A = lightSourceAmbient;
C = mix( vertexColor, vertexColorOverride, greaterThan( vertexColorOverride, vec4( 0.0 ) ) );
D = lightSourceDiffuse[0];
}
14 changes: 7 additions & 7 deletions res/shaders/f76_effectshader.frag
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#version 410 core

#include "uniforms.glsl"

uniform sampler2D BaseMap;
uniform sampler2D GreyscaleMap;
uniform samplerCube CubeMap;
Expand Down Expand Up @@ -46,12 +48,10 @@ in vec3 ViewDir;

in vec2 texCoord;

in vec4 A;
in vec4 C;
in vec4 D;

in mat3 btnMatrix;
in mat3 reflMatrix;
flat in mat3 reflMatrix;

out vec4 fragColor;

Expand Down Expand Up @@ -149,8 +149,8 @@ void main()
discard;
}

vec3 diffuse = A.rgb + (D.rgb * NdotL);
color.rgb = mix( color.rgb, color.rgb * D.rgb, lightingInfluence );
vec3 diffuse = lightSourceAmbient.rgb + (lightSourceDiffuse[0].rgb * NdotL);
color.rgb = mix( color.rgb, color.rgb * lightSourceDiffuse[0].rgb, lightingInfluence );

// Specular
float g = 1.0;
Expand All @@ -167,11 +167,11 @@ void main()
float m = roughness * (roughness * -4.0 + 10.0);
vec3 cube = textureLod( CubeMap, reflectedWS, max(m, 0.0) ).rgb;
cube *= envReflection * g;
cube = mix( cube, cube * D.rgb, lightingInfluence );
cube = mix( cube, cube * lightSourceDiffuse[0].rgb, lightingInfluence );
if ( hasEnvMask )
cube *= texture( EnvironmentMap, offset ).rgb;
color.rgb += cube * falloff;
}

fragColor = vec4( color.rgb * D.a, color.a );
fragColor = vec4( color.rgb * lightingControls.y, color.a );
}
17 changes: 9 additions & 8 deletions res/shaders/fo4_default.frag
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ in vec3 ViewDir;

in vec2 texCoord;

in vec4 A;
flat in vec4 A;
in vec4 C;
in vec4 D;
flat in vec4 D;
flat in float glowScale;

in mat3 btnMatrix;
in mat3 reflMatrix;
flat in mat3 reflMatrix;

out vec4 fragColor;

Expand Down Expand Up @@ -190,7 +191,7 @@ vec3 TorranceSparrow(float NdotL, float NdotH, float NdotV, float VdotH, vec3 co
return color * spec * M_PI;
}

vec3 tonemap(vec3 x, float y)
vec3 tonemap(vec3 x)
{
float a = 0.15;
float b = 0.50;
Expand All @@ -199,9 +200,9 @@ vec3 tonemap(vec3 x, float y)
float e = 0.02;
float f = 0.30;

vec3 z = x * (y * 4.22978723);
vec3 z = x * x * D.a * (A.a * 4.22978723);
z = (z * (a * z + b * c) + d * e) / (z * (a * z + b) + d * f) - e / f;
return z / (y * 0.93333333);
return sqrt(z / (A.a * 0.93333333));
}

vec4 colorLookup( float x, float y ) {
Expand Down Expand Up @@ -338,9 +339,9 @@ void main()
color.rgb += spec;
color.rgb += A.rgb * specMask * fresnelSchlick( VdotH, 0.2 ) * (1.0 - NdotV) * D.rgb;
// Emissive
color.rgb += emissive;
color.rgb += emissive * glowScale;

color.rgb = tonemap( color.rgb * D.a, A.a );
color.rgb = tonemap( color.rgb );

fragColor = color;
}
Loading

0 comments on commit 6cb8bf4

Please sign in to comment.