nightshade 0.13.3

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
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
use crate::ecs::world::Entity;
use std::collections::HashMap;
use wgpu::util::DeviceExt;

use super::types::{
    ClusterUniforms, DrawIndexedIndirect, GpuEntityRegistry, GpuLocalTransform, INITIAL_INSTANCES,
    INITIAL_LIGHTS, INITIAL_MATERIALS, LightData, LightGrid, MAX_LIGHTS_PER_CLUSTER, MaterialData,
    MaterialTextures, ModelMatrix, ObjectData, TOTAL_CLUSTERS,
};

pub(super) type BatchRange = (u32, u32, u32, u32);

pub(super) struct WorldGpuBuffers {
    pub materials_buffer: wgpu::Buffer,
    pub materials_buffer_size: usize,
    pub transform_buffer: wgpu::Buffer,
    pub transform_buffer_size: usize,
    pub object_buffer: wgpu::Buffer,
    pub object_buffer_size: usize,
    pub lights_buffer: wgpu::Buffer,
    pub lights_buffer_size: usize,
    pub custom_data_buffer: wgpu::Buffer,
    pub custom_data_buffer_size: usize,
    pub indirect_buffer: wgpu::Buffer,
    pub indirect_buffer_size: usize,
    pub indirect_reset_buffer: wgpu::Buffer,
    pub visible_indices_buffer: wgpu::Buffer,
    pub phase1_indirect_buffer: wgpu::Buffer,
    pub phase1_indirect_reset_buffer: wgpu::Buffer,
    pub phase1_visible_indices_buffer: wgpu::Buffer,
    pub local_transform_buffer: wgpu::Buffer,
    pub local_transform_buffer_size: usize,
    pub instance_bind_group: wgpu::BindGroup,
    pub phase1_instance_bind_group: wgpu::BindGroup,
    pub culling_bind_group: Option<wgpu::BindGroup>,
    pub phase1_culling_bind_group: Option<wgpu::BindGroup>,
    pub transform_compute_bind_group: Option<wgpu::BindGroup>,
    pub cluster_uniforms_buffer: wgpu::Buffer,
    pub light_grid_buffer: wgpu::Buffer,
    pub light_grid_reset_buffer: wgpu::Buffer,
    pub light_indices_buffer: wgpu::Buffer,
    pub cluster_bounds_bind_group: wgpu::BindGroup,
    pub cluster_assign_bind_group: Option<wgpu::BindGroup>,
    pub instanced_local_matrix_buffer: wgpu::Buffer,
    pub instanced_local_matrix_buffer_size: usize,
    pub instanced_compute_bind_group: Option<wgpu::BindGroup>,
}

