-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnanovg_sw.h
1702 lines (1563 loc) · 58.7 KB
/
nanovg_sw.h
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
//
// Copyright (c) 2020 Stylus Labs - see LICENSE.txt
// based on nanovg and nanosvg:
// Copyright (c) 2013 Mikko Mononen [email protected]
// and stb_truetype:
// Public domain; authored from 2009-2020 by Sean Barrett / RAD Game Tools
//
#ifndef NANOVG_SW_H
#define NANOVG_SW_H
#ifdef IDE_INCLUDES
#define NANOVG_SW_IMPLEMENTATION
#include "nanovg.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
enum NVGSWcreateFlags {
NVGSW_PATHS_XC = 1<<3, // use exact coverage algorithm for path rendering
NVGSW_SDFGEN = 1<<4, // to generate distance field textures for use by another renderer
};
// Create a NanoVG context; flags should be combination of the create flags above.
NVGcontext* nvgswCreate(int flags);
void nvgswDelete(NVGcontext* ctx);
void nvgswSetFramebuffer(NVGcontext* vg, void* dest, int w, int h, int rshift, int gshift, int bshift, int ashift);
typedef void (*taskFn_t)(void*);
typedef void (*poolSubmit_t)(taskFn_t, void*);
typedef void (*poolWait_t)(void);
void nvgswSetThreading(NVGcontext* vg, int xthreads, int ythreads, poolSubmit_t submit, poolWait_t wait);
#ifdef __cplusplus
}
#endif
#endif /* NANOVG_SW_H */
#ifdef NANOVG_SW_IMPLEMENTATION
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "nanovg.h"
#ifndef NVG_LOG
#include <stdio.h>
#define NVG_LOG(...) fprintf(stderr, __VA_ARGS__)
#endif
enum SWNVGpaintType {
SWNVG_PAINT_NONE = 0,
SWNVG_PAINT_COLOR,
SWNVG_PAINT_GRAD,
SWNVG_PAINT_IMAGE,
SWNVG_PAINT_ATLAS
};
#define SWNVG__SUBSAMPLES 5
#define SWNVG__FIXSHIFT 10
#define SWNVG__FIX (1 << SWNVG__FIXSHIFT)
#define SWNVG__FIXMASK (SWNVG__FIX-1)
#define SWNVG__MEMPAGE_SIZE 1024
typedef unsigned int rgba32_t;
struct SWNVGtexture {
int id;
void* data;
int width, height;
int type;
int flags;
};
typedef struct SWNVGtexture SWNVGtexture;
struct SWNVGcall {
int type;
int flags;
int image;
int edgeOffset;
int edgeCount;
int triangleOffset;
int triangleCount;
int uniformOffset;
int imgOffset;
int bounds[4];
NVGcompositeOperationState blendFunc;
SWNVGtexture* tex;
float scissorMat[6];
float paintMat[6];
rgba32_t innerCol;
rgba32_t outerCol;
float scissorExt[2];
float scissorScale[2];
float extent[2];
float radius;
float feather;
};
typedef struct SWNVGcall SWNVGcall;
typedef struct SWNVGedge {
float x0,y0, x1,y1;
int dir;
struct SWNVGedge* next;
} SWNVGedge;
typedef struct SWNVGactiveEdge {
int x,dx;
float ey;
int dir;
struct SWNVGactiveEdge *next;
} SWNVGactiveEdge;
typedef struct SWNVGmemPage {
unsigned char mem[SWNVG__MEMPAGE_SIZE];
int size;
struct SWNVGmemPage* next;
} SWNVGmemPage;
struct SWNVGcontext;
typedef struct SWNVGthreadCtx {
struct SWNVGcontext* context;
int threadnum;
int x0, y0, x1, y1;
SWNVGactiveEdge* freelist;
SWNVGmemPage* pages;
SWNVGmemPage* curpage;
unsigned char* scanline;
int cscanline;
int* lineLimits;
} SWNVGthreadCtx;
struct SWNVGcontext {
unsigned char* bitmap;
int width, height, stride;
int rshift, gshift, bshift, ashift;
SWNVGtexture* textures;
int ntextures;
int ctextures;
int textureId;
float devicePixelRatio;
int flags;
// Per frame buffers
SWNVGcall* calls;
int ccalls;
int ncalls;
struct NVGvertex* verts;
int cverts;
int nverts;
// rasterizer data
SWNVGedge* edges;
int nedges;
int cedges;
poolSubmit_t poolSubmit;
poolWait_t poolWait;
SWNVGthreadCtx* threads;
int xthreads;
int ythreads;
float* covtex;
};
typedef struct SWNVGcontext SWNVGcontext;
#define LINEAR_TO_SRGB_DIV 2047
static rgba32_t sRGBToLinear[256];
static unsigned char linearToSRGB[LINEAR_TO_SRGB_DIV + 1];
static float sRGBgamma = 2.31f;
static void swnvg__sRGBLUTCalc()
{
int i;
for (i = 0; i < 256; ++i)
sRGBToLinear[i] = (rgba32_t)(0.5f + powf(i/255.0f, sRGBgamma)*LINEAR_TO_SRGB_DIV);
for (i = 0; i < LINEAR_TO_SRGB_DIV + 1; ++i)
linearToSRGB[i] = (unsigned char)(0.5f + powf(i/((float)LINEAR_TO_SRGB_DIV), 1/sRGBgamma)*255);
}
static SWNVGmemPage* swnvg__nextPage(SWNVGthreadCtx* r, SWNVGmemPage* cur)
{
SWNVGmemPage *newp;
// If using existing chain, return the next page in chain
if (cur != NULL && cur->next != NULL) return cur->next;
// Alloc new page
newp = (SWNVGmemPage*)malloc(sizeof(SWNVGmemPage));
if (newp == NULL) return NULL;
memset(newp, 0, sizeof(SWNVGmemPage));
// Add to linked list
if (cur != NULL)
cur->next = newp;
else
r->pages = newp;
return newp;
}
static void swnvg__resetPool(SWNVGthreadCtx* r)
{
SWNVGmemPage* p = r->pages;
while (p != NULL) {
p->size = 0;
p = p->next;
}
r->curpage = r->pages;
}
static unsigned char* swnvg__alloc(SWNVGthreadCtx* r, int size)
{
unsigned char* buf;
if (size > SWNVG__MEMPAGE_SIZE) return NULL;
if (r->curpage == NULL || r->curpage->size+size > SWNVG__MEMPAGE_SIZE) {
r->curpage = swnvg__nextPage(r, r->curpage);
}
buf = &r->curpage->mem[r->curpage->size];
r->curpage->size += size;
return buf;
}
static void swnvg__addEdge(SWNVGcontext* r, NVGvertex* vtx)
{
SWNVGedge* e;
// Skip horizontal edges
if (vtx->y0 == vtx->y1) return;
if (r->nedges+1 > r->cedges) {
r->cedges = r->cedges > 0 ? r->cedges * 2 : 64;
r->edges = (SWNVGedge*)realloc(r->edges, sizeof(SWNVGedge) * r->cedges);
if (r->edges == NULL) return;
}
e = &r->edges[r->nedges];
r->nedges++;
if (vtx->y0 < vtx->y1) {
e->x0 = vtx->x0;
e->y0 = vtx->y0 * SWNVG__SUBSAMPLES;
e->x1 = vtx->x1;
e->y1 = vtx->y1 * SWNVG__SUBSAMPLES;
e->dir = 1;
} else {
e->x0 = vtx->x1;
e->y0 = vtx->y1 * SWNVG__SUBSAMPLES;
e->x1 = vtx->x0;
e->y1 = vtx->y0 * SWNVG__SUBSAMPLES;
e->dir = -1;
}
}
static SWNVGactiveEdge* swnvg__addActive(SWNVGthreadCtx* r, SWNVGedge* e, float startPoint)
{
SWNVGactiveEdge* z;
if (r->freelist != NULL) {
// Restore from freelist.
z = r->freelist;
r->freelist = z->next;
} else {
// Alloc new edge.
z = (SWNVGactiveEdge*)swnvg__alloc(r, sizeof(SWNVGactiveEdge));
if (z == NULL) return NULL;
}
float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0);
// STBTT_assert(e->y0 <= start_point);
// round dx down to avoid going too far
if (dxdy < 0)
z->dx = (int)(-floorf(SWNVG__FIX * -dxdy));
else
z->dx = (int)floorf(SWNVG__FIX * dxdy);
z->x = (int)floorf(SWNVG__FIX * (e->x0 + dxdy * (startPoint - e->y0)));
// z->x -= off_x * FIX;
z->ey = e->y1;
z->next = 0;
z->dir = e->dir;
return z;
}
static void swnvg__freeActive(SWNVGthreadCtx* r, SWNVGactiveEdge* z)
{
z->next = r->freelist;
r->freelist = z;
}
static void swnvg__fillScanlineAA(unsigned char* scanline, int len, int x0, int x1, int i, int j)
{
int maxWeight = (255 / SWNVG__SUBSAMPLES); // weight per vertical scanline
if (i == j) {
// x0,x1 are the same pixel, so compute combined coverage
scanline[i] = (unsigned char)(scanline[i] + ((x1 - x0) * maxWeight >> SWNVG__FIXSHIFT));
} else {
if (i >= 0) // add antialiasing for x0
scanline[i] = (unsigned char)(scanline[i] + (((SWNVG__FIX - (x0 & SWNVG__FIXMASK)) * maxWeight) >> SWNVG__FIXSHIFT));
else
i = -1; // clip
if (j < len) // add antialiasing for x1
scanline[j] = (unsigned char)(scanline[j] + (((x1 & SWNVG__FIXMASK) * maxWeight) >> SWNVG__FIXSHIFT));
else
j = len; // clip
for (++i; i < j; ++i) // fill pixels between x0 and x1
scanline[i] = (unsigned char)(scanline[i] + maxWeight);
}
}
// non-AA - assumes only one (sub)sample per actual scanline
static void swnvg__fillScanline(unsigned char* scanline, int len, int x0, int x1, int i, int j)
{
unsigned char maxWeight = 255;
if (i == j) {
// x0,x1 are the same pixel - determine if they span the center
scanline[i] = (x0 & SWNVG__FIXMASK) <= SWNVG__FIX/2 && (x1 & SWNVG__FIXMASK) > SWNVG__FIX/2 ? maxWeight : 0;
} else {
if (i >= 0)
scanline[i] = (x0 & SWNVG__FIXMASK) <= SWNVG__FIX/2 ? maxWeight : 0;
else
i = -1; // clip
if (j < len)
scanline[j] = (x1 & SWNVG__FIXMASK) > SWNVG__FIX/2 ? maxWeight : 0;
else
j = len; // clip
for (++i; i < j; ++i) // fill pixels between x0 and x1
scanline[i] = maxWeight;
}
}
static void swnvg__fillActiveEdges(SWNVGthreadCtx* r, SWNVGactiveEdge* e, int* xmin, int* xmax, int flags)
{
int x0 = 0, w = 0, left = r->x0, right = r->x1;
unsigned char* scanline = r->scanline;
int len = right - left + 1;
while (e != NULL) {
if (w == 0) {
// if we're currently at zero, we need to record the edge start point
x0 = e->x;
w = (flags & NVG_PATH_EVENODD) ? 1 : w + e->dir;
} else {
int x1 = e->x;
w = (flags & NVG_PATH_EVENODD) ? 0 : w + e->dir;
// if we went to zero, we need to draw
if (w == 0) {
int i = x0 >> SWNVG__FIXSHIFT;
int j = x1 >> SWNVG__FIXSHIFT;
if (i <= right && j >= left) {
if (i < *xmin) *xmin = i;
if (j > *xmax) *xmax = j;
(flags & NVG_PATH_NO_AA) ? swnvg__fillScanline(scanline, len, x0, x1, i - left, j - left)
: swnvg__fillScanlineAA(scanline, len, x0, x1, i - left, j - left);
}
}
}
e = e->next;
}
}
static float swnvg__lengthf(float x, float y) { return sqrtf(x*x + y*y); }
static float swnvg__maxf(float a, float b) { return a < b ? b : a; }
static float swnvg__minf(float a, float b) { return a < b ? a : b; }
static float swnvg__clampf(float a, float mn, float mx) { return a < mn ? mn : (a > mx ? mx : a); }
static int swnvg__maxi(int a, int b) { return a < b ? b : a; }
static int swnvg__mini(int a, int b) { return a < b ? a : b; }
static int swnvg__clampi(int a, int mn, int mx) { return a < mn ? mn : (a > mx ? mx : a); }
static float swnvg__absf(float a) { return a >= 0.0f ? a : -a; }
#define COLOR0(c) (c & 0xff)
#define COLOR1(c) ((c >> 8) & 0xff)
#define COLOR2(c) ((c >> 16) & 0xff)
#define COLOR3(c) ((c >> 24) & 0xff)
#define RGBA32_IS_OPAQUE(c) ((c & 0xFF000000) == 0xFF000000)
// all modern compilers appear to have built-in optimizations like this for x/255 and other const division
//static inline int swnvg__div255(int x) { return ((x+1) * 257) >> 16 } ; // this isn't exact in general
// note that we assume color is not premultiplied and do the equivalent of
// glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
static void swnvg__blendSRGB(unsigned char* dst, int cover, int cr, int cg, int cb, int ca)
{
int srca = (cover * ca)/255;
int ia = 255 - srca;
// src over blend
int r = (srca*cr + ia*(unsigned int)dst[0])/255;
int g = (srca*cg + ia*(unsigned int)dst[1])/255;
int b = (srca*cb + ia*(unsigned int)dst[2])/255;
int a = srca + (ia*(unsigned int)dst[3])/255; // *ca
// write
dst[0] = (unsigned char)r;
dst[1] = (unsigned char)g;
dst[2] = (unsigned char)b;
dst[3] = (unsigned char)a;
}
static void swnvg__blendLinear(unsigned char* dst, int cover, int cr, int cg, int cb, int ca)
{
unsigned int d0 = sRGBToLinear[dst[0]];
unsigned int d1 = sRGBToLinear[dst[1]];
unsigned int d2 = sRGBToLinear[dst[2]];
unsigned int s0 = sRGBToLinear[cr];
unsigned int s1 = sRGBToLinear[cg];
unsigned int s2 = sRGBToLinear[cb];
int srca = (cover * ca)/255;
int ia = 255 - srca;
int r = (srca*s0 + ia*d0)/255;
int g = (srca*s1 + ia*d1)/255;
int b = (srca*s2 + ia*d2)/255;
int a = srca + (ia*(unsigned int)dst[3])/255; //*ca
// write to bitmap
dst[0] = linearToSRGB[r]; // swnvg__mini(r, 2047)]; -- clamping only needed for premultiplication w/
dst[1] = linearToSRGB[g]; // swnvg__mini(g, 2047)]; -- conversion back to ints
dst[2] = linearToSRGB[b]; // swnvg__mini(b, 2047)];
dst[3] = (unsigned char)a;
}
static void swnvg__blend(unsigned char* dst, int cover, int cr, int cg, int cb, int ca, int linear)
{
linear ? swnvg__blendLinear(dst, cover, cr, cg, cb, ca) : swnvg__blendSRGB(dst, cover, cr, cg, cb, ca);
}
static void swnvg__blend8888(unsigned char* dst, int cover, int cr, int cg, int cb, int ca, int linear)
{
if(cover == 255 && ca == 255) {
dst[0] = (unsigned char)cr;
dst[1] = (unsigned char)cg;
dst[2] = (unsigned char)cb;
dst[3] = 255;
}
else
swnvg__blend(dst, cover, cr, cg, cb, ca, linear);
}
// assumes color is opaque
static void swnvg__blendOpaque(unsigned char* dst, int cover, rgba32_t rgba, int linear)
{
if(cover == 255)
*(rgba32_t*)dst = rgba;
else
swnvg__blend(dst, cover, COLOR0(rgba), COLOR1(rgba), COLOR2(rgba), COLOR3(rgba), linear);
}
static rgba32_t texelFetchRGBA32(SWNVGtexture* tex, int x, int y)
{
rgba32_t* data = (rgba32_t*)tex->data;
return data[x + y*tex->width];
}
static unsigned int swnvg__mix8(float fx, float fy, int t00, int t10, int t01, int t11)
{
//return mix(mix(t00, t10, f.x), mix(t01, t11, f.x), f.y);
float t0 = t00 + fx*(t10 - t00);
float t1 = t01 + fx*(t11 - t01);
return (unsigned int)(0.5f + t0 + fy*(t1 - t0));
}
static void swnvg__lerpAndBlend(unsigned char* dst, unsigned char cover, SWNVGtexture* tex, float ijx, float ijy, int linear)
{
ijx = swnvg__maxf(0.0f, ijx); ijy = swnvg__maxf(0.0f, ijy);
int ij00x = swnvg__mini((int)ijx, tex->width-1), ij00y = swnvg__mini((int)ijy, tex->height-1);
int ij11x = swnvg__mini((int)ijx + 1, tex->width-1), ij11y = swnvg__mini((int)ijy + 1, tex->height-1);
rgba32_t t00 = texelFetchRGBA32(tex, ij00x, ij00y);
rgba32_t t10 = texelFetchRGBA32(tex, ij11x, ij00y);
rgba32_t t01 = texelFetchRGBA32(tex, ij00x, ij11y);
rgba32_t t11 = texelFetchRGBA32(tex, ij11x, ij11y);
float fx = ijx - (int)ijx, fy = ijy - (int)ijy;
int c0 = swnvg__mix8(fx, fy, COLOR0(t00), COLOR0(t10), COLOR0(t01), COLOR0(t11));
int c1 = swnvg__mix8(fx, fy, COLOR1(t00), COLOR1(t10), COLOR1(t01), COLOR1(t11));
int c2 = swnvg__mix8(fx, fy, COLOR2(t00), COLOR2(t10), COLOR2(t01), COLOR2(t11));
int c3 = swnvg__mix8(fx, fy, COLOR3(t00), COLOR3(t10), COLOR3(t01), COLOR3(t11));
swnvg__blend8888(dst, cover, c0, c1, c2, c3, linear);
}
static int swnvg__getBlendFactor(int factor, int srca, int dsta)
{
switch(factor) {
case NVG_ZERO: return 0;
case NVG_ONE: return 255;
case NVG_SRC_ALPHA: return srca;
case NVG_ONE_MINUS_SRC_ALPHA: return 255 - srca;
case NVG_DST_ALPHA: return dsta;
case NVG_ONE_MINUS_DST_ALPHA: return 255 - dsta;
default: return -255;
}
}
static void swnvg__blendWithFunc(NVGcompositeOperationState* op, unsigned char* dst, int cover, rgba32_t rgba, int linear)
{
// TODO: support linear blending
int srca = (cover * COLOR3(rgba))/255;
int dsta = dst[3];
// get blend factors
int srcRGB = swnvg__getBlendFactor(op->srcRGB, srca, dsta);
int dstRGB = swnvg__getBlendFactor(op->dstRGB, srca, dsta);
int srcAlpha = swnvg__getBlendFactor(op->srcAlpha, srca, dsta);
int dstAlpha = swnvg__getBlendFactor(op->dstAlpha, srca, dsta);
// blend
int r = (srcRGB*COLOR0(rgba) + dstRGB*dst[0])/255;
int g = (srcRGB*COLOR1(rgba) + dstRGB*dst[1])/255;
int b = (srcRGB*COLOR2(rgba) + dstRGB*dst[2])/255;
int a = (srcAlpha*srca + dstAlpha*dst[3])/255;
// write
dst[0] = (unsigned char)r;
dst[1] = (unsigned char)g;
dst[2] = (unsigned char)b;
dst[3] = (unsigned char)a;
}
static void swnvg__scanlineSolid(unsigned char* dst, int count, unsigned char* cover, int x, int y, SWNVGcall* call)
{
int i;
int linear = call->flags & NVG_SRGB ? 1 : 0;
if(call->flags & NVG_PATH_SCISSOR) {
// apply scissor factor to coverage for non-trivial (i.e. rotated or skewed) scissor
float qx, qy, ssx, ssy, sscover;
for(i = 0; i < count; ++i) {
nvgTransformPoint(&qx, &qy, call->scissorMat, x + i, y);
ssx = 0.5f - (fabsf(qx) - call->scissorExt[0])*call->scissorScale[0];
ssy = 0.5f - (fabsf(qy) - call->scissorExt[1])*call->scissorScale[1];
sscover = swnvg__clampf(ssx, 0.0f, 1.0f) * swnvg__clampf(ssy, 0.0f, 1.0f);
cover[i] = (unsigned char)(cover[i] * sscover + 0.5f);
}
}
// note r,g,b may not actually be R,G,B (in particular, R and G could be switched)
if (call->type == SWNVG_PAINT_COLOR) {
rgba32_t c = call->innerCol;
if(call->flags & NVG_PATH_BLENDFUNC) {
for(i = 0; i < count; ++i, dst += 4)
swnvg__blendWithFunc(&call->blendFunc, dst, *cover++, c, linear);
}
else if(RGBA32_IS_OPAQUE(c)) {
for(i = 0; i < count; ++i, dst += 4)
swnvg__blendOpaque(dst, *cover++, c, linear);
}
else {
for(i = 0; i < count; ++i, dst += 4)
swnvg__blend(dst, *cover++, COLOR0(c), COLOR1(c), COLOR2(c), COLOR3(c), linear);
}
} else if (call->type == SWNVG_PAINT_IMAGE) {
rgba32_t* img = (rgba32_t*)call->tex->data;
float qx, qy;
float dqx = call->paintMat[0]*call->tex->width/call->extent[0];
float dqy = call->paintMat[1]*call->tex->height/call->extent[1];
nvgTransformPoint(&qx, &qy, call->paintMat, x, y);
// +/- 0.5 determined by experiment to match nanovg_gl
qx = (qx + 0.5f)*call->tex->width/call->extent[0] - 0.5f;
qy = (qy + 0.5f)*call->tex->height/call->extent[1] - 0.5f;
for (i = 0; i < count; ++i) {
if(call->tex->flags & NVG_IMAGE_NEAREST) {
int imgx = swnvg__clampi((int)(0.5f + qx), 0, call->tex->width-1);
int imgy = swnvg__clampi((int)(0.5f + qy), 0, call->tex->height-1);
rgba32_t c = img[imgy*call->tex->width + imgx];
if(RGBA32_IS_OPAQUE(c))
swnvg__blendOpaque(dst, *cover++, c, linear);
else
swnvg__blend(dst, *cover++, COLOR0(c), COLOR1(c), COLOR2(c), COLOR3(c), linear);
}
else
swnvg__lerpAndBlend(dst, *cover++, call->tex, qx, qy, linear);
qx += dqx; // for qx,qy => qx+1,qy
qy += dqy;
dst += 4;
}
} else if (call->type == SWNVG_PAINT_GRAD) {
float qx, qy;
int cr0 = linear ? (int)sRGBToLinear[COLOR0(call->innerCol)] : COLOR0(call->innerCol);
int cg0 = linear ? (int)sRGBToLinear[COLOR1(call->innerCol)] : COLOR1(call->innerCol);
int cb0 = linear ? (int)sRGBToLinear[COLOR2(call->innerCol)] : COLOR2(call->innerCol);
int ca0 = COLOR3(call->innerCol);
int cr1 = linear ? (int)sRGBToLinear[COLOR0(call->outerCol)] : COLOR0(call->outerCol);
int cg1 = linear ? (int)sRGBToLinear[COLOR1(call->outerCol)] : COLOR1(call->outerCol);
int cb1 = linear ? (int)sRGBToLinear[COLOR2(call->outerCol)] : COLOR2(call->outerCol);
int ca1 = COLOR3(call->outerCol);
for (i = 0; i < count; ++i) {
// can't just step qx, qy due to numerical issues w/ linear gradient
nvgTransformPoint(&qx, &qy, call->paintMat, x++, y);
float dx = fabsf(qx) - (call->extent[0] - call->radius);
float dy = fabsf(qy) - (call->extent[1] - call->radius);
float d0 = swnvg__minf(swnvg__maxf(dx, dy), 0.0f)
+ swnvg__lengthf(swnvg__maxf(dx, 0.0f), swnvg__maxf(dy, 0.0f)) - call->radius;
float d = (d0 + call->feather*0.5f)/call->feather;
if (call->tex) {
// texture for gradients with >2 stops
swnvg__lerpAndBlend(dst, *cover++, call->tex, d*call->tex->width, 0, linear);
} else {
d = swnvg__clampf(d, 0.0f, 1.0f);
int cr = (int)(0.5f + cr0*(1.0f - d) + cr1*d);
int cg = (int)(0.5f + cg0*(1.0f - d) + cg1*d);
int cb = (int)(0.5f + cb0*(1.0f - d) + cb1*d);
int ca = (int)(0.5f + ca0*(1.0f - d) + ca1*d);
if(linear)
swnvg__blend8888(dst, *cover++, linearToSRGB[cr], linearToSRGB[cg], linearToSRGB[cb], ca, linear);
else
swnvg__blend8888(dst, *cover++, cr, cg, cb, ca, linear);
}
//qx += call->paintMat[0]; // instead of tf*(x+1, y)
//qy += call->paintMat[1];
dst += 4;
}
}
}
static void swnvg__rasterizeSortedEdges(SWNVGthreadCtx* r, SWNVGcall* call)
{
SWNVGcontext* gl = r->context;
SWNVGactiveEdge *active = NULL;
int y, s;
int e = call->edgeOffset;
int eend = call->edgeOffset + call->edgeCount;
int xmin, xmax, xmin1, xmax1;
for (y = swnvg__maxi(r->y0, call->bounds[1]); y <= swnvg__mini(r->y1, call->bounds[3]); y++) {
xmin = gl->width;
xmax = 0;
for (s = 0; s < SWNVG__SUBSAMPLES; ++s) {
// we only process one subsample scanline for non-AA
if((call->flags & NVG_PATH_NO_AA) && s != SWNVG__SUBSAMPLES/2)
continue;
// find center of pixel for this scanline
float scany = (float)(y*SWNVG__SUBSAMPLES + s) + 0.5f;
SWNVGactiveEdge **step = &active;
// update active edges - remove active edges that terminate before the center of this scanline
while (*step) {
SWNVGactiveEdge *z = *step;
if (z->ey <= scany) {
*step = z->next; // delete from list
swnvg__freeActive(r, z);
} else {
z->x += z->dx; // advance to position for current scanline
step = &((*step)->next); // advance through list
}
}
// resort the list if needed
for (;;) {
int changed = 0;
step = &active;
while (*step && (*step)->next) {
if ((*step)->x > (*step)->next->x) {
SWNVGactiveEdge* t = *step;
SWNVGactiveEdge* q = t->next;
t->next = q->next;
q->next = t;
*step = q;
changed = 1;
}
step = &(*step)->next;
}
if (!changed) break;
}
// insert all edges that start before the center of this scanline -- omit ones that also end on this scanline
while (e < eend && gl->edges[e].y0 <= scany) {
if (gl->edges[e].y1 > scany) {
SWNVGactiveEdge* z = swnvg__addActive(r, &gl->edges[e], scany);
if (z == NULL) break;
if (call->flags & NVG_PATH_NO_AA)
z->dx *= SWNVG__SUBSAMPLES; // AA case uses per-subscanline step
// find insertion point
if (active == NULL) {
active = z;
} else if (z->x < active->x) {
// insert at front
z->next = active;
active = z;
} else {
// find thing to insert AFTER
SWNVGactiveEdge* p = active;
while (p->next && p->next->x < z->x)
p = p->next;
// at this point, p->next->x is NOT < z->x
z->next = p->next;
p->next = z;
}
}
e++;
}
// now process all active edges in non-zero fashion
if (active != NULL)
swnvg__fillActiveEdges(r, active, &xmin, &xmax, call->flags);
}
// clip xmin, xmax for memset
xmin = swnvg__maxi(xmin, r->x0);
xmax = swnvg__mini(xmax, r->x1);
// further clip for possible scissor
xmin1 = swnvg__maxi(xmin, call->bounds[0]);
xmax1 = swnvg__mini(xmax, call->bounds[2]);
if (xmin1 <= xmax1)
swnvg__scanlineSolid(&gl->bitmap[y*gl->stride + xmin1*4], xmax1-xmin1+1, &r->scanline[xmin1 - r->x0], xmin1, y, call);
// we fill x range clipped to scissor, but we have to clear entire range written by fillActiveEdges
if (xmin <= xmax)
memset(&r->scanline[xmin - r->x0], 0, xmax-xmin+1);
}
}
static float texFetchF32(SWNVGtexture* tex, int x, int y)
{
float* data = (float*)tex->data;
return data[x + y*tex->width];
}
static float texFetchF32Lerp(SWNVGtexture* tex, float ijx, float ijy, int ijminx, int ijminy, int ijmaxx, int ijmaxy)
{
int ij00x = swnvg__clampi((int)ijx, ijminx, ijmaxx), ij00y = swnvg__clampi((int)ijy, ijminy, ijmaxy);
int ij11x = swnvg__clampi((int)ijx + 1, ijminx, ijmaxx), ij11y = swnvg__clampi((int)ijy + 1, ijminy, ijmaxy);
float t00 = texFetchF32(tex, ij00x, ij00y);
float t10 = texFetchF32(tex, ij11x, ij00y);
float t01 = texFetchF32(tex, ij00x, ij11y);
float t11 = texFetchF32(tex, ij11x, ij11y);
float fx = ijx - (int)ijx, fy = ijy - (int)ijy;
//return mix(mix(t00, t10, f.x), mix(t01, t11, f.x), f.y);
float t0 = t00 + fx*(t10 - t00);
float t1 = t01 + fx*(t11 - t01);
return t0 + fy*(t1 - t0);
}
// recall that we do clamping instead of just adding a border around each glyph because dx,dy could be large
// at small font sizes
static float summedTextCov(SWNVGtexture* tex, float ijx, float ijy, float dx, float dy, int ijminx, int ijminy, int ijmaxx, int ijmaxy)
{
// for some reason, we need to shift by an extra (-0.5, -0.5) for summed case (here or in fons__getQuad)
//ijx -= 0.499999f; ijy -= 0.499999f;
float s11 = texFetchF32Lerp(tex, ijx + dx, ijy + dy, ijminx, ijminy, ijmaxx, ijmaxy);
float s01 = texFetchF32Lerp(tex, ijx - dx, ijy + dy, ijminx, ijminy, ijmaxx, ijmaxy);
float s10 = texFetchF32Lerp(tex, ijx + dx, ijy - dy, ijminx, ijminy, ijmaxx, ijmaxy);
float s00 = texFetchF32Lerp(tex, ijx - dx, ijy - dy, ijminx, ijminy, ijmaxx, ijmaxy);
float cov = (s11 - s01 - s10 + s00)/(255.0f*4.0f*dx*dy);
return swnvg__clampf(cov, 0.0f, 1.0f);
}
static unsigned char texFetch(SWNVGtexture* tex, int x, int y)
{
unsigned char* data = (unsigned char*)tex->data;
return data[x + y*tex->width];
}
static float texFetchLerp(SWNVGtexture* tex, float ijx, float ijy) //, int ijminx, int ijminy, int ijmaxx, int ijmaxy)
{
//int ij00x = swnvg__clampi((int)ijx, ijminx, ijmaxx), ij00y = swnvg__clampi((int)ijy, ijminy, ijmaxy);
//int ij11x = swnvg__clampi((int)ijx + 1, ijminx, ijmaxx), ij11y = swnvg__clampi((int)ijy + 1, ijminy, ijmaxy);
int ij00x = (int)ijx, ij00y = (int)ijy, ij11x = (int)ijx + 1, ij11y = (int)ijy + 1;
float t00 = texFetch(tex, ij00x, ij00y);
float t10 = texFetch(tex, ij11x, ij00y);
float t01 = texFetch(tex, ij00x, ij11y);
float t11 = texFetch(tex, ij11x, ij11y);
float fx = ijx - (int)ijx, fy = ijy - (int)ijy;
float t0 = t00 + fx*(t10 - t00);
float t1 = t01 + fx*(t11 - t01);
return t0 + fy*(t1 - t0);
}
// Super-sampled SDF text - nvgFontBlur can be used to make thicker or thinner
static float sdfCov(float D, float invsdfscale, float sdfoffset)
{
return D > 0.0f ? swnvg__clampf((D - 255.0f*0.5f)*invsdfscale + sdfoffset, 0.0f, 1.0f) : 0.0f;
}
static float superSDF(SWNVGtexture* tex, float s, float dr, float ijx, float ijy, float dx, float dy) //, int ijminx, int ijminy, int ijmaxx, int ijmaxy)
{
// check distance from center of nearest pixel and exit early if large enough
// doesn't help at all for very small font sizes, but >50% for larger sizes
float d = texFetch(tex, (int)(ijx + 0.5f), (int)(ijy + 0.5f));
float sd = (d - 255.0f*0.5f)*s + (dr - 0.5f); // note we're still using the half pixel scale here
if(sd < -1.415f) return 0; // sqrt(2) ... verified experimentally
if(sd > 1.415f) return 1.0f;
// prevent out-of-bounds read; note that GL SDF renderer does not clamp to glyph cell bounds either
if(ijx - dx < 0 || ijy - dy < 0 || ijx + dx + 1 >= tex->width || ijy + dy + 1 >= tex->height)
return sdfCov(texFetchLerp(tex, ijx, ijy), s/2, dr); // single sample
float d11 = texFetchLerp(tex, ijx + dx, ijy + dy);
float d10 = texFetchLerp(tex, ijx - dx, ijy + dy);
float d01 = texFetchLerp(tex, ijx + dx, ijy - dy);
float d00 = texFetchLerp(tex, ijx - dx, ijy - dy);
return 0.25f*(sdfCov(d11, s, dr) + sdfCov(d10, s, dr) + sdfCov(d01, s, dr) + sdfCov(d00, s, dr));
}
static void swnvg__rasterizeQuad(SWNVGthreadCtx* r, SWNVGcall* call, NVGvertex* v00, NVGvertex* v11)
{
SWNVGcontext* gl = r->context;
int x, y;
float s00 = call->tex->width * v00->x1;
float t00 = call->tex->height * v00->y1;
float ds = call->paintMat[0]/2;
float dt = call->paintMat[3]/2;
float sdfoffset = 0, invsdfscale = 0; // init to suppress warning
if(gl->flags & NVG_SDF_TEXT) {
sdfoffset = call->radius + 0.5f;
invsdfscale = 1/(0.5f * 32.0f*call->paintMat[0]); // 0.5 - we're sampling 4 0.5x0.5 subpixels
}
int linear = call->flags & NVG_SRGB;
int cr = COLOR0(call->innerCol);
int cg = COLOR1(call->innerCol);
int cb = COLOR2(call->innerCol);
int ca = COLOR3(call->innerCol);
int extentx = (int)call->extent[0], extenty = (int)call->extent[1];
// use texcoord center to figure out which atlas rect we are reading from
//int ijminx = ((int)(0.5f*(v00->x1 + v11->x1)*tex->width/extentx + 0.5f))*extentx;
//int ijminy = ((int)(0.5f*(v00->y1 + v11->y1)*tex->height/extenty + 0.5f))*extenty;
int ijminx = ((int)(s00/extentx + 0.5f))*extentx, ijminy = ((int)(t00/extenty + 0.5f))*extenty;
int ijmaxx = ijminx + extentx - 1, ijmaxy = ijminy + extenty - 1;
if(ijminx < 0 || ijminy < 0) return; // something went wrong
int xmin = swnvg__maxi(swnvg__maxi(call->bounds[0], r->x0), (int)v00->x0);
int ymin = swnvg__maxi(swnvg__maxi(call->bounds[1], r->y0), (int)v00->y0);
int xmax = swnvg__mini(swnvg__mini(call->bounds[2], r->x1), (int)(ceilf(v11->x0)));
int ymax = swnvg__mini(swnvg__mini(call->bounds[3], r->y1), (int)(ceilf(v11->y0)));
if(ymin > ymax || xmin > xmax) return;
float s0 = s00 - 2*ds*(v00->x0 - xmin - 0.25f); // not sure why we need ds/2 shift to get correct pos
float t = t00 - 2*dt*(v00->y0 - ymin - 0.25f);
float cover;
for(y = ymin; y <= ymax; ++y) {
unsigned char* dst = &gl->bitmap[y*gl->stride + xmin*4];
float s = s0;
for(x = xmin; x <= xmax; ++x) {
if(gl->flags & NVG_SDF_TEXT)
cover = superSDF(call->tex, invsdfscale, sdfoffset, s, t, ds/2, dt/2);
else
cover = summedTextCov(call->tex, s, t, ds, dt, ijminx, ijminy, ijmaxx, ijmaxy);
swnvg__blend8888(dst, (int)(255.0f*cover + 0.5f), cr, cg, cb, ca, linear);
s += 2*ds;
dst += 4;
}
t += 2*dt;
}
}
static SWNVGtexture* swnvg__allocTexture(SWNVGcontext* gl)
{
SWNVGtexture* tex = NULL;
int i;
for (i = 0; i < gl->ntextures; i++) {
if (gl->textures[i].id == 0) {
tex = &gl->textures[i];
break;
}
}
if (tex == NULL) {
if (gl->ntextures+1 > gl->ctextures) {
SWNVGtexture* textures;
int ctextures = swnvg__maxi(gl->ntextures+1, 4) + gl->ctextures/2; // 1.5x Overallocate
textures = (SWNVGtexture*)realloc(gl->textures, sizeof(SWNVGtexture)*ctextures);
if (textures == NULL) return NULL;
gl->textures = textures;
gl->ctextures = ctextures;
}
tex = &gl->textures[gl->ntextures++];
}
memset(tex, 0, sizeof(*tex));
tex->id = ++gl->textureId;
return tex;
}
static SWNVGtexture* swnvg__findTexture(SWNVGcontext* gl, int id)
{
int i;
if (!id) return NULL;
for (i = 0; i < gl->ntextures; i++)
if (gl->textures[i].id == id)
return &gl->textures[i];
return NULL;
}
static int swnvg__renderCreate(void* uptr)
{
static int staticInited = 0;
#if !defined(NDEBUG) || !defined(NVGSW_QUIET_FRAME)
SWNVGcontext* gl = (SWNVGcontext*)uptr;
NVG_LOG("nvg2: software renderer%s\n", gl->flags & NVGSW_PATHS_XC ? " (XC)" : "");
#endif
if(!staticInited) {
swnvg__sRGBLUTCalc();
staticInited = 1;
}
return 1;
}
static void swnvg__copyRGBAData(SWNVGcontext* gl, SWNVGtexture* tex, const void* data)
{
int ii;
int npix = tex->width*tex->height;
rgba32_t* dest = (rgba32_t*)tex->data;
rgba32_t* src = (rgba32_t*)data;
if(tex->flags & NVG_IMAGE_PREMULTIPLIED) {
// undo premultiplication
for(ii = 0; ii < npix; ++ii, ++dest, ++src) {
int r = COLOR0(*src);
int g = COLOR1(*src);
int b = COLOR2(*src);
int a = COLOR3(*src);
*dest = ((255*r)/a << gl->rshift | (255*g)/a << gl->gshift | (255*b)/a << gl->bshift | a << gl->ashift);
}
}
else if(gl->rshift == 0 && gl->gshift == 8 && gl->bshift == 16 && gl->ashift == 24) {
memcpy(dest, src, npix*4);
}
else {
for(ii = 0; ii < npix; ++ii, ++dest, ++src) {
int r = COLOR0(*src);
int g = COLOR1(*src);
int b = COLOR2(*src);
int a = COLOR3(*src);
*dest = (r << gl->rshift | g << gl->gshift | b << gl->bshift | a << gl->ashift);
}
}
}
static int swnvg__renderCreateTexture(void* uptr, int type, int w, int h, int imageFlags, const void* data)
{
SWNVGcontext* gl = (SWNVGcontext*)uptr;
SWNVGtexture* tex = swnvg__allocTexture(gl);
tex->width = w; tex->height = h; tex->flags = imageFlags; tex->type = type;
if(imageFlags & NVG_IMAGE_NOCOPY) // we'll require user to make sure image byte order matches framebuffer
tex->data = (void*)data;
else {
size_t nbytes = tex->type == NVG_TEXTURE_ALPHA ? w*h : w*h*4;
tex->data = malloc(nbytes);
if(!data) {}
else if(tex->type == NVG_TEXTURE_RGBA)
swnvg__copyRGBAData(gl, tex, data);
else
memcpy(tex->data, data, nbytes);
}
return tex->id;
}
static int swnvg__renderDeleteTexture(void* uptr, int image)
{
SWNVGcontext* gl = (SWNVGcontext*)uptr;
SWNVGtexture* tex = swnvg__findTexture(gl, image);
if(!tex) return 0;
if(!(tex->flags & NVG_IMAGE_NOCOPY))
free(tex->data);
memset(tex, 0, sizeof(SWNVGtexture));
return 1;
}
static int swnvg__renderUpdateTexture(void* uptr, int image, int x, int y, int w, int h, const void* data)
{
SWNVGcontext* gl = (SWNVGcontext*)uptr;
SWNVGtexture* tex = swnvg__findTexture(gl, image);
if(!tex) return 0;
if(tex->type == NVG_TEXTURE_RGBA)
swnvg__copyRGBAData(gl, tex, data); // only full update for now
else {
int nb = tex->type == NVG_TEXTURE_FLOAT ? 4 : 1;
int dy = y*tex->width*nb;
memcpy((char*)tex->data + dy, (const char*)data + dy, tex->width*h*nb); // no support for partial width
}
return 1;
}
static int swnvg__renderGetTextureSize(void* uptr, int image, int* w, int* h)
{
SWNVGcontext* gl = (SWNVGcontext*)uptr;
SWNVGtexture* tex = swnvg__findTexture(gl, image);
if (!tex) return 0;
if (w) *w = tex->width;
if (h) *h = tex->height;
return 1;
}
static void swnvg__renderViewport(void* uptr, float width, float height, float devicePixelRatio) {}
static void swnvg__renderCancel(void* uptr)
{
SWNVGcontext* gl = (SWNVGcontext*)uptr;
gl->nverts = 0;
gl->nedges = 0;
gl->ncalls = 0;
}
// exact coverage rasterization based on GPU renderer (nanovg_gl.h)
// - we now store the difference in coverage from the pixel to left, so we no longer write solid runs or
// recalculate integer coverage for every pixel of solid runs. With this change, performance matches non-XC
// for big paths and beats non-XC for small paths.
static void swnvg__addEdgeXC(SWNVGcontext* r, NVGvertex* vtx, float xmax)
{
SWNVGedge* e;
// Skip horizontal edges
//if (vtx->y0 == vtx->y1) return; -- horizontal edges needed for SDF generation
if (r->nedges+1 > r->cedges) {
r->cedges = r->cedges > 0 ? r->cedges * 2 : 64;
r->edges = (SWNVGedge*)realloc(r->edges, sizeof(SWNVGedge) * r->cedges);
if (r->edges == NULL) return;
}
e = &r->edges[r->nedges];
r->nedges++;
e->x0 = vtx->x0;
e->y0 = vtx->y0;
e->x1 = vtx->x1;
e->y1 = vtx->y1;