aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-render/src/lib.rs
blob: 08a285deab76c434ae764f239de92bf0677cc8cf (plain) (blame)
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
#![forbid(unsafe_code)]
//! Camera mathematics and graphics pipeline state.

/// A 64-byte transform block returned by the original Terrain camera ABI.
///
/// The original uses two selector-dependent transform pointers.  Their exact
/// matrix convention is still under recovery, so words are deliberately kept
/// losslessly rather than treated as a renderer-ready matrix.  The three
/// translation words have been confirmed at indices 3, 7, and 11.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct RawCameraTransform {
    /// The exact 16 little-endian dwords returned by the legacy camera.
    pub words: [u32; 16],
}

impl RawCameraTransform {
    /// Indices of the confirmed X, Y, and Z translation floats.
    pub const TRANSLATION_WORD_INDICES: [usize; 3] = [3, 7, 11];

    /// Returns the confirmed legacy world position (X, Y, Z).
    #[must_use]
    pub fn translation(self) -> [f32; 3] {
        Self::TRANSLATION_WORD_INDICES.map(|index| f32::from_bits(self.words[index]))
    }

    /// Inverts a finite row-major affine transform without assigning it a
    /// camera-space meaning.
    ///
    /// The legacy SIMD dispatch multiplies these blocks as ordinary row-major
    /// matrices and the confirmed camera samples have translation in the last
    /// column. `Some` therefore means only that this block has a non-singular
    /// affine inverse. Callers must still establish whether it is a
    /// camera-to-world transform before using the result as a view matrix.
    #[must_use]
    pub fn try_inverse_affine_row_major(self) -> Option<[f32; 16]> {
        let matrix = self.words.map(f32::from_bits);
        if !matrix.iter().all(|value| value.is_finite())
            || matrix[12].abs() > f32::EPSILON
            || matrix[13].abs() > f32::EPSILON
            || matrix[14].abs() > f32::EPSILON
            || (matrix[15] - 1.0).abs() > f32::EPSILON
        {
            return None;
        }

        let [m00, m01, m02, _, m10, m11, m12, _, m20, m21, m22, _, _, _, _, _] = matrix;
        let cofactor00 = m11.mul_add(m22, -(m12 * m21));
        let cofactor01 = m02.mul_add(m21, -(m01 * m22));
        let cofactor02 = m01.mul_add(m12, -(m02 * m11));
        let determinant = m00.mul_add(cofactor00, m10.mul_add(cofactor01, m20 * cofactor02));
        if !determinant.is_finite() || determinant == 0.0 {
            return None;
        }

        let inverse_determinant = determinant.recip();
        let inverse = [
            cofactor00 * inverse_determinant,
            m02.mul_add(m21, -(m01 * m22)) * inverse_determinant,
            cofactor02 * inverse_determinant,
            0.0,
            m12.mul_add(m20, -(m10 * m22)) * inverse_determinant,
            m00.mul_add(m22, -(m02 * m20)) * inverse_determinant,
            m02.mul_add(m10, -(m00 * m12)) * inverse_determinant,
            0.0,
            m10.mul_add(m21, -(m11 * m20)) * inverse_determinant,
            m01.mul_add(m20, -(m00 * m21)) * inverse_determinant,
            m00.mul_add(m11, -(m01 * m10)) * inverse_determinant,
            0.0,
            0.0,
            0.0,
            0.0,
            1.0,
        ];
        let [translation_x, translation_y, translation_z] = self.translation();
        let translation = [
            -(inverse[0].mul_add(
                translation_x,
                inverse[1].mul_add(translation_y, inverse[2] * translation_z),
            )),
            -(inverse[4].mul_add(
                translation_x,
                inverse[5].mul_add(translation_y, inverse[6] * translation_z),
            )),
            -(inverse[8].mul_add(
                translation_x,
                inverse[9].mul_add(translation_y, inverse[10] * translation_z),
            )),
        ];

        Some([
            inverse[0],
            inverse[1],
            inverse[2],
            translation[0],
            inverse[4],
            inverse[5],
            inverse[6],
            translation[1],
            inverse[8],
            inverse[9],
            inverse[10],
            translation[2],
            0.0,
            0.0,
            0.0,
            1.0,
        ])
    }

