//go:build ignore // Gen compiles shader.metal into a Metal library with Xcode's metal tools // and writes shaderlib.go, so that renderers start without compiling the // shader, which takes over 100 ms until Metal has cached it. It runs on // macOS with Xcode: go generate ./internal/gpu/metal package main import ( "bytes" "fmt" "go/format" "log" "os" "github.com/egoist/mygo/internal/gpu" "github.com/egoist/mygo/internal/gpu/metal" ) func main() { src, err := os.ReadFile("shader.metal") if err != nil { log.Fatal(err) } sum := gpu.SourceSum(string(src)) compile := func(sdk, min string) []byte { code, err := metal.CompileLibraryFor(string(src), sdk, min) if err != nil { log.Fatal(err) } return code } // macOS 12 and iOS 17 are the oldest MyGo runs on. write("shaderlib.go", "darwin && !ios", sum, map[string][]byte{"shaderLibrary": compile("macosx", "-mmacosx-version-min=12.0")}) // A library loads only on its platform: iOS devices' and the // simulator's differ, and the app takes its own (shaderlib_ios.go). write("shaderlib_ios_gen.go", "ios", sum, map[string][]byte{ "shaderLibraryDevice": compile("iphoneos", "-mios-version-min=17.0"), "shaderLibrarySimulator": compile("iphonesimulator", "-mios-simulator-version-min=17.0"), }) } func write(file, constraint, sum string, libs map[string][]byte) { var b bytes.Buffer fmt.Fprintf(&b, "// Code generated by gen.go from shader.metal; DO NOT EDIT.\n\n//go:build %s\n\npackage metal\n\n", constraint) fmt.Fprintf(&b, "// shaderLibrarySum is the SHA-256 of the shader.metal the libraries were\n// compiled from.\nconst shaderLibrarySum = %q\n\n", sum) for _, name := range []string{"shaderLibrary", "shaderLibraryDevice", "shaderLibrarySimulator"} { if code, ok := libs[name]; ok { b.WriteString(gpu.GoBytes(name, code)) b.WriteString("\n") } } out, err := format.Source(b.Bytes()) if err != nil { log.Fatal(err) } if err := os.WriteFile(file, out, 0o644); err != nil { log.Fatal(err) } }