Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_solari/src/realtime/mod.rs
9427 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_camera::Hdr;
9
use bevy_core_pipeline::{
10
prepass::{
11
DeferredPrepass, DeferredPrepassDoubleBuffer, DepthPrepass, DepthPrepassDoubleBuffer,
12
MotionVectorPrepass,
13
},
14
schedule::{Core3d, Core3dSystems},
15
};
16
use bevy_ecs::{component::Component, reflect::ReflectComponent, schedule::IntoScheduleConfigs};
17
use bevy_pbr::DefaultOpaqueRendererMethod;
18
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
19
use bevy_render::{
20
renderer::RenderDevice, ExtractSchedule, Render, RenderApp, RenderStartup, RenderSystems,
21
};
22
use bevy_shader::load_shader_library;
23
use extract::extract_solari_lighting;
24
use node::{init_solari_lighting_pipelines, solari_lighting};
25
use prepare::prepare_solari_lighting_resources;
26
use tracing::warn;
27
28
/// Raytraced direct and indirect lighting.
29
///
30
/// When using this plugin, it's highly recommended to set `shadow_maps_enabled: false` on all lights, as Solari replaces
31
/// traditional shadow mapping.
32
pub struct SolariLightingPlugin;
33
34
impl Plugin for SolariLightingPlugin {
35
fn build(&self, app: &mut App) {
36
load_shader_library!(app, "gbuffer_utils.wgsl");
37
load_shader_library!(app, "realtime_bindings.wgsl");
38
load_shader_library!(app, "presample_light_tiles.wgsl");
39
embedded_asset!(app, "restir_di.wgsl");
40
embedded_asset!(app, "restir_gi.wgsl");
41
load_shader_library!(app, "specular_gi.wgsl");
42
load_shader_library!(app, "world_cache_query.wgsl");
43
embedded_asset!(app, "world_cache_compact.wgsl");
44
embedded_asset!(app, "world_cache_update.wgsl");
45
46
load_shader_library!(app, "resolve_dlss_rr_textures.wgsl");
47
48
app.insert_resource(DefaultOpaqueRendererMethod::deferred());
49
}
50
51
fn finish(&self, app: &mut App) {
52
let render_app = app.sub_app_mut(RenderApp);
53
54
let render_device = render_app.world().resource::<RenderDevice>();
55
let features = render_device.features();
56
if !features.contains(SolariPlugins::required_wgpu_features()) {
57
warn!(
58
"SolariLightingPlugin not loaded. GPU lacks support for required features: {:?}.",
59
SolariPlugins::required_wgpu_features().difference(features)
60
);
61
return;
62
}
63
64
render_app
65
.add_systems(RenderStartup, init_solari_lighting_pipelines)
66
.add_systems(ExtractSchedule, extract_solari_lighting)
67
.add_systems(
68
Render,
69
prepare_solari_lighting_resources.in_set(RenderSystems::PrepareResources),
70
)
71
.add_systems(Core3d, solari_lighting.in_set(Core3dSystems::MainPass));
72
}
73
}
74
75
/// A component for a 3d camera entity to enable the Solari raytraced lighting system.
76
///
77
/// Must be used with `CameraMainTextureUsages::default().with(TextureUsages::STORAGE_BINDING)`, and
78
/// `Msaa::Off`.
79
#[derive(Component, Reflect, Clone)]
80
#[reflect(Component, Default, Clone)]
81
#[require(
82
Hdr,
83
DeferredPrepass,
84
DepthPrepass,
85
MotionVectorPrepass,
86
DeferredPrepassDoubleBuffer,
87
DepthPrepassDoubleBuffer
88
)]
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