aboutsummaryrefslogtreecommitdiff
path: root/apps/fparkan-game/src/camera_capture.rs
blob: fb193f8f5e1f6fe83fdb6665945848017b50081f (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
use super::{CapturedViewport, PreviewCameraFrame};
use fparkan_fx::atmosphere::{AtmosphereFrame, SunBirth};
use fparkan_render::{LegacyD3d7Projection, RawCameraTransform};
use serde::Deserialize;

#[derive(Clone, Debug, Deserialize, PartialEq)]
pub(super) struct LegacyCameraCapture {
    pub(super) schema: String,
    pub(super) render_input_usable: Option<bool>,
    pub(super) selector0_words: [u32; 16],
    pub(super) parented_camera: Option<bool>,
    pub(super) effective_world_matrix_space: Option<String>,
    pub(super) effective_world_matrix_words: Option<[u32; 16]>,
    pub(super) viewport: [i32; 4],
    pub(super) near_plane: f32,
    pub(super) far_plane: f32,
    pub(super) field_of_view_radians: f32,
    pub(super) atmosphere_seconds: Option<f32>,
    pub(super) sky_uv_clock: Option<SkyUvClock>,
    pub(super) geometry_projection: Option<GeometryProjection>,
}

#[derive(Clone, Debug, Deserialize, PartialEq)]
pub(super) struct SkyUvClock {
    pub(super) verified: bool,
    pub(super) elapsed_milliseconds: u32,
    pub(super) raw_clock: Option<u32>,
    pub(super) celestial_births: Option<CelestialBirthClocks>,
}

#[derive(Clone, Debug, Deserialize, PartialEq)]
pub(super) struct CelestialBirthClocks {
    sun: Option<CelestialClock>,
    moon: Option<CelestialClock>,
}

#[derive(Clone, Debug, Deserialize, PartialEq)]
struct CelestialClock {
    elapsed_milliseconds: u32,
    lifetime_milliseconds: u32,
}

impl CelestialBirthClocks {
    pub(super) fn apply(&self, atmosphere: &mut AtmosphereFrame) {
        for (sample, clock, angles, row) in [
            (&mut atmosphere.sun, &self.sun, [90.0, 30.0], 3),
            (&mut atmosphere.moon, &self.moon, [0.0, 50.0], 4),
        ] {
            sample.active = clock.is_some();
            sample.birth = clock.as_ref().map(|clock| SunBirth {
                start_seconds: atmosphere.real_seconds - clock.elapsed_milliseconds as f32 / 1000.0,
                lifetime_seconds: clock.lifetime_milliseconds as f32 / 1000.0,
                initial_angles_degrees: angles,
                material_row: row,
            });
        }
    }
}

#[derive(Clone, Debug, Deserialize, PartialEq)]
pub(super) struct GeometryProjection {
    verified: bool,
    viewport: [i32; 4],
    near_plane: f32,
    far_plane: f32,
    field_of_view_radians: f32,
}

pub(super) fn selected_atmosphere_seconds(
    command_line: Option<f32>,
    capture: Option<&LegacyCameraCapture>,
) -> Option<f32> {
    command_line.or_else(|| capture.and_then(|capture| capture.atmosphere_seconds))
}

impl LegacyCameraCapture {
    pub(super) fn preview_matrix_words(&self) -> Result<[u32; 16], String> {
        match (
            self.effective_world_matrix_space.as_deref(),
            self.effective_world_matrix_words,
        ) {
            (Some("world"), Some(words)) => Ok(words),
            (Some(_), _) => Err(
                "legacy camera effective_world_matrix_space must be 'world'".to_string(),
            ),
            (None, Some(_)) => Err(
                "legacy camera effective_world_matrix_words requires an explicit world-space marker"
                    .to_string(),
            ),
            (None, None) if self.parented_camera == Some(true) => Err(
                "parented legacy camera requires an effective world-space matrix".to_string(),
            ),
            (None, None) => Ok(self.selector0_words),
        }
    }

    pub(super) fn captured_viewport(&self) -> Result<CapturedViewport, String> {
        let width = self.viewport[2]
            .checked_sub(self.viewport[0])
            .filter(|width| *width > 0)
            .ok_or_else(|| "legacy camera capture has an invalid viewport width".to_string())?;
        let height = self.viewport[3]
            .checked_sub(self.viewport[1])
            .filter(|height| *height > 0)
            .ok_or_else(|| "legacy camera capture has an invalid viewport height".to_string())?;
        Ok(CapturedViewport {
            rect: self.viewport,
            extent: [
                u32::try_from(width)
                    .map_err(|_| "legacy camera viewport width exceeds u32".to_string())?,
                u32::try_from(height)
                    .map_err(|_| "legacy camera viewport height exceeds u32".to_string())?,
            ],
        })
    }

    pub(super) fn preview_camera_frame(&self) -> Result<PreviewCameraFrame, String> {
        let projection = if let Some(geometry) = &self.geometry_projection {
            if !geometry.verified || geometry.viewport != self.viewport {
                return Err(
                    "legacy camera geometry projection is unverified or has a different viewport"
                        .to_string(),
                );
            }
            LegacyD3d7Projection {
                viewport: geometry.viewport,
                near_plane: geometry.near_plane,
                far_plane: geometry.far_plane,
                field_of_view_radians: geometry.field_of_view_radians,
            }
        } else {
            LegacyD3d7Projection {
                viewport: self.viewport,
                near_plane: self.near_plane,
                far_plane: self.far_plane,
                field_of_view_radians: self.field_of_view_radians,
            }
        };
        let frame = PreviewCameraFrame::from_legacy_d3d7(
            RawCameraTransform {
                words: self.preview_matrix_words()?,
            },
            projection,
        )
        .ok_or_else(|| "legacy camera capture contains an invalid D3D7 camera".to_string())?;
        self.captured_viewport()?;
        Ok(frame)
    }
}

pub(super) fn load_legacy_camera_capture(
    path: &std::path::Path,
) -> Result<LegacyCameraCapture, String> {
    let bytes = std::fs::read(path).map_err(|err| format!("{}: {err}", path.display()))?;
    let capture =
        parse_legacy_camera_capture(&bytes).map_err(|err| format!("{}: {err}", path.display()))?;
    if capture.geometry_projection.is_none() {
        eprintln!("render_coverage captured_projection_unverified=1 (capture has no geometry_projection; using the recorded camera boundary, which can clip nearby surfaces)");
    }
    if capture.sky_uv_clock.is_none() {
        eprintln!("render_coverage captured_sky_phase_unverified=1 (capture has no sky UV clock; using replay elapsed time)");
    }
    if capture
        .sky_uv_clock
        .as_ref()
        .and_then(|c| c.celestial_births.as_ref())
        .is_none()
    {
        eprintln!("render_coverage captured_celestial_clock_unverified=1 (capture has no celestial birth clocks; using nominal schedule births)");
    }
    Ok(capture)
}

pub(super) fn parse_legacy_camera_capture(bytes: &[u8]) -> Result<LegacyCameraCapture, String> {
    // Windows PowerShell 5.1 writes redirected text as UTF-16LE with a BOM.
    // `capture-original-camera.ps1` intentionally emits plain JSON, so accept
    // that normal hand-off format as well as UTF-8 without making the caller
    // re-encode a read-only capture file.
    let json = decode_legacy_camera_capture_json(bytes)?;
    let capture: LegacyCameraCapture =
        serde_json::from_str(&json).map_err(|err| format!("invalid legacy camera JSON: {err}"))?;
    if capture.schema != "fparkan-legacy-camera-v1" {
        return Err("unsupported legacy camera capture schema".to_string());
    }
    if capture.effective_world_matrix_space.is_some()
        != capture.effective_world_matrix_words.is_some()
    {
        return Err(
            "effective_world_matrix_space and effective_world_matrix_words must be supplied together"
                .to_string(),
        );
    }
    if capture.render_input_usable == Some(false) {
        return Err("legacy camera capture is marked render_input_usable=false".to_string());
    }
    if capture
        .sky_uv_clock
        .as_ref()
        .is_some_and(|clock| !clock.verified)
    {
        return Err("legacy camera sky UV clock is unverified".to_string());
    }
    if let Some(births) = capture
        .sky_uv_clock
        .as_ref()
        .and_then(|c| c.celestial_births.as_ref())
    {
        if capture.atmosphere_seconds.is_none() {
            return Err("legacy camera celestial clocks require an atmosphere phase".to_string());
        }
        for clock in [&births.sun, &births.moon].into_iter().flatten() {
            if clock.lifetime_milliseconds == 0 || clock.lifetime_milliseconds > i32::MAX as u32 {
                return Err(
                    "legacy camera celestial clock has an invalid lifetime or age".to_string(),
                );
            }
        }
    }
    if capture
        .atmosphere_seconds
        .is_some_and(|seconds| !seconds.is_finite() || seconds < 0.0)
    {
        return Err(
            "legacy camera capture atmosphere_seconds must be finite and non-negative".to_string(),
        );
    }
    capture.preview_camera_frame()?;
    Ok(capture)
}

fn decode_legacy_camera_capture_json(bytes: &[u8]) -> Result<String, String> {
    let decode_utf16 = |words: Vec<u16>| {
        String::from_utf16(&words)
            .map_err(|err| format!("invalid UTF-16 legacy camera JSON: {err}"))
    };
    match bytes {
        [0xff, 0xfe, rest @ ..] => {
            let chunks = rest.chunks_exact(2);
            if !chunks.remainder().is_empty() {
                return Err("invalid UTF-16LE legacy camera JSON length".to_string());
            }
            decode_utf16(
                chunks
                    .map(|chunk| u16::from_le_bytes([chunk[0], chunk[1]]))
                    .collect(),
            )
        }
        [0xfe, 0xff, rest @ ..] => {
            let chunks = rest.chunks_exact(2);
            if !chunks.remainder().is_empty() {
                return Err("invalid UTF-16BE legacy camera JSON length".to_string());
            }
            decode_utf16(
                chunks
                    .map(|chunk| u16::from_be_bytes([chunk[0], chunk[1]]))
                    .collect(),
            )
        }
        [0xef, 0xbb, 0xbf, rest @ ..] => std::str::from_utf8(rest)
            .map(str::to_owned)
            .map_err(|err| format!("invalid UTF-8 legacy camera JSON: {err}")),
        _ => std::str::from_utf8(bytes)
            .map(str::to_owned)
            .map_err(|err| format!("invalid UTF-8 legacy camera JSON: {err}")),
    }
}

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

    fn json() -> serde_json::Value {
        let words = [
            1.0_f32, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
        ]
        .map(f32::to_bits);
        serde_json::json!({
            "schema": "fparkan-legacy-camera-v1", "selector0_words": words,
            "viewport": [0, 0, 1280, 1024], "near_plane": 5.0, "far_plane": 700.0,
            "field_of_view_radians": 1.3,
            "geometry_projection": {
                "verified": true, "viewport": [0, 0, 1280, 1024],
                "near_plane": 0.5, "far_plane": 700.0, "field_of_view_radians": 1.3,
            }
        })
    }

    #[test]
    fn celestial_capture_rejects_invalid_lifetime() {
        for lifetime in [0, i32::MAX as u32 + 1] {
            let mut json = json();
            json["atmosphere_seconds"] = serde_json::json!(395.471);
            json["sky_uv_clock"] = serde_json::json!({
                "verified": true, "elapsed_milliseconds": 123,
                "celestial_births": {"sun": {"elapsed_milliseconds": 1000,
                    "lifetime_milliseconds": lifetime}, "moon": null}
            });
            assert!(parse_legacy_camera_capture(&serde_json::to_vec(&json).unwrap()).is_err());
        }
    }

    #[test]
    fn celestial_capture_preserves_executed_sun_age_and_inactive_moon() {
        use fparkan_fx::{
            atmosphere::TypedAtmosphere,
            sky::{SkyFrame, SkyMaterials},
        };
        let mut json = json();
        json["atmosphere_seconds"] = serde_json::json!(395.471);
        json["sky_uv_clock"] = serde_json::json!({
            "verified": true, "elapsed_milliseconds": 123,
            "celestial_births": {"sun": {"elapsed_milliseconds": 378466,
                "lifetime_milliseconds": 525000}, "moon": null}
        });
        let capture = parse_legacy_camera_capture(&serde_json::to_vec(&json).unwrap()).unwrap();
        let mut missing_phase = json.clone();
        missing_phase
            .as_object_mut()
            .unwrap()
            .remove("atmosphere_seconds");
        assert!(parse_legacy_camera_capture(&serde_json::to_vec(&missing_phase).unwrap()).is_err());
        let births = capture.sky_uv_clock.unwrap().celestial_births.unwrap();
        let mut atmosphere = TypedAtmosphere {
            marker: 0,
            version: 0,
            tracks: Vec::new(),
            trailer: Default::default(),
        }
        .sample(395.471);
        births.apply(&mut atmosphere);
        let materials = SkyMaterials::default();
        let sky = SkyFrame::from_atmosphere(&atmosphere, &materials, atmosphere.real_seconds);
        // Executed GOG CSun state; its actual birth was at phase 17.005, not nominal 18.
        for (actual, native) in
            sky.sun
                .direction
                .into_iter()
                .zip([-0.33643198, -0.7397671, -0.5827162])
        {
            assert!((actual - native).abs() < 3e-6);
        }
        assert!(sky.sun.active);
        assert!(!sky.moon.active);
        assert_eq!(atmosphere.sun.birth.unwrap().lifetime_seconds, 525.0);
        assert!(atmosphere.moon.birth.is_none());
    }

    #[test]
    fn geometry_projection_preserves_surfaces_closer_than_restored_camera_near() {
        let mut json = json();
        let captured = parse_legacy_camera_capture(&serde_json::to_vec(&json).unwrap()).unwrap();
        let camera = captured.preview_camera_frame().unwrap();
        let point = [1.0, 0.0, 0.0, 1.0];
        let depth = |matrix: [f32; 16]| {
            (0..4)
                .map(|row| point[row] * matrix[row * 4 + 2])
                .sum::<f32>()
        };
        assert_eq!(camera.near_plane, 0.5);
        assert!(depth(camera.clip_from_world) > 0.0);
        json.as_object_mut().unwrap().remove("geometry_projection");
        let old = parse_legacy_camera_capture(&serde_json::to_vec(&json).unwrap()).unwrap();
        let old_camera = old.preview_camera_frame().unwrap();
        assert_eq!(old_camera.near_plane, 5.0);
        assert!(depth(old_camera.clip_from_world) < 0.0);
    }

    #[test]
    fn geometry_projection_survives_later_sky_depth_range() {
        let mut json = json();
        let world = parse_legacy_camera_capture(&serde_json::to_vec(&json).unwrap())
            .unwrap()
            .preview_camera_frame()
            .unwrap();
        json["near_plane"] = serde_json::json!(700.0);
        json["far_plane"] = serde_json::json!(50000.0);
        let after_sky = parse_legacy_camera_capture(&serde_json::to_vec(&json).unwrap())
            .unwrap()
            .preview_camera_frame()
            .unwrap();
        assert_eq!(after_sky.clip_from_world, world.clip_from_world);
        assert_eq!(after_sky.near_plane, 0.5);
    }

    #[test]
    fn geometry_projection_rejects_unverified_mismatched_or_invalid_input() {
        for (field, value) in [
            ("verified", serde_json::json!(false)),
            ("viewport", serde_json::json!([0, 0, 640, 480])),
            ("near_plane", serde_json::json!(0.0)),
            ("far_plane", serde_json::json!(0.25)),
        ] {
            let mut json = json();
            json["geometry_projection"][field] = value;
            assert!(
                parse_legacy_camera_capture(&serde_json::to_vec(&json).unwrap()).is_err(),
                "{field}"
            );
        }
    }
}