nightshade 0.54.0

A cross-platform data-oriented game engine.
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
use crate::ecs::loading::{
    loading_pipeline_is_active, loading_pipeline_mark_completed, loading_pipeline_poll_decodes,
    loading_pipeline_pop, loading_pipeline_recipe_ready, loading_pipeline_release_recipe_sources,
    loading_pipeline_requeue_front, loading_pipeline_reset_source_arena,
};
use crate::render::wgpu;
use crate::render::wgpu::DEPTH_PICK_SAMPLE_SIZE;
use crate::render::wgpu::pass_sync;
use crate::render::wgpu::presentation;
use crate::render::wgpu::render_configs::FrameOutputs;
use crate::render::wgpu::texture_uploads;
use crate::render_driver::scene_entity;

/// Renders one frame: the pre-frame steps that need both the world and the
/// renderer run first, then the frame inputs are composed, the renderer's
/// frame driver renders from them alone, and the outputs are restored into
/// the world.
pub fn render_frame(
    renderer: &mut crate::render::wgpu::WgpuRenderer,
    world: &mut crate::ecs::world::World,
) -> Result<(), Box<dyn std::error::Error>> {
    poll_depth_pick_readback(renderer, world);
    drain_loading_tasks(renderer, world);
    super::extract::prepare_text_meshes(renderer, world);
    #[cfg(feature = "egui")]
    super::egui::prepare_egui_pass(renderer, world);

    let mut inputs = super::inputs::compose_render_inputs(world);
    let mut outputs = FrameOutputs::default();
    let result = crate::render::wgpu::frame::render_frame(renderer, &mut inputs, &mut outputs);
    super::inputs::restore_render_inputs(world, inputs, outputs);
    result
}

pub fn configure_with_state(
    renderer: &mut crate::render::wgpu::WgpuRenderer,
    state: &mut dyn crate::run::State,
) -> Result<(), Box<dyn std::error::Error>> {
    pass_sync::rebind_glyph_atlas(renderer);

    state.configure_render_graph(
        &mut renderer.graph,
        &renderer.device,
        renderer.surface_format,
        crate::run::RenderResources {
            scene_color: renderer.targets.scene_color,
            depth: renderer.targets.depth,
            compute_output: renderer.targets.compute_output,
            swapchain: renderer.targets.swapchain,
            view_normals: renderer.targets.view_normals,
            velocity: renderer.targets.velocity,
            ssao_raw: renderer.targets.ssao_raw,
            ssao: renderer.targets.ssao,
            ssgi_raw: renderer.targets.ssgi_raw,
            ssgi: renderer.targets.ssgi,
            ssr_raw: renderer.targets.ssr_raw,
            ssr: renderer.targets.ssr,
            surface_width: renderer.surface_config.width,
            surface_height: renderer.surface_config.height,
        },
    );

    presentation::install_presentation_passes(renderer)
}

pub fn update_with_state(
    renderer: &mut crate::render::wgpu::WgpuRenderer,
    state: &mut dyn crate::run::State,
    world: &mut crate::ecs::world::World,
) -> Result<(), Box<dyn std::error::Error>> {
    let pending_font_loads: Vec<_> = world
        .res_mut::<crate::ecs::loading::LoadingState>()
        .pending_font_loads
        .drain(..)
        .collect();
    for pending_load in pending_font_loads {
        world
            .res_mut::<crate::ecs::text::resources::TextState>()
            .font_engine
            .load_font(pending_load.font_data);
    }

    state.update_render_graph(&mut renderer.graph, world);
    sync_cloth_write_targets(renderer, world);
    Ok(())
}

/// Gathers each cloth's mesh name and widened cull bounds from the world and
/// hands the cloth pass its vertex buffer write targets for this frame.
pub(crate) fn sync_cloth_write_targets(
    renderer: &mut crate::render::wgpu::WgpuRenderer,
    world: &crate::ecs::world::World,
) {
    let mut cloths: Vec<pass_sync::ClothWriteBounds> = Vec::new();
    for (_entity, (cloth, render_mesh)) in world
        .query_ref::<(
            &crate::ecs::cloth::components::Cloth,
            &crate::ecs::mesh::components::RenderMesh,
        )>()
        .iter()
    {
        let bounds = crate::ecs::cloth::systems::cloth_bounding_volume(cloth);
        cloths.push(pass_sync::ClothWriteBounds {
            mesh_name: render_mesh.name.clone(),
            center: [
                bounds.obb.center.x,
                bounds.obb.center.y,
                bounds.obb.center.z,
            ],
            sphere_radius: bounds.sphere_radius,
        });
    }
    pass_sync::set_cloth_write_targets(renderer, &cloths);
}

