issue-0028: ?Protocol = null sentinel-shaped optional protocols

Protocol structs registered via registerProtocolDecl carry a new
is_protocol flag; the ?T paths in sizeOf/typeSizeBytes/toLLVMType
recognise it and lay out ?Protocol as the protocol struct itself
(ctx == null IS the "none" state), matching how ?Closure / ?*T are
sentinel-shaped — no extra storage.

Method dispatch on ?Protocol auto-unwraps in lowerCall's field-access
path; the unwrap is structurally a no-op so we just rebind obj_ty to
the payload type. resolveCallParamTypes extended for optional-protocol
receivers so enum-literal args (gpu.create_texture(.r8, ...)) get the
right target_type and don't silently collapse to tag=0 : s32 — same
issue-0031-class bug closed in Session 66, one type-system layer
deeper.

Library: UIRenderer / UIPipeline / GlyphCache migrated from the verbose
gpu: GPU = ---; has_gpu: bool pattern to gpu: ?GPU = null. set_gpu no
longer maintains a parallel bool flag.

Bundled: dock.sx threads delta_time as a struct field rather than via
a global pointer (cleanup unrelated to issue-0028, committed alongside).

Verified: 85/85 regression tests pass; iOS-sim chess + macOS chess
both render correctly post-migration.
This commit is contained in:
agra
2026-05-18 18:32:55 +03:00
parent f9ecf9d00e
commit 79419b99bd
11 changed files with 117 additions and 93 deletions

View File

