//go:build darwin package metal import ( "fmt" "os" "sync" "github.com/egoist/mygo/internal/gpu" ) // The pipelines of the classes of batches (gpu.Class) are specialized from the // shader at creation, which costs a compile of up to 120 ms. A frame never // waits for one: pipeFor returns the general pipeline, which draws the same // pixels, until a worker goroutine has made the specialized one, and a cache // shared by every renderer of a device keeps them, so that a second window // finds them made. GPUs older than Metal's Mac2 family (canSpecialize) have // none: every batch draws with the general pipeline, and nothing compiles // behind the frames. // pipeKey names a pipeline: the pixel format of the target, the classes of // its batches (gpu.Batch.Classes: one, or those of joined batches) and // whether its instances have continuous corners, and how it blends. type pipeKey struct { format uint classes uint32 cont bool mode blendMode } // deviceKey is a pipeline of a device in the cache of all renderers. type deviceKey struct { device id pipeKey } // pipeCache holds the specialized pipelines of each device, never released: // they are small, and a device lives as long as the process does. var pipeCache = struct { sync.Mutex m map[deviceKey]id }{m: map[deviceKey]id{}} // syncPipelines makes pipeFor build a missing pipeline at once, for tests that // compare the specialized pipelines with the general ones and need them made. var syncPipelines bool // pipeState is a renderer's pipelines of the classes and the worker making them. type pipeState struct { mu sync.Mutex ready map[pipeKey]id queued map[pipeKey]bool work chan pipeKey wg sync.WaitGroup closed bool sync bool // build in pipeFor, see syncPipelines warmed [2]bool // by format owned []id // pipelines of renderers that do not use the cache nbuilt int // pipelines the worker made, for tests nfailed int beforeBuild func() // test hook, set before the first frame starts the worker // fallbacks counts the batches of the frame being encoded that drew with // the general pipeline because the specialized one was not made yet // (touched by the encoding goroutine only). fallbacks int } // warmSet returns the pipelines that frames most likely draw with in a target of // format: every class with and without continuous corners drawn over what is // there, and the plain and other fills drawn as opaque cores (which float16 // targets do not use, and which shadows do not need), and the classes that // small batches join most (gpu.Batch.Joined): shadows with the fills casting // them, and fills with their labels. A device that does not specialize warms // none. func (r *Renderer) warmSet(format uint) (keys []pipeKey) { for _, cl := range []uint32{1 << gpu.ClassPlain, 1 << gpu.ClassGlyph, 1 << gpu.ClassImage, 1 << gpu.ClassFill, 1 << gpu.ClassShadow, 1 << gpu.ClassSubpixel, 1<= 1<