File size: 38,183 Bytes
ddbb236
 
de4d6ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94f765e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39a1f38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ddbb236
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f9a369
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de4d6ae
 
 
 
 
 
 
 
 
 
 
 
 
39a1f38
 
 
 
 
 
 
 
 
 
de4d6ae
 
 
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
import math

import pytest
import torch

import kernels

ptd = kernels.get_kernel("phanerozoic/pathtracer-diff", version=1,
                         trust_remote_code=True)

requires_cuda = pytest.mark.skipif(not torch.cuda.is_available(),
                                   reason="CUDA required")


def quad(a, b, c, d):
    return [[a, b, c], [a, c, d]]


def add_box(verts, faces, mat, lo, hi, mat_id):
    x0, y0, z0 = lo
    x1, y1, z1 = hi
    base = len(verts)
    verts.extend([(x0, y0, z0), (x1, y0, z0), (x1, y0, z1), (x0, y0, z1),
                  (x0, y1, z0), (x1, y1, z0), (x1, y1, z1), (x0, y1, z1)])
    i = lambda k: base + k
    for q in [quad(i(0), i(1), i(2), i(3)),   # bottom
              quad(i(4), i(7), i(6), i(5)),   # top
              quad(i(0), i(4), i(5), i(1)),   # z0
              quad(i(3), i(2), i(6), i(7)),   # z1
              quad(i(0), i(3), i(7), i(4)),   # x0
              quad(i(1), i(5), i(6), i(2))]:  # x1
        faces.extend(q)
        mat.extend([mat_id, mat_id])


def furnace_scene(albedo=0.5, emission=0.25):
    verts, faces, mat = [], [], []
    add_box(verts, faces, mat, (-2, -2, -2), (2, 2, 2), 0)
    return ptd.Scene(verts, faces, mat,
                     albedo=[[albedo] * 3], emission=[[emission] * 3])


def cornell(with_box=True, albedo=None, emission=None):
    verts, faces, mat = [], [], []

    def wall(a, b, c, d, m):
        base = len(verts)
        verts.extend([a, b, c, d])
        faces.extend(quad(base, base + 1, base + 2, base + 3))
        mat.extend([m, m])

    X, Y, Z = 5.56, 5.488, 5.592
    wall((0, 0, 0), (X, 0, 0), (X, 0, Z), (0, 0, Z), 0)          # floor
    wall((0, Y, 0), (0, Y, Z), (X, Y, Z), (X, Y, 0), 0)          # ceiling
    wall((0, 0, Z), (X, 0, Z), (X, Y, Z), (0, Y, Z), 0)          # back
    wall((0, 0, 0), (0, 0, Z), (0, Y, Z), (0, Y, 0), 1)          # left (red)
    wall((X, 0, 0), (X, Y, 0), (X, Y, Z), (X, 0, Z), 2)          # right (green)
    wall((2.13, Y - 0.001, 2.27), (3.43, Y - 0.001, 2.27),
         (3.43, Y - 0.001, 3.32), (2.13, Y - 0.001, 3.32), 3)    # light
    if with_box:
        add_box(verts, faces, mat, (1.3, 0.0, 0.65), (2.4, 1.65, 1.7), 0)
    if albedo is None:
        albedo = [[0.73, 0.73, 0.73], [0.65, 0.05, 0.05],
                  [0.12, 0.45, 0.15], [0.78, 0.78, 0.78]]
    if emission is None:
        emission = [[0, 0, 0], [0, 0, 0], [0, 0, 0], [18.4, 15.6, 8.0]]
    return ptd.Scene(verts, faces, mat, albedo=albedo, emission=emission)


def cornell_camera():
    return ptd.Camera(position=(2.78, 2.73, -8.0), look_at=(2.78, 2.73, 2.8),
                      vfov_deg=39.0)


@requires_cuda
@pytest.mark.kernels_ci
def test_furnace_analytic_zero_variance():
    """Closed uniform box, BRDF sampling only: with cosine sampling the
    diffuse throughput multiplier is exactly the albedo, so every path
    returns exactly E * sum_{k<B} a^k and every pixel equals the analytic
    series regardless of the random walk."""
    a, E, B = 0.5, 0.25, 8
    scene = furnace_scene(a, E)
    cam = ptd.Camera(position=(0, 0, 0), look_at=(0, 0, 1), vfov_deg=60.0)
    img = ptd.render(scene, cam, 32, 32, spp=2, max_bounces=B, nee=False,
                     seed=0)
    expected = E * (1 - a ** B) / (1 - a)
    assert torch.isfinite(img).all()
    assert (img - expected).abs().max().item() < 1e-3, \
        (img.min().item(), img.max().item(), expected)


@requires_cuda
@pytest.mark.kernels_ci
def test_forward_bitwise_deterministic():
    scene = cornell()
    cam = cornell_camera()
    i1 = ptd.render(scene, cam, 96, 96, spp=8, max_bounces=4, seed=11)
    i2 = ptd.render(scene, cam, 96, 96, spp=8, max_bounces=4, seed=11)
    assert torch.equal(i1, i2)
    i3 = ptd.render(scene, cam, 96, 96, spp=8, max_bounces=4, seed=12)
    assert not torch.equal(i1, i3)


@requires_cuda
@pytest.mark.kernels_ci
def test_nee_matches_brdf_estimator():
    """The NEE-only and BRDF-only estimators target the same integral; their
    image means must agree within Monte Carlo tolerance."""
    verts, faces, mat = [], [], []
    add_box(verts, faces, mat, (0, 0, 0), (5, 5, 5), 0)
    base = len(verts)
    verts.extend([(1.0, 4.999, 1.0), (4.5, 4.999, 1.0), (4.5, 4.999, 4.5),
                  (1.0, 4.999, 4.5)])
    faces.extend(quad(base, base + 1, base + 2, base + 3))
    mat.extend([1, 1])
    scene = ptd.Scene(verts, faces, mat,
                      albedo=[[0.6, 0.6, 0.6], [0.0, 0.0, 0.0]],
                      emission=[[0, 0, 0], [3.0, 3.0, 3.0]])
    cam = ptd.Camera(position=(2.5, 2.5, 0.2), look_at=(2.5, 2.5, 5.0),
                     vfov_deg=70.0)
    m_nee = ptd.render(scene, cam, 64, 64, spp=64, max_bounces=4, nee=True,
                       seed=1).mean().item()
    m_brdf = ptd.render(scene, cam, 64, 64, spp=256, max_bounces=4, nee=False,
                        seed=2).mean().item()
    assert abs(m_nee - m_brdf) / m_brdf < 0.03, (m_nee, m_brdf)


