Table of Contents

Class MetalKernelParameterBinder

Namespace
DotCompute.Backends.Metal.Execution
Assembly
DotCompute.Backends.Metal.dll

Handles parameter binding for Metal kernel execution. Maps IUnifiedMemoryBuffer instances to Metal buffer bindings on compute command encoders.

public sealed class MetalKernelParameterBinder
Inheritance
MetalKernelParameterBinder
Inherited Members

Constructors

MetalKernelParameterBinder(ILogger<MetalKernelParameterBinder>?)

Initializes a new instance of the MetalKernelParameterBinder class.

public MetalKernelParameterBinder(ILogger<MetalKernelParameterBinder>? logger = null)

Parameters

logger ILogger<MetalKernelParameterBinder>

Optional logger for diagnostic information.

Methods

BindBuffer(nint, IUnifiedMemoryBuffer, int)

Binds a single buffer to a specific index on the compute encoder.

public void BindBuffer(nint encoder, IUnifiedMemoryBuffer buffer, int index)

Parameters

encoder nint

The Metal compute command encoder.

buffer IUnifiedMemoryBuffer

The buffer to bind.

index int

The binding index (must match [[buffer(N)]] in kernel).

Exceptions

ArgumentNullException

Thrown when buffer is null.

ArgumentException

Thrown when buffer is not a Metal buffer or has invalid state.

BindBufferWithOffset(nint, IUnifiedMemoryBuffer, nuint, int)

Binds a buffer with a specific offset.

public void BindBufferWithOffset(nint encoder, IUnifiedMemoryBuffer buffer, nuint offset, int index)

Parameters

encoder nint

The Metal compute command encoder.

buffer IUnifiedMemoryBuffer

The buffer to bind.

offset nuint

The offset in bytes into the buffer.

index int

The binding index (must match [[buffer(N)]] in kernel).

Exceptions

ArgumentNullException

Thrown when buffer is null.

ArgumentException

Thrown when buffer is not a Metal buffer or has invalid state.

ArgumentOutOfRangeException

Thrown when offset is negative.

BindBytes(nint, nint, nuint, int)

Binds raw bytes as a parameter (for small data like scalars or constants).

public void BindBytes(nint encoder, nint data, nuint length, int index)

Parameters

encoder nint

The Metal compute command encoder.

data nint

Pointer to the data to bind.

length nuint

Length of the data in bytes.

index int

The binding index (must match [[buffer(N)]] in kernel).

Remarks

This method is useful for binding small constant data (e.g., matrix dimensions, scalar parameters). For data larger than 4KB, consider using a buffer instead for better performance. The data is copied into the command buffer, so the source memory doesn't need to remain valid.

Exceptions

ArgumentNullException

Thrown when data pointer is zero.

ArgumentOutOfRangeException

Thrown when length or index is negative.

BindParameters(nint, params IUnifiedMemoryBuffer[])

Binds buffer parameters to a Metal compute command encoder.

public void BindParameters(nint encoder, params IUnifiedMemoryBuffer[] buffers)

Parameters

encoder nint

The Metal compute command encoder.

buffers IUnifiedMemoryBuffer[]

Array of buffers to bind as kernel parameters.

Remarks

Buffers are bound sequentially starting at index 0. The order must match the kernel's parameter declaration. For example, a kernel declared as:

kernel void myKernel(device float* a [[buffer(0)]],
                     device float* b [[buffer(1)]],
                     device float* result [[buffer(2)]])

Requires buffers to be passed in the order [a, b, result].

Exceptions

ArgumentNullException

Thrown when encoder is zero or buffers is null.

ArgumentException

Thrown when any buffer is not a Metal buffer or has invalid state.

BindValue<T>(nint, T, int)

Binds a typed value as a parameter (convenience wrapper for BindBytes).

public void BindValue<T>(nint encoder, T value, int index) where T : unmanaged

Parameters

encoder nint

The Metal compute command encoder.

value T

The value to bind.

index int

The binding index (must match [[buffer(N)]] in kernel).

Type Parameters

T

The unmanaged type of the value.

Exceptions

ArgumentNullException

Thrown when encoder is zero.

ArgumentOutOfRangeException

Thrown when index is negative.

ValidateBuffers(params IUnifiedMemoryBuffer[])

Validates that all buffers are Metal-compatible before binding.

public bool ValidateBuffers(params IUnifiedMemoryBuffer[] buffers)

Parameters

buffers IUnifiedMemoryBuffer[]

Array of buffers to validate.

Returns

bool

True if all buffers are valid Metal buffers; otherwise, false.

Exceptions

ArgumentNullException

Thrown when buffers is null.