concinnity-dev 0.18.69

The Concinnity dev tooling library: world authoring, the in-engine editor, the debug server, docs and packaging
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
// src/debug/hot_reload/passes.rs
//
// The world.jsonl / ProceduralMesh / VolumetricFog / world-loaded Shader stage
// reload passes: re-read the on-disk source, diff against the captured state,
// and apply changes through the backend. Each returns a small tally the drive
// logs.

use crate::gfx::graphics_system::hot_reload_sources::*;

// Per-reload tally for the volumetric-fog path. Counts are surfaced separately
// so a single info! line per asset class keeps the log readable.
#[derive(Default, Debug)]
pub(crate) struct FogReloadResult {
    // True when the resolved `Option<FogSettings>` differed from what was
    // last pushed; the trait call fired and the dedupe state advanced.
    pub updated: bool,
}

// Re-read `world.jsonl` and push the first declared `VolumetricFog` through
// [`RenderBackend::update_fog_settings`]. Disabled / missing assets push
// `None`. Each authored asset runs through the same clamp chain as init:
// [`crate::components::VolumetricFog::from_args`] (asset-side floors) then
// [`crate::gfx::volumetric_fog::FogSettings::resolve`] (gfx-side ceilings),
// so a reload cannot land out-of-range values.
//
// `last_pushed` is the dedupe state owned by the caller; the function
// compares the freshly-resolved value against it and only fires the trait
// call (and reports `updated = true`) on a real change. Backend-agnostic:
// rides on the `update_fog_settings` trait, which has a default no-op on
// backends that have not yet implemented runtime fog mutation.
pub(super) fn reload_volumetric_fog(
    path: &str,
    last_pushed: &mut Option<crate::gfx::volumetric_fog::FogSettings>,
    backend: &mut dyn crate::gfx::backend::RenderBackend,
) -> FogReloadResult {
    let mut result = FogReloadResult::default();

    let content = match std::fs::read_to_string(path) {
        Ok(c) => c,
        Err(e) => {
            tracing::error!(
                "VolumetricFog hot-reload: failed to read '{}': {} (no update)",
                path,
                e,
            );
            return result;
        }
    };
    let entries = match concinnity_cook::world::expand_world_from_str(
        &content,
        crate::authoring::assets_root::assets_dir().as_deref(),
    ) {
        Ok(v) => v,
        Err(e) => {
            tracing::error!(
                "VolumetricFog hot-reload: failed to parse '{}': {} (no update)",
                path,
                e,
            );
            return result;
        }
    };

    // First declared VolumetricFog wins; mirrors the init drain in
    // `run_init`. A missing entry or one with `enabled = false` resolves to
    // `None`, which disables the pass.
    let mut resolved: Option<crate::gfx::volumetric_fog::FogSettings> = None;
    for entry in &entries {
        let asset_type = entry.get("type").and_then(|v| v.as_str()).unwrap_or("");
        if asset_type != "VolumetricFog" {
            continue;
        }
        let args = entry
            .get("args")
            .cloned()
            .unwrap_or(serde_json::Value::Null);
        let parsed: crate::components::VolumetricFog = match serde_json::from_value(args) {
            Ok(p) => p,
            Err(e) => {
                tracing::warn!(
                    "VolumetricFog hot-reload: failed to parse args: {} (kept previous state)",
                    e,
                );
                return result;
            }
        };
        // Apply the build-side validator, exactly as a rebuild would at bake.
        let clamped = concinnity_world::validate::volumetric_fog(parsed);
        if clamped.enabled {
            resolved = Some(crate::gfx::volumetric_fog::FogSettings::resolve(
                clamped.color,
                clamped.density,
                clamped.height_falloff,
                clamped.height_reference,
                clamped.max_distance,
                clamped.phase_g,
                clamped.ambient,
            ));
        }
        break;
    }

    if resolved != *last_pushed {
        backend.update_fog_settings(resolved);
        *last_pushed = resolved;
        result.updated = true;
    }
    result
}

