From ffa04d0bb13d6edf9acb280ba0be02d389dc4c6e Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Wed, 16 Oct 2019 23:29:49 -0700 Subject: [PATCH] doing some hardcore storming on this dynamic vertex situation --- notes/drawable-flow | 1 + src/canvas/canvas_frame.rs | 88 ++++++++++++++++++++-------- src/canvas/canvas_state.rs | 6 +- src/canvas/shader/common.rs | 2 +- src/canvas/shader/dynamic_vertex.rs | 91 ++++++++++++++++------------- src/canvas/shader/generic_shader.rs | 2 +- src/canvas/shader/mod.rs | 2 +- 7 files changed, 120 insertions(+), 72 deletions(-) create mode 100644 notes/drawable-flow diff --git a/notes/drawable-flow b/notes/drawable-flow new file mode 100644 index 00000000..f217d36a --- /dev/null +++ b/notes/drawable-flow @@ -0,0 +1 @@ +7Vtdb+I4FP01SDMPVPkiwGOBdmelzk41VDPbp5WbmMRbE0eOKTC/fq8ThxAcCt2SJoyQKjW+dkxyzr33XNvQscfz1R8cxeFX5mPasQx/1bEnHcuyDXsA/6RlnVnMQd/NLAEnvrIVhin5hZXRUNYF8XFSGigYo4LEZaPHogh7omRDnLNlediM0fKnxijAmmHqIapbfxJfhMo6tIyi4wsmQZh/tGWonjnKRytDEiKfLbdM9k3HHnPGRHY1X40xlfDlwGT33e7p3TwZx5E45oaf9uPDX9Nn+y6cXn/DS/dltoi6io0XRBfqjR/wSqgHFuschmRJ5hRF0BrNWCSmqseENqIkiODag8fAHAwvmAsCCF6rDsFisHohof4dWrOFfNhEIO85b41CxskvmBZRNSd0c6GcwXJLI6byTjAbYOU4gTH3OQLmjukrWpUG3qFEKIPHKEVxQp42rzFHPCDRiAnB5mqQggZeB6/2Ym5umIQgwGyOBV/DEHXDUHGv3N/O3Xq55UuusoVbbjRQvCDlvsFm5oJguFAcv4HvvsZ3xxrJwCBYRsa1jK91jDUHENIrJDGcPeMxowyYnkQs8whC6Y4pdwqKZ2KvSyQx8kgU3KVjJk5h+a6AkCYG985oGjch8X0cSTqZQAJl3EmiYkYikSLVG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bz5f88ueVy9VaZ3Z1od56aVdbRS6KH9PTSUGnAf59vb59sdizbR3gwkxVV+RgUelxvgJ9mJ04Y+Q+VhoOmg8OqTLMtyhsKqu6xW0P1QVVZ971zF4/Mg7PcwWvNfp2jf49N5Ym0gDq7FNHd3RJtQY6oY/vak/C91elJlAgUwQKi+b3rrmkNW7d57VQUzYVonmM0fKRiQrP4nWxWxxS/N7Zv/gM= \ No newline at end of file diff --git a/src/canvas/canvas_frame.rs b/src/canvas/canvas_frame.rs index 773e6222..c552c3f9 100644 --- a/src/canvas/canvas_frame.rs +++ b/src/canvas/canvas_frame.rs @@ -13,6 +13,70 @@ pub struct CanvasFrame { pub text_drawables: HashMap, Vec> } + +/* +This is sort of the beginning of our interface with the user definable sprites. + +Will be taking in multiple type of items + +TEXT + FontHandle + VertexDefintion + color + position + instances (string) +Textured + TextureHandle + VertexDefintion + position + coords + size + +Vertex definition is directly correlated to the compiled code. How do I bucket these + +I guess I could store them and set handles like I do textures + +The only ent that can create these vertex handles is the vkprocessor. + So Text can only get a vertex definition by going like shader.get_definition() + + +Text + FontHandle + VertexHandle + + +Drawable must include + shader_handle (but how to I get this to the text? this is runtime) + +Okay, no. Maybe a default shader type of setup. With a shader handle override???? + +Type: Text + Textured + Img + Color + +frame.draw(text) { + + text.type == TEXT { // When it matches to default text shader + text_shader.get_definition() + text_shader.get_pipeline() + } + ... + else { // When the user passes in a shader + text.shader_handle.get_definition() + text.shader_handle.get_pipeline() + } +} + +// Has default shader +let text = Text::new("asdoif"); + +let frame = CanvasFrame::new(); +frame.draw(text); + +vkprocessor.run(frame); + +*/ impl CanvasFrame { /// Creates a bare canvas frame with empty accumulators @@ -53,30 +117,6 @@ impl CanvasFrame { } } - -struct Pair { - x: T, - y: T, -} - -impl Pair { - fn new(x: Vertex3D, y: Vertex3D) -> Self { - Self { - x, - y, - } - } -} - -impl Pair { - fn new(x: GlyphInstance, y: GlyphInstance) -> Self { - Self { - x, - y, - } - } -} - pub struct GenericCanvasFrame { frame_data: HashMap, Vec)>> } diff --git a/src/canvas/canvas_state.rs b/src/canvas/canvas_state.rs index 6a3d1a9c..6da40733 100644 --- a/src/canvas/canvas_state.rs +++ b/src/canvas/canvas_state.rs @@ -319,17 +319,17 @@ impl CanvasState { /// Load and Compile a shader with the filename at resources/shaders /// Takes physical and capabilities as we don't store that in Canvas - pub fn load_shader(&mut self, + pub fn load_shader(&mut self, filename: String, physical: PhysicalDevice, capabilities: Capabilities) -> Option> - where T: CompiledGraphicsPipeline, V: VertexDefinition { + where T: CompiledGraphicsPipeline { let handle = Arc::new(CompiledGraphicsPipelineHandle { handle: self.shader_buffers.len() as u32 }); - let shader: Box = Box::new(T::new::( + let shader: Box = Box::new(T::new( filename.clone(), self.device.clone(), handle.clone(), diff --git a/src/canvas/shader/common.rs b/src/canvas/shader/common.rs index 7562a3bf..8085e05c 100644 --- a/src/canvas/shader/common.rs +++ b/src/canvas/shader/common.rs @@ -103,7 +103,7 @@ pub struct CompiledGraphicsPipelineHandle { } pub trait CompiledGraphicsPipeline { - fn new(filename: String, + fn new(filename: String, device: Arc, handle: Arc, render_pass: Arc) -> Self where Self: Sized; diff --git a/src/canvas/shader/dynamic_vertex.rs b/src/canvas/shader/dynamic_vertex.rs index 1a592f74..bbafa93b 100644 --- a/src/canvas/shader/dynamic_vertex.rs +++ b/src/canvas/shader/dynamic_vertex.rs @@ -1,3 +1,10 @@ +use vulkano::pipeline::vertex::{VertexDefinition, InputRate, AttributeInfo, IncompatibleVertexDefinitionError, VertexSource}; +use vulkano::pipeline::shader::ShaderInterfaceDef; +use vulkano::buffer::BufferAccess; +use std::sync::Arc; +use cgmath::num_traits::real::Real; +use std::vec::IntoIter as VecIntoIter; + pub struct RuntimeVertexDef { buffers: Vec<(u32, usize, InputRate)>, vertex_buffer_ids: Vec<(usize, usize)>, @@ -6,50 +13,50 @@ pub struct RuntimeVertexDef { } impl RuntimeVertexDef { - pub fn from_primitive(primitive: gltf::Primitive) -> RuntimeVertexDef { - use gltf::mesh::Attribute; - use gltf::accessor::{DataType, Dimensions}; - + pub fn from_primitive(primitive: u32) -> RuntimeVertexDef { +// use gltf::mesh::Attribute; +// use gltf::accessor::{DataType, Dimensions}; +// let mut buffers = Vec::new(); let mut vertex_buffer_ids = Vec::new(); let mut attributes = Vec::new(); let mut num_vertices = u32::max_value(); - - for (attribute_id, attribute) in primitive.attributes().enumerate() { - let (name, accessor) = match attribute.clone() { - Attribute::Positions(accessor) => ("i_position".to_owned(), accessor), - Attribute::Normals(accessor) => ("i_normal".to_owned(), accessor), - Attribute::Tangents(accessor) => ("i_tangent".to_owned(), accessor), - Attribute::Colors(0, accessor) => ("i_color_0".to_owned(), accessor), - Attribute::TexCoords(0, accessor) => ("i_texcoord_0".to_owned(), accessor), - Attribute::TexCoords(1, accessor) => ("i_texcoord_1".to_owned(), accessor), - Attribute::Joints(0, accessor) => ("i_joints_0".to_owned(), accessor), - Attribute::Weights(0, accessor) => ("i_weights_0".to_owned(), accessor), - _ => unimplemented!(), - }; - - if (accessor.count() as u32) < num_vertices { - num_vertices = accessor.count() as u32; - } - - let infos = AttributeInfo { - offset: 0, - format: match (accessor.data_type(), accessor.dimensions()) { - (DataType::I8, Dimensions::Scalar) => Format::R8Snorm, - (DataType::U8, Dimensions::Scalar) => Format::R8Unorm, - (DataType::F32, Dimensions::Vec2) => Format::R32G32Sfloat, - (DataType::F32, Dimensions::Vec3) => Format::R32G32B32Sfloat, - (DataType::F32, Dimensions::Vec4) => Format::R32G32B32A32Sfloat, - _ => unimplemented!() - }, - }; - - let view = accessor.view(); - buffers.push((attribute_id as u32, view.stride().unwrap_or(accessor.size()), InputRate::Vertex)); - attributes.push((name, attribute_id as u32, infos)); - vertex_buffer_ids.push((view.buffer().index(), view.offset() + accessor.offset())); - } +// +// for (attribute_id, attribute) in primitive.attributes().enumerate() { +// let (name, accessor) = match attribute.clone() { +// Attribute::Positions(accessor) => ("i_position".to_owned(), accessor), +// Attribute::Normals(accessor) => ("i_normal".to_owned(), accessor), +// Attribute::Tangents(accessor) => ("i_tangent".to_owned(), accessor), +// Attribute::Colors(0, accessor) => ("i_color_0".to_owned(), accessor), +// Attribute::TexCoords(0, accessor) => ("i_texcoord_0".to_owned(), accessor), +// Attribute::TexCoords(1, accessor) => ("i_texcoord_1".to_owned(), accessor), +// Attribute::Joints(0, accessor) => ("i_joints_0".to_owned(), accessor), +// Attribute::Weights(0, accessor) => ("i_weights_0".to_owned(), accessor), +// _ => unimplemented!(), +// }; +// +// if (accessor.count() as u32) < num_vertices { +// num_vertices = accessor.count() as u32; +// } +// +// let infos = AttributeInfo { +// offset: 0, +// format: match (accessor.data_type(), accessor.dimensions()) { +// (DataType::I8, Dimensions::Scalar) => Format::R8Snorm, +// (DataType::U8, Dimensions::Scalar) => Format::R8Unorm, +// (DataType::F32, Dimensions::Vec2) => Format::R32G32Sfloat, +// (DataType::F32, Dimensions::Vec3) => Format::R32G32B32Sfloat, +// (DataType::F32, Dimensions::Vec4) => Format::R32G32B32A32Sfloat, +// _ => unimplemented!() +// }, +// }; +// +// let view = accessor.view(); +// buffers.push((attribute_id as u32, view.stride().unwrap_or(accessor.size()), InputRate::Vertex)); +// attributes.push((name, attribute_id as u32, infos)); +// vertex_buffer_ids.push((view.buffer().index(), view.offset() + accessor.offset())); +// } RuntimeVertexDef { buffers: buffers, @@ -100,9 +107,9 @@ unsafe impl VertexDefinition for RuntimeVertexDef } } -unsafe impl VertexSource>> for RuntimeVertexDef { - fn decode(&self, bufs: Vec>) - -> (Vec>, usize, usize) +unsafe impl VertexSource>> for RuntimeVertexDef { + fn decode(&self, bufs: Vec>) + -> (Vec>, usize, usize) { (bufs.into_iter().map(|b| Box::new(b) as Box<_>).collect(), self.num_vertices as usize, 1) } diff --git a/src/canvas/shader/generic_shader.rs b/src/canvas/shader/generic_shader.rs index 6587e8dd..5f3fd32c 100644 --- a/src/canvas/shader/generic_shader.rs +++ b/src/canvas/shader/generic_shader.rs @@ -40,7 +40,7 @@ impl CompiledGraphicsPipelineResources for GenericShader {} impl CompiledGraphicsPipeline for GenericShader { /// This will explode when the shader does not want to compile - fn new(filename: String, + fn new(filename: String, device: Arc, handle: Arc, render_pass: Arc) -> GenericShader { diff --git a/src/canvas/shader/mod.rs b/src/canvas/shader/mod.rs index 00ce26f9..1222e263 100644 --- a/src/canvas/shader/mod.rs +++ b/src/canvas/shader/mod.rs @@ -3,7 +3,7 @@ use crate::canvas::shader::common::CompiledGraphicsPipeline; pub mod common; pub mod generic_shader; pub mod text_shader; -use mod dynamic_vertex; +pub mod dynamic_vertex; use crate::canvas::shader::common::*; use crate::canvas::shader::generic_shader::*;