-
Notifications
You must be signed in to change notification settings - Fork 502
/
vertexcodec.cpp
1837 lines (1417 loc) · 51 KB
/
vertexcodec.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of meshoptimizer library; see meshoptimizer.h for version/license details
#include "meshoptimizer.h"
#include <assert.h>
#include <string.h>
// The block below auto-detects SIMD ISA that can be used on the target platform
#ifndef MESHOPTIMIZER_NO_SIMD
// The SIMD implementation requires SSSE3, which can be enabled unconditionally through compiler settings
#if defined(__AVX__) || defined(__SSSE3__)
#define SIMD_SSE
#endif
// An experimental implementation using AVX512 instructions; it's only enabled when AVX512 is enabled through compiler settings
#if defined(__AVX512VBMI2__) && defined(__AVX512VBMI__) && defined(__AVX512VL__) && defined(__POPCNT__)
#undef SIMD_SSE
#define SIMD_AVX
#endif
// MSVC supports compiling SSSE3 code regardless of compile options; we use a cpuid-based scalar fallback
#if !defined(SIMD_SSE) && !defined(SIMD_AVX) && defined(_MSC_VER) && !defined(__clang__) && (defined(_M_IX86) || defined(_M_X64))
#define SIMD_SSE
#define SIMD_FALLBACK
#endif
// GCC 4.9+ and clang 3.8+ support targeting SIMD ISA from individual functions; we use a cpuid-based scalar fallback
#if !defined(SIMD_SSE) && !defined(SIMD_AVX) && ((defined(__clang__) && __clang_major__ * 100 + __clang_minor__ >= 308) || (defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ >= 409)) && (defined(__i386__) || defined(__x86_64__))
#define SIMD_SSE
#define SIMD_FALLBACK
#define SIMD_TARGET __attribute__((target("ssse3")))
#endif
// GCC/clang define these when NEON support is available
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
#define SIMD_NEON
#endif
// On MSVC, we assume that ARM builds always target NEON-capable devices
#if !defined(SIMD_NEON) && defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
#define SIMD_NEON
#endif
// When targeting Wasm SIMD we can't use runtime cpuid checks so we unconditionally enable SIMD
#if defined(__wasm_simd128__)
#define SIMD_WASM
// Prevent compiling other variant when wasm simd compilation is active
#undef SIMD_NEON
#undef SIMD_SSE
#undef SIMD_AVX
#endif
#ifndef SIMD_TARGET
#define SIMD_TARGET
#endif
// When targeting AArch64/x64, optimize for latency to allow decoding of individual 16-byte groups to overlap
// We don't do this for 32-bit systems because we need 64-bit math for this and this will hurt in-order CPUs
#if defined(__x86_64__) || defined(_M_X64) || defined(__aarch64__) || defined(_M_ARM64)
#define SIMD_LATENCYOPT
#endif
// In switch dispatch, marking default case as unreachable allows to remove redundant bounds checks
#if defined(__GNUC__)
#define SIMD_UNREACHABLE() __builtin_unreachable()
#elif defined(_MSC_VER)
#define SIMD_UNREACHABLE() __assume(false)
#else
#define SIMD_UNREACHABLE() assert(!"Unreachable")
#endif
#endif // !MESHOPTIMIZER_NO_SIMD
#ifdef SIMD_SSE
#include <tmmintrin.h>
#endif
#if defined(SIMD_SSE) && defined(SIMD_FALLBACK)
#ifdef _MSC_VER
#include <intrin.h> // __cpuid
#else
#include <cpuid.h> // __cpuid
#endif
#endif
#ifdef SIMD_AVX
#include <immintrin.h>
#endif
#ifdef SIMD_NEON
#if defined(_MSC_VER) && defined(_M_ARM64)
#include <arm64_neon.h>
#else
#include <arm_neon.h>
#endif
#endif
#ifdef SIMD_WASM
#include <wasm_simd128.h>
#endif
#ifndef TRACE
#define TRACE 0
#endif
#if TRACE
#include <stdio.h>
#endif
#ifdef SIMD_WASM
#define wasmx_splat_v32x4(v, i) wasm_i32x4_shuffle(v, v, i, i, i, i)
#define wasmx_unpacklo_v8x16(a, b) wasm_i8x16_shuffle(a, b, 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23)
#define wasmx_unpackhi_v8x16(a, b) wasm_i8x16_shuffle(a, b, 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31)
#define wasmx_unpacklo_v16x8(a, b) wasm_i16x8_shuffle(a, b, 0, 8, 1, 9, 2, 10, 3, 11)
#define wasmx_unpackhi_v16x8(a, b) wasm_i16x8_shuffle(a, b, 4, 12, 5, 13, 6, 14, 7, 15)
#define wasmx_unpacklo_v64x2(a, b) wasm_i64x2_shuffle(a, b, 0, 2)
#define wasmx_unpackhi_v64x2(a, b) wasm_i64x2_shuffle(a, b, 1, 3)
#endif
namespace meshopt
{
const unsigned char kVertexHeader = 0xa0;
static int gEncodeVertexVersion = 0;
const size_t kVertexBlockSizeBytes = 8192;
const size_t kVertexBlockMaxSize = 256;
const size_t kByteGroupSize = 16;
const size_t kByteGroupDecodeLimit = 24;
const size_t kTailMinSize = 32; // must be >= kByteGroupDecodeLimit
static const int kBitsV0[4] = {0, 2, 4, 8};
static const int kBitsV1[5] = {0, 1, 2, 4, 8};
const int kEncodeMaxChannel = 3;
static size_t getVertexBlockSize(size_t vertex_size)
{
// make sure the entire block fits into the scratch buffer
size_t result = kVertexBlockSizeBytes / vertex_size;
// align to byte group size; we encode each byte as a byte group
// if vertex block is misaligned, it results in wasted bytes, so just truncate the block size
result &= ~(kByteGroupSize - 1);
return (result < kVertexBlockMaxSize) ? result : kVertexBlockMaxSize;
}
inline unsigned char zigzag(unsigned char v)
{
return ((signed char)(v) >> 7) ^ (v << 1);
}
inline unsigned short zigzag(unsigned short v)
{
return ((signed short)(v) >> 15) ^ (v << 1);
}
template <typename T>
inline T unzigzag(T v)
{
return (0 - (v & 1)) ^ (v >> 1);
}
#if TRACE
struct Stats
{
size_t size;
size_t header; // bytes for header
size_t bitg[9]; // bytes for bit groups
size_t bitc[8]; // bit consistency: how many bits are shared between all bytes in a group
size_t ctrl[4]; // number of control groups
};
static Stats* bytestats = NULL;
static Stats vertexstats[256];
#endif
static bool canEncodeZero(const unsigned char* buffer, size_t buffer_size)
{
for (size_t i = 0; i < buffer_size; ++i)
if (buffer[i])
return false;
return true;
}
static size_t encodeBytesGroupMeasure(const unsigned char* buffer, int bits)
{
assert(bits >= 0 && bits <= 8);
if (bits == 0)
return canEncodeZero(buffer, kByteGroupSize) ? 0 : size_t(-1);
if (bits == 8)
return kByteGroupSize;
size_t result = kByteGroupSize * bits / 8;
unsigned char sentinel = (1 << bits) - 1;
for (size_t i = 0; i < kByteGroupSize; ++i)
result += buffer[i] >= sentinel;
return result;
}
static unsigned char* encodeBytesGroup(unsigned char* data, const unsigned char* buffer, int bits)
{
assert(bits >= 0 && bits <= 8);
assert(kByteGroupSize % 8 == 0);
if (bits == 0)
return data;
if (bits == 8)
{
memcpy(data, buffer, kByteGroupSize);
return data + kByteGroupSize;
}
size_t byte_size = 8 / bits;
assert(kByteGroupSize % byte_size == 0);
// fixed portion: bits bits for each value
// variable portion: full byte for each out-of-range value (using 1...1 as sentinel)
unsigned char sentinel = (1 << bits) - 1;
for (size_t i = 0; i < kByteGroupSize; i += byte_size)
{
unsigned char byte = 0;
for (size_t k = 0; k < byte_size; ++k)
{
unsigned char enc = (buffer[i + k] >= sentinel) ? sentinel : buffer[i + k];
byte <<= bits;
byte |= enc;
}
// encode 1-bit groups in reverse bit order
// this makes them faster to decode alongside other groups
if (bits == 1)
byte = (unsigned char)(((byte * 0x80200802ull) & 0x0884422110ull) * 0x0101010101ull >> 32);
*data++ = byte;
}
for (size_t i = 0; i < kByteGroupSize; ++i)
{
if (buffer[i] >= sentinel)
{
*data++ = buffer[i];
}
}
return data;
}
static unsigned char* encodeBytes(unsigned char* data, unsigned char* data_end, const unsigned char* buffer, size_t buffer_size, const int bits[4])
{
assert(buffer_size % kByteGroupSize == 0);
unsigned char* header = data;
// round number of groups to 4 to get number of header bytes
size_t header_size = (buffer_size / kByteGroupSize + 3) / 4;
if (size_t(data_end - data) < header_size)
return NULL;
data += header_size;
memset(header, 0, header_size);
int last_bits = -1;
for (size_t i = 0; i < buffer_size; i += kByteGroupSize)
{
if (size_t(data_end - data) < kByteGroupDecodeLimit)
return NULL;
int best_bitk = 3;
size_t best_size = encodeBytesGroupMeasure(buffer + i, bits[best_bitk]);
for (int bitk = 0; bitk < 3; ++bitk)
{
size_t size = encodeBytesGroupMeasure(buffer + i, bits[bitk]);
// favor consistent bit selection across groups, but never replace literals
if (size < best_size || (size == best_size && bits[bitk] == last_bits && bits[best_bitk] != 8))
{
best_bitk = bitk;
best_size = size;
}
}
size_t header_offset = i / kByteGroupSize;
header[header_offset / 4] |= best_bitk << ((header_offset % 4) * 2);
int best_bits = bits[best_bitk];
unsigned char* next = encodeBytesGroup(data, buffer + i, best_bits);
assert(data + best_size == next);
data = next;
last_bits = best_bits;
#if TRACE
bytestats->bitg[best_bits] += best_size;
#endif
}
#if TRACE
bytestats->header += header_size;
#endif
return data;
}
static size_t encodeBytesMeasure(const unsigned char* buffer, size_t buffer_size, const int bits[4])
{
assert(buffer_size % kByteGroupSize == 0);
size_t result = 0;
// round number of groups to 4 to get number of header bytes
size_t header_size = (buffer_size / kByteGroupSize + 3) / 4;
result += header_size;
for (size_t i = 0; i < buffer_size; i += kByteGroupSize)
{
size_t best_size = size_t(-1);
for (int bitk = 0; bitk < 4; ++bitk)
{
size_t size = encodeBytesGroupMeasure(buffer + i, bits[bitk]);
best_size = (size < best_size) ? size : best_size;
}
result += best_size;
}
return result;
}
template <typename T, bool Xor>
static void encodeDeltas1(unsigned char* buffer, const unsigned char* vertex_data, size_t vertex_count, size_t vertex_size, const unsigned char last_vertex[256], size_t k)
{
size_t k0 = k & ~(sizeof(T) - 1);
int ks = (k & (sizeof(T) - 1)) * 8;
T p = last_vertex[k0];
for (size_t j = 1; j < sizeof(T); ++j)
p |= T(last_vertex[k0 + j]) << (j * 8);
for (size_t i = 0; i < vertex_count; ++i)
{
T v = vertex_data[i * vertex_size + k0];
for (size_t j = 1; j < sizeof(T); ++j)
v |= vertex_data[i * vertex_size + k0 + j] << (j * 8);
T d = Xor ? v ^ p : zigzag(T(v - p));
buffer[i] = (unsigned char)(d >> ks);
p = v;
}
}
static void encodeDeltas(unsigned char* buffer, const unsigned char* vertex_data, size_t vertex_count, size_t vertex_size, const unsigned char last_vertex[256], size_t k, int channel)
{
switch (channel)
{
case 0:
return encodeDeltas1<unsigned char, false>(buffer, vertex_data, vertex_count, vertex_size, last_vertex, k);
case 1:
return encodeDeltas1<unsigned short, false>(buffer, vertex_data, vertex_count, vertex_size, last_vertex, k);
case 2:
return encodeDeltas1<unsigned char, true>(buffer, vertex_data, vertex_count, vertex_size, last_vertex, k);
default:
assert(!"Unsupported channel encoding");
}
}
static int estimateChannel(const unsigned char* vertex_data, size_t vertex_count, size_t vertex_size, size_t k, int max_channel)
{
if (vertex_count == 0 || max_channel <= 1)
return 0;
unsigned char block[kVertexBlockMaxSize];
unsigned char last_vertex[256] = {};
memcpy(last_vertex, vertex_data, vertex_size);
size_t sizes[4] = {};
const int* bits = kBitsV1 + 1;
for (size_t i = 0; i < vertex_count; i += kVertexBlockMaxSize)
{
size_t block_size = i + kVertexBlockMaxSize < vertex_count ? kVertexBlockMaxSize : vertex_count - i;
size_t block_size_aligned = (block_size + kByteGroupSize - 1) & ~(kByteGroupSize - 1);
// we sometimes encode elements we didn't fill when rounding to kByteGroupSize
memset(block, 0, block_size_aligned);
for (int channel = 0; channel < max_channel; ++channel)
{
for (size_t j = 0; j < 4; ++j)
{
encodeDeltas(block, vertex_data + i * vertex_size, block_size, vertex_size, last_vertex, k + j, channel);
sizes[channel] += encodeBytesMeasure(block, block_size_aligned, bits);
}
}
}
int best_channel = 0;
for (int channel = 1; channel < max_channel; ++channel)
best_channel = (sizes[channel] < sizes[best_channel]) ? channel : best_channel;
return best_channel;
}
static unsigned char* encodeVertexBlock(unsigned char* data, unsigned char* data_end, const unsigned char* vertex_data, size_t vertex_count, size_t vertex_size, unsigned char last_vertex[256], const unsigned char* channels, int version)
{
assert(vertex_count > 0 && vertex_count <= kVertexBlockMaxSize);
assert(vertex_size % 4 == 0);
unsigned char buffer[kVertexBlockMaxSize];
assert(sizeof(buffer) % kByteGroupSize == 0);
size_t vertex_count_aligned = (vertex_count + kByteGroupSize - 1) & ~(kByteGroupSize - 1);
// we sometimes encode elements we didn't fill when rounding to kByteGroupSize
memset(buffer, 0, sizeof(buffer));
size_t control_size = version == 0 ? 0 : vertex_size / 4;
if (size_t(data_end - data) < control_size)
return NULL;
unsigned char* control = data;
data += control_size;
memset(control, 0, control_size);
for (size_t k = 0; k < vertex_size; ++k)
{
encodeDeltas(buffer, vertex_data, vertex_count, vertex_size, last_vertex, k, version == 0 ? 0 : channels[k / 4]);
#if TRACE
const unsigned char* olddata = data;
bytestats = &vertexstats[k];
for (size_t ig = 0; ig < vertex_count; ig += kByteGroupSize)
{
unsigned char last = (ig == 0) ? last_vertex[k] : vertex_data[vertex_size * (ig - 1) + k];
unsigned char delta = 0xff;
for (size_t i = ig; i < ig + kByteGroupSize && i < vertex_count; ++i)
delta &= ~(vertex_data[vertex_size * i + k] ^ last);
for (int j = 0; j < 8; ++j)
bytestats->bitc[j] += (vertex_count - ig < kByteGroupSize ? vertex_count - ig : kByteGroupSize) * ((delta >> j) & 1);
}
#endif
if (version == 0xe)
{
int best_ctrl = 3; // literal encoding
size_t best_bytes = vertex_count;
if (canEncodeZero(buffer, vertex_count))
{
// zero encoding
best_ctrl = 2;
best_bytes = 0;
}
else
{
// pick shortest control entry
for (int i = 0; i < 2; ++i)
{
size_t est_bytes = encodeBytesMeasure(buffer, vertex_count_aligned, kBitsV1 + i);
if (est_bytes < best_bytes)
{
best_ctrl = i;
best_bytes = est_bytes;
}
}
}
control[k / 4] |= best_ctrl << ((k % 4) * 2);
#if TRACE
vertexstats[k].ctrl[best_ctrl]++;
#endif
if (best_ctrl == 3)
{
// literal encoding
if (size_t(data_end - data) < vertex_count)
return NULL;
memcpy(data, buffer, vertex_count);
data += vertex_count;
}
else if (best_ctrl != 2)
{
unsigned char* next = encodeBytes(data, data_end, buffer, vertex_count_aligned, kBitsV1 + best_ctrl);
if (!next)
return NULL;
assert(data + best_bytes == next);
data = next;
}
}
else
{
data = encodeBytes(data, data_end, buffer, vertex_count_aligned, kBitsV0);
if (!data)
return NULL;
}
#if TRACE
bytestats = NULL;
vertexstats[k].size += data - olddata;
#endif
}
memcpy(last_vertex, &vertex_data[vertex_size * (vertex_count - 1)], vertex_size);
return data;
}
#if defined(SIMD_FALLBACK) || (!defined(SIMD_SSE) && !defined(SIMD_NEON) && !defined(SIMD_AVX) && !defined(SIMD_WASM))
static const unsigned char* decodeBytesGroup(const unsigned char* data, unsigned char* buffer, int bits)
{
#define READ() byte = *data++
#define NEXT(bits) enc = byte >> (8 - bits), byte <<= bits, encv = *data_var, *buffer++ = (enc == (1 << bits) - 1) ? encv : enc, data_var += (enc == (1 << bits) - 1)
unsigned char byte, enc, encv;
const unsigned char* data_var;
switch (bits)
{
case 0:
memset(buffer, 0, kByteGroupSize);
return data;
case 1:
data_var = data + 2;
// 2 groups with 8 1-bit values in each byte (reversed from the order in other groups)
READ();
byte = (unsigned char)(((byte * 0x80200802ull) & 0x0884422110ull) * 0x0101010101ull >> 32);
NEXT(1), NEXT(1), NEXT(1), NEXT(1), NEXT(1), NEXT(1), NEXT(1), NEXT(1);
READ();
byte = (unsigned char)(((byte * 0x80200802ull) & 0x0884422110ull) * 0x0101010101ull >> 32);
NEXT(1), NEXT(1), NEXT(1), NEXT(1), NEXT(1), NEXT(1), NEXT(1), NEXT(1);
return data_var;
case 2:
data_var = data + 4;
// 4 groups with 4 2-bit values in each byte
READ(), NEXT(2), NEXT(2), NEXT(2), NEXT(2);
READ(), NEXT(2), NEXT(2), NEXT(2), NEXT(2);
READ(), NEXT(2), NEXT(2), NEXT(2), NEXT(2);
READ(), NEXT(2), NEXT(2), NEXT(2), NEXT(2);
return data_var;
case 4:
data_var = data + 8;
// 8 groups with 2 4-bit values in each byte
READ(), NEXT(4), NEXT(4);
READ(), NEXT(4), NEXT(4);
READ(), NEXT(4), NEXT(4);
READ(), NEXT(4), NEXT(4);
READ(), NEXT(4), NEXT(4);
READ(), NEXT(4), NEXT(4);
READ(), NEXT(4), NEXT(4);
READ(), NEXT(4), NEXT(4);
return data_var;
case 8:
memcpy(buffer, data, kByteGroupSize);
return data + kByteGroupSize;
default:
assert(!"Unexpected bit length"); // unreachable
return data;
}
#undef READ
#undef NEXT
}
static const unsigned char* decodeBytes(const unsigned char* data, const unsigned char* data_end, unsigned char* buffer, size_t buffer_size, const int* bits)
{
assert(buffer_size % kByteGroupSize == 0);
// round number of groups to 4 to get number of header bytes
size_t header_size = (buffer_size / kByteGroupSize + 3) / 4;
if (size_t(data_end - data) < header_size)
return NULL;
const unsigned char* header = data;
data += header_size;
for (size_t i = 0; i < buffer_size; i += kByteGroupSize)
{
if (size_t(data_end - data) < kByteGroupDecodeLimit)
return NULL;
size_t header_offset = i / kByteGroupSize;
int bitsk = (header[header_offset / 4] >> ((header_offset % 4) * 2)) & 3;
data = decodeBytesGroup(data, buffer + i, bits[bitsk]);
}
return data;
}
template <typename T, bool Xor>
static void decodeDeltas1(const unsigned char* buffer, unsigned char* transposed, size_t vertex_count, size_t vertex_size, const unsigned char* last_vertex)
{
for (size_t k = 0; k < 4; k += sizeof(T))
{
size_t vertex_offset = k;
T p = last_vertex[0];
for (size_t j = 1; j < sizeof(T); ++j)
p |= last_vertex[j] << (8 * j);
for (size_t i = 0; i < vertex_count; ++i)
{
T v = buffer[i];
for (size_t j = 1; j < sizeof(T); ++j)
v |= buffer[i + vertex_count * j] << (8 * j);
v = Xor ? v ^ p : unzigzag(v) + p;
for (size_t j = 0; j < sizeof(T); ++j)
transposed[vertex_offset + j] = (unsigned char)(v >> (j * 8));
p = v;
vertex_offset += vertex_size;
}
buffer += vertex_count * sizeof(T);
last_vertex += sizeof(T);
}
}
static const unsigned char* decodeVertexBlock(const unsigned char* data, const unsigned char* data_end, unsigned char* vertex_data, size_t vertex_count, size_t vertex_size, unsigned char last_vertex[256], const unsigned char* channels, int version)
{
assert(vertex_count > 0 && vertex_count <= kVertexBlockMaxSize);
unsigned char buffer[kVertexBlockMaxSize * 4];
unsigned char transposed[kVertexBlockSizeBytes];
size_t vertex_count_aligned = (vertex_count + kByteGroupSize - 1) & ~(kByteGroupSize - 1);
assert(vertex_count <= vertex_count_aligned);
size_t control_size = version == 0 ? 0 : vertex_size / 4;
if (size_t(data_end - data) < control_size)
return NULL;
const unsigned char* control = data;
data += control_size;
for (size_t k = 0; k < vertex_size; k += 4)
{
unsigned char ctrl_byte = version == 0 ? 0 : control[k / 4];
for (size_t j = 0; j < 4; ++j)
{
int ctrl = (ctrl_byte >> (j * 2)) & 3;
if (ctrl == 3)
{
// literal encoding
if (size_t(data_end - data) < vertex_count)
return NULL;
memcpy(buffer + j * vertex_count, data, vertex_count);
data += vertex_count;
}
else if (ctrl == 2)
{
// zero encoding
memset(buffer + j * vertex_count, 0, vertex_count);
}
else
{
data = decodeBytes(data, data_end, buffer + j * vertex_count, vertex_count_aligned, version == 0 ? kBitsV0 : kBitsV1 + ctrl);
if (!data)
return NULL;
}
}
int channel = version == 0 ? 0 : channels[k / 4];
switch (channel)
{
case 0:
decodeDeltas1<unsigned char, false>(buffer, transposed + k, vertex_count, vertex_size, last_vertex + k);
break;
case 1:
decodeDeltas1<unsigned short, false>(buffer, transposed + k, vertex_count, vertex_size, last_vertex + k);
break;
case 2:
decodeDeltas1<unsigned int, true>(buffer, transposed + k, vertex_count, vertex_size, last_vertex + k);
break;
default:
// invalid channel type
return NULL;
}
}
memcpy(vertex_data, transposed, vertex_count * vertex_size);
memcpy(last_vertex, &transposed[vertex_size * (vertex_count - 1)], vertex_size);
return data;
}
#endif
#if defined(SIMD_SSE) || defined(SIMD_NEON) || defined(SIMD_WASM)
static unsigned char kDecodeBytesGroupShuffle[256][8];
static unsigned char kDecodeBytesGroupCount[256];
#ifdef __wasm__
__attribute__((cold)) // this saves 500 bytes in the output binary - we don't need to vectorize this loop!
#endif
static bool
decodeBytesGroupBuildTables()
{
for (int mask = 0; mask < 256; ++mask)
{
unsigned char shuffle[8];
unsigned char count = 0;
for (int i = 0; i < 8; ++i)
{
int maski = (mask >> i) & 1;
shuffle[i] = maski ? count : 0x80;
count += (unsigned char)(maski);
}
memcpy(kDecodeBytesGroupShuffle[mask], shuffle, 8);
kDecodeBytesGroupCount[mask] = count;
}
return true;
}
static bool gDecodeBytesGroupInitialized = decodeBytesGroupBuildTables();
#endif
#ifdef SIMD_SSE
SIMD_TARGET
inline __m128i decodeShuffleMask(unsigned char mask0, unsigned char mask1)
{
__m128i sm0 = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(&kDecodeBytesGroupShuffle[mask0]));
__m128i sm1 = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(&kDecodeBytesGroupShuffle[mask1]));
__m128i sm1off = _mm_set1_epi8(kDecodeBytesGroupCount[mask0]);
__m128i sm1r = _mm_add_epi8(sm1, sm1off);
return _mm_unpacklo_epi64(sm0, sm1r);
}
SIMD_TARGET
inline const unsigned char* decodeBytesGroupSimd(const unsigned char* data, unsigned char* buffer, int hbits)
{
switch (hbits)
{
case 0:
case 4:
{
__m128i result = _mm_setzero_si128();
_mm_storeu_si128(reinterpret_cast<__m128i*>(buffer), result);
return data;
}
case 1:
case 6:
{
#ifdef __GNUC__
typedef int __attribute__((aligned(1))) unaligned_int;
#else
typedef int unaligned_int;
#endif
#ifdef SIMD_LATENCYOPT
unsigned int data32;
memcpy(&data32, data, 4);
data32 &= data32 >> 1;
// arrange bits such that low bits of nibbles of data64 contain all 2-bit elements of data32
unsigned long long data64 = ((unsigned long long)data32 << 30) | (data32 & 0x3fffffff);
// adds all 1-bit nibbles together; the sum fits in 4 bits because datacnt=16 would have used mode 3
int datacnt = int(((data64 & 0x1111111111111111ull) * 0x1111111111111111ull) >> 60);
#endif
__m128i sel2 = _mm_cvtsi32_si128(*reinterpret_cast<const unaligned_int*>(data));
__m128i rest = _mm_loadu_si128(reinterpret_cast<const __m128i*>(data + 4));
__m128i sel22 = _mm_unpacklo_epi8(_mm_srli_epi16(sel2, 4), sel2);
__m128i sel2222 = _mm_unpacklo_epi8(_mm_srli_epi16(sel22, 2), sel22);
__m128i sel = _mm_and_si128(sel2222, _mm_set1_epi8(3));
__m128i mask = _mm_cmpeq_epi8(sel, _mm_set1_epi8(3));
int mask16 = _mm_movemask_epi8(mask);
unsigned char mask0 = (unsigned char)(mask16 & 255);
unsigned char mask1 = (unsigned char)(mask16 >> 8);
__m128i shuf = decodeShuffleMask(mask0, mask1);
__m128i result = _mm_or_si128(_mm_shuffle_epi8(rest, shuf), _mm_andnot_si128(mask, sel));
_mm_storeu_si128(reinterpret_cast<__m128i*>(buffer), result);
#ifdef SIMD_LATENCYOPT
return data + 4 + datacnt;
#else
return data + 4 + kDecodeBytesGroupCount[mask0] + kDecodeBytesGroupCount[mask1];
#endif
}
case 2:
case 7:
{
#ifdef SIMD_LATENCYOPT
unsigned long long data64;
memcpy(&data64, data, 8);
data64 &= data64 >> 1;
data64 &= data64 >> 2;
// adds all 1-bit nibbles together; the sum fits in 4 bits because datacnt=16 would have used mode 3
int datacnt = int(((data64 & 0x1111111111111111ull) * 0x1111111111111111ull) >> 60);
#endif
__m128i sel4 = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(data));
__m128i rest = _mm_loadu_si128(reinterpret_cast<const __m128i*>(data + 8));
__m128i sel44 = _mm_unpacklo_epi8(_mm_srli_epi16(sel4, 4), sel4);
__m128i sel = _mm_and_si128(sel44, _mm_set1_epi8(15));
__m128i mask = _mm_cmpeq_epi8(sel, _mm_set1_epi8(15));
int mask16 = _mm_movemask_epi8(mask);
unsigned char mask0 = (unsigned char)(mask16 & 255);
unsigned char mask1 = (unsigned char)(mask16 >> 8);
__m128i shuf = decodeShuffleMask(mask0, mask1);
__m128i result = _mm_or_si128(_mm_shuffle_epi8(rest, shuf), _mm_andnot_si128(mask, sel));
_mm_storeu_si128(reinterpret_cast<__m128i*>(buffer), result);
#ifdef SIMD_LATENCYOPT
return data + 8 + datacnt;
#else
return data + 8 + kDecodeBytesGroupCount[mask0] + kDecodeBytesGroupCount[mask1];
#endif
}
case 3:
case 8:
{
__m128i result = _mm_loadu_si128(reinterpret_cast<const __m128i*>(data));
_mm_storeu_si128(reinterpret_cast<__m128i*>(buffer), result);
return data + 16;
}
case 5:
{
__m128i rest = _mm_loadu_si128(reinterpret_cast<const __m128i*>(data + 2));
unsigned char mask0 = data[0];
unsigned char mask1 = data[1];
__m128i shuf = decodeShuffleMask(mask0, mask1);
__m128i result = _mm_shuffle_epi8(rest, shuf);
_mm_storeu_si128(reinterpret_cast<__m128i*>(buffer), result);
return data + 2 + kDecodeBytesGroupCount[mask0] + kDecodeBytesGroupCount[mask1];
}
default:
SIMD_UNREACHABLE();
}
}
#endif
#ifdef SIMD_AVX
static const __m128i decodeBytesGroupConfig[2][8] = {
{
_mm_setzero_si128(),
_mm_set1_epi8(3),
_mm_set1_epi8(15),
_mm_setzero_si128(),
_mm_setzero_si128(),
_mm_set1_epi8(1),
_mm_set1_epi8(3),
_mm_set1_epi8(15),
},
{
_mm_setzero_si128(),
_mm_setr_epi8(6, 4, 2, 0, 14, 12, 10, 8, 22, 20, 18, 16, 30, 28, 26, 24),
_mm_setr_epi8(4, 0, 12, 8, 20, 16, 28, 24, 36, 32, 44, 40, 52, 48, 60, 56),
_mm_setzero_si128(),
_mm_setzero_si128(),
_mm_setr_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),
_mm_setr_epi8(6, 4, 2, 0, 14, 12, 10, 8, 22, 20, 18, 16, 30, 28, 26, 24),
_mm_setr_epi8(4, 0, 12, 8, 20, 16, 28, 24, 36, 32, 44, 40, 52, 48, 60, 56),
},
};
SIMD_TARGET
inline const unsigned char* decodeBytesGroupSimd(const unsigned char* data, unsigned char* buffer, int hbits)
{
switch (hbits)
{
case 0:
case 4:
{
__m128i result = _mm_setzero_si128();
_mm_storeu_si128(reinterpret_cast<__m128i*>(buffer), result);
return data;
}
case 5: // 1-bit
case 1: // 2-bit
case 6:
case 2: // 4-bit
case 7:
{
const unsigned char* skip = data + (2 << (hbits < 3 ? hbits : hbits - 5));
__m128i selb = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(data));
__m128i rest = _mm_loadu_si128(reinterpret_cast<const __m128i*>(skip));
__m128i sent = decodeBytesGroupConfig[0][hbits];
__m128i ctrl = decodeBytesGroupConfig[1][hbits];
__m128i selw = _mm_shuffle_epi32(selb, 0x44);
__m128i sel = _mm_and_si128(sent, _mm_multishift_epi64_epi8(ctrl, selw));
__mmask16 mask16 = _mm_cmp_epi8_mask(sel, sent, _MM_CMPINT_EQ);
__m128i result = _mm_mask_expand_epi8(sel, mask16, rest);
_mm_storeu_si128(reinterpret_cast<__m128i*>(buffer), result);
return skip + _mm_popcnt_u32(mask16);
}
case 3:
case 8:
{
__m128i result = _mm_loadu_si128(reinterpret_cast<const __m128i*>(data));
_mm_storeu_si128(reinterpret_cast<__m128i*>(buffer), result);
return data + 16;
}
default:
SIMD_UNREACHABLE();
}
}
#endif
#ifdef SIMD_NEON
SIMD_TARGET
inline uint8x16_t shuffleBytes(unsigned char mask0, unsigned char mask1, uint8x8_t rest0, uint8x8_t rest1)
{
uint8x8_t sm0 = vld1_u8(kDecodeBytesGroupShuffle[mask0]);
uint8x8_t sm1 = vld1_u8(kDecodeBytesGroupShuffle[mask1]);
uint8x8_t r0 = vtbl1_u8(rest0, sm0);
uint8x8_t r1 = vtbl1_u8(rest1, sm1);
return vcombine_u8(r0, r1);
}
SIMD_TARGET
inline void neonMoveMask(uint8x16_t mask, unsigned char& mask0, unsigned char& mask1)