Files
openchamber-android-native/third_party/mygo/internal/gpu/metal/precompile.go
T
OpenChamber 6c0ee61750 feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
2026-11-18 13:22:59 +08:00

103 lines
2.9 KiB
Go

//go:build darwin
package metal
import (
"fmt"
"runtime"
"sync"
"unsafe"
)
// PrecompileShader starts making what a shader of package ui/shader draws
// with, whose library's source is src (ShaderSource), on a goroutine of
// its own, with the system's default GPU: its library, which Metal
// compiles from the source in about 0.1 s, and its pipeline for windows
// of 8-bit color, about as long again. Renderers on that GPU then draw
// with them, waiting for what is left, rather than make them on the
// shader's first draw. Metal keeps both on disk for the next launches.
// Package ui/shader calls it as it compiles a shader.
func PrecompileShader(src string) {
shaders.once.Do(func() {
shaders.byKey = map[string]*shaderEntry{}
shaders.sem = make(chan struct{}, max(1, runtime.NumCPU()-1))
if load() == nil {
shaders.device = createDevice() // owned, for the program's life
}
})
if shaders.device == 0 {
return
}
shaders.mu.Lock()
if shaders.byKey[src] != nil {
shaders.mu.Unlock()
return
}
e := &shaderEntry{done: make(chan struct{})}
shaders.byKey[src] = e
shaders.mu.Unlock()
go func() {
shaders.sem <- struct{}{}
defer func() { <-shaders.sem }()
defer close(e.done)
pool(func() { e.make(src) })
}()
}
// shaders holds what PrecompileShader made, by source.
var shaders struct {
once sync.Once
mu sync.Mutex
byKey map[string]*shaderEntry
sem chan struct{} // the goroutines making them at once
device id
}
// shaderEntry is what PrecompileShader made of a source: its library and
// pipeline (owned), or why not, once done is closed.
type shaderEntry struct {
done chan struct{}
lib, pipe id
err error
}
// precompiledFormat is the format of the pipeline PrecompileShader makes:
// that of windows that draw no wide gamut.
const precompiledFormat = pixelFormatBGRA8Unorm
func (e *shaderEntry) make(src string) {
var errObj id
e.lib = send(shaders.device, "newLibraryWithSource:options:error:", nsString(src), 0, uintptr(unsafe.Pointer(&errObj)))
if e.lib == 0 {
e.err = fmt.Errorf("metal: cannot compile the shader: %s", describe(errObj))
return
}
r := &Renderer{device: shaders.device}
e.pipe, _ = r.newPipeline(e.lib, "shader_ps", precompiledFormat, blendOver)
}
// sharedShader returns what PrecompileShader made of src for a renderer
// on device drawing format, waiting for it, retained: its library, and its
// pipeline when made for format (0 otherwise). ok is false when it made
// nothing for src on device.
func sharedShader(device id, src string, format uint) (lib, pipe id, ok bool) {
if device == 0 || device != shaders.device {
return 0, 0, false
}
shaders.mu.Lock()
e := shaders.byKey[src]
shaders.mu.Unlock()
if e == nil {
return 0, 0, false
}
<-e.done
if e.lib == 0 {
return 0, 0, false
}
lib = send(e.lib, "retain")
if format == precompiledFormat && e.pipe != 0 {
pipe = send(e.pipe, "retain")
}
return lib, pipe, true
}