// Per-reload tally for the procedural-mesh path. Counts are surfaced separately
// so a single info! line per asset class keeps the log readable.
#[derive(Default, Debug)]
pub(crate) struct ProceduralMeshReloadResult {
    // Procedural meshes whose args differed from the captured snapshot and
    // whose regenerated geometry was pushed to the backend (in-place
    // `update_mesh_geometry` or batched `rebuild_static_geometry`).
    pub regenerated: usize,
    // Procedural meshes whose args matched the snapshot: nothing pushed.
    pub unchanged: usize,
    // Procedural meshes whose generator returned an error (left at the
    // pre-reload geometry).
    pub failed: usize,
}

// Re-read `world.jsonl` and, for every captured `ProceduralMesh`, diff its
// current generator args against the init-time snapshot. When the args
// differ, re-run `compile_mesh_payload` + `deserialise_with_lods` to get
// fresh vertices / indices / LOD alternates, then dispatch to
// `update_mesh_geometry` (same vertex/index/LOD counts) or batch into a
// single `rebuild_static_geometry` (size changed); mirrors the file-backed
// `Mesh` reload's in-place-vs-rebuild dispatch but driven by JSONL args
// instead of a `.glb` file. Updates the captured args on success so a
// subsequent reload diffs against the post-edit state.
//
// Backend-agnostic: rides on the existing `update_mesh_geometry` /
// `rebuild_static_geometry` trait surface, implemented on all three backends.
pub(super) fn reload_procedural_meshes(
    path: &str,
    procedural_meshes: &mut ProceduralMeshSourceMap,
    backend: &mut dyn crate::gfx::backend::RenderBackend,
) -> ProceduralMeshReloadResult {
    let mut result = ProceduralMeshReloadResult::default();
    if procedural_meshes.is_empty() {
        return result;
    }

    let content = match std::fs::read_to_string(path) {
        Ok(c) => c,
        Err(e) => {
            tracing::error!(
                "ProceduralMesh hot-reload: failed to read '{}': {} (no regen)",
                path,
                e
            );
            return result;
        }
    };
    // Expand prefabs / etc. so auto-injected ProceduralMeshes match the
    // init-time captured set; otherwise an unchanged entry shows up as
    // "missing from JSONL" every reload.
    let entries = match concinnity_cook::world::expand_world_from_str(
        &content,
        crate::authoring::assets_root::assets_dir().as_deref(),
    ) {
        Ok(v) => v,
        Err(e) => {
            tracing::error!(
                "ProceduralMesh hot-reload: failed to parse '{}': {} (no regen)",
                path,
                e
            );
            return result;
        }
    };

    // Build a name -> entry map from the new JSONL for O(N) per-entry lookup:
    // the parsed component (typed equality against the captured init state,
    // so both sides see identical filled-in defaults) plus the raw args JSON
    // the regen compile consumes.
    let mut new_args_by_name: std::collections::HashMap<
        String,
        (crate::components::ProceduralMesh, serde_json::Value),
    > = std::collections::HashMap::new();
    for entry in &entries {
        let asset_type = entry.get("type").and_then(|v| v.as_str()).unwrap_or("");
        if asset_type != "ProceduralMesh" {
            continue;
        }
        let Some(name) = entry.get("name").and_then(|v| v.as_str()) else {
            continue;
        };
        let raw_args = entry
            .get("args")
            .cloned()
            .unwrap_or(serde_json::Value::Null);
        let parsed: crate::components::ProceduralMesh =
            match serde_json::from_value(raw_args.clone()) {
                Ok(p) => p,
                Err(e) => {
                    tracing::warn!(
                        "ProceduralMesh hot-reload: failed to parse '{}' args: {} \
                         (kept old geometry)",
                        name,
                        e
                    );
                    continue;
                }
            };
        new_args_by_name.insert(name.to_string(), (parsed, raw_args));
    }

    // Collect rebuild changes batched into a single rebuild call (mirrors the
    // file-backed Mesh path). Each entry either commits in place, queues a
    // rebuild change, or is unchanged.
    let mut rebuild_changes: Vec<crate::gfx::backend::DrawGeometryUpdate> = Vec::new();
    let mut rebuild_entry_indices: Vec<usize> = Vec::new();
    // Args to write back into the source map after a successful update,
    // staged here so a failed regen / rebuild doesn't clobber the captured
    // value (the diff next reload would then miss the still-pending edit).
    let mut staged_args: Vec<(usize, crate::components::ProceduralMesh)> = Vec::new();

    for (entry_idx, entry) in procedural_meshes.entries.iter().enumerate() {
        let Some((new_args, raw_args)) = new_args_by_name.get(&entry.name) else {
            // No matching entry in the new JSONL: treat as removed-from-jsonl.
            // We deliberately do not destroy the existing draws here: a Prop
            // referencing this mesh is still rendering through its draw slot.
            result.unchanged += 1;
            continue;
        };
        if new_args == &entry.args {
            result.unchanged += 1;
            continue;
        }

        // Regenerate from the new args. Routed through the build wrapper so a
        // live-edited `heightfield` ProceduralMesh still decodes its source
        // image (core's compile_mesh_payload links no image decoders).
        let payload = match concinnity_cook::mesh_compile::compile_mesh_payload(
            raw_args,
            crate::authoring::assets_root::assets_dir().as_deref(),
        ) {
            Ok(b) => b,
            Err(e) => {
                tracing::warn!(
                    "ProceduralMesh hot-reload: regen failed for '{}': {} (kept old geometry)",
                    entry.name,
                    e
                );
                result.failed += 1;
                continue;
            }
        };
        let (vertices, indices, lod_alternates) =
            match crate::gfx::mesh_payload::deserialise_with_lods(&payload) {
                Ok(t) => t,
                Err(e) => {
                    tracing::warn!(
                        "ProceduralMesh hot-reload: deserialise failed for '{}': {} \
                         (kept old geometry)",
                        entry.name,
                        e
                    );
                    result.failed += 1;
                    continue;
                }
            };

        // Match the file-backed Mesh dispatch: size-changing regens go through
        // `rebuild_static_geometry`, size-matched go through
        // `update_mesh_geometry`. The size check probes any one of the draw
        // slots: they were built from the same source so they all carry the
        // same vertex / index / LOD counts.
        let size_changed = entry.draw_indices.iter().any(|&draw_idx| {
            let base_changed = match backend.draw_geometry_size(draw_idx) {
                Some((v, i)) => v != vertices.len() || i != indices.len(),
                None => false,
            };
            let lod_changed = match backend.draw_lod_index_counts(draw_idx) {
                Some(counts) => {
                    counts.len() != lod_alternates.len()
                        || counts
                            .iter()
                            .zip(lod_alternates.iter())
                            .any(|(c, (_, idx))| *c != idx.len())
                }
                None => false,
            };
            base_changed || lod_changed
        });

        if size_changed {
            for &draw_idx in &entry.draw_indices {
                rebuild_changes.push(crate::gfx::backend::DrawGeometryUpdate {
                    draw_idx,
                    vertices: vertices.clone(),
                    indices: indices.clone(),
                    lod_alternates: lod_alternates.clone(),
                });
            }
            rebuild_entry_indices.push(entry_idx);
            staged_args.push((entry_idx, new_args.clone()));
            continue;
        }

        let mut slot_failures = 0usize;
        for &draw_idx in &entry.draw_indices {
            if let Err(e) =
                backend.update_mesh_geometry(draw_idx, &vertices, &indices, &lod_alternates)
            {
                tracing::warn!(
                    "ProceduralMesh hot-reload: backend rejected update for '{}' draw {}: \
                     {}",
                    entry.name,
                    draw_idx,
                    e
                );
                slot_failures += 1;
            }
        }
        if slot_failures == 0 {
            result.regenerated += 1;
            staged_args.push((entry_idx, new_args.clone()));
        } else {
            result.failed += 1;
        }
    }

    if !rebuild_changes.is_empty() {
        match backend.rebuild_static_geometry(rebuild_changes) {
            Ok(()) => {
                result.regenerated += rebuild_entry_indices.len();
            }
            Err(e) => {
                tracing::error!(
                    "ProceduralMesh hot-reload: rebuild_static_geometry failed: {} \
                     ({} entry/entries kept old geometry)",
                    e,
                    rebuild_entry_indices.len()
                );
                result.failed += rebuild_entry_indices.len();
                // Drop the staged-args writes for the failed rebuild so the
                // captured args still reflect what the backend is actually
                // rendering.
                let failed_idxs: std::collections::HashSet<usize> =
                    rebuild_entry_indices.iter().copied().collect();
                staged_args.retain(|(idx, _)| !failed_idxs.contains(idx));
            }
        }
    }

    for (entry_idx, new_args) in staged_args {
        if let Some(e) = procedural_meshes.entries.get_mut(entry_idx) {
            e.args = new_args;
        }
    }

    result
}

