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.
This commit is contained in:
OpenChamber committed 2026-11-18 13:22:59 +08:00
1 parent 2fb4a8ea79
commit 6c0ee61750
2538 files changed
+675779 -4792

No files matched your search

+102
View File
@@ -0,0 +1,102 @@
package gputest
import "github.com/egoist/mygo/internal/scene"
// BoxesScene returns a w×h frame of n rounded boxes of 24 pixels, each
// casting a shadow, in rows of 30, as an animation of boxes moving draws
// them: under translations (Element.Translate) when translated, else at
// the same places without transforms. The places fall between pixels.
func BoxesScene(w, h, n int, translated bool) *scene.Scene {
scene.FoldTranslations() // as package ui does
s := &scene.Scene{Width: w, Height: h, Clear: scene.Color{R: 246, G: 247, B: 249, A: 255}}
for i := range n {
x, y := 8+float32(i%30)*40+float32(i%7)*0.25, 8+float32(i/30)*38+float32(i%5)*0.2
m := scene.Translation(x, y)
box := scene.Rect{W: 24, H: 24}
if !translated {
m, box.X, box.Y = scene.Affine{}, x, y
}
cast := box
cast.Y += 2
c := scene.Color{R: uint8(40 + i*7%180), G: uint8(80 + i*13%150), B: uint8(120 + i*3%120), A: 255}
s.Ops = append(s.Ops,
scene.Op{Kind: scene.OpShadow, Rect: cast, Radii: scene.Uniform(6), Color: scene.Color{A: 70}, Blur: 8, Transform: m},
scene.Op{Kind: scene.OpFill, Rect: box, Radii: scene.Uniform(6), Color: c, Transform: m})
}
return s
}
// RowsScene returns a w×h frame of a list of n rows, as a window of a list
// draws them: each a rounded background, a label of glyphs, a rounded
// badge and a second label, so that plain fills and glyphs alternate.
func RowsScene(w, h, n int) *scene.Scene {
s := &scene.Scene{Width: w, Height: h, Clear: scene.Color{R: 250, G: 250, B: 250, A: 255}}
atlas, text, glyphs := glyphContent()
s.MaskAtlas, s.Text = atlas, text
run := func(x, y float32, k int, c scene.Color) {
start := len(s.Glyphs)
for _, g := range glyphs[:k] {
g.X, g.Y, g.Color = g.X-40+x, y, c
s.Glyphs = append(s.Glyphs, g)
}
s.Ops = append(s.Ops, scene.Op{Kind: scene.OpGlyphs, Start: int32(start), End: int32(len(s.Glyphs))})
}
rowH := float32(h-16) / float32(n)
for i := range n {
y := 8 + float32(i)*rowH
bg := scene.Color{R: 236, G: 238, B: 242, A: 255}
if i%2 == 1 {
bg = scene.Color{R: 244, G: 245, B: 247, A: 255}
}
s.Ops = append(s.Ops, scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 8, Y: y, W: float32(w) - 16, H: rowH - 2}, Radii: scene.Uniform(4), Color: bg})
run(20, y+1.5, 8+i%5, scene.Color{R: 30, G: 30, B: 34, A: 255})
s.Ops = append(s.Ops, scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 260, Y: y + 2, W: 40, H: rowH - 6}, Radii: scene.Uniform(6),
Color: scene.Color{R: uint8(60 + i*29%160), G: 120, B: 220, A: 255}})
run(320, y+1.5, 4+i%7, scene.Color{R: 110, G: 110, B: 120, A: 255})
}
return s
}
// PanelsScene returns a w×h frame of n cards in rows of 8, as a page of an
// app draws them: each a shadowed white card, its content in a rounded
// clip, an icon (one of four images), a label of glyphs and a colored
// badge. Clips and images change from card to card, so the frame takes
// several batches a card, as pages of real windows do.
func PanelsScene(w, h, n int) *scene.Scene {
s := &scene.Scene{Width: w, Height: h, Clear: scene.Color{R: 246, G: 247, B: 249, A: 255}}
atlas, text, glyphs := glyphContent()
s.MaskAtlas, s.Text = atlas, text
var icons [4]*scene.Image
for k := range icons {
pix := make([]byte, 16*16*4)
for i := 0; i < len(pix); i += 4 {
pix[i], pix[i+1], pix[i+2], pix[i+3] = byte(60*k), byte(i/4%16*16), 200, 255
}
icons[k] = scene.NewImageRGBA(16, 16, pix)
}
cw, ch := (float32(w)-16)/8, float32(64)
for i := range n {
x, y := 8+float32(i%8)*cw, 8+float32(i/8)*(ch+8)
card := scene.Rect{X: x + 4, Y: y, W: cw - 8, H: ch}
cast := card
cast.Y += 2
s.Ops = append(s.Ops,
scene.Op{Kind: scene.OpShadow, Rect: cast, Radii: scene.Uniform(8), Color: scene.Color{A: 40}, Blur: 6},
scene.Op{Kind: scene.OpFill, Rect: card, Radii: scene.Uniform(8), Color: scene.Color{R: 255, G: 255, B: 255, A: 255},
Border: scene.Uniform(1), BorderColor: scene.Color{R: 220, G: 222, B: 228, A: 255}},
scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: card.X + 1, Y: card.Y + 1, W: card.W - 2, H: card.H - 2}, Radii: scene.Uniform(7)},
scene.Op{Kind: scene.OpImage, Rect: scene.Rect{X: card.X + 10, Y: card.Y + 10, W: 20, H: 20}, Radii: scene.Uniform(4),
Image: icons[i%len(icons)], Src: scene.Rect{W: 16, H: 16}})
start := len(s.Glyphs)
for _, g := range glyphs[:6+i%6] {
g.X, g.Y, g.Color = g.X-40+card.X+40, card.Y+12, scene.Color{R: 30, G: 30, B: 34, A: 255}
s.Glyphs = append(s.Glyphs, g)
}
s.Ops = append(s.Ops,
scene.Op{Kind: scene.OpGlyphs, Start: int32(start), End: int32(len(s.Glyphs))},
scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: card.X + 10, Y: card.Y + 40, W: 36, H: 14}, Radii: scene.Uniform(7),
Color: scene.Color{R: uint8(60 + i*29%160), G: 120, B: 220, A: 255}},
scene.Op{Kind: scene.OpPopClip})
}
return s
}
+47
View File
@@ -0,0 +1,47 @@
package gputest
import "github.com/egoist/mygo/internal/scene"
// CanvasScene returns a 120×80 scene showing canvases (scene.NewCanvas): a
// 16×12 one drawn with fills, a rounded one, an image and a canvas of its
// own, shown 4 times as large pixelated, then smooth and flipped; and a
// canvas shown only within another, with nothing new to draw the second
// time the renderer draws the scene; and a canvas drawing a conic gradient,
// shown blurred, as a view blurred is (ui.Element.Blur).
func CanvasScene() *scene.Scene {
s := &scene.Scene{Width: 120, Height: 80, Clear: scene.Color{R: 246, G: 247, B: 249, A: 255}}
pix := make([]byte, 4*4*4)
for i := range 16 {
pix[4*i], pix[4*i+1], pix[4*i+2], pix[4*i+3] = byte(i*16), byte(255-i*16), 120, 255
}
img := scene.NewImageRGBA(4, 4, pix)
inner := scene.NewCanvas(4, 4)
inner.Scene.Ops = []scene.Op{{Kind: scene.OpFill, Rect: scene.Rect{W: 4, H: 4}, Color: scene.Color{R: 250, G: 204, B: 21, A: 255}},
{Kind: scene.OpFill, Rect: scene.Rect{X: 1, Y: 1, W: 2, H: 2}, Color: scene.Color{R: 20, G: 24, B: 32, A: 255}}}
c := scene.NewCanvas(16, 12)
c.Scene.Ops = []scene.Op{
{Kind: scene.OpFill, Rect: scene.Rect{W: 16, H: 12}, Color: scene.Color{R: 37, G: 99, B: 235, A: 255}},
{Kind: scene.OpFill, Rect: scene.Rect{X: 1, Y: 1, W: 6, H: 6}, Radii: [4]float32{3, 3, 3, 3}, Color: scene.Color{R: 220, G: 40, B: 40, A: 200}},
{Kind: scene.OpImage, Rect: scene.Rect{X: 9, Y: 1, W: 4, H: 4}, Image: img, Src: scene.Rect{W: 4, H: 4}},
{Kind: scene.OpImage, Rect: scene.Rect{X: 2, Y: 7, W: 4, H: 4}, Image: inner, Src: scene.Rect{W: 4, H: 4}},
{Kind: scene.OpFill, Rect: scene.Rect{X: 8, Y: 8, W: 8, H: 4}, Color: scene.Color{R: 255, G: 255, B: 255, A: 128}},
}
soft := scene.NewCanvas(40, 34)
soft.Scene.Stops = []scene.Stop{{Color: scene.Color{R: 220, G: 40, B: 40, A: 255}, At: 0}, {Color: scene.Color{R: 37, G: 99, B: 235, A: 255}, At: 0.6},
{Color: scene.Color{R: 220, G: 40, B: 40, A: 255}, At: 1}}
soft.Scene.Ops = []scene.Op{
{Kind: scene.OpFill, Rect: scene.Rect{X: 6, Y: 6, W: 28, H: 22}, Radii: [4]float32{6, 6, 6, 6}, Paint: scene.PaintConic,
Gradient: [4]float32{20, 17, 0.3, 0}, Stops: [2]int32{0, 3}},
{Kind: scene.OpFill, Rect: scene.Rect{X: 14, Y: 2, W: 4, H: 30}, Color: scene.Color{R: 20, G: 24, B: 32, A: 160}},
}
s.Canvases = []*scene.Image{c, soft}
s.Ops = []scene.Op{
{Kind: scene.OpImage, Rect: scene.Rect{X: 4, Y: 4, W: 64, H: 48}, Image: c, Src: scene.Rect{W: 16, H: 12}, Pixelated: true},
{Kind: scene.OpImage, Rect: scene.Rect{X: 72, Y: 4, W: 44, H: 33}, Image: c, Src: scene.Rect{X: 16, W: -16, H: 12}},
{Kind: scene.OpImage, Rect: scene.Rect{X: 74, Y: 42, W: 40, H: 34}, Image: soft, Src: scene.Rect{W: 40, H: 34}, Blur: 2.5},
{Kind: scene.OpImage, Rect: scene.Rect{X: 4, Y: 56, W: 40, H: 20}, Image: soft, Src: scene.Rect{X: 0, Y: 34, W: 40, H: -34}, Blur: 5},
}
return s
}
+102
View File
@@ -0,0 +1,102 @@
package gputest
import (
"fmt"
"image"
"maps"
"slices"
"strings"
"testing"
"github.com/egoist/mygo/internal/raster"
"github.com/egoist/mygo/internal/scene"
)
// BorderedCardScene returns the 756×640 frame of #531, a window of 378×320
// DIPs at scale 2: an opaque backdrop, and on it a card of an opaque white
// face with a border of 1 DIP (2 pixels) and corners of 12 DIPs (24
// pixels), continuous (as macOS draws them) or not, on whole pixels, large
// enough for the builder to draw it as an opaque core with pieces around it;
// and a button with the same border above it, too small for that. Their
// straight edges cover whole pixels, by 1 or 0, so a renderer draws them
// exactly as the CPU renderer does (see CompareStraightEdges).
func BorderedCardScene(continuous bool) *scene.Scene {
backdrop := scene.Color{R: 0xf4, G: 0xf4, B: 0xf6, A: 255}
face := scene.Color{R: 255, G: 255, B: 255, A: 255}
line := scene.Color{R: 0xe3, G: 0xe3, B: 0xe7, A: 255}
s := &scene.Scene{Width: 756, Height: 640}
s.Ops = append(s.Ops,
scene.Op{Kind: scene.OpFill, Rect: scene.Rect{W: 756, H: 640}, Color: backdrop},
scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 26, Y: 90, W: 730, H: 484}, Radii: [4]float32{24, 24, 24, 24}, Continuous: continuous,
Color: face, Border: scene.Uniform(2), BorderColor: line},
scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 100, Y: 16, W: 200, H: 56}, Radii: [4]float32{16, 16, 16, 16}, Continuous: continuous,
Color: face, Border: scene.Uniform(2), BorderColor: line})
return s
}
// CompareStraightEdges compares pixels a renderer drew, as Compare does,
// and requires more of those away from the corners of the scene's rounded
// fills, along their straight edges and inside them, where coverage is
// exactly 1 or 0 for shapes on whole pixels: each channel within 2 levels
// of the CPU renderer's, at every pixel. It reports the rows (or columns)
// that differ, so that a stray line along an edge shows where it is.
func CompareStraightEdges(t testing.TB, name string, got []byte, stride int, s *scene.Scene) {
t.Helper()
Compare(t, name, got, stride, s)
want := raster.NewImage(s.Width, s.Height)
raster.Render(want, s)
// The corners' boxes, as far as their curves reach (continuous ones 1.53
// times their radius) and the antialiasing a pixel beyond.
var corners []image.Rectangle
for _, op := range s.Ops {
if op.Kind != scene.OpFill || op.Radii == [4]float32{} {
continue
}
r := max(op.Radii[0], op.Radii[1], op.Radii[2], op.Radii[3])
e := int(r*1.6) + 3
x0, y0 := int(op.Rect.X)-2, int(op.Rect.Y)-2
x1, y1 := int(op.Rect.X+op.Rect.W)+2, int(op.Rect.Y+op.Rect.H)+2
corners = append(corners, image.Rect(x0, y0, x0+e, y0+e), image.Rect(x1-e, y0, x1, y0+e),
image.Rect(x1-e, y1-e, x1, y1), image.Rect(x0, y1-e, x0+e, y1))
}
off := 0
rows := map[int]int{}
var first []string
for y := range s.Height {
for x := range s.Width {
p := image.Pt(x, y)
loose := false
for _, c := range corners {
if p.In(c) {
loose = true
break
}
}
if loose {
continue
}
g := got[y*stride+4*x:][:4]
w := want.Pix[y*want.Stride+4*x:][:4]
d := 0
for c := range 4 {
d = max(d, absInt(int(g[c])-int(w[c])))
}
if d > 2 {
off++
rows[y]++
if len(first) < 8 {
first = append(first, fmt.Sprintf("%v: BGRA %x, want %x", p, g, w))
}
}
}
}
if off > 0 {
ys := slices.Sorted(maps.Keys(rows))
var where []string
for _, y := range ys[:min(len(ys), 16)] {
where = append(where, fmt.Sprintf("row %d: %d", y, rows[y]))
}
t.Errorf("%s: %d pixels away from the corners differ from the CPU renderer's by more than 2, in %d rows (%s); first %s",
name, off, len(rows), strings.Join(where, ", "), strings.Join(first, "; "))
}
}
+242
View File
@@ -0,0 +1,242 @@
package gputest
import (
"math/rand/v2"
"github.com/egoist/mygo/internal/scene"
)
// Changing makes random scenes and changes them a little at a time, as the
// frames of an app do, for the tests of renderers drawing only what
// changed: fills (with continuous corners too), shadows, glyphs, images,
// gradients, effects reading their backdrops, clips (one nested in
// another, transformed, now and then), transforms, and colors outside the
// sRGB gamut.
type Changing struct {
rnd *rand.Rand
atlas *scene.Atlas
img *scene.Image
// masks are rectangles of the atlas holding something.
masks [][4]uint16
// Clear is the color scenes start with.
Clear scene.Color
}
// NewChanging returns a maker of scenes from seed.
func NewChanging(seed uint64) *Changing {
m := &Changing{rnd: rand.New(rand.NewPCG(seed, 11)), atlas: scene.NewAtlas(1, 128, 128),
Clear: scene.Color{R: 250, G: 250, B: 250, A: 255}}
for range 6 {
w, h := 4+m.rnd.IntN(12), 4+m.rnd.IntN(12)
x, y, ok := m.atlas.Alloc(w, h)
if !ok {
break
}
m.atlas.Put(x, y, w, h, m.pixels(w*h), w)
m.masks = append(m.masks, [4]uint16{uint16(x), uint16(y), uint16(w), uint16(h)})
}
m.img = scene.NewImageRGBA(8, 8, m.pixels(8*8*4))
return m
}
func (m *Changing) pixels(n int) []byte {
p := make([]byte, n)
for i := range p {
p[i] = byte(m.rnd.IntN(256))
}
return p
}
func (m *Changing) color() scene.Color {
return scene.Color{R: uint8(m.rnd.IntN(256)), G: uint8(m.rnd.IntN(256)), B: uint8(m.rnd.IntN(256)), A: uint8(64 + m.rnd.IntN(192))}
}
func (m *Changing) rect() scene.Rect {
return scene.Rect{X: m.rnd.Float32()*300 - 10, Y: m.rnd.Float32()*220 - 10, W: 2 + m.rnd.Float32()*90, H: 2 + m.rnd.Float32()*70}
}
// transform returns no transform mostly, else a translation, a rotation or
// a scaling.
func (m *Changing) transform() scene.Affine {
switch m.rnd.IntN(8) {
case 0:
return scene.Translation(m.rnd.Float32()*20-10, m.rnd.Float32()*20-10)
case 1:
return scene.Translation(150, 110).Mul(scene.Rotation(m.rnd.Float32()*40 - 20)).Mul(scene.Translation(-150, -110))
case 2:
return scene.Scaling(0.8+m.rnd.Float32()*0.4, 0.8+m.rnd.Float32()*0.4)
}
return scene.Affine{}
}
// wide gives op colors outside the sRGB gamut, now and then.
func (m *Changing) wide(s *scene.Scene, op *scene.Op) {
if m.rnd.IntN(4) != 0 {
return
}
s.Wide = append(s.Wide, scene.WideColors{Color: [4]float32{-0.2 + m.rnd.Float32()*0.1, 1.1, -0.1, 1}, Set: scene.WideColor})
op.Wide = uint16(len(s.Wide))
}
// op returns a random operation that draws.
func (m *Changing) op(s *scene.Scene) scene.Op {
var op scene.Op
switch m.rnd.IntN(8) {
case 0, 1:
r := m.rnd.Float32() * 12
op = scene.Op{Kind: scene.OpFill, Rect: m.rect(), Radii: scene.Uniform(r), Color: m.color(),
Border: scene.Uniform(float32(m.rnd.IntN(3))), BorderColor: m.color(), Continuous: m.rnd.IntN(3) == 0}
if m.rnd.IntN(4) == 0 {
op.Paint, op.Color2 = scene.PaintLinear, m.color()
op.Gradient = [4]float32{op.Rect.X, op.Rect.Y, op.Rect.X + op.Rect.W, op.Rect.Y + op.Rect.H}
}
m.wide(s, &op)
case 2:
op = scene.Op{Kind: scene.OpShadow, Rect: m.rect(), Radii: scene.Uniform(4), Color: m.color(), Blur: m.rnd.Float32() * 16}
case 3, 4:
start := int32(len(s.Glyphs))
for range 1 + m.rnd.IntN(3) {
k := m.masks[m.rnd.IntN(len(m.masks))]
s.Glyphs = append(s.Glyphs, scene.Glyph{X: float32(m.rnd.IntN(300)), Y: float32(m.rnd.IntN(220)),
W: float32(k[2]), H: float32(k[3]), U: k[0], V: k[1], UW: k[2], VH: k[3], Color: m.color()})
}
op = scene.Op{Kind: scene.OpGlyphs, Start: start, End: int32(len(s.Glyphs))}
case 5:
op = scene.Op{Kind: scene.OpImage, Rect: m.rect(), Image: m.img, Src: scene.Rect{W: 8, H: 8}}
default:
r := m.rnd.Float32() * 20
fx := scene.EffectOp{Effect: LensEffect, Blur: float32(m.rnd.IntN(4)) * 3}
if m.rnd.IntN(3) == 0 {
fx.Effect = TintEffect
}
fx.Params[0] = [4]float32{m.rnd.Float32() * 4}
c := m.color()
fx.Params[1] = [4]float32{float32(c.R) / 255, float32(c.G) / 255, float32(c.B) / 255, float32(c.A) / 255}
s.Effects = append(s.Effects, fx)
return scene.Op{Kind: scene.OpEffect, Rect: m.rect(), Radii: scene.Uniform(r), Start: int32(len(s.Effects) - 1)}
}
op.Transform = m.transform()
return op
}
// Scene returns a scene of 320×240 pixels.
func (m *Changing) Scene() *scene.Scene {
s := &scene.Scene{Width: 320, Height: 240, Clear: m.Clear, MaskAtlas: m.atlas,
Text: scene.TextParams{GammaRatios: scene.GammaRatios(1.8), Contrast: 1}}
for range 12 {
s.Ops = append(s.Ops, m.op(s))
}
s.Ops = append(s.Ops, scene.Op{Kind: scene.OpPushClip, Rect: m.rect(), Radii: scene.Uniform(6)})
for range 6 {
s.Ops = append(s.Ops, m.op(s))
}
s.Ops = append(s.Ops, scene.Op{Kind: scene.OpPopClip})
for range 6 {
s.Ops = append(s.Ops, m.op(s))
}
return s
}
// Change changes s a little, as a frame of an app would, and says how.
func (m *Changing) Change(s *scene.Scene) string {
// Drawing operations, which can change freely, unlike clips.
var drawing []int
for i, op := range s.Ops {
if op.Kind != scene.OpPushClip && op.Kind != scene.OpPopClip {
drawing = append(drawing, i)
}
}
i := drawing[m.rnd.IntN(len(drawing))]
op := &s.Ops[i]
switch m.rnd.IntN(10) {
case 0:
op.Color = m.color()
return "color"
case 1:
if op.Kind == scene.OpGlyphs {
s.Glyphs[op.Start].X += 3
return "glyph moved"
}
op.Rect.X += m.rnd.Float32()*20 - 10
return "moved"
case 2:
s.Ops = append(s.Ops[:i+1], s.Ops[i:]...)
s.Ops[i] = m.op(s)
return "inserted"
case 3:
if len(drawing) > 4 {
s.Ops = append(s.Ops[:i], s.Ops[i+1:]...)
return "deleted"
}
return "nothing"
case 4:
for j := range s.Ops {
if s.Ops[j].Kind == scene.OpPushClip {
s.Ops[j].Rect = m.rect()
}
}
return "clip moved"
case 5:
// New pixels where glyphs are, as a transient mask of the next
// frame takes the place of the last one's.
k := m.masks[m.rnd.IntN(len(m.masks))]
m.atlas.Put(int(k[0]), int(k[1]), int(k[2]), int(k[3]), m.pixels(int(k[2])*int(k[3])), int(k[2]))
return "atlas pixels"
case 6:
copy(m.img.Pix, m.pixels(len(m.img.Pix)))
m.img.Changed()
return "image pixels"
case 7:
op.Transform = m.transform()
return "transform"
case 8:
if op.Wide != 0 {
s.Wide[op.Wide-1].Color[0] = -0.3 + m.rnd.Float32()*0.2
return "wide color"
}
if op.Kind == scene.OpEffect {
s.Effects[op.Start].Params[0][0] = m.rnd.Float32() * 4
return "effect"
}
return "nothing"
case 9:
return m.nest(s)
default:
return "nothing"
}
}
// nest takes the clip nested in the scene's, moved by a translation, out
// of s, or puts one in, with a fill: the renderers then cut what the
// outer clip holds with a mask, as it holds a transformed clip, rather
// than in the shader, which draws its edge a unit in the last place
// apart, so all it holds changes.
func (m *Changing) nest(s *scene.Scene) string {
for k := range s.Ops {
if s.Ops[k].Kind != scene.OpPushClip || !s.Ops[k].Transform.Set {
continue
}
end := k + 1
for end < len(s.Ops) && s.Ops[end].Kind != scene.OpPopClip {
end++
}
s.Ops = append(s.Ops[:k], s.Ops[min(end+1, len(s.Ops)):]...)
return "nested clip out"
}
// Before the outer clip's end.
at := -1
for k, op := range s.Ops {
if op.Kind == scene.OpPopClip {
at = k
break
}
}
if at < 0 {
return "nothing"
}
inner := scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{W: 60, H: 40}, Radii: scene.Uniform(4),
Transform: scene.Translation(m.rnd.Float32()*240, m.rnd.Float32()*180)}
fill := scene.Op{Kind: scene.OpFill, Rect: m.rect(), Color: m.color()}
s.Ops = append(s.Ops[:at], append([]scene.Op{inner, fill, {Kind: scene.OpPopClip}}, s.Ops[at:]...)...)
return "nested clip in"
}
+85
View File
@@ -0,0 +1,85 @@
package gputest
import "github.com/egoist/mygo/internal/scene"
// CornerClipScene has clips that reach into the rounded corners of a clip
// around them, whose corners must cut what they hold (#1418), at the size
// of the reference scene (Scene): first a card as package ui draws it at
// scale 2, rounded and clipped, holding a column that clips too and fills
// the card's bottom; then a card holding a pane with a clip nested in it
// at its bottom, a rounded clip in its corner, and one in its middle, away
// from the corners.
func CornerClipScene() *scene.Scene {
s := &scene.Scene{Width: 320, Height: 740, Clear: scene.Color{R: 246, G: 247, B: 249, A: 255}}
add := func(ops ...scene.Op) { s.Ops = append(s.Ops, ops...) }
r4 := func(v float32) [4]float32 { return [4]float32{v, v, v, v} }
white := scene.Color{R: 255, G: 255, B: 255, A: 255}
pop := scene.Op{Kind: scene.OpPopClip}
card := scene.Rect{X: 10, Y: 20, W: 300, H: 240}
add(scene.Op{Kind: scene.OpFill, Rect: card, Radii: r4(20), Color: white},
scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 12, Y: 22, W: 296, H: 236}, Radii: r4(18)},
scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 12, Y: 102, W: 296, H: 156}},
scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 12, Y: 102, W: 296, H: 156}, Color: scene.Color{G: 122, B: 255, A: 255}},
pop, pop,
scene.Op{Kind: scene.OpFill, Rect: card, Radii: r4(20), Border: scene.Uniform(2), BorderColor: scene.Color{R: 180, G: 180, B: 180, A: 255}})
card = scene.Rect{X: 10, Y: 300, W: 300, H: 280}
add(scene.Op{Kind: scene.OpFill, Rect: card, Radii: r4(24), Color: white},
scene.Op{Kind: scene.OpPushClip, Rect: card, Radii: r4(24)},
scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 10, Y: 300, W: 110, H: 280}},
scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 10, Y: 300, W: 110, H: 280}, Color: scene.Color{R: 210, G: 214, B: 224, A: 255}},
scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 10, Y: 500, W: 110, H: 80}},
scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 0, Y: 490, W: 130, H: 100}, Color: scene.Color{R: 220, G: 40, B: 40, A: 255}},
pop, pop,
scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 230, Y: 420, W: 80, H: 160}, Radii: r4(10)},
scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 220, Y: 410, W: 100, H: 180}, Color: scene.Color{R: 30, G: 160, B: 90, A: 255}},
pop,
scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 140, Y: 340, W: 80, H: 160}},
scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 130, Y: 330, W: 100, H: 180}, Color: scene.Color{R: 250, G: 204, B: 21, A: 255}},
pop, pop)
return s
}
// ContinuousCornerClipScene has clips with square corners against the
// sides of a clip with continuous corners, whose curves must cut what they
// hold (#1511), as package ui draws an app's rounded frame on macOS: first
// a frame holding a sidebar that clips, against its left side, a toolbar
// that clips, in its top right corner, and content that does not clip;
// then a card holding a pane whose edge is between pixels, and a rounded
// clip in its corner, which a mask cuts.
func ContinuousCornerClipScene() *scene.Scene {
s := &scene.Scene{Width: 640, Height: 420, Clear: scene.Color{R: 246, G: 247, B: 249, A: 255}}
add := func(ops ...scene.Op) {
for _, op := range ops {
op.Continuous = op.Radii != [4]float32{}
s.Ops = append(s.Ops, op)
}
}
white := scene.Color{R: 255, G: 255, B: 255, A: 255}
pop := scene.Op{Kind: scene.OpPopClip}
frame := scene.Rect{X: 16, Y: 16, W: 608, H: 240}
add(scene.Op{Kind: scene.OpFill, Rect: frame, Radii: scene.Uniform(14), Color: white},
scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 18, Y: 18, W: 604, H: 236}, Radii: scene.Uniform(12)},
scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 18, Y: 18, W: 180, H: 236}},
scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 0, Y: 0, W: 200, H: 280}, Color: scene.Color{R: 210, G: 214, B: 224, A: 255}},
scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 10, Y: 200, W: 60, H: 70}, Color: scene.Color{R: 220, G: 40, B: 40, A: 255}},
pop,
scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 190, Y: 10, W: 450, H: 260}, Color: scene.Color{G: 122, B: 255, A: 255}},
scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 198, Y: 18, W: 424, H: 40}},
scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 190, Y: 10, W: 450, H: 60}, Color: scene.Color{R: 30, G: 160, B: 90, A: 255}},
pop, pop,
scene.Op{Kind: scene.OpFill, Rect: frame, Radii: scene.Uniform(14), Border: scene.Uniform(2), BorderColor: scene.Color{R: 180, G: 180, B: 180, A: 255}})
card := scene.Rect{X: 16, Y: 280, W: 608, H: 124}
add(scene.Op{Kind: scene.OpFill, Rect: card, Radii: scene.Uniform(16), Color: white},
scene.Op{Kind: scene.OpPushClip, Rect: card, Radii: scene.Uniform(16)},
scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 16, Y: 280, W: 120.5, H: 124}},
scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 0, Y: 270, W: 160, H: 150}, Color: scene.Color{R: 250, G: 204, B: 21, A: 255}},
pop,
scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 488, Y: 344, W: 136, H: 60}, Radii: scene.Uniform(8)},
scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 480, Y: 330, W: 160, H: 90}, Color: scene.Color{R: 30, G: 160, B: 90, A: 255}},
pop, pop)
return s
}
+246
View File
@@ -0,0 +1,246 @@
package gputest
import (
"math"
"github.com/egoist/mygo/internal/scene"
)
// CullCases are scenes of overlapping fills and what they hide: text,
// images, shadows and fills under opaque panels, with square, round and
// continuous corners; panels that do not hide what is under them
// (translucent, transformed, at half opacity, gradients, clipped to less,
// rounded clips, an effect reading what is under them); layers as large as
// the frame; and an occluder in the clear color.
func CullCases() []CullCase {
white := scene.Color{R: 255, G: 255, B: 255, A: 255}
paper := scene.Color{R: 246, G: 247, B: 249, A: 255}
red := scene.Color{R: 220, G: 40, B: 40, A: 255}
blue := scene.Color{R: 37, G: 99, B: 235, A: 255}
ink := scene.Color{R: 20, G: 24, B: 32, A: 255}
r4 := func(r float32) [4]float32 { return [4]float32{r, r, r, r} }
// base returns a scene with a mask atlas holding a disc, an image, and
// the helpers adding ops.
type builder struct {
s *scene.Scene
u, v uint16
img *scene.Image
}
base := func(clear scene.Color) *builder {
s := &scene.Scene{Width: 400, Height: 300, Clear: clear,
Text: scene.TextParams{GammaRatios: scene.GammaRatios(1.8), Contrast: 1, SubpixelContrast: 0.5}}
s.MaskAtlas = scene.NewAtlas(1, 64, 64)
disc := make([]byte, 16*16)
for y := range 16 {
for x := range 16 {
dx, dy := float64(x)-7.5, float64(y)-7.5
disc[y*16+x] = byte(255 * clamp(7.5-math.Sqrt(dx*dx+dy*dy)))
}
}
x, y, _ := s.MaskAtlas.Alloc(16, 16)
s.MaskAtlas.Put(x, y, 16, 16, disc, 16)
pix := make([]byte, 8*8*4)
for i := 0; i < len(pix); i += 4 {
pix[i], pix[i+1], pix[i+2], pix[i+3] = byte(i), byte(255-i), 160, 255
}
return &builder{s: s, u: uint16(x), v: uint16(y), img: scene.NewImageRGBA(8, 8, pix)}
}
add := func(b *builder, op scene.Op) { b.s.Ops = append(b.s.Ops, op) }
fill := func(b *builder, r scene.Rect, radius float32, c scene.Color) {
add(b, scene.Op{Kind: scene.OpFill, Rect: r, Radii: r4(radius), Color: c})
}
text := func(b *builder, x, y float32, n int) {
start := int32(len(b.s.Glyphs))
for i := range n {
b.s.Glyphs = append(b.s.Glyphs, scene.Glyph{X: x + float32(i*14), Y: y, W: 16, H: 16, U: b.u, V: b.v, UW: 16, VH: 16, Color: ink})
}
add(b, scene.Op{Kind: scene.OpGlyphs, Start: start, End: int32(len(b.s.Glyphs))})
}
image := func(b *builder, r scene.Rect) {
add(b, scene.Op{Kind: scene.OpImage, Rect: r, Image: b.img, Src: scene.Rect{W: 8, H: 8}})
}
rect := func(x, y, w, h float32) scene.Rect { return scene.Rect{X: x, Y: y, W: w, H: h} }
var out []CullCase
cs := func(name string, b *builder, culled int) { out = append(out, CullCase{name, b.s, culled}) }
// Stacked panels: text, an image, a shadow and a fill under an opaque
// panel with square corners, which lies on the page, as large as the
// frame. 4 ops are hidden: the glyphs, the image, the shadow and the fill.
b := base(scene.Color{A: 255})
fill(b, rect(0, 0, 400, 300), 0, paper)
text(b, 60, 60, 5)
image(b, rect(100, 100, 40, 40))
add(b, scene.Op{Kind: scene.OpShadow, Rect: rect(150, 120, 60, 40), Radii: r4(8), Color: scene.Color{A: 90}, Blur: 12})
fill(b, rect(40.5, 40.25, 200, 150), 0, red)
fill(b, rect(20, 20, 300, 240), 0, white)
fill(b, rect(180, 100, 160, 120), 0, blue)
text(b, 200, 130, 4)
cs("stacked panels", b, 4)
// Rounded and continuous corners: a card hides what lies inside its
// curves, the glyphs; the small fills near its corners stay, and so does
// the large fill under it.
for _, cont := range []bool{false, true} {
b = base(paper)
fill(b, rect(10, 10, 380, 280), 0, blue)
fill(b, rect(60, 60, 20, 20), 0, red)
fill(b, rect(320, 220, 30, 30), 0, red)
text(b, 140, 130, 3)
add(b, scene.Op{Kind: scene.OpFill, Rect: rect(50, 50, 300, 200), Radii: r4(40), Continuous: cont, Color: white})
name := "rounded card"
if cont {
name = "continuous card"
}
cs(name, b, 1)
}
// A translucent panel, one at half opacity, a gradient and a
// transformed one hide nothing.
b = base(paper)
text(b, 60, 60, 4)
add(b, scene.Op{Kind: scene.OpFill, Rect: rect(20, 20, 200, 100), Color: scene.Color{R: 255, G: 255, B: 255, A: 200}})
text(b, 60, 140, 4)
add(b, scene.Op{Kind: scene.OpFill, Rect: rect(20, 120, 200, 100), Color: white, Opacity: 0.5})
text(b, 240, 60, 4)
add(b, scene.Op{Kind: scene.OpFill, Rect: rect(230, 20, 150, 100), Color: red, Color2: blue, Paint: scene.PaintLinear, Gradient: [4]float32{230, 0, 380, 0}})
text(b, 240, 160, 4)
add(b, scene.Op{Kind: scene.OpFill, Rect: rect(230, 130, 150, 100), Color: white, Transform: scene.Translation(2, 3).Mul(scene.Scaling(1, 1.01))})
cs("panels that hide nothing", b, 0)
// Transformed content under a panel is hidden all the same, by its
// bounds, and the panel's own transform is what stops it from hiding.
b = base(paper)
start := int32(len(b.s.Glyphs))
b.s.Glyphs = append(b.s.Glyphs, scene.Glyph{X: 60, Y: 60, W: 16, H: 16, U: b.u, V: b.v, UW: 16, VH: 16, Color: ink})
add(b, scene.Op{Kind: scene.OpGlyphs, Start: start, End: int32(len(b.s.Glyphs)), Transform: scene.Scaling(1.5, 1.2)})
add(b, scene.Op{Kind: scene.OpFill, Rect: rect(100, 100, 40, 40), Radii: r4(6), Color: red, Transform: scene.Scaling(1.1, 1).Mul(scene.Translation(3, 2))})
fill(b, rect(20, 20, 300, 240), 0, white)
cs("transformed content", b, 2)
// A wide translucent border is not the fill's color: glyphs under it stay.
b = base(paper)
text(b, 25, 25, 4)
add(b, scene.Op{Kind: scene.OpFill, Rect: rect(20, 20, 200, 100), Color: white, Border: scene.Uniform(30), BorderColor: scene.Color{R: 255, A: 100}})
cs("wide translucent border", b, 0)
// A clip: an opaque layer hides what lies in the clip, drawn before or
// after the clip, and in the layer; the page, which the clip does not
// reach, and text on it stay.
b = base(paper)
fill(b, rect(0, 0, 400, 300), 0, blue)
text(b, 40, 40, 3)
add(b, scene.Op{Kind: scene.OpPushClip, Rect: rect(30.5, 30, 200, 100)})
text(b, 60, 60, 3)
fill(b, rect(100, 90, 50, 80), 0, red)
fill(b, rect(0, 0, 400, 300), 0, white)
add(b, scene.Op{Kind: scene.OpPopClip})
text(b, 260, 200, 3)
cs("clipped occluder", b, 3)
// A clip with a fractional edge antialiases it, and shows what is under.
b = base(paper)
text(b, 60, 100, 3)
add(b, scene.Op{Kind: scene.OpPushClip, Rect: rect(30, 30, 200, 70.5)})
fill(b, rect(0, 0, 400, 300), 0, white)
add(b, scene.Op{Kind: scene.OpPopClip})
cs("fractional clip edge", b, 0)
b = base(paper)
add(b, scene.Op{Kind: scene.OpPushClip, Rect: rect(30, 30, 200, 100), Radii: r4(20)})
text(b, 60, 60, 3)
fill(b, rect(0, 0, 400, 300), 0, white) // under a rounded clip: antialiased edge, hides nothing
add(b, scene.Op{Kind: scene.OpPopClip})
cs("rounded clip", b, 0)
// A panel and what only partly lies under it.
b = base(paper)
text(b, 25, 60, 6) // reaches out of the panel at 20..100? starts at 25, ends ~109
fill(b, rect(20, 20, 85, 200), 0, white)
cs("glyphs partly exposed", b, 0)
// An effect reading what is under it, reaching out of the panel on top.
b = base(paper)
fill(b, rect(60, 60, 60, 60), 0, red)
fx := scene.EffectOp{Effect: LensEffect, Blur: 4}
fx.Params[0] = [4]float32{2}
fx.Params[1] = [4]float32{0.5, 0.5, 1, 0.3}
add(b, scene.Op{Kind: scene.OpEffect, Rect: rect(40, 40, 120, 120), Radii: r4(20), Start: 0})
b.s.Effects = append(b.s.Effects, fx)
fill(b, rect(30, 30, 100, 100), 0, white)
cs("effect between", b, 0)
// An occluder in the clear color hides what is under it all the same.
b = base(paper)
text(b, 60, 60, 4)
image(b, rect(100, 100, 40, 40))
fill(b, rect(30, 30, 200, 150), 10, paper)
cs("occluder in the clear color", b, 2)
// Layers as large as the frame; hole and shadows under them.
b = base(scene.Color{A: 255})
fill(b, rect(0, 0, 400, 300), 0, blue)
add(b, scene.Op{Kind: scene.OpShadow, Rect: rect(100, 100, 100, 100), Radii: r4(10), Color: scene.Color{A: 120}, Blur: 20})
add(b, scene.Op{Kind: scene.OpHole, Rect: rect(100, 100, 100, 100)})
fill(b, rect(-10, -10, 420, 320), 0, white)
fill(b, rect(0, 0, 400, 300), 0, paper)
cs("frame sized layers", b, 4)
// A hole after an opaque fill, and an opaque fill after a hole.
b = base(paper)
fill(b, rect(20, 20, 200, 200), 0, blue)
add(b, scene.Op{Kind: scene.OpHole, Rect: rect(110, 110, 20, 20), Radii: r4(4)})
fill(b, rect(100, 100, 100, 100), 0, red)
add(b, scene.Op{Kind: scene.OpHole, Rect: rect(120, 120, 20, 20)})
fill(b, rect(110, 110, 30, 30), 0, white)
cs("holes", b, 1)
// Nested panels, each lying on a larger one drawn later: all but the
// last are hidden.
b = base(paper)
for i := 18; i >= 0; i-- {
fill(b, rect(float32(i*8), float32(i*8), 400-float32(i*16), 300-float32(i*16)), 0, scene.Color{R: byte(40 + i*10), G: 100, B: 200, A: 255})
}
cs("nested panels", b, 18)
return out
}
// FillScene returns a w×h scene of plain fills, the kind most of a window
// is made of: layers as large as the frame, panels with square and round
// corners at fractional places, rows in a clip that holds some whole and
// cuts others, fills of every opacity, a gradient, a border and a transformed
// fill among them, over the clear color. Sizes at or above 2048 pixels take
// the interpolated coordinates across binades of float32.
func FillScene(w, h int) *scene.Scene {
s := &scene.Scene{Width: w, Height: h, Clear: scene.Color{R: 12, G: 10, B: 9, A: 255}}
fw, fh := float32(w), float32(h)
r4 := func(r float32) [4]float32 { return [4]float32{r, r, r, r} }
add := func(op scene.Op) { s.Ops = append(s.Ops, op) }
shade := func(i int) scene.Color {
return scene.Color{R: byte(20 + i*37%200), G: byte(10 + i*53%220), B: byte(40 + i*71%200), A: 255}
}
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{W: fw, H: fh}, Color: shade(1)})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: fw / 6, Y: 16.25, W: fw * 2 / 3, H: fh - 40.5}, Radii: r4(24), Color: shade(2)})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: fw/6 + 2, Y: 154, W: fw*2/3 - 4, H: fh - 190}, Color: shade(3)})
// Rows in a clip: some inside it, one cut at each end.
cx, cy, cw, ch := fw/6+2, float32(154), fw*2/3-4, fh-190
add(scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: cx + 10.5, Y: cy + 20.25, W: cw - 21, H: ch - 40}})
for i := range 40 {
y := cy + 10 + float32(i)*(ch-20)/40
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: cx + 5.5, Y: y, W: cw - 11, H: (ch-20)/40 - 1.5}, Color: shade(i + 4)})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: cx + 30, Y: y + 2, W: cw / 3, H: 6.5}, Radii: r4(3), Color: shade(i + 9), Opacity: 0.5 + float32(i%3)/6})
}
add(scene.Op{Kind: scene.OpPopClip})
// A rounded clip, and an op touching its edge.
add(scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: cx + 40, Y: cy + 40, W: 300, H: 200}, Radii: r4(20)})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: cx + 30, Y: cy + 100, W: 400, H: 60}, Color: shade(50)})
add(scene.Op{Kind: scene.OpPopClip})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: cx + 400, Y: cy + 30, W: 200, H: 80}, Color: shade(60), Color2: shade(61), Paint: scene.PaintLinear, Gradient: [4]float32{cx + 400, 0, cx + 600, 0}})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: cx + 400, Y: cy + 130, W: 200, H: 80}, Radii: r4(12), Color: shade(62), Border: scene.Uniform(2), BorderColor: shade(63)})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: cx + 400, Y: cy + 230, W: 200, H: 80}, Color: shade(64), Transform: scene.Scaling(1.01, 1)})
// The edges of the frame: fills reaching and passing it.
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: -5, Y: fh - 30, W: fw + 10, H: 30}, Color: shade(70)})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: fw - 20, Y: 0, W: 20, H: fh}, Color: shade(71), Opacity: 0.4})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 0, Y: 0, W: fw, H: 1.5}, Color: shade(72)})
return s
}
+77
View File
@@ -0,0 +1,77 @@
package gputest
import "github.com/egoist/mygo/internal/scene"
// Effects for the renderers' tests, as plugins define theirs (see
// scene.Effect). LensEffect shows its backdrop, sampled further right
// within 8 pixels of its edge, by p0.x pixels a pixel, mixed with the
// straight color p1 by its alpha; TintEffect paints p1 alone, reading no
// backdrop.
var (
LensEffect = &scene.Effect{
Name: "lens",
Backdrop: true,
Metal: scene.EffectCode{Source: `
float4 effect(float2 p, Effect e, texture2d<float> backdrop) {
float d = max(8.0f + sdRoundRect(p, e.rect, e.radii), 0.0f) * e.p0.x;
float3 c = sampleBackdrop(backdrop, e, p + float2(d, 0.0f));
c += (e.p1.rgb - c) * e.p1.a;
return float4(c, 1.0f);
}`},
HLSL: scene.EffectCode{Source: `
float4 effect(float2 p, Effect e) {
float d = max(8 + sdRoundRect(p, e.rect, e.radii), 0) * e.p0.x;
float3 c = sampleBackdrop(e, p + float2(d, 0));
c += (e.p1.rgb - c) * e.p1.a;
return float4(c, 1);
}`},
GLSL: `
vec4 effect(vec2 p, Effect e) {
float d = max(8.0 + sdRoundRect(p, e.rect, e.radii), 0.0) * e.p0.x;
vec3 c = sampleBackdrop(e, p + vec2(d, 0.0));
c += (e.p1.rgb - c) * e.p1.a;
return vec4(c, 1.0);
}`,
Pixels: func() scene.EffectPixels { return &testPixels{} },
}
TintEffect = &scene.Effect{
Name: "tint",
Metal: scene.EffectCode{Source: `
float4 effect(float2 p, Effect e, texture2d<float> backdrop) {
return float4(e.p1.rgb * e.p1.a, e.p1.a);
}`},
HLSL: scene.EffectCode{Source: `
float4 effect(float2 p, Effect e) {
return float4(e.p1.rgb * e.p1.a, e.p1.a);
}`},
GLSL: `
vec4 effect(vec2 p, Effect e) {
return vec4(e.p1.rgb * e.p1.a, e.p1.a);
}`,
Pixels: func() scene.EffectPixels { return &testPixels{} },
}
)
// testPixels draws LensEffect and TintEffect on the CPU.
type testPixels struct {
op *scene.EffectOp
rect scene.Rect
radii [4]float32
}
func (t *testPixels) Begin(op *scene.EffectOp, rect scene.Rect, radii [4]float32) {
t.op, t.rect, t.radii = op, rect, radii
}
func (t *testPixels) Color(x, y float32, b *scene.BackdropImage) [4]float32 {
tint := t.op.Params[1]
if b == nil {
return [4]float32{tint[0] * tint[3], tint[1] * tint[3], tint[2] * tint[3], tint[3]}
}
d := max(8+scene.SDRoundRect(t.rect, t.radii, x, y), 0) * t.op.Params[0][0]
c := b.Sample(x+d, y)
for i := range c {
c[i] += (tint[i] - c[i]) * tint[3]
}
return [4]float32{c[0], c[1], c[2], 1}
}
+838
View File
@@ -0,0 +1,838 @@
// Package gputest has what the tests of the GPU renderers share: a scene
// that draws every kind of operation, and a comparison of what a renderer
// drew with what the CPU renderer draws, which the renderers must match.
package gputest
import (
"fmt"
"image"
"image/png"
"math"
"os"
"path/filepath"
"testing"
"github.com/egoist/mygo/internal/raster"
"github.com/egoist/mygo/internal/scene"
)
// Scene returns a 320×600 scene with fills, borders of every width and
// dashed, gradients mixed in sRGB and Oklab, stripes, shadows, blurred or
// not, and cut by the boxes casting them, inner shadows, a hole, nested
// rounded clips, glyphs from both atlases and subpixel ones, plain and in
// gradients, with Direct2D's gamma and contrast, images, in color and in
// gray, and effects, reading their backdrop blurred at each size or not,
// over each other, clipped and at the frame's edge, or reading none.
func Scene() *scene.Scene {
s := &scene.Scene{Width: 320, Height: 740, Clear: scene.Color{R: 246, G: 247, B: 249, A: 255},
Text: scene.TextParams{GammaRatios: scene.GammaRatios(1.8), Contrast: 1, SubpixelContrast: 0.5}}
mask := scene.NewAtlas(1, 64, 64)
color := scene.NewAtlas(4, 64, 32)
s.MaskAtlas, s.ColorAtlas = mask, color
// A disc and a ring as glyph masks, and a color glyph.
disc := make([]byte, 16*16)
ring := make([]byte, 12*12)
for y := range 16 {
for x := range 16 {
dx, dy := float64(x)-7.5, float64(y)-7.5
disc[y*16+x] = byte(255 * clamp(7.5-math.Sqrt(dx*dx+dy*dy)))
if x < 12 && y < 12 {
ex, ey := float64(x)-5.5, float64(y)-5.5
d := math.Sqrt(ex*ex + ey*ey)
ring[y*12+x] = byte(255 * clamp(1.5-math.Abs(d-4)))
}
}
}
dx, dy, _ := mask.Alloc(16, 16)
mask.Put(dx, dy, 16, 16, disc, 16)
rx, ry, _ := mask.Alloc(12, 12)
mask.Put(rx, ry, 12, 12, ring, 12)
emoji := make([]byte, 10*10*4)
for i := 0; i < len(emoji); i += 4 {
k := i / 4
// Premultiplied: half transparent in the corner rows.
a := byte(255)
if k < 10 || k >= 90 {
a = 128
}
emoji[i], emoji[i+1], emoji[i+2], emoji[i+3] = byte(uint16(240)*uint16(a)/255), byte(uint16(160)*uint16(a)/255), byte(uint16(20)*uint16(a)/255), a
}
cx, cy, _ := color.Alloc(10, 10)
color.Put(cx, cy, 10, 10, emoji, 40)
// A subpixel disc: its red subpixels a third of a pixel left of its
// green ones, its blue ones a third right.
lcd := make([]byte, 16*16*4)
for y := range 16 {
for x := range 16 {
for c := range 3 {
dx, dy := float64(x)-7.5+float64(c-1)/3, float64(y)-7.5
lcd[(y*16+x)*4+c] = byte(255 * clamp(6.5-math.Sqrt(dx*dx+dy*dy)))
}
}
}
lx, ly, _ := color.Alloc(16, 16)
color.Put(lx, ly, 16, 16, lcd, 64)
pix := make([]byte, 8*8*4)
for y := range 8 {
for x := range 8 {
i := (y*8 + x) * 4
pix[i], pix[i+1], pix[i+2], pix[i+3] = byte(x*32), byte(y*32), 160, 255
}
}
img := scene.NewImageRGBA(8, 8, pix)
red := scene.Color{R: 220, G: 40, B: 40, A: 255}
blue := scene.Color{R: 37, G: 99, B: 235, A: 255}
ink := scene.Color{R: 20, G: 24, B: 32, A: 255}
r4 := func(r float32) [4]float32 { return [4]float32{r, r, r, r} }
add := func(op scene.Op) { s.Ops = append(s.Ops, op) }
add(scene.Op{Kind: scene.OpShadow, Rect: scene.Rect{X: 20, Y: 24, W: 120, H: 80}, Radii: r4(12), Color: scene.Color{A: 90}, Blur: 16})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 20, Y: 20, W: 120, H: 80}, Radii: r4(12), Color: scene.Color{R: 255, G: 255, B: 255, A: 255},
Border: scene.Uniform(1), BorderColor: scene.Color{R: 200, G: 204, B: 210, A: 255}})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 160.5, Y: 20.25, W: 140, H: 30}, Radii: [4]float32{15, 4, 15, 4},
Color: red, Color2: blue, Paint: scene.PaintLinear, Gradient: [4]float32{160, 20, 300, 50}})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 170, Y: 60, W: 60, H: 40}, Color: scene.Color{R: 37, G: 99, B: 235, A: 128}, Border: scene.Uniform(3), BorderColor: red, Radii: r4(8)})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 240, Y: 60, W: 40, H: 40}, Radii: r4(20), Color: blue, Opacity: 0.5})
add(scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 20, Y: 120, W: 200, H: 100}, Radii: r4(16)})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 0, Y: 110, W: 320, H: 130}, Color: scene.Color{R: 230, G: 236, B: 250, A: 255}})
add(scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 100, Y: 130, W: 200, H: 70}})
add(scene.Op{Kind: scene.OpShadow, Rect: scene.Rect{X: 120, Y: 150, W: 60, H: 30}, Radii: r4(6), Color: scene.Color{B: 80, A: 140}, Blur: 10})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 90, Y: 140, W: 50, H: 50}, Color: red, Radii: r4(4)})
add(scene.Op{Kind: scene.OpPopClip})
start := int32(len(s.Glyphs))
for i := range 6 {
s.Glyphs = append(s.Glyphs, scene.Glyph{X: float32(30 + i*20), Y: 200, W: 16, H: 16, U: uint16(dx), V: uint16(dy), UW: 16, VH: 16, Color: ink},
scene.Glyph{X: float32(32 + i*20), Y: 182, W: 12, H: 12, U: uint16(rx), V: uint16(ry), UW: 12, VH: 12, Color: blue})
}
s.Glyphs = append(s.Glyphs, scene.Glyph{X: 160, Y: 180, W: 10, H: 10, U: uint16(cx), V: uint16(cy), UW: 10, VH: 10, Color: scene.Color{A: 255}, Colored: true})
add(scene.Op{Kind: scene.OpGlyphs, Start: start, End: int32(len(s.Glyphs))})
add(scene.Op{Kind: scene.OpPopClip})
add(scene.Op{Kind: scene.OpImage, Rect: scene.Rect{X: 240, Y: 120, W: 8, H: 8}, Image: img, Src: scene.Rect{W: 8, H: 8}})
add(scene.Op{Kind: scene.OpImage, Rect: scene.Rect{X: 240, Y: 140, W: 64, H: 64}, Radii: r4(10), Image: img, Src: scene.Rect{W: 8, H: 8}})
// Pixel art: the whole image 2.5 times as large, each pixel a square of
// its own color with smooth edges, and a part of it four times, with
// none.
add(scene.Op{Kind: scene.OpImage, Rect: scene.Rect{X: 256, Y: 118, W: 20, H: 20}, Image: img, Src: scene.Rect{W: 8, H: 8}, Pixelated: true})
add(scene.Op{Kind: scene.OpImage, Rect: scene.Rect{X: 284, Y: 120, W: 16, H: 16}, Image: img, Src: scene.Rect{X: 2, Y: 3, W: 4, H: 4}, Pixelated: true})
// Sprites: a part flipped across both axes and tinted, pixelated, and
// one flipped across one, smooth, showing none of the pixels around.
add(scene.Op{Kind: scene.OpImage, Rect: scene.Rect{X: 284, Y: 60, W: 16, H: 16}, Image: img, Src: scene.Rect{X: 6, Y: 7, W: -4, H: -4}, Pixelated: true,
Color: scene.Color{R: 255, G: 200, B: 80, A: 220}})
add(scene.Op{Kind: scene.OpImage, Rect: scene.Rect{X: 284, Y: 80, W: 20, H: 20}, Image: img, Src: scene.Rect{X: 7, Y: 1, W: -5, H: 5}})
// Borders of different widths, dashed ones, Oklab gradients, stripes,
// glyphs in gradients and a gray image.
yellow := scene.Color{R: 250, G: 204, B: 21, A: 255}
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 20, Y: 240, W: 60, H: 30}, Radii: r4(10), Color: scene.Color{R: 255, G: 255, B: 255, A: 255},
Border: [4]float32{1, 4, 2, 0}, BorderColor: ink})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 90, Y: 240, W: 60, H: 30}, Radii: r4(8), Border: scene.Uniform(2), BorderColor: blue, Dashed: true})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 160, Y: 240, W: 70, H: 30}, Color: yellow, Border: [4]float32{0, 0, 3, 1}, BorderColor: red, Dashed: true})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 240, Y: 240, W: 64, H: 30}, Radii: r4(4), Color: blue, Color2: yellow, Paint: scene.PaintOklab, Gradient: [4]float32{240, 0, 304, 0}})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 20, Y: 280, W: 80, H: 30}, Color: red, Color2: scene.Color{}, Paint: scene.PaintLinear, Gradient: [4]float32{20, 0, 100, 0}})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 110, Y: 280, W: 80, H: 30}, Radii: r4(6), Color: scene.Color{R: 37, G: 99, B: 235, A: 200}, Color2: yellow,
Paint: scene.PaintStripes, Gradient: [4]float32{0.7071, -0.7071, 3, 8}, Border: scene.Uniform(1), BorderColor: ink})
start = int32(len(s.Glyphs))
for i := range 3 {
s.Glyphs = append(s.Glyphs, scene.Glyph{X: float32(200 + i*18), Y: 288, W: 16, H: 16, U: uint16(dx), V: uint16(dy), UW: 16, VH: 16, Color: ink})
}
add(scene.Op{Kind: scene.OpGlyphs, Start: start, End: int32(len(s.Glyphs)), Paint: scene.PaintOklab, Color: red, Color2: blue, Gradient: [4]float32{200, 288, 252, 304}})
add(scene.Op{Kind: scene.OpImage, Rect: scene.Rect{X: 270, Y: 280, W: 32, H: 32}, Radii: r4(6), Image: img, Src: scene.Rect{W: 8, H: 8}, Grayscale: true})
// Shadows that show only outside the boxes casting them: under a
// translucent card, without blur, and alone.
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 0, Y: 320, W: 320, H: 60}, Color: yellow})
card := scene.Rect{X: 20, Y: 330, W: 90, H: 36}
add(scene.Op{Kind: scene.OpShadow, Rect: scene.Rect{X: 20, Y: 334, W: 90, H: 36}, Radii: r4(10), Color: scene.Color{A: 120}, Blur: 12, Cast: card, CastRadii: r4(10)})
add(scene.Op{Kind: scene.OpFill, Rect: card, Radii: r4(10), Color: scene.Color{R: 255, G: 255, B: 255, A: 140}})
card = scene.Rect{X: 130.5, Y: 330.25, W: 70, H: 30}
add(scene.Op{Kind: scene.OpShadow, Rect: scene.Rect{X: 133.5, Y: 333.25, W: 70, H: 30}, Radii: r4(8), Color: ink, Cast: card, CastRadii: r4(8)})
add(scene.Op{Kind: scene.OpFill, Rect: card, Radii: r4(8), Color: scene.Color{R: 37, G: 99, B: 235, A: 90}})
add(scene.Op{Kind: scene.OpShadow, Rect: scene.Rect{X: 218, Y: 334, W: 84, H: 32}, Radii: r4(16), Color: red, Blur: 6,
Cast: scene.Rect{X: 220, Y: 334, W: 80, H: 28}, CastRadii: r4(14)})
// A hole through them, rounded and cut by a rounded clip, under a
// translucent fill and its shadow, as a web view under a popover.
add(scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 210, Y: 322, W: 104, H: 56}, Radii: r4(12)})
add(scene.Op{Kind: scene.OpHole, Rect: scene.Rect{X: 246.5, Y: 326.25, W: 80, H: 40}, Radii: r4(10)})
add(scene.Op{Kind: scene.OpShadow, Rect: scene.Rect{X: 270, Y: 344, W: 34, H: 30}, Radii: r4(6), Color: scene.Color{A: 100}, Blur: 8})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 270, Y: 340, W: 34, H: 30}, Radii: r4(6), Color: scene.Color{R: 37, G: 99, B: 235, A: 140}})
add(scene.Op{Kind: scene.OpPopClip})
// A hole at half opacity, as a web view in a page fading in.
add(scene.Op{Kind: scene.OpHole, Rect: scene.Rect{X: 112.5, Y: 354.5, W: 60, H: 20}, Radii: r4(6), Opacity: 0.5})
// Subpixel glyphs, dark on white and light on dark, half transparent,
// in a gradient, and clipped; thin mask glyphs, dark and light.
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 0, Y: 380, W: 160, H: 30}, Color: scene.Color{R: 255, G: 255, B: 255, A: 255}})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 160, Y: 380, W: 160, H: 30}, Color: scene.Color{R: 24, G: 24, B: 27, A: 255}})
sub := func(x float32, c scene.Color) scene.Glyph {
return scene.Glyph{X: x, Y: 387, W: 16, H: 16, U: uint16(lx), V: uint16(ly), UW: 16, VH: 16, Color: c, Subpixel: true}
}
start = int32(len(s.Glyphs))
s.Glyphs = append(s.Glyphs, sub(8, ink), sub(28, blue), sub(48, scene.Color{R: 20, G: 24, B: 32, A: 128}),
sub(168, scene.Color{R: 255, G: 255, B: 255, A: 255}), sub(188, yellow), sub(208, scene.Color{R: 113, G: 113, B: 122, A: 255}),
scene.Glyph{X: 68, Y: 387, W: 16, H: 16, U: uint16(dx), V: uint16(dy), UW: 16, VH: 16, Color: ink, Thin: true},
scene.Glyph{X: 228, Y: 387, W: 16, H: 16, U: uint16(dx), V: uint16(dy), UW: 16, VH: 16, Color: scene.Color{R: 244, G: 244, B: 245, A: 255}, Thin: true})
add(scene.Op{Kind: scene.OpGlyphs, Start: start, End: int32(len(s.Glyphs))})
start = int32(len(s.Glyphs))
s.Glyphs = append(s.Glyphs, sub(92, ink), sub(110, ink), sub(128, ink))
add(scene.Op{Kind: scene.OpGlyphs, Start: start, End: int32(len(s.Glyphs)), Paint: scene.PaintLinear, Color: red, Color2: blue, Gradient: [4]float32{92, 387, 144, 403}})
add(scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 250, Y: 384, W: 40, H: 22}, Radii: r4(8)})
start = int32(len(s.Glyphs))
s.Glyphs = append(s.Glyphs, sub(248, scene.Color{R: 255, G: 255, B: 255, A: 255}), sub(266, scene.Color{R: 255, G: 255, B: 255, A: 255}), sub(284, red))
add(scene.Op{Kind: scene.OpGlyphs, Start: start, End: int32(len(s.Glyphs))})
add(scene.Op{Kind: scene.OpPopClip})
// Panes of glass over stripes, a gradient and glyphs.
for i := range 8 {
c := []scene.Color{red, blue, yellow, ink}[i%4]
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: float32(i * 40), Y: 410, W: 20, H: 130}, Color: c})
}
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 0, Y: 470, W: 320, H: 20}, Color: red, Color2: blue, Paint: scene.PaintOklab, Gradient: [4]float32{0, 0, 320, 0}})
start = int32(len(s.Glyphs))
for i := range 14 {
s.Glyphs = append(s.Glyphs, scene.Glyph{X: float32(6 + i*22), Y: 516, W: 16, H: 16, U: uint16(dx), V: uint16(dy), UW: 16, VH: 16, Color: ink})
}
add(scene.Op{Kind: scene.OpGlyphs, Start: start, End: int32(len(s.Glyphs))})
// Effects over them: reading their backdrop blurred at a quarter of
// the size, over each other at the same size, unblurred, clipped and
// half transparent at full size, past the frame's edge at half the
// size; and one reading none.
effect := func(e *scene.Effect, r scene.Rect, radii [4]float32, blur, shift float32, tint scene.Color, opacity float32) {
fx := scene.EffectOp{Effect: e, Blur: blur}
fx.Params[0] = [4]float32{shift}
fx.Params[1] = [4]float32{float32(tint.R) / 255, float32(tint.G) / 255, float32(tint.B) / 255, float32(tint.A) / 255}
add(scene.Op{Kind: scene.OpEffect, Rect: r, Radii: radii, Start: int32(len(s.Effects)), Opacity: opacity})
s.Effects = append(s.Effects, fx)
}
white := scene.Color{R: 255, G: 255, B: 255, A: 90}
add(scene.Op{Kind: scene.OpShadow, Rect: scene.Rect{X: 12, Y: 424, W: 140, H: 44}, Radii: r4(22), Color: scene.Color{A: 40}, Blur: 16,
Cast: scene.Rect{X: 12, Y: 420, W: 140, H: 44}, CastRadii: r4(22)})
effect(LensEffect, scene.Rect{X: 12, Y: 420, W: 140, H: 44}, r4(22), 8, 2, white, 0)
effect(LensEffect, scene.Rect{X: 120, Y: 446, W: 60, H: 60}, r4(30), 12, 3, white, 0)
effect(LensEffect, scene.Rect{X: 190.5, Y: 418.25, W: 110, H: 50}, r4(16), 0, 1.5, scene.Color{R: 37, G: 99, B: 235, A: 120}, 0)
add(scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 30, Y: 480, W: 200, H: 50}, Radii: r4(12)})
effect(LensEffect, scene.Rect{X: 40, Y: 476, W: 240, H: 44}, [4]float32{22, 6, 22, 6}, 3, 1, scene.Color{A: 60}, 0.7)
add(scene.Op{Kind: scene.OpPopClip})
effect(LensEffect, scene.Rect{X: 284, Y: 494, W: 50, H: 50}, r4(14), 6, 2, white, 0)
effect(TintEffect, scene.Rect{X: 250, Y: 420, W: 30, H: 30}, r4(8), 0, 0, scene.Color{R: 220, G: 40, B: 40, A: 160}, 0)
// Inner shadows: a hairline blurred along the edge of a dark card,
// offset and spread, without blur, and with no hole left.
inset := func(box scene.Rect, radius float32, hole scene.Rect, holeRadius float32, c scene.Color, blur float32) {
add(scene.Op{Kind: scene.OpFill, Rect: box, Radii: r4(radius), Color: scene.Color{R: 39, G: 39, B: 42, A: 255}})
add(scene.Op{Kind: scene.OpShadow, Inset: true, Rect: hole, Radii: r4(holeRadius), Color: c, Blur: blur, Cast: box, CastRadii: r4(radius)})
}
inset(scene.Rect{X: 10, Y: 548, W: 90, H: 44}, 10, scene.Rect{X: 10, Y: 548, W: 90, H: 44}, 10, scene.Color{R: 255, G: 255, B: 255, A: 120}, 2)
inset(scene.Rect{X: 110.5, Y: 548.25, W: 90, H: 44}, 14, scene.Rect{X: 117.5, Y: 556.25, W: 82, H: 36}, 10, yellow, 8)
inset(scene.Rect{X: 210, Y: 548, W: 50, H: 44}, 12, scene.Rect{X: 216, Y: 555, W: 38, H: 30}, 6, red, 0)
inset(scene.Rect{X: 270, Y: 548, W: 40, H: 44}, 20, scene.Rect{}, 0, blue, 6)
// Gradients of stops: conic, in sRGB with a hard stop and in Oklab,
// linear, half transparent and to transparent, radial, off center
// with a hard stop and in Oklab to transparent, painting borders,
// plain and dashed, and glyphs; and images blurred, at full size and a
// quarter of it.
stops := func(st ...scene.Stop) [2]int32 {
a := int32(len(s.Stops))
s.Stops = append(s.Stops, st...)
return [2]int32{a, int32(len(s.Stops))}
}
green := scene.Color{R: 34, G: 197, B: 94, A: 255}
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 10, Y: 605, W: 60, H: 60}, Radii: r4(30), Paint: scene.PaintConic,
Gradient: [4]float32{40, 635, 0.4, 0}, Stops: stops(scene.Stop{Color: red, At: 0}, scene.Stop{Color: yellow, At: 0.3}, scene.Stop{Color: blue, At: 0.3}, scene.Stop{Color: green, At: 0.7}, scene.Stop{Color: red, At: 1})})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 80.5, Y: 605.25, W: 60, H: 60}, Radii: r4(8), Paint: scene.PaintConicOklab,
Gradient: [4]float32{100, 625, -1, 0}, Stops: stops(scene.Stop{Color: blue, At: 0.1}, scene.Stop{Color: yellow, At: 0.5}, scene.Stop{Color: red, At: 0.9})})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 150, Y: 605, W: 70, H: 25}, Radii: r4(6), Paint: scene.PaintLinearStops,
Gradient: [4]float32{150, 605, 220, 630}, Stops: stops(scene.Stop{Color: red, At: 0.1}, scene.Stop{Color: scene.Color{R: 37, G: 99, B: 235, A: 120}, At: 0.5}, scene.Stop{Color: yellow, At: 0.9})})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 150, Y: 637, W: 70, H: 25}, Paint: scene.PaintLinearStopsOklab,
Gradient: [4]float32{150, 0, 220, 0}, Stops: stops(scene.Stop{Color: blue, At: 0}, scene.Stop{Color: green, At: 0.6}, scene.Stop{Color: scene.Color{}, At: 1})})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 230.5, Y: 605.25, W: 80, H: 60}, Radii: r4(14), Color: scene.Color{R: 255, G: 255, B: 255, A: 120},
Border: scene.Uniform(6), BorderColor: scene.Color{R: 255, G: 255, B: 255, A: 255}, BorderPaint: true, Paint: scene.PaintConicOklab,
Gradient: [4]float32{270, 635, 0, 0}, Stops: stops(scene.Stop{Color: red, At: 0}, scene.Stop{Color: blue, At: 0.5}, scene.Stop{Color: red, At: 1})})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 230, Y: 670, W: 80, H: 30}, Radii: r4(6), Border: scene.Uniform(3),
BorderColor: scene.Color{R: 255, G: 255, B: 255, A: 200}, BorderPaint: true, Dashed: true, Paint: scene.PaintLinearStops,
Gradient: [4]float32{230, 0, 310, 0}, Stops: stops(scene.Stop{Color: green, At: 0}, scene.Stop{Color: red, At: 1})})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 200, Y: 704, W: 50, H: 32}, Radii: r4(8), Paint: scene.PaintRadial,
Gradient: [4]float32{213, 714, 26.3, 0}, Stops: stops(scene.Stop{Color: yellow, At: 0}, scene.Stop{Color: red, At: 0.5}, scene.Stop{Color: blue, At: 0.5}, scene.Stop{Color: green, At: 1})})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 256.5, Y: 704.25, W: 56, H: 32}, Radii: r4(16), Paint: scene.PaintRadialOklab,
Gradient: [4]float32{284.5, 720.25, 30, 0}, Stops: stops(scene.Stop{Color: scene.Color{R: 255, G: 255, B: 255, A: 255}, At: 0.1}, scene.Stop{Color: scene.Color{R: 37, G: 99, B: 235, A: 140}, At: 0.6}, scene.Stop{Color: scene.Color{}, At: 1})})
start = int32(len(s.Glyphs))
for i := range 3 {
s.Glyphs = append(s.Glyphs, scene.Glyph{X: float32(10 + i*18), Y: 676, W: 16, H: 16, U: uint16(dx), V: uint16(dy), UW: 16, VH: 16, Color: ink})
}
add(scene.Op{Kind: scene.OpGlyphs, Start: start, End: int32(len(s.Glyphs)), Paint: scene.PaintConic,
Gradient: [4]float32{36, 684, 0, 0}, Stops: stops(scene.Stop{Color: red, At: 0}, scene.Stop{Color: blue, At: 0.5}, scene.Stop{Color: yellow, At: 1})})
add(scene.Op{Kind: scene.OpImage, Rect: scene.Rect{X: 70, Y: 672, W: 48, H: 48}, Image: img, Src: scene.Rect{W: 8, H: 8}, Blur: 1.2})
big := make([]byte, 64*64*4)
for y := range 64 {
for x := range 64 {
i := (y*64 + x) * 4
if x >= 16 && x < 48 && y >= 20 && y < 44 {
big[i], big[i+1], big[i+2], big[i+3] = 220, 40, 40, 255
} else if (x/8+y/8)%2 == 0 && x > 4 && x < 60 && y > 4 && y < 60 {
big[i], big[i+1], big[i+2], big[i+3] = 18, 50, 117, 128
}
}
}
add(scene.Op{Kind: scene.OpImage, Rect: scene.Rect{X: 130, Y: 672, W: 64, H: 64}, Radii: r4(8), Image: scene.NewImageRGBA(64, 64, big), Src: scene.Rect{W: 64, H: 64}, Blur: 12})
return s
}
// Compare compares pixels a renderer drew, premultiplied BGRA rows of
// stride bytes, with what the CPU renderer draws of s. Antialiased edges
// and interpolated images may differ a little; anything else fails t. With
// MYGO_GPU_PNG naming a directory, it saves both images there.
func Compare(t testing.TB, name string, got []byte, stride int, s *scene.Scene) {
t.Helper()
want := raster.NewImage(s.Width, s.Height)
raster.Render(want, s)
worst, off := 0, 0
worstAt := image.Point{}
for y := range s.Height {
for x := range s.Width {
g := got[y*stride+4*x:][:4]
w := want.Pix[y*want.Stride+4*x:][:4]
d := 0
for c := range 4 {
d = max(d, absInt(int(g[c])-int(w[c])))
}
if d > worst {
worst, worstAt = d, image.Pt(x, y)
}
if d > 2 {
off++
}
}
}
if dir := os.Getenv("MYGO_GPU_PNG"); dir != "" {
g := &raster.Image{W: s.Width, H: s.Height, Stride: stride, Pix: got}
save(t, filepath.Join(dir, name+".png"), g)
save(t, filepath.Join(dir, name+"-cpu.png"), want)
}
// One pixel in two hundred may differ by more than 2 of 255, none by
// more than 24.
if worst > 24 || off*200 > s.Width*s.Height {
t.Errorf("%s: %d pixels differ from the CPU renderer's by more than 2, the most by %d at %v", name, off, worst, worstAt)
}
}
func save(t testing.TB, path string, m *raster.Image) {
f, err := os.Create(path)
if err != nil {
t.Fatal(err)
}
defer f.Close()
img := &image.RGBA{Pix: m.RGBA(), Stride: 4 * m.W, Rect: image.Rect(0, 0, m.W, m.H)}
if err := png.Encode(f, img); err != nil {
t.Fatal(fmt.Errorf("%s: %w", path, err))
}
}
func clamp(v float64) float64 { return max(0, min(1, v)) }
func absInt(v int) int {
if v < 0 {
return -v
}
return v
}
// ContinuousScene returns a 320×70 scene with continuous corners, which
// only the renderers of macOS draw (scene.Op.Continuous): a card with a
// border, its shadow and an inner one, a pill, a circle, which stays one,
// sides too short for their corners' curves, one with an inner shadow
// without blur, a corner reaching past the middle of its sides, a clip and
// an image, a shadow without blur, cut by the box casting it, and an effect
// reading its backdrop.
func ContinuousScene() *scene.Scene {
s := &scene.Scene{Width: 320, Height: 70, Clear: scene.Color{R: 236, G: 238, B: 242, A: 255}}
pix := make([]byte, 8*8*4)
for y := range 8 {
for x := range 8 {
i := (y*8 + x) * 4
pix[i], pix[i+1], pix[i+2], pix[i+3] = byte(x*32), byte(y*32), 160, 255
}
}
img := scene.NewImageRGBA(8, 8, pix)
red := scene.Color{R: 220, G: 40, B: 40, A: 255}
blue := scene.Color{R: 37, G: 99, B: 235, A: 255}
ink := scene.Color{R: 20, G: 24, B: 32, A: 255}
yellow := scene.Color{R: 250, G: 204, B: 21, A: 255}
r4 := func(r float32) [4]float32 { return [4]float32{r, r, r, r} }
add := func(op scene.Op) {
op.Continuous = true
s.Ops = append(s.Ops, op)
}
card := scene.Rect{X: 10, Y: 8, W: 70, H: 44}
add(scene.Op{Kind: scene.OpShadow, Rect: scene.Rect{X: 10, Y: 12, W: 70, H: 44}, Radii: r4(14), Color: scene.Color{A: 110}, Blur: 10,
Cast: card, CastRadii: r4(14)})
add(scene.Op{Kind: scene.OpFill, Rect: card, Radii: r4(14), Color: scene.Color{R: 255, G: 255, B: 255, A: 255},
Border: scene.Uniform(2), BorderColor: blue})
pad, padRadii := scene.InnerRadii(card, r4(14), scene.Uniform(2))
add(scene.Op{Kind: scene.OpShadow, Inset: true, Rect: scene.Rect{X: pad.X + 3, Y: pad.Y + 5, W: pad.W - 2, H: pad.H - 2}, Radii: [4]float32{11, 11, 11, 11},
Color: scene.Color{R: 220, G: 40, B: 40, A: 160}, Blur: 8, Cast: pad, CastRadii: padRadii})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 88.5, Y: 10.25, W: 70, H: 22}, Radii: r4(999), Color: red, Color2: blue,
Paint: scene.PaintLinear, Gradient: [4]float32{88, 10, 158, 32}})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 88, Y: 38, W: 24, H: 24}, Radii: r4(12), Color: ink})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 118, Y: 38, W: 40, H: 24}, Radii: r4(10), Color: blue, Border: scene.Uniform(1), BorderColor: ink})
add(scene.Op{Kind: scene.OpShadow, Inset: true, Rect: scene.Rect{X: 120, Y: 41, W: 36, H: 20}, Radii: r4(8), Color: yellow,
Cast: scene.Rect{X: 119, Y: 39, W: 38, H: 22}, CastRadii: r4(9)})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 166.5, Y: 8.25, W: 40, H: 54}, Radii: [4]float32{26, 6, 0, 0}, Color: red})
add(scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 214, Y: 6, W: 50, H: 58}, Radii: r4(16)})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 204, Y: 0, W: 70, H: 70}, Color: yellow})
add(scene.Op{Kind: scene.OpImage, Rect: scene.Rect{X: 222, Y: 14, W: 34, H: 34}, Radii: r4(9), Image: img, Src: scene.Rect{W: 8, H: 8}})
add(scene.Op{Kind: scene.OpPopClip})
card = scene.Rect{X: 272.5, Y: 10.25, W: 40, H: 40}
add(scene.Op{Kind: scene.OpShadow, Rect: scene.Rect{X: 275.5, Y: 14.25, W: 40, H: 40}, Radii: r4(12), Color: ink, Cast: card, CastRadii: r4(12)})
// An effect reading its backdrop, over the pill and the card.
fx := scene.EffectOp{Effect: LensEffect, Blur: 4}
fx.Params[0] = [4]float32{2}
fx.Params[1] = [4]float32{1, 1, 1, 0.35}
add(scene.Op{Kind: scene.OpEffect, Rect: scene.Rect{X: 60, Y: 18, W: 70, H: 30}, Radii: r4(15), Start: int32(len(s.Effects))})
s.Effects = append(s.Effects, fx)
return s
}
// ShadowCase is a panel with a stack of shadows cast by it, as an elevated
// surface has: its corner radius, the blur, spread and offset of each layer.
type ShadowCase struct {
Radius float32
Continuous bool
// Blur, Spread and DY describe each layer.
Blur, Spread, DY []float32
}
// ShadowPanelScene returns a 480×360 scene with a panel of 340×220 that casts
// the shadows of c, each shown only outside the panel (Cast), as kit surfaces
// draw them, and a fill over the panel.
func ShadowPanelScene(c ShadowCase) *scene.Scene {
s := &scene.Scene{Width: 480, Height: 360, Clear: scene.Color{R: 240, G: 241, B: 244, A: 255}}
panel := scene.Rect{X: 70, Y: 60, W: 340, H: 220}
radii := [4]float32{c.Radius, c.Radius, c.Radius, c.Radius}
for i := range c.Blur {
sp := c.Spread[i]
box := scene.Rect{X: panel.X - sp, Y: panel.Y - sp + c.DY[i], W: panel.W + 2*sp, H: panel.H + 2*sp}
s.Ops = append(s.Ops, scene.Op{Kind: scene.OpShadow, Rect: box, Radii: radii, Continuous: c.Continuous,
Color: scene.Color{A: 60}, Blur: c.Blur[i], Cast: panel, CastRadii: radii})
}
s.Ops = append(s.Ops, scene.Op{Kind: scene.OpFill, Rect: panel, Radii: radii, Continuous: c.Continuous,
Color: scene.Color{R: 255, G: 255, B: 255, A: 255}})
return s
}
// ShadowCases are panels of several radii, blurs, spreads and offsets,
// among them an offset larger than the blur and a shadow that does not reach
// out of the panel.
func ShadowCases() []ShadowCase {
return []ShadowCase{
{Radius: 0, Blur: []float32{2, 24}, Spread: []float32{0, 0}, DY: []float32{1, 12}},
{Radius: 12, Blur: []float32{2, 6, 12, 24}, Spread: []float32{0, -1, -3, -6}, DY: []float32{1, 3, 6, 12}},
{Radius: 40, Blur: []float32{16}, Spread: []float32{0}, DY: []float32{4}},
{Radius: 16, Continuous: true, Blur: []float32{12, 30}, Spread: []float32{2, 0}, DY: []float32{3, 8}},
{Radius: 8, Blur: []float32{4}, Spread: []float32{-2}, DY: []float32{40}},
{Radius: 60, Blur: []float32{8, 0}, Spread: []float32{0, 5}, DY: []float32{0, 0}},
{Radius: 30, Blur: []float32{60}, Spread: []float32{-40}, DY: []float32{-10}},
{Radius: 110, Blur: []float32{80, 6}, Spread: []float32{20, 0}, DY: []float32{0, 2}}, // a pill
}
}
// RingCase is a scene of border-only fills (a transparent solid color and a
// border), over content that shows when one of them wrongly changes a pixel.
type RingCase struct {
Name string
Scene *scene.Scene
// Split is whether the builder draws the first ring around its core.
Split bool
}
// RingCases are border-only fills at the size of panels (the shaders shade
// their cores for nothing unless the builder draws around them): square,
// round and continuous corners, uneven widths and radii, a dashed border,
// fractional places, borders under clips, translucent and opaque border
// colors, at half opacity, one with a border wider than half the panel,
// and fills that are not split: a gradient, a translucent color, a
// transformed fill, a small one and a thin one.
func RingCases() []RingCase {
blue := scene.Color{R: 37, G: 99, B: 235, A: 255}
red := scene.Color{R: 220, G: 40, B: 40, A: 255}
paper := scene.Color{R: 246, G: 247, B: 249, A: 255}
r4 := func(r float32) [4]float32 { return [4]float32{r, r, r, r} }
// content returns a 480×360 scene of fills, a shadow and a glyph run,
// which the ring is drawn over, and the clip to push, if any.
content := func() *scene.Scene {
s := &scene.Scene{Width: 480, Height: 360, Clear: scene.Color{R: 30, G: 34, B: 40, A: 255},
Text: scene.TextParams{GammaRatios: scene.GammaRatios(1.8), Contrast: 1, SubpixelContrast: 0.5}}
s.MaskAtlas, s.Text, s.Glyphs = glyphContent()
s.Ops = append(s.Ops,
scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 10, Y: 10, W: 460, H: 340}, Radii: r4(20), Color: paper},
scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 100, Y: 80, W: 200, H: 120}, Color: blue, Color2: red, Paint: scene.PaintLinear, Gradient: [4]float32{100, 0, 300, 0}},
scene.Op{Kind: scene.OpShadow, Rect: scene.Rect{X: 160, Y: 160, W: 160, H: 100}, Radii: r4(12), Color: scene.Color{A: 120}, Blur: 24})
s.Ops = append(s.Ops, scene.Op{Kind: scene.OpGlyphs, Start: 0, End: int32(len(s.Glyphs))})
return s
}
var out []RingCase
add := func(name string, split bool, clip *scene.Op, ops ...scene.Op) {
s := content()
if clip != nil {
s.Ops = append(s.Ops, *clip)
}
s.Ops = append(s.Ops, ops...)
if clip != nil {
s.Ops = append(s.Ops, scene.Op{Kind: scene.OpPopClip})
}
out = append(out, RingCase{name, s, split})
}
ring := func(r scene.Rect, radii [4]float32, w [4]float32, c scene.Color) scene.Op {
return scene.Op{Kind: scene.OpFill, Rect: r, Radii: radii, Border: w, BorderColor: c}
}
panel := scene.Rect{X: 20, Y: 20, W: 440, H: 320}
half := scene.Color{R: 255, G: 255, B: 255, A: 90}
add("square 1px", true, nil, ring(panel, [4]float32{}, scene.Uniform(1), half))
add("square 2px opaque", true, nil, ring(panel, [4]float32{}, scene.Uniform(2), red))
add("rounded", true, nil, ring(panel, r4(16), scene.Uniform(2), half))
add("continuous", true, nil, func() scene.Op {
op := ring(panel, r4(24), scene.Uniform(2), half)
op.Continuous = true
return op
}())
add("uneven widths and radii", true, nil, ring(panel, [4]float32{30, 0, 8, 20}, [4]float32{1, 3, 2, 5}, half))
add("uneven continuous", true, nil, func() scene.Op {
op := ring(panel, [4]float32{30, 6, 8, 20}, [4]float32{3, 1, 4, 2}, half)
op.Continuous = true
return op
}())
add("fractional place", true, nil, ring(scene.Rect{X: 20.5, Y: 20.25, W: 439.3, H: 320.6}, r4(12), scene.Uniform(1.5), half))
add("dashed", true, nil, func() scene.Op {
op := ring(panel, r4(10), scene.Uniform(2), blue)
op.Dashed = true
return op
}())
add("half opacity", true, nil, func() scene.Op {
op := ring(panel, r4(10), scene.Uniform(3), blue)
op.Opacity = 0.5
return op
}())
add("wide border", true, nil, ring(panel, r4(40), scene.Uniform(60), half))
add("clipped", true, &scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 30, Y: 30, W: 300, H: 280}}, ring(panel, r4(16), scene.Uniform(2), half))
add("fractional clip", true, &scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 30.5, Y: 30.25, W: 300.4, H: 280.7}}, ring(panel, r4(16), scene.Uniform(2), half))
add("rounded clip", true, &scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 30, Y: 30, W: 300, H: 280}, Radii: r4(30)}, ring(panel, r4(16), scene.Uniform(2), half))
add("frame edges", true, nil, ring(scene.Rect{X: -10, Y: -10, W: 500, H: 380}, r4(12), scene.Uniform(2), half))
add("two rings", true, nil, ring(panel, r4(16), scene.Uniform(2), half), ring(scene.Rect{X: 30, Y: 30, W: 420, H: 300}, r4(8), scene.Uniform(1), red))
add("circle", true, nil, ring(scene.Rect{X: 90.5, Y: 30.25, W: 300, H: 300}, r4(150), scene.Uniform(1), half))
add("pill", true, nil, ring(scene.Rect{X: 40, Y: 120, W: 400, H: 100}, r4(50), scene.Uniform(2), red))
add("uneven round", true, nil, ring(panel, [4]float32{150, 20, 90, 0}, [4]float32{1, 3, 2, 5}, half))
// Grooves rippling out of a record across a card, as a music app's:
// circles a pixel wide or less, most larger than the card, clipped by it.
func() {
var grooves []scene.Op
for i := range 12 {
rad := 30 + float32(i)*50
w := float32(1)
if i%2 == 1 {
w = 0.7
}
grooves = append(grooves, ring(scene.Rect{X: 360 - rad, Y: 180.5 - rad, W: 2 * rad, H: 2 * rad}, r4(rad), scene.Uniform(w), half))
}
add("grooves", true, &scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 20, Y: 60, W: 440, H: 240}, Radii: r4(16)}, grooves...)
}()
add("a fill with a border", false, nil, func() scene.Op {
op := ring(panel, r4(16), scene.Uniform(2), half)
op.Color = scene.Color{R: 255, G: 255, B: 255, A: 1}
return op
}())
add("gradient", false, nil, func() scene.Op {
op := ring(panel, r4(16), scene.Uniform(2), half)
op.Paint, op.Color, op.Color2, op.Gradient = scene.PaintLinear, scene.Color{A: 0}, scene.Color{R: 255, A: 200}, [4]float32{20, 0, 460, 0}
return op
}())
add("transformed", false, nil, func() scene.Op {
op := ring(panel, r4(16), scene.Uniform(2), half)
op.Transform = scene.Scaling(0.98, 0.98)
return op
}())
add("small", false, nil, ring(scene.Rect{X: 100, Y: 100, W: 40, H: 30}, r4(6), scene.Uniform(1), half))
add("thin", false, nil, ring(scene.Rect{X: 20, Y: 20, W: 440, H: 6}, r4(2), scene.Uniform(1), half))
add("border fills the panel", false, nil, ring(scene.Rect{X: 20, Y: 20, W: 100, H: 100}, [4]float32{}, scene.Uniform(60), half))
return out
}
// OpaqueScene returns a w×h scene (at least 600×500) of opaque fills large
// enough for the builder to draw as cores without blending, over content that
// shows if one is wrong: translucent shadows, glyphs and half transparent
// fills under them, and fills with square, round and continuous corners, a
// translucent border, fractional places, edges at the frame's, rectangular
// clips with whole and fractional edges, and some that are not cores (half
// opacity, a gradient, a rounded clip, a transformed fill).
func OpaqueScene(w, h int) *scene.Scene {
s := &scene.Scene{Width: w, Height: h, Clear: scene.Color{R: 30, G: 34, B: 40, A: 255}}
s.MaskAtlas, s.Text, s.Glyphs = glyphContent()
fw, fh := float32(w), float32(h)
r4 := func(r float32) [4]float32 { return [4]float32{r, r, r, r} }
add := func(op scene.Op) { s.Ops = append(s.Ops, op) }
color := func(i int) scene.Color {
return scene.Color{R: byte(20 + i*37%200), G: byte(10 + i*53%220), B: byte(40 + i*71%200), A: 255}
}
under := func(x, y float32) {
add(scene.Op{Kind: scene.OpShadow, Rect: scene.Rect{X: x, Y: y, W: 160, H: 100}, Radii: r4(12), Color: scene.Color{A: 140}, Blur: 20})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: x + 20, Y: y + 20, W: 200, H: 80}, Color: scene.Color{R: 255, G: 90, B: 20, A: 160}})
add(scene.Op{Kind: scene.OpGlyphs, Start: 0, End: int32(len(s.Glyphs))})
}
under(fw/6, 80)
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{W: fw, H: fh}, Color: color(1)})
under(fw/6, 100)
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 40, Y: 30.25, W: fw - 80.5, H: fh - 60}, Radii: r4(24), Continuous: true, Color: color(2),
Border: scene.Uniform(2), BorderColor: scene.Color{R: 255, G: 255, B: 255, A: 70}})
under(fw/3, 200)
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 60.5, Y: 70, W: fw - 140, H: fh - 140.5}, Color: color(3)})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 100, Y: 90, W: fw/2 + 0.5, H: fh/2 - 3.25}, Radii: r4(12), Color: color(4)})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: fw / 3, Y: 120, W: fw / 2, H: fh / 2}, Radii: [4]float32{40, 0, 16, 5}, Color: color(5)})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: -5, Y: fh - 130, W: fw + 10, H: 130}, Color: color(6)})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 0, Y: 0, W: 150, H: fh}, Color: color(7)})
add(scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 200, Y: 150, W: fw - 300, H: fh - 250}})
under(260, 200)
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 190, Y: 140, W: fw - 280, H: fh - 230}, Color: color(8)})
add(scene.Op{Kind: scene.OpPopClip})
add(scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 210.5, Y: 160.25, W: fw - 320.5, H: fh - 270.25}})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 190, Y: 140, W: fw - 280, H: fh - 230}, Color: color(9)})
add(scene.Op{Kind: scene.OpPopClip})
add(scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 220, Y: 170, W: fw - 340, H: fh - 290}, Radii: r4(30)})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 190, Y: 140, W: fw - 280, H: fh - 230}, Color: color(10)})
add(scene.Op{Kind: scene.OpPopClip})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 250, Y: 200, W: 300, H: 200}, Color: color(11), Opacity: 0.5})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 250, Y: 200, W: 300, H: 200}, Color: color(12), Color2: color(13), Paint: scene.PaintLinear, Gradient: [4]float32{250, 0, 550, 0}})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 260, Y: 210, W: 280, H: 180}, Color: color(14), Transform: scene.Scaling(1.01, 1)})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 300, Y: 250, W: 120, H: 90}, Color: color(15)}) // small: not a core
return s
}
// CullCase is a scene of ops, some of them under later opaque fills, and how
// many of them a renderer may leave out, which must change no pixel. The
// identity tests use it as a named scene.
type CullCase struct {
Name string
Scene *scene.Scene
// Culled is how many ops the shared builder leaves out.
Culled int
}
// glyphContent returns a mask atlas holding a disc, the text parameters, and a run
// of twelve glyphs of it, for scenes that draw text.
func glyphContent() (*scene.Atlas, scene.TextParams, []scene.Glyph) {
atlas := scene.NewAtlas(1, 64, 64)
disc := make([]byte, 16*16)
for y := range 16 {
for x := range 16 {
dx, dy := float64(x)-7.5, float64(y)-7.5
disc[y*16+x] = byte(255 * clamp(7.5-math.Sqrt(dx*dx+dy*dy)))
}
}
x, y, _ := atlas.Alloc(16, 16)
atlas.Put(x, y, 16, 16, disc, 16)
var glyphs []scene.Glyph
for i := range 12 {
glyphs = append(glyphs, scene.Glyph{X: 40 + float32(i*16), Y: 120, W: 16, H: 16, U: uint16(x), V: uint16(y), UW: 16, VH: 16, Color: scene.Color{A: 255}})
}
return atlas, scene.TextParams{GammaRatios: scene.GammaRatios(1.8), Contrast: 1, SubpixelContrast: 0.5}, glyphs
}
// Transformed returns a copy of s with a one pixel transformed fill drawn
// first, which makes the builder give rounded and rotated clips masks (a scene
// with any transformed op has them), so that every case also runs with masked
// clips.
func Transformed(s *scene.Scene) *scene.Scene {
c := *s
c.Ops = append([]scene.Op{{Kind: scene.OpFill, Rect: scene.Rect{X: 1, Y: 1, W: 3, H: 3}, Color: scene.Color{R: 200, A: 255}, Transform: scene.Scaling(1.01, 1)}}, s.Ops...)
return &c
}
// MaskedCase is a named scene whose clips are masked.
type MaskedCase struct {
Name string
Scene *scene.Scene
}
// MaskedCases are scenes with rounded, continuous and rotated clips (which
// are masks in a scene with a transformed op) holding what the builder
// draws in pieces or as cores: a border-only ring, a shadow cast by a box, large
// opaque fills, plain fills, glyphs and holes, with content after them in the
// same clip that reaches the clip's corners.
func MaskedCases() []MaskedCase {
r4 := func(r float32) [4]float32 { return [4]float32{r, r, r, r} }
build := func(name string, clip scene.Op, inner func(add func(scene.Op))) MaskedCase {
s := &scene.Scene{Width: 600, Height: 500, Clear: scene.Color{R: 240, G: 240, B: 240, A: 255}}
s.MaskAtlas, s.Text, s.Glyphs = glyphContent()
s.Ops = append(s.Ops, scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 5, Y: 5, W: 10, H: 10}, Color: scene.Color{R: 200, A: 255}, Transform: scene.Scaling(1.01, 1)})
s.Ops = append(s.Ops, scene.Op{Kind: scene.OpFill, Rect: scene.Rect{W: 600, H: 500}, Color: scene.Color{R: 90, G: 120, B: 200, A: 255}})
clip.Kind = scene.OpPushClip
s.Ops = append(s.Ops, clip)
inner(func(op scene.Op) { s.Ops = append(s.Ops, op) })
s.Ops = append(s.Ops, scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 0, Y: 0, W: 600, H: 120}, Color: scene.Color{G: 160, A: 200}},
scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 0, Y: 400, W: 600, H: 100}, Color: scene.Color{R: 160, A: 255}},
scene.Op{Kind: scene.OpGlyphs, Start: 0, End: int32(len(s.Glyphs))},
scene.Op{Kind: scene.OpPopClip})
return MaskedCase{name, s}
}
ring := func(add func(scene.Op)) {
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 30, Y: 20, W: 540, H: 460}, Radii: r4(20), Border: scene.Uniform(3), BorderColor: scene.Color{B: 200, A: 255}})
}
shadow := func(add func(scene.Op)) {
add(scene.Op{Kind: scene.OpShadow, Rect: scene.Rect{X: 60, Y: 60, W: 400, H: 300}, Radii: r4(12), Color: scene.Color{A: 120}, Blur: 20,
Cast: scene.Rect{X: 60, Y: 60, W: 400, H: 300}, CastRadii: r4(12)})
}
opaque := func(add func(scene.Op)) {
add(scene.Op{Kind: scene.OpHole, Rect: scene.Rect{X: 200, Y: 200, W: 40, H: 40}})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 20, Y: 20, W: 560, H: 460}, Color: scene.Color{R: 250, G: 250, B: 250, A: 255}})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 50, Y: 50, W: 300, H: 300}, Radii: r4(16), Color: scene.Color{R: 30, G: 200, B: 90, A: 255}})
add(scene.Op{Kind: scene.OpHole, Rect: scene.Rect{X: 100, Y: 100, W: 40, H: 40}})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 80, Y: 80, W: 200, H: 200}, Color: scene.Color{R: 10, B: 90, A: 255}})
}
all := func(add func(scene.Op)) { shadow(add); ring(add); opaque(add) }
rounded := scene.Op{Rect: scene.Rect{X: 40, Y: 30, W: 520, H: 440}, Radii: r4(40)}
continuous := scene.Op{Rect: scene.Rect{X: 40, Y: 30, W: 520, H: 440}, Radii: r4(40), Continuous: true}
rotated := scene.Op{Rect: scene.Rect{X: 40, Y: 30, W: 520, H: 440}, Transform: scene.Rotation(3)}
return []MaskedCase{
build("masked ring", rounded, ring), build("masked shadow", rounded, shadow), build("masked opaque", rounded, opaque),
build("masked continuous", continuous, all), build("masked rotated", rotated, all), build("masked everything", rounded, all),
}
}
// WideScene returns a 600×500 scene with colors outside the sRGB gamut
// (Scene.Wide, Op.Wide, Glyph.Wide) in large opaque fills, a ring, a gradient
// glyph run inside a continuous rounded clip, and plain fills, which the
// float16 targets of Metal keep and the others draw as their nearest sRGB
// colors.
func WideScene() *scene.Scene {
r4 := func(r float32) [4]float32 { return [4]float32{r, r, r, r} }
s := &scene.Scene{Width: 600, Height: 500, Clear: scene.Color{R: 30, G: 34, B: 40, A: 255}}
s.MaskAtlas, s.Text, s.Glyphs = glyphContent()
s.Wide = []scene.WideColors{
{Color: [4]float32{-0.25, 1.1, -0.1, 1}, Set: scene.WideColor},
{Color: [4]float32{1.15, -0.2, -0.1, 1}, Color2: [4]float32{-0.2, 0.3, 1.2, 1}, Set: scene.WideColor | scene.WideColor2},
{Border: [4]float32{1.2, 0.2, -0.1, 0.5}, Set: scene.WideBorder},
{Color: [4]float32{0.2, 1.2, 0.3, 0.5}, Set: scene.WideColor},
}
green, red := scene.Color{G: 255, A: 255}, scene.Color{R: 255, A: 255}
s.Glyphs = append([]scene.Glyph(nil), s.Glyphs...)
for i := range s.Glyphs {
s.Glyphs[i].Wide = 1
}
s.Ops = append(s.Ops,
scene.Op{Kind: scene.OpFill, Rect: scene.Rect{W: 600, H: 500}, Color: green, Wide: 1},
scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 30, Y: 30, W: 540, H: 440}, Radii: r4(24), Continuous: true, Color: green, Wide: 1,
Border: scene.Uniform(3), BorderColor: scene.Color{R: 255, A: 255}},
scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 20, Y: 20, W: 560, H: 460}, Radii: r4(16), Border: scene.Uniform(2), BorderColor: scene.Color{B: 255, A: 200}, Wide: 3},
scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 100, Y: 100, W: 300, H: 200}, Paint: scene.PaintOklab, Gradient: [4]float32{100, 0, 400, 0}, Color: green, Color2: red, Wide: 2},
scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 150, Y: 150, W: 300, H: 250}, Color: scene.Color{R: 60, G: 60, B: 255, A: 128}, Wide: 4},
scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 60, Y: 60, W: 400, H: 300}, Radii: r4(30), Continuous: true},
scene.Op{Kind: scene.OpGlyphs, Start: 0, End: int32(len(s.Glyphs)), Paint: scene.PaintOklab, Gradient: [4]float32{40, 0, 240, 0}, Color: green, Color2: red, Wide: 2},
scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 70, Y: 200, W: 380, H: 100}, Color: red, Wide: 1},
scene.Op{Kind: scene.OpPopClip})
return s
}
// WithoutNewPaints returns s without what shaders older than gradients of
// stops draw differently: ops painting with stops (scene.Stop), borders
// painted with a gradient (Op.BorderPaint) and images blurred, for tests
// that compare a renderer with an earlier copy of its shader.
func WithoutNewPaints(s *scene.Scene) *scene.Scene {
out := *s
out.Ops = nil
for _, op := range s.Ops {
if op.Paint.HasStops() || op.BorderPaint || op.Kind == scene.OpImage && op.Blur > 0 {
continue
}
out.Ops = append(out.Ops, op)
}
return &out
}
// Continuously returns a copy of s whose ops all have continuous corners
// (scene.Op.Continuous): fills, shadows, images, holes, effects and clips.
func Continuously(s *scene.Scene) *scene.Scene {
c := *s
c.Ops = append([]scene.Op(nil), s.Ops...)
for i := range c.Ops {
c.Ops[i].Continuous = true
}
return &c
}
// CardsScene returns a w×h scene of cards with continuous corners (as
// macOS draws them) at the sizes of panels, which the builder draws as
// nine-patches: each casting a soft shadow and a tight one, at places
// between pixels, with radii from 4 to 60 and uneven ones; a card with a
// border, a dashed border, a gradient, a translucent color, a border
// only, a large image, an inner shadow, a hard shadow, a shadow offset
// beyond its blur, one under a rounded clip and one under a continuous
// clip.
func CardsScene(w, h int) *scene.Scene {
s := &scene.Scene{Width: w, Height: h, Clear: scene.Color{R: 240, G: 241, B: 244, A: 255}}
pix := make([]byte, 32*32*4)
for y := range 32 {
for x := range 32 {
i := (y*32 + x) * 4
pix[i], pix[i+1], pix[i+2], pix[i+3] = byte(x*8), byte(y*8), 160, 255
}
}
img := scene.NewImageRGBA(32, 32, pix)
r4 := func(r float32) [4]float32 { return [4]float32{r, r, r, r} }
radii := [][4]float32{r4(4), r4(12), r4(24), r4(60), {30, 8, 0, 16}, r4(18), r4(999), r4(10), r4(36), {0, 40, 40, 0}, r4(16), r4(20)}
cols := 4
cw, ch := (float32(w)-40)/float32(cols)-30, (float32(h)-40)/3-30
white := scene.Color{R: 255, G: 255, B: 255, A: 255}
blue := scene.Color{R: 37, G: 99, B: 235, A: 255}
for i, r := range radii {
card := scene.Rect{X: 35 + float32(i%cols)*(cw+30) + float32(i%3)*0.25, Y: 35 + float32(i/cols)*(ch+30) + float32(i%5)*0.2, W: cw, H: ch}
if i == 6 {
card.H = ch / 2 // a pill
}
sh := card
sh.Y += 4
blur := float32(24)
switch i {
case 7:
blur = 0 // hard
case 8:
sh.Y += 40 // offset beyond the blur
blur = 8
}
fill := scene.Op{Kind: scene.OpFill, Rect: card, Radii: r, Color: white, Continuous: true}
switch i {
case 1:
fill.Border, fill.BorderColor = scene.Uniform(1.5), blue
case 2:
fill.Border, fill.BorderColor, fill.Dashed = scene.Uniform(2), blue, true
case 3:
fill.Paint, fill.Color2, fill.Gradient = scene.PaintLinear, blue, [4]float32{card.X, card.Y, card.X + card.W, card.Y + card.H}
case 4:
fill.Color = scene.Color{R: 255, G: 255, B: 255, A: 160}
case 5:
fill.Color, fill.Border, fill.BorderColor = scene.Color{}, [4]float32{1, 3, 2, 4}, blue
}
if i == 10 {
s.Ops = append(s.Ops, scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: card.X - 20, Y: card.Y - 10, W: card.W + 40, H: card.H}, Radii: r4(30)})
}
if i == 11 {
s.Ops = append(s.Ops, scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: card.X + 10, Y: card.Y - 30, W: card.W, H: card.H}, Radii: r4(30), Continuous: true})
}
// The soft shadow is opaque, so that the least difference shows.
s.Ops = append(s.Ops,
scene.Op{Kind: scene.OpShadow, Rect: sh, Radii: r, Color: scene.Color{A: 255}, Blur: blur, Cast: card, CastRadii: r, Continuous: true},
scene.Op{Kind: scene.OpShadow, Rect: card, Radii: r, Color: scene.Color{A: 40}, Blur: 3, Cast: card, CastRadii: r, Continuous: true},
fill)
switch i {
case 0:
// On whole pixels: the CPU renderer samples images between them
// differently.
in := scene.Rect{X: float32(math.Round(float64(card.X + 20))), Y: float32(math.Round(float64(card.Y + 20))),
W: float32(math.Round(float64(card.W - 40))), H: float32(math.Round(float64(card.H - 40)))}
s.Ops = append(s.Ops, scene.Op{Kind: scene.OpImage, Rect: in, Radii: r4(20), Image: img, Src: scene.Rect{W: 32, H: 32}, Continuous: true})
case 9:
in := scene.Rect{X: card.X + 10, Y: card.Y + 10, W: card.W - 20, H: card.H - 20}
s.Ops = append(s.Ops, scene.Op{Kind: scene.OpShadow, Inset: true, Rect: in, Radii: r4(12), Color: scene.Color{R: 220, G: 40, B: 40, A: 160},
Blur: 16, Cast: card, CastRadii: r, Continuous: true})
case 10, 11:
s.Ops = append(s.Ops, scene.Op{Kind: scene.OpPopClip})
}
}
return s
}
+107
View File
@@ -0,0 +1,107 @@
package gputest
import (
"math"
"github.com/egoist/mygo/internal/scene"
)
// MaskedClipScene has clips masked by gradients, linear of two colors and
// of stops, radial and conic, and by an image (a path's coverage),
// with fills, a shadow and an image inside, a clip within a masked one,
// rounded or not, a masked clip turned, and one only moved, by whole
// pixels and between them, which renderers fold; all of them again,
// scaled, under the reference scene's size (Scene); and part of an image
// over a rounded clip.
func MaskedClipScene() *scene.Scene {
scene.FoldTranslations() // as package ui does
s := &scene.Scene{Width: 320, Height: 740, Clear: scene.Color{R: 246, G: 247, B: 249, A: 255}}
add := func(ops ...scene.Op) { s.Ops = append(s.Ops, ops...) }
r4 := func(v float32) [4]float32 { return [4]float32{v, v, v, v} }
red, blue := scene.Color{R: 220, G: 40, B: 40, A: 255}, scene.Color{R: 40, G: 60, B: 220, A: 255}
black, clear := scene.Color{A: 255}, scene.Color{}
stops := func(st ...scene.Stop) [2]int32 {
a := int32(len(s.Stops))
s.Stops = append(s.Stops, st...)
return [2]int32{a, int32(len(s.Stops))}
}
content := func(x, y float32, m scene.Affine) {
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: x, Y: y, W: 90, H: 80}, Color: red, Transform: m},
scene.Op{Kind: scene.OpShadow, Rect: scene.Rect{X: x + 20, Y: y + 18, W: 40, H: 30}, Radii: r4(6), Blur: 8, Color: scene.Color{A: 160}, Transform: m},
scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: x + 20, Y: y + 15, W: 50, H: 40}, Radii: r4(10), Color: blue,
Border: scene.Uniform(2), BorderColor: scene.Color{R: 250, G: 250, B: 250, A: 255}, Transform: m})
}
// Two colors along a line, faded out to the right.
add(scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 10, Y: 10, W: 90, H: 80}, Paint: scene.PaintLinear,
Color: black, Color2: clear, Gradient: [4]float32{20, 0, 90, 0}})
content(10, 10, scene.Affine{})
add(scene.Op{Kind: scene.OpPopClip})
// Stops fading the top and the bottom, in a rounded clip, with a
// clip inside, and a rounded one inside that.
add(scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 110, Y: 10, W: 90, H: 80}, Radii: r4(14), Paint: scene.PaintLinearStops,
Gradient: [4]float32{0, 10, 0, 90}, Stops: stops(scene.Stop{Color: clear, At: 0}, scene.Stop{Color: black, At: 0.25},
scene.Stop{Color: black, At: 0.75}, scene.Stop{Color: clear, At: 1})})
add(scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 115, Y: 5, W: 70, H: 95}})
content(110, 10, scene.Affine{})
add(scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 150, Y: 40, W: 40, H: 40}, Radii: r4(12)})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 140, Y: 30, W: 60, H: 60}, Color: scene.Color{G: 160, B: 90, A: 255}})
add(scene.Op{Kind: scene.OpPopClip}, scene.Op{Kind: scene.OpPopClip}, scene.Op{Kind: scene.OpPopClip})
// Radial and conic.
add(scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 210, Y: 10, W: 90, H: 80}, Paint: scene.PaintRadial,
Gradient: [4]float32{255, 50, 45, 0}, Stops: stops(scene.Stop{Color: black, At: 0.4}, scene.Stop{Color: clear, At: 1})})
content(210, 10, scene.Affine{})
add(scene.Op{Kind: scene.OpPopClip})
add(scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 10, Y: 100, W: 90, H: 80}, Paint: scene.PaintConic,
Gradient: [4]float32{55, 140, 0.3, 0}, Stops: stops(scene.Stop{Color: clear, At: 0}, scene.Stop{Color: black, At: 1})})
content(10, 100, scene.Affine{})
add(scene.Op{Kind: scene.OpPopClip})
// A disc's coverage, as a path's mask, with an image inside.
const d = 64
pix := make([]byte, 4*d*d)
for y := range d {
for x := range d {
dx, dy := float64(x)+0.5-d/2, float64(y)+0.5-d/2
pix[4*(y*d+x)+3] = byte(255 * clamp(d/2-1-math.Sqrt(dx*dx+dy*dy)))
}
}
disc := scene.NewImageRGBA(d, d, pix)
img := make([]byte, 4*8*8)
for i := 0; i < len(img); i += 4 {
img[i], img[i+1], img[i+2], img[i+3] = byte(i*4), 120, byte(255-i*4), 255
}
add(scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 120, Y: 108, W: d, H: d}, Image: disc, Src: scene.Rect{W: d, H: d}})
content(110, 100, scene.Affine{})
add(scene.Op{Kind: scene.OpImage, Rect: scene.Rect{X: 160, Y: 140, W: 40, H: 40}, Image: scene.NewImageRGBA(8, 8, img), Src: scene.Rect{W: 8, H: 8}})
add(scene.Op{Kind: scene.OpPopClip})
// Turned.
turn := scene.Translation(255, 140).Mul(scene.Rotation(12)).Mul(scene.Translation(-255, -140))
add(scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 215, Y: 105, W: 80, H: 70}, Radii: r4(10), Paint: scene.PaintLinear,
Color: clear, Color2: black, Gradient: [4]float32{215, 105, 295, 175}, Transform: turn})
content(210, 100, turn)
add(scene.Op{Kind: scene.OpPopClip, Transform: turn})
// Moved by whole pixels, and between them, the gradient and the image
// with the clip.
for i, m := range []scene.Affine{scene.Translation(3, 2), scene.Translation(-4.25, 1.75)} {
x := 10 + 110*float32(i)
add(scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: x, Y: 200, W: 90, H: 80}, Paint: scene.PaintLinearStops,
Gradient: [4]float32{x, 0, x + 90, 0}, Stops: stops(scene.Stop{Color: clear, At: 0.1}, scene.Stop{Color: black, At: 0.6}), Transform: m})
content(x, 200, m)
add(scene.Op{Kind: scene.OpPopClip, Transform: m})
}
add(scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 230, Y: 208, W: d, H: d}, Image: disc, Src: scene.Rect{W: d, H: d}, Transform: scene.Translation(2.5, -1.25)})
content(220, 200, scene.Translation(2.5, -1.25))
add(scene.Op{Kind: scene.OpPopClip, Transform: scene.Translation(2.5, -1.25)})
// Scaled, the masks with the clips.
n, grow := len(s.Ops), scene.Translation(0, 300).Mul(scene.Scaling(1.2, 1.4))
for _, op := range s.Ops[:n] {
op.Transform = grow.Mul(op.Transform)
add(op)
}
// Part of an image's alpha over a rounded clip, as an element's
// layers of masks under its ClipInset (ui's Masks): the disc's middle,
// stretched.
add(scene.Op{Kind: scene.OpPushClip, Rect: scene.Rect{X: 10, Y: 696, W: 90, H: 40}, Radii: r4(8), Image: disc, Src: scene.Rect{X: 8, Y: 16, W: 48, H: 32}})
add(scene.Op{Kind: scene.OpFill, Rect: scene.Rect{X: 0, Y: 690, W: 110, H: 50}, Color: red})
add(scene.Op{Kind: scene.OpPopClip})
return s
}
+51
View File
@@ -0,0 +1,51 @@
package gputest
import "github.com/egoist/mygo/internal/scene"
// SpriteScene returns a w×h frame of a 2D game: layers of tiles of 16
// pixels drawn scale times as large, pixelated, from one sheet, covering
// the frame, and sprites of 16×32 over them, all ops of one image. With
// scale 3 at 1920×1080, three layers and 300 sprites, it draws 1,872
// sprites, about what a top-down farming game draws.
func SpriteScene(w, h int, scale float32, layers, sprites int) *scene.Scene {
s := &scene.Scene{Width: w, Height: h, Clear: scene.Color{R: 40, G: 44, B: 52, A: 255}}
// A sheet of 8×8 tiles of 16 pixels, each of its own colors.
const tiles, tile = 8, 16
pix := make([]byte, tiles*tile*tiles*tile*4)
for y := range tiles * tile {
for x := range tiles * tile {
i := (y*tiles*tile + x) * 4
tx, ty := x/tile, y/tile
pix[i] = byte(tx*32 + x%tile*2)
pix[i+1] = byte(ty*32 + y%tile*2)
pix[i+2] = byte((tx + ty) * 16)
pix[i+3] = 255
}
}
sheet := scene.NewImageRGBA(tiles*tile, tiles*tile, pix)
size := tile * scale
cols, rows := int(float32(w)/size)+1, int(float32(h)/size)+1
for l := range layers {
for r := range rows {
for c := range cols {
k := (r*cols + c + l*7) % (tiles * tiles)
src := scene.Rect{X: float32(k%tiles) * tile, Y: float32(k/tiles) * tile, W: tile, H: tile}
if l > 0 && (r+c+l)%3 != 0 {
continue // upper layers cover a third
}
s.Ops = append(s.Ops, scene.Op{Kind: scene.OpImage, Rect: scene.Rect{X: float32(c) * size, Y: float32(r) * size, W: size, H: size},
Image: sheet, Src: src, Pixelated: true})
}
}
}
for i := range sprites {
x := float32(i*97%max(w-int(size), 1)) + 0.5
y := float32(i*61%max(h-int(2*size), 1)) + 0.25
src := scene.Rect{X: float32(i%tiles) * tile, Y: 0, W: tile, H: 2 * tile}
if i%2 == 1 {
src.X, src.W = src.X+src.W, -src.W // facing left
}
s.Ops = append(s.Ops, scene.Op{Kind: scene.OpImage, Rect: scene.Rect{X: x, Y: y, W: size, H: 2 * size}, Image: sheet, Src: src, Pixelated: true})
}
return s
}
+107
View File
@@ -0,0 +1,107 @@
package gputest
import (
"math"
"github.com/egoist/mygo/internal/scene"
)
// TransformedScene exercises all primitives and effects under affine
// composition, with nested transformed clips and an inline glyph transform.
func TransformedScene() *scene.Scene {
s := Scene()
s.Clear = scene.Color{R: 100, G: 120, B: 160, A: 80}
m := scene.Translation(25, 20).Mul(scene.Rotation(4)).Mul(scene.Scaling(0.8, 0.9))
for i := range s.Ops {
s.Ops[i].Transform = m
}
if len(s.Glyphs) > 0 {
s.Glyphs[0].Transform = scene.Rotation(15)
}
return s
}
// PerspectiveScene exercises all primitives and effects seen in
// perspective: the scene turned 20 degrees about its vertical center line,
// seen from 900 pixels, as RotationY and Perspective make it, with a
// skewed glyph and a nested clip turned the other way.
func PerspectiveScene() *scene.Scene {
s := Scene()
s.Clear = scene.Color{R: 100, G: 120, B: 160, A: 80}
turn := func(degrees, d, cx, cy float32) scene.Affine {
sin, cos := math.Sincos(float64(degrees) * math.Pi / 180)
m := scene.Projective(float32(cos), 0, 0, 1, 0, 0, float32(sin)/d, 0)
return scene.Translation(cx, cy).Mul(m).Mul(scene.Translation(-cx, -cy))
}
m := turn(20, 900, float32(s.Width)/2, float32(s.Height)/2)
for i := range s.Ops {
s.Ops[i].Transform = m
}
if len(s.Glyphs) > 0 {
s.Glyphs[0].Transform = scene.Matrix(1, 0, 0.3, 1, 0, 0)
}
return s
}
// BehindOriginScene is a card far from its drawing's origin, turned over
// in perspective around its center, which the frame shows at (160, 370):
// the origin is behind the viewer, past the turned plane's horizon
// (scene.Affine.Behind), the card in front (#1406). A rounded clip, its
// shadow, a border and a gradient turn with it; red shows at the frame's
// center.
func BehindOriginScene() *scene.Scene {
s := &scene.Scene{Width: 320, Height: 740, Clear: scene.Color{R: 246, G: 247, B: 249, A: 255}}
sin, cos := math.Sincos(-60 * math.Pi / 180)
turn := scene.Projective(1, 0, 0, float32(cos), 0, 0, 0, float32(-sin)/600)
m := scene.Translation(160, 370).Mul(turn).Mul(scene.Translation(-1050, -1050))
r4 := func(v float32) [4]float32 { return [4]float32{v, v, v, v} }
red, blue := scene.Color{R: 220, G: 40, B: 40, A: 255}, scene.Color{R: 40, G: 60, B: 220, A: 255}
s.Ops = []scene.Op{
{Kind: scene.OpShadow, Rect: scene.Rect{X: 1000, Y: 1004, W: 100, H: 100}, Radii: r4(12), Blur: 6, Color: scene.Color{A: 120}, Transform: m},
{Kind: scene.OpPushClip, Rect: scene.Rect{X: 1000, Y: 1000, W: 100, H: 100}, Radii: r4(12), Transform: m},
{Kind: scene.OpFill, Rect: scene.Rect{X: 990, Y: 990, W: 120, H: 120}, Color: blue, Color2: red, Paint: scene.PaintLinear,
Gradient: [4]float32{1000, 1000, 1100, 1000}, Transform: m},
{Kind: scene.OpFill, Rect: scene.Rect{X: 1030, Y: 1030, W: 40, H: 40}, Radii: r4(8), Color: red,
Border: scene.Uniform(3), BorderColor: scene.Color{R: 250, G: 250, B: 250, A: 255}, Transform: m},
{Kind: scene.OpPopClip, Transform: m},
}
return s
}
// AxisClipScene is content under a hard clip that a pose scales and moves
// (a scroll view in a panel that is entering): the clip edges fall in the middle
// of pixels (the CPU renderer samples a pixel on a 4x4 grid, so its
// coverage is exact there).
func AxisClipScene() *scene.Scene {
s := &scene.Scene{Width: 200, Height: 160, Clear: scene.Color{R: 246, G: 247, B: 249, A: 255}}
m := scene.Translation(7.5, 5.5).Mul(scene.Scaling(.5, .5))
red, blue := scene.Color{R: 220, G: 40, B: 40, A: 255}, scene.Color{R: 40, G: 60, B: 220, A: 255}
s.Ops = []scene.Op{
{Kind: scene.OpFill, Rect: scene.Rect{X: 5, Y: 5, W: 120, H: 100}, Radii: [4]float32{14, 14, 14, 14}, Color: blue, Transform: m},
{Kind: scene.OpPushClip, Rect: scene.Rect{X: 10, Y: 12, W: 80, H: 60}, Transform: m},
{Kind: scene.OpFill, Rect: scene.Rect{W: 200, H: 160}, Color: red, Transform: m},
{Kind: scene.OpFill, Rect: scene.Rect{X: 40, Y: 30, W: 90, H: 70}, Radii: [4]float32{20, 20, 20, 20}, Color: blue, Transform: m},
{Kind: scene.OpPopClip, Transform: m},
}
return s
}
// TranslatedScenes are the reference scene moved by translations, by
// whole pixels and between them (not by halves: the centers of pixels
// would fall on the seam of a conic gradient, which renderers round
// apart), which renderers fold into the geometry
// of its fills, shadows, images and (by whole pixels) glyphs, its clips
// and effects keeping theirs (scene.TranslationFolder).
func TranslatedScenes() []*scene.Scene {
scene.FoldTranslations() // as package ui does
var ss []*scene.Scene
for _, m := range []scene.Affine{scene.Translation(12, 7), scene.Translation(-6.25, 4.25)} {
s := Scene()
s.Clear = scene.Color{R: 100, G: 120, B: 160, A: 80}
for i := range s.Ops {
s.Ops[i].Transform = m
}
ss = append(ss, s)
}
return ss
}
+62
View File
@@ -0,0 +1,62 @@
package gputest
import "github.com/egoist/mygo/internal/scene"
// NV12Frame returns a w×h frame of video in NV12 with the matrix m, its
// planes in memory with rows padded past their width, as decoders pad
// them: bars of saturated colors over a gradient of luma, which the
// renderers sample as planes and the CPU renderer converts (Restore).
func NV12Frame(w, h int, m scene.YUVMatrix) *scene.Image {
ls, cs := w+13, 2*((w+1)/2)+6
cw, ch := (w+1)/2, (h+1)/2
luma, chroma := make([]byte, ls*h), make([]byte, cs*ch)
for y := range h {
for x := range w {
luma[y*ls+x] = byte(16 + (219*x)/max(w-1, 1))
}
}
// Six bars of chroma, Cb and Cr from 16 to 240.
bars := [][2]byte{{240, 128}, {16, 240}, {128, 16}, {240, 240}, {16, 16}, {90, 200}}
for y := range ch {
for x := range cw {
c := bars[x*len(bars)/cw]
if y < ch/4 {
c = [2]byte{128, 128} // gray
}
chroma[y*cs+2*x], chroma[y*cs+2*x+1] = c[0], c[1]
}
}
return SurfaceImage(&scene.Surface{
Format: scene.SurfaceNV12,
Matrix: m,
Planes: [2]scene.Plane{{Pix: luma, Stride: ls}, {Pix: chroma, Stride: cs}},
}, w, h)
}
// SurfaceImage returns an image showing s, whose pixels convert it.
func SurfaceImage(s *scene.Surface, w, h int) *scene.Image {
img := scene.NewImageRGBA(w, h, nil)
img.Surface = s
img.Restore = func() []byte { return s.RGBA(nil, w, h) }
return img
}
// VideoScene draws frames of video in NV12 of each matrix, scaled,
// rounded, faded and in grayscale, as video elements show them.
func VideoScene() *scene.Scene {
s := &scene.Scene{Width: 360, Height: 260, Clear: scene.Color{R: 255, G: 255, B: 255, A: 255}}
add := func(op scene.Op) { s.Ops = append(s.Ops, op) }
for i, m := range []scene.YUVMatrix{scene.BT601, scene.BT709, scene.BT2020, scene.BT601 + scene.FullRange, scene.BT709 + scene.FullRange, scene.BT2020 + scene.FullRange} {
img := NV12Frame(96, 54, m)
x, y := float32(8+(i%3)*116), float32(8+(i/3)*72)
add(scene.Op{Kind: scene.OpImage, Rect: scene.Rect{X: x, Y: y, W: 96, H: 54}, Image: img, Src: scene.Rect{W: 96, H: 54}})
}
big := NV12Frame(64, 36, scene.BT709)
// Scaled up, with rounded corners, faded, and in grayscale.
add(scene.Op{Kind: scene.OpImage, Rect: scene.Rect{X: 8, Y: 160, W: 160, H: 90}, Radii: [4]float32{12, 12, 12, 12}, Image: big, Src: scene.Rect{W: 64, H: 36}})
add(scene.Op{Kind: scene.OpImage, Rect: scene.Rect{X: 184, Y: 160, W: 80, H: 45}, Image: big, Src: scene.Rect{W: 64, H: 36}, Opacity: 0.5})
add(scene.Op{Kind: scene.OpImage, Rect: scene.Rect{X: 272, Y: 160, W: 80, H: 45}, Image: big, Src: scene.Rect{W: 64, H: 36}, Grayscale: true})
// An odd size, whose chroma has a half sample at its edges.
add(scene.Op{Kind: scene.OpImage, Rect: scene.Rect{X: 184, Y: 216, W: 37, H: 21}, Image: NV12Frame(37, 21, scene.BT601), Src: scene.Rect{W: 37, H: 21}})
return s
}