Skip to main content

celestialsim/chunk_nodes/
upload.rs

1//! `celestial/chunk-upload` — CPU → GPU chunk descriptor + params + instance
2//! buffer updates (transfers, outside any compute list). Mirrors `nodes/upload.rs`.
3
4use crate::chunk_nodes::ChunkNode;
5use crate::chunk_pipeline::ChunkCtx;
6
7pub struct ChunkUpload;
8
9impl ChunkNode for ChunkUpload {
10    fn name(&self) -> &'static str {
11        "celestial/chunk-upload"
12    }
13
14    fn record(&mut self, ctx: &mut ChunkCtx<'_>) {
15        // CPU → GPU buffer updates must run outside any open compute list.
16        ctx.close_list();
17        let mut rd = ctx.rd.clone();
18        let terrain = ctx.terrain;
19        if let Some(stage) = ctx.stage.take() {
20            // Set the surface flags BEFORE upload_params so the repacked ChunkParams
21            // carries the global toggle + height scale. A custom GPU surface owns
22            // the toggle itself: on only once its shader compiled (else the buffers
23            // it fills are never written → fall back to procedural), with the
24            // layer's height scale.
25            if ctx.gpu.custom_requested() {
26                let hs = ctx.gpu.custom_height_scale();
27                let on = if ctx.gpu.custom_ready() { 1.0 } else { 0.0 };
28                ctx.gpu.set_surface(on, hs);
29            } else {
30                ctx.gpu.set_surface(stage.surface_enabled, stage.surface_height_scale);
31            }
32            ctx.gpu.upload_descs(&mut rd, &stage.desc_bytes, stage.realize_count);
33            ctx.gpu.upload_params(&mut rd, stage.realize_count, &terrain);
34            ctx.gpu.upload_instances(&mut rd, &stage.instance_bytes, stage.instance_count);
35            // Custom GPU surface: refresh its params UBO (chunk_count + live knobs)
36            // for the surface-custom dispatch. Transfer — must be outside a list.
37            ctx.gpu.upload_custom_params(&mut rd, stage.realize_count);
38            // Upload each realized chunk's CPU-surface color+height patch into its atlas slot.
39            for (slot, color, height, normal) in &stage.surface_patches {
40                ctx.gpu.upload_surface(&mut rd, *slot, color, height, normal);
41            }
42            // CEL-73 scatter staging: aux paths, visible list, per-layer params
43            // (fresh density/height each stage), and counter/draw-count zeroing.
44            ctx.gpu.upload_scatter(
45                &mut rd,
46                &stage.scatter_aux_bytes,
47                &stage.scatter_vis_bytes,
48                &stage.scatter_layer_params,
49            );
50            ctx.realize_count = stage.realize_count;
51            ctx.vis_count = stage.scatter_vis_count;
52        }
53    }
54}