mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-20 22:41:10 +01:00
[gpu] Refactor demo to virtual renderer interface
Replace backend-specific branches with a demo_renderer_t base class that both GL and Metal implement. This eliminates all if(metal) checks in the view code and makes adding future backends (WebGPU) clean. - Add demo-renderer.h with virtual interface - Add demo-renderer-gl.cc wrapping existing GL code - Rewrite demo-metal.mm to implement renderer interface - Move VBO/VAO management from demo_buffer into GL renderer - Fix Metal vsync (CAMetalLayer.displaySyncEnabled) - Fix dark mode crash on Metal (was calling GL directly) - Add set_background to renderer for clear color control Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
6910b9a3ed
commit
216fb4fb43
8 files changed
+440
-356
No files matched your search
+9
-36
@@ -30,9 +30,8 @@
|
||||
#include "gpu/demo-common.h"
|
||||
#include "gpu/demo-buffer.h"
|
||||
#include "gpu/demo-font.h"
|
||||
#include "gpu/demo-glstate.h"
|
||||
#include "gpu/demo-renderer.h"
|
||||
#include "gpu/demo-view.h"
|
||||
#include "gpu/demo-metal.h"
|
||||
|
||||
#include "options.hh"
|
||||
#include "font-options.hh"
|
||||
@@ -69,6 +68,8 @@ struct gpu_output_t
|
||||
else
|
||||
init_gl ();
|
||||
|
||||
vu = demo_view_create (renderer, window);
|
||||
|
||||
glfwSetWindowUserPointer (window, this);
|
||||
glfwSetFramebufferSizeCallback (window, [] (GLFWwindow *w, int width, int height) {
|
||||
auto *self = (gpu_output_t *) glfwGetWindowUserPointer (w);
|
||||
@@ -95,31 +96,7 @@ struct gpu_output_t
|
||||
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 */
|
||||
|
||||
demo_font_ = demo_font_create (font, renderer->get_atlas ());
|
||||
buf = demo_buffer_create ();
|
||||
|
||||
demo_point_t top_left = {0, 0};
|
||||
@@ -157,8 +134,7 @@ struct gpu_output_t
|
||||
glViewport (0, 0, fb_width, fb_height);
|
||||
}
|
||||
|
||||
st = demo_glstate_create ();
|
||||
vu = demo_view_create (st, window);
|
||||
renderer = demo_renderer_create_gl (window);
|
||||
}
|
||||
|
||||
void init_metal ()
|
||||
@@ -172,12 +148,11 @@ struct gpu_output_t
|
||||
fail (false, "Failed to create GLFW window");
|
||||
}
|
||||
|
||||
metal = demo_metal_create (window, 1024 * 1024);
|
||||
if (!metal) {
|
||||
renderer = demo_renderer_create_metal (window);
|
||||
if (!renderer) {
|
||||
glfwTerminate ();
|
||||
fail (false, "Failed to initialize Metal");
|
||||
}
|
||||
vu = demo_view_create_metal (metal, window);
|
||||
#else
|
||||
fail (false, "Metal is only available on macOS");
|
||||
#endif
|
||||
@@ -239,8 +214,7 @@ struct gpu_output_t
|
||||
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);
|
||||
delete renderer;
|
||||
|
||||
glfwDestroyWindow (window);
|
||||
glfwTerminate ();
|
||||
@@ -253,8 +227,7 @@ struct gpu_output_t
|
||||
hb_face_t *face = nullptr;
|
||||
|
||||
GLFWwindow *window = nullptr;
|
||||
demo_glstate_t *st = nullptr;
|
||||
demo_metal_t *metal = nullptr;
|
||||
demo_renderer_t *renderer = nullptr;
|
||||
demo_view_t *vu = nullptr;
|
||||
demo_font_t *demo_font_ = nullptr;
|
||||
demo_buffer_t *buf = nullptr;
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
/*
|
||||
* Copyright 2026 Behdad Esfahbod. All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef DEMO_METAL_H
|
||||
#define DEMO_METAL_H
|
||||
|
||||
#include "demo-common.h"
|
||||
#include "demo-shader.h" /* for glyph_vertex_t */
|
||||
|
||||
typedef struct demo_metal_t demo_metal_t;
|
||||
|
||||
demo_metal_t *
|
||||
demo_metal_create (GLFWwindow *window, unsigned int atlas_capacity);
|
||||
|
||||
void
|
||||
demo_metal_destroy (demo_metal_t *mt);
|
||||
|
||||
/* Atlas allocation -- same interface as demo_atlas_t */
|
||||
unsigned int
|
||||
demo_metal_atlas_alloc (demo_metal_t *mt,
|
||||
const char *data,
|
||||
unsigned int len_bytes);
|
||||
|
||||
unsigned int
|
||||
demo_metal_atlas_get_used (demo_metal_t *mt);
|
||||
|
||||
void
|
||||
demo_metal_atlas_clear (demo_metal_t *mt);
|
||||
|
||||
/* Rendering state */
|
||||
void
|
||||
demo_metal_set_gamma (demo_metal_t *mt, float gamma);
|
||||
|
||||
void
|
||||
demo_metal_set_foreground (demo_metal_t *mt,
|
||||
float r, float g, float b, float a);
|
||||
|
||||
/* Frame rendering */
|
||||
void
|
||||
demo_metal_display (demo_metal_t *mt,
|
||||
glyph_vertex_t *vertices,
|
||||
unsigned int vertex_count,
|
||||
int width,
|
||||
int height,
|
||||
float mat[16]);
|
||||
|
||||
#endif /* DEMO_METAL_H */
|
||||
+201
-177
@@ -4,7 +4,8 @@
|
||||
|
||||
#ifdef __APPLE__
|
||||
|
||||
#include "demo-metal.h"
|
||||
#include "demo-renderer.h"
|
||||
#include "demo-atlas.h"
|
||||
#include <hb-gpu.h>
|
||||
|
||||
#import <Metal/Metal.h>
|
||||
@@ -69,7 +70,8 @@ fragment float4 fragment_main(VertexOut in [[stage_in]],
|
||||
)msl";
|
||||
|
||||
|
||||
struct demo_metal_t {
|
||||
struct demo_renderer_metal_t : demo_renderer_t
|
||||
{
|
||||
GLFWwindow *window;
|
||||
|
||||
/* Metal objects */
|
||||
@@ -78,13 +80,17 @@ struct demo_metal_t {
|
||||
id<MTLRenderPipelineState> pipelineState;
|
||||
CAMetalLayer *metalLayer;
|
||||
|
||||
/* Atlas (CPU + GPU buffer) */
|
||||
/* Atlas (CPU shadow + GPU buffer) */
|
||||
demo_atlas_t *atlas;
|
||||
char *atlas_data;
|
||||
unsigned int atlas_capacity; /* in texels */
|
||||
unsigned int atlas_cursor; /* in texels */
|
||||
id<MTLBuffer> atlasBuffer;
|
||||
bool atlas_dirty;
|
||||
|
||||
/* Clear color */
|
||||
float bg[4];
|
||||
|
||||
/* Uniforms */
|
||||
struct {
|
||||
float matViewProjection[16];
|
||||
@@ -93,33 +99,186 @@ struct demo_metal_t {
|
||||
float _pad1;
|
||||
float foreground[4];
|
||||
} uniforms;
|
||||
|
||||
|
||||
demo_renderer_metal_t (GLFWwindow *window_) : window (window_)
|
||||
{
|
||||
bg[0] = bg[1] = bg[2] = bg[3] = 1.0f;
|
||||
memset (&uniforms, 0, sizeof (uniforms));
|
||||
uniforms.gamma = 1.0f;
|
||||
uniforms.foreground[3] = 1.0f;
|
||||
}
|
||||
|
||||
~demo_renderer_metal_t () override
|
||||
{
|
||||
demo_atlas_destroy (atlas);
|
||||
free (atlas_data);
|
||||
}
|
||||
|
||||
|
||||
/* -- Atlas -- */
|
||||
|
||||
static unsigned int
|
||||
atlas_alloc_cb (void *ctx, const char *data, unsigned int len_bytes)
|
||||
{
|
||||
auto *self = (demo_renderer_metal_t *) ctx;
|
||||
unsigned int len_texels = len_bytes / TEXEL_SIZE;
|
||||
if (self->atlas_cursor + len_texels > self->atlas_capacity)
|
||||
die ("Ran out of atlas memory");
|
||||
unsigned int offset = self->atlas_cursor;
|
||||
self->atlas_cursor += len_texels;
|
||||
memcpy (self->atlas_data + offset * TEXEL_SIZE, data, len_bytes);
|
||||
self->atlas_dirty = true;
|
||||
return offset;
|
||||
}
|
||||
|
||||
static unsigned int
|
||||
atlas_get_used_cb (void *ctx)
|
||||
{
|
||||
return ((demo_renderer_metal_t *) ctx)->atlas_cursor;
|
||||
}
|
||||
|
||||
static void
|
||||
atlas_clear_cb (void *ctx)
|
||||
{
|
||||
auto *self = (demo_renderer_metal_t *) ctx;
|
||||
self->atlas_cursor = 0;
|
||||
self->atlas_dirty = true;
|
||||
}
|
||||
|
||||
demo_atlas_t *get_atlas () override
|
||||
{
|
||||
return atlas;
|
||||
}
|
||||
|
||||
|
||||
/* -- State -- */
|
||||
|
||||
void setup () override {}
|
||||
|
||||
void set_gamma (float gamma) override
|
||||
{
|
||||
uniforms.gamma = gamma;
|
||||
}
|
||||
|
||||
void set_foreground (float r, float g, float b, float a) override
|
||||
{
|
||||
uniforms.foreground[0] = r;
|
||||
uniforms.foreground[1] = g;
|
||||
uniforms.foreground[2] = b;
|
||||
uniforms.foreground[3] = a;
|
||||
}
|
||||
|
||||
void set_background (float r, float g, float b, float a) override
|
||||
{
|
||||
bg[0] = r; bg[1] = g; bg[2] = b; bg[3] = a;
|
||||
}
|
||||
|
||||
bool set_srgb (bool enabled [[maybe_unused]]) override
|
||||
{
|
||||
/* Metal uses MTLPixelFormatBGRA8Unorm_sRGB; sRGB is always on. */
|
||||
return true;
|
||||
}
|
||||
|
||||
void toggle_vsync (bool &vsync) override
|
||||
{
|
||||
vsync = !vsync;
|
||||
metalLayer.displaySyncEnabled = vsync;
|
||||
}
|
||||
|
||||
|
||||
/* -- Frame -- */
|
||||
|
||||
void display (glyph_vertex_t *vertices, unsigned int count,
|
||||
int width, int height, float mat[16]) override
|
||||
{
|
||||
@autoreleasepool {
|
||||
|
||||
/* Update layer size */
|
||||
CGSize size = { (CGFloat) width, (CGFloat) height };
|
||||
metalLayer.drawableSize = size;
|
||||
|
||||
/* Update uniforms */
|
||||
memcpy (uniforms.matViewProjection, mat, 16 * sizeof (float));
|
||||
uniforms.viewport[0] = (float) width;
|
||||
uniforms.viewport[1] = (float) height;
|
||||
|
||||
/* Sync atlas if dirty */
|
||||
if (atlas_dirty)
|
||||
{
|
||||
memcpy (atlasBuffer.contents, atlas_data,
|
||||
atlas_cursor * TEXEL_SIZE);
|
||||
atlas_dirty = false;
|
||||
}
|
||||
|
||||
/* Get drawable */
|
||||
id<CAMetalDrawable> drawable = [metalLayer nextDrawable];
|
||||
if (!drawable)
|
||||
return;
|
||||
|
||||
/* Render pass */
|
||||
MTLRenderPassDescriptor *passDesc = [MTLRenderPassDescriptor renderPassDescriptor];
|
||||
passDesc.colorAttachments[0].texture = drawable.texture;
|
||||
passDesc.colorAttachments[0].loadAction = MTLLoadActionClear;
|
||||
passDesc.colorAttachments[0].storeAction = MTLStoreActionStore;
|
||||
passDesc.colorAttachments[0].clearColor = MTLClearColorMake (bg[0], bg[1], bg[2], bg[3]);
|
||||
|
||||
id<MTLCommandBuffer> commandBuffer = [commandQueue commandBuffer];
|
||||
id<MTLRenderCommandEncoder> encoder =
|
||||
[commandBuffer renderCommandEncoderWithDescriptor:passDesc];
|
||||
|
||||
[encoder setViewport:(MTLViewport){0, 0, (double) width, (double) height, 0, 1}];
|
||||
[encoder setRenderPipelineState:pipelineState];
|
||||
|
||||
if (count > 0)
|
||||
{
|
||||
id<MTLBuffer> vertexBuffer =
|
||||
[device newBufferWithBytes:vertices
|
||||
length:count * sizeof (glyph_vertex_t)
|
||||
options:MTLResourceStorageModeShared];
|
||||
|
||||
[encoder setVertexBuffer:vertexBuffer offset:0 atIndex:0];
|
||||
[encoder setVertexBytes:&uniforms length:sizeof (uniforms) atIndex:1];
|
||||
[encoder setFragmentBuffer:atlasBuffer offset:0 atIndex:0];
|
||||
[encoder setFragmentBytes:&uniforms length:sizeof (uniforms) atIndex:1];
|
||||
|
||||
[encoder drawPrimitives:MTLPrimitiveTypeTriangle
|
||||
vertexStart:0
|
||||
vertexCount:count];
|
||||
}
|
||||
|
||||
[encoder endEncoding];
|
||||
[commandBuffer presentDrawable:drawable];
|
||||
[commandBuffer commit];
|
||||
|
||||
} /* @autoreleasepool */
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
demo_metal_t *
|
||||
demo_metal_create (GLFWwindow *window, unsigned int atlas_capacity)
|
||||
demo_renderer_t *
|
||||
demo_renderer_create_metal (GLFWwindow *window)
|
||||
{
|
||||
@autoreleasepool {
|
||||
|
||||
demo_metal_t *mt = (demo_metal_t *) calloc (1, sizeof (demo_metal_t));
|
||||
mt->window = window;
|
||||
auto *r = new demo_renderer_metal_t (window);
|
||||
|
||||
/* Get Metal device */
|
||||
mt->device = MTLCreateSystemDefaultDevice ();
|
||||
if (!mt->device)
|
||||
r->device = MTLCreateSystemDefaultDevice ();
|
||||
if (!r->device)
|
||||
{
|
||||
free (mt);
|
||||
return NULL;
|
||||
delete r;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
mt->commandQueue = [mt->device newCommandQueue];
|
||||
r->commandQueue = [r->device newCommandQueue];
|
||||
|
||||
/* Set up CAMetalLayer on the GLFW window */
|
||||
NSWindow *nswindow = glfwGetCocoaWindow (window);
|
||||
mt->metalLayer = [CAMetalLayer layer];
|
||||
mt->metalLayer.device = mt->device;
|
||||
mt->metalLayer.pixelFormat = MTLPixelFormatBGRA8Unorm_sRGB;
|
||||
nswindow.contentView.layer = mt->metalLayer;
|
||||
r->metalLayer = [CAMetalLayer layer];
|
||||
r->metalLayer.device = r->device;
|
||||
r->metalLayer.pixelFormat = MTLPixelFormatBGRA8Unorm_sRGB;
|
||||
nswindow.contentView.layer = r->metalLayer;
|
||||
nswindow.contentView.wantsLayer = YES;
|
||||
|
||||
/* Compile shaders */
|
||||
@@ -131,15 +290,15 @@ demo_metal_create (GLFWwindow *window, unsigned int atlas_capacity)
|
||||
preamble, vert_src, frag_src, demo_metal_shader_source];
|
||||
|
||||
NSError *error = nil;
|
||||
id<MTLLibrary> library = [mt->device newLibraryWithSource:source
|
||||
options:nil
|
||||
error:&error];
|
||||
id<MTLLibrary> library = [r->device newLibraryWithSource:source
|
||||
options:nil
|
||||
error:&error];
|
||||
if (!library)
|
||||
{
|
||||
fprintf (stderr, "Metal shader compilation failed: %s\n",
|
||||
[[error localizedDescription] UTF8String]);
|
||||
free (mt);
|
||||
return NULL;
|
||||
delete r;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
id<MTLFunction> vertexFunc = [library newFunctionWithName:@"vertex_main"];
|
||||
@@ -147,27 +306,21 @@ demo_metal_create (GLFWwindow *window, unsigned int atlas_capacity)
|
||||
|
||||
/* Create vertex descriptor matching glyph_vertex_t layout */
|
||||
MTLVertexDescriptor *vertexDesc = [MTLVertexDescriptor vertexDescriptor];
|
||||
/* position: float2 at offset 0 */
|
||||
vertexDesc.attributes[0].format = MTLVertexFormatFloat2;
|
||||
vertexDesc.attributes[0].offset = 0;
|
||||
vertexDesc.attributes[0].bufferIndex = 0;
|
||||
/* texcoord: float2 at offset 8 */
|
||||
vertexDesc.attributes[1].format = MTLVertexFormatFloat2;
|
||||
vertexDesc.attributes[1].offset = 8;
|
||||
vertexDesc.attributes[1].bufferIndex = 0;
|
||||
/* normal: float2 at offset 16 */
|
||||
vertexDesc.attributes[2].format = MTLVertexFormatFloat2;
|
||||
vertexDesc.attributes[2].offset = 16;
|
||||
vertexDesc.attributes[2].bufferIndex = 0;
|
||||
/* emPerPos: float at offset 24 */
|
||||
vertexDesc.attributes[3].format = MTLVertexFormatFloat;
|
||||
vertexDesc.attributes[3].offset = 24;
|
||||
vertexDesc.attributes[3].bufferIndex = 0;
|
||||
/* glyphLoc: uint at offset 28 */
|
||||
vertexDesc.attributes[4].format = MTLVertexFormatUInt;
|
||||
vertexDesc.attributes[4].offset = 28;
|
||||
vertexDesc.attributes[4].bufferIndex = 0;
|
||||
/* stride = sizeof(glyph_vertex_t) = 32 */
|
||||
vertexDesc.layouts[0].stride = 32;
|
||||
|
||||
/* Create pipeline */
|
||||
@@ -176,7 +329,6 @@ demo_metal_create (GLFWwindow *window, unsigned int atlas_capacity)
|
||||
pipelineDesc.fragmentFunction = fragmentFunc;
|
||||
pipelineDesc.vertexDescriptor = vertexDesc;
|
||||
pipelineDesc.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm_sRGB;
|
||||
/* Alpha blending: SRC_ALPHA, ONE_MINUS_SRC_ALPHA */
|
||||
pipelineDesc.colorAttachments[0].blendingEnabled = YES;
|
||||
pipelineDesc.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactorSourceAlpha;
|
||||
pipelineDesc.colorAttachments[0].destinationRGBBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
|
||||
@@ -184,163 +336,35 @@ demo_metal_create (GLFWwindow *window, unsigned int atlas_capacity)
|
||||
pipelineDesc.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
|
||||
|
||||
error = nil;
|
||||
mt->pipelineState = [mt->device newRenderPipelineStateWithDescriptor:pipelineDesc
|
||||
error:&error];
|
||||
if (!mt->pipelineState)
|
||||
r->pipelineState = [r->device newRenderPipelineStateWithDescriptor:pipelineDesc
|
||||
error:&error];
|
||||
if (!r->pipelineState)
|
||||
{
|
||||
fprintf (stderr, "Metal pipeline creation failed: %s\n",
|
||||
[[error localizedDescription] UTF8String]);
|
||||
free (mt);
|
||||
return NULL;
|
||||
delete r;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/* Atlas */
|
||||
mt->atlas_capacity = atlas_capacity;
|
||||
mt->atlas_cursor = 0;
|
||||
mt->atlas_data = (char *) calloc (atlas_capacity, TEXEL_SIZE);
|
||||
mt->atlasBuffer = [mt->device newBufferWithLength:atlas_capacity * TEXEL_SIZE
|
||||
options:MTLResourceStorageModeShared];
|
||||
mt->atlas_dirty = false;
|
||||
unsigned int atlas_capacity = 1024 * 1024;
|
||||
r->atlas_capacity = atlas_capacity;
|
||||
r->atlas_cursor = 0;
|
||||
r->atlas_data = (char *) calloc (atlas_capacity, TEXEL_SIZE);
|
||||
r->atlasBuffer = [r->device newBufferWithLength:atlas_capacity * TEXEL_SIZE
|
||||
options:MTLResourceStorageModeShared];
|
||||
r->atlas_dirty = false;
|
||||
|
||||
/* Default uniforms */
|
||||
mt->uniforms.gamma = 1.0f;
|
||||
mt->uniforms.foreground[0] = 0.0f;
|
||||
mt->uniforms.foreground[1] = 0.0f;
|
||||
mt->uniforms.foreground[2] = 0.0f;
|
||||
mt->uniforms.foreground[3] = 1.0f;
|
||||
/* Create atlas wrapper for demo_font_t */
|
||||
demo_atlas_backend_t backend = {
|
||||
r,
|
||||
demo_renderer_metal_t::atlas_alloc_cb,
|
||||
demo_renderer_metal_t::atlas_get_used_cb,
|
||||
demo_renderer_metal_t::atlas_clear_cb,
|
||||
};
|
||||
r->atlas = demo_atlas_create_external (&backend);
|
||||
|
||||
return mt;
|
||||
|
||||
} /* @autoreleasepool */
|
||||
}
|
||||
|
||||
void
|
||||
demo_metal_destroy (demo_metal_t *mt)
|
||||
{
|
||||
if (!mt)
|
||||
return;
|
||||
|
||||
free (mt->atlas_data);
|
||||
free (mt);
|
||||
}
|
||||
|
||||
|
||||
unsigned int
|
||||
demo_metal_atlas_alloc (demo_metal_t *mt,
|
||||
const char *data,
|
||||
unsigned int len_bytes)
|
||||
{
|
||||
unsigned int len_texels = len_bytes / TEXEL_SIZE;
|
||||
|
||||
if (mt->atlas_cursor + len_texels > mt->atlas_capacity)
|
||||
die ("Ran out of atlas memory");
|
||||
|
||||
unsigned int offset = mt->atlas_cursor;
|
||||
mt->atlas_cursor += len_texels;
|
||||
|
||||
memcpy (mt->atlas_data + offset * TEXEL_SIZE, data, len_bytes);
|
||||
mt->atlas_dirty = true;
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
unsigned int
|
||||
demo_metal_atlas_get_used (demo_metal_t *mt)
|
||||
{
|
||||
return mt->atlas_cursor;
|
||||
}
|
||||
|
||||
void
|
||||
demo_metal_atlas_clear (demo_metal_t *mt)
|
||||
{
|
||||
mt->atlas_cursor = 0;
|
||||
mt->atlas_dirty = true;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
demo_metal_set_gamma (demo_metal_t *mt, float gamma)
|
||||
{
|
||||
mt->uniforms.gamma = gamma;
|
||||
}
|
||||
|
||||
void
|
||||
demo_metal_set_foreground (demo_metal_t *mt,
|
||||
float r, float g, float b, float a)
|
||||
{
|
||||
mt->uniforms.foreground[0] = r;
|
||||
mt->uniforms.foreground[1] = g;
|
||||
mt->uniforms.foreground[2] = b;
|
||||
mt->uniforms.foreground[3] = a;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
demo_metal_display (demo_metal_t *mt,
|
||||
glyph_vertex_t *vertices,
|
||||
unsigned int vertex_count,
|
||||
int width,
|
||||
int height,
|
||||
float mat[16])
|
||||
{
|
||||
@autoreleasepool {
|
||||
|
||||
/* Update layer size */
|
||||
CGSize size = { (CGFloat) width, (CGFloat) height };
|
||||
mt->metalLayer.drawableSize = size;
|
||||
|
||||
/* Update uniforms */
|
||||
memcpy (mt->uniforms.matViewProjection, mat, 16 * sizeof (float));
|
||||
mt->uniforms.viewport[0] = (float) width;
|
||||
mt->uniforms.viewport[1] = (float) height;
|
||||
|
||||
/* Sync atlas if dirty */
|
||||
if (mt->atlas_dirty)
|
||||
{
|
||||
memcpy (mt->atlasBuffer.contents, mt->atlas_data,
|
||||
mt->atlas_cursor * TEXEL_SIZE);
|
||||
mt->atlas_dirty = false;
|
||||
}
|
||||
|
||||
/* Get drawable */
|
||||
id<CAMetalDrawable> drawable = [mt->metalLayer nextDrawable];
|
||||
if (!drawable)
|
||||
return;
|
||||
|
||||
/* Render pass */
|
||||
MTLRenderPassDescriptor *passDesc = [MTLRenderPassDescriptor renderPassDescriptor];
|
||||
passDesc.colorAttachments[0].texture = drawable.texture;
|
||||
passDesc.colorAttachments[0].loadAction = MTLLoadActionClear;
|
||||
passDesc.colorAttachments[0].storeAction = MTLStoreActionStore;
|
||||
passDesc.colorAttachments[0].clearColor = MTLClearColorMake (1.0, 1.0, 1.0, 1.0);
|
||||
|
||||
id<MTLCommandBuffer> commandBuffer = [mt->commandQueue commandBuffer];
|
||||
id<MTLRenderCommandEncoder> encoder =
|
||||
[commandBuffer renderCommandEncoderWithDescriptor:passDesc];
|
||||
|
||||
[encoder setViewport:(MTLViewport){0, 0, (double) width, (double) height, 0, 1}];
|
||||
[encoder setRenderPipelineState:mt->pipelineState];
|
||||
|
||||
if (vertex_count > 0)
|
||||
{
|
||||
id<MTLBuffer> vertexBuffer =
|
||||
[mt->device newBufferWithBytes:vertices
|
||||
length:vertex_count * sizeof (glyph_vertex_t)
|
||||
options:MTLResourceStorageModeShared];
|
||||
|
||||
[encoder setVertexBuffer:vertexBuffer offset:0 atIndex:0];
|
||||
[encoder setVertexBytes:&mt->uniforms length:sizeof (mt->uniforms) atIndex:1];
|
||||
[encoder setFragmentBuffer:mt->atlasBuffer offset:0 atIndex:0];
|
||||
[encoder setFragmentBytes:&mt->uniforms length:sizeof (mt->uniforms) atIndex:1];
|
||||
|
||||
[encoder drawPrimitives:MTLPrimitiveTypeTriangle
|
||||
vertexStart:0
|
||||
vertexCount:vertex_count];
|
||||
}
|
||||
|
||||
[encoder endEncoding];
|
||||
[commandBuffer presentDrawable:drawable];
|
||||
[commandBuffer commit];
|
||||
return r;
|
||||
|
||||
} /* @autoreleasepool */
|
||||
}
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
* Copyright 2026 Behdad Esfahbod. All Rights Reserved.
|
||||
*/
|
||||
|
||||
#include "demo-renderer.h"
|
||||
#include "demo-atlas.h"
|
||||
#include "demo-glstate.h"
|
||||
|
||||
|
||||
struct demo_renderer_gl_t : demo_renderer_t
|
||||
{
|
||||
GLFWwindow *window;
|
||||
demo_glstate_t *st;
|
||||
GLuint vao_name;
|
||||
GLuint buf_name;
|
||||
|
||||
demo_renderer_gl_t (GLFWwindow *window_) : window (window_)
|
||||
{
|
||||
st = demo_glstate_create ();
|
||||
glGenVertexArrays (1, &vao_name);
|
||||
glGenBuffers (1, &buf_name);
|
||||
}
|
||||
|
||||
~demo_renderer_gl_t () override
|
||||
{
|
||||
glDeleteVertexArrays (1, &vao_name);
|
||||
glDeleteBuffers (1, &buf_name);
|
||||
demo_glstate_destroy (st);
|
||||
}
|
||||
|
||||
demo_atlas_t *get_atlas () override
|
||||
{
|
||||
return demo_glstate_get_atlas (st);
|
||||
}
|
||||
|
||||
void setup () override
|
||||
{
|
||||
demo_glstate_setup (st);
|
||||
}
|
||||
|
||||
void set_gamma (float gamma) override
|
||||
{
|
||||
demo_glstate_set_gamma (st, gamma);
|
||||
}
|
||||
|
||||
void set_foreground (float r, float g, float b, float a) override
|
||||
{
|
||||
demo_glstate_set_foreground (st, r, g, b, a);
|
||||
}
|
||||
|
||||
void set_background (float r, float g, float b, float a) override
|
||||
{
|
||||
glClearColor (r, g, b, a);
|
||||
}
|
||||
|
||||
bool set_srgb (bool enabled) override
|
||||
{
|
||||
#if defined(GL_FRAMEBUFFER_SRGB)
|
||||
while (glGetError () != GL_NO_ERROR)
|
||||
;
|
||||
if (enabled)
|
||||
{
|
||||
glEnable (GL_FRAMEBUFFER_SRGB);
|
||||
if (glGetError () != GL_NO_ERROR)
|
||||
return false;
|
||||
}
|
||||
else
|
||||
glDisable (GL_FRAMEBUFFER_SRGB);
|
||||
return true;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
void toggle_vsync (bool &vsync) override
|
||||
{
|
||||
vsync = !vsync;
|
||||
glfwSwapInterval (vsync ? 1 : 0);
|
||||
}
|
||||
|
||||
void display (glyph_vertex_t *vertices, unsigned int count,
|
||||
int width, int height, float mat[16]) override
|
||||
{
|
||||
glViewport (0, 0, width, height);
|
||||
demo_glstate_set_matrix (st, mat);
|
||||
|
||||
glClear (GL_COLOR_BUFFER_BIT);
|
||||
|
||||
if (count > 0)
|
||||
{
|
||||
GLint program;
|
||||
glGetIntegerv (GL_CURRENT_PROGRAM, &program);
|
||||
|
||||
glBindVertexArray (vao_name);
|
||||
glBindBuffer (GL_ARRAY_BUFFER, buf_name);
|
||||
glBufferData (GL_ARRAY_BUFFER,
|
||||
sizeof (glyph_vertex_t) * count,
|
||||
(const char *) vertices, GL_STATIC_DRAW);
|
||||
|
||||
GLsizei stride = sizeof (glyph_vertex_t);
|
||||
|
||||
GLint loc_pos = glGetAttribLocation (program, "a_position");
|
||||
glEnableVertexAttribArray (loc_pos);
|
||||
glVertexAttribPointer (loc_pos, 2, GL_FLOAT, GL_FALSE, stride,
|
||||
(const void *) offsetof (glyph_vertex_t, x));
|
||||
|
||||
GLint loc_tex = glGetAttribLocation (program, "a_texcoord");
|
||||
glEnableVertexAttribArray (loc_tex);
|
||||
glVertexAttribPointer (loc_tex, 2, GL_FLOAT, GL_FALSE, stride,
|
||||
(const void *) offsetof (glyph_vertex_t, tx));
|
||||
|
||||
GLint loc_normal = glGetAttribLocation (program, "a_normal");
|
||||
glEnableVertexAttribArray (loc_normal);
|
||||
glVertexAttribPointer (loc_normal, 2, GL_FLOAT, GL_FALSE, stride,
|
||||
(const void *) offsetof (glyph_vertex_t, nx));
|
||||
|
||||
GLint loc_epp = glGetAttribLocation (program, "a_emPerPos");
|
||||
glEnableVertexAttribArray (loc_epp);
|
||||
glVertexAttribPointer (loc_epp, 1, GL_FLOAT, GL_FALSE, stride,
|
||||
(const void *) offsetof (glyph_vertex_t, emPerPos));
|
||||
|
||||
GLint loc_glyph = glGetAttribLocation (program, "a_glyphLoc");
|
||||
glEnableVertexAttribArray (loc_glyph);
|
||||
glVertexAttribIPointer (loc_glyph, 1, GL_UNSIGNED_INT, stride,
|
||||
(const void *) offsetof (glyph_vertex_t, atlas_offset));
|
||||
|
||||
glDrawArrays (GL_TRIANGLES, 0, count);
|
||||
|
||||
glDisableVertexAttribArray (loc_pos);
|
||||
glDisableVertexAttribArray (loc_tex);
|
||||
glDisableVertexAttribArray (loc_normal);
|
||||
glDisableVertexAttribArray (loc_epp);
|
||||
glDisableVertexAttribArray (loc_glyph);
|
||||
glBindVertexArray (0);
|
||||
}
|
||||
|
||||
glfwSwapBuffers (window);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
demo_renderer_t *
|
||||
demo_renderer_create_gl (GLFWwindow *window)
|
||||
{
|
||||
return new demo_renderer_gl_t (window);
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2026 Behdad Esfahbod. All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef DEMO_RENDERER_H
|
||||
#define DEMO_RENDERER_H
|
||||
|
||||
#include "demo-common.h"
|
||||
#include "demo-shader.h"
|
||||
|
||||
typedef struct demo_atlas_t demo_atlas_t;
|
||||
|
||||
|
||||
struct demo_renderer_t
|
||||
{
|
||||
virtual ~demo_renderer_t () {}
|
||||
|
||||
/* Atlas for glyph data upload. */
|
||||
virtual demo_atlas_t *get_atlas () = 0;
|
||||
|
||||
/* One-time setup after atlas is populated. */
|
||||
virtual void setup () = 0;
|
||||
|
||||
/* Rendering state. */
|
||||
virtual void set_gamma (float gamma) = 0;
|
||||
virtual void set_foreground (float r, float g, float b, float a) = 0;
|
||||
virtual void set_background (float r, float g, float b, float a) = 0;
|
||||
|
||||
/* sRGB framebuffer control; returns false if unsupported. */
|
||||
virtual bool set_srgb (bool enabled) = 0;
|
||||
|
||||
/* Vsync toggle. */
|
||||
virtual void toggle_vsync (bool &vsync) = 0;
|
||||
|
||||
/* Render a frame. */
|
||||
virtual void display (glyph_vertex_t *vertices, unsigned int count,
|
||||
int width, int height, float mat[16]) = 0;
|
||||
};
|
||||
|
||||
|
||||
demo_renderer_t *
|
||||
demo_renderer_create_gl (GLFWwindow *window);
|
||||
|
||||
#ifdef __APPLE__
|
||||
demo_renderer_t *
|
||||
demo_renderer_create_metal (GLFWwindow *window);
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* DEMO_RENDERER_H */
|
||||
+30
-88
@@ -4,20 +4,18 @@
|
||||
*/
|
||||
|
||||
#include "demo-view.h"
|
||||
#include "demo-metal.h"
|
||||
|
||||
#include "trackball.hh"
|
||||
#include "matrix4x4.hh"
|
||||
|
||||
struct demo_view_t {
|
||||
demo_glstate_t *st;
|
||||
demo_metal_t *metal;
|
||||
demo_renderer_t *renderer;
|
||||
GLFWwindow *window;
|
||||
|
||||
/* Output */
|
||||
GLint vsync;
|
||||
hb_bool_t fullscreen;
|
||||
hb_bool_t dark_mode;
|
||||
bool vsync;
|
||||
bool fullscreen;
|
||||
bool dark_mode;
|
||||
enum { GAMMA_SRGB, GAMMA_2_2, GAMMA_NONE } gamma_mode;
|
||||
|
||||
/* Mouse handling */
|
||||
@@ -62,24 +60,11 @@ struct demo_view_t {
|
||||
};
|
||||
|
||||
demo_view_t *
|
||||
demo_view_create (demo_glstate_t *st, GLFWwindow *window)
|
||||
demo_view_create (demo_renderer_t *renderer, GLFWwindow *window)
|
||||
{
|
||||
demo_view_t *vu = (demo_view_t *) calloc (1, sizeof (demo_view_t));
|
||||
|
||||
vu->st = st;
|
||||
vu->window = window;
|
||||
vu->needs_redraw = true;
|
||||
demo_view_reset (vu);
|
||||
|
||||
return vu;
|
||||
}
|
||||
|
||||
demo_view_t *
|
||||
demo_view_create_metal (demo_metal_t *mt, GLFWwindow *window)
|
||||
{
|
||||
demo_view_t *vu = (demo_view_t *) calloc (1, sizeof (demo_view_t));
|
||||
|
||||
vu->metal = mt;
|
||||
vu->renderer = renderer;
|
||||
vu->window = window;
|
||||
vu->needs_redraw = true;
|
||||
demo_view_reset (vu);
|
||||
@@ -197,54 +182,25 @@ demo_view_toggle_animation (demo_view_t *vu)
|
||||
static void
|
||||
demo_view_toggle_vsync (demo_view_t *vu)
|
||||
{
|
||||
GLint vsync = !vu->vsync;
|
||||
glfwSwapInterval (vsync);
|
||||
vu->vsync = vsync;
|
||||
vu->renderer->toggle_vsync (vu->vsync);
|
||||
LOGI ("Setting vsync %s.\n", vu->vsync ? "on" : "off");
|
||||
}
|
||||
|
||||
static void
|
||||
demo_view_set_gamma_mode (demo_view_t *vu, int mode)
|
||||
{
|
||||
if (vu->metal)
|
||||
{
|
||||
/* Metal uses sRGB framebuffer; gamma in shader for mode 2.2 */
|
||||
if (mode == demo_view_t::GAMMA_SRGB)
|
||||
mode = demo_view_t::GAMMA_NONE; /* sRGB is automatic with MTLPixelFormatBGRA8Unorm_sRGB */
|
||||
vu->gamma_mode = (decltype(vu->gamma_mode)) mode;
|
||||
float gamma = mode == demo_view_t::GAMMA_2_2 ? (vu->dark_mode ? 1.f/2.2f : 2.2f) : 1.f;
|
||||
demo_metal_set_gamma (vu->metal, gamma);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Disable previous sRGB if any. */
|
||||
#if defined(GL_FRAMEBUFFER_SRGB)
|
||||
glDisable (GL_FRAMEBUFFER_SRGB);
|
||||
while (glGetError () != GL_NO_ERROR)
|
||||
;
|
||||
#endif
|
||||
vu->renderer->set_srgb (false);
|
||||
|
||||
if (mode == demo_view_t::GAMMA_SRGB)
|
||||
{
|
||||
#if defined(GL_FRAMEBUFFER_SRGB)
|
||||
while (glGetError () != GL_NO_ERROR)
|
||||
;
|
||||
glEnable (GL_FRAMEBUFFER_SRGB);
|
||||
if (glGetError () != GL_NO_ERROR)
|
||||
{
|
||||
LOGW ("sRGB framebuffer not available.\n");
|
||||
mode = demo_view_t::GAMMA_2_2;
|
||||
}
|
||||
#else
|
||||
if (mode == demo_view_t::GAMMA_SRGB)
|
||||
{
|
||||
if (!vu->renderer->set_srgb (true))
|
||||
mode = demo_view_t::GAMMA_2_2;
|
||||
#endif
|
||||
}
|
||||
|
||||
vu->gamma_mode = (decltype(vu->gamma_mode)) mode;
|
||||
float gamma = mode == demo_view_t::GAMMA_2_2 ? (vu->dark_mode ? 1.f/2.2f : 2.2f) : 1.f;
|
||||
demo_glstate_set_gamma (vu->st, gamma);
|
||||
}
|
||||
|
||||
vu->gamma_mode = (decltype(vu->gamma_mode)) mode;
|
||||
float gamma = mode == demo_view_t::GAMMA_2_2 ? (vu->dark_mode ? 1.f/2.2f : 2.2f) : 1.f;
|
||||
vu->renderer->set_gamma (gamma);
|
||||
|
||||
const char *names[] = {"sRGB", "gamma 2.2", "none"};
|
||||
LOGI ("Gamma correction: %s.\n", names[mode]);
|
||||
vu->needs_redraw = true;
|
||||
@@ -266,10 +222,17 @@ static void
|
||||
demo_view_toggle_dark (demo_view_t *vu)
|
||||
{
|
||||
vu->dark_mode = !vu->dark_mode;
|
||||
LOGI ("Setting %s mode.\n", vu->dark_mode ? "dark" : "light");
|
||||
if (vu->dark_mode)
|
||||
demo_glstate_set_foreground (vu->st, DARK_FG);
|
||||
{
|
||||
vu->renderer->set_foreground (DARK_FG);
|
||||
vu->renderer->set_background (DARK_BG);
|
||||
}
|
||||
else
|
||||
demo_glstate_set_foreground (vu->st, LIGHT_FG);
|
||||
{
|
||||
vu->renderer->set_foreground (LIGHT_FG);
|
||||
vu->renderer->set_background (LIGHT_BG);
|
||||
}
|
||||
demo_view_set_gamma_mode (vu, vu->gamma_mode);
|
||||
vu->needs_redraw = true;
|
||||
}
|
||||
@@ -619,42 +582,21 @@ demo_view_display (demo_view_t *vu, demo_buffer_t *buffer)
|
||||
-(extents.max_x + extents.min_x) / 2.,
|
||||
-(extents.max_y + extents.min_y) / 2., 0);
|
||||
|
||||
if (vu->metal)
|
||||
{
|
||||
unsigned int count;
|
||||
glyph_vertex_t *verts = demo_buffer_get_vertices (buffer, &count);
|
||||
demo_metal_display (vu->metal, verts, count, width, height, mat);
|
||||
}
|
||||
else
|
||||
{
|
||||
glViewport (0, 0, width, height);
|
||||
demo_glstate_set_matrix (vu->st, mat);
|
||||
unsigned int count;
|
||||
glyph_vertex_t *verts = demo_buffer_get_vertices (buffer, &count);
|
||||
vu->renderer->display (verts, count, width, height, mat);
|
||||
|
||||
if (vu->dark_mode)
|
||||
glClearColor (DARK_BG);
|
||||
else
|
||||
glClearColor (LIGHT_BG);
|
||||
glClear (GL_COLOR_BUFFER_BIT);
|
||||
|
||||
demo_buffer_draw (buffer);
|
||||
|
||||
glfwSwapBuffers (vu->window);
|
||||
}
|
||||
vu->needs_redraw = false;
|
||||
}
|
||||
|
||||
void
|
||||
demo_view_setup (demo_view_t *vu)
|
||||
{
|
||||
if (vu->metal)
|
||||
{
|
||||
demo_metal_set_gamma (vu->metal, 1.f);
|
||||
demo_metal_set_foreground (vu->metal, 0.f, 0.f, 0.f, 1.f);
|
||||
return;
|
||||
}
|
||||
if (!vu->vsync)
|
||||
demo_view_toggle_vsync (vu);
|
||||
demo_glstate_setup (vu->st);
|
||||
vu->renderer->setup ();
|
||||
vu->renderer->set_foreground (LIGHT_FG);
|
||||
vu->renderer->set_background (LIGHT_BG);
|
||||
demo_view_set_gamma_mode (vu, demo_view_t::GAMMA_SRGB);
|
||||
}
|
||||
|
||||
|
||||
@@ -9,16 +9,12 @@
|
||||
|
||||
#include "demo-common.h"
|
||||
#include "demo-buffer.h"
|
||||
#include "demo-glstate.h"
|
||||
#include "demo-renderer.h"
|
||||
|
||||
typedef struct demo_metal_t demo_metal_t;
|
||||
typedef struct demo_view_t demo_view_t;
|
||||
|
||||
demo_view_t *
|
||||
demo_view_create (demo_glstate_t *st, GLFWwindow *window);
|
||||
|
||||
demo_view_t *
|
||||
demo_view_create_metal (demo_metal_t *mt, GLFWwindow *window);
|
||||
demo_view_create (demo_renderer_t *renderer, GLFWwindow *window);
|
||||
|
||||
void
|
||||
demo_view_destroy (demo_view_t *vu);
|
||||
|
||||
@@ -8,6 +8,8 @@ gpu_demo_sources = files(
|
||||
'demo-font.cc',
|
||||
'demo-glstate.h',
|
||||
'demo-glstate.cc',
|
||||
'demo-renderer.h',
|
||||
'demo-renderer-gl.cc',
|
||||
'demo-shader.h',
|
||||
'demo-shader.cc',
|
||||
'demo-view.h',
|
||||
@@ -23,7 +25,6 @@ gpu_demo_sources = files(
|
||||
|
||||
if host_machine.system() == 'darwin'
|
||||
gpu_demo_sources += files(
|
||||
'demo-metal.h',
|
||||
'demo-metal.mm',
|
||||
)
|
||||
endif
|
||||
|
||||
Reference in new issue
Block a user