oxigaf-render 0.1.2

Differentiable 3D Gaussian Splatting rasterizer (wgpu)
Documentation
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
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
//! FLAME mesh binding for Gaussians.
//!
//! Each Gaussian is bound to a face on the FLAME mesh via barycentric
//! coordinates and a learnable local offset. This module computes the
//! world-space Gaussian positions from the current FLAME mesh pose and
//! the binding parameters.
//!
//! # Indexing contract
//!
//! Two different frame arrays appear in this module and they are **not**
//! interchangeable:
//!
//! * [`BindingResult::face_frames`] — one frame **per Gaussian**, the frame of
//!   the face that Gaussian is bound to. Used to orient the Gaussian
//!   ([`frame_to_quaternion`]).
//! * [`build_vertex_frames`] — one frame **per mesh vertex**. This is what the
//!   `flame_binding_bwd` shader indexes (`tbn_frames[vertex_id]`), so it is what
//!   [`FlameBindingBuffers::update_tbn_frames`] requires.

use nalgebra as na;
use oxigaf_flame::Mesh;

use crate::gaussian::GaussianModel;
use crate::RenderError;

/// Workgroup size of the `flame_binding_backward` kernel
/// (`shaders/flame_binding_bwd.wgsl`).
const BINDING_BWD_WORKGROUP_SIZE: u32 = 256;

/// Degeneracy threshold for vector lengths.
const FRAME_EPSILON: f32 = 1e-10;

/// Result of applying FLAME mesh binding to a Gaussian model.
#[derive(Debug)]
pub struct BindingResult {
    /// Updated world-space positions for each Gaussian.
    pub positions: Vec<[f32; 3]>,
    /// Coordinate frame of the bound face, **one entry per Gaussian**
    /// (not per mesh vertex — see the module-level indexing contract).
    pub face_frames: Vec<FaceFrame>,
}

/// Local coordinate frame on a mesh face.
#[derive(Debug, Clone, Copy)]
pub struct FaceFrame {
    /// Tangent direction (edge0 normalized).
    pub tangent: na::Vector3<f32>,
    /// Bitangent (normal × tangent).
    pub bitangent: na::Vector3<f32>,
    /// Face normal.
    pub normal: na::Vector3<f32>,
}

impl FaceFrame {
    /// Axis-aligned fallback frame, used when a normal or tangent is degenerate.
    #[must_use]
    pub fn axis_aligned() -> Self {
        Self {
            tangent: na::Vector3::x(),
            bitangent: na::Vector3::y(),
            normal: na::Vector3::z(),
        }
    }

    /// Build an orthonormal frame from a (possibly unnormalized) `normal` and a
    /// `tangent_hint`.
    ///
    /// The hint is Gram-Schmidt orthogonalized against the normal. When the
    /// normal is degenerate the frame falls back to [`FaceFrame::axis_aligned`];
    /// when only the hint is unusable (zero, non-finite, or parallel to the
    /// normal) an arbitrary perpendicular axis is chosen instead.
    #[must_use]
    pub fn from_normal_and_tangent(
        normal: na::Vector3<f32>,
        tangent_hint: na::Vector3<f32>,
    ) -> Self {
        let normal_len = normal.norm();
        if !normal_len.is_finite() || normal_len <= FRAME_EPSILON {
            return Self::axis_aligned();
        }
        let unit_normal = normal / normal_len;

        // Remove the normal component from the hint.
        let mut tangent = tangent_hint - unit_normal * unit_normal.dot(&tangent_hint);
        let mut tangent_len = tangent.norm();
        if !tangent_len.is_finite() || tangent_len <= FRAME_EPSILON {
            // Hint unusable: pick the world axis least aligned with the normal.
            let nx = unit_normal.x.abs();
            let ny = unit_normal.y.abs();
            let nz = unit_normal.z.abs();
            let axis = if nx <= ny && nx <= nz {
                na::Vector3::x()
            } else if ny <= nz {
                na::Vector3::y()
            } else {
                na::Vector3::z()
            };
            tangent = axis - unit_normal * unit_normal.dot(&axis);
            tangent_len = tangent.norm();
        }
        if !tangent_len.is_finite() || tangent_len <= FRAME_EPSILON {
            return Self::axis_aligned();
        }

        let tangent = tangent / tangent_len;
        let bitangent = unit_normal.cross(&tangent);
        Self {
            tangent,
            bitangent,
            normal: unit_normal,
        }
    }
}

/// Look up a mesh vertex, reporting the owning Gaussian on failure.
fn lookup_vertex(
    mesh: &Mesh,
    vertex_id: u32,
    gaussian_index: usize,
) -> Result<&na::Point3<f32>, RenderError> {
    mesh.vertices
        .get(vertex_id as usize)
        .ok_or_else(|| RenderError::InvalidGaussian {
            index: gaussian_index,
            reason: format!(
                "bound face references vertex {vertex_id}, but the mesh has {} vertices",
                mesh.vertices.len()
            ),
        })
}