/// Polls a pending GPU pick readback and, once the staging buffer is mapped,
/// stores the depth and entity-id samples on the picking resource and
/// resolves the picked world position through the requesting camera.
pub(crate) fn poll_depth_pick_readback(
    renderer: &mut crate::render::wgpu::WgpuRenderer,
    world: &mut crate::ecs::world::World,
) {
    if !renderer.depth_pick.pending {
        return;
    }
    let _ = renderer.device.poll(wgpu::PollType::Poll);

    if !renderer
        .depth_pick
        .map_complete
        .load(std::sync::atomic::Ordering::Relaxed)
    {
        return;
    }
    let buffer_slice = renderer.depth_pick.staging_buffer.slice(..);
    let data = buffer_slice.get_mapped_range();
    let mut depth_values = Vec::new();
    let mut entity_id_values = Vec::new();
    for chunk in data.chunks_exact(8) {
        let depth = f32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]);
        let entity_id = u32::from_le_bytes([chunk[4], chunk[5], chunk[6], chunk[7]]);
        depth_values.push(depth);
        entity_id_values.push(entity_id);
    }
    drop(data);
    renderer.depth_pick.staging_buffer.unmap();

    world
        .res_mut::<crate::ecs::gpu_picking::GpuPicking>()
        .set_depth_samples(
            depth_values,
            entity_id_values,
            DEPTH_PICK_SAMPLE_SIZE,
            DEPTH_PICK_SAMPLE_SIZE,
            renderer.depth_pick.center.0,
            renderer.depth_pick.center.1,
        );

    if let Some(camera_entity) = renderer.depth_pick.camera
        && let Some(matrices) =
            crate::ecs::camera::queries::query_camera_matrices(world, scene_entity(camera_entity))
    {
        let (texture_width, texture_height) = renderer.depth_pick.texture_size;
        let inverse_view_proj = (matrices.projection * matrices.view)
            .try_inverse()
            .unwrap_or_else(nalgebra_glm::Mat4::identity);

        world
            .res_mut::<crate::ecs::gpu_picking::GpuPicking>()
            .compute_result(
                &inverse_view_proj,
                texture_width as f32,
                texture_height as f32,
            );
    }

    renderer.depth_pick.pending = false;
    renderer
        .depth_pick
        .map_complete
        .store(false, std::sync::atomic::Ordering::Relaxed);
}

