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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
refactor(render): unify batch submission onto one primitive — REARCH_RENDER P0
esengine675777a3c345
diff --git a/CMakeLists.txt b/CMakeLists.txtindex eb59e54e0..2fd4d73ca 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -54,6 +54,8 @@ set(ESENGINE_SOURCESsrc/esengine/renderer/StateTracker.cppsrc/esengine/renderer/TransientBufferPool.cppsrc/esengine/renderer/DrawList.cpp+ src/esengine/renderer/BatchBuilder.cpp+ src/esengine/renderer/plugins/BatchPlugin.cppsrc/esengine/renderer/plugins/SpritePlugin.cppsrc/esengine/renderer/plugins/UIElementPlugin.cppsrc/esengine/renderer/plugins/TextPlugin.cpp@@ -138,7 +140,9 @@ set(ESENGINE_HEADERSsrc/esengine/renderer/DrawCommand.hppsrc/esengine/renderer/DrawList.hppsrc/esengine/renderer/ClipState.hpp+ src/esengine/renderer/BatchBuilder.hppsrc/esengine/renderer/RenderTypePlugin.hpp+ src/esengine/renderer/plugins/BatchPlugin.hppsrc/esengine/renderer/plugins/SpritePlugin.hppsrc/esengine/renderer/plugins/UIElementPlugin.hppsrc/esengine/renderer/plugins/TextPlugin.hppdiff --git a/src/esengine/renderer/BatchBuilder.cpp b/src/esengine/renderer/BatchBuilder.cppnew file mode 100644index 000000000..d977b4ca4--- /dev/null+++ b/src/esengine/renderer/BatchBuilder.cpp@@ -0,0 +1,89 @@+/**+ * @file BatchBuilder.cpp+ * @brief Implementation of the shared batch submission primitive.+ *+ * @author ESEngine Team+ * @date 2026+ *+ * @copyright Copyright (c) 2026 ESEngine Team+ * Licensed under the MIT License.+ */+#include "BatchBuilder.hpp"++#include <vector>++namespace esengine {++namespace {++// Reused across calls so offsetting local indices into the shared Batch stream costs no+// per-call heap allocation. The renderer collects single-threaded and each call fully+// consumes the scratch before returning, so a file-local buffer is safe — and it amortizes+// the allocation across the frame, mirroring DrawList::sorted_scratch_.+std::vector<u32> g_indexScratch;++template <typename IndexT>+void pushBatchCommandImpl(TransientBufferPool& pool, DrawList& drawList, const ClipState& clips,+ u32 vertexByteOffset, const IndexT* localIndices, u32 indexCount,+ const BatchDrawKey& key) {+ if (indexCount == 0) return;++ u32 baseVertex = vertexByteOffset / static_cast<u32>(sizeof(BatchVertex));+ g_indexScratch.resize(indexCount);+ for (u32 i = 0; i < indexCount; ++i) {+ g_indexScratch[i] = baseVertex + static_cast<u32>(localIndices[i]);+ }+ u32 indexOffset = pool.appendIndices(LayoutId::Batch, g_indexScratch.data(), indexCount);++ pushBatchDraw(drawList, clips, vertexByteOffset, indexOffset, indexCount, key);+}++} // namespace++void pushBatchDraw(DrawList& drawList, const ClipState& clips,+ u32 vertexByteOffset, u32 indexOffset, u32 indexCount,+ const BatchDrawKey& key) {+ if (indexCount == 0) return;++ DrawCommand cmd{};+ cmd.sort_key = DrawCommand::buildSortKey(key.stage, key.layer, key.shaderId,+ key.blend, 0, key.textureId, key.depth);+ cmd.index_offset = indexOffset;+ cmd.index_count = indexCount;+ cmd.vertex_byte_offset = vertexByteOffset;+ cmd.shader_id = key.shaderId;+ cmd.blend_mode = key.blend;+ cmd.layout_id = LayoutId::Batch;+ cmd.texture_count = 1;+ cmd.texture_ids[0] = key.textureId;+ cmd.entity = key.entity;+ cmd.type = key.type;+ cmd.layer = key.layer;++ clips.applyTo(key.entity, cmd);+ drawList.push(cmd);+}++void pushBatchCommand(TransientBufferPool& pool, DrawList& drawList, const ClipState& clips,+ u32 vertexByteOffset, const u32* localIndices, u32 indexCount,+ const BatchDrawKey& key) {+ pushBatchCommandImpl(pool, drawList, clips, vertexByteOffset, localIndices, indexCount, key);+}++void pushBatchCommand(TransientBufferPool& pool, DrawList& drawList, const ClipState& clips,+ u32 vertexByteOffset, const u16* localIndices, u32 indexCount,+ const BatchDrawKey& key) {+ pushBatchCommandImpl(pool, drawList, clips, vertexByteOffset, localIndices, indexCount, key);+}++void appendIndexedBatch(TransientBufferPool& pool, DrawList& drawList, const ClipState& clips,+ const BatchVertex* verts, u32 vertexCount,+ const u32* localIndices, u32 indexCount,+ const BatchDrawKey& key) {+ if (vertexCount == 0 || indexCount == 0) return;+ u32 vertexByteOffset = pool.appendVertices(+ LayoutId::Batch, verts, vertexCount * static_cast<u32>(sizeof(BatchVertex)));+ pushBatchCommandImpl(pool, drawList, clips, vertexByteOffset, localIndices, indexCount, key);+}++} // namespace esenginediff --git a/src/esengine/renderer/BatchBuilder.hpp b/src/esengine/renderer/BatchBuilder.hppnew file mode 100644index 000000000..210b0be5d--- /dev/null+++ b/src/esengine/renderer/BatchBuilder.hpp@@ -0,0 +1,125 @@+/**+ * @file BatchBuilder.hpp+ * @brief Backend-agnostic batch submission primitive shared by every batch render path.+ * @details Collapses the "append vertices -> offset indices by baseVertex -> assemble+ * DrawCommand -> apply clip state -> push" sequence that each batch emitter+ * (sprite/UI/text/particle/tilemap plugins + RenderFrame's spine/tile direct+ * submits) used to copy verbatim. Giving the renderer one submission face here+ * is the de-risking prerequisite for the GfxDevice keystone (docs/REARCH_RENDER.md+ * P0): the keystone rewrites DrawList::execute against a single producer, not six.+ *+ * @author ESEngine Team+ * @date 2026+ *+ * @copyright Copyright (c) 2026 ESEngine Team+ * Licensed under the MIT License.+ */+#pragma once++// =============================================================================+// Includes+// =============================================================================++#include "../core/Types.hpp"+#include "BatchVertex.hpp"+#include "BlendMode.hpp"+#include "ClipState.hpp"+#include "DrawCommand.hpp"+#include "DrawList.hpp"+#include "RenderItem.hpp"+#include "RenderStage.hpp"+#include "TransientBufferPool.hpp"++#include <glm/glm.hpp>++namespace esengine {++// =============================================================================+// Shared quad geometry (single source of truth)+// =============================================================================++/** @brief Canonical centered unit-quad corner positions (CCW). */+inline constexpr glm::vec4 BATCH_QUAD_POSITIONS[4] = {+ { -0.5f, -0.5f, 0.0f, 1.0f },+ { 0.5f, -0.5f, 0.0f, 1.0f },+ { 0.5f, 0.5f, 0.0f, 1.0f },+ { -0.5f, 0.5f, 0.0f, 1.0f },+};++/** @brief Unit-quad texture coordinates matching BATCH_QUAD_POSITIONS. */+inline constexpr glm::vec2 BATCH_QUAD_TEX_COORDS[4] = {+ { 0.0f, 0.0f },+ { 1.0f, 0.0f },+ { 1.0f, 1.0f },+ { 0.0f, 1.0f },+};++/** @brief Canonical two-triangle quad winding shared by every batch path. */+inline constexpr u32 BATCH_QUAD_INDICES[6] = { 0, 1, 2, 2, 3, 0 };++/** @brief Rotates a point around @p center using a precomputed cos/sin. */+inline glm::vec2 rotatePoint(const glm::vec2& center, f32 px, f32 py, f32 cosA, f32 sinA) {+ f32 dx = px - center.x;+ f32 dy = py - center.y;+ return { center.x + dx * cosA - dy * sinA,+ center.y + dx * sinA + dy * cosA };+}++// =============================================================================+// Batch submission+// =============================================================================++/** @brief Non-geometry attributes of a batch draw — everything needed to build the sort key + DrawCommand. */+struct BatchDrawKey {+ RenderStage stage = RenderStage::Transparent;+ i32 layer = 0;+ u32 shaderId = 0;+ BlendMode blend = BlendMode::Normal;+ u32 textureId = 0;+ f32 depth = 0.0f;+ Entity entity = INVALID_ENTITY;+ RenderType type = RenderType::Sprite;+};++/**+ * @brief Atomic primitive: assemble + clip + push one DrawCommand for an index range ALREADY+ * written into the Batch stream.+ * @details The single piece every batch path used to duplicate. Use directly when the caller+ * streams both vertices and indices in place (the particle emitter writes its whole+ * index range via writeIndices() before pushing one command).+ */+void pushBatchDraw(DrawList& drawList, const ClipState& clips,+ u32 vertexByteOffset, u32 indexOffset, u32 indexCount,+ const BatchDrawKey& key);++/**+ * @brief Pushes a DrawCommand for vertices ALREADY resident in the Batch stream at @p vertexByteOffset.+ * @details Offsets @p localIndices (0-based within the primitive) by the stream's baseVertex,+ * appends them, then delegates to pushBatchDraw. Use when the caller formatted vertices+ * in place via allocVertices()+vertexData() (the spine path). The u16/u32 overloads+ * cover both index source widths.+ */+void pushBatchCommand(TransientBufferPool& pool, DrawList& drawList, const ClipState& clips,+ u32 vertexByteOffset, const u32* localIndices, u32 indexCount,+ const BatchDrawKey& key);+void pushBatchCommand(TransientBufferPool& pool, DrawList& drawList, const ClipState& clips,+ u32 vertexByteOffset, const u16* localIndices, u32 indexCount,+ const BatchDrawKey& key);++/**+ * @brief Copies @p verts into the Batch stream, then pushes one DrawCommand spanning them.+ * @details The bulk form — a single command over @p vertexCount vertices / @p indexCount+ * indices (particle emitter, tilemap chunk, single tile).+ */+void appendIndexedBatch(TransientBufferPool& pool, DrawList& drawList, const ClipState& clips,+ const BatchVertex* verts, u32 vertexCount,+ const u32* localIndices, u32 indexCount,+ const BatchDrawKey& key);++/** @brief Convenience for a single 4-vertex quad with the canonical 6-index winding. */+inline void appendQuad(TransientBufferPool& pool, DrawList& drawList, const ClipState& clips,+ const BatchVertex quad[4], const BatchDrawKey& key) {+ appendIndexedBatch(pool, drawList, clips, quad, 4, BATCH_QUAD_INDICES, 6, key);+}++} // namespace esenginediff --git a/src/esengine/renderer/RenderFrameSubmit.cpp b/src/esengine/renderer/RenderFrameSubmit.cppindex 2e9a88cd5..077da2914 100644--- a/src/esengine/renderer/RenderFrameSubmit.cpp+++ b/src/esengine/renderer/RenderFrameSubmit.cpp@@ -1,17 +1,13 @@#include "RenderFrame.hpp"+#include "BatchBuilder.hpp"#include "BatchVertex.hpp"#include <glm/gtc/type_ptr.hpp>#include <algorithm>-#include <vector>namespace esengine {-namespace {-static constexpr u16 TILE_QUAD_IDX[6] = { 0, 1, 2, 2, 3, 0 };-} // namespace-void RenderFrame::submitTileQuad(const glm::vec2& position, const glm::vec2& size,const glm::vec2& uvOffset, const glm::vec2& uvScale,@@ -22,39 +18,23 @@ void RenderFrame::submitTileQuad(f32 hh = size.y * 0.5f;u32 pc = packColor(color);- BatchVertex verts[4];- verts[0] = { {position.x - hw, position.y - hh}, pc, uvOffset };- verts[1] = { {position.x + hw, position.y - hh}, pc, {uvOffset.x + uvScale.x, uvOffset.y} };- verts[2] = { {position.x + hw, position.y + hh}, pc, {uvOffset.x + uvScale.x, uvOffset.y + uvScale.y} };- verts[3] = { {position.x - hw, position.y + hh}, pc, {uvOffset.x, uvOffset.y + uvScale.y} };-- u32 vOff = pool_.appendVertices(LayoutId::Batch, verts, sizeof(verts));- u32 baseVertex = vOff / sizeof(BatchVertex);-- u32 indices[6];- for (u32 i = 0; i < 6; ++i) {- indices[i] = static_cast<u32>(baseVertex + TILE_QUAD_IDX[i]);- }- u32 iOff = pool_.appendIndices(LayoutId::Batch, indices, 6);-- DrawCommand cmd{};- cmd.sort_key = DrawCommand::buildSortKey(- current_stage_, layer, batch_shader_id_, BlendMode::Normal, 0, textureId, depth);- cmd.index_offset = iOff;- cmd.index_count = 6;- cmd.vertex_byte_offset = vOff;- cmd.shader_id = batch_shader_id_;- cmd.blend_mode = BlendMode::Normal;- cmd.layout_id = LayoutId::Batch;- cmd.texture_count = 1;- cmd.texture_ids[0] = textureId;- cmd.entity = entity;- cmd.type = RenderType::Sprite;- cmd.layer = layer;-- clip_state_.applyTo(entity, cmd);-- draw_list_.push(cmd);+ BatchVertex verts[4] = {+ { {position.x - hw, position.y - hh}, pc, uvOffset },+ { {position.x + hw, position.y - hh}, pc, {uvOffset.x + uvScale.x, uvOffset.y} },+ { {position.x + hw, position.y + hh}, pc, {uvOffset.x + uvScale.x, uvOffset.y + uvScale.y} },+ { {position.x - hw, position.y + hh}, pc, {uvOffset.x, uvOffset.y + uvScale.y} },+ };++ appendQuad(pool_, draw_list_, clip_state_, verts, BatchDrawKey{+ .stage = current_stage_,+ .layer = layer,+ .shaderId = batch_shader_id_,+ .blend = BlendMode::Normal,+ .textureId = textureId,+ .depth = depth,+ .entity = entity,+ .type = RenderType::Sprite,+ });}#ifdef ES_ENABLE_SPINE@@ -71,6 +51,8 @@ void RenderFrame::submitSpineBatch(glm::mat4 model = glm::make_mat4(transform16);BlendMode blend = static_cast<BlendMode>(std::clamp(blendMode, 0, 5));+ // Format the transformed vertices in place — a complex skeleton can carry many+ // vertices, so we avoid staging them into a temporary before the pool copy.u32 vBytes = static_cast<u32>(vertexCount) * sizeof(BatchVertex);u32 vOff = pool_.allocVertices(LayoutId::Batch, vBytes);auto* dst = reinterpret_cast<BatchVertex*>(pool_.vertexData(LayoutId::Batch) + vOff);@@ -82,30 +64,17 @@ void RenderFrame::submitSpineBatch(dst[i] = { {worldPos.x, worldPos.y}, pc, {v[2], v[3]} };}- u32 baseVertex = vOff / sizeof(BatchVertex);- std::vector<u32> offsetIndices(indexCount);- for (i32 i = 0; i < indexCount; ++i) {- offsetIndices[i] = static_cast<u32>(baseVertex + indices[i]);- }- u32 iOff = pool_.appendIndices(LayoutId::Batch, offsetIndices.data(), static_cast<u32>(indexCount));-- DrawCommand cmd{};- cmd.sort_key = DrawCommand::buildSortKey(- current_stage_, layer, batch_shader_id_, blend, 0, textureId, depth);- cmd.index_offset = iOff;- cmd.index_count = static_cast<u32>(indexCount);- cmd.vertex_byte_offset = vOff;- cmd.shader_id = batch_shader_id_;- cmd.blend_mode = blend;- cmd.layout_id = LayoutId::Batch;- cmd.texture_count = 1;- cmd.texture_ids[0] = textureId;- cmd.entity = entity;- cmd.type = RenderType::Spine;- cmd.layer = layer;-- clip_state_.applyTo(entity, cmd);- draw_list_.push(cmd);+ pushBatchCommand(pool_, draw_list_, clip_state_, vOff, indices,+ static_cast<u32>(indexCount), BatchDrawKey{+ .stage = current_stage_,+ .layer = layer,+ .shaderId = batch_shader_id_,+ .blend = blend,+ .textureId = textureId,+ .depth = depth,+ .entity = entity,+ .type = RenderType::Spine,+ });}#endifdiff --git a/src/esengine/renderer/plugins/BatchPlugin.cpp b/src/esengine/renderer/plugins/BatchPlugin.cppnew file mode 100644index 000000000..43615ea10--- /dev/null+++ b/src/esengine/renderer/plugins/BatchPlugin.cpp@@ -0,0 +1,115 @@+/**+ * @file BatchPlugin.cpp+ * @brief Shared quad / nine-slice geometry construction for batch render-type plugins.+ *+ * @author ESEngine Team+ * @date 2026+ *+ * @copyright Copyright (c) 2026 ESEngine Team+ * Licensed under the MIT License.+ */+#include "BatchPlugin.hpp"++#include <cmath>++namespace esengine {++void BatchPlugin::emitQuad(+ TransientBufferPool& buffers, DrawList& draw_list, const ClipState& clips,+ const glm::vec2& position, const glm::vec2& size, const glm::vec2& pivotNorm,+ f32 angle, const glm::vec2& uvOffset, const glm::vec2& uvScale,+ const glm::vec4& color, const BatchDrawKey& key+) {+ f32 ox = 0.5f - pivotNorm.x;+ f32 oy = 0.5f - pivotNorm.y;+ const u32 packedColor = packColor(color); // constant across the 4 verts++ BatchVertex verts[4];+ if (std::abs(angle) > 0.001f) {+ f32 cosA = std::cos(angle);+ f32 sinA = std::sin(angle);+ for (u32 i = 0; i < 4; ++i) {+ f32 lx = (BATCH_QUAD_POSITIONS[i].x + ox) * size.x;+ f32 ly = (BATCH_QUAD_POSITIONS[i].y + oy) * size.y;+ verts[i].position = glm::vec2(+ position.x + lx * cosA - ly * sinA,+ position.y + lx * sinA + ly * cosA+ );+ verts[i].color = packedColor;+ verts[i].texCoord = BATCH_QUAD_TEX_COORDS[i] * uvScale + uvOffset;+ }+ } else {+ for (u32 i = 0; i < 4; ++i) {+ verts[i].position = glm::vec2(+ position.x + (BATCH_QUAD_POSITIONS[i].x + ox) * size.x,+ position.y + (BATCH_QUAD_POSITIONS[i].y + oy) * size.y+ );+ verts[i].color = packedColor;+ verts[i].texCoord = BATCH_QUAD_TEX_COORDS[i] * uvScale + uvOffset;+ }+ }++ appendQuad(buffers, draw_list, clips, verts, key);+}++void BatchPlugin::emitNineSlice(+ TransientBufferPool& buffers, DrawList& draw_list, const ClipState& clips,+ const glm::vec2& position, const glm::vec2& size, const glm::vec2& pivotNorm,+ f32 angle, const glm::vec2& texSize, const glm::vec4& border,+ const glm::vec2& uvOffset, const glm::vec2& uvScale,+ const glm::vec4& color, const BatchDrawKey& key+) {+ f32 L = border.x;+ f32 R = border.y;+ f32 T = border.z;+ f32 B = border.w;++ f32 baseX = position.x - size.x * pivotNorm.x;+ f32 baseY = position.y - size.y * pivotNorm.y;++ f32 x[4] = { baseX, baseX + L, baseX + size.x - R, baseX + size.x };+ f32 y[4] = { baseY, baseY + B, baseY + size.y - T, baseY + size.y };++ f32 u[4], v[4];+ if (texSize.x > 0.0f && texSize.y > 0.0f) {+ u[0] = uvOffset.x;+ u[1] = uvOffset.x + L / texSize.x;+ u[2] = uvOffset.x + uvScale.x - R / texSize.x;+ u[3] = uvOffset.x + uvScale.x;+ v[0] = uvOffset.y;+ v[1] = uvOffset.y + B / texSize.y;+ v[2] = uvOffset.y + uvScale.y - T / texSize.y;+ v[3] = uvOffset.y + uvScale.y;+ } else {+ u[0] = uvOffset.x;+ u[1] = uvOffset.x;+ u[2] = uvOffset.x + uvScale.x;+ u[3] = uvOffset.x + uvScale.x;+ v[0] = uvOffset.y;+ v[1] = uvOffset.y;+ v[2] = uvOffset.y + uvScale.y;+ v[3] = uvOffset.y + uvScale.y;+ }++ f32 cosA = std::cos(angle);+ f32 sinA = std::sin(angle);+ const u32 pc = packColor(color);++ for (i32 row = 0; row < 3; ++row) {+ for (i32 col = 0; col < 3; ++col) {+ f32 pw = x[col + 1] - x[col];+ f32 ph = y[row + 1] - y[row];+ if (pw <= 0.0f || ph <= 0.0f) continue;++ BatchVertex verts[4];+ verts[0] = { rotatePoint(position, x[col], y[row], cosA, sinA), pc, {u[col], v[row]} };+ verts[1] = { rotatePoint(position, x[col + 1], y[row], cosA, sinA), pc, {u[col + 1], v[row]} };+ verts[2] = { rotatePoint(position, x[col + 1], y[row + 1], cosA, sinA), pc, {u[col + 1], v[row + 1]} };+ verts[3] = { rotatePoint(position, x[col], y[row + 1], cosA, sinA), pc, {u[col], v[row + 1]} };++ appendQuad(buffers, draw_list, clips, verts, key);+ }+ }+}++} // namespace esenginediff --git a/src/esengine/renderer/plugins/BatchPlugin.hpp b/src/esengine/renderer/plugins/BatchPlugin.hppnew file mode 100644index 000000000..ca1402a87--- /dev/null+++ b/src/esengine/renderer/plugins/BatchPlugin.hpp@@ -0,0 +1,57 @@+/**+ * @file BatchPlugin.hpp+ * @brief Base class for render-type plugins that emit textured quads into the Batch stream.+ * @details Sprite and UI rendering build the same rotated/unrotated quad and the same 3x3+ * nine-slice grid; only their pivot convention, border source type, and RenderType+ * differ. This base owns the shared geometry construction (parameterized over those+ * differences) and the batch shader id, leaving each plugin only its ECS query and+ * per-entity attribute resolution. Submission goes through BatchBuilder, so there is+ * one quad-emitting path for the renderer keystone to target (docs/REARCH_RENDER.md P0).+ *+ * @author ESEngine Team+ * @date 2026+ *+ * @copyright Copyright (c) 2026 ESEngine Team+ * Licensed under the MIT License.+ */+#pragma once++#include "../RenderTypePlugin.hpp"+#include "../BatchBuilder.hpp"+#include "../BatchVertex.hpp"++#include <glm/glm.hpp>++namespace esengine {++class BatchPlugin : public RenderTypePlugin {+public:+ void init(RenderFrameContext& ctx) override { batch_shader_id_ = ctx.batch_shader_id; }+ void shutdown() override {}++protected:+ /**+ * @brief Emits a single rotated/unrotated textured quad.+ * @param pivotNorm Pivot in [0,1] within the quad; (0.5,0.5) keeps it centered on @p position.+ * @param key Non-geometry attributes (stage/layer/shader/blend/texture/depth/entity/type).+ */+ void emitQuad(TransientBufferPool& buffers, DrawList& draw_list, const ClipState& clips,+ const glm::vec2& position, const glm::vec2& size, const glm::vec2& pivotNorm,+ f32 angle, const glm::vec2& uvOffset, const glm::vec2& uvScale,+ const glm::vec4& color, const BatchDrawKey& key);++ /**+ * @brief Emits a 3x3 nine-slice grid of quads sharing one BatchDrawKey.+ * @param border Slice insets as (left, right, top, bottom) in pixels.+ * @param texSize Texture dimensions; if either axis is <= 0 the slice UVs collapse to the outer rect.+ */+ void emitNineSlice(TransientBufferPool& buffers, DrawList& draw_list, const ClipState& clips,+ const glm::vec2& position, const glm::vec2& size, const glm::vec2& pivotNorm,+ f32 angle, const glm::vec2& texSize, const glm::vec4& border,+ const glm::vec2& uvOffset, const glm::vec2& uvScale,+ const glm::vec4& color, const BatchDrawKey& key);++ u32 batch_shader_id_ = 0;+};++} // namespace esenginediff --git a/src/esengine/renderer/plugins/ParticlePlugin.cpp b/src/esengine/renderer/plugins/ParticlePlugin.cppindex 0cddef351..f7e117191 100644--- a/src/esengine/renderer/plugins/ParticlePlugin.cpp+++ b/src/esengine/renderer/plugins/ParticlePlugin.cpp@@ -1,4 +1,5 @@#include "ParticlePlugin.hpp"+#include "../BatchBuilder.hpp"#include "../RenderContext.hpp"#include "../Texture.hpp"#include "../BatchVertex.hpp"@@ -11,8 +12,6 @@namespace esengine {-static constexpr u32 QUAD_INDICES[6] = { 0, 1, 2, 0, 2, 3 };-void ParticlePlugin::init(RenderFrameContext& ctx) {batch_shader_id_ = ctx.batch_shader_id;}@@ -141,31 +140,23 @@ void ParticlePlugin::collect(RenderCollectContext& collect_ctx) {// Patch base vertex into indicesu32 patched[6];for (u32 q = 0; q < 6; ++q) {- patched[q] = bv + QUAD_INDICES[q];+ patched[q] = bv + BATCH_QUAD_INDICES[q];}buffers.writeIndices(LayoutId::Batch, idxOffset + ii, patched, 6);++pi;});- DrawCommand cmd{};- cmd.sort_key = DrawCommand::buildSortKey(ctx.current_stage, emitter.layer, batch_shader_id_, blendMode, 0, textureId, emitterWorldPos.z);- cmd.index_offset = idxOffset;- cmd.index_count = idxCount;- cmd.vertex_byte_offset = vertByteOffset;- cmd.shader_id = batch_shader_id_;- cmd.blend_mode = blendMode;- cmd.layout_id = LayoutId::Batch;- cmd.texture_count = 1;- cmd.texture_ids[0] = textureId;- cmd.entity = entity;- cmd.entity_count = 1;- cmd.type = RenderType::Particle;- cmd.layer = emitter.layer;-- clips.applyTo(entity, cmd);-- draw_list.push(cmd);+ pushBatchDraw(draw_list, clips, vertByteOffset, idxOffset, idxCount, BatchDrawKey{+ .stage = ctx.current_stage,+ .layer = emitter.layer,+ .shaderId = batch_shader_id_,+ .blend = blendMode,+ .textureId = textureId,+ .depth = emitterWorldPos.z,+ .entity = entity,+ .type = RenderType::Particle,+ });}}diff --git a/src/esengine/renderer/plugins/SpritePlugin.cpp b/src/esengine/renderer/plugins/SpritePlugin.cppindex 03e640cd3..0189ecd6a 100644--- a/src/esengine/renderer/plugins/SpritePlugin.cpp+++ b/src/esengine/renderer/plugins/SpritePlugin.cpp@@ -10,10 +10,6 @@namespace esengine {-void SpritePlugin::init(RenderFrameContext& ctx) {- batch_shader_id_ = ctx.batch_shader_id;-}-void SpritePlugin::collect(RenderCollectContext& collect_ctx) {auto& registry = collect_ctx.registry;auto& frustum = collect_ctx.frustum;@@ -80,211 +76,46 @@ void SpritePlugin::collect(RenderCollectContext& collect_ctx) {bool hasTiling = sprite.tileSize.x > 0.0f && sprite.tileSize.y > 0.0f;u32 shaderId = (sprite.material != 0) ? sprite.material : batch_shader_id_;- BlendMode blend = BlendMode::Normal;++ BatchDrawKey key{+ .stage = ctx.current_stage,+ .layer = sprite.layer,+ .shaderId = shaderId,+ .blend = BlendMode::Normal,+ .textureId = textureId,+ .depth = position.z,+ .entity = entity,+ .type = RenderType::Sprite,+ };if (hasTiling) {- emitTiledQuads(buffers, draw_list,+ emitTiledQuads(buffers, draw_list, clips,glm::vec2(position), finalSize, sprite.pivot,- angle, position.z,- textureId, sprite.color, uvOff, uvSc,+ angle, sprite.color, uvOff, uvSc,sprite.tileSize * glm::vec2(scale),sprite.tileSpacing * glm::vec2(scale),- entity, ctx.current_stage, sprite.layer,- blend, shaderId, clips);+ key);} else if (useNineSlice) {- emitNineSlice(buffers, draw_list,+ emitNineSlice(buffers, draw_list, clips,glm::vec2(position), finalSize, sprite.pivot,- angle, position.z,- textureId, texSize, sliceBorder,- sprite.color, uvOff, uvSc,- entity, ctx.current_stage, sprite.layer,- blend, shaderId, clips);+ angle, texSize,+ glm::vec4(sliceBorder.left, sliceBorder.right, sliceBorder.top, sliceBorder.bottom),+ uvOff, uvSc, sprite.color, key);} else {- emitQuad(buffers, draw_list,+ emitQuad(buffers, draw_list, clips,glm::vec2(position), finalSize, sprite.pivot,- angle, position.z,- textureId, sprite.color, uvOff, uvSc,- entity, ctx.current_stage, sprite.layer,- blend, shaderId, clips);- }- }-}--void SpritePlugin::emitQuad(- TransientBufferPool& buffers, DrawList& draw_list,- const glm::vec2& position, const glm::vec2& size,- const glm::vec2& pivot,- f32 angle, f32 depth, u32 textureId,- const glm::vec4& color,- const glm::vec2& uvOffset, const glm::vec2& uvScale,- Entity entity, RenderStage stage, i32 layer,- BlendMode blend, u32 shaderId,- const ClipState& clips-) {- BatchVertex verts[4];- f32 ox = 0.5f - pivot.x;- f32 oy = 0.5f - pivot.y;- const u32 packedColor = packColor(color); // color is constant across the 4 verts-- if (std::abs(angle) > 0.001f) {- f32 cosA = std::cos(angle);- f32 sinA = std::sin(angle);- for (u32 i = 0; i < 4; ++i) {- f32 lx = (QUAD_POSITIONS[i].x + ox) * size.x;- f32 ly = (QUAD_POSITIONS[i].y + oy) * size.y;- verts[i].position = glm::vec2(- position.x + lx * cosA - ly * sinA,- position.y + lx * sinA + ly * cosA- );- verts[i].color = packedColor;- verts[i].texCoord = QUAD_TEX_COORDS[i] * uvScale + uvOffset;- }- } else {- for (u32 i = 0; i < 4; ++i) {- verts[i].position = glm::vec2(- position.x + (QUAD_POSITIONS[i].x + ox) * size.x,- position.y + (QUAD_POSITIONS[i].y + oy) * size.y- );- verts[i].color = packedColor;- verts[i].texCoord = QUAD_TEX_COORDS[i] * uvScale + uvOffset;- }- }-- u32 vOff = buffers.appendVertices(LayoutId::Batch, verts, sizeof(verts));- u32 baseVertex = vOff / sizeof(BatchVertex);-- u32 indices[6];- for (u32 i = 0; i < 6; ++i) {- indices[i] = static_cast<u32>(baseVertex + QUAD_INDICES[i]);- }- u32 iOff = buffers.appendIndices(LayoutId::Batch, indices, 6);-- DrawCommand cmd{};- cmd.sort_key = DrawCommand::buildSortKey(stage, layer, shaderId, blend, 0, textureId, depth);- cmd.index_offset = iOff;- cmd.index_count = 6;- cmd.vertex_byte_offset = vOff;- cmd.shader_id = shaderId;- cmd.blend_mode = blend;- cmd.layout_id = LayoutId::Batch;- cmd.texture_count = 1;- cmd.texture_ids[0] = textureId;- cmd.entity = entity;- cmd.type = RenderType::Sprite;- cmd.layer = layer;-- clips.applyTo(entity, cmd);-- draw_list.push(cmd);-}--void SpritePlugin::emitNineSlice(- TransientBufferPool& buffers, DrawList& draw_list,- const glm::vec2& position, const glm::vec2& size,- const glm::vec2& pivot,- f32 angle, f32 depth, u32 textureId,- const glm::vec2& texSize, const resource::SliceBorder& border,- const glm::vec4& color,- const glm::vec2& uvOffset, const glm::vec2& uvScale,- Entity entity, RenderStage stage, i32 layer,- BlendMode blend, u32 shaderId,- const ClipState& clips-) {- f32 L = border.left;- f32 R = border.right;- f32 T = border.top;- f32 B = border.bottom;-- f32 baseX = position.x - size.x * pivot.x;- f32 baseY = position.y - size.y * pivot.y;-- f32 x[4] = { baseX, baseX + L, baseX + size.x - R, baseX + size.x };- f32 y[4] = { baseY, baseY + B, baseY + size.y - T, baseY + size.y };-- f32 u[4] = {- uvOffset.x,- uvOffset.x + L / texSize.x,- uvOffset.x + uvScale.x - R / texSize.x,- uvOffset.x + uvScale.x- };- f32 v[4] = {- uvOffset.y,- uvOffset.y + B / texSize.y,- uvOffset.y + uvScale.y - T / texSize.y,- uvOffset.y + uvScale.y- };-- f32 cosA = std::cos(angle);- f32 sinA = std::sin(angle);-- auto rotatePoint = [&](f32 px, f32 py) -> glm::vec2 {- f32 dx = px - position.x;- f32 dy = py - position.y;- return glm::vec2(- position.x + dx * cosA - dy * sinA,- position.y + dx * sinA + dy * cosA- );- };-- for (i32 row = 0; row < 3; ++row) {- for (i32 col = 0; col < 3; ++col) {- f32 pw = x[col + 1] - x[col];- f32 ph = y[row + 1] - y[row];- if (pw <= 0.0f || ph <= 0.0f) continue;-- BatchVertex verts[4];- glm::vec2 p0 = rotatePoint(x[col], y[row]);- glm::vec2 p1 = rotatePoint(x[col + 1], y[row]);- glm::vec2 p2 = rotatePoint(x[col + 1], y[row + 1]);- glm::vec2 p3 = rotatePoint(x[col], y[row + 1]);-- u32 pc = packColor(color);- verts[0] = { p0, pc, {u[col], v[row]} };- verts[1] = { p1, pc, {u[col + 1], v[row]} };- verts[2] = { p2, pc, {u[col + 1], v[row + 1]} };- verts[3] = { p3, pc, {u[col], v[row + 1]} };-- u32 vOff = buffers.appendVertices(LayoutId::Batch, verts, sizeof(verts));- u32 baseVert = vOff / sizeof(BatchVertex);-- u32 indices[6];- for (u32 i = 0; i < 6; ++i) {- indices[i] = static_cast<u32>(baseVert + QUAD_INDICES[i]);- }- u32 iOff = buffers.appendIndices(LayoutId::Batch, indices, 6);-- DrawCommand cmd{};- cmd.sort_key = DrawCommand::buildSortKey(stage, layer, shaderId, blend, 0, textureId, depth);- cmd.index_offset = iOff;- cmd.index_count = 6;- cmd.vertex_byte_offset = vOff;- cmd.shader_id = shaderId;- cmd.blend_mode = blend;- cmd.layout_id = LayoutId::Batch;- cmd.texture_count = 1;- cmd.texture_ids[0] = textureId;- cmd.entity = entity;- cmd.type = RenderType::Sprite;- cmd.layer = layer;-- clips.applyTo(entity, cmd);-- draw_list.push(cmd);+ angle, uvOff, uvSc, sprite.color, key);}}}void SpritePlugin::emitTiledQuads(- TransientBufferPool& buffers, DrawList& draw_list,- const glm::vec2& position, const glm::vec2& size,- const glm::vec2& pivot,- f32 angle, f32 depth, u32 textureId,- const glm::vec4& color,+ TransientBufferPool& buffers, DrawList& draw_list, const ClipState& clips,+ const glm::vec2& position, const glm::vec2& size, const glm::vec2& pivot,+ f32 angle, const glm::vec4& color,const glm::vec2& uvOffset, const glm::vec2& uvScale,const glm::vec2& tileSize, const glm::vec2& tileSpacing,- Entity entity, RenderStage stage, i32 layer,- BlendMode blend, u32 shaderId,- const ClipState& clips+ const BatchDrawKey& key) {glm::vec2 step = tileSize + tileSpacing;if (step.x <= 0.0f || step.y <= 0.0f) return;@@ -295,36 +126,14 @@ void SpritePlugin::emitTiledQuads(f32 baseY = position.y - absSize.y * pivot.y;bool hasRotation = std::abs(angle) > 0.001f;- f32 cosA = 0.0f, sinA = 0.0f;+ f32 cosA = 1.0f, sinA = 0.0f;if (hasRotation) {cosA = std::cos(angle);sinA = std::sin(angle);}- auto rotatePoint = [&](f32 px, f32 py) -> glm::vec2 {- f32 dx = px - position.x;- f32 dy = py - position.y;- return glm::vec2(- position.x + dx * cosA - dy * sinA,- position.y + dx * sinA + dy * cosA- );- };-u32 pc = packColor(color);- DrawCommand tmpl{};- tmpl.sort_key = DrawCommand::buildSortKey(stage, layer, shaderId, blend, 0, textureId, depth);- tmpl.index_count = 6;- tmpl.shader_id = shaderId;- tmpl.blend_mode = blend;- tmpl.layout_id = LayoutId::Batch;- tmpl.texture_count = 1;- tmpl.texture_ids[0] = textureId;- tmpl.entity = entity;- tmpl.type = RenderType::Sprite;- tmpl.layer = layer;- clips.applyTo(entity, tmpl);-i32 tilesY = static_cast<i32>(std::ceil(absSize.y / step.y));i32 tilesX = static_cast<i32>(std::ceil(absSize.x / step.x));@@ -349,30 +158,18 @@ void SpritePlugin::emitTiledQuads(BatchVertex verts[4];if (hasRotation) {- verts[0] = { rotatePoint(x0, y0), pc, { uvOffset.x, uvOffset.y } };- verts[1] = { rotatePoint(x1, y0), pc, { uvOffset.x + tileUvScale.x, uvOffset.y } };- verts[2] = { rotatePoint(x1, y1), pc, { uvOffset.x + tileUvScale.x, uvOffset.y + tileUvScale.y } };- verts[3] = { rotatePoint(x0, y1), pc, { uvOffset.x, uvOffset.y + tileUvScale.y } };+ verts[0] = { rotatePoint(position, x0, y0, cosA, sinA), pc, { uvOffset.x, uvOffset.y } };+ verts[1] = { rotatePoint(position, x1, y0, cosA, sinA), pc, { uvOffset.x + tileUvScale.x, uvOffset.y } };+ verts[2] = { rotatePoint(position, x1, y1, cosA, sinA), pc, { uvOffset.x + tileUvScale.x, uvOffset.y + tileUvScale.y } };+ verts[3] = { rotatePoint(position, x0, y1, cosA, sinA), pc, { uvOffset.x, uvOffset.y + tileUvScale.y } };} else {- verts[0] = { { x0, y0 }, pc, { uvOffset.x, uvOffset.y } };+ verts[0] = { { x0, y0 }, pc, { uvOffset.x, uvOffset.y } };verts[1] = { { x1, y0 }, pc, { uvOffset.x + tileUvScale.x, uvOffset.y } };verts[2] = { { x1, y1 }, pc, { uvOffset.x + tileUvScale.x, uvOffset.y + tileUvScale.y } };- verts[3] = { { x0, y1 }, pc, { uvOffset.x, uvOffset.y + tileUvScale.y } };- }-- u32 vOff = buffers.appendVertices(LayoutId::Batch, verts, sizeof(verts));- u32 baseVert = vOff / sizeof(BatchVertex);-- u32 indices[6];- for (u32 i = 0; i < 6; ++i) {- indices[i] = static_cast<u32>(baseVert + QUAD_INDICES[i]);+ verts[3] = { { x0, y1 }, pc, { uvOffset.x, uvOffset.y + tileUvScale.y } };}- u32 iOff = buffers.appendIndices(LayoutId::Batch, indices, 6);- DrawCommand cmd = tmpl;- cmd.index_offset = iOff;- cmd.vertex_byte_offset = vOff;- draw_list.push(cmd);+ appendQuad(buffers, draw_list, clips, verts, key);}}}diff --git a/src/esengine/renderer/plugins/SpritePlugin.hpp b/src/esengine/renderer/plugins/SpritePlugin.hppindex 887a53b60..98ee69e41 100644--- a/src/esengine/renderer/plugins/SpritePlugin.hpp+++ b/src/esengine/renderer/plugins/SpritePlugin.hpp@@ -1,74 +1,25 @@#pragma once-#include "../RenderTypePlugin.hpp"-#include "../BatchVertex.hpp"+#include "BatchPlugin.hpp"#include "../../resource/TextureMetadata.hpp"namespace esengine {-class SpritePlugin : public RenderTypePlugin {+class SpritePlugin : public BatchPlugin {public:- void init(RenderFrameContext& ctx) override;- void shutdown() override {}-void collect(RenderCollectContext& ctx) override;private:- static constexpr glm::vec4 QUAD_POSITIONS[4] = {- { -0.5f, -0.5f, 0.0f, 1.0f },- { 0.5f, -0.5f, 0.0f, 1.0f },- { 0.5f, 0.5f, 0.0f, 1.0f },- { -0.5f, 0.5f, 0.0f, 1.0f }- };-- static constexpr glm::vec2 QUAD_TEX_COORDS[4] = {- { 0.0f, 0.0f },- { 1.0f, 0.0f },- { 1.0f, 1.0f },- { 0.0f, 1.0f }- };-- static constexpr u32 QUAD_INDICES[6] = { 0, 1, 2, 2, 3, 0 };-- void emitQuad(- TransientBufferPool& buffers, DrawList& draw_list,- const glm::vec2& position, const glm::vec2& size,- const glm::vec2& pivot,- f32 angle, f32 depth, u32 textureId,- const glm::vec4& color,- const glm::vec2& uvOffset, const glm::vec2& uvScale,- Entity entity, RenderStage stage, i32 layer,- BlendMode blend, u32 shaderId,- const ClipState& clips- );-- void emitNineSlice(- TransientBufferPool& buffers, DrawList& draw_list,- const glm::vec2& position, const glm::vec2& size,- const glm::vec2& pivot,- f32 angle, f32 depth, u32 textureId,- const glm::vec2& texSize, const resource::SliceBorder& border,- const glm::vec4& color,- const glm::vec2& uvOffset, const glm::vec2& uvScale,- Entity entity, RenderStage stage, i32 layer,- BlendMode blend, u32 shaderId,- const ClipState& clips- );-+ // Sprite-only: tiling repeats the texture across the quad. UI has no tiled variant,+ // so this stays here rather than in BatchPlugin. Emits one quad per tile via appendQuad.void emitTiledQuads(- TransientBufferPool& buffers, DrawList& draw_list,- const glm::vec2& position, const glm::vec2& size,- const glm::vec2& pivot,- f32 angle, f32 depth, u32 textureId,- const glm::vec4& color,+ TransientBufferPool& buffers, DrawList& draw_list, const ClipState& clips,+ const glm::vec2& position, const glm::vec2& size, const glm::vec2& pivot,+ f32 angle, const glm::vec4& color,const glm::vec2& uvOffset, const glm::vec2& uvScale,const glm::vec2& tileSize, const glm::vec2& tileSpacing,- Entity entity, RenderStage stage, i32 layer,- BlendMode blend, u32 shaderId,- const ClipState& clips+ const BatchDrawKey& key);-- u32 batch_shader_id_ = 0;};} // namespace esenginediff --git a/src/esengine/renderer/plugins/TextPlugin.cpp b/src/esengine/renderer/plugins/TextPlugin.cppindex 7c1c44bd0..0169b6bd3 100644--- a/src/esengine/renderer/plugins/TextPlugin.cpp+++ b/src/esengine/renderer/plugins/TextPlugin.cpp@@ -10,10 +10,6 @@namespace esengine {-void TextPlugin::init(RenderFrameContext& ctx) {- batch_shader_id_ = ctx.batch_shader_id;-}-u32 TextPlugin::decodeUtf8(const char* data, u16 length, u16& pos) {u8 b0 = static_cast<u8>(data[pos]);if (b0 < 0x80) {@@ -118,6 +114,19 @@ void TextPlugin::collect(RenderCollectContext& collect_ctx) {u16 textLen = static_cast<u16>(bt.text.size());u32 prevChar = 0;+ // Constant across every glyph of this text entity — build the draw key once.+ BatchDrawKey key{+ .stage = ctx.current_stage,+ .layer = bt.layer,+ .shaderId = batch_shader_id_,+ .blend = BlendMode::Normal,+ .textureId = textureId,+ .depth = position.z,+ .entity = entity,+ .type = RenderType::Text,+ };+ constexpr glm::vec2 CENTERED_PIVOT{0.5f, 0.5f};+for (u16 j = 0; j < textLen; ++j) {u32 charCode = decodeUtf8(textData, textLen, j);auto* glyph = font->getGlyph(charCode);@@ -139,11 +148,11 @@ void TextPlugin::collect(RenderCollectContext& collect_ctx) {glm::vec2 uvOffset(glyph->x / texW, uvY + uvH);glm::vec2 uvScale(glyph->width / texW, -uvH);- emitGlyphQuad(buffers, draw_list,- glm::vec2(posX, posY), glm::vec2(glyphW, glyphH),- position.z, textureId, bt.color, uvOffset, uvScale,- entity, ctx.current_stage, bt.layer,- batch_shader_id_, clips);+ // A glyph is an unrotated centered quad; the base emitter packs the+ // BATCH_QUAD_TEX_COORDS (0,0)->(1,1) exactly as the glyph UVs expect.+ emitQuad(buffers, draw_list, clips,+ glm::vec2(posX, posY), glm::vec2(glyphW, glyphH), CENTERED_PIVOT,+ 0.0f, uvOffset, uvScale, bt.color, key);}cursorX += (glyph->xAdvance + spacing) * fontScale;@@ -152,63 +161,4 @@ void TextPlugin::collect(RenderCollectContext& collect_ctx) {}}-void TextPlugin::emitGlyphQuad(- TransientBufferPool& buffers, DrawList& draw_list,- const glm::vec2& position, const glm::vec2& size,- f32 depth, u32 textureId,- const glm::vec4& color,- const glm::vec2& uvOffset, const glm::vec2& uvScale,- Entity entity, RenderStage stage, i32 layer,- u32 shaderId, const ClipState& clips-) {- BatchVertex verts[4];- f32 hw = size.x * 0.5f;- f32 hh = size.y * 0.5f;-- u32 pc = packColor(color);-- verts[0].position = glm::vec2(position.x - hw, position.y - hh);- verts[0].color = pc;- verts[0].texCoord = glm::vec2(0.0f, 0.0f) * uvScale + uvOffset;-- verts[1].position = glm::vec2(position.x + hw, position.y - hh);- verts[1].color = pc;- verts[1].texCoord = glm::vec2(1.0f, 0.0f) * uvScale + uvOffset;-- verts[2].position = glm::vec2(position.x + hw, position.y + hh);- verts[2].color = pc;- verts[2].texCoord = glm::vec2(1.0f, 1.0f) * uvScale + uvOffset;-- verts[3].position = glm::vec2(position.x - hw, position.y + hh);- verts[3].color = pc;- verts[3].texCoord = glm::vec2(0.0f, 1.0f) * uvScale + uvOffset;-- u32 vOff = buffers.appendVertices(LayoutId::Batch, verts, sizeof(verts));- u32 baseVertex = vOff / sizeof(BatchVertex);-- u32 indices[6];- for (u32 i = 0; i < 6; ++i) {- indices[i] = static_cast<u32>(baseVertex + QUAD_INDICES[i]);- }- u32 iOff = buffers.appendIndices(LayoutId::Batch, indices, 6);-- DrawCommand cmd{};- cmd.sort_key = DrawCommand::buildSortKey(stage, layer, shaderId, BlendMode::Normal, 0, textureId, depth);- cmd.index_offset = iOff;- cmd.index_count = 6;- cmd.vertex_byte_offset = vOff;- cmd.shader_id = shaderId;- cmd.blend_mode = BlendMode::Normal;- cmd.layout_id = LayoutId::Batch;- cmd.texture_count = 1;- cmd.texture_ids[0] = textureId;- cmd.entity = entity;- cmd.type = RenderType::Text;- cmd.layer = layer;-- clips.applyTo(entity, cmd);-- draw_list.push(cmd);-}-} // namespace esenginediff --git a/src/esengine/renderer/plugins/TextPlugin.hpp b/src/esengine/renderer/plugins/TextPlugin.hppindex aff3f8424..a0ac14548 100644--- a/src/esengine/renderer/plugins/TextPlugin.hpp+++ b/src/esengine/renderer/plugins/TextPlugin.hpp@@ -1,34 +1,16 @@#pragma once-#include "../RenderTypePlugin.hpp"-#include "../BatchVertex.hpp"+#include "BatchPlugin.hpp"namespace esengine {namespace text { class BitmapFont; }-class TextPlugin : public RenderTypePlugin {+class TextPlugin : public BatchPlugin {public:- void init(RenderFrameContext& ctx) override;- void shutdown() override {}-void collect(RenderCollectContext& ctx) override;private:- static constexpr u32 QUAD_INDICES[6] = { 0, 1, 2, 2, 3, 0 };-static u32 decodeUtf8(const char* data, u16 length, u16& pos);-- void emitGlyphQuad(- TransientBufferPool& buffers, DrawList& draw_list,- const glm::vec2& position, const glm::vec2& size,- f32 depth, u32 textureId,- const glm::vec4& color,- const glm::vec2& uvOffset, const glm::vec2& uvScale,- Entity entity, RenderStage stage, i32 layer,- u32 shaderId, const ClipState& clips- );-- u32 batch_shader_id_ = 0;};} // namespace esenginediff --git a/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp b/src/esengine/renderer/plugins/TilemapRenderPlugin.cppindex 1c8ffdb25..54558c3a7 100644--- a/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp+++ b/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp@@ -1,4 +1,5 @@#include "TilemapRenderPlugin.hpp"+#include "../BatchBuilder.hpp"#include "../../tilemap/TilemapSystem.hpp"#include "../../ecs/components/Transform.hpp"@@ -8,8 +9,6 @@namespace esengine {-static constexpr u32 QUAD_IDX[6] = { 0, 1, 2, 2, 3, 0 };-void TilemapRenderPlugin::init(RenderFrameContext& ctx) {batch_shader_id_ = ctx.batch_shader_id;}@@ -84,7 +83,7 @@ void TilemapRenderPlugin::rebuildChunk(cache.vertices.push_back({ {worldX - hw, worldY + hh}, packedColor, {u0, v0 + sv} });for (u32 i = 0; i < 6; ++i) {- cache.indices.push_back(baseVertex + QUAD_IDX[i]);+ cache.indices.push_back(baseVertex + BATCH_QUAD_INDICES[i]);}}}@@ -189,34 +188,21 @@ void TilemapRenderPlugin::collect(RenderCollectContext& collect_ctx) {if (indices_.empty()) continue;- u32 vBytes = static_cast<u32>(vertices_.size()) * sizeof(BatchVertex);- u32 vOff = buffers.appendVertices(LayoutId::Batch, vertices_.data(), vBytes);- u32 baseVertex = vOff / sizeof(BatchVertex);-- for (auto& idx : indices_) {- idx = static_cast<u32>(idx + baseVertex);- }- u32 iOff = buffers.appendIndices(LayoutId::Batch, indices_.data(), static_cast<u32>(indices_.size()));-- DrawCommand cmd{};- cmd.sort_key = DrawCommand::buildSortKey(- ctx.current_stage, layer.sort_layer, batch_shader_id_,- BlendMode::Normal, 0, glTextureId, layer.depth);- cmd.index_offset = iOff;- cmd.index_count = static_cast<u32>(indices_.size());- cmd.vertex_byte_offset = vOff;- cmd.shader_id = batch_shader_id_;- cmd.blend_mode = BlendMode::Normal;- cmd.layout_id = LayoutId::Batch;- cmd.texture_count = 1;- cmd.texture_ids[0] = glTextureId;- cmd.entity = entity;- cmd.type = RenderType::Sprite;- cmd.layer = layer.sort_layer;-- clips.applyTo(entity, cmd);-- draw_list.push(cmd);+ // indices_ are 0-based within the merged vertices_; appendIndexedBatch rebases them+ // onto the pool's baseVertex and assembles the command.+ appendIndexedBatch(buffers, draw_list, clips,+ vertices_.data(), static_cast<u32>(vertices_.size()),+ indices_.data(), static_cast<u32>(indices_.size()),+ BatchDrawKey{+ .stage = ctx.current_stage,+ .layer = layer.sort_layer,+ .shaderId = batch_shader_id_,+ .blend = BlendMode::Normal,+ .textureId = glTextureId,+ .depth = layer.depth,+ .entity = entity,+ .type = RenderType::Sprite,+ });}for (auto it = layer_caches_.begin(); it != layer_caches_.end(); ) {diff --git a/src/esengine/renderer/plugins/UIElementPlugin.cpp b/src/esengine/renderer/plugins/UIElementPlugin.cppindex 6f4ded5ba..1d3320bc1 100644--- a/src/esengine/renderer/plugins/UIElementPlugin.cpp+++ b/src/esengine/renderer/plugins/UIElementPlugin.cpp@@ -10,10 +10,6 @@namespace esengine {-void UIElementPlugin::init(RenderFrameContext& ctx) {- batch_shader_id_ = ctx.batch_shader_id;-}-void UIElementPlugin::collect(RenderCollectContext& collect_ctx) {auto& registry = collect_ctx.registry;auto& frustum = collect_ctx.frustum;@@ -39,6 +35,7 @@ void UIElementPlugin::collect(RenderCollectContext& collect_ctx) {f32 h = rect.computed_size_.y;if (w <= 0.0f && h <= 0.0f) continue;+ // UI bakes the pivot into the world position here, so the quad is emitted centered.f32 dx = (0.5f - rect.pivot.x) * w * scale.x;f32 dy = (0.5f - rect.pivot.y) * h * scale.y;f32 sinHalf = rotation.z;@@ -94,191 +91,29 @@ void UIElementPlugin::collect(RenderCollectContext& collect_ctx) {}glm::vec2 finalSize = rect.computed_size_ * glm::vec2(scale);- u32 shaderId = batch_shader_id_;- BlendMode blend = BlendMode::Normal;-- if (useNineSlice) {- emitNineSlice(buffers, draw_list,- glm::vec2(position), finalSize, angle, position.z,- textureId, texSize, sliceBorder,- renderer.color, renderer.uvOffset, renderer.uvScale,- entity, ctx.current_stage, layer,- blend, shaderId, clips);- } else {- emitQuad(buffers, draw_list,- glm::vec2(position), finalSize, angle, position.z,- textureId, renderer.color, renderer.uvOffset, renderer.uvScale,- entity, ctx.current_stage, layer,- blend, shaderId, clips);- }- }--}--void UIElementPlugin::emitQuad(- TransientBufferPool& buffers, DrawList& draw_list,- const glm::vec2& position, const glm::vec2& size,- f32 angle, f32 depth, u32 textureId,- const glm::vec4& color,- const glm::vec2& uvOffset, const glm::vec2& uvScale,- Entity entity, RenderStage stage, i32 layer,- BlendMode blend, u32 shaderId,- const ClipState& clips-) {- BatchVertex verts[4];- const u32 packedColor = packColor(color); // color is constant across the 4 verts-- if (std::abs(angle) > 0.001f) {- f32 cosA = std::cos(angle);- f32 sinA = std::sin(angle);- for (u32 i = 0; i < 4; ++i) {- f32 lx = QUAD_POSITIONS[i].x * size.x;- f32 ly = QUAD_POSITIONS[i].y * size.y;- verts[i].position = glm::vec2(- position.x + lx * cosA - ly * sinA,- position.y + lx * sinA + ly * cosA- );- verts[i].color = packedColor;- verts[i].texCoord = QUAD_TEX_COORDS[i] * uvScale + uvOffset;- }- } else {- for (u32 i = 0; i < 4; ++i) {- verts[i].position = glm::vec2(- position.x + QUAD_POSITIONS[i].x * size.x,- position.y + QUAD_POSITIONS[i].y * size.y- );- verts[i].color = packedColor;- verts[i].texCoord = QUAD_TEX_COORDS[i] * uvScale + uvOffset;- }- }-- u32 vOff = buffers.appendVertices(LayoutId::Batch, verts, sizeof(verts));- u32 baseVertex = vOff / sizeof(BatchVertex);-- u32 indices[6];- for (u32 i = 0; i < 6; ++i) {- indices[i] = static_cast<u32>(baseVertex + QUAD_INDICES[i]);- }- u32 iOff = buffers.appendIndices(LayoutId::Batch, indices, 6);-- DrawCommand cmd{};- cmd.sort_key = DrawCommand::buildSortKey(stage, layer, shaderId, blend, 0, textureId, depth);- cmd.index_offset = iOff;- cmd.index_count = 6;- cmd.vertex_byte_offset = vOff;- cmd.shader_id = shaderId;- cmd.blend_mode = blend;- cmd.layout_id = LayoutId::Batch;- cmd.texture_count = 1;- cmd.texture_ids[0] = textureId;- cmd.entity = entity;- cmd.type = RenderType::UIElement;- cmd.layer = layer;- clips.applyTo(entity, cmd);+ BatchDrawKey key{+ .stage = ctx.current_stage,+ .layer = layer,+ .shaderId = batch_shader_id_,+ .blend = BlendMode::Normal,+ .textureId = textureId,+ .depth = position.z,+ .entity = entity,+ .type = RenderType::UIElement,+ };- draw_list.push(cmd);-}--void UIElementPlugin::emitNineSlice(- TransientBufferPool& buffers, DrawList& draw_list,- const glm::vec2& position, const glm::vec2& size,- f32 angle, f32 depth, u32 textureId,- const glm::vec2& texSize, const glm::vec4& sliceBorder,- const glm::vec4& color,- const glm::vec2& uvOffset, const glm::vec2& uvScale,- Entity entity, RenderStage stage, i32 layer,- BlendMode blend, u32 shaderId,- const ClipState& clips-) {- f32 L = sliceBorder.x;- f32 R = sliceBorder.y;- f32 T = sliceBorder.z;- f32 B = sliceBorder.w;-- f32 baseX = position.x - size.x * 0.5f;- f32 baseY = position.y - size.y * 0.5f;-- f32 x[4] = { baseX, baseX + L, baseX + size.x - R, baseX + size.x };- f32 y[4] = { baseY, baseY + B, baseY + size.y - T, baseY + size.y };-- f32 u[4], v[4];- if (texSize.x > 0.0f && texSize.y > 0.0f) {- u[0] = uvOffset.x;- u[1] = uvOffset.x + L / texSize.x;- u[2] = uvOffset.x + uvScale.x - R / texSize.x;- u[3] = uvOffset.x + uvScale.x;- v[0] = uvOffset.y;- v[1] = uvOffset.y + B / texSize.y;- v[2] = uvOffset.y + uvScale.y - T / texSize.y;- v[3] = uvOffset.y + uvScale.y;- } else {- u[0] = uvOffset.x;- u[1] = uvOffset.x;- u[2] = uvOffset.x + uvScale.x;- u[3] = uvOffset.x + uvScale.x;- v[0] = uvOffset.y;- v[1] = uvOffset.y;- v[2] = uvOffset.y + uvScale.y;- v[3] = uvOffset.y + uvScale.y;- }-- f32 cosA = std::cos(angle);- f32 sinA = std::sin(angle);-- auto rotatePoint = [&](f32 px, f32 py) -> glm::vec2 {- f32 ddx = px - position.x;- f32 ddy = py - position.y;- return glm::vec2(- position.x + ddx * cosA - ddy * sinA,- position.y + ddx * sinA + ddy * cosA- );- };-- for (i32 row = 0; row < 3; ++row) {- for (i32 col = 0; col < 3; ++col) {- f32 pw = x[col + 1] - x[col];- f32 ph = y[row + 1] - y[row];- if (pw <= 0.0f || ph <= 0.0f) continue;+ constexpr glm::vec2 CENTERED_PIVOT{0.5f, 0.5f};- BatchVertex verts[4];- glm::vec2 p0 = rotatePoint(x[col], y[row]);- glm::vec2 p1 = rotatePoint(x[col + 1], y[row]);- glm::vec2 p2 = rotatePoint(x[col + 1], y[row + 1]);- glm::vec2 p3 = rotatePoint(x[col], y[row + 1]);-- u32 pc = packColor(color);- verts[0] = { p0, pc, {u[col], v[row]} };- verts[1] = { p1, pc, {u[col + 1], v[row]} };- verts[2] = { p2, pc, {u[col + 1], v[row + 1]} };- verts[3] = { p3, pc, {u[col], v[row + 1]} };-- u32 vOff = buffers.appendVertices(LayoutId::Batch, verts, sizeof(verts));- u32 baseVert = vOff / sizeof(BatchVertex);-- u32 indices[6];- for (u32 i = 0; i < 6; ++i) {- indices[i] = static_cast<u32>(baseVert + QUAD_INDICES[i]);- }- u32 iOff = buffers.appendIndices(LayoutId::Batch, indices, 6);-- DrawCommand cmd{};- cmd.sort_key = DrawCommand::buildSortKey(stage, layer, shaderId, blend, 0, textureId, depth);- cmd.index_offset = iOff;- cmd.index_count = 6;- cmd.vertex_byte_offset = vOff;- cmd.shader_id = shaderId;- cmd.blend_mode = blend;- cmd.layout_id = LayoutId::Batch;- cmd.texture_count = 1;- cmd.texture_ids[0] = textureId;- cmd.entity = entity;- cmd.type = RenderType::UIElement;- cmd.layer = layer;-- clips.applyTo(entity, cmd);-- draw_list.push(cmd);+ if (useNineSlice) {+ emitNineSlice(buffers, draw_list, clips,+ glm::vec2(position), finalSize, CENTERED_PIVOT,+ angle, texSize, sliceBorder,+ renderer.uvOffset, renderer.uvScale, renderer.color, key);+ } else {+ emitQuad(buffers, draw_list, clips,+ glm::vec2(position), finalSize, CENTERED_PIVOT,+ angle, renderer.uvOffset, renderer.uvScale, renderer.color, key);}}}diff --git a/src/esengine/renderer/plugins/UIElementPlugin.hpp b/src/esengine/renderer/plugins/UIElementPlugin.hppindex eb368431a..f6ae23e8b 100644--- a/src/esengine/renderer/plugins/UIElementPlugin.hpp+++ b/src/esengine/renderer/plugins/UIElementPlugin.hpp@@ -1,61 +1,17 @@#pragma once-#include "../RenderTypePlugin.hpp"-#include "../BatchVertex.hpp"+#include "BatchPlugin.hpp"#include "../../resource/TextureMetadata.hpp"namespace esengine {-class UIElementPlugin : public RenderTypePlugin {+class UIElementPlugin : public BatchPlugin {public:- void init(RenderFrameContext& ctx) override;- void shutdown() override {}-void collect(RenderCollectContext& ctx) override;private:- static constexpr glm::vec4 QUAD_POSITIONS[4] = {- { -0.5f, -0.5f, 0.0f, 1.0f },- { 0.5f, -0.5f, 0.0f, 1.0f },- { 0.5f, 0.5f, 0.0f, 1.0f },- { -0.5f, 0.5f, 0.0f, 1.0f }- };-- static constexpr glm::vec2 QUAD_TEX_COORDS[4] = {- { 0.0f, 0.0f },- { 1.0f, 0.0f },- { 1.0f, 1.0f },- { 0.0f, 1.0f }- };-- static constexpr u32 QUAD_INDICES[6] = { 0, 1, 2, 2, 3, 0 };-+ // UI draws above world content: its sort layer is offset past the world layer range.static constexpr i32 UI_BASE_LAYER = 1000;-- void emitQuad(- TransientBufferPool& buffers, DrawList& draw_list,- const glm::vec2& position, const glm::vec2& size,- f32 angle, f32 depth, u32 textureId,- const glm::vec4& color,- const glm::vec2& uvOffset, const glm::vec2& uvScale,- Entity entity, RenderStage stage, i32 layer,- BlendMode blend, u32 shaderId,- const ClipState& clips- );-- void emitNineSlice(- TransientBufferPool& buffers, DrawList& draw_list,- const glm::vec2& position, const glm::vec2& size,- f32 angle, f32 depth, u32 textureId,- const glm::vec2& texSize, const glm::vec4& sliceBorder,- const glm::vec4& color,- const glm::vec2& uvOffset, const glm::vec2& uvScale,- Entity entity, RenderStage stage, i32 layer,- BlendMode blend, u32 shaderId,- const ClipState& clips- );-- u32 batch_shader_id_ = 0;};} // namespace esengine