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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
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;
}
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;
return (u16)Min(half, sign | 0x7BFFu);
}
internal void draw_init(Arena *arena, DrawList *list)
{
MemSetZero(list, sizeof(*list));
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;
}
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)
{
f32 x0 = Min(rect.x0, rect.x1), x1 = Max(rect.x0, rect.x1);
f32 y0 = Min(rect.y0, rect.y1), y1 = Max(rect.y0, rect.y1);
Rect grown = {x0 - softness, y0 - softness, x1 + softness, 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_diamond(DrawList *list, Rect rect, u32 color, f32 border)
{
draw_shape(list, rect, color, color, 0.0f, border, 0.5f / list->scale, DRAW_KIND_DIAMOND);
}
internal void draw_line_dashed(DrawList *list, f32 x0, f32 y0, f32 x1, f32 y1, f32 thickness, f32 dash_period, f32 dash_phase, u32 color)
{
f32 half_w = Max(thickness * 0.5f, 0.35f / list->scale);
f32 softness = 0.6f / list->scale;
f32 pad = half_w + softness + 1.0f / list->scale;
Rect bounds = {Min(x0, x1) - pad, Min(y0, y1) - pad, Max(x0, x1) + pad, Max(y0, y1) + pad};
if(!draw_clip_visible(list, bounds)) {
return;
}
DrawInstance *instance = draw_push(list);
if(!instance) {
return;
}
f32 s = list->scale;
f32 p = dash_phase - floorf(dash_phase);
u32 p_u8 = (u32)(p * 255.0f + 0.5f) & 0xFFu;
instance->rect[0] = x0 * s;
instance->rect[1] = y0 * s;
instance->rect[2] = x1 * s;
instance->rect[3] = y1 * s;
instance->data[0] = (u32)f32_to_f16(half_w * s) | ((u32)f32_to_f16(dash_period * s) << 16);
instance->data[1] = (u32)f32_to_f16(softness * s) | (p_u8 << 16) | ((u32)DRAW_KIND_LINE << 24);
instance->colors[0] = instance->colors[1] = color_pack(color);
}
internal void draw_line(DrawList *list, f32 x0, f32 y0, f32 x1, f32 y1, f32 thickness, u32 color)
{
draw_line_dashed(list, x0, y0, x1, y1, thickness, 0.0f, 0.0f, color);
}
internal void draw_arrow(DrawList *list, f32 tail_x, f32 tail_y, f32 tip_x, f32 tip_y, f32 half_width, f32 border, DrawArrowStyle style, u32 color)
{
f32 softness = 0.6f / list->scale;
f32 pad = half_width + border + softness + 2.0f / list->scale;
Rect bounds = {Min(tail_x, tip_x) - pad, Min(tail_y, tip_y) - pad, Max(tail_x, tip_x) + pad, Max(tail_y, tip_y) + pad};
if(!draw_clip_visible(list, bounds)) {
return;
}
DrawInstance *instance = draw_push(list);
if(!instance) {
return;
}
f32 s = list->scale;
instance->rect[0] = tail_x * s;
instance->rect[1] = tail_y * s;
instance->rect[2] = tip_x * s;
instance->rect[3] = tip_y * s;
instance->data[0] = (u32)f32_to_f16(half_width * s) | ((u32)f32_to_f16(border * s) << 16);
instance->data[1] = (u32)f32_to_f16(softness * s) | (((u32)style & 0xFFu) << 16) | ((u32)DRAW_KIND_ARROW << 24);
instance->colors[0] = instance->colors[1] = color_pack(color);
}
internal void draw_bezier(DrawList *list, f32 x0, f32 y0, f32 cx0, f32 cy0, f32 cx1, f32 cy1, f32 x1, f32 y1, f32 thickness, f32 dash_period, f32 dash_phase, u32 color)
{
f32 bx0 = Min(Min(x0, x1), Min(cx0, cx1)) - thickness;
f32 by0 = Min(Min(y0, y1), Min(cy0, cy1)) - thickness;
f32 bx1 = Max(Max(x0, x1), Max(cx0, cx1)) + thickness;
f32 by1 = Max(Max(y0, y1), Max(cy0, cy1)) + thickness;
if(!draw_clip_visible(list, Rect{bx0, by0, bx1, by1})) {
return;
}
f32 chord = sqrtf((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));
u32 steps = (u32)Clamp((s32)(chord / 18.0f) + 6, 8, 28);
f32 px = x0, py = y0;
f32 accum = 0.0f;
for(u32 i = 1; i <= steps; ++i) {
f32 t = (f32)i / (f32)steps;
f32 u = 1.0f - t;
f32 uu = u * u, tt = t * t;
f32 nx = uu * u * x0 + 3.0f * uu * t * cx0 + 3.0f * u * tt * cx1 + tt * t * x1;
f32 ny = uu * u * y0 + 3.0f * uu * t * cy0 + 3.0f * u * tt * cy1 + tt * t * y1;
f32 ph = (dash_period > 0.5f) ? (dash_phase - accum / dash_period) : 0.0f;
draw_line_dashed(list, px, py, nx, ny, thickness, dash_period, ph, color);
accum += sqrtf((nx - px) * (nx - px) + (ny - py) * (ny - py));
px = nx;
py = ny;
}
}
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);
}
|