metal: GPU protocol + MetalGPU renders MSL triangle on iOS
Phase 8 step 3a of the Metal renderer port:
- New library/modules/gpu/ with types.sx (handles + ClearColor +
TextureFormat enum), api.sx (GPU :: protocol { ... } covering the
lifecycle / per-frame / resource / per-draw surface), and metal.sx
(MetalGPU backend implementing the protocol against CAMetalLayer).
Resource handles are 1-based indices into backend List(*void) tables.
MTL aggregates >16 bytes (MTLRegion, MTLScissorRect) pass via *T to
match arm64 Apple's indirect-by-reference ABI; MTLClearColor + CGSize
go through the HFA path as direct fn-pointer casts on objc_msgSend.
- UIKitPlatform got a gpu_mode: GpuMode toggle + sibling SxMetalView
class registration. In metal mode init skips EAGL context, the
did_finish_launching IMP skips the EAGL drawable-properties dict,
layoutSubviews reads the layer's bounds * dpi_scale into pixel_w/h
instead of allocating a GL renderbuffer, and end_frame is a no-op
(the MetalGPU owns its own present).
- examples/63-metal-clear.sx verifies the pipeline end-to-end on iOS
sim — compiles a pass-through MSL shader (packed_float2/packed_float4
to avoid alignment padding), uploads 3 vertices, draws a colored
triangle on a dark-blue clear.
Compiler fixes (filed-and-fixed in this branch):
- inline if X { return E; } followed by a fall-through final expression
no longer emits two terminators into the same basic block. Verified
by examples/83-inline-if-return-fallthrough.sx.
- Top-level type alias Name :: u32; now resolves correctly as the type
annotation on a global variable (was treated as ptr {}, breaking
comparisons + initializers). Verified by examples/84-global-type-alias.sx.
Issue->feature promotion:
- 16 historical examples/issue-NNNN.sx repros now confirmed-fixed and
renamed to focused feature names (67-82). Each gains a
tests/expected/*.txt + .exit pair so the regression suite covers them.
- 5 stale issue repros deleted (subsumed by broader tests).
Regression suite: 68 passing, 0 failed. macOS chess builds + runs; wasm
chess builds; iOS sim GLES chess still renders the full board; iOS sim
Metal demo renders the triangle.
This commit is contained in:
76
examples/68-generic-protocol-constraint.sx
Normal file
76
examples/68-generic-protocol-constraint.sx
Normal file
@@ -0,0 +1,76 @@
|
||||
// Generic struct `Animated($T: Lerpable)` monomorphized with a struct type — the
|
||||
// `#inline` protocol constraint participates in method dispatch via `self.from.lerp(...)`.
|
||||
|
||||
#import "modules/std.sx";
|
||||
#import "modules/math";
|
||||
|
||||
Lerpable :: protocol #inline {
|
||||
lerp :: (b: Self, t: f32) -> Self;
|
||||
}
|
||||
|
||||
Size :: struct {
|
||||
width, height: f32;
|
||||
zero :: () -> Size => .{ width = 0.0, height = 0.0 };
|
||||
}
|
||||
|
||||
impl Lerpable for Size {
|
||||
lerp :: (self: Size, b: Size, t: f32) -> Size {
|
||||
Size.{ width = self.width + (b.width - self.width) * t,
|
||||
height = self.height + (b.height - self.height) * t };
|
||||
}
|
||||
}
|
||||
|
||||
Animated :: struct ($T: Lerpable) {
|
||||
current: T;
|
||||
from: T;
|
||||
to: T;
|
||||
elapsed: f32;
|
||||
duration: f32;
|
||||
active: bool;
|
||||
|
||||
make :: (value: T) -> Animated(T) {
|
||||
Animated(T).{
|
||||
current = value, from = value, to = value,
|
||||
elapsed = 0.0, duration = 0.0, active = false
|
||||
};
|
||||
}
|
||||
|
||||
set_immediate :: (self: *Animated(T), value: T) {
|
||||
self.current = value;
|
||||
self.from = value;
|
||||
self.to = value;
|
||||
self.active = false;
|
||||
}
|
||||
|
||||
animate_to :: (self: *Animated(T), target: T, dur: f32) {
|
||||
self.from = self.current;
|
||||
self.to = target;
|
||||
self.elapsed = 0.0;
|
||||
self.duration = dur;
|
||||
self.active = true;
|
||||
}
|
||||
|
||||
tick :: (self: *Animated(T), dt: f32) {
|
||||
if !self.active { return; }
|
||||
self.elapsed += dt;
|
||||
t := clamp(self.elapsed / self.duration, 0.0, 1.0);
|
||||
self.current = self.from.lerp(self.to, t);
|
||||
if t >= 1.0 {
|
||||
self.current = self.to;
|
||||
self.active = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
main :: () -> void {
|
||||
anim := Animated(Size).make(Size.zero());
|
||||
anim.set_immediate(Size.{ width = 100.0, height = 50.0 });
|
||||
print("after set: {}x{}\n", anim.current.width, anim.current.height);
|
||||
|
||||
anim.animate_to(Size.{ width = 200.0, height = 100.0 }, 1.0);
|
||||
anim.tick(0.5);
|
||||
print("mid anim: {}x{}\n", anim.current.width, anim.current.height);
|
||||
|
||||
anim.tick(0.5);
|
||||
print("end anim: {}x{}\n", anim.current.width, anim.current.height);
|
||||
}
|
||||
Reference in New Issue
Block a user