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.

refactor(renderer): loose render-state setters leave the device interface

yhhaec698d36155
diff --git a/src/esengine/renderer/GLDevice.cpp b/src/esengine/renderer/GLDevice.cpp
index 829d7de4e..ab166d95c 100644
--- a/src/esengine/renderer/GLDevice.cpp
+++ b/src/esengine/renderer/GLDevice.cpp
@@ -245,9 +245,18 @@ void GLDevice::clear(bool color, bool depth, bool stencil) {
if (color) mask |= GL_COLOR_BUFFER_BIT;
if (depth) mask |= GL_DEPTH_BUFFER_BIT;
if (stencil) mask |= GL_STENCIL_BUFFER_BIT;
- if (mask != 0) {
- glClear(mask);
- }
+ if (mask == 0) return;
+
+ // Load-op semantics, same as beginRenderPass: glClear honors write masks, so a
+ // restrictive mask left by the previous pipeline would silently veto the clear.
+ // Force the cleared attachments' masks open and drop the cached pipeline (the
+ // next setPipeline re-applies its own masks). The scissor rectangle is honored —
+ // the TS multi-camera flow clears per-camera regions through it.
+ if (color) setColorMask(true, true, true, true);
+ if (depth) setDepthWrite(true);
+ if (stencil) setStencilMask(0xFF);
+ glClear(mask);
+ invalidatePipelineCache();
}
// =============================================================================
@@ -909,17 +918,7 @@ void GLDevice::deleteFramebuffer(FramebufferHandle framebuffer) {
void GLDevice::beginRenderPass(const RenderPassDesc& desc) {
glBindFramebuffer(GL_FRAMEBUFFER, static_cast<GLuint>(desc.target));
- if (desc.clearColor || desc.clearDepth || desc.clearStencil) {
- // Load-op semantics: a clear must not be vetoed by write masks a prior
- // pipeline left restrictive (glClear honors them), so force the cleared
- // attachments' masks open and drop the cached pipeline — the next
- // setPipeline re-applies its own masks.
- if (desc.clearColor) setColorMask(true, true, true, true);
- if (desc.clearDepth) setDepthWrite(true);
- if (desc.clearStencil) setStencilMask(0xFF);
- clear(desc.clearColor, desc.clearDepth, desc.clearStencil);
- invalidatePipelineCache();
- }
+ clear(desc.clearColor, desc.clearDepth, desc.clearStencil);
}
void GLDevice::endRenderPass() {
diff --git a/src/esengine/renderer/GLDevice.hpp b/src/esengine/renderer/GLDevice.hpp
index 9cc430214..8a0258f57 100644
--- a/src/esengine/renderer/GLDevice.hpp
+++ b/src/esengine/renderer/GLDevice.hpp
@@ -44,24 +44,9 @@ public:
void setClearStencil(i32 value) override;
void clear(bool color, bool depth, bool stencil) override;
- void setBlendEnabled(bool enabled) override;
- void setBlendMode(BlendMode mode) override;
-
- void setDepthTest(bool enabled) override;
- void setDepthWrite(bool enabled) override;
-
- void setStencilTest(bool enabled) override;
- void setStencilFunc(GfxStencilFunc func, i32 ref, u32 mask) override;
- void setStencilOp(GfxStencilOp sfail, GfxStencilOp dpfail, GfxStencilOp dppass) override;
- void setStencilMask(u32 mask) override;
- void setColorMask(bool r, bool g, bool b, bool a) override;
-
void setScissorTest(bool enabled) override;
void setScissor(i32 x, i32 y, i32 w, i32 h) override;
- void setCulling(bool enabled) override;
- void setCullFace(bool front) override;
-
BufferHandle createBuffer(const BufferDesc& desc, const void* initialData) override;
void deleteBuffer(BufferHandle buffer) override;
void updateBuffer(BufferHandle buffer, u32 offsetBytes, const void* data, u32 sizeBytes) override;
@@ -134,6 +119,20 @@ public:
i32 getInt(GfxIntParam name) override;
private:
+ // The loose state setters a pipeline bundles (blend/depth/stencil/cull/masks):
+ // backend-internal since the pipeline became the only way to set them.
+ void setBlendEnabled(bool enabled);
+ void setBlendMode(BlendMode mode);
+ void setDepthTest(bool enabled);
+ void setDepthWrite(bool enabled);
+ void setStencilTest(bool enabled);
+ void setStencilFunc(GfxStencilFunc func, i32 ref, u32 mask);
+ void setStencilOp(GfxStencilOp sfail, GfxStencilOp dpfail, GfxStencilOp dppass);
+ void setStencilMask(u32 mask);
+ void setColorMask(bool r, bool g, bool b, bool a);
+ void setCulling(bool enabled);
+ void setCullFace(bool front);
+
// Pipeline cache: a handle is (index + 1) into pipelines_; PipelineHandle::Invalid is 0.
// WebGL2 has no native pipeline object, so a pipeline is applied as a bundle of GL
// state, deduped by comparing handles (same pipeline -> skip the whole state apply).
diff --git a/src/esengine/renderer/GfxDevice.hpp b/src/esengine/renderer/GfxDevice.hpp
index 30aea5445..1a7ba9a07 100644
--- a/src/esengine/renderer/GfxDevice.hpp
+++ b/src/esengine/renderer/GfxDevice.hpp
@@ -73,50 +73,17 @@ public:
/** @brief Sets the value written to the stencil buffer by clear() */
virtual void setClearStencil(i32 value) = 0;
- /** @brief Clears framebuffer attachments */
+ /**
+ * @brief Clears attachments of the current pass target with load-op semantics.
+ * @details Write masks left by the previous pipeline are forced open (and the
+ * cached pipeline dropped); the scissor rectangle is honored. Prefer
+ * the clears in RenderPassDesc — this exists for the SDK-driven
+ * multi-camera flow and mid-pass stencil clears.
+ */
virtual void clear(bool color, bool depth, bool stencil) = 0;
// =========================================================================
- // Blend State
- // =========================================================================
-
- /** @brief Enables or disables blending */
- virtual void setBlendEnabled(bool enabled) = 0;
-
- /** @brief Sets blend mode using predefined BlendMode */
- virtual void setBlendMode(BlendMode mode) = 0;
-
- // =========================================================================
- // Depth State
- // =========================================================================
-
- /** @brief Enables or disables depth testing */
- virtual void setDepthTest(bool enabled) = 0;
-
- /** @brief Enables or disables depth buffer writes */
- virtual void setDepthWrite(bool enabled) = 0;
-
- // =========================================================================
- // Stencil State
- // =========================================================================
-
- /** @brief Enables or disables stencil testing */
- virtual void setStencilTest(bool enabled) = 0;
-
- /** @brief Sets stencil function */
- virtual void setStencilFunc(GfxStencilFunc func, i32 ref, u32 mask) = 0;
-
- /** @brief Sets stencil operations */
- virtual void setStencilOp(GfxStencilOp sfail, GfxStencilOp dpfail, GfxStencilOp dppass) = 0;
-
- /** @brief Sets stencil write mask */
- virtual void setStencilMask(u32 mask) = 0;
-
- /** @brief Sets color write mask */
- virtual void setColorMask(bool r, bool g, bool b, bool a) = 0;
-
- // =========================================================================
- // Scissor State
+ // Dynamic Per-Draw State (deliberately outside the pipeline; see PipelineState.hpp)
// =========================================================================
/** @brief Enables or disables scissor test */
@@ -125,16 +92,6 @@ public:
/** @brief Sets the scissor rectangle */
virtual void setScissor(i32 x, i32 y, i32 w, i32 h) = 0;
- // =========================================================================
- // Culling
- // =========================================================================
-
- /** @brief Enables or disables face culling */
- virtual void setCulling(bool enabled) = 0;
-
- /** @brief Sets which face to cull (true = front, false = back) */
- virtual void setCullFace(bool front) = 0;
-
// =========================================================================
// Buffers
// =========================================================================
diff --git a/src/esengine/renderer/RenderFrame.cpp b/src/esengine/renderer/RenderFrame.cpp
index b18d2373d..c1b6350a3 100644
--- a/src/esengine/renderer/RenderFrame.cpp
+++ b/src/esengine/renderer/RenderFrame.cpp
@@ -345,11 +345,9 @@ void RenderFrame::replayToDrawCall(i32 stopAtDrawCall) {
context_.lights().uploadAndBind();
draw_list_.execute(device_, pool_, context_.materials(), &frame_capture_);
- // Leave scissor/stencil disabled for whatever renders next; invalidate so the next
- // setPipeline re-applies (we changed stencil/scissor outside the pipeline here).
+ // Leave scissor disabled for whatever renders next; invalidate so the next
+ // setPipeline re-applies its full state (stencil included).
device_.setScissorTest(false);
- device_.setStencilTest(false);
- device_.setStencilMask(0xFF);
device_.invalidatePipelineCache();
frame_capture_.clearReplayMode();
diff --git a/tests/renderer/MockGfxDevice.hpp b/tests/renderer/MockGfxDevice.hpp
index 616b12eda..03c07104f 100644
--- a/tests/renderer/MockGfxDevice.hpp
+++ b/tests/renderer/MockGfxDevice.hpp
@@ -75,24 +75,9 @@ struct MockGfxDevice final : GfxDevice {
void setClearStencil(i32) override {}
void clear(bool, bool, bool) override {}
- void setBlendEnabled(bool) override {}
- void setBlendMode(BlendMode) override {}
-
- void setDepthTest(bool) override {}
- void setDepthWrite(bool) override {}
-
- void setStencilTest(bool) override {}
- void setStencilFunc(GfxStencilFunc, i32, u32) override {}
- void setStencilOp(GfxStencilOp, GfxStencilOp, GfxStencilOp) override {}
- void setStencilMask(u32) override {}
- void setColorMask(bool, bool, bool, bool) override {}
-
void setScissorTest(bool) override {}
void setScissor(i32, i32, i32, i32) override {}
- void setCulling(bool) override {}
- void setCullFace(bool) override {}
-
BufferHandle createBuffer(const BufferDesc& desc, const void* initialData) override {
++createBufferCalls;
lastBufferDesc = desc;