@requires_cuda
@pytest.mark.kernels_ci
def test_gradients_match_finite_differences():
    """Sampling directions never depend on albedo/emission, so with a fixed
    seed the loss is smooth in the parameters along the same paths and
    central differences must match the analytic replay gradients."""
    scene = cornell(with_box=False)
    cam = cornell_camera()
    H = W = 48
    torch.manual_seed(0)
    Wr = torch.rand(H, W, 3, device="cuda")

    def loss_of(albedo, emission):
        scene.albedo = albedo
        scene.emission = emission
        img = ptd.render(scene, cam, H, W, spp=8, max_bounces=3, nee=True,
                         seed=7)
        return (img * Wr).sum()

    alb0 = scene.albedo.clone()
    emi0 = scene.emission.clone()

    alb = alb0.clone().requires_grad_(True)
    emi = emi0.clone().requires_grad_(True)
    loss_of(alb, emi).backward()

    checks = []
    for (i, c) in [(0, 1), (1, 0), (2, 2)]:  # white g, red r, green b
        h = 2e-3
        ap = alb0.clone(); ap[i, c] += h
        am = alb0.clone(); am[i, c] -= h
        fd = (loss_of(ap, emi0) - loss_of(am, emi0)).item() / (2 * h)
        an = alb.grad[i, c].item()
        checks.append(("albedo", i, c, an, fd))
    h = 0.05
    ep = emi0.clone(); ep[3, 1] += h
    em = emi0.clone(); em[3, 1] -= h
    fd = (loss_of(alb0, ep) - loss_of(alb0, em)).item() / (2 * h)
    an = emi.grad[3, 1].item()
    checks.append(("emission", 3, 1, an, fd))

    for kind, i, c, an, fd in checks:
        assert fd != 0.0, (kind, i, c)
        assert abs(an - fd) / max(abs(fd), 1e-6) < 2e-2, \
            (kind, i, c, an, fd)


@requires_cuda
@pytest.mark.kernels_ci
def test_inverse_rendering_recovers_wall_albedo():
    """Recover the left-wall albedo from a rendered target by gradient
    descent through the kernel: the flagship no-framework-in-the-loop use."""
    target_row = torch.tensor([0.2, 0.5, 0.7], device="cuda")
    scene = cornell(with_box=False)
    cam = cornell_camera()
    H = W = 48

    tgt_alb = scene.albedo.clone()
    tgt_alb[1] = target_row
    scene.albedo = tgt_alb
    target = ptd.render(scene, cam, H, W, spp=8, max_bounces=3, seed=3).detach()

    alb = scene.albedo.clone()
    alb[1] = torch.tensor([0.5, 0.5, 0.5], device="cuda")
    alb = alb.requires_grad_(True)
    opt = torch.optim.Adam([alb], lr=0.05)
    losses = []
    for _ in range(80):
        opt.zero_grad()
        scene.albedo = alb
        img = ptd.render(scene, cam, H, W, spp=8, max_bounces=3, seed=3)
        loss = (img - target).square().mean()
        loss.backward()
        alb.grad[0] = 0
        alb.grad[2] = 0
        alb.grad[3] = 0
        opt.step()
        with torch.no_grad():
            alb.clamp_(0.02, 0.98)
        losses.append(loss.item())
    err = (alb[1].detach() - target_row).abs().max().item()
    assert err < 0.05, (alb[1].detach().tolist(), losses[0], losses[-1])
    assert losses[-1] < losses[0] * 0.1, (losses[0], losses[-1])


def cornell_tex(back_albedo):
    """Cornell walls with the back wall on its own material carrying a
    texture; per-corner UVs map the back quad to [0,1]^2."""
    verts, faces, mat, uvs = [], [], [], []

    def wall(a, b, c, d, m, uv=False):
        base = len(verts)
        verts.extend([a, b, c, d])
        faces.extend(quad(base, base + 1, base + 2, base + 3))
        mat.extend([m, m])
        if uv:
            uvs.extend([[(0, 0), (1, 0), (1, 1)], [(0, 0), (1, 1), (0, 1)]])
        else:
            uvs.extend([[(0, 0)] * 3, [(0, 0)] * 3])

    X, Y, Z = 5.56, 5.488, 5.592
    wall((0, 0, 0), (X, 0, 0), (X, 0, Z), (0, 0, Z), 0)
    wall((0, Y, 0), (0, Y, Z), (X, Y, Z), (X, Y, 0), 0)
    wall((0, 0, Z), (X, 0, Z), (X, Y, Z), (0, Y, Z), 4, uv=True)   # back
    wall((0, 0, 0), (0, 0, Z), (0, Y, Z), (0, Y, 0), 1)
    wall((X, 0, 0), (X, Y, 0), (X, Y, Z), (X, 0, Z), 2)
    wall((2.13, Y - 0.001, 2.27), (3.43, Y - 0.001, 2.27),
         (3.43, Y - 0.001, 3.32), (2.13, Y - 0.001, 3.32), 3)
    albedo = [torch.tensor([0.73, 0.73, 0.73]),
              torch.tensor([0.65, 0.05, 0.05]),
              torch.tensor([0.12, 0.45, 0.15]),
              torch.tensor([0.78, 0.78, 0.78]),
              back_albedo]
    emission = [[0, 0, 0], [0, 0, 0], [0, 0, 0], [18.4, 15.6, 8.0], [0, 0, 0]]
    return ptd.Scene(verts, faces, mat, albedo=albedo, emission=emission,
                     uvs=uvs)


