From 588373f10b68f4e520d860f46ddc7f6c9d110d7d Mon Sep 17 00:00:00 2001 From: caschmidt Date: Thu, 16 Jul 2026 17:35:51 -0600 Subject: [PATCH] renderer_gl: support external textures (BGFX_CAPS_TEXTURE_EXTERNAL) The OpenGL renderer never advertised BGFX_CAPS_TEXTURE_EXTERNAL and ignored the external native handle passed to createTexture, so createTexture2D(..., _external) tripped the "External texture is not supported!" assertion. The D3D11, D3D12, Metal and Vulkan renderers already support this path. Advertise the capability (importing an existing GL texture id via TextureGL::overrideInternal is valid on any GL/GLES context) and adopt the supplied id in createTexture. overrideInternal sets BGFX_SAMPLER_INTERNAL_SHARED so TextureGL::destroy will not delete the externally owned texture. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 266f2ffa-1a30-41c7-96de-0ec7782df1bd --- src/renderer_gl.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/renderer_gl.cpp b/src/renderer_gl.cpp index 584650858a..a1024d78be 100644 --- a/src/renderer_gl.cpp +++ b/src/renderer_gl.cpp @@ -2930,6 +2930,11 @@ namespace bgfx { namespace gl : 0 ; + // External textures are imported by wrapping an existing GL texture + // id (see createTexture/TextureGL::overrideInternal), which is valid + // on any GL/GLES context, so the capability is always advertised. + g_caps.supported |= BGFX_CAPS_TEXTURE_EXTERNAL; + g_caps.supported |= false || s_extension[Extension::EXT_texture_array].m_supported || s_extension[Extension::EXT_gpu_shader4].m_supported @@ -3429,8 +3434,18 @@ namespace bgfx { namespace gl void* createTexture(TextureHandle _handle, const Memory* _mem, uint64_t _flags, uint8_t _skip, uint64_t _external) override { - BX_UNUSED(_external); - m_textures[_handle.idx].create(_mem, _flags, _skip); + TextureGL& texture = m_textures[_handle.idx]; + texture.create(_mem, _flags, _skip); + + // When an external native handle is supplied, replace the freshly + // allocated GL texture with the caller-owned texture id. overrideInternal + // sets BGFX_SAMPLER_INTERNAL_SHARED so TextureGL::destroy will not delete + // the externally owned id. + if (0 != _external) + { + texture.overrideInternal(uintptr_t(_external) ); + } + return NULL; }