Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ on:
# charttable builds with Zig 0.16 into libcharttable.a, libcharttable.so and the
# C header. Every consumer takes one of those three, so CI builds all of them.
#
# Three jobs, because no single runner compiles the whole library:
# Four jobs, because no single runner compiles the whole library:
# * build-test — the gate. Vulkan backend, system codecs, the full test suite.
# * gles — the OpenGL ES backend (-Dgpu=gles) and the full test suite,
# drawn with Mesa's software renderer through a headless EGL.
# * macos — the only runner that compiles the Metal backend and the ObjC
# shim, and the only one that builds the demo app.
# * cross — every target a release ships, so a tag does not discover a
Expand Down Expand Up @@ -48,6 +50,30 @@ jobs:
- name: Build the shared library
run: zig build shared

gles:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7

# The backend links no GL library. Its tests open libEGL at run time and
# draw with Mesa's llvmpipe on the surfaceless platform, so the runner
# needs no display.
- name: Install EGL, Mesa and the codecs
run: |
sudo apt-get update
sudo apt-get install -y libegl1 libgles2 libegl-mesa0 libgl1-mesa-dri libwebp-dev libpng-dev

- name: Install Zig
uses: mlugg/setup-zig@v2
with:
version: 0.16.0

- name: Build and test
run: zig build install test -Dgpu=gles

- name: Build the shared library
run: zig build shared -Dgpu=gles

macos:
runs-on: macos-latest
steps:
Expand Down
2 changes: 2 additions & 0 deletions DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ charttable/
gpu_metal.zig + metal_shim.m/.h
gpu_vk.zig + c_vk.zig
gpu_d3d12.zig + c_d3d12.zig
gpu_gles.zig OpenGL ES 3.0 (-Dgpu=gles): the host's
context and framebuffer, GL loaded by name
gpu_sdl.zig + c_sdl.zig
raster.zig raster sources/layers (rework of
lookout raster.zig — spec "raster" type)
Expand Down
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

---

charttable is one Zig library behind a C ABI. It draws through Metal, Vulkan or Direct3D 12, so the same library serves macOS, iOS, Linux, Windows and Android. It does no networking. Tiles come from a local pmtiles archive, or from your code through a resource callback.
charttable is one Zig library behind a C ABI. It draws through Metal, Vulkan or Direct3D 12, so the same library serves macOS, iOS, Linux, Windows and Android. A build with `-Dgpu=gles` draws through OpenGL ES 3.0 instead, into a framebuffer of a context the host owns. It does no networking. Tiles come from a local pmtiles archive, or from your code through a resource callback.

## Install

Expand Down Expand Up @@ -49,6 +49,21 @@ charttable_tick(ct, dt_ms);
if (charttable_needs_redraw(ct)) charttable_render(ct);
```

A host with its own OpenGL ES context, such as a Qt Quick `QQuickFramebufferObject`, attaches that context and draws into its framebuffer. charttable loads the GL functions through the host's loader, so the library links no GL library. It creates no window and no EGL surface, and it never swaps buffers. Each call restores the GL state it changed.

```c
static void *get_proc(const char *name, void *user) {
return (void *)eglGetProcAddress(name);
}

charttable_gles_context gl = { .get_proc_address = get_proc };
charttable_attach_surface(ct, CHARTTABLE_NATIVE_GLES_CONTEXT, &gl, w_px, h_px);