@requires_cuda
@pytest.mark.kernels_ci
def test_texture_constants_equivalence():
    """Per-material constants and 1x1 textures share one code path: images
    agree to float rounding and the 1x1 texel gradient equals the constant
    gradient."""
    cam = cornell_camera()
    s1 = cornell(with_box=False)
    i1 = ptd.render(s1, cam, 64, 64, spp=8, max_bounces=3, seed=9)

    consts = [[0.73, 0.73, 0.73], [0.65, 0.05, 0.05], [0.12, 0.45, 0.15],
              [0.78, 0.78, 0.78]]
    texs = [torch.tensor(c, device="cuda").reshape(1, 1, 3) for c in consts]
    verts, faces, mat = [], [], []

    def wall(a, b, c, d, m):
        base = len(verts)
        verts.extend([a, b, c, d])
        faces.extend(quad(base, base + 1, base + 2, base + 3))
        mat.extend([m, m])

    X, Y, Z = 5.56, 5.488, 5.592
    wall((0, 0, 0), (X, 0, 0), (X, 0, Z), (0, 0, Z), 0)
    wall((0, Y, 0), (0, Y, Z), (X, Y, Z), (X, Y, 0), 0)
    wall((0, 0, Z), (X, 0, Z), (X, Y, Z), (0, Y, Z), 0)
    wall((0, 0, 0), (0, 0, Z), (0, Y, Z), (0, Y, 0), 1)
    wall((X, 0, 0), (X, Y, 0), (X, Y, Z), (X, 0, Z), 2)
    wall((2.13, Y - 0.001, 2.27), (3.43, Y - 0.001, 2.27),
         (3.43, Y - 0.001, 3.32), (2.13, Y - 0.001, 3.32), 3)
    emission = [[0, 0, 0], [0, 0, 0], [0, 0, 0], [18.4, 15.6, 8.0]]
    s2 = ptd.Scene(verts, faces, mat, albedo=texs, emission=emission)
    i2 = ptd.render(s2, cam, 64, 64, spp=8, max_bounces=3, seed=9)
    assert (i1 - i2).abs().max().item() < 5e-6

    torch.manual_seed(1)
    Wr = torch.rand(64, 64, 3, device="cuda")
    alb = s1.albedo.clone().requires_grad_(True)
    s1.albedo = alb
    (ptd.render(s1, cam, 64, 64, spp=8, max_bounces=3, seed=9) * Wr).sum().backward()
    texs_g = [t.clone().requires_grad_(True) for t in texs]
    s2.albedo_textures = texs_g
    (ptd.render(s2, cam, 64, 64, spp=8, max_bounces=3, seed=9) * Wr).sum().backward()
    for m in range(4):
        a = alb.grad[m]
        b = texs_g[m].grad.reshape(3)
        assert (a - b).abs().max().item() < 1e-3 + 1e-3 * a.abs().max().item()


@requires_cuda
@pytest.mark.kernels_ci
def test_texture_gradients_match_finite_differences():
    """Texel gradients through the bilinear-footprint adjoint match central
    differences along the same paths."""
    torch.manual_seed(2)
    base_tex = (torch.rand(4, 4, 3, device="cuda") * 0.6 + 0.2)
    H = W = 48
    cam = cornell_camera()
    torch.manual_seed(0)
    Wr = torch.rand(H, W, 3, device="cuda")

    def loss_of(t):
        scene = cornell_tex(t)
        img = ptd.render(scene, cam, H, W, spp=8, max_bounces=3, seed=7)
        return (img * Wr).sum()

    t = base_tex.clone().requires_grad_(True)
    loss_of(t).backward()

    worst = 0.0
    for (y, x, c) in [(0, 0, 0), (1, 2, 1), (3, 3, 2), (2, 1, 0)]:
        h = 2e-3
        tp = base_tex.clone(); tp[y, x, c] += h
        tm = base_tex.clone(); tm[y, x, c] -= h
        fd = (loss_of(tp) - loss_of(tm)).item() / (2 * h)
        an = t.grad[y, x, c].item()
        assert fd != 0.0, (y, x, c)
        worst = max(worst, abs(an - fd) / abs(fd))
    assert worst < 2e-2, worst


@requires_cuda
@pytest.mark.kernels_ci
def test_texture_inverse_recovery():
    """Recover an 8x8 back-wall texture from a rendered target: 192 unknowns,
    the regime per-material constants cannot express."""
    torch.manual_seed(4)
    target_tex = (torch.rand(8, 8, 3, device="cuda") * 0.7 + 0.15)
    cam = cornell_camera()
    H = W = 64
    scene = cornell_tex(target_tex)
    target = ptd.render(scene, cam, H, W, spp=8, max_bounces=3, seed=3).detach()

    t = torch.full((8, 8, 3), 0.5, device="cuda", requires_grad=True)
    scene_opt = cornell_tex(t)
    opt = torch.optim.Adam([t], lr=0.1)
    first = None
    for _ in range(200):
        opt.zero_grad()
        img = ptd.render(scene_opt, cam, H, W, spp=8, max_bounces=3, seed=3)
        loss = (img - target).square().mean()
        loss.backward()
        opt.step()
        with torch.no_grad():
            t.clamp_(0.02, 0.98)
        if first is None:
            first = loss.item()
    err = (t.detach() - target_tex).abs()
    assert loss.item() < first * 0.05, (first, loss.item())
    assert err.mean().item() < 0.08, err.mean().item()


@requires_cuda
@pytest.mark.kernels_ci
def test_estimators_agree_three_ways():
    """BRDF-only, NEE-only, and MIS target the same integral at the same
    bounce budget; their image means must agree within Monte Carlo
    tolerance."""
    scene = cornell(with_box=True)
    cam = cornell_camera()
    m_mis = ptd.render(scene, cam, 64, 64, spp=64, max_bounces=4,
                       estimator="mis", seed=1).mean().item()
    m_nee = ptd.render(scene, cam, 64, 64, spp=64, max_bounces=4,
                       estimator="nee", seed=2).mean().item()
    m_brdf = ptd.render(scene, cam, 64, 64, spp=1024, max_bounces=4,
                        estimator="brdf", seed=3).mean().item()
    assert abs(m_mis - m_nee) / m_nee < 0.03, (m_mis, m_nee)
    assert abs(m_mis - m_brdf) / m_brdf < 0.05, (m_mis, m_brdf)


