Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

29.7 KB
Raw Blame
use super::prepare::{
    SolariLightingResources, LIGHT_TILE_BLOCKS, WORLD_CACHE_ACTIVE_CELLS_COUNT_OFFSET,
    WORLD_CACHE_SIZE,
};
use crate::scene::RaytracingSceneBindings;
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
use bevy_anti_alias::dlss::ViewDlssRayReconstructionTextures;
use bevy_asset::{load_embedded_asset, AssetServer, Handle};
use bevy_core_pipeline::prepass::{
    PreviousViewData, PreviousViewUniformOffset, PreviousViewUniforms, ViewPrepassTextures,
    MOTION_VECTOR_PREPASS_FORMAT,
};
use bevy_ecs::{prelude::*, resource::Resource, system::Commands};
use bevy_render::{
    diagnostic::RecordDiagnostics as _,
    render_resource::{
        binding_types::{
            storage_buffer_sized, texture_2d, texture_depth_2d, texture_storage_2d, uniform_buffer,
            uniform_buffer_sized,
        },
        BindGroupEntries, BindGroupLayoutDescriptor, BindGroupLayoutEntries,
        CachedComputePipelineId, ComputePassDescriptor, ComputePipelineDescriptor, LoadOp,
        PipelineCache, RenderPassDescriptor, ShaderStages, StorageTextureAccess, TextureFormat,
        TextureFormatFeatureFlags, TextureSampleType,
    },
    renderer::{RenderAdapter, RenderContext, RenderDevice, RenderQueue, ViewQuery},
    view::{ViewTarget, ViewUniform, ViewUniformOffset, ViewUniforms},
};
use bevy_shader::{Shader, ShaderDefVal};
use bevy_utils::default;

/// Resource holding the Solari lighting pipeline configuration.
#[derive(Resource)]
pub struct SolariLightingPipelines {
    bind_group_layout: BindGroupLayoutDescriptor,
    bind_group_layout_restir: BindGroupLayoutDescriptor,
    bind_group_layout_world_cache_active_cells_dispatch: BindGroupLayoutDescriptor,
    bind_group_layout_resolve_dlss_rr_textures: BindGroupLayoutDescriptor,
    decay_world_cache_pipeline: CachedComputePipelineId,
    compact_world_cache_single_block_pipeline: CachedComputePipelineId,
    compact_world_cache_blocks_pipeline: CachedComputePipelineId,
    compact_world_cache_write_active_cells_pipeline: CachedComputePipelineId,
    sample_di_for_world_cache_pipeline: CachedComputePipelineId,
    sample_gi_for_world_cache_pipeline: CachedComputePipelineId,
    blend_new_world_cache_samples_pipeline: CachedComputePipelineId,
    presample_light_tiles_pipeline: CachedComputePipelineId,
    restir: RestirPipelines,
    no_restir: NoRestirPipelines,
    // raea: these three only when a view can have guides (DLSS-RR's, or guides.rs's)
    resolve_dlss_rr_textures_pipeline: Option<CachedComputePipelineId>,
}

struct RestirPipelines {
    initial_and_temporal: CachedComputePipelineId,
    initial_and_temporal_with_psr: Option<CachedComputePipelineId>,
    spatial_and_shade: CachedComputePipelineId,
}

struct NoRestirPipelines {
    initial_and_shade: CachedComputePipelineId,
    initial_and_shade_with_psr: Option<CachedComputePipelineId>,
}

#[cfg(any(not(feature = "dlss"), feature = "force_disable_dlss"))]
type SolariLightingViewQuery = (
    &'static SolariLightingResources,
    &'static ViewTarget,
    &'static ViewPrepassTextures,
    &'static ViewUniformOffset,
    &'static PreviousViewUniformOffset,
    Option<&'static super::guides::SolariGuideTextures>,
    &'static super::SolariLighting,
);