@@ -457,13 +457,11 @@ impl View for DockPanel {
// Dock — dockable container with drag-and-drop zones
// =============================================================================
// Global delta_time pointer — set by main.sx
g_dock_delta_time : *f32 = null;
Dock :: struct {
children: List(ViewChild);
alignments: List(Alignment);
interaction: *DockInteraction; // heap-allocated, shared with DockPanels
delta_time: *f32;
// Config
background: ?Color;
@@ -475,11 +473,12 @@ Dock :: struct {
enable_corners: bool;
on_dock: ?Closure(s64, DockZone);
make :: (interaction: *DockInteraction) -> Dock {
make :: (interaction: *DockInteraction, delta_time: *f32) -> Dock {
d : Dock = ---;
d.children = List(ViewChild).{};
d.alignments = List(Alignment).{};
d.interaction = interaction;
d.delta_time = delta_time;
d.background = null;
d.corner_radius = 0.0;
d.hint_size = 40.0;
@@ -519,8 +518,7 @@ impl View for Dock {
interaction := self.interaction;
interaction.ensure_capacity(self.children.len);
// Tick animations (g_dock_delta_time is always set before main loop)
dt : f32 = g_dock_delta_time.*;
dt : f32 = self.delta_time.*;
interaction.tick_animations(dt);
i : s64 = 0;

View File

@@ -179,20 +179,17 @@ GlyphCache :: struct {
last_shape_len: s64;
last_shape_size_q: u16;
// GPU protocol backend. When `has_gpu`, atlas creation + dirty uploads
// route through `gpu` instead of raw GL.
gpu: GPU = ---;
has_gpu: bool = false;
// GPU protocol backend. When set, atlas creation + dirty uploads route
// through `gpu` instead of raw GL.
gpu: ?GPU = null;
init :: (self: *GlyphCache, path: [:0]u8, default_size: f32) {
// Preserve any pre-set GPU dispatch across the zero-out — the
// surrounding struct memset would otherwise wipe it.
saved_gpu := self.gpu;
saved_has_gpu := self.has_gpu;
// Zero out the entire struct first (parent may be uninitialized with = ---)
memset(self, 0, size_of(GlyphCache));
self.gpu = saved_gpu;
self.has_gpu = saved_has_gpu;
// Load font file
file_size : s32 = 0;
@@ -264,7 +261,7 @@ GlyphCache :: struct {
// update_texture_region — same result as the GL path's glTexImage2D
// with the zeroed bitmap, but works whether or not the backend
// accepts CPU pixel pointers at create time.
if self.has_gpu {
if self.gpu != null {
self.texture_id = self.gpu.create_texture(
self.atlas_width, self.atlas_height, .r8, null);
self.dirty = true;
@@ -435,7 +432,7 @@ GlyphCache :: struct {
// the frame's render pass.
flush :: (self: *GlyphCache) {
if self.dirty == false { return; }
if self.has_gpu { return; }
if self.gpu != null { return; }
glBindTexture(GL_TEXTURE_2D, self.texture_id);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, self.atlas_width, self.atlas_height, GL_RED, GL_UNSIGNED_BYTE, self.bitmap);
@@ -443,7 +440,7 @@ GlyphCache :: struct {
}
upload_atlas_to_gpu :: (self: *GlyphCache) {
if self.has_gpu == false { return; }
if self.gpu == null { return; }
if self.dirty == false { return; }
self.gpu.update_texture_region(self.texture_id, 0, 0,
self.atlas_width, self.atlas_height, xx self.bitmap);
@@ -503,7 +500,7 @@ GlyphCache :: struct {
self.atlas_height = new_h;
// Recreate atlas at the new size.
if self.has_gpu {
if self.gpu != null {
// No destroy_texture in the GPU protocol yet — old atlas
// leaks in the backend table until process exit. Atlas grow
// is rare so this is acceptable for now.

View File

@@ -25,21 +25,17 @@ UIPipeline :: struct {
has_body: bool;
parent_allocator: Allocator;
// GPU protocol backend. When `has_gpu`, the pipeline propagates this
// to its renderer + font, and skips the per-frame GL state setup in
// GPU protocol backend. When set, the pipeline propagates this to its
// renderer + font, and skips the per-frame GL state setup in
// commit_gpu (Metal bakes blend mode into the pipeline state).
gpu: GPU = ---;
has_gpu: bool = false;
gpu: ?GPU = null;
// Set the GPU dispatch BEFORE calling init() / init_font() so the
// shaders + atlas land on the right backend.
set_gpu :: (self: *UIPipeline, gpu: GPU) {
self.gpu = gpu;
self.has_gpu = true;
self.renderer.gpu = gpu;
self.renderer.has_gpu = true;
self.font.gpu = gpu;
self.font.has_gpu = true;
self.gpu = xx gpu;
self.renderer.gpu = xx gpu;
self.font.gpu = xx gpu;
}
init :: (self: *UIPipeline, width: f32, height: f32) {
@@ -167,7 +163,7 @@ UIPipeline :: struct {
}
commit_gpu :: (self: *UIPipeline) {
if !self.has_gpu {
if self.gpu == null {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_DEPTH_TEST);
@@ -182,7 +178,7 @@ UIPipeline :: struct {
self.font.upload_atlas_to_gpu();
self.renderer.flush();
if !self.has_gpu {
if self.gpu == null {
glDisable(GL_BLEND);
}
}

View File

@@ -32,11 +32,10 @@ UIRenderer :: struct {
current_texture: u32;
draw_calls: s64;
// GPU protocol backend. When `has_gpu`, the renderer routes shader /
// buffer / texture / draw calls through `gpu` instead of raw GL. The
// chess game sets this on iOS to a boxed `*MetalGPU`.
gpu: GPU = ---;
has_gpu: bool = false;
// GPU protocol backend. When set, the renderer routes shader / buffer /
// texture / draw calls through `gpu` instead of raw GL. The chess game
// sets this on iOS to a boxed `*MetalGPU`.
gpu: ?GPU = null;
mtl_shader: ShaderHandle = 0;
mtl_vbuf: BufferHandle = 0;
// Per-frame byte offset into the Metal vertex buffer. Each flush writes
@@ -56,7 +55,7 @@ UIRenderer :: struct {
self.vertex_count = 0;
self.dpi_scale = 1.0;
if self.has_gpu {
if self.gpu != null {
// ── Metal backend (via GPU protocol) ───────────────────────
// Oversize the GPU buffer enough to hold many sub-batches per
// frame without wrapping. With per-flush offset advance, each
@@ -115,7 +114,7 @@ UIRenderer :: struct {
proj := Mat4.ortho(0.0, width, height, 0.0, -1.0, 1.0);
if self.has_gpu {
if self.gpu != null {
// Reset the per-frame ring offset; this frame's flushes start at 0.
self.mtl_buf_offset = 0;
// Pipeline state + vertex buffer + projection + initial texture.
@@ -251,7 +250,7 @@ UIRenderer :: struct {
case .clip_push: {
self.flush();
dpi := self.dpi_scale;
if self.has_gpu {
if self.gpu != null {
// Metal: pixel coords, top-left origin (no Y flip).
self.gpu.set_scissor(
xx (node.frame.origin.x * dpi),
@@ -272,7 +271,7 @@ UIRenderer :: struct {
}
case .clip_pop: {
self.flush();
if self.has_gpu {
if self.gpu != null {
self.gpu.disable_scissor();
} else {
glDisable(GL_SCISSOR_TEST);
@@ -290,7 +289,7 @@ UIRenderer :: struct {
upload_size : s64 = self.vertex_count * UI_VERTEX_BYTES;
if self.has_gpu {
if self.gpu != null {
// Mirror the GL path: bind current texture before drawing.
// current_texture may have changed since the last flush.
self.gpu.set_texture(0, self.current_texture);