Files
openchamber-android-native/third_party/mygo/internal/shaderir/msl/uniform.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

98 lines
2.6 KiB
Go

package msl
import (
"fmt"
"github.com/egoist/mygo/internal/shaderir"
)
// UniformLayout returns where each of p's uniform variables lies in the
// struct Uniforms of Fragment, in bytes, and the struct's size, as Metal
// lays it out: a vector of 3 takes the room of 4, a matrix is its
// columns, and an array's elements are as far apart as their alignment
// makes them.
func UniformLayout(p *shaderir.Program) (offsets []int, size int) {
align := 1
for i := range p.Uniforms {
s, a := sizeAlign(&p.Uniforms[i])
size = roundUp(size, a)
offsets = append(offsets, size)
size += s
align = max(align, a)
}
return offsets, roundUp(size, align)
}
// UniformLanes returns, for each lane of p's uniforms as package interp
// keeps them (in order, columns of matrices, elements of arrays), its
// offset in bytes in the struct Uniforms of Fragment, and the struct's
// size.
func UniformLanes(p *shaderir.Program) (lanes []int, size int) {
offsets, size := UniformLayout(p)
for i := range p.Uniforms {
for _, at := range typeLanes(&p.Uniforms[i]) {
lanes = append(lanes, offsets[i]+at)
}
}
return lanes, size
}
// typeLanes returns the offset in bytes of each lane of a value of type t
// in its Metal layout.
func typeLanes(t *shaderir.Type) []int {
var out []int
var walk func(t *shaderir.Type, base int)
walk = func(t *shaderir.Type, base int) {
switch {
case t.Main == shaderir.Array:
s, a := sizeAlign(&t.Sub[0])
stride := roundUp(s, a)
for i := range t.Length {
walk(&t.Sub[0], base+i*stride)
}
case t.IsMatrix():
n := t.MatrixSize()
col := shaderir.Type{Main: shaderir.VectorOf(shaderir.Float, n)}
_, a := sizeAlign(&col)
for c := range n {
walk(&col, base+c*roundUp(4*n, a))
}
case t.Main == shaderir.Bool:
out = append(out, base)
default:
n := max(t.VectorElementCount(), 1)
for i := range n {
out = append(out, base+4*i)
}
}
}
walk(t, 0)
return out
}
// sizeAlign returns the size and the alignment of t in Metal, in bytes.
func sizeAlign(t *shaderir.Type) (size, align int) {
switch t.Main {
case shaderir.Bool:
return 1, 1
case shaderir.Int, shaderir.Uint, shaderir.Float:
return 4, 4
case shaderir.Vec2, shaderir.IVec2, shaderir.UVec2:
return 8, 8
case shaderir.Vec3, shaderir.IVec3, shaderir.UVec3, shaderir.Vec4, shaderir.IVec4, shaderir.UVec4:
return 16, 16
case shaderir.Mat2:
return 16, 8
case shaderir.Mat3:
return 48, 16
case shaderir.Mat4:
return 64, 16
case shaderir.Array:
s, a := sizeAlign(&t.Sub[0])
return t.Length * roundUp(s, a), a
}
panic(fmt.Sprintf("msl: no layout for %s", t))
}
func roundUp(x, a int) int { return (x + a - 1) / a * a }