From 71b37556339ae976ea7dbef4e8d2791bc630fb26 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Fri, 29 May 2020 22:30:07 -0700 Subject: [PATCH] camera: split 2d and ui camera. remove resource_name mod --- crates/bevy_pbr/src/nodes/lights_node.rs | 16 +++++---- .../bevy_render/src/base_render_graph/mod.rs | 12 +++++-- .../assigned_meshes_draw_target.rs | 8 +++-- .../draw_targets/meshes_draw_target.rs | 8 +++-- crates/bevy_render/src/entity.rs | 9 +++-- crates/bevy_render/src/render_resource/mod.rs | 1 - .../src/render_resource/resource_name.rs | 33 ------------------- crates/bevy_ui/src/render/mod.rs | 26 ++++++++++++--- crates/bevy_ui/src/render/ui.vert | 2 +- examples/2d/sprite.rs | 2 +- examples/ui/text.rs | 2 +- examples/ui/ui.rs | 4 +-- examples/ui/ui_bench.rs | 2 +- src/prelude.rs | 1 - 14 files changed, 61 insertions(+), 65 deletions(-) delete mode 100644 crates/bevy_render/src/render_resource/resource_name.rs diff --git a/crates/bevy_pbr/src/nodes/lights_node.rs b/crates/bevy_pbr/src/nodes/lights_node.rs index 504496b657..9c4980c69e 100644 --- a/crates/bevy_pbr/src/nodes/lights_node.rs +++ b/crates/bevy_pbr/src/nodes/lights_node.rs @@ -1,7 +1,8 @@ use bevy_render::{ + base_render_graph, render_graph::{CommandQueue, Node, ResourceSlots, SystemNode}, render_resource::{ - resource_name, BufferInfo, BufferUsage, RenderResourceAssignment, RenderResourceAssignments, + BufferInfo, BufferUsage, RenderResourceAssignment, RenderResourceAssignments, }, renderer::{RenderContext, RenderResources}, }; @@ -55,10 +56,10 @@ impl SystemNode for LightsNode { let mut command_queue = self.command_queue.clone(); let max_lights = self.max_lights; (move |world: &mut SubWorld, - render_resources: Res, - // TODO: this write on RenderResourceAssignments will prevent this system from running in parallel with other systems that do the same - mut render_resource_assignments: ResMut, - query: &mut Query<(Read, Read, Read)>| { + render_resources: Res, + // TODO: this write on RenderResourceAssignments will prevent this system from running in parallel with other systems that do the same + mut render_resource_assignments: ResMut, + query: &mut Query<(Read, Read, Read)>| { if !lights_are_dirty { return; } @@ -76,7 +77,7 @@ impl SystemNode for LightsNode { ..Default::default() }); render_resource_assignments.set( - resource_name::uniform::LIGHTS, + base_render_graph::uniform::LIGHTS, RenderResourceAssignment::Buffer { resource: buffer, range: 0..light_uniform_size as u64, @@ -147,6 +148,7 @@ impl SystemNode for LightsNode { light_count_size as u64, total_size as u64, ); - }).system() + }) + .system() } } diff --git a/crates/bevy_render/src/base_render_graph/mod.rs b/crates/bevy_render/src/base_render_graph/mod.rs index c2909b0161..ffedb0568f 100644 --- a/crates/bevy_render/src/base_render_graph/mod.rs +++ b/crates/bevy_render/src/base_render_graph/mod.rs @@ -13,7 +13,7 @@ use crate::{ texture::{ Extent3d, Texture, TextureDescriptor, TextureDimension, TextureFormat, TextureUsage, }, - Color, render_resource::resource_name, + Color, }; use bevy_app::GetEventReader; use bevy_asset::AssetEvent; @@ -38,6 +38,12 @@ pub mod node { pub const MAIN_PASS: &str = "main_pass"; } +pub mod uniform { + pub const CAMERA: &str = "Camera"; + pub const CAMERA2D: &str = "Camera2d"; + pub const LIGHTS: &str = "Lights"; +} + impl Default for BaseRenderGraphConfig { fn default() -> Self { BaseRenderGraphConfig { @@ -72,11 +78,11 @@ impl BaseRenderGraphBuilder for RenderGraph { TextureCopyNode::new(resources.get_event_reader::>()), ); if config.add_3d_camera { - self.add_system_node(node::CAMERA, CameraNode::new(resource_name::uniform::CAMERA)); + self.add_system_node(node::CAMERA, CameraNode::new(uniform::CAMERA)); } if config.add_2d_camera { - self.add_system_node(node::CAMERA2D, CameraNode::new(resource_name::uniform::CAMERA2D)); + self.add_system_node(node::CAMERA2D, CameraNode::new(uniform::CAMERA2D)); } if config.add_main_depth_texture { diff --git a/crates/bevy_render/src/draw_target/draw_targets/assigned_meshes_draw_target.rs b/crates/bevy_render/src/draw_target/draw_targets/assigned_meshes_draw_target.rs index 0ad94c3e06..71db0febd8 100644 --- a/crates/bevy_render/src/draw_target/draw_targets/assigned_meshes_draw_target.rs +++ b/crates/bevy_render/src/draw_target/draw_targets/assigned_meshes_draw_target.rs @@ -7,7 +7,7 @@ use crate::{ pass::RenderPass, pipeline::{PipelineAssignments, PipelineDescriptor}, render_resource::{ - resource_name, EntitiesWaitingForAssets, EntityRenderResourceAssignments, + EntitiesWaitingForAssets, EntityRenderResourceAssignments, RenderResourceAssignments, ResourceInfo, }, renderer::RenderContext, @@ -17,6 +17,10 @@ use crate::{ #[derive(Default)] pub struct AssignedMeshesDrawTarget; +impl AssignedMeshesDrawTarget { + pub const NAME: &'static str = "AssignedMeshes"; +} + impl DrawTarget for AssignedMeshesDrawTarget { fn draw( &self, @@ -129,6 +133,6 @@ impl DrawTarget for AssignedMeshesDrawTarget { } fn get_name(&self) -> String { - resource_name::draw_target::ASSIGNED_MESHES.to_string() + AssignedMeshesDrawTarget::NAME.to_string() } } diff --git a/crates/bevy_render/src/draw_target/draw_targets/meshes_draw_target.rs b/crates/bevy_render/src/draw_target/draw_targets/meshes_draw_target.rs index 0ce955f3fc..3add484f7b 100644 --- a/crates/bevy_render/src/draw_target/draw_targets/meshes_draw_target.rs +++ b/crates/bevy_render/src/draw_target/draw_targets/meshes_draw_target.rs @@ -3,7 +3,7 @@ use crate::{ mesh::{self, Mesh}, pass::RenderPass, pipeline::PipelineDescriptor, - render_resource::{resource_name, ResourceInfo}, + render_resource::ResourceInfo, Renderable, }; use bevy_asset::Handle; @@ -12,6 +12,10 @@ use legion::prelude::*; #[derive(Default)] pub struct MeshesDrawTarget; +impl MeshesDrawTarget { + pub const NAME: &'static str = "Meshes"; +} + impl DrawTarget for MeshesDrawTarget { fn draw( &self, @@ -62,6 +66,6 @@ impl DrawTarget for MeshesDrawTarget { } fn get_name(&self) -> String { - resource_name::draw_target::MESHES.to_string() + MeshesDrawTarget::NAME.to_string() } } diff --git a/crates/bevy_render/src/entity.rs b/crates/bevy_render/src/entity.rs index 74e6a09f89..5ae9aac7f9 100644 --- a/crates/bevy_render/src/entity.rs +++ b/crates/bevy_render/src/entity.rs @@ -1,4 +1,4 @@ -use crate::{mesh::Mesh, Camera, Renderable, OrthographicCamera, PerspectiveCamera, render_resource::resource_name}; +use crate::{mesh::Mesh, Camera, Renderable, OrthographicCamera, PerspectiveCamera, base_render_graph}; use bevy_asset::Handle; use bevy_derive::EntityArchetype; use bevy_transform::components::{LocalToWorld, Rotation, Scale, Translation}; @@ -25,7 +25,7 @@ impl Default for PerspectiveCameraEntity { fn default() -> Self { PerspectiveCameraEntity { camera: Camera { - name: Some(resource_name::uniform::CAMERA.to_string()), + name: Some(base_render_graph::uniform::CAMERA.to_string()), ..Default::default() }, perspective_camera: Default::default(), @@ -46,8 +46,7 @@ impl OrthographicCameraEntity { pub fn ui() -> Self { OrthographicCameraEntity { camera: Camera { - // TODO: ui should have its own uniform - name: Some(resource_name::uniform::CAMERA2D.to_string()), + name: Some("UiCamera".to_string()), ..Default::default() }, orthographic_camera: Default::default(), @@ -60,7 +59,7 @@ impl Default for OrthographicCameraEntity { fn default() -> Self { OrthographicCameraEntity { camera: Camera { - name: Some(resource_name::uniform::CAMERA2D.to_string()), + name: Some(base_render_graph::uniform::CAMERA2D.to_string()), ..Default::default() }, orthographic_camera: Default::default(), diff --git a/crates/bevy_render/src/render_resource/mod.rs b/crates/bevy_render/src/render_resource/mod.rs index 61486e7f05..0882bf62ae 100644 --- a/crates/bevy_render/src/render_resource/mod.rs +++ b/crates/bevy_render/src/render_resource/mod.rs @@ -4,7 +4,6 @@ mod entity_render_resource_assignments; mod render_resource; mod render_resource_assignments; mod resource_info; -pub mod resource_name; pub use buffer::*; pub use entities_waiting_for_assets::*; diff --git a/crates/bevy_render/src/render_resource/resource_name.rs b/crates/bevy_render/src/render_resource/resource_name.rs deleted file mode 100644 index cea5ed3abe..0000000000 --- a/crates/bevy_render/src/render_resource/resource_name.rs +++ /dev/null @@ -1,33 +0,0 @@ -pub mod texture { - pub const SWAP_CHAIN: &str = "SwapChain"; - pub const DEPTH: &str = "Depth"; -} - -pub mod uniform { - pub const CAMERA: &str = "Camera"; - pub const CAMERA2D: &str = "Camera2d"; - pub const LIGHTS: &str = "Lights"; -} - -pub mod buffer { - pub const UI_INSTANCES: &str = "UiInstances"; - pub const TEMP_MESH_VERTEX_BUFFER_NAME: &str = "TempMeshVertexBuffer"; - pub const TEMP_MESH_INDEX_BUFFER_NAME: &str = "TempMeshIndexBuffer"; -} - -pub mod draw_target { - pub const MESHES: &str = "Meshes"; - pub const ASSIGNED_MESHES: &str = "AssignedMeshes"; - pub const ASSIGNED_BATCHES: &str = "AssignedBatches"; - pub const UI: &str = "Ui"; -} - -pub mod pass { - pub const MAIN: &str = "Main"; -} - -pub mod pipeline { - pub const FORWARD: &str = "Forward"; - pub const FORWARD_FLAT: &str = "ForwardFlat"; - pub const UI: &str = "Ui"; -} diff --git a/crates/bevy_ui/src/render/mod.rs b/crates/bevy_ui/src/render/mod.rs index edf162e85c..990b770dfa 100644 --- a/crates/bevy_ui/src/render/mod.rs +++ b/crates/bevy_ui/src/render/mod.rs @@ -5,7 +5,7 @@ use bevy_render::{ draw_target::AssignedMeshesDrawTarget, pipeline::{state_descriptors::*, PipelineDescriptor}, render_graph::{ - nodes::{AssetUniformNode, PassNode, UniformNode}, + nodes::{AssetUniformNode, PassNode, UniformNode, CameraNode}, RenderGraph, }, shader::{Shader, ShaderStage, ShaderStages}, @@ -61,6 +61,16 @@ pub fn build_ui_pipeline(shaders: &mut Assets) -> PipelineDescriptor { } } +pub mod node { + pub const COLOR_MATERIAL: &'static str = "color_material"; + pub const UI_CAMERA: &'static str = "ui_camera"; + pub const RECT: &'static str = "rect"; +} + +pub mod uniform { + pub const UI_CAMERA: &'static str = "UiCamera"; +} + pub trait UiRenderGraphBuilder { fn add_ui_graph(&mut self, resources: &Resources) -> &mut Self; } @@ -68,14 +78,20 @@ pub trait UiRenderGraphBuilder { impl UiRenderGraphBuilder for RenderGraph { fn add_ui_graph(&mut self, resources: &Resources) -> &mut Self { self.add_system_node( - "color_material", + node::COLOR_MATERIAL, AssetUniformNode::::new(false), ); - self.add_node_edge("color_material", base_render_graph::node::MAIN_PASS) + self.add_node_edge(node::COLOR_MATERIAL, base_render_graph::node::MAIN_PASS) .unwrap(); - self.add_system_node("rect", UniformNode::::new(false)); - self.add_node_edge("rect", base_render_graph::node::MAIN_PASS) + + self.add_system_node(node::UI_CAMERA, CameraNode::new(uniform::UI_CAMERA)); + self.add_node_edge(node::UI_CAMERA, base_render_graph::node::MAIN_PASS) .unwrap(); + + self.add_system_node(node::RECT, UniformNode::::new(false)); + self.add_node_edge(node::RECT, base_render_graph::node::MAIN_PASS) + .unwrap(); + let mut pipelines = resources.get_mut::>().unwrap(); let mut shaders = resources.get_mut::>().unwrap(); pipelines.set(UI_PIPELINE_HANDLE, build_ui_pipeline(&mut shaders)); diff --git a/crates/bevy_ui/src/render/ui.vert b/crates/bevy_ui/src/render/ui.vert index b4a3b334a7..a2e94f9d94 100644 --- a/crates/bevy_ui/src/render/ui.vert +++ b/crates/bevy_ui/src/render/ui.vert @@ -6,7 +6,7 @@ layout(location = 2) in vec2 Vertex_Uv; layout(location = 0) out vec2 v_Uv; -layout(set = 0, binding = 0) uniform Camera2d { +layout(set = 0, binding = 0) uniform UiCamera { mat4 ViewProj; }; diff --git a/examples/2d/sprite.rs b/examples/2d/sprite.rs index 5c87d27795..89b91a0d34 100644 --- a/examples/2d/sprite.rs +++ b/examples/2d/sprite.rs @@ -15,7 +15,7 @@ fn setup( let texture_handle = asset_server.load("assets/branding/icon.png").unwrap(); command_buffer .build() - .add_entity(OrthographicCameraEntity::default()) + .add_entity(OrthographicCameraEntity::ui()) .add_entity(SpriteEntity { rect: Rect { position: Vec2::new(300.0, 300.0), diff --git a/examples/ui/text.rs b/examples/ui/text.rs index 009d171dcb..6927ee683a 100644 --- a/examples/ui/text.rs +++ b/examples/ui/text.rs @@ -25,7 +25,7 @@ fn setup(command_buffer: &mut CommandBuffer, asset_server: Res) { command_buffer .build() // 2d camera - .add_entity(OrthographicCameraEntity::default()) + .add_entity(OrthographicCameraEntity::ui()) // texture .add_entity(LabelEntity { node: Node::new( diff --git a/examples/ui/ui.rs b/examples/ui/ui.rs index 2dbf4854b2..635bfcfd38 100644 --- a/examples/ui/ui.rs +++ b/examples/ui/ui.rs @@ -57,8 +57,8 @@ fn setup( // )), // ..Default::default() // }) - // 2d camera - .add_entity(OrthographicCameraEntity::default()) + // ui camera + .add_entity(OrthographicCameraEntity::ui()) // left vertical fill .add_entity(UiEntity { node: Node::new( diff --git a/examples/ui/ui_bench.rs b/examples/ui/ui_bench.rs index 9a9d169248..fe751a3670 100644 --- a/examples/ui/ui_bench.rs +++ b/examples/ui/ui_bench.rs @@ -23,7 +23,7 @@ fn placement_system( fn setup(command_buffer: &mut CommandBuffer, mut materials: ResMut>) { let mut builder = command_buffer.build(); - builder.add_entity(OrthographicCameraEntity::default()); + builder.add_entity(OrthographicCameraEntity::ui()); let mut prev = Vec2::default(); let count = 1000; diff --git a/src/prelude.rs b/src/prelude.rs index 8e5be21561..c65576daba 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -27,7 +27,6 @@ pub use crate::render::{ }, RenderGraph, }, - render_resource::resource_name, shader::{Shader, ShaderDefSuffixProvider, ShaderStage, ShaderStages}, texture::{Texture, TextureType}, Camera, OrthographicCamera, PerspectiveCamera, Color, ColorSource, Renderable,