@requires_cuda
@pytest.mark.kernels_ci
def test_conductor_under_env_estimators_agree():
    """A GGX conductor under a constant environment: MIS and BRDF-only
    means must agree, which jointly validates the VNDF weight, the GGX pdf,
    and the environment pdf."""
    verts, faces, mat = [], [], []

    def quad_w(a, b, c, d, m):
        base = len(verts)
        verts.extend([a, b, c, d])
        faces.extend(quad(base, base + 1, base + 2, base + 3))
        mat.extend([m, m])

    quad_w((-4, 0, -4), (4, 0, -4), (4, 0, 4), (-4, 0, 4), 0)   # floor
    add_box(verts, faces, mat, (-1, 0, -1), (1, 2, 1), 1)       # metal box
    env = torch.full((8, 16, 3), 1.0)
    scene = ptd.Scene(verts, faces, mat,
                      albedo=[[0.5, 0.5, 0.5], [0.9, 0.6, 0.3]],
                      emission=[[0, 0, 0], [0, 0, 0]],
                      material_types=[ptd.DIFFUSE, ptd.CONDUCTOR],
                      roughness=[0.3, 0.25], env=env)
    cam = ptd.Camera(position=(5, 3, 5), look_at=(0, 1, 0), vfov_deg=45.0)
    m_mis = ptd.render(scene, cam, 64, 64, spp=64, max_bounces=4,
                       estimator="mis", seed=1).mean().item()
    m_brdf = ptd.render(scene, cam, 64, 64, spp=512, max_bounces=4,
                        estimator="brdf", seed=2).mean().item()
    assert abs(m_mis - m_brdf) / m_brdf < 0.04, (m_mis, m_brdf)


@requires_cuda
@pytest.mark.kernels_ci
def test_fresnel_slab_analytic():
    """A smooth glass slab in front of a uniform emitter, viewed at normal
    incidence: the transmitted fraction sums the internal-bounce series to
    (1-R)/(1+R) with R = ((n-1)/(n+1))^2."""
    verts, faces, mat = [], [], []

    def quad_w(a, b, c, d, m):
        base = len(verts)
        verts.extend([a, b, c, d])
        faces.extend(quad(base, base + 1, base + 2, base + 3))
        mat.extend([m, m])

    quad_w((-10, -10, 5), (10, -10, 5), (10, 10, 5), (-10, 10, 5), 0)  # emitter
    add_box(verts, faces, mat, (-10, -10, 2.0), (10, 10, 2.2), 1)      # slab
    n_ior = 1.5
    scene = ptd.Scene(verts, faces, mat,
                      albedo=[[0, 0, 0], [0, 0, 0]],
                      emission=[[1.0, 1.0, 1.0], [0, 0, 0]],
                      material_types=[ptd.DIFFUSE, ptd.DIELECTRIC],
                      ior=[1.5, n_ior])
    cam = ptd.Camera(position=(0, 0, 0), look_at=(0, 0, 1), vfov_deg=10.0)
    img = ptd.render(scene, cam, 32, 32, spp=256, max_bounces=10,
                     estimator="brdf", seed=5)
    R = ((n_ior - 1) / (n_ior + 1)) ** 2
    expected = (1 - R) / (1 + R)
    got = img[12:20, 12:20].mean().item()
    assert abs(got - expected) / expected < 0.01, (got, expected)


@requires_cuda
@pytest.mark.kernels_ci
def test_env_furnace_diffuse():
    """A diffuse plane under a constant environment c: with BRDF sampling
    every sample returns exactly albedo * c (cosine pdf cancellation plus a
    guaranteed env hit), and MIS agrees in expectation."""
    verts, faces, mat = [], [], []
    base = len(verts)
    verts.extend([(-5, 0, -5), (5, 0, -5), (5, 0, 5), (-5, 0, 5)])
    faces.extend(quad(base, base + 1, base + 2, base + 3))
    mat.extend([0, 0])
    c = 0.8
    a = 0.6
    env = torch.full((8, 16, 3), c)
    scene = ptd.Scene(verts, faces, mat, albedo=[[a] * 3],
                      emission=[[0, 0, 0]], env=env)
    cam = ptd.Camera(position=(0, 2.0, 0.01), look_at=(0, 0, 0.2),
                     vfov_deg=30.0)
    img = ptd.render(scene, cam, 32, 32, spp=4, max_bounces=3,
                     estimator="brdf", seed=0)
    assert (img - a * c).abs().max().item() < 2e-3, \
        (img.min().item(), img.max().item(), a * c)
    m_mis = ptd.render(scene, cam, 32, 32, spp=256, max_bounces=3,
                       estimator="mis", seed=1).mean().item()
    assert abs(m_mis - a * c) / (a * c) < 0.02, (m_mis, a * c)


@requires_cuda
@pytest.mark.kernels_ci
def test_env_texel_gradients_match_fd():
    """Environment texels are linear in the estimate and the sampling CDF is
    detached, so same-seed central differences are exact."""
    torch.manual_seed(3)
    env0 = torch.rand(4, 8, 3, device="cuda") * 1.5 + 0.2
    verts, faces, mat = [], [], []
    base = len(verts)
    verts.extend([(-5, 0, -5), (5, 0, -5), (5, 0, 5), (-5, 0, 5)])
    faces.extend(quad(base, base + 1, base + 2, base + 3))
    mat.extend([0, 0])
    cam = ptd.Camera(position=(0, 2.0, 0.01), look_at=(0, 0, 0.5),
                     vfov_deg=40.0)
    torch.manual_seed(0)
    Wr = torch.rand(32, 32, 3, device="cuda")
    scene = ptd.Scene(verts, faces, mat, albedo=[[0.6] * 3],
                      emission=[[0, 0, 0]], env=env0)

    def loss_of(e):
        # swap texel values under the construction-time (detached) CDF, the
        # frozen-sampling semantics the kernel defines
        scene.env = e.to("cuda")
        img = ptd.render(scene, cam, 32, 32, spp=8, max_bounces=3, seed=11)
        return (img * Wr).sum()

    e = env0.clone().requires_grad_(True)
    loss_of(e).backward()
    worst = 0.0
    for (y, x, c) in [(0, 0, 0), (1, 4, 1), (1, 7, 2)]:  # upper hemisphere
        h = 5e-3
        ep = env0.clone(); ep[y, x, c] += h
        em = env0.clone(); em[y, x, c] -= h
        fd = (loss_of(ep) - loss_of(em)).item() / (2 * h)
        an = e.grad[y, x, c].item()
        assert fd != 0.0, (y, x, c)
        worst = max(worst, abs(an - fd) / abs(fd))
    assert worst < 2e-2, worst
    # a texel no path can see gets exactly zero from both sides
    assert e.grad[3, 0, 0].item() == 0.0


