/
tr_image_ktx.cpp
852 lines (791 loc) · 25.4 KB
/
tr_image_ktx.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
/*
===========================================================================
Copyright (C) 2014
This file is part of Daemon source code.
Daemon source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
Daemon source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Daemon source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
#include "tr_local.h"
namespace {
// KTX is a format for storing textures for OpenGL® and OpenGL® ES applications.
// See https://www.khronos.org/opengles/sdk/tools/KTX/file_format_spec/
#pragma pack(push, 1)
struct KTX_header_t {
byte identifier[12];
uint32_t endianness;
uint32_t glType;
uint32_t glTypeSize;
uint32_t glFormat;
uint32_t glInternalFormat;
uint32_t glBaseInternalFormat;
uint32_t pixelWidth;
uint32_t pixelHeight;
uint32_t pixelDepth;
uint32_t numberOfArrayElements;
uint32_t numberOfFaces;
uint32_t numberOfMipmapLevels;
uint32_t bytesOfKeyValueData;
};
#pragma pack(pop)
const byte KTX_identifier[12]{ 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A };
const uint32_t KTX_endianness{ 0x04030201U };
const uint32_t KTX_endianness_reverse{ 0x01020304u };
bool IsValidKTXHeader( const KTX_header_t *hdr, size_t file_size ) {
return hdr && file_size >= sizeof(KTX_header_t) &&
memcmp(hdr->identifier, KTX_identifier, sizeof(KTX_identifier)) == 0;
}
bool TryApplyKTXHeaderEndianness( KTX_header_t *hdr, bool &needReverseBytes ) {
switch (hdr->endianness) {
case KTX_endianness:
needReverseBytes = false;
return true;
case KTX_endianness_reverse:
hdr->glType = Swap32(hdr->glType);
hdr->glTypeSize = Swap32(hdr->glTypeSize);
hdr->glFormat = Swap32(hdr->glFormat);
hdr->glInternalFormat = Swap32(hdr->glInternalFormat);
hdr->glBaseInternalFormat = Swap32(hdr->glBaseInternalFormat);
hdr->pixelWidth = Swap32(hdr->pixelWidth);
hdr->pixelHeight = Swap32(hdr->pixelHeight);
hdr->pixelDepth = Swap32(hdr->pixelDepth);
hdr->numberOfArrayElements = Swap32(hdr->numberOfArrayElements);
hdr->numberOfFaces = Swap32(hdr->numberOfFaces);
hdr->numberOfMipmapLevels = Swap32(hdr->numberOfMipmapLevels);
hdr->bytesOfKeyValueData = Swap32(hdr->bytesOfKeyValueData);
needReverseBytes = true;
return true;
default:
return false;
}
}
bool IsSupportedKTXFormat( const KTX_header_t *hdr ) {
return !( hdr->glTypeSize != 1 ||
hdr->numberOfArrayElements != 0 ||
(hdr->numberOfFaces != 1 && hdr->numberOfFaces != 6) ||
hdr->numberOfMipmapLevels > MAX_TEXTURE_MIPS ||
hdr->pixelWidth == 0 || hdr->pixelHeight == 0 ||
hdr->pixelDepth != 0 );
}
bool TryParseInternalFormatBits( uint32_t glInternalFormat, int &bits ) {
switch (glInternalFormat) {
case GL_RGBA8:
return true;
case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
bits |= IF_BC1;
return true;
case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
bits |= IF_BC2;
return true;
case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
bits |= IF_BC3;
return true;
case GL_COMPRESSED_RED_RGTC1:
bits |= IF_BC4;
return true;
case GL_COMPRESSED_RG_RGTC2:
bits |= IF_BC5;
return true;
default:
return false;
}
}
uint32_t GetImageSize( const byte *ktxPosition, bool needReverseBytes ) {
uint32_t imageSize = *(reinterpret_cast<const uint32_t *>(ktxPosition));
if( needReverseBytes )
imageSize = Swap32(imageSize);
return PAD(imageSize, 4u);
}
bool IsNonArrayCubemapTexture( const KTX_header_t *hdr ) {
return hdr->numberOfArrayElements == 0 && hdr->numberOfFaces == 6;
}
bool IsValidKTXFileStreamPosition( const byte *position, size_t ktxSize, const byte *ktxData ) {
return position >= ktxData && position < ktxData + ktxSize;
}
bool LoadInMemoryKTX( const char *name, void *ktxData, size_t ktxSize,
byte **data, int *width, int *height, int *numLayers,
int *numMips, int *bits ) {
auto *hdr{ static_cast<KTX_header_t *>(ktxData) };
if( !IsValidKTXHeader( hdr, ktxSize ) ) {
Log::Warn("KTX image '%s' has an invalid format", name);
return false;
}
bool needReverseBytes{false};
if( !TryApplyKTXHeaderEndianness( hdr, needReverseBytes ) ) {
Log::Warn("KTX image '%s' has unknown endianness value '%d'", name,
hdr->endianness);
return false;
}
if( !IsSupportedKTXFormat( hdr ) ) {
Log::Warn("KTX image '%s' may be valid, but not supported", name);
return false;
}
if( !TryParseInternalFormatBits( hdr->glInternalFormat, *bits ) ) {
Log::Warn("KTX image '%s' has unsupported glInternalFormat value '%d'",
name, hdr->glInternalFormat);
return false;
}
*width = hdr->pixelWidth;
*height = hdr->pixelHeight;
*numMips = hdr->numberOfMipmapLevels;
*numLayers = hdr->numberOfFaces == 6 ? 6 : 0;
byte *firstImageDataPtr{ (byte *)(hdr + 1) + hdr->bytesOfKeyValueData };
if ( !IsValidKTXFileStreamPosition( firstImageDataPtr, ktxSize, static_cast<const byte *>(ktxData) ) ) {
Log::Warn("KTX image '%s' has bad bytesOfKeyValueData or texture data", name);
return false;
}
byte *ptr{ firstImageDataPtr };
size_t totalImageSize{ 0 };
// For most textures imageSize is the number of bytes of pixel data in the
// current LOD level. The exception is non-array cubemap textures where
// imageSize is the number of bytes in each face of the texture for the
// current LOD level.
const uint32_t mipmapLevelCoefficient{
IsNonArrayCubemapTexture(hdr) ? hdr->numberOfFaces : 1U};
for(uint32_t i{ 0 }; i < hdr->numberOfMipmapLevels; i++) {
const uint32_t imageSize{ GetImageSize( ptr, needReverseBytes ) };
totalImageSize += imageSize * mipmapLevelCoefficient;
ptr += sizeof(uint32_t) + imageSize;
}
// ptr points to next byte after ktxData buffer end.
if ( !IsValidKTXFileStreamPosition( ptr - 1, ktxSize, static_cast<const byte *>(ktxData) ) ) {
Log::Warn("KTX image '%s' has bad header or texture data", name);
return false;
}
ptr = firstImageDataPtr;
data[ 0 ] = (byte *)ri.Z_Malloc(totalImageSize);
uint32_t imageSize{ GetImageSize( ptr, needReverseBytes ) };
ptr += sizeof(uint32_t);
Com_Memcpy( data[ 0 ], ptr, imageSize );
ptr += imageSize;
for(uint32_t j{ 1 }; j < hdr->numberOfFaces; j++) {
data[ j ] = data[ j - 1 ] + imageSize;
Com_Memcpy( data[ j ], ptr, imageSize );
ptr += imageSize;
}
for(uint32_t i{ 1 }; i < hdr->numberOfMipmapLevels; i++) {
imageSize = GetImageSize( ptr, needReverseBytes );
ptr += sizeof(uint32_t);
for (uint32_t j{ 0 }; j < hdr->numberOfFaces; j++) {
const uint32_t idx{ i * hdr->numberOfFaces + j };
data[ idx ] = data[ idx - 1 ] + imageSize;
Com_Memcpy( data[ idx ], ptr, imageSize );
ptr += imageSize;
}
}
return true;
}
bool ApplyKTXHeaderGlProperties( uint32_t glInternalFormat, KTX_header_t &hdr, uint32_t &components ) {
components = std::numeric_limits<uint32_t>::max();
hdr.glInternalFormat = glInternalFormat;
switch( glInternalFormat ) {
case GL_ALPHA4:
case GL_ALPHA8:
hdr.glType = GL_UNSIGNED_BYTE;
hdr.glTypeSize = 1;
hdr.glFormat = GL_ALPHA;
hdr.glBaseInternalFormat = GL_ALPHA;
components = 1;
break;
case GL_ALPHA12:
case GL_ALPHA16:
hdr.glType = GL_UNSIGNED_SHORT;
hdr.glTypeSize = 2;
hdr.glFormat = GL_ALPHA;
hdr.glBaseInternalFormat = GL_ALPHA;
components = 1;
break;
case GL_R8:
case GL_R8_SNORM:
hdr.glType = GL_UNSIGNED_BYTE;
hdr.glTypeSize = 1;
hdr.glFormat = GL_RED;
hdr.glBaseInternalFormat = GL_RED;
components = 1;
break;
case GL_R16:
case GL_R16_SNORM:
hdr.glType = GL_UNSIGNED_SHORT;
hdr.glTypeSize = 2;
hdr.glFormat = GL_RED;
hdr.glBaseInternalFormat = GL_RED;
components = 1;
break;
case GL_RG8:
case GL_RG8_SNORM:
hdr.glType = GL_UNSIGNED_BYTE;
hdr.glTypeSize = 1;
hdr.glFormat = GL_RG;
hdr.glBaseInternalFormat = GL_RG;
components = 2;
break;
case GL_RG16:
case GL_RG16_SNORM:
hdr.glType = GL_UNSIGNED_SHORT;
hdr.glTypeSize = 2;
hdr.glFormat = GL_RG;
hdr.glBaseInternalFormat = GL_RG;
components = 2;
break;
case GL_R3_G3_B2:
hdr.glType = GL_UNSIGNED_BYTE_3_3_2;
hdr.glTypeSize = 1;
hdr.glFormat = GL_RGB;
hdr.glBaseInternalFormat = GL_RGB;
components = 1;
break;
case GL_RGB4:
case GL_RGB5:
#ifdef GL_RGB565
case GL_RGB565:
#endif
hdr.glType = GL_UNSIGNED_SHORT_5_6_5;
hdr.glTypeSize = 2;
hdr.glFormat = GL_RGB;
hdr.glBaseInternalFormat = GL_RGB;
components = 1;
break;
case GL_RGB8:
case GL_RGB8_SNORM:
case GL_SRGB8:
hdr.glType = GL_UNSIGNED_BYTE;
hdr.glTypeSize = 1;
hdr.glFormat = GL_RGBA; //GL_RGB;
hdr.glBaseInternalFormat = GL_RGBA; //GL_RGB;
components = 4; //3;
break;
case GL_RGB10:
case GL_RGB12:
case GL_RGB16:
case GL_RGB16_SNORM:
hdr.glType = GL_UNSIGNED_SHORT;
hdr.glTypeSize = 2;
hdr.glFormat = GL_RGB;
hdr.glBaseInternalFormat = GL_RGB;
components = 3;
break;
case GL_RGBA2:
case GL_RGBA4:
hdr.glType = GL_UNSIGNED_SHORT_4_4_4_4;
hdr.glTypeSize = 2;
hdr.glFormat = GL_RGBA;
hdr.glBaseInternalFormat = GL_RGBA;
components = 4;
break;
case GL_RGB5_A1:
hdr.glType = GL_UNSIGNED_SHORT_5_5_5_1;
hdr.glTypeSize = 2;
hdr.glFormat = GL_RGBA;
hdr.glBaseInternalFormat = GL_RGBA;
components = 1;
break;
case GL_RGBA8:
case GL_RGBA8_SNORM:
case GL_SRGB8_ALPHA8:
hdr.glType = GL_UNSIGNED_BYTE;
hdr.glTypeSize = 1;
hdr.glFormat = GL_RGBA;
hdr.glBaseInternalFormat = GL_RGBA;
components = 4;
break;
case GL_RGB10_A2:
hdr.glType = GL_UNSIGNED_INT_10_10_10_2;
hdr.glTypeSize = 4;
hdr.glFormat = GL_RGBA;
hdr.glBaseInternalFormat = GL_RGBA;
components = 1;
break;
case GL_RGB10_A2UI:
hdr.glType = GL_UNSIGNED_INT_10_10_10_2;
hdr.glTypeSize = 4;
hdr.glFormat = GL_RGBA_INTEGER;
hdr.glBaseInternalFormat = GL_RGBA;
components = 1;
break;
case GL_RGBA12:
case GL_RGBA16:
case GL_RGBA16_SNORM:
hdr.glType = GL_UNSIGNED_SHORT;
hdr.glTypeSize = 2;
hdr.glFormat = GL_RGBA;
hdr.glBaseInternalFormat = GL_RGBA;
components = 4;
break;
case GL_R16F:
case GL_R32F:
hdr.glType = GL_FLOAT;
hdr.glTypeSize = 4;
hdr.glFormat = GL_RED;
hdr.glBaseInternalFormat = GL_RED;
components = 1;
break;
case GL_RG16F:
case GL_RG32F:
hdr.glType = GL_FLOAT;
hdr.glTypeSize = 4;
hdr.glFormat = GL_RG;
hdr.glBaseInternalFormat = GL_RG;
components = 2;
break;
case GL_RGB16F:
case GL_RGB32F:
case GL_R11F_G11F_B10F:
case GL_RGB9_E5:
hdr.glType = GL_FLOAT;
hdr.glTypeSize = 4;
hdr.glFormat = GL_RGB;
hdr.glBaseInternalFormat = GL_RGB;
components = 3;
break;
case GL_RGBA16F:
case GL_RGBA32F:
hdr.glType = GL_FLOAT;
hdr.glTypeSize = 4;
hdr.glFormat = GL_RGBA;
hdr.glBaseInternalFormat = GL_RGBA;
components = 4;
break;
case GL_R8I:
hdr.glType = GL_BYTE;
hdr.glTypeSize = 1;
hdr.glFormat = GL_RED_INTEGER;
hdr.glBaseInternalFormat = GL_RED;
components = 1;
break;
case GL_R8UI:
hdr.glType = GL_UNSIGNED_BYTE;
hdr.glTypeSize = 1;
hdr.glFormat = GL_RED_INTEGER;
hdr.glBaseInternalFormat = GL_RED;
components = 1;
break;
case GL_R16I:
hdr.glType = GL_SHORT;
hdr.glTypeSize = 2;
hdr.glFormat = GL_RED_INTEGER;
hdr.glBaseInternalFormat = GL_RED;
components = 1;
break;
case GL_R16UI:
hdr.glType = GL_UNSIGNED_SHORT;
hdr.glTypeSize = 2;
hdr.glFormat = GL_RED_INTEGER;
hdr.glBaseInternalFormat = GL_RED;
components = 1;
break;
case GL_R32I:
hdr.glType = GL_INT;
hdr.glTypeSize = 4;
hdr.glFormat = GL_RED_INTEGER;
hdr.glBaseInternalFormat = GL_RED;
components = 1;
break;
case GL_R32UI:
hdr.glType = GL_UNSIGNED_INT;
hdr.glTypeSize = 4;
hdr.glFormat = GL_RED_INTEGER;
hdr.glBaseInternalFormat = GL_RED;
components = 1;
break;
case GL_RG8I:
hdr.glType = GL_BYTE;
hdr.glTypeSize = 1;
hdr.glFormat = GL_RG_INTEGER;
hdr.glBaseInternalFormat = GL_RG;
components = 2;
break;
case GL_RG8UI:
hdr.glType = GL_UNSIGNED_BYTE;
hdr.glTypeSize = 1;
hdr.glFormat = GL_RG_INTEGER;
hdr.glBaseInternalFormat = GL_RG;
components = 2;
break;
case GL_RG16I:
hdr.glType = GL_SHORT;
hdr.glTypeSize = 2;
hdr.glFormat = GL_RG_INTEGER;
hdr.glBaseInternalFormat = GL_RG;
components = 2;
break;
case GL_RG16UI:
hdr.glType = GL_UNSIGNED_SHORT;
hdr.glTypeSize = 2;
hdr.glFormat = GL_RG_INTEGER;
hdr.glBaseInternalFormat = GL_RG;
components = 2;
break;
case GL_RG32I:
hdr.glType = GL_INT;
hdr.glTypeSize = 4;
hdr.glFormat = GL_RG_INTEGER;
hdr.glBaseInternalFormat = GL_RG;
components = 2;
break;
case GL_RG32UI:
hdr.glType = GL_UNSIGNED_INT;
hdr.glTypeSize = 4;
hdr.glFormat = GL_RG_INTEGER;
hdr.glBaseInternalFormat = GL_RG;
components = 2;
break;
case GL_RGB8I:
hdr.glType = GL_BYTE;
hdr.glTypeSize = 1;
hdr.glFormat = GL_RGBA_INTEGER; //GL_RGB_INTEGER;
hdr.glBaseInternalFormat = GL_RGBA; //GL_RGB;
components = 4; //3;
break;
case GL_RGB8UI:
hdr.glType = GL_UNSIGNED_BYTE;
hdr.glTypeSize = 1;
hdr.glFormat = GL_RGBA_INTEGER; //GL_RGB_INTEGER;
hdr.glBaseInternalFormat = GL_RGBA; //GL_RGB;
components = 4; //3;
break;
case GL_RGB16I:
hdr.glType = GL_SHORT;
hdr.glTypeSize = 2;
hdr.glFormat = GL_RGB_INTEGER;
hdr.glBaseInternalFormat = GL_RGB;
components = 3;
break;
case GL_RGB16UI:
hdr.glType = GL_UNSIGNED_SHORT;
hdr.glTypeSize = 2;
hdr.glFormat = GL_RGB_INTEGER;
hdr.glBaseInternalFormat = GL_RGB;
components = 3;
break;
case GL_RGB32I:
hdr.glType = GL_INT;
hdr.glTypeSize = 4;
hdr.glFormat = GL_RGB_INTEGER;
hdr.glBaseInternalFormat = GL_RGB;
components = 3;
break;
case GL_RGB32UI:
hdr.glType = GL_UNSIGNED_INT;
hdr.glTypeSize = 4;
hdr.glFormat = GL_RGB_INTEGER;
hdr.glBaseInternalFormat = GL_RGB;
components = 3;
break;
case GL_RGBA8I:
hdr.glType = GL_BYTE;
hdr.glTypeSize = 1;
hdr.glFormat = GL_RGBA_INTEGER;
hdr.glBaseInternalFormat = GL_RGBA;
components = 4;
break;
case GL_RGBA8UI:
hdr.glType = GL_UNSIGNED_BYTE;
hdr.glTypeSize = 1;
hdr.glFormat = GL_RGBA_INTEGER;
hdr.glBaseInternalFormat = GL_RGBA;
components = 4;
break;
case GL_RGBA16I:
hdr.glType = GL_SHORT;
hdr.glTypeSize = 2;
hdr.glFormat = GL_RGBA_INTEGER;
hdr.glBaseInternalFormat = GL_RGBA;
components = 4;
break;
case GL_RGBA16UI:
hdr.glType = GL_UNSIGNED_SHORT;
hdr.glTypeSize = 2;
hdr.glFormat = GL_RGBA_INTEGER;
hdr.glBaseInternalFormat = GL_RGBA;
components = 4;
break;
case GL_RGBA32I:
hdr.glType = GL_INT;
hdr.glTypeSize = 4;
hdr.glFormat = GL_RGBA_INTEGER;
hdr.glBaseInternalFormat = GL_RGBA;
components = 4;
break;
case GL_RGBA32UI:
hdr.glType = GL_UNSIGNED_INT;
hdr.glTypeSize = 4;
hdr.glFormat = GL_RGBA_INTEGER;
hdr.glBaseInternalFormat = GL_RGBA;
components = 4;
break;
case GL_LUMINANCE4:
case GL_LUMINANCE8:
case GL_SLUMINANCE:
hdr.glType = GL_UNSIGNED_BYTE;
hdr.glTypeSize = 1;
hdr.glFormat = GL_LUMINANCE;
hdr.glBaseInternalFormat = GL_LUMINANCE;
components = 1;
break;
case GL_LUMINANCE12:
case GL_LUMINANCE16:
hdr.glType = GL_UNSIGNED_SHORT;
hdr.glTypeSize = 2;
hdr.glFormat = GL_LUMINANCE;
hdr.glBaseInternalFormat = GL_LUMINANCE;
components = 1;
break;
case GL_LUMINANCE4_ALPHA4:
case GL_LUMINANCE6_ALPHA2:
case GL_LUMINANCE8_ALPHA8:
case GL_SLUMINANCE8_ALPHA8:
hdr.glType = GL_UNSIGNED_BYTE;
hdr.glTypeSize = 1;
hdr.glFormat = GL_LUMINANCE_ALPHA;
hdr.glBaseInternalFormat = GL_LUMINANCE_ALPHA;
components = 2;
break;
case GL_LUMINANCE12_ALPHA4:
case GL_LUMINANCE12_ALPHA12:
case GL_LUMINANCE16_ALPHA16:
hdr.glType = GL_UNSIGNED_SHORT;
hdr.glTypeSize = 2;
hdr.glFormat = GL_LUMINANCE_ALPHA;
hdr.glBaseInternalFormat = GL_LUMINANCE_ALPHA;
components = 2;
break;
case GL_INTENSITY4:
case GL_INTENSITY8:
hdr.glType = GL_UNSIGNED_BYTE;
hdr.glTypeSize = 1;
hdr.glFormat = GL_INTENSITY;
hdr.glBaseInternalFormat = GL_INTENSITY;
components = 1;
break;
case GL_INTENSITY12:
case GL_INTENSITY16:
hdr.glType = GL_UNSIGNED_SHORT;
hdr.glTypeSize = 2;
hdr.glFormat = GL_INTENSITY;
hdr.glBaseInternalFormat = GL_INTENSITY;
components = 1;
break;
case GL_DEPTH_COMPONENT16:
hdr.glType = GL_UNSIGNED_SHORT;
hdr.glTypeSize = 2;
hdr.glFormat = GL_DEPTH_COMPONENT;
hdr.glBaseInternalFormat = GL_DEPTH_COMPONENT;
components = 1;
break;
case GL_DEPTH_COMPONENT24:
case GL_DEPTH_COMPONENT32:
hdr.glType = GL_UNSIGNED_INT;
hdr.glTypeSize = 4;
hdr.glFormat = GL_DEPTH_COMPONENT;
hdr.glBaseInternalFormat = GL_DEPTH_COMPONENT;
components = 1;
break;
case GL_DEPTH_COMPONENT32F:
hdr.glType = GL_FLOAT;
hdr.glTypeSize = 4;
hdr.glFormat = GL_DEPTH_COMPONENT;
hdr.glBaseInternalFormat = GL_DEPTH_COMPONENT;
components = 1;
break;
case GL_DEPTH24_STENCIL8:
hdr.glType = GL_UNSIGNED_INT_24_8;
hdr.glTypeSize = 4;
hdr.glFormat = GL_DEPTH_STENCIL;
hdr.glBaseInternalFormat = GL_DEPTH_STENCIL;
components = 1;
break;
case GL_DEPTH32F_STENCIL8:
hdr.glType = GL_FLOAT_32_UNSIGNED_INT_24_8_REV;
hdr.glTypeSize = 4;
hdr.glFormat = GL_DEPTH_STENCIL;
hdr.glBaseInternalFormat = GL_DEPTH_STENCIL;
components = 1;
break;
case GL_STENCIL_INDEX8:
hdr.glType = GL_UNSIGNED_BYTE;
hdr.glTypeSize = 1;
hdr.glFormat = GL_STENCIL_INDEX;
hdr.glBaseInternalFormat = GL_STENCIL_INDEX;
components = 1;
break;
case GL_COMPRESSED_RED_RGTC1:
case GL_COMPRESSED_SIGNED_RED_RGTC1:
hdr.glType = 0;
hdr.glTypeSize = 1;
hdr.glFormat = 0;
hdr.glBaseInternalFormat = GL_RED;
components = 0;
break;
case GL_COMPRESSED_RG_RGTC2:
case GL_COMPRESSED_SIGNED_RG_RGTC2:
hdr.glType = 0;
hdr.glTypeSize = 1;
hdr.glFormat = 0;
hdr.glBaseInternalFormat = GL_RG;
components = 0;
break;
case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
hdr.glType = 0;
hdr.glTypeSize = 1;
hdr.glFormat = 0;
hdr.glBaseInternalFormat = GL_RGB;
components = 0;
break;
case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
hdr.glType = 0;
hdr.glTypeSize = 1;
hdr.glFormat = 0;
hdr.glBaseInternalFormat = GL_RGBA;
components = 0;
break;
default:
break;
}
return components != std::numeric_limits<uint32_t>::max();
}
} // namespace
void LoadKTX( const char *name, byte **pic, int *width, int *height,
int *numLayers, int *numMips, int *bits, byte )
{
*pic = nullptr;
*numLayers = 0;
void *ktxData{ nullptr };
const size_t ktxSize = ri.FS_ReadFile( name, &ktxData );
if (!ktxData) {
return;
}
if ( !LoadInMemoryKTX( name, ktxData, ktxSize, pic, width, height, numLayers, numMips, bits ) ) {
if (*pic) {
ri.Free(*pic);
}
*pic = nullptr; // This signals failure.
}
ri.FS_FreeFile( ktxData );
}
void SaveImageKTX( const char *path, image_t *img )
{
KTX_header_t hdr;
Com_Memset( &hdr, 0, sizeof(hdr) );
Com_Memcpy( &hdr.identifier, KTX_identifier, sizeof( KTX_identifier ) );
hdr.endianness = KTX_endianness;
GLenum target;
GL_Bind( img );
if( img->type == GL_TEXTURE_CUBE_MAP ) {
hdr.numberOfFaces = 6;
target = GL_TEXTURE_CUBE_MAP_POSITIVE_X;
} else {
hdr.numberOfFaces = 1;
target = img->type;
}
uint32_t glInternalFormat;
glGetTexLevelParameteriv( target, 0, GL_TEXTURE_INTERNAL_FORMAT,
(GLint *)&glInternalFormat );
uint32_t components;
if( !ApplyKTXHeaderGlProperties( glInternalFormat, hdr, components ) ) {
Log::Warn( "KTX image '%s' format '%x' is not supported", path, glInternalFormat );
return;
}
glGetTexLevelParameteriv( target, 0, GL_TEXTURE_WIDTH,
(GLint *)&hdr.pixelWidth );
hdr.numberOfArrayElements = 0;
if( img->type == GL_TEXTURE_CUBE_MAP ) {
hdr.pixelHeight = hdr.pixelWidth;
hdr.pixelDepth = 0;
} else if( img->type == GL_TEXTURE_1D ) {
hdr.pixelHeight = 0;
hdr.pixelDepth = 0;
} else {
glGetTexLevelParameteriv( target, 0, GL_TEXTURE_HEIGHT,
(GLint *)&hdr.pixelHeight );
if( img->type == GL_TEXTURE_2D ) {
hdr.pixelDepth = 0;
} else {
glGetTexLevelParameteriv( target, 0, GL_TEXTURE_DEPTH,
(GLint *)&hdr.pixelDepth );
}
}
hdr.numberOfMipmapLevels = 1;
uint32_t mipWidth, mipHeight, mipDepth;
int mipFilter;
glGetTexParameteriv( target, GL_TEXTURE_MIN_FILTER,
(GLint *)&mipFilter );
if( mipFilter == GL_NEAREST_MIPMAP_NEAREST || mipFilter == GL_NEAREST_MIPMAP_LINEAR ||
mipFilter == GL_LINEAR_MIPMAP_NEAREST || mipFilter == GL_LINEAR_MIPMAP_LINEAR ) {
mipWidth = std::max(hdr.pixelWidth, 1u);
mipHeight = std::max(hdr.pixelHeight, 1u);
mipDepth = std::max(hdr.pixelDepth, 1u);
while( mipWidth > 1 || mipHeight > 1 || mipDepth > 1 ) {
hdr.numberOfMipmapLevels++;
if( mipWidth > 1 ) mipWidth >>= 1u;
if( mipHeight > 1 ) mipHeight >>= 1u;
if( mipDepth > 1 ) mipDepth >>= 1u;
}
}
hdr.bytesOfKeyValueData = 0;
uint32_t size = 0;
int mipSize;
mipWidth = std::max(hdr.pixelWidth, 1u);
mipHeight = std::max(hdr.pixelHeight, 1u);
mipDepth = std::max(hdr.pixelDepth, 1u);
for(uint32_t i = size = 0; i < hdr.numberOfMipmapLevels; i++ ) {
size += sizeof(uint32_t);
if( !hdr.glFormat ) {
glGetTexLevelParameteriv( target, i,
GL_TEXTURE_COMPRESSED_IMAGE_SIZE,
&mipSize );
} else {
mipSize = mipWidth * hdr.glTypeSize * components;
mipSize = PAD( mipSize, 4u );
mipSize *= mipHeight * mipDepth;
}
size += hdr.numberOfFaces * PAD( mipSize, 4u );
if( mipWidth > 1 ) mipWidth >>= 1u;
if( mipHeight > 1 ) mipHeight >>= 1u;
if( mipDepth > 1 ) mipDepth >>= 1u;
}
byte *data = (byte *)ri.Hunk_AllocateTempMemory( size + sizeof( hdr ) );
byte *ptr = data;
Com_Memcpy( ptr, &hdr, sizeof( hdr ) );
ptr += sizeof( hdr );
mipWidth = std::max(hdr.pixelWidth, 1u);
mipHeight = std::max(hdr.pixelHeight, 1u);
mipDepth = std::max(hdr.pixelDepth, 1u);
for(uint32_t i = 0; i < hdr.numberOfMipmapLevels; i++ ) {
if( !hdr.glFormat ) {
glGetTexLevelParameteriv( target, i,
GL_TEXTURE_COMPRESSED_IMAGE_SIZE,
&mipSize );
} else {
mipSize = mipWidth * hdr.glTypeSize * components;
mipSize = PAD( mipSize, 4u );
mipSize *= mipHeight * mipDepth;
}
*(int32_t *)ptr = PAD( mipSize, 4u );
ptr += sizeof( int32_t );
for(uint32_t j = 0; j < hdr.numberOfFaces; j++ ) {
if( !hdr.glFormat ) {
glGetCompressedTexImage( target + j, i, ptr );
} else {
glGetTexImage( target + j, i, hdr.glFormat,
hdr.glType, ptr );
}
ptr += PAD( mipSize, 4u );
}
if( mipWidth > 1 ) mipWidth >>= 1u;
if( mipHeight > 1 ) mipHeight >>= 1u;
if( mipDepth > 1 ) mipDepth >>= 1u;
}
ri.FS_WriteFile( path, data, size + sizeof( hdr ) );
ri.Hunk_FreeTempMemory( data );
}