Files
harfbuzz/util/gpu-output.hh
T
Behdad EsfahbodandClaude Opus 4.6 6910b9a3ed [gpu] Add Metal backend for macOS demo
Add MSL shader translations of the Slug GPU text rasterizer, and a
Metal rendering backend for hb-gpu-demo selectable via --metal flag.

Library changes:
- Add HB_GPU_SHADER_LANG_MSL enum value
- Add hb-gpu-vertex.msl and hb-gpu-fragment.msl
- Extend gen-gpu-shader-artifacts.py for MSL

Demo changes:
- Add demo-metal.h/mm with CAMetalLayer rendering
- Add external atlas backend for non-GL paths
- Add demo_buffer_get_vertices for Metal draw path
- Skip GL init when using Metal (GLFW_NO_API)
- Add --metal command-line flag

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-29 13:36:28 -06:00

264 lines
7.6 KiB
C++

/*
* Copyright (C) 2026 Behdad Esfahbod
*
* This is part of HarfBuzz, a text shaping library.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and its documentation for any purpose, provided that the
* above copyright notice and the following two paragraphs appear in
* all copies of this software.
*
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*
* Author(s): Behdad Esfahbod
*/
#ifndef GPU_OUTPUT_HH
#define GPU_OUTPUT_HH
#include "gpu/demo-common.h"
#include "gpu/demo-buffer.h"
#include "gpu/demo-font.h"
#include "gpu/demo-glstate.h"
#include "gpu/demo-view.h"
#include "gpu/demo-metal.h"
#include "options.hh"
#include "font-options.hh"
#define WINDOW_W 700
#define WINDOW_H 700
struct gpu_output_t
{
static constexpr bool repeat_shape = false;
void add_options (option_parser_t *parser HB_UNUSED) {}
bool use_metal = false;
template <typename app_t>
void init (hb_buffer_t *buffer_ HB_UNUSED, const app_t *app)
{
font = app->font;
face = hb_font_get_face (font);
int x_scale, y_scale;
hb_font_get_scale (font, &x_scale, &y_scale);
font_size = (double) y_scale;
glfwSetErrorCallback ([] (int error, const char *desc) {
fprintf (stderr, "GLFW error %d: %s\n", error, desc);
});
if (!glfwInit ())
fail (false, "Failed to initialize GLFW");
if (use_metal)
init_metal ();
else
init_gl ();
glfwSetWindowUserPointer (window, this);
glfwSetFramebufferSizeCallback (window, [] (GLFWwindow *w, int width, int height) {
auto *self = (gpu_output_t *) glfwGetWindowUserPointer (w);
demo_view_reshape_func (self->vu, width, height);
});
glfwSetKeyCallback (window, [] (GLFWwindow *w, int key, int scancode, int action, int mods) {
auto *self = (gpu_output_t *) glfwGetWindowUserPointer (w);
demo_view_key_func (self->vu, key, scancode, action, mods);
});
glfwSetCharCallback (window, [] (GLFWwindow *w, unsigned int cp) {
auto *self = (gpu_output_t *) glfwGetWindowUserPointer (w);
demo_view_char_func (self->vu, cp);
});
glfwSetMouseButtonCallback (window, [] (GLFWwindow *w, int button, int action, int mods) {
auto *self = (gpu_output_t *) glfwGetWindowUserPointer (w);
demo_view_mouse_func (self->vu, button, action, mods);
});
glfwSetScrollCallback (window, [] (GLFWwindow *w, double xoff, double yoff) {
auto *self = (gpu_output_t *) glfwGetWindowUserPointer (w);
demo_view_scroll_func (self->vu, xoff, yoff);
});
glfwSetCursorPosCallback (window, [] (GLFWwindow *w, double x, double y) {
auto *self = (gpu_output_t *) glfwGetWindowUserPointer (w);
demo_view_motion_func (self->vu, x, y);
});
demo_atlas_t *atlas;
if (metal)
{
static demo_atlas_backend_t metal_backend = {
nullptr,
[] (void *ctx, const char *data, unsigned int len) -> unsigned int {
return demo_metal_atlas_alloc ((demo_metal_t *) ctx, data, len);
},
[] (void *ctx) -> unsigned int {
return demo_metal_atlas_get_used ((demo_metal_t *) ctx);
},
[] (void *ctx) {
demo_metal_atlas_clear ((demo_metal_t *) ctx);
},
};
metal_backend.ctx = metal;
atlas = demo_atlas_create_external (&metal_backend);
}
else
atlas = demo_glstate_get_atlas (st);
demo_font_ = demo_font_create (font, atlas);
if (metal)
demo_atlas_destroy (atlas); /* font holds a ref */
buf = demo_buffer_create ();
demo_point_t top_left = {0, 0};
demo_buffer_move_to (buf, &top_left);
demo_buffer_current_line (buf, font_size);
}
void init_gl ()
{
glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
glfwWindowHint (GLFW_SRGB_CAPABLE, GLFW_TRUE);
window = glfwCreateWindow (WINDOW_W, WINDOW_H, "HarfBuzz GPU Demo", NULL, NULL);
if (!window) {
glfwTerminate ();
fail (false, "Failed to create GLFW window");
}
glfwMakeContextCurrent (window);
glewExperimental = GL_TRUE;
glewInit ();
while (glGetError () != GL_NO_ERROR)
;
if (!glewIsSupported ("GL_VERSION_3_3"))
fail (false, "OpenGL 3.3 not supported");
{
int fb_width, fb_height;
glfwGetFramebufferSize (window, &fb_width, &fb_height);
glViewport (0, 0, fb_width, fb_height);
}
st = demo_glstate_create ();
vu = demo_view_create (st, window);
}
void init_metal ()
{
#ifdef __APPLE__
glfwWindowHint (GLFW_CLIENT_API, GLFW_NO_API);
window = glfwCreateWindow (WINDOW_W, WINDOW_H, "HarfBuzz GPU Demo (Metal)", NULL, NULL);
if (!window) {
glfwTerminate ();
fail (false, "Failed to create GLFW window");
}
metal = demo_metal_create (window, 1024 * 1024);
if (!metal) {
glfwTerminate ();
fail (false, "Failed to initialize Metal");
}
vu = demo_view_create_metal (metal, window);
#else
fail (false, "Metal is only available on macOS");
#endif
}
void new_line ()
{
demo_buffer_current_line (buf, font_size);
}
void consume_text (hb_buffer_t *buffer HB_UNUSED,
const char *text HB_UNUSED,
unsigned text_len HB_UNUSED,
hb_bool_t utf8_clusters HB_UNUSED) {}
void consume_glyphs (hb_buffer_t *buffer,
const char *text HB_UNUSED,
unsigned text_len HB_UNUSED,
hb_bool_t utf8_clusters HB_UNUSED)
{
unsigned count = hb_buffer_get_length (buffer);
hb_glyph_info_t *infos = hb_buffer_get_glyph_infos (buffer, nullptr);
hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, nullptr);
for (unsigned i = 0; i < count; i++)
demo_buffer_add_glyph (buf, demo_font_,
font_size,
infos[i].codepoint,
pos[i].x_offset,
pos[i].y_offset,
pos[i].x_advance);
}
void error (const char *message)
{
fprintf (stderr, "error: %s\n", message);
}
template <typename app_t>
void finish (hb_buffer_t *buffer_ HB_UNUSED, const app_t *app HB_UNUSED)
{
demo_font_print_stats (demo_font_);
demo_view_print_help (vu);
demo_view_setup (vu);
demo_view_display (vu, buf);
glfwPollEvents ();
demo_view_display (vu, buf);
while (!glfwWindowShouldClose (window))
{
glfwPollEvents ();
if (demo_view_should_redraw (vu))
demo_view_display (vu, buf);
else
glfwWaitEvents ();
}
demo_buffer_destroy (buf);
demo_font_destroy (demo_font_);
demo_view_destroy (vu);
if (st) demo_glstate_destroy (st);
if (metal) demo_metal_destroy (metal);
glfwDestroyWindow (window);
glfwTerminate ();
}
private:
double font_size = 1;
hb_font_t *font = nullptr;
hb_face_t *face = nullptr;
GLFWwindow *window = nullptr;
demo_glstate_t *st = nullptr;
demo_metal_t *metal = nullptr;
demo_view_t *vu = nullptr;
demo_font_t *demo_font_ = nullptr;
demo_buffer_t *buf = nullptr;
};
#endif /* GPU_OUTPUT_HH */