@requires_cuda
@pytest.mark.kernels_ci
def test_conductor_albedo_gradients_match_fd():
    """Conductor F0 enters every term through Schlick Fresnel, which is
    affine in F0, so replay gradients must match same-seed central
    differences through GGX sampling and MIS."""
    cam = cornell_camera()
    H = W = 48
    torch.manual_seed(0)
    Wr = torch.rand(H, W, 3, device="cuda")
    base_alb = torch.tensor([[0.73, 0.73, 0.73], [0.9, 0.5, 0.2],
                             [0.12, 0.45, 0.15], [0.78, 0.78, 0.78]],
                            device="cuda")

    def loss_of(alb):
        verts, faces, mat = [], [], []

        def wall(a, b, c, d, m):
            b0 = len(verts)
            verts.extend([a, b, c, d])
            faces.extend(quad(b0, b0 + 1, b0 + 2, b0 + 3))
            mat.extend([m, m])

        X, Y, Z = 5.56, 5.488, 5.592
        wall((0, 0, 0), (X, 0, 0), (X, 0, Z), (0, 0, Z), 0)
        wall((0, Y, 0), (0, Y, Z), (X, Y, Z), (X, Y, 0), 0)
        wall((0, 0, Z), (X, 0, Z), (X, Y, Z), (0, Y, Z), 0)
        wall((0, 0, 0), (0, 0, Z), (0, Y, Z), (0, Y, 0), 1)   # conductor
        wall((X, 0, 0), (X, Y, 0), (X, Y, Z), (X, 0, Z), 2)
        wall((2.13, Y - 0.001, 2.27), (3.43, Y - 0.001, 2.27),
             (3.43, Y - 0.001, 3.32), (2.13, Y - 0.001, 3.32), 3)
        scene = ptd.Scene(verts, faces, mat, albedo=alb,
                          emission=[[0, 0, 0], [0, 0, 0], [0, 0, 0],
                                    [18.4, 15.6, 8.0]],
                          material_types=[ptd.DIFFUSE, ptd.CONDUCTOR,
                                          ptd.DIFFUSE, ptd.DIFFUSE],
                          roughness=[0.3, 0.2, 0.3, 0.3])
        img = ptd.render(scene, cam, H, W, spp=8, max_bounces=3, seed=13)
        return (img * Wr).sum()

    alb = base_alb.clone().requires_grad_(True)
    loss_of(alb).backward()
    worst = 0.0
    for (i, c) in [(1, 0), (1, 2), (0, 1)]:
        h = 2e-3
        ap = base_alb.clone(); ap[i, c] += h
        am = base_alb.clone(); am[i, c] -= h
        fd = (loss_of(ap) - loss_of(am)).item() / (2 * h)
        an = alb.grad[i, c].item()
        assert fd != 0.0, (i, c)
        worst = max(worst, abs(an - fd) / abs(fd))
    assert worst < 2e-2, worst


def cornell_mats(mat1_type, rough1=0.2, albedo1=(0.9, 0.5, 0.2)):
    verts, faces, mat = [], [], []

    def wall(a, b, c, d, m):
        b0 = len(verts)
        verts.extend([a, b, c, d])
        faces.extend(quad(b0, b0 + 1, b0 + 2, b0 + 3))
        mat.extend([m, m])

    X, Y, Z = 5.56, 5.488, 5.592
    wall((0, 0, 0), (X, 0, 0), (X, 0, Z), (0, 0, Z), 0)
    wall((0, Y, 0), (0, Y, Z), (X, Y, Z), (X, Y, 0), 0)
    wall((0, 0, Z), (X, 0, Z), (X, Y, Z), (0, Y, Z), 0)
    wall((0, 0, 0), (0, 0, Z), (0, Y, Z), (0, Y, 0), 1)
    wall((X, 0, 0), (X, Y, 0), (X, Y, Z), (X, 0, Z), 2)
    wall((2.13, Y - 0.001, 2.27), (3.43, Y - 0.001, 2.27),
         (3.43, Y - 0.001, 3.32), (2.13, Y - 0.001, 3.32), 3)
    albedo = [[0.73] * 3, list(albedo1), [0.12, 0.45, 0.15], [0.78] * 3]
    emission = [[0, 0, 0], [0, 0, 0], [0, 0, 0], [18.4, 15.6, 8.0]]
    return ptd.Scene(verts, faces, mat, albedo=albedo, emission=emission,
                     material_types=[0, mat1_type, 0, 0],
                     roughness=[0.3, rough1, 0.3, 0.3])


