1package render
2
3import (
4 "github.com/bnema/nefergui/internal/css"
5 "github.com/bnema/nefergui/internal/presentation/vkdevice"
6)
7
8// ListStats reports every skipped prepared quad by operation.
9type ListStats struct {
10 Drawn int
11 Skipped map[string]int
12}
13
14// ListInstances converts prepared physical quads to straight-sRGB GPU inputs.
15// Unknown operations are counted, not silently drawn as rectangles. Uploads
16// remain the caller's responsibility.
17func ListInstances(frame Frame) ([]vkdevice.Instance, ListStats) {
18 instances, _, stats := ListBatches(frame)
19 return instances, stats
20}
21
22// ListBatches groups consecutive atlas draws and isolates each image draw.
23// First indexes the returned instances, retaining exact painter's order.
24// The returned slices and map are freshly allocated and owned by the caller.
25func ListBatches(frame Frame) ([]vkdevice.Instance, []vkdevice.Batch, ListStats) {
26 return listBatches(frame, nil, nil, make(map[string]int))
27}
28
29// ListBuffer is a single-owner scratch area for repeated ListBatches calls.
30// The slices it returns alias its storage: they are valid only until the next
31// List or Release call, and must not be retained, shared across goroutines, or
32// used while another List may run. This is safe for consumers that copy the
33// data synchronously (Target.submit uploads instances into GPU memory and
34// resolve copies the batches before recording returns); anything that keeps the
35// slices past that point must use ListBatches instead. Retained frames hold
36// only render.Frame, never these slices, so a blocked frame is re-listed on retry.
37// Stats.Skipped shares the same lifetime. The zero value is ready to use.
38type ListBuffer struct {
39 instances []vkdevice.Instance
40 batches []vkdevice.Batch
41 skipped map[string]int
42}
43
44// bufferSlack is how much larger than needed retained storage may be before
45// List drops it, so one huge frame does not pin memory forever. It matches
46// layout's retainSlack.
47const bufferSlack = 4
48
49// List is ListBatches reusing this buffer's storage where capacity allows.
50func (b *ListBuffer) List(frame Frame) ([]vkdevice.Instance, []vkdevice.Batch, ListStats) {
51 b.Release()
52 if b.skipped == nil {
53 b.skipped = make(map[string]int)
54 }
55 clear(b.skipped)
56 instances, batches, stats := listBatches(frame, b.instances[:0], b.batches[:0], b.skipped)
57 b.instances, b.batches = instances[:0], batches[:0]
58 return instances, batches, stats
59}
60
61// Release drops borrowed image references from the previous result. Call it
62// once the result has been consumed so an idle buffer does not pin images.
63func (b *ListBuffer) Release() {
64 clear(b.batches[:cap(b.batches)])
65}
66
67// drawn reports whether the operation draws an instance.
68func drawn(op string) bool {
69 switch op {
70 case "rect", "selection", "caret", "preedit-underline", "preedit-caret", "glyph", "border", "outline", "shadow", "image":
71 return true
72 }
73 return false
74}
75
76// listBatches fills out and batches (both length zero), growing them only when
77// their capacity is short of the exact counts or absurdly larger than needed.
78func listBatches(frame Frame, out []vkdevice.Instance, batches []vkdevice.Batch, skipped map[string]int) ([]vkdevice.Instance, []vkdevice.Batch, ListStats) {
79 // Count first so both slices are sized exactly: no append growth, and no
80 // over-allocation for frames that skip quads.
81 drawCount, batchCount, open := 0, 0, false
82 for i := range frame.Quads {
83 op := frame.Quads[i].Op
84 if !drawn(op) {
85 continue
86 }
87 drawCount++
88 if op == "image" {
89 batchCount++
90 open = false
91 } else if !open {
92 batchCount++
93 open = true
94 }
95 }
96 if out == nil || cap(out) < drawCount || cap(out) > bufferSlack*drawCount+64 {
97 out = make([]vkdevice.Instance, 0, drawCount)
98 }
99 if batchCount == 0 {
100 batches = nil // as before: no draws, no batches
101 } else if cap(batches) < batchCount || cap(batches) > bufferSlack*batchCount+16 {
102 batches = make([]vkdevice.Batch, 0, batchCount)
103 }
104 stats := ListStats{Skipped: skipped}
105 for i := range frame.Quads {
106 q := &frame.Quads[i]
107 if !drawn(q.Op) {
108 stats.Skipped[q.Op]++
109 continue
110 }
111 if q.Op == "image" {
112 batches = append(batches, vkdevice.Batch{First: uint32(len(out)), Count: 1, Source: q.Image})
113 } else if len(batches) == 0 || batches[len(batches)-1].Source != nil {
114 batches = append(batches, vkdevice.Batch{First: uint32(len(out)), Count: 1})
115 } else {
116 batches[len(batches)-1].Count++
117 }
118 out = append(out, instanceOf(q))
119 }
120 stats.Drawn = len(out)
121 return out, batches, stats
122}
123
124// instanceOf converts one drawable quad to its GPU instance.
125func instanceOf(q *Quad) vkdevice.Instance {
126 v := vkdevice.Instance{
127 Bounds: [4]float32{float32(q.Rect.X), float32(q.Rect.Y), float32(q.Rect.W), float32(q.Rect.H)},
128 Clip: [4]float32{float32(q.Clip.X), float32(q.Clip.Y), float32(q.Clip.W), float32(q.Clip.H)},
129 Color: [4]float32{float32(q.Color.R), float32(q.Color.G), float32(q.Color.B), float32(q.Color.A)},
130 }
131 v.Shape = [4]float32{float32(q.Shape.X), float32(q.Shape.Y), float32(q.Shape.W), float32(q.Shape.H)}
132 v.Widths = [4]float32{float32(q.Widths.Top), float32(q.Widths.Right), float32(q.Widths.Bottom), float32(q.Widths.Left)}
133 v.Shadow = [4]float32{float32(q.Shadow[0]), float32(q.Shadow[1]), float32(q.Shadow[2]), float32(q.Shadow[3])}
134 for i, c := range [4]css.Color{q.Colors.Top, q.Colors.Right, q.Colors.Bottom, q.Colors.Left} {
135 v.Sides[i] = [4]float32{float32(c.R), float32(c.G), float32(c.B), float32(c.A * float64(v.Color[3]))}
136 }
137 switch q.Op {
138 case "image":
139 v.KindLayer[0] = 6
140 v.UV = [4]float32{0, 0, 1, 1}
141 case "border":
142 v.KindLayer[0] = 2
143 case "outline":
144 v.KindLayer[0] = 3
145 case "shadow":
146 v.KindLayer[0] = 4
147 if q.Inset {
148 v.KindLayer[0] = 5
149 }
150 }
151 for i, r := range q.Radii {
152 v.Radii[i] = float32(r)
153 }
154 if q.Op == "glyph" {
155 v.KindLayer = [4]float32{1, float32(q.Glyph.Page)}
156 v.UV = [4]float32{float32(q.Glyph.Rect.Min.X) / vkdevice.AtlasSize, float32(q.Glyph.Rect.Min.Y) / vkdevice.AtlasSize, float32(q.Glyph.Rect.Max.X) / vkdevice.AtlasSize, float32(q.Glyph.Rect.Max.Y) / vkdevice.AtlasSize}
157 }
158 return v
159}