Skip to content

Commit

Permalink
Legacy matrix removal for a bunch more games
Browse files Browse the repository at this point in the history
Halo 1, Katamari Damacy, SM64DS, Fez, Dark Souls, Dark Souls Collision Data
  • Loading branch information
magcius committed Jan 3, 2025
1 parent 3b074bb commit edf8cfd
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 96 deletions.
52 changes: 26 additions & 26 deletions src/DarkSouls/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,8 @@ class MaterialProgram_Base extends DeviceProgram {

public static BindingDefinitions = `
// Expected to be constant across the entire scene.
layout(std140) uniform ub_SceneParams {
Mat4x4 u_ProjectionView;
layout(std140, row_major) uniform ub_SceneParams {
mat4x4 u_ProjectionView;
vec4 u_CameraPosWorld; // DebugMode is in w
};
Expand Down Expand Up @@ -570,8 +570,8 @@ precision mediump float;
${MaterialProgram_Base.BindingDefinitions}
layout(std140) uniform ub_MeshFragParams {
Mat4x3 u_WorldFromLocal[1];
layout(std140, row_major) uniform ub_MeshFragParams {
mat4x3 u_WorldFromLocal[1];
vec4 u_DiffuseMapColor;
vec4 u_SpecularMapColor;
vec4 u_EnvDifColor;
Expand Down Expand Up @@ -651,18 +651,18 @@ layout(location = ${MaterialProgram_Base.a_Tangent1}) in vec4 a_Tangent1;
${GfxShaderLibrary.MulNormalMatrix}
void main() {
vec4 t_PositionWorld = Mul(_Mat4x4(u_WorldFromLocal[0]), vec4(a_Position, 1.0));
vec3 t_PositionWorld = u_WorldFromLocal[0] * vec4(a_Position, 1.0);
v_PositionWorld = t_PositionWorld.xyz;
gl_Position = Mul(u_ProjectionView, t_PositionWorld);
gl_Position = u_ProjectionView * vec4(t_PositionWorld, 1.0);
vec3 t_NormalWorld = MulNormalMatrix(u_WorldFromLocal[0], UNORM_TO_SNORM(a_Normal.xyz));
v_TangentSpaceBasisZ = t_NormalWorld;
vec3 t_TangentWorld0 = normalize(Mul(_Mat4x4(u_WorldFromLocal[0]), vec4(UNORM_TO_SNORM(a_Tangent0.xyz), 0.0)).xyz);
vec3 t_TangentWorld0 = normalize(u_WorldFromLocal[0] * vec4(UNORM_TO_SNORM(a_Tangent0.xyz), 0.0));
v_TangentSpaceBasisY0 = vec4(t_TangentWorld0, UNORM_TO_SNORM(a_Tangent0.w));
#ifdef HAS_TANGENT1
vec3 t_TangentWorld1 = normalize(Mul(_Mat4x4(u_WorldFromLocal[0]), vec4(UNORM_TO_SNORM(a_Tangent1.xyz), 0.0)).xyz);
vec3 t_TangentWorld1 = normalize(u_WorldFromLocal[0] * vec4(UNORM_TO_SNORM(a_Tangent1.xyz), 0.0));
v_TangentSpaceBasisY1 = vec4(t_TangentWorld1, UNORM_TO_SNORM(a_Tangent1.w));
#endif
Expand Down Expand Up @@ -1010,8 +1010,8 @@ ${MaterialProgram_Base.BindingDefinitions}
layout(binding = 8) uniform samplerCube u_TextureDummy;
layout(std140) uniform ub_MeshFragParams {
Mat4x3 u_WorldFromLocal[1];
layout(std140, row_major) uniform ub_MeshFragParams {
mat4x3 u_WorldFromLocal[1];
DirectionalLight u_DirectionalLight;
FogParams u_FogParams;
vec4 u_Misc[7];
Expand All @@ -1037,9 +1037,9 @@ layout(location = ${MaterialProgram_Base.a_Position}) in vec3 a_Position;
layout(location = ${MaterialProgram_Base.a_TexCoord0}) in vec4 a_TexCoord0;
void main() {
vec4 t_PositionWorld = Mul(_Mat4x4(u_WorldFromLocal[0]), vec4(a_Position, 1.0));
vec3 t_PositionWorld = u_WorldFromLocal[0] * vec4(a_Position, 1.0);
v_PositionWorld = t_PositionWorld.xyz;
gl_Position = Mul(u_ProjectionView, t_PositionWorld);
gl_Position = u_ProjectionView * vec4(t_PositionWorld, 1.0);
v_TexCoord0.xy = DecodeTexCoord(a_TexCoord0.xy) * u_TileScale.xx + u_TexScroll0.xy;
v_TexCoord1.xy = DecodeTexCoord(a_TexCoord0.xy) * u_TileScale.yy + u_TexScroll1.xy;
Expand Down Expand Up @@ -1076,8 +1076,8 @@ ${MaterialProgram_Base.BindingDefinitions}
layout(binding = 8) uniform samplerCube u_TextureDummy;
layout(std140) uniform ub_MeshFragParams {
Mat4x3 u_WorldFromLocal[1];
layout(std140, row_major) uniform ub_MeshFragParams {
mat4x3 u_WorldFromLocal[1];
DirectionalLight u_DirectionalLight;
FogParams u_FogParams;
vec4 u_Misc[7];
Expand Down Expand Up @@ -1129,20 +1129,20 @@ layout(location = ${MaterialProgram_Base.a_Tangent1}) in vec4 a_Tangent1;
${GfxShaderLibrary.MulNormalMatrix}
void main() {
vec4 t_PositionWorld = Mul(_Mat4x4(u_WorldFromLocal[0]), vec4(a_Position, 1.0));
vec3 t_PositionWorld = u_WorldFromLocal[0] * vec4(a_Position, 1.0);
v_PositionWorld = t_PositionWorld.xyz;
gl_Position = Mul(u_ProjectionView, t_PositionWorld);
gl_Position = u_ProjectionView * vec4(t_PositionWorld, 1.0);
v_Color = a_Color;
v_TexCoord0 = a_TexCoord0.xy;
v_TexCoordProj.xyz = gl_Position.xyw;
v_TangentSpaceBasisZ = MulNormalMatrix(u_WorldFromLocal[0], UNORM_TO_SNORM(a_Normal.xyz));
v_TangentSpaceBasisY = normalize(Mul(_Mat4x4(u_WorldFromLocal[0]), vec4(UNORM_TO_SNORM(a_Tangent0.xyz), 0.0)).xyz);
v_TangentSpaceBasisY = normalize(u_WorldFromLocal[0] * vec4(UNORM_TO_SNORM(a_Tangent0.xyz), 0.0));
v_TangentSpaceBasisX = normalize(cross(v_TangentSpaceBasisZ, v_TangentSpaceBasisY) * UNORM_TO_SNORM(a_Tangent0.w));
v_TexCoordProjX = Mul(u_ProjectionView, t_PositionWorld + vec4(v_TangentSpaceBasisX, 0.0)).xyw;
v_TexCoordProjY = Mul(u_ProjectionView, t_PositionWorld + vec4(v_TangentSpaceBasisY, 0.0)).xyw;
v_TexCoordProjX = (u_ProjectionView * vec4(t_PositionWorld + v_TangentSpaceBasisX, 0.0)).xyw;
v_TexCoordProjY = (u_ProjectionView * vec4(t_PositionWorld + v_TangentSpaceBasisY, 0.0)).xyw;
}
`;

Expand Down Expand Up @@ -1996,7 +1996,7 @@ class DepthOfFieldBlurProgram extends DeviceProgram {
uniform sampler2D u_TextureColor;
uniform sampler2D u_Texture2;
layout(std140) uniform ub_Params {
layout(std140, row_major) uniform ub_Params {
vec4 u_Misc[1];
};
#define u_DispersionSq (u_Misc[0].x)
Expand Down Expand Up @@ -2063,7 +2063,7 @@ class DepthOfFieldCombineProgram extends DeviceProgram {
uniform sampler2D u_TextureColor;
uniform sampler2D u_TextureFramebufferDepth;
layout(std140) uniform ub_Params {
layout(std140, row_major) uniform ub_Params {
vec4 u_Misc[3];
};
#define u_NearParam (u_Misc[1].xyz)
Expand Down Expand Up @@ -2269,7 +2269,7 @@ class BloomFilterProgram extends DeviceProgram {
uniform sampler2D u_TextureColor;
uniform sampler2D u_TextureFramebufferDepth;
layout(std140) uniform ub_Params {
layout(std140, row_major) uniform ub_Params {
vec4 u_Misc[3];
};
#define u_NearParam (u_Misc[0].xyz)
Expand Down Expand Up @@ -2456,7 +2456,7 @@ class BloomCombineProgram extends DeviceProgram {
uniform sampler2D u_TextureColor;
uniform sampler2D u_Texture2;
layout(std140) uniform ub_Params {
layout(std140, row_major) uniform ub_Params {
vec4 u_Misc[3];
};
#define u_NearParam (u_Misc[0].xyz)
Expand Down Expand Up @@ -2723,8 +2723,8 @@ class ToneCorrectProgram extends DeviceProgram {
uniform sampler2D u_TextureColor;
uniform sampler2D u_Texture2;
layout(std140) uniform ub_Params {
Mat4x3 u_ToneCorrectMatrix;
layout(std140, row_major) uniform ub_Params {
mat4x3 u_ToneCorrectMatrix;
vec4 u_Misc[1];
};
`;
Expand All @@ -2747,7 +2747,7 @@ void main() {
t_Color.rgb *= t_Exposure;
t_Color.rgb /= (t_Color.rgb + vec3(1.0));
t_Color.rgb = Mul(_Mat4x4(u_ToneCorrectMatrix), vec4(t_Color.rgb, 1.0)).rgb;
t_Color.rgb = u_ToneCorrectMatrix * vec4(t_Color.rgb, 1.0);
gl_FragColor = t_Color;
}
`;
Expand Down
15 changes: 8 additions & 7 deletions src/DarkSoulsCollisionData/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as UI from '../ui.js';

import * as IV from './iv.js';
import { GfxDevice, GfxBufferUsage, GfxBuffer, GfxFormat, GfxInputLayout, GfxProgram, GfxBindingLayoutDescriptor, GfxVertexBufferFrequency, GfxVertexAttributeDescriptor, GfxInputLayoutBufferDescriptor, GfxCullMode, GfxVertexBufferDescriptor, GfxIndexBufferDescriptor } from '../gfx/platform/GfxPlatform.js';
import { fillColor, fillMatrix4x4 } from '../gfx/helpers/UniformBufferHelpers.js';
import { fillColor, fillMatrix4x3, fillMatrix4x4 } from '../gfx/helpers/UniformBufferHelpers.js';
import { makeBackbufferDescSimple, standardFullClearRenderPassDescriptor } from '../gfx/helpers/RenderGraphHelpers.js';
import { makeStaticDataBuffer } from '../gfx/helpers/BufferHelpers.js';
import { GfxRenderHelper } from '../gfx/render/GfxRenderHelper.js';
Expand All @@ -25,12 +25,12 @@ class IVProgram extends DeviceProgram {
public override both = `
precision mediump float;
layout(std140) uniform ub_SceneParams {
Mat4x4 u_Projection;
Mat4x4 u_ModelView;
layout(std140, row_major) uniform ub_SceneParams {
mat4 u_Projection;
mat4x3 u_ModelView;
};
layout(std140) uniform ub_ObjectParams {
layout(std140, row_major) uniform ub_ObjectParams {
vec4 u_Color;
};
Expand All @@ -42,7 +42,8 @@ layout(location = ${IVProgram.a_Normal}) attribute vec3 a_Normal;
void mainVS() {
const float t_ModelScale = 20.0;
gl_Position = Mul(u_Projection, Mul(u_ModelView, vec4(a_Position * t_ModelScale, 1.0)));
vec3 t_PositionWorld = u_ModelView * vec4(a_Position * t_ModelScale, 1.0);
gl_Position = u_Projection * vec4(t_PositionWorld, 1.0);
vec3 t_LightDirection = normalize(vec3(.2, -1, .5));
float t_LightIntensityF = dot(-a_Normal, t_LightDirection);
float t_LightIntensityB = dot( a_Normal, t_LightDirection);
Expand Down Expand Up @@ -219,7 +220,7 @@ export class Scene implements Viewer.SceneGfx {
let offs = template.allocateUniformBuffer(IVProgram.ub_SceneParams, 32);
const mapped = template.mapUniformBufferF32(IVProgram.ub_SceneParams);
offs += fillMatrix4x4(mapped, offs, viewerInput.camera.projectionMatrix);
offs += fillMatrix4x4(mapped, offs, viewerInput.camera.viewMatrix);
offs += fillMatrix4x3(mapped, offs, viewerInput.camera.viewMatrix);

this.renderHelper.renderInstManager.setCurrentList(this.renderInstListMain);

Expand Down
16 changes: 10 additions & 6 deletions src/Fez/FezRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,19 @@ import { GeometryData } from './GeometryData.js';
import { Fez_Level, Fez_BackgroundPlane } from './XNB_Fez.js';
import { GfxrAttachmentSlot } from '../gfx/render/GfxRenderGraph.js';
import { GfxRenderCache } from '../gfx/render/GfxRenderCache.js';
import { GfxShaderLibrary } from '../gfx/helpers/GfxShaderLibrary.js';

class FezProgram {
public static ub_SceneParams = 0;
public static ub_ShapeParams = 1;

public both = `
layout(std140) uniform ub_SceneParams {
Mat4x4 u_Projection;
layout(std140, row_major) uniform ub_SceneParams {
mat4 u_Projection;
};
layout(std140) uniform ub_ShapeParams {
Mat4x3 u_BoneMatrix[1];
layout(std140, row_major) uniform ub_ShapeParams {
mat4x3 u_BoneMatrix[1];
vec4 u_LightDirection;
vec4 u_TexScaleBiasPre;
vec4 u_TexScaleBiasPost;
Expand All @@ -58,9 +59,12 @@ out vec3 v_Normal;
out vec2 v_TexCoord;
out vec3 v_ShadowTexCoord;
${GfxShaderLibrary.MulNormalMatrix}
void main() {
gl_Position = Mul(u_Projection, Mul(_Mat4x4(u_BoneMatrix[0]), vec4(a_Position, 1.0)));
v_Normal = normalize(Mul(_Mat4x4(u_BoneMatrix[0]), vec4(a_Normal, 0.0)).xyz);
vec3 t_PositionWorld = u_BoneMatrix[0] * vec4(a_Position, 1.0);
gl_Position = u_Projection * vec4(t_PositionWorld, 1.0);
v_Normal = MulNormalMatrix(u_BoneMatrix[0], a_Normal);
v_TexCoord = a_TexCoord.xy * u_TexScaleBiasPre.xy + u_TexScaleBiasPre.zw;
v_ShadowTexCoord = gl_Position.xyw;
}
Expand Down
49 changes: 23 additions & 26 deletions src/Halo1/scenes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,15 @@ varying vec3 v_Position;
public static common = `
precision mediump float;
layout(std140) uniform ub_SceneParams {
Mat4x4 u_Projection;
Mat4x4 u_ViewMatrix;
layout(std140, row_major) uniform ub_SceneParams {
mat4 u_ProjectionView;
vec3 u_PlayerPos;
vec4 u_FogColor;
vec4 u_FogDistances;
};
layout(std140) uniform ub_ModelParams {
Mat4x4 u_ModelMatrix;
layout(std140, row_major) uniform ub_ModelParams {
mat4x3 u_ModelMatrix;
};
layout(binding = 0) uniform sampler2D u_Texture0;
Expand Down Expand Up @@ -128,17 +127,17 @@ vec3 CalcTangentToWorld(in vec3 t_TangentNormal, in vec3 t_Basis0, in vec3 t_Bas

public override vert = `
${BaseProgram.vertexAttrs}
vec4 toWorldCoord(vec4 x) {
return Mul(u_ModelMatrix, x);
}
${GfxShaderLibrary.MulNormalMatrix}
void mainVS() {
gl_Position = Mul(u_Projection, Mul(u_ViewMatrix, toWorldCoord(vec4(a_Position, 1.0))));
vec3 t_PositionWorld = u_ModelMatrix * vec4(a_Position, 1.0);
gl_Position = u_ProjectionView * vec4(t_PositionWorld, 1.0);
v_UV = a_TexCoord;
v_Normal = normalize(toWorldCoord(vec4(a_Normal.xyz, 0.0)).xyz);
v_Binormal = normalize(toWorldCoord(vec4(a_Binormal.xyz, 0.0)).xyz);
v_Tangent = normalize(toWorldCoord(vec4(a_Tangent.xyz, 0.0)).xyz);
v_Position = toWorldCoord(vec4(a_Position.xyz, 1.0)).xyz;
v_Normal = MulNormalMatrix(u_ModelMatrix, a_Normal);
v_Binormal = normalize(u_ModelMatrix * vec4(a_Binormal.xyz, 0.0));
v_Tangent = normalize(u_ModelMatrix * vec4(a_Tangent.xyz, 0.0));
v_Position = t_PositionWorld;
}
`;

Expand Down Expand Up @@ -1228,7 +1227,7 @@ class ShaderEnvironmentProgram extends BaseProgram {
public static a_LightmapTexCoord = 6;

public static override varying = `
varying vec2 v_lightmapUV;
varying vec2 v_LightmapUV;
varying vec3 v_IncidentLight;
`;

Expand All @@ -1248,19 +1247,18 @@ layout(location = ${ShaderEnvironmentProgram.a_TexCoord}) in vec2 a_TexCoord;
layout(location = ${ShaderEnvironmentProgram.a_IncidentLight}) in vec3 a_IncidentLight;
layout(location = ${ShaderEnvironmentProgram.a_LightmapTexCoord}) in vec2 a_LightmapTexCoord;
vec4 toWorldCoord(vec4 x) {
return Mul(u_ModelMatrix, x);
}
${GfxShaderLibrary.MulNormalMatrix}
void mainVS() {
gl_Position = Mul(u_Projection, Mul(u_ViewMatrix, toWorldCoord(vec4(a_Position, 1.0))));
vec3 t_PositionWorld = u_ModelMatrix * vec4(a_Position, 1.0);
gl_Position = u_ProjectionView * vec4(t_PositionWorld, 1.0);
v_UV = a_TexCoord;
v_Normal = normalize(toWorldCoord(vec4(a_Normal.xyz, 0.0)).xyz);
v_Binormal = normalize(toWorldCoord(vec4(a_Binormal.xyz, 0.0)).xyz);
v_Tangent = normalize(toWorldCoord(vec4(a_Tangent.xyz, 0.0)).xyz);
v_Position = toWorldCoord(vec4(a_Position.xyz, 1.0)).xyz;
v_Normal = MulNormalMatrix(u_ModelMatrix, a_Normal);
v_Binormal = normalize(u_ModelMatrix * vec4(a_Binormal.xyz, 0.0));
v_Tangent = normalize(u_ModelMatrix * vec4(a_Tangent.xyz, 0.0));
v_Position = t_PositionWorld;
v_IncidentLight = a_IncidentLight;
v_lightmapUV = a_LightmapTexCoord;
v_LightmapUV = a_LightmapTexCoord;
}
`;

Expand Down Expand Up @@ -1370,7 +1368,7 @@ vec3 t_EyeWorld = normalize(u_PlayerPos - v_Position);

if (this.has_lightmap) {
fragBody.push(`
vec3 t_LightmapSample = texture(SAMPLER_2D(u_Texture1), v_lightmapUV).rgb;
vec3 t_LightmapSample = texture(SAMPLER_2D(u_Texture1), v_LightmapUV).rgb;
float t_Variance = dot(v_IncidentLight.rgb, v_IncidentLight.rgb);
float t_BumpAtten = (dot(v_IncidentLight, t_NormalWorld) * t_Variance) + (1.0 - t_Variance);
color.rgb *= t_LightmapSample * t_BumpAtten;
Expand Down Expand Up @@ -1964,8 +1962,7 @@ class HaloScene implements Viewer.SceneGfx {

let offs = template.allocateUniformBuffer(BaseProgram.ub_SceneParams, 32 + 12);
const mapped = template.mapUniformBufferF32(BaseProgram.ub_SceneParams);
offs += fillMatrix4x4(mapped, offs, this.mainView.clipFromViewMatrix);
offs += fillMatrix4x4(mapped, offs, this.mainView.viewFromWorldMatrix);
offs += fillMatrix4x4(mapped, offs, this.mainView.clipFromWorldMatrix);
offs += fillVec3v(mapped, offs, this.mainView.cameraPos);
offs += fillVec4v(mapped, offs, this.fogColor);
offs += fillVec4v(mapped, offs, this.fogDistances);
Expand Down
Loading

0 comments on commit edf8cfd

Please sign in to comment.