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): pin a batch draw's unused sampler slots to white

esengine621b96f97b11
diff --git a/src/esengine/renderer/DrawList.cpp b/src/esengine/renderer/DrawList.cpp
index fcd73efbb..f21107b97 100644
--- a/src/esengine/renderer/DrawList.cpp
+++ b/src/esengine/renderer/DrawList.cpp
@@ -100,7 +100,8 @@ void DrawList::finalize(TransientBufferPool& pool) {
}
void DrawList::execute(GfxDevice& device, TransientBufferPool& buffers,
- MaterialStore& materials, FrameCapture* capture) {
+ MaterialStore& materials, u32 white_texture_id,
+ FrameCapture* capture) {
PipelineDesc lastDesc{};
PipelineHandle lastHandle = PipelineHandle::Invalid;
@@ -149,11 +150,14 @@ void DrawList::execute(GfxDevice& device, TransientBufferPool& buffers,
}
// The batch shader declares 8 samplers, and WebGL2 invalidates a draw if any
// referenced sampler unit lacks a complete texture — even units the per-vertex
- // branch never samples. For the Batch layout, fill the unused slots with slot 0's
- // (always-valid) texture; other layouts bind only the samplers they declare.
+ // branch never samples. For the Batch layout, fill the unused slots with a
+ // STABLE white texture: those units then stay pinned across draws, so the
+ // per-slot bind cache makes them no-ops and only slot 0 rebinds per texture
+ // change (filling with this draw's own tex re-pointed all 8 units each time).
+ // Other layouts bind only the samplers they declare.
if (cmd.layout_id == LayoutId::Batch) {
for (u8 slot = 0; slot < MAX_CMD_TEXTURE_SLOTS; ++slot) {
- u32 tex = (slot < cmd.texture_count) ? cmd.texture_ids[slot] : cmd.texture_ids[0];
+ u32 tex = (slot < cmd.texture_count) ? cmd.texture_ids[slot] : white_texture_id;
device.bindTexture(slot, TextureHandle{tex});
}
} else {
diff --git a/src/esengine/renderer/DrawList.hpp b/src/esengine/renderer/DrawList.hpp
index c26fc8071..3b308cb03 100644
--- a/src/esengine/renderer/DrawList.hpp
+++ b/src/esengine/renderer/DrawList.hpp
@@ -29,8 +29,12 @@ public:
// (scissor, stencil ref, textures) is applied directly. Per-frame constants come from
// the FrameConstants UBO bound by RenderContext; per-material constants from each
// command's material UBO, bound here via MaterialStore::bindForDraw.
+ // white_texture_id fills a Batch draw's unused sampler slots — a STABLE texture
+ // so those slots stay pinned across draws and the bind cache dedups them
+ // (filling with the draw's own tex re-pointed all 8 units on every tex change).
void execute(GfxDevice& device, TransientBufferPool& buffers,
- MaterialStore& materials, FrameCapture* capture = nullptr);
+ MaterialStore& materials, u32 white_texture_id = 0,
+ FrameCapture* capture = nullptr);
u32 commandCount() const { return static_cast<u32>(commands_.size()); }
u32 mergedDrawCallCount() const { return merged_draw_calls_; }
diff --git a/src/esengine/renderer/RenderFrame.cpp b/src/esengine/renderer/RenderFrame.cpp
index ee0342eee..8228b093b 100644
--- a/src/esengine/renderer/RenderFrame.cpp
+++ b/src/esengine/renderer/RenderFrame.cpp
@@ -293,7 +293,7 @@ void RenderFrame::flush() {
{
ES_PROFILE_SCOPE("render.submit");
gpu_timer_.begin(device_);
- draw_list_.execute(device_, pool_, context_.materials(), &frame_capture_);
+ draw_list_.execute(device_, pool_, context_.materials(), context_.getWhiteTextureId(), &frame_capture_);
gpu_timer_.end(device_);
}
FrameProfiler::get().gpuScope("submit", gpu_timer_.lastMs());
@@ -407,7 +407,7 @@ void RenderFrame::replayToDrawCall(i32 stopAtDrawCall) {
context_.updateFrameConstants(view_projection_);
context_.lights().uploadAndBind();
- draw_list_.execute(device_, pool_, context_.materials(), &frame_capture_);
+ draw_list_.execute(device_, pool_, context_.materials(), context_.getWhiteTextureId(), &frame_capture_);
// Leave scissor disabled for whatever renders next; invalidate so the next
// setPipeline re-applies its full state (stencil included).
@@ -461,7 +461,7 @@ void RenderFrame::renderToTarget(ecs::Registry& registry, const glm::mat4& viewP
pool_.upload();
context_.updateFrameConstants(viewProjection);
context_.lights().uploadAndBind();
- draw_list_.execute(device_, pool_, context_.materials(), &frame_capture_);
+ draw_list_.execute(device_, pool_, context_.materials(), context_.getWhiteTextureId(), &frame_capture_);
frame_capture_.endCapture();
rt->unbind();