#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
type SolariLightingViewQuery = (
    &'static SolariLightingResources,
    &'static ViewTarget,
    &'static ViewPrepassTextures,
    &'static ViewUniformOffset,
    &'static PreviousViewUniformOffset,
    Option<&'static ViewDlssRayReconstructionTextures>,
    Option<&'static super::guides::SolariGuideTextures>,
    &'static super::SolariLighting,
);

pub fn solari_lighting(
    view: ViewQuery<SolariLightingViewQuery>,
    solari_pipelines: Option<Res<SolariLightingPipelines>>,
    pipeline_cache: Res<PipelineCache>,
    scene_bindings: Res<RaytracingSceneBindings>,
    view_uniforms: Res<ViewUniforms>,
    previous_view_uniforms: Res<PreviousViewUniforms>,
    render_device: Res<RenderDevice>,
    render_queue: Res<RenderQueue>,
    mut ctx: RenderContext,
) {
    #[cfg(any(not(feature = "dlss"), feature = "force_disable_dlss"))]
    let (
        solari_lighting_resources,
        view_target,
        view_prepass_textures,
        view_uniform_offset,
        previous_view_uniform_offset,
        view_guides,
        settings,
    ) = view.into_inner();

    #[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
    let (
        solari_lighting_resources,
        view_target,
        view_prepass_textures,
        view_uniform_offset,
        previous_view_uniform_offset,
        view_dlss_rr_textures,
        view_guides,
        settings,
    ) = view.into_inner();

    // raea: DLSS-RR's guides, else guides.rs's, both written by the same passes
    #[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
    let guides = view_dlss_rr_textures.map(|d| {
        [&d.diffuse_albedo, &d.specular_albedo, &d.normal_roughness, &d.depth, &d.specular_motion_vectors]
    });

    #[cfg(any(not(feature = "dlss"), feature = "force_disable_dlss"))]
    let guides: Option<[&bevy_render::texture::CachedTexture; 5]> = None;
    let guides = guides.or(view_guides.map(|d| {
        [&d.diffuse_albedo, &d.specular_albedo, &d.normal_roughness, &d.depth, &d.specular_motion_vectors]
    }));

    // raea: a bake's earlier writes, submitted since, finished to their files
    super::bake::finish(render_device.wgpu_device());

    let Some(pipelines) = solari_pipelines else {
        return;
    };
    let guides = guides.filter(|_| pipelines.resolve_dlss_rr_textures_pipeline.is_some());

    let restir = solari_lighting_resources.reservoirs.as_ref().zip(
        view_prepass_textures
            .previous_deferred_view()
            .zip(view_prepass_textures.previous_depth_only_view()),
    );

    // a view with guides takes the pipelines that write them (primary surface replacement)
    let psr = |id: Option<CachedComputePipelineId>| id.filter(|_| guides.is_some());
    let (initial_pipeline_id, spatial_pipeline_id) = if restir.is_some() {
        (
            psr(pipelines.restir.initial_and_temporal_with_psr)
                .unwrap_or(pipelines.restir.initial_and_temporal),
            Some(pipelines.restir.spatial_and_shade),
        )
    } else {
        (
            psr(pipelines.no_restir.initial_and_shade_with_psr)
                .unwrap_or(pipelines.no_restir.initial_and_shade),
            None,
        )
    };

    let (
        Some(decay_world_cache_pipeline),
        Some(compact_world_cache_single_block_pipeline),
        Some(compact_world_cache_blocks_pipeline),
        Some(compact_world_cache_write_active_cells_pipeline),
        Some(sample_di_for_world_cache_pipeline),
        Some(sample_gi_for_world_cache_pipeline),
        Some(blend_new_world_cache_samples_pipeline),
        Some(presample_light_tiles_pipeline),
        Some(initial_pipeline),
        Some(scene_bind_group),
        Some(gbuffer),
        Some(depth_buffer),
        Some(motion_vectors),
        Some(view_uniforms_binding),
        Some(previous_view_uniforms_binding),
    ) = (
        pipeline_cache.get_compute_pipeline(pipelines.decay_world_cache_pipeline),
        pipeline_cache.get_compute_pipeline(pipelines.compact_world_cache_single_block_pipeline),
        pipeline_cache.get_compute_pipeline(pipelines.compact_world_cache_blocks_pipeline),
        pipeline_cache
            .get_compute_pipeline(pipelines.compact_world_cache_write_active_cells_pipeline),
        pipeline_cache.get_compute_pipeline(pipelines.sample_di_for_world_cache_pipeline),
        pipeline_cache.get_compute_pipeline(pipelines.sample_gi_for_world_cache_pipeline),
        pipeline_cache.get_compute_pipeline(pipelines.blend_new_world_cache_samples_pipeline),
        pipeline_cache.get_compute_pipeline(pipelines.presample_light_tiles_pipeline),
        pipeline_cache.get_compute_pipeline(initial_pipeline_id),
        &scene_bindings.bind_group,
        view_prepass_textures.deferred_view(),
        view_prepass_textures.depth_only_view(),
        view_prepass_textures.motion_vectors_view(),
        view_uniforms.uniforms.binding(),
        previous_view_uniforms.uniforms.binding(),
    )
    else {
        return;
    };

    let spatial_and_shade_pipeline = match spatial_pipeline_id {
        Some(id) => match pipeline_cache.get_compute_pipeline(id) {
            None => return,
            pipeline => pipeline,
        },
        None => None,
    };

    let resolve_dlss_rr_textures_pipeline = match (guides, pipelines.resolve_dlss_rr_textures_pipeline) {
        (Some(_), Some(id)) => match pipeline_cache.get_compute_pipeline(id) {
            None => return,
            pipeline => pipeline,
        },
        _ => None,
    };

    // Past every bail-out, so this view's reservoirs will be brought up to this frame's light ids
    if restir.is_some() {
        scene_bindings.note_light_translations_consumed();
    }

    let view_target_attachment = view_target.get_unsampled_color_attachment();

    let s = solari_lighting_resources;
    let bind_group = render_device.create_bind_group(
        "solari_lighting_bind_group",
        &pipeline_cache.get_bind_group_layout(&pipelines.bind_group_layout),
        &BindGroupEntries::sequential((
            view_target_attachment.view,
            s.light_tile_samples.as_entire_binding(),
            s.light_tile_resolved_samples.as_entire_binding(),
            gbuffer,
            depth_buffer,
            motion_vectors,
            view_uniforms_binding.clone(),
            previous_view_uniforms_binding.clone(),
            s.world_cache.as_entire_binding(),
            s.constants.as_entire_binding(),
        )),
    );

    let bind_group_restir =
        restir.map(|(reservoirs, (previous_gbuffer, previous_depth_buffer))| {
            render_device.create_bind_group(
                "solari_lighting_bind_group_restir",
                &pipeline_cache.get_bind_group_layout(&pipelines.bind_group_layout_restir),
                &BindGroupEntries::sequential((
                    view_target_attachment.view,
                    s.light_tile_samples.as_entire_binding(),
                    s.light_tile_resolved_samples.as_entire_binding(),
                    gbuffer,
                    depth_buffer,
                    motion_vectors,
                    view_uniforms_binding,
                    previous_view_uniforms_binding,
                    s.world_cache.as_entire_binding(),
                    s.constants.as_entire_binding(),
                    previous_gbuffer,
                    previous_depth_buffer,
                    reservoirs.a.as_entire_binding(),
                    reservoirs.b.as_entire_binding(),
                )),
            )
        });

    let bind_group_world_cache_active_cells_dispatch = render_device.create_bind_group(
        "solari_lighting_bind_group_world_cache_active_cells_dispatch",
        &pipeline_cache
            .get_bind_group_layout(&pipelines.bind_group_layout_world_cache_active_cells_dispatch),
        &BindGroupEntries::single(s.world_cache_active_cells_dispatch.as_entire_binding()),
    );

    let bind_group_resolve_dlss_rr_textures = guides.map(|d| {
        render_device.create_bind_group(
            "solari_lighting_bind_group_resolve_dlss_rr_textures",
            &pipeline_cache
                .get_bind_group_layout(&pipelines.bind_group_layout_resolve_dlss_rr_textures),
            &BindGroupEntries::sequential((
                &d[0].default_view,
                &d[1].default_view,
                &d[2].default_view,
                &d[3].default_view,
                &d[4].default_view,
            )),
        )
    });

    let diagnostics = ctx.diagnostic_recorder();
    let diagnostics = diagnostics.as_deref();

    let command_encoder = ctx.command_encoder();

    // Clear the view target if we're the first node to write to it
    if matches!(view_target_attachment.ops.load, LoadOp::Clear(_)) {
        command_encoder.begin_render_pass(&RenderPassDescriptor {
            label: Some("solari_lighting_clear"),
            color_attachments: &[Some(view_target_attachment)],
            depth_stencil_attachment: None,
            timestamp_writes: None,
            occlusion_query_set: None,
            multiview_mask: None,
        });
    }

    // raea: one compute pass a stage instead of one for all, so RAEA_SOLARI_TIMES=1 can time each at its boundaries
    // (times.rs); bind groups 0 and 1 are set again in each
    let times = super::times::set(render_device.wgpu_device());
    macro_rules! stage {
        ($label:expr, $i:expr) => {{
            let mut pass = command_encoder.begin_compute_pass(&ComputePassDescriptor {
                label: Some($label),
                timestamp_writes: super::times::writes(times, $i),
            });
            pass.set_bind_group(0, scene_bind_group, &[]);
            pass.set_bind_group(
                1,
                &bind_group,
                &[
                    view_uniform_offset.offset,
                    previous_view_uniform_offset.offset,
                ],
            );
            pass
        }};
    }
    let mut pass = stage!("solari_lighting/light_tiles", 0);

    let dx = solari_lighting_resources.view_size.x.div_ceil(8);
    let dy = solari_lighting_resources.view_size.y.div_ceil(8);

    if let (Some(bind_group_resolve_dlss_rr_textures), Some(resolve_dlss_rr_textures_pipeline)) =
        (&bind_group_resolve_dlss_rr_textures, resolve_dlss_rr_textures_pipeline)
    {
        pass.set_bind_group(2, bind_group_resolve_dlss_rr_textures, &[]);
        pass.set_pipeline(resolve_dlss_rr_textures_pipeline);
        pass.dispatch_workgroups(dx, dy, 1);
    }

    let d = diagnostics.time_span(&mut pass, "solari_lighting/presample_light_tiles");
    pass.set_pipeline(presample_light_tiles_pipeline);
    pass.dispatch_workgroups(LIGHT_TILE_BLOCKS as u32, 1, 1);
    d.end(&mut pass);
    drop(pass);

    // raea: a baked cache (SolariLighting::world_cache_load) is held: no decay, no compaction, no update, so neither
    // time nor a reset changes it. A bake (world_cache_keep) skips the decay but on a reset, so every cell its camera
    // path meets stays, and runs the update world_cache_update_iterations times
    let frozen = !settings.world_cache_load.is_empty();
    let mut pass = stage!("solari_lighting/world_cache", 1);
    let d = diagnostics.time_span(&mut pass, "solari_lighting/world_cache");
    if !frozen {
    pass.set_bind_group(2, &bind_group_world_cache_active_cells_dispatch, &[]);

    if !settings.world_cache_keep || settings.reset {
        pass.set_pipeline(decay_world_cache_pipeline);
        pass.dispatch_workgroups((WORLD_CACHE_SIZE / 1024) as u32, 1, 1);
    }

    pass.set_pipeline(compact_world_cache_single_block_pipeline);
    pass.dispatch_workgroups((WORLD_CACHE_SIZE / 1024) as u32, 1, 1);

    pass.set_pipeline(compact_world_cache_blocks_pipeline);
    pass.dispatch_workgroups(1, 1, 1);

    pass.set_pipeline(compact_world_cache_write_active_cells_pipeline);
    pass.dispatch_workgroups((WORLD_CACHE_SIZE / 1024) as u32, 1, 1);

    pass.set_bind_group(2, None, &[]);

    for _ in 0..settings.world_cache_update_iterations.max(1) {
        pass.set_pipeline(sample_di_for_world_cache_pipeline);
        pass.dispatch_workgroups_indirect(
            &solari_lighting_resources.world_cache_active_cells_dispatch,
            0,
        );

        pass.set_pipeline(sample_gi_for_world_cache_pipeline);
        pass.dispatch_workgroups_indirect(
            &solari_lighting_resources.world_cache_active_cells_dispatch,
            0,
        );

        pass.set_pipeline(blend_new_world_cache_samples_pipeline);
        pass.dispatch_workgroups_indirect(
            &solari_lighting_resources.world_cache_active_cells_dispatch,
            0,
        );
    }
    }

    d.end(&mut pass);
    drop(pass);

    // raea: a bake's write (SolariLighting::world_cache_save): the cache copied out at the end of this frame's update
    // and written to the file by a later frame, once this one has been submitted (bake::finish)
    if !settings.world_cache_save.is_empty() {
        super::bake::save(render_device.wgpu_device(), command_encoder, &solari_lighting_resources.world_cache, &settings.world_cache_save);
    }

    let mut pass = stage!("solari_lighting/initial", 2);
    let d = diagnostics.time_span(&mut pass, "solari_lighting/lighting");

    if let Some(bind_group_restir) = &bind_group_restir {
        pass.set_bind_group(
            1,
            bind_group_restir,
            &[
                view_uniform_offset.offset,
                previous_view_uniform_offset.offset,
            ],
        );
    }

    if let Some(bind_group_resolve_dlss_rr_textures) = &bind_group_resolve_dlss_rr_textures {
        pass.set_bind_group(2, bind_group_resolve_dlss_rr_textures, &[]);
    }
    pass.set_pipeline(initial_pipeline);
    pass.dispatch_workgroups(dx, dy, 1);
    d.end(&mut pass);
    drop(pass);

    if let Some(spatial_and_shade_pipeline) = spatial_and_shade_pipeline {
        let mut pass = stage!("solari_lighting/spatial_and_shade", 3);
        if let Some(bind_group_restir) = &bind_group_restir {
            pass.set_bind_group(
                1,
                bind_group_restir,
                &[
                    view_uniform_offset.offset,
                    previous_view_uniform_offset.offset,
                ],
            );
        }
        if let Some(bind_group_resolve_dlss_rr_textures) = &bind_group_resolve_dlss_rr_textures {
            pass.set_bind_group(2, bind_group_resolve_dlss_rr_textures, &[]);
        }
        let d = diagnostics.time_span(&mut pass, "solari_lighting/spatial_and_shade");
        pass.set_pipeline(spatial_and_shade_pipeline);
        pass.dispatch_workgroups(dx, dy, 1);
        d.end(&mut pass);
    }
    if times.is_some() {
        super::times::after(render_device.wgpu_device(), ctx.command_encoder(), render_queue.get_timestamp_period());
    }

    // Active cell count readback.
    diagnostics.record_u32(
        ctx.command_encoder(),
        &s.world_cache.slice(
            WORLD_CACHE_ACTIVE_CELLS_COUNT_OFFSET
                ..WORLD_CACHE_ACTIVE_CELLS_COUNT_OFFSET + size_of::<u32>() as u64,
        ),
        "solari_lighting/world_cache_active_cells_count",
    );
}

