mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-19 14:01:10 +01:00
[util/gpu] Cross-backend palette upload via demo_renderer_t::set_palette
Reorganises the palette-upload plumbing so every backend handles
it uniformly and the hacky atlas-backend palette callback goes
away.
Before: demo_font_upload_palette in demo-font.cc mixed a GL
glUniform4fv direct call with a detour through an extra
demo_atlas_backend_t.upload_palette callback used by the Metal
renderer. demo-font and demo-atlas both knew about palettes.
After:
* demo_font_get_palette (font, out, capacity) just reports the
active palette as RGBA floats; demo-font no longer touches GL
state or the atlas for palettes.
* demo_renderer_t gains a virtual set_palette (rgba, count).
Default is a no-op. Backends override:
- demo-renderer-gl.cc -> demo_glstate_set_palette() ->
glUniform4fv.
- demo-metal.mm -> memcpy into the MTLBuffer's contents.
- demo-renderer-d3d11.cc -> ID3D11DeviceContext::UpdateSubresource
(palette buffer switched from IMMUTABLE to DEFAULT).
- hb-gpu-web-webgpu.cc -> wgpuQueueWriteBuffer on the palette
storage buffer.
* Callers invoke renderer->set_palette (palette, n) right after
demo_font_create:
- util/gpu-output.hh (desktop demo: GL / Metal / D3D11).
- util/gpu/web/hb-gpu-web.cc (WebGL).
- util/gpu/web/hb-gpu-web-webgpu.cc (WebGPU).
* The extra upload_palette field on demo_atlas_backend_t is
removed along with demo_atlas_upload_palette / _is_external --
atlas is back to being about glyph data only.
COLRv0 rendering now works with proper palette colours through
every backend that implements set_palette (WebGL, WebGPU, and
Metal/D3D11 once tested).
Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
9aec34108b
commit
cf3d69bc54
13 files changed
+94
-102
No files matched your search
@@ -141,6 +141,11 @@ struct gpu_output_t
|
||||
});
|
||||
|
||||
demo_font_ = demo_font_create (font, renderer->get_atlas ());
|
||||
{
|
||||
float palette[256 * 4] = {};
|
||||
unsigned n = demo_font_get_palette (demo_font_, palette, 256);
|
||||
if (n) renderer->set_palette (palette, n);
|
||||
}
|
||||
buf = demo_buffer_create ();
|
||||
|
||||
demo_point_t top_left = {0, 0};
|
||||
|
||||
@@ -165,21 +165,6 @@ 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)
|
||||
{
|
||||
|
||||
@@ -35,14 +35,6 @@ 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 {
|
||||
@@ -50,7 +42,6 @@ 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 *
|
||||
|
||||
+24
-67
@@ -18,67 +18,10 @@ struct demo_font_t {
|
||||
demo_atlas_t *atlas;
|
||||
hb_gpu_paint_t *p;
|
||||
|
||||
bool palette_uploaded;
|
||||
|
||||
unsigned int num_glyphs;
|
||||
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_gl (hb_face_t *face)
|
||||
{
|
||||
GLint program;
|
||||
glGetIntegerv (GL_CURRENT_PROGRAM, &program);
|
||||
if (!program) return;
|
||||
|
||||
GLint loc = glGetUniformLocation (program, "hb_gpu_palette");
|
||||
if (loc < 0)
|
||||
loc = glGetUniformLocation (program, "hb_gpu_palette[0]");
|
||||
if (loc < 0) return;
|
||||
|
||||
float palette[256 * 4] = {0};
|
||||
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)
|
||||
@@ -90,10 +33,6 @@ demo_font_create (hb_font_t *hb_font,
|
||||
font->glyph_cache = new glyph_cache_t ();
|
||||
font->atlas = demo_atlas_reference (atlas);
|
||||
font->p = hb_gpu_paint_create_or_fail ();
|
||||
/* Palette uniform needs a bound GL program; demo_font_create is
|
||||
* called before the renderer's program is made current. Upload
|
||||
* lazily on the first glyph request. */
|
||||
font->palette_uploaded = false;
|
||||
|
||||
return font;
|
||||
}
|
||||
@@ -125,18 +64,36 @@ demo_font_get_font (demo_font_t *font)
|
||||
return font->font;
|
||||
}
|
||||
|
||||
unsigned
|
||||
demo_font_get_palette (demo_font_t *font,
|
||||
float *out,
|
||||
unsigned capacity)
|
||||
{
|
||||
unsigned count = hb_ot_color_palette_get_colors (font->face, 0, 0, NULL, NULL);
|
||||
if (!count) return 0;
|
||||
if (count > capacity) count = capacity;
|
||||
|
||||
hb_color_t colors[256];
|
||||
unsigned returned = count;
|
||||
if (returned > 256) returned = 256;
|
||||
hb_ot_color_palette_get_colors (font->face, 0, 0, &returned, colors);
|
||||
|
||||
for (unsigned i = 0; i < returned; i++)
|
||||
{
|
||||
out[i * 4 + 0] = hb_color_get_red (colors[i]) / 255.f;
|
||||
out[i * 4 + 1] = hb_color_get_green (colors[i]) / 255.f;
|
||||
out[i * 4 + 2] = hb_color_get_blue (colors[i]) / 255.f;
|
||||
out[i * 4 + 3] = hb_color_get_alpha (colors[i]) / 255.f;
|
||||
}
|
||||
return returned;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
_demo_font_upload_glyph (demo_font_t *font,
|
||||
unsigned int glyph_index,
|
||||
glyph_info_t *glyph_info)
|
||||
{
|
||||
if (!font->palette_uploaded)
|
||||
{
|
||||
demo_font_upload_palette (font->face, font->atlas);
|
||||
font->palette_uploaded = true;
|
||||
}
|
||||
|
||||
hb_gpu_paint_clear (font->p);
|
||||
hb_gpu_paint_glyph (font->p, font->font, glyph_index);
|
||||
|
||||
|
||||
@@ -39,6 +39,14 @@ demo_font_lookup_glyph (demo_font_t *font,
|
||||
unsigned int glyph_index,
|
||||
glyph_info_t *glyph_info);
|
||||
|
||||
/* Returns the face's palette (index 0) as premultiplied-alpha
|
||||
* RGBA floats. Writes up to `capacity` entries to `out`; returns
|
||||
* the number of entries written. */
|
||||
unsigned
|
||||
demo_font_get_palette (demo_font_t *font,
|
||||
float *out,
|
||||
unsigned capacity);
|
||||
|
||||
void
|
||||
demo_font_clear_cache (demo_font_t *font);
|
||||
|
||||
|
||||
@@ -92,3 +92,17 @@ demo_glstate_set_stem_darkening (demo_glstate_t *st, bool enabled)
|
||||
glUniform1f (glGetUniformLocation (st->program, "u_stem_darkening"), enabled ? 1.f : 0.f);
|
||||
}
|
||||
|
||||
void
|
||||
demo_glstate_set_palette (demo_glstate_t *st,
|
||||
const float *rgba,
|
||||
unsigned count)
|
||||
{
|
||||
GLint loc = glGetUniformLocation (st->program, "hb_gpu_palette");
|
||||
if (loc < 0)
|
||||
loc = glGetUniformLocation (st->program, "hb_gpu_palette[0]");
|
||||
if (loc < 0) return;
|
||||
|
||||
if (count > 256) count = 256;
|
||||
glUniform4fv (loc, count, rgba);
|
||||
}
|
||||
|
||||
@@ -42,5 +42,10 @@ demo_glstate_set_debug (demo_glstate_t *st, bool enabled);
|
||||
void
|
||||
demo_glstate_set_stem_darkening (demo_glstate_t *st, bool enabled);
|
||||
|
||||
void
|
||||
demo_glstate_set_palette (demo_glstate_t *st,
|
||||
const float *rgba,
|
||||
unsigned count);
|
||||
|
||||
|
||||
#endif /* DEMO_GLSTATE_H */
|
||||
@@ -181,19 +181,17 @@ 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;
|
||||
}
|
||||
|
||||
void set_palette (const float *rgba, unsigned count) override
|
||||
{
|
||||
if (count > 256) count = 256;
|
||||
memcpy (paletteBuffer.contents, rgba, count * 4 * sizeof (float));
|
||||
}
|
||||
|
||||
|
||||
/* -- State -- */
|
||||
|
||||
@@ -506,7 +504,6 @@ 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);
|
||||
|
||||
|
||||
@@ -204,11 +204,11 @@ float4 ps_main (PSInput input) : SV_Target {
|
||||
device->CreateShaderResourceView (atlas.srv_buf, &srvd, &atlas.srv);
|
||||
}
|
||||
|
||||
/* Palette: 256 float4, zero-filled for now. */
|
||||
/* Palette: 256 float4, updated via set_palette(). */
|
||||
{
|
||||
D3D11_BUFFER_DESC pbd = {};
|
||||
pbd.ByteWidth = 256 * 4 * sizeof (float);
|
||||
pbd.Usage = D3D11_USAGE_IMMUTABLE;
|
||||
pbd.Usage = D3D11_USAGE_DEFAULT;
|
||||
pbd.BindFlags = D3D11_BIND_SHADER_RESOURCE;
|
||||
pbd.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED;
|
||||
pbd.StructureByteStride = 16;
|
||||
@@ -293,6 +293,13 @@ float4 ps_main (PSInput input) : SV_Target {
|
||||
void set_background (float r, float g, float b, float a) override { bg_r = r; bg_g = g; bg_b = b; bg_a = a; }
|
||||
void set_debug (bool e) override { debug_val = e ? 1.f : 0.f; }
|
||||
void set_stem_darkening (bool e) override { stem_val = e ? 1.f : 0.f; }
|
||||
void set_palette (const float *rgba, unsigned count) override
|
||||
{
|
||||
if (count > 256) count = 256;
|
||||
float buf[256 * 4] = {};
|
||||
memcpy (buf, rgba, count * 4 * sizeof (float));
|
||||
ctx->UpdateSubresource (palette_buf, 0, nullptr, buf, 0, 0);
|
||||
}
|
||||
bool set_srgb (bool) override { return false; }
|
||||
void toggle_vsync (bool &v) override { v = !v; vsync_on = v; }
|
||||
|
||||
|
||||
@@ -111,6 +111,11 @@ struct demo_renderer_gl_t : demo_renderer_t
|
||||
demo_glstate_set_stem_darkening (st, enabled);
|
||||
}
|
||||
|
||||
void set_palette (const float *rgba, unsigned count) override
|
||||
{
|
||||
demo_glstate_set_palette (st, rgba, count);
|
||||
}
|
||||
|
||||
bool set_srgb (bool enabled) override
|
||||
{
|
||||
#if defined(GL_FRAMEBUFFER_SRGB)
|
||||
|
||||
@@ -28,6 +28,13 @@ struct demo_renderer_t
|
||||
virtual void set_debug (bool enabled) = 0;
|
||||
virtual void set_stem_darkening (bool enabled) { (void) enabled; }
|
||||
|
||||
/* Upload a palette for the hb_gpu_palette uniform/buffer.
|
||||
* rgba: count entries, 4 floats each (RGBA [0..1]). Backends
|
||||
* that support fewer entries clamp; backends that don't support
|
||||
* colour paint at all may no-op. */
|
||||
virtual void set_palette (const float *rgba, unsigned count)
|
||||
{ (void) rgba; (void) count; }
|
||||
|
||||
/* sRGB framebuffer control; returns false if unsupported. */
|
||||
virtual bool set_srgb (bool enabled) = 0;
|
||||
|
||||
|
||||
@@ -245,6 +245,13 @@ struct demo_renderer_webgpu_t : demo_renderer_t
|
||||
|
||||
void set_debug (bool enabled) override { debug_mode = enabled; }
|
||||
void set_stem_darkening (bool enabled) override { stem_mode = enabled; }
|
||||
void set_palette (const float *rgba, unsigned count) override
|
||||
{
|
||||
if (count > 256) count = 256;
|
||||
float buf[256 * 4] = {};
|
||||
memcpy (buf, rgba, count * 4 * sizeof (float));
|
||||
wgpuQueueWriteBuffer (g_queue, g_palette_buf, 0, buf, sizeof (buf));
|
||||
}
|
||||
bool debug_mode = false;
|
||||
bool stem_mode = true;
|
||||
|
||||
@@ -417,6 +424,7 @@ web_load_font (const char *data, int len)
|
||||
|
||||
demo_font_destroy (current_demo_font);
|
||||
current_demo_font = demo_font_create (font, renderer->get_atlas ());
|
||||
{ float palette[256*4] = {}; unsigned n = demo_font_get_palette (current_demo_font, palette, 256); if (n) renderer->set_palette (palette, n); }
|
||||
|
||||
rebuild_buffer (custom_text ? current_text : default_text_en);
|
||||
demo_font_print_stats (current_demo_font);
|
||||
@@ -1009,6 +1017,7 @@ init_demo ()
|
||||
current_face = hb_face_create (current_blob, 0);
|
||||
current_font = hb_font_create (current_face);
|
||||
current_demo_font = demo_font_create (current_font, renderer->get_atlas ());
|
||||
{ float palette[256*4] = {}; unsigned n = demo_font_get_palette (current_demo_font, palette, 256); if (n) renderer->set_palette (palette, n); }
|
||||
|
||||
current_text = strdup (default_text_combined);
|
||||
|
||||
|
||||
@@ -102,6 +102,7 @@ web_load_font (const char *data, int len)
|
||||
/* Flush old font state */
|
||||
demo_font_destroy (current_demo_font);
|
||||
current_demo_font = demo_font_create (font, renderer->get_atlas ());
|
||||
{ float palette[256*4] = {}; unsigned n = demo_font_get_palette (current_demo_font, palette, 256); if (n) renderer->set_palette (palette, n); }
|
||||
|
||||
rebuild_buffer (custom_text ? current_text : default_text_en);
|
||||
demo_font_print_stats (current_demo_font);
|
||||
@@ -236,6 +237,7 @@ main ()
|
||||
current_face = hb_face_create (current_blob, 0);
|
||||
current_font = hb_font_create (current_face);
|
||||
current_demo_font = demo_font_create (current_font, renderer->get_atlas ());
|
||||
{ float palette[256*4] = {}; unsigned n = demo_font_get_palette (current_demo_font, palette, 256); if (n) renderer->set_palette (palette, n); }
|
||||
|
||||
current_text = strdup (default_text_combined);
|
||||
|
||||
|
||||
Reference in new issue
Block a user