metalicb

package
v0.6.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Rendered for darwin/amd64

Overview

Package metalicb provides a zero-cgo wrapper around Metal compute metal.MTLIndirectCommandBuffer graphs for single-replay N-layer dispatch workloads.

Encode a sequence of compute dispatches once on the CPU with Graph.SetComputeDispatch, optionally compact the buffer with Graph.Optimize, and replay the entire graph on subsequent steps with a single Graph.Replay call:

dev := metal.MTLCreateSystemDefaultDevice()
g, err := metalicb.NewGraph(dev, metalicb.Config{
	MaxCommands:      32,
	MaxKernelBuffers: 4,
})
if err != nil {
	return err
}
defer g.Close()

// Encode dispatches 0..31 once, then replay on each command buffer:
//   g.SetComputeDispatch(i, pipe, bindings, grid, tg)
//   g.Replay(computeEncoder, resources)

Compute pipelines bound inside a Graph must be compiled with SupportIndirectCommandBuffers enabled (see NewComputePipeline).

The zero value of Graph is not usable; construct one with NewGraph and release the underlying Metal object with Graph.Close.

Example
package main

import (
	"fmt"

	"github.com/tmc/apple/metal"
	"github.com/tmc/apple/x/metalicb"
)

func main() {
	dev := metal.MTLCreateSystemDefaultDevice()
	if !metalicb.SupportsComputeICB(dev) {
		fmt.Println("unsupported")
		return
	}

	g, err := metalicb.NewGraph(dev, metalicb.Config{
		MaxCommands:      8,
		MaxKernelBuffers: 4,
	})
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	defer g.Close()

	fmt.Println("size:", g.Size())
}
Output:
size: 8

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnsupported is returned when the Metal device does not support
	// compute indirect command buffers.
	ErrUnsupported = errors.New("metalicb: compute indirect command buffers unsupported")

	// ErrClosed is returned when a method is called on a nil or closed Graph.
	ErrClosed = errors.New("metalicb: graph closed")

	// ErrInvalidConfig is returned when Config contains invalid parameters.
	ErrInvalidConfig = errors.New("metalicb: invalid config")

	// ErrIndexOutOfRange is returned when a command index or range exceeds
	// the graph's capacity.
	ErrIndexOutOfRange = errors.New("metalicb: command index out of range")

	// ErrPipelineUnsupported is returned when a compute pipeline state was not
	// created with SupportIndirectCommandBuffers enabled.
	ErrPipelineUnsupported = errors.New("metalicb: pipeline does not support indirect command buffers")
)

Functions

func NewComputePipeline

func NewComputePipeline(dev metal.MTLDevice, fn metal.MTLFunction) (metal.MTLComputePipelineState, error)

NewComputePipeline compiles fn into a metal.MTLComputePipelineState with SupportIndirectCommandBuffers enabled so it can be bound inside a Graph.

func SupportsComputeICB

func SupportsComputeICB(dev metal.MTLDevice) bool

SupportsComputeICB reports whether dev supports compute indirect command buffers (Apple GPU family 3+ or Mac GPU family 2+).

Types

type BufferBinding

type BufferBinding struct {
	Buffer metal.MTLBuffer
	Offset uint
}

BufferBinding associates a Metal buffer and byte offset with a kernel argument slot when encoding a compute dispatch.

type Config

type Config struct {
	// MaxCommands is the fixed number of command slots allocated in the
	// indirect command buffer. Must be positive.
	MaxCommands int

	// MaxKernelBuffers is the maximum number of buffer arguments bound per
	// compute command. Ignored when InheritBuffers is true.
	MaxKernelBuffers int

	// InheritBuffers causes commands to inherit buffer bindings from the
	// parent compute command encoder instead of storing them per command.
	InheritBuffers bool

	// InheritPipelineState causes commands to inherit the compute pipeline
	// state from the parent encoder instead of storing it per command.
	InheritPipelineState bool

	// ConcurrentDispatch allows commands in the graph to overlap concurrently
	// on the GPU without automatic inter-command barriers. When false (the
	// default), SetComputeDispatch places a barrier before every command at
	// index > 0 so sequential pipelines observe prior writes.
	ConcurrentDispatch bool

	// DispatchThreads, when true, encodes grid dimensions as threads-per-grid
	// (MTLIndirectCommandTypeConcurrentDispatchThreads) instead of
	// threadgroups-per-grid (MTLIndirectCommandTypeConcurrentDispatch).
	DispatchThreads bool

	// Label is an optional debug label attached to the underlying
	// MTLIndirectCommandBuffer.
	Label string
}

