Skip to content

Instantly share code, notes, and snippets.

@pixelmatrix
Created October 9, 2025 18:05
Show Gist options
  • Select an option

  • Save pixelmatrix/86f8816c68fa1435e1a0124af894e651 to your computer and use it in GitHub Desktop.

Select an option

Save pixelmatrix/86f8816c68fa1435e1a0124af894e651 to your computer and use it in GitHub Desktop.

Revisions

  1. pixelmatrix created this gist Oct 9, 2025.
    20 changes: 20 additions & 0 deletions ShaderLibrary+Convenience.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    import SwiftUI

    extension ShaderLibrary {
    // Important: Cache this instance. Initializing it is expensive.
    static let library: ShaderLibrary = {
    // Make sure to use the right binary based on the target
    #if targetEnvironment(simulator)
    let filename = "Shaders-iphonesimulator"
    #else
    let filename = "Shaders-iphoneos"
    #endif

    // Bundle.module is for Swift packages. Use whatever bundle contains your shader files.
    guard let url = Bundle.module.url(forResource: filename, withExtension: "metallib") else {
    fatalError("Missing shader library")
    }

    return ShaderLibrary(url: url)
    }()
    }
    15 changes: 15 additions & 0 deletions build.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    # Compile for device
    xcrun -sdk iphoneos metal -o One.ir -c One.metal
    xcrun -sdk iphoneos metal -o Two.ir -c Two.metal
    xcrun -sdk iphoneos metallib -o Shaders-iphoneos.metallib One.ir Two.ir
    rm One.ir
    rm Two.ir

    # Compile for simulator
    xcrun -sdk iphonesimulator metal -o One.ir -c One.metal
    xcrun -sdk iphonesimulator metal -o Two.ir -c Two.metal
    xcrun -sdk iphonesimulator metallib -o Shaders-iphonesimulator.metallib One.ir Two.ir
    rm One.ir
    rm Two.ir

    # Include the .metallib files in your app / package's bundle