Skip to content

Commit

Permalink
More legacy matrix library removals
Browse files Browse the repository at this point in the history
Also fixes GTA3 in local mode.
  • Loading branch information
magcius committed Jan 3, 2025
1 parent edf8cfd commit a394d77
Show file tree
Hide file tree
Showing 32 changed files with 262 additions and 411 deletions.
3 changes: 2 additions & 1 deletion rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export default defineConfig({
// at the root.
copy: [
{ from: 'src/**/*.wasm', to: '[name][ext]' },
{ from: 'node_modules/librw/lib/librw.wasm' },
{ from: 'node_modules/librw/lib/librw.wasm', to: 'static/js/[name][ext]' },
{ from: 'src/vendor/basis_universal/basis_transcoder.wasm', to: 'static/js/[name][ext]' },
],
},
// Enable async TypeScript type checking.
Expand Down
12 changes: 6 additions & 6 deletions src/AShortHike/Scenes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ varying vec2 v_TexCoord0;
#if defined VERT
void mainVS() {
Mat4x3 t_WorldFromLocalMatrix = CalcWorldFromLocalMatrix();
vec3 t_PositionWorld = Mul(t_WorldFromLocalMatrix, vec4(a_Position, 1.0));
mat4x3 t_WorldFromLocalMatrix = CalcWorldFromLocalMatrix();
vec3 t_PositionWorld = t_WorldFromLocalMatrix * vec4(a_Position, 1.0);
vec3 t_LightDirection = normalize(vec3(.2, -1, .5));
vec3 normal = MulNormalMatrix(t_WorldFromLocalMatrix, normalize(a_Normal));
float t_LightIntensityF = dot(-normal, t_LightDirection);
float t_LightIntensityB = dot( normal, t_LightDirection);
gl_Position = Mul(u_ProjectionView, vec4(t_PositionWorld, 1.0));
gl_Position = u_ProjectionView * vec4(t_PositionWorld, 1.0);
v_LightIntensity = vec2(t_LightIntensityF, t_LightIntensityB);
v_TexCoord0 = CalcScaleBias(a_TexCoord0, u_MainTexST);
}
Expand Down Expand Up @@ -126,14 +126,14 @@ uniform sampler2D u_Splat3;
#ifdef VERT
void mainVS() {
Mat4x3 t_WorldFromLocalMatrix = CalcWorldFromLocalMatrix();
vec3 t_PositionWorld = Mul(t_WorldFromLocalMatrix, vec4(a_Position, 1.0));
mat4x3 t_WorldFromLocalMatrix = CalcWorldFromLocalMatrix();
vec3 t_PositionWorld = t_WorldFromLocalMatrix * vec4(a_Position, 1.0);
vec3 t_LightDirection = normalize(vec3(.2, -1, .5));
vec3 normal = MulNormalMatrix(t_WorldFromLocalMatrix, normalize(a_Normal));
float t_LightIntensityF = dot(-normal, t_LightDirection);
float t_LightIntensityB = dot( normal, t_LightDirection);
gl_Position = Mul(u_ProjectionView, vec4(t_PositionWorld, 1.0));
gl_Position = u_ProjectionView * vec4(t_PositionWorld, 1.0);
v_LightIntensity = vec2(t_LightIntensityF, t_LightIntensityB);
for (int i = 0; i < 6; i++)
Expand Down
10 changes: 5 additions & 5 deletions src/Common/Unity/GameObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ export class UnityShaderProgramBase extends DeviceProgram {
public static Common = `
precision mediump float;
layout(std140) uniform ub_SceneParams {
Mat4x4 u_ProjectionView;
layout(std140, row_major) uniform ub_SceneParams {
mat4 u_ProjectionView;
};
layout(std140) uniform ub_ShapeParams {
layout(std140, row_major) uniform ub_ShapeParams {
// TODO(jstpierre): Skinned mesh
Mat4x3 u_BoneMatrix[1];
mat4x3 u_BoneMatrix[1];
};
#ifdef VERT
Expand All @@ -137,7 +137,7 @@ layout(location = ${UnityChannel.BlendWeight}) attribute vec4 a_BlendWeight;
${GfxShaderLibrary.MulNormalMatrix}
${GfxShaderLibrary.CalcScaleBias}
Mat4x3 CalcWorldFromLocalMatrix() {
mat4x3 CalcWorldFromLocalMatrix() {
return u_BoneMatrix[0];
}
#endif
Expand Down
16 changes: 7 additions & 9 deletions src/DiddyKongRacing/DkrDrawCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ const FLAG_IS_INVISIBLE_GEOMETRY = 0x00000100;
const FLAG_IS_ENV_MAP_ENABLED = 0x00008000; // Spherical Environment Mapping
const FLAG_IS_TEXTURE_ANIMATED = 0x00010000;

const viewMatrixScratch = mat4.create();
const viewMatrixCalcScratch = mat4.create();
const scratchMatrix = mat4.create();
const mirrorMatrix = mat4.fromValues(
-1, 0, 0, 0,
0, 1, 0, 0,
Expand Down Expand Up @@ -259,16 +258,15 @@ export class DkrDrawCall {
offs += 16;
}

computeViewMatrix(viewMatrixScratch, viewerInput.camera);
if(DkrControlGlobals.ADV2_MIRROR.on) {
mat4.mul(viewMatrixCalcScratch, mirrorMatrix, params.modelMatrix);
mat4.mul(viewMatrixCalcScratch, viewMatrixScratch, viewMatrixCalcScratch);
if (DkrControlGlobals.ADV2_MIRROR.on) {
mat4.mul(scratchMatrix, mirrorMatrix, params.modelMatrix);
mat4.mul(scratchMatrix, viewerInput.camera.viewMatrix, scratchMatrix);
} else {
mat4.mul(viewMatrixCalcScratch, viewMatrixScratch, params.modelMatrix);
mat4.mul(scratchMatrix, viewerInput.camera.viewMatrix, params.modelMatrix);
}
offs += fillMatrix4x3(d, offs, viewMatrixCalcScratch);
offs += fillMatrix4x3(d, offs, scratchMatrix);

if(!!params.objAnim) {
if (!!params.objAnim) {
const currentFrameIndex = params.objAnim.getCurrentFrame();
this.vertexBufferDescriptors[0].byteOffset = this.objAnimPositionBufferByteOffset[params.objAnimIndex][currentFrameIndex];
const nextFrameIndex = (currentFrameIndex + 1) % params.objAnim.getKeyframes().length;
Expand Down
16 changes: 6 additions & 10 deletions src/DiddyKongRacing/DkrSprites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ import { DkrControlGlobals } from './DkrControlGlobals.js';
import { DkrObject, MODEL_TYPE_2D_BILLBOARD } from './DkrObject.js';
import { F3DDKR_Sprite_Program, MAX_NUM_OF_SPRITE_FRAMES, MAX_NUM_OF_SPRITE_INSTANCES } from './F3DDKR_Sprite_Program.js';

const viewMatrixScratch = mat4.create();
const viewMatrixCalcScratch = mat4.create();
const viewMatrixCalc2Scratch = mat4.create();
const scratchMatrix = mat4.create();
const mirrorMatrix = mat4.fromValues(
-1, 0, 0, 0,
0, 1, 0, 0,
Expand Down Expand Up @@ -239,8 +237,6 @@ export class DkrSprites {
this.bind(renderInst);
renderInst.sortKey = setSortKeyDepth(renderInst.sortKey, 0);

computeViewMatrix(viewMatrixScratch, viewerInput.camera);

assert(layerInstances.length <= MAX_NUM_OF_SPRITE_INSTANCES);
for (let i = 0; i < layerInstances.length; i++) {
const instanceObject: DkrObject = layerInstances[i];
Expand All @@ -253,13 +249,13 @@ export class DkrSprites {
const color = instanceObject.getSpriteColor();
offs += fillVec4v(d, offs, color);
if (DkrControlGlobals.ADV2_MIRROR.on) {
mat4.mul(viewMatrixCalcScratch, mirrorMatrix, instanceObject.getModelMatrix());
mat4.mul(viewMatrixCalcScratch, viewMatrixScratch, viewMatrixCalcScratch);
mat4.mul(scratchMatrix, mirrorMatrix, instanceObject.getModelMatrix());
mat4.mul(scratchMatrix, viewerInput.camera.viewMatrix, scratchMatrix);
} else {
mat4.mul(viewMatrixCalcScratch, viewMatrixScratch, instanceObject.getModelMatrix());
mat4.mul(scratchMatrix, viewerInput.camera.viewMatrix, instanceObject.getModelMatrix());
}
calcBillboardMatrix(viewMatrixCalc2Scratch, viewMatrixCalcScratch, CalcBillboardFlags.UseRollLocal | CalcBillboardFlags.PriorityZ | CalcBillboardFlags.UseZPlane);
offs += fillMatrix4x3(d, offs, viewMatrixCalc2Scratch);
calcBillboardMatrix(scratchMatrix, scratchMatrix, CalcBillboardFlags.UseRollLocal | CalcBillboardFlags.PriorityZ | CalcBillboardFlags.UseZPlane);
offs += fillMatrix4x3(d, offs, scratchMatrix);
}

// Set tex parameters
Expand Down
24 changes: 14 additions & 10 deletions src/DiddyKongRacing/F3DDKR_Program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ export class F3DDKR_Program extends DeviceProgram {
public override both = `
precision mediump float;
layout(std140) uniform ub_SceneParams {
Mat4x4 u_Projection;
layout(std140, row_major) uniform ub_SceneParams {
mat4 u_Projection;
};
layout(std140) uniform ub_DrawParams {
layout(std140, row_major) uniform ub_DrawParams {
vec4 u_Color;
vec4 u_Misc[1];
Mat4x3 u_ViewMatrix;
mat4x3 u_ModelViewMatrix;
};
#define u_TexCoordOffset (u_Misc[0].xy)
Expand Down Expand Up @@ -59,18 +59,21 @@ void main() {
vec3 pos;
bvec4 t_Options = DecodeOptions();
if(t_Options.w) { // t_Options.w = Use object animation
if (t_Options.w) { // t_Options.w = Use object animation
pos = mix(a_Position, a_Position_2, u_AnimProgress); // lerp between the keyframes.
} else {
pos = a_Position; // Just use the default position.
}
gl_Position = Mul(u_Projection, Mul(_Mat4x4(u_ViewMatrix), vec4(pos, 1.0)));
if(t_Options.z) {
vec3 t_PositionView = u_ModelViewMatrix * vec4(pos, 1.0);
gl_Position = u_Projection * vec4(t_PositionView, 1.0);
if (t_Options.z) {
v_Color = vec4(1.0, 1.0, 1.0, 1.0);
} else {
v_Color = vec4(a_Color.xyz, a_Color.w * u_Color.w);
}
v_TexCoord = a_TexCoord + u_TexCoordOffset.xy;
}
`;
Expand All @@ -94,17 +97,18 @@ void main() {
bvec4 t_Options = DecodeOptions();
if(t_Options.x) {
if (t_Options.x) {
textureColor = Texture2D_N64_Bilerp(PP_SAMPLER_2D(u_Texture), v_TexCoord);
}
if(t_Options.y) {
if (t_Options.y) {
vertexColor = v_Color;
}
gl_FragColor = vertexColor * textureColor;
if(gl_FragColor.a == 0.0) discard;
if (gl_FragColor.a == 0.0)
discard;
}
`;

Expand Down
14 changes: 8 additions & 6 deletions src/DiddyKongRacing/F3DDKR_Sprite_Program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ export class F3DDKR_Sprite_Program extends DeviceProgram {
public override both = `
precision mediump float;
layout(std140) uniform ub_SceneParams {
Mat4x4 u_Projection;
layout(std140, row_major) uniform ub_SceneParams {
mat4 u_Projection;
};
struct SpriteInstance {
vec4 info; // x = u_TexCoords index, y = Number of frames, z = alpha test, w = offset y
vec4 color;
Mat4x3 viewMatrix;
mat4x3 modelViewMatrix;
};
layout(std140) uniform ub_DrawParams {
layout(std140, row_major) uniform ub_DrawParams {
vec4 u_SpritesInfo; // x = Current frame.
SpriteInstance u_Instances[${MAX_NUM_OF_SPRITE_INSTANCES}];
};
layout(std140) uniform ub_TexParams {
layout(std140, row_major) uniform ub_TexParams {
vec4 u_TexCoords[${MAX_NUM_OF_SPRITE_FRAMES}];
};
Expand Down Expand Up @@ -59,7 +59,9 @@ void main() {
v_Color = instance.color;
gl_Position = Mul(u_Projection, Mul(_Mat4x4(instance.viewMatrix), vec4((a_Position+offset)*spriteSize, 1.0, 1.0)));
vec2 t_PositionLocal = (a_Position + offset) * spriteSize;
vec3 t_PositionView = instance.modelViewMatrix * vec4(t_PositionLocal, 1.0, 1.0);
gl_Position = u_Projection * vec4(t_PositionView, 1.0);
if(gl_VertexID == 0) {
v_TexCoord = vec2(x, y);
Expand Down
47 changes: 25 additions & 22 deletions src/DragonQuest8/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ export class DQ8Program extends DeviceProgram {
#define SMOOTH_SKINNING() (SKINNING_MATRIX_COUNT > 0)
precision mediump float;
layout(std140) uniform ub_SceneParams {
Mat4x4 u_Projection;
layout(std140, row_major) uniform ub_SceneParams {
mat4 u_Projection;
};
layout(std140) uniform ub_MDTSubmeshParams {
Mat4x4 u_ModelView;
Mat4x4 u_RigidTrans;
layout(std140, row_major) uniform ub_MDTSubmeshParams {
mat4x3 u_ModelView;
mat4x3 u_RigidTrans;
#if SMOOTH_SKINNING()
Mat4x3 u_JointMatrix[SKINNING_MATRIX_COUNT];
mat4x3 u_JointMatrix[SKINNING_MATRIX_COUNT];
#endif
vec4 u_BGColor;
vec4 u_BGColor2;
Expand Down Expand Up @@ -76,28 +76,31 @@ out vec2 v_TexCoord;
out vec4 v_col;
void mainVS() {
vec4 pos = vec4(a_Position, 1.0);
#if SMOOTH_SKINNING()
Mat4x3 t_JointMatrix = _Mat4x3(0.0);
Fma(t_JointMatrix, u_JointMatrix[int(a_JointIndices.x)], a_JointWeights.x);
Fma(t_JointMatrix, u_JointMatrix[int(a_JointIndices.y)], a_JointWeights.y);
Fma(t_JointMatrix, u_JointMatrix[int(a_JointIndices.z)], a_JointWeights.z);
Fma(t_JointMatrix, u_JointMatrix[int(a_JointIndices.w)], a_JointWeights.w);
pos = Mul(_Mat4x4(t_JointMatrix), pos);
mat4x3 t_JointMatrix = mat4x3(0.0);
t_JointMatrix += u_JointMatrix[int(a_JointIndices.x)] * a_JointWeights.x;
t_JointMatrix += u_JointMatrix[int(a_JointIndices.y)] * a_JointWeights.y;
t_JointMatrix += u_JointMatrix[int(a_JointIndices.z)] * a_JointWeights.z;
t_JointMatrix += u_JointMatrix[int(a_JointIndices.w)] * a_JointWeights.w;
#else
pos = Mul(u_RigidTrans,pos);
mat4x3 t_JointMatrix = u_RigidTrans;
#endif
vec3 t_PositionLocal = t_JointMatrix * vec4(a_Position, 1.0);
if (u_bIsSkybox > 0.0)
pos = Mul(u_RigidTrans,pos);
t_PositionLocal = u_RigidTrans * vec4(t_PositionLocal, 1.0); // ???
if (u_bIsSkybox > 0.0)
v_col = u_BGColor;
else
v_col = a_vColor;
if (u_bIsSkybox > 0.0 && pos.y < 2000.0 && pos.y > -2000.0)
if (u_bIsSkybox > 0.0 && t_PositionLocal.y < 2000.0 && t_PositionLocal.y > -2000.0)
v_col = u_BGColor2;
gl_Position = Mul(u_Projection, Mul(u_ModelView,pos));
vec3 t_PositionView = u_ModelView * vec4(t_PositionLocal, 1.0);
gl_Position = u_Projection * vec4(t_PositionView, 1.0);
v_TexCoord = a_TexCoord;
}
#endif
Expand Down Expand Up @@ -305,17 +308,17 @@ export class MDTSubmeshInstance {

let offs = renderInst.allocateUniformBuffer(DQ8Program.ub_MDTSubmeshParams, 16 * 2 + 12 + 16 * (this.mdtData.smoothSkinning ? MDS.MDS.maxJointCount : 0));
const d = renderInst.mapUniformBufferF32(DQ8Program.ub_MDTSubmeshParams);
offs += fillMatrix4x4(d, offs, viewMatrix);
offs += fillMatrix4x3(d, offs, viewMatrix);

if (jointPerVertCount && !bIsSkybox) {
mat4.invert(scratchMatrix, modelMatrix);
mat4.mul(scratchMatrix, scratchMatrix, boneMatrices[this.rigidJointId]);
offs += fillMatrix4x4(d, offs, scratchMatrix);
offs += fillMatrix4x3(d, offs, scratchMatrix);
} else {
if (!bIsSkybox)
offs += fillMatrix4x4(d, offs, boneMatrices[this.rigidJointId]);
offs += fillMatrix4x3(d, offs, boneMatrices[this.rigidJointId]);
else
offs += fillMatrix4x4(d, offs, modelMatrix);
offs += fillMatrix4x3(d, offs, modelMatrix);
}

if (this.mdtData.smoothSkinning) {
Expand Down
Loading

0 comments on commit a394d77

Please sign in to comment.