Config configures a compute Graph backed by a metal.MTLIndirectCommandBuffer.

type Graph

type Graph struct {
	// contains filtered or unexported fields
}

Graph wraps a metal.MTLIndirectCommandBuffer for reusable compute dispatch sequences.

func NewGraph

func NewGraph(dev metal.MTLDevice, cfg Config) (*Graph, error)

NewGraph allocates a compute Graph backed by a shared metal.MTLIndirectCommandBuffer on dev.

It returns ErrUnsupported if dev is nil or does not support compute indirect command buffers, and ErrInvalidConfig if cfg is invalid.

func (*Graph) Buffer

func (g *Graph) Buffer() metal.MTLIndirectCommandBuffer

Buffer returns the underlying metal.MTLIndirectCommandBuffer, or nil if g is nil or closed.

func (*Graph) Close

func (g *Graph) Close() error

Close releases the underlying metal.MTLIndirectCommandBuffer. Close is idempotent.

func (*Graph) EncodedCount

func (g *Graph) EncodedCount() int

EncodedCount returns the high-water count of command slots encoded via Graph.SetComputeDispatch since creation or the last Graph.Reset.

func (*Graph) Optimize

func (g *Graph) Optimize(cmdBuf metal.MTLCommandBuffer) error

Optimize encodes a blit pass on cmdBuf that optimizes the indirect command buffer for repeated GPU execution via OptimizeIndirectCommandBufferWithRange.

Callers must commit cmdBuf (and wait or sequence it before replay) for the optimization to take effect.

func (*Graph) Replay

func (g *Graph) Replay(encoder metal.MTLComputeCommandEncoder, resources []ResourceUsage) error

Replay declares resources resident on encoder via UseResourceUsage and executes all encoded commands in g with a single ExecuteCommandsInBufferWithRange call.

If no commands were encoded via Graph.SetComputeDispatch (for example, when commands were encoded by a GPU kernel), Replay executes the full [0, Size()) range.

func (*Graph) ReplayRange

func (g *Graph) ReplayRange(encoder metal.MTLComputeCommandEncoder, start, count int, resources []ResourceUsage) error

ReplayRange declares resources resident on encoder via UseResourceUsage and executes commands in [start, start+count).

func (*Graph) Reset

func (g *Graph) Reset() error

Reset clears all command slots in g to their default empty state via ResetWithRange and resets Graph.EncodedCount to 0.

func (*Graph) SetBarrier

func (g *Graph) SetBarrier(idx int, enable bool) error

SetBarrier explicitly enables or clears the execution barrier on command slot idx.

func (*Graph) SetComputeDispatch

func (g *Graph) SetComputeDispatch(
	idx int,
	pipeline metal.MTLComputePipelineState,
	buffers []BufferBinding,
	grid, tg metal.MTLSize,
) error

SetComputeDispatch encodes compute dispatch slot idx into g.

When Config.InheritPipelineState is false, pipeline must be non-nil and compiled with SupportIndirectCommandBuffers enabled. When Config.InheritBuffers is false, len(buffers) must not exceed Config.MaxKernelBuffers; nil entries in buffers leave that buffer slot unbound.

When Config.ConcurrentDispatch is false and idx > 0, SetComputeDispatch automatically inserts a barrier before the command so prior dispatches in the graph complete before slot idx executes.

func (*Graph) Size

func (g *Graph) Size() int

Size returns the total number of command slots allocated in g, or 0 if g is nil or closed.

type ResourceUsage

type ResourceUsage struct {
	Resource metal.MTLResource
	Usage    metal.MTLResourceUsage
}

ResourceUsage declares a Metal resource and its access mode so Graph.Replay can mark it resident on the parent compute command encoder via UseResourceUsage.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL