bevy_lit 0.9.1

A lighting 2d library for Bevy
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
use bevy::{
    asset::{embedded_asset, load_embedded_asset, AssetEventSystems},
    core_pipeline::FullscreenShader,
    ecs::{
        component::Tick,
        system::{lifetimeless::SRes, SystemChangeTick, SystemParamItem},
    },
    math::FloatOrd,
    mesh::MeshVertexBufferLayoutRef,
    prelude::*,
    render::{
        batching::no_gpu_preprocessing::batch_and_prepare_sorted_render_phase,
        extract_component::ExtractComponentPlugin,
        mesh::RenderMesh,
        render_asset::{prepare_assets, RenderAssets},
        render_phase::{
            AddRenderCommand, DrawFunctions, PhaseItem, PhaseItemExtraIndex, RenderCommand,
            RenderCommandResult, SetItemPipeline, TrackedRenderPass, ViewSortedRenderPhases,
        },
        render_resource::{
            binding_types::{sampler, texture_2d, uniform_buffer},
            BindGroup, BindGroupEntries, BindGroupLayout, BindGroupLayoutEntries,
            CachedRenderPipelineId, ColorTargetState, ColorWrites, FragmentState, PipelineCache,
            RenderPipelineDescriptor, SamplerBindingType, SamplerDescriptor, ShaderStages,
            SpecializedMeshPipeline, SpecializedMeshPipelineError, SpecializedMeshPipelines,
            TextureFormat, TextureSampleType,
        },
        renderer::RenderDevice,
        sync_world::{MainEntity, MainEntityHashMap},
        texture::{FallbackImage, GpuImage},
        view::{ExtractedView, RenderVisibleEntities},
        Extract, Render, RenderApp, RenderStartup, RenderSystems,
    },
    sprite_render::{
        init_mesh_2d_pipeline, DrawMesh2d, EntitiesNeedingSpecialization,
        EntitySpecializationTickPair, Mesh2dPipeline, Mesh2dPipelineKey, RenderMesh2dInstances,
        SetMesh2dBindGroup, SetMesh2dViewBindGroup, SpecializedMaterial2dPipelineCache,
        ViewKeyCache,
    },
    utils::Parallel,
};

use crate::{
    occlusion::LightOccluder2d,
    prelude::Lighting2dSettings,
    render::{extract_light2d_phases, VoronoiPhase},
};

pub struct Voronoi2dPlugin;
impl Plugin for Voronoi2dPlugin {
    fn build(&self, app: &mut App) {
        embedded_asset!(app, "mask.wgsl");
        embedded_asset!(app, "flood_seed.wgsl");
        embedded_asset!(app, "flood.wgsl");

        app.add_plugins(ExtractComponentPlugin::<LightOccluder2d>::default())
            .init_resource::<EntitiesNeedingSpecialization<Lighting2dSettings>>()
            .init_resource::<EntitiesNeedingSpecialization<LightOccluder2d>>()
            .add_systems(
                PostUpdate,
                (
                    check_views_needing_specialization,
                    check_materials_needing_specialization,
                )
                    .after(AssetEventSystems),
            );

        let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
            return;
        };

        render_app
            .init_resource::<SpecializedMeshPipelines<MaskPipeline>>()
            .init_resource::<RenderVoronoiMaterials>()
            .init_resource::<MaskMaterialBindGroups>()
            .init_resource::<DrawFunctions<VoronoiPhase>>()
            .init_resource::<SpecializedMaterial2dPipelineCache<LightOccluder2d>>()
            .init_resource::<EntitySpecializationTickPair<Lighting2dSettings>>()
            .init_resource::<EntitySpecializationTickPair<LightOccluder2d>>()
            .init_resource::<VoronoiViewSpecializationTicks>()
            .add_render_command::<VoronoiPhase, DrawMaskMesh>()
            .add_systems(
                ExtractSchedule,
                (
                    extract_entities_needs_specialization,
                    extract_views_need_specialization,
                    extract_voronoi_materials,
                )
                    .after(extract_light2d_phases),
            )
            .add_systems(
                RenderStartup,
                (
                    init_mask_pipeline.after(init_mesh_2d_pipeline),
                    init_flood_pipeline,
                ),
            )
            .add_systems(
                Render,
                (
                    specialize_mask_meshes
                        .in_set(RenderSystems::PrepareMeshes)
                        .after(prepare_assets::<RenderMesh>),
                    queue_mask_meshes
                        .in_set(RenderSystems::QueueMeshes)
                        .after(prepare_assets::<RenderMesh>),
                    batch_and_prepare_sorted_render_phase::<VoronoiPhase, Mesh2dPipeline>
                        .in_set(RenderSystems::PrepareResources),
                    prepare_mask_material_bind_groups.in_set(RenderSystems::PrepareBindGroups),
                ),
            );
    }
}

pub fn check_views_needing_specialization(
    needs_specialization: Query<
        Entity,
        (
            Or<(
                Changed<Camera>,
                Changed<Lighting2dSettings>,
                Changed<GlobalTransform>,
            )>,
            With<Lighting2dSettings>,
        ),
    >,
    mut par_local: Local<Parallel<Vec<Entity>>>,
    mut entities_needing_specialization: ResMut<EntitiesNeedingSpecialization<Lighting2dSettings>>,
) {
    entities_needing_specialization.clear();

    needs_specialization
        .par_iter()
        .for_each(|entity| par_local.borrow_local_mut().push(entity));

    par_local.drain_into(&mut entities_needing_specialization);
}

pub fn check_materials_needing_specialization(
    needs_specialization: Query<
        Entity,
        (
            Or<(
                Changed<Mesh2d>,
                AssetChanged<Mesh2d>,
                Changed<LightOccluder2d>,
                AssetChanged<LightOccluder2d>,
                Changed<GlobalTransform>,
            )>,
            With<LightOccluder2d>,
        ),
    >,
    mut par_local: Local<Parallel<Vec<Entity>>>,
    mut entities_needing_specialization: ResMut<EntitiesNeedingSpecialization<LightOccluder2d>>,
) {
    entities_needing_specialization.clear();

    needs_specialization
        .par_iter()
        .for_each(|entity| par_local.borrow_local_mut().push(entity));

    par_local.drain_into(&mut entities_needing_specialization);
}

#[derive(Resource, Deref, DerefMut, Default)]
pub struct VoronoiViewSpecializationTicks(MainEntityHashMap<Tick>);

pub fn extract_views_need_specialization(
    entities_needing_specialization: Extract<
        Res<EntitiesNeedingSpecialization<Lighting2dSettings>>,
    >,
    mut view_specialization_ticks: ResMut<VoronoiViewSpecializationTicks>,
    ticks: SystemChangeTick,
) {
    for entity in entities_needing_specialization.iter() {
        view_specialization_ticks.insert((*entity).into(), ticks.this_run());
    }
}

pub fn extract_entities_needs_specialization(
    entities_needing_specialization: Extract<Res<EntitiesNeedingSpecialization<LightOccluder2d>>>,
    mut entity_specialization_ticks: ResMut<EntitySpecializationTickPair<LightOccluder2d>>,
    mut removed_components: Extract<RemovedComponents<LightOccluder2d>>,
    mut specialized_view_pipeline_cache: ResMut<
        SpecializedMaterial2dPipelineCache<LightOccluder2d>,
    >,
    views: Query<&MainEntity, With<ExtractedView>>,
    ticks: SystemChangeTick,
) {
    for entity in removed_components.read() {
        entity_specialization_ticks.remove(&MainEntity::from(entity));
        for view in views {
            if let Some(cache) = specialized_view_pipeline_cache.get_mut(view) {
                cache.remove(&MainEntity::from(entity));
            }
        }
    }

    for entity in entities_needing_specialization.iter() {
        entity_specialization_ticks.insert((*entity).into(), ticks.this_run());
    }
}

#[derive(Resource, Deref, DerefMut, Default)]
pub struct RenderVoronoiMaterials(MainEntityHashMap<AssetId<Image>>);

fn extract_voronoi_materials(
    mut render_voronoi_instances: ResMut<RenderVoronoiMaterials>,
    query: Extract<Query<(Entity, &ViewVisibility, &LightOccluder2d), With<Mesh2d>>>,
) {
    render_voronoi_instances.clear();

    for (entity, view_visibility, material) in &query {
        if view_visibility.get() {
            render_voronoi_instances.insert(entity.into(), material.into());
        }
    }
}

#[derive(Resource)]
pub struct MaskPipeline {
    pub mesh_pipeline: Mesh2dPipeline,
    pub material_layout: BindGroupLayout,
    pub shader: Handle<Shader>,
}

impl SpecializedMeshPipeline for MaskPipeline {
    type Key = Mesh2dPipelineKey;

    fn specialize(
        &self,
        key: Self::Key,
        layout: &MeshVertexBufferLayoutRef,
    ) -> Result<RenderPipelineDescriptor, SpecializedMeshPipelineError> {
        let descriptor = self.mesh_pipeline.specialize(key, &layout)?;

        let mut mesh_layout = descriptor.layout.clone();
        mesh_layout.push(self.material_layout.clone());

        Ok(RenderPipelineDescriptor {
            label: Some("mask_pipeline".into()),
            layout: mesh_layout,
            fragment: Some(FragmentState {
                shader: self.shader.clone(),
                shader_defs: vec![],
                entry_point: Some("fragment".into()),
                targets: vec![Some(ColorTargetState {
                    format: TextureFormat::Rgba16Float,
                    blend: None,
                    write_mask: ColorWrites::ALL,
                })],
            }),
            depth_stencil: None,
            multisample: Default::default(),
            ..descriptor
        })
    }
}

pub fn init_mask_pipeline(
    mut commands: Commands,
    render_device: Res<RenderDevice>,
    mesh_2d_pipeline: Res<Mesh2dPipeline>,
    asset_server: Res<AssetServer>,
) {
    commands.insert_resource(MaskPipeline {
        mesh_pipeline: mesh_2d_pipeline.clone(),
        shader: load_embedded_asset!(asset_server.as_ref(), "mask.wgsl"),
        material_layout: render_device.create_bind_group_layout(
            "mask_material_bind_group_layout",
            &BindGroupLayoutEntries::sequential(
                ShaderStages::FRAGMENT,
                (
                    texture_2d(TextureSampleType::Float { filterable: true }),
                    sampler(SamplerBindingType::Filtering),
                ),
            ),
        ),
    });
}

pub fn specialize_mask_meshes(
    render_meshes: Res<RenderAssets<RenderMesh>>,
    pipeline_cache: Res<PipelineCache>,
    mut render_mesh_instances: ResMut<RenderMesh2dInstances>,
    mut mask_pipelines: ResMut<SpecializedMeshPipelines<MaskPipeline>>,
    mask_pipeline: Res<MaskPipeline>,
    view_key_cache: Res<ViewKeyCache>,
    views: Query<(&MainEntity, &RenderVisibleEntities)>,
    render_material_instances: Res<RenderVoronoiMaterials>,
    mut specialized_material_pipeline_cache: ResMut<
        SpecializedMaterial2dPipelineCache<LightOccluder2d>,
    >,
    material_specialization_ticks: Res<EntitySpecializationTickPair<LightOccluder2d>>,
    view_specialization_ticks: Res<VoronoiViewSpecializationTicks>,
    ticks: SystemChangeTick,
) {
    if render_material_instances.is_empty() {
        return;
    }

    for (view_entity, visible_entities) in &views {
        let Some(view_key) = view_key_cache.get(view_entity) else {
            continue;
        };

        let Some(view_tick) = view_specialization_ticks.get(view_entity) else {
            continue;
        };

        let view_specialized_material_pipeline_cache = specialized_material_pipeline_cache
            .entry(*view_entity)
            .or_default();

        for (_, view_entity) in visible_entities.iter::<Mesh2d>() {
            if !render_material_instances.contains_key(view_entity) {
                return;
            }

            let entity_tick = material_specialization_ticks.get(view_entity).unwrap();

            let last_specialized_tick = view_specialized_material_pipeline_cache
                .get(view_entity)
                .map(|(tick, _)| *tick);

            let needs_specialization = last_specialized_tick.is_none_or(|tick| {
                view_tick.is_newer_than(tick, ticks.this_run())
                    || entity_tick.is_newer_than(tick, ticks.this_run())
            });

            if !needs_specialization {
                continue;
            }

            let Some(mesh_instance) = render_mesh_instances.get_mut(view_entity) else {
                continue;
            };
            let Some(mesh) = render_meshes.get(mesh_instance.mesh_asset_id) else {
                continue;
            };

            let pipeline_id = mask_pipelines.specialize(
                &pipeline_cache,
                &mask_pipeline,
                *view_key | Mesh2dPipelineKey::from_primitive_topology(mesh.primitive_topology()),
                &mesh.layout,
            );
            let pipeline_id = match pipeline_id {
                Ok(id) => id,
                Err(err) => {
                    error!("{}", err);
                    continue;
                }
            };

            view_specialized_material_pipeline_cache
                .insert(*view_entity, (ticks.this_run(), pipeline_id));
        }
    }
}