impl WorldGpuBuffers {
    pub fn new(
        device: &wgpu::Device,
        instance_bind_group_layout: &wgpu::BindGroupLayout,
        cluster_bounds_bind_group_layout: &wgpu::BindGroupLayout,
        cluster_bounds_buffer: &wgpu::Buffer,
        morph_displacement_buffer: &wgpu::Buffer,
    ) -> Self {
        let materials_buffer_size = INITIAL_MATERIALS;
        let materials_buffer = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Mesh Materials Buffer (Per-World)"),
            size: (std::mem::size_of::<MaterialData>() * materials_buffer_size) as u64,
            usage: wgpu::BufferUsages::STORAGE
                | wgpu::BufferUsages::COPY_DST
                | wgpu::BufferUsages::COPY_SRC,
            mapped_at_creation: false,
        });

        let transform_buffer_size = INITIAL_INSTANCES;
        let transform_buffer = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Mesh Transform Buffer (Per-World)"),
            size: (std::mem::size_of::<ModelMatrix>() * transform_buffer_size) as u64,
            usage: wgpu::BufferUsages::STORAGE
                | wgpu::BufferUsages::COPY_DST
                | wgpu::BufferUsages::COPY_SRC,
            mapped_at_creation: false,
        });

        let object_buffer_size = INITIAL_INSTANCES;
        let object_buffer = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Mesh Object Buffer (Per-World)"),
            size: (std::mem::size_of::<ObjectData>() * object_buffer_size) as u64,
            usage: wgpu::BufferUsages::STORAGE
                | wgpu::BufferUsages::COPY_DST
                | wgpu::BufferUsages::COPY_SRC,
            mapped_at_creation: false,
        });

        let lights_buffer_size = INITIAL_LIGHTS;
        let lights_buffer = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Mesh Lights Buffer (Per-World)"),
            size: (std::mem::size_of::<LightData>() * lights_buffer_size) as u64,
            usage: wgpu::BufferUsages::STORAGE
                | wgpu::BufferUsages::COPY_DST
                | wgpu::BufferUsages::COPY_SRC,
            mapped_at_creation: false,
        });

        let custom_data_buffer_size = INITIAL_INSTANCES;
        let custom_data_buffer = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Mesh Custom Data Buffer (Per-World)"),
            size: (std::mem::size_of::<[f32; 4]>() * custom_data_buffer_size) as u64,
            usage: wgpu::BufferUsages::STORAGE
                | wgpu::BufferUsages::COPY_DST
                | wgpu::BufferUsages::COPY_SRC,
            mapped_at_creation: false,
        });

        let indirect_buffer_size = INITIAL_INSTANCES;
        let indirect_buffer = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Indirect Draw Buffer (Per-World)"),
            size: (std::mem::size_of::<DrawIndexedIndirect>() * indirect_buffer_size) as u64,
            usage: wgpu::BufferUsages::INDIRECT
                | wgpu::BufferUsages::STORAGE
                | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        let indirect_reset_buffer = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Indirect Reset Buffer (Per-World)"),
            size: (std::mem::size_of::<DrawIndexedIndirect>() * indirect_buffer_size) as u64,
            usage: wgpu::BufferUsages::COPY_SRC | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        let visible_indices_buffer = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Visible Indices Buffer (Per-World)"),
            size: (std::mem::size_of::<u32>() * INITIAL_INSTANCES) as u64,
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        let phase1_indirect_buffer = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Phase 1 Indirect Buffer (Per-World)"),
            size: (std::mem::size_of::<DrawIndexedIndirect>() * indirect_buffer_size) as u64,
            usage: wgpu::BufferUsages::INDIRECT
                | wgpu::BufferUsages::STORAGE
                | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        let phase1_indirect_reset_buffer = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Phase 1 Indirect Reset Buffer (Per-World)"),
            size: (std::mem::size_of::<DrawIndexedIndirect>() * indirect_buffer_size) as u64,
            usage: wgpu::BufferUsages::COPY_SRC | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        let phase1_visible_indices_buffer = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Phase 1 Visible Indices Buffer (Per-World)"),
            size: (std::mem::size_of::<u32>() * INITIAL_INSTANCES) as u64,
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        let local_transform_buffer_size = INITIAL_INSTANCES;
        let local_transform_buffer = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Local Transform Buffer (Per-World)"),
            size: (std::mem::size_of::<GpuLocalTransform>() * local_transform_buffer_size) as u64,
            usage: wgpu::BufferUsages::STORAGE
                | wgpu::BufferUsages::COPY_DST
                | wgpu::BufferUsages::COPY_SRC,
            mapped_at_creation: false,
        });

        let cluster_uniforms_buffer = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Cluster Uniforms Buffer (Per-World)"),
            size: std::mem::size_of::<ClusterUniforms>() as u64,
            usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        let light_grid_buffer = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Light Grid Buffer (Per-World)"),
            size: (std::mem::size_of::<LightGrid>() * TOTAL_CLUSTERS as usize) as u64,
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        let light_grid_reset_data: Vec<LightGrid> = vec![
            LightGrid {
                offset: 0,
                count: 0
            };
            TOTAL_CLUSTERS as usize
        ];
        let light_grid_reset_buffer =
            device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("Light Grid Reset Buffer (Per-World)"),
                contents: bytemuck::cast_slice(&light_grid_reset_data),
                usage: wgpu::BufferUsages::COPY_SRC,
            });

        let light_indices_buffer = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Light Indices Buffer (Per-World)"),
            size: (std::mem::size_of::<u32>()
                * TOTAL_CLUSTERS as usize
                * MAX_LIGHTS_PER_CLUSTER as usize) as u64,
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        let instanced_local_matrix_buffer_size = INITIAL_INSTANCES;
        let instanced_local_matrix_buffer = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Instanced Local Matrix Buffer (Per-World)"),
            size: (std::mem::size_of::<[[f32; 4]; 4]>() * instanced_local_matrix_buffer_size)
                as u64,
            usage: wgpu::BufferUsages::STORAGE
                | wgpu::BufferUsages::COPY_DST
                | wgpu::BufferUsages::COPY_SRC,
            mapped_at_creation: false,
        });

        let cluster_bounds_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("Cluster Bounds Bind Group (Per-World)"),
            layout: cluster_bounds_bind_group_layout,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: cluster_uniforms_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: cluster_bounds_buffer.as_entire_binding(),
                },
            ],
        });

        let instance_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("Mesh Instance Bind Group (Per-World)"),
            layout: instance_bind_group_layout,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: transform_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: materials_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: object_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: lights_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 4,
                    resource: visible_indices_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 5,
                    resource: custom_data_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 6,
                    resource: morph_displacement_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 7,
                    resource: light_grid_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 8,
                    resource: light_indices_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 9,
                    resource: cluster_uniforms_buffer.as_entire_binding(),
                },
            ],
        });

        let phase1_instance_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("Phase 1 Instance Bind Group (Per-World)"),
            layout: instance_bind_group_layout,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: transform_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: materials_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: object_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: lights_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 4,
                    resource: phase1_visible_indices_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 5,
                    resource: custom_data_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 6,
                    resource: morph_displacement_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 7,
                    resource: light_grid_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 8,
                    resource: light_indices_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 9,
                    resource: cluster_uniforms_buffer.as_entire_binding(),
                },
            ],
        });

        Self {
            materials_buffer,
            materials_buffer_size,
            transform_buffer,
            transform_buffer_size,
            object_buffer,
            object_buffer_size,
            lights_buffer,
            lights_buffer_size,
            custom_data_buffer,
            custom_data_buffer_size,
            indirect_buffer,
            indirect_buffer_size,
            indirect_reset_buffer,
            visible_indices_buffer,
            phase1_indirect_buffer,
            phase1_indirect_reset_buffer,
            phase1_visible_indices_buffer,
            local_transform_buffer,
            local_transform_buffer_size,
            instance_bind_group,
            phase1_instance_bind_group,
            culling_bind_group: None,
            phase1_culling_bind_group: None,
            transform_compute_bind_group: None,
            cluster_uniforms_buffer,
            light_grid_buffer,
            light_grid_reset_buffer,
            light_indices_buffer,
            cluster_bounds_bind_group,
            cluster_assign_bind_group: None,
            instanced_local_matrix_buffer,
            instanced_local_matrix_buffer_size,
            instanced_compute_bind_group: None,
        }
    }
}

