@geastack/gea3d 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -3
- package/package.json +1 -1
- package/src/native/gea3d_native.cpp +436 -24
- package/src/native/gea3d_raster.h +417 -12
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
|
|
30
30
|
#include <cstdint>
|
|
31
31
|
#include <cstring>
|
|
32
|
+
#include <utility>
|
|
32
33
|
#include <vector>
|
|
33
34
|
|
|
34
35
|
#if defined(__GNUC__) || defined(__clang__)
|
|
@@ -51,6 +52,7 @@ enum PrimFlag : std::uint8_t {
|
|
|
51
52
|
kSmooth = 1, // per-vertex colours interpolated across the primitive
|
|
52
53
|
kBlend = 2, // alpha-blended (drawn back-to-front after the opaque pass)
|
|
53
54
|
kAdd = 4, // with kBlend: added to the pixel (scaled by alpha), not mixed
|
|
55
|
+
kPrepared = 8, // triangle: corners sorted by y, edges set up (prepareTriangle)
|
|
54
56
|
};
|
|
55
57
|
|
|
56
58
|
// Screen coordinates are 28.4 fixed point; depth is near/w in (0, 1], larger
|
|
@@ -71,6 +73,10 @@ struct Prim {
|
|
|
71
73
|
std::int16_t rowY1;
|
|
72
74
|
// Painter's key: view distance of the primitive's farthest vertex.
|
|
73
75
|
float key;
|
|
76
|
+
// With kPrepared: edges 0->2, 0->1, 1->2 of the y-sorted corners, as
|
|
77
|
+
// x at the edge's first row centre and step per row (16.16 px).
|
|
78
|
+
std::int32_t edgeX[3];
|
|
79
|
+
std::int32_t edgeStep[3];
|
|
74
80
|
};
|
|
75
81
|
|
|
76
82
|
enum BackgroundMode : std::uint8_t {
|
|
@@ -135,18 +141,55 @@ struct Scratch {
|
|
|
135
141
|
int dirtyLast = -1;
|
|
136
142
|
};
|
|
137
143
|
|
|
144
|
+
|
|
138
145
|
namespace detail {
|
|
139
146
|
|
|
140
147
|
GEA3D_INLINE int minInt(int a, int b) { return a < b ? a : b; }
|
|
141
148
|
GEA3D_INLINE int maxInt(int a, int b) { return a > b ? a : b; }
|
|
149
|
+
#if defined(__XTENSA__) && !defined(__XTENSA_SOFT_FLOAT__)
|
|
150
|
+
// Xtensa's FPU has no min/max: the builtins become fminf/fmaxf calls, a
|
|
151
|
+
// function call per clamp. Compares keep fmax's NaN-to-`lo`.
|
|
152
|
+
#define GEA3D_XTENSA_FPU 1
|
|
153
|
+
GEA3D_INLINE float clampFloat(float v, float lo, float hi) { return !(v >= lo) ? lo : (v > hi ? hi : v); }
|
|
154
|
+
#else
|
|
155
|
+
#define GEA3D_XTENSA_FPU 0
|
|
142
156
|
// Branch-free on FPv5 (VMINNM/VMAXNM): a float compare's transfer to the
|
|
143
157
|
// integer flags stalls the Cortex-M7 pipeline.
|
|
144
158
|
GEA3D_INLINE float clampFloat(float v, float lo, float hi) { return __builtin_fminf(__builtin_fmaxf(v, lo), hi); }
|
|
159
|
+
#endif
|
|
145
160
|
GEA3D_INLINE int clampInt(int v, int lo, int hi) { return v < lo ? lo : (v > hi ? hi : v); }
|
|
146
161
|
GEA3D_INLINE int ctz(std::uint32_t value) { return __builtin_ctz(value); }
|
|
147
162
|
|
|
163
|
+
#if GEA3D_XTENSA_FPU
|
|
164
|
+
// Round to nearest, ties away from zero. roundf is a libm call on Xtensa; the
|
|
165
|
+
// truncating conversion is one instruction, and value - trunc(value) is exact
|
|
166
|
+
// for every float below 2^24 (larger ones are already integers).
|
|
167
|
+
GEA3D_INLINE std::int32_t roundToInt(float value)
|
|
168
|
+
{
|
|
169
|
+
std::int32_t whole = static_cast<std::int32_t>(value);
|
|
170
|
+
const float fraction = value - static_cast<float>(whole);
|
|
171
|
+
if (fraction >= 0.5f) whole++;
|
|
172
|
+
else if (fraction <= -0.5f) whole--;
|
|
173
|
+
return whole;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// 1 / value. GCC lowers a float division here to the soft-float __divsf3,
|
|
177
|
+
// though the FPU has a reciprocal seed: two Newton steps take its estimate to
|
|
178
|
+
// float precision. On the ESP32-S3 the vertex transform halved (7.1 -> 3.2 ms
|
|
179
|
+
// a frame for ~1,400 vertices) with this, clampFloat and roundToInt.
|
|
180
|
+
GEA3D_INLINE float reciprocal(float value)
|
|
181
|
+
{
|
|
182
|
+
float r;
|
|
183
|
+
asm("recip0.s %0, %1" : "=f"(r) : "f"(value));
|
|
184
|
+
r = r * (2.0f - value * r);
|
|
185
|
+
r = r * (2.0f - value * r);
|
|
186
|
+
return r;
|
|
187
|
+
}
|
|
188
|
+
#else
|
|
148
189
|
// Round to nearest, ties away from zero (VRINTA on FPv5, no libm call).
|
|
149
190
|
GEA3D_INLINE std::int32_t roundToInt(float value) { return static_cast<std::int32_t>(__builtin_roundf(value)); }
|
|
191
|
+
GEA3D_INLINE float reciprocal(float value) { return 1.0f / value; }
|
|
192
|
+
#endif
|
|
150
193
|
|
|
151
194
|
constexpr float kFixedOne = 65536.0f;
|
|
152
195
|
constexpr float kMaxSlope = 16000.0f; // px per row; steeper edges span under one row
|
|
@@ -157,13 +200,29 @@ struct Edge {
|
|
|
157
200
|
std::int32_t x = 0;
|
|
158
201
|
std::int32_t step = 0;
|
|
159
202
|
|
|
203
|
+
// x at the edge's first row centre and the step per row, 16.16 px.
|
|
204
|
+
static GEA3D_INLINE void setup(std::int32_t x0, std::int32_t y0, std::int32_t x1, std::int32_t y1, std::int32_t &xTop, std::int32_t &stepOut)
|
|
205
|
+
{
|
|
206
|
+
const float slope = clampFloat(static_cast<float>(x1 - x0) * reciprocal(static_cast<float>(y1 - y0)), -kMaxSlope, kMaxSlope);
|
|
207
|
+
const int top = (y0 - 8 + 15) >> 4;
|
|
208
|
+
const float xt = (static_cast<float>(x0) + static_cast<float>(top * 16 + 8 - y0) * slope) * (1.0f / 16.0f);
|
|
209
|
+
stepOut = roundToInt(slope * kFixedOne);
|
|
210
|
+
xTop = roundToInt(xt * kFixedOne);
|
|
211
|
+
}
|
|
212
|
+
|
|
160
213
|
GEA3D_INLINE void init(std::int32_t x0, std::int32_t y0, std::int32_t x1, std::int32_t y1, int row)
|
|
161
214
|
{
|
|
162
|
-
|
|
215
|
+
std::int32_t xTop, stepIn;
|
|
216
|
+
setup(x0, y0, x1, y1, xTop, stepIn);
|
|
217
|
+
at(xTop, stepIn, y0, row);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// From a prepared setup: the edge starting at y0 (28.4), at `row`.
|
|
221
|
+
GEA3D_INLINE void at(std::int32_t xTop, std::int32_t stepIn, std::int32_t y0, int row)
|
|
222
|
+
{
|
|
163
223
|
const int top = (y0 - 8 + 15) >> 4;
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
x = roundToInt(xTop * kFixedOne) + static_cast<std::int32_t>(static_cast<std::int64_t>(row - top) * step);
|
|
224
|
+
step = stepIn;
|
|
225
|
+
x = xTop + static_cast<std::int32_t>(static_cast<std::int64_t>(row - top) * stepIn);
|
|
167
226
|
}
|
|
168
227
|
|
|
169
228
|
GEA3D_INLINE void advance() { x += step; }
|
|
@@ -213,7 +272,49 @@ GEA3D_INLINE Pixel packRgb(std::int32_t r, std::int32_t g, std::int32_t b)
|
|
|
213
272
|
// A constant-colour run: paired 32-bit stores on 16-bit framebuffers.
|
|
214
273
|
GEA3D_INLINE void fillPixels(Pixel *dst, int count, Pixel color)
|
|
215
274
|
{
|
|
216
|
-
#if GEA_PIXEL_FORMAT_IS_8888
|
|
275
|
+
#if GEA_PIXEL_PIE_FILL16 && !GEA_PIXEL_FORMAT_IS_8888 && !GEA_PIXEL_FORMAT_IS_GRAY
|
|
276
|
+
// Raster rows average ~50 px, where the shared span fill spent ~90 of its
|
|
277
|
+
// ~108 cycles on a per-pixel head and tail and an 8-store pattern. Here the
|
|
278
|
+
// head and tail are at most three stores each (16/32/64-bit by address and
|
|
279
|
+
// length bits) and the vector is one broadcast load.
|
|
280
|
+
const std::uint32_t pair = static_cast<std::uint32_t>(color) | (static_cast<std::uint32_t>(color) << 16);
|
|
281
|
+
if (count >= 16) {
|
|
282
|
+
if ((reinterpret_cast<std::uintptr_t>(dst) & 2u) != 0) {
|
|
283
|
+
*dst++ = color;
|
|
284
|
+
count--;
|
|
285
|
+
}
|
|
286
|
+
if ((reinterpret_cast<std::uintptr_t>(dst) & 4u) != 0) {
|
|
287
|
+
*reinterpret_cast<std::uint32_t *>(dst) = pair;
|
|
288
|
+
dst += 2;
|
|
289
|
+
count -= 2;
|
|
290
|
+
}
|
|
291
|
+
if ((reinterpret_cast<std::uintptr_t>(dst) & 8u) != 0) {
|
|
292
|
+
std::uint32_t *wide = reinterpret_cast<std::uint32_t *>(dst);
|
|
293
|
+
wide[0] = pair;
|
|
294
|
+
wide[1] = pair;
|
|
295
|
+
dst += 4;
|
|
296
|
+
count -= 4;
|
|
297
|
+
}
|
|
298
|
+
const int vec = count >> 3;
|
|
299
|
+
std::uint32_t lane = pair;
|
|
300
|
+
const std::uint32_t *lanePtr = &lane;
|
|
301
|
+
asm volatile(
|
|
302
|
+
"ee.vldbc.32 q0, %[lane]\n"
|
|
303
|
+
"loopnez %[vec], 1f\n"
|
|
304
|
+
"ee.vst.128.ip q0, %[dst], 16\n"
|
|
305
|
+
"1:\n"
|
|
306
|
+
: [dst] "+r"(dst)
|
|
307
|
+
: [lane] "r"(lanePtr), [vec] "r"(vec)
|
|
308
|
+
: "memory");
|
|
309
|
+
count &= 7;
|
|
310
|
+
} else if ((reinterpret_cast<std::uintptr_t>(dst) & 2u) != 0 && count > 0) {
|
|
311
|
+
*dst++ = color;
|
|
312
|
+
count--;
|
|
313
|
+
}
|
|
314
|
+
std::uint32_t *wide = reinterpret_cast<std::uint32_t *>(dst);
|
|
315
|
+
for (int pairs = count >> 1; pairs > 0; pairs--) *wide++ = pair;
|
|
316
|
+
if ((count & 1) != 0) *reinterpret_cast<Pixel *>(wide) = color;
|
|
317
|
+
#elif GEA_PIXEL_FORMAT_IS_8888 || GEA_PIXEL_FORMAT_IS_GRAY || GEA_PIXEL_PIE_FILL16
|
|
217
318
|
pixel::fillNative(dst, count, color);
|
|
218
319
|
#else
|
|
219
320
|
if (count <= 0) return;
|
|
@@ -327,6 +428,133 @@ GEA3D_INLINE void shadeEach(Pixel *dst, int count, std::int32_t r, std::int32_t
|
|
|
327
428
|
#endif
|
|
328
429
|
}
|
|
329
430
|
|
|
431
|
+
#if GEA_PIXEL_PIE_FILL16 && !GEA_PIXEL_FORMAT_IS_8888 && !GEA_PIXEL_FORMAT_IS_GRAY
|
|
432
|
+
#define GEA3D_PIE_SHADE 1
|
|
433
|
+
// ESP32-S3 PIE Gouraud span: eight RGB565 pixels per iteration. Each channel
|
|
434
|
+
// steps in 32-bit lanes (four pixels per q register), so every pixel is
|
|
435
|
+
// exactly packRgb(r + dr*i, ...) of the scalar walk: for a channel in
|
|
436
|
+
// 0..0xFFFFFF, (r >> 8) & 0xF800 == (r >> 19) << 11 and likewise for green
|
|
437
|
+
// and blue, which needs shifts only, no mask registers. Two groups of four
|
|
438
|
+
// are narrowed to 16-bit lanes with one unzip and stored with one 128-bit
|
|
439
|
+
// store. q0-q2 hold the channels, q6 streams the 4-pixel steps from memory;
|
|
440
|
+
// q registers are caller-saved and switched lazily by the IDF port.
|
|
441
|
+
// dst must be 16-byte aligned; count8 >= 1 groups of eight.
|
|
442
|
+
GEA3D_INLINE void shadePie(Pixel *dst, int count8, std::int32_t r, std::int32_t g, std::int32_t b, std::int32_t dr, std::int32_t dg, std::int32_t db)
|
|
443
|
+
{
|
|
444
|
+
alignas(16) std::int32_t start[12] = {r, r + dr, r + 2 * dr, r + 3 * dr, g, g + dg, g + 2 * dg, g + 3 * dg, b, b + db, b + 2 * db, b + 3 * db};
|
|
445
|
+
alignas(16) std::int32_t step[12] = {4 * dr, 4 * dr, 4 * dr, 4 * dr, 4 * dg, 4 * dg, 4 * dg, 4 * dg, 4 * db, 4 * db, 4 * db, 4 * db};
|
|
446
|
+
const std::int32_t *acc = start;
|
|
447
|
+
const std::int32_t *st = step;
|
|
448
|
+
const int s19 = 19, s11 = 11, s18 = 18, s5 = 5;
|
|
449
|
+
asm volatile(
|
|
450
|
+
"ee.vld.128.ip q0, %[acc], 16\n"
|
|
451
|
+
"ee.vld.128.ip q1, %[acc], 16\n"
|
|
452
|
+
"ee.vld.128.ip q2, %[acc], 0\n"
|
|
453
|
+
"loopnez %[n], 1f\n"
|
|
454
|
+
// pixels 0..3 -> q3
|
|
455
|
+
"wsr.sar %[s19]\n"
|
|
456
|
+
"ee.vsr.32 q3, q0\n"
|
|
457
|
+
"ee.vsr.32 q7, q2\n"
|
|
458
|
+
"wsr.sar %[s11]\n"
|
|
459
|
+
"ee.vsl.32 q3, q3\n"
|
|
460
|
+
"ee.orq q3, q3, q7\n"
|
|
461
|
+
"wsr.sar %[s18]\n"
|
|
462
|
+
"ee.vsr.32 q7, q1\n"
|
|
463
|
+
"wsr.sar %[s5]\n"
|
|
464
|
+
"ee.vsl.32 q7, q7\n"
|
|
465
|
+
"ee.orq q3, q3, q7\n"
|
|
466
|
+
"ee.vld.128.ip q6, %[st], 16\n"
|
|
467
|
+
"ee.vadds.s32 q0, q0, q6\n"
|
|
468
|
+
"ee.vld.128.ip q6, %[st], 16\n"
|
|
469
|
+
"ee.vadds.s32 q1, q1, q6\n"
|
|
470
|
+
"ee.vld.128.ip q6, %[st], -32\n"
|
|
471
|
+
"ee.vadds.s32 q2, q2, q6\n"
|
|
472
|
+
// pixels 4..7 -> q4
|
|
473
|
+
"wsr.sar %[s19]\n"
|
|
474
|
+
"ee.vsr.32 q4, q0\n"
|
|
475
|
+
"ee.vsr.32 q7, q2\n"
|
|
476
|
+
"wsr.sar %[s11]\n"
|
|
477
|
+
"ee.vsl.32 q4, q4\n"
|
|
478
|
+
"ee.orq q4, q4, q7\n"
|
|
479
|
+
"wsr.sar %[s18]\n"
|
|
480
|
+
"ee.vsr.32 q7, q1\n"
|
|
481
|
+
"wsr.sar %[s5]\n"
|
|
482
|
+
"ee.vsl.32 q7, q7\n"
|
|
483
|
+
"ee.orq q4, q4, q7\n"
|
|
484
|
+
"ee.vld.128.ip q6, %[st], 16\n"
|
|
485
|
+
"ee.vadds.s32 q0, q0, q6\n"
|
|
486
|
+
"ee.vld.128.ip q6, %[st], 16\n"
|
|
487
|
+
"ee.vadds.s32 q1, q1, q6\n"
|
|
488
|
+
"ee.vld.128.ip q6, %[st], -32\n"
|
|
489
|
+
"ee.vadds.s32 q2, q2, q6\n"
|
|
490
|
+
// Low halves of both groups, in pixel order, into q3 (q4 = zeros).
|
|
491
|
+
"ee.vunzip.16 q3, q4\n"
|
|
492
|
+
#if GEA_EMBEDDED_PIXEL_PANEL_ENDIAN
|
|
493
|
+
// Panel byte order: split low/high bytes, re-interleave swapped.
|
|
494
|
+
"ee.vunzip.8 q3, q4\n"
|
|
495
|
+
"ee.vzip.8 q4, q3\n"
|
|
496
|
+
"ee.vst.128.ip q4, %[dst], 16\n"
|
|
497
|
+
#else
|
|
498
|
+
"ee.vst.128.ip q3, %[dst], 16\n"
|
|
499
|
+
#endif
|
|
500
|
+
"1:\n"
|
|
501
|
+
: [dst] "+r"(dst), [acc] "+r"(acc), [st] "+r"(st)
|
|
502
|
+
: [n] "r"(count8), [s19] "r"(s19), [s11] "r"(s11), [s18] "r"(s18), [s5] "r"(s5)
|
|
503
|
+
: "memory");
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
// ESP32-S3 PIE additive blend: dst = dst + src per RGB565 channel, each
|
|
507
|
+
// saturating, eight pixels per iteration -- exactly addPixel(src, dst, 255).
|
|
508
|
+
// Blue and green add in place (masked to their fields, so neither can carry
|
|
509
|
+
// into the next); red is moved down a bit first so its sum stays positive in
|
|
510
|
+
// a signed 16-bit lane, then clamped and moved back. vsr/vsl shift 32-bit
|
|
511
|
+
// lanes: the bit that crosses into the lower lane's top is masked off, and
|
|
512
|
+
// the clamped red never sets the bit that would cross back.
|
|
513
|
+
// dst and src must be 16-byte aligned; count8 >= 1 groups of eight.
|
|
514
|
+
alignas(16) inline std::uint16_t g_addPieMasks[24] = {
|
|
515
|
+
0x001F, 0x001F, 0x001F, 0x001F, 0x001F, 0x001F, 0x001F, 0x001F,
|
|
516
|
+
0x07E0, 0x07E0, 0x07E0, 0x07E0, 0x07E0, 0x07E0, 0x07E0, 0x07E0,
|
|
517
|
+
0x7C00, 0x7C00, 0x7C00, 0x7C00, 0x7C00, 0x7C00, 0x7C00, 0x7C00,
|
|
518
|
+
};
|
|
519
|
+
GEA3D_INLINE void addPie(Pixel *dst, const Pixel *src, int count8)
|
|
520
|
+
{
|
|
521
|
+
const std::uint16_t *masks = g_addPieMasks;
|
|
522
|
+
const int one = 1;
|
|
523
|
+
asm volatile(
|
|
524
|
+
"ee.vld.128.ip q5, %[masks], 16\n"
|
|
525
|
+
"ee.vld.128.ip q6, %[masks], 16\n"
|
|
526
|
+
"ee.vld.128.ip q7, %[masks], 0\n"
|
|
527
|
+
"wsr.sar %[one]\n"
|
|
528
|
+
"loopnez %[n], 1f\n"
|
|
529
|
+
"ee.vld.128.ip q0, %[src], 16\n"
|
|
530
|
+
"ee.vld.128.ip q1, %[dst], 0\n"
|
|
531
|
+
"ee.andq q2, q0, q5\n"
|
|
532
|
+
"ee.andq q3, q1, q5\n"
|
|
533
|
+
"ee.vadds.s16 q2, q2, q3\n"
|
|
534
|
+
"ee.vmin.s16 q2, q2, q5\n"
|
|
535
|
+
"ee.andq q3, q0, q6\n"
|
|
536
|
+
"ee.andq q4, q1, q6\n"
|
|
537
|
+
"ee.vadds.s16 q3, q3, q4\n"
|
|
538
|
+
"ee.vmin.s16 q3, q3, q6\n"
|
|
539
|
+
"ee.orq q2, q2, q3\n"
|
|
540
|
+
"ee.vsr.32 q0, q0\n"
|
|
541
|
+
"ee.vsr.32 q1, q1\n"
|
|
542
|
+
"ee.andq q0, q0, q7\n"
|
|
543
|
+
"ee.andq q1, q1, q7\n"
|
|
544
|
+
"ee.vadds.s16 q0, q0, q1\n"
|
|
545
|
+
"ee.vmin.s16 q0, q0, q7\n"
|
|
546
|
+
"ee.vsl.32 q0, q0\n"
|
|
547
|
+
"ee.orq q2, q2, q0\n"
|
|
548
|
+
"ee.vst.128.ip q2, %[dst], 16\n"
|
|
549
|
+
"1:\n"
|
|
550
|
+
: [dst] "+r"(dst), [src] "+r"(src), [masks] "+r"(masks)
|
|
551
|
+
: [n] "r"(count8), [one] "r"(one)
|
|
552
|
+
: "memory");
|
|
553
|
+
}
|
|
554
|
+
#else
|
|
555
|
+
#define GEA3D_PIE_SHADE 0
|
|
556
|
+
#endif
|
|
557
|
+
|
|
330
558
|
// A Gouraud span whose channels stay in 0..0xFFFFFF (16.16). Spans that
|
|
331
559
|
// change colour rarely are filled as flat runs between the changes; the
|
|
332
560
|
// pixels are exactly those of the per-pixel walk.
|
|
@@ -334,6 +562,39 @@ GEA3D_INLINE void shadePixels(Pixel *dst, int count, std::int32_t r, std::int32_
|
|
|
334
562
|
{
|
|
335
563
|
const int last = count - 1;
|
|
336
564
|
const int changes = levelChanges(r, r + dr * last, kShiftR) + levelChanges(g, g + dg * last, kShiftG) + levelChanges(b, b + db * last, kShiftB);
|
|
565
|
+
// A linear ramp whose ends pack to one pixel colour packs to it all the
|
|
566
|
+
// way: a fogged or gently lit face's short rows are mostly such runs.
|
|
567
|
+
if (changes == 0) {
|
|
568
|
+
fillPixels(dst, count, packRgb(r, g, b));
|
|
569
|
+
return;
|
|
570
|
+
}
|
|
571
|
+
#if GEA3D_PIE_SHADE
|
|
572
|
+
// Short rows (Neon's are ~19 px) spent most of their time in the scalar
|
|
573
|
+
// head and tail around one vector group, ~400 cycles a row. Shade whole
|
|
574
|
+
// groups into an aligned scratch row and copy the span out: lanes past the
|
|
575
|
+
// span (whose channels may leave 0..0xFFFFFF) stay in their own lanes and
|
|
576
|
+
// are dropped, so the span is exactly the scalar walk's.
|
|
577
|
+
if (count >= 4 && count <= 64) {
|
|
578
|
+
alignas(16) Pixel row[64];
|
|
579
|
+
shadePie(row, (count + 7) >> 3, r, g, b, dr, dg, db);
|
|
580
|
+
std::memcpy(dst, row, static_cast<std::size_t>(count) * sizeof(Pixel));
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
#endif
|
|
584
|
+
#if GEA3D_PIE_SHADE
|
|
585
|
+
// Long steady runs stay flat vector fills; anything busier is shaded
|
|
586
|
+
// eight pixels at a time with no per-run divides.
|
|
587
|
+
if (count >= 16 && count < 32 * (changes + 1)) {
|
|
588
|
+
const int head = static_cast<int>(((16u - (reinterpret_cast<std::uintptr_t>(dst) & 15u)) & 15u) >> 1);
|
|
589
|
+
shadeEach(dst, head, r, g, b, dr, dg, db);
|
|
590
|
+
const std::int32_t rh = r + dr * head, gh = g + dg * head, bh = b + db * head;
|
|
591
|
+
const int body = (count - head) >> 3;
|
|
592
|
+
shadePie(dst + head, body, rh, gh, bh, dr, dg, db);
|
|
593
|
+
const int done = head + (body << 3);
|
|
594
|
+
shadeEach(dst + done, count - done, r + dr * done, g + dg * done, b + db * done, dr, dg, db);
|
|
595
|
+
return;
|
|
596
|
+
}
|
|
597
|
+
#endif
|
|
337
598
|
if (count < 4 * (changes + 1)) {
|
|
338
599
|
shadeEach(dst, count, r, g, b, dr, dg, db);
|
|
339
600
|
return;
|
|
@@ -358,6 +619,7 @@ GEA3D_INLINE void shadePixels(Pixel *dst, int count, std::int32_t r, std::int32_
|
|
|
358
619
|
|
|
359
620
|
} // namespace detail
|
|
360
621
|
|
|
622
|
+
|
|
361
623
|
// The primitives touching one band, in sorted order (indices into
|
|
362
624
|
// Frame::prims), the first `opaque` of them opaque.
|
|
363
625
|
struct BandList {
|
|
@@ -371,6 +633,31 @@ enum class Occlusion : std::uint8_t {
|
|
|
371
633
|
kPainter, // background first, then back to front: for fast buffers
|
|
372
634
|
};
|
|
373
635
|
|
|
636
|
+
// Sorts a triangle's corners by y (the order drawTriangle would pick, ties
|
|
637
|
+
// included, so its own sort leaves them) and sets its edges up once, on the
|
|
638
|
+
// frame-build core: every band the triangle crosses otherwise re-did this,
|
|
639
|
+
// ~250 cycles a triangle a band on the ESP32-S3.
|
|
640
|
+
inline void prepareTriangle(Prim &p)
|
|
641
|
+
{
|
|
642
|
+
if (p.type != kTriangle) return;
|
|
643
|
+
const auto swapCorners = [&p](int a, int b) {
|
|
644
|
+
std::swap(p.x[a], p.x[b]);
|
|
645
|
+
std::swap(p.y[a], p.y[b]);
|
|
646
|
+
std::swap(p.depth[a], p.depth[b]);
|
|
647
|
+
std::swap(p.r[a], p.r[b]);
|
|
648
|
+
std::swap(p.g[a], p.g[b]);
|
|
649
|
+
std::swap(p.b[a], p.b[b]);
|
|
650
|
+
};
|
|
651
|
+
if (p.y[0] > p.y[1]) swapCorners(0, 1);
|
|
652
|
+
if (p.y[0] > p.y[2]) swapCorners(0, 2);
|
|
653
|
+
if (p.y[1] > p.y[2]) swapCorners(1, 2);
|
|
654
|
+
if (p.y[2] == p.y[0]) return; // no area: drawTriangle skips it
|
|
655
|
+
detail::Edge::setup(p.x[0], p.y[0], p.x[2], p.y[2], p.edgeX[0], p.edgeStep[0]);
|
|
656
|
+
if (p.y[1] != p.y[0]) detail::Edge::setup(p.x[0], p.y[0], p.x[1], p.y[1], p.edgeX[1], p.edgeStep[1]);
|
|
657
|
+
if (p.y[2] != p.y[1]) detail::Edge::setup(p.x[1], p.y[1], p.x[2], p.y[2], p.edgeX[2], p.edgeStep[2]);
|
|
658
|
+
p.flags |= kPrepared;
|
|
659
|
+
}
|
|
660
|
+
|
|
374
661
|
class Rasterizer {
|
|
375
662
|
public:
|
|
376
663
|
Rasterizer(const Frame &frame, Scratch &scratch, Pixel *base, int stride, int x0, int y0, int x1, int y1, Occlusion occlusion = Occlusion::kCoverage)
|
|
@@ -378,6 +665,7 @@ public:
|
|
|
378
665
|
{
|
|
379
666
|
width_ = x1 - x0 + 1;
|
|
380
667
|
height_ = y1 - y0 + 1;
|
|
668
|
+
alignedRows_ = (reinterpret_cast<std::uintptr_t>(base) & 15u) == 0 && (static_cast<std::size_t>(stride) * sizeof(Pixel)) % 16u == 0;
|
|
381
669
|
if (width_ <= 0 || height_ <= 0) return;
|
|
382
670
|
|
|
383
671
|
if (!painter_) bindCoverage();
|
|
@@ -453,6 +741,9 @@ private:
|
|
|
453
741
|
Scratch &scratch_;
|
|
454
742
|
Pixel *base_;
|
|
455
743
|
int stride_;
|
|
744
|
+
// Every row starts on a 16-byte boundary: addRunPie may widen a span to
|
|
745
|
+
// whole vector groups within its row.
|
|
746
|
+
bool alignedRows_ = false;
|
|
456
747
|
int x0_, y0_, x1_, y1_;
|
|
457
748
|
int width_ = 0;
|
|
458
749
|
int height_ = 0;
|
|
@@ -574,7 +865,7 @@ private:
|
|
|
574
865
|
if (rowFirst > rowLast) return;
|
|
575
866
|
|
|
576
867
|
const SpanState span = spanState(prim, blendPass, i0, i1, i2);
|
|
577
|
-
const Walk walk{X0, Y0, X1, Y1, X2, Y2, rowFirst, rowLast, ySplit, cross > 0};
|
|
868
|
+
const Walk walk{X0, Y0, X1, Y1, X2, Y2, rowFirst, rowLast, ySplit, cross > 0, (prim.flags & kPrepared) ? &prim : nullptr};
|
|
578
869
|
|
|
579
870
|
// Painter's order without blending or depth: spans go straight to the band.
|
|
580
871
|
if (painter_ && !span.blend && !useDepth_) {
|
|
@@ -597,6 +888,7 @@ private:
|
|
|
597
888
|
std::int32_t x0, y0, x1, y1, x2, y2;
|
|
598
889
|
int rowFirst, rowLast, ySplit;
|
|
599
890
|
bool longLeft;
|
|
891
|
+
const Prim *prepared; // edges set up by prepareTriangle, or null
|
|
600
892
|
};
|
|
601
893
|
|
|
602
894
|
// Calls emit(row, xa, xb) for the pixels [xa, xb) of each row, clipped to
|
|
@@ -605,16 +897,19 @@ private:
|
|
|
605
897
|
GEA3D_INLINE void walkRows(const Walk &w, Emit emit)
|
|
606
898
|
{
|
|
607
899
|
detail::Edge longEdge;
|
|
608
|
-
|
|
900
|
+
if (w.prepared) longEdge.at(w.prepared->edgeX[0], w.prepared->edgeStep[0], w.y0, w.rowFirst);
|
|
901
|
+
else longEdge.init(w.x0, w.y0, w.x2, w.y2, w.rowFirst);
|
|
609
902
|
detail::Edge shortEdge;
|
|
610
903
|
int row = w.rowFirst;
|
|
611
904
|
if (row < w.ySplit) {
|
|
612
|
-
|
|
905
|
+
if (w.prepared) shortEdge.at(w.prepared->edgeX[1], w.prepared->edgeStep[1], w.y0, row);
|
|
906
|
+
else shortEdge.init(w.x0, w.y0, w.x1, w.y1, row);
|
|
613
907
|
row = walkSection(longEdge, shortEdge, w.longLeft, row, detail::minInt(w.ySplit - 1, w.rowLast), emit);
|
|
614
908
|
}
|
|
615
909
|
if (row > w.rowLast || w.y2 == w.y1) return;
|
|
616
910
|
|
|
617
|
-
|
|
911
|
+
if (w.prepared) shortEdge.at(w.prepared->edgeX[2], w.prepared->edgeStep[2], w.y1, row);
|
|
912
|
+
else shortEdge.init(w.x1, w.y1, w.x2, w.y2, row);
|
|
618
913
|
walkSection(longEdge, shortEdge, w.longLeft, row, w.rowLast, emit);
|
|
619
914
|
}
|
|
620
915
|
|
|
@@ -662,7 +957,7 @@ private:
|
|
|
662
957
|
return span;
|
|
663
958
|
}
|
|
664
959
|
|
|
665
|
-
const float invArea =
|
|
960
|
+
const float invArea = detail::reciprocal(area);
|
|
666
961
|
if (span.smooth) {
|
|
667
962
|
span.r.init(sx, sy, invArea, prim.r[i0], prim.r[i1], prim.r[i2]);
|
|
668
963
|
span.g.init(sx, sy, invArea, prim.g[i0], prim.g[i1], prim.g[i2]);
|
|
@@ -877,6 +1172,18 @@ private:
|
|
|
877
1172
|
const Pixel flat = span.prim->color;
|
|
878
1173
|
const int alpha = span.prim->alpha;
|
|
879
1174
|
const bool add = (span.prim->flags & kAdd) != 0;
|
|
1175
|
+
#if !(GEA_PIXEL_FORMAT_IS_8888 || GEA_PIXEL_FORMAT_IS_GRAY)
|
|
1176
|
+
if (add && !span.depthTest && !span.depthWrite) {
|
|
1177
|
+
#if GEA3D_PIE_SHADE && !GEA_EMBEDDED_PIXEL_PANEL_ENDIAN
|
|
1178
|
+
if (alpha == 255 && alignedRows_ && (!span.smooth || (inRange(r, dr, last) && inRange(g, dg, last) && inRange(b, db, last)))) {
|
|
1179
|
+
addRunPie(dst, count, span.smooth, flat, r, g, b, dr, dg, db);
|
|
1180
|
+
return;
|
|
1181
|
+
}
|
|
1182
|
+
#endif
|
|
1183
|
+
addRun(dst, count, span.smooth, flat, r, g, b, dr, dg, db, alpha);
|
|
1184
|
+
return;
|
|
1185
|
+
}
|
|
1186
|
+
#endif
|
|
880
1187
|
for (int i = 0; i < count; i++) {
|
|
881
1188
|
bool visible = true;
|
|
882
1189
|
if (span.depthTest) {
|
|
@@ -897,6 +1204,104 @@ private:
|
|
|
897
1204
|
}
|
|
898
1205
|
}
|
|
899
1206
|
|
|
1207
|
+
#if !(GEA_PIXEL_FORMAT_IS_8888 || GEA_PIXEL_FORMAT_IS_GRAY)
|
|
1208
|
+
// An additive run on RGB565: the same sums as addPixel(packRgb(..)) per
|
|
1209
|
+
// pixel, with the loop invariants hoisted and the 565 channels of the
|
|
1210
|
+
// source taken straight from the colour ramp instead of packed and
|
|
1211
|
+
// unpacked again. Pixels whose scaled source is black are left alone.
|
|
1212
|
+
static void addRun(Pixel *dst, int count, bool smooth, Pixel flat, std::int32_t r, std::int32_t g, std::int32_t b, std::int32_t dr, std::int32_t dg, std::int32_t db, int alpha)
|
|
1213
|
+
{
|
|
1214
|
+
const int scale = alpha + (alpha >> 7);
|
|
1215
|
+
if (!smooth) {
|
|
1216
|
+
const std::uint32_t f = pixel::toRgb565(flat);
|
|
1217
|
+
const int ar = (static_cast<int>(f >> 11) * scale) >> 8;
|
|
1218
|
+
const int ag = (static_cast<int>((f >> 5) & 63) * scale) >> 8;
|
|
1219
|
+
const int ab = (static_cast<int>(f & 31) * scale) >> 8;
|
|
1220
|
+
if ((ar | ag | ab) == 0) return;
|
|
1221
|
+
for (int i = 0; i < count; i++) dst[i] = addRgb565(dst[i], ar, ag, ab);
|
|
1222
|
+
return;
|
|
1223
|
+
}
|
|
1224
|
+
for (int i = 0; i < count; i++) {
|
|
1225
|
+
const int ar = ((clampColor(r) >> 19) * scale) >> 8;
|
|
1226
|
+
const int ag = ((clampColor(g) >> 18) * scale) >> 8;
|
|
1227
|
+
const int ab = ((clampColor(b) >> 19) * scale) >> 8;
|
|
1228
|
+
r += dr;
|
|
1229
|
+
g += dg;
|
|
1230
|
+
b += db;
|
|
1231
|
+
if ((ar | ag | ab) == 0) continue;
|
|
1232
|
+
dst[i] = addRgb565(dst[i], ar, ag, ab);
|
|
1233
|
+
}
|
|
1234
|
+
}
|
|
1235
|
+
|
|
1236
|
+
#if GEA3D_PIE_SHADE && !GEA_EMBEDDED_PIXEL_PANEL_ENDIAN
|
|
1237
|
+
// addRun at full opacity, eight pixels at a time between a scalar head
|
|
1238
|
+
// (to 16-byte alignment) and tail: the source ramp is shaded into an
|
|
1239
|
+
// aligned row (shadePie: the caller checked its channels stay in range),
|
|
1240
|
+
// then added in. Pixel for pixel the same as addRun.
|
|
1241
|
+
static void addRunPie(Pixel *dst, int count, bool smooth, Pixel flat, std::int32_t r, std::int32_t g, std::int32_t b, std::int32_t dr, std::int32_t dg, std::int32_t db)
|
|
1242
|
+
{
|
|
1243
|
+
if (count < 4) {
|
|
1244
|
+
addOpaqueRamp(dst, count, smooth, flat, r, g, b, dr, dg, db);
|
|
1245
|
+
return;
|
|
1246
|
+
}
|
|
1247
|
+
// Whole aligned groups only, from the 16-byte boundary at or before the
|
|
1248
|
+
// span: lanes outside it add zero, a saturating add of zero leaves a
|
|
1249
|
+
// pixel as it was, and band rows are 16-byte aligned, so the widened
|
|
1250
|
+
// groups stay in this row. The scalar head and tail this replaces were
|
|
1251
|
+
// ~10 cycles a pixel, most of a ~40 px glow row.
|
|
1252
|
+
const int lead = static_cast<int>((reinterpret_cast<std::uintptr_t>(dst) & 15u) >> 1);
|
|
1253
|
+
Pixel *const aligned = dst - lead;
|
|
1254
|
+
const int total = lead + count;
|
|
1255
|
+
alignas(16) Pixel src[64];
|
|
1256
|
+
for (int done = 0; done < total;) {
|
|
1257
|
+
const int chunk = detail::minInt(total - done, 64);
|
|
1258
|
+
const int groups = (chunk + 7) >> 3;
|
|
1259
|
+
const int first = done - lead; // span index of src[0]
|
|
1260
|
+
if (smooth) detail::shadePie(src, groups, r + dr * first, g + dg * first, b + db * first, dr, dg, db);
|
|
1261
|
+
else detail::fillPixels(src, groups << 3, flat);
|
|
1262
|
+
if (done == 0)
|
|
1263
|
+
for (int k = 0; k < lead; k++) src[k] = 0;
|
|
1264
|
+
for (int k = chunk; k < (groups << 3); k++) src[k] = 0;
|
|
1265
|
+
detail::addPie(aligned + done, src, groups);
|
|
1266
|
+
done += groups << 3;
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
#endif
|
|
1270
|
+
|
|
1271
|
+
// addRun at full opacity with every channel of the ramp in range: no
|
|
1272
|
+
// clamps, no scale, no zero test -- the scalar head and tail (and the
|
|
1273
|
+
// short rows) of addRunPie.
|
|
1274
|
+
static GEA3D_INLINE void addOpaqueRamp(Pixel *dst, int count, bool smooth, Pixel flat, std::int32_t r, std::int32_t g, std::int32_t b, std::int32_t dr, std::int32_t dg, std::int32_t db)
|
|
1275
|
+
{
|
|
1276
|
+
if (!smooth) {
|
|
1277
|
+
const std::uint32_t f = pixel::toRgb565(flat);
|
|
1278
|
+
r = static_cast<std::int32_t>(f >> 11) << 19;
|
|
1279
|
+
g = static_cast<std::int32_t>((f >> 5) & 63) << 18;
|
|
1280
|
+
b = static_cast<std::int32_t>(f & 31) << 19;
|
|
1281
|
+
dr = dg = db = 0;
|
|
1282
|
+
}
|
|
1283
|
+
for (int i = 0; i < count; i++) {
|
|
1284
|
+
const std::uint32_t c = pixel::toRgb565(dst[i]);
|
|
1285
|
+
const int rr = detail::minInt(31, static_cast<int>(c >> 11) + (r >> 19));
|
|
1286
|
+
const int gg = detail::minInt(63, static_cast<int>((c >> 5) & 63) + (g >> 18));
|
|
1287
|
+
const int bb = detail::minInt(31, static_cast<int>(c & 31) + (b >> 19));
|
|
1288
|
+
dst[i] = pixel::fromRgb565(static_cast<std::uint16_t>((rr << 11) | (gg << 5) | bb));
|
|
1289
|
+
r += dr;
|
|
1290
|
+
g += dg;
|
|
1291
|
+
b += db;
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
|
|
1295
|
+
static GEA3D_INLINE Pixel addRgb565(Pixel bg, int ar, int ag, int ab)
|
|
1296
|
+
{
|
|
1297
|
+
const std::uint32_t c = pixel::toRgb565(bg);
|
|
1298
|
+
const int rr = detail::minInt(31, static_cast<int>(c >> 11) + ar);
|
|
1299
|
+
const int gg = detail::minInt(63, static_cast<int>((c >> 5) & 63) + ag);
|
|
1300
|
+
const int bb = detail::minInt(31, static_cast<int>(c & 31) + ab);
|
|
1301
|
+
return pixel::fromRgb565(static_cast<std::uint16_t>((rr << 11) | (gg << 5) | bb));
|
|
1302
|
+
}
|
|
1303
|
+
#endif
|
|
1304
|
+
|
|
900
1305
|
static GEA3D_INLINE int absStep(std::int32_t step) { return step < 0 ? -step : step; }
|
|
901
1306
|
|
|
902
1307
|
// inRange for all three channels, when their ramps fit 32 bits: a value
|
|
@@ -1016,7 +1421,7 @@ private:
|
|
|
1016
1421
|
w.from = 1;
|
|
1017
1422
|
}
|
|
1018
1423
|
const float span = mb - ma;
|
|
1019
|
-
w.inv = span > 0.0f ?
|
|
1424
|
+
w.inv = span > 0.0f ? detail::reciprocal(span) : 0.0f;
|
|
1020
1425
|
const float slope = (nb - na) * w.inv;
|
|
1021
1426
|
const int across = detail::maxInt(prim.width, 1);
|
|
1022
1427
|
w.lo = -((across - 1) >> 1);
|
|
@@ -1028,7 +1433,7 @@ private:
|
|
|
1028
1433
|
w.last = detail::minInt(detail::roundToInt(mb), w.xMajor ? x1_ : y1_);
|
|
1029
1434
|
// Where the minor coordinate can round into the region, with slack.
|
|
1030
1435
|
if (slope != 0.0f) {
|
|
1031
|
-
const float perMinor =
|
|
1436
|
+
const float perMinor = detail::reciprocal(slope);
|
|
1032
1437
|
float m0 = ma + (static_cast<float>(w.minorLo - 1 - w.hi) - na) * perMinor;
|
|
1033
1438
|
float m1 = ma + (static_cast<float>(w.minorHi + 1 - w.lo) - na) * perMinor;
|
|
1034
1439
|
if (slope < 0.0f) swapFloat(m0, m1);
|