forked from DaemonEngine/Daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtr_image.cpp
More file actions
3201 lines (2654 loc) · 88.3 KB
/
tr_image.cpp
File metadata and controls
3201 lines (2654 loc) · 88.3 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
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
Copyright (C) 2006-2011 Robert Beckebans <trebor_7@users.sourceforge.net>
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
===========================================================================
*/
// tr_image.c
#include <common/FileSystem.h>
#include "InternalImage.h"
#include "tr_local.h"
#include <iomanip>
#include "Material.h"
int gl_filter_min = GL_LINEAR_MIPMAP_NEAREST;
int gl_filter_max = GL_LINEAR;
image_t *r_imageHashTable[ IMAGE_FILE_HASH_SIZE ];
#define Tex_ByteToFloat(v) ( ( (int)(v) - 128 ) / 127.0f )
#define Tex_FloatToByte(v) ( 128 + (int) ( (v) * 127.0f + 0.5 ) )
struct textureMode_t
{
const char *name;
int minimize, maximize;
};
static const textureMode_t modes[] =
{
{ "GL_NEAREST", GL_NEAREST, GL_NEAREST },
{ "GL_LINEAR", GL_LINEAR, GL_LINEAR },
{ "GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST },
{ "GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR },
{ "GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST },
{ "GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR }
};
/*
================
return a hash value for the filename
================
*/
unsigned int GenerateImageHashValue( const char *fname )
{
int i;
unsigned hash;
char letter;
// better use Log::CommandInteractionMessage
// Log::Notice("tr_image::GenerateImageHashValue: '%s'", fname);
hash = 0;
i = 0;
while ( fname[ i ] != '\0' )
{
letter = Str::ctolower( fname[ i ] );
if ( letter == '\\' )
{
letter = '/'; // damn path names
}
hash += ( unsigned )( letter ) * ( i + 119 );
i++;
}
hash &= ( IMAGE_FILE_HASH_SIZE - 1 );
return hash;
}
/*
===============
GL_TextureMode
===============
*/
void GL_TextureMode( const char *string )
{
int i;
for ( i = 0; i < 6; i++ )
{
if ( !Q_stricmp( modes[ i ].name, string ) )
{
break;
}
}
if ( i == 6 )
{
Log::Warn("bad filter name" );
return;
}
gl_filter_min = modes[ i ].minimize;
gl_filter_max = modes[ i ].maximize;
if ( glConfig2.usingBindlessTextures && !tr.images.empty() )
{
Log::Notice( "Changing filter type of existing bindless textures requires a restart" );
return;
}
// change all the existing mipmap texture objects
for ( image_t *image : tr.images )
{
if ( image->filterType == filterType_t::FT_DEFAULT )
{
GL_Bind( image );
// set texture filter
glTexParameterf( image->type, GL_TEXTURE_MIN_FILTER, gl_filter_min );
glTexParameterf( image->type, GL_TEXTURE_MAG_FILTER, gl_filter_max );
// set texture anisotropy
if ( glConfig2.textureAnisotropyAvailable )
{
glTexParameterf( image->type, GL_TEXTURE_MAX_ANISOTROPY_EXT, glConfig2.textureAnisotropy );
}
}
}
}
class ListImagesCmd : public Cmd::StaticCmd
{
public:
ListImagesCmd() : StaticCmd("listImages", Cmd::RENDERER, "list images loaded in renderer") {}
void Run( const Cmd::Args &args ) const override
{
static const std::unordered_map<GLenum, std::string> imageTypeName = {
{ GL_TEXTURE_2D, "2D" },
{ GL_TEXTURE_3D, "3D" },
{ GL_TEXTURE_CUBE_MAP, "CUBE" },
};
typedef std::pair<const std::string, int> nameSizePair;
static const std::unordered_map<uint32_t, nameSizePair> imageFormatNameSize = {
/* Generic format that does not have any specified actual format
and implementations can choose one of a few different ones
GL4.6 spec (https://registry.khronos.org/OpenGL/specs/gl/glspec46.core.pdf):
8.5. TEXTURE IMAGE SPECIFICATION
"If internalformat is specified as a base internal format, the GL stores the resulting texture with
internal component resolutions of its own choosing, referred to as the effective internal format."
Use 4 bytes as an estimate: */
{ GL_RGBA, { "RGBA", 4 } },
{ GL_RGB8, { "RGB8", 3 } },
{ GL_RGBA8, { "RGBA8", 4 } },
{ GL_RGB16, { "RGB16", 6 } },
{ GL_RGBA16, { "RGBA16", 8 } },
{ GL_RGB16F, { "RGB16F", 6 } },
{ GL_RGB32F, { "RGB32F", 12 } },
{ GL_RGBA16F, { "RGBA16F", 8 } },
{ GL_RGBA32F, { "RGBA32F", 16 } },
{ GL_RGBA32UI, { "RGBA32UI", 16 } },
{ GL_ALPHA16F_ARB, { "A16F", 2 } },
{ GL_ALPHA32F_ARB, { "A32F", 4 } },
{ GL_R16F, { "R16F", 2 } },
{ GL_R32F, { "R32F", 4 } },
{ GL_LUMINANCE_ALPHA16F_ARB, { "LA16F", 4 } },
{ GL_LUMINANCE_ALPHA32F_ARB, { "LA32F", 8 } },
{ GL_RG16F, { "RG16F", 4 } },
{ GL_RG32F, { "RG32F", 8 } },
// Compressed formats here use imageDataSize multiplier as blocksize
/* Generic compressed format that does not have any specified actual format
and implementations can compress it in whatever way
GL4.6 spec (https://registry.khronos.org/OpenGL/specs/gl/glspec46.core.pdf):
8.5. TEXTURE IMAGE SPECIFICATION
"Generic compressed internal formats are never used directly as the internal formats of texture images.
If internalformat is one of the six generic compressed internal formats, its value is replaced by the symbolic constant
for a specific compressed internal format of the GL’s choosing with the same base internal format."
Use 4x4 blocks with 8 bytes per block here as an estimate: */
{ GL_COMPRESSED_RGBA, { "RGBAC", 8 } },
/* https://registry.khronos.org/OpenGL/extensions/EXT/EXT_texture_compression_s3tc.txt
S3TC Compressed Texture Image Formats
"Compressed texture images stored using the S3TC compressed image formats
are represented as a collection of 4x4 texel blocks, where each block
contains 64 or 128 bits of texel data. The image is encoded as a normal
2D raster image in which each 4x4 block is treated as a single pixel. If
an S3TC image has a width or height that is not a multiple of four, the
data corresponding to texels outside the image are irrelevant and
undefined.
When an S3TC image with a width of w, height of h, and block size of
blocksize (8 or 16 bytes) is decoded, the corresponding image size (in
bytes) is: ceil( w / 4 ) * ceil( h / 4 ) * blocksize."
All of the following have blocksize of 8 bytes: */
{ GL_COMPRESSED_RGB_S3TC_DXT1_EXT, { "DXT1", 8 } },
{ GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, { "DXT1a", 8 } },
{ GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, { "DXT3", 8 } },
{ GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, { "DXT5", 8 } },
/* GL4.6 spec: Appendix D Compressed Texture Image Formats
D.1 RGTC Compressed Texture Image Formats (https://registry.khronos.org/OpenGL/specs/gl/glspec46.core.pdf)
Khronos Data Format Specification v1.1 rev 9
14. RGTC Compressed Texture Image Formats (https://registry.khronos.org/DataFormat/specs/1.1/dataformat.1.1.html#RGTC)
"Compressed texture images stored using the RGTC compressed image encodings are represented as a collection of 4×4 texel blocks,
where each block contains 64 or 128 bits of texel data. The image is encoded as a normal 2D raster image in which each 4×4 block
is treated as a single pixel. If an RGTC image has a width or height that is not a multiple of four,
the data corresponding to texels outside the image are irrelevant and undefined.
When an RGTC image with a width of w, height of h, and block size of blocksize (8 or 16 bytes) is decoded,
the corresponding image size (in bytes) is: ceil( w / 4 ) * ceil( h / 4 ) * blocksize."
All of the following use blocksize of 8 bytes: */
{ GL_COMPRESSED_RED_RGTC1, { "RGTC1r", 8 } },
{ GL_COMPRESSED_SIGNED_RED_RGTC1, { "RGTC1Sr", 8 } },
{ GL_COMPRESSED_RG_RGTC2, { "RGTC2rg", 8 } },
{ GL_COMPRESSED_SIGNED_RG_RGTC2, { "RGTC2Srg", 8 } },
// These usually still use a 32-bit format internally
{ GL_DEPTH_COMPONENT16, { "D16", 2 } },
{ GL_DEPTH_COMPONENT24, { "D24", 3 } },
{ GL_DEPTH_COMPONENT32, { "D32", 4 } },
{ GL_DEPTH24_STENCIL8, { "D24S8", 4 } },
};
static const std::unordered_map<wrapTypeEnum_t, std::string> wrapTypeName = {
{ wrapTypeEnum_t::WT_REPEAT, "rept" },
{ wrapTypeEnum_t::WT_CLAMP, "clmp" },
{ wrapTypeEnum_t::WT_EDGE_CLAMP, "eclmp" },
{ wrapTypeEnum_t::WT_ZERO_CLAMP, "0clmp" },
{ wrapTypeEnum_t::WT_ONE_CLAMP, "1clmp" },
{ wrapTypeEnum_t::WT_ALPHA_ZERO_CLAMP, "a0clmp" },
};
const char *yesno[] = { "no", "yes" };
const char *filter = args.Argc() > 1 ? args.Argv( 1 ).c_str() : nullptr;
// Header names
std::string num = "num";
std::string width = "width";
std::string height = "height";
std::string layers = "layers";
std::string mm = "mm";
std::string type = "type";
std::string format = "format";
std::string twrap = "wrap.t";
std::string swrap = "wrap.s";
std::string name = "name";
// Number sizes
size_t numLen = 5;
size_t widthLen = 5;
size_t heightLen = 5;
size_t layersLen = 5;
size_t mmLen = 3;
size_t typeLen = 4;
size_t formatLen = 4;
size_t twrapLen = 4;
size_t swrapLen = 4;
// Header number sizes
numLen = std::max( numLen, num.length() );
widthLen = std::max( widthLen, width.length() );
heightLen = std::max( heightLen, height.length() );
layersLen = std::max( layersLen, layers.length() );
mmLen = std::max( mmLen, mm.length() );
typeLen = std::max( typeLen, type.length() );
formatLen = std::max( formatLen, format.length() );
twrapLen = std::max( twrapLen, twrap.length() );
swrapLen = std::max( swrapLen, swrap.length() );
// Value sizes
for ( const auto& kv : imageTypeName )
{
typeLen = std::max( typeLen, kv.second.length() );
}
for ( const auto& kv : imageFormatNameSize )
{
formatLen = std::max( formatLen, kv.second.first.length() );
}
for ( const auto& kv: wrapTypeName )
{
// 2 for the "t." and "s." prefix length.
twrapLen = std::max( twrapLen, 2 + kv.second.length() );
swrapLen = std::max( swrapLen, 2 + kv.second.length() );
}
std::string separator = " ";
std::stringstream lineStream;
// Print header
lineStream << std::left;
lineStream << std::setw(numLen) << num << separator;
lineStream << std::right;
lineStream << std::setw(widthLen) << width << separator;
lineStream << std::setw(heightLen) << height << separator;
lineStream << std::setw(layersLen) << layers << separator;
lineStream << std::left;
lineStream << std::setw(mmLen) << mm << separator;
lineStream << std::setw(typeLen) << type << separator;
lineStream << std::setw(formatLen) << format << separator;
lineStream << std::setw(twrapLen) << twrap << separator;
lineStream << std::setw(swrapLen) << swrap << separator;
lineStream << name;
std::string lineSeparator( lineStream.str().length(), '-' );
Print( lineSeparator );
Print( lineStream.str() );
Print( lineSeparator );
int texels = 0;
int dataSize = 0;
for ( size_t i = 0; i < tr.images.size(); i++ )
{
const image_t *image = tr.images[ i ];
if ( filter && !Com_Filter( filter, image->name, true ) )
{
continue;
}
mm = yesno[ image->filterType == filterType_t::FT_DEFAULT ];
if ( !imageTypeName.count( image->type ) )
{
Log::Debug( "Undocumented image type %i (%X) for image %s",
image->type, image->type, image->name );
type = Str::Format( "0x%4X", image->type );
}
else
{
type = imageTypeName.at( image->type );
}
int imageDataSize = 0;
texels += imageDataSize;
if ( !imageFormatNameSize.count( image->internalFormat ) )
{
Log::Debug( "Undocumented image format %i (%X) for image %s",
image->internalFormat, image->internalFormat, image->name );
format = Str::Format( "0x%04X", image->internalFormat);
imageDataSize *= 4;
}
else
{
switch ( image->internalFormat ) {
// Compressed formats encode blocks of 4x4 texels
case GL_COMPRESSED_RGBA:
case GL_COMPRESSED_RED_RGTC1:
case GL_COMPRESSED_SIGNED_RED_RGTC1:
case GL_COMPRESSED_RG_RGTC2:
case GL_COMPRESSED_SIGNED_RG_RGTC2:
case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
{
format = imageFormatNameSize.at( image->internalFormat ).first;
uint16_t imageWidth = image->uploadWidth;
uint16_t imageHeight = image->uploadHeight;
uint16_t imageLayers = image->numLayers;
int numMips = 1;
if ( image->filterType == filterType_t::FT_DEFAULT ) {
numMips = log2f( std::max( std::max( imageWidth, imageHeight ), imageLayers ) ) + 1;
}
for ( int j = 0; j < numMips; j++ ) {
imageDataSize += ceil( imageWidth / 4.0 ) * ceil( imageHeight / 4.0 ) * imageLayers;
imageWidth >>= imageWidth > 1 ? 1 : 0;
imageHeight >>= imageHeight > 1 ? 1 : 0;
imageLayers >>= imageLayers > 1 ? 1 : 0;
}
imageDataSize *= imageFormatNameSize.at( image->internalFormat ).second;
break;
}
default:
{
format = imageFormatNameSize.at( image->internalFormat ).first;
uint16_t imageWidth = image->uploadWidth;
uint16_t imageHeight = image->uploadHeight;
uint16_t imageLayers = image->numLayers;
int numMips = 1;
if ( image->filterType == filterType_t::FT_DEFAULT ) {
numMips = log2f( std::max( std::max( imageWidth, imageHeight ), imageLayers ) ) + 1;
}
for ( int j = 0; j < numMips; j++ ) {
imageDataSize += imageWidth * imageHeight * imageLayers;
imageWidth >>= imageWidth > 1 ? 1 : 0;
imageHeight >>= imageHeight > 1 ? 1 : 0;
imageLayers >>= imageLayers > 1 ? 1 : 0;
}
imageDataSize *= imageFormatNameSize.at( image->internalFormat ).second;
break;
}
}
}
if ( !wrapTypeName.count( image->wrapType.t ) )
{
Log::Debug( "Undocumented wrapType.t %i for image %s",
Util::ordinal(image->wrapType.t), image->name );
twrap = Str::Format( "t.%4i", Util::ordinal(image->wrapType.t) );
}
else
{
twrap = "t." + wrapTypeName.at( image->wrapType.t );
}
if ( !wrapTypeName.count( image->wrapType.s ) )
{
Log::Debug( "Undocumented wrapType.s %i for image %s",
Util::ordinal(image->wrapType.s), image->name );
swrap = Str::Format( "s.%4i", Util::ordinal(image->wrapType.s) );
}
else
{
swrap = "s." + wrapTypeName.at( image->wrapType.s );
}
name = image->name;
lineStream.clear();
lineStream.str("");
lineStream << std::left;
lineStream << std::setw(numLen) << i << separator;
lineStream << std::right;
lineStream << std::setw(widthLen) << image->uploadWidth << separator;
lineStream << std::setw(heightLen) << image->uploadHeight << separator;
lineStream << std::setw(layersLen) << image->numLayers << separator;
lineStream << std::left;
lineStream << std::setw(mmLen) << mm << separator;
lineStream << std::setw(typeLen) << type << separator;
lineStream << std::setw(formatLen) << format << separator;
lineStream << std::setw(twrapLen) << twrap << separator;
lineStream << std::setw(swrapLen) << swrap << separator;
lineStream << name;
Print( lineStream.str() );
dataSize += imageDataSize;
}
Print( lineSeparator );
Print( "%i total texels (not including mipmaps)", texels );
Print( "%d.%02d MB total image memory (estimated)",
dataSize / ( 1024 * 1024 ), ( dataSize % ( 1024 * 1024 ) ) * 100 / ( 1024 * 1024 ) );
Print( "%i total images", tr.images.size() );
Print( lineSeparator );
}
};
static ListImagesCmd listImagesCmdRegistration;
//=======================================================================
/*
================
ResampleTexture
Used to resample images in a more general than quartering fashion.
This will only be filtered properly if the resampled size
is greater than half the original size.
If a larger shrinking is needed, use the mipmap function
before or after.
================
*/
static void ResampleTexture( unsigned *in, int inwidth, int inheight, unsigned *out, int outwidth, int outheight,
bool normalMap )
{
int x, y;
unsigned *inrow, *inrow2;
unsigned frac, fracstep;
unsigned p1[ 2048 ], p2[ 2048 ];
byte *pix1, *pix2, *pix3, *pix4;
vec3_t n, n2, n3, n4;
fracstep = inwidth * 0x10000 / outwidth;
frac = fracstep >> 2;
for ( x = 0; x < outwidth; x++ )
{
p1[ x ] = 4 * ( frac >> 16 );
frac += fracstep;
}
frac = 3 * ( fracstep >> 2 );
for ( x = 0; x < outwidth; x++ )
{
p2[ x ] = 4 * ( frac >> 16 );
frac += fracstep;
}
if ( normalMap )
{
for ( y = 0; y < outheight; y++ )
{
inrow = in + inwidth * ( int )( ( y + 0.25 ) * inheight / outheight );
inrow2 = in + inwidth * ( int )( ( y + 0.75 ) * inheight / outheight );
for ( x = 0; x < outwidth; x++ )
{
pix1 = ( byte * ) inrow + p1[ x ];
pix2 = ( byte * ) inrow + p2[ x ];
pix3 = ( byte * ) inrow2 + p1[ x ];
pix4 = ( byte * ) inrow2 + p2[ x ];
n[ 0 ] = Tex_ByteToFloat( pix1[ 0 ] );
n[ 1 ] = Tex_ByteToFloat( pix1[ 1 ] );
n[ 2 ] = Tex_ByteToFloat( pix1[ 2 ] );
n2[ 0 ] = Tex_ByteToFloat( pix2[ 0 ] );
n2[ 1 ] = Tex_ByteToFloat( pix2[ 1 ] );
n2[ 2 ] = Tex_ByteToFloat( pix2[ 2 ] );
n3[ 0 ] = Tex_ByteToFloat( pix3[ 0 ] );
n3[ 1 ] = Tex_ByteToFloat( pix3[ 1 ] );
n3[ 2 ] = Tex_ByteToFloat( pix3[ 2 ] );
n4[ 0 ] = Tex_ByteToFloat( pix4[ 0 ] );
n4[ 1 ] = Tex_ByteToFloat( pix4[ 1 ] );
n4[ 2 ] = Tex_ByteToFloat( pix4[ 2 ] );
VectorAdd( n, n2, n );
VectorAdd( n, n3, n );
VectorAdd( n, n4, n );
if ( !VectorNormalize( n ) )
{
VectorSet( n, 0, 0, 1 );
}
( ( byte * )( out ) ) [ 0 ] = Tex_FloatToByte( n[ 0 ] );
( ( byte * )( out ) ) [ 1 ] = Tex_FloatToByte( n[ 1 ] );
( ( byte * )( out ) ) [ 2 ] = Tex_FloatToByte( n[ 2 ] );
( ( byte * )( out ) ) [ 3 ] = 255;
++out;
}
}
}
else
{
for ( y = 0; y < outheight; y++ )
{
inrow = in + inwidth * ( int )( ( y + 0.25 ) * inheight / outheight );
inrow2 = in + inwidth * ( int )( ( y + 0.75 ) * inheight / outheight );
for ( x = 0; x < outwidth; x++ )
{
pix1 = ( byte * ) inrow + p1[ x ];
pix2 = ( byte * ) inrow + p2[ x ];
pix3 = ( byte * ) inrow2 + p1[ x ];
pix4 = ( byte * ) inrow2 + p2[ x ];
( ( byte * )( out ) ) [ 0 ] = ( pix1[ 0 ] + pix2[ 0 ] + pix3[ 0 ] + pix4[ 0 ] ) >> 2;
( ( byte * )( out ) ) [ 1 ] = ( pix1[ 1 ] + pix2[ 1 ] + pix3[ 1 ] + pix4[ 1 ] ) >> 2;
( ( byte * )( out ) ) [ 2 ] = ( pix1[ 2 ] + pix2[ 2 ] + pix3[ 2 ] + pix4[ 2 ] ) >> 2;
( ( byte * )( out ) ) [ 3 ] = ( pix1[ 3 ] + pix2[ 3 ] + pix3[ 3 ] + pix4[ 3 ] ) >> 2;
++out;
}
}
}
}
/*
==================
R_UnpackDXT5A
==================
*/
static void
R_UnpackDXT5A( const byte *in, byte *out )
{
unsigned int bits0, bits1, val;
int i;
bits0 = in[2] + (in[3] << 8) + (in[4] << 16);
bits1 = in[5] + (in[6] << 8) + (in[7] << 16);
if( in[0] > in[1] ) {
for( i = 0; i < 8; i++ ) {
val = bits0 & 7; bits0 >>= 3;
switch( val ) {
case 0: val = in[0]; break;
case 1: val = in[1]; break;
case 2: val = (6 * in[0] + 1 * in[1]) / 7; break;
case 3: val = (5 * in[0] + 2 * in[1]) / 7; break;
case 4: val = (4 * in[0] + 3 * in[1]) / 7; break;
case 5: val = (3 * in[0] + 4 * in[1]) / 7; break;
case 6: val = (2 * in[0] + 5 * in[1]) / 7; break;
case 7: val = (1 * in[0] + 6 * in[1]) / 7; break;
}
out[ i ] = val;
val = bits1 & 7; bits1 >>= 3;
switch( val ) {
case 0: val = in[0]; break;
case 1: val = in[1]; break;
case 2: val = (6 * in[0] + 1 * in[1]) / 7; break;
case 3: val = (5 * in[0] + 2 * in[1]) / 7; break;
case 4: val = (4 * in[0] + 3 * in[1]) / 7; break;
case 5: val = (3 * in[0] + 4 * in[1]) / 7; break;
case 6: val = (2 * in[0] + 5 * in[1]) / 7; break;
case 7: val = (1 * in[0] + 6 * in[1]) / 7; break;
}
out[ i + 8 ] = val;
}
} else {
for( i = 0; i < 8; i++ ) {
val = bits0 & 7; bits0 >>= 3;
switch( val ) {
case 0: val = in[0]; break;
case 1: val = in[1]; break;
case 2: val = (4 * in[0] + 1 * in[1]) / 5; break;
case 3: val = (3 * in[0] + 2 * in[1]) / 5; break;
case 4: val = (2 * in[0] + 3 * in[1]) / 5; break;
case 5: val = (1 * in[0] + 4 * in[1]) / 5; break;
case 6: val = 0; break;
case 7: val = 0xff; break;
}
out[ i ] = val;
val = bits1 & 7; bits1 >>= 3;
switch( val ) {
case 0: val = in[0]; break;
case 1: val = in[1]; break;
case 2: val = (4 * in[0] + 1 * in[1]) / 5; break;
case 3: val = (3 * in[0] + 2 * in[1]) / 5; break;
case 4: val = (2 * in[0] + 3 * in[1]) / 5; break;
case 5: val = (1 * in[0] + 4 * in[1]) / 5; break;
case 6: val = 0; break;
case 7: val = 0xff; break;
}
out[ i + 8 ] = val;
}
}
}
static void
R_PackDXT1_Green( const byte *in, byte *out )
{
int i;
byte min, max;
byte lim1, lim2, lim3;
unsigned int bits0, bits1;
min = max = in[ 0 ];
for( i = 1; i < 16; i++ ) {
if( in[ i ] < min )
min = in[ i ];
if( in[ i ] > max )
max = in[ i ];
}
// truncate min and max to 6 bits
i = (max - min) >> 3;
min = (min + i) & 0xfc;
max = (max - i) & 0xfc;
if( min == max ) {
if( max == 0xfc ) {
min -= 4;
} else {
max += 4;
}
}
min |= min >> 6;
max |= max >> 6;
// find best match for every pixel
lim1 = (max + 5 * min) / 6;
lim2 = (max + min) / 2;
lim3 = (5 * max + min) / 6;
bits0 = bits1 = 0;
for( i = 7; i >= 0; i-- ) {
bits0 <<= 2;
bits1 <<= 2;
if( in[ i ] > lim2 ) {
if( in[ i ] > lim3 ) {
bits0 |= 0;
} else {
bits0 |= 2;
}
} else {
if( in[ i ] > lim1 ) {
bits0 |= 3;
} else {
bits0 |= 1;
}
}
if( in[ i + 8 ] > lim2 ) {
if( in[ i + 8 ] > lim3 ) {
bits1 |= 0;
} else {
bits1 |= 2;
}
} else {
if( in[ i + 8 ] > lim1 ) {
bits1 |= 3;
} else {
bits1 |= 1;
}
}
}
// write back DXT1 format data into out[]
out[0] = (max & 0x1c) << 3;
out[1] = 0xf8 | (max >> 5);
out[2] = (min & 0x1c) << 3;
out[3] = 0xf8 | (min >> 5);
out[4] = bits0 & 0xff;
out[5] = (bits0 >> 8) & 0xff;
out[6] = bits1 & 0xff;
out[7] = (bits1 >> 8) & 0xff;
}
/*
==================
R_ConvertBC5Image
If the OpenGL doesn't support BC5 (= OpenGL rgtc) textures, convert
them to BC3 (= dxt5). Sampling the original texture yields RGBA = XY01,
the converted texture yields RGBA = 1Y0X, so the shader can reconstruct
XY as vec2(tex.x * tex.a, tex.y).
==================
*/
static void
R_ConvertBC5Image(const byte **in, byte **out, int numMips, int numLayers,
int width, int height, bool is3D )
{
int blocks = 0;
int mipWidth, mipHeight, mipLayers, mipSize;
byte data[16], *to;
const byte *from;
int i, j, k;
// Allocate buffer for converted images
mipWidth = width;
mipHeight = height;
mipLayers = numLayers;
for ( i = 0; i < numMips; i++ ) {
mipSize = ((mipWidth + 3) >> 2) * ((mipHeight + 3) >> 2);
blocks += mipSize * mipLayers;
if( mipWidth > 1 )
mipWidth >>= 1;
if( mipHeight > 1 )
mipHeight >>= 1;
if( is3D && mipLayers > 1 )
mipLayers >>= 1;
}
*out = to = (byte *)ri.Hunk_AllocateTempMemory( blocks * 16 );
// Convert each mipmap
mipWidth = width;
mipHeight = height;
mipLayers = numLayers;
for ( i = 0; i < numMips; i++ ) {
mipSize = ((mipWidth + 3) >> 2) * ((mipHeight + 3) >> 2);
for( j = 0; j < mipLayers; j++ ) {
from = in[ i * numLayers + j ];
in [ i * numLayers + j ] = to;
for( k = 0; k < mipSize; k++ ) {
// red channel is unchanged
memcpy( to, from, 8 );
// green channel is converted to DXT1
R_UnpackDXT5A( from + 8, data );
R_PackDXT1_Green( data, to + 8 );
from += 16; to += 16;
}
}
if( mipWidth > 1 )
mipWidth >>= 1;
if( mipHeight > 1 )
mipHeight >>= 1;
if( is3D && mipLayers > 1 )
mipLayers >>= 1;
}
}
/*
===============
R_UploadImage
dataArray points to an array of numLayers * numMips pointers, first all layers
of mip level 0, then all layers of mip level 1, etc.
Note that for 3d textures the higher mip levels have fewer layers, e.g. mip
level 1 has only numLayers/2 layers. There are still numLayers pointers in
the dataArray for every mip level, the unneeded elements at the end aren't used.
===============
*/
void R_UploadImage( const char *name, const byte **dataArray, int numLayers, int numMips, image_t *image, const imageParams_t &imageParams )
{
const byte *data;
byte *scaledBuffer = nullptr;
int mipWidth, mipHeight, mipLayers, mipSize, blockSize = 0;
int i, c;
const byte *scan;
GLenum target;
GLenum format = GL_RGBA;
GLenum internalFormat = GL_RGB;
static const vec4_t oneClampBorder = { 1, 1, 1, 1 };
static const vec4_t zeroClampBorder = { 0, 0, 0, 1 };
static const vec4_t alphaZeroClampBorder = { 0, 0, 0, 0 };
if ( numMips <= 0 )
numMips = 1;
GL_Bind( image );
int scaledWidth = image->width;
int scaledHeight = image->height;
// If r_imageFitScreen is disabled, use nopicmip instead.
if ( ( image->bits & IF_FITSCREEN ) && !r_imageFitScreen.Get() )
{
image->bits &= ~IF_FITSCREEN;
image->bits |= IF_NOPICMIP;
}
int customScalingStep = R_GetImageCustomScalingStep( image, imageParams );
R_DownscaleImageDimensions( customScalingStep, &scaledWidth, &scaledHeight, &dataArray, numLayers, &numMips );
// clamp to the current upper OpenGL limit
// scale both axis down equally so we don't have to
// deal with a half mip resampling
if ( image->type == GL_TEXTURE_CUBE_MAP )
{
while ( scaledWidth > glConfig2.maxCubeMapTextureSize || scaledHeight > glConfig2.maxCubeMapTextureSize )
{
scaledWidth >>= 1;
scaledHeight >>= 1;
}
}
else
{
while ( scaledWidth > glConfig.maxTextureSize || scaledHeight > glConfig.maxTextureSize )
{
scaledWidth >>= 1;
scaledHeight >>= 1;
}
}
// set target
switch ( image->type )
{
case GL_TEXTURE_3D:
target = GL_TEXTURE_3D;
break;
case GL_TEXTURE_CUBE_MAP:
target = GL_TEXTURE_CUBE_MAP_POSITIVE_X;
break;
default:
target = GL_TEXTURE_2D;
break;
}
if ( image->bits & ( IF_DEPTH16 | IF_DEPTH24 | IF_DEPTH32 ) )
{
format = GL_DEPTH_COMPONENT;
if ( image->bits & IF_DEPTH16 )
{
internalFormat = GL_DEPTH_COMPONENT16;
}
else if ( image->bits & IF_DEPTH24 )
{
internalFormat = GL_DEPTH_COMPONENT24;
}
else if ( image->bits & IF_DEPTH32 )
{
internalFormat = GL_DEPTH_COMPONENT32;
}
}
else if ( image->bits & ( IF_PACKED_DEPTH24_STENCIL8 ) )
{
format = GL_DEPTH_STENCIL;
internalFormat = GL_DEPTH24_STENCIL8;
}
else if ( image->bits & IF_RGBA16 )
{
if ( !glConfig2.textureRGBA16BlendAvailable )
{
Log::Warn("RGBA16 image '%s' cannot be blended", image->name );
internalFormat = GL_RGBA8;
}
else
{
internalFormat = GL_RGBA16;
}
}
else if ( image->bits & ( IF_RGBA16F | IF_RGBA32F | IF_TWOCOMP16F | IF_TWOCOMP32F | IF_ONECOMP16F | IF_ONECOMP32F ) )
{
if( !glConfig2.textureFloatAvailable ) {
Log::Warn("floating point image '%s' cannot be loaded", image->name );
internalFormat = GL_RGBA8;
}
else if ( image->bits & IF_RGBA16F )
{
internalFormat = GL_RGBA16F;
}
else if ( image->bits & IF_RGBA32F )
{
internalFormat = GL_RGBA32F;
}
else if ( image->bits & IF_TWOCOMP16F )
{
internalFormat = glConfig2.textureRGAvailable ?
GL_RG16F : GL_LUMINANCE_ALPHA16F_ARB;
}
else if ( image->bits & IF_TWOCOMP32F )
{
internalFormat = glConfig2.textureRGAvailable ?
GL_RG32F : GL_LUMINANCE_ALPHA32F_ARB;
}
else if ( image->bits & IF_ONECOMP16F )
{
internalFormat = glConfig2.textureRGAvailable ?
GL_R16F : GL_ALPHA16F_ARB;
}
else if ( image->bits & IF_ONECOMP32F )
{
internalFormat = glConfig2.textureRGAvailable ?
GL_R32F : GL_ALPHA32F_ARB;
}
}
else if ( image->bits & ( IF_RGBA32UI ) )
{
if( !glConfig2.textureIntegerAvailable ) {
Log::Warn( "integer image '%s' cannot be loaded", image->name );
}
internalFormat = GL_RGBA32UI;
format = GL_RGBA_INTEGER;
}
else if ( IsImageCompressed( image->bits ) )
{
if( !GLEW_EXT_texture_compression_dxt1 &&
!GLEW_EXT_texture_compression_s3tc ) {
Log::Warn("compressed image '%s' cannot be loaded", image->name );
internalFormat = GL_RGBA8;
}
else if( image->bits & IF_BC1 ) {
format = GL_NONE;
internalFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
blockSize = 8;
}
else if ( image->bits & IF_BC2 ) {
format = GL_NONE;
internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
blockSize = 16;
}
else if ( image->bits & IF_BC3 ) {
format = GL_NONE;
internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
blockSize = 16;
}
else if ( image->bits & IF_BC4 ) {
if( !glConfig2.textureCompressionRGTCAvailable ) {