pub(super) struct WorldRenderState {
    pub gpu_registry: GpuEntityRegistry,
    pub entity_to_transform_index: HashMap<Entity, u32>,
    pub cached_entities: Vec<Entity>,
    pub cached_transforms: Vec<ModelMatrix>,
    pub cached_objects: Vec<ObjectData>,

    pub opaque_instances: Vec<BatchRange>,
    pub opaque_double_sided_instances: Vec<BatchRange>,
    pub transparent_instances: Vec<BatchRange>,
    pub overlay_opaque_instances: Vec<BatchRange>,
    pub overlay_opaque_double_sided_instances: Vec<BatchRange>,
    pub overlay_transparent_instances: Vec<BatchRange>,
    pub instanced_opaque_batches: Vec<BatchRange>,
    pub instanced_opaque_double_sided_batches: Vec<BatchRange>,
    pub instanced_transparent_batches: Vec<BatchRange>,
    pub regular_object_count: u32,
    pub instanced_transform_ranges: HashMap<Entity, (u32, u32)>,
    pub instanced_compute_dirty: bool,

    pub object_count: u32,

    pub indirect_reset_count: usize,

    pub last_camera_hash: u64,
    pub camera_changed: bool,
    pub num_directional_lights: u32,
    pub num_total_lights: u32,

