ui: per-flush byte-offset on Metal vertex buffer fixes chess board
UIRenderer.flush wrote to mtl_vbuf at byte offset 0 on every flush. Metal records draw commands but reads the buffer at GPU execution time, so a frame with multiple flushes ended up rendering whatever the LAST writer left in the buffer for every draw. Chess UI hit this hard: each of the 32 pieces in the initial position triggers two bind_texture flushes (atlas -> pieces -> atlas), so ~64 mid-frame flushes silently rendered the final info-panel batch over the board and the sprites. New GPU protocol method update_buffer_at(buf, data, size, byte_offset); Metal impl writes at offset via [*]u8 arithmetic on [buf contents]. UIRenderer tracks mtl_buf_offset (reset in begin, advanced per flush, aligned to 16B, wraps on overflow) and draws each batch with vertex_off = byte_off / UI_VERTEX_BYTES. Metal buffer over-allocated 4x the per-flush max (~3 MB) for headroom. GL path untouched — glBufferData already orphans the storage. 71/71 regression tests pass. Metal-clear example, macOS GL chess, and WASM chess all still build.
This commit is contained in:
@@ -28,6 +28,13 @@ GPU :: protocol {
|
||||
create_shader :: (vsrc: string, fsrc: string) -> ShaderHandle;
|
||||
create_buffer :: (size_bytes: s64) -> BufferHandle;
|
||||
update_buffer :: (buf: BufferHandle, data: *void, size_bytes: s64);
|
||||
// Sub-buffer write at a byte offset. Required for Metal where re-using
|
||||
// the same buffer slice across multiple draws in a single command
|
||||
// encoder is a race: the GPU executes draws asynchronously and reads
|
||||
// shared-storage buffer contents at execution time, so the LAST writer
|
||||
// wins if every flush targets offset 0. Renderers that issue more than
|
||||
// one draw per frame must advance their write offset between flushes.
|
||||
update_buffer_at :: (buf: BufferHandle, data: *void, size_bytes: s64, byte_offset: s64);
|
||||
create_texture :: (w: s32, h: s32, format: TextureFormat, pixels: *void) -> TextureHandle;
|
||||
update_texture_region :: (tex: TextureHandle, x: s32, y: s32, w: s32, h: s32, pixels: *void);
|
||||
|
||||
|
||||
@@ -149,6 +149,12 @@ impl GPU for MetalGPU {
|
||||
}
|
||||
}
|
||||
|
||||
update_buffer_at :: (self: *MetalGPU, buf: BufferHandle, data: *void, size_bytes: s64, byte_offset: s64) {
|
||||
inline if OS == .ios {
|
||||
metal_update_buffer_at_ios(self, buf, data, size_bytes, byte_offset);
|
||||
}
|
||||
}
|
||||
|
||||
create_texture :: (self: *MetalGPU, w: s32, h: s32, format: TextureFormat, pixels: *void) -> TextureHandle {
|
||||
inline if OS != .ios { return 0; }
|
||||
metal_create_texture_ios(self, w, h, format, pixels);
|
||||
@@ -445,6 +451,21 @@ metal_update_buffer_ios :: (self: *MetalGPU, handle: u32, data: *void, size_byte
|
||||
memcpy(dst, data, size_bytes);
|
||||
}
|
||||
|
||||
metal_update_buffer_at_ios :: (self: *MetalGPU, handle: u32, data: *void, size_bytes: s64, byte_offset: s64) {
|
||||
inline if OS != .ios { return; }
|
||||
buf := metal_lookup_buffer(self, handle);
|
||||
if buf == null { return; }
|
||||
if data == null { return; }
|
||||
if size_bytes <= 0 { return; }
|
||||
if byte_offset < 0 { return; }
|
||||
|
||||
msg_o : (*void, *void) -> *void = xx objc_msgSend;
|
||||
base := msg_o(buf, sel_registerName("contents".ptr));
|
||||
if base == null { return; }
|
||||
dst : [*]u8 = xx base;
|
||||
memcpy(xx @dst[byte_offset], data, size_bytes);
|
||||
}
|
||||
|
||||
metal_lookup_buffer :: (self: *MetalGPU, handle: u32) -> *void {
|
||||
inline if OS != .ios { return null; }
|
||||
if handle == 0 { return null; }
|
||||
|
||||
Reference in New Issue
Block a user