scena 1.7.2

A Rust-native scene-graph renderer with typed scene state, glTF assets, and explicit prepare/render lifecycles.
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
use serde_json::Value;

use super::validate_target;
use crate::scene::recipe::SceneRecipeDiagnosticV1;
use crate::scene::recipe::validation::diagnostic;

pub(super) const GROUNDED_FIELDS: &[&str] = &["id", "target", "plane_y", "tolerance"];
pub(super) const HELPER_OCCLUDED_FIELDS: &[&str] =
    &["id", "helper", "occluder", "tolerance_pixels"];
pub(super) const OCCLUSION_FIELDS: &[&str] = &["id", "front", "back", "tolerance_pixels"];
const BACKEND_FIELDS: &[&str] = &["backend", "gpu_device"];
const CLIPPING_FIELDS: &[&str] = &[
    "active_clipping_planes",
    "section_box_active",
    "section_box_inverted",
];
pub(super) const STATE_FIELDS: &[&str] = &["id", "import", "active_material_variant"];
pub(super) const TRANSFORM_FIELDS: &[&str] = &[
    "id",
    "target",
    "translation",
    "scale",
    "rotation_degrees",
    "translation_tolerance",
    "scale_tolerance",
    "rotation_tolerance_degrees",
];
pub(super) const SEPARATION_FIELDS: &[&str] = &["id", "a", "b", "min_gap", "tolerance"];
const BACKEND_VALUES: &[&str] = &[
    "headless",
    "headless_gpu",
    "surface_descriptor",
    "native_surface",
    "web_gpu",
    "web_gl2",
];

pub(super) fn validate_grounded_expectation(
    path: &str,
    object: &serde_json::Map<String, Value>,
    diagnostics: &mut Vec<SceneRecipeDiagnosticV1>,
) {
    validate_target(
        &format!("{path}.target"),
        object.get("target"),
        false,
        diagnostics,
    );
    if let Some(value) = object.get("plane_y") {
        match value.as_f64() {
            Some(value) if value.is_finite() => {}
            _ => diagnostics.push(diagnostic(
                "invalid_expect",
                "error",
                format!("{path}.plane_y"),
                "grounded expectation plane_y must be finite",
                "use a finite floor height such as 0.0",
                None,
                false,
            )),
        }
    }
    if let Some(value) = object.get("tolerance") {
        match value.as_f64() {
            Some(value) if value.is_finite() && value >= 0.0 => {}
            _ => diagnostics.push(diagnostic(
                "invalid_expect",
                "error",
                format!("{path}.tolerance"),
                "grounded expectation tolerance must be finite and non-negative",
                "use a small world-space tolerance such as 0.01",
                None,
                false,
            )),
        }
    }
}

pub(super) fn validate_helper_occluded_expectation(
    path: &str,
    object: &serde_json::Map<String, Value>,
    diagnostics: &mut Vec<SceneRecipeDiagnosticV1>,
) {
    validate_target(
        &format!("{path}.helper"),
        object.get("helper"),
        false,
        diagnostics,
    );
    validate_target(
        &format!("{path}.occluder"),
        object.get("occluder"),
        false,
        diagnostics,
    );
    if let Some(value) = object.get("tolerance_pixels")
        && value
            .as_u64()
            .is_none_or(|value| value > u64::from(u32::MAX))
    {
        diagnostics.push(diagnostic(
            "invalid_expect",
            "error",
            format!("{path}.tolerance_pixels"),
            "helper occlusion tolerance_pixels must be a non-negative integer",
            "use 0 for a hard no-overdraw check or a small integer for antialiasing tolerance",
            None,
            false,
        ));
    }
}