    /// Reproduces Ngi32's `Direct3D7` view-matrix conversion for this transform.
    ///
    /// The legacy renderer copies selector-0 into its camera state, then maps
    /// its axes and translation in this exact order before calling
    /// `IDirect3DDevice7::SetTransform(D3DTRANSFORMSTATE_VIEW, ...)`. This is
    /// deliberately distinct from [`Self::try_inverse_affine_row_major`]: it
    /// includes the original renderer's coordinate-system conversion.
    ///
    /// The returned matrix is row-major D3D7 data, not yet a Vulkan view
    /// matrix. A later adapter must explicitly account for clip-space and
    /// shader-vector conventions.
    #[must_use]
    pub fn try_direct3d7_view_row_major(self) -> Option<[f32; 16]> {
        let matrix = self.words.map(f32::from_bits);
        if !matrix.iter().all(|value| value.is_finite())
            || matrix[12].abs() > f32::EPSILON
            || matrix[13].abs() > f32::EPSILON
            || matrix[14].abs() > f32::EPSILON
            || (matrix[15] - 1.0).abs() > f32::EPSILON
        {
            return None;
        }

        let [m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, _, _, _, _] = matrix;
        Some([
            -m01,
            m02,
            m00,
            0.0,
            -m11,
            m12,
            m10,
            0.0,
            -m21,
            m22,
            m20,
            0.0,
            m23.mul_add(m21, m13.mul_add(m11, m03 * m01)),
            -(m23.mul_add(m22, m13.mul_add(m12, m03 * m02))),
            -(m00.mul_add(m03, m23.mul_add(m20, m13 * m10))),
            1.0,
        ])
    }
}

/// Parameters consumed by Ngi32's `Direct3D7` projection-matrix builder.
///
/// These are a separate legacy-renderer boundary from
/// [`RawCameraProjection`]. The latter preserves Terrain's source ABI, while
/// this type records the already-resolved Ngi32 values submitted to `Direct3D7`.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct LegacyD3d7Projection {
    /// Viewport rectangle as `(left, top, right, bottom)`.
    pub viewport: [i32; 4],
    /// Positive camera near-plane distance.
    pub near_plane: f32,
    /// Camera far-plane distance, greater than [`Self::near_plane`].
    pub far_plane: f32,
    /// Full field-of-view angle in radians.
    pub field_of_view_radians: f32,
}

impl LegacyD3d7Projection {
    /// Reconstructs the exact row-major matrix passed to D3D7 projection state.
    ///
    /// Ngi32 uses the viewport's `width / height`, writes `cos(fov / 2)` to
    /// the diagonal and `sin(fov / 2)` to both the depth scale and
    /// homogeneous-W term. The ratio after D3D's perspective divide is
    /// therefore the expected cotangent scale. This remains legacy D3D7 data
    /// rather than a Vulkan projection.
    #[must_use]
    pub fn try_direct3d7_projection_row_major(self) -> Option<[f32; 16]> {
        let width = self.viewport[2].checked_sub(self.viewport[0])?;
        let height = self.viewport[3].checked_sub(self.viewport[1])?;
        if width <= 0
            || height <= 0
            || !self.near_plane.is_finite()
            || !self.far_plane.is_finite()
            || !self.field_of_view_radians.is_finite()
            || self.near_plane <= 0.0
            || self.far_plane <= self.near_plane
            || self.field_of_view_radians <= 0.0
            || self.field_of_view_radians >= std::f32::consts::PI
        {
            return None;
        }

        // Ngi32 converts these signed viewport dimensions into single-precision
        // arithmetic before building the legacy matrix.

        let aspect = (width as f32) / (height as f32);
        let half_fov = self.field_of_view_radians * 0.5;
        let cosine = half_fov.cos();
        let sine = half_fov.sin();
        let depth_scale = sine / (1.0 - self.near_plane / self.far_plane);
        [aspect, cosine, sine, depth_scale]
            .iter()
            .all(|value| value.is_finite())
            .then_some([
                cosine,
                0.0,
                0.0,
                0.0,
                0.0,
                aspect * cosine,
                0.0,
                0.0,
                0.0,
                0.0,
                depth_scale,
                sine,
                0.0,
                0.0,
                -(depth_scale * self.near_plane),
                0.0,
            ])
    }
}

/// Affine placement inputs consumed by `Iron3D`'s recovered Euler-matrix builder.
///
/// This records the exact matrix construction at `iron3d.dll` RVA `0x36610`.
/// It is intentionally distinct from a mission placement contract: the static
/// trace has not yet proved that TMA's three raw orientation floats reach this
/// builder unchanged for every object category.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct LegacyIron3dEulerTransform {
    /// World-space translation passed to the builder.
    pub translation: [f32; 3],
    /// Three angles in the builder's `(x, y, z)` input order, in radians.
    pub orientation_radians: [f32; 3],
}

