Use RenderStartup in custom_post_processing example (#19886)
# Objective - This example uses a FromWorld impl to initialize a resource on startup - #19887 ## Solution - Use RenderStartup instead ## Testing - The example still works as expected
This commit is contained in:
parent
83cd46a4dd
commit
735eb88db9
@ -27,7 +27,7 @@ use bevy::{
|
||||
},
|
||||
renderer::{RenderContext, RenderDevice},
|
||||
view::ViewTarget,
|
||||
RenderApp,
|
||||
RenderApp, RenderStartup,
|
||||
},
|
||||
};
|
||||
|
||||
@ -66,6 +66,10 @@ impl Plugin for PostProcessPlugin {
|
||||
return;
|
||||
};
|
||||
|
||||
// RenderStartup runs once on startup after all plugins are built
|
||||
// It is useful to initialize data that will only live in the RenderApp
|
||||
render_app.add_systems(RenderStartup, setup_pipeline);
|
||||
|
||||
render_app
|
||||
// Bevy's renderer uses a render graph which is a collection of nodes in a directed acyclic graph.
|
||||
// It currently runs on each view/camera and executes each node in the specified order.
|
||||
@ -97,17 +101,6 @@ impl Plugin for PostProcessPlugin {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
fn finish(&self, app: &mut App) {
|
||||
// We need to get the render app from the main app
|
||||
let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
|
||||
return;
|
||||
};
|
||||
|
||||
render_app
|
||||
// Initialize the pipeline
|
||||
.init_resource::<PostProcessPipeline>();
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Hash, PartialEq, Eq, Clone, RenderLabel)]
|
||||
@ -233,10 +226,13 @@ struct PostProcessPipeline {
|
||||
pipeline_id: CachedRenderPipelineId,
|
||||
}
|
||||
|
||||
impl FromWorld for PostProcessPipeline {
|
||||
fn from_world(world: &mut World) -> Self {
|
||||
let render_device = world.resource::<RenderDevice>();
|
||||
|
||||
fn setup_pipeline(
|
||||
mut commands: Commands,
|
||||
render_device: Res<RenderDevice>,
|
||||
asset_server: Res<AssetServer>,
|
||||
fullscreen_shader: Res<FullscreenShader>,
|
||||
pipeline_cache: Res<PipelineCache>,
|
||||
) {
|
||||
// We need to define the bind group layout used for our pipeline
|
||||
let layout = render_device.create_bind_group_layout(
|
||||
"post_process_bind_group_layout",
|
||||
@ -253,17 +249,14 @@ impl FromWorld for PostProcessPipeline {
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// We can create the sampler here since it won't change at runtime and doesn't depend on the view
|
||||
let sampler = render_device.create_sampler(&SamplerDescriptor::default());
|
||||
|
||||
// Get the shader handle
|
||||
let shader = world.load_asset(SHADER_ASSET_PATH);
|
||||
let shader = asset_server.load(SHADER_ASSET_PATH);
|
||||
// This will setup a fullscreen triangle for the vertex state.
|
||||
let vertex_state = world.resource::<FullscreenShader>().to_vertex_state();
|
||||
|
||||
let pipeline_id = world
|
||||
.resource_mut::<PipelineCache>()
|
||||
let vertex_state = fullscreen_shader.to_vertex_state();
|
||||
let pipeline_id = pipeline_cache
|
||||
// This will add the pipeline to the cache and queue its creation
|
||||
.queue_render_pipeline(RenderPipelineDescriptor {
|
||||
label: Some("post_process_pipeline".into()),
|
||||
@ -289,13 +282,11 @@ impl FromWorld for PostProcessPipeline {
|
||||
push_constant_ranges: vec![],
|
||||
zero_initialize_workgroup_memory: false,
|
||||
});
|
||||
|
||||
Self {
|
||||
commands.insert_resource(PostProcessPipeline {
|
||||
layout,
|
||||
sampler,
|
||||
pipeline_id,
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// This is the component that will get passed to the shader
|
||||
|
Loading…
Reference in New Issue
Block a user