/// Compute the binding positions for all Gaussians given the current FLAME mesh.
///
/// For each Gaussian:
/// 1. Look up its bound face on the mesh.
/// 2. Interpolate the position using barycentric coordinates.
/// 3. Compute the face's local frame (tangent, bitangent, normal).
/// 4. Apply the local offset in the face's local frame.
///
/// Rigid Gaussians have `local_offsets = [0, 0, 0]`.
///
/// # Errors
///
/// * [`RenderError::MismatchedBufferSizes`] when `face_indices`, `barycentric`
///   or `local_offsets` do not have exactly one entry per Gaussian (they are
///   empty for any model loaded from PLY/SafeTensors without binding data).
/// * [`RenderError::ValidationError`] when the mesh has no faces or vertices.
/// * [`RenderError::InvalidGaussian`] when a Gaussian references a face index
///   outside the mesh, or a face references a vertex outside the mesh.
pub fn apply_binding(model: &GaussianModel, mesh: &Mesh) -> Result<BindingResult, RenderError> {
    let n = model.len();

    // The three binding arrays are independent Vecs; all must be fully populated.
    if model.face_indices.len() != n {
        return Err(RenderError::MismatchedBufferSizes {
            expected: n,
            actual: model.face_indices.len(),
        });
    }
    if model.barycentric.len() != n {
        return Err(RenderError::MismatchedBufferSizes {
            expected: n,
            actual: model.barycentric.len(),
        });
    }
    if model.local_offsets.len() != n {
        return Err(RenderError::MismatchedBufferSizes {
            expected: n,
            actual: model.local_offsets.len(),
        });
    }

    if n == 0 {
        return Ok(BindingResult {
            positions: Vec::new(),
            face_frames: Vec::new(),
        });
    }

    if mesh.faces.is_empty() || mesh.vertices.is_empty() {
        return Err(RenderError::ValidationError(format!(
            "FLAME binding requires a non-empty mesh, got {} faces and {} vertices",
            mesh.faces.len(),
            mesh.vertices.len()
        )));
    }

    let mut positions = Vec::with_capacity(n);
    let mut face_frames = Vec::with_capacity(n);

    for i in 0..n {
        let face_idx = model.face_indices[i] as usize;
        let bary = model.barycentric[i];
        let offset = model.local_offsets[i];

        // Get face vertices (no silent clamping: an out-of-range index is a bug
        // in the binding data, not something to paper over with the last face).
        let face = mesh
            .faces
            .get(face_idx)
            .ok_or_else(|| RenderError::InvalidGaussian {
                index: i,
                reason: format!(
                    "bound face index {face_idx} out of range (mesh has {} faces)",
                    mesh.faces.len()
                ),
            })?;
        let v0 = lookup_vertex(mesh, face[0], i)?;
        let v1 = lookup_vertex(mesh, face[1], i)?;
        let v2 = lookup_vertex(mesh, face[2], i)?;

        // Interpolate position
        let base_pos = v0.coords * bary[0] + v1.coords * bary[1] + v2.coords * bary[2];

        // Compute face frame (edge0 is already perpendicular to the face normal,
        // so the Gram-Schmidt step inside `from_normal_and_tangent` is a no-op).
        let edge0 = v1 - v0;
        let edge1 = v2 - v0;
        let frame = FaceFrame::from_normal_and_tangent(edge0.cross(&edge1), edge0);

        // Apply local offset in face frame
        let world_offset =
            frame.tangent * offset[0] + frame.bitangent * offset[1] + frame.normal * offset[2];

        let pos = base_pos + world_offset;
        positions.push([pos.x, pos.y, pos.z]);
        face_frames.push(frame);
    }

    Ok(BindingResult {
        positions,
        face_frames,
    })
}

/// Build one TBN frame **per mesh vertex**.
///
/// This is the array the `flame_binding_bwd` shader expects: it indexes
/// `tbn_frames[vertex_id]`, so entry `k` must be the frame of mesh vertex `k`.
/// The returned vector always has exactly `mesh.vertices.len()` entries.
///
/// The normal is taken from `mesh.normals` when that array is present, sized
/// consistently and non-degenerate; otherwise it is recomputed as the
/// area-weighted average of the incident face normals. The tangent is the
/// average incident edge direction, orthogonalized against the normal.
/// Isolated or degenerate vertices get [`FaceFrame::axis_aligned`].
#[must_use]
pub fn build_vertex_frames(mesh: &Mesh) -> Vec<FaceFrame> {
    let num_vertices = mesh.vertices.len();
    let mut accum_normal = vec![na::Vector3::<f32>::zeros(); num_vertices];
    let mut accum_tangent = vec![na::Vector3::<f32>::zeros(); num_vertices];

    for face in &mesh.faces {
        let i0 = face[0] as usize;
        let i1 = face[1] as usize;
        let i2 = face[2] as usize;
        let (Some(v0), Some(v1), Some(v2)) = (
            mesh.vertices.get(i0),
            mesh.vertices.get(i1),
            mesh.vertices.get(i2),
        ) else {
            // Skip faces referencing vertices outside the mesh.
            continue;
        };

        let edge0 = v1 - v0;
        let edge1 = v2 - v0;
        // Un-normalized cross product => area weighting.
        let face_normal = edge0.cross(&edge1);

        for &vid in &[i0, i1, i2] {
            if let Some(slot) = accum_normal.get_mut(vid) {
                *slot += face_normal;
            }
            if let Some(slot) = accum_tangent.get_mut(vid) {
                *slot += edge0;
            }
        }
    }

    // Only trust the mesh's own normals when the array is sized consistently.
    let use_mesh_normals = mesh.normals.len() == num_vertices;

    (0..num_vertices)
        .map(|i| {
            let mut normal = accum_normal[i];
            if use_mesh_normals {
                if let Some(mesh_normal) = mesh.normals.get(i) {
                    let len = mesh_normal.norm();
                    if len.is_finite() && len > FRAME_EPSILON {
                        normal = *mesh_normal;
                    }
                }
            }
            FaceFrame::from_normal_and_tangent(normal, accum_tangent[i])
        })
        .collect()
}

/// Build one TBN frame **per mesh face**, in face order.
///
/// This is the frame [`apply_binding`] actually applies the local offset in, so
/// a backward pass that projects gradients onto `build_face_frames(mesh)`
/// indexed by `face_indices[i]` is the exact adjoint of the forward binding.
/// (Per-vertex frames from [`build_vertex_frames`] are an approximation of it.)
///
/// Faces referencing vertices outside the mesh get [`FaceFrame::axis_aligned`],
/// so the returned vector always has exactly `mesh.faces.len()` entries.
#[must_use]
pub fn build_face_frames(mesh: &Mesh) -> Vec<FaceFrame> {
    mesh.faces
        .iter()
        .map(|face| {
            let (Some(v0), Some(v1), Some(v2)) = (
                mesh.vertices.get(face[0] as usize),
                mesh.vertices.get(face[1] as usize),
                mesh.vertices.get(face[2] as usize),
            ) else {
                return FaceFrame::axis_aligned();
            };
            let edge0 = v1 - v0;
            let edge1 = v2 - v0;
            FaceFrame::from_normal_and_tangent(edge0.cross(&edge1), edge0)
        })
        .collect()
}