@requires_cuda
@pytest.mark.kernels_ci
def test_plastic_estimators_and_fd():
    """Rough plastic: MIS and BRDF-only agree (validating the mixed-lobe
    pdf), and the albedo gradient through both lobes matches central
    differences (the diffuse part is affine, the coat constant)."""
    cam = cornell_camera()
    scene = cornell_mats(ptd.PLASTIC, rough1=0.25)
    m_mis = ptd.render(scene, cam, 64, 64, spp=64, max_bounces=4,
                       estimator="mis", seed=1).mean().item()
    m_brdf = ptd.render(scene, cam, 64, 64, spp=768, max_bounces=4,
                        estimator="brdf", seed=2).mean().item()
    assert abs(m_mis - m_brdf) / m_brdf < 0.04, (m_mis, m_brdf)

    H = W = 48
    torch.manual_seed(0)
    Wr = torch.rand(H, W, 3, device="cuda")
    base = torch.tensor([[0.73] * 3, [0.9, 0.5, 0.2], [0.12, 0.45, 0.15],
                         [0.78] * 3], device="cuda")

    def loss_of(alb):
        s = cornell_mats(ptd.PLASTIC, rough1=0.25)
        s.albedo = alb
        return (ptd.render(s, cam, H, W, spp=8, max_bounces=3, seed=13)
                * Wr).sum()

    alb = base.clone().requires_grad_(True)
    loss_of(alb).backward()
    worst = 0.0
    for (i, c) in [(1, 0), (1, 2)]:
        h = 2e-3
        ap = base.clone(); ap[i, c] += h
        am = base.clone(); am[i, c] -= h
        fd = (loss_of(ap) - loss_of(am)).item() / (2 * h)
        assert fd != 0.0
        worst = max(worst, abs(alb.grad[i, c].item() - fd) / abs(fd))
    assert worst < 2e-2, worst


@requires_cuda
@pytest.mark.kernels_ci
def test_rough_dielectric_smooth_limit():
    """A rough-dielectric slab at alpha = 0.02 must transmit the smooth
    analytic series (1-R)/(1+R) at normal incidence within tolerance."""
    verts, faces, mat = [], [], []

    def quad_w(a, b, c, d, m):
        base = len(verts)
        verts.extend([a, b, c, d])
        faces.extend(quad(base, base + 1, base + 2, base + 3))
        mat.extend([m, m])

    quad_w((-10, -10, 5), (10, -10, 5), (10, 10, 5), (-10, 10, 5), 0)
    add_box(verts, faces, mat, (-10, -10, 2.0), (10, 10, 2.2), 1)
    scene = ptd.Scene(verts, faces, mat, albedo=[[0, 0, 0], [0, 0, 0]],
                      emission=[[1.0] * 3, [0, 0, 0]],
                      material_types=[ptd.DIFFUSE, ptd.ROUGH_DIELECTRIC],
                      roughness=[0.3, 0.02], ior=[1.5, 1.5])
    cam = ptd.Camera(position=(0, 0, 0), look_at=(0, 0, 1), vfov_deg=10.0)
    img = ptd.render(scene, cam, 32, 32, spp=512, max_bounces=10,
                     estimator="brdf", seed=5)
    R = (0.5 / 2.5) ** 2
    expected = (1 - R) / (1 + R)
    got = img[12:20, 12:20].mean().item()
    assert abs(got - expected) / expected < 0.02, (got, expected)


@requires_cuda
@pytest.mark.kernels_ci
def test_emission_texture_fd_and_inverse():
    """Per-texel emission: gradients are linear-exact against central
    differences, and an emissive panel texture recovers from the room it
    lights."""
    cam = cornell_camera()
    H = W = 48
    torch.manual_seed(0)
    Wr = torch.rand(H, W, 3, device="cuda")

    def build(etex):
        verts, faces, mat = [], [], []

        def wall(a, b, c, d, m, uv=False):
            b0 = len(verts)
            verts.extend([a, b, c, d])
            faces.extend(quad(b0, b0 + 1, b0 + 2, b0 + 3))
            mat.extend([m, m])
            uvs.extend([[(0, 0), (1, 0), (1, 1)], [(0, 0), (1, 1), (0, 1)]]
                       if uv else [[(0, 0)] * 3] * 2)

        uvs = []
        X, Y, Z = 5.56, 5.488, 5.592
        wall((0, 0, 0), (X, 0, 0), (X, 0, Z), (0, 0, Z), 0)
        wall((0, Y, 0), (0, Y, Z), (X, Y, Z), (X, Y, 0), 0)
        wall((0, 0, Z), (X, 0, Z), (X, Y, Z), (0, Y, Z), 0)
        wall((0, 0, 0), (0, 0, Z), (0, Y, Z), (0, Y, 0), 1)
        wall((X, 0, 0), (X, Y, 0), (X, Y, Z), (X, 0, Z), 2)
        wall((2.13, Y - 0.001, 2.27), (3.43, Y - 0.001, 2.27),
             (3.43, Y - 0.001, 3.32), (2.13, Y - 0.001, 3.32), 3, uv=True)
        albedo = [[0.73] * 3, [0.65, 0.05, 0.05], [0.12, 0.45, 0.15],
                  [0.78] * 3]
        emission = [torch.zeros(3), torch.zeros(3), torch.zeros(3), etex]
        return ptd.Scene(verts, faces, mat, albedo=albedo, emission=emission,
                         uvs=uvs)

    torch.manual_seed(5)
    e0 = torch.rand(4, 8, 3, device="cuda") * 12.0 + 3.0
    scene = build(e0)

    def loss_of(e):
        scene.emission_textures[3] = e.to("cuda")
        img = ptd.render(scene, cam, H, W, spp=8, max_bounces=3, seed=7)
        return (img * Wr).sum()

    e = e0.clone().requires_grad_(True)
    loss_of(e).backward()
    worst = 0.0
    for (y, x, c) in [(0, 0, 0), (2, 5, 1), (3, 7, 2)]:
        h = 0.1
        ep = e0.clone(); ep[y, x, c] += h
        em = e0.clone(); em[y, x, c] -= h
        fd = (loss_of(ep) - loss_of(em)).item() / (2 * h)
        assert fd != 0.0, (y, x, c)
        worst = max(worst, abs(e.grad[y, x, c].item() - fd) / abs(fd))
    assert worst < 2e-2, worst

    target = ptd.render(build(e0), cam, H, W, spp=8, max_bounces=3,
                        seed=3).detach()
    t = torch.full((4, 8, 3), 8.0, device="cuda", requires_grad=True)
    s_opt = build(t)
    opt = torch.optim.Adam([t], lr=0.4)
    first = None
    for _ in range(150):
        opt.zero_grad()
        loss = (ptd.render(s_opt, cam, H, W, spp=8, max_bounces=3, seed=3)
                - target).square().mean()
        loss.backward()
        opt.step()
        with torch.no_grad():
            t.clamp_(0.1, 30.0)
        if first is None:
            first = loss.item()
    assert loss.item() < first * 0.05, (first, loss.item())
    rel = ((t.detach() - e0).abs() / e0).mean().item()
    assert rel < 0.2, rel