/// Initializes the Solari lighting pipelines at render startup.
pub fn init_solari_lighting_pipelines(
    mut commands: Commands,
    pipeline_cache: Res<PipelineCache>,
    scene_bindings: Res<RaytracingSceneBindings>,
    asset_server: Res<AssetServer>,
    render_adapter: Res<RenderAdapter>,
    guides: Option<Res<super::guides::SolariGuideBuffers>>,
) {
    // raea: the pipelines that write the guides, for DLSS-RR or for guides.rs's
    let psr = guides.is_some() || cfg!(all(feature = "dlss", not(feature = "force_disable_dlss")));
    let guides_rs = guides.is_some_and(|g| g.reflection_depth);
    let motion_vectors_storage_read_write = render_adapter
        .get_texture_format_features(MOTION_VECTOR_PREPASS_FORMAT)
        .flags
        .contains(TextureFormatFeatureFlags::STORAGE_READ_WRITE);
    let motion_vectors_binding = if motion_vectors_storage_read_write {
        texture_storage_2d(TextureFormat::Rg16Float, StorageTextureAccess::ReadWrite)
    } else {
        texture_2d(TextureSampleType::Float { filterable: false })
    };

    let bind_group_layout = BindGroupLayoutDescriptor::new(
        "solari_lighting_bind_group_layout",
        &BindGroupLayoutEntries::sequential(
            ShaderStages::COMPUTE,
            (
                texture_storage_2d(TextureFormat::Rgba16Float, StorageTextureAccess::ReadWrite),
                storage_buffer_sized(false, None),
                storage_buffer_sized(false, None),
                texture_2d(TextureSampleType::Uint),
                texture_depth_2d(),
                motion_vectors_binding,
                uniform_buffer::<ViewUniform>(true),
                uniform_buffer::<PreviousViewData>(true),
                storage_buffer_sized(false, None),
                uniform_buffer_sized(false, None),
            ),
        ),
    );

    let bind_group_layout_restir = BindGroupLayoutDescriptor::new(
        "solari_lighting_bind_group_layout_restir",
        &BindGroupLayoutEntries::sequential(
            ShaderStages::COMPUTE,
            (
                texture_storage_2d(TextureFormat::Rgba16Float, StorageTextureAccess::ReadWrite),
                storage_buffer_sized(false, None),
                storage_buffer_sized(false, None),
                texture_2d(TextureSampleType::Uint),
                texture_depth_2d(),
                motion_vectors_binding,
                uniform_buffer::<ViewUniform>(true),
                uniform_buffer::<PreviousViewData>(true),
                storage_buffer_sized(false, None),
                uniform_buffer_sized(false, None),
                texture_2d(TextureSampleType::Uint),
                texture_depth_2d(),
                storage_buffer_sized(false, None),
                storage_buffer_sized(false, None),
            ),
        ),
    );

    let bind_group_layout_world_cache_active_cells_dispatch = BindGroupLayoutDescriptor::new(
        "solari_lighting_bind_group_layout_world_cache_active_cells_dispatch",
        &BindGroupLayoutEntries::single(ShaderStages::COMPUTE, storage_buffer_sized(false, None)),
    );

    let bind_group_layout_resolve_dlss_rr_textures = BindGroupLayoutDescriptor::new(
        "solari_lighting_bind_group_layout_resolve_dlss_rr_textures",
        &BindGroupLayoutEntries::sequential(
            ShaderStages::COMPUTE,
            (
                texture_storage_2d(TextureFormat::Rgba8Unorm, StorageTextureAccess::WriteOnly),
                texture_storage_2d(TextureFormat::Rgba8Unorm, StorageTextureAccess::WriteOnly),
                texture_storage_2d(TextureFormat::Rgba16Float, StorageTextureAccess::WriteOnly),
                texture_storage_2d(TextureFormat::R32Float, StorageTextureAccess::WriteOnly),
                texture_storage_2d(TextureFormat::Rg16Float, StorageTextureAccess::WriteOnly),
            ),
        ),
    );

    let create_pipeline = |label: &'static str,
                           entry_point: &'static str,
                           shader: Handle<Shader>,
                           restir: bool,
                           extra_bind_group: ExtraBindGroup,
                           extra_shader_defs: Vec<ShaderDefVal>| {
        let group_1 = if restir {
            &bind_group_layout_restir
        } else {
            &bind_group_layout
        };
        let mut layout = vec![scene_bindings.bind_group_layout.clone(), group_1.clone()];
        match extra_bind_group {
            ExtraBindGroup::None => {}
            ExtraBindGroup::WorldCacheDispatch => {
                layout.push(bind_group_layout_world_cache_active_cells_dispatch.clone());
            }
            ExtraBindGroup::DlssRrGuideBuffers => {
                layout.push(bind_group_layout_resolve_dlss_rr_textures.clone());
            }
        }

        let mut shader_defs = vec![ShaderDefVal::UInt(
            "WORLD_CACHE_SIZE".into(),
            WORLD_CACHE_SIZE as u32,
        )];
        if motion_vectors_storage_read_write {
            shader_defs.push("MOTION_VECTORS_READ_WRITE".into());
        }
        if restir {
            shader_defs.push("RESTIR".into());
        }
        match extra_bind_group {
            ExtraBindGroup::None => {}
            ExtraBindGroup::WorldCacheDispatch => {
                shader_defs.push("WORLD_CACHE_NON_ATOMIC_LIFE_BUFFER".into());
            }
            ExtraBindGroup::DlssRrGuideBuffers => {
                shader_defs.push("DLSS_RR_GUIDE_BUFFERS".into());
                // raea: guides.rs's guides (MetalFX's) take one depth a pixel, so a pixel moving as its reflection does
                // is as far off as it (resolve_dlss_rr_textures.wesl); DLSS-RR has a specular motion of its own
                if guides_rs {
                    shader_defs.push("PSR_DEPTH_FOLLOWS_MOTION".into());
                }
            }
        }
        shader_defs.extend_from_slice(&extra_shader_defs);

        pipeline_cache.queue_compute_pipeline(ComputePipelineDescriptor {
            label: Some(label.into()),
            layout,
            shader,
            shader_defs,
            entry_point: Some(entry_point.into()),
            ..default()
        })
    };

    commands.insert_resource(SolariLightingPipelines {
        bind_group_layout: bind_group_layout.clone(),
        bind_group_layout_restir: bind_group_layout_restir.clone(),
        bind_group_layout_world_cache_active_cells_dispatch:
            bind_group_layout_world_cache_active_cells_dispatch.clone(),
        bind_group_layout_resolve_dlss_rr_textures: bind_group_layout_resolve_dlss_rr_textures
            .clone(),
        decay_world_cache_pipeline: create_pipeline(
            "solari_lighting_decay_world_cache_pipeline",
            "decay_world_cache",
            load_embedded_asset!(asset_server.as_ref(), "world_cache_compact.wesl"),
            false,
            ExtraBindGroup::WorldCacheDispatch,
            vec![],
        ),
        compact_world_cache_single_block_pipeline: create_pipeline(
            "solari_lighting_compact_world_cache_single_block_pipeline",
            "compact_world_cache_single_block",
            load_embedded_asset!(asset_server.as_ref(), "world_cache_compact.wesl"),
            false,
            ExtraBindGroup::WorldCacheDispatch,
            vec![],
        ),
        compact_world_cache_blocks_pipeline: create_pipeline(
            "solari_lighting_compact_world_cache_blocks_pipeline",
            "compact_world_cache_blocks",
            load_embedded_asset!(asset_server.as_ref(), "world_cache_compact.wesl"),
            false,
            ExtraBindGroup::WorldCacheDispatch,
            vec![],
        ),
        compact_world_cache_write_active_cells_pipeline: create_pipeline(
            "solari_lighting_compact_world_cache_write_active_cells_pipeline",
            "compact_world_cache_write_active_cells",
            load_embedded_asset!(asset_server.as_ref(), "world_cache_compact.wesl"),
            false,
            ExtraBindGroup::WorldCacheDispatch,
            vec![],
        ),
        sample_di_for_world_cache_pipeline: create_pipeline(
            "solari_lighting_sample_di_for_world_cache_pipeline",
            "sample_di",
            load_embedded_asset!(asset_server.as_ref(), "world_cache_update.wesl"),
            false,
            ExtraBindGroup::None,
            vec![],
        ),
        sample_gi_for_world_cache_pipeline: create_pipeline(
            "solari_lighting_sample_gi_for_world_cache_pipeline",
            "sample_gi",
            load_embedded_asset!(asset_server.as_ref(), "world_cache_update.wesl"),
            false,
            ExtraBindGroup::None,
            vec!["WORLD_CACHE_QUERY_ATOMIC_MAX_LIFETIME".into()],
        ),
        blend_new_world_cache_samples_pipeline: create_pipeline(
            "solari_lighting_blend_new_world_cache_samples_pipeline",
            "blend_new_samples",
            load_embedded_asset!(asset_server.as_ref(), "world_cache_update.wesl"),
            false,
            ExtraBindGroup::None,
            vec![],
        ),
        presample_light_tiles_pipeline: create_pipeline(
            "solari_lighting_presample_light_tiles_pipeline",
            "presample_light_tiles",
            load_embedded_asset!(asset_server.as_ref(), "presample_light_tiles.wesl"),
            false,
            ExtraBindGroup::None,
            vec![],
        ),
        restir: RestirPipelines {
            initial_and_temporal: create_pipeline(
                "solari_lighting_initial_and_temporal_pipeline",
                "initial_and_temporal",
                load_embedded_asset!(asset_server.as_ref(), "restir.wesl"),
                true,
                ExtraBindGroup::None,
                vec![],
            ),
            initial_and_temporal_with_psr: psr.then(|| {
                create_pipeline(
                    "solari_lighting_initial_and_temporal_with_psr_pipeline",
                    "initial_and_temporal",
                    load_embedded_asset!(asset_server.as_ref(), "restir.wesl"),
                    true,
                    ExtraBindGroup::DlssRrGuideBuffers,
                    vec![],
                )
            }),
            spatial_and_shade: create_pipeline(
                "solari_lighting_spatial_and_shade_pipeline",
                "spatial_and_shade",
                load_embedded_asset!(asset_server.as_ref(), "restir.wesl"),
                true,
                ExtraBindGroup::None,
                vec!["SPATIAL_MERGE".into()],
            ),
        },
        no_restir: NoRestirPipelines {
            initial_and_shade: create_pipeline(
                "solari_lighting_initial_and_shade_pipeline",
                "initial_and_shade",
                load_embedded_asset!(asset_server.as_ref(), "no_restir.wesl"),
                false,
                ExtraBindGroup::None,
                vec![],
            ),
            initial_and_shade_with_psr: psr.then(|| {
                create_pipeline(
                    "solari_lighting_initial_and_shade_with_psr_pipeline",
                    "initial_and_shade",
                    load_embedded_asset!(asset_server.as_ref(), "no_restir.wesl"),
                    false,
                    ExtraBindGroup::DlssRrGuideBuffers,
                    vec![],
                )
            }),
        },
        resolve_dlss_rr_textures_pipeline: psr.then(|| {
            create_pipeline(
                "solari_lighting_resolve_dlss_rr_textures_pipeline",
                "resolve_dlss_rr_textures",
                load_embedded_asset!(asset_server.as_ref(), "resolve_dlss_rr_textures.wesl"),
                false,
                ExtraBindGroup::DlssRrGuideBuffers,
                vec![],
            )
        }),
    });
}

enum ExtraBindGroup {
    None,
    WorldCacheDispatch,
    DlssRrGuideBuffers,
}