File: Principled.ispc

package info (click to toggle)
ospray 3.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,048 kB
  • sloc: cpp: 80,569; ansic: 951; sh: 805; makefile: 170; python: 69
file content (1078 lines) | stat: -rw-r--r-- 34,611 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
// Copyright 2009 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#include "Principled.ih"

#include "Medium.ih"
#include "common/Ray.ih"
#include "render/Material.ih"
#include "render/bsdfs/Conductor.ih"
#include "render/bsdfs/Dielectric.ih"
#include "render/bsdfs/DielectricLayer.ih"
#include "render/bsdfs/Lambert.ih"
#include "render/bsdfs/LambertTransmission.ih"
#include "render/bsdfs/MicrofacetConductor.ih"
#include "render/bsdfs/MicrofacetDielectric.ih"
#include "render/bsdfs/MicrofacetDielectricLayer.ih"
#include "render/bsdfs/MicrofacetSheenLayer.ih"
#include "render/bsdfs/MultiBSDF.ih"
#include "render/bsdfs/OrenNayar.ih"
#include "render/bsdfs/PassthroughLayer.ih"
#include "render/bsdfs/ThinDielectric.ih"
#include "render/bsdfs/ThinMicrofacetDielectric.ih"
#include "render/bsdfs/Transmission.ih"
#include "texture/TextureParam.ih"
// c++ shared
#include "PrincipledShared.h"

///////////////////////////////////////////////////////////////////////////////
// Implementation

OSPRAY_BEGIN_ISPC_NAMESPACE

struct Principled_Diffuse_BSDF
{
  DEFINE_MULTIBSDF(2);
  OrenNayar lambert;
  BSDF lambertTransmission;
};

struct Principled_Plastic_BSDF
{
  MicrofacetDielectricLayer dielectricLayer;
  Principled_Diffuse_BSDF diffuse;
};

struct Principled_Glass_BSDF
{
  int bsdfId;
  MicrofacetDielectric dielectric;
  ThinMicrofacetDielectric thinDielectric;
  BSDF transmission;
};

struct Principled_Base_MultiBSDF
{
  DEFINE_MULTIBSDF(3);
  MicrofacetConductor metal;
  Principled_Glass_BSDF glass;
  Principled_Plastic_BSDF plastic;
};

struct Principled_ClearCoat_BSDF
{
  MicrofacetDielectricLayer dielectricLayer;
  Principled_Base_MultiBSDF base;
};

struct Principled_SheenLayer_BSDF
{
  MicrofacetSheenLayer sheenLayer;
  Principled_ClearCoat_BSDF clearCoat;
};

struct Principled_BSDF
{
  DEFINE_MULTIBSDF(2);
  BSDF transmission;
  Principled_SheenLayer_BSDF sheen;
};

