Skip to content

Commit

Permalink
Start removing workaround matrix library
Browse files Browse the repository at this point in the history
With ANGLE's Metal backend, I trust it for things like arrays of row_major matrices
  • Loading branch information
magcius committed Jan 2, 2025
1 parent 4f27e46 commit 47b254c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 69 deletions.
9 changes: 2 additions & 7 deletions src/DataFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,8 @@ export class DataFetcher {
public async init() {
if (IS_DEVELOPMENT) {
// Check for the existence of a /data directory.
const url = getDataURLForPath('', true);
try {
await this.fetchURL(url, {});
this.useDevelopmentStorage = true;
} catch(e) {
this.useDevelopmentStorage = false;
}
// TODO(jstpierre): Put back this fix for rsbuild.
this.useDevelopmentStorage = true;
} else {
this.useDevelopmentStorage = false;
}
Expand Down
65 changes: 31 additions & 34 deletions src/OcarinaOfTime3D/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class DMPProgram extends DeviceProgram {
public static a_BoneWeights = 7;

public static BindingsDefinition = `
struct Light {
vec4 Ambient;
vec4 Diffuse;
Expand All @@ -66,12 +65,12 @@ struct Light {
};
// Expected to be constant across the entire scene.
layout(std140) uniform ub_SceneParams {
Mat4x4 u_Projection;
layout(std140, row_major) uniform ub_SceneParams {
mat4 u_Projection;
};
// Expected to change with each material.
layout(std140) uniform ub_MaterialParams {
layout(std140, row_major) uniform ub_MaterialParams {
vec4 u_MatDiffuseColor;
vec4 u_MatAmbientColor;
vec4 u_MaterialFlags;
Expand All @@ -83,7 +82,7 @@ layout(std140) uniform ub_MaterialParams {
vec4 u_FogStartEnd;
vec4 u_ConstantColor[6];
Mat4x3 u_TexMtx[3];
mat4x3 u_TexMtx[3];
vec4 u_MatMisc[1];
};
Expand All @@ -96,9 +95,9 @@ layout(std140) uniform ub_MaterialParams {
#define u_RenderFog (u_MaterialFlags.z)
#define u_DepthOffset (u_MatMisc[0].w)
layout(std140) uniform ub_PrmParams {
Mat4x3 u_BoneMatrix[16];
Mat4x3 u_ViewMatrix;
layout(std140, row_major) uniform ub_PrmParams {
mat4x3 u_BoneMatrix[16];
mat4x3 u_ViewMatrix;
vec4 u_PrmMisc[1];
};
Expand Down Expand Up @@ -379,10 +378,10 @@ uniform samplerCube u_Cubemap;
case LutInput.CosNormalView: index = "dot(t_Normal, normalize(v_View.xyz))"; break;
case LutInput.CosLightNormal: index = "dot(t_LightVector, t_Normal)"; break;
case LutInput.CosLightSpot: index = "dot(t_LightVector, t_SpotDir)"; break;
case LutInput.CosPhi:{
const half_angle_proj = "normalize(t_HalfVector) - t_Normal * dot(t_Normal, normalize(t_HalfVector))"
index = `dot(${half_angle_proj}, t_Tangent)`;
} break;
case LutInput.CosPhi: {
const half_angle_proj = "normalize(t_HalfVector) - t_Normal * dot(t_Normal, normalize(t_HalfVector))"
index = `dot(${half_angle_proj}, t_Tangent)`;
} break;
}

output = `texture(SAMPLER_2D(u_TextureLUT), vec2(((${index} + 1.0) * 0.5) + (1.0 / 512.0), ${this.generateFloat(sampler.index)})).r`;
Expand All @@ -404,7 +403,7 @@ in vec2 v_TexCoord2;
in vec3 v_Normal;
in vec4 v_QuatNormal;
in float v_Depth;
in vec4 v_View;
in vec3 v_View;
vec3 QuatRotate(vec4 q, vec3 v) {
return v + 2.0 * cross(q.xyz, cross(q.xyz, v) + q.w * v);
Expand Down Expand Up @@ -474,11 +473,12 @@ out vec2 v_TexCoord2;
out vec3 v_Normal;
out vec4 v_QuatNormal;
out float v_Depth;
out vec4 v_View;
out vec3 v_View;
vec4 CalcQuatFromNormal(vec3 normal){
vec4 CalcQuatFromNormal(vec3 normal) {
float QuatZ = 0.5 * (normal.z + 1.0);
if (QuatZ <= 0.0) return vec4(1.0, 0.0, 0.0, 0.0);
if (QuatZ <= 0.0)
return vec4(1.0, 0.0, 0.0, 0.0);
QuatZ = 1.0 / sqrt(QuatZ);
return vec4((0.5 * normal.xy) * QuatZ, (1.0 / QuatZ), 0.0);
}
Expand Down Expand Up @@ -517,7 +517,6 @@ vec4 FullQuatCalcFallback(in vec4 t_temp0, in vec4 t_Normal, in vec4 t_temp1) {
}
vec4 CalcQuatFromTangent(in vec3 t_Tangent) {
vec4 t_temp0 = vec4(normalize(cross(v_Normal, t_Tangent)), 0.0);
vec4 t_temp1 = vec4(cross(t_temp0.xyz, v_Normal.xyz), t_temp0.z);
vec4 t_Normal = vec4(v_Normal.xyz, t_temp0.x);
Expand Down Expand Up @@ -569,7 +568,7 @@ vec3 CalcTextureCoordRaw(in int t_Idx) {
} else if (t_MappingMode == 1) {
// UV mapping.
vec2 t_TexSrc = CalcTextureSrc(t_Params.y);
return Mul(u_TexMtx[t_Idx], vec4(t_TexSrc, 0.0, 1.0));
return u_TexMtx[t_Idx] * vec4(t_TexSrc, 0.0, 1.0);
} else if (t_MappingMode == 2) {
// Cube env mapping.
//vec3 t_Incident = normalize(vec3(t_Position.xy, -t_Position.z) - vec3(u_CameraPos.xy, -u_CameraPos.z));
Expand All @@ -580,7 +579,7 @@ vec3 CalcTextureCoordRaw(in int t_Idx) {
// Sphere env mapping.
// Convert view-space normal to proper place.
vec2 t_TexSrc = (v_Normal.xy * 0.5) + 0.5;
return Mul(u_TexMtx[t_Idx], vec4(t_TexSrc, 0.0, 1.0));
return u_TexMtx[t_Idx] * vec4(t_TexSrc, 0.0, 1.0);
} else if (t_MappingMode == 4) {
// Projection mapping.
// Not implemented yet.
Expand All @@ -599,7 +598,7 @@ vec3 CalcTextureCoord(in int t_Idx) {
void main() {
// Compute our matrix.
Mat4x3 t_BoneMatrix;
mat4x3 t_BoneMatrix;
vec4 t_BoneWeights = a_BoneWeights;
Expand All @@ -613,28 +612,26 @@ void main() {
if (u_BoneDimension < 1.0)
t_BoneWeights.x = 0.0;
if ((t_BoneWeights.x + t_BoneWeights.y + t_BoneWeights.z + t_BoneWeights.w) > 0.0) {
t_BoneMatrix = _Mat4x3(0.0);
Fma(t_BoneMatrix, u_BoneMatrix[int(a_BoneIndices.x)], t_BoneWeights.x);
Fma(t_BoneMatrix, u_BoneMatrix[int(a_BoneIndices.y)], t_BoneWeights.y);
Fma(t_BoneMatrix, u_BoneMatrix[int(a_BoneIndices.z)], t_BoneWeights.z);
Fma(t_BoneMatrix, u_BoneMatrix[int(a_BoneIndices.w)], t_BoneWeights.w);
if (any(greaterThan(t_BoneWeights.xyzw, vec4(0.0)))) {
t_BoneMatrix = mat4x3(0.0);
t_BoneMatrix += u_BoneMatrix[int(a_BoneIndices.x)] * t_BoneWeights.x;
t_BoneMatrix += u_BoneMatrix[int(a_BoneIndices.y)] * t_BoneWeights.y;
t_BoneMatrix += u_BoneMatrix[int(a_BoneIndices.z)] * t_BoneWeights.z;
t_BoneMatrix += u_BoneMatrix[int(a_BoneIndices.w)] * t_BoneWeights.w;
} else {
// If we have no bone weights, then we're in rigid skinning, so take the first bone index.
// If we're single-bone, then our bone indices will be 0, so this also works for that.
t_BoneMatrix = u_BoneMatrix[int(a_BoneIndices.x)];
}
vec4 t_LocalPosition = vec4(a_Position, 1.0);
vec4 t_ModelPosition = Mul(_Mat4x4(t_BoneMatrix), t_LocalPosition);
vec4 t_ViewPosition = Mul(_Mat4x4(u_ViewMatrix), t_ModelPosition);
gl_Position = Mul(u_Projection, t_ViewPosition);
vec3 t_ModelPosition = t_BoneMatrix * vec4(a_Position, 1.0);
vec3 t_ViewPosition = u_ViewMatrix * vec4(t_ModelPosition, 1.0);
gl_Position = u_Projection * vec4(t_ViewPosition, 1.0);
vec3 t_ModelNormal = MulNormalMatrix(t_BoneMatrix, a_Normal);
vec3 t_ModelTangent = Mul(_Mat4x4(t_BoneMatrix), vec4(a_Tangent, 0.0)).xyz;
vec3 t_ViewTangent = normalize(Mul(_Mat4x4(u_ViewMatrix), vec4(t_ModelTangent, 0.0)).xyz);
v_Normal = normalize(Mul(_Mat4x4(u_ViewMatrix), vec4(t_ModelNormal, 0.0)).xyz);
vec3 t_ModelTangent = (t_BoneMatrix * vec4(a_Tangent, 0.0)).xyz;
vec3 t_ViewTangent = normalize((u_ViewMatrix * vec4(t_ModelTangent, 0.0)).xyz);
v_Normal = normalize((u_ViewMatrix * vec4(t_ModelNormal, 0.0)).xyz);
v_QuatNormal = vec4(1.0, 0.0, 0.0, 0.0);
v_Depth = gl_Position.w;
Expand Down
8 changes: 4 additions & 4 deletions src/gfx/helpers/DebugDraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ const bindingLayouts: GfxBindingLayoutDescriptor[] = [
];

const debugDrawVS = `
layout(std140) uniform ub_BaseData {
Mat4x4 u_ClipFromView;
Mat4x3 u_ViewFromWorld;
layout(std140, row_major) uniform ub_BaseData {
mat4 u_ClipFromView;
mat4x3 u_ViewFromWorld;
vec4 u_Misc[1];
};
Expand All @@ -60,7 +60,7 @@ flat out uint v_Flags;
void main() {
uint t_Flags = uint(a_Color.a);
gl_Position = Mul(u_ClipFromView, Mul(_Mat4x4(u_ViewFromWorld), vec4(a_Position.xyz, 1.0)));
gl_Position = u_ClipFromView * vec4(u_ViewFromWorld * vec4(a_Position.xyz, 1.0), 1.0);
if (gl_InstanceID >= 1) {
uint t_LineIndex = uint(gl_InstanceID - 1);
Expand Down
5 changes: 5 additions & 0 deletions src/gfx/helpers/GfxShaderLibrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ vec3 MulNormalMatrix(Mat4x3 t_Matrix, vec3 t_Value) {
vec3 t_SqScale = vec3(dot(t_Col0, t_Col0), dot(t_Col1, t_Col1), dot(t_Col2, t_Col2));
return normalize(Mul(t_Matrix, vec4(t_Value / t_SqScale, 0.0)));
}
vec3 MulNormalMatrix(mat4x3 t_Matrix, vec3 t_Value) {
vec3 t_SqScale = vec3(dot(t_Matrix[0], t_Matrix[0]), dot(t_Matrix[1], t_Matrix[1]), dot(t_Matrix[2], t_Matrix[2]));
return normalize(t_Matrix * vec4(t_Value / t_SqScale, 0.0));
}
`;

export const CalcScaleBias: string = `
Expand Down
47 changes: 23 additions & 24 deletions src/gx/gx_material.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,8 @@ export function materialHasDynamicAlphaTest(material: { hasDynamicAlphaTest?: bo
function generateBindingsDefinition(material: { hasPostTexMtxBlock?: boolean, hasLightsBlock?: boolean, hasFogBlock?: boolean, usePnMtxIdx?: boolean, hasDynamicAlphaTest?: boolean }): string {
return `
// Expected to be constant across the entire scene.
layout(std140) uniform ub_SceneParams {
Mat4x4 u_Projection;
layout(std140, row_major) uniform ub_SceneParams {
mat4 u_Projection;
vec4 u_Misc0;
};
Expand All @@ -358,19 +358,19 @@ struct FogBlock {
};
// Expected to change with each material.
layout(std140) uniform ub_MaterialParams {
layout(std140, row_major) uniform ub_MaterialParams {
vec4 u_ColorMatReg[2];
vec4 u_ColorAmbReg[2];
vec4 u_KonstColor[4];
vec4 u_Color[4];
Mat4x3 u_TexMtx[10];
mat4x3 u_TexMtx[10];
vec4 u_TextureSizes[4];
vec4 u_TextureBiases[2];
Mat4x2 u_IndTexMtx[3];
mat4x2 u_IndTexMtx[3];
// Optional parameters.
${materialHasPostTexMtxBlock(material) ? `
Mat4x3 u_PostTexMtx[20];
mat4x3 u_PostTexMtx[20];
` : ``}
${materialHasLightsBlock(material) ? `
Light u_LightParams[8];
Expand All @@ -384,11 +384,11 @@ ${materialHasDynamicAlphaTest(material) ? `
};
// Expected to change with each shape draw.
layout(std140) uniform ub_DrawParams {
layout(std140, row_major) uniform ub_DrawParams {
${materialUsePnMtxIdx(material) ? `
Mat4x3 u_PosMtx[10];
mat4x3 u_PosMtx[10];
` : `
Mat4x3 u_PosMtx[1];
mat4x3 u_PosMtx[1];
`}
};
Expand Down Expand Up @@ -573,23 +573,23 @@ ${this.generateLightAttnFn(chan, lightName)}
}

// Output is a vec3, src is a vec4.
private generateMulPntMatrixStatic(pnt: GX.TexGenMatrix, src: string, funcName: string = `Mul`): string {
private generateMulPntMatrixStatic(pnt: GX.TexGenMatrix, src: string, nrm: boolean = false): string {
if (pnt === GX.TexGenMatrix.IDENTITY) {
return `${src}.xyz`;
} else if (pnt >= GX.TexGenMatrix.TEXMTX0) {
const texMtxIdx = (pnt - GX.TexGenMatrix.TEXMTX0) / 3;
return `${funcName}(u_TexMtx[${texMtxIdx}], ${src})`;
return nrm ? `MulNormalMatrix(u_TexMtx[${texMtxIdx}], ${src})` : `(u_TexMtx[${texMtxIdx}] * ${src})`;
} else if (pnt >= GX.TexGenMatrix.PNMTX0) {
const pnMtxIdx = (pnt - GX.TexGenMatrix.PNMTX0) / 3;
return `${funcName}(u_PosMtx[${pnMtxIdx}], ${src})`;
return nrm ? `MulNormalMatrix(u_PosMtx[${pnMtxIdx}], ${src})` : `(u_PosMtx[${pnMtxIdx}] * ${src})`;
} else {
throw "whoops";
}
}

// Output is a vec3, src is a vec4.
private generateMulPntMatrixDynamic(attrStr: string, src: string, funcName: string = `Mul`): string {
return `${funcName}(GetPosTexMatrix(${attrStr}), ${src})`;
private generateMulPntMatrixDynamic(attrStr: string, src: string, nrm: boolean = false): string {
return nrm ? `MulNormalMatrix(GetPosTexMatrix(${attrStr}), ${src})` : `(GetPosTexMatrix(${attrStr}) * ${src})`;
}

private generateTexMtxIdxAttr(index: GX.TexCoordID): string {
Expand Down Expand Up @@ -642,7 +642,7 @@ ${this.generateLightAttnFn(chan, lightName)}
return `${src}.xyz`;
} else if (texCoordGen.postMatrix >= GX.PostTexGenMatrix.PTTEXMTX0) {
const texMtxIdx = (texCoordGen.postMatrix - GX.PostTexGenMatrix.PTTEXMTX0) / 3;
return `Mul(u_PostTexMtx[${texMtxIdx}], ${src})`;
return `(u_PostTexMtx[${texMtxIdx}] * ${src})`;
} else {
throw "whoops";
}
Expand Down Expand Up @@ -1115,15 +1115,15 @@ ${this.generateLightAttnFn(chan, lightName)}
case GX.IndTexMtxID._0:
case GX.IndTexMtxID._1:
case GX.IndTexMtxID._2:
return `Mul(u_IndTexMtx[${indTexMtxIdx}], vec4(${indTexCoord}, 0.0))`;
return `u_IndTexMtx[${indTexMtxIdx}] * vec4(${indTexCoord}, 0.0)`;
case GX.IndTexMtxID.S0:
case GX.IndTexMtxID.S1:
case GX.IndTexMtxID.S2:
return `(u_IndTexMtx[${indTexMtxIdx}].mx.w * ReadTexCoord${stage.texCoordId}() * ${indTexCoord}.xx)`;
return `(u_IndTexMtx[${indTexMtxIdx}][3][0] * ReadTexCoord${stage.texCoordId}() * ${indTexCoord}.xx)`;
case GX.IndTexMtxID.T0:
case GX.IndTexMtxID.T1:
case GX.IndTexMtxID.T2:
return `(u_IndTexMtx[${indTexMtxIdx}].mx.w * ReadTexCoord${stage.texCoordId}() * ${indTexCoord}.yy)`;
return `(u_IndTexMtx[${indTexMtxIdx}][3][0] * ReadTexCoord${stage.texCoordId}() * ${indTexCoord}.yy)`;
default:
throw "whoops";
}
Expand Down Expand Up @@ -1385,11 +1385,10 @@ ${this.generateFogAdj(`t_FogBase`)}
}

private generateMul(attr: string, pos: boolean, nrm: boolean): string {
const mul = nrm ? `MulNormalMatrix` : `Mul`;
const src = nrm ? attr : `vec4(${attr}.xyz, ${pos ? `1.0` : `0.0`})`;
const gen = materialUsePnMtxIdx(this.material) ?
this.generateMulPntMatrixDynamic(`a_Position.w`, src, mul) :
this.generateMulPntMatrixStatic(GX.TexGenMatrix.PNMTX0, src, mul);
this.generateMulPntMatrixDynamic(`a_Position.w`, src, nrm) :
this.generateMulPntMatrixStatic(GX.TexGenMatrix.PNMTX0, src, nrm);
return pos ? gen : `normalize(${gen}.xyz)`;
}

Expand All @@ -1413,10 +1412,10 @@ ${this.generateTexCoordVaryings()}
${both}
${this.generateVertAttributeDefs()}
Mat4x3 GetPosTexMatrix(float t_MtxIdxFloat) {
mat4x3 GetPosTexMatrix(float t_MtxIdxFloat) {
uint t_MtxIdx = uint(t_MtxIdxFloat);
if (t_MtxIdx == 20u)
return _Mat4x3(1.0);
return mat4x3(1.0);
else if (t_MtxIdx >= 10u)
return u_TexMtx[(t_MtxIdx - 10u)];
else
Expand All @@ -1440,7 +1439,7 @@ void main() {
vec4 t_ColorChanTemp;
${this.generateLightChannels()}
${this.generateTexGens()}
gl_Position = Mul(u_Projection, vec4(t_Position, 1.0));
gl_Position = u_Projection * vec4(t_Position, 1.0);
}
`;

Expand Down

0 comments on commit 47b254c

Please sign in to comment.