pub fn queue_mask_meshes(
    mask_draw_functions: Res<DrawFunctions<VoronoiPhase>>,
    render_meshes: Res<RenderAssets<RenderMesh>>,
    mut render_mesh_instances: ResMut<RenderMesh2dInstances>,
    mut mask_render_phase: ResMut<ViewSortedRenderPhases<VoronoiPhase>>,
    views: Query<(&MainEntity, &ExtractedView, &RenderVisibleEntities)>,
    render_material_instances: Res<RenderVoronoiMaterials>,
    mut specialized_material_pipeline_cache: ResMut<
        SpecializedMaterial2dPipelineCache<LightOccluder2d>,
    >,
) {
    if render_material_instances.is_empty() {
        return;
    }

    for (view_entity, view, visible_entities) in &views {
        let Some(mask_phase) = mask_render_phase.get_mut(&view.retained_view_entity) else {
            continue;
        };

        let draw_mask_mesh = mask_draw_functions.read().id::<DrawMaskMesh>();

        let view_specialized_material_pipeline_cache = specialized_material_pipeline_cache
            .entry(*view_entity)
            .or_default();

        for (render_entity, view_entity) in visible_entities.iter::<Mesh2d>() {
            if !render_material_instances.contains_key(view_entity) {
                return;
            }

            let Some(mesh_instance) = render_mesh_instances.get_mut(view_entity) else {
                continue;
            };
            let Some(mesh) = render_meshes.get(mesh_instance.mesh_asset_id) else {
                continue;
            };

            let Some((_, pipeline_id)) = view_specialized_material_pipeline_cache.get(view_entity)
            else {
                continue;
            };

            mask_phase.add(VoronoiPhase {
                sort_key: FloatOrd(mesh_instance.transforms.world_from_local.translation.z),
                pipeline: *pipeline_id,
                draw_function: draw_mask_mesh,
                entity: (*render_entity, *view_entity),
                batch_range: 0..1,
                extra_index: PhaseItemExtraIndex::None,
                indexed: mesh.indexed(),
            });
        }
    }
}

#[derive(Resource, Deref, DerefMut, Default)]
pub struct MaskMaterialBindGroups(MainEntityHashMap<BindGroup>);

pub fn prepare_mask_material_bind_groups(
    render_device: Res<RenderDevice>,
    pipeline: Res<MaskPipeline>,
    images: Res<RenderAssets<GpuImage>>,
    fallback_image: Res<FallbackImage>,
    voronoi_materials: Res<RenderVoronoiMaterials>,
    mut bind_groups: ResMut<MaskMaterialBindGroups>,
) {
    // Only update bind groups for entities that have changed or are new
    bind_groups.retain(|entity, _| voronoi_materials.contains_key(entity));

    for (entity, alpha_mask) in voronoi_materials.iter() {
        let alpha_mask_image = if let Some(image) = images.get(*alpha_mask) {
            image
        } else {
            &fallback_image.d2
        };
        let sampler = render_device.create_sampler(&SamplerDescriptor::default());
        let bind_group = render_device.create_bind_group(
            "mask_material_bind_group",
            &pipeline.material_layout,
            &BindGroupEntries::sequential((&alpha_mask_image.texture_view, &sampler)),
        );
        bind_groups.insert(*entity, bind_group);
    }
}

