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
fix(renderer): break GL feedback loop when a render target is also sampled
yhh27e34ed06e1a
diff --git a/src/esengine/renderer/GLDevice.cpp b/src/esengine/renderer/GLDevice.cppindex 59ab44cac..245419e49 100644--- a/src/esengine/renderer/GLDevice.cpp+++ b/src/esengine/renderer/GLDevice.cpp@@ -856,6 +856,19 @@ void GLDevice::bindTextureForEdit(u32 id) {if (active_texture_unit_ < kTextureSlots) bound_texture_[active_texture_unit_] = id;}+void GLDevice::evictSamplerBinding(u32 textureId) {+ if (textureId == 0) return;+ for (u32 slot = 0; slot < kTextureSlots; ++slot) {+ if (bound_texture_[slot] != textureId) continue;+ if (active_texture_unit_ != slot) {+ glActiveTexture(GL_TEXTURE0 + slot);+ active_texture_unit_ = slot;+ }+ glBindTexture(GL_TEXTURE_2D, 0);+ bound_texture_[slot] = 0;+ }+}+TextureHandle GLDevice::createTexture(const TextureDesc& desc, const void* pixels) {GLuint id = 0;glGenTextures(1, &id);@@ -964,12 +977,14 @@ FramebufferHandle GLDevice::createFramebuffer(const FramebufferDesc& desc) {glDeleteFramebuffers(1, &id);return FramebufferHandle::Default;}+ framebuffer_textures_[id] = {static_cast<u32>(desc.color0), static_cast<u32>(desc.depthStencil)};return FramebufferHandle{id};}void GLDevice::deleteFramebuffer(FramebufferHandle framebuffer) {GLuint id = static_cast<GLuint>(framebuffer);if (id != 0) glDeleteFramebuffers(1, &id);+ framebuffer_textures_.erase(id);}void GLDevice::clearStencil(i32 value) {@@ -978,7 +993,23 @@ void GLDevice::clearStencil(i32 value) {}void GLDevice::beginRenderPass(const RenderPassDesc& desc) {- glBindFramebuffer(GL_FRAMEBUFFER, static_cast<GLuint>(desc.target));+ const GLuint target = static_cast<GLuint>(desc.target);+ glBindFramebuffer(GL_FRAMEBUFFER, target);++ // Feedback-loop guard: a render target's own attachment must not remain bound to+ // a sampler unit while the target is drawn into — GL leaves that undefined and+ // some drivers raise GL_INVALID_OPERATION ("feedback loop"). This bites when a+ // pass samples a render texture to composite it on screen and the NEXT frame+ // draws back into that same texture (e.g. a live minimap). Detach it up front;+ // the default framebuffer (0) owns no texture, so skip it.+ if (target != 0) {+ auto it = framebuffer_textures_.find(target);+ if (it != framebuffer_textures_.end()) {+ evictSamplerBinding(it->second.color);+ evictSamplerBinding(it->second.depthStencil);+ }+ }+if (!desc.clearColor && !desc.clearDepth && !desc.clearStencil) return;// Load-op values ride the pass — no sticky device clear state to drift.diff --git a/src/esengine/renderer/GLDevice.hpp b/src/esengine/renderer/GLDevice.hppindex ab953af45..2ed93526a 100644--- a/src/esengine/renderer/GLDevice.hpp+++ b/src/esengine/renderer/GLDevice.hpp@@ -162,6 +162,11 @@ private:// redundant per-draw binds (every gl* call is a WASM→JS FFI crossing).void bindTextureForEdit(u32 id);+ // Detach a texture from every sampler slot it lingers in, keeping the sampler+ // cache coherent. Used by beginRenderPass to break feedback loops: a render+ // target's own attachment must not stay bound to a sampler while it is drawn to.+ void evictSamplerBinding(u32 textureId);+std::vector<PipelineDesc> pipelines_;PipelineHandle current_pipeline_ = PipelineHandle::Invalid;GfxStencilMode current_stencil_mode_ = GfxStencilMode::Off;@@ -207,6 +212,16 @@ private:std::unordered_map<u32, BufferMeta> buffer_meta_;std::unordered_map<u32, GfxPixelFormat> texture_formats_;+ // The textures a framebuffer owns (color + depth/stencil attachment ids),+ // recorded at createFramebuffer. beginRenderPass consults this to detach the+ // target's own attachments from any sampler slot before drawing into it — the+ // GL feedback-loop guard. 0 = no such attachment.+ struct FramebufferTextures {+ u32 color = 0;+ u32 depthStencil = 0;+ };+ std::unordered_map<u32, FramebufferTextures> framebuffer_textures_;+// Completed readbacks parked until taken: GL reads synchronously at request// time, so the async contract resolves on the caller's first poll.std::unordered_map<u32, std::vector<u8>> readbacks_;