2020-07-17 01:26:21 +00:00
|
|
|
pub mod camera;
|
|
|
|
pub mod color;
|
2020-03-14 19:56:37 +00:00
|
|
|
pub mod mesh;
|
2021-12-14 03:58:23 +00:00
|
|
|
pub mod primitives;
|
|
|
|
pub mod render_asset;
|
|
|
|
pub mod render_component;
|
2020-02-18 02:36:31 +00:00
|
|
|
pub mod render_graph;
|
2021-12-14 03:58:23 +00:00
|
|
|
pub mod render_phase;
|
|
|
|
pub mod render_resource;
|
2020-04-25 00:46:54 +00:00
|
|
|
pub mod renderer;
|
2022-02-16 21:17:37 +00:00
|
|
|
pub mod settings;
|
2020-03-20 19:47:33 +00:00
|
|
|
pub mod texture;
|
2021-12-14 03:58:23 +00:00
|
|
|
pub mod view;
|
2020-04-06 03:19:02 +00:00
|
|
|
|
2020-07-17 01:26:21 +00:00
|
|
|
pub mod prelude {
|
2021-04-27 18:29:33 +00:00
|
|
|
#[doc(hidden)]
|
2020-07-17 01:26:21 +00:00
|
|
|
pub use crate::{
|
2021-12-14 03:58:23 +00:00
|
|
|
camera::{
|
|
|
|
Camera, OrthographicCameraBundle, OrthographicProjection, PerspectiveCameraBundle,
|
|
|
|
PerspectiveProjection,
|
|
|
|
},
|
2020-07-17 02:34:18 +00:00
|
|
|
color::Color,
|
|
|
|
mesh::{shape, Mesh},
|
2021-12-14 03:58:23 +00:00
|
|
|
render_resource::Shader,
|
|
|
|
texture::Image,
|
|
|
|
view::{ComputedVisibility, Msaa, Visibility},
|
2020-07-17 01:26:21 +00:00
|
|
|
};
|
|
|
|
}
|
2020-04-05 21:12:14 +00:00
|
|
|
|
2022-01-04 20:08:12 +00:00
|
|
|
use bevy_utils::tracing::debug;
|
2021-12-14 03:58:23 +00:00
|
|
|
pub use once_cell;
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
camera::CameraPlugin,
|
|
|
|
color::Color,
|
|
|
|
mesh::MeshPlugin,
|
2022-01-03 07:59:25 +00:00
|
|
|
primitives::{CubemapFrusta, Frustum},
|
2021-12-14 03:58:23 +00:00
|
|
|
render_graph::RenderGraph,
|
|
|
|
render_resource::{RenderPipelineCache, Shader, ShaderLoader},
|
|
|
|
renderer::render_system,
|
|
|
|
texture::ImagePlugin,
|
|
|
|
view::{ViewPlugin, WindowRenderPlugin},
|
2020-07-17 01:26:21 +00:00
|
|
|
};
|
2021-12-14 03:58:23 +00:00
|
|
|
use bevy_app::{App, AppLabel, Plugin};
|
|
|
|
use bevy_asset::{AddAsset, AssetServer};
|
|
|
|
use bevy_ecs::prelude::*;
|
|
|
|
use std::ops::{Deref, DerefMut};
|
2021-03-10 22:37:02 +00:00
|
|
|
|
2021-12-14 03:58:23 +00:00
|
|
|
/// Contains the default Bevy rendering backend based on wgpu.
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct RenderPlugin;
|
|
|
|
|
|
|
|
/// The labels of the default App rendering stages.
|
2021-02-18 21:20:37 +00:00
|
|
|
#[derive(Debug, Hash, PartialEq, Eq, Clone, StageLabel)]
|
|
|
|
pub enum RenderStage {
|
2021-12-14 03:58:23 +00:00
|
|
|
/// Extract data from the "app world" and insert it into the "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 the extracted data for the GPU.
|
|
|
|
Prepare,
|
|
|
|
|
|
|
|
/// Create [`BindGroups`](crate::render_resource::BindGroup) that depend on
|
|
|
|
/// [`Prepare`](RenderStage::Prepare) data and queue up draw calls to run during the
|
|
|
|
/// [`Render`](RenderStage::Render) stage.
|
|
|
|
Queue,
|
|
|
|
|
|
|
|
// TODO: This could probably be moved in favor of a system ordering abstraction in Render or Queue
|
|
|
|
/// Sort the [`RenderPhases`](crate::render_phase::RenderPhase) here.
|
|
|
|
PhaseSort,
|
|
|
|
|
|
|
|
/// Actual rendering happens here.
|
|
|
|
/// In most cases, only the render backend should insert resources here.
|
2021-02-18 21:20:37 +00:00
|
|
|
Render,
|
2021-12-14 03:58:23 +00:00
|
|
|
|
|
|
|
/// Cleanup render resources here.
|
|
|
|
Cleanup,
|
2020-06-01 21:33:00 +00:00
|
|
|
}
|
2020-04-06 08:57:00 +00:00
|
|
|
|
2021-12-14 03:58:23 +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
|
|
|
|
}
|
2020-05-03 17:54:30 +00:00
|
|
|
}
|
2020-04-05 21:12:14 +00:00
|
|
|
|
2021-12-14 03:58:23 +00:00
|
|
|
impl DerefMut for RenderWorld {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.0
|
2020-05-03 17:54:30 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-05 21:12:14 +00:00
|
|
|
|
2021-12-14 03:58:23 +00:00
|
|
|
/// A Label for the rendering sub-app.
|
|
|
|
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, AppLabel)]
|
|
|
|
pub struct RenderApp;
|
|
|
|
|
|
|
|
/// A "scratch" world used to avoid allocating new worlds every frame when
|
|
|
|
/// swapping out the [`RenderWorld`].
|
|
|
|
#[derive(Default)]
|
|
|
|
struct ScratchRenderWorld(World);
|
|
|
|
|
2020-08-08 03:22:17 +00:00
|
|
|
impl Plugin for RenderPlugin {
|
2021-12-14 03:58:23 +00:00
|
|
|
/// Initializes the renderer, sets up the [`RenderStage`](RenderStage) and creates the rendering sub-app.
|
2021-07-27 20:21:06 +00:00
|
|
|
fn build(&self, app: &mut App) {
|
2022-02-16 21:17:37 +00:00
|
|
|
let options = app
|
2021-12-14 03:58:23 +00:00
|
|
|
.world
|
2022-02-16 21:17:37 +00:00
|
|
|
.get_resource::<settings::WgpuSettings>()
|
2021-12-14 03:58:23 +00:00
|
|
|
.cloned()
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
2022-01-08 10:39:43 +00:00
|
|
|
app.add_asset::<Shader>()
|
2022-02-18 22:56:57 +00:00
|
|
|
.add_debug_asset::<Shader>()
|
2022-01-08 10:39:43 +00:00
|
|
|
.init_asset_loader::<ShaderLoader>()
|
2022-02-18 22:56:57 +00:00
|
|
|
.init_debug_asset_loader::<ShaderLoader>()
|
2022-01-08 10:39:43 +00:00
|
|
|
.register_type::<Color>();
|
|
|
|
|
|
|
|
if let Some(backends) = options.backends {
|
|
|
|
let instance = wgpu::Instance::new(backends);
|
|
|
|
let surface = {
|
2022-02-27 22:37:18 +00:00
|
|
|
let windows = app.world.resource_mut::<bevy_window::Windows>();
|
2022-01-08 10:39:43 +00:00
|
|
|
let raw_handle = windows.get_primary().map(|window| unsafe {
|
|
|
|
let handle = window.raw_window_handle().get_handle();
|
|
|
|
instance.create_surface(&handle)
|
|
|
|
});
|
|
|
|
raw_handle
|
|
|
|
};
|
|
|
|
let request_adapter_options = wgpu::RequestAdapterOptions {
|
|
|
|
power_preference: options.power_preference,
|
|
|
|
compatible_surface: surface.as_ref(),
|
|
|
|
..Default::default()
|
|
|
|
};
|
2022-02-16 21:17:37 +00:00
|
|
|
let (device, queue, adapter_info) = futures_lite::future::block_on(
|
|
|
|
renderer::initialize_renderer(&instance, &options, &request_adapter_options),
|
|
|
|
);
|
|
|
|
debug!("Configured wgpu adapter Limits: {:#?}", device.limits());
|
|
|
|
debug!("Configured wgpu adapter Features: {:#?}", device.features());
|
2022-01-08 10:39:43 +00:00
|
|
|
app.insert_resource(device.clone())
|
|
|
|
.insert_resource(queue.clone())
|
2022-02-16 21:17:37 +00:00
|
|
|
.insert_resource(adapter_info.clone())
|
2022-01-08 10:39:43 +00:00
|
|
|
.init_resource::<ScratchRenderWorld>()
|
|
|
|
.register_type::<Frustum>()
|
|
|
|
.register_type::<CubemapFrusta>();
|
|
|
|
let render_pipeline_cache = RenderPipelineCache::new(device.clone());
|
2022-02-27 22:37:18 +00:00
|
|
|
let asset_server = app.world.resource::<AssetServer>().clone();
|
2022-01-08 10:39:43 +00:00
|
|
|
|
|
|
|
let mut render_app = App::empty();
|
|
|
|
let mut extract_stage =
|
|
|
|
SystemStage::parallel().with_system(RenderPipelineCache::extract_shaders);
|
|
|
|
// 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())
|
|
|
|
.add_stage(RenderStage::PhaseSort, SystemStage::parallel())
|
|
|
|
.add_stage(
|
|
|
|
RenderStage::Render,
|
|
|
|
SystemStage::parallel()
|
|
|
|
.with_system(RenderPipelineCache::process_pipeline_queue_system)
|
|
|
|
.with_system(render_system.exclusive_system().at_end()),
|
|
|
|
)
|
|
|
|
.add_stage(RenderStage::Cleanup, SystemStage::parallel())
|
|
|
|
.insert_resource(instance)
|
|
|
|
.insert_resource(device)
|
|
|
|
.insert_resource(queue)
|
2022-02-16 21:17:37 +00:00
|
|
|
.insert_resource(adapter_info)
|
2022-01-08 10:39:43 +00:00
|
|
|
.insert_resource(render_pipeline_cache)
|
|
|
|
.insert_resource(asset_server)
|
|
|
|
.init_resource::<RenderGraph>();
|
|
|
|
|
|
|
|
app.add_sub_app(RenderApp, render_app, move |app_world, render_app| {
|
2021-12-14 03:58:23 +00:00
|
|
|
#[cfg(feature = "trace")]
|
2022-03-18 04:19:21 +00:00
|
|
|
let _render_span = bevy_utils::tracing::info_span!("renderer subapp").entered();
|
2022-01-08 10:39:43 +00:00
|
|
|
{
|
|
|
|
#[cfg(feature = "trace")]
|
2022-03-18 04:19:21 +00:00
|
|
|
let _stage_span =
|
|
|
|
bevy_utils::tracing::info_span!("stage", name = "reserve_and_flush")
|
|
|
|
.entered();
|
2022-01-08 10:39:43 +00:00
|
|
|
|
|
|
|
// 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")]
|
2022-03-18 04:19:21 +00:00
|
|
|
let _stage_span =
|
|
|
|
bevy_utils::tracing::info_span!("stage", name = "extract").entered();
|
2022-01-08 10:39:43 +00:00
|
|
|
|
|
|
|
// extract
|
|
|
|
extract(app_world, render_app);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
#[cfg(feature = "trace")]
|
2022-03-18 04:19:21 +00:00
|
|
|
let _stage_span =
|
|
|
|
bevy_utils::tracing::info_span!("stage", name = "prepare").entered();
|
2022-01-08 10:39:43 +00:00
|
|
|
|
|
|
|
// prepare
|
|
|
|
let prepare = render_app
|
|
|
|
.schedule
|
|
|
|
.get_stage_mut::<SystemStage>(&RenderStage::Prepare)
|
|
|
|
.unwrap();
|
|
|
|
prepare.run(&mut render_app.world);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
#[cfg(feature = "trace")]
|
2022-03-18 04:19:21 +00:00
|
|
|
let _stage_span =
|
|
|
|
bevy_utils::tracing::info_span!("stage", name = "queue").entered();
|
2022-01-08 10:39:43 +00:00
|
|
|
|
|
|
|
// queue
|
|
|
|
let queue = render_app
|
|
|
|
.schedule
|
|
|
|
.get_stage_mut::<SystemStage>(&RenderStage::Queue)
|
|
|
|
.unwrap();
|
|
|
|
queue.run(&mut render_app.world);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
#[cfg(feature = "trace")]
|
2022-03-18 04:19:21 +00:00
|
|
|
let _stage_span =
|
|
|
|
bevy_utils::tracing::info_span!("stage", name = "sort").entered();
|
2022-01-08 10:39:43 +00:00
|
|
|
|
|
|
|
// 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")]
|
2022-03-18 04:19:21 +00:00
|
|
|
let _stage_span =
|
|
|
|
bevy_utils::tracing::info_span!("stage", name = "render").entered();
|
2022-01-08 10:39:43 +00:00
|
|
|
|
|
|
|
// render
|
|
|
|
let render = render_app
|
|
|
|
.schedule
|
|
|
|
.get_stage_mut::<SystemStage>(&RenderStage::Render)
|
|
|
|
.unwrap();
|
|
|
|
render.run(&mut render_app.world);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
#[cfg(feature = "trace")]
|
2022-03-18 04:19:21 +00:00
|
|
|
let _stage_span =
|
|
|
|
bevy_utils::tracing::info_span!("stage", name = "cleanup").entered();
|
2022-01-08 10:39:43 +00:00
|
|
|
|
|
|
|
// 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-12-14 03:58:23 +00:00
|
|
|
|
|
|
|
app.add_plugin(WindowRenderPlugin)
|
|
|
|
.add_plugin(CameraPlugin)
|
|
|
|
.add_plugin(ViewPlugin)
|
|
|
|
.add_plugin(MeshPlugin)
|
2022-03-15 22:26:46 +00:00
|
|
|
// NOTE: Load this after renderer initialization so that it knows about the supported
|
|
|
|
// compressed texture formats
|
2021-12-14 03:58:23 +00:00
|
|
|
.add_plugin(ImagePlugin);
|
2020-04-05 21:12:14 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-19 22:16:24 +00:00
|
|
|
|
2021-12-14 03:58:23 +00:00
|
|
|
/// Executes the [`Extract`](RenderStage::Extract) stage of the renderer.
|
|
|
|
/// This updates the render world with the extracted ECS data of the current frame.
|
|
|
|
fn extract(app_world: &mut World, render_app: &mut App) {
|
|
|
|
let extract = render_app
|
|
|
|
.schedule
|
|
|
|
.get_stage_mut::<SystemStage>(&RenderStage::Extract)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
|
|
|
|
extract.run(app_world);
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
|
|
|
|
extract.apply_buffers(&mut render_app.world);
|
2021-04-19 22:16:24 +00:00
|
|
|
}
|