Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_solari/src/realtime/mod.rs
6596 views
1
mod extract;
2
mod node;
3
mod prepare;
4
5
use crate::SolariPlugins;
6
use bevy_app::{App, Plugin};
7
use bevy_asset::embedded_asset;
8
use bevy_core_pipeline::{
9
core_3d::graph::{Core3d, Node3d},
10
prepass::{DeferredPrepass, DepthPrepass, MotionVectorPrepass},
11
};
12
use bevy_ecs::{component::Component, reflect::ReflectComponent, schedule::IntoScheduleConfigs};
13
use bevy_pbr::DefaultOpaqueRendererMethod;
14
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
15
use bevy_render::{
16
render_graph::{RenderGraphExt, ViewNodeRunner},
17
renderer::RenderDevice,
18
view::Hdr,
19
ExtractSchedule, Render, RenderApp, RenderSystems,
20
};
21
use bevy_shader::load_shader_library;
22
use extract::extract_solari_lighting;
23
use node::SolariLightingNode;
24
use prepare::prepare_solari_lighting_resources;
25
use tracing::warn;
26
27
/// Raytraced direct and indirect lighting.
28
///
29
/// When using this plugin, it's highly recommended to set `shadows_enabled: false` on all lights, as Solari replaces
30
/// traditional shadow mapping.
31
pub struct SolariLightingPlugin;
32
33
impl Plugin for SolariLightingPlugin {
34
fn build(&self, app: &mut App) {
35
load_shader_library!(app, "presample_light_tiles.wgsl");
36
embedded_asset!(app, "restir_di.wgsl");
37
embedded_asset!(app, "restir_gi.wgsl");
38
load_shader_library!(app, "world_cache_query.wgsl");
39
embedded_asset!(app, "world_cache_compact.wgsl");
40
embedded_asset!(app, "world_cache_update.wgsl");
41
42
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
43
embedded_asset!(app, "resolve_dlss_rr_textures.wgsl");
44
45
app.insert_resource(DefaultOpaqueRendererMethod::deferred());
46
}
47
48
fn finish(&self, app: &mut App) {
49
let render_app = app.sub_app_mut(RenderApp);
50
51
let render_device = render_app.world().resource::<RenderDevice>();
52
let features = render_device.features();
53
if !features.contains(SolariPlugins::required_wgpu_features()) {
54
warn!(
55
"SolariLightingPlugin not loaded. GPU lacks support for required features: {:?}.",
56
SolariPlugins::required_wgpu_features().difference(features)
57
);
58
return;
59
}
60
61
render_app
62
.add_systems(ExtractSchedule, extract_solari_lighting)
63
.add_systems(
64
Render,
65
prepare_solari_lighting_resources.in_set(RenderSystems::PrepareResources),
66
)
67
.add_render_graph_node::<ViewNodeRunner<SolariLightingNode>>(
68
Core3d,
69
node::graph::SolariLightingNode,
70
)
71
.add_render_graph_edges(
72
Core3d,
73
(
74
Node3d::EndPrepasses,
75
node::graph::SolariLightingNode,
76
Node3d::EndMainPass,
77
),
78
);
79
}
80
}
81
82
/// A component for a 3d camera entity to enable the Solari raytraced lighting system.
83
///
84
/// Must be used with `CameraMainTextureUsages::default().with(TextureUsages::STORAGE_BINDING)`, and
85
/// `Msaa::Off`.
86
#[derive(Component, Reflect, Clone)]
87
#[reflect(Component, Default, Clone)]
88
#[require(Hdr, DeferredPrepass, DepthPrepass, MotionVectorPrepass)]
89
pub struct SolariLighting {
90
/// Set to true to delete the saved temporal history (past frames).
91
///
92
/// Useful for preventing ghosting when the history is no longer
93
/// representative of the current frame, such as in sudden camera cuts.
94
///
95
/// After setting this to true, it will automatically be toggled
96
/// back to false at the end of the frame.
97
pub reset: bool,
98
}
99
100
impl Default for SolariLighting {
101
fn default() -> Self {
102
Self {
103
reset: true, // No temporal history on the first frame
104
}
105
}
106
}
107
108