/
DrawBuffer.cpp
602 lines (535 loc) · 16.4 KB
/
DrawBuffer.cpp
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
#include <algorithm>
#include <cmath>
#include <vector>
#include <stddef.h>
#include "Common/System/Display.h"
#include "Common/Math/math_util.h"
#include "Common/Render/TextureAtlas.h"
#include "Common/Render/DrawBuffer.h"
#include "Common/Render/Text/draw_text.h"
#include "Common/Data/Encoding/Utf8.h"
#include "Common/Data/Text/WrapText.h"
#include "Common/Log.h"
#include "Common/StringUtils.h"
enum {
// Enough?
MAX_VERTS = 65536,
};
DrawBuffer::DrawBuffer() : count_(0), atlas(0) {
verts_ = new Vertex[MAX_VERTS];
fontscalex = 1.0f;
fontscaley = 1.0f;
inited_ = false;
}
DrawBuffer::~DrawBuffer() {
delete [] verts_;
}
void DrawBuffer::Init(Draw::DrawContext *t3d, Draw::Pipeline *pipeline) {
using namespace Draw;
if (inited_)
return;
draw_ = t3d;
inited_ = true;
if (pipeline->RequiresBuffer()) {
vbuf_ = draw_->CreateBuffer(MAX_VERTS * sizeof(Vertex), BufferUsageFlag::DYNAMIC | BufferUsageFlag::VERTEXDATA);
} else {
vbuf_ = nullptr;
}
}
Draw::InputLayout *DrawBuffer::CreateInputLayout(Draw::DrawContext *t3d) {
using namespace Draw;
InputLayoutDesc desc = {
{
{ sizeof(Vertex), false },
},
{
{ 0, SEM_POSITION, DataFormat::R32G32B32_FLOAT, 0 },
{ 0, SEM_TEXCOORD0, DataFormat::R32G32_FLOAT, 12 },
{ 0, SEM_COLOR0, DataFormat::R8G8B8A8_UNORM, 20 },
},
};
return t3d->CreateInputLayout(desc);
}
void DrawBuffer::Shutdown() {
if (vbuf_) {
vbuf_->Release();
vbuf_ = nullptr;
}
inited_ = false;
alphaStack_.clear();
drawMatrixStack_.clear();
pipeline_ = nullptr;
draw_ = nullptr;
count_ = 0;
}
void DrawBuffer::Begin(Draw::Pipeline *program) {
pipeline_ = program;
count_ = 0;
}
void DrawBuffer::Flush(bool set_blend_state) {
using namespace Draw;
if (count_ == 0)
return;
if (!pipeline_) {
ERROR_LOG(G3D, "DrawBuffer: No program set, skipping flush!");
count_ = 0;
return;
}
draw_->BindPipeline(pipeline_);
VsTexColUB ub{};
memcpy(ub.WorldViewProj, drawMatrix_.getReadPtr(), sizeof(Lin::Matrix4x4));
draw_->UpdateDynamicUniformBuffer(&ub, sizeof(ub));
if (vbuf_) {
draw_->UpdateBuffer(vbuf_, (const uint8_t *)verts_, 0, sizeof(Vertex) * count_, Draw::UPDATE_DISCARD);
draw_->BindVertexBuffers(0, 1, &vbuf_, nullptr);
int offset = 0;
draw_->Draw(count_, offset);
} else {
draw_->DrawUP((const void *)verts_, count_);
}
count_ = 0;
}
void DrawBuffer::V(float x, float y, float z, uint32_t color, float u, float v) {
_assert_msg_(count_ < MAX_VERTS, "Overflowed the DrawBuffer");
Vertex *vert = &verts_[count_++];
vert->x = x;
vert->y = y;
vert->z = z;
vert->rgba = alpha_ == 1.0f ? color : alphaMul(color, alpha_);
vert->u = u;
vert->v = v;
}
void DrawBuffer::Rect(float x, float y, float w, float h, uint32_t color, int align) {
DoAlign(align, &x, &y, &w, &h);
RectVGradient(x, y, w, h, color, color);
}
void DrawBuffer::hLine(float x1, float y, float x2, uint32_t color) {
Rect(x1, y, x2 - x1, pixel_in_dps_y, color);
}
void DrawBuffer::vLine(float x, float y1, float y2, uint32_t color) {
Rect(x, y1, pixel_in_dps_x, y2 - y1, color);
}
void DrawBuffer::vLineAlpha50(float x, float y1, float y2, uint32_t color) {
Rect(x, y1, pixel_in_dps_x, y2 - y1, (color | 0xFF000000) & 0x7F000000);
}
void DrawBuffer::RectVGradient(float x, float y, float w, float h, uint32_t colorTop, uint32_t colorBottom) {
V(x, y, 0, colorTop, 0, 0);
V(x + w, y, 0, colorTop, 1, 0);
V(x + w, y + h, 0, colorBottom, 1, 1);
V(x, y, 0, colorTop, 0, 0);
V(x + w, y + h, 0, colorBottom, 1, 1);
V(x, y + h, 0, colorBottom, 0, 1);
}
void DrawBuffer::RectOutline(float x, float y, float w, float h, uint32_t color, int align) {
hLine(x, y, x + w + pixel_in_dps_x, color);
hLine(x, y + h, x + w + pixel_in_dps_x, color);
vLine(x, y, y + h + pixel_in_dps_y, color);
vLine(x + w, y, y + h + pixel_in_dps_y, color);
}
void DrawBuffer::MultiVGradient(float x, float y, float w, float h, GradientStop *stops, int numStops) {
for (int i = 0; i < numStops - 1; i++) {
float t0 = stops[i].t, t1 = stops[i+1].t;
uint32_t c0 = stops[i].t, c1 = stops[i+1].t;
RectVGradient(x, y + h * t0, w, h * (t1 - t0), c0, c1);
}
}
void DrawBuffer::Rect(float x, float y, float w, float h,
float u, float v, float uw, float uh,
uint32_t color) {
V(x, y, 0, color, u, v);
V(x + w, y, 0, color, u + uw, v);
V(x + w, y + h, 0, color, u + uw, v + uh);
V(x, y, 0, color, u, v);
V(x + w, y + h, 0, color, u + uw, v + uh);
V(x, y + h, 0, color, u, v + uh);
}
void DrawBuffer::Line(ImageID atlas_image, float x1, float y1, float x2, float y2, float thickness, uint32_t color) {
const AtlasImage *image = atlas->getImage(atlas_image);
if (!image)
return;
// No caps yet!
// Pre-rotated - we are making a thick line here
float dx = -(y2 - y1);
float dy = x2 - x1;
float len = sqrtf(dx * dx + dy * dy) / thickness;
if (len <= 0.0f)
len = 1.0f;
dx /= len;
dy /= len;
float x[4] = { x1 - dx, x2 - dx, x1 + dx, x2 + dx };
float y[4] = { y1 - dy, y2 - dy, y1 + dy, y2 + dy };
V(x[0], y[0], color, image->u1, image->v1);
V(x[1], y[1], color, image->u2, image->v1);
V(x[2], y[2], color, image->u1, image->v2);
V(x[2], y[2], color, image->u1, image->v2);
V(x[1], y[1], color, image->u2, image->v1);
V(x[3], y[3], color, image->u2, image->v2);
}
bool DrawBuffer::MeasureImage(ImageID atlas_image, float *w, float *h) {
const AtlasImage *image = atlas->getImage(atlas_image);
if (image) {
*w = (float)image->w;
*h = (float)image->h;
return true;
} else {
*w = 0;
*h = 0;
return false;
}
}
void DrawBuffer::DrawImage(ImageID atlas_image, float x, float y, float scale, Color color, int align) {
const AtlasImage *image = atlas->getImage(atlas_image);
if (!image)
return;
float w = (float)image->w * scale;
float h = (float)image->h * scale;
if (align & ALIGN_HCENTER) x -= w / 2;
if (align & ALIGN_RIGHT) x -= w;
if (align & ALIGN_VCENTER) y -= h / 2;
if (align & ALIGN_BOTTOM) y -= h;
DrawImageStretch(atlas_image, x, y, x + w, y + h, color);
}
void DrawBuffer::DrawImageStretch(ImageID atlas_image, float x1, float y1, float x2, float y2, Color color) {
const AtlasImage *image = atlas->getImage(atlas_image);
if (!image)
return;
V(x1, y1, color, image->u1, image->v1);
V(x2, y1, color, image->u2, image->v1);
V(x2, y2, color, image->u2, image->v2);
V(x1, y1, color, image->u1, image->v1);
V(x2, y2, color, image->u2, image->v2);
V(x1, y2, color, image->u1, image->v2);
}
inline void rot(float *v, float angle, float xc, float yc) {
const float x = v[0] - xc;
const float y = v[1] - yc;
const float sa = sinf(angle);
const float ca = cosf(angle);
v[0] = x * ca + y * -sa + xc;
v[1] = x * sa + y * ca + yc;
}
void DrawBuffer::DrawImageRotated(ImageID atlas_image, float x, float y, float scale, float angle, Color color, bool mirror_h) {
const AtlasImage *image = atlas->getImage(atlas_image);
if (!image)
return;
float w = (float)image->w * scale;
float h = (float)image->h * scale;
float x1 = x - w / 2;
float x2 = x + w / 2;
float y1 = y - h / 2;
float y2 = y + h / 2;
float v[6][2] = {
{x1, y1},
{x2, y1},
{x2, y2},
{x1, y1},
{x2, y2},
{x1, y2},
};
float u1 = image->u1;
float u2 = image->u2;
if (mirror_h) {
float temp = u1;
u1 = u2;
u2 = temp;
}
const float uv[6][2] = {
{u1, image->v1},
{u2, image->v1},
{u2, image->v2},
{u1, image->v1},
{u2, image->v2},
{u1, image->v2},
};
for (int i = 0; i < 6; i++) {
rot(v[i], angle, x, y);
V(v[i][0], v[i][1], 0, color, uv[i][0], uv[i][1]);
}
}
// TODO: add arc support
void DrawBuffer::Circle(float xc, float yc, float radius, float thickness, int segments, float startAngle, uint32_t color, float u_mul) {
float angleDelta = PI * 2 / segments;
float uDelta = 1.0f / segments;
float t2 = thickness / 2.0f;
float r1 = radius + t2;
float r2 = radius - t2;
for (int i = 0; i < segments + 1; i++) {
float angle1 = i * angleDelta;
float angle2 = (i + 1) * angleDelta;
float u1 = u_mul * i * uDelta;
float u2 = u_mul * (i + 1) * uDelta;
// TODO: get rid of one pair of cos/sin per loop, can reuse from last iteration
float c1 = cosf(angle1), s1 = sinf(angle1), c2 = cosf(angle2), s2 = sinf(angle2);
const float x[4] = {c1 * r1 + xc, c2 * r1 + xc, c1 * r2 + xc, c2 * r2 + xc};
const float y[4] = {s1 * r1 + yc, s2 * r1 + yc, s1 * r2 + yc, s2 * r2 + yc};
V(x[0], y[0], color, u1, 0);
V(x[1], y[1], color, u2, 0);
V(x[2], y[2], color, u1, 1);
V(x[1], y[1], color, u2, 0);
V(x[3], y[3], color, u2, 1);
V(x[2], y[2], color, u1, 1);
}
}
void DrawBuffer::DrawTexRect(float x1, float y1, float x2, float y2, float u1, float v1, float u2, float v2, Color color) {
V(x1, y1, color, u1, v1);
V(x2, y1, color, u2, v1);
V(x2, y2, color, u2, v2);
V(x1, y1, color, u1, v1);
V(x2, y2, color, u2, v2);
V(x1, y2, color, u1, v2);
}
void DrawBuffer::DrawImage4Grid(ImageID atlas_image, float x1, float y1, float x2, float y2, Color color, float corner_scale) {
const AtlasImage *image = atlas->getImage(atlas_image);
if (!image) {
return;
}
float u1 = image->u1, v1 = image->v1, u2 = image->u2, v2 = image->v2;
float um = (u2 + u1) * 0.5f;
float vm = (v2 + v1) * 0.5f;
float iw2 = (image->w * 0.5f) * corner_scale;
float ih2 = (image->h * 0.5f) * corner_scale;
float xa = x1 + iw2;
float xb = x2 - iw2;
float ya = y1 + ih2;
float yb = y2 - ih2;
// Top row
DrawTexRect(x1, y1, xa, ya, u1, v1, um, vm, color);
DrawTexRect(xa, y1, xb, ya, um, v1, um, vm, color);
DrawTexRect(xb, y1, x2, ya, um, v1, u2, vm, color);
// Middle row
DrawTexRect(x1, ya, xa, yb, u1, vm, um, vm, color);
DrawTexRect(xa, ya, xb, yb, um, vm, um, vm, color);
DrawTexRect(xb, ya, x2, yb, um, vm, u2, vm, color);
// Bottom row
DrawTexRect(x1, yb, xa, y2, u1, vm, um, v2, color);
DrawTexRect(xa, yb, xb, y2, um, vm, um, v2, color);
DrawTexRect(xb, yb, x2, y2, um, vm, u2, v2, color);
}
void DrawBuffer::DrawImage2GridH(ImageID atlas_image, float x1, float y1, float x2, Color color, float corner_scale) {
const AtlasImage *image = atlas->getImage(atlas_image);
float um = (image->u1 + image->u2) * 0.5f;
float iw2 = (image->w * 0.5f) * corner_scale;
float xa = x1 + iw2;
float xb = x2 - iw2;
float u1 = image->u1, v1 = image->v1, u2 = image->u2, v2 = image->v2;
float y2 = y1 + image->h;
DrawTexRect(x1, y1, xa, y2, u1, v1, um, v2, color);
DrawTexRect(xa, y1, xb, y2, um, v1, um, v2, color);
DrawTexRect(xb, y1, x2, y2, um, v1, u2, v2, color);
}
class AtlasWordWrapper : public WordWrapper {
public:
// Note: maxW may be height if rotated.
AtlasWordWrapper(const AtlasFont &atlasfont, float scale, const char *str, float maxW, int flags) : WordWrapper(str, maxW, flags), atlasfont_(atlasfont), scale_(scale) {
}
protected:
float MeasureWidth(const char *str, size_t bytes) override;
const AtlasFont &atlasfont_;
const float scale_;
};
float AtlasWordWrapper::MeasureWidth(const char *str, size_t bytes) {
float w = 0.0f;
for (UTF8 utf(str); utf.byteIndex() < (int)bytes; ) {
uint32_t c = utf.next();
if (c == '&') {
// Skip ampersand prefixes ("&&" is an ampersand.)
c = utf.next();
}
const AtlasChar *ch = atlasfont_.getChar(c);
if (!ch)
ch = atlasfont_.getChar('?');
w += ch->wx * scale_;
}
return w;
}
void DrawBuffer::MeasureTextCount(FontID font, const char *text, int count, float *w, float *h) {
const AtlasFont *atlasfont = atlas->getFont(font);
if (!atlasfont) {
*w = 0.0f;
*h = 0.0f;
return;
}
unsigned int cval;
float wacc = 0;
float maxX = 0.0f;
int lines = 1;
UTF8 utf(text);
while (true) {
if (utf.end())
break;
if (utf.byteIndex() >= count)
break;
cval = utf.next();
// Translate non-breaking space to space.
if (cval == 0xA0) {
cval = ' ';
} else if (cval == '\n') {
maxX = std::max(maxX, wacc);
wacc = 0;
lines++;
continue;
} else if (cval == '\t') {
cval = ' ';
} else if (cval == '&' && utf.peek() != '&') {
// Ignore lone ampersands
continue;
}
const AtlasChar *c = atlasfont->getChar(cval);
if (c) {
wacc += c->wx * fontscalex;
}
}
if (w) *w = std::max(wacc, maxX);
if (h) *h = atlasfont->height * fontscaley * lines;
}
void DrawBuffer::MeasureTextRect(FontID font_id, const char *text, int count, const Bounds &bounds, float *w, float *h, int align) {
if (!text || font_id.isInvalid()) {
*w = 0.0f;
*h = 0.0f;
return;
}
std::string toMeasure = std::string(text, count);
int wrap = align & (FLAG_WRAP_TEXT | FLAG_ELLIPSIZE_TEXT);
if (wrap) {
const AtlasFont *font = atlas->getFont(font_id);
if (!font) {
*w = 0.0f;
*h = 0.0f;
return;
}
AtlasWordWrapper wrapper(*font, fontscalex, toMeasure.c_str(), bounds.w, wrap);
toMeasure = wrapper.Wrapped();
}
MeasureTextCount(font_id, toMeasure.c_str(), (int)toMeasure.length(), w, h);
}
void DrawBuffer::MeasureText(FontID font, const char *text, float *w, float *h) {
return MeasureTextCount(font, text, (int)strlen(text), w, h);
}
void DrawBuffer::DrawTextShadow(FontID font, const char *text, float x, float y, Color color, int flags) {
uint32_t alpha = (color >> 1) & 0xFF000000;
DrawText(font, text, x + 2, y + 2, alpha, flags);
DrawText(font, text, x, y, color, flags);
}
void DrawBuffer::DoAlign(int flags, float *x, float *y, float *w, float *h) {
if (flags & ALIGN_HCENTER) *x -= *w / 2;
if (flags & ALIGN_RIGHT) *x -= *w;
if (flags & ALIGN_VCENTER) *y -= *h / 2;
if (flags & ALIGN_BOTTOM) *y -= *h;
if (flags & (ROTATE_90DEG_LEFT | ROTATE_90DEG_RIGHT)) {
std::swap(*w, *h);
std::swap(*x, *y);
}
}
// TODO: Actually use the rect properly, take bounds.
void DrawBuffer::DrawTextRect(FontID font, const char *text, float x, float y, float w, float h, Color color, int align) {
if (align & ALIGN_HCENTER) {
x += w / 2;
} else if (align & ALIGN_RIGHT) {
x += w;
}
if (align & ALIGN_VCENTER) {
y += h / 2;
} else if (align & ALIGN_BOTTOM) {
y += h;
}
std::string toDraw = text;
int wrap = align & (FLAG_WRAP_TEXT | FLAG_ELLIPSIZE_TEXT);
const AtlasFont *atlasfont = atlas->getFont(font);
if (wrap && atlasfont) {
AtlasWordWrapper wrapper(*atlasfont, fontscalex, toDraw.c_str(), w, wrap);
toDraw = wrapper.Wrapped();
}
float totalWidth, totalHeight;
MeasureTextRect(font, toDraw.c_str(), (int)toDraw.size(), Bounds(x, y, w, h), &totalWidth, &totalHeight, align);
std::vector<std::string> lines;
SplitString(toDraw, '\n', lines);
float baseY = y;
if (align & ALIGN_VCENTER) {
baseY -= totalHeight / 2;
align = align & ~ALIGN_VCENTER;
} else if (align & ALIGN_BOTTOM) {
baseY -= totalHeight;
align = align & ~ALIGN_BOTTOM;
}
// This allows each line to be horizontally centered by itself.
for (const std::string &line : lines) {
DrawText(font, line.c_str(), x, baseY, color, align);
float tw, th;
MeasureText(font, line.c_str(), &tw, &th);
baseY += th;
}
}
// ROTATE_* doesn't yet work right.
void DrawBuffer::DrawText(FontID font, const char *text, float x, float y, Color color, int align) {
// rough estimate
size_t textLen = strlen(text);
if (count_ + textLen * 6 > MAX_VERTS) {
Flush(true);
if (textLen * 6 >= MAX_VERTS) {
textLen = std::min(MAX_VERTS / 6 - 10, (int)textLen);
}
}
const AtlasFont *atlasfont = atlas->getFont(font);
if (!atlasfont)
return;
unsigned int cval;
float w, h;
MeasureText(font, text, &w, &h);
if (align) {
DoAlign(align, &x, &y, &w, &h);
}
if (align & ROTATE_90DEG_LEFT) {
x -= atlasfont->ascend * fontscaley;
// y += h;
} else {
y += atlasfont->ascend * fontscaley;
}
float sx = x;
UTF8 utf(text);
for (size_t i = 0; i < textLen; i++) {
if (utf.end())
break;
cval = utf.next();
// Translate non-breaking space to space.
if (cval == 0xA0) {
cval = ' ';
} else if (cval == '\n') {
y += atlasfont->height * fontscaley;
x = sx;
continue;
} else if (cval == '\t') {
cval = ' ';
} else if (cval == '&' && utf.peek() != '&') {
// Ignore lone ampersands
continue;
}
const AtlasChar *ch = atlasfont->getChar(cval);
if (!ch)
ch = atlasfont->getChar('?');
if (ch) {
const AtlasChar &c = *ch;
float cx1, cy1, cx2, cy2;
if (align & ROTATE_90DEG_LEFT) {
cy1 = y - c.ox * fontscalex;
cx1 = x + c.oy * fontscaley;
cy2 = y - (c.ox + c.pw) * fontscalex;
cx2 = x + (c.oy + c.ph) * fontscaley;
} else {
cx1 = x + c.ox * fontscalex;
cy1 = y + c.oy * fontscaley;
cx2 = x + (c.ox + c.pw) * fontscalex;
cy2 = y + (c.oy + c.ph) * fontscaley;
}
V(cx1, cy1, color, c.sx, c.sy);
V(cx2, cy1, color, c.ex, c.sy);
V(cx2, cy2, color, c.ex, c.ey);
V(cx1, cy1, color, c.sx, c.sy);
V(cx2, cy2, color, c.ex, c.ey);
V(cx1, cy2, color, c.sx, c.ey);
if (align & ROTATE_90DEG_LEFT)
y -= c.wx * fontscalex;
else
x += c.wx * fontscalex;
}
}
}