A Swift Package Manager build tool plugin that compiles .hlsl sources into platform-appropriate binaries:
- Apple platforms: uses
dxc -metalto emit.metallib. - Linux/Windows: uses
dxc -spirv -fspv-target-env=vulkan1.2to emit.spv.
The plugin is intended to be attached to your shader or resource target so generated outputs are available alongside your package/app resources.
dxcavailable at/opt/dxc/bin/dxc, inPATH, or viaDXC_PATH.- On Apple:
dxcbuilt with Metal support andmetal-shaderconverterinstalled.
// Package.swift (consumer)
.package(url: "https://github.com/openorbit/spm-hlsl-plugin.git", branch: "main"),
.target(
name: "GameShaders",
resources: [.process("Shaders")], // where your .hlsl files live
plugins: [.plugin(name: "HLSLBuildPlugin", package: "spm-hlsl-plugin")]
)Place your .hlsl files anywhere inside the target (e.g. Shaders/triangle.hlsl). The plugin emits generated binaries into its work directory and SwiftPM treats them as generated resources, so you can load them from Bundle.module using the output names.
Defaults:
- Entry point:
mainorHLSL_ENTRY_POINT. - Target profile:
HLSL_TARGET_PROFILEor a heuristic based on the filename (*_vs*->vs_6_0,*_ps*->ps_6_0,*_cs*->cs_6_0). - Extra args: split from
HLSL_EXTRA_ARGS. - Output extension:
.metallibon Apple,.spvelsewhere.
Override per file by adding a sidecar JSON file next to the shader (e.g. Shaders/triangle.hlsl.json):
{
"entryPoint": "vsMain",
"targetProfile": "vs_6_6",
"outputName": "triangle.metallib",
"outputExtension": "metallib",
"extraArgs": ["-O3", "-DDEBUG=0"]
}- If
dxccannot be found the build fails with a clear error; setDXC_PATHto point at your compiler if needed. - On Apple,
-metalis passed sodxcwill invokemetal-shaderconverterautomatically when available. - On non-Apple platforms, SPIR-V output targets Vulkan 1.2 by default. Adjust with
HLSL_EXTRA_ARGSor sidecarextraArgsif you need a different profile or options.