SYCL_EXTERNAL const varying BSDF *uniform Principled_getBSDF(
    const uniform Material *uniform super,
    uniform ShadingContext *uniform ctx,
    const DifferentialGeometry &dg,
    const Ray &,
    const Medium &currentMedium,
    const uniform FeatureFlagsHandler &)
{
  const uniform Principled *uniform self =
      (const uniform Principled *uniform)super;

  // Allocate memory and initialize material BSDF
  varying Principled_BSDF *uniform bsdf = (varying Principled_BSDF * uniform)
      ShadingContext_alloc(ctx, sizeof(Principled_BSDF));
  varying MultiBSDF *uniform multiRoot = (varying MultiBSDF * uniform) bsdf;
  MultiBSDF_Constructor(multiRoot, 2);
  bsdf->super.bsdfType = BSDF_TYPE_PRINCIPLED;

  // create the default shading frame
  varying linear3f *uniform frame = LinearSpace3f_create(ctx,
      makeShadingFrame(dg, self->normalMap, self->normalRot, self->normal));
  const float rotation =
      clamp(self->rotation * get1f(self->rotationMap, dg, 1.f));
  const float theta = 2.f * (float)pi * rotation;
  if (theta > 0.f) {
    frame->vx = rotate(frame->vx, frame->vz, theta);
    frame->vy = cross(frame->vz, frame->vx);
  }

  float opacity = clamp(self->opacity * get1f(self->opacityMap, dg, 1.f));
  if (hasAlpha(self->baseColorMap)) {
    vec4f baseColorSample = get4f(self->baseColorMap, dg, make_vec4f(1.f));
    opacity *= baseColorSample.w;
  }

  if (self->opacity > EPS) {
    varying linear3f *uniform baseFrame = frame;
    if (valid(self->baseNormalMap)) {
      baseFrame = LinearSpace3f_create(ctx,
          makeShadingFrame(
              dg, self->baseNormalMap, self->baseNormalRot, self->baseNormal));
      if (theta > 0.f) {
        baseFrame->vx = rotate(baseFrame->vx, baseFrame->vz, theta);
        baseFrame->vy = cross(baseFrame->vz, baseFrame->vx);
      }
    }

    varying MultiBSDF *uniform multiBase =
        (varying MultiBSDF * uniform) & bsdf->sheen.clearCoat.base;
    MultiBSDF_Constructor(multiBase, 3);

    const vec3f baseColor =
        clamp(self->baseColor * get3f(self->baseColorMap, dg, make_vec3f(1.f))
            * make_vec3f(dg.color));
    const float specular =
        clamp(self->specular * get1f(self->specularMap, dg, 1.f));
    const float metallic =
        clamp(self->metallic * get1f(self->metallicMap, dg, 1.f));
    const float roughness =
        clamp(self->roughness * get1f(self->roughnessMap, dg, 1.f));
    const float anisotropy =
        clamp(self->anisotropy * get1f(self->anisotropyMap, dg, 1.f));
    const bool fromOutside =
        self->thin ? true : eq(currentMedium, self->outsideMedium);

    // dielectric base
    const float dielectric = (1.f - metallic);
    if (dielectric > EPS) {
      const float transmission =
          clamp(self->transmission * get1f(self->transmissionMap, dg, 1.f));

      float ior = self->ior * get1f(self->iorMap, dg, 1.f);
      if (ior < 1.f)
        ior = rcp(ior);
      float eta = fromOutside ? self->outsideMedium.ior * rcp(ior)
                              : ior * rcp(self->outsideMedium.ior);
      eta = clamp(eta, 1.f / 3.f, 3.f); // clamp to common range due to LUTs

      // plastic base
      const float plastic = dielectric * (1.f - transmission);
      if (plastic > EPS) {
        varying MultiBSDF *uniform multiDiffuse = (varying MultiBSDF * uniform)
            & bsdf->sheen.clearCoat.base.plastic.diffuse;
        MultiBSDF_Constructor(multiDiffuse, 2);

        varying Principled_Plastic_BSDF *uniform plasticBsdf =
            &bsdf->sheen.clearCoat.base.plastic;

        // diffuse
        const float diffuse =
            clamp(self->diffuse * get1f(self->diffuseMap, dg, 1.f));
        const vec3f diffuseColor = baseColor * diffuse;
        const float backlight = self->thin
            ? clamp(self->backlight * get1f(self->backlightMap, dg, 1.f),
                  0.f,
                  2.f)
            : 0.f;
        const float diffuseTransmission = backlight * 0.5f;
        const float diffuseReflection = 1.f - diffuseTransmission;

        if (diffuseReflection > EPS) {
          if (self->roughness < EPS)
            Lambert_Constructor(
                &plasticBsdf->diffuse.lambert.super, baseFrame, diffuseColor);
          else
            OrenNayar_Constructor(&plasticBsdf->diffuse.lambert,
                baseFrame,
                diffuseColor,
                roughness);

          MultiBSDF_add(multiDiffuse,
              0,
              &plasticBsdf->diffuse.lambert.super,
              diffuseReflection,
              diffuseReflection);
        }

        if (diffuseTransmission > EPS) {
          LambertTransmission_Constructor(
              &plasticBsdf->diffuse.lambertTransmission,
              baseFrame,
              diffuseColor);
          MultiBSDF_add(multiDiffuse,
              1,
              &plasticBsdf->diffuse.lambertTransmission,
              diffuseTransmission,
              diffuseTransmission);
        }

        // specular
        if (self->specular > EPS) {
          if (self->roughness < EPS) {
            DielectricLayer_Constructor((varying DielectricLayer * uniform)
                    & plasticBsdf->dielectricLayer,
                baseFrame,
                &plasticBsdf->diffuse.super,
                eta,
                make_vec3f(1.f),
                1.f,
                specular);
          } else {
            MicrofacetDielectricLayer_Constructor(&plasticBsdf->dielectricLayer,
                super->microfacetAlbedoTables,
                baseFrame,
                &plasticBsdf->diffuse.super,
                eta,
                make_vec3f(1.f),
                1.f,
                roughness,
                anisotropy,
                specular);
          }
        } else {
          PassthroughLayer_Constructor((varying PassthroughLayer * uniform)
                  & plasticBsdf->dielectricLayer,
              &plasticBsdf->diffuse.super);
        }

        MultiBSDF_add(multiBase,
            2,
            &plasticBsdf->dielectricLayer.super,
            plastic,
            plastic * max(diffuse, specular));
      }

      // glass base
      const float glass = dielectric * transmission * specular;
      if (glass > EPS) {
        varying Principled_Glass_BSDF *uniform glassBsdf =
            &bsdf->sheen.clearCoat.base.glass;

        if (abs(eta - 1.f) > EPS) {
          if (!self->thin) {
            if (self->roughness < EPS)
              Dielectric_Constructor(
                  (varying Dielectric * uniform) & glassBsdf->dielectric,
                  baseFrame,
                  eta);
            else
              MicrofacetDielectric_Constructor(&glassBsdf->dielectric,
                  super->microfacetAlbedoTables,
                  baseFrame,
                  eta,
                  roughness,
                  anisotropy);

            glassBsdf->bsdfId = 1;
            MultiBSDF_add(
                multiBase, 1, &glassBsdf->dielectric.super, glass, glass);
          } else {
            // thin
            const vec3f transmissionColor = clamp(self->transmissionColor
                * get3f(self->transmissionColorMap, dg, make_vec3f(1.f)));
            const float transmissionDepth = max(self->transmissionDepth
                    * get1f(self->transmissionDepthMap, dg, 1.f),
                EPS);
            const float thickness =
                max(self->thickness * get1f(self->thicknessMap, dg, 1.f), 0.f);
            const vec3f attenuation =
                logf(transmissionColor) / transmissionDepth * thickness;

            if (self->roughness < EPS)
              ThinDielectric_Constructor((varying ThinDielectric * uniform)
                      & glassBsdf->thinDielectric,
                  baseFrame,
                  eta,
                  attenuation);
            else
              ThinMicrofacetDielectric_Constructor(&glassBsdf->thinDielectric,
                  super->microfacetAlbedoTables,
                  baseFrame,
                  eta,
                  roughness,
                  anisotropy,
                  attenuation);

            glassBsdf->bsdfId = 2;
            MultiBSDF_add(
                multiBase, 1, &glassBsdf->thinDielectric.super, glass, glass);
          }
        } else {
          Transmission_Constructor(
              &glassBsdf->transmission, baseFrame, make_vec3f(1.f));
          glassBsdf->bsdfId = 0;
          MultiBSDF_add(multiBase, 1, &glassBsdf->transmission, glass, glass);
        }
      }
    }

    // conductor base
    const float conductor = metallic * specular;
    if (conductor > EPS) {
      const vec3f edgeColor = clamp(
          self->edgeColor * get3f(self->edgeColorMap, dg, make_vec3f(1.f)));

      Fresnel *uniform fresnel =
          FresnelConductorArtistic_create(ctx, baseColor, edgeColor);
      varying MicrofacetConductor *uniform metalBsdf =
          &bsdf->sheen.clearCoat.base.metal;
      if (self->roughness < EPS)
        Conductor_Constructor(
            (varying Conductor * uniform) metalBsdf, baseFrame, fresnel);
      else
        MicrofacetConductor_Constructor(metalBsdf,
            super->microfacetAlbedoTables,
            baseFrame,
            fresnel,
            roughness,
            anisotropy);

      MultiBSDF_add(multiBase, 0, &metalBsdf->super, conductor, conductor);
    }

    // coatings
    varying linear3f *uniform coatFrame = frame;

    // clear coat
    if (self->coat > EPS) {
      const float coat = clamp(self->coat * get1f(self->coatMap, dg, 1.f));
      float coatIor = self->coatIor * get1f(self->coatIorMap, dg, 1.f);
      if (coatIor < 1.f)
        coatIor = rcp(coatIor);
      float coatEta = fromOutside ? self->outsideMedium.ior * rcp(coatIor)
                                  : coatIor * rcp(self->outsideMedium.ior);
      coatEta =
          clamp(coatEta, 1.f / 3.f, 3.f); // clamp to common range due to LUTs

      const vec3f coatColor = clamp(
          self->coatColor * get3f(self->coatColorMap, dg, make_vec3f(1.f)));
      const float coatThickness = max(
          self->coatThickness * get1f(self->coatThicknessMap, dg, 1.f), 0.f);

      if (valid(self->coatNormalMap)) {
        coatFrame = LinearSpace3f_create(ctx,
            makeShadingFrame(dg,
                self->coatNormalMap,
                self->coatNormalRot,
                self->coatNormal));
      }

      if (self->coatRoughness < EPS || abs(coatEta - 1.f) < EPS) {
        DielectricLayer_Constructor((varying DielectricLayer * uniform)
                & bsdf->sheen.clearCoat.dielectricLayer,
            coatFrame,
            &bsdf->sheen.clearCoat.base.super,
            coatEta,
            coatColor,
            coatThickness,
            coat);
      } else {
        const float coatRoughness =
            clamp(self->coatRoughness * get1f(self->coatRoughnessMap, dg, 1.f));
        MicrofacetDielectricLayer_Constructor(
            &bsdf->sheen.clearCoat.dielectricLayer,
            super->microfacetAlbedoTables,
            coatFrame,
            &bsdf->sheen.clearCoat.base.super,
            coatEta,
            coatColor,
            coatThickness,
            coatRoughness,
            0.f,
            coat);
      }
    } else {
      PassthroughLayer_Constructor((varying PassthroughLayer * uniform)
              & bsdf->sheen.clearCoat.dielectricLayer,
          &bsdf->sheen.clearCoat.base.super);
    }

    // sheen
    if (self->sheen > EPS) {
      const float sheen = clamp(self->sheen * get1f(self->sheenMap, dg, 1.f));
      vec3f sheenColor = clamp(
          self->sheenColor * get3f(self->sheenColorMap, dg, make_vec3f(1.f)));
      const float sheenTint =
          clamp(self->sheenTint * get1f(self->sheenTintMap, dg, 1.f));
      sheenColor = lerp(sheenTint, sheenColor, baseColor);
      const float sheenRoughness =
          clamp(self->sheenRoughness * get1f(self->sheenRoughnessMap, dg, 1.f));

      MicrofacetSheenLayer_Constructor(&bsdf->sheen.sheenLayer,
          super->microfacetAlbedoTables,
          coatFrame,
          &bsdf->sheen.clearCoat.dielectricLayer.super,
          sheenColor,
          sheenRoughness,
          sheen);
    } else
      PassthroughLayer_Constructor(
          (varying PassthroughLayer * uniform) & bsdf->sheen.sheenLayer,
          &bsdf->sheen.clearCoat.dielectricLayer.super);

    MultiBSDF_add(
        multiRoot, 1, &bsdf->sheen.sheenLayer.super, opacity, opacity);
  }

  // cut-out transparency
  if (1.f - self->opacity > EPS || valid(self->opacityMap)
      || hasAlpha(self->baseColorMap)) {
    const float transparency = 1.f - opacity;
    Transmission_Constructor(
        &bsdf->transmission, frame, make_vec3f(transparency));
    MultiBSDF_add(multiRoot, 0, &bsdf->transmission, 1.f, transparency);
  }

  return &bsdf->super;
}

