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): opaque is a blend mode, and the merge stops assuming depth follows the material

esengine1e8a6129844d
diff --git a/sdk/src/render/blend.ts b/sdk/src/render/blend.ts
index 429917653..b4b379e7b 100644
--- a/sdk/src/render/blend.ts
+++ b/sdk/src/render/blend.ts
@@ -5,10 +5,33 @@
* @brief Blend mode definitions for rendering
*/
+/**
+ * Mirrors the engine's `BlendMode` (src/esengine/renderer/draw/BlendMode.hpp) — the
+ * values are serialized into material assets, so they are fixed.
+ */
export enum BlendMode {
+ /** SrcAlpha, OneMinusSrcAlpha — the default. */
Normal = 0,
+ /** SrcAlpha, One — glow, particles. */
Additive = 1,
+ /** DstColor, OneMinusSrcAlpha — shadows, multiply. */
Multiply = 2,
+ /** One, OneMinusSrcColor — lighten. */
Screen = 3,
+ /** One, OneMinusSrcAlpha — for premultiplied-alpha sources. */
PremultipliedAlpha = 4,
+ /** One, One — additive with a premultiplied-alpha source. */
+ PmaAdditive = 5,
+ /** Takes the brighter pixel (GL_MAX). */
+ Lighten = 6,
+ /** Takes the darker pixel (GL_MIN). */
+ Darken = 7,
+ /** Screen on light, Multiply on dark; requires shader support. */
+ Overlay = 8,
+ /**
+ * No blending — the source replaces the destination. An opaque sprite pays
+ * nothing to read the destination it would ignore, and this is the only mode
+ * a draw may write depth in, since a blended one must stay in painter's order.
+ */
+ None = 9,
}
diff --git a/src/esengine/renderer/draw/BlendMode.hpp b/src/esengine/renderer/draw/BlendMode.hpp
index 7e673aa47..c62f5833b 100644
--- a/src/esengine/renderer/draw/BlendMode.hpp
+++ b/src/esengine/renderer/draw/BlendMode.hpp
@@ -29,6 +29,13 @@ enum class BlendMode : u8 {
Lighten = 6, // GL_MAX blend equation (take brighter pixel)
Darken = 7, // GL_MIN blend equation (take darker pixel)
Overlay = 8, // Screen on light, Multiply on dark (requires shader)
+ // No blending: the source replaces the destination. Every other mode reads the
+ // destination for each fragment, which an opaque sprite pays for nothing, and
+ // — the reason this exists — an opaque draw is the only kind that may WRITE
+ // depth, since a blended one has to stay behind the painter's order. Last in
+ // the list rather than first because these values are serialized into material
+ // assets; Normal must stay 0.
+ None = 9,
};
} // namespace esengine
diff --git a/src/esengine/renderer/draw/DrawCommand.hpp b/src/esengine/renderer/draw/DrawCommand.hpp
index a33912534..a75b61150 100644
--- a/src/esengine/renderer/draw/DrawCommand.hpp
+++ b/src/esengine/renderer/draw/DrawCommand.hpp
@@ -162,9 +162,21 @@ struct DrawCommand {
if (shader_id != next.shader_id) return false;
if (blend_mode != next.blend_mode) return false;
if (layout_id != next.layout_id) return false;
- // Same material handle => same uniforms/textures/depth/cull; different ones must not
+ // Same material handle => same uniforms/textures; different ones must not
// coalesce. material_id 0 (no material) shares the path's defaults, so they still merge.
if (material_id != next.material_id) return false;
+ // Depth state is checked on its own rather than trusted to follow from the
+ // material. It used to: every draw resolved its depth from a material, so equal
+ // material_id implied equal state, and material_id 0 meant one shared set of
+ // defaults. Once a layer derives depth from the stage instead, two material-0
+ // draws in the same layer can differ — one opaque and writing depth, one blended
+ // and not — and merging them would silently give one of them the other's state.
+ // The symptom would be a blended sprite occasionally clipping what is behind it,
+ // depending on whether the two happened to land adjacent. Compared here as render
+ // state, not as stage: the stage is the reason, these three are the effect.
+ if (depth_test != next.depth_test) return false;
+ if (depth_write != next.depth_write) return false;
+ if (cull != next.cull) return false;
if (state_flags != next.state_flags) return false;
if (state_flags & CMD_STATE_SCISSOR) {
if (scissor != next.scissor) return false;
diff --git a/src/esengine/renderer/draw/DrawList.cpp b/src/esengine/renderer/draw/DrawList.cpp
index eaad4c905..e15eab661 100644
--- a/src/esengine/renderer/draw/DrawList.cpp
+++ b/src/esengine/renderer/draw/DrawList.cpp
@@ -119,7 +119,13 @@ void DrawList::execute(GfxDevice& device, TransientBufferPool& buffers,
desc.program = ShaderHandle{cmd.shader_id};
desc.vertexLayout = buffers.layoutHandle(cmd.layout_id);
desc.blend = cmd.blend_mode;
- desc.blendEnabled = true;
+ // Opaque is a blend mode, not a second switch beside one: a material that
+ // says None is asking for the source to replace the destination, and any
+ // other value is asking to read it. Keeping it in the same field is what
+ // stops "Additive, blending off" from being expressible at all — and the
+ // sort key and canMergeWith already carry blend, so nothing else has to
+ // learn about this.
+ desc.blendEnabled = cmd.blend_mode != BlendMode::None;
desc.depthTest = cmd.depth_test;
desc.depthWrite = cmd.depth_write;
desc.stencil = stencil;
diff --git a/src/esengine/renderer/rhi/GLDevice.cpp b/src/esengine/renderer/rhi/GLDevice.cpp
index 3b8e699fc..12978c09f 100644
--- a/src/esengine/renderer/rhi/GLDevice.cpp
+++ b/src/esengine/renderer/rhi/GLDevice.cpp
@@ -323,6 +323,13 @@ void GLDevice::setBlendMode(BlendMode mode) {
case BlendMode::Overlay:
glBlendFuncSeparate(GL_DST_COLOR, GL_SRC_COLOR, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
break;
+ case BlendMode::None:
+ // A pipeline asking for None disables GL_BLEND (PipelineDesc::blendEnabled),
+ // so this function is never sampled. Setting the default anyway keeps the
+ // cached state honest: current_blend_ is keyed on the mode, and a later
+ // pipeline that re-enables blending must not inherit whatever was left here.
+ glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
+ break;
}
if (mode != BlendMode::Lighten && mode != BlendMode::Darken) {
diff --git a/src/esengine/renderer/webgpu/WebGPUMappings.hpp b/src/esengine/renderer/webgpu/WebGPUMappings.hpp
index 81bfad759..0d23ea589 100644
--- a/src/esengine/renderer/webgpu/WebGPUMappings.hpp
+++ b/src/esengine/renderer/webgpu/WebGPUMappings.hpp
@@ -75,6 +75,10 @@ inline BlendStateWGPU toWGPUBlend(BlendMode mode) {
case BlendMode::Darken:
return { comp(WGPUBlendOperation_Min, WGPUBlendFactor_One, WGPUBlendFactor_One),
comp(WGPUBlendOperation_Min, WGPUBlendFactor_One, WGPUBlendFactor_One) };
+ // None never reaches a live blend state — the pipeline drops the blend target
+ // entirely (WebGPUDevice: target.blend = blendEnabled ? &state : nullptr) — but
+ // it is spelled out so the mapping stays total rather than leaning on `default`.
+ case BlendMode::None:
case BlendMode::Normal:
case BlendMode::Overlay:
default:
diff --git a/tests/renderer/MockGfxDevice.hpp b/tests/renderer/MockGfxDevice.hpp
index d01ee1ba3..4b611d9d5 100644
--- a/tests/renderer/MockGfxDevice.hpp
+++ b/tests/renderer/MockGfxDevice.hpp
@@ -14,6 +14,8 @@ namespace esengine {
struct MockGfxDevice final : GfxDevice {
// call counters
+ // The render state a draw resolved to, as the pipeline saw it.
+ PipelineDesc lastPipelineDesc{};
int useProgramCalls = 0;
int bindTextureCalls = 0;
int beginRenderPassCalls = 0;
@@ -180,7 +182,10 @@ struct MockGfxDevice final : GfxDevice {
u32 getUniformBlockIndex(ShaderHandle, const char*) override { return GFX_INVALID_UNIFORM_BLOCK; }
void uniformBlockBinding(ShaderHandle, u32, u32) override {}
- PipelineHandle createPipeline(const PipelineDesc&) override { return static_cast<PipelineHandle>(++nextPipelineId); }
+ PipelineHandle createPipeline(const PipelineDesc& desc) override {
+ lastPipelineDesc = desc;
+ return static_cast<PipelineHandle>(++nextPipelineId);
+ }
void setPipeline(PipelineHandle) override { ++setPipelineCalls; }
void setStencilReference(i32) override {}
void invalidatePipelineCache() override {}
diff --git a/tests/renderer/test_batch_builder.cpp b/tests/renderer/test_batch_builder.cpp
index e75897f61..bb45e4094 100644
--- a/tests/renderer/test_batch_builder.cpp
+++ b/tests/renderer/test_batch_builder.cpp
@@ -253,3 +253,58 @@ TEST_CASE("sort order: inside one layer, opaque draws before blended") {
CHECK(h.list.command(0).texture_ids[0] == 61);
CHECK(h.list.command(1).texture_ids[0] == 60);
}
+
+// Opaque is a blend MODE, not a switch beside one — so "no blending" has to survive
+// the trip from the draw's blend field to the pipeline the device is handed. Without
+// this the scene path hardcoded blendEnabled, and an opaque material silently kept
+// reading the destination it was asking to replace.
+TEST_CASE("execute: BlendMode::None turns blending off in the resolved pipeline") {
+ Harness h;
+ MaterialStore materials;
+ BatchVertex quad[4] = {};
+
+ BatchDrawKey key = quadKey(42);
+ key.blend = BlendMode::None;
+ appendQuad(h.pool, h.list, h.clips, quad, key);
+ h.list.finalize(h.pool);
+ h.pool.upload();
+ h.list.execute(h.device, h.pool, materials);
+
+ CHECK(h.device.lastPipelineDesc.blend == BlendMode::None);
+ CHECK(h.device.lastPipelineDesc.blendEnabled == false);
+}
+
+TEST_CASE("execute: every other blend mode leaves blending on") {
+ Harness h;
+ MaterialStore materials;
+ BatchVertex quad[4] = {};
+
+ appendQuad(h.pool, h.list, h.clips, quad, quadKey(42));
+ h.list.finalize(h.pool);
+ h.pool.upload();
+ h.list.execute(h.device, h.pool, materials);
+
+ CHECK(h.device.lastPipelineDesc.blend == BlendMode::Normal);
+ CHECK(h.device.lastPipelineDesc.blendEnabled == true);
+}
+
+// Two draws that agree on everything the merge used to look at, and disagree on
+// whether they write depth. Before a layer could derive depth state from its stage,
+// equal material_id implied equal depth state and this could not arise; now it can,
+// and merging them would hand one of them the other's depth behaviour.
+TEST_CASE("finalize: draws with different depth state never coalesce") {
+ Harness h;
+ BatchVertex quad[4] = {};
+
+ BatchDrawKey writes = quadKey(42);
+ writes.depthWrite = true;
+ appendQuad(h.pool, h.list, h.clips, quad, writes);
+
+ BatchDrawKey reads = quadKey(43);
+ reads.depthWrite = false;
+ appendQuad(h.pool, h.list, h.clips, quad, reads);
+
+ h.list.finalize(h.pool);
+
+ CHECK(h.list.mergedDrawCallCount() == 2);
+}