impl LegacyIron3dEulerTransform {
    /// Reconstructs the builder's finite row-major affine matrix.
    ///
    /// The recovered x87 code evaluates `Rz(z) * Ry(y) * Rx(x)` and writes
    /// translation into the last column. This uses portable `f32` trigonometry;
    /// any future x87-compatibility path must be separately capture-validated.
    #[must_use]
    pub fn try_row_major(self) -> Option<[f32; 16]> {
        if !self
            .translation
            .iter()
            .chain(self.orientation_radians.iter())
            .all(|value| value.is_finite())
        {
            return None;
        }
        let [x, y, z] = self.orientation_radians;
        let (sin_x, cos_x) = x.sin_cos();
        let (sin_y, cos_y) = y.sin_cos();
        let (sin_z, cos_z) = z.sin_cos();
        let [translation_x, translation_y, translation_z] = self.translation;
        let matrix = [
            cos_z * cos_y,
            cos_z * sin_y * sin_x - sin_z * cos_x,
            cos_z * sin_y * cos_x + sin_z * sin_x,
            translation_x,
            sin_z * cos_y,
            sin_z * sin_y * sin_x + cos_z * cos_x,
            sin_z * sin_y * cos_x - cos_z * sin_x,
            translation_y,
            -sin_y,
            cos_y * sin_x,
            cos_y * cos_x,
            translation_z,
            0.0,
            0.0,
            0.0,
            1.0,
        ];
        matrix
            .iter()
            .all(|value| value.is_finite())
            .then_some(matrix)
    }

    /// Applies the recovered rotation after a component-wise local scale.
    ///
    /// This is the affine `R * (scale * point) + translation` convention used
    /// by the static source-world bridge. The original dynamic transform path
    /// needs its own capture evidence before it may replace this static path.
    #[must_use]
    pub fn try_transform_scaled_point(self, point: [f32; 3], scale: [f32; 3]) -> Option<[f32; 3]> {
        if !point
            .iter()
            .chain(scale.iter())
            .all(|value| value.is_finite())
        {
            return None;
        }
        let matrix = self.try_row_major()?;
        let scaled = [
            point[0] * scale[0],
            point[1] * scale[1],
            point[2] * scale[2],
        ];
        let transformed = [
            matrix[0] * scaled[0] + matrix[1] * scaled[1] + matrix[2] * scaled[2] + matrix[3],
            matrix[4] * scaled[0] + matrix[5] * scaled[1] + matrix[6] * scaled[2] + matrix[7],
            matrix[8] * scaled[0] + matrix[9] * scaled[1] + matrix[10] * scaled[2] + matrix[11],
        ];
        transformed
            .iter()
            .all(|value| value.is_finite())
            .then_some(transformed)
    }
}

/// Fixed-function blend behaviour represented without a graphics API type.
///
/// This is the compatibility contract for the native MAT0 pipeline category.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum LegacyBlendMode {
    /// Do not blend the fragment with the existing colour.
    #[default]
    Opaque,
    /// Blend using source alpha.
    SourceAlpha,
    /// Add the source-alpha-scaled fragment to the existing colour.
    Additive,
    /// Replace the source factor with zero and use source colour as the
    /// destination factor.
    ZeroSourceColor,
    /// Multiply the source by destination colour and destination by source
    /// colour, matching the native two-colour blend mode.
    DestColorSourceColor,
}

/// Depth-buffer behaviour represented without a graphics API type.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum LegacyDepthMode {
    /// No depth attachment is used.
    #[default]
    Disabled,
    /// Test depth and write passing fragments.
    TestWrite,
    /// Test depth without modifying it.
    TestReadOnly,
}

/// Triangle culling behaviour represented without a graphics API type.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum LegacyCullMode {
    /// Keep both front- and back-facing triangles.
    #[default]
    Disabled,
    /// Cull back-facing triangles.
    BackFace,
    /// Cull front-facing triangles.
    FrontFace,
}

/// Legacy fixed-function state that changes graphics-pipeline structure.
///
/// Alpha reference is deliberately not present: it is dynamic material data,
/// whereas this state records only whether an alpha-test shader variant is used.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct LegacyPipelineState {
    /// Colour blend mode.
    pub blend: LegacyBlendMode,
    /// Depth test/write mode.
    pub depth: LegacyDepthMode,
    /// Face culling mode.
    pub cull: LegacyCullMode,
    /// Whether alpha-test shader logic is enabled.
    pub alpha_test: bool,
}

