mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-30 01:54:21 +01:00
[gpu] Add Emscripten web demo; mobile touch support
Web build using Emscripten + WebGL2 with HB_GPU_ATLAS_2D mode. Touch support: drag to pan, tap to toggle animation. FPS counter overlay. Viewport meta for mobile. Fix cursor position on first touch by querying glfwGetCursorPos in mouse_func. Left click/tap also toggles animation. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
d46f8e3444
commit
90e8b99cd5
7 files changed
+318
-11
No files matched your search
+6
-6
@@ -73,7 +73,7 @@
|
||||
* backed by a GL_RGBA16I format buffer. Each glyph occupies a
|
||||
* contiguous range of texels at a known offset:
|
||||
*
|
||||
* |[<!-- language="C" -->
|
||||
* |[<!-- language="plain" -->
|
||||
* GLuint buf, tex;
|
||||
* glGenBuffers (1, &buf);
|
||||
* glGenTextures (1, &tex);
|
||||
@@ -97,7 +97,7 @@
|
||||
* function and a fragment rendering function. Prepend a #version
|
||||
* directive and append your own main():
|
||||
*
|
||||
* |[<!-- language="C" -->
|
||||
* |[<!-- language="plain" -->
|
||||
* unsigned vert_count, frag_count;
|
||||
* const char * const *vert_lib = hb_gpu_shader_vertex_sources (
|
||||
* HB_GPU_SHADER_LANG_GLSL, &vert_count);
|
||||
@@ -117,7 +117,7 @@
|
||||
*
|
||||
* The vertex library provides one function:
|
||||
*
|
||||
* |[<!-- language="GLSL" -->
|
||||
* |[<!-- language="plain" -->
|
||||
* void hb_gpu_dilate (inout vec2 position, inout vec2 texcoord,
|
||||
* vec2 normal, vec4 jac,
|
||||
* mat4 m, vec2 viewport);
|
||||
@@ -163,7 +163,7 @@
|
||||
*
|
||||
* A typical vertex main:
|
||||
*
|
||||
* |[<!-- language="GLSL" -->
|
||||
* |[<!-- language="plain" -->
|
||||
* uniform mat4 u_matViewProjection;
|
||||
* uniform vec2 u_viewport;
|
||||
*
|
||||
@@ -198,7 +198,7 @@
|
||||
*
|
||||
* The fragment library provides one function:
|
||||
*
|
||||
* |[<!-- language="GLSL" -->
|
||||
* |[<!-- language="plain" -->
|
||||
* float hb_gpu_render (vec2 renderCoord, uint glyphLoc);
|
||||
* ]|
|
||||
*
|
||||
@@ -225,7 +225,7 @@
|
||||
*
|
||||
* A typical fragment main:
|
||||
*
|
||||
* |[<!-- language="GLSL" -->
|
||||
* |[<!-- language="plain" -->
|
||||
* in vec2 v_texcoord;
|
||||
* flat in uint v_glyphLoc;
|
||||
* out vec4 fragColor;
|
||||
|
||||
+49
-2
@@ -6,12 +6,18 @@
|
||||
|
||||
#define TEXEL_SIZE 8 /* sizeof RGBA16I texel */
|
||||
|
||||
#ifdef HB_GPU_ATLAS_2D
|
||||
#define ATLAS_TEX_WIDTH 4096
|
||||
#endif
|
||||
|
||||
struct demo_atlas_t {
|
||||
unsigned int refcount;
|
||||
|
||||
GLuint tex_unit;
|
||||
GLuint tex_name;
|
||||
#ifndef HB_GPU_ATLAS_2D
|
||||
GLuint buf_name;
|
||||
#endif
|
||||
GLuint capacity; /* in texels */
|
||||
GLuint cursor; /* in texels */
|
||||
};
|
||||
@@ -27,16 +33,25 @@ demo_atlas_create (unsigned int capacity)
|
||||
at->refcount = 1;
|
||||
|
||||
glGetIntegerv (GL_ACTIVE_TEXTURE, (GLint *) &at->tex_unit);
|
||||
glGenBuffers (1, &at->buf_name);
|
||||
glGenTextures (1, &at->tex_name);
|
||||
at->capacity = capacity;
|
||||
at->cursor = 0;
|
||||
|
||||
#ifdef HB_GPU_ATLAS_2D
|
||||
demo_atlas_bind_texture (at);
|
||||
unsigned height = (capacity + ATLAS_TEX_WIDTH - 1) / ATLAS_TEX_WIDTH;
|
||||
gl(TexImage2D) (GL_TEXTURE_2D, 0, GL_RGBA16I,
|
||||
ATLAS_TEX_WIDTH, height, 0,
|
||||
GL_RGBA_INTEGER, GL_SHORT, NULL);
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
#else
|
||||
glGenBuffers (1, &at->buf_name);
|
||||
glBindBuffer (GL_TEXTURE_BUFFER, at->buf_name);
|
||||
glBufferData (GL_TEXTURE_BUFFER, capacity * TEXEL_SIZE, NULL, GL_STATIC_DRAW);
|
||||
|
||||
demo_atlas_bind_texture (at);
|
||||
gl(TexBuffer) (GL_TEXTURE_BUFFER, GL_RGBA16I, at->buf_name);
|
||||
#endif
|
||||
|
||||
return at;
|
||||
}
|
||||
@@ -55,7 +70,9 @@ demo_atlas_destroy (demo_atlas_t *at)
|
||||
return;
|
||||
|
||||
glDeleteTextures (1, &at->tex_name);
|
||||
#ifndef HB_GPU_ATLAS_2D
|
||||
glDeleteBuffers (1, &at->buf_name);
|
||||
#endif
|
||||
free (at);
|
||||
}
|
||||
|
||||
@@ -63,7 +80,11 @@ static void
|
||||
demo_atlas_bind_texture (demo_atlas_t *at)
|
||||
{
|
||||
glActiveTexture (at->tex_unit);
|
||||
#ifdef HB_GPU_ATLAS_2D
|
||||
glBindTexture (GL_TEXTURE_2D, at->tex_name);
|
||||
#else
|
||||
glBindTexture (GL_TEXTURE_BUFFER, at->tex_name);
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
@@ -73,6 +94,9 @@ demo_atlas_set_uniforms (demo_atlas_t *at)
|
||||
glGetIntegerv (GL_CURRENT_PROGRAM, (GLint *) &program);
|
||||
|
||||
glUniform1i (glGetUniformLocation (program, "hb_gpu_atlas"), at->tex_unit - GL_TEXTURE0);
|
||||
#ifdef HB_GPU_ATLAS_2D
|
||||
glUniform1i (glGetUniformLocation (program, "hb_gpu_atlas_width"), ATLAS_TEX_WIDTH);
|
||||
#endif
|
||||
}
|
||||
|
||||
unsigned int
|
||||
@@ -88,11 +112,34 @@ demo_atlas_alloc (demo_atlas_t *at,
|
||||
unsigned int offset = at->cursor;
|
||||
at->cursor += len_texels;
|
||||
|
||||
#ifdef HB_GPU_ATLAS_2D
|
||||
demo_atlas_bind_texture (at);
|
||||
/* Upload row by row since data may span multiple rows. */
|
||||
unsigned int remaining = len_texels;
|
||||
unsigned int src_offset = 0;
|
||||
unsigned int dst_offset = offset;
|
||||
while (remaining)
|
||||
{
|
||||
unsigned int x = dst_offset % ATLAS_TEX_WIDTH;
|
||||
unsigned int y = dst_offset / ATLAS_TEX_WIDTH;
|
||||
unsigned int row_count = ATLAS_TEX_WIDTH - x;
|
||||
if (row_count > remaining)
|
||||
row_count = remaining;
|
||||
gl(TexSubImage2D) (GL_TEXTURE_2D, 0,
|
||||
x, y, row_count, 1,
|
||||
GL_RGBA_INTEGER, GL_SHORT,
|
||||
data + src_offset * TEXEL_SIZE);
|
||||
src_offset += row_count;
|
||||
dst_offset += row_count;
|
||||
remaining -= row_count;
|
||||
}
|
||||
#else
|
||||
glBindBuffer (GL_TEXTURE_BUFFER, at->buf_name);
|
||||
glBufferSubData (GL_TEXTURE_BUFFER,
|
||||
offset * TEXEL_SIZE,
|
||||
len_bytes,
|
||||
data);
|
||||
#endif
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
@@ -131,14 +131,20 @@ demo_shader_create_program (void)
|
||||
{
|
||||
GLuint vertex_shader, fragment_shader, program;
|
||||
|
||||
const GLchar *vert_sources[] = {"#version 330\n",
|
||||
#ifdef HB_GPU_ATLAS_2D
|
||||
const GLchar *preamble = "#version 300 es\nprecision highp float;\nprecision highp int;\n#define HB_GPU_ATLAS_2D\n";
|
||||
#else
|
||||
const GLchar *preamble = "#version 330\n";
|
||||
#endif
|
||||
|
||||
const GLchar *vert_sources[] = {preamble,
|
||||
hb_gpu_shader_vertex_source (HB_GPU_SHADER_LANG_GLSL),
|
||||
demo_vertex_glsl};
|
||||
vertex_shader = compile_shader (GL_VERTEX_SHADER,
|
||||
ARRAY_LEN (vert_sources),
|
||||
vert_sources);
|
||||
|
||||
const GLchar *frag_sources[] = {"#version 330\n",
|
||||
const GLchar *frag_sources[] = {preamble,
|
||||
hb_gpu_shader_fragment_source (HB_GPU_SHADER_LANG_GLSL),
|
||||
demo_fragment_glsl};
|
||||
fragment_shader = compile_shader (GL_FRAGMENT_SHADER,
|
||||
|
||||
@@ -366,10 +366,15 @@ demo_view_mouse_func (demo_view_t *vu, int button, int action, int mods)
|
||||
vu->buttons &= ~(1 << button);
|
||||
vu->modifiers = mods;
|
||||
|
||||
glfwGetCursorPos (vu->window, &vu->cursorx, &vu->cursory);
|
||||
double x = vu->cursorx, y = vu->cursory;
|
||||
|
||||
switch (button)
|
||||
{
|
||||
case GLFW_MOUSE_BUTTON_LEFT:
|
||||
if (action == GLFW_RELEASE && !vu->dragged && !vu->click_handled)
|
||||
demo_view_toggle_animation (vu);
|
||||
break;
|
||||
case GLFW_MOUSE_BUTTON_RIGHT:
|
||||
switch (action) {
|
||||
case GLFW_PRESS:
|
||||
@@ -501,7 +506,7 @@ demo_view_print_help (demo_view_t *vu)
|
||||
LOGI (" Right drag Rotate\n");
|
||||
LOGI (" Shift + right drag Adjust perspective\n");
|
||||
LOGI (" Right drag and release Spin animation\n");
|
||||
LOGI (" Right click Toggle animation\n");
|
||||
LOGI (" Click / tap Toggle animation\n");
|
||||
LOGI ("\n");
|
||||
}
|
||||
|
||||
|
||||
Executable
+44
@@ -0,0 +1,44 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Build the hb-gpu web demo with Emscripten.
|
||||
# Prerequisites: source emsdk/emsdk_env.sh
|
||||
|
||||
set -e
|
||||
|
||||
SRCDIR="$(cd "$(dirname "$0")/../../.." && pwd)"
|
||||
OUTDIR="$(dirname "$0")/out"
|
||||
|
||||
mkdir -p "$OUTDIR"
|
||||
|
||||
COMMON_FLAGS="-O2 -I$SRCDIR/src -DHB_NO_MT -DHAVE_ROUND -DHB_GPU_ATLAS_2D"
|
||||
|
||||
# Compile C files
|
||||
emcc $COMMON_FLAGS -c "$SRCDIR/util/gpu/matrix4x4.c" -o "$OUTDIR/matrix4x4.o"
|
||||
emcc $COMMON_FLAGS -c "$SRCDIR/util/gpu/trackball.c" -o "$OUTDIR/trackball.o"
|
||||
|
||||
# Compile C++ files and link
|
||||
em++ \
|
||||
-std=c++17 \
|
||||
$COMMON_FLAGS \
|
||||
"$SRCDIR/src/harfbuzz.cc" \
|
||||
"$SRCDIR/src/hb-gpu.cc" \
|
||||
"$SRCDIR/src/hb-gpu-draw.cc" \
|
||||
"$SRCDIR/src/hb-gpu-shaders.cc" \
|
||||
"$SRCDIR/util/gpu/demo-atlas.cc" \
|
||||
"$SRCDIR/util/gpu/demo-buffer.cc" \
|
||||
"$SRCDIR/util/gpu/demo-font.cc" \
|
||||
"$SRCDIR/util/gpu/demo-glstate.cc" \
|
||||
"$SRCDIR/util/gpu/demo-shader.cc" \
|
||||
"$SRCDIR/util/gpu/demo-view.cc" \
|
||||
"$SRCDIR/util/gpu/web/hb-gpu-web.cc" \
|
||||
"$OUTDIR/matrix4x4.o" \
|
||||
"$OUTDIR/trackball.o" \
|
||||
-sUSE_GLFW=3 \
|
||||
-sUSE_WEBGL2=1 \
|
||||
-sFULL_ES3=1 \
|
||||
-sALLOW_MEMORY_GROWTH=1 \
|
||||
--shell-file "$SRCDIR/util/gpu/web/shell.html" \
|
||||
-o "$OUTDIR/index.html"
|
||||
|
||||
echo "Built: $OUTDIR/index.html"
|
||||
echo "Serve: python3 -m http.server -d $OUTDIR"
|
||||
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
/*
|
||||
* Web (Emscripten) build of the hb-gpu demo.
|
||||
*
|
||||
* Reuses the desktop demo code with two changes:
|
||||
* 1. emscripten_set_main_loop replaces the blocking while loop
|
||||
* 2. HB_GPU_ATLAS_2D is defined (WebGL2 lacks texture buffers)
|
||||
*
|
||||
* Build:
|
||||
* source emsdk/emsdk_env.sh
|
||||
* ./util/gpu/web/build.sh
|
||||
*
|
||||
* Serve:
|
||||
* python3 -m http.server -d util/gpu/web/out
|
||||
*/
|
||||
|
||||
#include <emscripten.h>
|
||||
#include <emscripten/html5.h>
|
||||
|
||||
/* Pull in the demo code — all headers are self-contained */
|
||||
#include "../demo-common.h"
|
||||
#include "../demo-buffer.h"
|
||||
#include "../demo-font.h"
|
||||
#include "../demo-glstate.h"
|
||||
#include "../demo-view.h"
|
||||
|
||||
#include "../default-text.hh"
|
||||
#include "../default-font.hh"
|
||||
|
||||
static demo_glstate_t *st;
|
||||
static demo_view_t *vu;
|
||||
static demo_buffer_t *buffer;
|
||||
|
||||
static void
|
||||
main_loop_iter ()
|
||||
{
|
||||
glfwPollEvents ();
|
||||
|
||||
if (demo_view_should_redraw (vu))
|
||||
demo_view_display (vu, buffer);
|
||||
}
|
||||
|
||||
static void
|
||||
framebuffer_size_func (GLFWwindow *window, int width, int height)
|
||||
{ demo_view_reshape_func (vu, width, height); }
|
||||
|
||||
static void
|
||||
key_func (GLFWwindow *window, int key, int scancode, int action, int mods)
|
||||
{ demo_view_key_func (vu, key, scancode, action, mods); }
|
||||
|
||||
static void
|
||||
char_func (GLFWwindow *window, unsigned int codepoint)
|
||||
{ demo_view_char_func (vu, codepoint); }
|
||||
|
||||
static void
|
||||
mouse_func (GLFWwindow *window, int button, int action, int mods)
|
||||
{ demo_view_mouse_func (vu, button, action, mods); }
|
||||
|
||||
static void
|
||||
scroll_func (GLFWwindow *window, double xoffset, double yoffset)
|
||||
{ demo_view_scroll_func (vu, xoffset, yoffset); }
|
||||
|
||||
static void
|
||||
cursor_func (GLFWwindow *window, double x, double y)
|
||||
{ demo_view_motion_func (vu, x, y); }
|
||||
|
||||
int
|
||||
main ()
|
||||
{
|
||||
if (!glfwInit ())
|
||||
{
|
||||
fprintf (stderr, "Failed to initialize GLFW\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 0);
|
||||
glfwWindowHint (GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
|
||||
|
||||
/* Query actual canvas size from the browser. */
|
||||
int canvas_w = 700, canvas_h = 700;
|
||||
emscripten_get_canvas_element_size ("#canvas", &canvas_w, &canvas_h);
|
||||
|
||||
GLFWwindow *window = glfwCreateWindow (canvas_w, canvas_h, "HarfBuzz GPU Demo", NULL, NULL);
|
||||
if (!window)
|
||||
{
|
||||
glfwTerminate ();
|
||||
fprintf (stderr, "Failed to create GLFW window\n");
|
||||
return 1;
|
||||
}
|
||||
glfwMakeContextCurrent (window);
|
||||
|
||||
glfwSetFramebufferSizeCallback (window, framebuffer_size_func);
|
||||
glfwSetKeyCallback (window, key_func);
|
||||
glfwSetCharCallback (window, char_func);
|
||||
glfwSetMouseButtonCallback (window, mouse_func);
|
||||
glfwSetScrollCallback (window, scroll_func);
|
||||
glfwSetCursorPosCallback (window, cursor_func);
|
||||
|
||||
{
|
||||
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);
|
||||
|
||||
hb_blob_t *blob = hb_blob_create ((const char *) default_font,
|
||||
sizeof (default_font),
|
||||
HB_MEMORY_MODE_READONLY,
|
||||
NULL, NULL);
|
||||
hb_face_t *face = hb_face_create (blob, 0);
|
||||
hb_font_t *font = hb_font_create (face);
|
||||
demo_font_t *demo_font = demo_font_create (font, demo_glstate_get_atlas (st));
|
||||
|
||||
buffer = demo_buffer_create ();
|
||||
demo_point_t top_left = {0, 0};
|
||||
demo_buffer_move_to (buffer, &top_left);
|
||||
demo_buffer_add_text (buffer, default_text, demo_font, 1);
|
||||
|
||||
demo_font_print_stats (demo_font);
|
||||
|
||||
demo_view_setup (vu);
|
||||
demo_view_display (vu, buffer);
|
||||
|
||||
emscripten_set_main_loop (main_loop_iter, 0, 1);
|
||||
|
||||
/* Not reached */
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
|
||||
<title>HarfBuzz GPU Demo</title>
|
||||
<style>
|
||||
body { margin: 0; background: #222; overflow: hidden; touch-action: none; }
|
||||
canvas { display: block; width: 100vw; height: 100vh; }
|
||||
#fps { position: fixed; top: 8px; right: 12px; color: #999;
|
||||
font: 14px monospace; pointer-events: none; z-index: 1; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="fps"></div>
|
||||
<canvas id="canvas" oncontextmenu="event.preventDefault()" tabindex="-1"></canvas>
|
||||
<script>
|
||||
var canvas = document.getElementById('canvas');
|
||||
function resizeCanvas () {
|
||||
var dpr = window.devicePixelRatio || 1;
|
||||
canvas.width = Math.round(canvas.clientWidth * dpr);
|
||||
canvas.height = Math.round(canvas.clientHeight * dpr);
|
||||
}
|
||||
resizeCanvas();
|
||||
window.addEventListener('resize', resizeCanvas);
|
||||
|
||||
var fpsEl = document.getElementById('fps');
|
||||
var frames = 0, lastTime = performance.now();
|
||||
(function fpsLoop () {
|
||||
requestAnimationFrame(fpsLoop);
|
||||
frames++;
|
||||
var now = performance.now();
|
||||
if (now - lastTime >= 1000) {
|
||||
fpsEl.textContent = Math.round(frames * 1000 / (now - lastTime)) + ' fps';
|
||||
frames = 0;
|
||||
lastTime = now;
|
||||
}
|
||||
})();
|
||||
|
||||
var Module = {
|
||||
canvas: canvas,
|
||||
onRuntimeInitialized: function () {
|
||||
console.log('HarfBuzz GPU Demo loaded');
|
||||
}
|
||||
};
|
||||
</script>
|
||||
{{{ SCRIPT }}}
|
||||
</body>
|
||||
</html>
|
||||
Reference in new issue
Block a user