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.

feat(render): multi-texture batch coalescing (8 samplers + per-vertex slot) — REARCH_RENDER RC7-2

esengine39c58d05ccd1
diff --git a/desktop/scripts/headless-verify.mjs b/desktop/scripts/headless-verify.mjs
index cc7acfab6..29aa39a48 100644
--- a/desktop/scripts/headless-verify.mjs
+++ b/desktop/scripts/headless-verify.mjs
@@ -107,7 +107,8 @@ app.whenReady().then(async () => {
const spread = (max[0] - min[0]) + (max[1] - min[1]) + (max[2] - min[2]);
return { w: c.width, h: c.height, totalPixels: px.length / 4, nonZeroPixels: nonZero, min, max, spread, rendered: spread > 16 };
})()`);
- finish({ ok: true, entityCount, capture }, server);
+ const drawCalls = await exec('window.__estellaHeadless.api.getStats().drawCalls');
+ finish({ ok: true, entityCount, drawCalls, capture }, server);
} catch (e) {
finish({ ok: false, error: String((e && e.stack) || e) }, server);
}
diff --git a/desktop/src/engine/EditorControlSurface.ts b/desktop/src/engine/EditorControlSurface.ts
index 40082ad39..3751f2efd 100644
--- a/desktop/src/engine/EditorControlSurface.ts
+++ b/desktop/src/engine/EditorControlSurface.ts
@@ -186,9 +186,12 @@ export class EditorControlSurfaceImpl {
return { rgba, width, height };
}
- /** Live counts for quick assertions (entity count is headless-friendly). */
- getStats(): { entities: number } {
- return { entities: EngineHost.world?.getAllEntities().length ?? 0 };
+ /** Live counts for quick assertions (entity count + last frame's merged draw calls). */
+ getStats(): { entities: number; drawCalls: number } {
+ return {
+ entities: EngineHost.world?.getAllEntities().length ?? 0,
+ drawCalls: EngineHost.module?.renderer_getDrawCalls?.() ?? 0,
+ };
}
/**
diff --git a/src/esengine/data/shaders/batch.esshader b/src/esengine/data/shaders/batch.esshader
index 9969ae161..8fde7644c 100644
--- a/src/esengine/data/shaders/batch.esshader
+++ b/src/esengine/data/shaders/batch.esshader
@@ -5,6 +5,7 @@
layout(location = 0) in vec2 a_position;
layout(location = 1) in vec4 a_color;
layout(location = 2) in vec2 a_texCoord;
+layout(location = 3) in float a_texIndex;
layout(std140) uniform FrameConstants {
mat4 u_projection;
@@ -12,11 +13,13 @@ layout(std140) uniform FrameConstants {
out vec4 v_color;
out vec2 v_texCoord;
+flat out int v_texIndex;
void main() {
gl_Position = u_projection * vec4(a_position, 0.0, 1.0);
v_color = a_color;
v_texCoord = a_texCoord;
+ v_texIndex = int(a_texIndex);
}
#pragma end
@@ -25,13 +28,25 @@ precision mediump float;
in vec4 v_color;
in vec2 v_texCoord;
+flat in int v_texIndex;
-uniform sampler2D u_texture;
+// Up to 8 textures bound per multi-texture batch. GLSL ES 3.00 forbids indexing a
+// sampler array with a non-uniform expression, so the slot is selected by a constant
+// branch chain (the standard WebGL2 multi-texture batching technique).
+uniform sampler2D u_textures[8];
out vec4 fragColor;
void main() {
- vec4 texColor = texture(u_texture, v_texCoord);
+ vec4 texColor;
+ if (v_texIndex == 0) texColor = texture(u_textures[0], v_texCoord);
+ else if (v_texIndex == 1) texColor = texture(u_textures[1], v_texCoord);
+ else if (v_texIndex == 2) texColor = texture(u_textures[2], v_texCoord);
+ else if (v_texIndex == 3) texColor = texture(u_textures[3], v_texCoord);
+ else if (v_texIndex == 4) texColor = texture(u_textures[4], v_texCoord);
+ else if (v_texIndex == 5) texColor = texture(u_textures[5], v_texCoord);
+ else if (v_texIndex == 6) texColor = texture(u_textures[6], v_texCoord);
+ else texColor = texture(u_textures[7], v_texCoord);
fragColor = texColor * v_color;
}
#pragma end
diff --git a/src/esengine/renderer/BatchBuilder.cpp b/src/esengine/renderer/BatchBuilder.cpp
index d977b4ca4..d31acc0fb 100644
--- a/src/esengine/renderer/BatchBuilder.cpp
+++ b/src/esengine/renderer/BatchBuilder.cpp
@@ -24,7 +24,8 @@ std::vector<u32> g_indexScratch;
template <typename IndexT>
void pushBatchCommandImpl(TransientBufferPool& pool, DrawList& drawList, const ClipState& clips,
- u32 vertexByteOffset, const IndexT* localIndices, u32 indexCount,
+ u32 vertexByteOffset, u32 vertexCount,
+ const IndexT* localIndices, u32 indexCount,
const BatchDrawKey& key) {
if (indexCount == 0) return;
@@ -35,22 +36,23 @@ void pushBatchCommandImpl(TransientBufferPool& pool, DrawList& drawList, const C
}
u32 indexOffset = pool.appendIndices(LayoutId::Batch, g_indexScratch.data(), indexCount);
- pushBatchDraw(drawList, clips, vertexByteOffset, indexOffset, indexCount, key);
+ pushBatchDraw(drawList, clips, vertexByteOffset, vertexCount, indexOffset, indexCount, key);
}
} // namespace
void pushBatchDraw(DrawList& drawList, const ClipState& clips,
- u32 vertexByteOffset, u32 indexOffset, u32 indexCount,
+ u32 vertexByteOffset, u32 vertexCount, 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);
+ key.blend, 0, key.depth);
cmd.index_offset = indexOffset;
cmd.index_count = indexCount;
cmd.vertex_byte_offset = vertexByteOffset;
+ cmd.vertex_count = vertexCount;
cmd.shader_id = key.shaderId;
cmd.blend_mode = key.blend;
cmd.layout_id = LayoutId::Batch;
@@ -65,15 +67,17 @@ void pushBatchDraw(DrawList& drawList, const ClipState& clips,
}
void pushBatchCommand(TransientBufferPool& pool, DrawList& drawList, const ClipState& clips,
- u32 vertexByteOffset, const u32* localIndices, u32 indexCount,
+ u32 vertexByteOffset, u32 vertexCount,
+ const u32* localIndices, u32 indexCount,
const BatchDrawKey& key) {
- pushBatchCommandImpl(pool, drawList, clips, vertexByteOffset, localIndices, indexCount, key);
+ pushBatchCommandImpl(pool, drawList, clips, vertexByteOffset, vertexCount, localIndices, indexCount, key);
}
void pushBatchCommand(TransientBufferPool& pool, DrawList& drawList, const ClipState& clips,
- u32 vertexByteOffset, const u16* localIndices, u32 indexCount,
+ u32 vertexByteOffset, u32 vertexCount,
+ const u16* localIndices, u32 indexCount,
const BatchDrawKey& key) {
- pushBatchCommandImpl(pool, drawList, clips, vertexByteOffset, localIndices, indexCount, key);
+ pushBatchCommandImpl(pool, drawList, clips, vertexByteOffset, vertexCount, localIndices, indexCount, key);
}
void appendIndexedBatch(TransientBufferPool& pool, DrawList& drawList, const ClipState& clips,
@@ -83,7 +87,7 @@ void appendIndexedBatch(TransientBufferPool& pool, DrawList& drawList, const Cli
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);
+ pushBatchCommandImpl(pool, drawList, clips, vertexByteOffset, vertexCount, localIndices, indexCount, key);
}
} // namespace esengine
diff --git a/src/esengine/renderer/BatchBuilder.hpp b/src/esengine/renderer/BatchBuilder.hpp
index 210b0be5d..ff511a4e4 100644
--- a/src/esengine/renderer/BatchBuilder.hpp
+++ b/src/esengine/renderer/BatchBuilder.hpp
@@ -89,7 +89,7 @@ struct BatchDrawKey {
* index range via writeIndices() before pushing one command).
*/
void pushBatchDraw(DrawList& drawList, const ClipState& clips,
- u32 vertexByteOffset, u32 indexOffset, u32 indexCount,
+ u32 vertexByteOffset, u32 vertexCount, u32 indexOffset, u32 indexCount,
const BatchDrawKey& key);
/**
@@ -100,10 +100,10 @@ void pushBatchDraw(DrawList& drawList, const ClipState& clips,
* cover both index source widths.
*/
void pushBatchCommand(TransientBufferPool& pool, DrawList& drawList, const ClipState& clips,
- u32 vertexByteOffset, const u32* localIndices, u32 indexCount,
+ u32 vertexByteOffset, u32 vertexCount, const u32* localIndices, u32 indexCount,
const BatchDrawKey& key);
void pushBatchCommand(TransientBufferPool& pool, DrawList& drawList, const ClipState& clips,
- u32 vertexByteOffset, const u16* localIndices, u32 indexCount,
+ u32 vertexByteOffset, u32 vertexCount, const u16* localIndices, u32 indexCount,
const BatchDrawKey& key);
/**
diff --git a/src/esengine/renderer/BatchVertex.hpp b/src/esengine/renderer/BatchVertex.hpp
index 1b14b20bb..827a71ebd 100644
--- a/src/esengine/renderer/BatchVertex.hpp
+++ b/src/esengine/renderer/BatchVertex.hpp
@@ -9,6 +9,7 @@ struct BatchVertex {
glm::vec2 position;
u32 color;
glm::vec2 texCoord;
+ f32 texIndex = 0.0f; // sampler slot within the draw's texture set; assigned at merge time
};
inline u32 packColor(f32 r, f32 g, f32 b, f32 a) {
diff --git a/src/esengine/renderer/DrawCommand.hpp b/src/esengine/renderer/DrawCommand.hpp
index 05577b4e7..ffafa1dff 100644
--- a/src/esengine/renderer/DrawCommand.hpp
+++ b/src/esengine/renderer/DrawCommand.hpp
@@ -46,9 +46,15 @@ struct DrawCommand {
// with per-instance attributes based at vertex_byte_offset (see LayoutId::ParticleInstance).
u32 instance_count = 0;
+ // Vertices owned by this command (from vertex_byte_offset). Needed so the merge pass
+ // can rewrite their texIndex when coalescing into a multi-texture batch.
+ u32 vertex_count = 0;
+
+ // Texture is no longer part of the sort key: dropping it lets draws that differ only
+ // by texture sort adjacent and coalesce into one multi-texture batch (up to 8 textures,
+ // selected per-vertex in the shader). Order within a layer is otherwise unchanged.
static u64 buildSortKey(RenderStage stage, i32 layer, u32 shaderId,
- BlendMode blend, u16 stateFlags,
- u32 textureId, f32 depth) {
+ BlendMode blend, u16 stateFlags, f32 depth) {
u64 stageKey = static_cast<u64>(stage) << 60;
i32 normalizedLayer = std::clamp(layer + 32768, 0, 65535);
@@ -57,7 +63,6 @@ struct DrawCommand {
u64 shaderKey = static_cast<u64>(shaderId & 0xFF) << 36;
u64 blendKey = static_cast<u64>(blend) << 33;
u64 flagsKey = static_cast<u64>(stateFlags & 0x03) << 31;
- u64 texKey = static_cast<u64>(textureId & 0x1FFFF) << 14;
u32 depthBits;
if (stage == RenderStage::Transparent || stage == RenderStage::Overlay) {
@@ -69,7 +74,18 @@ struct DrawCommand {
}
u64 depthKey = static_cast<u64>(depthBits & 0x3FFF);
- return stageKey | layerKey | shaderKey | blendKey | flagsKey | texKey | depthKey;
+ return stageKey | layerKey | shaderKey | blendKey | flagsKey | depthKey;
+ }
+
+ /** @brief Finds @p texId in this command's texture set, adds it (returns its slot), or
+ * -1 if the set is full. Used by the merge to assign per-vertex sampler slots. */
+ i32 addTextureSlot(u32 texId) {
+ for (u8 i = 0; i < texture_count; ++i) {
+ if (texture_ids[i] == texId) return static_cast<i32>(i);
+ }
+ if (texture_count >= MAX_CMD_TEXTURE_SLOTS) return -1;
+ texture_ids[texture_count] = texId;
+ return static_cast<i32>(texture_count++);
}
bool canMergeWith(const DrawCommand& next) const {
@@ -85,12 +101,8 @@ struct DrawCommand {
if (state_flags & (CMD_STATE_STENCIL_WRITE | CMD_STATE_STENCIL_TEST)) {
if (stencil_ref != next.stencil_ref) return false;
}
- if (texture_count != next.texture_count) return false;
- if (texture_count > 0 &&
- std::memcmp(texture_ids, next.texture_ids,
- texture_count * sizeof(u32)) != 0) {
- return false;
- }
+ // Texture compatibility is decided by the merge (the combined set must fit in 8
+ // slots), not here, so different-texture draws can coalesce.
if (index_offset + index_count != next.index_offset) return false;
return true;
}
diff --git a/src/esengine/renderer/DrawList.cpp b/src/esengine/renderer/DrawList.cpp
index 2c4384b92..5ba3a3fa4 100644
--- a/src/esengine/renderer/DrawList.cpp
+++ b/src/esengine/renderer/DrawList.cpp
@@ -1,10 +1,22 @@
#include "DrawList.hpp"
+#include "BatchVertex.hpp"
#include <glm/glm.hpp>
#include <algorithm>
namespace esengine {
+namespace {
+// Stamp a merged sampler slot onto every vertex of a Batch-layout command. The vertices
+// are still in CPU staging at finalize time (finalize runs before upload), so the shader
+// later samples u_textures[texIndex]. Only the Batch layout carries a texIndex attribute.
+void rewriteTexIndex(TransientBufferPool& pool, const DrawCommand& cmd, i32 slot) {
+ auto* verts = reinterpret_cast<BatchVertex*>(pool.vertexData(LayoutId::Batch) + cmd.vertex_byte_offset);
+ f32 fslot = static_cast<f32>(slot);
+ for (u32 k = 0; k < cmd.vertex_count; ++k) verts[k].texIndex = fslot;
+}
+} // namespace
+
void DrawList::clear() {
commands_.clear();
sort_entries_.clear();
@@ -15,7 +27,7 @@ void DrawList::push(const DrawCommand& cmd) {
commands_.push_back(cmd);
}
-void DrawList::finalize() {
+void DrawList::finalize(TransientBufferPool& pool) {
u32 count = static_cast<u32>(commands_.size());
if (count == 0) {
merged_draw_calls_ = 0;
@@ -27,9 +39,12 @@ void DrawList::finalize() {
sort_entries_[i] = { commands_[i].sort_key, i };
}
+ // Tie-break on emit index so same-key commands keep submission order: that keeps an
+ // emit-contiguous run contiguous after sorting, which is what lets the merge coalesce it
+ // (including across different textures, now that texture is out of the key).
std::sort(sort_entries_.begin(), sort_entries_.end(),
[](const SortEntry& a, const SortEntry& b) {
- return a.key < b.key;
+ return a.key < b.key || (a.key == b.key && a.index < b.index);
});
// Gather into a reused scratch buffer (not a fresh per-frame vector) and swap
@@ -44,13 +59,35 @@ void DrawList::finalize() {
u32 writeIdx = 0;
for (u32 i = 0; i < count; ++i) {
- if (writeIdx > 0 && commands_[writeIdx - 1].canMergeWith(commands_[i])) {
- commands_[writeIdx - 1].index_count += commands_[i].index_count;
- commands_[writeIdx - 1].entity_count += commands_[i].entity_count;
- } else {
+ bool didMerge = false;
+ if (writeIdx > 0) {
+ DrawCommand& head = commands_[writeIdx - 1];
+ if (head.canMergeWith(commands_[i])) {
+ if (head.layout_id == LayoutId::Batch && commands_[i].texture_count >= 1) {
+ // Multi-texture: give this command's texture a slot in the head's set
+ // (or bail to a new draw if all 8 slots are taken), then stamp its verts.
+ i32 slot = head.addTextureSlot(commands_[i].texture_ids[0]);
+ if (slot >= 0) {
+ rewriteTexIndex(pool, commands_[i], slot);
+ head.index_count += commands_[i].index_count;
+ head.entity_count += commands_[i].entity_count;
+ didMerge = true;
+ }
+ } else {
+ head.index_count += commands_[i].index_count;
+ head.entity_count += commands_[i].entity_count;
+ didMerge = true;
+ }
+ }
+ }
+ if (!didMerge) {
if (writeIdx != i) {
commands_[writeIdx] = commands_[i];
}
+ // First command of a run owns slot 0 of its (so far single-texture) set.
+ if (commands_[writeIdx].layout_id == LayoutId::Batch) {
+ rewriteTexIndex(pool, commands_[writeIdx], 0);
+ }
++writeIdx;
}
}
diff --git a/src/esengine/renderer/DrawList.hpp b/src/esengine/renderer/DrawList.hpp
index d876e1250..956b18034 100644
--- a/src/esengine/renderer/DrawList.hpp
+++ b/src/esengine/renderer/DrawList.hpp
@@ -15,7 +15,10 @@ public:
void clear();
void push(const DrawCommand& cmd);
- void finalize();
+ // Sorts, then coalesces contiguous compatible commands — combining up to 8 textures
+ // into one multi-texture batch and stamping each command's per-vertex sampler slot
+ // (hence the pool, whose staging it rewrites; call before upload()).
+ void finalize(TransientBufferPool& pool);
// Each merged command resolves to an immutable pipeline (program + layout + blend +
// depth + stencil + cull) bound via GfxDevice::setPipeline; per-draw dynamic state
diff --git a/src/esengine/renderer/ImmediateDraw.cpp b/src/esengine/renderer/ImmediateDraw.cpp
index 9c840c866..2c99b6a08 100644
--- a/src/esengine/renderer/ImmediateDraw.cpp
+++ b/src/esengine/renderer/ImmediateDraw.cpp
@@ -26,6 +26,7 @@
#include <glm/gtc/type_ptr.hpp>
#include <array>
#include <cmath>
+#include <string>
namespace esengine {
@@ -70,8 +71,12 @@ void ImmediateDraw::init() {
if (shader && shader->isValid()) {
batch_shader_id_ = shader->getProgramId();
device_.useProgram(batch_shader_id_);
- i32 texLoc = device_.getUniformLocation(batch_shader_id_, "u_texture");
- if (texLoc >= 0) device_.setUniform1i(texLoc, 0);
+ // Immediate draw is single-texture (slot 0), but bind all 8 samplers for parity
+ // with the world batch shader.
+ for (i32 i = 0; i < 8; ++i) {
+ i32 loc = device_.getUniformLocation(batch_shader_id_, ("u_textures[" + std::to_string(i) + "]").c_str());
+ if (loc >= 0) device_.setUniform1i(loc, i);
+ }
device_.useProgram(0);
// One immutable pipeline: the batch shader, Batch layout, normal blend, depth
diff --git a/src/esengine/renderer/RenderFrame.cpp b/src/esengine/renderer/RenderFrame.cpp
index c63722061..0f698d008 100644
--- a/src/esengine/renderer/RenderFrame.cpp
+++ b/src/esengine/renderer/RenderFrame.cpp
@@ -175,8 +175,10 @@ void RenderFrame::flush() {
// Drop any pipeline a prior phase left bound, so the first draw re-applies its state.
device_.invalidatePipelineCache();
+ // finalize() sorts + coalesces and rewrites per-vertex texIndex into the staging, so it
+ // must run before upload() ships that staging to the GPU.
+ draw_list_.finalize(pool_);
pool_.upload();
- draw_list_.finalize();
context_.updateFrameConstants(view_projection_);
draw_list_.execute(device_, pool_, &frame_capture_);
@@ -369,13 +371,15 @@ u32 RenderFrame::initBatchShader() {
Shader* shader = resource_manager_.getShader(handle);
if (shader && shader->isValid()) {
+ u32 prog = shader->getProgramId();
shader->bind();
- i32 texLoc = device_.getUniformLocation(shader->getProgramId(), "u_texture");
- if (texLoc >= 0) {
- device_.setUniform1i(texLoc, 0);
+ // Bind the 8 multi-texture samplers to units 0..7 (per-program, set once).
+ for (i32 i = 0; i < 8; ++i) {
+ i32 loc = device_.getUniformLocation(prog, ("u_textures[" + std::to_string(i) + "]").c_str());
+ if (loc >= 0) device_.setUniform1i(loc, i);
}
shader->unbind();
- return shader->getProgramId();
+ return prog;
}
ES_LOG_ERROR("Failed to create batch shader");
diff --git a/src/esengine/renderer/RenderFrameSubmit.cpp b/src/esengine/renderer/RenderFrameSubmit.cpp
index 077da2914..4763e426d 100644
--- a/src/esengine/renderer/RenderFrameSubmit.cpp
+++ b/src/esengine/renderer/RenderFrameSubmit.cpp
@@ -64,7 +64,7 @@ void RenderFrame::submitSpineBatch(
dst[i] = { {worldPos.x, worldPos.y}, pc, {v[2], v[3]} };
}
- pushBatchCommand(pool_, draw_list_, clip_state_, vOff, indices,
+ pushBatchCommand(pool_, draw_list_, clip_state_, vOff, static_cast<u32>(vertexCount), indices,
static_cast<u32>(indexCount), BatchDrawKey{
.stage = current_stage_,
.layer = layer,
diff --git a/src/esengine/renderer/ShaderEmbeds.generated.hpp b/src/esengine/renderer/ShaderEmbeds.generated.hpp
index d762038d4..ffe79d942 100644
--- a/src/esengine/renderer/ShaderEmbeds.generated.hpp
+++ b/src/esengine/renderer/ShaderEmbeds.generated.hpp
@@ -9,6 +9,7 @@ inline constexpr const char* BATCH = R"esshader(#pragma shader "Batch"
layout(location = 0) in vec2 a_position;
layout(location = 1) in vec4 a_color;
layout(location = 2) in vec2 a_texCoord;
+layout(location = 3) in float a_texIndex;
layout(std140) uniform FrameConstants {
mat4 u_projection;
@@ -16,11 +17,13 @@ layout(std140) uniform FrameConstants {
out vec4 v_color;
out vec2 v_texCoord;
+flat out int v_texIndex;
void main() {
gl_Position = u_projection * vec4(a_position, 0.0, 1.0);
v_color = a_color;
v_texCoord = a_texCoord;
+ v_texIndex = int(a_texIndex);
}
#pragma end
@@ -29,13 +32,25 @@ precision mediump float;
in vec4 v_color;
in vec2 v_texCoord;
+flat in int v_texIndex;
-uniform sampler2D u_texture;
+// Up to 8 textures bound per multi-texture batch. GLSL ES 3.00 forbids indexing a
+// sampler array with a non-uniform expression, so the slot is selected by a constant
+// branch chain (the standard WebGL2 multi-texture batching technique).
+uniform sampler2D u_textures[8];
out vec4 fragColor;
void main() {
- vec4 texColor = texture(u_texture, v_texCoord);
+ vec4 texColor;
+ if (v_texIndex == 0) texColor = texture(u_textures[0], v_texCoord);
+ else if (v_texIndex == 1) texColor = texture(u_textures[1], v_texCoord);
+ else if (v_texIndex == 2) texColor = texture(u_textures[2], v_texCoord);
+ else if (v_texIndex == 3) texColor = texture(u_textures[3], v_texCoord);
+ else if (v_texIndex == 4) texColor = texture(u_textures[4], v_texCoord);
+ else if (v_texIndex == 5) texColor = texture(u_textures[5], v_texCoord);
+ else if (v_texIndex == 6) texColor = texture(u_textures[6], v_texCoord);
+ else texColor = texture(u_textures[7], v_texCoord);
fragColor = texColor * v_color;
}
#pragma end
diff --git a/src/esengine/renderer/TransientBufferPool.cpp b/src/esengine/renderer/TransientBufferPool.cpp
index 4f25a9fbc..f8ec45cec 100644
--- a/src/esengine/renderer/TransientBufferPool.cpp
+++ b/src/esengine/renderer/TransientBufferPool.cpp
@@ -241,13 +241,15 @@ void TransientBufferPool::setupStream(LayoutId layout) {
switch (layout) {
case LayoutId::Batch: {
- constexpr u32 STRIDE = 20;
+ constexpr u32 STRIDE = 24; // BatchVertex: pos(8) + color(4) + uv(8) + texIndex(4)
device_.enableVertexAttrib(0);
device_.vertexAttribPointer(0, 2, GfxDataType::Float, false, STRIDE, 0);
device_.enableVertexAttrib(1);
device_.vertexAttribPointer(1, 4, GfxDataType::UnsignedByte, true, STRIDE, 8);
device_.enableVertexAttrib(2);
device_.vertexAttribPointer(2, 2, GfxDataType::Float, false, STRIDE, 12);
+ device_.enableVertexAttrib(3);
+ device_.vertexAttribPointer(3, 1, GfxDataType::Float, false, STRIDE, 20);
break;
}
case LayoutId::Shape: {
diff --git a/src/esengine/renderer/plugins/ParticlePlugin.cpp b/src/esengine/renderer/plugins/ParticlePlugin.cpp
index 4d78257b7..fce5c42ec 100644
--- a/src/esengine/renderer/plugins/ParticlePlugin.cpp
+++ b/src/esengine/renderer/plugins/ParticlePlugin.cpp
@@ -140,7 +140,7 @@ void ParticlePlugin::collect(RenderCollectContext& collect_ctx) {
DrawCommand cmd{};
cmd.sort_key = DrawCommand::buildSortKey(
- ctx.current_stage, emitter.layer, particle_shader_id_, blendMode, 0, textureId,
+ ctx.current_stage, emitter.layer, particle_shader_id_, blendMode, 0,
emitterWorldPos.z);
cmd.index_offset = 0; // static unit-quad indices
cmd.index_count = 6;
diff --git a/src/esengine/renderer/plugins/ShapePlugin.cpp b/src/esengine/renderer/plugins/ShapePlugin.cpp
index 7987f5267..6b8a8fcaf 100644
--- a/src/esengine/renderer/plugins/ShapePlugin.cpp
+++ b/src/esengine/renderer/plugins/ShapePlugin.cpp
@@ -100,7 +100,7 @@ void ShapePlugin::collect(RenderCollectContext& collect_ctx) {
u32 iOff = buffers.appendIndices(LayoutId::Shape, indices, 6);
DrawCommand cmd{};
- cmd.sort_key = DrawCommand::buildSortKey(ctx.current_stage, shape.layer, shape_shader_id_, BlendMode::Normal, 0, 0, position.z);
+ cmd.sort_key = DrawCommand::buildSortKey(ctx.current_stage, shape.layer, shape_shader_id_, BlendMode::Normal, 0, position.z);
cmd.index_offset = iOff;
cmd.index_count = 6;
cmd.vertex_byte_offset = vOff;