-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathgropengltexture.cpp
More file actions
1986 lines (1563 loc) · 54.9 KB
/
gropengltexture.cpp
File metadata and controls
1986 lines (1563 loc) · 54.9 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) Volition, Inc. 1999. All rights reserved.
*
* All source code herein is the property of Volition, Inc. You may not sell
* or otherwise commercially exploit the source or things you created based on the
* source.
*
*/
#ifdef _WIN32
#include <windows.h>
#endif
#define BMPMAN_INTERNAL
#include "gropenglstate.h"
#include "gropengltexture.h"
#include "gropengldraw.h"
#include "bmpman/bm_internal.h"
#include "bmpman/bmpman.h"
#include "cmdline/cmdline.h"
#include "ddsutils/ddsutils.h"
#include "globalincs/systemvars.h"
#include "graphics/grinternal.h"
#include "math/vecmat.h"
#include "options/Option.h"
#include "osapi/osregistry.h"
matrix4 GL_texture_matrix;
int GL_texture_ram = 0;
int GL_min_texture_width = 0;
GLint GL_max_texture_width = 0;
int GL_min_texture_height = 0;
GLint GL_max_texture_height = 0;
int GL_textures_in_frame = 0;
int GL_last_detail = -1;
GLint GL_supported_texture_units = 2;
int GL_should_preload = 0;
GLfloat GL_anisotropy = 1.0f;
int GL_mipmap_filter = 0;
GLenum GL_texture_target = GL_TEXTURE_2D;
GLenum GL_texture_face = GL_TEXTURE_2D;
GLenum GL_texture_addressing = GL_REPEAT;
bool GL_rendering_to_texture = false;
GLint GL_max_renderbuffer_size = 0;
extern int GLOWMAP;
extern int SPECMAP;
extern int ENVMAP;
const SCP_vector<std::pair<int, std::pair<const char*, int>>> TextureFilteringValues = {{0, {"Bilinear", 1677}},
{ 1, {"Trilinear", 1678}}};
static void parse_texture_filtering_func()
{
SCP_string mode;
stuff_string(mode, F_NAME);
// Convert to lowercase once
SCP_tolower(mode);
// Use a map to associate strings with their respective actions
static const std::unordered_map<std::string, std::function<void()>> effectActions = {
{"bilinear", []() { GL_mipmap_filter = 0; }},
{"trilinear", []() { GL_mipmap_filter = 1; }}};
auto it = effectActions.find(mode);
if (it != effectActions.end()) {
it->second(); // Execute the corresponding action
} else {
error_display(0, "%s is not a valid texturing filtering setting", mode.c_str());
}
}
static auto TextureFilteringOption __UNUSED = options::OptionBuilder<int>("Graphics.TextureFilter",
std::pair<const char*, int>{"Texture Filtering", 1763},
std::pair<const char*, int>{"Texture filtering option", 1764})
.importance(1)
.category(std::make_pair("Graphics", 1825))
.values(TextureFilteringValues)
.default_func([]() {return GL_mipmap_filter;})
.bind_to_once(&GL_mipmap_filter)
.flags({options::OptionFlags::ForceMultiValueSelection})
.parser(parse_texture_filtering_func)
.finish();
static SCP_vector<float> anisotropic_value_enumerator()
{
float max;
if (!gr_get_property(gr_property::MAX_ANISOTROPY, &max)) {
return SCP_vector<float>();
}
if (max <= 2.0f) {
return SCP_vector<float>();
}
SCP_vector<float> out;
// We assume here that the anisotropy levels are powers of two...
float current = 1.0f;
while (current <= max) {
out.push_back(current);
current *= 2.0f;
}
return out;
}
static SCP_string anisotropic_display(float val)
{
if (val < 2.0f) {
return "None";
}
SCP_string out;
sprintf(out, "%.0fx", val);
return out;
}
static float anisotropic_default()
{
float max;
if (!gr_get_property(gr_property::MAX_ANISOTROPY, &max)) {
return 1.0f;
}
return max;
}
static auto AnisotropyOption = options::OptionBuilder<float>("Graphics.Anisotropy",
std::pair<const char*, int>{"Anistropic filtering", 1736},
std::pair<const char*, int>{"Controls the amount of anistropic filtering of the textures", 1737})
.enumerator(anisotropic_value_enumerator)
.category(std::make_pair("Graphics", 1825))
.display(anisotropic_display)
.default_func(anisotropic_default)
.level(options::ExpertLevel::Advanced)
.importance(78)
.finish();
// forward declarations
int opengl_free_texture(tcache_slot_opengl *t);
int opengl_create_texture (int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot = NULL);
extern int get_num_mipmap_levels(int w, int h);
void opengl_set_additive_tex_env()
{
GL_CHECK_FOR_ERRORS("start of set_additive_tex_env()");
GL_CHECK_FOR_ERRORS("end of set_additive_tex_env()");
}
void opengl_set_modulate_tex_env()
{
GL_CHECK_FOR_ERRORS("start of set_modulate_tex_env()");
GL_CHECK_FOR_ERRORS("end of set_modulate_tex_env()");
}
void opengl_set_texture_target( GLenum target )
{
GL_texture_target = target;
}
void opengl_tcache_init()
{
GL_should_preload = 1;
GL_min_texture_width = 16;
GL_min_texture_height = 16;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &GL_max_texture_width);
GL_max_texture_height = GL_max_texture_width;
// if we are not using sections then make sure we have the min texture size available to us
// 1024 is what we need with the standard resolutions - taylor
if (GL_max_texture_width < 1024) {
Error(LOCATION, "A minimum texture size of \"1024x1024\" is required for FS2_Open but only \"%ix%i\" was found. Can not continue.", GL_max_texture_width, GL_max_texture_height);
}
// check what mipmap filter we should be using
// 0 == Bilinear
// 1 == Trilinear
GL_mipmap_filter = os_config_read_uint(NULL, "TextureFilter", 1);
if (GL_mipmap_filter > 1) {
GL_mipmap_filter = 1;
}
// max size (width and/or height) that we can use for framebuffer/renderbuffer
glGetIntegerv(GL_MAX_RENDERBUFFER_SIZE, &GL_max_renderbuffer_size);
// if we can't do at least 128x128 then just disable FBOs
if (GL_max_renderbuffer_size < 128) {
mprintf(("WARNING: Max dimensions of FBO, %ix%i, is less the required minimum!! Extension will be disabled!\n", GL_max_renderbuffer_size, GL_max_renderbuffer_size));
Cmdline_no_fbo = 1;
}
// anisotropy
if ( GLAD_GL_EXT_texture_filter_anisotropic ) {
// set max value first thing
GL_anisotropy = GL_state.Constants.GetMaxAnisotropy();
if (Using_in_game_options) {
GL_anisotropy = AnisotropyOption->getValue();
} else {
// now for the user setting
if (Cmdline_aniso_level != 0)
GL_anisotropy = (GLfloat)Cmdline_aniso_level;
}
CLAMP(GL_anisotropy, 1.0f, GL_state.Constants.GetMaxAnisotropy());
}
Assert( GL_supported_texture_units >= 2 );
GL_last_detail = Detail.hardware_textures;
GL_textures_in_frame = 0;
vm_matrix4_set_identity(&GL_texture_matrix);
}
void opengl_tcache_flush()
{
for (auto& block : bm_blocks) {
for (auto& slot : block) {
opengl_free_texture(static_cast<tcache_slot_opengl*>(slot.gr_info));
}
}
}
extern void opengl_kill_all_render_targets();
void opengl_tcache_shutdown()
{
opengl_kill_all_render_targets();
opengl_tcache_flush();
GL_textures_in_frame = 0;
}
void opengl_tcache_frame()
{
GL_textures_in_frame = 0;
}
extern bool GL_initted;
void opengl_free_texture_slot(bitmap_slot* slot)
{
if ( !GL_initted ) {
return;
}
auto tcache_slot = static_cast<tcache_slot_opengl*>(slot->gr_info);
if (tcache_slot == nullptr) {
// This slot hasn't been initialized yet and can be skipped safely
return;
}
opengl_free_texture(tcache_slot);
}
/**
* Determine if a bitmap is in API memory, so that we can just reuse it rather
* that having to load it from disk again
*/
bool opengl_texture_slot_valid(int handle)
{
auto t = bm_get_gr_info<tcache_slot_opengl>(handle);
if (t->bitmap_handle < 0) {
return false;
}
if (t->bitmap_handle != handle) {
return false;
}
if ( !glIsTexture(t->texture_id) ) {
return false;
}
return true;
}
int opengl_free_texture(tcache_slot_opengl *t)
{
if (!t->texture_id) {
// No texture here anymore
return 1;
}
Assertion(t->used, "Tried to free unused texture slot. This shouldn't happen!");
// First mark this as unused and then check if all frames are unused. If that's the case we can free the texture array
t->used = false;
// Check if the bitmap handle is valid
if (t->bitmap_handle >= 0) {
int num_frames = 0;
auto animation_begin = bm_get_base_frame(t->bitmap_handle, &num_frames);
if (animation_begin < 0) {
// Some kind of error
return 0;
}
// Get the index of the first slot and then check every slot of the animation
auto end_slot_handle = animation_begin + num_frames - 1; // Slots are contiguous for the same animation. Since num_frames is 1-base we need to substract 1
bool something_in_use = false; // Will be used to check if there is still a frame left in use
for (int slot_handle = animation_begin; slot_handle <= end_slot_handle; ++slot_handle) {
auto current_slot = bm_get_gr_info<tcache_slot_opengl>(slot_handle);
something_in_use = something_in_use || current_slot->used; // We use or here since only one slot needs to be in use
if (something_in_use) {
// If this is true then we can break early since we already determined that one frame is in use
break;
}
}
if (something_in_use) {
// There are still used frames left in the animation so we can't free the texture object here
// We still need to reset this slot though since it has been "freed"
t->reset();
// Everything is fine
return 1;
}
// No frame is used anymore so now we can free the texture
} else {
// If we are here then the texture slot must contain an FBO texture
Assertion(t->fbo_id >= 0, "Found texture slot with invalid bitmap number which wasn't an FBO!");
}
GR_DEBUG_SCOPE("Delete texture");
// ok, now we know its legal to free everything safely
GL_state.Texture.Delete(t->texture_id);
glDeleteTextures (1, &t->texture_id);
// Finally, reset the data of this slot
t->reset();
return 1;
}
// data == start of bitmap data
// bmap_w == width of source bitmap
// bmap_h == height of source bitmap
// tex_w == width of final texture
// tex_h == height of final texture
static int opengl_texture_set_level(int bitmap_handle, int bitmap_type, int bmap_w, int bmap_h, int tex_w, int tex_h, int tex_d,
ubyte* data, tcache_slot_opengl* tSlot, int base_level, int mipmap_levels,
bool resize, GLenum intFormat) {
GR_DEBUG_SCOPE("Upload single frame");
int ret_val = 1;
ubyte* bmp_data = data;
ubyte *texmem = NULL, *texmemp = NULL;
int skip_size = 0;
GL_CHECK_FOR_ERRORS("start of create_texture_sub()");
// bogus
if ((tSlot == nullptr) || (bmp_data == nullptr)) {
return 0;
}
if ((bitmap_type == TCACHE_TYPE_AABITMAP) || (bitmap_type == TCACHE_TYPE_INTERFACE)) {
tSlot->u_scale = (float)bmap_w / (float)tex_w;
tSlot->v_scale = (float)bmap_h / (float)tex_h;
} else {
tSlot->u_scale = 1.0f;
tSlot->v_scale = 1.0f;
}
// set the byte per pixel multiplier
auto byte_mult = (tSlot->bpp >> 3);
GLenum texFormat = GL_UNSIGNED_SHORT_1_5_5_5_REV;
GLenum glFormat = GL_BGRA;
// GL_BGRA_EXT is *much* faster with some hardware/drivers
if (byte_mult == 4) {
texFormat = GL_UNSIGNED_INT_8_8_8_8_REV;
intFormat = (gr_screen.bits_per_pixel == 32) ? GL_RGBA8 : GL_RGB5_A1;
glFormat = GL_BGRA;
} else if (byte_mult == 3) {
texFormat = GL_UNSIGNED_BYTE;
intFormat = (gr_screen.bits_per_pixel == 32) ? GL_RGB8 : GL_RGB5;
glFormat = GL_BGR;
} else if (byte_mult == 2) {
texFormat = GL_UNSIGNED_SHORT_1_5_5_5_REV;
intFormat = GL_RGB5_A1;
glFormat = GL_BGRA;
} else if (byte_mult == 1) {
Assertion(bitmap_type == TCACHE_TYPE_AABITMAP,
"Invalid type for bitmap: %s BMPMAN handle: %d. Type expected was 0, we got %d instead.\nThis can be "
"caused by using texture compression on a non-power-of-2 texture.\n",
bm_get_filename(bitmap_handle), bitmap_handle, bitmap_type);
texFormat = GL_UNSIGNED_BYTE;
intFormat = GL_RED;
glFormat = GL_RED;
} else {
texFormat = GL_UNSIGNED_SHORT_1_5_5_5_REV;
intFormat = GL_RGBA;
glFormat = GL_BGRA;
}
// check for compressed image types
auto block_size = 0;
switch (bm_is_compressed(bitmap_handle)) {
case DDS_DXT1:
case DDS_CUBEMAP_DXT1:
intFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
block_size = 8;
break;
case DDS_DXT3:
case DDS_CUBEMAP_DXT3:
intFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
block_size = 16;
break;
case DDS_DXT5:
case DDS_CUBEMAP_DXT5:
intFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
block_size = 16;
break;
case DDS_BC7:
intFormat = GL_COMPRESSED_RGBA_BPTC_UNORM_ARB;
block_size = 16;
break;
}
if (bitmap_type == TCACHE_TYPE_CUBEMAP) {
tSlot->texture_target = GL_TEXTURE_CUBE_MAP;
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
auto dsize = 0;
auto doffset = 0;
switch (bitmap_type) {
case TCACHE_TYPE_COMPRESSED: {
if (block_size > 0) {
auto mipmap_w = bmap_w;
auto mipmap_h = bmap_h;
for (auto i = 0; i < mipmap_levels + base_level; i++) {
// size of data block (4x4)
dsize = ((mipmap_h + 3) / 4) * ((mipmap_w + 3) / 4) * block_size;
if (i >= base_level) {
glCompressedTexSubImage3D(tSlot->texture_target, i - base_level, 0, 0, tSlot->array_index, mipmap_w,
mipmap_h, 1, intFormat, dsize, bmp_data + doffset);
}
// adjust the data offset for the next block
doffset += dsize;
// reduce size by half for the next pass
mipmap_w >>= 1;
mipmap_h >>= 1;
if (mipmap_w <= 0) {
mipmap_w = 1;
}
if (mipmap_h <= 0) {
mipmap_h = 1;
}
}
} else {
Int3();
return 0;
}
break;
}
case TCACHE_TYPE_AABITMAP: {
texmem = (ubyte*)vm_malloc(tex_w * tex_h * byte_mult);
texmemp = texmem;
Assert(texmem != nullptr);
int luminance = 0;
for (auto i = 0; i < tex_h; i++) {
for (auto j = 0; j < tex_w; j++) {
if ((i < bmap_h) && (j < bmap_w)) {
if (byte_mult > 1) {
luminance = 0;
if (byte_mult > 3) {
for (auto k = 0; k < 3; k++) {
luminance += bmp_data[(i * bmap_w + j) * byte_mult + k];
}
*texmemp++ =
(ubyte)((luminance / 3) * (bmp_data[(i * bmap_w + j) * byte_mult + 3] / 255.0f));
} else {
for (auto k = 0; k < byte_mult; k++) {
luminance += bmp_data[(i * bmap_w + j) * byte_mult + k];
}
*texmemp++ = (ubyte)(luminance / byte_mult);
}
} else {
*texmemp++ = bmp_data[i * bmap_w + j];
}
} else {
*texmemp++ = 0;
}
}
}
GLenum aa_format = GL_RED;
glTexSubImage3D(tSlot->texture_target, 0, 0, 0, tSlot->array_index, tex_w, tex_h, 1, aa_format,
GL_UNSIGNED_BYTE, texmem);
if (texmem != nullptr) {
vm_free(texmem);
}
break;
}
case TCACHE_TYPE_INTERFACE: {
// if we aren't resizing then we can just use bmp_data directly
if (resize) {
texmem = (ubyte*)vm_malloc(tex_w * tex_h * byte_mult);
texmemp = texmem;
Assert(texmem != nullptr);
for (auto i = 0; i < tex_h; i++) {
for (auto j = 0; j < tex_w; j++) {
if ((i < bmap_h) && (j < bmap_w)) {
for (auto k = 0; k < byte_mult; k++) {
*texmemp++ = bmp_data[(i * bmap_w + j) * byte_mult + k];
}
} else {
for (auto k = 0; k < byte_mult; k++) {
*texmemp++ = 0;
}
}
}
}
}
glTexSubImage3D(tSlot->texture_target, 0, 0, 0, tSlot->array_index, tex_w, tex_h, 1, glFormat, texFormat,
(resize) ? texmem : bmp_data);
if (texmem != nullptr) {
vm_free(texmem);
}
break;
}
case TCACHE_TYPE_CUBEMAP: {
Assert(!resize);
Assert(texmem == nullptr);
// we have to load in all 6 faces...
doffset = 0;
for (auto i = 0; i < 6; i++) {
// We need to use the actual size of the bitmap here since tex_h/w is already
// adjusted for the size of the target bitmap. Since we are not resizing here that is not a problem
auto mipmap_w = bmap_w;
auto mipmap_h = bmap_h;
// check if it's a compressed cubemap first
if (block_size > 0) {
for (auto level = 0; level < mipmap_levels + base_level; level++) {
// size of data block (4x4)
dsize = ((mipmap_h + 3) / 4) * ((mipmap_w + 3) / 4) * block_size;
if (level >= base_level) {
// We skipped ahead to the base level so we can start uploading frames now
glCompressedTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, level - base_level, 0, 0,
mipmap_w, mipmap_h, intFormat, dsize, bmp_data + doffset);
}
// adjust the data offset for the next block
doffset += dsize;
// reduce size by half for the next pass
mipmap_w >>= 1;
mipmap_h >>= 1;
if (mipmap_w <= 0) {
mipmap_w = 1;
}
if (mipmap_h <= 0) {
mipmap_h = 1;
}
}
} else {
// nope, it's uncompressed...
for (auto level = 0; level < mipmap_levels + base_level; level++) {
dsize = mipmap_h * mipmap_w * byte_mult;
if (level >= base_level) {
glTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, level - base_level, 0, 0, mipmap_w,
mipmap_h, glFormat, texFormat, bmp_data + doffset);
}
// base image is done so now take care of any mipmap levels
doffset += dsize;
mipmap_w >>= 1;
mipmap_h >>= 1;
if (mipmap_w <= 0) {
mipmap_w = 1;
}
if (mipmap_h <= 0) {
mipmap_h = 1;
}
}
}
}
break;
}
case TCACHE_TYPE_3DTEX:
//Just upload as-is.
glTexSubImage3D(tSlot->texture_target, 0, 0, 0, 0, tex_w, tex_h, tex_d,
glFormat, texFormat, bmp_data + doffset);
break;
default: {
// if we aren't resizing then we can just use bmp_data directly
if (resize) {
texmem = (ubyte*)vm_malloc(tex_w * tex_h * byte_mult);
texmemp = texmem;
Assert(texmem != nullptr);
fix u, utmp, v, du, dv;
u = v = 0;
du = ((bmap_w - 1) * F1_0) / tex_w;
dv = ((bmap_h - 1) * F1_0) / tex_h;
for (auto j = 0; j < tex_h; j++) {
utmp = u;
for (auto i = 0; i < tex_w; i++) {
for (auto k = 0; k < byte_mult; k++) {
*texmemp++ = bmp_data[(f2i(v) * bmap_w + f2i(utmp)) * byte_mult + k];
}
utmp += du;
}
v += dv;
}
}
auto mipmap_w = tex_w;
auto mipmap_h = tex_h;
// if we are doing mipmap resizing we need to account for adjusted tex size
// (we can end up with only one mipmap level if base_level is high enough so don't check it)
if (base_level > 0) {
Assertion(resize == false, "Cannot use manual and mipmap resizing at the same time!");
Assert(texmem == nullptr);
// If we have mipmaps then tex_w/h are already adjusted for the base level but that will cause problems with
// the code below. Instead we set the values to the actual size of the bitmap here to make sure we can
// iterate properly through the mipmap levels.
mipmap_w = bmap_w;
mipmap_h = bmap_h;
}
for (auto i = 0; i < mipmap_levels + base_level; i++) {
// size of data block (4x4)
dsize = mipmap_h * mipmap_w * byte_mult;
if (i >= base_level) {
glTexSubImage3D(tSlot->texture_target, i - base_level, 0, 0, tSlot->array_index, mipmap_w, mipmap_h, 1,
glFormat, texFormat, (texmem != nullptr) ? texmem : bmp_data + doffset);
}
// adjust the data offset for the next block
doffset += dsize;
// reduce size by half for the next pass
mipmap_w >>= 1;
mipmap_h >>= 1;
if (mipmap_w <= 0) {
mipmap_w = 1;
}
if (mipmap_h <= 0) {
mipmap_h = 1;
}
}
if (texmem != nullptr) {
vm_free(texmem);
}
break;
}
} // end switch
tSlot->bitmap_handle = bitmap_handle;
tSlot->size = (dsize) ? ((doffset + dsize) - skip_size) : (tex_w * tex_h * byte_mult);
tSlot->w = (ushort)tex_w;
tSlot->h = (ushort)tex_h;
GL_textures_in_frame += tSlot->size;
GL_CHECK_FOR_ERRORS("end of create_texture_sub()");
return ret_val;
}
static GLenum opengl_get_internal_format(int handle, int bitmap_type, int bpp) {
// set the byte per pixel multiplier
auto byte_mult = (bpp >> 3);
// check for compressed image types
switch ( bm_is_compressed(handle) ) {
case DDS_DXT1:
case DDS_CUBEMAP_DXT1:
return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
case DDS_DXT3:
case DDS_CUBEMAP_DXT3:
return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
break;
case DDS_DXT5:
case DDS_CUBEMAP_DXT5:
return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
case DDS_BC7:
return GL_COMPRESSED_RGBA_BPTC_UNORM_ARB;
default:
// Not compressed
break;
}
if (byte_mult == 4) {
return (gr_screen.bits_per_pixel == 32) ? GL_RGBA8 : GL_RGB5_A1;
} else if (byte_mult == 3) {
return (gr_screen.bits_per_pixel == 32) ? GL_RGB8 : GL_RGB5;
} else if (byte_mult == 2) {
return GL_RGB5_A1;
} else if (byte_mult == 1) {
Assertion( bitmap_type == TCACHE_TYPE_AABITMAP, "Invalid type for bitmap: %s BMPMAN handle: %d. Type expected was 0, we got %d instead.\nThis can be caused by using texture compression on a non-power-of-2 texture.\n", bm_get_filename(handle), handle, bitmap_type );
return GL_R8;
} else {
return GL_RGBA8;
}
}
void opengl_determine_bpp_and_flags(int bitmap_handle, int bitmap_type, ushort& flags, int& bpp) {
flags = 0;
bpp = 16;
switch (bitmap_type) {
case TCACHE_TYPE_AABITMAP:
flags |= BMP_AABITMAP;
bpp = 8;
break;
case TCACHE_TYPE_CUBEMAP:
case TCACHE_TYPE_NORMAL:
flags |= BMP_TEX_OTHER;
if (bm_get_type(bitmap_handle) == BM_TYPE_PCX) {
// PCX is special since the locking code only works with bpp = 16 for some reason
bpp = 16;
} else {
if (bm_has_alpha_channel(bitmap_handle)) {
bpp = 32; // Since this bitmap has an alpha channel we need to respect that
} else {
bpp = 24; // RGB, 8-bits per channel
}
}
break;
case TCACHE_TYPE_INTERFACE:
case TCACHE_TYPE_XPARENT:
flags |= BMP_TEX_XPARENT;
if (bm_get_type(bitmap_handle) == BM_TYPE_PCX) {
// PCX is special since the locking code only works with bpp = 16 for some reason
bpp = 16;
} else {
bpp = 32; // RGBA, 8-bits per channel
}
break;
case TCACHE_TYPE_COMPRESSED:
switch ( bm_is_compressed(bitmap_handle) ) {
case DDS_DXT1: //dxt1
bpp = 24;
flags |= BMP_TEX_DXT1;
break;
case DDS_DXT3: //dxt3
bpp = 32;
flags |= BMP_TEX_DXT3;
break;
case DDS_DXT5: //dxt5
bpp = 32;
flags |= BMP_TEX_DXT5;
break;
case DDS_BC7: //bc7
bpp = 32;
flags |= BMP_TEX_BC7;
break;
case DDS_CUBEMAP_DXT1:
bpp = 24;
flags |= BMP_TEX_CUBEMAP;
break;
case DDS_CUBEMAP_DXT3:
case DDS_CUBEMAP_DXT5:
bpp = 32;
flags |= BMP_TEX_CUBEMAP;
break;
default:
Assert( 0 );
break;
}
break;
case TCACHE_TYPE_3DTEX:
bpp = 32;
break;
}
}
void opengl_tex_array_storage(GLenum target, GLint levels, GLenum format, GLint width, GLint height, GLint frames) {
if (target == GL_TEXTURE_CUBE_MAP) {
Assertion(frames == 1, "Cube map texture arrays aren't supported yet!");
if (GLAD_GL_ARB_texture_storage) {
// This version has a better way of specifying the texture storage
if ( levels == 1 ) {
// looks like we only have one mipmap for this cube map
// allocate additional storage for the mip maps we're going to later generate using glGenerateMipMap
levels = get_num_mipmap_levels(width, height);
}
glTexStorage2D(target, levels, format, width, height);
} else {
for (auto i = 0; i < 6; ++i) {
auto mip_width = width;
auto mip_height = height;
for (auto j = 0; j < levels; j++) {
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, j, format, mip_width, mip_height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
nullptr);
mip_width = std::max(1, (mip_width / 2));
mip_height = std::max(1, (mip_height / 2));
}
}
// Make sure that the image is complete with the right number of mipmap levels
glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels);
}
} else {
if (GLAD_GL_ARB_texture_storage) {
// This version has a better way of specifying the texture storage
glTexStorage3D(target, levels, format, width, height, frames);
} else {
for (auto i = 0; i < levels; i++) {
glTexImage3D(target, i, format, width, height, frames, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
width = std::max(1, (width / 2));
height = std::max(1, (height / 2));
}
// Make sure that the image is complete with the right number of mipmap levels
glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels);
}
}
}
int opengl_create_texture(int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot)
{
GR_DEBUG_SCOPE("Create Texture");
if (tslot == NULL) {
return 0;
}
int num_frames = 0;
// This takes care of checking if the animation can be put into a texture array
auto animation_begin = bm_get_base_frame(bitmap_handle, &num_frames);
if (animation_begin < 0) {
// Some kind of error
return 0;
}
int width, height, depth = 1;
bm_get_info(animation_begin, &width, &height, nullptr, nullptr);
auto start_slot = bm_get_gr_info<tcache_slot_opengl>(animation_begin, true);
if (start_slot->bitmap_handle != -1) {
Assertion(start_slot->bitmap_handle != animation_begin, "opengl_create_texture was called for the same bitmap again!");
if (start_slot->texture_id != 0) {
// Delete the previous texture that was stored in this slot
GL_state.Texture.Delete(start_slot->texture_id);
glDeleteTextures(1, &start_slot->texture_id);
start_slot->reset();
}
}
auto max_levels = bm_get_num_mipmaps(bitmap_handle);
auto base_level = 0;
auto resize = false;
if ( (Detail.hardware_textures < 4) && (bitmap_type != TCACHE_TYPE_AABITMAP) && (bitmap_type != TCACHE_TYPE_INTERFACE)
&& (bitmap_type != TCACHE_TYPE_CUBEMAP) && (bitmap_type != TCACHE_TYPE_3DTEX)
&& ((bitmap_type != TCACHE_TYPE_COMPRESSED) || ((bitmap_type == TCACHE_TYPE_COMPRESSED) && (max_levels > 1))) )
{
if (max_levels == 1) {
// if we are going to cull the size then we need to force a resize
// Detail.hardware_textures goes from 0 to 4.
width /= (16 >> Detail.hardware_textures);
height /= (16 >> Detail.hardware_textures);
CLAMP(width, GL_min_texture_width, GL_max_texture_width);
CLAMP(height, GL_min_texture_height, GL_max_texture_height);
resize = true;
} else {
// we have mipmap levels so use those as a resize point (image should already be power-of-2)
base_level = -(Detail.hardware_textures - 4);
Assert(base_level >= 0);
if (base_level >= max_levels) {
base_level = max_levels - 1;
}
Assert( (max_levels - base_level) >= 1 );
// Adjust the size to match the size of the used base-mipmap
width >>= base_level;
height >>= base_level;
}
}
if ( (width < 1) || (height< 1) ) {
mprintf(("Bitmap %s is too small at %dx%d.\n", bm_get_filename(bitmap_handle), width, height));
return 0;
}
// there should only ever be one mipmap level for interface graphics!!!
if ( (bitmap_type == TCACHE_TYPE_INTERFACE) && (max_levels > 1) ) {
max_levels = 1;
}
// if we ended up locking a texture that wasn't originally compressed then this should catch it
if ( bitmap_type != TCACHE_TYPE_CUBEMAP && bitmap_type != TCACHE_TYPE_3DTEX && bm_is_compressed(bitmap_handle) ) {
bitmap_type = TCACHE_TYPE_COMPRESSED;
}
// Create the texture array and bind it
glGenTextures(1, &tslot->texture_id);
tslot->texture_target = GL_TEXTURE_2D_ARRAY;
if ( bitmap_type == TCACHE_TYPE_CUBEMAP ) {
tslot->texture_target = GL_TEXTURE_CUBE_MAP;
}
else if ( bitmap_type == TCACHE_TYPE_3DTEX ) {
tslot->texture_target = GL_TEXTURE_3D;
}
if (tslot->texture_id == 0) {
mprintf(("!!OpenGL DEBUG!! t->texture_id == 0\n"));
tslot->reset();
return 0;
}
// This is very important! All bitmaps are bound as texture arrays even if they only contain a single image. This
// simplifies shader handling since they only ever work with texture arrays instead of having to introduce another