SYCL_EXTERNAL vec3f Principled_getTransparency(
    const uniform Material *uniform super,
    const DifferentialGeometry &dg,
    const Ray &ray,
    const Medium &currentMedium,
    const uniform FeatureFlagsHandler &)
{
  const uniform Principled *uniform self =
      (const uniform Principled *uniform)super;
  vec3f T = make_vec3f(0.f);

  float opacity = clamp(self->opacity * get1f(self->opacityMap, dg, 1.f));
  if (hasAlpha(self->baseColorMap)) {
    vec4f baseColorSample = get4f(self->baseColorMap, dg, make_vec4f(1.f));
    opacity *= baseColorSample.w;
  }

  const float transparency = 1.f - opacity;
  const float transmission =
      clamp(self->transmission * get1f(self->transmissionMap, dg, 1.f));

  // early exit
  if (transparency <= EPS && transmission <= EPS)
    return T;

  // cut-out opacity
  if (self->opacity > EPS) {
    // glass base
    const float specular =
        clamp(self->specular * get1f(self->specularMap, dg, 1.f));
    const float metallic =
        clamp(self->metallic * get1f(self->metallicMap, dg, 1.f));
    const float dielectric = (1.f - metallic);
    const float glass = dielectric * transmission * specular;

    if (glass > EPS) {
      const bool fromOutside =
          self->thin ? true : eq(currentMedium, self->outsideMedium);
      float ior = self->ior * get1f(self->iorMap, dg, 1.f);
      if (ior < 1.f)
        ior = rcp(ior);
      float eta = fromOutside ? self->outsideMedium.ior * rcp(ior)
                              : ior * rcp(self->outsideMedium.ior);
      eta = clamp(eta, 1.f / 3.f, 3.f); // clamp to common range due to LUTs
      const float cosThetaO = max(-dot(ray.dir, dg.Ns), 0.f);

      if (abs(eta - 1.f) > EPS) {
        if (!self->thin) {
          // solid
          // use microfacet if textured due to different
          // transparent shadow behavior
          if (self->roughness < EPS)
            T = Dielectric_getTransparency(cosThetaO, eta);
        } else {
          // thin
          const vec3f transmissionColor = clamp(self->transmissionColor
              * get3f(self->transmissionColorMap, dg, make_vec3f(1.f)));
          const float transmissionDepth = max(self->transmissionDepth
                  * get1f(self->transmissionDepthMap, dg, 1.f),
              EPS);
          const float thickness =
              max(self->thickness * get1f(self->thicknessMap, dg, 1.f), 0.f);
          const vec3f attenuation =
              logf(transmissionColor) / transmissionDepth * thickness;

          if (self->roughness < EPS) {
            T = ThinDielectric_getTransparency(cosThetaO, eta, attenuation);
          } else {
            const float roughness =
                clamp(self->roughness * get1f(self->roughnessMap, dg, 1.f));
            T = ThinMicrofacetDielectric_getTransparency(
                super->microfacetAlbedoTables,
                cosThetaO,
                eta,
                roughness,
                attenuation);
          }
        }
      } else {
        T = make_vec3f(1.f);
      }

      if (reduce_max(T) > 0.f) {
        // clear coat
        if (self->coat > EPS) {
          const float coat = clamp(self->coat * get1f(self->coatMap, dg, 1.f));
          float coatIor = self->coatIor * get1f(self->coatIorMap, dg, 1.f);
          if (coatIor < 1.f)
            coatIor = rcp(coatIor);
          float coatEta = fromOutside ? self->outsideMedium.ior * rcp(coatIor)
                                      : coatIor * rcp(self->outsideMedium.ior);
          coatEta = clamp(
              coatEta, 1.f / 3.f, 3.f); // clamp to common range due to LUTs

          if (abs(coatEta - 1.f) > EPS) {
            const float coatEta = fromOutside
                ? self->outsideMedium.ior * rcp(coatIor)
                : coatIor * rcp(self->outsideMedium.ior);
            const vec3f coatColor = clamp(self->coatColor
                * get3f(self->coatColorMap, dg, make_vec3f(1.f)));
            const float coatThickness = max(
                self->coatThickness * get1f(self->coatThicknessMap, dg, 1.f),
                0.f);

            if (self->coatRoughness < EPS) {
              T = T
                  * DielectricLayer_getTransparency(
                      cosThetaO, coatEta, coatColor, coatThickness, coat);
            } else {
              const float coatRoughness = clamp(
                  self->coatRoughness * get1f(self->coatRoughnessMap, dg, 1.f));
              T = T
                  * MicrofacetDielectricLayer_getTransparency(
                      super->microfacetAlbedoTables,
                      cosThetaO,
                      coatEta,
                      coatColor,
                      coatThickness,
                      coatRoughness,
                      0.f,
                      coat);
            }
          }
        }

        // sheen
        if (self->sheen > EPS) {
          const float sheen =
              clamp(self->sheen * get1f(self->sheenMap, dg, 1.f));
          const float sheenRoughness = clamp(
              self->sheenRoughness * get1f(self->sheenRoughnessMap, dg, 1.f));

          T = T
              * MicrofacetSheenLayer_getTransparency(
                  super->microfacetAlbedoTables,
                  cosThetaO,
                  sheenRoughness,
                  sheen);
        }

        T = T * glass;
      }
    }
  }

  // cut-out transparency
  T = T * opacity + transparency;

  return T;
}