/// Compute position regularization: sum of squared distances between
/// each Gaussian's current position and its binding position.
pub fn position_regularization(
    current_positions: &[[f32; 3]],
    binding_positions: &[[f32; 3]],
) -> f32 {
    current_positions
        .iter()
        .zip(binding_positions.iter())
        .map(|(cur, bind)| {
            let dx = cur[0] - bind[0];
            let dy = cur[1] - bind[1];
            let dz = cur[2] - bind[2];
            dx * dx + dy * dy + dz * dz
        })
        .sum()
}

/// Compute scale regularization: penalize Gaussians whose scale exceeds `max_scale`.
pub fn scale_regularization(scales: &[[f32; 3]], max_scale: f32) -> f32 {
    scales
        .iter()
        .map(|s| {
            let excess_x = (s[0].exp() - max_scale).max(0.0);
            let excess_y = (s[1].exp() - max_scale).max(0.0);
            let excess_z = (s[2].exp() - max_scale).max(0.0);
            excess_x * excess_x + excess_y * excess_y + excess_z * excess_z
        })
        .sum()
}

/// Convert a face frame to a quaternion (for initializing Gaussian rotations).
pub fn frame_to_quaternion(frame: &FaceFrame) -> [f32; 4] {
    // Build rotation matrix from frame columns (tangent, bitangent, normal)
    let rot = na::Matrix3::from_columns(&[frame.tangent, frame.bitangent, frame.normal]);
    let uq = na::UnitQuaternion::from_rotation_matrix(&na::Rotation3::from_matrix_unchecked(rot));
    let q = uq.quaternion();
    // wgpu convention: [x, y, z, w]
    [q.i, q.j, q.k, q.w]
}

/// CPU reference for the `flame_binding_backward` kernel.
///
/// Projects `∂L/∂position` onto each Gaussian's bound TBN frame:
/// `∂L/∂offset = [dot(g, T), dot(g, B), dot(g, N)]`. Gaussians whose frame is
/// degenerate (zero normal) receive a zero gradient, matching the shader.
///
/// `vertex_frames` must be indexed **by mesh vertex** (see
/// [`build_vertex_frames`]), `position_grads` and `vertex_ids` by Gaussian.
///
/// # Errors
///
/// * [`RenderError::MismatchedBufferSizes`] when `vertex_ids` and
///   `position_grads` differ in length.
/// * [`RenderError::InvalidGaussian`] when a vertex id is out of range.
pub fn offset_gradients_cpu(
    position_grads: &[[f32; 3]],
    vertex_ids: &[u32],
    vertex_frames: &[FaceFrame],
) -> Result<Vec<[f32; 3]>, RenderError> {
    if vertex_ids.len() != position_grads.len() {
        return Err(RenderError::MismatchedBufferSizes {
            expected: position_grads.len(),
            actual: vertex_ids.len(),
        });
    }

    let mut out = Vec::with_capacity(position_grads.len());
    for (i, (&vid, grad)) in vertex_ids.iter().zip(position_grads.iter()).enumerate() {
        let frame =
            vertex_frames
                .get(vid as usize)
                .ok_or_else(|| RenderError::InvalidGaussian {
                    index: i,
                    reason: format!(
                        "bound vertex id {vid} out of range ({} vertex frames)",
                        vertex_frames.len()
                    ),
                })?;
        // Match the shader's degenerate-frame guard (`length(N) > 0.0001`).
        if frame.normal.norm() <= 1e-4 {
            out.push([0.0, 0.0, 0.0]);
            continue;
        }
        let g = na::Vector3::new(grad[0], grad[1], grad[2]);
        out.push([
            g.dot(&frame.tangent),
            g.dot(&frame.bitangent),
            g.dot(&frame.normal),
        ]);
    }
    Ok(out)
}

/// GPU buffers for the FLAME binding backward pass.
///
/// These buffers feed the `flame_binding_bwd` compute pipeline, which computes
/// gradients w.r.t. the learnable local offsets given gradients w.r.t. the
/// world-space Gaussian positions.
///
/// # Frame table
///
/// The shader reads `tbn_frames[binding_info[gaussian_id].vertex_id]`, i.e.
/// `tbn_frames` is a **frame table** of `num_vertices` entries and
/// `binding_info` holds one index into it per Gaussian. Two configurations are
/// valid, and `num_vertices` names the table length in both:
///
/// * frame table = [`build_vertex_frames`], indices = mesh vertex ids —
///   an approximation, one frame per vertex;
/// * frame table = [`build_face_frames`], indices = `GaussianModel::face_indices` —
///   exactly the frames [`apply_binding`] used in the forward pass.
///
/// Passing the per-Gaussian [`BindingResult::face_frames`] here is always wrong:
/// entry `k` would be the frame of Gaussian `k`, not of index `k`.
///
/// Typical use, once per training step:
///
/// 1. [`update_binding_info`](Self::update_binding_info) — vertex id per Gaussian (static).
/// 2. [`update_tbn_frames`](Self::update_tbn_frames) — per-**vertex** frames for the current pose.
/// 3. [`update_position_gradients`](Self::update_position_gradients) or
///    [`copy_position_gradients_from`](Self::copy_position_gradients_from) — `∂L/∂position`.
/// 4. [`backward`](Self::backward) (or [`record_backward`](Self::record_backward) +
///    [`read_offset_gradients`](Self::read_offset_gradients)) — dispatch and read `∂L/∂offset`.
pub struct FlameBindingBuffers {
    /// Binding info buffer (vertex_id per Gaussian).
    pub binding_info: wgpu::Buffer,
    /// TBN frames buffer (tangent, bitangent, normal **per mesh vertex**).
    pub tbn_frames: wgpu::Buffer,
    /// Position gradients input buffer.
    pub position_grads: wgpu::Buffer,
    /// Local offset gradients output buffer.
    pub offset_grads: wgpu::Buffer,
    /// Uniform buffer for parameters.
    pub uniforms: wgpu::Buffer,
    /// Bind group for the backward pass.
    pub bind_group: wgpu::BindGroup,
    /// Number of Gaussians.
    num_gaussians: usize,
    /// Number of vertices.
    num_vertices: usize,
}

