2021-04-11 20:13:07 +00:00
|
|
|
pub mod camera;
|
|
|
|
pub mod color;
|
|
|
|
pub mod mesh;
|
2021-06-25 03:04:28 +00:00
|
|
|
pub mod render_asset;
|
Modular Rendering (#2831)
This changes how render logic is composed to make it much more modular. Previously, all extraction logic was centralized for a given "type" of rendered thing. For example, we extracted meshes into a vector of ExtractedMesh, which contained the mesh and material asset handles, the transform, etc. We looked up bindings for "drawn things" using their index in the `Vec<ExtractedMesh>`. This worked fine for built in rendering, but made it hard to reuse logic for "custom" rendering. It also prevented us from reusing things like "extracted transforms" across contexts.
To make rendering more modular, I made a number of changes:
* Entities now drive rendering:
* We extract "render components" from "app components" and store them _on_ entities. No more centralized uber lists! We now have true "ECS-driven rendering"
* To make this perform well, I implemented #2673 in upstream Bevy for fast batch insertions into specific entities. This was merged into the `pipelined-rendering` branch here: #2815
* Reworked the `Draw` abstraction:
* Generic `PhaseItems`: each draw phase can define its own type of "rendered thing", which can define its own "sort key"
* Ported the 2d, 3d, and shadow phases to the new PhaseItem impl (currently Transparent2d, Transparent3d, and Shadow PhaseItems)
* `Draw` trait and and `DrawFunctions` are now generic on PhaseItem
* Modular / Ergonomic `DrawFunctions` via `RenderCommands`
* RenderCommand is a trait that runs an ECS query and produces one or more RenderPass calls. Types implementing this trait can be composed to create a final DrawFunction. For example the DrawPbr DrawFunction is created from the following DrawCommand tuple. Const generics are used to set specific bind group locations:
```rust
pub type DrawPbr = (
SetPbrPipeline,
SetMeshViewBindGroup<0>,
SetStandardMaterialBindGroup<1>,
SetTransformBindGroup<2>,
DrawMesh,
);
```
* The new `custom_shader_pipelined` example illustrates how the commands above can be reused to create a custom draw function:
```rust
type DrawCustom = (
SetCustomMaterialPipeline,
SetMeshViewBindGroup<0>,
SetTransformBindGroup<2>,
DrawMesh,
);
```
* ExtractComponentPlugin and UniformComponentPlugin:
* Simple, standardized ways to easily extract individual components and write them to GPU buffers
* Ported PBR and Sprite rendering to the new primitives above.
* Removed staging buffer from UniformVec in favor of direct Queue usage
* Makes UniformVec much easier to use and more ergonomic. Completely removes the need for custom render graph nodes in these contexts (see the PbrNode and view Node removals and the much simpler call patterns in the relevant Prepare systems).
* Added a many_cubes_pipelined example to benchmark baseline 3d rendering performance and ensure there were no major regressions during this port. Avoiding regressions was challenging given that the old approach of extracting into centralized vectors is basically the "optimal" approach. However thanks to a various ECS optimizations and render logic rephrasing, we pretty much break even on this benchmark!
* Lifetimeless SystemParams: this will be a bit divisive, but as we continue to embrace "trait driven systems" (ex: ExtractComponentPlugin, UniformComponentPlugin, DrawCommand), the ergonomics of `(Query<'static, 'static, (&'static A, &'static B, &'static)>, Res<'static, C>)` were getting very hard to bear. As a compromise, I added "static type aliases" for the relevant SystemParams. The previous example can now be expressed like this: `(SQuery<(Read<A>, Read<B>)>, SRes<C>)`. If anyone has better ideas / conflicting opinions, please let me know!
* RunSystem trait: a way to define Systems via a trait with a SystemParam associated type. This is used to implement the various plugins mentioned above. I also added SystemParamItem and QueryItem type aliases to make "trait stye" ecs interactions nicer on the eyes (and fingers).
* RenderAsset retrying: ensures that render assets are only created when they are "ready" and allows us to create bind groups directly inside render assets (which significantly simplified the StandardMaterial code). I think ultimately we should swap this out on "asset dependency" events to wait for dependencies to load, but this will require significant asset system changes.
* Updated some built in shaders to account for missing MeshUniform fields
2021-09-23 06:16:11 +00:00
|
|
|
pub mod render_component;
|
2021-04-11 20:13:07 +00:00
|
|
|
pub mod render_graph;
|
2021-06-02 02:59:17 +00:00
|
|
|
pub mod render_phase;
|
2021-04-11 20:13:07 +00:00
|
|
|
pub mod render_resource;
|
|
|
|
pub mod renderer;
|
|
|
|
pub mod shader;
|
|
|
|
pub mod texture;
|
2021-06-02 02:59:17 +00:00
|
|
|
pub mod view;
|
2021-04-11 20:13:07 +00:00
|
|
|
|
|
|
|
pub use once_cell;
|
|
|
|
|
|
|
|
use crate::{
|
2021-06-02 02:59:17 +00:00
|
|
|
camera::CameraPlugin,
|
|
|
|
mesh::MeshPlugin,
|
|
|
|
render_graph::RenderGraph,
|
2021-06-21 23:28:52 +00:00
|
|
|
renderer::render_system,
|
|
|
|
texture::ImagePlugin,
|
2021-06-02 02:59:17 +00:00
|
|
|
view::{ViewPlugin, WindowRenderPlugin},
|
2021-04-11 20:13:07 +00:00
|
|
|
};
|
2021-08-24 06:37:28 +00:00
|
|
|
use bevy_app::{App, AppLabel, Plugin};
|
Modular Rendering (#2831)
This changes how render logic is composed to make it much more modular. Previously, all extraction logic was centralized for a given "type" of rendered thing. For example, we extracted meshes into a vector of ExtractedMesh, which contained the mesh and material asset handles, the transform, etc. We looked up bindings for "drawn things" using their index in the `Vec<ExtractedMesh>`. This worked fine for built in rendering, but made it hard to reuse logic for "custom" rendering. It also prevented us from reusing things like "extracted transforms" across contexts.
To make rendering more modular, I made a number of changes:
* Entities now drive rendering:
* We extract "render components" from "app components" and store them _on_ entities. No more centralized uber lists! We now have true "ECS-driven rendering"
* To make this perform well, I implemented #2673 in upstream Bevy for fast batch insertions into specific entities. This was merged into the `pipelined-rendering` branch here: #2815
* Reworked the `Draw` abstraction:
* Generic `PhaseItems`: each draw phase can define its own type of "rendered thing", which can define its own "sort key"
* Ported the 2d, 3d, and shadow phases to the new PhaseItem impl (currently Transparent2d, Transparent3d, and Shadow PhaseItems)
* `Draw` trait and and `DrawFunctions` are now generic on PhaseItem
* Modular / Ergonomic `DrawFunctions` via `RenderCommands`
* RenderCommand is a trait that runs an ECS query and produces one or more RenderPass calls. Types implementing this trait can be composed to create a final DrawFunction. For example the DrawPbr DrawFunction is created from the following DrawCommand tuple. Const generics are used to set specific bind group locations:
```rust
pub type DrawPbr = (
SetPbrPipeline,
SetMeshViewBindGroup<0>,
SetStandardMaterialBindGroup<1>,
SetTransformBindGroup<2>,
DrawMesh,
);
```
* The new `custom_shader_pipelined` example illustrates how the commands above can be reused to create a custom draw function:
```rust
type DrawCustom = (
SetCustomMaterialPipeline,
SetMeshViewBindGroup<0>,
SetTransformBindGroup<2>,
DrawMesh,
);
```
* ExtractComponentPlugin and UniformComponentPlugin:
* Simple, standardized ways to easily extract individual components and write them to GPU buffers
* Ported PBR and Sprite rendering to the new primitives above.
* Removed staging buffer from UniformVec in favor of direct Queue usage
* Makes UniformVec much easier to use and more ergonomic. Completely removes the need for custom render graph nodes in these contexts (see the PbrNode and view Node removals and the much simpler call patterns in the relevant Prepare systems).
* Added a many_cubes_pipelined example to benchmark baseline 3d rendering performance and ensure there were no major regressions during this port. Avoiding regressions was challenging given that the old approach of extracting into centralized vectors is basically the "optimal" approach. However thanks to a various ECS optimizations and render logic rephrasing, we pretty much break even on this benchmark!
* Lifetimeless SystemParams: this will be a bit divisive, but as we continue to embrace "trait driven systems" (ex: ExtractComponentPlugin, UniformComponentPlugin, DrawCommand), the ergonomics of `(Query<'static, 'static, (&'static A, &'static B, &'static)>, Res<'static, C>)` were getting very hard to bear. As a compromise, I added "static type aliases" for the relevant SystemParams. The previous example can now be expressed like this: `(SQuery<(Read<A>, Read<B>)>, SRes<C>)`. If anyone has better ideas / conflicting opinions, please let me know!
* RunSystem trait: a way to define Systems via a trait with a SystemParam associated type. This is used to implement the various plugins mentioned above. I also added SystemParamItem and QueryItem type aliases to make "trait stye" ecs interactions nicer on the eyes (and fingers).
* RenderAsset retrying: ensures that render assets are only created when they are "ready" and allows us to create bind groups directly inside render assets (which significantly simplified the StandardMaterial code). I think ultimately we should swap this out on "asset dependency" events to wait for dependencies to load, but this will require significant asset system changes.
* Updated some built in shaders to account for missing MeshUniform fields
2021-09-23 06:16:11 +00:00
|
|
|
use bevy_asset::AssetServer;
|
2021-04-11 20:13:07 +00:00
|
|
|
use bevy_ecs::prelude::*;
|
Modular Rendering (#2831)
This changes how render logic is composed to make it much more modular. Previously, all extraction logic was centralized for a given "type" of rendered thing. For example, we extracted meshes into a vector of ExtractedMesh, which contained the mesh and material asset handles, the transform, etc. We looked up bindings for "drawn things" using their index in the `Vec<ExtractedMesh>`. This worked fine for built in rendering, but made it hard to reuse logic for "custom" rendering. It also prevented us from reusing things like "extracted transforms" across contexts.
To make rendering more modular, I made a number of changes:
* Entities now drive rendering:
* We extract "render components" from "app components" and store them _on_ entities. No more centralized uber lists! We now have true "ECS-driven rendering"
* To make this perform well, I implemented #2673 in upstream Bevy for fast batch insertions into specific entities. This was merged into the `pipelined-rendering` branch here: #2815
* Reworked the `Draw` abstraction:
* Generic `PhaseItems`: each draw phase can define its own type of "rendered thing", which can define its own "sort key"
* Ported the 2d, 3d, and shadow phases to the new PhaseItem impl (currently Transparent2d, Transparent3d, and Shadow PhaseItems)
* `Draw` trait and and `DrawFunctions` are now generic on PhaseItem
* Modular / Ergonomic `DrawFunctions` via `RenderCommands`
* RenderCommand is a trait that runs an ECS query and produces one or more RenderPass calls. Types implementing this trait can be composed to create a final DrawFunction. For example the DrawPbr DrawFunction is created from the following DrawCommand tuple. Const generics are used to set specific bind group locations:
```rust
pub type DrawPbr = (
SetPbrPipeline,
SetMeshViewBindGroup<0>,
SetStandardMaterialBindGroup<1>,
SetTransformBindGroup<2>,
DrawMesh,
);
```
* The new `custom_shader_pipelined` example illustrates how the commands above can be reused to create a custom draw function:
```rust
type DrawCustom = (
SetCustomMaterialPipeline,
SetMeshViewBindGroup<0>,
SetTransformBindGroup<2>,
DrawMesh,
);
```
* ExtractComponentPlugin and UniformComponentPlugin:
* Simple, standardized ways to easily extract individual components and write them to GPU buffers
* Ported PBR and Sprite rendering to the new primitives above.
* Removed staging buffer from UniformVec in favor of direct Queue usage
* Makes UniformVec much easier to use and more ergonomic. Completely removes the need for custom render graph nodes in these contexts (see the PbrNode and view Node removals and the much simpler call patterns in the relevant Prepare systems).
* Added a many_cubes_pipelined example to benchmark baseline 3d rendering performance and ensure there were no major regressions during this port. Avoiding regressions was challenging given that the old approach of extracting into centralized vectors is basically the "optimal" approach. However thanks to a various ECS optimizations and render logic rephrasing, we pretty much break even on this benchmark!
* Lifetimeless SystemParams: this will be a bit divisive, but as we continue to embrace "trait driven systems" (ex: ExtractComponentPlugin, UniformComponentPlugin, DrawCommand), the ergonomics of `(Query<'static, 'static, (&'static A, &'static B, &'static)>, Res<'static, C>)` were getting very hard to bear. As a compromise, I added "static type aliases" for the relevant SystemParams. The previous example can now be expressed like this: `(SQuery<(Read<A>, Read<B>)>, SRes<C>)`. If anyone has better ideas / conflicting opinions, please let me know!
* RunSystem trait: a way to define Systems via a trait with a SystemParam associated type. This is used to implement the various plugins mentioned above. I also added SystemParamItem and QueryItem type aliases to make "trait stye" ecs interactions nicer on the eyes (and fingers).
* RenderAsset retrying: ensures that render assets are only created when they are "ready" and allows us to create bind groups directly inside render assets (which significantly simplified the StandardMaterial code). I think ultimately we should swap this out on "asset dependency" events to wait for dependencies to load, but this will require significant asset system changes.
* Updated some built in shaders to account for missing MeshUniform fields
2021-09-23 06:16:11 +00:00
|
|
|
use std::ops::{Deref, DerefMut};
|
2021-10-08 19:55:24 +00:00
|
|
|
use wgpu::Backends;
|
2021-04-11 20:13:07 +00:00
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct RenderPlugin;
|
|
|
|
|
|
|
|
/// The names of the default App stages
|
|
|
|
#[derive(Debug, Hash, PartialEq, Eq, Clone, StageLabel)]
|
|
|
|
pub enum RenderStage {
|
|
|
|
/// Extract data from "app world" and insert it into "render world". This step should be kept
|
|
|
|
/// as short as possible to increase the "pipelining potential" for running the next frame
|
|
|
|
/// while rendering the current frame.
|
|
|
|
Extract,
|
|
|
|
|
|
|
|
/// Prepare render resources from extracted data.
|
|
|
|
Prepare,
|
|
|
|
|
|
|
|
/// Create Bind Groups that depend on Prepare data and queue up draw calls to run during the Render stage.
|
|
|
|
Queue,
|
|
|
|
|
2021-06-02 02:59:17 +00:00
|
|
|
// TODO: This could probably be moved in favor of a system ordering abstraction in Render or Queue
|
2021-06-18 18:21:18 +00:00
|
|
|
/// Sort RenderPhases here
|
2021-06-02 02:59:17 +00:00
|
|
|
PhaseSort,
|
|
|
|
|
2021-04-11 20:13:07 +00:00
|
|
|
/// Actual rendering happens here. In most cases, only the render backend should insert resources here
|
|
|
|
Render,
|
2021-06-02 02:59:17 +00:00
|
|
|
|
|
|
|
/// Cleanup render resources here.
|
|
|
|
Cleanup,
|
2021-04-11 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 17:52:24 +00:00
|
|
|
/// The Render App World. This is only available as a resource during the Extract step.
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct RenderWorld(World);
|
|
|
|
|
|
|
|
impl Deref for RenderWorld {
|
|
|
|
type Target = World;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for RenderWorld {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 00:31:21 +00:00
|
|
|
/// Label for the rendering sub-app
|
2021-08-24 06:37:28 +00:00
|
|
|
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, AppLabel)]
|
|
|
|
pub struct RenderApp;
|
2021-08-24 00:31:21 +00:00
|
|
|
|
2021-07-28 17:52:24 +00:00
|
|
|
/// A "scratch" world used to avoid allocating new worlds every frame when
|
|
|
|
// swapping out the Render World.
|
|
|
|
#[derive(Default)]
|
|
|
|
struct ScratchRenderWorld(World);
|
|
|
|
|
2021-04-11 20:13:07 +00:00
|
|
|
impl Plugin for RenderPlugin {
|
|
|
|
fn build(&self, app: &mut App) {
|
2021-06-21 23:28:52 +00:00
|
|
|
let (instance, device, queue) =
|
|
|
|
futures_lite::future::block_on(renderer::initialize_renderer(
|
2021-10-08 19:55:24 +00:00
|
|
|
wgpu::util::backend_bits_from_env().unwrap_or(Backends::PRIMARY),
|
2021-06-21 23:28:52 +00:00
|
|
|
&wgpu::RequestAdapterOptions {
|
|
|
|
power_preference: wgpu::PowerPreference::HighPerformance,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
&wgpu::DeviceDescriptor::default(),
|
|
|
|
));
|
|
|
|
app.insert_resource(device.clone())
|
2021-07-28 17:52:24 +00:00
|
|
|
.insert_resource(queue.clone())
|
|
|
|
.init_resource::<ScratchRenderWorld>();
|
Modular Rendering (#2831)
This changes how render logic is composed to make it much more modular. Previously, all extraction logic was centralized for a given "type" of rendered thing. For example, we extracted meshes into a vector of ExtractedMesh, which contained the mesh and material asset handles, the transform, etc. We looked up bindings for "drawn things" using their index in the `Vec<ExtractedMesh>`. This worked fine for built in rendering, but made it hard to reuse logic for "custom" rendering. It also prevented us from reusing things like "extracted transforms" across contexts.
To make rendering more modular, I made a number of changes:
* Entities now drive rendering:
* We extract "render components" from "app components" and store them _on_ entities. No more centralized uber lists! We now have true "ECS-driven rendering"
* To make this perform well, I implemented #2673 in upstream Bevy for fast batch insertions into specific entities. This was merged into the `pipelined-rendering` branch here: #2815
* Reworked the `Draw` abstraction:
* Generic `PhaseItems`: each draw phase can define its own type of "rendered thing", which can define its own "sort key"
* Ported the 2d, 3d, and shadow phases to the new PhaseItem impl (currently Transparent2d, Transparent3d, and Shadow PhaseItems)
* `Draw` trait and and `DrawFunctions` are now generic on PhaseItem
* Modular / Ergonomic `DrawFunctions` via `RenderCommands`
* RenderCommand is a trait that runs an ECS query and produces one or more RenderPass calls. Types implementing this trait can be composed to create a final DrawFunction. For example the DrawPbr DrawFunction is created from the following DrawCommand tuple. Const generics are used to set specific bind group locations:
```rust
pub type DrawPbr = (
SetPbrPipeline,
SetMeshViewBindGroup<0>,
SetStandardMaterialBindGroup<1>,
SetTransformBindGroup<2>,
DrawMesh,
);
```
* The new `custom_shader_pipelined` example illustrates how the commands above can be reused to create a custom draw function:
```rust
type DrawCustom = (
SetCustomMaterialPipeline,
SetMeshViewBindGroup<0>,
SetTransformBindGroup<2>,
DrawMesh,
);
```
* ExtractComponentPlugin and UniformComponentPlugin:
* Simple, standardized ways to easily extract individual components and write them to GPU buffers
* Ported PBR and Sprite rendering to the new primitives above.
* Removed staging buffer from UniformVec in favor of direct Queue usage
* Makes UniformVec much easier to use and more ergonomic. Completely removes the need for custom render graph nodes in these contexts (see the PbrNode and view Node removals and the much simpler call patterns in the relevant Prepare systems).
* Added a many_cubes_pipelined example to benchmark baseline 3d rendering performance and ensure there were no major regressions during this port. Avoiding regressions was challenging given that the old approach of extracting into centralized vectors is basically the "optimal" approach. However thanks to a various ECS optimizations and render logic rephrasing, we pretty much break even on this benchmark!
* Lifetimeless SystemParams: this will be a bit divisive, but as we continue to embrace "trait driven systems" (ex: ExtractComponentPlugin, UniformComponentPlugin, DrawCommand), the ergonomics of `(Query<'static, 'static, (&'static A, &'static B, &'static)>, Res<'static, C>)` were getting very hard to bear. As a compromise, I added "static type aliases" for the relevant SystemParams. The previous example can now be expressed like this: `(SQuery<(Read<A>, Read<B>)>, SRes<C>)`. If anyone has better ideas / conflicting opinions, please let me know!
* RunSystem trait: a way to define Systems via a trait with a SystemParam associated type. This is used to implement the various plugins mentioned above. I also added SystemParamItem and QueryItem type aliases to make "trait stye" ecs interactions nicer on the eyes (and fingers).
* RenderAsset retrying: ensures that render assets are only created when they are "ready" and allows us to create bind groups directly inside render assets (which significantly simplified the StandardMaterial code). I think ultimately we should swap this out on "asset dependency" events to wait for dependencies to load, but this will require significant asset system changes.
* Updated some built in shaders to account for missing MeshUniform fields
2021-09-23 06:16:11 +00:00
|
|
|
let asset_server = app.world.get_resource::<AssetServer>().unwrap().clone();
|
2021-06-21 23:28:52 +00:00
|
|
|
|
2021-04-11 20:13:07 +00:00
|
|
|
let mut render_app = App::empty();
|
|
|
|
let mut extract_stage = SystemStage::parallel();
|
|
|
|
// don't apply buffers when the stage finishes running
|
|
|
|
// extract stage runs on the app world, but the buffers are applied to the render world
|
|
|
|
extract_stage.set_apply_buffers(false);
|
|
|
|
render_app
|
|
|
|
.add_stage(RenderStage::Extract, extract_stage)
|
|
|
|
.add_stage(RenderStage::Prepare, SystemStage::parallel())
|
|
|
|
.add_stage(RenderStage::Queue, SystemStage::parallel())
|
2021-06-02 02:59:17 +00:00
|
|
|
.add_stage(RenderStage::PhaseSort, SystemStage::parallel())
|
2021-06-21 23:28:52 +00:00
|
|
|
.add_stage(
|
|
|
|
RenderStage::Render,
|
|
|
|
SystemStage::parallel().with_system(render_system.exclusive_system()),
|
|
|
|
)
|
2021-06-02 02:59:17 +00:00
|
|
|
.add_stage(RenderStage::Cleanup, SystemStage::parallel())
|
2021-06-21 23:28:52 +00:00
|
|
|
.insert_resource(instance)
|
|
|
|
.insert_resource(device)
|
|
|
|
.insert_resource(queue)
|
Modular Rendering (#2831)
This changes how render logic is composed to make it much more modular. Previously, all extraction logic was centralized for a given "type" of rendered thing. For example, we extracted meshes into a vector of ExtractedMesh, which contained the mesh and material asset handles, the transform, etc. We looked up bindings for "drawn things" using their index in the `Vec<ExtractedMesh>`. This worked fine for built in rendering, but made it hard to reuse logic for "custom" rendering. It also prevented us from reusing things like "extracted transforms" across contexts.
To make rendering more modular, I made a number of changes:
* Entities now drive rendering:
* We extract "render components" from "app components" and store them _on_ entities. No more centralized uber lists! We now have true "ECS-driven rendering"
* To make this perform well, I implemented #2673 in upstream Bevy for fast batch insertions into specific entities. This was merged into the `pipelined-rendering` branch here: #2815
* Reworked the `Draw` abstraction:
* Generic `PhaseItems`: each draw phase can define its own type of "rendered thing", which can define its own "sort key"
* Ported the 2d, 3d, and shadow phases to the new PhaseItem impl (currently Transparent2d, Transparent3d, and Shadow PhaseItems)
* `Draw` trait and and `DrawFunctions` are now generic on PhaseItem
* Modular / Ergonomic `DrawFunctions` via `RenderCommands`
* RenderCommand is a trait that runs an ECS query and produces one or more RenderPass calls. Types implementing this trait can be composed to create a final DrawFunction. For example the DrawPbr DrawFunction is created from the following DrawCommand tuple. Const generics are used to set specific bind group locations:
```rust
pub type DrawPbr = (
SetPbrPipeline,
SetMeshViewBindGroup<0>,
SetStandardMaterialBindGroup<1>,
SetTransformBindGroup<2>,
DrawMesh,
);
```
* The new `custom_shader_pipelined` example illustrates how the commands above can be reused to create a custom draw function:
```rust
type DrawCustom = (
SetCustomMaterialPipeline,
SetMeshViewBindGroup<0>,
SetTransformBindGroup<2>,
DrawMesh,
);
```
* ExtractComponentPlugin and UniformComponentPlugin:
* Simple, standardized ways to easily extract individual components and write them to GPU buffers
* Ported PBR and Sprite rendering to the new primitives above.
* Removed staging buffer from UniformVec in favor of direct Queue usage
* Makes UniformVec much easier to use and more ergonomic. Completely removes the need for custom render graph nodes in these contexts (see the PbrNode and view Node removals and the much simpler call patterns in the relevant Prepare systems).
* Added a many_cubes_pipelined example to benchmark baseline 3d rendering performance and ensure there were no major regressions during this port. Avoiding regressions was challenging given that the old approach of extracting into centralized vectors is basically the "optimal" approach. However thanks to a various ECS optimizations and render logic rephrasing, we pretty much break even on this benchmark!
* Lifetimeless SystemParams: this will be a bit divisive, but as we continue to embrace "trait driven systems" (ex: ExtractComponentPlugin, UniformComponentPlugin, DrawCommand), the ergonomics of `(Query<'static, 'static, (&'static A, &'static B, &'static)>, Res<'static, C>)` were getting very hard to bear. As a compromise, I added "static type aliases" for the relevant SystemParams. The previous example can now be expressed like this: `(SQuery<(Read<A>, Read<B>)>, SRes<C>)`. If anyone has better ideas / conflicting opinions, please let me know!
* RunSystem trait: a way to define Systems via a trait with a SystemParam associated type. This is used to implement the various plugins mentioned above. I also added SystemParamItem and QueryItem type aliases to make "trait stye" ecs interactions nicer on the eyes (and fingers).
* RenderAsset retrying: ensures that render assets are only created when they are "ready" and allows us to create bind groups directly inside render assets (which significantly simplified the StandardMaterial code). I think ultimately we should swap this out on "asset dependency" events to wait for dependencies to load, but this will require significant asset system changes.
* Updated some built in shaders to account for missing MeshUniform fields
2021-09-23 06:16:11 +00:00
|
|
|
.insert_resource(asset_server)
|
|
|
|
.init_resource::<RenderGraph>();
|
2021-06-02 02:59:17 +00:00
|
|
|
|
2021-08-24 06:37:28 +00:00
|
|
|
app.add_sub_app(RenderApp, render_app, move |app_world, render_app| {
|
2021-10-02 19:16:32 +00:00
|
|
|
#[cfg(feature = "trace")]
|
|
|
|
let render_span = bevy_utils::tracing::info_span!("renderer subapp");
|
|
|
|
#[cfg(feature = "trace")]
|
|
|
|
let _render_guard = render_span.enter();
|
|
|
|
{
|
|
|
|
#[cfg(feature = "trace")]
|
|
|
|
let stage_span =
|
|
|
|
bevy_utils::tracing::info_span!("stage", name = "reserve_and_flush");
|
|
|
|
#[cfg(feature = "trace")]
|
|
|
|
let _stage_guard = stage_span.enter();
|
|
|
|
|
|
|
|
// reserve all existing app entities for use in render_app
|
|
|
|
// they can only be spawned using `get_or_spawn()`
|
|
|
|
let meta_len = app_world.entities().meta.len();
|
|
|
|
render_app
|
|
|
|
.world
|
|
|
|
.entities()
|
|
|
|
.reserve_entities(meta_len as u32);
|
|
|
|
|
|
|
|
// flushing as "invalid" ensures that app world entities aren't added as "empty archetype" entities by default
|
|
|
|
// these entities cannot be accessed without spawning directly onto them
|
|
|
|
// this _only_ works as expected because clear_entities() is called at the end of every frame.
|
|
|
|
render_app.world.entities_mut().flush_as_invalid();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
#[cfg(feature = "trace")]
|
|
|
|
let stage_span = bevy_utils::tracing::info_span!("stage", name = "extract");
|
|
|
|
#[cfg(feature = "trace")]
|
|
|
|
let _stage_guard = stage_span.enter();
|
|
|
|
|
|
|
|
// extract
|
|
|
|
extract(app_world, render_app);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
#[cfg(feature = "trace")]
|
|
|
|
let stage_span = bevy_utils::tracing::info_span!("stage", name = "prepare");
|
|
|
|
#[cfg(feature = "trace")]
|
|
|
|
let _stage_guard = stage_span.enter();
|
|
|
|
|
|
|
|
// prepare
|
|
|
|
let prepare = render_app
|
|
|
|
.schedule
|
|
|
|
.get_stage_mut::<SystemStage>(&RenderStage::Prepare)
|
|
|
|
.unwrap();
|
|
|
|
prepare.run(&mut render_app.world);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
#[cfg(feature = "trace")]
|
|
|
|
let stage_span = bevy_utils::tracing::info_span!("stage", name = "queue");
|
|
|
|
#[cfg(feature = "trace")]
|
|
|
|
let _stage_guard = stage_span.enter();
|
|
|
|
|
|
|
|
// queue
|
|
|
|
let queue = render_app
|
|
|
|
.schedule
|
|
|
|
.get_stage_mut::<SystemStage>(&RenderStage::Queue)
|
|
|
|
.unwrap();
|
|
|
|
queue.run(&mut render_app.world);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
#[cfg(feature = "trace")]
|
|
|
|
let stage_span = bevy_utils::tracing::info_span!("stage", name = "sort");
|
|
|
|
#[cfg(feature = "trace")]
|
|
|
|
let _stage_guard = stage_span.enter();
|
|
|
|
|
|
|
|
// phase sort
|
|
|
|
let phase_sort = render_app
|
|
|
|
.schedule
|
|
|
|
.get_stage_mut::<SystemStage>(&RenderStage::PhaseSort)
|
|
|
|
.unwrap();
|
|
|
|
phase_sort.run(&mut render_app.world);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
#[cfg(feature = "trace")]
|
|
|
|
let stage_span = bevy_utils::tracing::info_span!("stage", name = "render");
|
|
|
|
#[cfg(feature = "trace")]
|
|
|
|
let _stage_guard = stage_span.enter();
|
|
|
|
|
|
|
|
// render
|
|
|
|
let render = render_app
|
|
|
|
.schedule
|
|
|
|
.get_stage_mut::<SystemStage>(&RenderStage::Render)
|
|
|
|
.unwrap();
|
|
|
|
render.run(&mut render_app.world);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
#[cfg(feature = "trace")]
|
|
|
|
let stage_span = bevy_utils::tracing::info_span!("stage", name = "cleanup");
|
|
|
|
#[cfg(feature = "trace")]
|
|
|
|
let _stage_guard = stage_span.enter();
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
let cleanup = render_app
|
|
|
|
.schedule
|
|
|
|
.get_stage_mut::<SystemStage>(&RenderStage::Cleanup)
|
|
|
|
.unwrap();
|
|
|
|
cleanup.run(&mut render_app.world);
|
|
|
|
|
|
|
|
render_app.world.clear_entities();
|
|
|
|
}
|
2021-04-11 20:13:07 +00:00
|
|
|
});
|
|
|
|
|
2021-06-21 23:28:52 +00:00
|
|
|
app.add_plugin(WindowRenderPlugin)
|
2021-06-02 02:59:17 +00:00
|
|
|
.add_plugin(CameraPlugin)
|
|
|
|
.add_plugin(ViewPlugin)
|
|
|
|
.add_plugin(MeshPlugin)
|
2021-06-21 23:28:52 +00:00
|
|
|
.add_plugin(ImagePlugin);
|
2021-04-11 20:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extract(app_world: &mut World, render_app: &mut App) {
|
|
|
|
let extract = render_app
|
|
|
|
.schedule
|
|
|
|
.get_stage_mut::<SystemStage>(&RenderStage::Extract)
|
|
|
|
.unwrap();
|
2021-07-28 17:52:24 +00:00
|
|
|
|
|
|
|
// temporarily add the render world to the app world as a resource
|
|
|
|
let scratch_world = app_world.remove_resource::<ScratchRenderWorld>().unwrap();
|
|
|
|
let render_world = std::mem::replace(&mut render_app.world, scratch_world.0);
|
|
|
|
app_world.insert_resource(RenderWorld(render_world));
|
|
|
|
|
2021-04-11 20:13:07 +00:00
|
|
|
extract.run(app_world);
|
2021-07-28 17:52:24 +00:00
|
|
|
|
|
|
|
// add the render world back to the render app
|
|
|
|
let render_world = app_world.remove_resource::<RenderWorld>().unwrap();
|
|
|
|
let scratch_world = std::mem::replace(&mut render_app.world, render_world.0);
|
|
|
|
app_world.insert_resource(ScratchRenderWorld(scratch_world));
|
|
|
|
|
2021-04-11 20:13:07 +00:00
|
|
|
extract.apply_buffers(&mut render_app.world);
|
|
|
|
}
|