@requires_cuda
@pytest.mark.kernels_ci
def test_medium_beer_lambert_and_fd():
    """Gray purely-absorbing medium: the mean transmitted radiance follows
    Beer-Lambert, and sigma gradients match central differences exactly
    (the sampling rate is detached)."""
    verts, faces, mat = [], [], []
    b0 = len(verts)
    verts.extend([(-10, -10, 4), (10, -10, 4), (10, 10, 4), (-10, 10, 4)])
    faces.extend(quad(b0, b0 + 1, b0 + 2, b0 + 3))
    mat.extend([0, 0])
    cam = ptd.Camera(position=(0, 0, 0), look_at=(0, 0, 1), vfov_deg=10.0)

    def build(sa, ss):
        return ptd.Scene(verts, faces, mat, albedo=[[0, 0, 0]],
                         emission=[[2.0, 2.0, 2.0]], medium=(sa, ss))

    sa0 = torch.tensor([0.25, 0.25, 0.25], device="cuda")
    ss0 = torch.tensor([1e-6, 1e-6, 1e-6], device="cuda")
    scene = build(sa0, ss0)
    img = ptd.render(scene, cam, 32, 32, spp=1024, max_bounces=2,
                     estimator="brdf", seed=4)
    expected = 2.0 * math.exp(-0.25 * 4.0)
    got = img.mean().item()
    assert abs(got - expected) / expected < 0.02, (got, expected)

    torch.manual_seed(0)
    Wr = torch.rand(32, 32, 3, device="cuda")

    def loss_of(sa, ss):
        s = build(sa, ss)
        s.med_sbar = scene.med_sbar  # keep the frozen rate identical
        img = ptd.render(s, cam, 32, 32, spp=16, max_bounces=2, seed=9)
        return (img * Wr).sum()

    sa = sa0.clone().requires_grad_(True)
    ss = ss0.clone().requires_grad_(True)
    loss_of(sa, ss).backward()
    h = 5e-3
    sap = sa0.clone(); sap[1] += h
    sam = sa0.clone(); sam[1] -= h
    fd = (loss_of(sap, ss0) - loss_of(sam, ss0)).item() / (2 * h)
    assert fd != 0.0
    assert abs(sa.grad[1].item() - fd) / abs(fd) < 2e-2, \
        (sa.grad[1].item(), fd)


@requires_cuda
@pytest.mark.kernels_ci
def test_medium_scattering_estimators_and_fd():
    """Foggy Cornell: MIS and BRDF-only agree in the mean, and the
    sigma_s gradient matches central differences under detached sampling."""
    def build(sa, ss):
        verts, faces, mat = [], [], []

        def wall(a, b, c, d, m):
            b0 = len(verts)
            verts.extend([a, b, c, d])
            faces.extend(quad(b0, b0 + 1, b0 + 2, b0 + 3))
            mat.extend([m, m])

        X, Y, Z = 5.56, 5.488, 5.592
        wall((0, 0, 0), (X, 0, 0), (X, 0, Z), (0, 0, Z), 0)
        wall((0, Y, 0), (0, Y, Z), (X, Y, Z), (X, Y, 0), 0)
        wall((0, 0, Z), (X, 0, Z), (X, Y, Z), (0, Y, Z), 0)
        wall((0, 0, 0), (0, 0, Z), (0, Y, Z), (0, Y, 0), 1)
        wall((X, 0, 0), (X, Y, 0), (X, Y, Z), (X, 0, Z), 2)
        wall((2.13, Y - 0.001, 2.27), (3.43, Y - 0.001, 2.27),
             (3.43, Y - 0.001, 3.32), (2.13, Y - 0.001, 3.32), 3)
        return ptd.Scene(verts, faces, mat,
                         albedo=[[0.73] * 3, [0.65, 0.05, 0.05],
                                 [0.12, 0.45, 0.15], [0.78] * 3],
                         emission=[[0, 0, 0], [0, 0, 0], [0, 0, 0],
                                   [18.4, 15.6, 8.0]], medium=(sa, ss))

    cam = cornell_camera()
    sa0 = torch.tensor([0.02, 0.02, 0.02], device="cuda")
    ss0 = torch.tensor([0.06, 0.06, 0.06], device="cuda")
    fog = build(sa0, ss0)
    m_mis = ptd.render(fog, cam, 64, 64, spp=64, max_bounces=6,
                       estimator="mis", seed=1).mean().item()
    m_brdf = ptd.render(fog, cam, 64, 64, spp=1024, max_bounces=6,
                        estimator="brdf", seed=2).mean().item()
    assert abs(m_mis - m_brdf) / m_brdf < 0.05, (m_mis, m_brdf)

    torch.manual_seed(0)
    Wr = torch.rand(48, 48, 3, device="cuda")
    sbar = fog.med_sbar

    def loss_of(sa, ss):
        s = build(sa, ss)
        s.med_sbar = sbar
        img = ptd.render(s, cam, 48, 48, spp=8, max_bounces=4, seed=11)
        return (img * Wr).sum()

    sa = sa0.clone().requires_grad_(True)
    ss = ss0.clone().requires_grad_(True)
    loss_of(sa, ss).backward()
    h = 2e-3
    ssp = ss0.clone(); ssp[0] += h
    ssm = ss0.clone(); ssm[0] -= h
    fd = (loss_of(sa0, ssp) - loss_of(sa0, ssm)).item() / (2 * h)
    assert fd != 0.0
    assert abs(ss.grad[0].item() - fd) / abs(fd) < 2e-2, \
        (ss.grad[0].item(), fd)


