Documentation
¶
Overview ¶
Package msl implements Metal Shading Language (MSL) code generation for naga.
MSL is Apple's shader language for the Metal graphics API. It is based on C++14 with extensions for GPU programming, including explicit address spaces, attribute-based parameter binding, and a metal:: namespace for standard library functions.
Usage ¶
To compile a WGSL shader to MSL:
module, err := wgsl.Parse(source)
if err != nil {
return err
}
options := msl.Options{
LangVersion: msl.Version{Major: 2, Minor: 1},
}
mslCode, err := msl.Compile(module, options)
if err != nil {
return err
}
MSL Language Versions ¶
The backend supports MSL 1.2 through 3.0. Features used depend on the target version:
- MSL 1.2: Basic shaders, most texture operations
- MSL 2.0: Tessellation, indirect command buffers
- MSL 2.1: Improved array handling
- MSL 2.3: Ray tracing, 64-bit atomics
- MSL 3.0: Mesh shaders, extended features
Type Mapping ¶
WGSL types map to MSL as follows:
WGSL MSL ---- --- bool bool i32 int u32 uint f32 float f16 half vec2<T> metal::T2 vec3<T> metal::T3 vec4<T> metal::T4 mat4x4<f32> metal::float4x4 array<T, N> array<T, N> (wrapped in struct) texture_2d metal::texture2d<float> sampler metal::sampler
Address Spaces ¶
WGSL address spaces map to MSL as:
uniform -> constant storage -> device private -> thread workgroup -> threadgroup function -> thread (stack)
Entry Points ¶
Entry points are generated with appropriate stage keywords:
- vertex: Vertex shaders with [[stage_in]], [[vertex_id]], etc.
- fragment: Fragment shaders with [[position]], [[color(N)]], etc.
- kernel: Compute shaders with [[thread_position_in_grid]], etc.
Helper Functions ¶
Some WGSL operations require polyfill functions in MSL:
- naga_div: Safe integer division (typed overloads, handles zero and INT_MIN/-1)
- naga_mod: Safe integer modulo (typed overloads, handles zero and INT_MIN/-1)
- naga_modf: modf with WGSL-compatible result struct
- naga_frexp: frexp with WGSL-compatible result struct
- _mslBufferSizes: Struct for runtime-sized array buffer sizes
Index ¶
- Variables
- type AttributeMapping
- type BindExternalTextureTarget
- type BindSamplerTarget
- type BindTarget
- type BoundsCheckPolicies
- type BoundsCheckPolicy
- type EntryPointResources
- type EntryPointSelector
- type InlineSampler
- type Options
- type PipelineOptions
- type SamplerAddress
- type SamplerBorderColor
- type SamplerCompareFunc
- type SamplerCoord
- type SamplerFilter
- type TranslationInfo
- type Version
- type VertexBufferMapping
- type VertexBufferStepMode
- type VertexFormat
Constants ¶
This section is empty.
Variables ¶
var ( Version1_0 = Version{Major: 1, Minor: 0} Version1_2 = Version{Major: 1, Minor: 2} Version2_0 = Version{Major: 2, Minor: 0} Version2_1 = Version{Major: 2, Minor: 1} Version2_3 = Version{Major: 2, Minor: 3} Version2_4 = Version{Major: 2, Minor: 4} Version3_0 = Version{Major: 3, Minor: 0} Version3_1 = Version{Major: 3, Minor: 1} )
Common MSL versions.
Functions ¶
This section is empty.
Types ¶
type AttributeMapping ¶ added in v0.15.0
type AttributeMapping struct {
ShaderLocation uint32
Offset uint32
Format VertexFormat
}
AttributeMapping maps a vertex attribute to a shader location.
type BindExternalTextureTarget ¶ added in v0.15.0
BindExternalTextureTarget specifies the Metal binding slots for an external texture global variable. External textures are lowered to 3 texture planes and a constant buffer of NagaExternalTextureParams.
type BindSamplerTarget ¶ added in v0.15.0
type BindSamplerTarget struct {
// IsInline indicates the sampler is an inline (constexpr) sampler.
IsInline bool
// Slot is the binding slot (for resource samplers) or index into
// Options.InlineSamplers (for inline samplers).
Slot uint8
}
BindSamplerTarget specifies how a sampler is bound.
type BindTarget ¶
type BindTarget struct {
// Buffer is the buffer binding slot. Nil if not bound as buffer.
Buffer *uint8
// Texture is the texture binding slot. Nil if not bound as texture.
Texture *uint8
// Sampler is the sampler binding slot. Nil if not bound as sampler.
Sampler *BindSamplerTarget
// ExternalTexture is the binding for external texture planes + params.
ExternalTexture *BindExternalTextureTarget
// Mutable indicates if this is a read-write resource.
Mutable bool
}
BindTarget specifies the Metal binding slots for a resource.
type BoundsCheckPolicies ¶
type BoundsCheckPolicies struct {
// Index applies to array, vector, and matrix indexing.
Index BoundsCheckPolicy
// Buffer applies to buffer (storage/uniform) accesses.
Buffer BoundsCheckPolicy
// Image applies to texture read/write operations.
Image BoundsCheckPolicy
// BindingArray applies to binding array (texture array) indexing.
BindingArray BoundsCheckPolicy
}
BoundsCheckPolicies configures bounds checking for different access types.
func DefaultBoundsCheckPolicies ¶
func DefaultBoundsCheckPolicies() BoundsCheckPolicies
DefaultBoundsCheckPolicies returns conservative bounds check policies.
func (BoundsCheckPolicies) Contains ¶ added in v0.15.0
func (p BoundsCheckPolicies) Contains(policy BoundsCheckPolicy) bool
Contains returns true if any of the policy fields equals the given policy. Note: BindingArray is intentionally excluded, matching the Rust implementation.
type BoundsCheckPolicy ¶
type BoundsCheckPolicy uint8
BoundsCheckPolicy controls how out-of-bounds accesses are handled.
const ( // BoundsCheckUnchecked performs no bounds checking. // Out-of-bounds accesses have undefined behavior. BoundsCheckUnchecked BoundsCheckPolicy = iota // BoundsCheckReadZeroSkipWrite returns zero for out-of-bounds reads // and skips out-of-bounds writes. BoundsCheckReadZeroSkipWrite // BoundsCheckRestrict clamps indices to valid range. BoundsCheckRestrict )
type EntryPointResources ¶
type EntryPointResources struct {
// Resources maps (group, binding) pairs to Metal bind targets.
Resources map[ir.ResourceBinding]BindTarget
// PushConstantBuffer is the buffer slot for push constants.
// Nil if push constants are not used.
PushConstantBuffer *uint8
// SizesBuffer is the buffer slot for runtime array sizes.
// Required when using runtime-sized arrays.
SizesBuffer *uint8
// ImmediatesBuffer is the buffer slot for immediate data.
// Nil if immediate data is not used.
ImmediatesBuffer *uint8
}
EntryPointResources maps WGSL resource bindings to Metal binding slots.
type EntryPointSelector ¶
type EntryPointSelector struct {
Stage ir.ShaderStage
Name string
}
EntryPointSelector identifies a specific entry point.
type InlineSampler ¶ added in v0.15.0
type InlineSampler struct {
Coord SamplerCoord
Address [3]SamplerAddress
BorderColor SamplerBorderColor
MagFilter SamplerFilter
MinFilter SamplerFilter
MipFilter *SamplerFilter // nil if not set
CompareFunc SamplerCompareFunc
}
InlineSampler defines an inline (constexpr) sampler in Metal.
type Options ¶
type Options struct {
// LangVersion is the target MSL version.
// Defaults to Version2_1 if zero.
LangVersion Version
// PerEntryPointMap maps entry point names to their resource bindings.
// If nil, bindings are auto-generated.
PerEntryPointMap map[string]EntryPointResources
// InlineSamplers defines constexpr samplers to be inlined into the code.
// Referenced by BindSamplerTarget.Slot when IsInline is true.
InlineSamplers []InlineSampler
// BoundsCheckPolicies controls bounds checking behavior.
BoundsCheckPolicies BoundsCheckPolicies
// ZeroInitializeWorkgroupMemory enables zero-initialization of
// workgroup (threadgroup) memory at the start of compute shaders.
ZeroInitializeWorkgroupMemory bool
// ForceLoopBounding adds loop iteration limits to prevent infinite loops.
ForceLoopBounding bool
// FakeMissingBindings generates placeholder bindings for resources
// that are referenced but not in the PerEntryPointMap.
FakeMissingBindings bool
// PipelineConstants specifies values for pipeline-overridable constants.
PipelineConstants map[string]float64
// AllowAndForcePointSize forces point size output for vertex shaders.
AllowAndForcePointSize bool
// VertexPullingTransform enables vertex pulling transformation.
VertexPullingTransform bool
// VertexBufferMappings describes the vertex buffer layout for vertex pulling.
VertexBufferMappings []VertexBufferMapping
}
Options configures MSL code generation.
func DefaultOptions ¶
func DefaultOptions() Options
DefaultOptions returns sensible default options for MSL generation.
type PipelineOptions ¶
type PipelineOptions struct {
// EntryPoint specifies which entry point to compile.
// If nil, all entry points are compiled.
EntryPoint *EntryPointSelector
// AllowAndForcePointSize forces point size output for vertex shaders.
AllowAndForcePointSize bool
}
PipelineOptions configures options specific to a single pipeline/entry point.
type SamplerAddress ¶ added in v0.15.0
type SamplerAddress int
SamplerAddress specifies the addressing mode for an inline sampler.
const ( SamplerAddressRepeat SamplerAddress = iota SamplerAddressMirroredRepeat SamplerAddressClampToEdge SamplerAddressClampToZero SamplerAddressClampToBorder )
type SamplerBorderColor ¶ added in v0.15.0
type SamplerBorderColor int
SamplerBorderColor specifies the border color for an inline sampler.
const ( SamplerBorderColorTransparentBlack SamplerBorderColor = iota SamplerBorderColorOpaqueBlack SamplerBorderColorOpaqueWhite )
type SamplerCompareFunc ¶ added in v0.15.0
type SamplerCompareFunc int
SamplerCompareFunc specifies the comparison function for an inline sampler.
const ( SamplerCompareFuncNever SamplerCompareFunc = iota SamplerCompareFuncLess SamplerCompareFuncLessEqual SamplerCompareFuncGreater SamplerCompareFuncGreaterEqual SamplerCompareFuncEqual SamplerCompareFuncNotEqual SamplerCompareFuncAlways )
type SamplerCoord ¶ added in v0.15.0
type SamplerCoord int
SamplerCoord specifies the coordinate system for an inline sampler.
const ( SamplerCoordNormalized SamplerCoord = iota SamplerCoordPixel )
type SamplerFilter ¶ added in v0.15.0
type SamplerFilter int
SamplerFilter specifies the filtering mode for an inline sampler.
const ( SamplerFilterNearest SamplerFilter = iota SamplerFilterLinear )
type TranslationInfo ¶
type TranslationInfo struct {
// EntryPointNames maps original entry point names to generated MSL names.
EntryPointNames map[string]string
// RequiresSizesBuffer indicates if a sizes buffer is needed for
// runtime-sized arrays.
RequiresSizesBuffer bool
}
TranslationInfo contains information about the compiled MSL output.
func Compile ¶
Compile generates MSL source code from an IR module. Returns the MSL source as a string and translation info, or an error.
func CompileWithPipeline ¶
func CompileWithPipeline(module *ir.Module, options Options, pipeline PipelineOptions) (string, TranslationInfo, error)
CompileWithPipeline generates MSL source code with pipeline-specific options.
type Version ¶
Version represents an MSL language version.
type VertexBufferMapping ¶ added in v0.15.0
type VertexBufferMapping struct {
ID uint32
Stride uint32
StepMode VertexBufferStepMode
Attributes []AttributeMapping
}
VertexBufferMapping describes a vertex buffer and its attributes.
type VertexBufferStepMode ¶ added in v0.15.0
type VertexBufferStepMode int
VertexBufferStepMode defines how to advance the data in vertex buffers.
const ( VertexStepModeConstant VertexBufferStepMode = 0 VertexStepModeByVertex VertexBufferStepMode = 1 VertexStepModeByInstance VertexBufferStepMode = 2 )
type VertexFormat ¶ added in v0.15.0
type VertexFormat int
VertexFormat describes the format of a vertex attribute.
const ( VertexFormatUint8 VertexFormat = 0 VertexFormatUint8x2 VertexFormat = 1 VertexFormatUint8x4 VertexFormat = 2 VertexFormatSint8 VertexFormat = 3 VertexFormatSint8x2 VertexFormat = 4 VertexFormatSint8x4 VertexFormat = 5 VertexFormatUnorm8 VertexFormat = 6 VertexFormatUnorm8x2 VertexFormat = 7 VertexFormatUnorm8x4 VertexFormat = 8 VertexFormatSnorm8 VertexFormat = 9 VertexFormatSnorm8x2 VertexFormat = 10 VertexFormatSnorm8x4 VertexFormat = 11 VertexFormatUint16 VertexFormat = 12 VertexFormatUint16x2 VertexFormat = 13 VertexFormatUint16x4 VertexFormat = 14 VertexFormatSint16 VertexFormat = 15 VertexFormatSint16x2 VertexFormat = 16 VertexFormatSint16x4 VertexFormat = 17 VertexFormatUnorm16 VertexFormat = 18 VertexFormatUnorm16x2 VertexFormat = 19 VertexFormatUnorm16x4 VertexFormat = 20 VertexFormatSnorm16 VertexFormat = 21 VertexFormatSnorm16x2 VertexFormat = 22 VertexFormatSnorm16x4 VertexFormat = 23 VertexFormatFloat16 VertexFormat = 24 VertexFormatFloat16x2 VertexFormat = 25 VertexFormatFloat16x4 VertexFormat = 26 VertexFormatFloat32 VertexFormat = 27 VertexFormatFloat32x2 VertexFormat = 28 VertexFormatFloat32x3 VertexFormat = 29 VertexFormatFloat32x4 VertexFormat = 30 VertexFormatUint32 VertexFormat = 31 VertexFormatUint32x2 VertexFormat = 32 VertexFormatUint32x3 VertexFormat = 33 VertexFormatUint32x4 VertexFormat = 34 VertexFormatSint32 VertexFormat = 35 VertexFormatSint32x2 VertexFormat = 36 VertexFormatSint32x3 VertexFormat = 37 VertexFormatSint32x4 VertexFormat = 38 VertexFormatUnorm10_10_10_2 VertexFormat = 43 VertexFormatUnorm8x4Bgra VertexFormat = 44 )