/* each frame, with the context current */
charttable_gles_target t = { .framebuffer = fbo, .width = w_px, .height = h_px };
charttable_render_gles(ct, &t);
```

The API is one opaque handle and about forty functions. A mutex inside the handle serializes them, so every call is safe from any thread. The three functions that touch the surface must be called from the thread that owns it, because the platform requires that.

## What it renders
Expand All @@ -64,7 +79,7 @@ A layer or property charttable does not support yet is skipped, and the reason g
- **A tile is turned into triangles once.** After that, a pan, zoom or rotation draws the same geometry with a new matrix. A map sitting still uses no CPU.
- **Restyling touches the paint stream only.** Geometry and evaluated paint sit in separate vertex streams. Changing a color, an opacity or a whole palette re-uploads the paint and leaves the geometry alone, so a day-to-night switch lands in one frame.
- **Features appear at the zoom the style names.** Each vertex carries the zoom range it is visible in, so a feature fades in at 12.4 if the style says 12.4, not at 13.
- **One scene, three backends.** Metal, Vulkan and D3D12 draw from the same prepared scene, so a fix in the renderer reaches all three.
- **One scene, four backends.** Metal, Vulkan, D3D12 and OpenGL ES draw from the same prepared scene, so a fix in the renderer reaches all of them.

## Build

Expand All @@ -76,7 +91,7 @@ zig build lib # libcharttable.a and the header, into zig-out
zig build shared # the shared library, beside it
```

Add `-Dcodec-source` to compile libwebp and libpng in rather than link the platform copies. A cross build needs it. `-Dgpu=vk` picks the Vulkan backend on Windows, where D3D12 is the default. `-Dversion` sets what `charttable_version()` reports and what the shared library stamps into its soname. A release passes the tag, and a source build reports `0.0.0-dev`.
Add `-Dcodec-source` to compile libwebp and libpng in rather than link the platform copies. A cross build needs it. `-Dgpu=vk` picks the Vulkan backend on Windows, where D3D12 is the default. `-Dgpu=gles` picks the OpenGL ES 3.0 backend on any target off Apple. Its tests draw with a headless context from libEGL, and skip when libEGL is absent. `-Dversion` sets what `charttable_version()` reports and what the shared library stamps into its soname. A release passes the tag, and a source build reports `0.0.0-dev`.

## Status

