//go:build !js // Command gen compiles the glass's and the blur's shaders ahead of time, // so that renderers start without compiling them: the Metal libraries on // macOS, with Xcode's metal tools, into shaders_darwin.go, and the // Direct3D bytecode on Windows, with the system's d3dcompiler_47.dll, into // shaders_windows.go. Run it on each: go generate ./plugins/glass package main import ( "bytes" "fmt" "go/format" "log" "os" "runtime" "strings" "github.com/egoist/mygo/internal/gpu" ) // shader is a shader to compile, and the variables holding its code and // the SHA-256 of what that was compiled from. type shader struct{ file, codeVar, sumVar string } func main() { file := "shaders_darwin.go" shaders := []shader{{"glass.metal", "metalLibrary", "metalSum"}, {"blur.metal", "blurMetalLibrary", "blurMetalSum"}} if runtime.GOOS == "windows" { file = "shaders_windows.go" shaders = []shader{{"glass.hlsl", "hlslBytecode", "hlslSum"}, {"blur.hlsl", "blurBytecode", "blurHLSLSum"}} } var names []string for _, sh := range shaders { names = append(names, sh.file) } var b bytes.Buffer fmt.Fprintf(&b, "// Code generated by internal/gen from %s; DO NOT EDIT.\n\n//go:build %s\n\npackage glass\n\n", strings.Join(names, " and "), runtime.GOOS) for _, sh := range shaders { compiled, code, err := compile(sh.file) if err != nil { log.Fatal(err) } fmt.Fprintf(&b, "// %s is the SHA-256 of what %s was compiled from: %s\n// within the renderer's head and tail.\nconst %s = %q\n\n", sh.sumVar, sh.codeVar, sh.file, sh.sumVar, gpu.SourceSum(compiled)) b.WriteString(gpu.GoBytes(sh.codeVar, 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) } }