[gpu] Fix Metal segfault: route palette upload through atlas backend

The Metal path creates a GLFW window with GLFW_NO_API (no GL
context), but demo_font_upload_palette() unconditionally called
glGetIntegerv(), causing a segfault on the first glyph lookup.

Split palette upload into GL and external-backend paths:

- Extract demo_font_get_palette() to read CPAL colors into a
  float array, shared by both paths.
- GL path (demo_font_upload_palette_gl) uploads via glUniform4fv
  as before.
- External-backend path uploads via a new upload_palette callback
  in demo_atlas_backend_t.
- Metal renderer implements the callback, writing palette data
  directly to its paletteBuffer.

This fixes the --metal crash and enables proper COLRv0 palette
color rendering on Metal.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Behdad Esfahbod committed 2026-04-13 15:59:29 -06:00
1 parent dfd5d5b3cb
commit 9aec34108b
4 files changed
+74 -17

No files matched your search

+15
View File
@@ -165,6 +165,21 @@ demo_atlas_alloc (demo_atlas_t *at,
return offset;
}
hb_bool_t
demo_atlas_is_external (demo_atlas_t *at)
{
return at->ext.ctx != nullptr;
}
void
demo_atlas_upload_palette (demo_atlas_t *at,
const float *rgba,
unsigned int count)
{
if (at->ext.ctx && at->ext.upload_palette)
at->ext.upload_palette (at->ext.ctx, rgba, count);
}
unsigned int
demo_atlas_get_used (demo_atlas_t *at)
{
+9
View File
@@ -35,6 +35,14 @@ demo_atlas_clear (demo_atlas_t *at);
void
demo_atlas_set_uniforms (demo_atlas_t *at);
hb_bool_t
demo_atlas_is_external (demo_atlas_t *at);
void
demo_atlas_upload_palette (demo_atlas_t *at,
const float *rgba,
unsigned int count);
/* Create an atlas backed by external callbacks (for Metal, etc.) */
typedef struct {
@@ -42,6 +50,7 @@ typedef struct {
unsigned int (*alloc) (void *ctx, const char *data, unsigned int len_bytes);
unsigned int (*get_used) (void *ctx);
void (*clear) (void *ctx);
void (*upload_palette) (void *ctx, const float *rgba, unsigned int count);
} demo_atlas_backend_t;
demo_atlas_t *
+41 -17
View File
@@ -3,6 +3,7 @@
*/
#include "demo-font.h"
#include "demo-atlas.h"
#include <hb-ot.h>
@@ -23,8 +24,30 @@ struct demo_font_t {
unsigned int sum_bytes;
};
static unsigned
demo_font_get_palette (hb_face_t *face, float *palette)
{
unsigned count = hb_ot_color_palette_get_colors (face, 0, 0, NULL, NULL);
if (!count) return 0;
if (count > 256) count = 256;
hb_color_t colors[256];
unsigned returned = count;
hb_ot_color_palette_get_colors (face, 0, 0, &returned, colors);
for (unsigned i = 0; i < returned; i++)
{
palette[i * 4 + 0] = hb_color_get_red (colors[i]) / 255.f;
palette[i * 4 + 1] = hb_color_get_green (colors[i]) / 255.f;
palette[i * 4 + 2] = hb_color_get_blue (colors[i]) / 255.f;
palette[i * 4 + 3] = hb_color_get_alpha (colors[i]) / 255.f;
}
return returned;
}
static void
demo_font_upload_palette (hb_face_t *face)
demo_font_upload_palette_gl (hb_face_t *face)
{
GLint program;
glGetIntegerv (GL_CURRENT_PROGRAM, &program);
@@ -35,26 +58,27 @@ demo_font_upload_palette (hb_face_t *face)
loc = glGetUniformLocation (program, "hb_gpu_palette[0]");
if (loc < 0) return;
unsigned count = hb_ot_color_palette_get_colors (face, 0, 0, NULL, NULL);
if (!count) return;
if (count > 256) count = 256;
hb_color_t colors[256];
unsigned returned = count;
hb_ot_color_palette_get_colors (face, 0, 0, &returned, colors);
float palette[256 * 4] = {0};
for (unsigned i = 0; i < returned; i++)
{
palette[i * 4 + 0] = hb_color_get_red (colors[i]) / 255.f;
palette[i * 4 + 1] = hb_color_get_green (colors[i]) / 255.f;
palette[i * 4 + 2] = hb_color_get_blue (colors[i]) / 255.f;
palette[i * 4 + 3] = hb_color_get_alpha (colors[i]) / 255.f;
}
unsigned count = demo_font_get_palette (face, palette);
if (!count) return;
glUniform4fv (loc, 256, palette);
}
static void
demo_font_upload_palette (hb_face_t *face, demo_atlas_t *atlas)
{
if (demo_atlas_is_external (atlas))
{
float palette[256 * 4] = {0};
unsigned count = demo_font_get_palette (face, palette);
if (count)
demo_atlas_upload_palette (atlas, palette, 256);
}
else
demo_font_upload_palette_gl (face);
}
demo_font_t *
demo_font_create (hb_font_t *hb_font,
demo_atlas_t *atlas)
@@ -109,7 +133,7 @@ _demo_font_upload_glyph (demo_font_t *font,
{
if (!font->palette_uploaded)
{
demo_font_upload_palette (font->face);
demo_font_upload_palette (font->face, font->atlas);
font->palette_uploaded = true;
}
+9
View File
@@ -181,6 +181,14 @@ struct demo_renderer_metal_t : demo_renderer_t
self->atlas_dirty = true;
}
static void
atlas_upload_palette_cb (void *ctx, const float *rgba, unsigned int count)
{
auto *self = (demo_renderer_metal_t *) ctx;
if (count > 256) count = 256;
memcpy (self->paletteBuffer.contents, rgba, count * 4 * sizeof (float));
}
demo_atlas_t *get_atlas () override
{
return atlas;
@@ -498,6 +506,7 @@ demo_renderer_create_metal (GLFWwindow *window)
demo_renderer_metal_t::atlas_alloc_cb,
demo_renderer_metal_t::atlas_get_used_cb,
demo_renderer_metal_t::atlas_clear_cb,
demo_renderer_metal_t::atlas_upload_palette_cb,
};
r->atlas = demo_atlas_create_external (&backend);