pub(super) fn validate_occlusion_expectation(
    path: &str,
    object: &serde_json::Map<String, Value>,
    diagnostics: &mut Vec<SceneRecipeDiagnosticV1>,
) {
    validate_target(
        &format!("{path}.front"),
        object.get("front"),
        false,
        diagnostics,
    );
    validate_target(
        &format!("{path}.back"),
        object.get("back"),
        false,
        diagnostics,
    );
    if let Some(value) = object.get("tolerance_pixels")
        && value
            .as_u64()
            .is_none_or(|value| value > u64::from(u32::MAX))
    {
        diagnostics.push(diagnostic(
            "invalid_expect",
            "error",
            format!("{path}.tolerance_pixels"),
            "occlusion tolerance_pixels must be a non-negative integer",
            "use 0 for a hard depth-order check or a small integer for antialiasing tolerance",
            None,
            false,
        ));
    }
}

pub(super) fn validate_backend_expectation(
    value: Option<&Value>,
    diagnostics: &mut Vec<SceneRecipeDiagnosticV1>,
) {
    let Some(value) = value else {
        return;
    };
    let Some(object) = value.as_object() else {
        diagnostics.push(diagnostic(
            "invalid_expect",
            "error",
            "$.expect.expect_backend",
            "expect_backend must be an object",
            "emit expect_backend:{backend?,gpu_device?}",
            None,
            false,
        ));
        return;
    };
    for key in object.keys() {
        if !BACKEND_FIELDS.contains(&key.as_str()) {
            diagnostics.push(diagnostic(
                "unknown_field",
                "error",
                format!("$.expect.expect_backend.{key}"),
                format!("field '{key}' is not accepted here"),
                "remove the field or use backend/gpu_device",
                None,
                false,
            ));
        }
    }
    if let Some(backend) = object.get("backend") {
        match backend.as_str() {
            Some(value) if BACKEND_VALUES.contains(&value) => {}
            _ => diagnostics.push(diagnostic(
                "invalid_expect",
                "error",
                "$.expect.expect_backend.backend",
                "backend must be one of headless, headless_gpu, surface_descriptor, native_surface, web_gpu, or web_gl2",
                "set backend to the renderer backend the recipe must use",
                None,
                false,
            )),
        }
    }
    if let Some(gpu_device) = object.get("gpu_device")
        && !gpu_device.is_boolean()
    {
        diagnostics.push(diagnostic(
            "invalid_expect",
            "error",
            "$.expect.expect_backend.gpu_device",
            "gpu_device must be a boolean",
            "set gpu_device to true when GPU execution is required",
            None,
            false,
        ));
    }
}

pub(super) fn validate_clipping_expectation(
    value: Option<&Value>,
    diagnostics: &mut Vec<SceneRecipeDiagnosticV1>,
) {
    let Some(value) = value else {
        return;
    };
    let Some(object) = value.as_object() else {
        diagnostics.push(diagnostic(
            "invalid_expect",
            "error",
            "$.expect.expect_clipping",
            "expect_clipping must be an object",
            "emit expect_clipping:{active_clipping_planes?,section_box_active?,section_box_inverted?}",
            None,
            false,
        ));
        return;
    };
    for key in object.keys() {
        if !CLIPPING_FIELDS.contains(&key.as_str()) {
            diagnostics.push(diagnostic(
                "unknown_field",
                "error",
                format!("$.expect.expect_clipping.{key}"),
                format!("field '{key}' is not accepted here"),
                "remove the field or use active_clipping_planes/section_box_active/section_box_inverted",
                None,
                false,
            ));
        }
    }
    if let Some(value) = object.get("active_clipping_planes")
        && value
            .as_u64()
            .is_none_or(|value| value > u64::from(u32::MAX))
    {
        diagnostics.push(diagnostic(
            "invalid_expect",
            "error",
            "$.expect.expect_clipping.active_clipping_planes",
            "active_clipping_planes must be a non-negative integer",
            "set the exact active user clipping-plane count expected after recipe build",
            None,
            false,
        ));
    }
    for field in ["section_box_active", "section_box_inverted"] {
        if let Some(value) = object.get(field)
            && !value.is_boolean()
        {
            diagnostics.push(diagnostic(
                "invalid_expect",
                "error",
                format!("$.expect.expect_clipping.{field}"),
                format!("{field} must be a boolean"),
                "set true or false",
                None,
                false,
            ));
        }
    }
}