Expand Down
32 changes: 27 additions & 5 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ pub fn build(b: *std.Build) void {

// Windows can draw with either backend: D3D12 is the default there, and
// the Vulkan backend has a win32 surface path for a host that would rather
// run on the loader. Everywhere else this option has nothing to choose.
const GpuBackend = enum { auto, vk, d3d12 };
const gpu_choice = b.option(GpuBackend, "gpu", "Windows renderer backend: d3d12 (default) or vk") orelse .auto;
const use_d3d12 = windows and gpu_choice != .vk;
const use_vk = !apple and !use_d3d12;
// run on the loader. `gles` picks the OpenGL ES 3.0 backend on any target
// off Apple, for a host that draws with a GL context of its own.
const GpuBackend = enum { auto, vk, d3d12, gles };
const gpu_choice = b.option(GpuBackend, "gpu", "Renderer backend: d3d12 (Windows default), vk (default off Apple and Windows), or gles") orelse .auto;
if (apple and gpu_choice == .gles) @panic("-Dgpu=gles: Apple targets draw with Metal");
const use_gles = !apple and gpu_choice == .gles;
const use_d3d12 = windows and !use_gles and gpu_choice != .vk;
const use_vk = !apple and !use_d3d12 and !use_gles;

// The library module every consumer imports (`@import("charttable")`).
const mod = b.addModule("charttable", .{
Expand Down Expand Up @@ -78,6 +81,7 @@ pub fn build(b: *std.Build) void {
ct_opts.addOption(bool, "libpng", use_libpng);
// Which renderer src/gpu/gpu.zig selects on Windows.
ct_opts.addOption(bool, "gpu_d3d12", use_d3d12);
ct_opts.addOption(bool, "gpu_gles", use_gles);
// Where the vendored spec conformance fixtures live (absolute, so the
// test binary finds them regardless of its own cwd). The harness skips
// itself when the directory is absent.
Expand Down Expand Up @@ -135,6 +139,24 @@ pub fn build(b: *std.Build) void {
for (hlsl) |e| m.addAnonymousImport(e[0], .{ .root_source_file = b.path(e[1]) });
}

// The OpenGL ES backend (src/gpu/gpu_gles.zig). The GLSL ES rides in
// as source and the context compiles it at open. Every GL function
// comes from the host's loader, or from libEGL opened at run time for
// a snapshot, so the build links no GL library.
if (use_gles) {
const glsl = [_][2][]const u8{
.{ "gles_prelude", "shaders/gles/prelude.glsl" },
.{ "gles_fill_vert", "shaders/gles/fill.vert" },
.{ "gles_fill_frag", "shaders/gles/fill.frag" },
.{ "gles_pattern_vert", "shaders/gles/pattern.vert" },
.{ "gles_pattern_frag", "shaders/gles/pattern.frag" },
.{ "gles_sprite_vert", "shaders/gles/sprite.vert" },
.{ "gles_sprite_frag", "shaders/gles/sprite.frag" },
.{ "gles_sdf_frag", "shaders/gles/sdf.frag" },
};
for (glsl) |e| m.addAnonymousImport(e[0], .{ .root_source_file = b.path(e[1]) });
}

if (use_vk) {
const spv = [_][2][]const u8{
.{ "fill_vert_spv", "shaders/vk/fill.vert.spv" },
Expand Down
57 changes: 54 additions & 3 deletions include/charttable.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
* One opaque handle, one mutex behind it. Every function here is safe to call
* from any thread and is serialized internally, with ONE exception that is a
* platform rule rather than ours: charttable_attach_surface, _render and
* _snapshot_rgba must be called from the thread that owns the surface.
* _snapshot_rgba must be called from the thread that owns the surface. With
* an OpenGL ES context (CHARTTABLE_NATIVE_GLES_CONTEXT), _render_gles,
* _detach_surface and _close join them, and the context must be current on
* that thread for each call.
*
* Conventions:
* - Sizes and positions are LOGICAL POINTS; the library scales by the
Expand Down Expand Up @@ -70,13 +73,52 @@ typedef enum {
CHARTTABLE_NATIVE_X11_WINDOW = 5, /* charttable_x11_window* (vk) */
CHARTTABLE_NATIVE_ANDROID_WINDOW = 7, /* ANativeWindow* (vk) */
CHARTTABLE_NATIVE_WAYLAND_SURFACE = 8, /* charttable_wayland_surface* (vk) */
CHARTTABLE_NATIVE_D3D12_PANEL = 10 /* no handle (d3d12 backend): the
CHARTTABLE_NATIVE_D3D12_PANEL = 10, /* no handle (d3d12 backend): the
* renderer makes a composition
* swapchain and the host composes
* it; fetch it with
* charttable_d3d12_swapchain */
CHARTTABLE_NATIVE_GLES_CONTEXT = 11 /* charttable_gles_context* (gles
* backend, -Dgpu=gles) */
} charttable_native_kind;

/* ---- OpenGL ES (a library built with -Dgpu=gles) -------------------------
*
* charttable draws with the host's OpenGL ES 3.0 context. It creates no
* window, no EGLSurface and no context, and it never swaps buffers. It loads
* every GL function through get_proc_address, so the library links no GL
* library. Make the context current before attach, render, render_gles,
* snapshot, detach and close, on the thread that owns it.
*
* Each of those calls saves the GL state it changes and restores it before
* it returns: the framebuffer bindings, the viewport, the enables, the blend
* state, the color mask, the clear color, the program, the vertex array, the
* array, uniform and pixel buffer bindings, the texture and sampler of unit
* 0, and the pack and unpack parameters. charttable needs no depth or stencil
* attachment: it draws in paint order.
*
* In a library with this backend, kind CHARTTABLE_NATIVE_NONE opens libEGL at
* run time and makes a headless context of its own, for snapshots. */

/* Return the address of the GL function `name` (for example "glDrawArrays"),
* or NULL. eglGetProcAddress, or QOpenGLContext::getProcAddress, does this. */
typedef void *(*charttable_gles_get_proc)(const char *name, void *user);

/* `native` for CHARTTABLE_NATIVE_GLES_CONTEXT. charttable reads it only
* during charttable_attach_surface. */
typedef struct {
charttable_gles_get_proc get_proc_address;
void *user; /* passed back to get_proc_address */
} charttable_gles_context;

/* A framebuffer of the host's context. 0 names the default framebuffer.
* width and height are pixels. */
typedef struct {
uint32_t framebuffer;
uint32_t width;
uint32_t height;
} charttable_gles_target;

/* Create the render surface. `native` is a CAMetalLayer* when kind is
* CHARTTABLE_NATIVE_METAL_LAYER, NULL for CHARTTABLE_NATIVE_D3D12_PANEL and
* for offscreen-only rendering. */
Expand Down Expand Up @@ -261,9 +303,18 @@ int charttable_idle(charttable *);
uint32_t charttable_pending_tiles(charttable *);

/* Draw into the attached surface. Returns 1 if it drew, 0 if there was
* nothing to draw, negative on error. */
* nothing to draw, negative on error. With CHARTTABLE_NATIVE_GLES_CONTEXT it
* draws into the framebuffer bound for drawing, at the size of the last
* charttable_resize times the pixel density. */
int charttable_render(charttable *);

/* OpenGL ES only. Clear `target` to the style's background and draw a frame
* into it. The size of the target becomes the size of the frame in pixels, so
* a resized framebuffer needs only a new target; call charttable_resize too,
* for the viewport in points. Returns 1 if it drew, CHARTTABLE_ERR_UNSUPPORTED
* on another backend or kind, other negative values on error. */
int charttable_render_gles(charttable *, const charttable_gles_target *target);

/* Render offscreen and copy RGBA8 (top-down, w*h*4) into dst. */
int charttable_snapshot_rgba(charttable *, uint8_t *dst, size_t len);

Expand Down
28 changes: 28 additions & 0 deletions shaders/gles/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# GLSL ES 3.00 shaders (OpenGL ES)

charttable's scene contract (src/scene/types.zig) stated in GLSL ES 3.00, for
the backend that `-Dgpu=gles` selects. There is no build-time shader step:
gpu_gles.zig embeds these files and the host's context compiles them at open.
Every stage is compiled as two strings, `prelude.glsl` and then the stage, so
the uniform block and the per-draw uniforms are declared once.

Each stage is a port of the file of the same name in `shaders/vk/`. Keep them
in step with those, and with `shaders/metal.metal`, the reference statement of
the contract.

## Differences from the Vulkan shaders

- The block `U` is `scene.Uniforms`, std140, at uniform buffer binding 0. The
backend writes it once per frame.
- The fields that change per draw are plain uniforms: `u_clip`
(`clip_rect`), `u_cell` (`cell_px`) and `u_halo` (`color`).
- `pattern.frag` counts `gl_FragCoord.y` from the top row with `u_fb_h`, the
target height. GL counts it from the bottom row, and the other backends
from the top.
- Every sampler reads texture unit 0.

## The vertex layout

The attribute locations are the ones in `shaders/vk/README.md`. The integer
attributes (`a_zwin`, `a_flags`, `a_pack`) use `glVertexAttribIPointer`, and
the paint streams are normalized `GL_UNSIGNED_BYTE` x4.
15 changes: 15 additions & 0 deletions shaders/gles/fill.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Flat color from stream B, clipped to the tile of the draw. A port of
// shaders/vk/fill.frag. prelude.glsl comes first.
in vec4 v_color;
in vec2 v_world;
layout(location = 0) out vec4 o_color;

bool clipped(vec2 world) {
return world.x < u_clip.x || world.y < u_clip.y ||
world.x > u_clip.z || world.y > u_clip.w;
}

void main() {
if (clipped(v_world)) discard;
o_color = v_color;
}
45 changes: 45 additions & 0 deletions shaders/gles/fill.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// charttable fill vertex shader (OpenGL ES 3.0): flat-color triangles, for
// area fills and line work. A port of shaders/vk/fill.vert. Keep the two in
// step. prelude.glsl comes first.
layout(location = 0) in vec2 a_pos;
layout(location = 1) in vec2 a_off;
layout(location = 2) in uint a_zwin;
layout(location = 3) in uint a_flags;
layout(location = 4) in float a_depth;
layout(location = 5) in vec4 a_color;
layout(location = 6) in vec4 a_color_hi;

out vec4 v_color;
out vec2 v_world;

vec2 world_of(vec2 p, vec2 off) {
vec2 back = vec2( off.x * u.rot_cos + off.y * u.rot_sin,
-off.x * u.rot_sin + off.y * u.rot_cos);
return p + back * u.size_scale * u.world_per_px;
}

bool gate(uint zwin) {
float zmin = float(zwin & 0xFFFFu);
float zmax = float(zwin >> 16);
return zmin <= u.zoom && u.zoom <= zmax;
}

vec2 screen_offset(vec2 off, uint flags) {
if ((flags & 1u) != 0u) {
off = vec2(off.x * u.rot_cos - off.y * u.rot_sin,
off.x * u.rot_sin + off.y * u.rot_cos);
}
return off;
}

void main() {
vec4 clip = u.mvp * vec4(a_pos, 0.0, 1.0);
vec2 off = screen_offset(a_off, a_flags);
float wslope = (float((a_flags >> 8) & 0xFFu) - 128.0) * (1.0 / 32.0);
off *= exp2(wslope * u.zoom_t);
clip.xy += off * u.px_to_clip * u.size_scale * clip.w;
clip.z = a_depth * clip.w;
gl_Position = gate(a_zwin) ? clip : vec4(0.0, 0.0, 2.0, 1.0); // z=2 -> clipped
v_color = mix(a_color, a_color_hi, clamp(u.zoom_t, 0.0, 1.0));
v_world = world_of(a_pos, off);
}
23 changes: 23 additions & 0 deletions shaders/gles/pattern.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Phase = (fragment - world origin) / cell, in framebuffer px, with y counted
// from the top row as the other backends count it. A port of
// shaders/vk/pattern.frag. prelude.glsl comes first.
uniform sampler2D cell;

in vec2 v_anchor;
in vec2 v_world;
layout(location = 0) out vec4 o_color;

bool clipped(vec2 world) {
return world.x < u_clip.x || world.y < u_clip.y ||
world.x > u_clip.z || world.y > u_clip.w;
}

void main() {
if (clipped(v_world)) discard;
vec2 frag = vec2(gl_FragCoord.x, u_fb_h - gl_FragCoord.y);
vec2 sz = max(u_cell, vec2(1.0));
vec2 uv = fract((frag - v_anchor) / sz);
vec4 c = texture(cell, uv);
if (c.a < 0.02) discard;
o_color = c;
}
34 changes: 34 additions & 0 deletions shaders/gles/pattern.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Area fills tiled from a pattern cell. A port of shaders/vk/pattern.vert.
// prelude.glsl comes first.
layout(location = 0) in vec2 a_pos;
layout(location = 1) in vec2 a_off;
layout(location = 2) in uint a_zwin;
layout(location = 3) in uint a_flags;
layout(location = 4) in float a_depth;

out vec2 v_anchor;
out vec2 v_world;

vec2 world_of(vec2 p, vec2 off) {
vec2 back = vec2( off.x * u.rot_cos + off.y * u.rot_sin,
-off.x * u.rot_sin + off.y * u.rot_cos);
return p + back * u.size_scale * u.world_per_px;
}

void main() {
vec4 clip = u.mvp * vec4(a_pos, 0.0, 1.0);
vec2 off = a_off;
if ((a_flags & 1u) != 0u) {
off = vec2(off.x * u.rot_cos - off.y * u.rot_sin,
off.x * u.rot_sin + off.y * u.rot_cos);
}
clip.xy += off * u.px_to_clip * u.size_scale * clip.w;
clip.z = a_depth * clip.w;

float zmin = float(a_zwin & 0xFFFFu);
float zmax = float(a_zwin >> 16);
bool vis = zmin <= u.zoom && u.zoom <= zmax;
gl_Position = vis ? clip : vec4(0.0, 0.0, 2.0, 1.0);
v_anchor = u.anchor_px;
v_world = world_of(a_pos, off);
}
Loading
Loading