// Re-expand the world's stories from disk and return every compiled `Story`
// whose graph changed since the last pass. The expansion is the same one the
// build runs (`expand_world_from_str` includes the StoryImport pass), so the
// returned graphs match what a fresh `cn debug` would compile; `cn debug`
// only, where the interner is warm and re-interning the generated names
// yields the ids the running world already carries. `snapshots` is the
// caller-owned name -> compiled-args map used to skip unchanged stories (a
// save of one story's `.md` must not re-render the others); it starts empty,
// so the first pass after a `.md` event hands over every story once.
//
// Limits, by design: the pass re-reads the graph only. Stage furniture is
// fixed at init, so a story whose widest menu grew renders (and accepts
// clicks for) only the buttons the world was built with, and a new image or
// audio file needs a restart (the sprite atlas pool and clip cache are
// init-time; a swapped-in id that was never pooled falls back with a warn).
pub(super) fn reload_stories(
    path: &str,
    snapshots: &mut std::collections::HashMap<String, serde_json::Value>,
) -> Vec<crate::components::Story> {
    // Each expanded Story deserializes its scaffold name-references, so the
    // name resolver must be installed. In a running editor the initial build
    // already set it up; installing it here too is a cheap no-op and keeps a
    // standalone reload correct.
    crate::ecs::asset_id::ensure_name_resolver();
    let content = match std::fs::read_to_string(path) {
        Ok(c) => c,
        Err(e) => {
            tracing::error!(
                "story hot-reload: failed to read '{}': {} (no update)",
                path,
                e
            );
            return Vec::new();
        }
    };
    let entries = match concinnity_cook::world::expand_world_from_str(
        &content,
        crate::authoring::assets_root::assets_dir().as_deref(),
    ) {
        Ok(v) => v,
        Err(e) => {
            tracing::error!(
                "story hot-reload: failed to expand '{}': {} (kept the running story)",
                path,
                e,
            );
            return Vec::new();
        }
    };
    let mut out = Vec::new();
    for entry in &entries {
        if entry.get("type").and_then(|t| t.as_str()) != Some("Story") {
            continue;
        }
        let Some(name) = entry.get("name").and_then(|n| n.as_str()) else {
            continue;
        };
        let args = entry
            .get("args")
            .cloned()
            .unwrap_or(serde_json::Value::Null);
        if snapshots.get(name) == Some(&args) {
            continue;
        }
        match serde_json::from_value::<crate::components::Story>(args.clone()) {
            Ok(story) => {
                snapshots.insert(name.to_string(), args);
                out.push(story);
            }
            Err(e) => {
                tracing::error!(
                    "story hot-reload: failed to parse compiled story '{}': {} \
                     (kept the running story)",
                    name,
                    e,
                );
            }
        }
    }
    out
}