pub(crate) fn drain_loading_tasks(
    renderer: &mut crate::render::wgpu::WgpuRenderer,
    world: &mut crate::ecs::world::World,
) {
    // Collect images the worker pool finished decoding. The GPU upload below
    // is the only throttled step; decoding already ran off this thread.
    loading_pipeline_poll_decodes(
        &mut world
            .res_mut::<crate::ecs::loading::LoadingState>()
            .pipeline,
        crate::ecs::loading::DECODE_POLL_BUDGET,
    );

    let budget = world
        .res::<crate::ecs::loading::LoadingState>()
        .pipeline
        .tasks_per_frame
        .max(1);
    let mut uploaded_textures: Vec<crate::render::asset_id::TextureId> = Vec::new();
    let mut deferred: Vec<crate::ecs::loading::LoadingTask> = Vec::new();
    let mut reserved_any = false;
    let mut uploads = 0usize;
    while uploads < budget {
        let Some(task) = loading_pipeline_pop(
            &mut world
                .res_mut::<crate::ecs::loading::LoadingState>()
                .pipeline,
        ) else {
            break;
        };
        let category = task.category();
        let label = match &task {
            crate::ecs::loading::LoadingTask::ReserveLayer { texture, .. }
            | crate::ecs::loading::LoadingTask::UploadDecodedTexture { texture, .. }
            | crate::ecs::loading::LoadingTask::MaterializeTexture { texture, .. } => {
                crate::render::wgpu::texture_cache::texture_cache_texture_name(
                    world.res::<crate::render::wgpu::texture_cache::TextureCache>(),
                    texture.index,
                )
                .map(str::to_string)
                .unwrap_or_else(|| format!("texture {}", texture.index))
            }
        };
        match task {
            crate::ecs::loading::LoadingTask::ReserveLayer {
                texture,
                usage,
                sampler,
            } => {
                // Reserving a stable layer and writing its placeholder is cheap and
                // must happen before geometry first renders, so it does not draw from
                // the per-frame upload budget.
                if texture_uploads::reserve_material_layer(
                    renderer,
                    world.res::<crate::render::wgpu::texture_cache::TextureCache>(),
                    texture,
                    usage,
                    sampler,
                ) {
                    reserved_any = true;
                }
            }
            crate::ecs::loading::LoadingTask::UploadDecodedTexture {
                texture,
                rgba_data,
                width,
                height,
                usage,
                sampler,
            } => {
                texture_uploads::upload_material_texture(
                    renderer,
                    world.res_mut::<crate::render::wgpu::texture_cache::TextureCache>(),
                    texture_uploads::MaterialTextureUploadRequest {
                        texture,
                        rgba_data: &rgba_data,
                        width,
                        height,
                        usage,
                        sampler,
                    },
                );
                uploaded_textures.push(texture);
                uploads += 1;
            }
            crate::ecs::loading::LoadingTask::MaterializeTexture {
                texture,
                recipe,
                usage,
                sampler,
            } => {
                // Hold the task until every source image it samples has finished
                // decoding in the worker pool, then upload into the reserved layer.
                if !loading_pipeline_recipe_ready(
                    &world.res::<crate::ecs::loading::LoadingState>().pipeline,
                    &recipe,
                ) {
                    deferred.push(crate::ecs::loading::LoadingTask::MaterializeTexture {
                        texture,
                        recipe,
                        usage,
                        sampler,
                    });
                    continue;
                }
                let produced = crate::ecs::loading::execute_texture_recipe(
                    &recipe,
                    &world
                        .res::<crate::ecs::loading::LoadingState>()
                        .pipeline
                        .decoded_images,
                );
                loading_pipeline_release_recipe_sources(
                    &mut world
                        .res_mut::<crate::ecs::loading::LoadingState>()
                        .pipeline,
                    &recipe,
                );
                if let Some(decoded) = produced {
                    // Capture the materialized RGBA into texture_sources for any
                    // texture whose recipe is composite (RG-pack, RGB+A pack,
                    // spec/gloss conversion). Direct-recipe textures already have
                    // their PNG bytes captured at queue_gltf_load and are skipped
                    // here via the `entry`-style insert.
                    let texture_name =
                        crate::render::wgpu::texture_cache::texture_cache_texture_name(
                            world.res::<crate::render::wgpu::texture_cache::TextureCache>(),
                            texture.index,
                        )
                        .map(str::to_string);
                    if let Some(name) = texture_name {
                        world
                            .res_mut::<crate::ecs::asset_state::AssetState>()
                            .texture_sources
                            .entry(name)
                            .or_insert_with(|| crate::ecs::asset_state::TextureSourceBytes {
                                data: crate::ecs::asset_state::TextureSourceData::Rgba {
                                    rgba: decoded.rgba.clone(),
                                    width: decoded.width,
                                    height: decoded.height,
                                },
                                usage,
                                sampler,
                            });
                    }
                    texture_uploads::upload_material_texture(
                        renderer,
                        world.res_mut::<crate::render::wgpu::texture_cache::TextureCache>(),
                        texture_uploads::MaterialTextureUploadRequest {
                            texture,
                            rgba_data: &decoded.rgba,
                            width: decoded.width,
                            height: decoded.height,
                            usage,
                            sampler,
                        },
                    );
                    uploaded_textures.push(texture);
                } else {
                    tracing::warn!("texture recipe produced no data for '{}'", label);
                }
                uploads += 1;
            }
        }
        loading_pipeline_mark_completed(
            &mut world
                .res_mut::<crate::ecs::loading::LoadingState>()
                .pipeline,
            label,
            category,
        );
    }
    for task in deferred.into_iter().rev() {
        loading_pipeline_requeue_front(
            &mut world
                .res_mut::<crate::ecs::loading::LoadingState>()
                .pipeline,
            task,
        );
    }
    // Stable layers mean a streamed pixel upload never changes a material's layer
    // index, so only a fresh reservation (or a freshly inserted material) needs the
    // resolve plus instance rebuild.
    let needs_resolve = reserved_any
        || !world
            .res::<crate::ecs::asset_state::AssetState>()
            .material_registry
            .pending_resolve
            .is_empty();
    if needs_resolve {
        let texture_cache =
            std::mem::take(world.res_mut::<crate::render::wgpu::texture_cache::TextureCache>());
        crate::ecs::material::resources::material_registry_resolve_uploaded_textures(
            &mut world
                .res_mut::<crate::ecs::asset_state::AssetState>()
                .material_registry,
            &texture_cache,
            &uploaded_textures,
        );
        *world.res_mut::<crate::render::wgpu::texture_cache::TextureCache>() = texture_cache;
        world
            .res_mut::<crate::render::mesh_state::MeshRenderState>()
            .request_full_rebuild();
    }
    if !loading_pipeline_is_active(&world.res::<crate::ecs::loading::LoadingState>().pipeline) {
        loading_pipeline_reset_source_arena(
            &mut world
                .res_mut::<crate::ecs::loading::LoadingState>()
                .pipeline,
        );
    }

    let evicted = crate::render::wgpu::texture_cache::texture_cache_remove_unused(
        world.res_mut::<crate::render::wgpu::texture_cache::TextureCache>(),
    );
    if !evicted.is_empty() {
        texture_uploads::unregister_evicted_textures(renderer, &evicted);
        for name in &evicted {
            world
                .res_mut::<crate::ecs::asset_state::AssetState>()
                .texture_sources
                .remove(name);
        }
        world
            .res_mut::<crate::render::mesh_state::MeshRenderState>()
            .request_full_rebuild();
    }
}