render: add ClearColor resource

This commit is contained in:
Carter Anderson 2020-06-25 15:24:27 -07:00
parent dfde160741
commit 8a8d01aa88
6 changed files with 49 additions and 1 deletions

View file

@ -187,6 +187,10 @@ path = "examples/ui/ui.rs"
name = "ui_bench"
path = "examples/ui/ui_bench.rs"
[[example]]
name = "clear_color"
path = "examples/window/clear_color.rs"
[[example]]
name = "multiple_windows"
path = "examples/window/multiple_windows.rs"

View file

@ -114,6 +114,8 @@ impl BaseRenderGraphBuilder for RenderGraph {
sample_count: 1,
});
main_pass_node.use_default_clear_color(0);
if config.add_3d_camera {
main_pass_node.add_camera(camera::CAMERA);
}

View file

@ -18,6 +18,28 @@ impl TextureAttachment {
}
}
#[derive(Clone, Debug)]
pub struct ClearColor {
pub color: Color,
}
impl ClearColor {
pub fn new(color: Color) -> Self {
ClearColor {
color,
}
}
}
impl Default for ClearColor {
fn default() -> Self {
Self {
color: Color::rgb(0.1, 0.1, 0.1),
}
}
}
#[derive(Debug, Clone)]
pub struct RenderPassColorAttachmentDescriptor {
/// The actual color attachment.

View file

@ -1,6 +1,6 @@
use crate::{
draw::{Draw, RenderCommand},
pass::{PassDescriptor, TextureAttachment},
pass::{PassDescriptor, TextureAttachment, ClearColor},
pipeline::PipelineDescriptor,
render_graph::{Node, ResourceSlotInfo, ResourceSlots},
render_resource::{BindGroupId, BufferId, RenderResourceBindings, RenderResourceType},
@ -15,6 +15,7 @@ pub struct PassNode {
cameras: Vec<String>,
color_attachment_input_indices: Vec<Option<usize>>,
depth_stencil_attachment_input_index: Option<usize>,
default_clear_color_inputs: Vec<usize>,
}
impl PassNode {
@ -50,12 +51,17 @@ impl PassNode {
cameras: Vec::new(),
color_attachment_input_indices,
depth_stencil_attachment_input_index,
default_clear_color_inputs: Vec::new(),
}
}
pub fn add_camera(&mut self, camera_name: &str) {
self.cameras.push(camera_name.to_string());
}
pub fn use_default_clear_color(&mut self, color_attachment_index: usize) {
self.default_clear_color_inputs.push(color_attachment_index);
}
}
impl Node for PassNode {
@ -76,6 +82,11 @@ impl Node for PassNode {
let active_cameras= resources.get::<ActiveCameras>().unwrap();
for (i, color_attachment) in self.descriptor.color_attachments.iter_mut().enumerate() {
if self.default_clear_color_inputs.contains(&i) {
if let Some(default_clear_color) = resources.get::<ClearColor>() {
color_attachment.clear_color = default_clear_color.color;
}
}
if let Some(input_index) = self.color_attachment_input_indices[i] {
color_attachment.attachment =
TextureAttachment::Id(input.get(input_index).unwrap().get_texture().unwrap());

View file

@ -0,0 +1,8 @@
use bevy::prelude::*;
fn main() {
App::build()
.add_resource(ClearColor::new(Color::rgb(0.2, 0.2, 0.8)))
.add_default_plugins()
.run();
}

View file

@ -17,6 +17,7 @@ pub use crate::{
draw::Draw,
entity::*,
mesh::{shape, Mesh},
pass::ClearColor,
pipeline::{PipelineDescriptor, RenderPipelines},
render_graph::{
nodes::{