impl FlameBindingBuffers {
    /// Create new FLAME binding buffers.
    ///
    /// # Arguments
    ///
    /// * `device` - WGPU device
    /// * `bind_group_layout` - Bind group layout for the flame_binding_bwd shader
    /// * `num_gaussians` - Number of Gaussians
    /// * `num_vertices` - Number of mesh vertices
    pub fn new(
        device: &wgpu::Device,
        bind_group_layout: &wgpu::BindGroupLayout,
        num_gaussians: usize,
        num_vertices: usize,
    ) -> Self {
        use wgpu::util::DeviceExt;

        // Zero-sized storage buffers are a validation error, so every buffer is
        // allocated with room for at least one element.
        let gaussian_slots = num_gaussians.max(1) as u64;
        let vertex_slots = num_vertices.max(1) as u64;

        // Binding info buffer: [vertex_id, pad, pad, pad] per Gaussian
        let binding_info = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("flame_binding_info"),
            size: gaussian_slots * 16, // 4 u32s = 16 bytes
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        // TBN frames buffer: [T, pad, B, pad, N, pad] per vertex
        let tbn_frames = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("flame_tbn_frames"),
            size: vertex_slots * 48, // 3 vec4s = 48 bytes
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        // Position gradients buffer: [grad, pad] per Gaussian
        let position_grads = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("flame_position_grads"),
            size: gaussian_slots * 16, // vec4 = 16 bytes
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        // Offset gradients buffer: [grad, pad] per Gaussian
        let offset_grads = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("flame_offset_grads"),
            size: gaussian_slots * 16,
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
            mapped_at_creation: false,
        });

        // Uniforms buffer
        let num_gaussians_u32 = u32::try_from(num_gaussians).unwrap_or(u32::MAX);
        let uniforms_data = [num_gaussians_u32, 0, 0, 0]; // [num_gaussians, pad, pad, pad]
        let uniforms = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: Some("flame_binding_uniforms"),
            contents: bytemuck::cast_slice(&uniforms_data),
            usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
        });

        // Create bind group
        let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("flame_binding_bwd_bind_group"),
            layout: bind_group_layout,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: uniforms.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: binding_info.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: tbn_frames.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: position_grads.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 4,
                    resource: offset_grads.as_entire_binding(),
                },
            ],
        });

        Self {
            binding_info,
            tbn_frames,
            position_grads,
            offset_grads,
            uniforms,
            bind_group,
            num_gaussians,
            num_vertices,
        }
    }

    /// Number of Gaussians these buffers were allocated for.
    #[inline]
    #[must_use]
    pub fn num_gaussians(&self) -> usize {
        self.num_gaussians
    }

    /// Number of mesh vertices these buffers were allocated for.
    #[inline]
    #[must_use]
    pub fn num_vertices(&self) -> usize {
        self.num_vertices
    }

    /// Update binding info (vertex IDs per Gaussian).
    ///
    /// # Arguments
    ///
    /// * `queue` - WGPU queue
    /// * `vertex_ids` - Vertex ID for each Gaussian; must contain exactly
    ///   `num_gaussians` entries so no stale entry survives in the buffer.
    ///
    /// # Errors
    ///
    /// * [`RenderError::MismatchedBufferSizes`] when the slice length differs
    ///   from `num_gaussians`.
    /// * [`RenderError::InvalidGaussian`] when a vertex id would index past the
    ///   TBN frame buffer (the shader has no bounds check).
    pub fn update_binding_info(
        &self,
        queue: &wgpu::Queue,
        vertex_ids: &[u32],
    ) -> Result<(), RenderError> {
        if vertex_ids.len() != self.num_gaussians {
            return Err(RenderError::MismatchedBufferSizes {
                expected: self.num_gaussians,
                actual: vertex_ids.len(),
            });
        }

        // Pack as [vertex_id, pad, pad, pad]
        let mut data = Vec::with_capacity(self.num_gaussians * 4);
        for (i, &vid) in vertex_ids.iter().enumerate() {
            if vid as usize >= self.num_vertices {
                return Err(RenderError::InvalidGaussian {
                    index: i,
                    reason: format!(
                        "bound vertex id {vid} out of range (mesh has {} vertices)",
                        self.num_vertices
                    ),
                });
            }
            data.extend_from_slice(&[vid, 0, 0, 0]);
        }
        if !data.is_empty() {
            queue.write_buffer(&self.binding_info, 0, bytemuck::cast_slice(&data));
        }
        Ok(())
    }

    /// Update TBN frames.
    ///
    /// # Arguments
    ///
    /// * `queue` - WGPU queue
    /// * `vertex_frames` - **Per-vertex** frames from [`build_vertex_frames`];
    ///   must contain exactly `num_vertices` entries. Passing the per-Gaussian
    ///   `BindingResult::face_frames` here is a bug: the shader indexes this
    ///   buffer by vertex id.
    ///
    /// # Errors
    ///
    /// [`RenderError::MismatchedBufferSizes`] when the slice length differs from
    /// `num_vertices`.
    pub fn update_tbn_frames(
        &self,
        queue: &wgpu::Queue,
        vertex_frames: &[FaceFrame],
    ) -> Result<(), RenderError> {
        if vertex_frames.len() != self.num_vertices {
            return Err(RenderError::MismatchedBufferSizes {
                expected: self.num_vertices,
                actual: vertex_frames.len(),
            });
        }

        // Pack as [T, pad, B, pad, N, pad] (3 vec4s = 48 bytes per frame)
        let mut data = Vec::with_capacity(vertex_frames.len() * 12);
        for frame in vertex_frames {
            // Tangent
            data.extend_from_slice(&[frame.tangent.x, frame.tangent.y, frame.tangent.z, 0.0]);
            // Bitangent
            data.extend_from_slice(&[frame.bitangent.x, frame.bitangent.y, frame.bitangent.z, 0.0]);
            // Normal
            data.extend_from_slice(&[frame.normal.x, frame.normal.y, frame.normal.z, 0.0]);
        }
        if !data.is_empty() {
            queue.write_buffer(&self.tbn_frames, 0, bytemuck::cast_slice(&data));
        }
        Ok(())
    }

    /// Update position gradients from CPU data.
    ///
    /// # Arguments
    ///
    /// * `queue` - WGPU queue
    /// * `gradients` - Position gradients `[N × 3]`; must contain exactly
    ///   `num_gaussians` entries.
    ///
    /// # Errors
    ///
    /// [`RenderError::MismatchedBufferSizes`] when the slice length differs from
    /// `num_gaussians`.
    pub fn update_position_gradients(
        &self,
        queue: &wgpu::Queue,
        gradients: &[[f32; 3]],
    ) -> Result<(), RenderError> {
        if gradients.len() != self.num_gaussians {
            return Err(RenderError::MismatchedBufferSizes {
                expected: self.num_gaussians,
                actual: gradients.len(),
            });
        }

        // Pack as vec4 [grad, pad]
        let mut data = Vec::with_capacity(self.num_gaussians * 4);
        for grad in gradients {
            data.extend_from_slice(&[grad[0], grad[1], grad[2], 0.0]);
        }
        if !data.is_empty() {
            queue.write_buffer(&self.position_grads, 0, bytemuck::cast_slice(&data));
        }
        Ok(())
    }

    /// Copy position gradients straight from a GPU gradient buffer.
    ///
    /// `src` must use the padded `[f32; 4]`-per-Gaussian layout of
    /// `GradientBuffers::grad_positions` and carry `COPY_SRC` usage. This avoids
    /// a GPU→CPU→GPU round trip when chaining `preprocess_bwd` into
    /// `flame_binding_bwd`.
    ///
    /// # Errors
    ///
    /// [`RenderError::BufferOverflow`] when `src` is smaller than
    /// `num_gaussians × 16` bytes.
    pub fn copy_position_gradients_from(
        &self,
        encoder: &mut wgpu::CommandEncoder,
        src: &wgpu::Buffer,
    ) -> Result<(), RenderError> {
        let required = (self.num_gaussians as u64) * 16;
        if required == 0 {
            return Ok(());
        }
        if src.size() < required {
            return Err(RenderError::BufferOverflow {
                buffer_name: "flame_position_grads".to_string(),
                max_size: src.size(),
                requested: required,
            });
        }
        encoder.copy_buffer_to_buffer(src, 0, &self.position_grads, 0, required);
        Ok(())
    }

    /// Record the `flame_binding_backward` dispatch into `encoder`.
    ///
    /// `pipeline` is the `flame_binding_bwd` compute pipeline (see
    /// `Pipelines::flame_binding_bwd`). The kernel writes every element of
    /// `offset_grads`, so no clear is needed beforehand.
    pub fn record_backward(
        &self,
        encoder: &mut wgpu::CommandEncoder,
        pipeline: &wgpu::ComputePipeline,
    ) {
        self.record_backward_with_timestamps(encoder, pipeline, None);
    }

    /// [`record_backward`](Self::record_backward) with GPU timestamp writes.
    ///
    /// `timestamp_writes` comes from
    /// [`GpuTimestampProfiler::pass_writes`](crate::profiler::GpuTimestampProfiler::pass_writes);
    /// passing `None` records the pass untimed.
    pub fn record_backward_with_timestamps(
        &self,
        encoder: &mut wgpu::CommandEncoder,
        pipeline: &wgpu::ComputePipeline,
        timestamp_writes: Option<wgpu::ComputePassTimestampWrites<'_>>,
    ) {
        if self.num_gaussians == 0 {
            return;
        }
        let n = u32::try_from(self.num_gaussians).unwrap_or(u32::MAX);
        let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: Some("flame_binding_bwd"),
            timestamp_writes,
        });
        pass.set_pipeline(pipeline);
        pass.set_bind_group(0, &self.bind_group, &[]);
        pass.dispatch_workgroups(n.div_ceil(BINDING_BWD_WORKGROUP_SIZE), 1, 1);
    }

    /// Read `∂L/∂local_offset` back to the CPU.
    ///
    /// # Errors
    ///
    /// [`RenderError::BufferMapFailed`] when the staging buffer cannot be mapped.
    pub fn read_offset_gradients(
        &self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
    ) -> Result<Vec<[f32; 3]>, RenderError> {
        if self.num_gaussians == 0 {
            return Ok(Vec::new());
        }
        let byte_size = (self.num_gaussians as u64) * 16;

        let staging = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("flame_offset_grads_staging"),
            size: byte_size,
            usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("flame_offset_grads_readback"),
        });
        encoder.copy_buffer_to_buffer(&self.offset_grads, 0, &staging, 0, byte_size);
        queue.submit(std::iter::once(encoder.finish()));

        let slice = staging.slice(..);
        let (tx, rx) = std::sync::mpsc::channel();
        slice.map_async(wgpu::MapMode::Read, move |result| {
            tx.send(result).ok();
        });
        let _ = device.poll(wgpu::PollType::Wait {
            submission_index: None,
            timeout: None,
        });
        rx.recv()
            .map_err(|e| RenderError::BufferMapFailed {
                buffer_name: "flame_offset_grads".to_string(),
                error: format!("Channel recv failed: {e}"),
            })?
            .map_err(|e| RenderError::BufferMapFailed {
                buffer_name: "flame_offset_grads".to_string(),
                error: e.to_string(),
            })?;

        let data = slice
            .get_mapped_range()
            .map_err(|e| RenderError::BufferMapFailed {
                buffer_name: "flame_offset_grads".to_string(),
                error: format!("Mapped range failed: {e}"),
            })?;
        let floats: &[f32] = bytemuck::cast_slice(&data);
        let grads: Vec<[f32; 3]> = floats
            .chunks_exact(4)
            .take(self.num_gaussians)
            .map(|c| [c[0], c[1], c[2]])
            .collect();
        drop(data);
        staging.unmap();

        Ok(grads)
    }

    /// Dispatch the backward pass and read the resulting offset gradients.
    ///
    /// Convenience wrapper around [`record_backward`](Self::record_backward) and
    /// [`read_offset_gradients`](Self::read_offset_gradients); it submits its own
    /// command buffer and blocks until the GPU is done.
    ///
    /// # Errors
    ///
    /// [`RenderError::BufferMapFailed`] when the readback fails.
    pub fn backward(
        &self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        pipeline: &wgpu::ComputePipeline,
    ) -> Result<Vec<[f32; 3]>, RenderError> {
        let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("flame_binding_backward"),
        });
        self.record_backward(&mut encoder, pipeline);
        queue.submit(std::iter::once(encoder.finish()));
        let _ = device.poll(wgpu::PollType::Wait {
            submission_index: None,
            timeout: None,
        });
        self.read_offset_gradients(device, queue)
    }

    /// Get the bind group for rendering.
    pub fn bind_group(&self) -> &wgpu::BindGroup {
        &self.bind_group
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// One unit triangle in the z = 0 plane: normal = +z, edge0 = +x.
    fn unit_triangle_mesh() -> Mesh {
        Mesh::new(
            vec![
                na::Point3::new(0.0, 0.0, 0.0),
                na::Point3::new(1.0, 0.0, 0.0),
                na::Point3::new(0.0, 1.0, 0.0),
            ],
            vec![[0, 1, 2]],
        )
    }

    fn model_with_gaussians(n: usize) -> GaussianModel {
        GaussianModel {
            gaussians: vec![
                crate::gaussian::GaussianAttributes {
                    position: [0.0, 0.0, 0.0],
                    _pad0: 0.0,
                    rotation: [0.0, 0.0, 0.0, 1.0],
                    scale: [0.0, 0.0, 0.0],
                    opacity: 0.0,
                };
                n
            ],
            sh_coeffs: vec![0.0; n * 3],
            sh_degree: 0,
            face_indices: Vec::new(),
            barycentric: Vec::new(),
            local_offsets: Vec::new(),
            is_rigid: Vec::new(),
        }
    }

    #[test]
    fn test_position_reg_zero_for_matching() {
        let positions = vec![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
        let binding = vec![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
        assert!((position_regularization(&positions, &binding)).abs() < 1e-10);
    }

    #[test]
    fn test_scale_reg_zero_under_max() {
        // log-scales of 0.0 → exp(0) = 1.0, max_scale = 2.0 → no penalty
        let scales = vec![[0.0, 0.0, 0.0]];
        assert!((scale_regularization(&scales, 2.0)).abs() < 1e-10);
    }

    // --- Regression: apply_binding must not panic on missing binding data ---

    #[test]
    fn test_apply_binding_missing_arrays_is_error_not_panic() {
        let model = model_with_gaussians(3); // no face_indices/barycentric/offsets
        let mesh = unit_triangle_mesh();
        let err = apply_binding(&model, &mesh).expect_err("must reject empty binding arrays");
        assert!(matches!(
            err,
            RenderError::MismatchedBufferSizes {
                expected: 3,
                actual: 0
            }
        ));
    }

    #[test]
    fn test_apply_binding_empty_mesh_is_error_not_panic() {
        let mut model = model_with_gaussians(1);
        model.face_indices = vec![0];
        model.barycentric = vec![[1.0, 0.0, 0.0]];
        model.local_offsets = vec![[0.0, 0.0, 0.0]];
        let empty_mesh = Mesh::new(Vec::new(), Vec::new());
        let err = apply_binding(&model, &empty_mesh).expect_err("must reject an empty mesh");
        assert!(matches!(err, RenderError::ValidationError(_)));
    }

    #[test]
    fn test_apply_binding_out_of_range_face_is_error_not_clamped() {
        let mut model = model_with_gaussians(1);
        model.face_indices = vec![7]; // mesh has 1 face
        model.barycentric = vec![[1.0, 0.0, 0.0]];
        model.local_offsets = vec![[0.0, 0.0, 0.0]];
        let mesh = unit_triangle_mesh();
        let err = apply_binding(&model, &mesh).expect_err("must reject out-of-range face index");
        assert!(matches!(err, RenderError::InvalidGaussian { index: 0, .. }));
    }

    #[test]
    fn test_apply_binding_out_of_range_vertex_is_error() {
        let mut model = model_with_gaussians(1);
        model.face_indices = vec![0];
        model.barycentric = vec![[1.0, 0.0, 0.0]];
        model.local_offsets = vec![[0.0, 0.0, 0.0]];
        // Face references vertex 9 which does not exist. Built by struct literal
        // because `Mesh::new` itself indexes vertices while computing normals.
        let mesh = Mesh {
            vertices: vec![
                na::Point3::new(0.0, 0.0, 0.0),
                na::Point3::new(1.0, 0.0, 0.0),
            ],
            normals: vec![na::Vector3::zeros(); 2],
            faces: vec![[0, 1, 9]],
            uv_coords: Vec::new(),
        };
        let err = apply_binding(&model, &mesh).expect_err("must reject out-of-range vertex index");
        assert!(matches!(err, RenderError::InvalidGaussian { index: 0, .. }));
    }

    #[test]
    fn test_apply_binding_empty_model_is_ok() {
        let model = model_with_gaussians(0);
        let mesh = unit_triangle_mesh();
        let result = apply_binding(&model, &mesh).expect("empty model binds trivially");
        assert!(result.positions.is_empty());
        assert!(result.face_frames.is_empty());
    }

    #[test]
    fn test_apply_binding_offset_applied_in_face_frame() {
        let mut model = model_with_gaussians(1);
        model.face_indices = vec![0];
        model.barycentric = vec![[1.0, 0.0, 0.0]]; // v0
        model.local_offsets = vec![[0.0, 0.0, 2.0]]; // 2 along the normal (+z)
        let mesh = unit_triangle_mesh();
        let result = apply_binding(&model, &mesh).expect("binding");
        let p = result.positions[0];
        assert!((p[0]).abs() < 1e-6, "x = {}", p[0]);
        assert!((p[1]).abs() < 1e-6, "y = {}", p[1]);
        assert!((p[2] - 2.0).abs() < 1e-6, "z = {}", p[2]);
        // face_frames is per Gaussian, not per vertex.
        assert_eq!(result.face_frames.len(), 1);
    }

    // --- Regression: TBN frames must be per vertex ---

    #[test]
    fn test_build_vertex_frames_length_matches_vertex_count() {
        let mesh = unit_triangle_mesh();
        let frames = build_vertex_frames(&mesh);
        assert_eq!(frames.len(), mesh.vertices.len());
        for frame in &frames {
            // z-plane triangle → +z normal at every vertex.
            assert!((frame.normal.z - 1.0).abs() < 1e-5, "{:?}", frame.normal);
            // Orthonormality.
            assert!(frame.tangent.dot(&frame.normal).abs() < 1e-5);
            assert!((frame.tangent.norm() - 1.0).abs() < 1e-5);
            assert!((frame.bitangent.norm() - 1.0).abs() < 1e-5);
        }
    }

    #[test]
    fn test_build_face_frames_matches_apply_binding_frames() {
        let mesh = unit_triangle_mesh();
        let face_frames = build_face_frames(&mesh);
        assert_eq!(face_frames.len(), mesh.faces.len());

        let mut model = model_with_gaussians(1);
        model.face_indices = vec![0];
        model.barycentric = vec![[0.5, 0.25, 0.25]];
        model.local_offsets = vec![[0.1, 0.2, 0.3]];
        let result = apply_binding(&model, &mesh).expect("binding");

        // The frame the forward pass used must be the face-indexed table entry.
        let forward = result.face_frames[0];
        let table = face_frames[0];
        assert!((forward.tangent - table.tangent).norm() < 1e-6);
        assert!((forward.bitangent - table.bitangent).norm() < 1e-6);
        assert!((forward.normal - table.normal).norm() < 1e-6);
    }

    #[test]
    fn test_build_face_frames_bad_face_gets_fallback() {
        let mesh = Mesh {
            vertices: vec![
                na::Point3::new(0.0, 0.0, 0.0),
                na::Point3::new(1.0, 0.0, 0.0),
            ],
            normals: vec![na::Vector3::zeros(); 2],
            faces: vec![[0, 1, 5]],
            uv_coords: Vec::new(),
        };
        let frames = build_face_frames(&mesh);
        assert_eq!(frames.len(), 1);
        assert!((frames[0].normal.z - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_build_vertex_frames_independent_of_gaussian_count() {
        // 4 vertices, 2 faces: the frame array must stay vertex-sized even
        // though a bound model could have any number of Gaussians.
        let mesh = Mesh::new(
            vec![
                na::Point3::new(0.0, 0.0, 0.0),
                na::Point3::new(1.0, 0.0, 0.0),
                na::Point3::new(0.0, 1.0, 0.0),
                na::Point3::new(1.0, 1.0, 0.0),
            ],
            vec![[0, 1, 2], [1, 3, 2]],
        );
        assert_eq!(build_vertex_frames(&mesh).len(), 4);
    }

    #[test]
    fn test_build_vertex_frames_isolated_vertex_gets_fallback() {
        // Vertex 3 belongs to no face → degenerate accumulators.
        let mesh = Mesh::new(
            vec![
                na::Point3::new(0.0, 0.0, 0.0),
                na::Point3::new(1.0, 0.0, 0.0),
                na::Point3::new(0.0, 1.0, 0.0),
                na::Point3::new(5.0, 5.0, 5.0),
            ],
            vec![[0, 1, 2]],
        );
        let frames = build_vertex_frames(&mesh);
        assert_eq!(frames.len(), 4);
        let isolated = frames[3];
        assert!((isolated.tangent.norm() - 1.0).abs() < 1e-5);
        assert!((isolated.normal.norm() - 1.0).abs() < 1e-5);
        assert!(isolated.tangent.dot(&isolated.normal).abs() < 1e-5);
    }

    #[test]
    fn test_build_vertex_frames_skips_faces_with_bad_indices() {
        let mesh = Mesh {
            vertices: vec![
                na::Point3::new(0.0, 0.0, 0.0),
                na::Point3::new(1.0, 0.0, 0.0),
                na::Point3::new(0.0, 1.0, 0.0),
            ],
            normals: vec![na::Vector3::zeros(); 3],
            faces: vec![[0, 1, 2], [0, 1, 42]],
            uv_coords: Vec::new(),
        };
        let frames = build_vertex_frames(&mesh);
        assert_eq!(frames.len(), 3);
        assert!((frames[0].normal.z - 1.0).abs() < 1e-5);
    }

    // --- Backward-pass math ---

    #[test]
    fn test_offset_gradients_cpu_projects_onto_vertex_frame() {
        let mesh = unit_triangle_mesh();
        let frames = build_vertex_frames(&mesh);
        // Two Gaussians bound to vertex 2 and vertex 0 respectively.
        let vertex_ids = vec![2u32, 0u32];
        let grads = vec![[0.0, 0.0, 1.0], [0.0, 0.0, -3.0]];
        let out = offset_gradients_cpu(&grads, &vertex_ids, &frames).expect("projection");
        assert_eq!(out.len(), 2);
        // Gradient purely along +z projects entirely onto the normal component.
        assert!((out[0][2] - 1.0).abs() < 1e-5, "{:?}", out[0]);
        assert!(out[0][0].abs() < 1e-5 && out[0][1].abs() < 1e-5);
        assert!((out[1][2] + 3.0).abs() < 1e-5, "{:?}", out[1]);
    }

    #[test]
    fn test_offset_gradients_cpu_rejects_length_mismatch() {
        let frames = build_vertex_frames(&unit_triangle_mesh());
        let err = offset_gradients_cpu(&[[1.0, 0.0, 0.0]], &[], &frames)
            .expect_err("length mismatch must error");
        assert!(matches!(err, RenderError::MismatchedBufferSizes { .. }));
    }

    #[test]
    fn test_offset_gradients_cpu_rejects_out_of_range_vertex() {
        let frames = build_vertex_frames(&unit_triangle_mesh());
        let err = offset_gradients_cpu(&[[1.0, 0.0, 0.0]], &[99], &frames)
            .expect_err("out-of-range vertex id must error");
        assert!(matches!(err, RenderError::InvalidGaussian { index: 0, .. }));
    }

    /// Regression (F138): the offset gradient the backward pass produces must
    /// be the true adjoint of [`apply_binding`], i.e. `∂position/∂offset`
    /// evaluated in the **face** frame the forward pass used. Without it
    /// `∂L/∂offset` is identically zero and the offset parameter group never
    /// trains.
    ///
    /// Checked against central differences of `apply_binding` itself, so a
    /// change to either side that breaks the pairing fails here.
    #[test]
    fn test_offset_gradients_are_the_adjoint_of_apply_binding() {
        // A tilted, non-axis-aligned triangle so none of the three frame axes
        // is a coordinate axis and a wrong frame cannot pass by accident.
        let mesh = Mesh::new(
            vec![
                na::Point3::new(0.1, -0.2, 0.3),
                na::Point3::new(1.3, 0.4, -0.2),
                na::Point3::new(-0.4, 1.1, 0.7),
            ],
            vec![[0, 1, 2]],
        );
        let frames = build_face_frames(&mesh);

        let mut model = model_with_gaussians(1);
        model.face_indices = vec![0];
        model.barycentric = vec![[0.25, 0.35, 0.4]];
        model.local_offsets = vec![[0.13, -0.07, 0.21]];

        // An arbitrary, non-symmetric upstream gradient ∂L/∂position.
        let grad_pos = [0.7_f32, -1.3, 0.45];
        let analytic = offset_gradients_cpu(&[grad_pos], &model.face_indices, &frames)
            .expect("projection onto the bound face frame");

        // Central differences of L(offset) = dot(grad_pos, position(offset)),
        // whose exact gradient is the adjoint under test.
        let loss = |offset: [f32; 3]| -> f32 {
            let mut probe = model.clone();
            probe.local_offsets = vec![offset];
            let p = apply_binding(&probe, &mesh).expect("binding").positions[0];
            grad_pos[0] * p[0] + grad_pos[1] * p[1] + grad_pos[2] * p[2]
        };

        let h = 1e-3_f32;
        for axis in 0..3 {
            let mut plus = model.local_offsets[0];
            let mut minus = model.local_offsets[0];
            plus[axis] += h;
            minus[axis] -= h;
            let numeric = (loss(plus) - loss(minus)) / (2.0 * h);
            assert!(
                (analytic[0][axis] - numeric).abs() < 1e-3,
                "axis {axis}: analytic {} vs numeric {numeric}",
                analytic[0][axis]
            );
        }

        // ...and the whole gradient must be non-trivial, so the test cannot
        // pass by both sides being zero.
        assert!(
            analytic[0].iter().any(|v| v.abs() > 1e-3),
            "{:?}",
            analytic[0]
        );
    }

    /// The per-*vertex* table is an approximation; the per-*face* table is
    /// the exact adjoint. Confirm they really differ on a mesh where the
    /// vertex normals are averaged, so passing the wrong one is detectable.
    #[test]
    fn test_face_and_vertex_frame_tables_are_not_interchangeable() {
        // Two faces meeting at an angle → averaged vertex normals differ from
        // either face normal.
        let mesh = Mesh::new(
            vec![
                na::Point3::new(0.0, 0.0, 0.0),
                na::Point3::new(1.0, 0.0, 0.0),
                na::Point3::new(0.0, 1.0, 0.0),
                na::Point3::new(1.0, 1.0, 1.0),
            ],
            vec![[0, 1, 2], [1, 3, 2]],
        );
        let face_frames = build_face_frames(&mesh);
        let vertex_frames = build_vertex_frames(&mesh);
        assert_eq!(face_frames.len(), 2);
        assert_eq!(vertex_frames.len(), 4);

        let grads = vec![[0.0_f32, 0.0, 1.0]];
        let by_face = offset_gradients_cpu(&grads, &[1], &face_frames).expect("face projection");
        let by_vertex =
            offset_gradients_cpu(&grads, &[1], &vertex_frames).expect("vertex projection");
        let delta: f32 = (0..3)
            .map(|i| (by_face[0][i] - by_vertex[0][i]).abs())
            .sum();
        assert!(
            delta > 1e-4,
            "the two frame tables must not be silently interchangeable: {by_face:?} vs {by_vertex:?}"
        );
    }

    #[test]
    fn test_frame_from_degenerate_normal_falls_back() {
        let frame = FaceFrame::from_normal_and_tangent(na::Vector3::zeros(), na::Vector3::x());
        assert!((frame.normal.z - 1.0).abs() < 1e-6);
        assert!((frame.tangent.x - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_frame_from_parallel_tangent_hint_stays_orthonormal() {
        // Hint parallel to the normal is unusable; an arbitrary axis is chosen.
        let frame = FaceFrame::from_normal_and_tangent(
            na::Vector3::new(0.0, 0.0, 2.0),
            na::Vector3::new(0.0, 0.0, 5.0),
        );
        assert!((frame.normal.z - 1.0).abs() < 1e-6);
        assert!((frame.tangent.norm() - 1.0).abs() < 1e-6);
        assert!(frame.tangent.dot(&frame.normal).abs() < 1e-6);
        assert!((frame.bitangent.norm() - 1.0).abs() < 1e-6);
    }
}