SYCL_EXTERNAL void Principled_selectNextMedium(
    const uniform Material *uniform super,
    const DifferentialGeometry &dg,
    Medium &currentMedium)
{
  const uniform Principled *uniform self =
      (const uniform Principled *uniform)super;

  if (self->thin || self->transmission <= EPS)
    return;

  if (eq(currentMedium, self->outsideMedium)) {
    float ior = self->ior * get1f(self->iorMap, dg, 1.f);
    if (ior < 1.f)
      ior = rcp(ior);
    const vec3f transmissionColor = clamp(self->transmissionColor
        * get3f(self->transmissionColorMap, dg, make_vec3f(1.f)));
    const float transmissionDepth = max(
        self->transmissionDepth * get1f(self->transmissionDepthMap, dg, 1.f),
        EPS);

    currentMedium.ior = ior;
    currentMedium.attenuation = logf(transmissionColor) / transmissionDepth;
  } else {
    currentMedium = self->outsideMedium;
  }
}

// Lambert BSDF

inline BSDF_EvalRes LambertBSDF_eval(
    const varying OrenNayar *uniform self, const vec3f &wo, const vec3f &wi)
{
  if (self->super.bsdfType == BSDF_TYPE_OREN_NAYAR)
    return OrenNayar_eval(&self->super, wo, wi);
  else
    return Lambert_eval(&self->super, wo, wi);
}

