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(renderer): observe deferred batches without inheriting the final mock

yhh043f4319aae4
diff --git a/tests/renderer/MockGfxDevice.hpp b/tests/renderer/MockGfxDevice.hpp
index 53271727a..40928da87 100644
--- a/tests/renderer/MockGfxDevice.hpp
+++ b/tests/renderer/MockGfxDevice.hpp
@@ -4,6 +4,7 @@
// is a no-op stub. Implementing the full GfxDevice contract here also proves the
// interface stays self-consistent as it evolves.
#pragma once
+#include <functional>
#include "esengine/renderer/rhi/GfxDevice.hpp"
@@ -71,6 +72,7 @@ struct MockGfxDevice final : GfxDevice {
int setIndexBufferCalls = 0;
int setPipelineCalls = 0;
int drawElementsCalls = 0;
+ std::function<void(const PipelineDesc&, u32)> observeDrawElements;
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
@@ -168,6 +170,7 @@ struct MockGfxDevice final : GfxDevice {
void drawElements(u32 indexCount, GfxDataType, u32 byteOffset) override {
++drawElementsCalls;
+ if (observeDrawElements) observeDrawElements(lastPipelineDesc, byteOffset);
lastDrawIndexCount = indexCount;
lastDrawIndexByteOffset = byteOffset;
}
diff --git a/tests/renderer/test_batch_builder.cpp b/tests/renderer/test_batch_builder.cpp
index 12187720d..9e10951d8 100644
--- a/tests/renderer/test_batch_builder.cpp
+++ b/tests/renderer/test_batch_builder.cpp
@@ -40,15 +40,13 @@
using namespace esengine;
TEST_CASE("presented primitives retain depth state and order until the scene pass") {
- struct Device : MockGfxDevice {
- std::vector<PipelineDesc> states;
- std::vector<u32> offsets;
- void drawElements(u32 count, GfxDataType type, u32 offset) override {
- MockGfxDevice::drawElements(count, type, offset);
- states.push_back(lastPipelineDesc);
- offsets.push_back(offset);
- }
- } device;
+ MockGfxDevice device;
+ std::vector<PipelineDesc> states;
+ std::vector<u32> offsets;
+ device.observeDrawElements = [&](const PipelineDesc& state, u32 offset) {
+ states.push_back(state);
+ offsets.push_back(offset);
+ };
RenderContext context(device);
resource::ResourceManager resources;
resources.init(device);
@@ -69,21 +67,21 @@ TEST_CASE("presented primitives retain depth state and order until the scene pas
draw.deferEnd();
CHECK(device.drawElementsCalls == 0);
draw.flushDeferred();
- REQUIRE(device.states.size() == 3);
- CHECK_FALSE(device.states[0].depthTest);
- CHECK(device.states[0].depthWrite);
- CHECK_FALSE(device.states[1].depthTest);
- CHECK_FALSE(device.states[1].depthWrite);
- CHECK(device.states[2].depthTest);
- CHECK_FALSE(device.states[2].depthWrite);
- CHECK((device.offsets == std::vector<u32>{0, 6 * sizeof(u32), 12 * sizeof(u32)}));
+ REQUIRE(states.size() == 3);
+ CHECK_FALSE(states[0].depthTest);
+ CHECK(states[0].depthWrite);
+ CHECK_FALSE(states[1].depthTest);
+ CHECK_FALSE(states[1].depthWrite);
+ CHECK(states[2].depthTest);
+ CHECK_FALSE(states[2].depthWrite);
+ CHECK((offsets == std::vector<u32>{0, 6 * sizeof(u32), 12 * sizeof(u32)}));
draw.flushDeferred();
CHECK(device.drawElementsCalls == 3);
draw.begin(glm::mat4(1.0f), 800, 600);
line();
draw.end();
CHECK(device.drawElementsCalls == 4);
- CHECK(device.offsets.back() == 0);
+ CHECK(offsets.back() == 0);
draw.shutdown();
resources.shutdown();
}