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(resource): the texture cache counters count the path a shipped build takes

yhh5c47831ab612
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index c64ddd7ea..4158e1749 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -31,7 +31,7 @@ env:
test_compressed_format test_batch_builder test_boundary_span test_webgpu_device
test_clip_depth test_shadow_atlas
test_text_edit test_render_graph test_device_loss test_physics_interpolation
- test_bitmap_font
+ test_bitmap_font test_texture_cache
jobs:
build-emscripten:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6cd1df6dd..2b5238d08 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,6 +14,40 @@ published separately; it ships inside the editor.
## [Unreleased]
+### Fixed
+
+- **`ResourceStats.cacheHits` and `.cacheMisses` counted a loading path the
+ engine no longer takes.** They are written by `loadTexture`, `loadShader` and
+ `loadTextureByGUID` — the file-loading model, of which `loadShader` and
+ `loadTextureByGUID` have no caller at all and `loadTexture` has exactly one
+ (a bitmap font page). A shipped build decodes in the host and registers an id,
+ so both numbers read ~0 for every game while being published in an
+ `@experimental` interface every SDK user can read — the same shape as
+ `render.culled` one layer out. The project's own census reads six fields of
+ `ResourceStats` and pointedly not these two.
+
+ `acquireTextureByPath` is where a shipped frame actually decides: a resident
+ path hands back the texture and an upload does not happen; anything else and
+ one does. It books the hit and the miss now, so the pair means "requests that
+ avoided an upload" — which is what the names always said.
+
+ New harness `test_texture_cache` pins it: `ResourceManager.cpp` against
+ `MockGfxDevice`, no GL and no engine link. Reverted, four of its assertions go
+ red at once.
+
+- **`check-cpp-tests` could not run on any machine that has a compiler.** It
+ strips the options no plain cmake can honour (`EMSCRIPTEN`, `ES_BUILD_WEB`,
+ `ES_SANITIZE`) from its configure and then asked for the targets DECLARED
+ behind them anyway, so it died on `test_webgpu_device` with `No rule to make
+ target` — a red gate about the machine rather than the code. It drops those
+ targets now, and reports 18 harnesses built and passing here with the 9 that
+ link the engine left to CI.
+
+- **Deleted `RenderContextStats`.** Two fields, a `reset()`, and two accessors on
+ `RenderContext` — never written, never read, by anything in the repo. Its
+ `drawCalls` and `triangleCount` are the ones `RenderFrame::Stats` actually
+ maintains, so what it offered was a second answer that could only ever be zero.
+
### Added
- **The batcher is held to what it achieves.** `DrawList` has been publishing
diff --git a/src/esengine/renderer/frame/RenderContext.hpp b/src/esengine/renderer/frame/RenderContext.hpp
index f7d11547e..b805b610f 100644
--- a/src/esengine/renderer/frame/RenderContext.hpp
+++ b/src/esengine/renderer/frame/RenderContext.hpp
@@ -31,24 +31,6 @@
namespace esengine {
-// =============================================================================
-// Renderer Statistics
-// =============================================================================
-
-/**
- * @brief Statistics for rendering performance analysis
- */
-struct RenderContextStats {
- u32 drawCalls = 0; ///< Number of draw calls this frame
- u32 triangleCount = 0; ///< Number of triangles rendered this frame
-
- /** @brief Resets all counters to zero */
- void reset() {
- drawCalls = 0;
- triangleCount = 0;
- }
-};
-
// =============================================================================
// RenderContext Class
// =============================================================================
@@ -125,18 +107,6 @@ public:
*/
const glm::mat4& viewProjection() const { return viewProjection_; }
- /**
- * @brief Gets the rendering statistics
- * @return Reference to the stats
- */
- RenderContextStats& stats() { return stats_; }
-
- /**
- * @brief Gets the rendering statistics (const)
- * @return Const reference to the stats
- */
- const RenderContextStats& stats() const { return stats_; }
-
// =========================================================================
// Internal Resources
// =========================================================================
@@ -196,7 +166,6 @@ private:
void uploadFrameConstants(const glm::mat4& upload, const glm::mat4& engine);
glm::mat4 viewProjection_{1.0f};
- RenderContextStats stats_;
TextureHandle whiteTexture_ = TextureHandle::Invalid;
TextureHandle blackTexture_ = TextureHandle::Invalid;
diff --git a/src/esengine/resource/ResourceManager.cpp b/src/esengine/resource/ResourceManager.cpp
index fb31a8a06..2f762a76d 100644
--- a/src/esengine/resource/ResourceManager.cpp
+++ b/src/esengine/resource/ResourceManager.cpp
@@ -363,13 +363,23 @@ void ResourceManager::setTextureBudget(usize bytes) {
TextureHandle ResourceManager::acquireTextureByPath(const std::string& path) {
auto handle = textures_.findByPath(path);
- if (!handle.isValid()) return handle;
+ if (!handle.isValid()) {
+ ++stats_.cacheMisses;
+ return handle;
+ }
// A texture on the placeholder no longer holds this path's content, so a
// residency hit answers the re-upload with the very thing it replaces — a
// recovery that confirms itself and draws white. Linear: empty at rest.
for (TextureHandle awaiting : awaitingReupload_) {
- if (awaiting == handle) return TextureHandle{};
+ if (awaiting == handle) {
+ ++stats_.cacheMisses;
+ return TextureHandle{};
+ }
}
+ // The cache decision a shipped build makes: it decodes in the host and
+ // registers an id, so this is the one place a texture request avoids an
+ // upload. loadTexture, which also counts, is a path only BitmapFont takes.
+ ++stats_.cacheHits;
textures_.addRef(handle);
return handle;
}
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 42a554e3c..3901a8c86 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -102,6 +102,28 @@ target_include_directories(test_shadow_atlas PRIVATE
)
add_test(NAME test_shadow_atlas COMMAND test_shadow_atlas)
+# Texture path cache: compiles ResourceManager.cpp against MockGfxDevice and asserts
+# which requests avoided an upload. A shipped build decodes in the host and registers
+# an id, so acquireTextureByPath is the only place that decision is made.
+add_executable(test_texture_cache
+ resource/test_texture_cache.cpp
+ ${CMAKE_SOURCE_DIR}/src/esengine/resource/ResourceManager.cpp
+ ${CMAKE_SOURCE_DIR}/src/esengine/resource/ShaderParser.cpp
+ ${CMAKE_SOURCE_DIR}/src/esengine/text/BitmapFont.cpp
+ ${CMAKE_SOURCE_DIR}/src/esengine/renderer/rhi/Shader.cpp
+ ${CMAKE_SOURCE_DIR}/src/esengine/renderer/rhi/Texture.cpp
+ ${CMAKE_SOURCE_DIR}/src/esengine/renderer/rhi/Buffer.cpp
+ ${CMAKE_SOURCE_DIR}/src/esengine/renderer/draw/DrawParams.cpp
+ ${CMAKE_SOURCE_DIR}/src/esengine/core/Log.cpp
+)
+target_include_directories(test_texture_cache PRIVATE
+ ${CMAKE_SOURCE_DIR}/src
+ ${CMAKE_SOURCE_DIR}/tests
+ ${CMAKE_SOURCE_DIR}/third_party/glm
+ ${CMAKE_SOURCE_DIR}/third_party/stb
+)
+add_test(NAME test_texture_cache COMMAND test_texture_cache)
+
# 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/resource/test_texture_cache.cpp b/tests/resource/test_texture_cache.cpp
new file mode 100644
index 000000000..97d398398
--- /dev/null
+++ b/tests/resource/test_texture_cache.cpp
@@ -0,0 +1,61 @@
+// Native MSVC/CTest harness for the texture path cache (ResourceManager).
+//
+// ResourceManager.cpp against MockGfxDevice — no GL, no engine link. It asserts
+// which texture requests avoided an upload, and which are about to cause one.
+
+#include "../renderer/MockGfxDevice.hpp"
+#include "esengine/resource/ResourceManager.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)
+
+int main() {
+ // --- a revived texture is a hit, an unknown path is a miss ---
+ {
+ MockGfxDevice d;
+ resource::ResourceManager rm;
+ rm.init(d);
+
+ const auto handle = rm.registerExternalTexture(7, 4, 4);
+ CHECK(handle.isValid(), "an external texture registers");
+ rm.registerTextureWithPath(handle, "a.png");
+
+ const auto missing = rm.acquireTextureByPath("nobody.png");
+ CHECK(!missing.isValid(), "a path the pool never saw hands back nothing");
+ CHECK(rm.getStats().cacheMisses == 1, "and books a miss, because an upload follows it");
+ CHECK(rm.getStats().cacheHits == 0, "with no hit to go with it");
+
+ const auto again = rm.acquireTextureByPath("a.png");
+ CHECK(again == handle, "a resident path hands back the very same texture");
+ CHECK(rm.getStats().cacheHits == 1, "and books the hit — an upload that did not happen");
+ CHECK(rm.getStats().cacheMisses == 1, "leaving the miss count where it was");
+
+ rm.shutdown();
+ }
+
+ // --- the count is of REQUESTS, so asking twice books twice ---
+ {
+ MockGfxDevice d;
+ resource::ResourceManager rm;
+ rm.init(d);
+
+ const auto handle = rm.registerExternalTexture(9, 2, 2);
+ rm.registerTextureWithPath(handle, "b.png");
+ rm.acquireTextureByPath("b.png");
+ rm.acquireTextureByPath("b.png");
+
+ CHECK(rm.getStats().cacheHits == 2, "two requests, two uploads avoided");
+ rm.shutdown();
+ }
+
+ std::printf(g_failures == 0 ? "\nALL PASS\n" : "\n%d FAILURE(S)\n", g_failures);
+ return g_failures == 0 ? 0 : 1;
+}
diff --git a/tools/check-cpp-tests.mjs b/tools/check-cpp-tests.mjs
index eed13df42..a71146ed5 100644
--- a/tools/check-cpp-tests.mjs
+++ b/tools/check-cpp-tests.mjs
@@ -163,15 +163,19 @@ if (!has('cmake')) {
process.exit(0);
}
+// The options CI configures with that no plain cmake can honour: EMSCRIPTEN and
+// ES_BUILD_WEB are the emscripten toolchain's, ES_SANITIZE a build of its own.
+const fromToolchain = new Set(['EMSCRIPTEN', 'ES_BUILD_WEB', 'ES_SANITIZE']);
+
const skip = enginelinked();
-const buildable = targets.filter((t) => !skip.has(t));
+// A target DECLARED behind one of those cannot exist in a configure that drops
+// it, and asking for one is `No rule to make target` — a red gate about the
+// machine rather than the code, on every checkout that has a compiler.
+const buildable = targets.filter((t) => !skip.has(t)
+ && !(conditions.get(t) ?? []).some((opt) => fromToolchain.has(opt)));
mkdirSync(BUILD, { recursive: true });
try {
- // The options CI configures with, minus what no plain cmake can honour
- // (EMSCRIPTEN / ES_BUILD_WEB are the emscripten toolchain's, ES_SANITIZE a
- // build of its own). Absence under the rest is the platform's.
- const fromToolchain = new Set(['EMSCRIPTEN', 'ES_BUILD_WEB', 'ES_SANITIZE']);
const options = [...new Set(configures.flatMap((c) => [...c.on]))]
.filter((o) => !fromToolchain.has(o))
.map((o) => `-D${o}=ON`);