Skip to content

Commit 5614b60

Browse files
committed
Port more games to new matrix workaround library
1 parent 1e7ae85 commit 5614b60

16 files changed

+157
-142
lines changed

src/AShortHike/Scenes.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class TempMaterialProgram extends UnityShaderProgramBase {
1919
public override both = `
2020
${UnityShaderProgramBase.Common}
2121
22-
layout(std140, row_major) uniform ub_MaterialParams {
22+
layout(std140) uniform ub_MaterialParams {
2323
vec4 u_Color;
2424
vec4 u_MainTexST;
2525
vec4 u_Misc[1];
@@ -39,7 +39,7 @@ void mainVS() {
3939
float t_LightIntensityF = dot(-normal, t_LightDirection);
4040
float t_LightIntensityB = dot( normal, t_LightDirection);
4141
42-
gl_Position = u_ProjectionView * vec4(t_PositionWorld, 1.0);
42+
gl_Position = UnpackMatrix(u_ProjectionView) * vec4(t_PositionWorld, 1.0);
4343
v_LightIntensity = vec2(t_LightIntensityF, t_LightIntensityB);
4444
v_TexCoord0 = CalcScaleBias(a_TexCoord0, u_MainTexST);
4545
}
@@ -108,7 +108,7 @@ class TerrainMaterialProgram extends UnityShaderProgramBase {
108108
public override both = `
109109
${UnityShaderProgramBase.Common}
110110
111-
layout(std140, row_major) uniform ub_MaterialParams {
111+
layout(std140) uniform ub_MaterialParams {
112112
vec4 u_Color;
113113
vec4 u_TexST[6];
114114
vec4 u_Misc[1];
@@ -133,7 +133,7 @@ void mainVS() {
133133
float t_LightIntensityF = dot(-normal, t_LightDirection);
134134
float t_LightIntensityB = dot( normal, t_LightDirection);
135135
136-
gl_Position = u_ProjectionView * vec4(t_PositionWorld, 1.0);
136+
gl_Position = UnpackMatrix(u_ProjectionView) * vec4(t_PositionWorld, 1.0);
137137
v_LightIntensity = vec2(t_LightIntensityF, t_LightIntensityB);
138138
139139
for (int i = 0; i < 6; i++)

src/Common/Unity/GameObject.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,15 @@ export class UnityShaderProgramBase extends DeviceProgram {
110110
public static Common = `
111111
precision mediump float;
112112
113-
layout(std140, row_major) uniform ub_SceneParams {
114-
mat4 u_ProjectionView;
113+
${GfxShaderLibrary.MatrixLibrary}
114+
115+
layout(std140) uniform ub_SceneParams {
116+
Mat4x4 u_ProjectionView;
115117
};
116118
117-
layout(std140, row_major) uniform ub_ShapeParams {
119+
layout(std140) uniform ub_ShapeParams {
118120
// TODO(jstpierre): Skinned mesh
119-
mat4x3 u_BoneMatrix[1];
121+
Mat3x4 u_BoneMatrix[1];
120122
};
121123
122124
#ifdef VERT
@@ -138,7 +140,7 @@ ${GfxShaderLibrary.MulNormalMatrix}
138140
${GfxShaderLibrary.CalcScaleBias}
139141
140142
mat4x3 CalcWorldFromLocalMatrix() {
141-
return u_BoneMatrix[0];
143+
return UnpackMatrix(u_BoneMatrix[0]);
142144
}
143145
#endif
144146
`;

src/DarkSouls/render.ts

+35-28
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,11 @@ class MaterialProgram_Base extends DeviceProgram {
355355
public static ub_MeshFragParams = 1;
356356

357357
public static BindingDefinitions = `
358+
${GfxShaderLibrary.MatrixLibrary}
359+
358360
// Expected to be constant across the entire scene.
359-
layout(std140, row_major) uniform ub_SceneParams {
360-
mat4 u_ProjectionView;
361+
layout(std140) uniform ub_SceneParams {
362+
Mat4x4 u_ProjectionView;
361363
vec4 u_CameraPosWorld; // DebugMode is in w
362364
};
363365
@@ -570,8 +572,8 @@ precision mediump float;
570572
571573
${MaterialProgram_Base.BindingDefinitions}
572574
573-
layout(std140, row_major) uniform ub_MeshFragParams {
574-
mat4x3 u_WorldFromLocal[1];
575+
layout(std140) uniform ub_MeshFragParams {
576+
Mat3x4 u_WorldFromLocal[1];
575577
vec4 u_DiffuseMapColor;
576578
vec4 u_SpecularMapColor;
577579
vec4 u_EnvDifColor;
@@ -651,18 +653,19 @@ layout(location = ${MaterialProgram_Base.a_Tangent1}) in vec4 a_Tangent1;
651653
${GfxShaderLibrary.MulNormalMatrix}
652654
653655
void main() {
654-
vec3 t_PositionWorld = u_WorldFromLocal[0] * vec4(a_Position, 1.0);
656+
mat4x3 t_WorldFromLocal = UnpackMatrix(u_WorldFromLocal[0]);
657+
vec3 t_PositionWorld = t_WorldFromLocal * vec4(a_Position, 1.0);
655658
v_PositionWorld = t_PositionWorld.xyz;
656-
gl_Position = u_ProjectionView * vec4(t_PositionWorld, 1.0);
659+
gl_Position = UnpackMatrix(u_ProjectionView) * vec4(t_PositionWorld, 1.0);
657660
658-
vec3 t_NormalWorld = MulNormalMatrix(u_WorldFromLocal[0], UNORM_TO_SNORM(a_Normal.xyz));
661+
vec3 t_NormalWorld = MulNormalMatrix(t_WorldFromLocal, UNORM_TO_SNORM(a_Normal.xyz));
659662
v_TangentSpaceBasisZ = t_NormalWorld;
660663
661-
vec3 t_TangentWorld0 = normalize(u_WorldFromLocal[0] * vec4(UNORM_TO_SNORM(a_Tangent0.xyz), 0.0));
664+
vec3 t_TangentWorld0 = normalize(t_WorldFromLocal * vec4(UNORM_TO_SNORM(a_Tangent0.xyz), 0.0));
662665
v_TangentSpaceBasisY0 = vec4(t_TangentWorld0, UNORM_TO_SNORM(a_Tangent0.w));
663666
664667
#ifdef HAS_TANGENT1
665-
vec3 t_TangentWorld1 = normalize(u_WorldFromLocal[0] * vec4(UNORM_TO_SNORM(a_Tangent1.xyz), 0.0));
668+
vec3 t_TangentWorld1 = normalize(t_WorldFromLocal * vec4(UNORM_TO_SNORM(a_Tangent1.xyz), 0.0));
666669
v_TangentSpaceBasisY1 = vec4(t_TangentWorld1, UNORM_TO_SNORM(a_Tangent1.w));
667670
#endif
668671
@@ -1010,8 +1013,8 @@ ${MaterialProgram_Base.BindingDefinitions}
10101013
10111014
layout(binding = 8) uniform samplerCube u_TextureDummy;
10121015
1013-
layout(std140, row_major) uniform ub_MeshFragParams {
1014-
mat4x3 u_WorldFromLocal[1];
1016+
layout(std140) uniform ub_MeshFragParams {
1017+
Mat3x4 u_WorldFromLocal[1];
10151018
DirectionalLight u_DirectionalLight;
10161019
FogParams u_FogParams;
10171020
vec4 u_Misc[7];
@@ -1037,9 +1040,10 @@ layout(location = ${MaterialProgram_Base.a_Position}) in vec3 a_Position;
10371040
layout(location = ${MaterialProgram_Base.a_TexCoord0}) in vec4 a_TexCoord0;
10381041
10391042
void main() {
1040-
vec3 t_PositionWorld = u_WorldFromLocal[0] * vec4(a_Position, 1.0);
1043+
mat4x3 t_WorldFromLocal = UnpackMatrix(u_WorldFromLocal[0]);
1044+
vec3 t_PositionWorld = t_WorldFromLocal * vec4(a_Position, 1.0);
10411045
v_PositionWorld = t_PositionWorld.xyz;
1042-
gl_Position = u_ProjectionView * vec4(t_PositionWorld, 1.0);
1046+
gl_Position = UnpackMatrix(u_ProjectionView) * vec4(t_PositionWorld, 1.0);
10431047
10441048
v_TexCoord0.xy = DecodeTexCoord(a_TexCoord0.xy) * u_TileScale.xx + u_TexScroll0.xy;
10451049
v_TexCoord1.xy = DecodeTexCoord(a_TexCoord0.xy) * u_TileScale.yy + u_TexScroll1.xy;
@@ -1076,8 +1080,8 @@ ${MaterialProgram_Base.BindingDefinitions}
10761080
10771081
layout(binding = 8) uniform samplerCube u_TextureDummy;
10781082
1079-
layout(std140, row_major) uniform ub_MeshFragParams {
1080-
mat4x3 u_WorldFromLocal[1];
1083+
layout(std140) uniform ub_MeshFragParams {
1084+
Mat3x4 u_WorldFromLocal[1];
10811085
DirectionalLight u_DirectionalLight;
10821086
FogParams u_FogParams;
10831087
vec4 u_Misc[7];
@@ -1129,20 +1133,21 @@ layout(location = ${MaterialProgram_Base.a_Tangent1}) in vec4 a_Tangent1;
11291133
${GfxShaderLibrary.MulNormalMatrix}
11301134
11311135
void main() {
1132-
vec3 t_PositionWorld = u_WorldFromLocal[0] * vec4(a_Position, 1.0);
1136+
mat4x3 t_WorldFromLocal = UnpackMatrix(u_WorldFromLocal[0]);
1137+
vec3 t_PositionWorld = t_WorldFromLocal * vec4(a_Position, 1.0);
11331138
v_PositionWorld = t_PositionWorld.xyz;
1134-
gl_Position = u_ProjectionView * vec4(t_PositionWorld, 1.0);
1139+
gl_Position = UnpackMatrix(u_ProjectionView) * vec4(t_PositionWorld, 1.0);
11351140
11361141
v_Color = a_Color;
11371142
v_TexCoord0 = a_TexCoord0.xy;
11381143
v_TexCoordProj.xyz = gl_Position.xyw;
11391144
1140-
v_TangentSpaceBasisZ = MulNormalMatrix(u_WorldFromLocal[0], UNORM_TO_SNORM(a_Normal.xyz));
1141-
v_TangentSpaceBasisY = normalize(u_WorldFromLocal[0] * vec4(UNORM_TO_SNORM(a_Tangent0.xyz), 0.0));
1145+
v_TangentSpaceBasisZ = MulNormalMatrix(t_WorldFromLocal, UNORM_TO_SNORM(a_Normal.xyz));
1146+
v_TangentSpaceBasisY = normalize(t_WorldFromLocal * vec4(UNORM_TO_SNORM(a_Tangent0.xyz), 0.0));
11421147
v_TangentSpaceBasisX = normalize(cross(v_TangentSpaceBasisZ, v_TangentSpaceBasisY) * UNORM_TO_SNORM(a_Tangent0.w));
11431148
1144-
v_TexCoordProjX = (u_ProjectionView * vec4(t_PositionWorld + v_TangentSpaceBasisX, 0.0)).xyw;
1145-
v_TexCoordProjY = (u_ProjectionView * vec4(t_PositionWorld + v_TangentSpaceBasisY, 0.0)).xyw;
1149+
v_TexCoordProjX = (UnpackMatrix(u_ProjectionView) * vec4(t_PositionWorld + v_TangentSpaceBasisX, 0.0)).xyw;
1150+
v_TexCoordProjY = (UnpackMatrix(u_ProjectionView) * vec4(t_PositionWorld + v_TangentSpaceBasisY, 0.0)).xyw;
11461151
}
11471152
`;
11481153

@@ -1996,7 +2001,7 @@ class DepthOfFieldBlurProgram extends DeviceProgram {
19962001
uniform sampler2D u_TextureColor;
19972002
uniform sampler2D u_Texture2;
19982003
1999-
layout(std140, row_major) uniform ub_Params {
2004+
layout(std140) uniform ub_Params {
20002005
vec4 u_Misc[1];
20012006
};
20022007
#define u_DispersionSq (u_Misc[0].x)
@@ -2063,7 +2068,7 @@ class DepthOfFieldCombineProgram extends DeviceProgram {
20632068
uniform sampler2D u_TextureColor;
20642069
uniform sampler2D u_TextureFramebufferDepth;
20652070
2066-
layout(std140, row_major) uniform ub_Params {
2071+
layout(std140) uniform ub_Params {
20672072
vec4 u_Misc[3];
20682073
};
20692074
#define u_NearParam (u_Misc[1].xyz)
@@ -2269,7 +2274,7 @@ class BloomFilterProgram extends DeviceProgram {
22692274
uniform sampler2D u_TextureColor;
22702275
uniform sampler2D u_TextureFramebufferDepth;
22712276
2272-
layout(std140, row_major) uniform ub_Params {
2277+
layout(std140) uniform ub_Params {
22732278
vec4 u_Misc[3];
22742279
};
22752280
#define u_NearParam (u_Misc[0].xyz)
@@ -2456,7 +2461,7 @@ class BloomCombineProgram extends DeviceProgram {
24562461
uniform sampler2D u_TextureColor;
24572462
uniform sampler2D u_Texture2;
24582463
2459-
layout(std140, row_major) uniform ub_Params {
2464+
layout(std140) uniform ub_Params {
24602465
vec4 u_Misc[3];
24612466
};
24622467
#define u_NearParam (u_Misc[0].xyz)
@@ -2720,11 +2725,13 @@ class Bloom {
27202725

27212726
class ToneCorrectProgram extends DeviceProgram {
27222727
public static Common = `
2728+
${GfxShaderLibrary.MatrixLibrary}
2729+
27232730
uniform sampler2D u_TextureColor;
27242731
uniform sampler2D u_Texture2;
27252732
2726-
layout(std140, row_major) uniform ub_Params {
2727-
mat4x3 u_ToneCorrectMatrix;
2733+
layout(std140) uniform ub_Params {
2734+
Mat3x4 u_ToneCorrectMatrix;
27282735
vec4 u_Misc[1];
27292736
};
27302737
`;
@@ -2747,7 +2754,7 @@ void main() {
27472754
t_Color.rgb *= t_Exposure;
27482755
t_Color.rgb /= (t_Color.rgb + vec3(1.0));
27492756
2750-
t_Color.rgb = u_ToneCorrectMatrix * vec4(t_Color.rgb, 1.0);
2757+
t_Color.rgb = UnpackMatrix(u_ToneCorrectMatrix) * vec4(t_Color.rgb, 1.0);
27512758
gl_FragColor = t_Color;
27522759
}
27532760
`;

src/NeonWhite/Scenes.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void mainVS() {
4040
float t_LightIntensityF = dot(-normal, t_LightDirection);
4141
float t_LightIntensityB = dot( normal, t_LightDirection);
4242
43-
gl_Position = u_ProjectionView * vec4(t_PositionWorld, 1.0);
43+
gl_Position = UnpackMatrix(u_ProjectionView) * vec4(t_PositionWorld, 1.0);
4444
v_LightIntensity = vec2(t_LightIntensityF, t_LightIntensityB);
4545
v_TexCoord0 = CalcScaleBias(a_TexCoord0, u_MainTexST);
4646
}

src/OuterWilds/Scenes.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void mainVS() {
3939
float t_LightIntensityF = dot(-normal, t_LightDirection);
4040
float t_LightIntensityB = dot( normal, t_LightDirection);
4141
42-
gl_Position = u_ProjectionView * vec4(t_PositionWorld, 1.0);
42+
gl_Position = UnpackMatrix(u_ProjectionView) * vec4(t_PositionWorld, 1.0);
4343
v_LightIntensity = vec2(t_LightIntensityF, t_LightIntensityB);
4444
v_TexCoord0 = CalcScaleBias(a_TexCoord0, u_MainTexST);
4545
}
@@ -133,7 +133,7 @@ void mainVS() {
133133
float t_LightIntensityF = dot(-normal, t_LightDirection);
134134
float t_LightIntensityB = dot( normal, t_LightDirection);
135135
136-
gl_Position = u_ProjectionView * vec4(t_PositionWorld, 1.0);
136+
gl_Position = UnpackMatrix(u_ProjectionView) * vec4(t_PositionWorld, 1.0);
137137
v_LightIntensity = vec2(t_LightIntensityF, t_LightIntensityB);
138138
139139
for (int i = 0; i < 6; i++)

src/SourceEngine/Materials/MaterialBase.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,18 @@ export class MaterialShaderTemplateBase extends UberShaderTemplateBasic {
122122
// #define DEBUG_DIFFUSEONLY 1
123123
// #define DEBUG_FULLBRIGHT 1
124124
125-
layout(std140, row_major) uniform ub_SceneParams {
126-
mat4 u_ProjectionView;
125+
${GfxShaderLibrary.MatrixLibrary}
126+
127+
layout(std140) uniform ub_SceneParams {
128+
Mat4x4 u_ProjectionView;
127129
vec4 u_SceneMisc[3];
128130
};
129131
130-
layout(std140, row_major) uniform ub_SkinningParams {
132+
layout(std140) uniform ub_SkinningParams {
131133
#if SKINNING_MODE == ${SkinningMode.Smooth}
132-
mat4x3 u_BoneMatrix[${MaterialShaderTemplateBase.MaxSkinningParamsBoneMatrix}];
134+
Mat3x4 u_BoneMatrix[${MaterialShaderTemplateBase.MaxSkinningParamsBoneMatrix}];
133135
#else
134-
mat4x3 u_ModelMatrix;
136+
Mat3x4 u_ModelMatrix;
135137
#endif
136138
};
137139
@@ -252,14 +254,14 @@ mat4x3 CalcWorldFromLocalMatrix() {
252254
// Calculate our per-vertex position.
253255
mat4x3 t_WorldFromLocalMatrix = mat4x3(0.0);
254256
255-
t_WorldFromLocalMatrix += u_BoneMatrix[int(a_BoneIndices.x)] * a_BoneWeights.x;
256-
t_WorldFromLocalMatrix += u_BoneMatrix[int(a_BoneIndices.y)] * a_BoneWeights.y;
257-
t_WorldFromLocalMatrix += u_BoneMatrix[int(a_BoneIndices.z)] * a_BoneWeights.z;
258-
t_WorldFromLocalMatrix += u_BoneMatrix[int(a_BoneIndices.w)] * a_BoneWeights.w;
257+
t_WorldFromLocalMatrix += UnpackMatrix(u_BoneMatrix[int(a_BoneIndices.x)]) * a_BoneWeights.x;
258+
t_WorldFromLocalMatrix += UnpackMatrix(u_BoneMatrix[int(a_BoneIndices.y)]) * a_BoneWeights.y;
259+
t_WorldFromLocalMatrix += UnpackMatrix(u_BoneMatrix[int(a_BoneIndices.z)]) * a_BoneWeights.z;
260+
t_WorldFromLocalMatrix += UnpackMatrix(u_BoneMatrix[int(a_BoneIndices.w)]) * a_BoneWeights.w;
259261
260262
return t_WorldFromLocalMatrix;
261263
#else
262-
return u_ModelMatrix;
264+
return UnpackMatrix(u_ModelMatrix);
263265
#endif
264266
}
265267
#endif

src/SourceEngine/Materials/Material_Eyes.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ precision mediump float;
1919
2020
${MaterialShaderTemplateBase.Common}
2121
22-
layout(std140, row_major) uniform ub_ObjectParams {
23-
mat4x2 u_BaseTransform;
24-
mat4x2 u_IrisTransform;
22+
layout(std140) uniform ub_ObjectParams {
23+
Mat2x4 u_BaseTransform;
24+
Mat2x4 u_IrisTransform;
2525
};
2626
2727
varying vec3 v_PositionWorld;
@@ -37,10 +37,10 @@ void mainVS() {
3737
mat4x3 t_WorldFromLocalMatrix = CalcWorldFromLocalMatrix();
3838
vec3 t_PositionWorld = t_WorldFromLocalMatrix * vec4(a_Position, 1.0);
3939
v_PositionWorld.xyz = t_PositionWorld;
40-
gl_Position = u_ProjectionView * vec4(t_PositionWorld, 1.0);
40+
gl_Position = UnpackMatrix(u_ProjectionView) * vec4(t_PositionWorld, 1.0);
4141
42-
v_TexCoord0.xy = u_BaseTransform * vec4(a_TexCoord01.xy, 1.0, 1.0);
43-
v_TexCoord0.zw = u_IrisTransform * vec4(t_PositionWorld, 1.0);
42+
v_TexCoord0.xy = UnpackMatrix(u_BaseTransform) * vec4(a_TexCoord01.xy, 1.0, 1.0);
43+
v_TexCoord0.zw = UnpackMatrix(u_IrisTransform) * vec4(t_PositionWorld, 1.0);
4444
4545
// XXX(jstpierre): Move lighting into common helpers
4646
v_Lighting.rgb = vec3(1.0);

0 commit comments

Comments
 (0)