mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-21 23:11:04 +01:00
[gpu] Fix garbled rendering after font change in web demo
The GL renderer cached the vertex buffer upload by comparing the data pointer and count. After clearing and refilling the buffer with a new font's glyphs, std::vector reused the same allocation, so the pointer and count matched — the upload was skipped and the GPU drew with stale vertex data containing old atlas offsets. Fix by adding a generation counter to demo_buffer_t that increments on clear(). The renderer compares generation and count to decide whether to re-upload. Assisted-by: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
3c291a5015
commit
c86cb51bf6
7 files changed
+21
-5
No files matched your search
@@ -10,6 +10,7 @@ struct demo_buffer_t {
|
||||
std::vector<glyph_vertex_t> *vertices;
|
||||
demo_extents_t ink_extents;
|
||||
demo_extents_t logical_extents;
|
||||
unsigned generation;
|
||||
};
|
||||
|
||||
demo_buffer_t *
|
||||
@@ -41,6 +42,7 @@ demo_buffer_clear (demo_buffer_t *buffer)
|
||||
buffer->vertices->clear ();
|
||||
demo_extents_clear (&buffer->ink_extents);
|
||||
demo_extents_clear (&buffer->logical_extents);
|
||||
buffer->generation++;
|
||||
}
|
||||
|
||||
void
|
||||
@@ -173,3 +175,9 @@ demo_buffer_get_vertices (demo_buffer_t *buffer,
|
||||
return nullptr;
|
||||
return &(*buffer->vertices)[0];
|
||||
}
|
||||
|
||||
unsigned
|
||||
demo_buffer_get_generation (demo_buffer_t *buffer)
|
||||
{
|
||||
return buffer->generation;
|
||||
}
|
||||
@@ -55,5 +55,8 @@ glyph_vertex_t *
|
||||
demo_buffer_get_vertices (demo_buffer_t *buffer,
|
||||
unsigned int *count);
|
||||
|
||||
unsigned
|
||||
demo_buffer_get_generation (demo_buffer_t *buffer);
|
||||
|
||||
|
||||
#endif /* DEMO_BUFFER_H */
|
||||
@@ -251,6 +251,7 @@ struct demo_renderer_metal_t : demo_renderer_t
|
||||
/* -- Frame -- */
|
||||
|
||||
void display (glyph_vertex_t *vertices, unsigned int count,
|
||||
unsigned generation HB_UNUSED,
|
||||
int width, int height, float mat[16]) override
|
||||
{
|
||||
@autoreleasepool {
|
||||
|
||||
@@ -275,6 +275,7 @@ float4 ps_main (PSInput input) : SV_Target {
|
||||
void toggle_vsync (bool &v) override { v = !v; vsync_on = v; }
|
||||
|
||||
void display (glyph_vertex_t *vertices, unsigned count,
|
||||
unsigned generation HB_UNUSED,
|
||||
int width, int height, float mat[16]) override
|
||||
{
|
||||
if (width != cur_width || height != cur_height)
|
||||
|
||||
@@ -15,11 +15,11 @@ struct demo_renderer_gl_t : demo_renderer_t
|
||||
GLuint buf_name;
|
||||
bool vao_ready;
|
||||
unsigned int uploaded_count;
|
||||
glyph_vertex_t *uploaded_ptr;
|
||||
unsigned uploaded_generation;
|
||||
|
||||
demo_renderer_gl_t (GLFWwindow *window_)
|
||||
: window (window_), vao_ready (false),
|
||||
uploaded_count (0), uploaded_ptr (nullptr)
|
||||
uploaded_count (0), uploaded_generation (0)
|
||||
{
|
||||
st = demo_glstate_create ();
|
||||
glGenVertexArrays (1, &vao_name);
|
||||
@@ -137,6 +137,7 @@ struct demo_renderer_gl_t : demo_renderer_t
|
||||
}
|
||||
|
||||
void display (glyph_vertex_t *vertices, unsigned int count,
|
||||
unsigned generation,
|
||||
int width, int height, float mat[16]) override
|
||||
{
|
||||
glViewport (0, 0, width, height);
|
||||
@@ -150,14 +151,14 @@ struct demo_renderer_gl_t : demo_renderer_t
|
||||
|
||||
glBindVertexArray (vao_name);
|
||||
|
||||
if (vertices != uploaded_ptr || count != uploaded_count)
|
||||
if (count != uploaded_count || generation != uploaded_generation)
|
||||
{
|
||||
glBindBuffer (GL_ARRAY_BUFFER, buf_name);
|
||||
glBufferData (GL_ARRAY_BUFFER,
|
||||
sizeof (glyph_vertex_t) * count,
|
||||
(const char *) vertices, GL_STATIC_DRAW);
|
||||
uploaded_ptr = vertices;
|
||||
uploaded_count = count;
|
||||
uploaded_generation = generation;
|
||||
}
|
||||
|
||||
glDrawArrays (GL_TRIANGLES, 0, count);
|
||||
|
||||
@@ -34,8 +34,9 @@ struct demo_renderer_t
|
||||
/* Vsync toggle. */
|
||||
virtual void toggle_vsync (bool &vsync) = 0;
|
||||
|
||||
/* Render a frame. */
|
||||
/* Render a frame. generation changes when buffer content is rebuilt. */
|
||||
virtual void display (glyph_vertex_t *vertices, unsigned int count,
|
||||
unsigned generation,
|
||||
int width, int height, float mat[16]) = 0;
|
||||
};
|
||||
|
||||
|
||||
@@ -244,6 +244,7 @@ struct demo_renderer_webgpu_t : demo_renderer_t
|
||||
void toggle_vsync (bool &vsync) override { /* always vsync in browser */ }
|
||||
|
||||
void display (glyph_vertex_t *vertices, unsigned int count,
|
||||
unsigned generation HB_UNUSED,
|
||||
int width, int height, float mat[16]) override
|
||||
{
|
||||
WGPUSurfaceTexture surfTex;
|
||||
|
||||
Reference in new issue
Block a user