impl LegacyPipelineState {
    /// Applies the native MAT0 pipeline category while retaining the caller's
    /// depth and cull state.
    ///
    /// Categories 0..4 are the complete table recovered from the native
    /// material path. The native table enables `GREATER_EQUAL` alpha testing
    /// for categories 1..4; the alpha reference remains per draw range.
    #[must_use]
    pub const fn with_material_category(self, category: u8) -> Option<Self> {
        let (blend, alpha_test) = match category {
            0 => (LegacyBlendMode::Opaque, false),
            1 => (LegacyBlendMode::SourceAlpha, true),
            2 => (LegacyBlendMode::Additive, true),
            3 => (LegacyBlendMode::ZeroSourceColor, true),
            4 => (LegacyBlendMode::DestColorSourceColor, true),
            _ => return None,
        };
        Some(Self {
            blend,
            alpha_test,
            ..self
        })
    }
}

/// Canonical, backend-neutral key for a graphics-pipeline variant.
///
/// The value is explicitly packed rather than hashed, so captures and caches
/// remain stable across processes and Rust toolchain updates. Three bits are
/// reserved for the five native material blend modes.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct PipelineKey(u8);

impl PipelineKey {
    /// Returns the canonical packed representation.
    #[must_use]
    pub const fn packed(self) -> u8 {
        self.0
    }
}