// Per-reload tally for the world-loaded shader-stage path. Counts are
// surfaced separately from the asset-payload / world / procedural-mesh
// tallies so a single info line per asset class keeps the log readable.
#[derive(Default, Debug)]
pub(crate) struct ShaderStageReloadResult {
    // Shader stage sources that re-compiled successfully and contributed
    // to the backend pipeline rebuild. Counts every kind that the caller
    // re-read off disk, including stages whose source bytes were
    // byte-for-byte unchanged (a no-op shader save still re-compiles,
    // detecting a no-op would mean caching every shader's byte payload).
    pub recompiled: usize,
    // Shader stage sources that failed to compile (logged per-stage in
    // the helper). The backend rebuild is skipped entirely on any failure:
    // the live pipelines stay bound and the next save can recover.
    pub failed: usize,
    // `true` when the backend pipeline rebuild ran and swapped fresh
    // pipelines into the live context. `false` when one or more stages
    // failed to compile or the backend rejected the rebuild: the next
    // save retries from scratch.
    pub pipelines_rebuilt: bool,
}

// Re-compile every captured world-loaded Shader stage source from disk and
// hand the fresh bytes to the backend for a main / instanced / shadow
// pipeline rebuild. Sibling of [`reload_shaders`] in
// [`crate::metal::hot_reload`] (which targets the engine's bundled shader
// directory) but driven by the asset hot-reload
// watcher when one of the world's authored shader sources changes.
//
// Each captured kind goes through
// [`concinnity_cook::shader::compile_shader`]; on any per-stage compile
// failure the helper logs and aborts the whole pass without touching the
// backend, so a typo in one shader never desyncs the others. When every
// stage compiles cleanly the bytes are forwarded through
// [`crate::gfx::backend::RenderBackend::update_world_shader_pipelines`],
// which runs the rebuild-then-swap dance on Metal (and is a no-op on
// Vulkan + DirectX until those backends grow an impl).
//
// Does not detect "unchanged source": every fire recompiles every
// captured stage. A `.metal` save is rare enough (and the compile cheap
// enough) that the savings from per-source mtime tracking would not be
// worth the additional state.
pub(super) fn reload_shader_stages(
    shader_stages: &ShaderStageSourceMap,
    backend: &mut dyn crate::gfx::backend::RenderBackend,
) -> ShaderStageReloadResult {
    use crate::components::ShaderKind;

    let mut result = ShaderStageReloadResult::default();
    if shader_stages.is_empty() {
        return result;
    }

    let mut compiled: std::collections::HashMap<ShaderKind, Vec<u8>> =
        std::collections::HashMap::new();
    for entry in &shader_stages.entries {
        let compile_args = concinnity_cook::shader::ShaderCompileArgs {
            source_path: entry.resolved_path.clone(),
            // Asset name is used by the metal compiler to derive temp paths
            // for the `.air` / `.metallib` intermediates. The source's bare
            // filename (without extension) is a stable per-stage identifier:
            // two stages on different kinds with the same source file
            // are rare in practice but the kind suffix keeps the paths
            // disjoint if it ever happens.
            asset_name: format!(
                "{}_{}",
                std::path::Path::new(&entry.resolved_path)
                    .file_stem()
                    .and_then(|s| s.to_str())
                    .unwrap_or("shader_stage"),
                match entry.kind {
                    ShaderKind::Vertex => "vert",
                    ShaderKind::Fragment => "frag",
                    ShaderKind::VertexInstanced => "vert_inst",
                }
            ),
            kind: entry.kind.compile_kind().to_string(),
            // Hot reload only ever recompiles the world default Shader's
            // captured stages; a world whose shader set forces the bindless
            // entry point had that checked at `cn build`.
            required_entry: None,
        };
        match concinnity_cook::shader::compile_shader(compile_args) {
            Ok(bytes) => {
                compiled.insert(entry.kind, bytes);
                result.recompiled += 1;
            }
            Err(e) => {
                tracing::error!(
                    "Shader hot-reload: failed to compile '{}' ({:?}): {} \
                     (live pipelines kept their previous source)",
                    entry.resolved_path,
                    entry.kind,
                    e
                );
                result.failed += 1;
            }
        }
    }
    if result.failed > 0 {
        return result;
    }

    let vert = compiled.get(&ShaderKind::Vertex).map(|v| v.as_slice());
    let frag = compiled.get(&ShaderKind::Fragment).map(|v| v.as_slice());
    let vert_inst = compiled
        .get(&ShaderKind::VertexInstanced)
        .map(|v| v.as_slice());

    // Shadow shaders are engine-internal: never world-asset hot-reloaded here.
    match backend.update_world_shader_pipelines(vert, frag, None, vert_inst) {
        Ok(()) => {
            result.pipelines_rebuilt = true;
        }
        Err(e) => {
            tracing::error!(
                "Shader hot-reload: backend pipeline rebuild rejected: {} \
                 (live pipelines kept their previous source)",
                e
            );
        }
    }
    result
}