Skip to content

Add an OpenGL ES 3.0 backend that draws into a host framebuffer - #10

Merged
beetlebugorg merged 1 commit into
mainfrom
issue-9
Nov 30, 2026
Merged

beetlebugorg merged 1 commit into
mainfrom
issue-9

Conversation

@beetlebugorg

Copy link
Copy Markdown
Owner

Closes #9

A build with -Dgpu=gles draws through OpenGL ES 3.0, into a framebuffer of a context the host owns. This is the integration the issue asks for: a Qt Quick host with a QQuickFramebufferObject gives its current context and its framebuffer, and charttable draws the chart there. The layout, the scene, the batcher and the CPU side stay as they are. The backend is one more implementation of the Gpu API behind src/gpu/gpu.zig.

What changed

  • src/gpu/gpu_gles.zig is the new backend. It draws the same prepared scene as the Vulkan backend: the same vertex streams, blend state, draw order and batcher. It has the fill, pattern, sprite and SDF programs. Raster tiles ride the sprite program, as in the other backends.
  • shaders/gles/ holds GLSL ES 3.00 ports of shaders/vk/. The context compiles them at attach. prelude.glsl declares the uniform block and the per-draw uniforms once for every stage.
  • The C ABI (include/charttable.h, src/capi.zig):
    • CHARTTABLE_NATIVE_GLES_CONTEXT (11) with a charttable_gles_context, which carries the host's get_proc_address and a user pointer.
    • charttable_render_gles(ct, &(charttable_gles_target){fbo, w, h}) draws into that framebuffer. The size of the target becomes the pixel size of the frame, so a resized framebuffer needs only a new target.
    • charttable_render on a GLES surface draws into the framebuffer that is bound for drawing.
    • charttable_snapshot_rgba draws into a framebuffer of its own and leaves the host's alone.
  • build.zig: -Dgpu=gles on any target off Apple.
  • README, DESIGN.md, shaders/gles/README.md, and a gles CI job.

How the requirements in the issue are met

  • The host's current context: yes. charttable loads every GL function through the host's loader, so the library links no GL library and adds no dependency.
  • No window, no EGLSurface, no buffer swap: yes. It never creates any of them.
  • Framebuffer resize: each charttable_render_gles takes the size from its target. Call charttable_resize too, for the viewport in points.
  • GL state ownership: every call that issues GL commands saves the state it changes and restores it before it returns. The header lists that state: the framebuffer bindings, the viewport, the enables, the blend state, the color mask, the clear color, the program, the vertex array, the buffer bindings, the texture and sampler of unit 0, and the pack and unpack parameters.
  • Depth and stencil: none needed. No pipeline of the Vulkan backend writes depth, so its depth test only refuses depth 1.0, which no range uses. This backend draws in paint order with the depth test off.
  • Threads: attach, render, render_gles, snapshot, detach and close must run on the thread with the context current. The header says so. The build worker never touches GL: in this backend makeScene copies the scene to CPU memory, and adoptScene makes the GL objects on the owner thread.

Choices to review

  • The backend is chosen at build time, as -Dgpu=vk is on Windows. One library holds one backend, so the released archives do not change. A Linux host that wants GLES builds with -Dgpu=gles.
  • With CHARTTABLE_NATIVE_NONE the GLES build opens libEGL at run time (surfaceless Mesa first, then the default display) and makes a headless context, for snapshots and tests. On Windows the Zig standard library cannot open a shared library, so there only a host context works.
  • charttable_attach_surface now returns CHARTTABLE_ERR_UNSUPPORTED for a kind the backend does not know. Before, @enumFromInt on such a kind was undefined behaviour. This is how a Vulkan build refuses the new kind.
  • MSAA and the overlay pass of the Vulkan backend are not in this backend. Neither is part of the Gpu API that the rest of the library calls.

How it was tested

  • zig build test -Dgpu=gles: 235 pass, 18 skip, 0 fail. The GPU tests draw through a headless EGL context. The new tests check:
    • a clear, and that the host's GL state comes back unchanged;
    • a draw into a host framebuffer: picture top up, and the size taken from the target;
    • a snapshot read back top-down;
    • pattern and sprite draws that sample their textures;
    • a staged scene that holds no GL object until adoption;
    • the full C ABI path: charttable_attach_surface with a host loader, charttable_render_gles into the host's framebuffer with its binding restored, and a snapshot that leaves the host's pixels alone.
  • The existing "first light" pixel test passes on the new backend. The tests that need chart data skip here, as on the other backends without that data.
  • zig build test on the default Vulkan backend: 231 pass, 18 skip, 0 fail. The new C ABI test checks that Vulkan refuses the kind.
  • zig fmt --check src/ build/ build.zig passes.
  • zig build lib shared -Dgpu=gles builds for x86_64-linux-gnu, aarch64-linux-gnu, x86_64-windows-gnu and aarch64-windows-gnu. The default backend still builds for aarch64-linux-gnu and x86_64-windows-gnu.
  • The header compiles as C99 and as C++ with -Wall -Werror.
  • On this machine a native build fails at link time: the system's crt1.o has .sframe relocations that the Zig 0.16 linker rejects. The same happens on main before this change. I ran the builds with -Dtarget=x86_64-linux-gnu to work around it, and the Vulkan tests also needed --search-prefix /usr. CI is not affected.
  • I have not tested inside a Qt application.

A host that already renders with OpenGL ES, such as a Qt Quick
application with a QQuickFramebufferObject, had to share Vulkan images
with its GL context to show a chart. `-Dgpu=gles` now builds the library
with an OpenGL ES 3.0 backend. It draws the same prepared scene as the
other backends, with the same vertex streams, blend state, draw order
and batcher.

The host attaches its current context with the new kind
CHARTTABLE_NATIVE_GLES_CONTEXT and a charttable_gles_context, which
carries the host's get_proc_address. The library loads every GL
function through it and links no GL library. charttable_render_gles
draws into a framebuffer the host names, at the size the host gives.
charttable_render draws into the framebuffer bound for drawing.
charttable creates no window, no EGL surface and no context, and it
never swaps buffers.

Every call that issues GL commands saves the state it changes and
restores it before it returns. The backend needs no depth attachment.
No pipeline of the Vulkan backend writes depth, so its depth test only
refuses depth 1.0, which no range uses.

makeScene runs on the build worker, where the host's context is not
current. In this backend it copies the scene to CPU memory, and
adoptScene makes the GL objects on the owner thread.

With kind CHARTTABLE_NATIVE_NONE the backend opens libEGL at run time
and makes a headless context of its own, for snapshots and for the
tests. attach_surface now refuses a kind the backend does not know with
CHARTTABLE_ERR_UNSUPPORTED, where it converted it with @enumFromInt.

The shaders are GLSL ES 3.00 ports of shaders/vk, in shaders/gles. A CI
job builds and tests the backend with Mesa through a headless EGL.
@beetlebugorg
beetlebugorg merged commit 20ad835 into main Nov 30, 2026
7 checks passed
@beetlebugorg
beetlebugorg deleted the issue-9 branch November 30, 2026 18:02
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.

Feature Request: Add an OpenGL ES Backend

1 participant