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.

fix(render): the preview projections speak the convention the engine converts from

yhh531289e1406b
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index f617e2726..1783e0cb3 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -29,6 +29,7 @@ env:
test_registry_ondestroy test_view_api_matrix
test_shader_device test_draw_params test_texture_fb test_buffer_device
test_compressed_format test_batch_builder test_boundary_span test_webgpu_device
+ test_clip_depth
test_text_edit test_render_graph test_device_loss test_physics_interpolation
jobs:
diff --git a/src/esengine/bindings/RendererBindings.cpp b/src/esengine/bindings/RendererBindings.cpp
index efe1f8fa4..78198fe53 100644
--- a/src/esengine/bindings/RendererBindings.cpp
+++ b/src/esengine/bindings/RendererBindings.cpp
@@ -1131,7 +1131,7 @@ void renderer_renderMaterialPreview(u32 materialId, i32 w, i32 h) {
g_transformSystem->update(world);
}
- const glm::mat4 vp = glm::ortho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
+ const glm::mat4 vp = math::ortho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
g_renderFrame->renderToTarget(reg, vp, static_cast<u32>(w), static_cast<u32>(h));
}
@@ -1186,7 +1186,7 @@ void renderer_renderMeshPreview(u32 meshId, i32 w, i32 h) {
half = std::max({half, std::abs(v.x), std::abs(v.y)});
}
half *= 1.08f;
- const glm::mat4 proj = glm::ortho(-half, half, -half, half, 0.01f, radius * 8.0f);
+ const glm::mat4 proj = math::ortho(-half, half, -half, half, 0.01f, radius * 8.0f);
g_renderFrame->renderToTarget(reg, proj * view, static_cast<u32>(w), static_cast<u32>(h));
}
diff --git a/src/esengine/math/Math.hpp b/src/esengine/math/Math.hpp
index 336e0a13e..d5a6a599a 100644
--- a/src/esengine/math/Math.hpp
+++ b/src/esengine/math/Math.hpp
@@ -199,7 +199,10 @@ inline bool approxEqual(const glm::vec3& a, const glm::vec3& b, float epsilon =
* @return Orthographic projection matrix
*/
inline glm::mat4 ortho(float left, float right, float bottom, float top, float nearPlane, float farPlane) {
- return glm::ortho(left, right, bottom, top, nearPlane, farPlane);
+ // _NO, spelled out: the build defines GLM_FORCE_DEPTH_ZERO_TO_ONE, and a
+ // projection that silently followed it would contradict the one convention
+ // ClipDepthRange states the engine builds for.
+ return glm::orthoRH_NO(left, right, bottom, top, nearPlane, farPlane);
}
/**
@@ -211,7 +214,8 @@ inline glm::mat4 ortho(float left, float right, float bottom, float top, float n
* @return Perspective projection matrix
*/
inline glm::mat4 perspective(float fov, float aspect, float nearPlane, float farPlane) {
- return glm::perspective(fov, aspect, nearPlane, farPlane);
+ // See ortho: the engine's convention is stated, not inherited from a macro.
+ return glm::perspectiveRH_NO(fov, aspect, nearPlane, farPlane);
}
/**
diff --git a/src/esengine/renderer/frame/FrameConstants.hpp b/src/esengine/renderer/frame/FrameConstants.hpp
index 1aa8c2d00..a6b22696e 100644
--- a/src/esengine/renderer/frame/FrameConstants.hpp
+++ b/src/esengine/renderer/frame/FrameConstants.hpp
@@ -19,6 +19,7 @@
#pragma once
#include "../../core/Types.hpp"
+#include "../rhi/GfxEnums.hpp"
#include <glm/glm.hpp>
@@ -54,6 +55,21 @@ inline glm::vec4 cameraFromViewProjection(const glm::mat4& viewProjection) {
return glm::vec4(-glm::vec3(axis) / len, 0.0f);
}
+/**
+ * @brief The projection @p p as a device keeping @p range wants it.
+ *
+ * @details The engine builds for [-1, 1], so MinusOneToOne returns @p p itself and
+ * such a device is handed the matrix bit for bit. ZeroToOne averages row 2
+ * with row 3, which is that remap written as a matrix; the viewpoint
+ * cameraFromViewProjection reads is invariant under it.
+ */
+inline glm::mat4 toClipDepthRange(const glm::mat4& p, ClipDepthRange range) {
+ if (range == ClipDepthRange::MinusOneToOne) return p;
+ glm::mat4 out = p;
+ for (int col = 0; col < 4; ++col) out[col][2] = (p[col][2] + p[col][3]) * 0.5f;
+ return out;
+}
+
/** @brief Indexed uniform binding point the FrameConstants block is bound to. */
inline constexpr u32 FRAME_CONSTANTS_BINDING = 0;
diff --git a/src/esengine/renderer/frame/RenderContext.cpp b/src/esengine/renderer/frame/RenderContext.cpp
index c6985310c..2ceb7c73c 100644
--- a/src/esengine/renderer/frame/RenderContext.cpp
+++ b/src/esengine/renderer/frame/RenderContext.cpp
@@ -155,17 +155,8 @@ void RenderContext::initFrameUbo() {
ES_LOG_DEBUG("FrameConstants UBO created (handle: {})", static_cast<u32>(frameUbo_));
}
-// The same projection with its clip z mapped from [-1, 1] onto [0, 1]: row 2
-// becomes the average of rows 2 and 3, which is that map written as a matrix.
-static glm::mat4 toZeroToOneDepth(const glm::mat4& p) {
- glm::mat4 out = p;
- for (int col = 0; col < 4; ++col) out[col][2] = (p[col][2] + p[col][3]) * 0.5f;
- return out;
-}
-
void RenderContext::updateCameraConstants(const glm::mat4& viewProjection) {
- const bool zeroToOne = device_.clipDepthRange() == ClipDepthRange::ZeroToOne;
- uploadFrameConstants(zeroToOne ? toZeroToOneDepth(viewProjection) : viewProjection,
+ uploadFrameConstants(toClipDepthRange(viewProjection, device_.clipDepthRange()),
viewProjection);
}
diff --git a/src/esengine/renderer/frame/RenderContext.hpp b/src/esengine/renderer/frame/RenderContext.hpp
index 15e671424..d3ea2325b 100644
--- a/src/esengine/renderer/frame/RenderContext.hpp
+++ b/src/esengine/renderer/frame/RenderContext.hpp
@@ -151,13 +151,6 @@ public:
TextureHandle getWhiteTexture() const { return whiteTexture_; }
- /**
- * @brief Uploads the per-frame view-projection into the shared FrameConstants UBO.
- * @details Called once per render pass before its draws; every engine shader reads
- * u_projection from this single UBO (bound at FRAME_CONSTANTS_BINDING).
- * The camera's own placement rides along, recovered from the matrix — every
- * path in hands over exactly one view-projection and nothing else.
- */
/**
* @brief Point the frame at what a CAMERA sees.
* @details Keeps the projection in the engine's convention and uploads it in
diff --git a/src/esengine/renderer/frame/RenderFrame.cpp b/src/esengine/renderer/frame/RenderFrame.cpp
index 766ddaa5c..264f165a3 100644
--- a/src/esengine/renderer/frame/RenderFrame.cpp
+++ b/src/esengine/renderer/frame/RenderFrame.cpp
@@ -950,9 +950,9 @@ void RenderFrame::renderShadowMap(ecs::Registry& registry) {
}
}
const glm::mat4 lightView = glm::lookAt(centre + toLight * castDistance, centre, up);
- // Zero-to-one depth on purpose: the engine's shared ortho() is the GL convention
- // (z in [-1,1]) and WebGPU clips everything below 0 of it. Written out here rather
- // than reused, because this matrix is the one place the choice is free.
+ // Written out rather than built from math::ortho: this maps the occluders it
+ // draws onto exactly [0,1], so it needs no conversion for either device — which
+ // is what lets the depths it writes be compared against the receiver's own copy.
glm::mat4 lightProj(1.0f);
const f32 range = castDistance + radius * 2.0f;
lightProj[0][0] = 1.0f / radius;
diff --git a/src/esengine/renderer/rhi/GfxEnums.hpp b/src/esengine/renderer/rhi/GfxEnums.hpp
index 0d5246a86..9239766be 100644
--- a/src/esengine/renderer/rhi/GfxEnums.hpp
+++ b/src/esengine/renderer/rhi/GfxEnums.hpp
@@ -427,6 +427,19 @@ enum class GfxDeviceLostReason : u8 {
Internal, ///< The backend failed inside itself and said no more than that.
};
+/**
+ * @brief What a device's clip volume does with depth.
+ *
+ * @details The engine builds every projection for [-1, 1], which its frustum
+ * extraction and its published frustum-corner API also speak. A device
+ * keeping [0, 1] is handed the same projection in its own terms by
+ * RenderContext::updateCameraConstants, the only place that happens.
+ */
+enum class ClipDepthRange : u8 {
+ MinusOneToOne, ///< OpenGL / OpenGL ES / WebGL.
+ ZeroToOne, ///< WebGPU, and every native API behind it.
+};
+
/**
* @brief Everything a human needs to know about one device loss.
*
@@ -442,19 +455,6 @@ enum class GfxDeviceLostReason : u8 {
* Its own type because a device has an identity for its whole life,
* while a loss report exists only after one.
*/
-/**
- * @brief What a device's clip volume does with depth.
- *
- * @details The engine builds every projection for [-1, 1], which its frustum
- * extraction and its published frustum-corner API also speak. A device
- * keeping [0, 1] is handed the same projection in its own terms by
- * RenderContext::updateCameraConstants, the only place that happens.
- */
-enum class ClipDepthRange : u8 {
- MinusOneToOne, ///< OpenGL / OpenGL ES / WebGL.
- ZeroToOne, ///< WebGPU, and every native API behind it.
-};
-
struct GfxDeviceIdentity {
std::string backend; ///< "WebGL2", "WebGPU".
std::string vendor;
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 853a30e24..51c2186fc 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -82,6 +82,15 @@ target_include_directories(test_draw_params PRIVATE
)
add_test(NAME test_draw_params COMMAND test_draw_params)
+# Clip-depth convention: the engine builds for [-1,1] and a device keeping [0,1]
+# is handed the same projection in its terms. Header-only math, so no engine link.
+add_executable(test_clip_depth renderer/test_clip_depth.cpp)
+target_include_directories(test_clip_depth PRIVATE
+ ${CMAKE_SOURCE_DIR}/src
+ ${CMAKE_SOURCE_DIR}/third_party/glm
+)
+add_test(NAME test_clip_depth COMMAND test_clip_depth)
+
# Shader variant (feature-permutation) system (RC7-6). Compiles ShaderParser.cpp
# directly — no GL/engine link — and checks #pragma feature parsing + #define injection.
add_executable(test_shader_variant
diff --git a/tests/renderer/test_clip_depth.cpp b/tests/renderer/test_clip_depth.cpp
new file mode 100644
index 000000000..486f3223b
--- /dev/null
+++ b/tests/renderer/test_clip_depth.cpp
@@ -0,0 +1,82 @@
+// Clip-depth convention. Asserts what a frame cannot show: that the GL path is a
+// literal pass-through, that the converted one maps the volume exactly, and that
+// the viewpoint the shaders read does not move under the conversion.
+
+#include "esengine/renderer/frame/FrameConstants.hpp"
+#include "esengine/math/Math.hpp"
+
+#include <cstdio>
+#include <cmath>
+#include <cstring>
+
+using namespace esengine;
+
+static int g_failures = 0;
+#define CHECK(cond, msg) \
+ do { \
+ if (!(cond)) { std::printf("FAIL: %s\n", msg); ++g_failures; } \
+ else { std::printf("ok: %s\n", msg); } \
+ } while (0)
+
+/// Clip z of a world point under @p m, after the perspective divide.
+static float ndcZ(const glm::mat4& m, const glm::vec3& world) {
+ const glm::vec4 clip = m * glm::vec4(world, 1.0f);
+ return clip.z / clip.w;
+}
+
+static bool near(float a, float b) { return std::fabs(a - b) < 1e-5f; }
+
+int main() {
+ // The symmetric slab a 2D camera frames: far in both directions, which is what
+ // puts content behind the camera plane at a negative clip z.
+ const glm::mat4 cam = math::ortho(-100.0f, 100.0f, -100.0f, 100.0f, -1000.0f, 1000.0f);
+
+ // The engine's own convention, stated rather than inherited from GLM's macro.
+ CHECK(near(ndcZ(cam, {0, 0, 1000}), -1.0f), "engine ortho: the near edge is -1");
+ CHECK(near(ndcZ(cam, {0, 0, 0}), 0.0f), "engine ortho: the middle is 0");
+ CHECK(near(ndcZ(cam, {0, 0, -1000}), 1.0f), "engine ortho: the far edge is +1");
+
+ // A device keeping the engine's range gets the matrix untouched. Bit-for-bit,
+ // not approximately: this is what makes a WebGL2 build unchanged by the seam.
+ const glm::mat4 gl = toClipDepthRange(cam, ClipDepthRange::MinusOneToOne);
+ CHECK(std::memcmp(&gl[0][0], &cam[0][0], sizeof(glm::mat4)) == 0,
+ "MinusOneToOne returns the projection bit for bit");
+
+ // A device keeping [0,1] gets the same volume expressed in its own range, so
+ // the slab's edges land on the edges rather than half of it being discarded.
+ const glm::mat4 wgpu = toClipDepthRange(cam, ClipDepthRange::ZeroToOne);
+ CHECK(near(ndcZ(wgpu, {0, 0, 1000}), 0.0f), "ZeroToOne: the near edge is 0");
+ CHECK(near(ndcZ(wgpu, {0, 0, 0}), 0.5f), "ZeroToOne: the middle is 0.5");
+ CHECK(near(ndcZ(wgpu, {0, 0, -1000}), 1.0f), "ZeroToOne: the far edge is +1");
+
+ // The case the gate exists for: content behind an ortho camera plane is inside
+ // the slab, so it must survive on a device that discards everything below 0.
+ CHECK(ndcZ(cam, {0, 0, 1}) < 0.0f, "behind the camera plane is negative in [-1,1]");
+ CHECK(ndcZ(wgpu, {0, 0, 1}) > 0.0f, "and is kept once converted");
+
+ // Nothing outside the slab is let in by the conversion.
+ CHECK(ndcZ(wgpu, {0, 0, 1500}) < 0.0f, "ZeroToOne still clips past the near edge");
+ CHECK(ndcZ(wgpu, {0, 0, -1500}) > 1.0f, "ZeroToOne still clips past the far edge");
+
+ // Where the eye is comes out of the matrix, and both the frame constants and
+ // the CPU read it — so the conversion must not move it. Orthographic: a
+ // direction pointing at the viewer.
+ const glm::vec4 eyeGl = cameraFromViewProjection(cam);
+ const glm::vec4 eyeWgpu = cameraFromViewProjection(wgpu);
+ CHECK(near(eyeGl.x, eyeWgpu.x) && near(eyeGl.y, eyeWgpu.y)
+ && near(eyeGl.z, eyeWgpu.z) && near(eyeGl.w, eyeWgpu.w),
+ "the orthographic viewpoint is invariant under the conversion");
+
+ // And perspective: an eye POINT, which divides down from a different column.
+ const glm::mat4 persp = math::perspective(1.0f, 1.6f, 0.1f, 1000.0f)
+ * math::lookAt({0, 0, 300}, {0, 0, 0}, {0, 1, 0});
+ const glm::vec4 pGl = cameraFromViewProjection(persp);
+ const glm::vec4 pWgpu = cameraFromViewProjection(
+ toClipDepthRange(persp, ClipDepthRange::ZeroToOne));
+ CHECK(near(pGl.w, 1.0f) && near(pWgpu.w, 1.0f), "a perspective camera reports an eye point");
+ CHECK(near(pGl.x, pWgpu.x) && near(pGl.y, pWgpu.y) && near(pGl.z, pWgpu.z),
+ "the perspective viewpoint is invariant under the conversion");
+
+ std::printf(g_failures ? "\n%d check(s) failed\n" : "\nall checks passed\n", g_failures);
+ return g_failures == 0 ? 0 : 1;
+}