impl From<LegacyPipelineState> for PipelineKey {
    fn from(state: LegacyPipelineState) -> Self {
        let blend = match state.blend {
            LegacyBlendMode::Opaque => 0,
            LegacyBlendMode::SourceAlpha => 1,
            LegacyBlendMode::Additive => 2,
            LegacyBlendMode::ZeroSourceColor => 3,
            LegacyBlendMode::DestColorSourceColor => 4,
        };
        let depth = match state.depth {
            LegacyDepthMode::Disabled => 0,
            LegacyDepthMode::TestWrite => 1,
            LegacyDepthMode::TestReadOnly => 2,
        };
        let cull = match state.cull {
            LegacyCullMode::Disabled => 0,
            LegacyCullMode::BackFace => 1,
            LegacyCullMode::FrontFace => 2,
        };
        Self(blend | (depth << 3) | (cull << 5) | (u8::from(state.alpha_test) << 7))
    }
}

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

    fn multiply_row_major(left: [f32; 16], right: [f32; 16]) -> [f32; 16] {
        let mut result = [0.0; 16];
        for row in 0..4 {
            for column in 0..4 {
                result[row * 4 + column] = (0..4)
                    .map(|index| left[row * 4 + index] * right[index * 4 + column])
                    .sum();
            }
        }
        result
    }

    fn assert_matrix_approximately_identity(matrix: [f32; 16]) {
        for (index, value) in matrix.into_iter().enumerate() {
            let expected = if index / 4 == index % 4 { 1.0 } else { 0.0 };
            assert!(
                (value - expected).abs() < 0.000_02,
                "matrix element {index}: expected {expected}, got {value}"
            );
        }
    }

    #[test]
    fn raw_camera_transform_inverts_only_non_singular_affine_blocks() {
        let source = [
            0.0, -1.0, 0.0, 433.544_7, 0.948_985, 0.0, 0.315_322, 652.292_5, -0.315_322, 0.0,
            0.948_985, 10.673_42, 0.0, 0.0, 0.0, 1.0,
        ];
        let transform = RawCameraTransform {
            words: source.map(f32::to_bits),
        };
        let inverse = transform
            .try_inverse_affine_row_major()
            .expect("observed affine transform is invertible");

        assert_matrix_approximately_identity(multiply_row_major(source, inverse));
        assert_matrix_approximately_identity(multiply_row_major(inverse, source));

        let singular = RawCameraTransform { words: [0_u32; 16] };
        assert_eq!(singular.try_inverse_affine_row_major(), None);
    }

    #[test]
    fn raw_camera_transform_reproduces_direct3d7_view_axis_conversion() {
        let transform = RawCameraTransform {
            words: [
                0.0_f32.to_bits(),
                (-1.0_f32).to_bits(),
                0.0_f32.to_bits(),
                10.0_f32.to_bits(),
                1.0_f32.to_bits(),
                0.0_f32.to_bits(),
                0.0_f32.to_bits(),
                20.0_f32.to_bits(),
                0.0_f32.to_bits(),
                0.0_f32.to_bits(),
                1.0_f32.to_bits(),
                30.0_f32.to_bits(),
                0.0_f32.to_bits(),
                0.0_f32.to_bits(),
                0.0_f32.to_bits(),
                1.0_f32.to_bits(),
            ],
        };

        assert_eq!(
            transform.try_direct3d7_view_row_major(),
            Some([
                1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, -10.0, -30.0, -20.0,
                1.0,
            ])
        );
        assert_eq!(
            RawCameraTransform { words: [0_u32; 16] }.try_direct3d7_view_row_major(),
            None
        );
    }

    #[test]
    fn legacy_d3d7_projection_matches_recovered_camera_formula() {
        let projection = LegacyD3d7Projection {
            viewport: [0, 0, 1024, 768],
            near_plane: 0.5,
            far_plane: 700.0,
            field_of_view_radians: 1.3,
        };
        let matrix = projection
            .try_direct3d7_projection_row_major()
            .expect("live Ngi32 projection parameters are valid");
        let half_fov = 0.65_f32;
        let depth_scale = half_fov.sin() * 700.0_f32 / 699.5;

        assert_eq!(matrix[0], half_fov.cos());
        assert_eq!(matrix[5], (4.0 / 3.0) * half_fov.cos());
        assert_eq!(matrix[10], depth_scale);
        assert_eq!(matrix[11], half_fov.sin());
        assert_eq!(matrix[14], -(depth_scale * 0.5));
        assert_eq!(
            LegacyD3d7Projection {
                viewport: [0, 0, 0, 768],
                ..projection
            }
            .try_direct3d7_projection_row_major(),
            None
        );
    }

    #[test]
    fn iron3d_euler_builder_uses_rz_ry_rx_and_last_column_translation() {
        let matrix = LegacyIron3dEulerTransform {
            translation: [418.103_18, 717.433, 3.040_938_9],
            orientation_radians: [0.0, 0.0, std::f32::consts::FRAC_PI_2],
        }
        .try_row_major()
        .expect("finite recovered builder inputs");

        assert!((matrix[0]).abs() < 0.000_001);
        assert!((matrix[1] + 1.0).abs() < 0.000_001);
        assert!((matrix[4] - 1.0).abs() < 0.000_001);
        assert!((matrix[5]).abs() < 0.000_001);
        assert_eq!(matrix[3], 418.103_18);
        assert_eq!(matrix[7], 717.433);
        assert_eq!(matrix[11], 3.040_938_9);
        assert_eq!(matrix[15], 1.0);
        assert_eq!(
            LegacyIron3dEulerTransform {
                translation: [10.0, 20.0, 30.0],
                orientation_radians: [0.0, 0.0, std::f32::consts::FRAC_PI_2],
            }
            .try_transform_scaled_point([2.0, 3.0, 4.0], [2.0, 1.0, 0.5]),
            Some([7.0, 24.0, 32.0])
        );
        assert_eq!(
            LegacyIron3dEulerTransform {
                translation: [0.0, 0.0, 0.0],
                orientation_radians: [f32::NAN, 0.0, 0.0],
            }
            .try_row_major(),
            None
        );
    }

    #[test]
    fn native_material_categories_preserve_depth_and_cull_state() {
        let base = LegacyPipelineState {
            depth: LegacyDepthMode::TestWrite,
            cull: LegacyCullMode::BackFace,
            ..LegacyPipelineState::default()
        };
        let expected = [
            (LegacyBlendMode::Opaque, false),
            (LegacyBlendMode::SourceAlpha, true),
            (LegacyBlendMode::Additive, true),
            (LegacyBlendMode::ZeroSourceColor, true),
            (LegacyBlendMode::DestColorSourceColor, true),
        ];
        for (category, (blend, alpha_test)) in expected.into_iter().enumerate() {
            let state = base
                .with_material_category(category as u8)
                .expect("native category is represented");
            assert_eq!(state.blend, blend);
            assert_eq!(state.alpha_test, alpha_test);
            assert_eq!(state.depth, base.depth);
            assert_eq!(state.cull, base.cull);
        }
        assert_eq!(base.with_material_category(5), None);
    }

    #[test]
    fn native_material_categories_have_distinct_pipeline_keys() {
        let base = LegacyPipelineState {
            depth: LegacyDepthMode::TestWrite,
            ..LegacyPipelineState::default()
        };
        let keys: Vec<_> = (0..=4)
            .map(|category| {
                PipelineKey::from(
                    base.with_material_category(category)
                        .expect("native category is represented"),
                )
            })
            .collect();
        for (index, key) in keys.iter().enumerate() {
            assert!(keys[index + 1..].iter().all(|other| other != key));
        }
    }
}