    pub max_hierarchy_depth: u32,
    pub hierarchy_dirty: bool,
    pub entity_depths: Vec<u32>,
    pub entity_parent_indices: Vec<i32>,

    pub last_used_frame: u64,

    pub gpu_buffers: Option<WorldGpuBuffers>,

    pub ibl_brdf_lut_view: Option<wgpu::TextureView>,
    pub ibl_irradiance_view: Option<wgpu::TextureView>,
    pub ibl_prefiltered_view: Option<wgpu::TextureView>,
    pub ibl_irradiance_b_view: Option<wgpu::TextureView>,
    pub ibl_prefiltered_b_view: Option<wgpu::TextureView>,
    pub ibl_blend_factor: f32,
    pub ibl_dirty: bool,

    pub cached_name_to_material_id: HashMap<String, u32>,
    pub cached_material_map: HashMap<Entity, u32>,
    pub cached_custom_data: Vec<[f32; 4]>,
    pub cached_transparent_material_ids: std::collections::HashSet<u32>,
    pub cached_double_sided_material_ids: std::collections::HashSet<u32>,
    pub frames_since_full_rebuild: u64,
    pub free_slots_by_group: HashMap<(u32, u32), Vec<u32>>,

    pub material_bind_groups: HashMap<u32, wgpu::BindGroup>,
    pub material_bind_group_cache_key: HashMap<u32, MaterialTextures>,
    pub material_bind_group_found_textures: HashMap<u32, MaterialTextures>,
}

impl WorldRenderState {
    pub fn new() -> Self {
        Self {
            gpu_registry: GpuEntityRegistry::new(),
            entity_to_transform_index: HashMap::new(),
            cached_entities: Vec::new(),
            cached_transforms: Vec::new(),
            cached_objects: Vec::new(),

            opaque_instances: Vec::new(),
            opaque_double_sided_instances: Vec::new(),
            transparent_instances: Vec::new(),
            overlay_opaque_instances: Vec::new(),
            overlay_opaque_double_sided_instances: Vec::new(),
            overlay_transparent_instances: Vec::new(),
            instanced_opaque_batches: Vec::new(),
            instanced_opaque_double_sided_batches: Vec::new(),
            instanced_transparent_batches: Vec::new(),
            regular_object_count: 0,
            instanced_transform_ranges: HashMap::new(),
            instanced_compute_dirty: false,

            object_count: 0,

            indirect_reset_count: 0,

            last_camera_hash: 0,
            camera_changed: false,
            num_directional_lights: 0,
            num_total_lights: 0,

            max_hierarchy_depth: 0,
            hierarchy_dirty: false,
            entity_depths: Vec::new(),
            entity_parent_indices: Vec::new(),

            last_used_frame: 0,

            gpu_buffers: None,

            ibl_brdf_lut_view: None,
            ibl_irradiance_view: None,
            ibl_prefiltered_view: None,
            ibl_irradiance_b_view: None,
            ibl_prefiltered_b_view: None,
            ibl_blend_factor: 0.0,
            ibl_dirty: false,

            cached_name_to_material_id: HashMap::new(),
            cached_material_map: HashMap::new(),
            cached_custom_data: Vec::new(),
            cached_transparent_material_ids: std::collections::HashSet::new(),
            cached_double_sided_material_ids: std::collections::HashSet::new(),
            frames_since_full_rebuild: 0,
            free_slots_by_group: HashMap::new(),

            material_bind_groups: HashMap::new(),
            material_bind_group_cache_key: HashMap::new(),
            material_bind_group_found_textures: HashMap::new(),
        }
    }
}

impl Default for WorldRenderState {
    fn default() -> Self {
        Self::new()
    }
}