inline BSDF_SampleRes LambertBSDF_sample(const varying OrenNayar *uniform self,
    const vec3f &wo,
    const vec2f &s,
    float ss)
{
  if (self->super.bsdfType == BSDF_TYPE_OREN_NAYAR)
    return OrenNayar_sample(&self->super, wo, s, ss);
  else
    return Lambert_sample(&self->super, wo, s, ss);
}

// Diffuse BSDF

__noinline BSDF_EvalRes DiffuseBSDF_eval(
    const varying Principled_Diffuse_BSDF *uniform self,
    const vec3f &wo,
    const vec3f &wi)
{
  MULTIBSDF_EVAL_BEGIN();
  MULTIBSDF_EVAL_CHILD(0, &self->lambert, LambertBSDF_eval);
  MULTIBSDF_EVAL_CHILD(1, &self->lambertTransmission, LambertTransmission_eval);
  MULTIBSDF_EVAL_END();
  return MULTIBSDF_EVAL_GET();
}

__noinline BSDF_SampleRes DiffuseBSDF_sample(
    const varying Principled_Diffuse_BSDF *uniform self,
    const vec3f &wo,
    const vec2f &s,
    float ss)
{
  MULTIBSDF_SAMPLE_BEGIN();
  MULTIBSDF_SAMPLE_CHILD(0, &self->lambert, LambertBSDF_sample);
  MULTIBSDF_SAMPLE_CHILD(
      1, &self->lambertTransmission, LambertTransmission_sample);
  MULTIBSDF_SAMPLE_EVAL();
  MULTIBSDF_EVAL_CHILD(0, &self->lambert, LambertBSDF_eval);
  MULTIBSDF_EVAL_CHILD(1, &self->lambertTransmission, LambertTransmission_eval);
  MULTIBSDF_SAMPLE_END();
  return MULTIBSDF_SAMPLE_GET();
}

// Plastic DielectricLayer BSDF

inline BSDF_EvalRes Plastic_DielectricLayerBSDF_eval(
    const varying Principled_Plastic_BSDF *uniform self,
    const vec3f &wo,
    const vec3f &wi)
{
  DIELECTRICLAYER_EVAL(self->dielectricLayer,
      self->diffuse.super.scatteringType,
      &self->diffuse,
      DiffuseBSDF_eval);
  return DIELECTRICLAYER_EVAL_GET();
}

inline BSDF_SampleRes Plastic_DielectricLayerBSDF_sample(
    const varying Principled_Plastic_BSDF *uniform self,
    const vec3f &wo,
    const vec2f &s,
    float ss)
{
  DIELECTRICLAYER_SAMPLE(self->dielectricLayer,
      self->diffuse.super.scatteringType,
      &self->diffuse,
      DiffuseBSDF_sample);
  return DIELECTRICLAYER_SAMPLE_GET();
}

