Repository navigation
Expand file tree
/
Copy pathGradientComponent.cpp
More file actions
957 lines (876 loc) · 34.2 KB
/
Copy pathGradientComponent.cpp
File metadata and controls
957 lines (876 loc) · 34.2 KB
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
#include "GradientComponent.h"
#include "PixelFormat.h"
#include <cstring>
#include <deki/Object.h>
#include <deki/Engine.h>
#include <deki/LogSystem.h>
#include "deki-rendering/CameraComponent.h"
#include "deki-rendering/Fingerprint.h"
#include "deki-rendering/QuadBlit.h"
// In every build: the bake is allocated through Deki::Memory, so the editor
// takes the same failure path as the device.
#include <deki/providers/Memory.h>
#include <cmath>
#include <algorithm>
namespace Deki2D
{
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
// How far from 0 an angle's cos or sin may be and still count as 0: float
// pi/2 and pi are off by under 1e-7.
constexpr float kStraightEpsilon = 1e-6f;
// Bayer matrices for ordered dithering
static const uint8_t kBayer2x2[4] = { 0, 2, 3, 1 };
static const uint8_t kBayer4x4[16] = { 0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5 };
static const uint8_t kBayer8x8[64] = { 0, 32, 8, 40, 2, 34, 10, 42, 48, 16, 56, 24, 50, 18, 58, 26,
12, 44, 4, 36, 14, 46, 6, 38, 60, 28, 52, 20, 62, 30, 54, 22,
3, 35, 11, 43, 1, 33, 9, 41, 51, 19, 59, 27, 49, 17, 57, 25,
15, 47, 7, 39, 13, 45, 5, 37, 63, 31, 55, 23, 61, 29, 53, 21 };
// 16x16: 256 values, 0-255
static const uint8_t kBayer16x16[256] = {
0, 192, 48, 240, 12, 204, 60, 252, 3, 195, 51, 243, 15, 207, 63, 255, 128, 64, 176, 112, 140, 76,
188, 124, 131, 67, 179, 115, 143, 79, 191, 127, 32, 224, 16, 208, 44, 236, 28, 220, 35, 227, 19, 211,
47, 239, 31, 223, 160, 96, 144, 80, 172, 108, 156, 92, 163, 99, 147, 83, 175, 111, 159, 95, 8, 200,
56, 248, 4, 196, 52, 244, 11, 203, 59, 251, 7, 199, 55, 247, 136, 72, 184, 120, 132, 68, 180, 116,
139, 75, 187, 123, 135, 71, 183, 119, 40, 232, 24, 216, 36, 228, 20, 212, 43, 235, 27, 219, 39, 231,
23, 215, 168, 104, 152, 88, 164, 100, 148, 84, 171, 107, 155, 91, 167, 103, 151, 87, 2, 194, 50, 242,
14, 206, 62, 254, 1, 193, 49, 241, 13, 205, 61, 253, 130, 66, 178, 114, 142, 78, 190, 126, 129, 65,
177, 113, 141, 77, 189, 125, 34, 226, 18, 210, 46, 238, 30, 222, 33, 225, 17, 209, 45, 237, 29, 221,
162, 98, 146, 82, 174, 110, 158, 94, 161, 97, 145, 81, 173, 109, 157, 93, 10, 202, 58, 250, 6, 198,
54, 246, 9, 201, 57, 249, 5, 197, 53, 245, 138, 74, 186, 122, 134, 70, 182, 118, 137, 73, 185, 121,
133, 69, 181, 117, 42, 234, 26, 218, 38, 230, 22, 214, 41, 233, 25, 217, 37, 229, 21, 213, 170, 106,
154, 90, 166, 102, 150, 86, 169, 105, 153, 89, 165, 101, 149, 85
};
GradientComponent::GradientComponent(float w, float h)
: DekiRendering::RendererComponent(),
gradientType(GradientType::Linear),
tileMode(GradientTileMode::None),
ditherMode(GradientDitherMode::Ordered4x4),
ditherScale(1),
width(w),
height(h),
angle(0.0f),
centerX(0.5f),
centerY(0.5f),
radius(0.5f),
stopCount(2),
stop1Position(0.0f),
stop1Color(Deki::Color::White),
stop2Position(1.0f),
stop2Color(Deki::Color::Black),
stop3Position(0.0f),
stop3Color(Deki::Color::Black),
stop4Position(0.0f),
stop4Color(Deki::Color::Black),
tileWidth(0.0f),
tileHeight(0.0f)
{
// White to black by default
stops[0] = GradientStop(0.0f, Deki::Color::White);
stops[1] = GradientStop(1.0f, Deki::Color::Black);
stops[2] = GradientStop();
stops[3] = GradientStop();
}
GradientComponent::~GradientComponent()
{
// Copies QuadBlit scaled from the bake (cacheScaled) go with it, so a
// new buffer at the same address is never mistaken for this one.
ForgetBake();
}
void GradientComponent::ForgetBake()
{
if (m_Baked)
{
QuadBlit::ForgetScaled(m_Baked.Data(), m_Baked.Bytes());
}
}
void GradientComponent::WriteStopsToProperties()
{
if (stopCount >= 1)
{
stop1Position = stops[0].position;
stop1Color = stops[0].color;
}
if (stopCount >= 2)
{
stop2Position = stops[1].position;
stop2Color = stops[1].color;
}
if (stopCount >= 3)
{
stop3Position = stops[2].position;
stop3Color = stops[2].color;
}
if (stopCount >= 4)
{
stop4Position = stops[3].position;
stop4Color = stops[3].color;
}
}
uint64_t GradientComponent::ComputeBakeKey(int32_t widthPx, int32_t heightPx) const
{
// Everything RenderToBuffer reads, a word at a time.
DekiRendering::Fingerprint f;
f.Add(static_cast<uint32_t>(gradientType) | (static_cast<uint32_t>(tileMode) << 8) |
(static_cast<uint32_t>(ditherMode) << 16) | (static_cast<uint32_t>(ditherScale) << 24));
f.Add(widthPx);
f.Add(heightPx);
f.Add(angle);
f.Add(centerX);
f.Add(centerY);
f.Add(radius);
f.Add(tileWidth);
f.Add(tileHeight);
f.Add(static_cast<uint32_t>(stopCount));
for (int i = 0; i < stopCount && i < kMaxStops; ++i)
{
const Deki::Color& c = stops[i].color;
f.Add(stops[i].position);
f.Add(static_cast<uint32_t>(c.r) | (static_cast<uint32_t>(c.g) << 8) | (static_cast<uint32_t>(c.b) << 16) |
(static_cast<uint32_t>(c.a) << 24));
}
return f.Value();
}
void GradientComponent::SetGradientType(GradientType type)
{
gradientType = type;
}
void GradientComponent::SetLinearGradient(float angleRadians)
{
gradientType = GradientType::Linear;
angle = angleRadians; // Radians, the engine convention
}
void GradientComponent::SetRadialGradient(float centerXPos, float centerYPos, float radiusVal)
{
gradientType = GradientType::Radial;
centerX = std::clamp(centerXPos, 0.0f, 1.0f);
centerY = std::clamp(centerYPos, 0.0f, 1.0f);
radius = std::clamp(radiusVal, 0.0f, 1.0f);
}
void GradientComponent::AddColorStop(float position, uint8_t r, uint8_t g, uint8_t b)
{
if (stopCount >= kMaxStops)
{
return;
}
stops[stopCount] = GradientStop(std::clamp(position, 0.0f, 1.0f), r, g, b);
stopCount++;
// Keep stops sorted by position (bubble sort: at most 4 stops)
for (int i = 0; i < stopCount - 1; i++)
{
for (int j = 0; j < stopCount - i - 1; j++)
{
if (stops[j].position > stops[j + 1].position)
{
GradientStop temp = stops[j];
stops[j] = stops[j + 1];
stops[j + 1] = temp;
}
}
}
WriteStopsToProperties();
}
void GradientComponent::ClearColorStops()
{
stopCount = 0;
}
void GradientComponent::SyncStopsFromProperties()
{
if (stopCount >= 1)
{
stops[0].position = stop1Position;
stops[0].color = stop1Color;
}
if (stopCount >= 2)
{
stops[1].position = stop2Position;
stops[1].color = stop2Color;
}
if (stopCount >= 3)
{
stops[2].position = stop3Position;
stops[2].color = stop3Color;
}
if (stopCount >= 4)
{
stops[3].position = stop4Position;
stops[3].color = stop4Color;
}
}
void GradientComponent::SetSimpleGradient(uint8_t startR, uint8_t startG, uint8_t startB, uint8_t endR, uint8_t endG,
uint8_t endB)
{
ClearColorStops();
AddColorStop(0.0f, startR, startG, startB);
AddColorStop(1.0f, endR, endG, endB);
}
void GradientComponent::SetTiling(GradientTileMode mode, float tileW, float tileH)
{
tileMode = mode;
tileWidth = tileW;
tileHeight = tileH;
}
void GradientComponent::SetDithering(GradientDitherMode mode)
{
ditherMode = mode;
}
void GradientComponent::SetArea(float w, float h)
{
width = w;
height = h;
}
DEKI_FAST_ATTR float GradientComponent::CalculateGradientPosition(float normX, float normY) const
{
switch (gradientType)
{
case GradientType::Linear:
{
// The angle's cos/sin and the projection's range over the unit
// square are worked out once per bake in RenderToBuffer.
const float projection = normX * m_CosAngle + normY * m_SinAngle;
const float along = projection - m_LinearLow;
// A range that is a power of two (any gradient along an axis)
// scales exactly by its inverse; division is done in software on
// an ESP32 and was most of a bake.
return std::clamp(m_LinearInverse > 0.0f ? along * m_LinearInverse : along / m_LinearRange, 0.0f, 1.0f);
}
case GradientType::Radial:
{
float dx = normX - centerX;
float dy = normY - centerY;
float distance = sqrtf(dx * dx + dy * dy);
return std::clamp(distance / radius, 0.0f, 1.0f);
}
case GradientType::Conical:
{
float dx = normX - centerX;
float dy = normY - centerY;
float angle_rad = atan2f(dy, dx) + M_PI; // 0 to 2 pi
return angle_rad / (2.0f * M_PI);
}
default: return normX;
}
}
DEKI_FAST_ATTR void GradientComponent::InterpolateColor(float position, uint8_t* r, uint8_t* g, uint8_t* b) const
{
if (stopCount == 0)
{
*r = *g = *b = 0;
return;
}
if (stopCount == 1)
{
*r = stops[0].color.r;
*g = stops[0].color.g;
*b = stops[0].color.b;
return;
}
position = std::clamp(position, 0.0f, 1.0f);
// Before the first stop or past the last, the end colour holds
if (position <= stops[0].position)
{
*r = stops[0].color.r;
*g = stops[0].color.g;
*b = stops[0].color.b;
return;
}
if (position >= stops[stopCount - 1].position)
{
*r = stops[stopCount - 1].color.r;
*g = stops[stopCount - 1].color.g;
*b = stops[stopCount - 1].color.b;
return;
}
for (int i = 0; i < stopCount - 1; i++)
{
if (position >= stops[i].position && position <= stops[i + 1].position)
{
float range = stops[i + 1].position - stops[i].position;
float t = (position - stops[i].position) / range;
*r = (uint8_t)(stops[i].color.r + t * (stops[i + 1].color.r - stops[i].color.r));
*g = (uint8_t)(stops[i].color.g + t * (stops[i + 1].color.g - stops[i].color.g));
*b = (uint8_t)(stops[i].color.b + t * (stops[i + 1].color.b - stops[i].color.b));
return;
}
}
*r = *g = *b = 0;
}
DEKI_FAST_ATTR float GradientComponent::SampleBayerThreshold(int32_t x, int32_t y) const
{
// M / N^2, as Pixelorama does (no +0.5 centring), which gives crisp
// hard-edged transitions at t=0 and t=1.
switch (ditherMode)
{
case GradientDitherMode::Ordered2x2: return kBayer2x2[(y & 1) * 2 + (x & 1)] / 4.0f;
case GradientDitherMode::Ordered4x4: return kBayer4x4[(y & 3) * 4 + (x & 3)] / 16.0f;
case GradientDitherMode::Ordered8x8: return kBayer8x8[(y & 7) * 8 + (x & 7)] / 64.0f;
case GradientDitherMode::Ordered16x16: return kBayer16x16[(y & 15) * 16 + (x & 15)] / 256.0f;
default: return 0.0f;
}
}
DEKI_FAST_ATTR void GradientComponent::PickStopByThreshold(float position, float threshold, uint8_t* r, uint8_t* g,
uint8_t* b) const
{
// Pixelorama-style dithering: rather than blending, pick one of the two
// stops around the position, by whether the position between them
// reaches a Bayer threshold. Every pixel is exactly one of the authored
// colours, and the pattern fills the transitions: the stippled pixel-art
// look.
//
// Matches Pixelorama's Gradient.gdshader:
// - position < stops[0].position: first stop, solid
// - position >= stops[N-1].position: last stop, solid
// - between two stops: local_t >= threshold picks the upper stop
if (stopCount == 0)
{
*r = *g = *b = 0;
return;
}
if (stopCount == 1)
{
*r = stops[0].color.r;
*g = stops[0].color.g;
*b = stops[0].color.b;
return;
}
if (position < stops[0].position)
{
*r = stops[0].color.r;
*g = stops[0].color.g;
*b = stops[0].color.b;
return;
}
if (position >= stops[stopCount - 1].position)
{
*r = stops[stopCount - 1].color.r;
*g = stops[stopCount - 1].color.g;
*b = stops[stopCount - 1].color.b;
return;
}
for (int i = 0; i < stopCount - 1; i++)
{
if (position >= stops[i].position && position < stops[i + 1].position)
{
float range = stops[i + 1].position - stops[i].position;
float local_t = (range > 0.0f) ? (position - stops[i].position) / range : 0.0f;
const GradientStop& picked = (local_t >= threshold) ? stops[i + 1] : stops[i];
*r = picked.color.r;
*g = picked.color.g;
*b = picked.color.b;
return;
}
}
*r = *g = *b = 0;
}
DEKI_FAST_ATTR uint16_t GradientComponent::ConvertToRGB565(uint8_t r, uint8_t g, uint8_t b) const
{
return ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
}
void GradientComponent::RenderPixel(int32_t x, int32_t y, uint16_t color, uint8_t* renderBuffer, int screenWidth,
int screenHeight) const
{
if (x < 0 || x >= screenWidth || y < 0 || y >= screenHeight)
{
return;
}
uint16_t* buffer16 = (uint16_t*)renderBuffer;
buffer16[y * screenWidth + x] = color;
}
void GradientComponent::PrepareBake()
{
// The inspector edits the stopN properties
SyncStopsFromProperties();
m_CosAngle = cosf(angle);
m_SinAngle = sinf(angle);
// Straight along an axis when the angle is a whole number of right
// angles: cos(pi/2) is -4.4e-8 in float, which tilted a vertical gradient
// by a hair, and where a row met a colour's edge one pixel of it turned.
if (std::fabs(m_CosAngle) < kStraightEpsilon)
{
m_CosAngle = 0.0f;
m_SinAngle = m_SinAngle < 0.0f ? -1.0f : 1.0f;
}
else if (std::fabs(m_SinAngle) < kStraightEpsilon)
{
m_SinAngle = 0.0f;
m_CosAngle = m_CosAngle < 0.0f ? -1.0f : 1.0f;
}
// Over the unit square a linear gradient's projection runs from
// m_LinearLow to m_LinearLow + m_LinearRange.
m_LinearLow = std::min(0.0f, m_CosAngle) + std::min(0.0f, m_SinAngle);
m_LinearRange = (std::max(0.0f, m_CosAngle) + std::max(0.0f, m_SinAngle)) - m_LinearLow;
int exponent = 0;
m_LinearInverse = std::frexp(m_LinearRange, &exponent) == 0.5f ? 1.0f / m_LinearRange : 0.0f;
}
void GradientComponent::TileSizes(int32_t artW, int32_t artH, int32_t& tileW, int32_t& tileH) const
{
const float ppm = Deki::EngineSettings::Global().pixelsPerMeter;
const int32_t tileWPx = static_cast<int32_t>(tileWidth * ppm);
const int32_t tileHPx = static_cast<int32_t>(tileHeight * ppm);
tileW = tileWPx > 0 ? tileWPx : artW;
tileH = tileHPx > 0 ? tileHPx : artH;
}
float GradientComponent::NormAlong(int32_t i, int32_t full, int32_t tile, bool tiled) const
{
if (tileMode == GradientTileMode::Mirror)
{
const int32_t t = i % (tile * 2);
return t < tile ? (float)t / tile : 1.0f - (float)(t - tile) / tile;
}
return tiled ? (float)(i % tile) / tile : (float)i / full;
}
void GradientComponent::FindStraightAxes(int32_t artW, int32_t artH, bool& sameAcross, bool& sameDown)
{
sameAcross = false;
sameDown = false;
// A dither's pattern changes along both axes, and a radial or conical
// gradient's position does.
if (stopCount == 0 || ditherMode != GradientDitherMode::None || gradientType != GradientType::Linear || artW <= 0 ||
artH <= 0)
{
return;
}
PrepareBake();
int32_t tileW = 0;
int32_t tileH = 0;
TileSizes(artW, artH, tileW, tileH);
const bool mirror = tileMode == GradientTileMode::Mirror;
const bool tileAcross = tileMode == GradientTileMode::Horizontal || tileMode == GradientTileMode::Both;
const bool tileDown = tileMode == GradientTileMode::Vertical || tileMode == GradientTileMode::Both;
// The spans the position moves one way along (see RenderToBuffer): the
// whole width, or a tile, or half a mirrored one.
const int32_t spanX = (tileAcross || mirror) ? tileW : artW;
const int32_t spanY = (tileDown || mirror) ? tileH : artH;
// One colour along a line: the position only moves one way within a span,
// so every position on the line lies between the spans' ends. Between two
// stops each channel only moves one way with it too, so when the ends are
// the same colour and no stop lies inside their range, every pixel
// between is that colour. (Compared by colour, not position: cos(pi/2) is
// not 0 in float, so a vertical gradient's positions differ along a row
// by a hair that no pixel shows.)
auto pixelAt = [this](float position) -> uint16_t
{
uint8_t r, g, b;
InterpolateColor(position, &r, &g, &b);
return ConvertToRGB565(r, g, b);
};
auto sameAlong = [this, &pixelAt](int32_t count, int32_t span, auto&& at) -> bool
{
const float first = at(0);
const uint16_t colour = pixelAt(first);
float low = first;
float high = first;
for (int32_t start = 0; start < count; start += span)
{
const int32_t last = std::min(start + span, count) - 1;
for (const float p : { at(start), at(last) })
{
if (p != p || pixelAt(p) != colour)
{
return false;
}
low = std::min(low, p);
high = std::max(high, p);
}
}
for (int i = 0; i < stopCount; i++)
{
if (stops[i].position > low && stops[i].position < high)
{
return false;
}
}
return true;
};
sameAcross = true;
for (int32_t y = 0; y < artH && sameAcross; y++)
{
const float normY = NormAlong(y, artH, tileH, tileDown);
sameAcross = sameAlong(artW, spanX, [&](int32_t x)
{ return CalculateGradientPosition(NormAlong(x, artW, tileW, tileAcross), normY); });
}
sameDown = true;
for (int32_t x = 0; x < artW && sameDown; x++)
{
const float normX = NormAlong(x, artW, tileW, tileAcross);
sameDown = sameAlong(artH, spanY, [&](int32_t y)
{ return CalculateGradientPosition(normX, NormAlong(y, artH, tileH, tileDown)); });
}
}
bool GradientComponent::RenderToBuffer(uint8_t* buffer, int32_t outW, int32_t outH, int32_t artW, int32_t artH,
int32_t ditherArt)
{
if (!buffer || stopCount == 0)
{
return false;
}
// width/height are world meters; the layout runs on the art grid.
const int32_t widthPx = artW;
const int32_t heightPx = artH;
if (widthPx <= 0 || heightPx <= 0 || outW <= 0 || outH <= 0)
{
return false;
}
if (ditherArt < 1)
{
ditherArt = 1;
}
PrepareBake();
uint16_t* buffer16 = reinterpret_cast<uint16_t*>(buffer);
int32_t renderWidth = widthPx;
int32_t renderHeight = heightPx;
int32_t actualTileWidth = 0;
int32_t actualTileHeight = 0;
TileSizes(renderWidth, renderHeight, actualTileWidth, actualTileHeight);
// The Bayer pattern is laid out on the art grid, cells ditherArt art
// pixels wide, unless the bake is smaller than the art: then it is on the
// output grid, cells ditherArt art pixels in whole output pixels, so it
// stays a regular stipple instead of art pixels dropped unevenly.
const bool ditherOnArt = outW >= widthPx && outH >= heightPx;
const int32_t outCell =
std::max<int32_t>(1, static_cast<int32_t>(std::lround(static_cast<double>(ditherArt) * outW / widthPx)));
const bool tileAcross = tileMode == GradientTileMode::Horizontal || tileMode == GradientTileMode::Both;
const bool tileDown = tileMode == GradientTileMode::Vertical || tileMode == GradientTileMode::Both;
// Per art column: its place across (worked out once, not on every row),
// then the current art row's gradient position, and its pixel when the
// gradient is not dithered.
Deki::Buffer<uint8_t> table;
const size_t tableColumns = static_cast<size_t>(renderWidth);
if (!table.Allocate(tableColumns * (2 * sizeof(float) + sizeof(uint16_t)), Deki::Memory::External))
{
DEKI_LOG_WARNING("GradientComponent: no room for the %d columns of a bake; not drawing it", (int)renderWidth);
return false;
}
float* columns = reinterpret_cast<float*>(table.Data());
float* positions = columns + tableColumns;
uint16_t* artPixels = reinterpret_cast<uint16_t*>(positions + tableColumns);
for (int32_t x = 0; x < renderWidth; x++)
{
columns[x] = NormAlong(x, renderWidth, actualTileWidth, tileAcross);
}
// Along a row, a linear gradient's position only ever moves one way while
// the columns do: across the whole width, or one tile (half a mirrored
// one) at a time. Two columns at the same position then have every column
// between them at it too, so runs of one position are found by galloping
// out from their first column and filled, instead of worked out column by
// column. A gradient along an axis is a run or two a row.
const bool monotonic = gradientType == GradientType::Linear;
int32_t span = renderWidth;
if (tileAcross || tileMode == GradientTileMode::Mirror)
{
span = actualTileWidth;
}
const bool dithered = ditherMode != GradientDitherMode::None;
// Neighbouring pixels mostly land on the same colour, so the last one is
// kept. The bake used to work every pixel out from scratch, and a
// full-screen background took 0.7 s on an ESP32-S3, the first frame of
// every scene that has one.
bool haveLast = false;
float lastPos = 0.0f;
float lastThreshold = 0.0f;
uint16_t lastPixel = 0;
auto pixelAt = [&](float gradPos, float threshold) -> uint16_t
{
if (!haveLast || gradPos != lastPos || threshold != lastThreshold)
{
uint8_t r, g, b;
if (!dithered)
{
// No dither: blend smoothly between stops
InterpolateColor(gradPos, &r, &g, &b);
}
else
{
PickStopByThreshold(gradPos, threshold, &r, &g, &b);
}
lastPixel = DekiPixel::ToMemory565(ConvertToRGB565(r, g, b));
lastPos = gradPos;
lastThreshold = threshold;
haveLast = true;
}
return lastPixel;
};
// Art columns first to last at one position. Undithered, that is one
// pixel for all of them.
auto fill = [&](int32_t first, int32_t last, float gradPos)
{
if (dithered)
{
std::fill(positions + first, positions + last + 1, gradPos);
}
else
{
std::fill(artPixels + first, artPixels + last + 1, pixelAt(gradPos, 0.0f));
}
};
for (int32_t oy = 0; oy < outH; oy++)
{
// The art row this output row covers (the same row for every output
// row of a whole-number upscale).
const int32_t y = static_cast<int32_t>((static_cast<int64_t>(oy) * renderHeight) / outH);
const int32_t cellY = ditherOnArt ? y / ditherArt : oy / outCell;
uint16_t* row = buffer16 + static_cast<size_t>(oy) * outW;
// An output row on the same art row as the one above has its columns.
if (oy == 0 || y != static_cast<int32_t>((static_cast<int64_t>(oy - 1) * renderHeight) / outH))
{
const float normY = NormAlong(y, renderHeight, actualTileHeight, tileDown);
if (!monotonic)
{
for (int32_t x = 0; x < renderWidth; x++)
{
fill(x, x, CalculateGradientPosition(columns[x], normY));
}
}
for (int32_t start = 0; monotonic && start < renderWidth; start += span)
{
const int32_t end = std::min(start + span, renderWidth);
int32_t x = start;
float at = CalculateGradientPosition(columns[x], normY);
while (x < end)
{
// The run reaches `last` and stops before `beyond`, where
// the position is `beyondAt` (unless `beyond` is the end).
int32_t last = x;
int32_t beyond = end;
float beyondAt = 0.0f;
for (int32_t step = 1; last + step < end; step *= 2)
{
const float p = CalculateGradientPosition(columns[last + step], normY);
if (p != at)
{
beyond = last + step;
beyondAt = p;
break;
}
last += step;
}
while (beyond - last > 1)
{
const int32_t mid = last + (beyond - last) / 2;
const float p = CalculateGradientPosition(columns[mid], normY);
if (p == at)
{
last = mid;
}
else
{
beyond = mid;
beyondAt = p;
}
}
fill(x, last, at);
x = beyond;
at = beyondAt;
}
}
}
if (!dithered && outW == renderWidth)
{
std::memcpy(row, artPixels, static_cast<size_t>(outW) * sizeof(uint16_t));
continue;
}
// The art column under output column ox is ox * renderWidth / outW,
// stepped along with its remainder instead of divided out per pixel.
int32_t x = 0;
int32_t xRemainder = 0;
for (int32_t ox = 0; ox < outW; ox++)
{
if (dithered)
{
// Stipple dither picks one of the two stops around the
// position by a Bayer threshold, read on the art grid like the
// colour (see RenderToBuffer in the header): zooming magnifies
// the device's pattern instead of laying out a new one.
const int32_t cellX = ditherOnArt ? x / ditherArt : ox / outCell;
row[ox] = pixelAt(positions[x], SampleBayerThreshold(cellX, cellY));
}
else
{
row[ox] = artPixels[x];
}
xRemainder += renderWidth;
if (xRemainder >= outW)
{
x += xRemainder / outW;
xRemainder %= outW;
}
}
}
return true;
}
void GradientComponent::CheckBakeUniformity(int32_t widthPx, int32_t heightPx)
{
// Read off the pixels rather than the angle: cos(pi/2) is not exactly 0
// in float, so a "vertical" gradient could still differ along a row, and
// a dithered one does. Each check stops at the first difference.
const uint16_t* px = reinterpret_cast<const uint16_t*>(m_Baked.Data());
const size_t w = static_cast<size_t>(widthPx);
m_UniformRows = true;
for (int32_t y = 0; y < heightPx && m_UniformRows; ++y)
{
const uint16_t* row = px + static_cast<size_t>(y) * w;
for (size_t x = 1; x < w; ++x)
{
if (row[x] != row[0])
{
m_UniformRows = false;
break;
}
}
}
m_UniformColumns = !m_UniformRows;
for (int32_t y = 1; y < heightPx && m_UniformColumns; ++y)
{
m_UniformColumns = std::memcmp(px + static_cast<size_t>(y) * w, px, w * sizeof(uint16_t)) == 0;
}
// A one-column bake is its own row colours.
if (m_UniformRows && widthPx > 1 && m_RowColors.Allocate(static_cast<size_t>(heightPx), Deki::Memory::Internal))
{
for (int32_t y = 0; y < heightPx; ++y)
{
m_RowColors[y] = px[static_cast<size_t>(y) * w];
}
}
else
{
m_RowColors.Reset();
}
}
bool GradientComponent::RenderContent(const Deki::Object* owner, QuadBlit::Source& outSource, float& outPivotX,
float& outPivotY, uint8_t& outTintR, uint8_t& outTintG, uint8_t& outTintB,
uint8_t& outTintA)
{
if (!owner || stopCount == 0)
{
return false;
}
// width/height are world meters. The layout is on the art grid (the
// project's pixels per meter); see RenderToBuffer.
const float artPPM = Deki::EngineSettings::Global().pixelsPerMeter;
const int32_t artW = static_cast<int32_t>(width * artPPM);
const int32_t artH = static_cast<int32_t>(height * artPPM);
if (artW <= 0 || artH <= 0)
{
return false;
}
// Drawn at its own size or larger, it is baked on the art grid: every art
// pixel is one colour, so a bigger bake would only repeat pixels, and
// QuadBlit magnifies it evenly (each art pixel the same whole number of
// screen pixels, give or take one) without a re-bake on every zoom.
// Drawn smaller (a camera showing more art than the screen has pixels, the
// editor zoomed out), it is baked at the view's density, 1:1 on the
// screen, so no art pixel is dropped unevenly as it is drawn.
int32_t widthPx = artW;
int32_t heightPx = artH;
const DekiRendering::DrawView& view = DekiRendering::CurrentDrawView();
if (view.pixelsPerMeter > 0.0f && view.pixelsPerMeter < artPPM)
{
float bakePPM = view.pixelsPerMeter;
// At most twice the view's own area: a big gradient mostly off the
// view would otherwise ask for a bake many screens wide. Past that it
// is scaled up a little as it is drawn.
const double viewArea = static_cast<double>(view.width) * view.height;
const double bakeArea = static_cast<double>(width) * bakePPM * height * bakePPM;
if (viewArea > 0.0 && bakeArea > viewArea * 2.0)
{
bakePPM *= static_cast<float>(std::sqrt(viewArea * 2.0 / bakeArea));
}
widthPx = std::max<int32_t>(1, static_cast<int32_t>(std::lround(width * bakePPM)));
heightPx = std::max<int32_t>(1, static_cast<int32_t>(std::lround(height * bakePPM)));
}
// A Bayer cell covers ditherScale art pixels (a power of two, 1..16).
int32_t ditherArt = 1;
if (ditherScale >= 16)
{
ditherArt = 16;
}
else if (ditherScale >= 8)
{
ditherArt = 8;
}
else if (ditherScale >= 4)
{
ditherArt = 4;
}
else if (ditherScale >= 2)
{
ditherArt = 2;
}
// The inspector edits the stopN properties
SyncStopsFromProperties();
// Re-bake only when an input changed. The bake is the expensive part
// (per-pixel trig for radial/conical); the blit reuses it every frame.
uint64_t key = ComputeBakeKey(widthPx, heightPx);
key = (key ^ static_cast<uint64_t>(artW)) * 1099511628211ull;
key = (key ^ static_cast<uint64_t>(artH)) * 1099511628211ull;
key = (key ^ static_cast<uint64_t>(ditherArt)) * 1099511628211ull;
// A bake already refused is not attempted again until an input changes.
if (m_BakeFailed && m_BakeFailedKey == key)
{
return false;
}
if (!m_Baked || m_BakeKey != key)
{
ForgetBake();
// A gradient that changes along one axis only is baked as one column
// or one row, stretched over its rectangle as it is drawn: a
// full-screen background is a few hundred pixels instead of a
// screenful.
bool sameAcross = false;
bool sameDown = false;
FindStraightAxes(artW, artH, sameAcross, sameDown);
const int32_t bakeW = sameAcross ? 1 : widthPx;
const int32_t bakeH = sameDown ? 1 : heightPx;
const size_t need = static_cast<size_t>(bakeW) * static_cast<size_t>(bakeH) * 2; // RGB565
// Allocate() leaves an unchanged size alone, so the common path costs
// nothing. A full bake is the object's size in pixels times two: 150
// KB for a full-screen one at 320x240, more on a bigger panel, and a
// device without PSRAM can simply refuse it.
if (!m_Baked.Allocate(need, Deki::Memory::External))
{
DEKI_LOG_WARNING("GradientComponent: no room for a %dx%d bake (%u bytes); "
"not drawing it",
(int)bakeW, (int)bakeH, (unsigned)need);
m_BakeFailed = true;
m_BakeFailedKey = key;
return false;
}
if (!RenderToBuffer(m_Baked.Data(), bakeW, bakeH, artW, artH, ditherArt))
{
m_Baked.Reset();
m_BakeFailed = true;
m_BakeFailedKey = key;
return false;
}
m_BakeFailed = false;
m_BakeWidth = bakeW;
m_BakeHeight = bakeH;
CheckBakeUniformity(bakeW, bakeH);
m_BakeKey = key;
++m_BakeVersion;
}
outSource = QuadBlit::MakeSource(m_Baked.Data(), m_BakeWidth, m_BakeHeight, QuadBlit::PixelLayout::RGB565(),
false // ownsPixels: the component owns its bake
);
outSource.stretchWidth = m_BakeWidth != widthPx ? widthPx : 0;
outSource.stretchHeight = m_BakeHeight != heightPx ? heightPx : 0;
outSource.uniformRows = m_UniformRows;
outSource.uniformColumns = m_UniformColumns;
// Drawn bigger than its art (a camera closer than the art's density), a
// full bake is magnified on every blit; QuadBlit keeps a copy at the drawn
// size instead. The bake stays as it is until ForgetBake. A strip is
// filled as it is drawn and needs none.
outSource.cacheScaled = outSource.stretchWidth == 0 && outSource.stretchHeight == 0;
outSource.rowColors = m_UniformRows && m_RowColors ? m_RowColors.Data() : nullptr;
// The full bake's density: the renderer then scales it by the object's
// scale alone, 1:1 for an unscaled object.
outSource.pixelsPerMeter = static_cast<float>(widthPx) / width;
// Centre pivot
outPivotX = 0.5f;
outPivotY = 0.5f;
// White: no tint
outTintR = outTintG = outTintB = outTintA = 255;
return true;
}
} // namespace Deki2D