def _geo_scene(light_y=4.0, occluder_dx=None, single_vert=None):
    verts, faces, mat = [], [], []
    b0 = len(verts)
    verts.extend([(-4, 0, -4), (4, 0, -4), (4, 0, 4), (-4, 0, 4)])  # floor
    faces.extend(quad(b0, b0 + 1, b0 + 2, b0 + 3))
    mat.extend([0, 0])
    b0 = len(verts)
    verts.extend([(-0.8, light_y, -0.8), (0.8, light_y, -0.8),
                  (0.8, light_y, 0.8), (-0.8, light_y, 0.8)])       # light
    faces.extend(quad(b0, b0 + 1, b0 + 2, b0 + 3))
    mat.extend([1, 1])
    if occluder_dx is not None:
        d = occluder_dx
        b0 = len(verts)
        verts.extend([(-1.0 + d, 2.0, -1.0), (1.0 + d, 2.0, -1.0),
                      (1.0 + d, 2.0, 1.0), (-1.0 + d, 2.0, 1.0)])   # blocker
        faces.extend(quad(b0, b0 + 1, b0 + 2, b0 + 3))
        mat.extend([2, 2])
    if single_vert is not None:
        vid, dx = single_vert
        v = list(verts[vid])
        v[0] += dx
        verts[vid] = tuple(v)
    albedo = [[0.7, 0.7, 0.7], [0.8] * 3, [0.5] * 3]
    emission = [[0, 0, 0], [10.0, 10.0, 10.0], [0, 0, 0]]
    nm = max(mat) + 1
    return ptd.Scene(verts, faces, mat, albedo=albedo[:nm],
                     emission=emission[:nm])


def _geo_loss(scene, cam, Wr, seed=3):
    # brdf at B=2 renders exactly the full direct-lighting integral (no MIS
    # truncation, no indirect), the transport term the geometry pass
    # differentiates
    img = ptd.render(scene, cam, 64, 64, spp=64, max_bounces=2,
                     estimator="brdf", seed=seed)
    return (img * Wr).sum()


@requires_cuda
@pytest.mark.kernels_ci
def test_geometry_interior_matches_fd_smooth():
    """Translating the area light vertically: the dual-number interior term
    (shading + light-area measure) plus the light's own primary-silhouette
    term must match seed-averaged central differences of the direct
    image."""
    cam = ptd.Camera(position=(0, 5.0, 7.0), look_at=(0, 0, 0),
                     vfov_deg=45.0)
    torch.manual_seed(0)
    Wr = torch.rand(64, 64, 3, device="cuda")
    h = 5e-2
    fds = []
    for seed in range(3, 9):
        fds.append((_geo_loss(_geo_scene(light_y=4.0 + h), cam, Wr, seed=seed)
                    - _geo_loss(_geo_scene(light_y=4.0 - h), cam, Wr,
                                seed=seed)).item() / (2 * h))
    fd = sum(fds) / len(fds)
    scene = _geo_scene(light_y=4.0)
    gv = ptd.geometry_grad(scene, cam, Wr, spp=128, edge_samples=1 << 18,
                           seed=11)
    an = gv[4:8, 1].sum().item()  # the light's four verts, y components
    assert fd != 0.0
    assert abs(an - fd) / abs(fd) < 0.15, (an, fd)


@requires_cuda
@pytest.mark.kernels_ci
def test_geometry_boundary_per_vertex_fd():
    """Moving each occluder vertex sweeps its shadow (boundary term) and
    its own image outline (primary-silhouette term); the interior term is
    zero on the occluder (translating a plane inside itself changes
    nothing). Each vertex's x-gradient must match the finite-difference
    derivative averaged over seeds (a single-seed FD of a visibility
    discontinuity is flip noise)."""
    cam = ptd.Camera(position=(0, 5.0, 7.0), look_at=(0, 0, 0),
                     vfov_deg=45.0)
    torch.manual_seed(0)
    Wr = torch.rand(64, 64, 3, device="cuda")
    scene = _geo_scene(occluder_dx=0.0)
    gv = ptd.geometry_grad(scene, cam, Wr, spp=64, edge_samples=1 << 20,
                           seed=7)
    h = 0.1
    for vid in (8, 9, 10, 11):
        fds = []
        for seed in range(3, 9):
            fp = _geo_scene(occluder_dx=0.0, single_vert=(vid, h))
            fm = _geo_scene(occluder_dx=0.0, single_vert=(vid, -h))
            fds.append((_geo_loss(fp, cam, Wr, seed=seed) -
                        _geo_loss(fm, cam, Wr, seed=seed)).item() / (2 * h))
        fd = sum(fds) / len(fds)
        an = gv[vid, 0].item()
        assert fd != 0.0
        assert an * fd > 0.0, (vid, an, fd)
        assert abs(an - fd) / abs(fd) < 0.35, (vid, an, fd)


@requires_cuda
@pytest.mark.kernels_ci
def test_validation():
    verts, faces, mat = [], [], []
    add_box(verts, faces, mat, (0, 0, 0), (1, 1, 1), 0)
    with pytest.raises(ValueError):
        ptd.Scene(verts, faces, mat, albedo=[[0.5] * 3] * 65,
                  emission=[[0.0] * 3] * 65)
    scene = ptd.Scene(verts, faces, mat, albedo=[[0.5] * 3],
                      emission=[[1.0] * 3])
    cam = ptd.Camera(position=(0.5, 0.5, 0.5), look_at=(0.5, 0.5, 1.0))
    with pytest.raises(ValueError):
        ptd.render(scene, cam, 8, 8, spp=1, max_bounces=17)
    with pytest.raises(ValueError):
        ptd.render(scene, cam, 8, 8, spp=1, estimator="mis", nee=True)
    with pytest.raises(ValueError):
        ptd.Scene(verts, faces, mat, albedo=[[0.5] * 3],
                  emission=[[1.0] * 3], material_types=[7])
    env = torch.full((4, 8, 3), 1.0)
    scene_env = ptd.Scene(verts, faces, mat, albedo=[[0.5] * 3],
                          emission=[[1.0] * 3], env=env)
    with pytest.raises(ValueError):
        ptd.render(scene_env, cam, 8, 8, spp=1, estimator="nee")
    img = ptd.render(scene, cam, 8, 8, spp=2, max_bounces=2)
    assert img.shape == (8, 8, 3) and img.dtype == torch.float32
    assert torch.isfinite(img).all()