Skip to content

Commit

Permalink
Port remaining games to new matrix library
Browse files Browse the repository at this point in the history
  • Loading branch information
magcius committed Feb 2, 2025
1 parent e20e74e commit 8d80483
Show file tree
Hide file tree
Showing 19 changed files with 572 additions and 565 deletions.
9 changes: 1 addition & 8 deletions rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,7 @@ export default defineConfig({
// Enable async TypeScript type checking.
plugins: [pluginTypeCheck()],
tools: {
// Add a rule to treat .glsl files as source assets.
rspack(_config, { addRules }) {
addRules([
{
test: /\.glsl$/,
type: 'asset/source',
},
]);
rspack(_config) {
},
// Disable standards-compliant class field transforms.
swc: {
Expand Down
107 changes: 0 additions & 107 deletions src/GrandTheftAuto3/program.glsl

This file was deleted.

116 changes: 112 additions & 4 deletions src/GrandTheftAuto3/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import * as UI from "../ui.js";
import * as Viewer from "../viewer.js";
import * as rw from "librw";
// @ts-ignore
import program_glsl from './program.glsl';
import { TextureMapping, TextureBase } from "../TextureHolder.js";
import { GfxDevice, GfxFormat, GfxBufferUsage, GfxBuffer, GfxVertexAttributeDescriptor, GfxVertexBufferFrequency, GfxInputLayout, GfxProgram, GfxTexFilterMode, GfxMipFilterMode, GfxWrapMode, GfxTextureDimension, GfxRenderPass, GfxMegaStateDescriptor, GfxBlendMode, GfxBlendFactor, GfxBindingLayoutDescriptor, GfxCullMode, GfxVertexBufferDescriptor, GfxIndexBufferDescriptor, GfxInputLayoutBufferDescriptor, GfxInputLayoutDescriptor, GfxTextureUsage, GfxSamplerFormatKind } from "../gfx/platform/GfxPlatform.js";
import { makeStaticDataBuffer } from "../gfx/helpers/BufferHelpers.js";
Expand All @@ -24,6 +22,7 @@ import { GfxRenderCache } from "../gfx/render/GfxRenderCache.js";
import { setAttachmentStateSimple } from "../gfx/helpers/GfxMegaStateDescriptorHelpers.js";
import { GraphObjBase } from "../SceneBase.js";
import { GfxrAttachmentSlot } from "../gfx/render/GfxRenderGraph.js";
import { GfxShaderLibrary } from "../gfx/helpers/GfxShaderLibrary.js";

const TIME_FACTOR = 2500; // one day cycle per minute

Expand Down Expand Up @@ -185,8 +184,117 @@ class GTA3Program extends DeviceProgram {

public static ub_SceneParams = 0;

private static program = program_glsl;
public override both = GTA3Program.program;
public override both = `
precision mediump float;
precision lowp sampler2DArray;
${GfxShaderLibrary.MatrixLibrary}
layout(std140) uniform ub_SceneParams {
Mat4x4 u_Projection;
Mat3x4 u_ViewMatrix;
Mat3x4 u_WorldMatrix;
vec4 u_Frustum;
vec4 u_AmbientColor;
vec4 u_SkyTopColor;
vec4 u_SkyBotColor;
vec4 u_WaterColor;
vec4 u_WaterOrigin;
float u_Time;
};
uniform sampler2DArray u_Texture;
#ifdef SKY
varying vec3 v_Position;
#else
varying vec4 v_Color;
varying vec3 v_TexCoord;
varying vec3 v_TexScroll;
#endif
#ifdef VERT
layout(location = 0) in vec3 a_Position;
#ifdef SKY
void main() {
gl_Position = vec4(a_Position, 1.0);
v_Position = a_Position;
}
#else
layout(location = 1) in vec4 a_Color;
layout(location = 2) in vec3 a_TexCoord;
layout(location = 3) in vec3 a_TexScroll;
void main() {
vec3 t_PositionView = UnpackMatrix(u_ViewMatrix) * vec4(a_Position, 1.0);
gl_Position = UnpackMatrix(u_Projection) * vec4(t_PositionView, 1.0);
v_Color = a_Color;
v_TexCoord = a_TexCoord;
v_TexScroll = a_TexScroll;
}
#endif
#endif
#ifdef FRAG
#ifdef SKY
void main() {
gl_FragColor = mix(u_SkyBotColor, u_SkyTopColor, v_Position.y);
// TODO: get this working again
vec3 nearPlane = v_Position * u_Frustum.xyz;
vec3 cameraRay = UnpackMatrix(u_WorldMatrix) * vec4(nearPlane, 0.0);
vec3 cameraPos = UnpackMatrix(u_WorldMatrix) * vec4(vec3(0.0), 1.0);
float elevation = atan(cameraRay.y, length(cameraRay.zx));
gl_FragColor = mix(u_SkyBotColor, u_SkyTopColor, clamp(abs(elevation / radians(45.0)), 0.0, 1.0));
gl_FragDepth = 0.0;
float t = (u_WaterOrigin.y - cameraPos.y) / cameraRay.y;
vec3 oceanPlane = cameraPos + t * cameraRay;
vec2 uv = (oceanPlane.zx - u_WaterOrigin.zx) / 32.0;
vec4 oceanSample = texture(SAMPLER_2DArray(u_Texture), vec3(uv, 0));
if (t > 0.0 && (abs(oceanPlane.z - u_WaterOrigin.z) >= u_WaterOrigin.w - 32.0 ||
abs(oceanPlane.x - u_WaterOrigin.x) >= u_WaterOrigin.w - 32.0)) {
vec4 t_Color = u_WaterColor;
t_Color *= oceanSample;
gl_FragColor = mix(gl_FragColor, t_Color, t_Color.a);
// slightly overlap water tiles to avoid seam
vec3 clipOffset = 0.01 * vec3(0, 0, 1);
vec3 viewSpacePos = (UnpackMatrix(u_ViewMatrix) * vec4(oceanPlane, 1.0)) + clipOffset;
vec4 clipSpacePos = UnpackMatrix(u_Projection) * vec4(viewSpacePos, 1.0);
float depthNDC = clipSpacePos.z / clipSpacePos.w;
gl_FragDepth = 0.5 + 0.5 * depthNDC;
}
}
#else
void main() {
#ifdef WATER
vec4 t_Color = u_WaterColor;
#else
vec4 t_Color = v_Color;
t_Color.rgb += u_AmbientColor.rgb;
#endif
vec3 uv = v_TexCoord;
if (v_TexScroll.z > 0.0)
uv.xy += v_TexScroll.xy * fract(u_Time / v_TexScroll.z);
// Work around naga bug https://github.com/gfx-rs/wgpu/issues/6596
uv.z = round(uv.z);
vec4 tex = texture(SAMPLER_2DArray(u_Texture), uv);
if (v_TexCoord.z >= 0.0)
t_Color *= tex;
#ifdef ALPHA_TEST
if (t_Color.a ALPHA_TEST) discard;
#endif
gl_FragColor = t_Color;
}
#endif
#endif
`;

constructor(def: GTA3ProgramDef = {}) {
super();
Expand Down
18 changes: 9 additions & 9 deletions src/HeavyIron/rw/pipelines/AtomicAllInOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ struct DirectionalLight {
vec4 color;
};
layout(std140, row_major) uniform ub_AtomicParams {
mat4 u_Projection;
mat4x3 u_ViewMatrix;
mat4x3 u_ModelMatrix;
layout(std140) uniform ub_AtomicParams {
Mat4x4 u_Projection;
Mat3x4 u_ViewMatrix;
Mat3x4 u_ModelMatrix;
DirectionalLight u_DirectionalLights[MAX_DIRECTIONAL_LIGHTS];
vec4 u_AmbientColor;
vec4 u_FogColor;
Expand All @@ -63,7 +63,7 @@ layout(std140, row_major) uniform ub_AtomicParams {
#define u_EnablePrelit ((u_AtomicFlags & 0x1) != 0)
#define u_EnableLight ((u_AtomicFlags & 0x2) != 0)
layout(std140, row_major) uniform ub_MeshParams {
layout(std140) uniform ub_MeshParams {
vec4 u_MaterialColor;
vec4 u_MeshMisc;
};
Expand Down Expand Up @@ -92,11 +92,11 @@ ${GfxShaderLibrary.saturate}
${GfxShaderLibrary.MulNormalMatrix}
void main() {
vec3 t_PositionWorld = u_ModelMatrix * vec4(a_Position, 1.0);
vec3 t_PositionView = u_ViewMatrix * vec4(t_PositionWorld, 1.0);
gl_Position = u_Projection * vec4(t_PositionView, 1.0);
vec3 t_PositionWorld = UnpackMatrix(u_ModelMatrix) * vec4(a_Position, 1.0);
vec3 t_PositionView = UnpackMatrix(u_ViewMatrix) * vec4(t_PositionWorld, 1.0);
gl_Position = UnpackMatrix(u_Projection) * vec4(t_PositionView, 1.0);
vec3 t_Normal = MulNormalMatrix(u_ModelMatrix, a_Normal);
vec3 t_Normal = MulNormalMatrix(UnpackMatrix(u_ModelMatrix), a_Normal);
vec4 t_Color = u_EnablePrelit ? a_Color : (u_EnableLight ? vec4(0, 0, 0, 1) : vec4(1.0));
Expand Down
10 changes: 5 additions & 5 deletions src/InteractiveExamples/FoxFur.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ class FurProgram extends DeviceProgram {
public static ub_ShapeParams = 0;

public override both = `
layout(std140, row_major) uniform ub_ShapeParams {
mat4 u_Projection;
mat4x3 u_BoneMatrix[1];
layout(std140) uniform ub_ShapeParams {
Mat4x4 u_Projection;
Mat3x4 u_BoneMatrix[1];
vec4 u_Misc[3];
vec4 u_TintColor;
};
Expand Down Expand Up @@ -159,8 +159,8 @@ out vec2 v_TexCoord;
void main() {
vec3 t_PositionLocal = a_Position.xyz + (a_Normal.xyz * u_LayerMagnitude);
vec3 t_PositionView = u_BoneMatrix[0] * vec4(t_PositionLocal, 1.0);
gl_Position = u_Projection * vec4(t_PositionView, 1.0);
vec3 t_PositionView = UnpackMatrix(u_BoneMatrix[0]) * vec4(t_PositionLocal, 1.0);
gl_Position = UnpackMatrix(u_Projection) * vec4(t_PositionView, 1.0);
v_TexCoord = a_TexCoord.xy;
}
`;
Expand Down
24 changes: 13 additions & 11 deletions src/InteractiveExamples/Tess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,20 @@ class PatchProgram extends DeviceProgram {

public override both = `
${GfxShaderLibrary.saturate}
${GfxShaderLibrary.MatrixLibrary}
layout(std140, row_major) uniform ub_SceneParams {
mat4 u_ProjectionView;
layout(std140) uniform ub_SceneParams {
Mat4x4 u_ProjectionView;
vec4 u_CameraPosWorld;
};
struct WaveParam {
vec4 Param[1];
};
layout(std140, row_major) uniform ub_ObjectParams {
mat4x3 u_PatchToMeshMatrix;
mat4x3 u_MeshToWorldMatrix;
layout(std140) uniform ub_ObjectParams {
Mat3x4 u_PatchToMeshMatrix;
Mat3x4 u_MeshToWorldMatrix;
WaveParam u_Wave[2];
vec4 u_ColorAdd;
};
Expand Down Expand Up @@ -89,8 +90,8 @@ void main() {
vec3 t_PatchPos = vec3(t_PatchLoc.x, 0.0, t_PatchLoc.y);
vec3 t_PatchNrm = vec3(0.0, 1.0, 0.0);
vec3 t_MeshPos = u_PatchToMeshMatrix * vec4(t_PatchPos, 1.0);
vec3 t_MeshNrm = u_PatchToMeshMatrix * vec4(t_PatchNrm, 0.0);
vec3 t_MeshPos = UnpackMatrix(u_PatchToMeshMatrix) * vec4(t_PatchPos, 1.0);
vec3 t_MeshNrm = UnpackMatrix(u_PatchToMeshMatrix) * vec4(t_PatchNrm, 0.0);
vec3 t_MeshTng = vec3(1.0, 0.0, 0.0);
vec2 t_TexCoordMesh;
t_TexCoordMesh.xy = a_TexCoord.xy;
Expand All @@ -104,16 +105,17 @@ void main() {
t_TexCoordMesh.y = t_MeshNrm.y * 0.5 + 0.5;
#endif
v_PositionWorld = u_MeshToWorldMatrix * vec4(t_MeshPos, 1.0);
v_NormalWorld = MulNormalMatrix(u_MeshToWorldMatrix, t_MeshNrm);
v_TangentWorld = normalize(u_MeshToWorldMatrix * vec4(t_MeshTng, 0.0));
mat4x3 t_MeshToWorldMatrix = UnpackMatrix(u_MeshToWorldMatrix);
v_PositionWorld = t_MeshToWorldMatrix * vec4(t_MeshPos, 1.0);
v_NormalWorld = MulNormalMatrix(t_MeshToWorldMatrix, t_MeshNrm);
v_TangentWorld = normalize(t_MeshToWorldMatrix * vec4(t_MeshTng, 0.0));
#ifdef MODE_SPHERE
v_PositionWorld += v_NormalWorld * CalcWaveHeight(0u, t_TexCoordMesh.xy);
v_PositionWorld += v_NormalWorld * CalcWaveHeight(1u, t_TexCoordMesh.xy);
#endif
gl_Position = u_ProjectionView * vec4(v_PositionWorld, 1.0);
gl_Position = UnpackMatrix(u_ProjectionView) * vec4(v_PositionWorld, 1.0);
v_NormalMesh.xyz = t_MeshNrm.xyz;
v_TexCoordLocal.xy = a_TexCoord.xy;
Expand Down
Loading

0 comments on commit 8d80483

Please sign in to comment.