Skip to content

Commit 1c5ee80

Browse files
committed
Use RenderStartup for atmosphere rendering.
1 parent 877d278 commit 1c5ee80

File tree

2 files changed

+301
-283
lines changed

2 files changed

+301
-283
lines changed

crates/bevy_pbr/src/atmosphere/mod.rs

Lines changed: 92 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ use bevy_core_pipeline::core_3d::graph::Node3d;
4242
use bevy_ecs::{
4343
component::Component,
4444
query::{Changed, QueryItem, With},
45-
schedule::IntoScheduleConfigs,
46-
system::{lifetimeless::Read, Query},
45+
resource::Resource,
46+
schedule::{common_conditions::resource_exists, IntoScheduleConfigs, SystemSet},
47+
system::{lifetimeless::Read, Commands, Query, Res},
48+
world::World,
4749
};
4850
use bevy_math::{UVec2, UVec3, Vec3};
4951
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
@@ -52,6 +54,7 @@ use bevy_render::{
5254
load_shader_library,
5355
render_resource::{DownlevelFlags, ShaderType, SpecializedRenderPipelines},
5456
view::Hdr,
57+
RenderStartup,
5558
};
5659
use bevy_render::{
5760
extract_component::{ExtractComponent, ExtractComponentPlugin},
@@ -68,12 +71,14 @@ use resources::{
6871
};
6972
use tracing::warn;
7073

74+
use crate::resources::{
75+
init_atmosphere_bind_group_layouts, init_atmosphere_lut_pipelines, init_atmosphere_samplers,
76+
init_render_sky_bind_group_layouts,
77+
};
78+
7179
use self::{
7280
node::{AtmosphereLutsNode, AtmosphereNode, RenderSkyNode},
73-
resources::{
74-
prepare_atmosphere_bind_groups, prepare_atmosphere_textures, AtmosphereBindGroupLayouts,
75-
AtmosphereLutPipelines, AtmosphereSamplers,
76-
},
81+
resources::{prepare_atmosphere_bind_groups, prepare_atmosphere_textures},
7782
};
7883

7984
#[doc(hidden)]
@@ -100,40 +105,39 @@ impl Plugin for AtmospherePlugin {
100105
UniformComponentPlugin::<Atmosphere>::default(),
101106
UniformComponentPlugin::<AtmosphereSettings>::default(),
102107
));
103-
}
104108

105-
fn finish(&self, app: &mut App) {
106109
let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
107110
return;
108111
};
109112

