-
Notifications
You must be signed in to change notification settings - Fork 53
/
bgfxvg_renderer.cpp
4546 lines (3820 loc) · 139 KB
/
bgfxvg_renderer.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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// TODO:
// 4) Layers
// 5) Check polygon winding and force CCW order because otherwise AA fringes are generated inside the polygon!
#include "vg.h"
#include "bgfxvg_renderer.h"
#include "shape.h"
#include "path.h"
#include "stroker.h"
//#define FONTSTASH_IMPLEMENTATION
#include "../nanovg/fontstash.h"
#include <bgfx/embedded_shader.h>
#include <bx/allocator.h>
#include <bx/math.h>
#include <bx/mutex.h>
#include <bx/simd_t.h>
#include <bx/handlealloc.h>
#include <memory.h>
#include <math.h>
#include <float.h>
#include "util.h"
// Shaders
#include "shaders/vs_solid_color.bin.h"
#include "shaders/fs_solid_color.bin.h"
#include "shaders/vs_gradient.bin.h"
#include "shaders/fs_gradient.bin.h"
#include "shaders/vs_stencil.bin.h"
#include "shaders/fs_stencil.bin.h"
BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4127) // conditional expression is constant (e.g. BezierTo)
BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4706) // assignment within conditional expression
BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wshadow");
namespace vg
{
#define MAX_STATE_STACK_SIZE 16
#define MAX_VB_VERTICES 65536
#define MAX_TEXTURES 64
#define MAX_FONT_IMAGES 4
#define MIN_FONT_ATLAS_SIZE 512
#if !defined(FONS_QUAD_SIMD) || !FONS_QUAD_SIMD
#error "FONS_QUAD_SIMD should be set to 1"
#endif
#if !FONS_CUSTOM_WHITE_RECT
#pragma message("FONS_CUSTOM_WHITE_RECT should be set to 1 to avoid UV problems with cached shapes")
#endif
static const bgfx::EmbeddedShader s_EmbeddedShaders[] =
{
BGFX_EMBEDDED_SHADER(vs_solid_color),
BGFX_EMBEDDED_SHADER(fs_solid_color),
BGFX_EMBEDDED_SHADER(vs_gradient),
BGFX_EMBEDDED_SHADER(fs_gradient),
BGFX_EMBEDDED_SHADER(vs_stencil),
BGFX_EMBEDDED_SHADER(fs_stencil),
BGFX_EMBEDDED_SHADER_END()
};
#define CMD_READ(type, buffer) *(type*)buffer; buffer += sizeof(type);
#if VG_CONFIG_UV_INT16
typedef int16_t uv_t;
#else
typedef float uv_t;
#endif
struct State
{
float m_TransformMtx[6];
float m_ScissorRect[4];
float m_GlobalAlpha;
float m_FontScale;
float m_AvgScale;
};
struct VertexBuffer
{
float* m_Pos;
uv_t* m_UV;
uint32_t* m_Color;
uint32_t m_Count;
bgfx::DynamicVertexBufferHandle m_PosBufferHandle;
bgfx::DynamicVertexBufferHandle m_UVBufferHandle;
bgfx::DynamicVertexBufferHandle m_ColorBufferHandle;
};
struct IndexBuffer
{
uint16_t* m_Indices;
uint32_t m_Count;
uint32_t m_Capacity;
bgfx::DynamicIndexBufferHandle m_bgfxHandle;
};
struct ImagePattern
{
float m_Matrix[6];
float m_Alpha;
ImageHandle m_ImageHandle;
};
struct Gradient
{
float m_Matrix[9];
float m_Params[4]; // {Extent.x, Extent.y, Radius, Feather}
float m_InnerColor[4];
float m_OuterColor[4];
};
// All textures are RGBA
struct Image
{
uint16_t m_Width;
uint16_t m_Height;
uint32_t m_Flags;
bgfx::TextureHandle m_bgfxHandle;
inline void reset()
{
m_bgfxHandle = BGFX_INVALID_HANDLE;
m_Width = 0;
m_Height = 0;
m_Flags = 0;
}
};
struct ClipState
{
ClipRule::Enum m_Rule;
uint32_t m_FirstCmdID;
uint32_t m_NumCmds;
};
struct DrawCommand
{
enum Type : uint32_t
{
Type_TexturedVertexColor = 0,
Type_ColorGradient = 1,
Type_Clip = 2,
NumTypes
};
Type m_Type;
ClipState m_ClipState;
uint32_t m_VertexBufferID;
uint32_t m_FirstVertexID;
uint32_t m_FirstIndexID;
uint32_t m_NumVertices;
uint32_t m_NumIndices;
uint16_t m_ScissorRect[4];
uint16_t m_HandleID; // Type_TexturedVertexColor => ImageHandle, Type_ColorGradient => GradientHandle
};
struct CachedDrawCommand
{
float* m_Pos;
uv_t* m_UV;
uint32_t* m_Color;
uint16_t* m_Indices;
uint16_t m_NumVertices;
uint16_t m_NumIndices;
};
struct CachedTextCommand
{
String* m_Text;
uint32_t m_Alignment;
Color m_Color;
float m_Pos[2];
};
struct CachedShape
{
CachedDrawCommand* m_DrawCommands;
CachedTextCommand* m_StaticTextCommands;
const uint8_t** m_TextCommands;
uint32_t m_NumDrawCommands;
uint32_t m_NumTextCommands;
uint32_t m_NumStaticTextCommands;
float m_InvTransformMtx[6];
float m_AvgScale;
CachedShape()
: m_AvgScale(0.0f)
, m_DrawCommands(nullptr)
, m_NumDrawCommands(0)
, m_TextCommands(nullptr)
, m_StaticTextCommands(nullptr)
, m_NumTextCommands(0)
, m_NumStaticTextCommands(0)
{
bx::memSet(m_InvTransformMtx, 0, sizeof(float) * 6);
m_InvTransformMtx[0] = m_InvTransformMtx[3] = 1.0f;
}
~CachedShape()
{}
};
struct Context
{
bx::AllocatorI* m_Allocator;
VertexBuffer* m_VertexBuffers;
uint32_t m_NumVertexBuffers;
uint32_t m_VertexBufferCapacity;
float** m_Vec2DataPool;
uint32_t m_Vec2DataPoolCapacity;
uint32_t** m_Uint32DataPool;
uint32_t m_Uint32DataPoolCapacity;
#if VG_CONFIG_UV_INT16
int16_t** m_UVDataPool;
uint32_t m_UVDataPoolCapacity;
#endif
#if BX_CONFIG_SUPPORTS_THREADING
bx::Mutex m_DataPoolMutex;
#endif
DrawCommand* m_DrawCommands;
uint32_t m_NumDrawCommands;
uint32_t m_DrawCommandCapacity;
ClipState m_ClipState;
DrawCommand* m_ClipCommands;
uint32_t m_NumClipCommands;
uint32_t m_ClipCommandCapacity;
bool m_RecordClipCommands;
Image* m_Images;
uint32_t m_ImageCapacity;
bx::HandleAllocT<MAX_TEXTURES> m_ImageAlloc;
Path* m_Path;
Stroker* m_Stroker;
float* m_TransformedPathVertices;
uint32_t m_NumTransformedPathVertices;
bool m_PathVerticesTransformed;
State m_StateStack[MAX_STATE_STACK_SIZE];
uint32_t m_CurStateID;
IndexBuffer* m_IndexBuffers;
IndexBuffer* m_ActiveIndexBuffer;
uint32_t m_NumIndexBuffers;
FONSquad* m_TextQuads;
float* m_TextVertices;
uint32_t m_TextQuadCapacity;
FONScontext* m_FontStashContext;
FONSstring m_TextString;
ImageHandle m_FontImages[MAX_FONT_IMAGES];
int m_FontImageID;
void* m_FontData[VG_CONFIG_MAX_FONTS];
uint32_t m_NextFontID;
Gradient m_Gradients[VG_CONFIG_MAX_GRADIENTS];
uint32_t m_NextGradientID;
ImagePattern m_ImagePatterns[VG_CONFIG_MAX_IMAGE_PATTERNS];
uint32_t m_NextImagePatternID;
bgfx::VertexDecl m_PosVertexDecl;
bgfx::VertexDecl m_UVVertexDecl;
bgfx::VertexDecl m_ColorVertexDecl;
bgfx::ProgramHandle m_ProgramHandle[DrawCommand::NumTypes];
bgfx::UniformHandle m_TexUniform;
bgfx::UniformHandle m_PaintMatUniform;
bgfx::UniformHandle m_ExtentRadiusFeatherUniform;
bgfx::UniformHandle m_InnerColorUniform;
bgfx::UniformHandle m_OuterColorUniform;
uint16_t m_WinWidth;
uint16_t m_WinHeight;
float m_DevicePixelRatio;
float m_TesselationTolerance;
float m_FringeWidth;
uint8_t m_ViewID;
bool m_ForceNewDrawCommand;
bool m_ForceNewClipCommand;
Context(bx::AllocatorI* allocator, uint8_t viewID);
~Context();
// Helpers...
inline State* getState() { return &m_StateStack[m_CurStateID]; }
inline VertexBuffer* getVertexBuffer() { return &m_VertexBuffers[m_NumVertexBuffers - 1]; }
#if VG_CONFIG_UV_INT16
inline void getWhitePixelUV(int16_t* uv)
{
int w, h;
getImageSize(m_FontImages[0], &w, &h);
uv[0] = INT16_MAX / (int16_t)w;
uv[1] = INT16_MAX / (int16_t)h;
}
#else
inline void getWhitePixelUV(float* uv)
{
int w, h;
getImageSize(m_FontImages[0], &w, &h);
uv[0] = 0.5f / (float)w;
uv[1] = 0.5f / (float)h;
}
#endif
bool init();
void beginFrame(uint32_t windowWidth, uint32_t windowHeight, float devicePixelRatio);
void endFrame();
// Commands
void beginPath();
void moveTo(float x, float y);
void lineTo(float x, float y);
void bezierTo(float c1x, float c1y, float c2x, float c2y, float x, float y);
void arcTo(float x1, float y1, float x2, float y2, float radius);
void rect(float x, float y, float w, float h);
void roundedRect(float x, float y, float w, float h, float r);
void roundedRectVarying(float x, float y, float w, float h, float rtl, float rbl, float rbr, float rtr);
void circle(float cx, float cy, float r);
void polyline(const float* coords, uint32_t numPoints);
void closePath();
void fillConvexPath(Color col, bool aa);
void fillConvexPath(GradientHandle gradient, bool aa);
void fillConvexPath(ImagePatternHandle img, bool aa);
void fillConcavePath(Color col, bool aa);
void strokePath(Color col, float width, bool aa, LineCap::Enum lineCap, LineJoin::Enum lineJoin);
void strokePath(vg::GradientHandle gradient, float width, bool aa, LineCap::Enum lineCap, LineJoin::Enum lineJoin);
GradientHandle createLinearGradient(float sx, float sy, float ex, float ey, Color icol, Color ocol);
GradientHandle createBoxGradient(float x, float y, float w, float h, float r, float f, Color icol, Color ocol);
GradientHandle createRadialGradient(float cx, float cy, float inr, float outr, Color icol, Color ocol);
ImagePatternHandle createImagePattern(float cx, float cy, float w, float h, float angle, ImageHandle image, float alpha);
void text(const Font& font, uint32_t alignment, Color color, float x, float y, const char* text, const char* end);
void textBox(const Font& font, uint32_t alignment, Color color, float x, float y, float breakWidth, const char* text, const char* end);
float calcTextBounds(const Font& font, uint32_t alignment, float x, float y, const char* text, const char* end, float* bounds);
void calcTextBoxBounds(const Font& font, uint32_t alignment, float x, float y, float breakWidth, const char* text, const char* end, float* bounds, uint32_t flags);
float getTextLineHeight(const Font& font, uint32_t alignment);
int textBreakLines(const Font& font, uint32_t alignment, const char* text, const char* end, float breakRowWidth, TextRow* rows, int maxRows, uint32_t flags);
int textGlyphPositions(const Font& font, uint32_t alignment, float x, float y, const char* text, const char* end, GlyphPosition* positions, int maxPositions);
// Clipping
void beginClip(ClipRule::Enum rule);
void endClip();
void resetClip();
// State
void pushState();
void popState();
void resetScissor();
void setScissor(float x, float y, float w, float h);
bool intersectScissor(float x, float y, float w, float h);
void setGlobalAlpha(float alpha);
void onTransformationMatrixUpdated();
void transformIdentity();
void transformScale(float x, float y);
void transformTranslate(float x, float y);
void transformRotate(float ang_rad);
void transformMult(const float* mtx, bool pre);
void transformPath();
// Shapes
Shape* createShape(uint32_t flags);
void destroyShape(Shape* shape);
void submitShape(Shape* shape, GetStringByIDFunc* stringCallback, void* userData);
// Vertex buffers
VertexBuffer* allocVertexBuffer();
float* allocVertexBufferData_Vec2();
uint32_t* allocVertexBufferData_Uint32();
void releaseVertexBufferData_Vec2(float* data);
void releaseVertexBufferData_Uint32(uint32_t* data);
#if VG_CONFIG_UV_INT16
int16_t* allocVertexBufferData_UV();
void releaseVertexBufferData_UV(int16_t* data);
#endif
// Index buffers
IndexBuffer* allocIndexBuffer();
void releaseIndexBuffer(uint16_t* data);
// Draw commands
DrawCommand* allocDrawCommand_TexturedVertexColor(uint32_t numVertices, uint32_t numIndices, ImageHandle img);
DrawCommand* allocDrawCommand_ColorGradient(uint32_t numVertices, uint32_t numIndices, GradientHandle gradient);
DrawCommand* allocDrawCommand_Clip(uint32_t numVertices, uint32_t numIndices);
void createDrawCommand_VertexColor(const float* vtx, uint32_t numVertices, const uint32_t* colors, uint32_t numColors, const uint16_t* indices, uint32_t numIndices);
void createDrawCommand_Textured(const float* vtx, uint32_t numVertices, const uint32_t* colors, uint32_t numColors, ImageHandle imgHandle, const float* uvMatrix, const uint16_t* indices, uint32_t numIndices);
void createDrawCommand_Gradient(const float* vtx, uint32_t numVertices, GradientHandle gradientHandle, const uint16_t* indices, uint32_t numIndices);
void createDrawCommand_Clip(const float* vtx, uint32_t numVertices, const uint16_t* indices, uint32_t numIndices);
// Textures
ImageHandle allocImage();
ImageHandle createImageRGBA(uint16_t width, uint16_t height, uint32_t flags, const uint8_t* data);
bool updateImage(ImageHandle img, uint16_t x, uint16_t y, uint16_t w, uint16_t h, const uint8_t* data);
bool deleteImage(ImageHandle img);
void getImageSize(ImageHandle image, int* w, int* h);
bool isImageHandleValid(ImageHandle image);
// Fonts
FontHandle loadFontFromMemory(const char* name, const uint8_t* data, uint32_t size);
bool allocTextAtlas();
void flushTextAtlasTexture();
// Rendering
void renderTextQuads(uint32_t numQuads, Color color);
// Caching
void cachedShapeReset(CachedShape* shape);
void cachedShapeAddDrawCommand(CachedShape* shape, const DrawCommand* cmd);
void cachedShapeAddTextCommand(CachedShape* shape, const uint8_t* cmdData);
void cachedShapeRender(const CachedShape* shape, GetStringByIDFunc* stringCallback, void* userData);
// Strings
String* createString(const Font& font, const char* text, const char* end);
void destroyString(String* str);
void text(String* str, uint32_t alignment, Color color, float x, float y);
};
static void releaseVertexBufferDataCallback_Vec2(void* ptr, void* userData)
{
Context* ctx = (Context*)userData;
ctx->releaseVertexBufferData_Vec2((float*)ptr);
}
static void releaseVertexBufferDataCallback_Uint32(void* ptr, void* userData)
{
Context* ctx = (Context*)userData;
ctx->releaseVertexBufferData_Uint32((uint32_t*)ptr);
}
#if VG_CONFIG_UV_INT16
static void releaseVertexBufferDataCallback_UV(void* ptr, void* userData)
{
Context* ctx = (Context*)userData;
ctx->releaseVertexBufferData_UV((int16_t*)ptr);
}
#endif
static void releaseIndexBufferCallback(void* ptr, void* userData)
{
Context* ctx = (Context*)userData;
ctx->releaseIndexBuffer((uint16_t*)ptr);
}
inline void transformPos2D(float x, float y, const float* mtx, float* res)
{
res[0] = mtx[0] * x + mtx[2] * y + mtx[4];
res[1] = mtx[1] * x + mtx[3] * y + mtx[5];
}
inline void transformVec2D(float x, float y, const float* mtx, float* res)
{
res[0] = mtx[0] * x + mtx[2] * y;
res[1] = mtx[1] * x + mtx[3] * y;
}
inline bool invertMatrix3(const float* t, float* inv)
{
// nvgTransformInverse
double invdet, det = (double)t[0] * t[3] - (double)t[2] * t[1];
if (det > -1e-6 && det < 1e-6) {
bx::memSet(inv, 0, sizeof(float) * 6);
inv[0] = inv[2] = 1.0f;
return false;
}
invdet = 1.0 / det;
inv[0] = (float)(t[3] * invdet);
inv[2] = (float)(-t[2] * invdet);
inv[4] = (float)(((double)t[2] * t[5] - (double)t[3] * t[4]) * invdet);
inv[1] = (float)(-t[1] * invdet);
inv[3] = (float)(t[0] * invdet);
inv[5] = (float)(((double)t[1] * t[4] - (double)t[0] * t[5]) * invdet);
return true;
}
inline void multiplyMatrix3(const float* __restrict a, const float* __restrict b, float* __restrict res)
{
res[0] = a[0] * b[0] + a[2] * b[1];
res[1] = a[1] * b[0] + a[3] * b[1];
res[2] = a[0] * b[2] + a[2] * b[3];
res[3] = a[1] * b[2] + a[3] * b[3];
res[4] = a[0] * b[4] + a[2] * b[5] + a[4];
res[5] = a[1] * b[4] + a[3] * b[5] + a[5];
}
inline float getAverageScale(const float* t)
{
// nvg__getAverageScale
float sx = bx::sqrt(t[0] * t[0] + t[2] * t[2]);
float sy = bx::sqrt(t[1] * t[1] + t[3] * t[3]);
return (sx + sy) * 0.5f;
}
inline float quantize(float a, float d)
{
return ((int)(a / d + 0.5f)) * d;
}
inline float getFontScale(const float* xform)
{
return bx::min<float>(quantize(getAverageScale(xform), 0.01f), 4.0f);
}
void batchTransformPositions(const float* __restrict v, uint32_t n, float* __restrict p, const float* __restrict mtx);
void batchTransformPositions_Unaligned(const float* __restrict v, uint32_t n, float* __restrict p, const float* __restrict mtx);
void batchTransformDrawIndices(const uint16_t* __restrict src, uint32_t n, uint16_t* __restrict dst, uint16_t delta);
void batchTransformTextQuads(const FONSquad* __restrict quads, uint32_t n, const float* __restrict mtx, float* __restrict transformedVertices);
#if VG_CONFIG_UV_INT16
void generateUVs(const float* __restrict v, uint32_t n, int16_t* __restrict uv, const float* __restrict mtx);
#endif
//////////////////////////////////////////////////////////////////////////
// BGFXVGRenderer
//
BGFXVGRenderer::BGFXVGRenderer() : m_Context(nullptr)
{
}
BGFXVGRenderer::~BGFXVGRenderer()
{
if (m_Context) {
BX_DELETE(m_Context->m_Allocator, m_Context);
m_Context = nullptr;
}
}
bool BGFXVGRenderer::init(uint8_t viewID, bx::AllocatorI* allocator)
{
m_Context = (Context*)BX_NEW(allocator, Context)(allocator, viewID);
if (!m_Context) {
return false;
}
if (!m_Context->init()) {
BX_DELETE(allocator, m_Context);
return false;
}
return true;
}
void BGFXVGRenderer::BeginFrame(uint32_t windowWidth, uint32_t windowHeight, float devicePixelRatio)
{
m_Context->beginFrame(windowWidth, windowHeight, devicePixelRatio);
}
void BGFXVGRenderer::EndFrame()
{
m_Context->endFrame();
}
void BGFXVGRenderer::BeginPath()
{
m_Context->beginPath();
}
void BGFXVGRenderer::MoveTo(float x, float y)
{
m_Context->moveTo(x, y);
}
void BGFXVGRenderer::LineTo(float x, float y)
{
m_Context->lineTo(x, y);
}
void BGFXVGRenderer::BezierTo(float c1x, float c1y, float c2x, float c2y, float x, float y)
{
m_Context->bezierTo(c1x, c1y, c2x, c2y, x, y);
}
void BGFXVGRenderer::ArcTo(float x1, float y1, float x2, float y2, float radius)
{
m_Context->arcTo(x1, y1, x2, y2, radius);
}
void BGFXVGRenderer::Rect(float x, float y, float w, float h)
{
m_Context->rect(x, y, w, h);
}
void BGFXVGRenderer::RoundedRect(float x, float y, float w, float h, float r)
{
m_Context->roundedRect(x, y, w, h, r);
}
void BGFXVGRenderer::RoundedRectVarying(float x, float y, float w, float h, float rtl, float rbl, float rbr, float rtr)
{
m_Context->roundedRectVarying(x, y, w, h, rtl, rbl, rbr, rtr);
}
void BGFXVGRenderer::Circle(float cx, float cy, float r)
{
m_Context->circle(cx, cy, r);
}
void BGFXVGRenderer::Polyline(const float* coords, uint32_t numPoints)
{
m_Context->polyline(coords, numPoints);
}
void BGFXVGRenderer::ClosePath()
{
m_Context->closePath();
}
void BGFXVGRenderer::FillConvexPath(Color col, bool aa)
{
#if VG_CONFIG_FORCE_AA_OFF
aa = false;
#endif
m_Context->fillConvexPath(col, aa);
}
void BGFXVGRenderer::FillConvexPath(GradientHandle gradient, bool aa)
{
#if VG_CONFIG_FORCE_AA_OFF
aa = false;
#endif
m_Context->fillConvexPath(gradient, aa);
}
void BGFXVGRenderer::FillConvexPath(ImagePatternHandle img, bool aa)
{
#if VG_CONFIG_FORCE_AA_OFF
aa = false;
#endif
m_Context->fillConvexPath(img, aa);
}
void BGFXVGRenderer::FillConcavePath(Color col, bool aa)
{
#if VG_CONFIG_FORCE_AA_OFF
aa = false;
#endif
m_Context->fillConcavePath(col, aa);
}
void BGFXVGRenderer::StrokePath(Color col, float width, bool aa, LineCap::Enum lineCap, LineJoin::Enum lineJoin)
{
#if VG_CONFIG_FORCE_AA_OFF
aa = false;
#endif
m_Context->strokePath(col, width, aa, lineCap, lineJoin);
}
void BGFXVGRenderer::StrokePath(vg::GradientHandle gradient, float width, bool aa, LineCap::Enum lineCap, LineJoin::Enum lineJoin)
{
#if VG_CONFIG_FORCE_AA_OFF
aa = false;
#endif
m_Context->strokePath(gradient, width, aa, lineCap, lineJoin);
}
void BGFXVGRenderer::BeginClip(ClipRule::Enum rule)
{
m_Context->beginClip(rule);
}
void BGFXVGRenderer::EndClip()
{
m_Context->endClip();
}
void BGFXVGRenderer::ResetClip()
{
m_Context->resetClip();
}
GradientHandle BGFXVGRenderer::LinearGradient(float sx, float sy, float ex, float ey, Color icol, Color ocol)
{
return m_Context->createLinearGradient(sx, sy, ex, ey, icol, ocol);
}
GradientHandle BGFXVGRenderer::BoxGradient(float x, float y, float w, float h, float r, float f, Color icol, Color ocol)
{
return m_Context->createBoxGradient(x, y, w, h, r, f, icol, ocol);
}
GradientHandle BGFXVGRenderer::RadialGradient(float cx, float cy, float inr, float outr, Color icol, Color ocol)
{
return m_Context->createRadialGradient(cx, cy, inr, outr, icol, ocol);
}
ImagePatternHandle BGFXVGRenderer::ImagePattern(float cx, float cy, float w, float h, float angle, ImageHandle image, float alpha)
{
return m_Context->createImagePattern(cx, cy, w, h, angle, image, alpha);
}
ImageHandle BGFXVGRenderer::CreateImageRGBA(uint16_t w, uint16_t h, uint32_t imageFlags, const uint8_t* data)
{
return m_Context->createImageRGBA(w, h, imageFlags, data);
}
void BGFXVGRenderer::UpdateImage(ImageHandle image, const uint8_t* data)
{
if (!isValid(image)) {
return;
}
int w, h;
m_Context->getImageSize(image, &w, &h);
m_Context->updateImage(image, 0, 0, (uint16_t)w, (uint16_t)h, data);
}
void BGFXVGRenderer::UpdateSubImage(ImageHandle image, uint16_t x, uint16_t y, uint16_t w, uint16_t h, const uint8_t* data)
{
m_Context->updateImage(image, x, y, w, h, data);
}
void BGFXVGRenderer::GetImageSize(ImageHandle image, int* w, int* h)
{
m_Context->getImageSize(image, w, h);
}
void BGFXVGRenderer::DeleteImage(ImageHandle image)
{
m_Context->deleteImage(image);
}
bool BGFXVGRenderer::IsImageHandleValid(ImageHandle image)
{
return m_Context->isImageHandleValid(image);
}
void BGFXVGRenderer::PushState()
{
m_Context->pushState();
}
void BGFXVGRenderer::PopState()
{
m_Context->popState();
}
void BGFXVGRenderer::ResetScissor()
{
m_Context->resetScissor();
}
void BGFXVGRenderer::Scissor(float x, float y, float w, float h)
{
m_Context->setScissor(x, y, w, h);
}
bool BGFXVGRenderer::IntersectScissor(float x, float y, float w, float h)
{
return m_Context->intersectScissor(x, y, w, h);
}
void BGFXVGRenderer::LoadIdentity()
{
m_Context->transformIdentity();
}
void BGFXVGRenderer::Scale(float x, float y)
{
m_Context->transformScale(x, y);
}
void BGFXVGRenderer::Translate(float x, float y)
{
m_Context->transformTranslate(x, y);
}
void BGFXVGRenderer::Rotate(float ang_rad)
{
m_Context->transformRotate(ang_rad);
}
void BGFXVGRenderer::ApplyTransform(const float* mtx, bool pre)
{
m_Context->transformMult(mtx, pre);
}
void BGFXVGRenderer::SetGlobalAlpha(float alpha)
{
m_Context->setGlobalAlpha(alpha);
}
void BGFXVGRenderer::Text(const Font& font, uint32_t alignment, Color color, float x, float y, const char* text, const char* end)
{
m_Context->text(font, alignment, color, x, y, text, end);
}
void BGFXVGRenderer::TextBox(const Font& font, uint32_t alignment, Color color, float x, float y, float breakWidth, const char* text, const char* end)
{
m_Context->textBox(font, alignment, color, x, y, breakWidth, text, end);
}
float BGFXVGRenderer::CalcTextBounds(const Font& font, uint32_t alignment, float x, float y, const char* text, const char* end, float* bounds)
{
return m_Context->calcTextBounds(font, alignment, x, y, text, end, bounds);
}
void BGFXVGRenderer::CalcTextBoxBounds(const Font& font, uint32_t alignment, float x, float y, float breakWidth, const char* text, const char* end, float* bounds, uint32_t flags)
{
m_Context->calcTextBoxBounds(font, alignment, x, y, breakWidth, text, end, bounds, flags);
}
float BGFXVGRenderer::GetTextLineHeight(const Font& font, uint32_t alignment)
{
return m_Context->getTextLineHeight(font, alignment);
}
int BGFXVGRenderer::TextBreakLines(const Font& font, uint32_t alignment, const char* text, const char* end, float breakRowWidth, TextRow* rows, int maxRows, uint32_t flags)
{
return m_Context->textBreakLines(font, alignment, text, end, breakRowWidth, rows, maxRows, flags);
}
int BGFXVGRenderer::TextGlyphPositions(const Font& font, uint32_t alignment, float x, float y, const char* text, const char* end, GlyphPosition* positions, int maxPositions)
{
return m_Context->textGlyphPositions(font, alignment, x, y, text, end, positions, maxPositions);
}
FontHandle BGFXVGRenderer::LoadFontFromMemory(const char* name, const uint8_t* data, uint32_t size)
{
return m_Context->loadFontFromMemory(name, data, size);
}
Font BGFXVGRenderer::CreateFontWithSize(const char* name, float size)
{
Font f;
f.m_Handle.idx = (uint16_t)fonsGetFontByName(m_Context->m_FontStashContext, name);
f.m_Size = size;
return f;
}
Shape* BGFXVGRenderer::CreateShape(uint32_t flags)
{
return m_Context->createShape(flags);
}
void BGFXVGRenderer::DestroyShape(Shape* shape)
{
m_Context->destroyShape(shape);
}
void BGFXVGRenderer::SubmitShape(Shape* shape)
{
m_Context->submitShape(shape, nullptr, nullptr);
}
#if VG_CONFIG_SHAPE_DYNAMIC_TEXT
void BGFXVGRenderer::SubmitShape(Shape* shape, GetStringByIDFunc stringCallback, void* userData)
{
m_Context->submitShape(shape, &stringCallback, userData);
}
#endif // VG_CONFIG_SHAPE_DYNAMIC_TEXT
String* BGFXVGRenderer::CreateString(const char* fontName, float fontSize, const char* text, const char* end)
{
return m_Context->createString(CreateFontWithSize(fontName, fontSize), text, end);
}
void BGFXVGRenderer::DestroyString(String* str)
{
m_Context->destroyString(str);
}
void BGFXVGRenderer::Text(String* str, uint32_t alignment, Color color, float x, float y)
{
m_Context->text(str, alignment, color, x, y);
}
//////////////////////////////////////////////////////////////////////////
// Context
//
Context::Context(bx::AllocatorI* allocator, uint8_t viewID)
: m_Allocator(allocator)
, m_VertexBuffers(nullptr)
, m_NumVertexBuffers(0)
, m_VertexBufferCapacity(0)
, m_Vec2DataPool(nullptr)
, m_Uint32DataPool(nullptr)
, m_Vec2DataPoolCapacity(0)
, m_Uint32DataPoolCapacity(0)
#if VG_CONFIG_UV_INT16
, m_UVDataPool(nullptr)
, m_UVDataPoolCapacity(0)
#endif
, m_DrawCommands(nullptr)
, m_NumDrawCommands(0)
, m_DrawCommandCapacity(0)
, m_ClipCommands(nullptr)
, m_NumClipCommands(0)
, m_ClipCommandCapacity(0)
, m_Images(nullptr)
, m_ImageCapacity(0)
, m_Path(nullptr)
, m_Stroker(nullptr)
, m_TransformedPathVertices(nullptr)
, m_NumTransformedPathVertices(0)
, m_PathVerticesTransformed(false)
, m_CurStateID(0)
, m_IndexBuffers(nullptr)
, m_NumIndexBuffers(0)
, m_ActiveIndexBuffer(nullptr)
, m_TextQuads(nullptr)
, m_TextVertices(nullptr)
, m_TextQuadCapacity(0)
, m_FontStashContext(nullptr)
, m_FontImageID(0)
, m_WinWidth(0)
, m_WinHeight(0)
, m_DevicePixelRatio(1.0f)
, m_TesselationTolerance(1.0f)
, m_FringeWidth(1.0f)
, m_ViewID(viewID)
, m_ForceNewDrawCommand(false)
, m_ForceNewClipCommand(false)
, m_NextFontID(0)
, m_NextGradientID(0)
, m_NextImagePatternID(0)
, m_RecordClipCommands(false)
{
for (uint32_t i = 0; i < MAX_FONT_IMAGES; ++i) {
m_FontImages[i] = VG_INVALID_HANDLE;
}
for (uint32_t i = 0; i < DrawCommand::NumTypes; ++i) {
m_ProgramHandle[i] = BGFX_INVALID_HANDLE;
}
m_TexUniform = BGFX_INVALID_HANDLE;
m_PaintMatUniform = BGFX_INVALID_HANDLE;
m_ExtentRadiusFeatherUniform = BGFX_INVALID_HANDLE;
m_InnerColorUniform = BGFX_INVALID_HANDLE;
m_OuterColorUniform = BGFX_INVALID_HANDLE;
bx::memSet(m_FontData, 0, sizeof(void*) * VG_CONFIG_MAX_FONTS);
bx::memSet(m_Gradients, 0, sizeof(Gradient) * VG_CONFIG_MAX_GRADIENTS);
bx::memSet(m_ImagePatterns, 0, sizeof(ImagePattern) * VG_CONFIG_MAX_IMAGE_PATTERNS);
bx::memSet(m_StateStack, 0, sizeof(State) * MAX_STATE_STACK_SIZE);
fonsInitString(&m_TextString);
}
Context::~Context()
{
fonsDestroyString(&m_TextString);
for (uint32_t i = 0; i < DrawCommand::NumTypes; ++i) {
if (bgfx::isValid(m_ProgramHandle[i])) {
bgfx::destroy(m_ProgramHandle[i]);
m_ProgramHandle[i] = BGFX_INVALID_HANDLE;
}
}
bgfx::destroy(m_TexUniform);
bgfx::destroy(m_PaintMatUniform);
bgfx::destroy(m_ExtentRadiusFeatherUniform);
bgfx::destroy(m_InnerColorUniform);
bgfx::destroy(m_OuterColorUniform);
for (uint32_t i = 0; i < m_VertexBufferCapacity; ++i) {
VertexBuffer* vb = &m_VertexBuffers[i];
if (bgfx::isValid(vb->m_PosBufferHandle)) {
bgfx::destroy(vb->m_PosBufferHandle);
vb->m_PosBufferHandle = BGFX_INVALID_HANDLE;
}
if (bgfx::isValid(vb->m_UVBufferHandle)) {
bgfx::destroy(vb->m_UVBufferHandle);
vb->m_UVBufferHandle = BGFX_INVALID_HANDLE;
}
if (bgfx::isValid(vb->m_ColorBufferHandle)) {
bgfx::destroy(vb->m_ColorBufferHandle);
vb->m_ColorBufferHandle = BGFX_INVALID_HANDLE;
}
}
BX_FREE(m_Allocator, m_VertexBuffers);
m_VertexBuffers = nullptr;
m_VertexBufferCapacity = 0;
m_NumVertexBuffers = 0;
for (uint32_t i = 0; i < m_NumIndexBuffers; ++i) {
IndexBuffer* ib = &m_IndexBuffers[i];
if (bgfx::isValid(ib->m_bgfxHandle)) {
bgfx::destroy(ib->m_bgfxHandle);
ib->m_bgfxHandle = BGFX_INVALID_HANDLE;
}
BX_ALIGNED_FREE(m_Allocator, ib->m_Indices, 16);
ib->m_Indices = nullptr;
ib->m_Capacity = 0;