pub(super) fn validate_state_expectation(
    path: &str,
    object: &serde_json::Map<String, Value>,
    diagnostics: &mut Vec<SceneRecipeDiagnosticV1>,
) {
    match object.get("import").and_then(Value::as_str) {
        Some(value) if !value.trim().is_empty() => {}
        _ => diagnostics.push(diagnostic(
            "invalid_expect",
            "error",
            format!("{path}.import"),
            "state expectation import must be a non-empty string",
            "set import to an id from imports[]",
            None,
            false,
        )),
    }
    if let Some(value) = object.get("active_material_variant") {
        match value.as_str() {
            Some(value) if !value.trim().is_empty() => {}
            None if value.is_null() => {}
            _ => diagnostics.push(diagnostic(
                "invalid_expect",
                "error",
                format!("{path}.active_material_variant"),
                "active_material_variant must be a non-empty string or null",
                "omit or set null for the default variant, or set a declared material variant name",
                None,
                false,
            )),
        }
    }
}

pub(super) fn validate_transform_expectation(
    path: &str,
    object: &serde_json::Map<String, Value>,
    diagnostics: &mut Vec<SceneRecipeDiagnosticV1>,
) {
    validate_target(
        &format!("{path}.target"),
        object.get("target"),
        false,
        diagnostics,
    );
    let mut has_component = false;
    for field in ["translation", "scale", "rotation_degrees"] {
        if object.contains_key(field) {
            has_component = true;
        }
        validate_vec3_optional(&format!("{path}.{field}"), object.get(field), diagnostics);
    }
    if !has_component {
        diagnostics.push(diagnostic(
            "invalid_expect",
            "error",
            path,
            "transform expectation must declare at least one of translation, scale, or rotation_degrees",
            "add a world-space translation, scale, or intrinsic X/Y/Z rotation_degrees expectation",
            None,
            false,
        ));
    }
    for field in [
        "translation_tolerance",
        "scale_tolerance",
        "rotation_tolerance_degrees",
    ] {
        if let Some(value) = object.get(field) {
            match value.as_f64() {
                Some(value) if value.is_finite() && value >= 0.0 => {}
                _ => diagnostics.push(diagnostic(
                    "invalid_expect",
                    "error",
                    format!("{path}.{field}"),
                    format!("{field} must be finite and non-negative"),
                    "omit the tolerance or use a small non-negative numeric value",
                    None,
                    false,
                )),
            }
        }
    }
}

pub(super) fn validate_separation_expectation(
    path: &str,
    object: &serde_json::Map<String, Value>,
    diagnostics: &mut Vec<SceneRecipeDiagnosticV1>,
) {
    validate_target(&format!("{path}.a"), object.get("a"), false, diagnostics);
    validate_target(&format!("{path}.b"), object.get("b"), false, diagnostics);
    for field in ["min_gap", "tolerance"] {
        if let Some(value) = object.get(field) {
            match value.as_f64() {
                Some(value) if value.is_finite() && value >= 0.0 => {}
                _ => diagnostics.push(diagnostic(
                    "invalid_expect",
                    "error",
                    format!("{path}.{field}"),
                    format!("{field} must be finite and non-negative"),
                    "omit the field or use a non-negative world-space distance in meters",
                    None,
                    false,
                )),
            }
        }
    }
}

fn validate_vec3_optional(
    path: &str,
    value: Option<&Value>,
    diagnostics: &mut Vec<SceneRecipeDiagnosticV1>,
) {
    let Some(value) = value else {
        return;
    };
    let Some(values) = value.as_array() else {
        push_vec3_error(path, diagnostics);
        return;
    };
    if values.len() != 3
        || !values
            .iter()
            .all(|value| value.as_f64().is_some_and(f64::is_finite))
    {
        push_vec3_error(path, diagnostics);
    }
}

fn push_vec3_error(path: &str, diagnostics: &mut Vec<SceneRecipeDiagnosticV1>) {
    diagnostics.push(diagnostic(
        "invalid_expect",
        "error",
        path,
        "transform expectation vector must contain exactly three finite numbers",
        "emit [x,y,z] with finite numeric values",
        None,
        false,
    ));
}