-
Notifications
You must be signed in to change notification settings - Fork 9
/
CCubeMapProcessor.cpp
3226 lines (2725 loc) · 117 KB
/
CCubeMapProcessor.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
//--------------------------------------------------------------------------------------
//CCubeMapFilter
// Classes for filtering and processing cubemaps
//
//
//--------------------------------------------------------------------------------------
// (C) 2005 ATI Research, Inc., All rights reserved.
//--------------------------------------------------------------------------------------
#include "CCubeMapProcessor.h"
#include <string.h>
#define CP_PI 3.14159265358979323846
//Filtering options for each thread
SThreadOptionsThread0 sg_FilterOptionsCPU0;
//Thread functions used to run filtering routines in the CPU0 process
// note that these functions can not be member functions since thread initiation
// must start from a global or static global function
unsigned long WINAPI ThreadProcCPU0Filter()
{
//filter cube map mipchain
sg_FilterOptionsCPU0.m_cmProc->FilterCubeMapMipChain(
sg_FilterOptionsCPU0.m_BaseFilterAngle,
sg_FilterOptionsCPU0.m_InitialMipAngle,
sg_FilterOptionsCPU0.m_MipAnglePerLevelScale,
sg_FilterOptionsCPU0.m_FilterType,
// SL BEGIN
// sg_FilterOptionsCPU0.m_FixupType,
// SL END
sg_FilterOptionsCPU0.m_FixupWidth,
sg_FilterOptionsCPU0.m_bUseSolidAngle
// SL BEGIN
,sg_FilterOptionsCPU0.m_MCO
// SL END
);
return(CP_THREAD_COMPLETED);
}
// SL BEGIN
unsigned long WINAPI ThreadProcCPU0FilterMultithread()
{
//filter cube map mipchain
sg_FilterOptionsCPU0.m_cmProc->FilterCubeMapMipChainMultithread(
sg_FilterOptionsCPU0.m_BaseFilterAngle,
sg_FilterOptionsCPU0.m_InitialMipAngle,
sg_FilterOptionsCPU0.m_MipAnglePerLevelScale,
sg_FilterOptionsCPU0.m_FilterType,
sg_FilterOptionsCPU0.m_FixupWidth,
sg_FilterOptionsCPU0.m_bUseSolidAngle,
sg_FilterOptionsCPU0.m_MCO
);
return(CP_THREAD_COMPLETED);
}
// SL END
//Filtering options for each thread
SThreadOptionsThread1 sg_FilterOptionsCPU1;
//Thread functions used to run filtering routines in the process for CPU1
// note that these functions can not be member functions since thread initiation
// must start from a global or static global function
//This thread entry point function is called by thread 1 which is initiated
// by thread 0
unsigned long WINAPI ThreadProcCPU1Filter()
{
//filter subset of faces
sg_FilterOptionsCPU1.m_cmProc->FilterCubeSurfaces(
sg_FilterOptionsCPU1.m_SrcCubeMap,
sg_FilterOptionsCPU1.m_DstCubeMap,
sg_FilterOptionsCPU1.m_FilterConeAngle,
sg_FilterOptionsCPU1.m_FilterType,
sg_FilterOptionsCPU1.m_bUseSolidAngle,
sg_FilterOptionsCPU1.m_FaceIdxStart,
sg_FilterOptionsCPU1.m_FaceIdxEnd,
sg_FilterOptionsCPU1.m_ThreadIdx
// SL BEGIN
,sg_FilterOptionsCPU1.m_MCO.SpecularPower
,sg_FilterOptionsCPU1.m_MCO.LightingModel
,sg_FilterOptionsCPU1.m_MCO.FixupType
// SL END
);
return(CP_THREAD_COMPLETED);
}
//------------------------------------------------------------------------------
// D3D cube map face specification
// mapping from 3D x,y,z cube map lookup coordinates
// to 2D within face u,v coordinates
//
// --------------------> U direction
// | (within-face texture space)
// | _____
// | | |
// | | +Y |
// | _____|_____|_____ _____
// | | | | | |
// | | -X | +Z | +X | -Z |
// | |_____|_____|_____|_____|
// | | |
// | | -Y |
// | |_____|
// |
// v V direction
// (within-face texture space)
//------------------------------------------------------------------------------
//Information about neighbors and how texture coorrdinates change across faces
// in ORDER of left, right, top, bottom (e.g. edges corresponding to u=0,
// u=1, v=0, v=1 in the 2D coordinate system of the particular face.
//Note this currently assumes the D3D cube face ordering and orientation
CPCubeMapNeighbor sg_CubeNgh[6][4] =
{
//XPOS face
{{CP_FACE_Z_POS, CP_EDGE_RIGHT },
{CP_FACE_Z_NEG, CP_EDGE_LEFT },
{CP_FACE_Y_POS, CP_EDGE_RIGHT },
{CP_FACE_Y_NEG, CP_EDGE_RIGHT }},
//XNEG face
{{CP_FACE_Z_NEG, CP_EDGE_RIGHT },
{CP_FACE_Z_POS, CP_EDGE_LEFT },
{CP_FACE_Y_POS, CP_EDGE_LEFT },
{CP_FACE_Y_NEG, CP_EDGE_LEFT }},
//YPOS face
{{CP_FACE_X_NEG, CP_EDGE_TOP },
{CP_FACE_X_POS, CP_EDGE_TOP },
{CP_FACE_Z_NEG, CP_EDGE_TOP },
{CP_FACE_Z_POS, CP_EDGE_TOP }},
//YNEG face
{{CP_FACE_X_NEG, CP_EDGE_BOTTOM},
{CP_FACE_X_POS, CP_EDGE_BOTTOM},
{CP_FACE_Z_POS, CP_EDGE_BOTTOM},
{CP_FACE_Z_NEG, CP_EDGE_BOTTOM}},
//ZPOS face
{{CP_FACE_X_NEG, CP_EDGE_RIGHT },
{CP_FACE_X_POS, CP_EDGE_LEFT },
{CP_FACE_Y_POS, CP_EDGE_BOTTOM },
{CP_FACE_Y_NEG, CP_EDGE_TOP }},
//ZNEG face
{{CP_FACE_X_POS, CP_EDGE_RIGHT },
{CP_FACE_X_NEG, CP_EDGE_LEFT },
{CP_FACE_Y_POS, CP_EDGE_TOP },
{CP_FACE_Y_NEG, CP_EDGE_BOTTOM }}
};
//3x2 matrices that map cube map indexing vectors in 3d
// (after face selection and divide through by the
// _ABSOLUTE VALUE_ of the max coord)
// into NVC space
//Note this currently assumes the D3D cube face ordering and orientation
#define CP_UDIR 0
#define CP_VDIR 1
#define CP_FACEAXIS 2
float32 sgFace2DMapping[6][3][3] = {
//XPOS face
{{ 0, 0, -1}, //u towards negative Z
{ 0, -1, 0}, //v towards negative Y
{1, 0, 0}}, //pos X axis
//XNEG face
{{0, 0, 1}, //u towards positive Z
{0, -1, 0}, //v towards negative Y
{-1, 0, 0}}, //neg X axis
//YPOS face
{{1, 0, 0}, //u towards positive X
{0, 0, 1}, //v towards positive Z
{0, 1 , 0}}, //pos Y axis
//YNEG face
{{1, 0, 0}, //u towards positive X
{0, 0 , -1}, //v towards negative Z
{0, -1 , 0}}, //neg Y axis
//ZPOS face
{{1, 0, 0}, //u towards positive X
{0, -1, 0}, //v towards negative Y
{0, 0, 1}}, //pos Z axis
//ZNEG face
{{-1, 0, 0}, //u towards negative X
{0, -1, 0}, //v towards negative Y
{0, 0, -1}}, //neg Z axis
};
//The 12 edges of the cubemap, (entries are used to index into the neighbor table)
// this table is used to average over the edges.
int32 sg_CubeEdgeList[12][2] = {
{CP_FACE_X_POS, CP_EDGE_LEFT},
{CP_FACE_X_POS, CP_EDGE_RIGHT},
{CP_FACE_X_POS, CP_EDGE_TOP},
{CP_FACE_X_POS, CP_EDGE_BOTTOM},
{CP_FACE_X_NEG, CP_EDGE_LEFT},
{CP_FACE_X_NEG, CP_EDGE_RIGHT},
{CP_FACE_X_NEG, CP_EDGE_TOP},
{CP_FACE_X_NEG, CP_EDGE_BOTTOM},
{CP_FACE_Z_POS, CP_EDGE_TOP},
{CP_FACE_Z_POS, CP_EDGE_BOTTOM},
{CP_FACE_Z_NEG, CP_EDGE_TOP},
{CP_FACE_Z_NEG, CP_EDGE_BOTTOM}
};
//Information about which of the 8 cube corners are correspond to the
// the 4 corners in each cube face
// the order is upper left, upper right, lower left, lower right
int32 sg_CubeCornerList[6][4] = {
{ CP_CORNER_PPP, CP_CORNER_PPN, CP_CORNER_PNP, CP_CORNER_PNN }, // XPOS face
{ CP_CORNER_NPN, CP_CORNER_NPP, CP_CORNER_NNN, CP_CORNER_NNP }, // XNEG face
{ CP_CORNER_NPN, CP_CORNER_PPN, CP_CORNER_NPP, CP_CORNER_PPP }, // YPOS face
{ CP_CORNER_NNP, CP_CORNER_PNP, CP_CORNER_NNN, CP_CORNER_PNN }, // YNEG face
{ CP_CORNER_NPP, CP_CORNER_PPP, CP_CORNER_NNP, CP_CORNER_PNP }, // ZPOS face
{ CP_CORNER_PPN, CP_CORNER_NPN, CP_CORNER_PNN, CP_CORNER_NNN } // ZNEG face
};
//--------------------------------------------------------------------------------------
//Error handling for cube map processor
// Pop up dialog box, and terminate application
//--------------------------------------------------------------------------------------
void CPFatalError(const char *a_Msg)
{
MessageBox(NULL, a_Msg, "Error: Application Terminating", MB_OK);
exit(EM_FATAL_ERROR);
}
// SL BEGIN
void slerp(float32* res, float32* a, float32* b, float32 t)
{
float32 angle = acosf(VM_DOTPROD3(a, b));
if (0.0f == angle)
{
res[0] = a[0];
res[1] = a[1];
res[2] = a[2];
}
else if (CP_PI == angle)
{
// Can't recovert!
res[0] = 0;
res[1] = 0;
res[2] = 0;
}
else
{
res[0] = (sinf((1.0-t)*angle)/sinf(angle))*a[0] + (sinf(t*angle)/sinf(angle))*b[0];
res[1] = (sinf((1.0-t)*angle)/sinf(angle))*a[1] + (sinf(t*angle)/sinf(angle))*b[1];
res[2] = (sinf((1.0-t)*angle)/sinf(angle))*a[2] + (sinf(t*angle)/sinf(angle))*b[2];
}
}
#define LERP(A, B, FACTOR) ( (A) + (FACTOR)*((B) - (A)) )
// SL END
//--------------------------------------------------------------------------------------
// Convert cubemap face texel coordinates and face idx to 3D vector
// note the U and V coords are integer coords and range from 0 to size-1
// this routine can be used to generate a normalizer cube map
//--------------------------------------------------------------------------------------
// SL BEGIN
void TexelCoordToVect(int32 a_FaceIdx, float32 a_U, float32 a_V, int32 a_Size, float32 *a_XYZ, int32 a_FixupType)
{
float32 nvcU, nvcV;
float32 tempVec[3];
if (a_FixupType == CP_FIXUP_STRETCH && a_Size > 1)
{
// Code from Nvtt : http://code.google.com/p/nvidia-texture-tools/source/browse/trunk/src/nvtt/CubeSurface.cpp
// transform from [0..res - 1] to [-1 .. 1], match up edges exactly.
nvcU = (2.0f * (float32)a_U / ((float32)a_Size - 1.0f) ) - 1.0f;
nvcV = (2.0f * (float32)a_V / ((float32)a_Size - 1.0f) ) - 1.0f;
}
else
{
// Change from original AMD code
// transform from [0..res - 1] to [- (1 - 1 / res) .. (1 - 1 / res)]
// + 0.5f is for texel center addressing
nvcU = (2.0f * ((float32)a_U + 0.5f) / (float32)a_Size ) - 1.0f;
nvcV = (2.0f * ((float32)a_V + 0.5f) / (float32)a_Size ) - 1.0f;
}
if (a_FixupType == CP_FIXUP_WARP && a_Size > 1)
{
// Code from Nvtt : http://code.google.com/p/nvidia-texture-tools/source/browse/trunk/src/nvtt/CubeSurface.cpp
float32 a = powf(float32(a_Size), 2.0f) / powf(float32(a_Size - 1), 3.0f);
nvcU = a * powf(nvcU, 3) + nvcU;
nvcV = a * powf(nvcV, 3) + nvcV;
// Get current vector
//generate x,y,z vector (xform 2d NVC coord to 3D vector)
//U contribution
VM_SCALE3(a_XYZ, sgFace2DMapping[a_FaceIdx][CP_UDIR], nvcU);
//V contribution
VM_SCALE3(tempVec, sgFace2DMapping[a_FaceIdx][CP_VDIR], nvcV);
VM_ADD3(a_XYZ, tempVec, a_XYZ);
//add face axis
VM_ADD3(a_XYZ, sgFace2DMapping[a_FaceIdx][CP_FACEAXIS], a_XYZ);
//normalize vector
VM_NORM3(a_XYZ, a_XYZ);
}
else if (a_FixupType == CP_FIXUP_BENT && a_Size > 1)
{
// Method following description of Physically based rendering slides from CEDEC2011 of TriAce
// Get vector at edge
float32 EdgeNormalU[3];
float32 EdgeNormalV[3];
float32 EdgeNormal[3];
float32 EdgeNormalMinusOne[3];
// Recover vector at edge
//U contribution
VM_SCALE3(EdgeNormalU, sgFace2DMapping[a_FaceIdx][CP_UDIR], nvcU < 0.0 ? -1.0f : 1.0f);
//V contribution
VM_SCALE3(EdgeNormalV, sgFace2DMapping[a_FaceIdx][CP_VDIR], nvcV < 0.0 ? -1.0f : 1.0f);
VM_ADD3(EdgeNormal, EdgeNormalV, EdgeNormalU);
//add face axis
VM_ADD3(EdgeNormal, sgFace2DMapping[a_FaceIdx][CP_FACEAXIS], EdgeNormal);
//normalize vector
VM_NORM3(EdgeNormal, EdgeNormal);
// Get vector at (edge - 1)
float32 nvcUEdgeMinus1 = (2.0f * ((float32)(nvcU < 0.0f ? 0 : a_Size-1) + 0.5f) / (float32)a_Size ) - 1.0f;
float32 nvcVEdgeMinus1 = (2.0f * ((float32)(nvcV < 0.0f ? 0 : a_Size-1) + 0.5f) / (float32)a_Size ) - 1.0f;
// Recover vector at (edge - 1)
//U contribution
VM_SCALE3(EdgeNormalU, sgFace2DMapping[a_FaceIdx][CP_UDIR], nvcUEdgeMinus1);
//V contribution
VM_SCALE3(EdgeNormalV, sgFace2DMapping[a_FaceIdx][CP_VDIR], nvcVEdgeMinus1);
VM_ADD3(EdgeNormalMinusOne, EdgeNormalV, EdgeNormalU);
//add face axis
VM_ADD3(EdgeNormalMinusOne, sgFace2DMapping[a_FaceIdx][CP_FACEAXIS], EdgeNormalMinusOne);
//normalize vector
VM_NORM3(EdgeNormalMinusOne, EdgeNormalMinusOne);
// Get angle between the two vector (which is 50% of the two vector presented in the TriAce slide)
float32 AngleNormalEdge = acosf(VM_DOTPROD3(EdgeNormal, EdgeNormalMinusOne));
// Here we assume that high resolution required less offset than small resolution (TriAce based this on blur radius and custom value)
// Start to increase from 50% to 100% target angle from 128x128x6 to 1x1x6
float32 NumLevel = (logf(min(a_Size, 128)) / logf(2)) - 1;
AngleNormalEdge = LERP(0.5 * AngleNormalEdge, AngleNormalEdge, 1.0f - (NumLevel/6) );
float32 factorU = abs((2.0f * ((float32)a_U) / (float32)(a_Size - 1) ) - 1.0f);
float32 factorV = abs((2.0f * ((float32)a_V) / (float32)(a_Size - 1) ) - 1.0f);
AngleNormalEdge = LERP(0.0f, AngleNormalEdge, max(factorU, factorV) );
// Get current vector
//generate x,y,z vector (xform 2d NVC coord to 3D vector)
//U contribution
VM_SCALE3(a_XYZ, sgFace2DMapping[a_FaceIdx][CP_UDIR], nvcU);
//V contribution
VM_SCALE3(tempVec, sgFace2DMapping[a_FaceIdx][CP_VDIR], nvcV);
VM_ADD3(a_XYZ, tempVec, a_XYZ);
//add face axis
VM_ADD3(a_XYZ, sgFace2DMapping[a_FaceIdx][CP_FACEAXIS], a_XYZ);
//normalize vector
VM_NORM3(a_XYZ, a_XYZ);
float32 RadiantAngle = AngleNormalEdge;
// Get angle between face normal and current normal. Used to push the normal away from face normal.
float32 AngleFaceVector = acosf(VM_DOTPROD3(sgFace2DMapping[a_FaceIdx][CP_FACEAXIS], a_XYZ));
// Push the normal away from face normal by an angle of RadiantAngle
slerp(a_XYZ, sgFace2DMapping[a_FaceIdx][CP_FACEAXIS], a_XYZ, 1.0f + RadiantAngle / AngleFaceVector);
}
else
{
//generate x,y,z vector (xform 2d NVC coord to 3D vector)
//U contribution
VM_SCALE3(a_XYZ, sgFace2DMapping[a_FaceIdx][CP_UDIR], nvcU);
//V contribution
VM_SCALE3(tempVec, sgFace2DMapping[a_FaceIdx][CP_VDIR], nvcV);
VM_ADD3(a_XYZ, tempVec, a_XYZ);
//add face axis
VM_ADD3(a_XYZ, sgFace2DMapping[a_FaceIdx][CP_FACEAXIS], a_XYZ);
//normalize vector
VM_NORM3(a_XYZ, a_XYZ);
}
}
// SL END
//--------------------------------------------------------------------------------------
// Convert 3D vector to cubemap face texel coordinates and face idx
// note the U and V coords are integer coords and range from 0 to size-1
// this routine can be used to generate a normalizer cube map
//
// returns face IDX and texel coords
//--------------------------------------------------------------------------------------
// SL BEGIN
/*
Mapping Texture Coordinates to Cube Map Faces
Because there are multiple faces, the mapping of texture coordinates to positions on cube map faces
is more complicated than the other texturing targets. The EXT_texture_cube_map extension is purposefully
designed to be consistent with DirectX 7's cube map arrangement. This is also consistent with the cube
map arrangement in Pixar's RenderMan package.
For cube map texturing, the (s,t,r) texture coordinates are treated as a direction vector (rx,ry,rz)
emanating from the center of a cube. (The q coordinate can be ignored since it merely scales the vector
without affecting the direction.) At texture application time, the interpolated per-fragment (s,t,r)
selects one of the cube map face's 2D mipmap sets based on the largest magnitude coordinate direction
the major axis direction). The target column in the table below explains how the major axis direction
maps to the 2D image of a particular cube map target.
major axis
direction target sc tc ma
---------- --------------------------------- --- --- ---
+rx GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
-rx GL_TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
+ry GL_TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
-ry GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
+rz GL_TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
-rz GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
Using the sc, tc, and ma determined by the major axis direction as specified in the table above,
an updated (s,t) is calculated as follows
s = ( sc/|ma| + 1 ) / 2
t = ( tc/|ma| + 1 ) / 2
If |ma| is zero or very nearly zero, the results of the above two equations need not be defined
(though the result may not lead to GL interruption or termination). Once the cube map face's 2D mipmap
set and (s,t) is determined, texture fetching and filtering proceeds like standard OpenGL 2D texturing.
*/
// Note this method return U and V in range from 0 to size-1
// SL END
void VectToTexelCoord(float32 *a_XYZ, int32 a_Size, int32 *a_FaceIdx, int32 *a_U, int32 *a_V )
{
float32 nvcU, nvcV;
float32 absXYZ[3];
float32 maxCoord;
float32 onFaceXYZ[3];
int32 faceIdx;
int32 u, v;
//absolute value 3
VM_ABS3(absXYZ, a_XYZ);
if( (absXYZ[0] >= absXYZ[1]) && (absXYZ[0] >= absXYZ[2]) )
{
maxCoord = absXYZ[0];
if(a_XYZ[0] >= 0) //face = XPOS
{
faceIdx = CP_FACE_X_POS;
}
else
{
faceIdx = CP_FACE_X_NEG;
}
}
else if ( (absXYZ[1] >= absXYZ[0]) && (absXYZ[1] >= absXYZ[2]) )
{
maxCoord = absXYZ[1];
if(a_XYZ[1] >= 0) //face = XPOS
{
faceIdx = CP_FACE_Y_POS;
}
else
{
faceIdx = CP_FACE_Y_NEG;
}
}
else // if( (absXYZ[2] > absXYZ[0]) && (absXYZ[2] > absXYZ[1]) )
{
maxCoord = absXYZ[2];
if(a_XYZ[2] >= 0) //face = XPOS
{
faceIdx = CP_FACE_Z_POS;
}
else
{
faceIdx = CP_FACE_Z_NEG;
}
}
//divide through by max coord so face vector lies on cube face
VM_SCALE3(onFaceXYZ, a_XYZ, 1.0f/maxCoord);
nvcU = VM_DOTPROD3(sgFace2DMapping[ faceIdx ][CP_UDIR], onFaceXYZ );
nvcV = VM_DOTPROD3(sgFace2DMapping[ faceIdx ][CP_VDIR], onFaceXYZ );
// SL BEGIN
// Modify original AMD code to return value from 0 to Size - 1
u = (int32)floor( (a_Size - 1) * 0.5f * (nvcU + 1.0f) );
v = (int32)floor( (a_Size - 1) * 0.5f * (nvcV + 1.0f) );
// SL END
*a_FaceIdx = faceIdx;
*a_U = u;
*a_V = v;
}
//--------------------------------------------------------------------------------------
// gets texel ptr in a cube map given a direction vector, and an array of
// CImageSurfaces that represent the cube faces.
//
//--------------------------------------------------------------------------------------
CP_ITYPE *GetCubeMapTexelPtr(float32 *a_XYZ, CImageSurface *a_Surface)
{
int32 u, v, faceIdx;
//get face idx and u, v texel coordinate in face
VectToTexelCoord(a_XYZ, a_Surface[0].m_Width, &faceIdx, &u, &v );
return( a_Surface[faceIdx].GetSurfaceTexelPtr(u, v) );
}
//--------------------------------------------------------------------------------------
// Compute solid angle of given texel in cubemap face for weighting taps in the
// kernel by the area they project to on the unit sphere.
//
// Note that this code uses an approximation to the solid angle, by treating the
// two triangles that make up the quad comprising the texel as planar. If more
// accuracy is required, the solid angle per triangle lying on the sphere can be
// computed using the sum of the interior angles - PI.
//
//--------------------------------------------------------------------------------------
// SL BEGIN
// Replace the calcul of the solidAngle by a better approximation.
#if 0
float32 TexelCoordSolidAngle(int32 a_FaceIdx, float32 a_U, float32 a_V, int32 a_Size)
{
float32 cornerVect[4][3];
float64 cornerVect64[4][3];
float32 halfTexelStep = 0.5f; //note u, and v are in texel coords (where each texel is one unit)
float64 edgeVect0[3];
float64 edgeVect1[3];
float64 xProdVect[3];
float64 texelArea;
//compute 4 corner vectors of texel
TexelCoordToVect(a_FaceIdx, a_U - halfTexelStep, a_V - halfTexelStep, a_Size, cornerVect[0] );
TexelCoordToVect(a_FaceIdx, a_U - halfTexelStep, a_V + halfTexelStep, a_Size, cornerVect[1] );
TexelCoordToVect(a_FaceIdx, a_U + halfTexelStep, a_V - halfTexelStep, a_Size, cornerVect[2] );
TexelCoordToVect(a_FaceIdx, a_U + halfTexelStep, a_V + halfTexelStep, a_Size, cornerVect[3] );
VM_NORM3_UNTYPED(cornerVect64[0], cornerVect[0] );
VM_NORM3_UNTYPED(cornerVect64[1], cornerVect[1] );
VM_NORM3_UNTYPED(cornerVect64[2], cornerVect[2] );
VM_NORM3_UNTYPED(cornerVect64[3], cornerVect[3] );
//area of triangle defined by corners 0, 1, and 2
VM_SUB3_UNTYPED(edgeVect0, cornerVect64[1], cornerVect64[0] );
VM_SUB3_UNTYPED(edgeVect1, cornerVect64[2], cornerVect64[0] );
VM_XPROD3_UNTYPED(xProdVect, edgeVect0, edgeVect1 );
texelArea = 0.5f * sqrt( VM_DOTPROD3_UNTYPED(xProdVect, xProdVect ) );
//area of triangle defined by corners 1, 2, and 3
VM_SUB3_UNTYPED(edgeVect0, cornerVect64[2], cornerVect64[1] );
VM_SUB3_UNTYPED(edgeVect1, cornerVect64[3], cornerVect64[1] );
VM_XPROD3_UNTYPED(xProdVect, edgeVect0, edgeVect1 );
texelArea += 0.5f * sqrt( VM_DOTPROD3_UNTYPED(xProdVect, xProdVect ) );
return texelArea;
}
#else
/** Original code from Ignacio Castaño
* This formula is from Manne Öhrström's thesis.
* Take two coordiantes in the range [-1, 1] that define a portion of a
* cube face and return the area of the projection of that portion on the
* surface of the sphere.
**/
static float32 AreaElement( float32 x, float32 y )
{
return atan2(x * y, sqrt(x * x + y * y + 1));
}
float32 TexelCoordSolidAngle(int32 a_FaceIdx, float32 a_U, float32 a_V, int32 a_Size)
{
// transform from [0..res - 1] to [- (1 - 1 / res) .. (1 - 1 / res)]
// (+ 0.5f is for texel center addressing)
float32 U = (2.0f * ((float32)a_U + 0.5f) / (float32)a_Size ) - 1.0f;
float32 V = (2.0f * ((float32)a_V + 0.5f) / (float32)a_Size ) - 1.0f;
// Shift from a demi texel, mean 1.0f / a_Size with U and V in [-1..1]
float32 InvResolution = 1.0f / a_Size;
// U and V are the -1..1 texture coordinate on the current face.
// Get projected area for this texel
float32 x0 = U - InvResolution;
float32 y0 = V - InvResolution;
float32 x1 = U + InvResolution;
float32 y1 = V + InvResolution;
float32 SolidAngle = AreaElement(x0, y0) - AreaElement(x0, y1) - AreaElement(x1, y0) + AreaElement(x1, y1);
return SolidAngle;
}
#endif
// SL END
//--------------------------------------------------------------------------------------
//Builds a normalizer cubemap
//
// Takes in a cube face size, and an array of 6 surfaces to write the cube faces into
//
// Note that this normalizer cube map stores the vectors in unbiased -1 to 1 range.
// if _bx2 style scaled and biased vectors are needed, uncomment the SCALE and BIAS
// below
//--------------------------------------------------------------------------------------
// SL BEGIN
void CCubeMapProcessor::BuildNormalizerCubemap(int32 a_Size, CImageSurface *a_Surface, int32 a_FixupType)
// SL END
{
int32 iCubeFace, u, v;
//iterate over cube faces
for(iCubeFace=0; iCubeFace<6; iCubeFace++)
{
a_Surface[iCubeFace].Clear();
a_Surface[iCubeFace].Init(a_Size, a_Size, 3);
//fast texture walk, build normalizer cube map
CP_ITYPE *texelPtr = a_Surface[iCubeFace].m_ImgData;
for(v=0; v < a_Surface[iCubeFace].m_Height; v++)
{
for(u=0; u < a_Surface[iCubeFace].m_Width; u++)
{
// SL_BEGIN
TexelCoordToVect(iCubeFace, (float32)u, (float32)v, a_Size, texelPtr, a_FixupType);
// SL END
//VM_SCALE3(texelPtr, texelPtr, 0.5f);
//VM_BIAS3(texelPtr, texelPtr, 0.5f);
texelPtr += a_Surface[iCubeFace].m_NumChannels;
}
}
}
}
//--------------------------------------------------------------------------------------
//Builds a normalizer cubemap, with the texels solid angle stored in the fourth component
//
//Takes in a cube face size, and an array of 6 surfaces to write the cube faces into
//
//Note that this normalizer cube map stores the vectors in unbiased -1 to 1 range.
// if _bx2 style scaled and biased vectors are needed, uncomment the SCALE and BIAS
// below
//--------------------------------------------------------------------------------------
// SL BEGIN
void CCubeMapProcessor::BuildNormalizerSolidAngleCubemap(int32 a_Size, CImageSurface *a_Surface, int32 a_FixupType)
// SL END
{
int32 iCubeFace, u, v;
//iterate over cube faces
for(iCubeFace=0; iCubeFace<6; iCubeFace++)
{
a_Surface[iCubeFace].Clear();
a_Surface[iCubeFace].Init(a_Size, a_Size, 4); //First three channels for norm cube, and last channel for solid angle
//fast texture walk, build normalizer cube map
CP_ITYPE *texelPtr = a_Surface[iCubeFace].m_ImgData;
for(v=0; v<a_Surface[iCubeFace].m_Height; v++)
{
for(u=0; u<a_Surface[iCubeFace].m_Width; u++)
{
// SL_BEGIN
TexelCoordToVect(iCubeFace, (float32)u, (float32)v, a_Size, texelPtr, a_FixupType);
// SL END
//VM_SCALE3(texelPtr, texelPtr, 0.5f);
//VM_BIAS3(texelPtr, texelPtr, 0.5f);
*(texelPtr + 3) = TexelCoordSolidAngle(iCubeFace, (float32)u, (float32)v, a_Size);
texelPtr += a_Surface[iCubeFace].m_NumChannels;
}
}
}
}
//--------------------------------------------------------------------------------------
//Clear filter extents for the 6 cube map faces
//--------------------------------------------------------------------------------------
void CCubeMapProcessor::ClearFilterExtents(CBBoxInt32 *aFilterExtents)
{
int32 iCubeFaces;
for(iCubeFaces=0; iCubeFaces<6; iCubeFaces++)
{
aFilterExtents[iCubeFaces].Clear();
}
}
//--------------------------------------------------------------------------------------
//Define per-face bounding box filter extents
//
// These define conservative texel regions in each of the faces the filter can possibly
// process. When the pixels in the regions are actually processed, the dot product
// between the tap vector and the center tap vector is used to determine the weight of
// the tap and whether or not the tap is within the cone.
//
//--------------------------------------------------------------------------------------
void CCubeMapProcessor::DetermineFilterExtents(float32 *a_CenterTapDir, int32 a_SrcSize, int32 a_BBoxSize,
CBBoxInt32 *a_FilterExtents )
{
int32 u, v;
int32 faceIdx;
int32 minU, minV, maxU, maxV;
int32 i;
//neighboring face and bleed over amount, and width of BBOX for
// left, right, top, and bottom edges of this face
int32 bleedOverAmount[4];
int32 bleedOverBBoxMin[4];
int32 bleedOverBBoxMax[4];
int32 neighborFace;
int32 neighborEdge;
//get face idx, and u, v info from center tap dir
VectToTexelCoord(a_CenterTapDir, a_SrcSize, &faceIdx, &u, &v );
//define bbox size within face
a_FilterExtents[faceIdx].Augment(u - a_BBoxSize, v - a_BBoxSize, 0);
a_FilterExtents[faceIdx].Augment(u + a_BBoxSize, v + a_BBoxSize, 0);
a_FilterExtents[faceIdx].ClampMin(0, 0, 0);
a_FilterExtents[faceIdx].ClampMax(a_SrcSize-1, a_SrcSize-1, 0);
//u and v extent in face corresponding to center tap
minU = a_FilterExtents[faceIdx].m_minCoord[0];
minV = a_FilterExtents[faceIdx].m_minCoord[1];
maxU = a_FilterExtents[faceIdx].m_maxCoord[0];
maxV = a_FilterExtents[faceIdx].m_maxCoord[1];
//bleed over amounts for face across u=0 edge (left)
bleedOverAmount[0] = (a_BBoxSize - u);
bleedOverBBoxMin[0] = minV;
bleedOverBBoxMax[0] = maxV;
//bleed over amounts for face across u=1 edge (right)
bleedOverAmount[1] = (u + a_BBoxSize) - (a_SrcSize-1);
bleedOverBBoxMin[1] = minV;
bleedOverBBoxMax[1] = maxV;
//bleed over to face across v=0 edge (up)
bleedOverAmount[2] = (a_BBoxSize - v);
bleedOverBBoxMin[2] = minU;
bleedOverBBoxMax[2] = maxU;
//bleed over to face across v=1 edge (down)
bleedOverAmount[3] = (v + a_BBoxSize) - (a_SrcSize-1);
bleedOverBBoxMin[3] = minU;
bleedOverBBoxMax[3] = maxU;
//compute bleed over regions in neighboring faces
for(i=0; i<4; i++)
{
if(bleedOverAmount[i] > 0)
{
neighborFace = sg_CubeNgh[faceIdx][i].m_Face;
neighborEdge = sg_CubeNgh[faceIdx][i].m_Edge;
//For certain types of edge abutments, the bleedOverBBoxMin, and bleedOverBBoxMax need to
// be flipped: the cases are
// if a left edge mates with a left or bottom edge on the neighbor
// if a top edge mates with a top or right edge on the neighbor
// if a right edge mates with a right or top edge on the neighbor
// if a bottom edge mates with a bottom or left edge on the neighbor
//Seeing as the edges are enumerated as follows
// left =0
// right =1
// top =2
// bottom =3
//
// so if the edge enums are the same, or the sum of the enums == 3,
// the bbox needs to be flipped
if( (i == neighborEdge) || ((i+neighborEdge) == 3) )
{
bleedOverBBoxMin[i] = (a_SrcSize-1) - bleedOverBBoxMin[i];
bleedOverBBoxMax[i] = (a_SrcSize-1) - bleedOverBBoxMax[i];
}
//The way the bounding box is extended onto the neighboring face
// depends on which edge of neighboring face abuts with this one
switch(sg_CubeNgh[faceIdx][i].m_Edge)
{
case CP_EDGE_LEFT:
a_FilterExtents[neighborFace].Augment(0, bleedOverBBoxMin[i], 0);
a_FilterExtents[neighborFace].Augment(bleedOverAmount[i], bleedOverBBoxMax[i], 0);
break;
case CP_EDGE_RIGHT:
a_FilterExtents[neighborFace].Augment( (a_SrcSize-1), bleedOverBBoxMin[i], 0);
a_FilterExtents[neighborFace].Augment( (a_SrcSize-1) - bleedOverAmount[i], bleedOverBBoxMax[i], 0);
break;
case CP_EDGE_TOP:
a_FilterExtents[neighborFace].Augment(bleedOverBBoxMin[i], 0, 0);
a_FilterExtents[neighborFace].Augment(bleedOverBBoxMax[i], bleedOverAmount[i], 0);
break;
case CP_EDGE_BOTTOM:
a_FilterExtents[neighborFace].Augment(bleedOverBBoxMin[i], (a_SrcSize-1), 0);
a_FilterExtents[neighborFace].Augment(bleedOverBBoxMax[i], (a_SrcSize-1) - bleedOverAmount[i], 0);
break;
}
//clamp filter extents in non-center tap faces to remain within surface
a_FilterExtents[neighborFace].ClampMin(0, 0, 0);
a_FilterExtents[neighborFace].ClampMax(a_SrcSize-1, a_SrcSize-1, 0);
}
//If the bleed over amount bleeds past the adjacent face onto the opposite face
// from the center tap face, then process the opposite face entirely for now.
//Note that the cases in which this happens, what usually happens is that
// more than one edge bleeds onto the opposite face, and the bounding box
// encompasses the entire cube map face.
if(bleedOverAmount[i] > a_SrcSize)
{
uint32 oppositeFaceIdx;
//determine opposite face
switch(faceIdx)
{
case CP_FACE_X_POS:
oppositeFaceIdx = CP_FACE_X_NEG;
break;
case CP_FACE_X_NEG:
oppositeFaceIdx = CP_FACE_X_POS;
break;
case CP_FACE_Y_POS:
oppositeFaceIdx = CP_FACE_Y_NEG;
break;
case CP_FACE_Y_NEG:
oppositeFaceIdx = CP_FACE_Y_POS;
break;
case CP_FACE_Z_POS:
oppositeFaceIdx = CP_FACE_Z_NEG;
break;
case CP_FACE_Z_NEG:
oppositeFaceIdx = CP_FACE_Z_POS;
break;
default:
break;
}
//just encompass entire face for now
a_FilterExtents[oppositeFaceIdx].Augment(0, 0, 0);
a_FilterExtents[oppositeFaceIdx].Augment((a_SrcSize-1), (a_SrcSize-1), 0);
}
}
minV=minV;
}
//--------------------------------------------------------------------------------------
//ProcessFilterExtents
// Process bounding box in each cube face
//
//--------------------------------------------------------------------------------------
void CCubeMapProcessor::ProcessFilterExtents(float32 *a_CenterTapDir, float32 a_DotProdThresh,
CBBoxInt32 *a_FilterExtents, CImageSurface *a_NormCubeMap, CImageSurface *a_SrcCubeMap,
CP_ITYPE *a_DstVal, uint32 a_FilterType, bool8 a_bUseSolidAngleWeighting
// SL BEGIN
,float32 a_SpecularPower
,int32 a_LightingModel
// SL END
)
{
int32 iFaceIdx, u, v;
int32 faceWidth;
int32 k;
//pointers used to walk across the image surface to accumulate taps
CP_ITYPE *normCubeRowStartPtr;
CP_ITYPE *srcCubeRowStartPtr;
CP_ITYPE *texelVect;
//accumulators are 64-bit floats in order to have the precision needed
// over a summation of a large number of pixels
float64 dstAccum[4];
float64 weightAccum;
CP_ITYPE tapDotProd; //dot product between center tap and current tap
int32 normCubePitch;
int32 srcCubePitch;
int32 normCubeRowWalk;
int32 srcCubeRowWalk;
int32 uStart, uEnd;
int32 vStart, vEnd;
int32 nSrcChannels;
nSrcChannels = a_SrcCubeMap[0].m_NumChannels;
//norm cube map and srcCubeMap have same face width
faceWidth = a_NormCubeMap[0].m_Width;
//amount to add to pointer to move to next scanline in images
normCubePitch = faceWidth * a_NormCubeMap[0].m_NumChannels;
srcCubePitch = faceWidth * a_SrcCubeMap[0].m_NumChannels;
//dest accum
for(k=0; k<m_NumChannels; k++)
{
dstAccum[k] = 0.0f;
}
weightAccum = 0.0f;
// SL BEGIN
// Add a more efficient path (without test and switch) for cosine power,
// Basically just a copy past.
if (a_FilterType != CP_FILTER_TYPE_COSINE_POWER)
{
// SL END
//iterate over cubefaces
for(iFaceIdx=0; iFaceIdx<6; iFaceIdx++ )
{
//if bbox is non empty
if(a_FilterExtents[iFaceIdx].Empty() == FALSE)
{
uStart = a_FilterExtents[iFaceIdx].m_minCoord[0];
vStart = a_FilterExtents[iFaceIdx].m_minCoord[1];
uEnd = a_FilterExtents[iFaceIdx].m_maxCoord[0];
vEnd = a_FilterExtents[iFaceIdx].m_maxCoord[1];
normCubeRowStartPtr = a_NormCubeMap[iFaceIdx].m_ImgData + (a_NormCubeMap[iFaceIdx].m_NumChannels *
((vStart * faceWidth) + uStart) );
srcCubeRowStartPtr = a_SrcCubeMap[iFaceIdx].m_ImgData + (a_SrcCubeMap[iFaceIdx].m_NumChannels *
((vStart * faceWidth) + uStart) );
//note that <= is used to ensure filter extents always encompass at least one pixel if bbox is non empty
for(v = vStart; v <= vEnd; v++)
{
normCubeRowWalk = 0;
srcCubeRowWalk = 0;
for(u = uStart; u <= uEnd; u++)
{
//pointer to direction in cube map associated with texel
texelVect = (normCubeRowStartPtr + normCubeRowWalk);
//check dot product to see if texel is within cone
tapDotProd = VM_DOTPROD3(texelVect, a_CenterTapDir);
if( tapDotProd >= a_DotProdThresh )
{
CP_ITYPE weight;
//for now just weight all taps equally, but ideally
// weight should be proportional to the solid angle of the tap
if(a_bUseSolidAngleWeighting == TRUE)
{ //solid angle stored in 4th channel of normalizer/solid angle cube map
weight = *(texelVect+3);
}
else
{ //all taps equally weighted
weight = 1.0f;
}
switch(a_FilterType)
{
case CP_FILTER_TYPE_CONE:
case CP_FILTER_TYPE_ANGULAR_GAUSSIAN:
{
//weights are in same lookup table for both of these filter types
weight *= m_FilterLUT[(int32)(tapDotProd * (m_NumFilterLUTEntries - 1))];
}
break;
case CP_FILTER_TYPE_COSINE:
{
if(tapDotProd > 0.0f)
{
weight *= tapDotProd;
}
else
{
weight = 0.0f;
}
}
break;
case CP_FILTER_TYPE_DISC:
default:
break;
}