Files
sx/library/modules/ui/pipeline.sx
agra a1647eab9b metal: pause step 3b pending sx-side fixes (filed 0024-0030)
Step 3b code is wired across UIRenderer + GlyphCache + UIPipeline +
chess game (gpu_mode = .metal on iOS, MetalGPU bound via the GPU
protocol). macOS GL chess, iOS-sim GLES chess, and iOS-sim Metal
triangle (63-metal-clear.sx) all still render.

iOS-sim Metal chess crashes inside replaceRegion uploading the 1MB
font atlas. Bisecting that crash exposed several sx-language issues
where mid-bisect tracers (NSLog inside if/else branch bodies) didn't
produce output, blocking further investigation.

Filing each finding as examples/issue-NNNN.sx rather than working
around piecemeal:

Bugs:
- 0024 NSLog/foreign-call inside if/else body not producing output
- 0025 C-ABI param coercion incomplete for composites >16B
       (combined direct-call abiCoerceParamType TODO + call_indirect
        path that doesn't apply C-ABI coercion at all)
- 0026 replaceRegion 1MB upload crash (likely downstream of 0025)

Features needed for step 4 + cleanup:
- 0027 Obj-C block bridge (^{...}) for animateWithDuration:
- 0028 Optional protocol box (?GPU = null) replaces T = ---; has_T: bool
- 0029 destroy_texture/buffer/shader on GPU protocol
- 0030 extern cross-file globals

Library-side: renderer.sx + glyph_cache.sx + pipeline.sx gain a
`gpu: GPU = ---; has_gpu: bool` field pair + branches that route every
GL touchpoint through the protocol when has_gpu. glyph_cache.init
saves/restores those fields around its memset. pipeline.set_gpu()
propagates to renderer + font. Renderer's MSL shader source added as
UI_MSL_SRC using packed_float2/packed_float4 to keep the 12-float
interleaved vertex layout tight (48 bytes).

metal.sx: dual-phase init (init(null, 0, 0) for eager device+queue,
re-init with the layer once UIKit installs the SxMetalView).
setStorageMode:.shared on every texture descriptor to ensure CPU-
writable atlas pixels on Apple Silicon iOS-sim.

Regression suite: 68 passing, 0 failed. WASM chess build currently
broken under step 3b state (silent compiler crash); documented in
CHECKPOINT.md, likely fallout from one of the filed issues (probably
0028 — the verbose protocol-box pattern). Step 3b resumes after
0024-0030 land.
2026-05-17 21:17:17 +03:00

185 lines
6.0 KiB
Plaintext
Executable File

#import "modules/std.sx";
#import "modules/allocators.sx";
#import "modules/opengl.sx";
#import "modules/gpu/api.sx";
#import "modules/ui/types.sx";
#import "modules/ui/render.sx";
#import "modules/ui/events.sx";
#import "modules/ui/view.sx";
#import "modules/ui/renderer.sx";
UIPipeline :: struct {
renderer: UIRenderer;
render_tree: RenderTree;
font: GlyphCache;
screen_width: f32;
screen_height: f32;
root: ViewChild;
has_root: bool;
// Frame arena infrastructure
arena_a: Arena;
arena_b: Arena;
frame_index: s64;
body: Closure() -> View;
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
// commit_gpu (Metal bakes blend mode into the pipeline state).
gpu: GPU = ---;
has_gpu: bool = false;
// 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;
}
init :: (self: *UIPipeline, width: f32, height: f32) {
self.render_tree = RenderTree.init();
self.renderer.init();
self.screen_width = width;
self.screen_height = height;
self.has_root = false;
self.has_body = false;
self.frame_index = 0;
}
init_font :: (self: *UIPipeline, path: [:0]u8, size: f32, dpi_scale: f32) {
self.font.init(path, size);
self.font.set_dpi_scale(dpi_scale);
self.renderer.dpi_scale = dpi_scale;
set_global_font(@self.font);
}
set_root :: (self: *UIPipeline, view: View) {
self.root = .{ view = view };
self.has_root = true;
}
set_body :: (self: *UIPipeline, body_fn: Closure() -> View) {
self.body = body_fn;
self.has_body = true;
self.parent_allocator = context.allocator;
// Initialize both arenas (256KB initial, grows automatically)
self.arena_a.create(self.parent_allocator, 262144);
self.arena_b.create(self.parent_allocator, 262144);
self.frame_index = 0;
}
resize :: (self: *UIPipeline, width: f32, height: f32) {
self.screen_width = width;
self.screen_height = height;
}
// Re-layout and re-render the existing view tree at current screen size.
// Does NOT rebuild from body — safe to call from C callbacks (no arena/context needed).
tick_relayout :: (self: *UIPipeline) {
if self.has_root == false { return; }
proposal := ProposedSize.fixed(self.screen_width, self.screen_height);
self.root.view.size_that_fits(proposal);
self.root.computed_frame = Frame.{
origin = Point.zero(),
size = Size.{ width = self.screen_width, height = self.screen_height }
};
self.root.view.layout(self.root.computed_frame);
self.render_tree.clear();
ctx := RenderContext.init(@self.render_tree);
self.root.view.render(@ctx, self.root.computed_frame);
self.commit_gpu();
}
// Process a single event through the view tree
dispatch_event :: (self: *UIPipeline, event: *Event) -> bool {
if self.has_root == false { return false; }
self.root.view.handle_event(event, self.root.computed_frame);
}
// Run one frame: layout → render → commit
tick :: (self: *UIPipeline) {
if self.has_body {
self.tick_with_body();
return;
}
if self.has_root == false { return; }
proposal := ProposedSize.fixed(self.screen_width, self.screen_height);
// Layout
self.root.view.size_that_fits(proposal);
self.root.computed_frame = Frame.{
origin = Point.zero(),
size = Size.{ width = self.screen_width, height = self.screen_height }
};
self.root.view.layout(self.root.computed_frame);
// Render to tree
self.render_tree.clear();
ctx := RenderContext.init(@self.render_tree);
self.root.view.render(@ctx, self.root.computed_frame);
// Commit to GPU
self.commit_gpu();
}
tick_with_body :: (self: *UIPipeline) {
build_arena : *Arena = if self.frame_index & 1 == 0 then @self.arena_a else @self.arena_b;
build_arena.reset();
// Reset render_tree nodes (backing is stale after arena reset)
self.render_tree.nodes.items = null;
self.render_tree.nodes.len = 0;
self.render_tree.nodes.cap = 0;
push Context.{ allocator = xx build_arena, data = context.data } {
// Workaround: self.body() crashes through struct field (issue-0010)
body_fn := self.body;
root_view := body_fn();
self.root = .{ view = root_view };
self.has_root = true;
proposal := ProposedSize.fixed(self.screen_width, self.screen_height);
self.root.view.size_that_fits(proposal);
self.root.computed_frame = Frame.{
origin = Point.zero(),
size = Size.{ width = self.screen_width, height = self.screen_height }
};
self.root.view.layout(self.root.computed_frame);
self.render_tree.clear();
ctx := RenderContext.init(@self.render_tree);
self.root.view.render(@ctx, self.root.computed_frame);
self.commit_gpu();
}
self.frame_index += 1;
}
commit_gpu :: (self: *UIPipeline) {
if !self.has_gpu {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_DEPTH_TEST);
}
self.renderer.begin(self.screen_width, self.screen_height, self.font.texture_id);
self.renderer.process(@self.render_tree);
self.renderer.flush();
if !self.has_gpu {
glDisable(GL_BLEND);
}
}
}