Repository navigation
Expand file tree
/
Copy pathformatpacking.cpp
More file actions
1606 lines (1407 loc) · 48.5 KB
/
Copy pathformatpacking.cpp
File metadata and controls
1606 lines (1407 loc) · 48.5 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
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
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2019-2026 Baldur Karlsson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
#include "formatpacking.h"
#include <float.h>
#include <math.h>
#include "api/replay/data_types.h"
#include "api/replay/rdcpair.h"
#include "common/common.h"
#include "os/os_specific.h"
// for(int i=0; i < 256; i++)
// {
// uint8_t comp = i&0xff;
// float srgbF = float(comp)/255.0f;
//
// if(srgbF <= 0.04045f)
// SRGB8_lookuptable[comp] = srgbF/12.92f;
// else
// SRGB8_lookuptable[comp] = powf((0.055f + srgbF) / 1.055f, 2.4f);
// }
float SRGB8_lookuptable[256] = {
0.000000f, 0.000304f, 0.000607f, 0.000911f, 0.001214f, 0.001518f, 0.001821f, 0.002125f,
0.002428f, 0.002732f, 0.003035f, 0.003347f, 0.003677f, 0.004025f, 0.004391f, 0.004777f,
0.005182f, 0.005605f, 0.006049f, 0.006512f, 0.006995f, 0.007499f, 0.008023f, 0.008568f,
0.009134f, 0.009721f, 0.010330f, 0.010960f, 0.011612f, 0.012286f, 0.012983f, 0.013702f,
0.014444f, 0.015209f, 0.015996f, 0.016807f, 0.017642f, 0.018500f, 0.019382f, 0.020289f,
0.021219f, 0.022174f, 0.023153f, 0.024158f, 0.025187f, 0.026241f, 0.027321f, 0.028426f,
0.029557f, 0.030713f, 0.031896f, 0.033105f, 0.034340f, 0.035601f, 0.036889f, 0.038204f,
0.039546f, 0.040915f, 0.042311f, 0.043735f, 0.045186f, 0.046665f, 0.048172f, 0.049707f,
0.051269f, 0.052861f, 0.054480f, 0.056128f, 0.057805f, 0.059511f, 0.061246f, 0.063010f,
0.064803f, 0.066626f, 0.068478f, 0.070360f, 0.072272f, 0.074214f, 0.076185f, 0.078187f,
0.080220f, 0.082283f, 0.084376f, 0.086500f, 0.088656f, 0.090842f, 0.093059f, 0.095307f,
0.097587f, 0.099899f, 0.102242f, 0.104616f, 0.107023f, 0.109462f, 0.111932f, 0.114435f,
0.116971f, 0.119538f, 0.122139f, 0.124772f, 0.127438f, 0.130136f, 0.132868f, 0.135633f,
0.138432f, 0.141263f, 0.144128f, 0.147027f, 0.149960f, 0.152926f, 0.155926f, 0.158961f,
0.162029f, 0.165132f, 0.168269f, 0.171441f, 0.174647f, 0.177888f, 0.181164f, 0.184475f,
0.187821f, 0.191202f, 0.194618f, 0.198069f, 0.201556f, 0.205079f, 0.208637f, 0.212231f,
0.215861f, 0.219526f, 0.223228f, 0.226966f, 0.230740f, 0.234551f, 0.238398f, 0.242281f,
0.246201f, 0.250158f, 0.254152f, 0.258183f, 0.262251f, 0.266356f, 0.270498f, 0.274677f,
0.278894f, 0.283149f, 0.287441f, 0.291771f, 0.296138f, 0.300544f, 0.304987f, 0.309469f,
0.313989f, 0.318547f, 0.323143f, 0.327778f, 0.332452f, 0.337164f, 0.341914f, 0.346704f,
0.351533f, 0.356400f, 0.361307f, 0.366253f, 0.371238f, 0.376262f, 0.381326f, 0.386430f,
0.391573f, 0.396755f, 0.401978f, 0.407240f, 0.412543f, 0.417885f, 0.423268f, 0.428691f,
0.434154f, 0.439657f, 0.445201f, 0.450786f, 0.456411f, 0.462077f, 0.467784f, 0.473532f,
0.479320f, 0.485150f, 0.491021f, 0.496933f, 0.502887f, 0.508881f, 0.514918f, 0.520996f,
0.527115f, 0.533276f, 0.539480f, 0.545725f, 0.552011f, 0.558340f, 0.564712f, 0.571125f,
0.577581f, 0.584078f, 0.590619f, 0.597202f, 0.603827f, 0.610496f, 0.617207f, 0.623960f,
0.630757f, 0.637597f, 0.644480f, 0.651406f, 0.658375f, 0.665387f, 0.672443f, 0.679543f,
0.686685f, 0.693872f, 0.701102f, 0.708376f, 0.715694f, 0.723055f, 0.730461f, 0.737911f,
0.745404f, 0.752942f, 0.760525f, 0.768151f, 0.775822f, 0.783538f, 0.791298f, 0.799103f,
0.806952f, 0.814847f, 0.822786f, 0.830770f, 0.838799f, 0.846873f, 0.854993f, 0.863157f,
0.871367f, 0.879622f, 0.887923f, 0.896269f, 0.904661f, 0.913099f, 0.921582f, 0.930111f,
0.938686f, 0.947307f, 0.955974f, 0.964686f, 0.973445f, 0.982251f, 0.991102f, 1.000000f,
};
Vec3f ConvertFromR9G9B9E5(uint32_t data)
{
// get mantissas
uint32_t mantissas[] = {
((data >> 0) & 0x1ff),
((data >> 9) & 0x1ff),
((data >> 18) & 0x1ff),
};
// get shared exponent
uint32_t exp = ((data >> 27) & 0x1f);
// none of the mantissas have a leading implicit 1 like normal floats (otherwise the shared
// exponent would be a bit pointless and all floats would have to be within a power of two of each
// other).
// We could shift each mantissa up until the top bit is set, then overflow that into the implicit
// bit and adjust the exponent along with, then plug these into normal floats.
// OR we could just manually calculate the effective scale from the exponent and multiply by the
// mantissas.
float scale = powf(2.0f, float(exp) - 15.0f);
// floats have 23 bit mantissa, 8bit exponent
// R11G11B10 has 6/6/5 bit mantissas, 5bit exponents
const int mantissaShift = 23 - 9;
Vec3f ret;
uint32_t *retu = (uint32_t *)&ret.x;
float *retf = (float *)&ret.x;
for(int i = 0; i < 3; i++)
{
if(mantissas[i] == 0 && exp == 0)
{
retu[i] = 0;
}
else
{
if(exp == 0x1f)
{
// infinity or nan
retu[i] = 0x7f800000 | mantissas[i] << mantissaShift;
}
else
{
retf[i] = scale * (float(mantissas[i]) / 512.0f);
}
}
}
return ret;
}
uint32_t ConvertToR9G9B9E5(Vec3f data)
{
float rgb[3] = {data.x, data.y, data.z};
uint32_t encodedPixel = 0;
int exp = -10;
// we pick the highest exponent, losing bits off the bottom of any value that
// needs a lower one, rather than picking a lower one and having to saturate
// values that need a higher one
for(int channel = 0; channel < 3; channel++)
{
int e = 0;
frexpf(rgb[channel], &e);
exp = std::max(exp, e);
}
for(int channel = 0; channel < 3; channel++)
encodedPixel |= uint32_t(rgb[channel] * 511.0 / (1 << exp)) << (9 * channel);
encodedPixel |= (exp + 15) << 27;
return encodedPixel;
}
Vec3f ConvertFromR11G11B10(uint32_t data)
{
uint32_t mantissas[3] = {
(data >> 0) & 0x3f,
(data >> 11) & 0x3f,
(data >> 22) & 0x1f,
};
int32_t exponents[3] = {
int32_t(data >> 6) & 0x1f,
int32_t(data >> 17) & 0x1f,
int32_t(data >> 27) & 0x1f,
};
Vec3f ret;
uint32_t *retu = (uint32_t *)&ret.x;
// floats have 23 bit mantissa, 8bit exponent
// R11G11B10 has 6/6/5 bit mantissas, 5bit exponents
const int mantissaShift[] = {23 - 6, 23 - 6, 23 - 5};
for(int i = 0; i < 3; i++)
{
if(mantissas[i] == 0 && exponents[i] == 0)
{
retu[i] = 0;
}
else
{
if(exponents[i] == 0x1f)
{
// infinity or nan
retu[i] = 0x7f800000 | mantissas[i] << mantissaShift[i];
}
else if(exponents[i] != 0)
{
// shift exponent and mantissa to the right range for 32bit floats
retu[i] = (exponents[i] + (127 - 15)) << 23 | mantissas[i] << mantissaShift[i];
}
else if(exponents[i] == 0)
{
// we know xMantissa isn't 0 also, or it would have been caught above
exponents[i] = 1;
const uint32_t hiddenBit = 0x40 >> (i == 2 ? 1 : 0);
// shift until hidden bit is set
while((mantissas[i] & hiddenBit) == 0)
{
mantissas[i] <<= 1;
exponents[i]--;
}
// remove the hidden bit
mantissas[i] &= ~hiddenBit;
retu[i] = (exponents[i] + (127 - 15)) << 23 | mantissas[i] << mantissaShift[i];
}
}
}
return ret;
}
uint32_t ConvertToR11G11B10(Vec3f data)
{
// convert each component to half first
uint16_t halves[3] = {
ConvertToHalf(data.x),
ConvertToHalf(data.y),
ConvertToHalf(data.z),
};
// extract mantissas, exponents, signs
bool signs[3] = {
(halves[0] & 0x8000) != 0,
(halves[1] & 0x8000) != 0,
(halves[2] & 0x8000) != 0,
};
uint32_t mantissas[3] = {
(halves[0] & 0x03FFU),
(halves[1] & 0x03FFU),
(halves[2] & 0x03FFU),
};
uint32_t exponents[3] = {
(halves[0] & 0x7C00U) >> 10,
(halves[1] & 0x7C00U) >> 10,
(halves[2] & 0x7C00U) >> 10,
};
// normalise NaN/inf values so we can truncate mantissa without converting NaN to inf
for(int i = 0; i < 3; i++)
{
if(exponents[i] == 0x1f)
{
if(mantissas[i])
{
// if the mantissa is set, it's a NaN, set mantissa fully
mantissas[i] = 0x3FF;
}
else
{
// otherwise it's inf. If it's negative inf, clamp to 0
if(signs[i])
{
exponents[i] = 0;
mantissas[i] = 0;
}
}
}
else
{
// clamp negative finite values to 0
if(signs[i])
{
exponents[i] = 0;
mantissas[i] = 0;
}
}
}
// truncate and encode
return (mantissas[0] >> 4) << 0 | (mantissas[1] >> 4) << 11 | (mantissas[2] >> 5) << 22 |
uint32_t(exponents[0]) << 6 | uint32_t(exponents[1]) << 17 | uint32_t(exponents[2]) << 27;
}
float ConvertFromSRGB8(uint8_t comp)
{
return SRGB8_lookuptable[comp];
}
float ConvertSRGBToLinear(float srgbF)
{
if(srgbF <= 0.04045f)
return srgbF / 12.92f;
if(srgbF < 0.0f)
srgbF = 0.0f;
else if(srgbF > 1.0f)
srgbF = 1.0f;
return powf((0.055f + srgbF) / 1.055f, 2.4f);
}
Vec4f ConvertSRGBToLinear(Vec4f srgbF)
{
return Vec4f(ConvertSRGBToLinear(srgbF.x), ConvertSRGBToLinear(srgbF.y),
ConvertSRGBToLinear(srgbF.z), srgbF.w);
}
float ConvertLinearToSRGB(float linear)
{
if(linear <= 0.0031308f)
return 12.92f * linear;
if(linear < 0.0f)
linear = 0.0f;
else if(linear > 1.0f)
linear = 1.0f;
return 1.055f * powf(linear, 1.0f / 2.4f) - 0.055f;
}
// Similar to DecodePixelData() but it casts the results to float for all component types
FloatVector DecodeFormattedComponents(const ResourceFormat &fmt, const byte *data, bool *success)
{
PixelValue val;
DecodePixelData(fmt, data, val, success);
FloatVector ret;
if(fmt.compType == CompType::UInt)
{
ret.x = (float)val.uintValue[0];
ret.y = (float)val.uintValue[1];
ret.z = (float)val.uintValue[2];
ret.w = (float)val.uintValue[3];
}
else if(fmt.compType == CompType::SInt)
{
ret.x = (float)val.intValue[0];
ret.y = (float)val.intValue[1];
ret.z = (float)val.intValue[2];
ret.w = (float)val.intValue[3];
}
else
{
ret.x = val.floatValue[0];
ret.y = val.floatValue[1];
ret.z = val.floatValue[2];
ret.w = val.floatValue[3];
}
return ret;
}
void EncodeFormattedComponents(const ResourceFormat &fmt, FloatVector v, byte *data, bool *success)
{
uint64_t dummy[4] = {};
if(!data)
data = (byte *)&dummy;
// assume success, we'll set it to false if we hit an error
if(success)
*success = true;
if(fmt.type == ResourceFormatType::R10G10B10A2)
{
if(fmt.BGRAOrder())
std::swap(v.x, v.z);
uint32_t u;
if(fmt.compType == CompType::SNorm)
u = ConvertToR10G10B10A2SNorm(v);
else if(fmt.compType == CompType::UInt)
u = ConvertToR10G10B10A2(Vec4u(uint32_t(v.x), uint32_t(v.y), uint32_t(v.z), uint32_t(v.w)));
else
u = ConvertToR10G10B10A2(v);
memcpy(data, &u, sizeof(u));
}
else if(fmt.type == ResourceFormatType::R11G11B10)
{
uint32_t u = ConvertToR11G11B10(Vec3f(v.x, v.y, v.z));
memcpy(data, &u, sizeof(u));
}
else if(fmt.type == ResourceFormatType::R5G5B5A1)
{
if(!fmt.BGRAOrder())
std::swap(v.x, v.z);
uint16_t u = ConvertToB5G5R5A1(v);
memcpy(data, &u, sizeof(u));
}
else if(fmt.type == ResourceFormatType::R5G6B5)
{
if(!fmt.BGRAOrder())
std::swap(v.x, v.z);
uint16_t u = ConvertToB5G6R5(Vec3f(v.x, v.y, v.z));
memcpy(data, &u, sizeof(u));
}
else if(fmt.type == ResourceFormatType::R4G4B4A4)
{
if(!fmt.BGRAOrder())
std::swap(v.x, v.z);
uint16_t u = ConvertToB4G4R4A4(v);
memcpy(data, &u, sizeof(u));
}
else if(fmt.type == ResourceFormatType::R4G4)
{
uint8_t u = ConvertToR4G4(Vec2f(v.x, v.y));
memcpy(data, &u, sizeof(u));
}
else if(fmt.type == ResourceFormatType::R9G9B9E5)
{
uint32_t u = ConvertToR9G9B9E5(Vec3f(v.x, v.y, v.z));
memcpy(data, &u, sizeof(u));
}
else if(fmt.type == ResourceFormatType::Regular || fmt.type == ResourceFormatType::A8 ||
fmt.type == ResourceFormatType::S8)
{
const float *comp = &v.x;
CompType compType = fmt.compType;
for(size_t c = 0; c < fmt.compCount; c++)
{
// alpha is never interpreted as sRGB
if(compType == CompType::UNormSRGB && c == 3)
compType = CompType::UNorm;
if(fmt.compByteWidth == 8)
{
// we just upcast
double *d = (double *)data;
uint64_t *u64 = (uint64_t *)data;
int64_t *i64 = (int64_t *)data;
if(compType == CompType::Float)
{
*d = *comp;
}
else if(compType == CompType::UInt || compType == CompType::UScaled)
{
*u64 = uint64_t(*comp);
}
else if(compType == CompType::SInt || compType == CompType::SScaled)
{
*i64 = int64_t(*comp);
}
else
{
if(success)
*success = false;
}
}
else if(fmt.compByteWidth == 4)
{
float *f = (float *)data;
uint32_t *u32 = (uint32_t *)data;
int32_t *i32 = (int32_t *)data;
if(compType == CompType::Float || compType == CompType::Depth)
{
*f = *comp;
}
else if(compType == CompType::UInt || compType == CompType::UScaled)
{
*u32 = uint32_t(RDCCLAMP(*comp, 0.0f, float(UINT32_MAX)));
}
else if(compType == CompType::SInt || compType == CompType::SScaled)
{
*i32 = int32_t(RDCCLAMP(*comp, float(INT32_MIN), float(INT32_MAX)));
}
else
{
if(success)
*success = false;
}
}
else if(fmt.compByteWidth == 3 && compType == CompType::Depth)
{
// 24-bit depth is a weird edge case we need to assemble it by hand
uint8_t *u8 = (uint8_t *)data;
uint32_t depth = uint32_t(RDCCLAMP(*comp, 0.0f, 1.0f) * 16777215.0f);
u8[0] = uint8_t((depth & 0x0000ff) >> 0);
u8[1] = uint8_t((depth & 0x00ff00) >> 8);
u8[2] = uint8_t((depth & 0xff0000) >> 16);
}
else if(fmt.compByteWidth == 2)
{
uint16_t *u16 = (uint16_t *)data;
int16_t *i16 = (int16_t *)data;
if(compType == CompType::Float)
{
*u16 = ConvertToHalf(*comp);
}
else if(compType == CompType::UInt || compType == CompType::UScaled)
{
*u16 = (uint16_t)RDCCLAMP(*comp, 0.0f, float(UINT16_MAX));
}
else if(compType == CompType::SInt || compType == CompType::SScaled)
{
*i16 = (int16_t)RDCCLAMP(*comp, float(INT16_MIN), float(INT16_MAX));
}
// 16-bit depth is UNORM
else if(compType == CompType::UNorm || compType == CompType::Depth)
{
*u16 = uint16_t(RDCCLAMP(*comp, 0.0f, 1.0f) * float(0xffff) + 0.5f);
}
else if(compType == CompType::SNorm)
{
float f = RDCCLAMP(*comp, -1.0f, 1.0f) * 0x7fff;
if(f < 0.0f)
*i16 = int16_t(f - 0.5f);
else
*i16 = int16_t(f + 0.5f);
}
else
{
if(success)
*success = false;
}
}
else if(fmt.compByteWidth == 1)
{
uint8_t *u8 = (uint8_t *)data;
int8_t *i8 = (int8_t *)data;
if(compType == CompType::UInt || compType == CompType::UScaled)
{
*u8 = (uint8_t)RDCCLAMP(*comp, 0.0f, float(UINT8_MAX));
}
else if(compType == CompType::SInt || compType == CompType::SScaled)
{
*i8 = (int8_t)RDCCLAMP(*comp, float(INT8_MIN), float(INT8_MAX));
}
else if(compType == CompType::UNormSRGB)
{
*u8 = uint8_t(ConvertLinearToSRGB(*comp) * float(0xff) + 0.5f);
}
else if(compType == CompType::UNorm)
{
*u8 = uint8_t(RDCCLAMP(*comp, 0.0f, 1.0f) * float(0xff) + 0.5f);
}
else if(compType == CompType::SNorm)
{
float f = RDCCLAMP(*comp, -1.0f, 1.0f) * 0x7f;
if(f < 0.0f)
*i8 = int8_t(f - 0.5f);
else
*i8 = int8_t(f + 0.5f);
}
else
{
if(success)
*success = false;
}
}
else
{
RDCERR("Unexpected format to convert from %u %u", fmt.compByteWidth, compType);
}
comp++;
data += fmt.compByteWidth;
}
}
else
{
if(success)
*success = false;
}
}
void DecodePixelData(const ResourceFormat &fmt, const byte *data, PixelValue &out, bool *success)
{
out.floatValue = {0.0f, 0.0f, 0.0f, 1.0f};
if(fmt.compType == CompType::UInt || fmt.compType == CompType::SInt || fmt.compCount == 4)
out.floatValue[3] = 0.0f;
const uint64_t dummy[2] = {0, 0};
if(!data)
data = (const byte *)dummy;
// assume success, we'll set it to false if we hit an error
if(success)
*success = true;
if(fmt.type == ResourceFormatType::R10G10B10A2)
{
if(fmt.compType == CompType::SNorm)
{
Vec4f v = ConvertFromR10G10B10A2SNorm(*(const uint32_t *)data);
out.floatValue = {v.x, v.y, v.z, v.w};
}
else if(fmt.compType == CompType::UInt)
{
Vec4u v = ConvertFromR10G10B10A2UInt(*(const uint32_t *)data);
out.uintValue = {v.x, v.y, v.z, v.w};
}
else
{
Vec4f v = ConvertFromR10G10B10A2(*(const uint32_t *)data);
if(fmt.compType == CompType::SInt)
{
out.intValue = {(int32_t)v.x, (int32_t)v.y, (int32_t)v.z, (int32_t)v.w};
}
else
{
out.floatValue = {v.x, v.y, v.z, v.w};
}
}
// the different types are a union so we can ignore format and just treat it as a data swap
if(fmt.BGRAOrder())
std::swap(out.uintValue[0], out.uintValue[2]);
}
else if(fmt.type == ResourceFormatType::R11G11B10)
{
Vec3f v = ConvertFromR11G11B10(*(const uint32_t *)data);
out.floatValue = {v.x, v.y, v.z};
}
else if(fmt.type == ResourceFormatType::R5G5B5A1)
{
Vec4f v = ConvertFromB5G5R5A1(*(const uint16_t *)data);
out.floatValue = {v.x, v.y, v.z, v.w};
// conversely we *expect* BGRA order for this format and the above conversion implicitly flips
// when bit-unpacking. So if the format wasn't BGRA order, flip it back
if(!fmt.BGRAOrder())
std::swap(out.floatValue[0], out.floatValue[2]);
}
else if(fmt.type == ResourceFormatType::R5G6B5)
{
Vec3f v = ConvertFromB5G6R5(*(const uint16_t *)data);
out.floatValue = {v.x, v.y, v.z};
// conversely we *expect* BGRA order for this format and the above conversion implicitly flips
// when bit-unpacking. So if the format wasn't BGRA order, flip it back
if(!fmt.BGRAOrder())
std::swap(out.floatValue[0], out.floatValue[2]);
}
else if(fmt.type == ResourceFormatType::R4G4B4A4)
{
Vec4f v = ConvertFromB4G4R4A4(*(const uint16_t *)data);
out.floatValue = {v.x, v.y, v.z, v.w};
// conversely we *expect* BGRA order for this format and the above conversion implicitly flips
// when bit-unpacking. So if the format wasn't BGRA order, flip it back
if(!fmt.BGRAOrder())
std::swap(out.floatValue[0], out.floatValue[2]);
}
else if(fmt.type == ResourceFormatType::R4G4)
{
Vec4f v = ConvertFromR4G4(*(const uint8_t *)data);
out.floatValue = {v.x, v.y};
}
else if(fmt.type == ResourceFormatType::R9G9B9E5)
{
Vec3f v = ConvertFromR9G9B9E5(*(const uint32_t *)data);
out.floatValue = {v.x, v.y, v.z};
}
else if(fmt.type == ResourceFormatType::D16S8)
{
uint32_t val = *(const uint32_t *)data;
out.floatValue[0] = float(val & 0x00ffff) / 65535.0f;
out.floatValue[1] = float((val & 0xff0000) >> 16) / 255.0f;
out.floatValue[2] = 0.0f;
}
else if(fmt.type == ResourceFormatType::D24S8)
{
uint32_t val = *(const uint32_t *)data;
out.floatValue[0] = float(val & 0x00ffffff) / 16777215.0f;
out.floatValue[1] = float((val & 0xff000000) >> 24) / 255.0f;
out.floatValue[2] = 0.0f;
}
else if(fmt.type == ResourceFormatType::D32S8)
{
struct ds
{
float f;
uint32_t s;
} val;
val = *(const ds *)data;
out.floatValue[0] = val.f;
out.floatValue[1] = float(val.s) / 255.0f;
out.floatValue[2] = 0.0f;
}
else if(fmt.type == ResourceFormatType::Regular || fmt.type == ResourceFormatType::A8 ||
fmt.type == ResourceFormatType::S8)
{
CompType compType = fmt.compType;
for(size_t c = 0; c < fmt.compCount; c++)
{
// alpha is never interpreted as sRGB
if(compType == CompType::UNormSRGB && c == 3)
compType = CompType::UNorm;
if(fmt.compByteWidth == 8)
{
// we just downcast
const uint64_t *u64 = (const uint64_t *)data;
const int64_t *i64 = (const int64_t *)data;
if(compType == CompType::Float)
{
out.floatValue[c] = float(*(const double *)u64);
}
else if(compType == CompType::UInt)
{
out.uintValue[c] = uint32_t(*u64);
}
else if(compType == CompType::UScaled)
{
out.floatValue[c] = float(*u64);
}
else if(compType == CompType::SInt)
{
out.intValue[c] = int32_t(*i64);
}
else if(compType == CompType::SScaled)
{
out.floatValue[c] = float(*i64);
}
}
else if(fmt.compByteWidth == 4)
{
const uint32_t *u32 = (const uint32_t *)data;
const int32_t *i32 = (const int32_t *)data;
if(compType == CompType::Float || compType == CompType::Depth)
{
out.floatValue[c] = *(const float *)u32;
}
else if(compType == CompType::UInt)
{
out.uintValue[c] = uint32_t(*u32);
}
else if(compType == CompType::UScaled)
{
out.floatValue[c] = float(*u32);
}
else if(compType == CompType::SInt)
{
out.intValue[c] = int32_t(*i32);
}
else if(compType == CompType::SScaled)
{
out.floatValue[c] = float(*i32);
}
}
else if(fmt.compByteWidth == 3 && compType == CompType::Depth)
{
// 24-bit depth is a weird edge case we need to assemble it by hand
const uint8_t *u8 = (const uint8_t *)data;
uint32_t depth = 0;
depth |= uint32_t(u8[0]);
depth |= uint32_t(u8[1]) << 8;
depth |= uint32_t(u8[2]) << 16;
out.floatValue[c] = float(depth) / float(16777215.0f);
}
else if(fmt.compByteWidth == 2)
{
const uint16_t *u16 = (const uint16_t *)data;
const int16_t *i16 = (const int16_t *)data;
if(compType == CompType::Float)
{
out.floatValue[c] = ConvertFromHalf(*u16);
}
else if(compType == CompType::UInt)
{
out.uintValue[c] = uint32_t(*u16);
}
else if(compType == CompType::UScaled)
{
out.floatValue[c] = float(*u16);
}
else if(compType == CompType::SInt)
{
out.intValue[c] = int32_t(*i16);
}
else if(compType == CompType::SScaled)
{
out.floatValue[c] = float(*i16);
}
// 16-bit depth is UNORM
else if(compType == CompType::UNorm || compType == CompType::Depth)
{
out.floatValue[c] = float(*u16) / 65535.0f;
}
else if(compType == CompType::SNorm)
{
float f = -1.0f;
if(*i16 == -32768)
f = -1.0f;
else
f = ((float)*i16) / 32767.0f;
out.floatValue[c] = f;
}
}
else if(fmt.compByteWidth == 1)
{
const uint8_t *u8 = (const uint8_t *)data;
const int8_t *i8 = (const int8_t *)data;
if(compType == CompType::UInt)
{
out.uintValue[c] = uint32_t(*u8);
}
else if(compType == CompType::UScaled)
{
out.floatValue[c] = float(*u8);
}
else if(compType == CompType::SInt)
{
out.intValue[c] = int32_t(*i8);
}
else if(compType == CompType::SScaled)
{
out.floatValue[c] = float(*i8);
}
else if(compType == CompType::UNormSRGB)
{
out.floatValue[c] = ConvertFromSRGB8(*u8);
}
else if(compType == CompType::UNorm)
{
out.floatValue[c] = float(*u8) / 255.0f;
}
else if(compType == CompType::SNorm)
{
float f = -1.0f;
if(*i8 == -128)
f = -1.0f;
else
f = ((float)*i8) / 127.0f;
out.floatValue[c] = f;
}
}
else
{
RDCERR("Unexpected format to convert from %u %u", fmt.compByteWidth, compType);
}
data += fmt.compByteWidth;
}
if(fmt.type == ResourceFormatType::A8)
{
out.floatValue[3] = out.floatValue[0];
out.floatValue[0] = 0.0f;
}
else if(fmt.type == ResourceFormatType::S8)
{
out.uintValue[1] = out.uintValue[0];
out.uintValue[0] = 0;
}
// the different types are a union so we can ignore format and just treat it as a data swap
if(fmt.BGRAOrder())
std::swap(out.uintValue[0], out.uintValue[2]);
}
else
{
if(success)
*success = false;
}
}
#if ENABLED(ENABLE_UNIT_TESTS)
#undef None
#include "catch/catch.hpp"
#include "common/formatting.h"
template <>
rdcstr DoStringise(const FloatVector &el)
{
return StringFormat::Fmt("{%f, %f, %f, %f}", el.x, el.y, el.z, el.w);
}
TEST_CASE("Check ConvertComponents", "[format]")
{
ResourceFormat fmt;
fmt.type = ResourceFormatType::Regular;
union
{
uint8_t u8[4];
uint16_t u16[4];
uint32_t u32[4];
uint64_t u64[4];
int8_t i8[4];
int16_t i16[4];
int32_t i32[4];
int64_t i64[4];
float f[4];
double d[4];
} data;
SECTION("8-bit")
{
fmt.compByteWidth = 1;
data.u8[0] = 255;
data.u8[1] = 50;
data.u8[2] = 200;
data.u8[3] = 100;
SECTION("UNorm")
{
fmt.compType = CompType::UNorm;
fmt.compCount = 4;
CHECK(DecodeFormattedComponents(fmt, (byte *)&data) ==
FloatVector(1.0f, 50.0f / 255.0f, 200.0f / 255.0f, 100.0f / 255.0f));
fmt.compCount = 3;
CHECK(DecodeFormattedComponents(fmt, (byte *)&data) ==
FloatVector(1.0f, 50.0f / 255.0f, 200.0f / 255.0f, 1.0f));
fmt.compCount = 2;
CHECK(DecodeFormattedComponents(fmt, (byte *)&data) ==
FloatVector(1.0f, 50.0f / 255.0f, 0.0f, 1.0f));
fmt.compCount = 1;
CHECK(DecodeFormattedComponents(fmt, (byte *)&data) == FloatVector(1.0f, 0.0f, 0.0f, 1.0f));
fmt.compCount = 4;
fmt.SetBGRAOrder(true);
CHECK(DecodeFormattedComponents(fmt, (byte *)&data) ==
FloatVector(200.0f / 255.0f, 50.0f / 255.0f, 1.0f, 100.0f / 255.0f));
fmt.compCount = 3;
fmt.SetBGRAOrder(true);
CHECK(DecodeFormattedComponents(fmt, (byte *)&data) ==
FloatVector(200.0f / 255.0f, 50.0f / 255.0f, 1.0f, 1.0f));
};
SECTION("UNorm SRGB")
{
fmt.compType = CompType::UNormSRGB;
fmt.compCount = 4;
// alpha should still be 100.0f / 255.0f because alpha is not sRGB corrected
CHECK(DecodeFormattedComponents(fmt, (byte *)&data) ==
FloatVector(1.0f, 0.031896f, 0.577581f, 100.0f / 255.0f));
fmt.compCount = 3;
CHECK(DecodeFormattedComponents(fmt, (byte *)&data) ==
FloatVector(1.0f, 0.031896f, 0.577581f, 1.0f));
fmt.compCount = 2;
CHECK(DecodeFormattedComponents(fmt, (byte *)&data) == FloatVector(1.0f, 0.031896f, 0.0f, 1.0f));
fmt.compCount = 1;
CHECK(DecodeFormattedComponents(fmt, (byte *)&data) == FloatVector(1.0f, 0.0f, 0.0f, 1.0f));
fmt.compCount = 4;
fmt.SetBGRAOrder(true);
CHECK(DecodeFormattedComponents(fmt, (byte *)&data) ==
FloatVector(0.577581f, 0.031896f, 1.0f, 100.0f / 255.0f));
fmt.compCount = 3;
fmt.SetBGRAOrder(true);
CHECK(DecodeFormattedComponents(fmt, (byte *)&data) ==
FloatVector(0.577581f, 0.031896f, 1.0f, 1.0f));
};
SECTION("UInt")
{
fmt.compType = CompType::UInt;
fmt.compCount = 4;
CHECK(DecodeFormattedComponents(fmt, (byte *)&data) ==
FloatVector(255.0f, 50.0f, 200.0f, 100.0f));
fmt.compCount = 3;
CHECK(DecodeFormattedComponents(fmt, (byte *)&data) == FloatVector(255.0f, 50.0f, 200.0f, 0.0f));
fmt.compCount = 2;
CHECK(DecodeFormattedComponents(fmt, (byte *)&data) == FloatVector(255.0f, 50.0f, 0.0f, 0.0f));
fmt.compCount = 1;
CHECK(DecodeFormattedComponents(fmt, (byte *)&data) == FloatVector(255.0f, 0.0f, 0.0f, 0.0f));
fmt.compCount = 4;
fmt.SetBGRAOrder(true);
CHECK(DecodeFormattedComponents(fmt, (byte *)&data) ==
FloatVector(200.0f, 50.0f, 255.0f, 100.0f));
fmt.compCount = 3;
fmt.SetBGRAOrder(true);
CHECK(DecodeFormattedComponents(fmt, (byte *)&data) == FloatVector(200.0f, 50.0f, 255.0f, 0.0f));
};
data.i8[0] = 127;
data.i8[1] = 50;
data.i8[2] = -128;