pub type DrawMaskMesh = (
    SetItemPipeline,
    SetMesh2dViewBindGroup<0>,
    SetMesh2dBindGroup<1>,
    SetMaskMaterialBindGroup<2>,
    DrawMesh2d,
);

pub struct SetMaskMaterialBindGroup<const I: usize>;
impl<P: PhaseItem, const I: usize> RenderCommand<P> for SetMaskMaterialBindGroup<I> {
    type Param = SRes<MaskMaterialBindGroups>;
    type ViewQuery = ();
    type ItemQuery = ();

    #[inline]
    fn render<'w>(
        item: &P,
        _view: (),
        _item_query: Option<()>,
        bind_groups: SystemParamItem<'w, '_, Self::Param>,
        pass: &mut TrackedRenderPass<'w>,
    ) -> RenderCommandResult {
        let bind_groups = bind_groups.into_inner();
        let Some(bind_group) = bind_groups.get(&item.main_entity()) else {
            return RenderCommandResult::Skip;
        };
        pass.set_bind_group(I, &bind_group, &[]);
        RenderCommandResult::Success
    }
}

#[derive(Resource)]
pub struct FloodPipeline {
    pub seed_layout: BindGroupLayout,
    pub seed_pipeline: CachedRenderPipelineId,
    pub layout: BindGroupLayout,
    pub pipeline: CachedRenderPipelineId,
}

pub fn init_flood_pipeline(
    mut commands: Commands,
    render_device: Res<RenderDevice>,
    fullscreen_shader: Res<FullscreenShader>,
    pipeline_cache: Res<PipelineCache>,
    asset_server: Res<AssetServer>,
) {
    let seed_layout = render_device.create_bind_group_layout(
        "flood_seed_bind_group_layout",
        &BindGroupLayoutEntries::sequential(
            ShaderStages::FRAGMENT,
            (
                texture_2d(TextureSampleType::Float { filterable: true }),
                sampler(SamplerBindingType::Filtering),
            ),
        ),
    );

    let fullscreen_vertex_state = fullscreen_shader.to_vertex_state();

    let seed_pipeline = pipeline_cache.queue_render_pipeline(RenderPipelineDescriptor {
        label: Some("flood_seed_pipeline".into()),
        layout: vec![seed_layout.clone()],
        vertex: fullscreen_vertex_state.clone(),
        fragment: Some(FragmentState {
            shader: load_embedded_asset!(asset_server.as_ref(), "flood_seed.wgsl"),
            shader_defs: vec![],
            entry_point: Some("fragment".into()),
            targets: vec![Some(ColorTargetState {
                format: TextureFormat::Rgba16Float,
                blend: None,
                write_mask: ColorWrites::ALL,
            })],
        }),
        ..default()
    });

    let layout = render_device.create_bind_group_layout(
        "flood_bind_group_layout",
        &BindGroupLayoutEntries::sequential(
            ShaderStages::FRAGMENT,
            (
                texture_2d(TextureSampleType::Float { filterable: true }),
                sampler(SamplerBindingType::Filtering),
                uniform_buffer::<UVec2>(false),
            ),
        ),
    );

    let pipeline = pipeline_cache.queue_render_pipeline(RenderPipelineDescriptor {
        label: Some("flood_pipeline".into()),
        layout: vec![layout.clone()],
        vertex: fullscreen_vertex_state,
        fragment: Some(FragmentState {
            shader: load_embedded_asset!(asset_server.as_ref(), "flood.wgsl"),
            shader_defs: vec![],
            entry_point: Some("fragment".into()),
            targets: vec![Some(ColorTargetState {
                format: TextureFormat::Rgba16Float,
                blend: None,
                write_mask: ColorWrites::ALL,
            })],
        }),
        ..default()
    });

    commands.insert_resource(FloodPipeline {
        seed_pipeline,
        seed_layout,
        layout,
        pipeline,
    });
}