Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

perf(render): a model matrix travels as three rows, not four

esengine8354a05909e1
diff --git a/src/esengine/data/shaders/mesh.esshader b/src/esengine/data/shaders/mesh.esshader
index 1f1d61536..7ff338934 100644
--- a/src/esengine/data/shaders/mesh.esshader
+++ b/src/esengine/data/shaders/mesh.esshader
@@ -53,10 +53,12 @@ layout(location = 6) in vec4 a_weights;
// 8.. and not 3..: a channel's semantic IS its location, so adding normals to a
// mesh must not move the per-object record (see MESH_INSTANCE_FIRST_LOCATION).
#ifndef SKINNED
+// Three ROWS of an affine 4x4. The fourth is (0,0,0,1) on every transform a
+// Transform can state, so carrying it spends an attribute slot and 16 bytes an
+// instance on a constant.
layout(location = 8) in vec4 a_model0;
layout(location = 9) in vec4 a_model1;
layout(location = 10) in vec4 a_model2;
-layout(location = 11) in vec4 a_model3;
#endif
layout(location = 12) in vec4 a_instTint;
#if defined(MESH_NORMALS) && !defined(SKINNED)
@@ -142,7 +144,10 @@ void main() {
+ a_weights.w * u_bones[a_joints.w];
vec4 world = skin * vec4(local, 1.0);
#else
- mat4 model = mat4(a_model0, a_model1, a_model2, a_model3);
+ mat4 model = mat4(vec4(a_model0.x, a_model1.x, a_model2.x, 0.0),
+ vec4(a_model0.y, a_model1.y, a_model2.y, 0.0),
+ vec4(a_model0.z, a_model1.z, a_model2.z, 0.0),
+ vec4(a_model0.w, a_model1.w, a_model2.w, 1.0));
vec4 world = model * vec4(local, 1.0);
#endif
gl_Position = u_projection * world;
@@ -245,7 +250,6 @@ struct VSIn {
@location(8) a_model0 : vec4f,
@location(9) a_model1 : vec4f,
@location(10) a_model2 : vec4f,
- @location(11) a_model3 : vec4f,
#endif
@location(12) a_instTint : vec4f,
// What the GLSL twin reads as gl_VertexID: a morph delta is addressed by it.
@@ -299,7 +303,10 @@ struct VSOut {
+ v.a_weights.w * skin.bones[v.a_joints.w];
let world = pose * vec4f(local, 1.0);
#else
- let model = mat4x4f(v.a_model0, v.a_model1, v.a_model2, v.a_model3);
+ let model = mat4x4f(vec4f(v.a_model0.x, v.a_model1.x, v.a_model2.x, 0.0),
+ vec4f(v.a_model0.y, v.a_model1.y, v.a_model2.y, 0.0),
+ vec4f(v.a_model0.z, v.a_model1.z, v.a_model2.z, 0.0),
+ vec4f(v.a_model0.w, v.a_model1.w, v.a_model2.w, 1.0));
let world = model * vec4f(local, 1.0);
#endif
diff --git a/src/esengine/renderer/plugins/MeshPlugin.cpp b/src/esengine/renderer/plugins/MeshPlugin.cpp
index 14df26e9a..52e595ca0 100644
--- a/src/esengine/renderer/plugins/MeshPlugin.cpp
+++ b/src/esengine/renderer/plugins/MeshPlugin.cpp
@@ -672,14 +672,19 @@ void MeshPlugin::collect(RenderCollectContext& collect_ctx) {
glm::mat4 model = glm::translate(glm::mat4(1.0f), position)
* glm::mat4_cast(rotation)
* glm::scale(glm::mat4(1.0f), scale);
- std::memcpy(dst, &model[0][0], 64);
- std::memcpy(dst + 64, &tintRGBA, 4);
+ // Three rows, not four columns: the row the shader rebuilds is
+ // the one an affine transform never varies.
+ for (u32 row = 0; row < 3; ++row) {
+ const glm::vec4 r{model[0][row], model[1][row], model[2][row], model[3][row]};
+ std::memcpy(dst + row * 16, &r, 16);
+ }
+ std::memcpy(dst + 48, &tintRGBA, 4);
if (resident->hasNormals) {
// Written per object rather than derived per vertex: this is
// the transform a normal takes under a non-uniform scale.
const glm::mat3 nrm = glm::transpose(glm::inverse(glm::mat3(model)));
for (u32 row = 0; row < 3; ++row) {
- std::memcpy(dst + 68 + row * 12, &nrm[row][0], 12);
+ std::memcpy(dst + 52 + row * 12, &nrm[row][0], 12);
}
}
}
diff --git a/src/esengine/renderer/rhi/GfxEnums.hpp b/src/esengine/renderer/rhi/GfxEnums.hpp
index 3fed2d64c..fb48718c1 100644
--- a/src/esengine/renderer/rhi/GfxEnums.hpp
+++ b/src/esengine/renderer/rhi/GfxEnums.hpp
@@ -93,12 +93,14 @@ enum class LayoutId : u8 {
static constexpr u32 LAYOUT_COUNT = 5;
-/// One per-object record for a resident mesh: a model matrix and a tint.
-static constexpr u32 MESH_INSTANCE_STRIDE = 68;
+/// One per-object record for a resident mesh: three ROWS of an affine model
+/// matrix, then a tint. The matrix's fourth row is (0,0,0,1) on every transform
+/// a Transform can state, so it is neither sent nor given an attribute slot.
+static constexpr u32 MESH_INSTANCE_STRIDE = 52;
/// The same plus a normal matrix (three vec3 rows), for geometry with normals.
/// Carried per object because a non-uniform scale makes the model matrix the
/// wrong transform for a normal, and inverting one per vertex is the alternative.
-static constexpr u32 MESH_INSTANCE_STRIDE_LIT = 104;
+static constexpr u32 MESH_INSTANCE_STRIDE_LIT = 88;
/// A SKINNED object's record: the tint, and nothing else. No model matrix — a
/// skinned mesh's own transform is ignored (glTF says so) because its joints are
/// already placed in the world — and the pose itself is a uniform block.
diff --git a/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp b/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp
index 7fb52d8c6..57bdb944d 100644
--- a/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp
+++ b/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp
@@ -404,10 +404,12 @@ layout(location = 6) in vec4 a_weights;
#ifndef SKINNED
+
+
+
layout(location = 8) in vec4 a_model0;
layout(location = 9) in vec4 a_model1;
layout(location = 10) in vec4 a_model2;
-layout(location = 11) in vec4 a_model3;
#endif
layout(location = 12) in vec4 a_instTint;
#if defined(MESH_NORMALS) && !defined(SKINNED)
@@ -493,7 +495,10 @@ void main() {
+ a_weights.w * u_bones[a_joints.w];
vec4 world = skin * vec4(local, 1.0);
#else
- mat4 model = mat4(a_model0, a_model1, a_model2, a_model3);
+ mat4 model = mat4(vec4(a_model0.x, a_model1.x, a_model2.x, 0.0),
+ vec4(a_model0.y, a_model1.y, a_model2.y, 0.0),
+ vec4(a_model0.z, a_model1.z, a_model2.z, 0.0),
+ vec4(a_model0.w, a_model1.w, a_model2.w, 1.0));
vec4 world = model * vec4(local, 1.0);
#endif
gl_Position = u_projection * world;
@@ -596,7 +601,6 @@ struct VSIn {
@location(8) a_model0 : vec4f,
@location(9) a_model1 : vec4f,
@location(10) a_model2 : vec4f,
- @location(11) a_model3 : vec4f,
#endif
@location(12) a_instTint : vec4f,
@@ -650,7 +654,10 @@ struct VSOut {
+ v.a_weights.w * skin.bones[v.a_joints.w];
let world = pose * vec4f(local, 1.0);
#else
- let model = mat4x4f(v.a_model0, v.a_model1, v.a_model2, v.a_model3);
+ let model = mat4x4f(vec4f(v.a_model0.x, v.a_model1.x, v.a_model2.x, 0.0),
+ vec4f(v.a_model0.y, v.a_model1.y, v.a_model2.y, 0.0),
+ vec4f(v.a_model0.z, v.a_model1.z, v.a_model2.z, 0.0),
+ vec4f(v.a_model0.w, v.a_model1.w, v.a_model2.w, 1.0));
let world = model * vec4f(local, 1.0);
#endif
diff --git a/src/esengine/resource/ResourceManager.cpp b/src/esengine/resource/ResourceManager.cpp
index 0ead4c1ff..41c81cf6d 100644
--- a/src/esengine/resource/ResourceManager.cpp
+++ b/src/esengine/resource/ResourceManager.cpp
@@ -559,17 +559,17 @@ bool ResourceManager::realizeMesh(Mesh& mesh, ConstSpan<u8> vertexBytes, ConstSp
// its shader does not consume, which WebGPU rejects. A skinned record thus
// carries neither model nor normal matrix — its bones are world-space.
if (!skinned) {
- for (u32 row = 0; row < 4; ++row) {
+ for (u32 row = 0; row < 3; ++row) {
layout.attributes[next++] = {MESH_INSTANCE_FIRST_LOCATION + row, 4, GfxDataType::Float,
false, row * 16u, 1};
}
}
layout.attributes[next++] = {MESH_INSTANCE_FIRST_LOCATION + 4, 4, GfxDataType::UnsignedByte,
- true, skinned ? 0u : 64u, 1};
+ true, skinned ? 0u : 48u, 1};
if (hasNormals && !skinned) {
for (u32 row = 0; row < 3; ++row) {
layout.attributes[next++] = {MESH_INSTANCE_FIRST_LOCATION + 5 + row, 3,
- GfxDataType::Float, false, 68 + row * 12u, 1};
+ GfxDataType::Float, false, 52 + row * 12u, 1};
}
}
layout.attributeCount = next;
diff --git a/src/esengine/resource/ShaderParser.cpp b/src/esengine/resource/ShaderParser.cpp
index 553c1e10b..85579996f 100644
--- a/src/esengine/resource/ShaderParser.cpp
+++ b/src/esengine/resource/ShaderParser.cpp
@@ -170,7 +170,6 @@ std::string canonicalVertexStageWGSL(bool lit) {
" @location(8) a_model0 : vec4f,\n"
" @location(9) a_model1 : vec4f,\n"
" @location(10) a_model2 : vec4f,\n"
- " @location(11) a_model3 : vec4f,\n"
" @location(12) a_instTint : vec4f,\n"
"#endif\n"
"#ifdef MESH_NORMALS\n"
@@ -233,7 +232,9 @@ std::string canonicalVertexStageWGSL(bool lit) {
" let world = pose * vec4f(local, 1.0);\n"
" out.v_color = v.a_color * v.a_instTint;\n"
"#elif defined(MESH)\n"
- " let world = mat4x4f(v.a_model0, v.a_model1, v.a_model2, v.a_model3) * vec4f(local, 1.0);\n"
+ " let world = vec4f(dot(v.a_model0, vec4f(local, 1.0)),\n"
+ " dot(v.a_model1, vec4f(local, 1.0)),\n"
+ " dot(v.a_model2, vec4f(local, 1.0)), 1.0);\n"
" out.v_color = v.a_color * v.a_instTint;\n"
"#else\n"
" let world = vec4f(v.a_position, 1.0);\n"
@@ -345,7 +346,6 @@ std::string canonicalVertexStage(bool lit) {
"layout(location = 8) in vec4 a_model0;\n"
"layout(location = 9) in vec4 a_model1;\n"
"layout(location = 10) in vec4 a_model2;\n"
- "layout(location = 11) in vec4 a_model3;\n"
"layout(location = 12) in vec4 a_instTint;\n"
"#endif\n"
"#ifdef MESH_NORMALS\n"
@@ -428,7 +428,9 @@ std::string canonicalVertexStage(bool lit) {
" vec4 world = skin * vec4(local, 1.0);\n"
" v_color = a_color * a_instTint;\n"
"#elif defined(MESH)\n"
- " vec4 world = mat4(a_model0, a_model1, a_model2, a_model3) * vec4(local, 1.0);\n"
+ " vec4 world = vec4(dot(a_model0, vec4(local, 1.0)),\n"
+ " dot(a_model1, vec4(local, 1.0)),\n"
+ " dot(a_model2, vec4(local, 1.0)), 1.0);\n"
" v_color = a_color * a_instTint;\n"
"#else\n"
" vec4 world = vec4(a_position, 1.0);\n"