// Plastic MicrofacetDielectricLayer BSDF

inline BSDF_EvalRes Plastic_MicrofacetDielectricLayerBSDF_eval(
    const varying Principled_Plastic_BSDF *uniform self,
    const vec3f &wo,
    const vec3f &wi)
{
  MICROFACETDIELECTRICLAYER_EVAL(self->dielectricLayer,
      self->diffuse.super.scatteringType,
      &self->diffuse,
      DiffuseBSDF_eval);
  return MICROFACETDIELECTRICLAYER_EVAL_GET();
}

inline BSDF_SampleRes Plastic_MicrofacetDielectricLayerBSDF_sample(
    const varying Principled_Plastic_BSDF *uniform self,
    const vec3f &wo,
    const vec2f &s,
    float ss)
{
  MICROFACETDIELECTRICLAYER_SAMPLE(self->dielectricLayer,
      self->diffuse.super.scatteringType,
      &self->diffuse,
      DiffuseBSDF_eval,
      DiffuseBSDF_sample);
  return MICROFACETDIELECTRICLAYER_SAMPLE_GET();
}

// Plastic BSDF

__noinline BSDF_EvalRes PlasticBSDF_eval(
    const varying Principled_Plastic_BSDF *uniform self,
    const vec3f &wo,
    const vec3f &wi)
{
  // Skip dielectric layer if no specular
  if (self->dielectricLayer.weight < EPS)
    return DiffuseBSDF_eval(&self->diffuse, wo, wi);

  if (self->dielectricLayer.super.bsdfType
      == BSDF_TYPE_MICROFACET_DIELECTRIC_LAYER)
    return Plastic_MicrofacetDielectricLayerBSDF_eval(self, wo, wi);
  else
    return Plastic_DielectricLayerBSDF_eval(self, wo, wi);
}

inline BSDF_SampleRes PlasticBSDF_sample(
    const varying Principled_Plastic_BSDF *uniform self,
    const vec3f &wo,
    const vec2f &s,
    float ss)
{
  // Skip dielectric layer if no specular
  if (self->dielectricLayer.weight < EPS)
    return DiffuseBSDF_sample(&self->diffuse, wo, s, ss);

  if (self->dielectricLayer.super.bsdfType
      == BSDF_TYPE_MICROFACET_DIELECTRIC_LAYER)
    return Plastic_MicrofacetDielectricLayerBSDF_sample(self, wo, s, ss);
  else
    return Plastic_DielectricLayerBSDF_sample(self, wo, s, ss);
}

// Glass Dielectric BSDF

inline BSDF_EvalRes Glass_DielectricBSDF_eval(
    const varying MicrofacetDielectric *uniform self,
    const vec3f &wo,
    const vec3f &wi)
{
  if (self->super.bsdfType == BSDF_TYPE_MICROFACET_DIELECTRIC)
    return MicrofacetDielectric_eval(&self->super, wo, wi);
  else
    return Dielectric_eval(&self->super, wo, wi);
}

inline BSDF_SampleRes Glass_DielectricBSDF_sample(
    const varying MicrofacetDielectric *uniform self,
    const vec3f &wo,
    const vec2f &s,
    float ss)
{
  if (self->super.bsdfType == BSDF_TYPE_MICROFACET_DIELECTRIC)
    return MicrofacetDielectric_sample(&self->super, wo, s, ss);
  else
    return Dielectric_sample(&self->super, wo, s, ss);
}

// Glass ThinDielectric BSDF

inline BSDF_EvalRes Glass_ThinDielectricBSDF_eval(
    const varying ThinMicrofacetDielectric *uniform self,
    const vec3f &wo,
    const vec3f &wi)
{
  if (self->super.bsdfType == BSDF_TYPE_THIN_MICROFACET_DIELECTRIC)
    return ThinMicrofacetDielectric_eval(&self->super, wo, wi);
  else
    return ThinDielectric_eval(&self->super, wo, wi);
}

inline BSDF_SampleRes Glass_ThinDielectricBSDF_sample(
    const varying ThinMicrofacetDielectric *uniform self,
    const vec3f &wo,
    const vec2f &s,
    float ss)
{
  if (self->super.bsdfType == BSDF_TYPE_THIN_MICROFACET_DIELECTRIC)
    return ThinMicrofacetDielectric_sample(&self->super, wo, s, ss);
  else
    return ThinDielectric_sample(&self->super, wo, s, ss);
}

// Glass BSDF

__noinline BSDF_EvalRes GlassBSDF_eval(
    const varying Principled_Glass_BSDF *uniform self,
    const vec3f &wo,
    const vec3f &wi)
{
  if (self->bsdfId == 1)
    return Glass_DielectricBSDF_eval(&self->dielectric, wo, wi);
  else if (self->bsdfId == 2)
    return Glass_ThinDielectricBSDF_eval(&self->thinDielectric, wo, wi);
  else
    return make_BSDF_EvalRes_zero();
}

