Skip to content

ExternalTexture_OpenGL: implement GetInfo/Set/Get for GL texture handles#1780

Open
cdschmidt wants to merge 2 commits into
BabylonJS:masterfrom
cdschmidt:users/caschmidt/external-texture-opengl
Open

ExternalTexture_OpenGL: implement GetInfo/Set/Get for GL texture handles#1780
cdschmidt wants to merge 2 commits into
BabylonJS:masterfrom
cdschmidt:users/caschmidt/external-texture-opengl

Conversation

@cdschmidt

@cdschmidt cdschmidt commented Jul 13, 2026

Copy link
Copy Markdown

Implements the previously-stubbed OpenGL backend of the ExternalTexture plugin. Mirrors the D3D11 backend pattern: per-API Impl class derived from ImplBase, GetInfo/Set/Get triplet, full bgfx<->GL format table.

Key details:

  • 96-row s_textureFormat[] with static_assert against bgfx::TextureFormat::Count.
  • All non-core extension constants (S3TC, BPTC, ASTC, BGRA, etc.) guarded with #ifndef + OpenGL registry hex fallbacks so it compiles on minimal Mesa GLES headers.
  • Always sets BGFX_TEXTURE_RT (FBO bridging is the only supported use case; GL has no introspection equivalent of D3D11 BIND_RENDER_TARGET).
  • glIsTexture validation + pre/post-bind error drain; bounded mip walk; GL_TEXTURE_BINDING_2D save/restore on every exit path.
  • Sets NumLayers = 1 (GL_TEXTURE_2D is single-layer); array/multisample/cube handles are rejected by the GL_TEXTURE_2D-only bind check.
  • Throws on unsupported format rather than returning Unknown.
  • MSAA explicitly unsupported for now (would need TEXTURE_2D_MULTISAMPLE + GL_TEXTURE_SAMPLES query).

Rebased onto latest master and ported to the shared-dispatcher ExternalTexture interface (#1566).

@cdschmidt
cdschmidt force-pushed the users/caschmidt/external-texture-opengl branch 2 times, most recently from ac67b20 to 44becb9 Compare July 13, 2026 22:03
@cdschmidt cdschmidt changed the title ExternalTexture_OpenGL: implement GetInfo/Assign for GL texture handles ExternalTexture_OpenGL: implement GetInfo/Set/Get for GL texture handles Jul 13, 2026
@cdschmidt
cdschmidt force-pushed the users/caschmidt/external-texture-opengl branch from 44becb9 to 7733546 Compare July 13, 2026 22:13
@cdschmidt

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree company="Microsoft"

@cdschmidt
cdschmidt marked this pull request as ready for review July 14, 2026 18:57
Copilot AI review requested due to automatic review settings July 14, 2026 18:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request replaces the previously stubbed OpenGL(OpenGLES) ExternalTexture backend with a working implementation, aligning the OpenGL path with the existing per-API Impl + shared-dispatcher pattern used by the other backends.

Changes:

  • Implement OpenGL ExternalTexture::Impl GetInfo/Set/Get to support importing GL texture handles into bgfx.
  • Add a bgfx-format ⇄ GL internalFormat reverse-lookup table (with sRGB detection) plus guarded enum fallbacks for minimal GLES headers.
  • Validate texture handles via glIsTexture, enforce GL_TEXTURE_2D-only handles, and query dimensions/mip count via GL texture level parameters.

Comment thread Plugins/ExternalTexture/Source/ExternalTexture_OpenGL.cpp Outdated
Comment thread Plugins/ExternalTexture/Source/ExternalTexture_OpenGL.cpp Outdated
Comment thread Plugins/ExternalTexture/Source/ExternalTexture_OpenGL.cpp Outdated
Implements the previously-stubbed OpenGL backend of the ExternalTexture
plugin. Mirrors the D3D11 backend pattern: per-API Impl class derived
from ImplBase, GetInfo/Set/Get triplet, full bgfx<->GL format table.

Key details:
- 96-row s_textureFormat[] with static_assert against bgfx::TextureFormat::Count.
- All non-core extension constants (S3TC, BPTC, ASTC, BGRA, etc.) guarded
  with #ifndef + OpenGL registry hex fallbacks so it compiles on minimal
  Mesa GLES headers.
- Always sets BGFX_TEXTURE_RT (FBO bridging is the only supported use case;
  GL has no introspection equivalent of D3D11 BIND_RENDER_TARGET).
- glIsTexture validation + pre/post-bind error drain; bounded mip walk;
  GL_TEXTURE_BINDING_2D save/restore on every exit path.
- Sets NumLayers = 1 (GL_TEXTURE_2D is single-layer); array/multisample/cube
  handles are rejected by the GL_TEXTURE_2D-only bind check.
- Reverse format lookup skips the BGRA-ordered rows (BGRA8/BGRA4/BGR5A1/
  B5G6R5): GL has no distinct BGRA internal format, so bgfx maps them to the
  same GL internal format as their RGBA twins. Skipping ensures a GL_RGBA8
  attachment resolves to RGBA8 rather than the earlier-listed BGRA8 (which
  would swap R/B).
- Throws on unsupported format rather than returning Unknown.
- MSAA explicitly unsupported for now (would need TEXTURE_2D_MULTISAMPLE +
  GL_TEXTURE_SAMPLES query).

Review feedback:
- Guard the GLES 3.1 header include with __has_include, falling back to gl3.h
  (declaring glGetTexLevelParameteriv + GL_TEXTURE_WIDTH/HEIGHT) so builds on
  toolchains without gl31.h don't fail.
- Expand the GL_TEXTURE_2D-only rejection message to also name array textures.
- Enable OpenGL test coverage: implement the GL Helpers::CreateTexture/
  DestroyTexture, drop SKIP_EXTERNAL_TEXTURE_TESTS on UNIX (run Construction/
  CreateForJavaScript/Update/AddToContextAsyncAndUpdate). Render-path tests
  (SKIP_RENDER_TESTS) and the array/layer-index test (SKIP_EXTERNAL_TEXTURE_
  ARRAY_TESTS) remain skipped pending GL readback + array support.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@cdschmidt
cdschmidt force-pushed the users/caschmidt/external-texture-opengl branch from 0f2a2eb to c7e9a7f Compare July 14, 2026 22:05
Grow the OpenGL backend's s_textureFormat[] reverse-map table to cover all
100 bgfx::TextureFormat::Count formats (was 96), fixing the
"bgfx::TextureFormat::Count == BX_COUNTOF(s_textureFormat)" static assertion
that broke every Linux/OpenGL build. Reads GL internal formats back via ES 3.0
framebuffer-attachment queries and matches them to bgfx formats.

Add optional width/height hints to the ExternalTexture constructor and Update:
required on the OpenGL/OpenGL ES backend (ES 3.0 cannot query a texture's
dimensions from a bare handle) and ignored on D3D/Metal, which introspect the
native texture.

Move the UnitTests SKIP_RENDER_TESTS / SKIP_EXTERNAL_TEXTURE_ARRAY_TESTS
definitions into a GRAPHICS_API==OpenGL block so they apply to every OpenGL
build rather than only the Linux path.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 266f2ffa-1a30-41c7-96de-0ec7782df1bd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants