-
Notifications
You must be signed in to change notification settings - Fork 1
/
texture.cpp
2350 lines (2130 loc) · 90.4 KB
/
texture.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//============================================================================
// Mini Spin-X Library
//
// Copyright (c) 2024, Jarkko Lempiainen
// All rights reserved.
//============================================================================
#include "sxp_src/sxp_pch.h"
#include "texture.h"
#include "sxp_src/core/math/math.h"
#include "sxp_src/core/math/bit_math.h"
#include "sxp_src/core/math/numeric.h"
#include "sxp_src/core/fsys/fsys.h"
#ifdef PFC_ENGINEOP_NVTEXTURETOOLS
#include "sxp_extlibs/nvtexturetools/src/nvtt/nvtt.h"
#endif
using namespace pfc;
//----------------------------------------------------------------------------
//============================================================================
// external library dependencies
//============================================================================
// nvtexturetools
#ifdef PFC_ENGINEOP_NVTEXTURETOOLS
#ifdef PFC_ENGINEOP_CUDA
#pragma comment(lib, PFC_STR(PFC_CAT3(nvtexturetools_,PFC_BUILD_STR,_cuda)PFC_COMPILER_LIB_EXT))
#pragma comment(lib, "cudart.lib")
#else
#pragma comment(lib, PFC_STR(PFC_CAT2(nvtexturetools_,PFC_BUILD_STR)PFC_COMPILER_LIB_EXT))
#endif
#endif
//----------------------------------------------------------------------------
//============================================================================
// texture format configuration
//============================================================================
// name bpp type conversion-fmt R-mask G-mask B-mask A-mask rgba-order
#define PFC_TEXFORMAT_LIST \
PFC_TEXFORMAT(a8, 8, 1, texfmttype_rgba, a8r8g8b8, 0, 0, 0, 0, 0, 0, 0, 8, 0x0004) \
PFC_TEXFORMAT(r8, 8, 1, texfmttype_rgba, a8r8g8b8, 0, 8, 0, 0, 0, 0, 0, 0, 0x0001) \
PFC_TEXFORMAT(a16, 16, 1, texfmttype_rgba, a32b32g32r32, 0, 0, 0, 0, 0, 0, 0, 16, 0x0004) \
PFC_TEXFORMAT(r16, 16, 1, texfmttype_rgba, a32b32g32r32, 0, 16, 0, 0, 0, 0, 0, 0, 0x0001) \
PFC_TEXFORMAT(r16f, 16, 1, texfmttype_rgba16f, a32b32g32r32f, 0, 16, 0, 0, 0, 0, 0, 0, 0x0001) \
PFC_TEXFORMAT(a8r8, 16, 1, texfmttype_rgba, a8r8g8b8, 0, 8, 0, 0, 0, 0, 8, 8, 0x0041) \
PFC_TEXFORMAT(g8r8, 16, 1, texfmttype_rgba, a8r8g8b8, 0, 8, 8, 8, 0, 0, 0, 0, 0x0021) \
PFC_TEXFORMAT(r5g6b5, 16, 1, texfmttype_rgba, a8r8g8b8, 11, 5, 5, 6, 0, 5, 0, 0, 0x0123) \
PFC_TEXFORMAT(a1r5g5b5, 16, 1, texfmttype_rgba, a8r8g8b8, 10, 5, 5, 5, 0, 5, 15, 1, 0x4123) \
PFC_TEXFORMAT(a4r4g4b4, 16, 1, texfmttype_rgba, a8r8g8b8, 8, 4, 4, 4, 0, 4, 12, 4, 0x4123) \
PFC_TEXFORMAT(r8g8b8, 24, 1, texfmttype_rgba, a8r8g8b8, 16, 8, 8, 8, 0, 8, 0, 0, 0x0123) \
PFC_TEXFORMAT(b8g8r8, 24, 1, texfmttype_rgba, a8r8g8b8, 0, 8, 8, 8, 16, 8, 0, 0, 0x0321) \
PFC_TEXFORMAT(a32, 32, 1, texfmttype_rgba, a32b32g32r32, 0, 0, 0, 0, 0, 0, 0, 32, 0x0004) \
PFC_TEXFORMAT(r32, 32, 1, texfmttype_rgba, a32b32g32r32, 0, 32, 0, 0, 0, 0, 0, 0, 0x0001) \
PFC_TEXFORMAT(r32f, 32, 1, texfmttype_rgba32f, a32b32g32r32f, 0, 32, 0, 0, 0, 0, 0, 0, 0x0001) \
PFC_TEXFORMAT(a16r16, 32, 1, texfmttype_rgba, a32b32g32r32, 0, 16, 0, 0, 0, 0, 16, 16, 0x0041) \
PFC_TEXFORMAT(g16r16, 32, 1, texfmttype_rgba, a32b32g32r32, 0, 16, 16, 16, 0, 0, 0, 0, 0x0021) \
PFC_TEXFORMAT(g16r16f, 32, 1, texfmttype_rgba16f, a32b32g32r32f, 0, 16, 16, 16, 0, 0, 0, 0, 0x0021) \
PFC_TEXFORMAT(a8r8g8b8, 32, 1, texfmttype_rgba, a8r8g8b8, 16, 8, 8, 8, 0, 8, 24, 8, 0x4123) \
PFC_TEXFORMAT(a8b8g8r8, 32, 1, texfmttype_rgba, a8r8g8b8, 0, 8, 8, 8, 16, 8, 24, 8, 0x4321) \
PFC_TEXFORMAT(b8g8r8a8, 32, 1, texfmttype_rgba, a8r8g8b8, 8, 8, 16, 8, 24, 8, 0, 8, 0x3214) \
PFC_TEXFORMAT(a2b10g10r10, 32, 1, texfmttype_rgba, a32b32g32r32, 0, 10, 10, 10, 20, 10, 30, 2, 0x4321) \
PFC_TEXFORMAT(b16g16r16, 48, 1, texfmttype_rgba, a32b32g32r32, 0, 16, 16, 16, 32, 16, 0, 0, 0x0321) \
PFC_TEXFORMAT(a32r32, 64, 1, texfmttype_rgba, a32b32g32r32, 0, 32, 0, 0, 0, 0, 32, 32, 0x0041) \
PFC_TEXFORMAT(g32r32, 64, 1, texfmttype_rgba, a32b32g32r32, 0, 32, 32, 32, 0, 0, 0, 0, 0x0021) \
PFC_TEXFORMAT(g32r32f, 64, 1, texfmttype_rgba32f, a32b32g32r32f, 0, 32, 32, 32, 0, 0, 0, 0, 0x0021) \
PFC_TEXFORMAT(b16g16r16a16, 64, 1, texfmttype_rgba, a32b32g32r32, 16, 16, 32, 16, 48, 16, 0, 16, 0x3214) \
PFC_TEXFORMAT(a16b16g16r16, 64, 1, texfmttype_rgba, a32b32g32r32, 0, 16, 16, 16, 32, 16, 48, 16, 0x4321) \
PFC_TEXFORMAT(a16b16g16r16f, 64, 1, texfmttype_rgba16f, a32b32g32r32f, 0, 16, 16, 16, 32, 16, 48, 16, 0x4321) \
PFC_TEXFORMAT(b32g32r32f, 96, 1, texfmttype_rgba32f, a32b32g32r32f, 0, 32, 32, 32, 64, 32, 0, 0, 0x0321) \
PFC_TEXFORMAT(a32b32g32r32, 128, 1, texfmttype_rgba, a32b32g32r32, 0, 32, 32, 32, 64, 32, 96, 32, 0x4321) \
PFC_TEXFORMAT(a32b32g32r32f, 128, 1, texfmttype_rgba32f, a32b32g32r32f, 0, 32, 32, 32, 64, 32, 96, 32, 0x4321) \
PFC_TEXFORMAT(bc1, 4, 4, texfmttype_bc, a8r8g8b8, 0, -1, 0, -1, 0, -1, 0, 0, 0x0000) \
PFC_TEXFORMAT(bc1a, 4, 4, texfmttype_bc, a8r8g8b8, 0, -1, 0, -1, 0, -1, 0, -1, 0x0000) \
PFC_TEXFORMAT(bc2, 8, 4, texfmttype_bc, a8r8g8b8, 0, -1, 0, -1, 0, -1, 0, -1, 0x0000) \
PFC_TEXFORMAT(bc3, 8, 4, texfmttype_bc, a8r8g8b8, 0, -1, 0, -1, 0, -1, 0, -1, 0x0000) \
PFC_TEXFORMAT(bc6h, 8, 4, texfmttype_bc, a32b32g32r32f, 0, -1, 0, -1, 0, -1, 0, 0, 0x0000) \
PFC_TEXFORMAT(bc7, 8, 4, texfmttype_bc, a8r8g8b8, 0, -1, 0, -1, 0, -1, 0, -1, 0x0000)
//----------------------------------------------------------------------------
//============================================================================
// list of texture formats for bidirectional (to/from) conversion
//============================================================================
#define PFC_TEXFORMAT_CONVERSION_LIST \
PFC_TEXFORMAT(a8) \
PFC_TEXFORMAT(r8) \
PFC_TEXFORMAT(a16) \
PFC_TEXFORMAT(r16) \
PFC_TEXFORMAT(r16f) \
PFC_TEXFORMAT(a8r8) \
PFC_TEXFORMAT(g8r8) \
PFC_TEXFORMAT(r5g6b5) \
PFC_TEXFORMAT(a1r5g5b5) \
PFC_TEXFORMAT(a4r4g4b4) \
PFC_TEXFORMAT(r8g8b8) \
PFC_TEXFORMAT(b8g8r8) \
PFC_TEXFORMAT(a32) \
PFC_TEXFORMAT(r32) \
PFC_TEXFORMAT(r32f) \
PFC_TEXFORMAT(a16r16) \
PFC_TEXFORMAT(g16r16) \
PFC_TEXFORMAT(g16r16f) \
PFC_TEXFORMAT(a8r8g8b8) \
PFC_TEXFORMAT(a8b8g8r8) \
PFC_TEXFORMAT(b8g8r8a8) \
PFC_TEXFORMAT(a2b10g10r10) \
PFC_TEXFORMAT(b16g16r16) \
PFC_TEXFORMAT(a32r32) \
PFC_TEXFORMAT(g32r32) \
PFC_TEXFORMAT(g32r32f) \
PFC_TEXFORMAT(b16g16r16a16) \
PFC_TEXFORMAT(a16b16g16r16) \
PFC_TEXFORMAT(a16b16g16r16f) \
PFC_TEXFORMAT(b32g32r32f) \
PFC_TEXFORMAT(a32b32g32r32) \
PFC_TEXFORMAT(a32b32g32r32f)
//----------------------------------------------------------------------------
//============================================================================
// locals
//============================================================================
namespace
{
//==========================================================================
// get_converter_input_format
//==========================================================================
e_texture_format get_converter_input_format(e_texture_format fmt_)
{
// get texture format for texture conversion for given format
switch(fmt_)
{
#define PFC_TEXFORMAT(name__, bpp__, block_size__, type__, conv_format__, rmask_pos__, rmask_size__, gmask_pos__, gmask_size__, bmask_pos__, bmask_size__, amask_pos__, amask_size__, rgba_order__)\
case texfmt_##name__: return texfmt_##conv_format__;
PFC_TEXFORMAT_LIST
#undef PFC_TEXFORMAT
case texfmt_none:
case texfmt_enum_end: PFC_ERROR("Invalid argument\r\n");
}
PFC_ERROR("Unsupported texture format\r\n");
return texfmt_none;
}
//--------------------------------------------------------------------------
//==========================================================================
// cpu_texture_manager_2d
//==========================================================================
class cpu_texture_manager_2d: public texture_manager_base
{
public:
cpu_texture_manager_2d()
{
// initialize manager
mip_data=0;
width=height=0;
num_mips=0;
array_size=0;
m_format=texfmt_none;
}
//----
virtual void create_texture(const texture_loader &tl_, const texture_loader_params ¶ms_, unsigned num_mips_)
{
// create texture resources
unsigned block_size=texfmt_blocksize(params_.target_format);
width=max(block_size, texsize(tl_.width(), params_.resize_mode)>>params_.first_mip_level);
height=max(block_size, texsize(tl_.height(), params_.resize_mode)>>params_.first_mip_level);
num_mips=texture_mips_2d(tl_.width(), tl_.height(), params_.target_format, num_mips_);
array_size=tl_.array_size();
m_format=params_.target_format;
mip_data=PFC_MEM_ALLOC(texture_size_2d(width, height, params_.target_format, num_mips)*array_size);
}
//----
virtual void finalize()
{
}
//----
virtual void *lock(const texture_layer &layer_, int &pitch_)
{
// return pointer & pitch of given mip level
pitch_=texture_pitch(width, m_format, layer_.mip_level);
void *data=((uint8_t*)mip_data)+(layer_.mip_level?texture_size_2d(width, height, m_format, layer_.mip_level):0)+layer_.array_slice*texture_size_2d(width, height, m_format, num_mips);
return data;
}
//----
virtual void unlock(const texture_layer &layer_)
{
}
//------------------------------------------------------------------------
// public data
void *mip_data;
unsigned width, height;
unsigned num_mips;
unsigned array_size;
//------------------------------------------------------------------------
private:
e_texture_format m_format;
};
//--------------------------------------------------------------------------
//==========================================================================
// cpu_texture_manager_3d
//==========================================================================
class cpu_texture_manager_3d: public texture_manager_base
{
public:
cpu_texture_manager_3d()
{
// initialize manager
mip_data=0;
width=height=depth=0;
m_format=texfmt_none;
num_mips=0;
}
//----
virtual void create_texture(const texture_loader &tl_, const texture_loader_params ¶ms_, unsigned num_mips_)
{
// create texture resources
unsigned block_size=texfmt_blocksize(params_.target_format);
width=max(block_size, texsize(tl_.width(), params_.resize_mode)>>params_.first_mip_level);
height=max(block_size, texsize(tl_.height(), params_.resize_mode)>>params_.first_mip_level);
depth=max(block_size, texsize(tl_.depth(), params_.resize_mode)>>params_.first_mip_level);
num_mips=texture_mips_3d(tl_.width(), tl_.height(), tl_.depth(), params_.target_format, num_mips_);
m_format=params_.target_format;
mip_data=PFC_MEM_ALLOC(texture_size_3d(width, height, depth, m_format, num_mips));
}
//----
virtual void finalize()
{
}
//----
virtual void *lock(const texture_layer &layer_, int &pitch_)
{
// return pointer & pitch of given mip level
pitch_=texture_pitch(width, m_format, layer_.mip_level);
unsigned slice_pitch=texture_mip_size_2d(width, height, m_format, layer_.mip_level);
void *data=((uint8_t*)mip_data)+(layer_.mip_level?texture_size_3d(width, height, depth, m_format, layer_.mip_level):0)+slice_pitch*layer_.volume_slice;
return data;
}
//----
virtual void unlock(const texture_layer &layer_)
{
}
//------------------------------------------------------------------------
// public data
void *mip_data;
unsigned width, height, depth;
unsigned num_mips;
//------------------------------------------------------------------------
private:
e_texture_format m_format;
};
//--------------------------------------------------------------------------
//==========================================================================
// cpu_texture_manager_cube
//==========================================================================
class cpu_texture_manager_cube: public texture_manager_base
{
public:
cpu_texture_manager_cube()
{
// initialize manager
edge_len=0;
num_mips=0;
array_size=0;
m_face_size=0;
m_format=texfmt_none;
}
//----
virtual void create_texture(const texture_loader &tl_, const texture_loader_params ¶ms_, unsigned num_mips_)
{
// create texture data
unsigned block_size=texfmt_blocksize(params_.target_format);
edge_len=max(block_size, texsize(tl_.width(), params_.resize_mode)>>params_.first_mip_level);
num_mips=texture_mips_2d(tl_.width(), tl_.height(), params_.target_format, num_mips_);
array_size=tl_.array_size();
m_format=params_.target_format;
m_face_size=texture_size_2d(edge_len, edge_len, m_format, num_mips);
mip_data.resize(array_size*6);
for(unsigned fi=0; fi<6; ++fi)
mip_data[fi]=PFC_MEM_ALLOC(m_face_size);
}
//----
void finalize()
{
}
//----
virtual void *lock(const texture_layer &layer_, int &pitch_)
{
// return pointer & pitch of given mip level, and setup sub-resource
pitch_=texture_pitch(edge_len, m_format, layer_.mip_level);
void *data=((char*)mip_data[layer_.face+layer_.array_slice*6])+(layer_.mip_level?texture_size_2d(edge_len, edge_len, m_format, layer_.mip_level):0);
return data;
}
//----
virtual void unlock(const texture_layer &layer_)
{
}
//------------------------------------------------------------------------
// public data
array<void*> mip_data;
unsigned edge_len;
unsigned num_mips;
//------------------------------------------------------------------------
private:
unsigned m_face_size;
unsigned array_size;
e_texture_format m_format;
};
} // namespace <anonymous>
//----------------------------------------------------------------------------
//============================================================================
// CUDA acceleration
//============================================================================
static bool s_is_cuda_texture_compression=true;
//----
void pfc::enable_cuda_texture_compression(bool enable_)
{
s_is_cuda_texture_compression=enable_;
}
//----
bool pfc::is_cuda_texture_compression()
{
return s_is_cuda_texture_compression;
}
//----------------------------------------------------------------------------
//============================================================================
// enum_str
//============================================================================
// e_texture_type
#define PFC_ENUM_NAMESPACE pfc
#define PFC_ENUM_TYPE e_texture_type
#define PFC_ENUM_PREFIX textype_
#define PFC_ENUM_VALS PFC_ENUM_VAL(2d)\
PFC_ENUM_VAL(3d)\
PFC_ENUM_VAL(cube)
#include "sxp_src/core/enum.inc"
//----
// e_texture_content
#define PFC_ENUM_NAMESPACE pfc
#define PFC_ENUM_TYPE e_texture_content
#define PFC_ENUM_PREFIX texcontent_
#define PFC_ENUM_VALS PFC_ENUM_VAL(color)\
PFC_ENUM_VAL(normal)
#include "sxp_src/core/enum.inc"
//----
// e_texture_format_type
#define PFC_ENUM_NAMESPACE pfc
#define PFC_ENUM_TYPE e_texture_format_type
#define PFC_ENUM_PREFIX texfmttype_
#define PFC_ENUM_VALS PFC_ENUM_VAL(rgba)\
PFC_ENUM_VAL(rgba16f)\
PFC_ENUM_VAL(rgba32f)\
PFC_ENUM_VAL(bc)
#include "sxp_src/core/enum.inc"
//----
// e_texture_format
#define PFC_ENUM_NAMESPACE pfc
#define PFC_ENUM_TYPE e_texture_format
#define PFC_ENUM_PREFIX texfmt_
#define PFC_ENUM_VALS PFC_ENUM_VAL(a8)\
PFC_ENUM_VAL(r8)\
PFC_ENUM_VAL(a16)\
PFC_ENUM_VAL(r16)\
PFC_ENUM_VAL(r16f)\
PFC_ENUM_VAL(a8r8)\
PFC_ENUM_VAL(g8r8)\
PFC_ENUM_VAL(r5g6b5)\
PFC_ENUM_VAL(a1r5g5b5)\
PFC_ENUM_VAL(a4r4g4b4)\
PFC_ENUM_VAL(r8g8b8)\
PFC_ENUM_VAL(b8g8r8)\
PFC_ENUM_VAL(r32)\
PFC_ENUM_VAL(r32f)\
PFC_ENUM_VAL(a16r16)\
PFC_ENUM_VAL(g16r16)\
PFC_ENUM_VAL(g16r16f)\
PFC_ENUM_VAL(a8r8g8b8)\
PFC_ENUM_VAL(a8b8g8r8)\
PFC_ENUM_VAL(b8g8r8a8)\
PFC_ENUM_VAL(a2b10g10r10)\
PFC_ENUM_VAL(b16g16r16)\
PFC_ENUM_VAL(a32r32)\
PFC_ENUM_VAL(g32r32)\
PFC_ENUM_VAL(g32r32f)\
PFC_ENUM_VAL(b16g16r16a16)\
PFC_ENUM_VAL(a16b16g16r16)\
PFC_ENUM_VAL(a16b16g16r16f)\
PFC_ENUM_VAL(a32b32g32r32)\
PFC_ENUM_VAL(a32b32g32r32f)\
PFC_ENUM_VAL(bc1)\
PFC_ENUM_VAL(bc1a)\
PFC_ENUM_VAL(bc2)\
PFC_ENUM_VAL(bc3)\
PFC_ENUM_VAL(bc6h)\
PFC_ENUM_VAL(bc7)
#define PFC_ENUM_DEP_VALS PFC_ENUM_DEP_VAL(dxt1, bc1)\
PFC_ENUM_DEP_VAL(dxt1a, bc1a)\
PFC_ENUM_DEP_VAL(dxt3, bc2)\
PFC_ENUM_DEP_VAL(dxt5, bc3)
#include "sxp_src/core/enum.inc"
//----
// e_texture_color_space
#define PFC_ENUM_NAMESPACE pfc
#define PFC_ENUM_TYPE e_texture_color_space
#define PFC_ENUM_PREFIX texcolspace_
#define PFC_ENUM_VALS PFC_ENUM_VAL(linear_rgb)\
PFC_ENUM_VAL(srgb)
#include "sxp_src/core/enum.inc"
//----
// e_mip_filter
#define PFC_ENUM_NAMESPACE pfc
#define PFC_ENUM_TYPE e_mip_filter
#define PFC_ENUM_PREFIX mipfilter_
#define PFC_ENUM_VALS PFC_ENUM_VAL(box)\
PFC_ENUM_VAL(sinc)
#include "sxp_src/core/enum.inc"
//----
// e_texture_filter
#define PFC_ENUM_NAMESPACE pfc
#define PFC_ENUM_TYPE e_texture_filter
#define PFC_ENUM_PREFIX texfilter_
#define PFC_ENUM_VALS PFC_ENUM_VAL(point)\
PFC_ENUM_VAL(linear)\
PFC_ENUM_VAL(anisotropic)
#include "sxp_src/core/enum.inc"
//----
// e_texture_compression_quality
#define PFC_ENUM_NAMESPACE pfc
#define PFC_ENUM_TYPE e_texture_compression_quality
#define PFC_ENUM_PREFIX texcq_
#define PFC_ENUM_VALS PFC_ENUM_VAL(fastest)\
PFC_ENUM_VAL(normal)\
PFC_ENUM_VAL(production)\
PFC_ENUM_VAL(highest)
#include "sxp_src/core/enum.inc"
//----
// e_texture_resize_mode
#define PFC_ENUM_NAMESPACE pfc
#define PFC_ENUM_TYPE e_texture_resize_mode
#define PFC_ENUM_PREFIX texresize_
#define PFC_ENUM_VALS PFC_ENUM_VAL(none)\
PFC_ENUM_VAL(prev_pow2)\
PFC_ENUM_VAL(next_pow2)\
PFC_ENUM_VAL(nearest_pow2)
#include "sxp_src/core/enum.inc"
//----------------------------------------------------------------------------
//============================================================================
// texformat_cfg
//============================================================================
template<e_texture_format format> struct texformat_cfg;
#define PFC_TEXFORMAT(name__, bpp__, block_size__, type__, conv_format__, rmask_pos__, rmask_size__, gmask_pos__, gmask_size__, bmask_pos__, bmask_size__, amask_pos__, amask_size__, rgba_order__)\
template<>\
struct texformat_cfg<texfmt_##name__>\
{\
enum {bpp=bpp__,\
block_size=block_size__,\
type=type__,\
conversion_format=texfmt_##conv_format__,\
rgba_order=rgba_order__,\
rmask_pos=rmask_pos__, rmask_size=rmask_size__>0?rmask_size__:0,\
gmask_pos=gmask_pos__, gmask_size=gmask_size__>0?gmask_size__:0,\
bmask_pos=bmask_pos__, bmask_size=bmask_size__>0?bmask_size__:0,\
amask_pos=amask_pos__, amask_size=amask_size__>0?amask_size__:0};\
};
PFC_TEXFORMAT_LIST
#undef PFC_TEXFORMAT
//----------------------------------------------------------------------------
//============================================================================
// texture_loader::loader_null
//============================================================================
class texture_loader::loader_null: public loader_base
{
public:
// construction
loader_null();
//--------------------------------------------------------------------------
// data loading
virtual bool layer(texture_layer&) const;
virtual void load_layer(void*, unsigned pitch_);
virtual void skip_layer();
};
//----------------------------------------------------------------------------
texture_loader::loader_null::loader_null()
{
}
//----------------------------------------------------------------------------
bool texture_loader::loader_null::layer(texture_layer&) const
{
return false;
}
//----
void texture_loader::loader_null::load_layer(void*, unsigned pitch_)
{
}
//----
void texture_loader::loader_null::skip_layer()
{
}
//----------------------------------------------------------------------------
//============================================================================
// texture_loader
//============================================================================
namespace pfc
{
bool get_texture_loader_dds(texture_loader::loader_variant_t&, texture_loader&);
bool get_texture_loader_jp2(texture_loader::loader_variant_t&, texture_loader&);
bool get_texture_loader_jpg(texture_loader::loader_variant_t&, texture_loader&);
bool get_texture_loader_png(texture_loader::loader_variant_t&, texture_loader&);
bool get_texture_loader_psd(texture_loader::loader_variant_t&, texture_loader&);
bool get_texture_loader_tga(texture_loader::loader_variant_t&, texture_loader&);
bool get_texture_loader_tiff(texture_loader::loader_variant_t&, texture_loader&);
bool get_texture_loader_webp(texture_loader::loader_variant_t&, texture_loader&);
} // namespace pfc
//----
texture_loader::texture_loader(bin_input_stream_base &s_)
:m_stream(s_)
,m_loader(meta_type<loader_null>())
{
// init
m_type=textype_none;
m_format=texfmt_none;
m_target_format=texfmt_none;
m_enable_gamma=true;
m_num_mips=0;
m_width=0;
m_height=0;
m_depth=0;
m_array_size=0;
// try to load the texture with different format loaders
m_file_format=filefmt_none;
if(get_texture_loader_jpg(m_loader, *this))
m_file_format=filefmt_jpg;
else if(get_texture_loader_png(m_loader, *this))
m_file_format=filefmt_png;
else if(get_texture_loader_webp(m_loader, *this))
m_file_format=filefmt_webp;
else if(get_texture_loader_dds(m_loader, *this))
m_file_format=filefmt_dds;
else if(get_texture_loader_jp2(m_loader, *this))
m_file_format=filefmt_jp2;
else if(get_texture_loader_tga(m_loader, *this))
m_file_format=filefmt_tga;
else if(get_texture_loader_tiff(m_loader, *this))
m_file_format=filefmt_tiff;
else if(get_texture_loader_psd(m_loader, *this))
m_file_format=filefmt_psd;
else
PFC_WARN("Unable to load the texture format\r\n");
m_target_format=m_format;
}
//----------------------------------------------------------------------------
void texture_loader::load_layer(void *p_, unsigned pitch_)
{
// load texture layer
PFC_ASSERT_MSG(m_format==m_target_format || (texfmt_type(m_format)!=texfmttype_bc && texfmt_type(m_target_format)!=texfmttype_bc),
("Unable to perform format conversion from %s to %s\r\n", enum_string(m_format), enum_string(m_target_format)));
if(!pitch_)
pitch_=m_width*texfmt_bpp(m_target_format)/8;
m_loader->load_layer(p_, pitch_);
}
//----------------------------------------------------------------------------
//============================================================================
// texture_loader::loader_base
//============================================================================
texture_loader::loader_base::~loader_base()
{
}
//----------------------------------------------------------------------------
//============================================================================
// texture_manager_base
//============================================================================
texture_manager_base::texture_manager_base()
{
}
//----------------------------------------------------------------------------
//============================================================================
// texture_converter
//============================================================================
texture_converter::texture_converter()
{
reset();
}
//----
void texture_converter::reset()
{
// initialize converter
m_input_format=texfmt_a8r8g8b8;
m_target_format=texfmt_none;
m_content_type=texcontent_color;
m_compression_quality=texcq_highest;
m_mip_filter=mipfilter_sinc;
m_gamma_input=1.0f;
m_gamma_output=1.0f;
mem_zero(m_targets, sizeof(m_targets));
}
//----------------------------------------------------------------------------
void texture_converter::set_input_format(e_texture_format fmt_)
{
PFC_ASSERT(fmt_==texfmt_a8r8g8b8 || fmt_==texfmt_a16b16g16r16f || fmt_==texfmt_a32b32g32r32f);
m_input_format=fmt_;
}
//----------------------------------------------------------------------------
void texture_converter::convert(const void *source_, unsigned width_, unsigned height_, e_texture_resize_mode resize_mode_)
{
#ifdef PFC_ENGINEOP_NVTEXTURETOOLS
// check inputs
using namespace nvtt;
PFC_ASSERT_MSG(source_, ("Source texture not defined\r\n"));
PFC_ASSERT_MSG(m_target_format!=texfmt_none, ("Target format not defined\r\n"));
// setup input options
InputOptions iop;
switch(m_input_format)
{
case texfmt_a8r8g8b8: iop.setFormat(InputFormat_BGRA_8UB); break;
case texfmt_a16b16g16r16f: iop.setFormat(InputFormat_RGBA_16F); break;
case texfmt_a32b32g32r32f: iop.setFormat(InputFormat_RGBA_32F); break;
default: PFC_ERRORF("Given input format \"%s\" not supported\r\n", enum_string(m_input_format));
}
switch(m_mip_filter)
{
case mipfilter_none: iop.setMipmapGeneration(false); break;
case mipfilter_box: iop.setMipmapFilter(MipmapFilter_Box); break;
case mipfilter_sinc: iop.setMipmapFilter(MipmapFilter_Kaiser); break;
default: PFC_ERRORF("Given mip filter \"%s\" not supported\r\n", enum_string(m_mip_filter));
}
if(m_content_type==texcontent_normal)
{
iop.setNormalMap(true);
iop.setNormalizeMipmaps(true);
}
switch(resize_mode_)
{
case texresize_none: iop.setRoundMode(RoundMode_None); break;
case texresize_prev_pow2: iop.setRoundMode(RoundMode_ToPreviousPowerOfTwo); break;
case texresize_next_pow2: iop.setRoundMode(RoundMode_ToNextPowerOfTwo); break;
case texresize_nearest_pow2: iop.setRoundMode(RoundMode_ToNearestPowerOfTwo); break;
default: PFC_ERRORF("Texture resize mode \"%s\" not supported\r\n", enum_string(resize_mode_));
}
iop.setTextureLayout(TextureType_2D, width_, height_);
iop.setMipmapData(source_, width_, height_);
iop.setGamma(m_gamma_input, m_gamma_output);
//==========================================================================
// output_handler
//==========================================================================
struct output_handler: OutputHandler
{
output_handler(pair<void*, unsigned> *targets_, unsigned full_scan_bytes_)
{
targets=targets_;
full_scan_bytes=full_scan_bytes_;
scan_bytes=0;
scan_pitch=0;
scan_start=0;
scan_end=0;
scan_cursor=0;
}
//------------------------------------------------------------------------
virtual void beginImage(int size_, int width_, int height_, int depth_, int face_, int mip_level_)
{
// init mip level
PFC_ASSERT(mip_level_<16);
scan_bytes=full_scan_bytes>>mip_level_;
scan_pitch=targets[mip_level_].second;
scan_start=targets[mip_level_].first;
scan_end=(uint8_t*)scan_start+scan_bytes;
scan_cursor=scan_start;
}
//------------------------------------------------------------------------
virtual void endImage()
{
}
//------------------------------------------------------------------------
virtual bool writeData(const void *data_, int size_)
{
// ignore non-image data
if(!scan_start)
return true;
// copy data
do
{
unsigned num_bytes=min(int((uint8_t*)scan_end-(uint8_t*)scan_cursor), size_);
mem_copy(scan_cursor, data_, num_bytes);
size_-=num_bytes;
(uint8_t*&)data_+=num_bytes;
(uint8_t*&)scan_cursor+=num_bytes;
PFC_ASSERT(scan_cursor<=scan_end);
if(scan_cursor==scan_end)
{
// restart scan
(uint8_t*&)scan_start+=scan_pitch;
scan_end=(uint8_t*)scan_start+scan_bytes;
scan_cursor=scan_start;
}
} while(size_);
return true;
}
//------------------------------------------------------------------------
pair<void*, unsigned> *targets;
unsigned full_scan_bytes;
unsigned scan_bytes;
unsigned scan_pitch;
void *scan_start;
void *scan_end;
void *scan_cursor;
};
//--------------------------------------------------------------------------
// setup target format
CompressionOptions cop;
unsigned full_scan_size=0, width=texsize(width_, resize_mode_);
switch(texfmt_type(m_target_format))
{
// BC-format target type
case texfmttype_bc:
{
// set BC-format target format
width=(width+3)&-4;
switch(m_target_format)
{
case texfmt_bc1: cop.setFormat(Format_BC1); full_scan_size=width*2; break;
case texfmt_bc1a: cop.setFormat(Format_BC1a); full_scan_size=width*2; break;
case texfmt_bc2: cop.setFormat(Format_BC2); full_scan_size=width*4; break;
case texfmt_bc3: cop.setFormat(Format_BC3); full_scan_size=width*4; break;
case texfmt_bc6h: cop.setFormat(Format_BC6); full_scan_size=width*4; break;
case texfmt_bc7: cop.setFormat(Format_BC7); full_scan_size=width*4; break;
default: PFC_ERROR("Unsupported target BC-format\r\n");
}
} break;
// uint RGBA target type
case texfmttype_rgba:
{
// set RGBA target format
unsigned target_bpp=texfmt_bpp(m_target_format);
full_scan_size=width*target_bpp/8;
uint32_t rmask, gmask, bmask, amask;
texfmt_rgba_mask32(m_target_format, rmask, gmask, bmask, amask);
cop.setFormat(Format_RGB);
cop.setPixelFormat(target_bpp, rmask, gmask, bmask, amask);
} break;
// RGBA 16-bit float target type
case texfmttype_rgba16f:
{
PFC_ERROR_NOT_IMPL();
/*todo*/
} break;
// RGBA 32-bit float target type
case texfmttype_rgba32f:
{
PFC_ERROR_NOT_IMPL();
/*todo*/
} break;
// Unsupported
default: PFC_ERRORF("Unsupported target format type \"%s\"\r\n", enum_string(m_target_format));
}
// setup compression quality
switch(m_compression_quality)
{
case texcq_fastest: cop.setQuality(Quality_Fastest); break;
case texcq_normal: cop.setQuality(Quality_Normal); break;
case texcq_production: cop.setQuality(Quality_Production); break;
case texcq_highest: cop.setQuality(Quality_Highest); break;
default: PFC_ERRORF("Unsupported texture compression quality setting \"%s\"\r\n", enum_string(m_compression_quality));
}
// setup output options
OutputOptions oop;
oop.setContainer(Container_DDS10);
output_handler oh(m_targets, full_scan_size);
oop.setOutputHandler(&oh);
// compress the texture & generate mipmaps
Compressor c;
c.enableCudaAcceleration(s_is_cuda_texture_compression);
c.process(iop, cop, oop);
#else
PFC_ERROR("Unable to perform texture conversion without NVIDIA Texture Tools library\r\n");
#endif
}
//----------------------------------------------------------------------------
//============================================================================
// tex2d_base
//============================================================================
PFC_CLASS_DEF(tex2d_base);
//----------------------------------------------------------------------------
void tex2d_base::init(unsigned width_, unsigned height_, int num_mips_, e_texture_format format_, e_texture_color_space color_space_, const mip_data*, unsigned array_size_)
{
// setup texture properties
m_format=format_;
m_color_space=color_space_;
m_num_mips=format_?texture_mips_2d(width_, height_, format_, num_mips_):num_mips_;
m_width=width_;
m_height=height_;
m_source_width=width_;
m_source_height=height_;
m_array_size=array_size_;
}
//----
void tex2d_base::load(bin_input_stream_base &s_, const texture_loader_params ¶ms_)
{
texture_loader tl(s_);
load(tl, params_);
}
//----
void tex2d_base::swap(tex2d_base &tex_)
{
// swap 2d texture contents
pfc::swap(m_format, tex_.m_format);
pfc::swap(m_color_space, tex_.m_color_space);
pfc::swap(m_num_mips, tex_.m_num_mips);
pfc::swap(m_width, tex_.m_width);
pfc::swap(m_height, tex_.m_height);
pfc::swap(m_array_size, tex_.m_array_size);
pfc::swap(m_source_height, tex_.m_source_height);
pfc::swap(m_source_width, tex_.m_source_width);
}
//----------------------------------------------------------------------------
//============================================================================
// tex3d_base
//============================================================================
PFC_CLASS_DEF(tex3d_base);
//----------------------------------------------------------------------------
void tex3d_base::init(unsigned width_, unsigned height_, unsigned depth_, int num_mips_, e_texture_format format_, e_texture_color_space color_space_, const mip_data*)
{
// setup volume texture properties
m_format=format_;
m_color_space=color_space_;
m_num_mips=format_?texture_mips_3d(width_, height_, depth_, format_, num_mips_):num_mips_;
m_width=width_;
m_height=height_;
m_depth=depth_;
m_source_width=width_;
m_source_height=height_;
m_source_depth=depth_;
}
//----
void tex3d_base::load(bin_input_stream_base &s_, const texture_loader_params ¶ms_)
{
texture_loader tl(s_);
load(tl, params_);
}
//----
void tex3d_base::swap(tex3d_base &tex_)
{
// swap 3d texture contents
pfc::swap(m_format, tex_.m_format);
pfc::swap(m_color_space, tex_.m_color_space);
pfc::swap(m_num_mips, tex_.m_num_mips);
pfc::swap(m_width, tex_.m_width);
pfc::swap(m_height, tex_.m_height);
pfc::swap(m_depth, tex_.m_depth);
pfc::swap(m_source_width, tex_.m_source_width);
pfc::swap(m_source_height, tex_.m_source_height);
pfc::swap(m_source_depth, tex_.m_source_depth);
}
//----------------------------------------------------------------------------
//============================================================================
// texcube_base
//============================================================================
PFC_CLASS_DEF(texcube_base);
//----------------------------------------------------------------------------
void texcube_base::init(unsigned edge_len_, int num_mips_, e_texture_format format_, e_texture_color_space color_space_, const mip_data*)
{
// setup cube texture properties
m_format=format_;
m_color_space=color_space_;
m_num_mips=format_?texture_mips_2d(edge_len_, edge_len_, format_, num_mips_):num_mips_;
m_edge_len=edge_len_;
m_source_edge_len=edge_len_;
}
//----
void texcube_base::load(bin_input_stream_base &s_, const texture_loader_params ¶ms_)
{
texture_loader tl(s_);
load(tl, params_);
}
//----
void texcube_base::swap(texcube_base &tex_)
{
// swap cube texture contents
pfc::swap(m_format, tex_.m_format);
pfc::swap(m_color_space, tex_.m_color_space);
pfc::swap(m_num_mips, tex_.m_num_mips);
pfc::swap(m_edge_len, tex_.m_edge_len);
pfc::swap(m_source_edge_len, tex_.m_source_edge_len);
}
//----------------------------------------------------------------------------
//============================================================================
// cpu_tex2d
//============================================================================
PFC_CLASS_DEF(cpu_tex2d);
PFC_INTROSPEC_CPP_DEF(cpu_tex2d)
{
PFC_CUSTOM_STREAMING(0);
switch(unsigned(PE::pe_type))
{
case penum_input:
{
// release old texture resources and load texture data
PFC_MEM_FREE(m_mip_data);
m_mip_data=0;
unsigned tex_size=texture_size_2d(m_width, m_height, m_format, m_num_mips)*m_array_size;
m_mip_data=PFC_MEM_ALLOC(tex_size);
pe_.data(m_mip_data, tex_size);
} break;
case penum_output:
{
// save texture data
if(m_width && m_height)
{
unsigned tex_size=texture_size_2d(m_width, m_height, m_format, m_num_mips)*m_array_size;