Last updated: 2026-09-07

Metal Runtime

Metal Command

All API Sections

Metal command buffer wrapper — dispatch recording and GPU synchronization.

This module is the low-level bridge between ZINC's compute dispatchers and the Objective-C Metal shim. It records compute work, barriers, and timing mode so higher-level inference code can stay backend-agnostic.

4 exports 14 methods src/metal/command.zig

4 exports shown

enum

CommandEncoderMode

#
pub const CommandEncoderMode = enum(u8)

Encoder policy used when opening a Metal compute command buffer.

src/metal/command.zig:32

struct

MetalCommand

#
pub const MetalCommand = struct

A recorded command buffer that encodes compute dispatches for the GPU.

src/metal/command.zig:38

Methods

14

method

MetalCommand.setTimingLabel

#
pub fn setTimingLabel(self: *MetalCommand, label: []const u8) void

Attach a one-dispatch timing label consumed by `ZINC_METAL_KERNEL_TIMING`.

The timing aggregator copies the bytes before this label can go stale.

src/metal/command.zig:52

method

MetalCommand.clearTimingLabel

#
pub fn clearTimingLabel(self: *MetalCommand) void

No public doc comment yet.

src/metal/command.zig:56

method

MetalCommand.dispatch

#
pub fn dispatch( self: *MetalCommand, pipe: *const MetalPipeline, grid: [3]u32, block: [3]u32, bufs: []const *const MetalBuffer, push_data: ?*const anyopaque, push_size: usize, ) void

Encode a compute dispatch binding buffers, push constants, grid, and block sizes.

src/metal/command.zig:61

method

MetalCommand.dispatchV2

#
pub fn dispatchV2( self: *MetalCommand, pipe: *const MetalPipeline, grid: [3]u32, block: [3]u32, bufs: []const *const MetalBuffer, push_data: ?*const anyopaque, push_size: usize, push_idx: u32, ) void

Dispatch with explicit push constant buffer index.

SPIRV-Cross compiled shaders place push constants at a specific buffer index (often 0 or 1). Data buffers in `bufs` are bound at all other indices in order, skipping push_idx.

src/metal/command.zig:98

method

MetalCommand.dispatchV2WithTgMem

#
pub fn dispatchV2WithTgMem( self: *MetalCommand, pipe: *const MetalPipeline, grid: [3]u32, block: [3]u32, bufs: []const *const MetalBuffer, push_data: ?*const anyopaque, push_size: usize, push_idx: u32, tg_mem_size: u32, ) void

Dispatch with explicit threadgroup memory allocation.

src/metal/command.zig:133

method

MetalCommand.barrier

#
pub fn barrier(self: *MetalCommand) void

Insert a memory barrier ensuring all prior dispatches complete before subsequent ones.

src/metal/command.zig:170

method

MetalCommand.barrierBuffers

#
pub fn barrierBuffers(self: *MetalCommand, bufs: []const *const MetalBuffer) void

Insert a resource-scoped memory barrier for the listed buffers.

src/metal/command.zig:183

method

MetalCommand.barrierResourceBuffers

#
pub fn barrierResourceBuffers(self: *MetalCommand, bufs: []const *const MetalBuffer) void

Insert a true resource-scoped barrier.

Use only when later work depends on the listed buffers and independent prior dispatches should stay free to overlap on a concurrent encoder.

src/metal/command.zig:229

method

MetalCommand.commitAndWait

#
pub fn commitAndWait(self: *MetalCommand) void

Commit the command buffer to the GPU and block until execution completes.

src/metal/command.zig:265

method

MetalCommand.commitAsync

#
pub fn commitAsync(self: *MetalCommand) void

Commit the command buffer for async GPU execution; call `wait` later to synchronize.

src/metal/command.zig:273

method

MetalCommand.wait

#
pub fn wait(self: *MetalCommand) void

Block until an async-committed command buffer finishes execution.

src/metal/command.zig:281

method

MetalCommand.waitGpuDurationNs

#
pub fn waitGpuDurationNs(self: *MetalCommand) u64

Wait for completion and return Metal's GPU execution duration.

src/metal/command.zig:289

method

MetalCommand.releaseCompleted

#
pub fn releaseCompleted(self: *MetalCommand) void

Release an async command buffer that is known to be completed by a later queue-ordered wait.

The shim falls back to waiting if it is still pending.

src/metal/command.zig:300

method

MetalCommand.gpuDurationNs

#
pub fn gpuDurationNs(self: *const MetalCommand) u64

Return Metal-reported GPU execution time for a completed async command.

src/metal/command.zig:308

function

beginCommand

#
pub fn beginCommand(ctx: ?*shim.MetalCtx) !MetalCommand

Allocate a new command buffer from the given Metal context.

src/metal/command.zig:317

function

beginCommandWithMode

#
pub fn beginCommandWithMode(ctx: ?*shim.MetalCtx, mode: CommandEncoderMode) !MetalCommand

Allocate a command buffer using the requested encoder/barrier policy.

src/metal/command.zig:322