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 (Session.tick 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.
46const bufferSlack = 4
47
48// List is ListBatches reusing this buffer's storage where capacity allows.
49func (b *ListBuffer) List(frame Frame) ([]vkdevice.Instance, []vkdevice.Batch, ListStats) {
50 b.Release()
51 if b.skipped == nil {
52 b.skipped = make(map[string]int)
53 }
54 clear(b.skipped)
55 instances, batches, stats := listBatches(frame, b.instances[:0], b.batches[:0], b.skipped)
56 b.instances, b.batches = instances[:0], batches[:0]
57 return instances, batches, stats
58}
59
60// Release drops borrowed image references from the previous result. Call it
61// once the result has been consumed so an idle buffer does not pin images.
62func (b *ListBuffer) Release() {
63 clear(b.batches[:cap(b.batches)])
64}
65
66// drawn reports whether the operation draws an instance.
67func drawn(op string) bool {
68 switch op {
69 case "rect", "selection", "caret", "preedit-underline", "preedit-caret", "glyph", "border", "outline", "shadow", "image":
70 return true
71 }
72 return false
73}
74
75// listBatches fills out and batches (both length zero), growing them only when
76// their capacity is short of the exact counts or absurdly larger than needed.
77func listBatches(frame Frame, out []vkdevice.Instance, batches []vkdevice.Batch, skipped map[string]int) ([]vkdevice.Instance, []vkdevice.Batch, ListStats) {
78 // Count first so both slices are sized exactly: no append growth, and no
79 // over-allocation for frames that skip quads.
80 drawCount, batchCount, open := 0, 0, false
81 for i := range frame.Quads {
82 op := frame.Quads[i].Op
83 if !drawn(op) {
84 continue
85 }
86 drawCount++
87 if op == "image" {
88 batchCount++
89 open = false
90 } else if !open {
91 batchCount++
92 open = true
93 }
94 }
95 if out == nil || cap(out) < drawCount || cap(out) > bufferSlack*drawCount+64 {
96 out = make([]vkdevice.Instance, 0, drawCount)
97 }
98 if batchCount == 0 {
99 batches = nil // as before: no draws, no batches
100 } else if cap(batches) < batchCount || cap(batches) > bufferSlack*batchCount+16 {
101 batches = make([]vkdevice.Batch, 0, batchCount)
102 }
103 stats := ListStats{Skipped: skipped}
104 for i := range frame.Quads {
105 q := &frame.Quads[i]
106 if !drawn(q.Op) {
107 stats.Skipped[q.Op]++
108 continue
109 }
110 if q.Op == "image" {
111 batches = append(batches, vkdevice.Batch{First: uint32(len(out)), Count: 1, Source: q.Image})
112 } else if len(batches) == 0 || batches[len(batches)-1].Source != nil {
113 batches = append(batches, vkdevice.Batch{First: uint32(len(out)), Count: 1})
114 } else {
115 batches[len(batches)-1].Count++
116 }
117 out = append(out, instanceOf(q))
118 }
119 stats.Drawn = len(out)
120 return out, batches, stats
121}
122
123// instanceOf converts one drawable quad to its GPU instance.
124func instanceOf(q *Quad) vkdevice.Instance {
125 v := vkdevice.Instance{
126 Bounds: [4]float32{float32(q.Rect.X), float32(q.Rect.Y), float32(q.Rect.W), float32(q.Rect.H)},
127 Clip: [4]float32{float32(q.Clip.X), float32(q.Clip.Y), float32(q.Clip.W), float32(q.Clip.H)},
128 Color: [4]float32{float32(q.Color.R), float32(q.Color.G), float32(q.Color.B), float32(q.Color.A)},
129 }
130 v.Shape = [4]float32{float32(q.Shape.X), float32(q.Shape.Y), float32(q.Shape.W), float32(q.Shape.H)}
131 v.Widths = [4]float32{float32(q.Widths.Top), float32(q.Widths.Right), float32(q.Widths.Bottom), float32(q.Widths.Left)}
132 v.Shadow = [4]float32{float32(q.Shadow[0]), float32(q.Shadow[1]), float32(q.Shadow[2]), float32(q.Shadow[3])}
133 for i, c := range [4]css.Color{q.Colors.Top, q.Colors.Right, q.Colors.Bottom, q.Colors.Left} {
134 v.Sides[i] = [4]float32{float32(c.R), float32(c.G), float32(c.B), float32(c.A * float64(v.Color[3]))}
135 }
136 switch q.Op {
137 case "image":
138 v.KindLayer[0] = 6
139 v.UV = [4]float32{0, 0, 1, 1}
140 case "border":
141 v.KindLayer[0] = 2
142 case "outline":
143 v.KindLayer[0] = 3
144 case "shadow":
145 v.KindLayer[0] = 4
146 if q.Inset {
147 v.KindLayer[0] = 5
148 }
149 }
150 for i, r := range q.Radii {
151 v.Radii[i] = float32(r)
152 }
153 if q.Op == "glyph" {
154 v.KindLayer = [4]float32{1, float32(q.Glyph.Page)}
155 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}
156 }
157 return v
158}