inline BSDF_SampleRes GlassBSDF_sample(
    const varying Principled_Glass_BSDF *uniform self,
    const vec3f &wo,
    const vec2f &s,
    float ss)
{
  if (self->bsdfId == 1)
    return Glass_DielectricBSDF_sample(&self->dielectric, wo, s, ss);
  else if (self->bsdfId == 2)
    return Glass_ThinDielectricBSDF_sample(&self->thinDielectric, wo, s, ss);
  else
    return Transmission_sample(&self->transmission, wo, s, ss);
}

// Metal BSDF

__noinline BSDF_EvalRes MetalBSDF_eval(
    const varying MicrofacetConductor *uniform self,
    const vec3f &wo,
    const vec3f &wi)
{
  if (self->super.bsdfType == BSDF_TYPE_MICROFACET_CONDUCTOR)
    return MicrofacetConductor_eval(self, wo, wi);
  else
    return make_BSDF_EvalRes_zero();
}

inline BSDF_SampleRes MetalBSDF_sample(
    const varying MicrofacetConductor *uniform self,
    const vec3f &wo,
    const vec2f &s,
    float ss)
{
  if (self->super.bsdfType == BSDF_TYPE_MICROFACET_CONDUCTOR)
    return MicrofacetConductor_sample(self, wo, s, ss);
  else
    return Conductor_sample(&self->super, wo, s, ss);
}

// Base BSDF

__noinline BSDF_EvalRes BaseBSDF_eval(
    const varying Principled_Base_MultiBSDF *uniform self,
    const vec3f &wo,
    const vec3f &wi)
{
  MULTIBSDF_EVAL_BEGIN();
  MULTIBSDF_EVAL_CHILD(0, &self->metal, MetalBSDF_eval);
  MULTIBSDF_EVAL_CHILD(1, &self->glass, GlassBSDF_eval);
  MULTIBSDF_EVAL_CHILD(2, &self->plastic, PlasticBSDF_eval);
  MULTIBSDF_EVAL_END();
  return MULTIBSDF_EVAL_GET();
}

__noinline BSDF_SampleRes BaseBSDF_sample(
    const varying Principled_Base_MultiBSDF *uniform self,
    const vec3f &wo,
    const vec2f &s,
    float ss)
{
  MULTIBSDF_SAMPLE_BEGIN();
  MULTIBSDF_SAMPLE_CHILD(0, &self->metal, MetalBSDF_sample);
  MULTIBSDF_SAMPLE_CHILD(1, &self->glass, GlassBSDF_sample);
  MULTIBSDF_SAMPLE_CHILD(2, &self->plastic, PlasticBSDF_sample);
  MULTIBSDF_SAMPLE_EVAL();
  MULTIBSDF_EVAL_CHILD(0, &self->metal, MetalBSDF_eval);
  MULTIBSDF_EVAL_CHILD(1, &self->glass, GlassBSDF_eval);
  MULTIBSDF_EVAL_CHILD(2, &self->plastic, PlasticBSDF_eval);
  MULTIBSDF_SAMPLE_END();
  return MULTIBSDF_SAMPLE_GET();
}

// ClearCoat DielectricLayer BSDF

inline BSDF_EvalRes ClearCoat_DielectricLayerBSDF_eval(
    const varying Principled_ClearCoat_BSDF *uniform self,
    const vec3f &wo,
    const vec3f &wi)
{
  DIELECTRICLAYER_EVAL(self->dielectricLayer,
      self->base.super.scatteringType,
      &self->base,
      BaseBSDF_eval);
  return DIELECTRICLAYER_EVAL_GET();
}

inline BSDF_SampleRes ClearCoat_DielectricLayerBSDF_sample(
    const varying Principled_ClearCoat_BSDF *uniform self,
    const vec3f &wo,
    const vec2f &s,
    float ss)
{
  DIELECTRICLAYER_SAMPLE(self->dielectricLayer,
      self->base.super.scatteringType,
      &self->base,
      BaseBSDF_sample);
  return DIELECTRICLAYER_SAMPLE_GET();
}

// ClearCoat MicrofacetDielectricLayer BSDF

inline BSDF_EvalRes ClearCoat_MicrofacetDielectricLayerBSDF_eval(
    const varying Principled_ClearCoat_BSDF *uniform self,
    const vec3f &wo,
    const vec3f &wi)
{
  MICROFACETDIELECTRICLAYER_EVAL(self->dielectricLayer,
      self->base.super.scatteringType,
      &self->base,
      BaseBSDF_eval);
  return MICROFACETDIELECTRICLAYER_EVAL_GET();
}

inline BSDF_SampleRes ClearCoat_MicrofacetDielectricLayerBSDF_sample(
    const varying Principled_ClearCoat_BSDF *uniform self,
    const vec3f &wo,
    const vec2f &s,
    float ss)
{
  MICROFACETDIELECTRICLAYER_SAMPLE(self->dielectricLayer,
      self->base.super.scatteringType,
      &self->base,
      BaseBSDF_eval,
      BaseBSDF_sample);
  return MICROFACETDIELECTRICLAYER_SAMPLE_GET();
}

