Add SPIR-V to Metal tooling

This commit is contained in:
Meta Vens 2026-06-11 06:57:22 +01:00
commit 788a75dba5
4 changed files with 88 additions and 0 deletions

View file

@ -78,15 +78,24 @@ CultMath keeps portable DXC outside git under `.tools/`:
```powershell
.\tools\get-dxc.ps1
.\tools\get-spirv-cross.ps1
.\tools\compile-hlsl-spirv.ps1 `
-ShaderPath E:\Projects\Odin\crates\muninn-move-tracker\shaders\MoveSphereCandidate.comp.hlsl `
-OutputPath E:\Projects\Odin\crates\muninn-move-tracker\artifacts\shader\MoveSphereCandidate.comp.spv `
-IncludePath E:\Projects\CultMath\shaders
.\tools\compile-spirv-metal.ps1 `
-SpirvPath E:\Projects\Odin\crates\muninn-move-tracker\artifacts\shader\MoveSphereCandidate.comp.spv `
-OutputPath E:\Projects\Odin\crates\muninn-move-tracker\artifacts\shader\MoveSphereCandidate.comp.metal
```
DXC emits Vulkan SPIR-V with `-spirv`. `-TargetEnv vulkan1.2` is exposed by the
script, but the current Windows DXC release compiled this shader successfully
with DXC's default Vulkan target while rejecting the dotted target-env value.
The SPIR-V script applies default Vulkan register shifts for cbuffers, textures,
UAVs, and samplers so cross-compiled Metal buffers do not alias by resource
class. SPIRV-Cross emits Metal Shading Language from SPIR-V on Windows; building
an iPad-ready `.metallib` still belongs to Apple's Metal compiler on an Apple
toolchain.
## Doctrine

View file

@ -9,6 +9,10 @@ param(
[string]$Profile = "cs_6_0",
[string[]]$IncludePath = @(),
[string]$TargetEnv = "",
[int]$VulkanCBufferShift = 0,
[int]$VulkanTextureShift = 10,
[int]$VulkanUavShift = 20,
[int]$VulkanSamplerShift = 30,
[string]$DxcPath = ""
)
@ -44,6 +48,13 @@ if ($TargetEnv) {
$arguments += "-fspv-target-env=$TargetEnv"
}
$arguments += @(
"-fvk-b-shift", $VulkanCBufferShift.ToString(), "0",
"-fvk-t-shift", $VulkanTextureShift.ToString(), "0",
"-fvk-u-shift", $VulkanUavShift.ToString(), "0",
"-fvk-s-shift", $VulkanSamplerShift.ToString(), "0"
)
foreach ($include in $resolvedIncludes) {
$arguments += @("-I", $include)
}

View file

@ -0,0 +1,34 @@
param(
[Parameter(Mandatory = $true)]
[string]$SpirvPath,
[Parameter(Mandatory = $true)]
[string]$OutputPath,
[int]$MslVersion = 23000,
[string]$SpirvCrossPath = ""
)
$ErrorActionPreference = "Stop"
$repoRoot = Resolve-Path (Join-Path $PSScriptRoot "..")
if (-not $SpirvCrossPath) {
$SpirvCrossPath = Join-Path $repoRoot ".tools\spirv-cross\build\spirv-cross.exe"
}
if (-not (Test-Path $SpirvCrossPath)) {
throw "SPIRV-Cross not found at '$SpirvCrossPath'. Run tools\get-spirv-cross.ps1 first or pass -SpirvCrossPath."
}
$resolvedSpirv = Resolve-Path $SpirvPath
$outputDirectory = Split-Path -Parent $OutputPath
if ($outputDirectory) {
New-Item -ItemType Directory -Force -Path $outputDirectory | Out-Null
}
& $SpirvCrossPath $resolvedSpirv --msl --msl-version $MslVersion --output $OutputPath
if ($LASTEXITCODE -ne 0) {
throw "spirv-cross failed with exit code $LASTEXITCODE."
}
Get-Item -LiteralPath $OutputPath

34
tools/get-spirv-cross.ps1 Normal file
View file

@ -0,0 +1,34 @@
param(
[string]$InstallRoot = ".tools\spirv-cross"
)
$ErrorActionPreference = "Stop"
$repoRoot = Resolve-Path (Join-Path $PSScriptRoot "..")
$installPath = Join-Path $repoRoot $InstallRoot
$sourcePath = Join-Path $installPath "src"
$buildPath = Join-Path $installPath "build"
New-Item -ItemType Directory -Force -Path $installPath | Out-Null
if (-not (Test-Path $sourcePath)) {
git clone --depth 1 https://github.com/KhronosGroup/SPIRV-Cross.git $sourcePath
}
cmake `
-S $sourcePath `
-B $buildPath `
-G Ninja `
-DSPIRV_CROSS_CLI=ON `
-DSPIRV_CROSS_ENABLE_TESTS=OFF `
-DSPIRV_CROSS_ENABLE_C_API=OFF
cmake --build $buildPath --target spirv-cross --config Release
$spirvCrossPath = Join-Path $buildPath "spirv-cross.exe"
if (-not (Test-Path $spirvCrossPath)) {
throw "SPIRV-Cross executable was not found after build."
}
& $spirvCrossPath --version
Write-Host "SPIRV-Cross installed at $spirvCrossPath"