ui render graph

This commit is contained in:
Carter Anderson 2020-05-03 12:35:07 -07:00
parent 5537eabb8c
commit dac9cac776
7 changed files with 130 additions and 68 deletions

View file

@ -7,7 +7,7 @@ use std::{any::TypeId, marker::PhantomData};
use uuid::Uuid;
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct HandleId(Uuid);
pub struct HandleId(pub Uuid);
pub const DEFAULT_HANDLE_ID: HandleId = HandleId(Uuid::from_bytes([
238, 232, 56, 216, 245, 246, 77, 29, 165, 188, 211, 202, 249, 248, 15, 4,
]));
@ -24,13 +24,20 @@ pub struct Handle<T> {
}
impl<T> Handle<T> {
pub fn new(id: HandleId) -> Self {
pub const fn new(id: HandleId) -> Self {
Handle {
id,
marker: PhantomData,
}
}
pub const fn from_bytes(bytes: [u8; 16]) -> Self {
Handle {
id: HandleId(Uuid::from_bytes(bytes)),
marker: PhantomData,
}
}
pub fn from_untyped(untyped_handle: HandleUntyped) -> Option<Handle<T>>
where
T: 'static,

View file

@ -87,6 +87,7 @@ pub enum UniformPropertyType {
Int,
Float,
UVec4,
Vec2,
Vec3,
Vec4,
Mat3,
@ -101,6 +102,7 @@ impl UniformPropertyType {
UniformPropertyType::Int => 4,
UniformPropertyType::Float => 4,
UniformPropertyType::UVec4 => 4 * 4,
UniformPropertyType::Vec2 => 4 * 2,
UniformPropertyType::Vec3 => 4 * 3,
UniformPropertyType::Vec4 => 4 * 4,
UniformPropertyType::Mat3 => 4 * 4 * 3,

View file

@ -269,6 +269,8 @@ fn reflect_uniform_numeric(type_description: &ReflectTypeDescription) -> Uniform
} else {
match (number_type, traits.numeric.vector.component_count) {
(NumberType::Int, 1) => UniformPropertyType::Int,
(NumberType::Float, 0) => UniformPropertyType::Float,
(NumberType::Float, 2) => UniformPropertyType::Vec2,
(NumberType::Float, 3) => UniformPropertyType::Vec3,
(NumberType::Float, 4) => UniformPropertyType::Vec4,
(NumberType::UInt, 4) => UniformPropertyType::UVec4,

View file

@ -1,12 +1,30 @@
use super::Node;
use bevy_derive::EntityArchetype;
use bevy_render::Renderable;
use crate::Rect;
use bevy_render::{mesh::Mesh, Renderable};
use crate::{Rect, render::UI_PIPELINE_HANDLE, QUAD_HANDLE};
use bevy_asset::Handle;
#[derive(EntityArchetype, Default)]
#[derive(EntityArchetype)]
#[module(meta = false)]
pub struct UiEntity {
pub node: Node,
pub rect: Rect,
pub renderable: Renderable,
pub mesh: Handle<Mesh>, // TODO: maybe abstract this out
// pub renderable: Renderable,
}
impl Default for UiEntity {
fn default() -> Self {
UiEntity {
node: Default::default(),
rect: Default::default(),
mesh: QUAD_HANDLE,
// renderable: Renderable {
// pipelines: vec![
// UI_PIPELINE_HANDLE
// ],
// ..Default::default()
// }
}
}
}

View file

@ -3,21 +3,38 @@ pub mod entity;
mod margins;
mod node;
mod rect;
mod render;
mod ui_update_system;
pub use anchors::*;
pub use margins::*;
pub use node::*;
pub use rect::*;
pub use render::*;
pub use ui_update_system::*;
use bevy_app::{AppBuilder, AppPlugin};
use bevy_render::{mesh::{shape::Quad, Mesh}, render_graph::RenderGraph};
use bevy_asset::{AssetStorage, Handle};
use glam::Vec2;
#[derive(Default)]
pub struct UiPlugin;
pub const QUAD_HANDLE: Handle<Mesh> = Handle::from_bytes([179, 41, 129, 128, 95, 217, 79, 194, 167, 95, 107, 115, 97, 151, 20, 62]);
impl AppPlugin for UiPlugin {
fn build(&self, app: &mut AppBuilder) {
{
let mut render_graph = app.resources().get_mut::<RenderGraph>().unwrap();
render_graph.add_ui_graph(app.resources());
let mut meshes = app.resources().get_mut::<AssetStorage<Mesh>>().unwrap();
meshes.add_with_handle(QUAD_HANDLE, Mesh::from(Quad {
size: Vec2::new(1.0, 1.0),
}));
}
app.add_system(ui_update_system());
}
}

View file

@ -1,61 +1,77 @@
// use crate::{
// pipeline::state_descriptors::{
// BlendDescriptor, BlendFactor, BlendOperation, ColorStateDescriptor, ColorWrite,
// CompareFunction, CullMode, DepthStencilStateDescriptor, FrontFace,
// RasterizationStateDescriptor, StencilStateFaceDescriptor,
// },
// render_graph::RenderGraphBuilder,
// render_resource::resource_name,
// shader::{Shader, ShaderStage},
// texture::TextureFormat,
// };
// pub trait UiPipelineBuilder {
// fn add_ui_pipeline(&mut self) -> &mut Self;
// }
use bevy_asset::{AssetStorage, Handle};
use bevy_render::{
draw_target::AssignedMeshesDrawTarget,
pipeline::{state_descriptors::*, PipelineDescriptor},
render_graph::{nodes::{UniformNode, PassNode}, RenderGraph},
shader::{Shader, ShaderStage, ShaderStages},
texture::TextureFormat, base_render_graph,
};
use legion::prelude::Resources;
use crate::Rect;
// impl<'a, 'b, 'c> UiPipelineBuilder for RenderGraphBuilder<'a, 'b, 'c> {
// fn add_ui_pipeline(&mut self) -> &mut Self {
// self.add_pipeline(resource_name::pipeline::UI, |builder| {
// builder
// .with_vertex_shader(Shader::from_glsl(
// ShaderStage::Vertex,
// include_str!("ui.vert"),
// ))
// .with_fragment_shader(Shader::from_glsl(
// ShaderStage::Fragment,
// include_str!("ui.frag"),
// ))
// .with_rasterization_state(RasterizationStateDescriptor {
// front_face: FrontFace::Ccw,
// cull_mode: CullMode::None,
// depth_bias: 0,
// depth_bias_slope_scale: 0.0,
// depth_bias_clamp: 0.0,
// })
// .with_depth_stencil_state(DepthStencilStateDescriptor {
// format: TextureFormat::Depth32Float,
// depth_write_enabled: false,
// depth_compare: CompareFunction::Always,
// stencil_front: StencilStateFaceDescriptor::IGNORE,
// stencil_back: StencilStateFaceDescriptor::IGNORE,
// stencil_read_mask: 0,
// stencil_write_mask: 0,
// })
// .add_color_state(ColorStateDescriptor {
// format: TextureFormat::Bgra8UnormSrgb,
// color_blend: BlendDescriptor {
// src_factor: BlendFactor::SrcAlpha,
// dst_factor: BlendFactor::OneMinusSrcAlpha,
// operation: BlendOperation::Add,
// },
// alpha_blend: BlendDescriptor {
// src_factor: BlendFactor::One,
// dst_factor: BlendFactor::One,
// operation: BlendOperation::Add,
// },
// write_mask: ColorWrite::ALL,
// })
// .add_draw_target(resource_name::draw_target::UI);
// })
// }
// }
pub const UI_PIPELINE_HANDLE: Handle<PipelineDescriptor> =
Handle::from_bytes([
163, 238, 40, 24, 156, 49, 73, 203, 156, 189, 249, 55, 133, 242, 116, 51,
]);
pub fn build_ui_pipeline(shaders: &mut AssetStorage<Shader>) -> PipelineDescriptor {
PipelineDescriptor {
rasterization_state: Some(RasterizationStateDescriptor {
front_face: FrontFace::Ccw,
cull_mode: CullMode::None,
depth_bias: 0,
depth_bias_slope_scale: 0.0,
depth_bias_clamp: 0.0,
}),
depth_stencil_state: Some(DepthStencilStateDescriptor {
format: TextureFormat::Depth32Float,
depth_write_enabled: true,
depth_compare: CompareFunction::Less,
stencil_front: StencilStateFaceDescriptor::IGNORE,
stencil_back: StencilStateFaceDescriptor::IGNORE,
stencil_read_mask: 0,
stencil_write_mask: 0,
}),
color_states: vec![ColorStateDescriptor {
format: TextureFormat::Bgra8UnormSrgb,
color_blend: BlendDescriptor {
src_factor: BlendFactor::SrcAlpha,
dst_factor: BlendFactor::OneMinusSrcAlpha,
operation: BlendOperation::Add,
},
alpha_blend: BlendDescriptor {
src_factor: BlendFactor::One,
dst_factor: BlendFactor::One,
operation: BlendOperation::Add,
},
write_mask: ColorWrite::ALL,
}],
..PipelineDescriptor::new(ShaderStages {
vertex: shaders.add(Shader::from_glsl(
ShaderStage::Vertex,
include_str!("ui.vert"),
)),
fragment: Some(shaders.add(Shader::from_glsl(
ShaderStage::Fragment,
include_str!("ui.frag"),
))),
})
}
}
pub trait UiRenderGraphBuilder {
fn add_ui_graph(&mut self, resources: &Resources) -> &mut Self;
}
impl UiRenderGraphBuilder for RenderGraph {
fn add_ui_graph(&mut self, resources: &Resources) -> &mut Self {
self.add_system_node_named("rect", UniformNode::<Rect>::new(false), resources);
self.add_node_edge("rect", base_render_graph::node::MAIN_PASS).unwrap();
let mut pipelines = resources.get_mut::<AssetStorage<PipelineDescriptor>>().unwrap();
let mut shaders = resources.get_mut::<AssetStorage<Shader>>().unwrap();
pipelines.add_with_handle(UI_PIPELINE_HANDLE, build_ui_pipeline(&mut shaders));
let main_pass: &mut PassNode = self.get_node_mut(base_render_graph::node::MAIN_PASS).unwrap();
main_pass.add_pipeline(UI_PIPELINE_HANDLE, vec![Box::new(AssignedMeshesDrawTarget)]);
self
}
}

View file

@ -20,6 +20,6 @@ layout(set = 1, binding = 0) uniform Rect {
void main() {
v_Color = Rect_Color;
vec3 position = Vertex_Position * vec3(Rect_Size, 0.0);
position = position + vec4(Rect_Position + Rect_Size / 2.0, -Rect_ZIndex, 0.0);
gl_Position = ViewProj * vec4(position, 1.0)
position = position + vec3(Rect_Position + Rect_Size / 2.0, -Rect_ZIndex);
gl_Position = ViewProj * vec4(position, 1.0);
}