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.

test(render): the frame's end is counted, not just attributed

esengine79e95b7698ff
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 45d5c1678..f60424c2c 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -32,6 +32,7 @@ env:
test_clip_depth test_shadow_atlas test_particle_shapes
test_text_edit test_render_graph test_device_loss test_physics_interpolation
test_bitmap_font test_texture_cache test_aot_host test_aot_conformance
+ test_frame_lifecycle
jobs:
build-emscripten:
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 0f1dfccbe..07b578900 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -378,6 +378,19 @@ target_include_directories(test_batch_builder PRIVATE
${CMAKE_SOURCE_DIR}/src
${CMAKE_SOURCE_DIR}/third_party/glm
)
+
+# How many times a frame is closed, which the static gate cannot ask. The GL
+# backends stub endFrame, so a second close is invisible on WebGL2 and shows up
+# only as an empty WebGPU readback; this counts it on a mock device instead.
+add_executable(test_frame_lifecycle
+ renderer/test_frame_lifecycle.cpp
+)
+target_link_libraries(test_frame_lifecycle PRIVATE esengine)
+target_include_directories(test_frame_lifecycle PRIVATE
+ ${CMAKE_SOURCE_DIR}/src
+ ${CMAKE_SOURCE_DIR}/third_party/glm
+)
+add_test(NAME test_frame_lifecycle COMMAND test_frame_lifecycle)
add_test(NAME test_batch_builder COMMAND test_batch_builder)
# GfxDevice compressed-texture entry (RC6-A). Header-only: drives MockGfxDevice to
diff --git a/tests/renderer/MockGfxDevice.hpp b/tests/renderer/MockGfxDevice.hpp
index 89bf11f1d..52666b0fd 100644
--- a/tests/renderer/MockGfxDevice.hpp
+++ b/tests/renderer/MockGfxDevice.hpp
@@ -69,6 +69,12 @@ struct MockGfxDevice final : GfxDevice {
int setPipelineCalls = 0;
int drawElementsCalls = 0;
int drawElementsInstancedCalls = 0;
+ // How many times the frame was declared over. A backend that borrows the
+ // swapchain image gives it back here, so a frame that says so twice releases
+ // an image it no longer holds — invisible on the GL backends, which stub this.
+ int endFrameCalls = 0;
+ void endFrame() override { ++endFrameCalls; }
+
int requestReadbackCalls = 0;
int takeReadbackCalls = 0;
int discardReadbackCalls = 0;
diff --git a/tests/renderer/test_frame_lifecycle.cpp b/tests/renderer/test_frame_lifecycle.cpp
new file mode 100644
index 000000000..a41921b4c
--- /dev/null
+++ b/tests/renderer/test_frame_lifecycle.cpp
@@ -0,0 +1,80 @@
+// How many times a frame is declared over, which is once. `GfxDevice::endFrame`
+// serves a booked capture, so a second close fails the next frame's — and the
+// static gate asks WHO closes a frame, never how many times.
+
+#include "MockGfxDevice.hpp"
+#include "esengine/renderer/frame/RenderFrame.hpp"
+#include "esengine/renderer/frame/RenderContext.hpp"
+#include "esengine/resource/ResourceManager.hpp"
+#include "esengine/ecs/Registry.hpp"
+
+#include <cstdio>
+
+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)
+
+namespace {
+
+constexpr u32 kW = 64;
+constexpr u32 kH = 64;
+
+/** A frame with everything in it that runs between the two ends: two cameras,
+ * and the screen overlay after them. What the closing count must survive. */
+void drawBusyFrame(RenderFrame& frame, ecs::Registry& registry) {
+ const glm::mat4 vp(1.0f);
+ frame.beginFrame();
+
+ frame.begin(vp);
+ frame.end();
+
+ frame.begin(vp);
+ frame.end();
+
+ frame.beginScreenOverlay(vp, 0, 0, kW, kH);
+ frame.submitScreenOverlay(registry);
+ frame.endScreenOverlay(0);
+
+ frame.endFrame();
+}
+
+} // namespace
+
+int main() {
+ MockGfxDevice device;
+ RenderContext context(device);
+ resource::ResourceManager resources;
+ ecs::Registry registry;
+
+ // No init(): that builds the post-process pipeline, whose shaders a mock
+ // device cannot create. What is counted here is the frame's two ends, and
+ // those do not go through it.
+ RenderFrame frame(device, context, resources);
+
+ drawBusyFrame(frame, registry);
+ CHECK(device.endFrameCalls == 1,
+ "one frame closes the device once, through two cameras and the overlay");
+
+ // Frame two, from a device that is still counting: a close that leaks into
+ // the NEXT frame's opening is the shape this exists to catch, and a
+ // single-frame count cannot see it.
+ drawBusyFrame(frame, registry);
+ CHECK(device.endFrameCalls == 2,
+ "the second frame adds exactly one more, so opening a frame closes nothing");
+
+ // ...and a frame nobody drew into. beginFrame/endFrame with nothing between
+ // them is still one close, not zero and not two.
+ frame.beginFrame();
+ frame.endFrame();
+ CHECK(device.endFrameCalls == 3, "an empty frame closes once too");
+
+ // No shutdown() either: nothing was initialised.
+
+ std::printf(g_failures ? "\n%d check(s) failed\n" : "\nall checks passed\n", g_failures);
+ return g_failures == 0 ? 0 : 1;
+}
diff --git a/tools/check-frame-lifecycle.mjs b/tools/check-frame-lifecycle.mjs
index 8114b1559..ebb6bff49 100644
--- a/tools/check-frame-lifecycle.mjs
+++ b/tools/check-frame-lifecycle.mjs
@@ -33,11 +33,12 @@ function stripComments(text) {
}
/**
- * Where the device may be told the frame is over. `endFrame` is the pair of
- * `beginFrame`; `beginFrame` also closes a frame a host forgot to, which is the
- * forgiving half and belongs to the same owner.
+ * Where the device may be told the frame is over. ONE place: closing it
+ * anywhere else costs a host that does honour the pairing its booked capture,
+ * which is served against an image the earlier close already released.
+ * `test_frame_lifecycle` holds the count this rule cannot see.
*/
-const OWNERS = ['RenderFrame::endFrame', 'RenderFrame::beginFrame'];
+const OWNERS = ['RenderFrame::endFrame'];
/** Which C++ function a line falls inside — `Class::name(` at column zero. */
function enclosingFunction(lines, index) {