From cd0c1e60528a15fef71f47040f310dd13e16dc17 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Thu, 6 Feb 2020 23:38:28 -0800 Subject: [PATCH] oh my god it compiles. A month of brainstorming comes out to a single enum --- notes/CanvasFrame.txt | 7 +++ src/canvas/canvas_frame.rs | 102 +++++++++++++++++++++++-------------- src/canvas/canvas_state.rs | 60 ++++++++++++++++++---- src/main.rs | 33 +++++++++--- 4 files changed, 147 insertions(+), 55 deletions(-) diff --git a/notes/CanvasFrame.txt b/notes/CanvasFrame.txt index cb0acb3a..588f9f6b 100644 --- a/notes/CanvasFrame.txt +++ b/notes/CanvasFrame.txt @@ -42,7 +42,14 @@ Drawable needs a few things: The handle is queiried and then turned into a descriptor set in the draw_commands. +* What if I pass using function params? But then how do I store??? +==== Problem ==== +I need to store vectors of multiple unequal types in a vec + +vec> + +Why can't I pass in -------------------- diff --git a/src/canvas/canvas_frame.rs b/src/canvas/canvas_frame.rs index 4a215dea..6e0d8f30 100644 --- a/src/canvas/canvas_frame.rs +++ b/src/canvas/canvas_frame.rs @@ -1,4 +1,4 @@ -use crate::util::vertex_3d::{Vertex3D}; +use crate::util::vertex_3d::Vertex3D; use std::sync::Arc; use std::collections::HashMap; use std::hash::Hash; @@ -7,63 +7,95 @@ use crate::canvas::managed::shader::dynamic_vertex::RuntimeVertexDef; use crate::canvas::managed::handles::{CanvasTextureHandle, CanvasImageHandle, CanvasFontHandle, Handle}; use crate::canvas::managed::shader::text_shader::GlyphInstance; use vulkano::pipeline::vertex::Vertex; +use std::any::Any; + // I don't think this is going to work without getting into Box'ing -pub trait DrawableTest where H: Handle{ +pub trait DrawableTest where H: Handle { fn get_vertices(&self) -> Vec; fn get_handle(&self) -> H; } -pub struct DrawableTestee { - pub vertices: Vec, - pub handle: Arc -} -impl DrawableTest for DrawableTestee { - fn get_vertices(&self) -> Vec { - unimplemented!() +mod dynhash { + use std::any::Any; + use std::hash::{Hash, Hasher}; + + pub trait DynEq: Any { + fn dyn_eq(&self, other: &dyn DynEq) -> bool; + + fn as_any(&self) -> &dyn Any; } - fn get_handle(&self) -> H { - unimplemented!() + pub trait DynHash: DynEq { + fn dyn_hash(&self, hasher: &mut dyn Hasher); + + fn as_dyn_eq(&self) -> &dyn DynEq; } -} -pub struct ImplVertexData { - pub x: i32, - pub y: i32, -} -impl VertexData for ImplVertexData { + impl DynEq for H { + fn dyn_eq(&self, other: &dyn DynEq) -> bool { + if let Some(other) = other.as_any().downcast_ref::() { + self == other + } else { + false + } + } -} + fn as_any(&self) -> &dyn Any { + self + } + } + + impl DynHash for H { + fn dyn_hash(&self, mut hasher: &mut dyn Hasher) { + H::hash(self, &mut hasher) + } -pub trait VertexData : Sized { + fn as_dyn_eq(&self) -> &dyn DynEq { + self + } + } + impl PartialEq for dyn DynHash { + fn eq(&self, other: &dyn DynHash) -> bool { + self.dyn_eq(other.as_dyn_eq()) + } + } + + impl Eq for dyn DynHash {} + + impl Hash for dyn DynHash { + fn hash(&self, hasher: &mut H) { + self.dyn_hash(hasher) + } + } } +use crate::canvas::canvas_frame::dynhash::DynHash; + + + // CanvasFrameTest will be drawn to by objects implementing DrawableTest -pub struct CanvasFrameTest { - pub map: HashMap>, +pub struct CanvasFrameTest { + pub map: HashMap, Vec>, } -impl CanvasFrameTest { - - pub fn draw(&mut self, drawable: &dyn DrawableTest) { - drawable.get_vertices(); - self.map.insert(drawable.get_handle(), drawable.get_vertices()); +impl CanvasFrameTest { + pub fn draw(&mut self, drawable: Vec) { + self.map.insert(Box::new(10), drawable); } } pub trait Drawable { - fn get_vertices(&self) -> Vec<(f32, f32, f32)>; fn get_color(&self) -> (f32, f32, f32, f32); fn get_ti_coords(&self) -> Vec<(f32, f32)>; fn get_texture_handle(&self) -> Option>; fn get_image_handle(&self) -> Option>; - // fn get_text_handle(&self) -> Option>; +// fn get_text_handle(&self) -> Option>; // These needs to return a vector of raw-ass data in addition to the definition for this data fn collect(&self) -> Vec { @@ -74,36 +106,30 @@ pub trait Drawable { // color: [color.0, color.1, color.2, color.3], // ti_position: [b.0, b.1], // }).collect() - // TODO +// TODO vec![RuntimeVertexDef::from_primitive(0)] } } - - -pub trait VertexDefinitionAndData { - -} - +pub trait VertexDefinitionAndData {} pub struct CanvasFrame { pub colored_drawables: Vec, pub textured_drawables: HashMap, Vec>>, pub image_drawables: HashMap, Vec>>, - pub text_drawables: HashMap, Vec> + pub text_drawables: HashMap, Vec>, } impl CanvasFrame { - /// Creates a bare canvas frame with empty accumulators a pub fn new() -> CanvasFrame { CanvasFrame { colored_drawables: vec![], textured_drawables: Default::default(), image_drawables: Default::default(), - text_drawables: Default::default() + text_drawables: Default::default(), } } diff --git a/src/canvas/canvas_state.rs b/src/canvas/canvas_state.rs index 910e7e47..d51c41b9 100644 --- a/src/canvas/canvas_state.rs +++ b/src/canvas/canvas_state.rs @@ -18,7 +18,7 @@ use vulkano::swapchain::Capabilities; use winit::Window; use vulkano::pipeline::viewport::Viewport; use vulkano::descriptor::descriptor::DescriptorDescTy::TexelBuffer; -use crate::canvas::canvas_frame::{CanvasFrame, CanvasFrameTest, VertexData}; +use crate::canvas::canvas_frame::{CanvasFrame, CanvasFrameTest}; use std::hash::Hash; use crate::util::vertex_3d::{Vertex3D, TextVertex3D}; use vulkano::pipeline::depth_stencil::{StencilFaceFlags, DynamicStencilValue}; @@ -69,6 +69,7 @@ pub struct CanvasState { } + impl CanvasState { /// This method is called once during initialization, then again whenever the window is resized pub fn window_size_dependent_setup(&mut self, images: &[Arc>]) @@ -484,22 +485,61 @@ impl CanvasState { o } + // This is taking in a canvas frame, which should be some sort of matrix of vertices of generic // types and handles - pub fn draw_commands_test - (&mut self, + pub fn draw_commands_test(&mut self, mut command_buffer: AutoCommandBufferBuilder, framebuffers: Vec>, image_num: usize, - canvas_frame: CanvasFrameTest) -> AutoCommandBufferBuilder { + canvas_frame: CanvasFrameTest) -> AutoCommandBufferBuilder { - let v = Vec::::new(); + // Specify the color to clear the framebuffer with i.e. blue + let clear_values = vec!( + ClearValue::Float([0.0, 0.0, 1.0, 1.0]), + ClearValue::DepthStencil((1.0, 0x00)), + ); - let x = ImmutableBuffer::from_iter( - v.iter().cloned(), - BufferUsage::all(), - self.queue.clone(), - ).unwrap().0; + self.dynamic_state = DynamicState { + line_width: None, + viewports: self.dynamic_state.viewports.clone(), + scissors: None, + compare_mask: None, + write_mask: None, + reference: None, + }; + + let mut command_buffer = command_buffer.begin_render_pass( + framebuffers[image_num].clone(), false, clear_values.clone(), + ).unwrap(); + + // Solid colors + let mut shader = self.shader_buffers.get( + self.get_shader_handle(String::from("color-passthrough")) + .unwrap().clone().get_handle() as usize + ).unwrap(); + + + + for (k,v) in canvas_frame.map { + + let value : Vec = v.iter().map(|v| v.clone()).collect(); + + let buffer : Arc<(dyn BufferAccess + Send + Sync)> = ImmutableBuffer::from_iter( + value.iter().cloned(), + BufferUsage::vertex_buffer(), + self.queue.clone(), + ).unwrap().0; + + if !self.colored_vertex_buffer.is_empty() { + command_buffer = command_buffer.draw( + shader.get_pipeline().clone(), + &self.dynamic_state.clone(), + vec![buffer.clone()], + (), (), + ).unwrap(); + } + } command_buffer diff --git a/src/main.rs b/src/main.rs index 00178eb9..6889542b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,7 +25,7 @@ use crate::util::load_raw; use crate::sprite::{Poly, Text, TextHandle, TextVertex, TextInstance}; use vulkano::instance::debug::DebugCallback; use crate::compute::compu_frame::CompuFrame; -use crate::canvas::canvas_frame::{CanvasFrame, DrawableTestee, CanvasFrameTest, ImplVertexData}; +use crate::canvas::canvas_frame::{CanvasFrame, CanvasFrameTest}; use crate::compute::managed::compu_sprite::CompuSprite; use std::sync::Arc; use crate::canvas::managed::handles::CanvasTextureHandle; @@ -55,6 +55,23 @@ pub mod compute; */ +pub struct ImplVertexData1 { + pub x: i32, + pub y: i32, +} + +pub struct ImplVertexData2 { + pub x: i32, + pub y: i32, +} + +impl ImplVertexData1 {} +impl ImplVertexData2 {} + +enum VertexTypes { + VType1(ImplVertexData1), + VType2(ImplVertexData2), +} pub fn main() { hprof::start_frame(); @@ -182,12 +199,14 @@ pub fn main() { break; } - let dt = DrawableTestee{ - vertices: vec![ImplVertexData{ x: 0, y: 0 }], - handle: Arc::new(Default::default()) - }; - let mut cft :CanvasFrameTest= CanvasFrameTest{ map: Default::default() }; - cft.draw(&dt); +// let dt = DrawableTestee{ +// vertices: vec![ImplVertexData{ x: 0, y: 0 }], +// handle: Arc::new(CanvasTextureHandle{ handle: 0 }) +// }; + let mut cft : CanvasFrameTest = + CanvasFrameTest{ map: Default::default() }; + + // cft.draw(&dt); let mut compu_frame = CompuFrame::new(); // compu_frame.add(compute_buffer.clone(), compute_kernel.clone());