// ClearCoat BSDF

__noinline BSDF_EvalRes ClearCoatBSDF_eval(
    const varying Principled_ClearCoat_BSDF *uniform self,
    const vec3f &wo,
    const vec3f &wi)
{
  // Skip dielectric layer if no clear coat
  if (self->dielectricLayer.weight < EPS)
    return BaseBSDF_eval(&self->base, wo, wi);

  if (self->dielectricLayer.super.bsdfType
      == BSDF_TYPE_MICROFACET_DIELECTRIC_LAYER)
    return ClearCoat_MicrofacetDielectricLayerBSDF_eval(self, wo, wi);
  else
    return ClearCoat_DielectricLayerBSDF_eval(self, wo, wi);
}

__noinline BSDF_SampleRes ClearCoatBSDF_sample(
    const varying Principled_ClearCoat_BSDF *uniform self,
    const vec3f &wo,
    const vec2f &s,
    float ss)
{
  // Skip dielectric layer if no clear coat
  if (self->dielectricLayer.weight < EPS)
    return BaseBSDF_sample(&self->base, wo, s, ss);

  if (self->dielectricLayer.super.bsdfType
      == BSDF_TYPE_MICROFACET_DIELECTRIC_LAYER)
    return ClearCoat_MicrofacetDielectricLayerBSDF_sample(self, wo, s, ss);
  else
    return ClearCoat_DielectricLayerBSDF_sample(self, wo, s, ss);
}

// SheenLayer BSDF

__noinline BSDF_EvalRes SheenLayerBSDF_eval(
    const varying Principled_SheenLayer_BSDF *uniform self,
    const vec3f &wo,
    const vec3f &wi)
{
  // Skip sheen layer if does not exists
  if (self->sheenLayer.weight < EPS)
    return ClearCoatBSDF_eval(&self->clearCoat, wo, wi);

  MICROFACETSHEENLAYER_EVAL(
      self->sheenLayer, &self->clearCoat, ClearCoatBSDF_eval);
  return MICROFACETSHEENLAYER_EVAL_GET();
}

inline BSDF_SampleRes SheenLayerBSDF_sample(
    const varying Principled_SheenLayer_BSDF *uniform self,
    const vec3f &wo,
    const vec2f &s,
    float ss)
{
  // Skip sheen layer if does not exists
  if (self->sheenLayer.weight < EPS)
    return ClearCoatBSDF_sample(&self->clearCoat, wo, s, ss);

  MICROFACETSHEENLAYER_SAMPLE(self->sheenLayer,
      &self->clearCoat,
      ClearCoatBSDF_eval,
      ClearCoatBSDF_sample);
  return MICROFACETSHEENLAYER_SAMPLE_GET();
}

// Principled BSDF

SYCL_EXTERNAL BSDF_EvalRes Principled_BSDF_eval(
    const varying BSDF *uniform super, const vec3f &wo, const vec3f &wi)
{
  const varying Principled_BSDF *uniform self =
      (const varying Principled_BSDF *uniform)super;

  MULTIBSDF_EVAL_BEGIN();
  // 0 - omitted, no evaluation needed for transmission BSDF
  MULTIBSDF_EVAL_CHILD(1, &self->sheen, SheenLayerBSDF_eval);
  MULTIBSDF_EVAL_END();
  return MULTIBSDF_EVAL_GET();
}

SYCL_EXTERNAL BSDF_SampleRes Principled_BSDF_sample(
    const varying BSDF *uniform super,
    const vec3f &wo,
    const vec2f &s,
    float ss)
{
  const varying Principled_BSDF *uniform self =
      (const varying Principled_BSDF *uniform)super;

  MULTIBSDF_SAMPLE_BEGIN();
  MULTIBSDF_SAMPLE_CHILD(0, &self->transmission, Transmission_sample);
  MULTIBSDF_SAMPLE_CHILD(1, &self->sheen, SheenLayerBSDF_sample);
  MULTIBSDF_SAMPLE_EVAL();
  // 0 - omitted, no evaluation needed for transmission BSDF
  MULTIBSDF_EVAL_CHILD(1, &self->sheen, SheenLayerBSDF_eval);
  MULTIBSDF_SAMPLE_END();
  return MULTIBSDF_SAMPLE_GET();
}

SYCL_EXTERNAL vec3f Principled_getEmission(const Material *uniform _self,
    const varying DifferentialGeometry &dg,
    const uniform FeatureFlagsHandler &)
{
  const Principled *uniform self = (Principled * uniform) _self;

  return self->emission * get3f(self->emissionMap, dg, make_vec3f(1.f));
}

///////////////////////////////////////////////////////////////////////////////
// External API

export void *uniform Principled_getBSDF_addr()
{
  return (void *uniform)Principled_getBSDF;
}

export void *uniform Principled_getTransparency_addr()
{
  return (void *uniform)Principled_getTransparency;
}

export void *uniform Principled_selectNextMedium_addr()
{
  return (void *uniform)Principled_selectNextMedium;
}

export void *uniform Principled_getEmission_addr()
{
  return (void *uniform)Principled_getEmission;
}

OSPRAY_END_ISPC_NAMESPACE