110-
let render_adapter = render_app.world().resource::<RenderAdapter>();
111-
112-
if !render_adapter
113-
.get_downlevel_capabilities()
114-
.flags
115-
.contains(DownlevelFlags::COMPUTE_SHADERS)
116-
{
117-
warn!("AtmospherePlugin not loaded. GPU lacks support for compute shaders.");
118-
return;
119-
}
120-
121-
if !render_adapter
122-
.get_texture_format_features(TextureFormat::Rgba16Float)
123-
.allowed_usages
124-
.contains(TextureUsages::STORAGE_BINDING)
125-
{
126-
warn!("AtmospherePlugin not loaded. GPU lacks support: TextureFormat::Rgba16Float does not support TextureUsages::STORAGE_BINDING.");
127-
return;
128-
}
129-
130113
render_app
131-
.init_resource::<AtmosphereBindGroupLayouts>()
132-
.init_resource::<RenderSkyBindGroupLayouts>()
133-
.init_resource::<AtmosphereSamplers>()
134-
.init_resource::<AtmosphereLutPipelines>()
135114
.init_resource::<AtmosphereTransforms>()
136115
.init_resource::<SpecializedRenderPipelines<RenderSkyBindGroupLayouts>>()
116+
.configure_sets(
117+
RenderStartup,
118+
AtmosphereSystems
119+
.after(check_atmosphere_supported)
120+
.run_if(resource_exists::<AtmosphereSupported>),
121+
)
122+
.configure_sets(
123+
Render,
124+
AtmosphereSystems.run_if(resource_exists::<AtmosphereSupported>),
125+
)
126+
.add_systems(RenderStartup, check_atmosphere_supported)
127+
.add_systems(
128+
RenderStartup,
129+
(
130+
add_atmosphere_render_graph_nodes,
131+
init_render_sky_bind_group_layouts,
132+
init_atmosphere_samplers,
133+
(
134+
init_atmosphere_bind_group_layouts,
135+
init_atmosphere_lut_pipelines,
136+
)
137+
.chain(),
138+
)
139+
.in_set(AtmosphereSystems),
140+
)
137141
.add_systems(
138142
Render,
139143
(
@@ -142,36 +146,66 @@ impl Plugin for AtmospherePlugin {
142146
prepare_atmosphere_textures.in_set(RenderSystems::PrepareResources),
143147
prepare_atmosphere_transforms.in_set(RenderSystems::PrepareResources),
144148
prepare_atmosphere_bind_groups.in_set(RenderSystems::PrepareBindGroups),
145-
),
146-
)
147-
.add_render_graph_node::<ViewNodeRunner<AtmosphereLutsNode>>(
148-
Core3d,
149-
AtmosphereNode::RenderLuts,
150-
)
151-
.add_render_graph_edges(
152-
Core3d,
153-
(
154-
// END_PRE_PASSES -> RENDER_LUTS -> MAIN_PASS
155-
Node3d::EndPrepasses,
156-
AtmosphereNode::RenderLuts,
157-
Node3d::StartMainPass,
158-
),
159-
)
160-
.add_render_graph_node::<ViewNodeRunner<RenderSkyNode>>(
161-
Core3d,
162-
AtmosphereNode::RenderSky,
163-
)
164-
.add_render_graph_edges(
165-
Core3d,
166-
(
167-
Node3d::MainOpaquePass,
168-
AtmosphereNode::RenderSky,
169-
Node3d::MainTransparentPass,
170-
),
149+
)
150+
.in_set(AtmosphereSystems),
171151
);
172152
}
173153
}
174154

155+
#[derive(SystemSet, Hash, PartialEq, Eq, Clone, Debug)]
156+
struct AtmosphereSystems;
157+
158+
#[derive(Resource)]
159+
struct AtmosphereSupported;
160+
161+
fn check_atmosphere_supported(mut commands: Commands, render_adapter: Res<RenderAdapter>) {
162+
if !render_adapter
163+
.get_downlevel_capabilities()
164+
.flags
165+
.contains(DownlevelFlags::COMPUTE_SHADERS)
166+
{
167+
warn!("AtmospherePlugin not loaded. GPU lacks support for compute shaders.");
168+
return;
169+
}
170+
171+
if !render_adapter
172+
.get_texture_format_features(TextureFormat::Rgba16Float)
173+
.allowed_usages
174+
.contains(TextureUsages::STORAGE_BINDING)
175+
{
176+
warn!("AtmospherePlugin not loaded. GPU lacks support: TextureFormat::Rgba16Float does not support TextureUsages::STORAGE_BINDING.");
177+
return;
178+
}
179+
180+
commands.insert_resource(AtmosphereSupported);
181+
}
182+
183+
fn add_atmosphere_render_graph_nodes(world: &mut World) {
184+
world
185+
.add_render_graph_node::<ViewNodeRunner<AtmosphereLutsNode>>(
186+
Core3d,
187+
AtmosphereNode::RenderLuts,
188+
)
189+
.add_render_graph_edges(
190+
Core3d,
191+
(
192+
// END_PRE_PASSES -> RENDER_LUTS -> MAIN_PASS
193+
Node3d::EndPrepasses,
194+
AtmosphereNode::RenderLuts,
195+
Node3d::StartMainPass,
196+
),
197+
)
198+
.add_render_graph_node::<ViewNodeRunner<RenderSkyNode>>(Core3d, AtmosphereNode::RenderSky)
199+
.add_render_graph_edges(
200+
Core3d,
201+
(
202+
Node3d::MainOpaquePass,
203+
AtmosphereNode::RenderSky,
204+
Node3d::MainTransparentPass,
205+
),
206+
);
207+
}
208+
175209
/// This component describes the atmosphere of a planet, and when added to a camera
176210
/// will enable atmospheric scattering for that camera. This is only compatible with
177211
/// HDR cameras.

0 commit comments

Comments
 (0)