-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtests.hlsl
1394 lines (1194 loc) · 47.7 KB
/
tests.hlsl
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
#ifndef BXDFTESTS_TESTS_HLSL
#define BXDFTESTS_TESTS_HLSL
#include "nbl/builtin/hlsl/cpp_compat.hlsl"
#include "nbl/builtin/hlsl/random/xoroshiro.hlsl"
#include "nbl/builtin/hlsl/random/pcg.hlsl"
#include "nbl/builtin/hlsl/sampling/uniform.hlsl"
#include "nbl/builtin/hlsl/bxdf/common.hlsl"
#include "nbl/builtin/hlsl/bxdf/reflection.hlsl"
#include "nbl/builtin/hlsl/bxdf/transmission.hlsl"
#include "nbl/builtin/hlsl/bxdf/bxdf_traits.hlsl"
#ifndef __HLSL_VERSION
#include <glm/gtc/quaternion.hpp>
#include <glm/gtx/hash.hpp>
#include <unordered_map>
#include <vector>
#include <cmath>
#include <format>
#include <functional>
#include "ImfRgbaFile.h"
#include "ImfArray.h"
#include "ImfHeader.h"
#include "ImfNamespace.h"
#include <iostream>
#include "nlohmann/json.hpp"
namespace IMF = Imf;
namespace IMATH = Imath;
using namespace IMF;
using namespace IMATH;
using json = nlohmann::json;
#endif
namespace nbl
{
namespace hlsl
{
using ray_dir_info_t = bxdf::ray_dir_info::SBasic<float>;
using iso_interaction = bxdf::surface_interactions::SIsotropic<ray_dir_info_t>;
using aniso_interaction = bxdf::surface_interactions::SAnisotropic<ray_dir_info_t>;
using sample_t = bxdf::SLightSample<ray_dir_info_t>;
using iso_cache = bxdf::SIsotropicMicrofacetCache<float>;
using aniso_cache = bxdf::SAnisotropicMicrofacetCache<float>;
using quotient_pdf_t = bxdf::quotient_and_pdf<float32_t3, float>;
using spectral_t = vector<float, 3>;
using params_t = bxdf::SBxDFParams<float>;
using bool32_t3 = vector<bool, 3>;
namespace impl
{
inline float rngFloat01(NBL_REF_ARG(nbl::hlsl::Xoroshiro64Star) rng)
{
return (float)rng() / numeric_limits<uint32_t>::max;
}
template<typename T>
struct RNGUniformDist;
template<>
struct RNGUniformDist<float32_t>
{
static float32_t __call(NBL_REF_ARG(nbl::hlsl::Xoroshiro64Star) rng)
{
return rngFloat01(rng);
}
};
template<uint16_t N>
struct RNGUniformDist<vector<float32_t, N>>
{
static vector<float32_t, N> __call(NBL_REF_ARG(nbl::hlsl::Xoroshiro64Star) rng)
{
vector<float32_t, N> retval;
for (int i = 0; i < N; i++)
retval[i] = rngFloat01(rng);
return retval;
}
};
}
template<typename T>
T rngUniformDist(NBL_REF_ARG(nbl::hlsl::Xoroshiro64Star) rng)
{
return impl::RNGUniformDist<T>::__call(rng);
}
template<typename T>
bool checkEq(T a, T b, float32_t eps)
{
return nbl::hlsl::all<vector<bool, T::length()>>(nbl::hlsl::max<T>(a / b, b / a) <= (T)(1 + eps));
}
template<typename T>
bool checkLt(T a, T b)
{
return nbl::hlsl::all<vector<bool, T::length()>>(a < b);
}
template<typename T>
bool checkZero(T a, float32_t eps)
{
return nbl::hlsl::all<vector<bool, T::length()>>(nbl::hlsl::abs<T>(a) < (T)eps);
}
template<>
bool checkZero<float32_t>(float32_t a, float32_t eps)
{
return nbl::hlsl::abs<float32_t>(a) < eps;
}
#ifndef __HLSL_VERSION
// because atan2 is not in tgmath.hlsl yet
// takes in normalized vectors
inline float32_t3 polarToCartesian(float32_t2 theta_phi)
{
return float32_t3(std::cos(theta_phi.y) * std::cos(theta_phi.x),
std::sin(theta_phi.y) * std::cos(theta_phi.x),
std::sin(theta_phi.x));
}
inline float32_t2 cartesianToPolar(float32_t3 coords)
{
return float32_t2(std::acos(clamp<float>(coords.z, -1, 1)), std::atan2(coords.y, coords.x));
}
#endif
struct SBxDFTestResources
{
static SBxDFTestResources create(uint32_t2 seed)
{
SBxDFTestResources retval;
retval.rng = nbl::hlsl::Xoroshiro64Star::construct(seed);
retval.u = float32_t3(rngUniformDist<float32_t2>(retval.rng), 0.0);
retval.V.direction = nbl::hlsl::normalize<float32_t3>(uniform_sphere_generate<float>(rngUniformDist<float32_t2>(retval.rng)));
retval.N = nbl::hlsl::normalize<float32_t3>(uniform_sphere_generate<float>(rngUniformDist<float32_t2>(retval.rng)));
float32_t2x3 tb = math::frisvad<float>(retval.N);
#ifndef __HLSL_VERSION
const float angle = 2 * numbers::pi<float> * rngUniformDist<float>(retval.rng);
glm::quat rot = glm::angleAxis(angle, retval.N);
retval.T = rot * tb[0];
retval.B = rot * tb[1];
#else
retval.T = tb[0];
retval.B = tb[1];
#endif
retval.alpha.x = rngUniformDist<float>(retval.rng);
retval.alpha.y = rngUniformDist<float>(retval.rng);
retval.eta = 1.3;
retval.ior = float32_t2(1.3, 2.0);
retval.luma_coeff = float32_t3(0.2126, 0.7152, 0.0722); // luma coefficients for Rec. 709
return retval;
}
float eps = 1e-3; // epsilon
uint32_t state; // init state seed, for debugging
nbl::hlsl::Xoroshiro64Star rng;
ray_dir_info_t V;
float32_t3 N;
float32_t3 T;
float32_t3 B;
float32_t3 u;
float32_t2 alpha;
float eta;
float32_t2 ior;
float32_t3 luma_coeff;
};
struct STestInitParams
{
bool logInfo;
uint32_t state;
uint32_t samples;
uint32_t thetaSplits;
uint32_t phiSplits;
bool writeFrequencies;
};
enum ErrorType : uint32_t
{
BET_NONE = 0,
BET_NEGATIVE_VAL, // pdf/quotient/eval < 0
BET_PDF_ZERO, // pdf = 0
BET_QUOTIENT_INF, // quotient -> inf
BET_JACOBIAN,
BET_PDF_EVAL_DIFF,
BET_RECIPROCITY,
BET_NOBREAK, // not an error code, ones after this don't break
BET_INVALID,
BET_PRINT_MSG
};
struct TestBase
{
void init(uint32_t2 seed)
{
rc = SBxDFTestResources::create(seed);
isointer = iso_interaction::create(rc.V, rc.N);
anisointer = aniso_interaction::create(isointer, rc.T, rc.B);
}
virtual ErrorType compute() { return BET_NONE; }
SBxDFTestResources rc;
iso_interaction isointer;
aniso_interaction anisointer;
#ifndef __HLSL_VERSION
std::string name = "base";
std::string errMsg = "";
#endif
};
struct FailureCallback
{
virtual void __call(ErrorType error, NBL_REF_ARG(TestBase) failedFor, bool logInfo) {}
};
template<class BxDF>
struct TestBxDFBase : TestBase
{
BxDF bxdf;
};
template<class BxDF>
struct TestBxDF : TestBxDFBase<BxDF>
{
using base_t = TestBxDFBase<BxDF>;
void initBxDF(SBxDFTestResources _rc)
{
base_t::bxdf = BxDF::create(); // default to lambertian bxdf
#ifndef __HLSL_VERSION
base_t::name = "Lambertian BxDF";
#endif
}
};
template<>
struct TestBxDF<bxdf::reflection::SOrenNayarBxDF<sample_t, iso_interaction, aniso_interaction, spectral_t>> : TestBxDFBase<bxdf::reflection::SOrenNayarBxDF<sample_t, iso_interaction, aniso_interaction, spectral_t>>
{
using base_t = TestBxDFBase<bxdf::reflection::SOrenNayarBxDF<sample_t, iso_interaction, aniso_interaction, spectral_t>>;
void initBxDF(SBxDFTestResources _rc)
{
base_t::bxdf = bxdf::reflection::SOrenNayarBxDF<sample_t, iso_interaction, aniso_interaction, spectral_t>::create(_rc.alpha.x);
#ifndef __HLSL_VERSION
base_t::name = "OrenNayar BRDF";
#endif
}
};
template<>
struct TestBxDF<bxdf::reflection::SBeckmannBxDF<sample_t, iso_cache, aniso_cache, spectral_t>> : TestBxDFBase<bxdf::reflection::SBeckmannBxDF<sample_t, iso_cache, aniso_cache, spectral_t>>
{
using base_t = TestBxDFBase<bxdf::reflection::SBeckmannBxDF<sample_t, iso_cache, aniso_cache, spectral_t>>;
template<bool aniso>
void initBxDF(SBxDFTestResources _rc)
{
if (aniso)
{
base_t::bxdf = bxdf::reflection::SBeckmannBxDF<sample_t, iso_cache, aniso_cache, spectral_t>::create(rc.alpha.x,rc.alpha.y,(float32_t3)(rc.ior.x),(float32_t3)(rc.ior.y));
#ifndef __HLSL_VERSION
base_t::name = "Beckmann Aniso BRDF";
#endif
}
else
{
base_t::bxdf = bxdf::reflection::SBeckmannBxDF<sample_t, iso_cache, aniso_cache, spectral_t>::create(rc.alpha.x,(float32_t3)(rc.ior.x),(float32_t3)(rc.ior.y));
#ifndef __HLSL_VERSION
base_t::name = "Beckmann BRDF";
#endif
}
}
};
template<>
struct TestBxDF<bxdf::reflection::SGGXBxDF<sample_t, iso_cache, aniso_cache, spectral_t>> : TestBxDFBase<bxdf::reflection::SGGXBxDF<sample_t, iso_cache, aniso_cache, spectral_t>>
{
using base_t = TestBxDFBase<bxdf::reflection::SGGXBxDF<sample_t, iso_cache, aniso_cache, spectral_t>>;
template<bool aniso>
void initBxDF(SBxDFTestResources _rc)
{
if (aniso)
{
base_t::bxdf = bxdf::reflection::SGGXBxDF<sample_t, iso_cache, aniso_cache, spectral_t>::create(rc.alpha.x,rc.alpha.y,(float32_t3)(rc.ior.x),(float32_t3)(rc.ior.y));
#ifndef __HLSL_VERSION
base_t::name = "GGX Aniso BRDF";
#endif
}
else
{
base_t::bxdf = bxdf::reflection::SGGXBxDF<sample_t, iso_cache, aniso_cache, spectral_t>::create(rc.alpha.x,(float32_t3)(rc.ior.x),(float32_t3)(rc.ior.y));
#ifndef __HLSL_VERSION
base_t::name = "GGX BRDF";
#endif
}
}
};
template<>
struct TestBxDF<bxdf::transmission::SSmoothDielectricBxDF<sample_t, iso_cache, aniso_cache, spectral_t>> : TestBxDFBase<bxdf::transmission::SSmoothDielectricBxDF<sample_t, iso_cache, aniso_cache, spectral_t>>
{
using base_t = TestBxDFBase<bxdf::transmission::SSmoothDielectricBxDF<sample_t, iso_cache, aniso_cache, spectral_t>>;
void initBxDF(SBxDFTestResources _rc)
{
base_t::bxdf = bxdf::transmission::SSmoothDielectricBxDF<sample_t, iso_cache, aniso_cache, spectral_t>::create(rc.eta);
#ifndef __HLSL_VERSION
base_t::name = "Smooth dielectric BSDF";
#endif
}
};
template<>
struct TestBxDF<bxdf::transmission::SSmoothDielectricBxDF<sample_t, iso_cache, aniso_cache, spectral_t, true>> : TestBxDFBase<bxdf::transmission::SSmoothDielectricBxDF<sample_t, iso_cache, aniso_cache, spectral_t, true>>
{
using base_t = TestBxDFBase<bxdf::transmission::SSmoothDielectricBxDF<sample_t, iso_cache, aniso_cache, spectral_t, true>>;
void initBxDF(SBxDFTestResources _rc)
{
base_t::bxdf = bxdf::transmission::SSmoothDielectricBxDF<sample_t, iso_cache, aniso_cache, spectral_t, true>::create(float32_t3(rc.eta * rc.eta),rc.luma_coeff);
#ifndef __HLSL_VERSION
base_t::name = "Thin smooth dielectric BSDF";
#endif
}
};
template<>
struct TestBxDF<bxdf::transmission::SBeckmannDielectricBxDF<sample_t, iso_cache, aniso_cache, spectral_t>> : TestBxDFBase<bxdf::transmission::SBeckmannDielectricBxDF<sample_t, iso_cache, aniso_cache, spectral_t>>
{
using base_t = TestBxDFBase<bxdf::transmission::SBeckmannDielectricBxDF<sample_t, iso_cache, aniso_cache, spectral_t>>;
template<bool aniso>
void initBxDF(SBxDFTestResources _rc)
{
if (aniso)
{
base_t::bxdf = bxdf::transmission::SBeckmannDielectricBxDF<sample_t, iso_cache, aniso_cache, spectral_t>::create(rc.eta,rc.alpha.x,rc.alpha.y);
#ifndef __HLSL_VERSION
base_t::name = "Beckmann Dielectric Aniso BSDF";
#endif
}
else
{
base_t::bxdf = bxdf::transmission::SBeckmannDielectricBxDF<sample_t, iso_cache, aniso_cache, spectral_t>::create(rc.eta,rc.alpha.x);
#ifndef __HLSL_VERSION
base_t::name = "Beckmann Dielectric BSDF";
#endif
}
}
};
template<>
struct TestBxDF<bxdf::transmission::SGGXDielectricBxDF<sample_t, iso_cache, aniso_cache, spectral_t>> : TestBxDFBase<bxdf::transmission::SGGXDielectricBxDF<sample_t, iso_cache, aniso_cache, spectral_t>>
{
using base_t = TestBxDFBase<bxdf::transmission::SGGXDielectricBxDF<sample_t, iso_cache, aniso_cache, spectral_t>>;
template<bool aniso>
void initBxDF(SBxDFTestResources _rc)
{
if (aniso)
{
base_t::bxdf = bxdf::transmission::SGGXDielectricBxDF<sample_t, iso_cache, aniso_cache, spectral_t>::create(rc.eta,rc.alpha.x,rc.alpha.y);
#ifndef __HLSL_VERSION
base_t::name = "GGX Dielectric Aniso BSDF";
#endif
}
else
{
base_t::bxdf = bxdf::transmission::SGGXDielectricBxDF<sample_t, iso_cache, aniso_cache, spectral_t>::create(rc.eta,rc.alpha.x);
#ifndef __HLSL_VERSION
base_t::name = "GGX Dielectric BSDF";
#endif
}
}
};
template<class T>
struct is_basic_brdf : bool_constant<
is_same<T, bxdf::reflection::SLambertianBxDF<sample_t, iso_interaction, aniso_interaction, spectral_t>>::value ||
is_same<T, bxdf::reflection::SOrenNayarBxDF<sample_t, iso_interaction, aniso_interaction, spectral_t>>::value
> {};
template<class T>
struct is_microfacet_brdf : bool_constant<
is_same<T, bxdf::reflection::SBeckmannBxDF<sample_t, iso_cache, aniso_cache, spectral_t>>::value ||
is_same<T, bxdf::reflection::SGGXBxDF<sample_t, iso_cache, aniso_cache, spectral_t>>::value
> {};
template<class T>
struct is_basic_bsdf : bool_constant<
is_same<T, bxdf::transmission::SLambertianBxDF<sample_t, iso_interaction, aniso_interaction, spectral_t>>::value ||
is_same<T, bxdf::transmission::SSmoothDielectricBxDF<sample_t, iso_cache, aniso_cache, spectral_t>>::value ||
is_same<T, bxdf::transmission::SSmoothDielectricBxDF<sample_t, iso_cache, aniso_cache, spectral_t, true>>::value
> {};
template<class T>
struct is_microfacet_bsdf : bool_constant<
is_same<T, bxdf::transmission::SBeckmannDielectricBxDF<sample_t, iso_cache, aniso_cache, spectral_t>>::value ||
is_same<T, bxdf::transmission::SGGXDielectricBxDF<sample_t, iso_cache, aniso_cache, spectral_t>>::value
> {};
template<class T>
NBL_CONSTEXPR bool is_basic_brdf_v = is_basic_brdf<T>::value;
template<class T>
NBL_CONSTEXPR bool is_microfacet_brdf_v = is_microfacet_brdf<T>::value;
template<class T>
NBL_CONSTEXPR bool is_basic_bsdf_v = is_basic_bsdf<T>::value;
template<class T>
NBL_CONSTEXPR bool is_microfacet_bsdf_v = is_microfacet_bsdf<T>::value;
template<class BxDF, bool aniso = false>
struct TestJacobian : TestBxDF<BxDF>
{
using base_t = TestBxDFBase<BxDF>;
using this_t = TestJacobian<BxDF, aniso>;
virtual ErrorType compute() override
{
aniso_cache cache, dummy;
iso_cache isocache;
params_t params;
float32_t3 ux = base_t::rc.u + float32_t3(base_t::rc.eps,0,0);
float32_t3 uy = base_t::rc.u + float32_t3(0,base_t::rc.eps,0);
if NBL_CONSTEXPR_FUNC (is_basic_brdf_v<BxDF>)
{
s = base_t::bxdf.generate(base_t::anisointer, base_t::rc.u.xy);
sx = base_t::bxdf.generate(base_t::anisointer, ux.xy);
sy = base_t::bxdf.generate(base_t::anisointer, uy.xy);
params = params_t::template create<sample_t, iso_interaction>(s, base_t::isointer, bxdf::BCM_MAX);
}
if NBL_CONSTEXPR_FUNC (is_microfacet_brdf_v<BxDF>)
{
s = base_t::bxdf.generate(base_t::anisointer, base_t::rc.u.xy, cache);
sx = base_t::bxdf.generate(base_t::anisointer, ux.xy, dummy);
sy = base_t::bxdf.generate(base_t::anisointer, uy.xy, dummy);
if NBL_CONSTEXPR_FUNC (aniso)
params = params_t::template create<sample_t, aniso_interaction, aniso_cache>(s, base_t::anisointer, cache, bxdf::BCM_MAX);
else
{
isocache = (iso_cache)cache;
params = params_t::template create<sample_t, iso_interaction, iso_cache>(s, base_t::isointer, isocache, bxdf::BCM_MAX);
}
}
if NBL_CONSTEXPR_FUNC (is_basic_bsdf_v<BxDF>)
{
s = base_t::bxdf.generate(base_t::anisointer, base_t::rc.u);
sx = base_t::bxdf.generate(base_t::anisointer, ux);
sy = base_t::bxdf.generate(base_t::anisointer, uy);
params = params_t::template create<sample_t, iso_interaction>(s, base_t::isointer, bxdf::BCM_ABS);
}
if NBL_CONSTEXPR_FUNC (is_microfacet_bsdf_v<BxDF>)
{
s = base_t::bxdf.generate(base_t::anisointer, base_t::rc.u, cache);
sx = base_t::bxdf.generate(base_t::anisointer, ux, dummy);
sy = base_t::bxdf.generate(base_t::anisointer, uy, dummy);
if NBL_CONSTEXPR_FUNC (aniso)
params = params_t::template create<sample_t, aniso_interaction, aniso_cache>(s, base_t::anisointer, cache, bxdf::BCM_ABS);
else
{
isocache = (iso_cache)cache;
params = params_t::template create<sample_t, iso_interaction, iso_cache>(s, base_t::isointer, isocache, bxdf::BCM_ABS);
}
}
// TODO: add checks with need clamp trait
if (bxdf_traits<BxDF>::type == BT_BRDF)
{
if (s.NdotL <= bit_cast<float>(numeric_limits<float>::min))
return BET_INVALID;
}
else if (bxdf_traits<BxDF>::type == BT_BSDF)
{
if (abs<float>(s.NdotL) <= bit_cast<float>(numeric_limits<float>::min))
return BET_INVALID;
}
if NBL_CONSTEXPR_FUNC (is_basic_brdf_v<BxDF> || is_basic_bsdf_v<BxDF>)
{
pdf = base_t::bxdf.quotient_and_pdf(params);
bsdf = float32_t3(base_t::bxdf.eval(params));
}
if NBL_CONSTEXPR_FUNC (is_microfacet_brdf_v<BxDF> || is_microfacet_bsdf_v<BxDF>)
{
if NBL_CONSTEXPR_FUNC (aniso)
{
pdf = base_t::bxdf.quotient_and_pdf(params);
bsdf = float32_t3(base_t::bxdf.eval(params));
}
else
{
pdf = base_t::bxdf.quotient_and_pdf(params);
bsdf = float32_t3(base_t::bxdf.eval(params));
}
}
return BET_NONE;
}
ErrorType test()
{
if (bxdf_traits<BxDF>::type == BT_BRDF)
{
if (base_t::isointer.NdotV <= bit_cast<float>(numeric_limits<float>::min))
return BET_INVALID;
}
else if (bxdf_traits<BxDF>::type == BT_BSDF)
{
if (abs<float>(base_t::isointer.NdotV) <= bit_cast<float>(numeric_limits<float>::min))
return BET_INVALID;
}
ErrorType res = compute();
if (res != BET_NONE)
return res;
if (checkZero<float>(pdf.pdf, 1e-5)) // something generated cannot have 0 probability of getting generated
return BET_PDF_ZERO;
if (!checkLt<float32_t3>(pdf.quotient, (float32_t3)numeric_limits<float>::infinity)) // importance sampler's job to prevent inf
return BET_QUOTIENT_INF;
if (checkZero<float32_t3>(bsdf, 1e-5) || checkZero<float32_t3>(pdf.quotient, 1e-5))
return BET_NONE; // produces an "impossible" sample
if (checkLt<float32_t3>(bsdf, (float32_t3)0.0) || checkLt<float32_t3>(pdf.quotient, (float32_t3)0.0) || pdf.pdf < 0.0)
return BET_NEGATIVE_VAL;
// get BET_jacobian
float32_t2x2 m = float32_t2x2(sx.TdotL - s.TdotL, sy.TdotL - s.TdotL, sx.BdotL - s.BdotL, sy.BdotL - s.BdotL);
float det = nbl::hlsl::determinant<float32_t2x2>(m);
if (!checkZero<float>(det * pdf.pdf / s.NdotL, 1e-5))
return BET_JACOBIAN;
if (!checkEq<float32_t3>(pdf.value(), bsdf, 5e-2))
return BET_PDF_EVAL_DIFF;
return BET_NONE;
}
static void run(NBL_CONST_REF_ARG(STestInitParams) initparams, NBL_REF_ARG(FailureCallback) cb)
{
uint32_t2 state = pcg32x2(initparams.state);
this_t t;
t.init(state);
t.rc.state = initparams.state;
if NBL_CONSTEXPR_FUNC (is_microfacet_brdf_v<BxDF> || is_microfacet_bsdf_v<BxDF>)
t.template initBxDF<aniso>(t.rc);
else
t.initBxDF(t.rc);
ErrorType e = t.test();
if (e != BET_NONE)
cb.__call(e, t, initparams.logInfo);
}
sample_t s, sx, sy;
quotient_pdf_t pdf;
float32_t3 bsdf;
};
template<class BxDF, bool aniso = false>
struct TestReciprocity : TestBxDF<BxDF>
{
using base_t = TestBxDFBase<BxDF>;
using this_t = TestReciprocity<BxDF, aniso>;
virtual ErrorType compute() override
{
aniso_cache cache, rec_cache;
iso_cache isocache, rec_isocache;
if NBL_CONSTEXPR_FUNC (is_basic_brdf_v<BxDF>)
{
s = base_t::bxdf.generate(base_t::anisointer, base_t::rc.u.xy);
params = params_t::template create<sample_t, iso_interaction>(s, base_t::isointer, bxdf::BCM_MAX);
}
if NBL_CONSTEXPR_FUNC (is_microfacet_brdf_v<BxDF>)
{
s = base_t::bxdf.generate(base_t::anisointer, base_t::rc.u.xy, cache);
if NBL_CONSTEXPR_FUNC (aniso)
params = params_t::template create<sample_t, aniso_interaction, aniso_cache>(s, base_t::anisointer, cache, bxdf::BCM_MAX);
else
{
isocache = (iso_cache)cache;
params = params_t::template create<sample_t, iso_interaction, iso_cache>(s, base_t::isointer, isocache, bxdf::BCM_MAX);
}
}
if NBL_CONSTEXPR_FUNC (is_basic_bsdf_v<BxDF>)
{
s = base_t::bxdf.generate(base_t::anisointer, base_t::rc.u);
params = params_t::template create<sample_t, iso_interaction>(s, base_t::isointer, bxdf::BCM_ABS);
}
if NBL_CONSTEXPR_FUNC (is_microfacet_bsdf_v<BxDF>)
{
s = base_t::bxdf.generate(base_t::anisointer, base_t::rc.u, cache);
if NBL_CONSTEXPR_FUNC (aniso)
params = params_t::template create<sample_t, aniso_interaction, aniso_cache>(s, base_t::anisointer, cache, bxdf::BCM_ABS);
else
{
isocache = (iso_cache)cache;
params = params_t::template create<sample_t, iso_interaction, iso_cache>(s, base_t::isointer, isocache, bxdf::BCM_ABS);
}
}
// TODO: add checks with need clamp trait
if (bxdf_traits<BxDF>::type == BT_BRDF)
{
if (s.NdotL <= bit_cast<float>(numeric_limits<float>::min))
return BET_INVALID;
}
else if (bxdf_traits<BxDF>::type == BT_BSDF)
{
if (abs<float>(s.NdotL) <= bit_cast<float>(numeric_limits<float>::min))
return BET_INVALID;
}
float32_t3x3 toTangentSpace = base_t::anisointer.getToTangentSpace();
ray_dir_info_t rec_V = s.L;
ray_dir_info_t rec_localV = ray_dir_info_t::transform(toTangentSpace, rec_V);
ray_dir_info_t rec_localL = ray_dir_info_t::transform(toTangentSpace, base_t::rc.V);
rec_s = sample_t::createFromTangentSpace(rec_localV.direction, rec_localL, base_t::anisointer.getFromTangentSpace());
iso_interaction rec_isointer = iso_interaction::create(rec_V, base_t::rc.N);
aniso_interaction rec_anisointer = aniso_interaction::create(rec_isointer, base_t::rc.T, base_t::rc.B);
rec_cache = cache;
rec_cache.VdotH = cache.LdotH;
rec_cache.LdotH = cache.VdotH;
rec_isocache = (iso_cache)rec_cache;
if NBL_CONSTEXPR_FUNC (is_basic_brdf_v<BxDF>)
rec_params = params_t::template create<sample_t, iso_interaction>(rec_s, rec_isointer, bxdf::BCM_MAX);
if NBL_CONSTEXPR_FUNC (is_microfacet_brdf_v<BxDF>)
{
if NBL_CONSTEXPR_FUNC (aniso)
rec_params = params_t::template create<sample_t, aniso_interaction, aniso_cache>(rec_s, rec_anisointer, rec_cache, bxdf::BCM_MAX);
else
{
rec_isocache = (iso_cache)rec_cache;
rec_params = params_t::template create<sample_t, iso_interaction, iso_cache>(rec_s, rec_isointer, rec_isocache, bxdf::BCM_MAX);
}
}
if NBL_CONSTEXPR_FUNC (is_basic_bsdf_v<BxDF>)
rec_params = params_t::template create<sample_t, iso_interaction>(rec_s, rec_isointer, bxdf::BCM_ABS);
if NBL_CONSTEXPR_FUNC (is_microfacet_bsdf_v<BxDF>)
{
if NBL_CONSTEXPR_FUNC (aniso)
rec_params = params_t::template create<sample_t, aniso_interaction, aniso_cache>(rec_s, rec_anisointer, rec_cache, bxdf::BCM_ABS);
else
{
rec_isocache = (iso_cache)rec_cache;
rec_params = params_t::template create<sample_t, iso_interaction, iso_cache>(rec_s, rec_isointer, rec_isocache, bxdf::BCM_ABS);
}
}
if NBL_CONSTEXPR_FUNC (is_basic_brdf_v<BxDF> || is_basic_bsdf_v<BxDF>)
{
bsdf = float32_t3(base_t::bxdf.eval(params));
rec_bsdf = float32_t3(base_t::bxdf.eval(rec_params));
}
if NBL_CONSTEXPR_FUNC (is_microfacet_brdf_v<BxDF> || is_microfacet_bsdf_v<BxDF>)
{
if NBL_CONSTEXPR_FUNC (aniso)
{
bsdf = float32_t3(base_t::bxdf.eval(params));
rec_bsdf = float32_t3(base_t::bxdf.eval(rec_params));
}
else
{
bsdf = float32_t3(base_t::bxdf.eval(params));
rec_bsdf = float32_t3(base_t::bxdf.eval(rec_params));
}
}
return BET_NONE;
}
ErrorType test()
{
if (bxdf_traits<BxDF>::type == BT_BRDF)
{
if (base_t::isointer.NdotV <= bit_cast<float>(numeric_limits<float>::min))
return BET_INVALID;
}
else if (bxdf_traits<BxDF>::type == BT_BSDF)
{
if (abs<float>(base_t::isointer.NdotV) <= bit_cast<float>(numeric_limits<float>::min))
return BET_INVALID;
}
ErrorType res = compute();
if (res != BET_NONE)
return res;
if (checkZero<float32_t3>(bsdf, 1e-5))
return BET_NONE; // produces an "impossible" sample
if (checkLt<float32_t3>(bsdf, (float32_t3)0.0))
return BET_NEGATIVE_VAL;
float32_t3 a = bsdf * nbl::hlsl::abs<float>(params.NdotV);
float32_t3 b = rec_bsdf * nbl::hlsl::abs<float>(rec_params.NdotV);
if (!(a == b)) // avoid division by 0
if (!checkEq<float32_t3>(a, b, 1e-2))
return BET_RECIPROCITY;
return BET_NONE;
}
static void run(NBL_CONST_REF_ARG(STestInitParams) initparams, NBL_REF_ARG(FailureCallback) cb)
{
uint32_t2 state = pcg32x2(initparams.state);
this_t t;
t.init(state);
t.rc.state = initparams.state;
if NBL_CONSTEXPR_FUNC (is_microfacet_brdf_v<BxDF> || is_microfacet_bsdf_v<BxDF>)
t.template initBxDF<aniso>(t.rc);
else
t.initBxDF(t.rc);
ErrorType e = t.test();
if (e != BET_NONE)
cb.__call(e, t, initparams.logInfo);
}
sample_t s, rec_s;
float32_t3 bsdf, rec_bsdf;
params_t params, rec_params;
};
#ifndef __HLSL_VERSION // because unordered_map
template<class BxDF, bool aniso = false>
struct TestBucket : TestBxDF<BxDF>
{
using base_t = TestBxDFBase<BxDF>;
using this_t = TestBucket<BxDF, aniso>;
void clearBuckets()
{
for (float y = -1.0f; y < 1.0f; y += stride)
{
for (float x = -1.0f; x < 1.0f; x += stride)
{
buckets[float32_t2(x, y)] = 0;
}
}
}
float bin(float a)
{
float diff = std::fmod(a, stride);
float b = (a < 0) ? -stride : 0.0f;
return a - diff + b;
}
virtual ErrorType compute() override
{
clearBuckets();
aniso_cache cache;
iso_cache isocache;
params_t params;
sample_t s;
quotient_pdf_t pdf;
float32_t3 bsdf;
for (uint32_t i = 0; i < numSamples; i++)
{
float32_t3 u = float32_t3(rngUniformDist<float32_t2>(base_t::rc.rng), 0.0);
if NBL_CONSTEXPR_FUNC (is_basic_brdf_v<BxDF>)
{
s = base_t::bxdf.generate(base_t::anisointer, u.xy);
params = params_t::template create<sample_t, iso_interaction>(s, base_t::isointer, bxdf::BCM_MAX);
}
if NBL_CONSTEXPR_FUNC (is_microfacet_brdf_v<BxDF>)
{
s = base_t::bxdf.generate(base_t::anisointer, u.xy, cache);
if NBL_CONSTEXPR_FUNC (aniso)
params = params_t::template create<sample_t, aniso_interaction, aniso_cache>(s, base_t::anisointer, cache, bxdf::BCM_MAX);
else
{
isocache = (iso_cache)cache;
params = params_t::template create<sample_t, iso_interaction, iso_cache>(s, base_t::isointer, isocache, bxdf::BCM_MAX);
}
}
if NBL_CONSTEXPR_FUNC (is_basic_bsdf_v<BxDF>)
{
s = base_t::bxdf.generate(base_t::anisointer, u);
params = params_t::template create<sample_t, iso_interaction>(s, base_t::isointer, bxdf::BCM_ABS);
}
if NBL_CONSTEXPR_FUNC (is_microfacet_bsdf_v<BxDF>)
{
s = base_t::bxdf.generate(base_t::anisointer, u, cache);
if NBL_CONSTEXPR_FUNC (aniso)
params = params_t::template create<sample_t, aniso_interaction, aniso_cache>(s, base_t::anisointer, cache, bxdf::BCM_ABS);
else
{
isocache = (iso_cache)cache;
params = params_t::template create<sample_t, iso_interaction, iso_cache>(s, base_t::isointer, isocache, bxdf::BCM_ABS);
}
}
if NBL_CONSTEXPR_FUNC (is_basic_brdf_v<BxDF> || is_basic_bsdf_v<BxDF>)
{
pdf = base_t::bxdf.quotient_and_pdf(params);
bsdf = float32_t3(base_t::bxdf.eval(params));
}
if NBL_CONSTEXPR_FUNC (is_microfacet_brdf_v<BxDF> || is_microfacet_bsdf_v<BxDF>)
{
if NBL_CONSTEXPR_FUNC (aniso)
{
pdf = base_t::bxdf.quotient_and_pdf(params);
bsdf = float32_t3(base_t::bxdf.eval(params));
}
else
{
pdf = base_t::bxdf.quotient_and_pdf(params);
bsdf = float32_t3(base_t::bxdf.eval(params));
}
}
// put s into bucket
float32_t3x3 toTangentSpace = base_t::anisointer.getToTangentSpace();
const ray_dir_info_t localL = ray_dir_info_t::transform(toTangentSpace, s.L);
const float32_t2 coords = cartesianToPolar(localL.direction);
float32_t2 bucket = float32_t2(bin(coords.x * numbers::inv_pi<float>), bin(coords.y * 0.5f * numbers::inv_pi<float>));
if (pdf.pdf == numeric_limits<float>::infinity)
buckets[bucket] += 1;
}
#ifndef __HLSL_VERSION
// double check this conversion makes sense
for (auto const& b : buckets) {
if (!selective || b.second > 0)
{
const float32_t3 v = polarToCartesian(b.first * float32_t2(1, 2) * numbers::pi<float>);
base_t::errMsg += std::format("({:.3f},{:.3f},{:.3f}): {}\n", v.x, v.y, v.z, b.second);
}
}
#endif
return BET_NONE;
}
ErrorType test()
{
if (bxdf_traits<BxDF>::type == BT_BRDF)
{
if (base_t::isointer.NdotV <= bit_cast<float>(numeric_limits<float>::min))
return BET_INVALID;
}
else if (bxdf_traits<BxDF>::type == BT_BSDF)
{
if (abs<float>(base_t::isointer.NdotV) <= bit_cast<float>(numeric_limits<float>::min))
return BET_INVALID;
}
ErrorType res = compute();
if (res != BET_NONE)
return res;
return (base_t::errMsg.length() == 0) ? BET_NONE : BET_PRINT_MSG;
}
static void run(NBL_CONST_REF_ARG(STestInitParams) initparams, NBL_REF_ARG(FailureCallback) cb)
{
uint32_t2 state = pcg32x2(initparams.state);
this_t t;
t.init(state);
t.rc.state = initparams.state;
t.numSamples = initparams.samples;
if NBL_CONSTEXPR_FUNC (is_microfacet_brdf_v<BxDF> || is_microfacet_bsdf_v<BxDF>)
t.template initBxDF<aniso>(t.rc);
else
t.initBxDF(t.rc);
ErrorType e = t.test();
if (e != BET_NONE)
cb.__call(e, t, initparams.logInfo);
}
bool selective = true; // print only buckets with count > 0
float stride = 0.2f;
uint32_t numSamples = 500;
std::unordered_map<float32_t2, uint32_t, std::hash<float32_t2>> buckets;
};
inline float adaptiveSimpson(const std::function<float(float)>& f, float x0, float x1, float eps = 1e-6, int depth = 6)
{
int count = 0;
std::function<float(float, float, float, float, float, float, float, float, int)> integrate =
[&](float a, float b, float c, float fa, float fb, float fc, float I, float eps, int depth)
{
float d = 0.5f * (a + b);
float e = 0.5f * (b + c);
float fd = f(d);
float fe = f(e);
float h = c - a;
float I0 = (1.0f / 12.0f) * h * (fa + 4 * fd + fb);
float I1 = (1.0f / 12.0f) * h * (fb + 4 * fe + fc);
float Ip = I0 + I1;
count++;
if (depth <= 0 || std::abs(Ip - I) < 15 * eps)
return Ip + (1.0f / 15.0f) * (Ip - I);
return integrate(a, d, b, fa, fd, fb, I0, .5f * eps, depth - 1) +
integrate(b, e, c, fb, fe, fc, I1, .5f * eps, depth - 1);
};
float a = x0;
float b = 0.5f * (x0 + x1);
float c = x1;
float fa = f(a);
float fb = f(b);
float fc = f(c);
float I = (c - a) * (1.0f / 6.0f) * (fa + 4.f * fb + fc);
return integrate(a, b, c, fa, fb, fc, I, eps, depth);
}
inline float adaptiveSimpson2D(const std::function<float(float, float)>& f, float32_t2 x0, float32_t2 x1, float eps = 1e-6, int depth = 6)
{
const auto integrate = [&](float y) -> float
{
return adaptiveSimpson(std::bind(f, std::placeholders::_1, y), x0.x, x1.x, eps, depth);
};
return adaptiveSimpson(integrate, x0.y, x1.y, eps, depth);
}
// adapted from pbrt chi2 test: https://github.com/mmp/pbrt-v4/blob/792aaaa08d97dbedf11a3bb23e246b6443d847b4/src/pbrt/bsdfs_test.cpp#L280
template<class BxDF, bool aniso = false>
struct TestChi2 : TestBxDF<BxDF>
{
using base_t = TestBxDFBase<BxDF>;
using this_t = TestChi2<BxDF, aniso>;
void clearBuckets()
{
const uint32_t freqSize = thetaSplits * phiSplits;
countFreq.resize(freqSize);
std::fill(countFreq.begin(), countFreq.end(), 0);
integrateFreq.resize(freqSize);
std::fill(integrateFreq.begin(), integrateFreq.end(), 0);
}
double RLGamma(double a, double x) {
const double epsilon = 0.000000000000001;
const double big = 4503599627370496.0;
const double bigInv = 2.22044604925031308085e-16;
assert(a >= 0 && x >= 0);
if (x == 0)
return 0.0f;
double ax = (a * std::log(x)) - x - std::lgamma(a);
if (ax < -709.78271289338399)
return a < x ? 1.0 : 0.0;
if (x <= 1 || x <= a)
{
double r2 = a;