2020-07-17 01:26:21 +00:00
|
|
|
use super::{
|
|
|
|
CameraNode, PassNode, RenderGraph, SharedBuffersNode, TextureCopyNode, WindowSwapChainNode,
|
|
|
|
WindowTextureNode,
|
|
|
|
};
|
2020-05-03 17:54:30 +00:00
|
|
|
use crate::{
|
|
|
|
pass::{
|
2020-07-10 20:47:31 +00:00
|
|
|
LoadOp, Operations, PassDescriptor, RenderPassColorAttachmentDescriptor,
|
|
|
|
RenderPassDepthStencilAttachmentDescriptor, TextureAttachment,
|
2020-05-03 17:54:30 +00:00
|
|
|
},
|
2020-06-04 06:53:00 +00:00
|
|
|
texture::{Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsage},
|
2020-05-30 05:30:07 +00:00
|
|
|
Color,
|
2020-05-03 17:54:30 +00:00
|
|
|
};
|
2020-08-09 23:13:04 +00:00
|
|
|
use bevy_property::Properties;
|
2020-07-25 06:04:45 +00:00
|
|
|
use bevy_window::WindowId;
|
2020-05-03 17:54:30 +00:00
|
|
|
|
2020-08-09 23:13:04 +00:00
|
|
|
/// A component that indicates that an entity should be drawn in the "main pass"
|
|
|
|
#[derive(Default, Properties)]
|
|
|
|
pub struct MainPass;
|
|
|
|
|
2020-07-30 01:15:15 +00:00
|
|
|
pub struct Msaa {
|
|
|
|
pub samples: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Msaa {
|
|
|
|
fn default() -> Self {
|
2020-07-30 21:35:07 +00:00
|
|
|
Self { samples: 1 }
|
2020-07-30 20:20:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Msaa {
|
|
|
|
pub fn color_attachment_descriptor(
|
|
|
|
&self,
|
|
|
|
attachment: TextureAttachment,
|
|
|
|
resolve_target: TextureAttachment,
|
|
|
|
ops: Operations<Color>,
|
|
|
|
) -> RenderPassColorAttachmentDescriptor {
|
|
|
|
if self.samples > 1 {
|
|
|
|
RenderPassColorAttachmentDescriptor {
|
|
|
|
attachment,
|
|
|
|
resolve_target: Some(resolve_target),
|
|
|
|
ops,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
RenderPassColorAttachmentDescriptor {
|
|
|
|
attachment,
|
|
|
|
resolve_target: None,
|
|
|
|
ops,
|
|
|
|
}
|
2020-07-30 01:15:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-03 17:54:30 +00:00
|
|
|
pub struct BaseRenderGraphConfig {
|
2020-05-06 01:44:32 +00:00
|
|
|
pub add_2d_camera: bool,
|
|
|
|
pub add_3d_camera: bool,
|
2020-05-11 16:01:31 +00:00
|
|
|
pub add_main_depth_texture: bool,
|
2020-05-06 01:44:32 +00:00
|
|
|
pub add_main_pass: bool,
|
|
|
|
pub connect_main_pass_to_swapchain: bool,
|
2020-05-11 16:01:31 +00:00
|
|
|
pub connect_main_pass_to_main_depth_texture: bool,
|
2020-05-03 17:54:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub mod node {
|
|
|
|
pub const PRIMARY_SWAP_CHAIN: &str = "swapchain";
|
2020-06-26 06:04:08 +00:00
|
|
|
pub const CAMERA3D: &str = "camera3d";
|
2020-05-03 17:54:30 +00:00
|
|
|
pub const CAMERA2D: &str = "camera2d";
|
2020-05-18 18:31:11 +00:00
|
|
|
pub const TEXTURE_COPY: &str = "texture_copy";
|
2020-05-11 16:01:31 +00:00
|
|
|
pub const MAIN_DEPTH_TEXTURE: &str = "main_pass_depth_texture";
|
2020-07-30 01:15:15 +00:00
|
|
|
pub const MAIN_SAMPLED_COLOR_ATTACHMENT: &str = "main_pass_sampled_color_attachment";
|
2020-05-03 17:54:30 +00:00
|
|
|
pub const MAIN_PASS: &str = "main_pass";
|
2020-06-17 05:23:33 +00:00
|
|
|
pub const SHARED_BUFFERS: &str = "shared_buffers";
|
2020-05-03 17:54:30 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 22:58:06 +00:00
|
|
|
pub mod camera {
|
2020-06-26 06:04:08 +00:00
|
|
|
pub const CAMERA3D: &str = "Camera3d";
|
2020-05-30 05:30:07 +00:00
|
|
|
pub const CAMERA2D: &str = "Camera2d";
|
|
|
|
}
|
|
|
|
|
2020-05-03 17:54:30 +00:00
|
|
|
impl Default for BaseRenderGraphConfig {
|
|
|
|
fn default() -> Self {
|
|
|
|
BaseRenderGraphConfig {
|
|
|
|
add_2d_camera: true,
|
|
|
|
add_3d_camera: true,
|
|
|
|
add_main_pass: true,
|
2020-05-11 16:01:31 +00:00
|
|
|
add_main_depth_texture: true,
|
2020-05-03 17:54:30 +00:00
|
|
|
connect_main_pass_to_swapchain: true,
|
2020-05-11 16:01:31 +00:00
|
|
|
connect_main_pass_to_main_depth_texture: true,
|
2020-05-03 17:54:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// The "base render graph" provides a core set of render graph nodes which can be used to build any graph.
|
|
|
|
/// By itself this graph doesn't do much, but it allows Render plugins to interop with each other by having a common
|
|
|
|
/// set of nodes. It can be customized using `BaseRenderGraphConfig`.
|
|
|
|
pub trait BaseRenderGraphBuilder {
|
2020-07-30 01:15:15 +00:00
|
|
|
fn add_base_graph(&mut self, config: &BaseRenderGraphConfig, msaa: &Msaa) -> &mut Self;
|
2020-05-03 17:54:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BaseRenderGraphBuilder for RenderGraph {
|
2020-07-30 01:15:15 +00:00
|
|
|
fn add_base_graph(&mut self, config: &BaseRenderGraphConfig, msaa: &Msaa) -> &mut Self {
|
2020-06-04 06:53:00 +00:00
|
|
|
self.add_node(node::TEXTURE_COPY, TextureCopyNode::default());
|
2020-05-03 17:54:30 +00:00
|
|
|
if config.add_3d_camera {
|
2020-06-26 06:04:08 +00:00
|
|
|
self.add_system_node(node::CAMERA3D, CameraNode::new(camera::CAMERA3D));
|
2020-05-03 17:54:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.add_2d_camera {
|
2020-06-23 22:58:06 +00:00
|
|
|
self.add_system_node(node::CAMERA2D, CameraNode::new(camera::CAMERA2D));
|
2020-05-03 17:54:30 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 05:23:33 +00:00
|
|
|
self.add_node(node::SHARED_BUFFERS, SharedBuffersNode::default());
|
2020-05-11 16:01:31 +00:00
|
|
|
if config.add_main_depth_texture {
|
2020-05-14 00:35:48 +00:00
|
|
|
self.add_node(
|
2020-05-11 16:01:31 +00:00
|
|
|
node::MAIN_DEPTH_TEXTURE,
|
2020-05-03 17:54:30 +00:00
|
|
|
WindowTextureNode::new(
|
2020-07-25 06:04:45 +00:00
|
|
|
WindowId::primary(),
|
2020-05-03 17:54:30 +00:00
|
|
|
TextureDescriptor {
|
|
|
|
size: Extent3d {
|
|
|
|
depth: 1,
|
|
|
|
width: 1,
|
|
|
|
height: 1,
|
|
|
|
},
|
|
|
|
mip_level_count: 1,
|
2020-07-30 01:15:15 +00:00
|
|
|
sample_count: msaa.samples,
|
2020-05-03 17:54:30 +00:00
|
|
|
dimension: TextureDimension::D2,
|
|
|
|
format: TextureFormat::Depth32Float, // PERF: vulkan docs recommend using 24 bit depth for better performance
|
|
|
|
usage: TextureUsage::OUTPUT_ATTACHMENT,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
2020-05-11 16:01:31 +00:00
|
|
|
}
|
2020-05-03 17:54:30 +00:00
|
|
|
|
2020-05-11 16:01:31 +00:00
|
|
|
if config.add_main_pass {
|
2020-07-30 20:20:27 +00:00
|
|
|
let mut main_pass_node = PassNode::<&MainPass>::new(PassDescriptor {
|
|
|
|
color_attachments: vec![msaa.color_attachment_descriptor(
|
|
|
|
TextureAttachment::Input("color_attachment".to_string()),
|
|
|
|
TextureAttachment::Input("color_resolve_target".to_string()),
|
|
|
|
Operations {
|
2020-07-10 20:47:31 +00:00
|
|
|
load: LoadOp::Clear(Color::rgb(0.1, 0.1, 0.1)),
|
|
|
|
store: true,
|
|
|
|
},
|
2020-07-30 20:20:27 +00:00
|
|
|
)],
|
2020-06-23 23:52:50 +00:00
|
|
|
depth_stencil_attachment: Some(RenderPassDepthStencilAttachmentDescriptor {
|
|
|
|
attachment: TextureAttachment::Input("depth".to_string()),
|
2020-07-10 20:47:31 +00:00
|
|
|
depth_ops: Some(Operations {
|
|
|
|
load: LoadOp::Clear(1.0),
|
|
|
|
store: true,
|
|
|
|
}),
|
|
|
|
stencil_ops: None,
|
2020-06-23 23:52:50 +00:00
|
|
|
}),
|
2020-07-30 01:15:15 +00:00
|
|
|
sample_count: msaa.samples,
|
2020-06-23 23:52:50 +00:00
|
|
|
});
|
|
|
|
|
2020-06-25 22:24:27 +00:00
|
|
|
main_pass_node.use_default_clear_color(0);
|
|
|
|
|
2020-06-23 23:52:50 +00:00
|
|
|
if config.add_3d_camera {
|
2020-06-26 06:04:08 +00:00
|
|
|
main_pass_node.add_camera(camera::CAMERA3D);
|
2020-06-23 23:52:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.add_2d_camera {
|
|
|
|
main_pass_node.add_camera(camera::CAMERA2D);
|
|
|
|
}
|
|
|
|
|
2020-07-10 08:37:06 +00:00
|
|
|
self.add_node(node::MAIN_PASS, main_pass_node);
|
2020-05-03 17:54:30 +00:00
|
|
|
|
2020-05-18 18:31:11 +00:00
|
|
|
self.add_node_edge(node::TEXTURE_COPY, node::MAIN_PASS)
|
|
|
|
.unwrap();
|
2020-06-17 05:23:33 +00:00
|
|
|
self.add_node_edge(node::SHARED_BUFFERS, node::MAIN_PASS)
|
|
|
|
.unwrap();
|
|
|
|
|
2020-05-03 17:54:30 +00:00
|
|
|
if config.add_3d_camera {
|
2020-06-26 06:04:08 +00:00
|
|
|
self.add_node_edge(node::CAMERA3D, node::MAIN_PASS).unwrap();
|
2020-05-03 17:54:30 +00:00
|
|
|
}
|
2020-05-06 01:44:32 +00:00
|
|
|
|
2020-05-03 17:54:30 +00:00
|
|
|
if config.add_2d_camera {
|
|
|
|
self.add_node_edge(node::CAMERA2D, node::MAIN_PASS).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 00:35:48 +00:00
|
|
|
self.add_node(
|
2020-05-03 17:54:30 +00:00
|
|
|
node::PRIMARY_SWAP_CHAIN,
|
2020-07-25 06:04:45 +00:00
|
|
|
WindowSwapChainNode::new(WindowId::primary()),
|
2020-05-03 17:54:30 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if config.connect_main_pass_to_swapchain {
|
|
|
|
self.add_slot_edge(
|
|
|
|
node::PRIMARY_SWAP_CHAIN,
|
|
|
|
WindowSwapChainNode::OUT_TEXTURE,
|
|
|
|
node::MAIN_PASS,
|
2020-07-30 01:15:15 +00:00
|
|
|
if msaa.samples > 1 {
|
|
|
|
"color_resolve_target"
|
|
|
|
} else {
|
|
|
|
"color_attachment"
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
if msaa.samples > 1 {
|
|
|
|
self.add_node(
|
|
|
|
node::MAIN_SAMPLED_COLOR_ATTACHMENT,
|
|
|
|
WindowTextureNode::new(
|
|
|
|
WindowId::primary(),
|
|
|
|
TextureDescriptor {
|
|
|
|
size: Extent3d {
|
|
|
|
depth: 1,
|
|
|
|
width: 1,
|
|
|
|
height: 1,
|
|
|
|
},
|
|
|
|
mip_level_count: 1,
|
|
|
|
sample_count: msaa.samples,
|
|
|
|
dimension: TextureDimension::D2,
|
|
|
|
format: TextureFormat::Bgra8UnormSrgb,
|
|
|
|
usage: TextureUsage::OUTPUT_ATTACHMENT,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
self.add_slot_edge(
|
|
|
|
node::MAIN_SAMPLED_COLOR_ATTACHMENT,
|
|
|
|
WindowSwapChainNode::OUT_TEXTURE,
|
|
|
|
node::MAIN_PASS,
|
|
|
|
"color_attachment",
|
2020-05-03 17:54:30 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
2020-05-11 16:01:31 +00:00
|
|
|
if config.connect_main_pass_to_main_depth_texture {
|
|
|
|
self.add_slot_edge(
|
|
|
|
node::MAIN_DEPTH_TEXTURE,
|
|
|
|
WindowTextureNode::OUT_TEXTURE,
|
|
|
|
node::MAIN_PASS,
|
|
|
|
"depth",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
2020-05-03 17:54:30 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|