-
Notifications
You must be signed in to change notification settings - Fork 1
/
TextureConversions.cpp
1300 lines (1069 loc) · 40.1 KB
/
TextureConversions.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
/* Version: MPL 1.1/LGPL 3.0
*
* "The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is the Oblivion Graphics Extender, short OBGE.
*
* The Initial Developer of the Original Code is
* Ethatron <[email protected]>. Portions created by The Initial
* Developer are Copyright (C) 2011 The Initial Developer.
* All Rights Reserved.
*
* Contributor(s):
* Timeslip (Version 1)
* scanti (Version 2)
* IlmrynAkios (Version 3)
*
* Alternatively, the contents of this file may be used under the terms
* of the GNU Library General Public License Version 3 license (the
* "LGPL License"), in which case the provisions of LGPL License are
* applicable instead of those above. If you wish to allow use of your
* version of this file only under the terms of the LGPL License and not
* to allow others to use your version of this file under the MPL,
* indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL License.
* If you do not delete the provisions above, a recipient may use your
* version of this file under either the MPL or the LGPL License."
*/
#ifndef OBGE_NOSHADER
#include <d3d9.h>
#include <d3dx9.h>
#include "D3D9.hpp"
#include "D3D9Device.hpp"
#include "TextureConversions.h"
#define STB_DXT_IMPLEMENTATION
#include "TextureConversions-stb_dxt_104.h"
#include "TextureConversions-squish.h"
#define fabs(x) ((x) >= 0 ? (x) : -(x))
inline int rint(float n) {
return max(min((int)floor(n + 0.5f), 255.0f), 0.0f);
//if (n >= 0.0f)
// return max((int)floor(n + 0.5f), 255);
//if (n <= 0.0f)
// return min((int) ceil(n - 0.5f), 0);
}
/* AWSOME:
* http://aras-p.info/texts/CompactNormalStorage.html
*/
#define MIPMAP_MINIMUM 1 // 4
/* make normals occupy the full[0,255] color-cube */
#define NORMALS_CUBESPACE
/* http://www.gamedev.net/topic/539608-mip-mapping-normal-maps/page__whichpage__1%25EF%25BF%25BD */
#define NORMALS_SCALEBYLEVEL 2
/* different ways of encoding the normals */
#undef NORMALS_INTEGER
#undef NORMALS_FLOAT_XYZ
#undef NORMALS_FLOAT_XYZ_TANGENTSPACE
#undef NORMALS_FLOAT_XY_TANGENTSPACE
#define NORMALS_FLOAT_DXDY_TANGENTSPACE 0.5f
/* http://diaryofagraphicsprogrammer.blogspot.com/2009/01/partial-derivative-normal-maps.html
*
* The idea is to store the paritial derivate of the normal in two channels of the map like this
*
* dx = (-nx/nz);
* dy = (-ny/nz);
*
* Then you can reconstruct the normal like this:
*
* nx = -dx;
* ny = -dy;
* nz = 1;
* normalize(n);
*
* The advantage is that you do not have to reconstruct Z, so you can skip one instruction in
* each pixel shader that uses normal maps.
*/
#undef NORMALS_FLOAT_AZ_TANGENTSPACE
/* http://www.gamedev.net/topic/535230-storing-normals-as-spherical-coordinates/
*
* Encode:
* return (float2(atan2(nrmNorm.y, nrmNorm.x) / M_PI, nrmNorm.z) + 1.0f) * 0.5f;
*
* Decode:
* float2 spGPUAngles = spherical.xy * 2.0f - 1.0f;
* float2 sincosTheta; sincos(spGPUAngles.x * M_PI, sincosTheta.x, sincosTheta.y);
* float2 sincosPhi = float2(sqrt(1.0f - spGPUAngles.y * spGPUAngles.y), spGPUAngles.y);
*
* return float3(sincosTheta.y * sincosPhi.x, sincosTheta.x * sincosPhi.x, sincosPhi.y);
*
* Storing z instead of acos(z) just saves some ops the decoding, you still need sin(phi) and cos(phi) to reconstruct XY. You just happen to already have cos(acos(z)), and the trig identity: '1 - sin(x)^2 = cos(x)^2' does the rest.
*
* Edit:
* Didn't answer the question I guess. You are seeing odd results because the conversion back from spherical is missing a step. You are computing sincos(theta) but not sincos(phi). The reason why I say store just normal.z and not acos(normal.z) is because the length of the vector is 1.0f (did I mention this method of encode/decode only works on normalized vectors) and so doing acos(normal.z/1) and then recovering cos(acos(normal.z/1)) is a very silly thing to do. Instead I store normal.z, and then compute sin(phi) by using the law of sines.
*/
#define ACCUMODE_LINEAR ( 0 << 0) // RGBH
#define ACCUMODE_GAMMA ( 1 << 0) // RGBH
#define ACCUMODE_FLAT ( 0 << 0) // XYZD
#define ACCUMODE_SCALE ( 1 << 0) // XYZD
#define NORMMODE_LINEAR ( 0 << 0) // RGBH
#define NORMMODE_GAMMA ( 1 << 0) // RGBH
#define TRGTNORM_CUBESPACE ( 1 << 0) // XYZD
#define TRGTMODE_CODING (15 << 1)
#define TRGTMODE_CODING_RGB ( 0 << 1)
#define TRGTMODE_CODING_XYZt ( 1 << 1)
#define TRGTMODE_CODING_XYt ( 2 << 1)
#define TRGTMODE_CODING_DXDYt ( 3 << 1)
#define TRGTMODE_CODING_DXDYDZt ( 4 << 1)
#define TRGTMODE_CODING_AZt ( 5 << 1)
#define TRGTMODE_CODING_XYZ ( 7 << 1)
#define TRGTMODE_CODING_XY ( 8 << 1)
/* ####################################################################################
*/
template<int mode>
static void AccuRGBH(long *bs, ULONG b, int level, int l) {
/* seperate the channels and build the sum */
bs[0] += (b >> 24) & 0xFF; /*h*/
bs[1] += (b >> 16) & 0xFF; /*b*/
bs[2] += (b >> 8) & 0xFF; /*g*/
bs[3] += (b >> 0) & 0xFF; /*r*/
}
template<int mode>
static void AccuRGBM(long *bs, ULONG b, int level, int l) {
/* seperate the channels and build the sum */
bs[0] = max(bs[0], (long)
(b >> 24) & 0xFF); /*h*/
bs[1] += (b >> 16) & 0xFF; /*b*/
bs[2] += (b >> 8) & 0xFF; /*g*/
bs[3] += (b >> 0) & 0xFF; /*r*/
}
template<int mode>
static void AccuRGBH(float *bs, ULONG b, int level, int l) {
/* seperate the channels and build the sum */
bs[0] += ((b >> 24) & 0xFF) ; /*h*/
bs[1] += powf((float)((b >> 16) & 0xFF) / 0xFF, 2.2f); /*b*/
bs[2] += powf((float)((b >> 8) & 0xFF) / 0xFF, 2.2f); /*g*/
bs[3] += powf((float)((b >> 0) & 0xFF) / 0xFF, 2.2f); /*r*/
}
template<int mode>
static void AccuRGBM(float *bs, ULONG b, int level, int l) {
/* seperate the channels and build the sum */
bs[0] = max(bs[0],
((b >> 24) & 0xFF) ); /*h*/
bs[1] += powf((float)((b >> 16) & 0xFF) / 0xFF, 2.2f); /*b*/
bs[2] += powf((float)((b >> 8) & 0xFF) / 0xFF, 2.2f); /*g*/
bs[3] += powf((float)((b >> 0) & 0xFF) / 0xFF, 2.2f); /*r*/
}
template<int mode>
static void AccuXYZD(long *ns, ULONG n, int level, int l) {
long vec[4];
vec[0] = ((n >> 24) & 0xFF) - 0x00; /*d[ 0,1]*/
vec[1] = ((n >> 16) & 0xFF) - 0x80; /*z[-1,1]*/
vec[2] = ((n >> 8) & 0xFF) - 0x80; /*y[-1,1]*/
vec[3] = ((n >> 0) & 0xFF) - 0x80; /*x[-1,1]*/
if (mode & ACCUMODE_SCALE) {
/* lower z (heighten the virtual displacement) every level */
vec[1] *= (level * NORMALS_SCALEBYLEVEL) - l;
vec[1] /= (level * NORMALS_SCALEBYLEVEL);
}
ns[0] += vec[0];
ns[1] += vec[1];
ns[2] += vec[2];
ns[3] += vec[3];
}
template<int mode>
static void AccuXYZD(float *nn, ULONG n, int level, int l) {
float vec[4], len;
vec[0] = (float)((n >> 24) & 0xFF);
vec[1] = (float)((n >> 16) & 0xFF); vec[1] /= 0xFF; vec[1] -= 0.5f; vec[1] /= 0.5f;
vec[2] = (float)((n >> 8) & 0xFF); vec[2] /= 0xFF; vec[2] -= 0.5f; vec[2] /= 0.5f;
vec[3] = (float)((n >> 0) & 0xFF); vec[3] /= 0xFF; vec[3] -= 0.5f; vec[3] /= 0.5f;
if (mode & ACCUMODE_SCALE) {
/* lower z (heighten the virtual displacement) every level */
vec[1] *= (level * NORMALS_SCALEBYLEVEL) - l;
vec[1] /= (level * NORMALS_SCALEBYLEVEL);
}
len = sqrt(vec[1] * vec[1] + vec[2] * vec[2] + vec[3] * vec[3]);
vec[1] /= len;
vec[2] /= len;
vec[3] /= len;
nn[0] += vec[0];
nn[1] += vec[1];
nn[2] += vec[2];
nn[3] += vec[3];
}
template<int mode>
static void AccuXYCD(long *nn, ULONG n, int level, int l) {
abort();
}
template<int mode>
static void AccuXYCD(float *nn, ULONG n, int level, int l) {
float vec[5], len;
vec[0] = (float)((n >> 24) & 0xFF);
vec[1] = (float)((n >> 16) & 0xFF);
vec[2] = (float)((n >> 8) & 0xFF); vec[2] /= 0xFF; vec[2] -= 0.5f; vec[2] /= 0.5f;
vec[3] = (float)((n >> 0) & 0xFF); vec[3] /= 0xFF; vec[3] -= 0.5f; vec[3] /= 0.5f;
vec[4] = sqrt(1.0f - min(1.0f, vec[2] * vec[2] + vec[3] * vec[3]));
if (mode & ACCUMODE_SCALE) {
/* lower z (heighten the virtual displacement) every level */
vec[4] *= (level * NORMALS_SCALEBYLEVEL) - l;
vec[4] /= (level * NORMALS_SCALEBYLEVEL);
}
len = sqrt(vec[4] * vec[4] + vec[2] * vec[2] + vec[3] * vec[3]);
vec[2] /= len;
vec[3] /= len;
vec[4] /= len;
nn[0] += vec[0];
nn[1] += vec[1];
nn[2] += vec[2];
nn[3] += vec[3];
nn[4] += vec[4];
}
/* ####################################################################################
*/
template<int mode>
static void NormRGBH(long *obs, long *bs, int av) {
/* build average of each channel an join */
obs[0] = bs[0] / av; /*h*/
obs[1] = bs[1] / av; /*b*/
obs[2] = bs[2] / av; /*g*/
obs[3] = bs[3] / av; /*r*/
}
template<int mode>
static void NormRGBM(long *obs, long *bs, int av) {
/* build average of each channel an join */
obs[0] = bs[0] ; /*h*/
obs[1] = bs[1] / av; /*b*/
obs[2] = bs[2] / av; /*g*/
obs[3] = bs[3] / av; /*r*/
}
template<int mode>
static void NormRGBH(float *obs, float *bs, int av) {
/* build average of each channel an join */
obs[0] = (bs[0] / av ) ; /*d[ 0,1]*/
obs[1] = powf(bs[1] / av, 1.0f / 2.2f) * 0xFF; /*z[-1,1]*/
obs[2] = powf(bs[2] / av, 1.0f / 2.2f) * 0xFF; /*y[-1,1]*/
obs[3] = powf(bs[3] / av, 1.0f / 2.2f) * 0xFF; /*x[-1,1]*/
}
template<int mode>
static void NormRGBM(float *obs, float *bs, int av) {
/* build average of each channel an join */
obs[0] = bs[0] ; /*h*/
obs[1] = powf(bs[1] / av, 1.0f / 2.2f) * 0xFF; /*b*/
obs[2] = powf(bs[2] / av, 1.0f / 2.2f) * 0xFF; /*g*/
obs[3] = powf(bs[3] / av, 1.0f / 2.2f) * 0xFF; /*r*/
}
template<int mode>
static void NormXYZD(long *ons, long *ns, int av) {
ons[0] = ns[0] / av; /*d[ 0,1]*/
ons[1] = ns[1] / av; /*z[-1,1]*/
ons[2] = ns[2] / av; /*y[-1,1]*/
ons[3] = ns[3] / av; /*x[-1,1]*/
}
template<int mode>
static void NormXYZD(float *onn, float *nn, int av) {
float len;
len = sqrt(nn[1] * nn[1] + nn[2] * nn[2] + nn[3] * nn[3]);
onn[0] = nn[0] / av; /*d[ 0,1]*/
onn[1] = nn[1] / len; /*z[-1,1]*/
onn[2] = nn[2] / len; /*y[-1,1]*/
onn[3] = nn[3] / len; /*x[-1,1]*/
}
template<int mode>
static void NormXYCD(long *onn, long *nn, int av) {
abort();
}
template<int mode>
static void NormXYCD(float *onn, float *nn, int av) {
float len;
len = sqrt(nn[4] * nn[4] + nn[2] * nn[2] + nn[3] * nn[3]);
onn[0] = nn[0] / av; /*d[ 0,1]*/
onn[1] = nn[1] / av; /*c[-1,1]*/
onn[2] = nn[2] / len; /*y[-1,1]*/
onn[3] = nn[3] / len; /*x[-1,1]*/
//onn[4] = nn[4] / len; /*z[-1,1]*/
}
/* ####################################################################################
*/
template<int mode>
static void LookRGBH(long *bs, long *br) {
/* collect magnitudes */
br[0] = max(bs[0], br[1]); /*h*/
br[1] = max(bs[1], br[1]); /*b*/
br[2] = max(bs[2], br[2]); /*g*/
br[3] = max(bs[3], br[3]); /*r*/
}
template<int mode>
void LookRGBH(float *bs, float *br) {
/* collect magnitudes */
br[0] = max(bs[0], br[1]); /*h*/
br[1] = max(bs[1], br[1]); /*b*/
br[2] = max(bs[2], br[2]); /*g*/
br[3] = max(bs[3], br[3]); /*r*/
}
template<int mode>
static void LookXYZD(long *ns, long *nr) {
/* collect magnitudes */
nr[1] = max(abs(ns[1]), nr[1]); /*z[-1,1]*/
nr[2] = max(abs(ns[2]), nr[2]); /*y[-1,1]*/
nr[3] = max(abs(ns[3]), nr[3]); /*x[-1,1]*/
}
template<int mode>
static void LookXYZD(float *nn, float *nr) {
if ((mode & TRGTMODE_CODING) == TRGTMODE_CODING_DXDYDZt) {
/* calculate maximum partial derivative */
float rel = (
max(
fabs(nn[2]),
fabs(nn[3])
)
/ fabs(nn[1])
);
/* collect magnitudes */
nr[1] = max( rel , nr[1]); /*r[ 0,inf]*/
nr[2] = max(fabs(nn[2]), nr[2]); /*y[-1,1]*/
nr[3] = max(fabs(nn[3]), nr[3]); /*x[-1,1]*/
}
else if ((mode & TRGTMODE_CODING) != TRGTMODE_CODING_XYZ) {
/* collect magnitudes */
nr[1] = max(fabs(nn[1]), nr[1]); /*z[-1,1]*/
nr[2] = max(fabs(nn[2]), nr[2]); /*y[-1,1]*/
nr[3] = max(fabs(nn[3]), nr[3]); /*x[-1,1]*/
}
}
template<int mode>
static void LookXYCD(long *nn, long *nr) {
abort();
}
template<int mode>
static void LookXYCD(float *nn, float *nr) {
/* collect magnitudes */
nr[2] = max(abs(nn[2]), nr[2]); /*y[-1,1]*/
nr[3] = max(abs(nn[3]), nr[3]); /*x[-1,1]*/
}
/* ####################################################################################
*/
template<int mode>
static ULONG JoinRGBH(long *bs, long *br) {
ULONG b = 0;
/* build average of each channel an join */
b |= (bs[0] << 24); /*h*/
b |= (bs[1] << 16); /*b*/
b |= (bs[2] << 8); /*g*/
b |= (bs[3] << 0); /*r*/
return b;
}
template<int mode>
static ULONG JoinRGBH(float *bs, float *br) {
ULONG b = 0;
/* build average of each channel an join */
b |= (rint(bs[0]) << 24); /*d[ 0,1]*/
b |= (rint(bs[1]) << 16); /*z[-1,1]*/
b |= (rint(bs[2]) << 8); /*y[-1,1]*/
b |= (rint(bs[3]) << 0); /*x[-1,1]*/
return b;
}
template<int mode>
static ULONG JoinXYZD(long *ns, long *nr) {
ULONG n = 0;
n |= ((ns[0] + 0x00)) << 24; /*d[ 0,1]*/
n |= ((ns[1]) << 16) + 0x80; /*z[-1,1]*/
n |= ((ns[2]) << 8) + 0x80; /*y[-1,1]*/
n |= ((ns[3]) << 0) + 0x80; /*x[-1,1]*/
return n;
}
template<int mode>
static ULONG JoinXYZD(float *nn, float *nr) {
ULONG n = 0;
float vec[4], len;
float derivb = NORMALS_FLOAT_DXDY_TANGENTSPACE; // [0.5f,1.0f]
vec[0] = nn[0];
vec[1] = nn[1];
vec[2] = nn[2];
vec[3] = nn[3];
/* ################################################################# */
if (((mode & TRGTMODE_CODING) == TRGTMODE_CODING_DXDYt) ||
((mode & TRGTMODE_CODING) == TRGTMODE_CODING_DXDYDZt)) {
if ((mode & TRGTMODE_CODING) == TRGTMODE_CODING_DXDYDZt) {
/* get the minimum divider we have to support
* based on the vectors in the 4x4 block
*
* this will select a constant z-multiplier over
* the full 4x4 block with that we calculate the
* partial derivative:
*
* x / z * multiplier;
* y / z * multiplier;
* z / z * multiplier;
*/
float derivx = 1.0f / nr[1]; // [...,1.0f]
if (derivb < derivx)
derivb = derivx;
if (derivb > 1.0f)
derivb = 1.0f;
}
#if 0
vec[1] =
max(
fabs(vec[1]),
max(
fabs(vec[2]) * derivb,
fabs(vec[3]) * derivb
));
#else
float up = derivb *
max(
fabs(vec[2]),
fabs(vec[3])
)
/ fabs(vec[1]);
if (up > 1.0f) {
vec[2] /= up;
vec[3] /= up;
}
#endif
}
else if ((mode & TRGTMODE_CODING) != TRGTMODE_CODING_XYZ) {
vec[1] = max(0.0f, vec[1]);
}
/* ################################################################# */
if ((mode & TRGTMODE_CODING) == TRGTMODE_CODING_XYZ) {
if (mode & TRGTNORM_CUBESPACE)
len = max(vec[1], max(vec[2], vec[3]));
else
len = sqrt(vec[1] * vec[1] + vec[2] * vec[2] + vec[3] * vec[3]);
vec[1] /= len; vec[1] *= 0.5f; vec[1] += 0.5f; vec[1] *= 0xFF;
vec[2] /= len; vec[2] *= 0.5f; vec[2] += 0.5f; vec[2] *= 0xFF;
vec[3] /= len; vec[3] *= 0.5f; vec[3] += 0.5f; vec[3] *= 0xFF;
}
/* ################################################################# */
else if ((mode & TRGTMODE_CODING) == TRGTMODE_CODING_XYZt) {
if (mode & TRGTNORM_CUBESPACE)
len = max(vec[1], max(vec[2], vec[3]));
else
len = sqrt(vec[1] * vec[1] + vec[2] * vec[2] + vec[3] * vec[3]);
vec[1] /= len; vec[1] *= 1.0f; vec[1] += 0.0f; vec[1] *= 0xFF;
vec[2] /= len; vec[2] *= 0.5f; vec[2] += 0.5f; vec[2] *= 0xFF;
vec[3] /= len; vec[3] *= 0.5f; vec[3] += 0.5f; vec[3] *= 0xFF;
}
/* ################################################################# */
else if ((mode & TRGTMODE_CODING) == TRGTMODE_CODING_XYt) {
if (mode & TRGTNORM_CUBESPACE) {
float lnn;
len = sqrt(vec[1] * vec[1] + vec[2] * vec[2] + vec[3] * vec[3]);
lnn = sqrt(vec[2] * vec[2] + vec[3] * vec[3]);
float factor = (2.0f - max(vec[2] / lnn, vec[3] / lnn)) / len;
vec[1] = 1.0f;
vec[2] *= factor; vec[2] *= 0.5f; vec[2] += 0.5f; vec[2] *= 0xFF;
vec[3] *= factor; vec[3] *= 0.5f; vec[3] += 0.5f; vec[3] *= 0xFF;
}
else {
len = sqrt(vec[1] * vec[1] + vec[2] * vec[2] + vec[3] * vec[3]);
vec[1] = 1.0f;
vec[2] /= len; vec[2] *= 0.5f; vec[2] += 0.5f; vec[2] *= 0xFF;
vec[3] /= len; vec[3] *= 0.5f; vec[3] += 0.5f; vec[3] *= 0xFF;
}
}
/* ################################################################# */
else if (((mode & TRGTMODE_CODING) == TRGTMODE_CODING_DXDYt) ||
((mode & TRGTMODE_CODING) == TRGTMODE_CODING_DXDYDZt)) {
if (mode & TRGTNORM_CUBESPACE) {
/* this format is a bit special */
len = vec[1] / derivb;
float lnn = sqrt(vec[2] * vec[2] + vec[3] * vec[3]);
float factor = (2.0f - max(vec[2] / lnn, vec[3] / lnn)) / len;
vec[1] /= len; vec[1] *= 0.5f; vec[1] += 0.5f; vec[1] *= 0xFF;
vec[2] *= factor; vec[2] *= 0.5f; vec[2] += 0.5f; vec[2] *= 0xFF;
vec[3] *= factor; vec[3] *= 0.5f; vec[3] += 0.5f; vec[3] *= 0xFF;
derivb *= 0.5f; derivb += 0.5f; derivb *= 0xFF;
#if 0
if (1) {
float chk[4], cln, fct;
chk[2] = ((vec[2] / 0xFF) - 0.5f) / 0.5f;
chk[3] = ((vec[3] / 0xFF) - 0.5f) / 0.5f;
cln = sqrt(chk[2] * chk[2] + chk[3] * chk[3]);
fct = 2.0f - max(chk[2] / cln, chk[3] / cln);
chk[1] = 1.0f * derivb;
chk[2] /= fct;
chk[3] /= fct;
cln = sqrt(chk[1] * chk[1] + chk[2] * chk[2] + chk[3] * chk[3]);
chk[1] /= cln;
chk[2] /= cln;
chk[3] /= cln;
cln = 1;
}
#endif
assert(fabs(vec[1] - derivb) < 0.001f);
}
else {
/* this format is fully compatible with the built-in shaders */
len = vec[1] / derivb;
vec[1] /= len; vec[1] *= 0.5f; vec[1] += 0.5f; vec[1] *= 0xFF;
vec[2] /= len; vec[2] *= 0.5f; vec[2] += 0.5f; vec[2] *= 0xFF;
vec[3] /= len; vec[3] *= 0.5f; vec[3] += 0.5f; vec[3] *= 0xFF;
derivb *= 0.5f; derivb += 0.5f; derivb *= 0xFF;
assert(fabs(vec[1] - derivb) < 0.001f);
}
}
/* ################################################################# */
else if ((mode & TRGTMODE_CODING) == TRGTMODE_CODING_AZt) {
float ang;
len = sqrt(vec[1] * vec[1] + vec[2] * vec[2] + vec[3] * vec[3]);
ang = atan2(vec[2], vec[3]) / (float)M_PI; vec[2] = ang;
vec[1] *= 1.0f; vec[1] += 0.0f; vec[1] *= 0xFF;
vec[2] *= 0.5f; vec[2] += 0.5f; vec[3] *= 0xFF;
vec[3] = 1.0f;
}
n |= (rint(vec[0]) << 24); /*d[ 0,1]*/
n |= (rint(vec[1]) << 16); /*z[-1,1]*/
n |= (rint(vec[2]) << 8); /*y[-1,1]*/
n |= (rint(vec[3]) << 0); /*x[-1,1]*/
return n;
}
template<int mode>
static ULONG JoinXYCD(long *nn, long *nr) {
abort(); return 0;
}
template<int mode>
static ULONG JoinXYCD(float *nn, float *nr) {
ULONG n = 0;
float vec[5], len;
vec[0] = nn[0];
vec[1] = nn[1];
vec[2] = nn[2];
vec[3] = nn[3];
vec[4] = sqrt(1.0f - min(1.0f, vec[2] * vec[2] + vec[3] * vec[3]));
len = sqrt(vec[4] * vec[4] + vec[2] * vec[2] + vec[3] * vec[3]);
vec[2] /= len; vec[2] *= 0.5f; vec[2] += 0.5f; vec[2] *= 0xFF;
vec[3] /= len; vec[3] *= 0.5f; vec[3] += 0.5f; vec[3] *= 0xFF;
vec[4] /= len; vec[4] *= 0.5f; vec[4] += 0.5f; vec[4] *= 0xFF;
n |= (rint(vec[0]) << 24); /*d[ 0,1]*/
n |= (rint(vec[1]) << 16); /*c[-1,1]*/
n |= (rint(vec[2]) << 8); /*y[-1,1]*/
n |= (rint(vec[3]) << 0); /*x[-1,1]*/
return n;
}
/* ####################################################################################
*/
static bool TextureConvert(D3DSURFACE_DESC &info, LPDIRECT3DTEXTURE9 *tex) {
LPDIRECT3DTEXTURE9 replct;
LPDIRECT3DSURFACE9 stex, srep;
HRESULT res;
//lastOBGEDirect3DDevice9->CreateTexture(info.Width, info.Height, 0, 0, D3DFMT_A8B8G8R8, D3DPOOL_SYSTEMMEM, &replct, NULL);
//lastOBGEDirect3DDevice9->CreateTexture(info.Width, info.Height, 0, 0, D3DFMT_A8B8G8R8, D3DPOOL_DEFAULT, &replct, NULL);
//lastOBGEDirect3DDevice9->CreateTexture(info.Width, info.Height, 1, 0, D3DFMT_A8B8G8R8, D3DPOOL_DEFAULT, &replct, NULL);
//lastOBGEDirect3DDevice9->CreateTexture(info.Width, info.Height, 1, D3DUSAGE_AUTOGENMIPMAP, D3DFMT_A8B8G8R8, D3DPOOL_DEFAULT, &replct, NULL);
//lastOBGEDirect3DDevice9->CreateTexture(info.Width, info.Height, 1, D3DUSAGE_AUTOGENMIPMAP, D3DFMT_A8B8G8R8, D3DPOOL_DEFAULT, &replct, NULL);
//lastOBGEDirect3DDevice9->CreateTexture(info.Width, info.Height, 1, D3DUSAGE_AUTOGENMIPMAP, D3DFMT_A8B8G8R8, D3DPOOL_SYSTEMMEM, &replct, NULL);
lastOBGEDirect3DDevice9->CreateTexture(info.Width, info.Height, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &replct, NULL);
if (!replct)
return false;
(*tex)->GetSurfaceLevel(0, &stex);
replct->GetSurfaceLevel(0, &srep);
res = D3DXLoadSurfaceFromSurface(srep, NULL, NULL, stex, NULL, NULL, D3DX_FILTER_NONE, 0);
stex->Release();
srep->Release();
if (res == D3D_OK) {
(*tex)->Release();
(*tex) = replct;
return true;
}
else {
replct->Release();
replct = (*tex);
return false;
}
}
/* ####################################################################################
*/
#define D3DFMT_ATI1 ((D3DFORMAT)MAKEFOURCC('A', 'T', 'I', '1'))
#define D3DFMT_ATI2 ((D3DFORMAT)MAKEFOURCC('A', 'T', 'I', '2'))
#include "GUIs_DebugWindow.hpp"
#define TCOMPRESS_A 0
#define TCOMPRESS_LA 1
#define TCOMPRESS_RGB 2
#define TCOMPRESS_RGBA 3
#define TCOMPRESS_RGBH 4
#define TCOMPRESS_COLOR(fmt) (((fmt) >= TCOMPRESS_A) && ((fmt) <= TCOMPRESS_RGBH))
#define TCOMPRESS_TRANS(fmt) (((fmt) == TCOMPRESS_LA) || ((fmt) == TCOMPRESS_RGBA))
#define TCOMPRESS_xyZ 5
#define TCOMPRESS_XY 6
#define TCOMPRESS_XYz 7
#define TCOMPRESS_xyz 8
#define TCOMPRESS_xyzD 9
#define TCOMPRESS_XYZ 10
#define TCOMPRESS_XYZD 11
#define TCOMPRESS_NINDEP(fmt) (((fmt) >= TCOMPRESS_xyZ) && ((fmt) <= TCOMPRESS_xyzD))
#define TCOMPRESS_NORMAL(fmt) (((fmt) >= TCOMPRESS_xyZ) && ((fmt) <= TCOMPRESS_XYZD))
static int TCOMPRESS_CHANNELS(int fmt) {
switch (fmt) {
case TCOMPRESS_A : return 1;
case TCOMPRESS_LA : return 2;
case TCOMPRESS_RGB : return 3;
case TCOMPRESS_RGBA : return 4;
case TCOMPRESS_RGBH : return 4;
case TCOMPRESS_XY : return 2;
case TCOMPRESS_xyZ : return 1;
case TCOMPRESS_XYz : return 2;
case TCOMPRESS_xyz : return 3;
case TCOMPRESS_xyzD : return 4;
case TCOMPRESS_XYZ : return 3;
case TCOMPRESS_XYZD : return 4;
}
return 0;
}
template<typename UTYPE, typename type, int format>
static bool TextureCompressDXT(LPDIRECT3DTEXTURE9 *tex) {
#ifdef OBGE_DEVLING
DebugWindow *dw = DebugWindow::Get();
#endif
LPDIRECT3DTEXTURE9 text = NULL;
D3DSURFACE_DESC texd, texo;
D3DLOCKED_RECT texr;
D3DLOCKED_RECT texs;
(*tex)->GetLevelDesc(0, &texo);
#if 0
/* Converts a height map into a normal map. The (x,y,z)
* components of each normal are mapped to the (r,g,t)
* channels of the output texture.
*/
HRESULT D3DXComputeNormalMap(
__out LPDIRECT3DTEXTURE9 pTexture,
__in LPDIRECT3DTEXTURE9 pSrcTexture,
__in const PALETTEENTRY *pSrcPalette,
__in DWORD Flags,
__in DWORD Channel,
__in FLOAT Amplitude
);
#endif
/* convert to ARGB8 (TODO: support at least the 16bit formats as well) */
if ((texo.Format != D3DFMT_A8R8G8B8) && !TextureConvert(texo, tex))
return false;
/* the lowest mip-level contains a row or a column of 4x4 blocks
* we won't generate mip-levels for mips smaller than the BTC-area
*/
int levels = 1;
int ww = texo.Width;
int hh = texo.Height;
while ((ww > MIPMAP_MINIMUM) && (hh > MIPMAP_MINIMUM)) {
ww = (ww + 1) >> 1;
hh = (hh + 1) >> 1;
levels++;
}
/* create the textures */
int flags = squish::kColourIterativeClusterFit;
switch (TCOMPRESS_CHANNELS(format)) {
case 4: flags |= squish::kDxt5; lastOBGEDirect3DDevice9->CreateTexture(texo.Width, texo.Height, levels, 0, D3DFMT_DXT5, D3DPOOL_SYSTEMMEM, &text, NULL); break;
case 3: flags |= squish::kDxt1; lastOBGEDirect3DDevice9->CreateTexture(texo.Width, texo.Height, levels, 0, D3DFMT_DXT1, D3DPOOL_SYSTEMMEM, &text, NULL); break;
case 2: flags |= squish::kDxt5; lastOBGEDirect3DDevice9->CreateTexture(texo.Width, texo.Height, levels, 0, D3DFMT_ATI2, D3DPOOL_SYSTEMMEM, &text, NULL); break;
case 1: flags |= squish::kDxt5; lastOBGEDirect3DDevice9->CreateTexture(texo.Width, texo.Height, levels, 0, D3DFMT_ATI1, D3DPOOL_SYSTEMMEM, &text, NULL); break;
}
/**/ if (TCOMPRESS_TRANS(format))
flags |= squish::kWeightColourByAlpha;
/**/ if (TCOMPRESS_COLOR(format))
flags |= squish::kColourMetricPerceptual;
else if (TCOMPRESS_NORMAL(format))
flags |= squish::kColourMetricUniform;
/* damit */
if (!text) {
if (text) text->Release();
return false;
}
(*tex)->LockRect(0, &texs, NULL, 0);
DWORD texl = text->GetLevelCount();
DWORD level = texl;
for (int l = 0; l < level; l++) {
/* square dimension of this surface-level */
/* square area of this surface-level */
int lv = (1 << l);
int av = lv * lv;
text->GetLevelDesc(l, &texd);
text->LockRect(l, &texr, NULL, 0);
ULONG *sTex = (ULONG *)texs.pBits;
ULONG *dTex = (ULONG *)texr.pBits;
/* loop over 4x4-blocks of this level (DXT5) */
for (int y = 0; y < texd.Height; y += 4) {
#ifdef OBGE_DEVLING
if (dw) dw->SetProgress(l, level, y, texd.Height);
#endif
for (int x = 0; x < texd.Width; x += 4) {
UTYPE bTex[2][4*4];
type fTex[2][4*4][4];
/* generate this level's 4x4-block from the original surface */
for (int ly = 0; ly < 4; ly += 1)
for (int lx = 0; lx < 4; lx += 1) {
int yl = ((y + ly) << l);
int xl = ((x + lx) << l);
type ts[8] = {0};
/* access all pixels this level's 4x4-block represents in
* the full dimension original surface (high quality mip-mapping)
*/
for (int oy = 0; oy < lv; oy += 1)
for (int ox = 0; ox < lv; ox += 1) {
/* assume tiling: wrap pixels around */
int posx = (xl + ox) % texo.Width;
int posy = (yl + oy) % texo.Height;
ULONG t = sTex[(posy * texo.Width) + posx];
/* read ARGB -> ABGR */
t = //t;
(((t >> 24) & 0xFF) << 24 /*h*/)
| (((t >> 16) & 0xFF) << 0 /*r*/)
| (((t >> 8) & 0xFF) << 8 /*g*/)
| (((t >> 0) & 0xFF) << 16 /*t*/);
/**/ if (TCOMPRESS_COLOR (format))
AccuRGBH<ACCUMODE_LINEAR>(ts, t, level, l);
else if (TCOMPRESS_XY == format)
AccuXYCD<ACCUMODE_SCALE >(ts, t, level, l);
else if (TCOMPRESS_NORMAL(format))
AccuXYZD<ACCUMODE_SCALE >(ts, t, level, l);
}
/* build average of each channel */
/**/ if (TCOMPRESS_COLOR (format))
NormRGBH<TRGTMODE_CODING_RGB >(fTex[0][(ly * 4) + lx], ts, av);
else if (TCOMPRESS_XY == format)
NormXYCD<TRGTMODE_CODING_XY >(fTex[0][(ly * 4) + lx], ts, av);
else if (TCOMPRESS_NINDEP(format))
NormXYZD<TRGTMODE_CODING_XYZ >(fTex[0][(ly * 4) + lx], ts, av);
else if (TCOMPRESS_NORMAL(format))
NormXYZD<TRGTMODE_CODING_DXDYDZt>(fTex[0][(ly * 4) + lx], ts, av);
}
type tr[4] = {0};
/* analyze this level's 4x4-block */
for (int ly = 0; ly < 4; ly += 1)
for (int lx = 0; lx < 4; lx += 1) {
/**/ if (TCOMPRESS_COLOR (format))
LookRGBH<TRGTMODE_CODING_RGB >(fTex[0][(ly * 4) + lx], tr);
else if (TCOMPRESS_XY == format)
LookXYCD<TRGTMODE_CODING_XY >(fTex[0][(ly * 4) + lx], tr);
else if (TCOMPRESS_NINDEP(format))
LookXYZD<TRGTMODE_CODING_XYZ >(fTex[0][(ly * 4) + lx], tr);
else if (TCOMPRESS_NORMAL(format))
LookXYZD<TRGTMODE_CODING_DXDYDZt>(fTex[0][(ly * 4) + lx], tr);
}
/* generate this level's 4x4-block from the original surface */
for (int ly = 0; ly < 4; ly += 1)
for (int lx = 0; lx < 4; lx += 1) {
/* build average of each channel an join */
ULONG t;
/**/ if (TCOMPRESS_COLOR (format))
t = JoinRGBH<TRGTMODE_CODING_RGB >(fTex[0][(ly * 4) + lx], tr);
else if (TCOMPRESS_XY == format)
t = JoinXYCD<TRGTMODE_CODING_XY >(fTex[0][(ly * 4) + lx], tr);
else if (TCOMPRESS_NINDEP(format))
t = JoinXYZD<TRGTMODE_CODING_XYZ >(fTex[0][(ly * 4) + lx], tr);
else if (TCOMPRESS_NORMAL(format))
t = JoinXYZD<TRGTMODE_CODING_DXDYDZt>(fTex[0][(ly * 4) + lx], tr);
/* write the result ABGR, BGR */
switch (TCOMPRESS_CHANNELS(format)) {
/* ABGR -> RGBA */
case 4: bTex[0][(ly * 4) + lx] = (t) | 0x00000000; break;
/* -BGR -> RGB- */
case 3: bTex[0][(ly * 4) + lx] = (t) | 0xFF000000; break;
/* --YX -> XY-- */
/* AL-- -> LA-- */
case 2: /**/ if (format == TCOMPRESS_LA) bTex[0][(ly * 4) + lx] = (t << 0) & 0xFF000000,
bTex[1][(ly * 4) + lx] = (t << 8) & 0xFF000000;
else bTex[0][(ly * 4) + lx] = (t << 16) & 0xFF000000,
bTex[1][(ly * 4) + lx] = (t << 24) & 0xFF000000;
break;
/* -Z-- -> Z--- */
/* A--- -> A--- */
case 1: /**/ if (format == TCOMPRESS_A ) bTex[0][(ly * 4) + lx] = (t << 0) & 0xFF000000;
else if (format == TCOMPRESS_xyZ) bTex[0][(ly * 4) + lx] = (t << 8) & 0xFF000000;
else bTex[0][(ly * 4) + lx] = (t << 24) & 0xFF000000;
break;
}
}
/* compress to DXT5 */
#if 0
/**/ if (TCOMPRESS_COLOR(format))
stb_compress_dxt_block((unsigned char *)dTex, (unsigned char *)bTex[0], true, STB_DXT_DITHER | STB_DXT_HIGHQUAL);
else if (TCOMPRESS_NORMAL(format))
stb_compress_dxt_block((unsigned char *)dTex, (unsigned char *)bTex[0], true, STB_DXT_NORMAL | STB_DXT_HIGHQUAL);
#else
switch (TCOMPRESS_CHANNELS(format)) {
case 4: squish::Compress ((unsigned char *)bTex[0], dTex + 0, flags); break;
case 3: squish::Compress ((unsigned char *)bTex[0], dTex + 0, flags); break;
case 2: squish::CompressAlphaDxt5((unsigned char *)bTex[0], 0xFFFF, dTex + 0);
squish::CompressAlphaDxt5((unsigned char *)bTex[1], 0xFFFF, dTex + 2); break;
case 1: squish::CompressAlphaDxt5((unsigned char *)bTex[0], 0xFFFF, dTex + 0); break;
}
#endif
/* advance pointer of compressed blocks */
switch (TCOMPRESS_CHANNELS(format)) {
case 4: dTex += ((8+8) / 4); break; /* 4x4x4 -> 16bytes */
case 3: dTex += (( 8 ) / 4); break; /* 4x4x3 -> 8bytes */
case 2: dTex += ((4+4) / 2); break; /* 4x4x2 -> 16bytes */
case 1: dTex += (( 4 ) / 2); break; /* 4x4x1 -> 8bytes */
}
#if 0
for (int ly = 0; ly < 4; ly += 1)
for (int lx = 0; lx < 4; lx += 1) {
dTex[((y + ly) * texd.Width) + (x + lx)] = bTex[0][(ly * 4) + lx];
}
#endif
}
}
text->UnlockRect(l);
}
(*tex)->UnlockRect(0);
(*tex)->Release();
(*tex) = text;
return true;
}
bool TextureCompressRGBH(LPDIRECT3DTEXTURE9 *base, bool gamma) {
bool res = true;
if (gamma) res = res && TextureCompressDXT<ULONG, float, TCOMPRESS_RGBH>(base);
else res = res && TextureCompressDXT<ULONG, long , TCOMPRESS_RGBH>(base);
return res;
}
bool TextureCompressRGBA(LPDIRECT3DTEXTURE9 *base, bool gamma) {
bool res = true;
if (gamma) res = res && TextureCompressDXT<ULONG, float, TCOMPRESS_RGBA>(base);
else res = res && TextureCompressDXT<ULONG, long , TCOMPRESS_RGBA>(base);
return res;
}
bool TextureCompressRGB(LPDIRECT3DTEXTURE9 *base, bool gamma) {
bool res = true;
if (gamma) res = res && TextureCompressDXT<ULONG, float, TCOMPRESS_RGB>(base);
else res = res && TextureCompressDXT<ULONG, long , TCOMPRESS_RGB>(base);
return res;
}
bool TextureCompressLA(LPDIRECT3DTEXTURE9 *base) {
bool res = true;
res = res && TextureCompressDXT<ULONG, long , TCOMPRESS_LA>(base);
return res;