Convert basic example cases to use RenderStartup
.
This commit is contained in:
parent
33bed5dd70
commit
5c7dd2eec3
@ -27,7 +27,7 @@ use bevy::{
|
||||
sync_component::SyncComponentPlugin,
|
||||
sync_world::{MainEntityHashMap, RenderEntity},
|
||||
view::{ExtractedView, RenderVisibleEntities, ViewTarget},
|
||||
Extract, Render, RenderApp, RenderSystems,
|
||||
Extract, Render, RenderApp, RenderStartup, RenderSystems,
|
||||
},
|
||||
sprite::{
|
||||
extract_mesh2d, DrawMesh2d, Material2dBindGroupId, Mesh2dPipeline, Mesh2dPipelineKey,
|
||||
@ -132,14 +132,16 @@ pub struct ColoredMesh2dPipeline {
|
||||
shader: Handle<Shader>,
|
||||
}
|
||||
|
||||
impl FromWorld for ColoredMesh2dPipeline {
|
||||
fn from_world(world: &mut World) -> Self {
|
||||
Self {
|
||||
mesh2d_pipeline: Mesh2dPipeline::from_world(world),
|
||||
// Get the shader from the shader resource we inserted in the plugin.
|
||||
shader: world.resource::<ColoredMesh2dShader>().0.clone(),
|
||||
}
|
||||
}
|
||||
fn init_colored_mesh_2d_pipeline(
|
||||
mut commands: Commands,
|
||||
mesh2d_pipeline: Res<Mesh2dPipeline>,
|
||||
colored_mesh2d_shader: Res<ColoredMesh2dShader>,
|
||||
) {
|
||||
commands.insert_resource(ColoredMesh2dPipeline {
|
||||
mesh2d_pipeline: mesh2d_pipeline.clone(),
|
||||
// Clone the shader from the shader resource we inserted in the plugin.
|
||||
shader: colored_mesh2d_shader.0.clone(),
|
||||
});
|
||||
}
|
||||
|
||||
// We implement `SpecializedPipeline` to customize the default rendering from `Mesh2dPipeline`
|
||||
@ -307,6 +309,7 @@ impl Plugin for ColoredMesh2dPlugin {
|
||||
.add_render_command::<Transparent2d, DrawColoredMesh2d>()
|
||||
.init_resource::<SpecializedRenderPipelines<ColoredMesh2dPipeline>>()
|
||||
.init_resource::<RenderColoredMesh2dInstances>()
|
||||
.add_systems(RenderStartup, init_colored_mesh_2d_pipeline)
|
||||
.add_systems(
|
||||
ExtractSchedule,
|
||||
extract_colored_mesh2d.after(extract_mesh2d),
|
||||
@ -316,13 +319,6 @@ impl Plugin for ColoredMesh2dPlugin {
|
||||
queue_colored_mesh2d.in_set(RenderSystems::QueueMeshes),
|
||||
);
|
||||
}
|
||||
|
||||
fn finish(&self, app: &mut App) {
|
||||
// Register our custom pipeline
|
||||
app.get_sub_app_mut(RenderApp)
|
||||
.unwrap()
|
||||
.init_resource::<ColoredMesh2dPipeline>();
|
||||
}
|
||||
}
|
||||
|
||||
/// Extract the [`ColoredMesh2d`] marker component into the render app
|
||||
|
@ -12,7 +12,7 @@ use bevy::{
|
||||
render_resource::{binding_types::texture_storage_2d, *},
|
||||
renderer::{RenderContext, RenderDevice},
|
||||
texture::GpuImage,
|
||||
Render, RenderApp, RenderSystems,
|
||||
Render, RenderApp, RenderStartup, RenderSystems,
|
||||
},
|
||||
};
|
||||
use std::borrow::Cow;
|
||||
@ -103,20 +103,17 @@ impl Plugin for GameOfLifeComputePlugin {
|
||||
// for operation on by the compute shader and display on the sprite.
|
||||
app.add_plugins(ExtractResourcePlugin::<GameOfLifeImages>::default());
|
||||
let render_app = app.sub_app_mut(RenderApp);
|
||||
render_app.add_systems(
|
||||
Render,
|
||||
prepare_bind_group.in_set(RenderSystems::PrepareBindGroups),
|
||||
);
|
||||
render_app
|
||||
.add_systems(RenderStartup, init_game_of_life_pipeline)
|
||||
.add_systems(
|
||||
Render,
|
||||
prepare_bind_group.in_set(RenderSystems::PrepareBindGroups),
|
||||
);
|
||||
|
||||
let mut render_graph = render_app.world_mut().resource_mut::<RenderGraph>();
|
||||
render_graph.add_node(GameOfLifeLabel, GameOfLifeNode::default());
|
||||
render_graph.add_node_edge(GameOfLifeLabel, bevy::render::graph::CameraDriverLabel);
|
||||
}
|
||||
|
||||
fn finish(&self, app: &mut App) {
|
||||
let render_app = app.sub_app_mut(RenderApp);
|
||||
render_app.init_resource::<GameOfLifePipeline>();
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Resource, Clone, ExtractResource)]
|
||||
@ -157,40 +154,41 @@ struct GameOfLifePipeline {
|
||||
update_pipeline: CachedComputePipelineId,
|
||||
}
|
||||
|
||||
impl FromWorld for GameOfLifePipeline {
|
||||
fn from_world(world: &mut World) -> Self {
|
||||
let render_device = world.resource::<RenderDevice>();
|
||||
let texture_bind_group_layout = render_device.create_bind_group_layout(
|
||||
"GameOfLifeImages",
|
||||
&BindGroupLayoutEntries::sequential(
|
||||
ShaderStages::COMPUTE,
|
||||
(
|
||||
texture_storage_2d(TextureFormat::R32Float, StorageTextureAccess::ReadOnly),
|
||||
texture_storage_2d(TextureFormat::R32Float, StorageTextureAccess::WriteOnly),
|
||||
),
|
||||
fn init_game_of_life_pipeline(
|
||||
mut commands: Commands,
|
||||
render_device: Res<RenderDevice>,
|
||||
asset_server: Res<AssetServer>,
|
||||
pipeline_cache: Res<PipelineCache>,
|
||||
) {
|
||||
let texture_bind_group_layout = render_device.create_bind_group_layout(
|
||||
"GameOfLifeImages",
|
||||
&BindGroupLayoutEntries::sequential(
|
||||
ShaderStages::COMPUTE,
|
||||
(
|
||||
texture_storage_2d(TextureFormat::R32Float, StorageTextureAccess::ReadOnly),
|
||||
texture_storage_2d(TextureFormat::R32Float, StorageTextureAccess::WriteOnly),
|
||||
),
|
||||
);
|
||||
let shader = world.load_asset(SHADER_ASSET_PATH);
|
||||
let pipeline_cache = world.resource::<PipelineCache>();
|
||||
let init_pipeline = pipeline_cache.queue_compute_pipeline(ComputePipelineDescriptor {
|
||||
layout: vec![texture_bind_group_layout.clone()],
|
||||
shader: shader.clone(),
|
||||
entry_point: Some(Cow::from("init")),
|
||||
..default()
|
||||
});
|
||||
let update_pipeline = pipeline_cache.queue_compute_pipeline(ComputePipelineDescriptor {
|
||||
layout: vec![texture_bind_group_layout.clone()],
|
||||
shader,
|
||||
entry_point: Some(Cow::from("update")),
|
||||
..default()
|
||||
});
|
||||
),
|
||||
);
|
||||
let shader = asset_server.load(SHADER_ASSET_PATH);
|
||||
let init_pipeline = pipeline_cache.queue_compute_pipeline(ComputePipelineDescriptor {
|
||||
layout: vec![texture_bind_group_layout.clone()],
|
||||
shader: shader.clone(),
|
||||
entry_point: Some(Cow::from("init")),
|
||||
..default()
|
||||
});
|
||||
let update_pipeline = pipeline_cache.queue_compute_pipeline(ComputePipelineDescriptor {
|
||||
layout: vec![texture_bind_group_layout.clone()],
|
||||
shader,
|
||||
entry_point: Some(Cow::from("update")),
|
||||
..default()
|
||||
});
|
||||
|
||||
GameOfLifePipeline {
|
||||
texture_bind_group_layout,
|
||||
init_pipeline,
|
||||
update_pipeline,
|
||||
}
|
||||
}
|
||||
commands.insert_resource(GameOfLifePipeline {
|
||||
texture_bind_group_layout,
|
||||
init_pipeline,
|
||||
update_pipeline,
|
||||
});
|
||||
}
|
||||
|
||||
enum GameOfLifeState {
|
||||
|
@ -55,7 +55,7 @@ use bevy::{
|
||||
renderer::RenderContext,
|
||||
sync_world::MainEntity,
|
||||
view::{ExtractedView, RenderVisibleEntities, RetainedViewEntity, ViewTarget},
|
||||
Extract, Render, RenderApp, RenderDebugFlags, RenderSystems,
|
||||
Extract, Render, RenderApp, RenderDebugFlags, RenderStartup, RenderSystems,
|
||||
},
|
||||
};
|
||||
use nonmax::NonMaxU32;
|
||||
@ -127,6 +127,7 @@ impl Plugin for MeshStencilPhasePlugin {
|
||||
.init_resource::<DrawFunctions<Stencil3d>>()
|
||||
.add_render_command::<Stencil3d, DrawMesh3dStencil>()
|
||||
.init_resource::<ViewSortedRenderPhases<Stencil3d>>()
|
||||
.add_systems(RenderStartup, init_stencil_pipeline)
|
||||
.add_systems(ExtractSchedule, extract_camera_phases)
|
||||
.add_systems(
|
||||
Render,
|
||||
@ -143,16 +144,6 @@ impl Plugin for MeshStencilPhasePlugin {
|
||||
// Tell the node to run after the main pass
|
||||
.add_render_graph_edges(Core3d, (Node3d::MainOpaquePass, CustomDrawPassLabel));
|
||||
}
|
||||
|
||||
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;
|
||||
};
|
||||
// The pipeline needs the RenderDevice to be created and it's only available once plugins
|
||||
// are initialized
|
||||
render_app.init_resource::<StencilPipeline>();
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Resource)]
|
||||
@ -167,13 +158,15 @@ struct StencilPipeline {
|
||||
shader_handle: Handle<Shader>,
|
||||
}
|
||||
|
||||
impl FromWorld for StencilPipeline {
|
||||
fn from_world(world: &mut World) -> Self {
|
||||
Self {
|
||||
mesh_pipeline: MeshPipeline::from_world(world),
|
||||
shader_handle: world.resource::<AssetServer>().load(SHADER_ASSET_PATH),
|
||||
}
|
||||
}
|
||||
fn init_stencil_pipeline(
|
||||
mut commands: Commands,
|
||||
mesh_pipeline: Res<MeshPipeline>,
|
||||
asset_server: Res<AssetServer>,
|
||||
) {
|
||||
commands.insert_resource(StencilPipeline {
|
||||
mesh_pipeline: mesh_pipeline.clone(),
|
||||
shader_handle: asset_server.load(SHADER_ASSET_PATH),
|
||||
});
|
||||
}
|
||||
|
||||
// For more information on how SpecializedMeshPipeline work, please look at the
|
||||
|
@ -32,7 +32,7 @@ use bevy::{
|
||||
renderer::RenderDevice,
|
||||
sync_world::MainEntity,
|
||||
view::{ExtractedView, NoFrustumCulling, NoIndirectDrawing},
|
||||
Render, RenderApp, RenderSystems,
|
||||
Render, RenderApp, RenderStartup, RenderSystems,
|
||||
},
|
||||
};
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
@ -102,6 +102,7 @@ impl Plugin for CustomMaterialPlugin {
|
||||
app.sub_app_mut(RenderApp)
|
||||
.add_render_command::<Transparent3d, DrawCustom>()
|
||||
.init_resource::<SpecializedMeshPipelines<CustomPipeline>>()
|
||||
.add_systems(RenderStartup, init_custom_pipeline)
|
||||
.add_systems(
|
||||
Render,
|
||||
(
|
||||
@ -110,10 +111,6 @@ impl Plugin for CustomMaterialPlugin {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
fn finish(&self, app: &mut App) {
|
||||
app.sub_app_mut(RenderApp).init_resource::<CustomPipeline>();
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Pod, Zeroable)]
|
||||
@ -203,15 +200,15 @@ struct CustomPipeline {
|
||||
mesh_pipeline: MeshPipeline,
|
||||
}
|
||||
|
||||
impl FromWorld for CustomPipeline {
|
||||
fn from_world(world: &mut World) -> Self {
|
||||
let mesh_pipeline = world.resource::<MeshPipeline>();
|
||||
|
||||
CustomPipeline {
|
||||
shader: world.load_asset(SHADER_ASSET_PATH),
|
||||
mesh_pipeline: mesh_pipeline.clone(),
|
||||
}
|
||||
}
|
||||
fn init_custom_pipeline(
|
||||
mut commands: Commands,
|
||||
asset_server: Res<AssetServer>,
|
||||
mesh_pipeline: Res<MeshPipeline>,
|
||||
) {
|
||||
commands.insert_resource(CustomPipeline {
|
||||
shader: asset_server.load(SHADER_ASSET_PATH),
|
||||
mesh_pipeline: mesh_pipeline.clone(),
|
||||
});
|
||||
}
|
||||
|
||||
impl SpecializedMeshPipeline for CustomPipeline {
|
||||
|
@ -39,7 +39,7 @@ use bevy::{
|
||||
},
|
||||
view::NoIndirectDrawing,
|
||||
view::{self, ExtractedView, RenderVisibleEntities, ViewTarget, VisibilityClass},
|
||||
Render, RenderApp, RenderSystems,
|
||||
Render, RenderApp, RenderStartup, RenderSystems,
|
||||
},
|
||||
};
|
||||
|
||||
@ -118,20 +118,12 @@ impl Plugin for CustomRenderedMeshPipelinePlugin {
|
||||
.init_resource::<SpecializedMeshPipelines<CustomMeshPipeline>>()
|
||||
// We need to use a custom draw command so we need to register it
|
||||
.add_render_command::<Opaque3d, DrawSpecializedPipelineCommands>()
|
||||
.add_systems(RenderStartup, init_custom_mesh_pipeline)
|
||||
.add_systems(
|
||||
Render,
|
||||
queue_custom_mesh_pipeline.in_set(RenderSystems::Queue),
|
||||
);
|
||||
}
|
||||
|
||||
fn finish(&self, app: &mut App) {
|
||||
let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
|
||||
return;
|
||||
};
|
||||
// Creating this pipeline needs the RenderDevice and RenderQueue
|
||||
// which are only available once rendering plugins are initialized.
|
||||
render_app.init_resource::<CustomMeshPipeline>();
|
||||
}
|
||||
}
|
||||
|
||||
/// A marker component that represents an entity that is to be rendered using
|
||||
@ -174,15 +166,17 @@ struct CustomMeshPipeline {
|
||||
shader_handle: Handle<Shader>,
|
||||
}
|
||||
|
||||
impl FromWorld for CustomMeshPipeline {
|
||||
fn from_world(world: &mut World) -> Self {
|
||||
// Load the shader
|
||||
let shader_handle: Handle<Shader> = world.resource::<AssetServer>().load(SHADER_ASSET_PATH);
|
||||
Self {
|
||||
mesh_pipeline: MeshPipeline::from_world(world),
|
||||
shader_handle,
|
||||
}
|
||||
}
|
||||
fn init_custom_mesh_pipeline(
|
||||
mut commands: Commands,
|
||||
asset_server: Res<AssetServer>,
|
||||
mesh_pipeline: Res<MeshPipeline>,
|
||||
) {
|
||||
// Load the shader
|
||||
let shader_handle: Handle<Shader> = asset_server.load(SHADER_ASSET_PATH);
|
||||
commands.insert_resource(CustomMeshPipeline {
|
||||
mesh_pipeline: mesh_pipeline.clone(),
|
||||
shader_handle,
|
||||
});
|
||||
}
|
||||
|
||||
impl SpecializedMeshPipeline for CustomMeshPipeline {
|
||||
|
Loading…
Reference in New Issue
Block a user