1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
internal u32 color_lerp(u32 a, u32 b, f32 t)
{
t = Clamp(t, 0.0f, 1.0f);
u32 result = 0;
for(u32 shift = 0; shift < 32; shift += 8) {
f32 ca = (f32)((a >> shift) & 0xFF);
f32 cb = (f32)((b >> shift) & 0xFF);
result |= ((u32)(ca + (cb - ca) * t + 0.5f) & 0xFF) << shift;
}
return result;
}
internal Rect rect_intersect(Rect a, Rect b)
{
Rect result = {Max(a.x0, b.x0), Max(a.y0, b.y0), Min(a.x1, b.x1), Min(a.y1, b.y1)};
if(result.x1 < result.x0) result.x1 = result.x0;
if(result.y1 < result.y0) result.y1 = result.y0;
return result;
}
// NOTE: f32 -> ieee half, round to nearest. the shape params are pixel sizes (0..a few hundred),
// 11 bits of mantissa is far below what the sdf can show. denormals flush to 0, huge clamps.
internal inline u16 f32_to_f16(f32 value)
{
u32 bits = 0;
MemCpy(&bits, &value, 4);
u32 sign = (bits >> 16) & 0x8000u;
s32 exponent = (s32)((bits >> 23) & 0xFF) - 127 + 15;
u32 mantissa = bits & 0x7FFFFFu;
if(exponent <= 0) {
return (u16)sign;
}
if(exponent >= 31) {
return (u16)(sign | 0x7BFFu);
}
u32 half = sign | ((u32)exponent << 10) | (mantissa >> 13);
half += (mantissa >> 12) & 1; // round, a carry into the exponent is still correct
return (u16)Min(half, sign | 0x7BFFu);
}
internal void draw_init(Arena *arena, DrawList *list)
{
MemSetZero(list, sizeof(*list));
// NOTE: not zeroed: every instance is fully written before it's read, so the pages are only
// touched (and only count towards rss) as far as the ui actually gets
list->own = (DrawInstance *)push_cache_aligned_raw(arena, sizeof(DrawInstance) * DRAW_MAX_INSTANCES);
list->instances = list->own;
}
internal void draw_open_batch(DrawList *list)
{
if(list->batch_count > 0 && list->batches[list->batch_count - 1].count == 0) {
--list->batch_count; // reuse an empty batch
}
if(list->batch_count >= DRAW_MAX_BATCHES) {
return;
}
Rect clip = list->clips[list->clip_depth - 1];
f32 s = list->scale;
s32 x0 = (s32)floorf(clip.x0 * s), y0 = (s32)floorf(clip.y0 * s);
s32 x1 = (s32)ceilf(clip.x1 * s), y1 = (s32)ceilf(clip.y1 * s);
x0 = Clamp(x0, 0, (s32)list->width);
y0 = Clamp(y0, 0, (s32)list->height);
x1 = Clamp(x1, x0, (s32)list->width);
y1 = Clamp(y1, y0, (s32)list->height);
DrawBatch *batch = &list->batches[list->batch_count++];
batch->first = list->count;
batch->count = 0;
batch->clip[0] = x0;
batch->clip[1] = y0;
batch->clip[2] = x1 - x0;
batch->clip[3] = y1 - y0;
}
internal void draw_begin(DrawList *list, u32 width, u32 height, f32 scale)
{
list->peak = Max(list->peak, list->count);
list->count = 0;
list->batch_count = 0;
list->scale = scale;
list->width = width;
list->height = height;
list->clip_depth = 1;
list->rounding = 1.0f;
list->clips[0] = Rect{0, 0, (f32)width / scale, (f32)height / scale};
draw_open_batch(list);
}
internal void draw_clip_push(DrawList *list, Rect clip)
{
if(list->clip_depth >= DRAW_MAX_CLIPS) {
return;
}
list->clips[list->clip_depth] = rect_intersect(list->clips[list->clip_depth - 1], clip);
++list->clip_depth;
draw_open_batch(list);
}
internal void draw_clip_pop(DrawList *list)
{
if(list->clip_depth > 1) {
--list->clip_depth;
draw_open_batch(list);
}
}
internal b32 draw_clip_visible(DrawList *list, Rect rect)
{
Rect clip = list->clips[list->clip_depth - 1];
return rect.x1 > clip.x0 && rect.x0 < clip.x1 && rect.y1 > clip.y0 && rect.y0 < clip.y1;
}
internal DrawInstance *draw_push(DrawList *list)
{
if(list->count >= DRAW_MAX_INSTANCES || list->batch_count == 0) {
return NULL;
}
DrawInstance *instance = &list->instances[list->count++];
++list->batches[list->batch_count - 1].count;
return instance;
}
internal void draw_shape(DrawList *list, Rect rect, u32 top, u32 bottom, f32 radius, f32 border, f32 softness, DrawKind kind)
{
// NOTE: cull against the clip on the cpu, saves the gpu a quad and keeps batches tight
Rect grown = {rect.x0 - softness, rect.y0 - softness, rect.x1 + softness, rect.y1 + softness};
if(!draw_clip_visible(list, grown)) {
return;
}
DrawInstance *instance = draw_push(list);
if(!instance) {
return;
}
f32 s = list->scale;
instance->rect[0] = rect.x0 * s;
instance->rect[1] = rect.y0 * s;
instance->rect[2] = rect.x1 * s;
instance->rect[3] = rect.y1 * s;
instance->data[0] = (u32)f32_to_f16((kind == DRAW_KIND_RECT ? radius * list->rounding : radius) * s) | ((u32)f32_to_f16(border * s) << 16);
instance->data[1] = (u32)f32_to_f16(softness * s) | ((u32)kind << 24);
instance->colors[0] = color_pack(top);
instance->colors[1] = color_pack(bottom);
}
internal void draw_rect(DrawList *list, Rect rect, u32 color, f32 radius)
{
draw_shape(list, rect, color, color, radius, 0.0f, 0.5f / list->scale, DRAW_KIND_RECT);
}
internal void draw_rect_gradient(DrawList *list, Rect rect, u32 top, u32 bottom, f32 radius)
{
draw_shape(list, rect, top, bottom, radius, 0.0f, 0.5f / list->scale, DRAW_KIND_RECT);
}
internal void draw_border(DrawList *list, Rect rect, u32 color, f32 radius, f32 thickness)
{
draw_shape(list, rect, color, color, radius, thickness, 0.5f / list->scale, DRAW_KIND_RECT);
}
internal void draw_shadow(DrawList *list, Rect rect, u32 color, f32 radius, f32 blur)
{
draw_shape(list, rect, color, color, radius, 0.0f, blur, DRAW_KIND_RECT);
}
internal void draw_triangle(DrawList *list, Rect rect, u32 color)
{
draw_shape(list, rect, color, color, 0.0f, 0.0f, 0.5f / list->scale, DRAW_KIND_TRIANGLE);
}
internal void draw_circle(DrawList *list, f32 cx, f32 cy, f32 r, u32 color)
{
draw_shape(list, Rect{cx - r, cy - r, cx + r, cy + r}, color, color, r, 0.0f, 0.5f / list->scale, DRAW_KIND_CIRCLE);
}
internal void draw_square(DrawList *list, f32 cx, f32 cy, f32 half, u32 color)
{
draw_rect(list, Rect{cx - half, cy - half, cx + half, cy + half}, color, 0.0f);
}
internal void draw_ring(DrawList *list, f32 cx, f32 cy, f32 r, f32 thickness, u32 color)
{
draw_shape(list, Rect{cx - r, cy - r, cx + r, cy + r}, color, color, r, thickness, 0.5f / list->scale, DRAW_KIND_CIRCLE);
}
internal void draw_glyph_physical(DrawList *list, f32 x0, f32 y0, f32 x1, f32 y1, u32 atlas_x, u32 atlas_y, u32 color)
{
f32 s = list->scale;
Rect logical = {x0 / s, y0 / s, x1 / s, y1 / s};
if(!draw_clip_visible(list, logical)) {
return;
}
DrawInstance *instance = draw_push(list);
if(!instance) {
return;
}
u32 w = (u32)(x1 - x0);
u32 h = (u32)(y1 - y0);
instance->rect[0] = x0;
instance->rect[1] = y0;
instance->rect[2] = x1;
instance->rect[3] = y1;
instance->data[0] = atlas_x | (atlas_y << 16);
instance->data[1] = (w & 0xFFF) | ((h & 0xFFF) << 12) | ((u32)DRAW_KIND_GLYPH << 24);
instance->colors[0] = instance->colors[1] = color_pack(color);
}
|