diff --git a/src/DarkSouls/render.ts b/src/DarkSouls/render.ts index 0b798385b..0966ff2a3 100644 --- a/src/DarkSouls/render.ts +++ b/src/DarkSouls/render.ts @@ -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 }; @@ -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; @@ -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 @@ -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]; @@ -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; @@ -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]; @@ -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; } `; @@ -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) @@ -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) @@ -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) @@ -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) @@ -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]; }; `; @@ -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; } `; diff --git a/src/DarkSoulsCollisionData/render.ts b/src/DarkSoulsCollisionData/render.ts index 73bd84246..667c52b6d 100644 --- a/src/DarkSoulsCollisionData/render.ts +++ b/src/DarkSoulsCollisionData/render.ts @@ -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'; @@ -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; }; @@ -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); @@ -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); diff --git a/src/Fez/FezRenderer.ts b/src/Fez/FezRenderer.ts index 341ef7f14..062d193d3 100644 --- a/src/Fez/FezRenderer.ts +++ b/src/Fez/FezRenderer.ts @@ -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; @@ -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; } diff --git a/src/Halo1/scenes.ts b/src/Halo1/scenes.ts index 6d4a60162..91594b18b 100644 --- a/src/Halo1/scenes.ts +++ b/src/Halo1/scenes.ts @@ -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; @@ -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; } `; @@ -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; `; @@ -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; } `; @@ -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; @@ -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); diff --git a/src/KatamariDamacy/render.ts b/src/KatamariDamacy/render.ts index 36fd22b86..cea566e84 100644 --- a/src/KatamariDamacy/render.ts +++ b/src/KatamariDamacy/render.ts @@ -16,6 +16,7 @@ import { setAttachmentStateSimple } from "../gfx/helpers/GfxMegaStateDescriptorH import { AABB } from "../Geometry.js"; import { convertToCanvas } from "../gfx/helpers/TextureConversionHelpers.js"; import ArrayBufferSlice from "../ArrayBufferSlice.js"; +import { GfxShaderLibrary } from "../gfx/helpers/GfxShaderLibrary.js"; export class KatamariDamacyProgram extends DeviceProgram { public static a_Position = 0; @@ -29,16 +30,15 @@ export class KatamariDamacyProgram extends DeviceProgram { precision mediump float; // 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_ProjectionView; vec4 u_LightDirs[2]; vec4 u_LightColors[3]; }; -layout(std140) uniform ub_ModelParams { - Mat4x3 u_BoneMatrix[SKINNING_MATRIX_COUNT]; - Mat4x3 u_NormalMatrix[SKINNING_MATRIX_COUNT]; - Mat4x2 u_TextureMatrix[1]; +layout(std140, row_major) uniform ub_ModelParams { + mat4x3 u_BoneMatrix[SKINNING_MATRIX_COUNT]; + mat4x2 u_TextureMatrix[1]; vec4 u_Color; vec4 u_Misc[1]; }; @@ -57,13 +57,16 @@ layout(location = 0) in vec4 a_Position; layout(location = 1) in vec3 a_Normal; layout(location = 2) in vec2 a_TexCoord; +${GfxShaderLibrary.MulNormalMatrix} + void main() { int t_SkinningIndex = int(a_Position.w); - gl_Position = Mul(u_Projection, Mul(_Mat4x4(u_BoneMatrix[t_SkinningIndex]), vec4(a_Position.xyz, 1.0))); - v_TexCoord = Mul(_Mat4x4(u_TextureMatrix[0]), vec4(a_TexCoord, 0.0, 1.0)).xy; + vec3 t_PositionWorld = u_BoneMatrix[t_SkinningIndex] * vec4(a_Position.xyz, 1.0); + gl_Position = u_ProjectionView * vec4(t_PositionWorld, 1.0); + v_TexCoord = u_TextureMatrix[0] * vec4(a_TexCoord, 0.0, 1.0); #ifdef LIGHTING - vec3 t_Normal = normalize(Mul(_Mat4x4(u_NormalMatrix[t_SkinningIndex]), vec4(a_Normal, 0.0)).xyz); + vec3 t_Normal = MulNormalMatrix(u_BoneMatrix[t_SkinningIndex], a_Normal); v_DiffuseLighting = u_LightColors[2].rgb; for (int i = 0; i < 2; i++) v_DiffuseLighting += max(dot(t_Normal, u_LightDirs[i].xyz), 0.0) * u_LightColors[i].rgb; @@ -278,7 +281,7 @@ export class BINModelPartInstance { }); } - public prepareToRender(renderInstManager: GfxRenderInstManager, modelViewMatrices: mat4[], modelMatrices: mat4[], textureMatrix: ReadonlyMat4, currentPalette: number): void { + public prepareToRender(renderInstManager: GfxRenderInstManager, modelMatrices: ReadonlyMat4[], textureMatrix: ReadonlyMat4, currentPalette: number): void { const renderInst = renderInstManager.newRenderInst(); renderInst.setGfxProgram(this.gfxProgram); renderInst.setMegaStateFlags(this.megaStateFlags); @@ -292,8 +295,6 @@ export class BINModelPartInstance { let offs = renderInst.allocateUniformBuffer(KatamariDamacyProgram.ub_ModelParams, 12*2*this.transformCount+8+4+4); const d = renderInst.mapUniformBufferF32(KatamariDamacyProgram.ub_ModelParams); - for (let i = 0; i < this.transformCount; i++) - offs += fillMatrix4x3(d, offs, modelViewMatrices[i]); for (let i = 0; i < this.transformCount; i++) offs += fillMatrix4x3(d, offs, modelMatrices[i]); offs += fillMatrix4x2(d, offs, scratchTextureMatrix); @@ -334,7 +335,7 @@ export class BINModelInstance { this.visible = visible; } - public prepareToRender(renderInstManager: GfxRenderInstManager, viewerInput: Viewer.ViewerRenderInput, toNoclip: mat4, currentPalette: number, depth = 0): void { + public prepareToRender(renderInstManager: GfxRenderInstManager, viewerInput: Viewer.ViewerRenderInput, toNoclip: ReadonlyMat4, currentPalette: number, depth = 0): void { if (!this.visible) return; @@ -350,14 +351,11 @@ export class BINModelInstance { template.sortKey = makeSortKey(this.layer) template.sortKey = setSortKeyDepth(template.sortKey, depth); - mat4.mul(scratchModelViews[0], viewerInput.camera.viewMatrix, scratchModelMatrices[0]); - for (let i = 0; i < this.skinningMatrices.length; i++) { + for (let i = 0; i < this.skinningMatrices.length; i++) mat4.mul(scratchModelMatrices[i + 1], toNoclip, this.skinningMatrices[i]); - mat4.mul(scratchModelViews[i + 1], viewerInput.camera.viewMatrix, scratchModelMatrices[i + 1]); - } for (let i = 0; i < this.modelParts.length; i++) - this.modelParts[i].prepareToRender(renderInstManager, scratchModelViews, scratchModelMatrices, this.textureMatrix, currentPalette); + this.modelParts[i].prepareToRender(renderInstManager, scratchModelMatrices, this.textureMatrix, currentPalette); renderInstManager.popTemplate(); } diff --git a/src/KatamariDamacy/scenes.ts b/src/KatamariDamacy/scenes.ts index 34bddc7c2..98007bbcd 100644 --- a/src/KatamariDamacy/scenes.ts +++ b/src/KatamariDamacy/scenes.ts @@ -145,7 +145,7 @@ const lightingSetupList: number[] = [0, 0, 1, 2, 0, 0, 0, 0, 0, 3, 4, 0, 0]; const lightDirScratch = vec3.create(); export function fillSceneParamsData(d: Float32Array, camera: Camera, lightingIndex: number = -1, offs: number = 0): void { - offs += fillMatrix4x4(d, offs, camera.projectionMatrix); + offs += fillMatrix4x4(d, offs, camera.clipFromWorldMatrix); if (lightingIndex === -1) { for (let i = 0; i < 5; i++) offs += fillVec3v(d, offs, Vec3Zero); diff --git a/src/SuperMario64DS/render.ts b/src/SuperMario64DS/render.ts index bdf5baf57..8e0557521 100644 --- a/src/SuperMario64DS/render.ts +++ b/src/SuperMario64DS/render.ts @@ -21,6 +21,7 @@ import { CalcBillboardFlags, calcBillboardMatrix, computeMatrixWithoutScale } fr import { GfxRenderCache } from '../gfx/render/GfxRenderCache.js'; import { setAttachmentStateSimple } from '../gfx/helpers/GfxMegaStateDescriptorHelpers.js'; import { White, colorNewCopy } from '../Color.js'; +import { GfxShaderLibrary } from '../gfx/helpers/GfxShaderLibrary.js'; export class NITRO_Program extends DeviceProgram { public static a_Position = 0; @@ -37,16 +38,16 @@ export class NITRO_Program extends DeviceProgram { precision mediump float; // 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; // Light configuration vec4 u_LightDir[4]; vec4 u_LightColor[4]; }; // Expected to change with each material. -layout(std140) uniform ub_MaterialParams { - Mat4x2 u_TexMtx[1]; +layout(std140, row_major) uniform ub_MaterialParams { + mat4x2 u_TexMtx[1]; vec4 u_Misc[4]; }; #define u_DiffuseColor (u_Misc[0].xyz) @@ -56,8 +57,8 @@ layout(std140) uniform ub_MaterialParams { #define u_TexCoordMode (u_Misc[0].w) #define u_LightMask (u_Misc[1].w) -layout(std140) uniform ub_DrawParams { - Mat4x3 u_PosMtx[32]; +layout(std140, row_major) uniform ub_DrawParams { + mat4x3 u_PosMtx[32]; }; uniform sampler2D u_Texture; @@ -94,22 +95,25 @@ vec3 CalcLight(in vec3 vtxNormal) { return ret; } +${GfxShaderLibrary.MulNormalMatrix} + void main() { - Mat4x3 t_PosMtx = u_PosMtx[int(a_PosMtxIdx)]; - gl_Position = Mul(u_Projection, Mul(_Mat4x4(t_PosMtx), vec4(a_Position, 1.0))); + mat4x3 t_PosMtx = u_PosMtx[int(a_PosMtxIdx)]; + vec3 t_PositionView = t_PosMtx * vec4(a_Position, 1.0); + gl_Position = u_Projection * vec4(t_PositionView, 1.0); v_Color = a_Color; if (a_Color.r < 0.0) { // Turn on lighting - vec3 vtxNormal = normalize(Mul(_Mat4x4(t_PosMtx), vec4(a_Normal, 0.0))).xyz; - v_Color.rgb = CalcLight(vtxNormal); + vec3 t_NormalView = MulNormalMatrix(t_PosMtx, a_Normal); + v_Color.rgb = CalcLight(t_NormalView); } vec2 t_TexSpaceCoord; if (u_TexCoordMode == 2.0) { // TexCoordMode.NORMAL - v_TexCoord = Mul(u_TexMtx[0], vec4(a_Normal, 1.0)).st; + v_TexCoord = u_TexMtx[0] * vec4(a_Normal, 1.0); } else { - v_TexCoord = Mul(u_TexMtx[0], vec4(a_UV, 1.0, 1.0)).st; + v_TexCoord = u_TexMtx[0] * vec4(a_UV, 1.0, 1.0); } } `;