From 80c0d323becd942437af4a89437c7958e09fbc0e Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Mon, 10 Feb 2020 23:39:58 -0800 Subject: [PATCH] Going to just normalize to a single hardcoded enum of vertex definitions. Seems like quite a backpeddle.... --- notes/Home.txt | 32 +++++++++++++++ src/canvas/canvas_frame.rs | 27 +++---------- src/canvas/canvas_state.rs | 63 ++++++++++++++++++----------- src/canvas/managed/handles.rs | 2 +- src/main.rs | 76 ++++++++++++++++++++++++----------- src/sprite.rs | 19 ++++++++- src/vkprocessor.rs | 15 ++++--- 7 files changed, 160 insertions(+), 74 deletions(-) diff --git a/notes/Home.txt b/notes/Home.txt index 4e26d3b1..efd51566 100644 --- a/notes/Home.txt +++ b/notes/Home.txt @@ -39,8 +39,18 @@ In Shader lie the shader compilers, pipelines, and supporting data. I need to put them where I create them. The actual shader doesn't. +=== canvas frame === +The current workflow: +enum of vertex types + sprite holds vertex::type1 + poly holds vertex::type2 +canvasframe holds + +canvasState.run takes canvasFrame + +canvasState.draw_commands_test(command_buffer, framebuffers, image_num, canvas_frame); @@ -48,13 +58,35 @@ In Shader lie the shader compilers, pipelines, and supporting data. -------------------- +===== Links ===== +Dynamic Keys in HashMap +https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=b4630cf98afa88fc0fbffdb0112af1c1 +VertexDefinition +https://github.com/tomaka/vulkano-examples/blob/81f5a4eb3c5f3fcc6a194d3c41e53b2bc66f7add/gltf/gltf_system.rs#L687-L795 +pub enum Content { + Text(String), + Number(u32), +} +impl From for String { + fn wrap(self) { + Content::Text(self) + } +} +impl From for u32 { + fn wrap(self) { + Content::Number(self) + } +} +fn any_content(value : T) where T : IntoContent { + let content : Content = value.wrap(); +} diff --git a/src/canvas/canvas_frame.rs b/src/canvas/canvas_frame.rs index 6e0d8f30..e67cebd8 100644 --- a/src/canvas/canvas_frame.rs +++ b/src/canvas/canvas_frame.rs @@ -11,14 +11,12 @@ use std::any::Any; // I don't think this is going to work without getting into Box'ing -pub trait DrawableTest where H: Handle { - fn get_vertices(&self) -> Vec; +pub trait DrawableTest, H: Handle + DynHash> { + fn get_vertices(&self) -> VTypes; fn get_handle(&self) -> H; } - - -mod dynhash { +pub mod dynhash { use std::any::Any; use std::hash::{Hash, Hasher}; @@ -73,17 +71,16 @@ mod dynhash { } } use crate::canvas::canvas_frame::dynhash::DynHash; - - +use crate::VertexTypes; // CanvasFrameTest will be drawn to by objects implementing DrawableTest pub struct CanvasFrameTest { - pub map: HashMap, Vec>, + pub map: HashMap, VTypes>, } impl CanvasFrameTest { - pub fn draw(&mut self, drawable: Vec) { + pub fn draw(&mut self, drawable: VTypes) { self.map.insert(Box::new(10), drawable); } } @@ -133,18 +130,6 @@ impl CanvasFrame { } } -// pub fn draw_test(&mut self, drawable: &dyn DrawableTest) { -// let h = drawable.get_handle(); -// -// let v = drawable.get_vertices(); -// -// let v = v.get(0).unwrap(); -// -// // need to fill up the drawables.... -// -// -// } - // TODO: Fix this for text and fonts /// Accumulates the drawables collected Vertex2D's diff --git a/src/canvas/canvas_state.rs b/src/canvas/canvas_state.rs index d51c41b9..0db23002 100644 --- a/src/canvas/canvas_state.rs +++ b/src/canvas/canvas_state.rs @@ -2,7 +2,7 @@ use vulkano::command_buffer::{AutoCommandBufferBuilder, DynamicState}; use std::collections::{HashMap, HashSet}; use vulkano::buffer::{BufferAccess, BufferUsage, ImmutableBuffer, CpuAccessibleBuffer}; use std::sync::Arc; -use vulkano::format::{ClearValue, Format, R8Unorm}; +use vulkano::format::{ClearValue, Format, R8Unorm, ClearValuesTuple}; use vulkano::framebuffer::{FramebufferAbstract, Framebuffer, RenderPass, RenderPassAbstract}; use vulkano::device::{Device, Queue}; use vulkano::instance::PhysicalDevice; @@ -33,6 +33,7 @@ use crate::canvas::managed::handles::{CanvasTextureHandle, CanvasImageHandle, Ca use crate::canvas::managed::gpu_buffers::{CanvasImage, CanvasTexture, CanvasFont}; use crate::canvas::managed::shader::shader_common::CompiledGraphicsPipeline; use crate::canvas::managed::shader::generic_shader::GenericShader; +use crate::VertexTypes; /// Canvas state is used for storage of texture and image buffers in addition to vertex buffers @@ -101,6 +102,7 @@ impl CanvasState { device: Arc, physical: PhysicalDevice, capabilities: Capabilities) -> CanvasState { + let format = capabilities.supported_formats[0].0; let render_pass = Arc::new(vulkano::single_pass_renderpass!( @@ -488,11 +490,11 @@ impl CanvasState { // 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 { // Specify the color to clear the framebuffer with i.e. blue let clear_values = vec!( @@ -513,32 +515,47 @@ impl CanvasState { 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)> = match v.into() { + + VertexTypes::TexturedType(vertices) => { + + // Solid colors + let mut shader = self.shader_buffers.get( + self.get_shader_handle(String::from("color-passthrough")) + .unwrap().clone().get_handle() as usize + ).unwrap(); + + let buffer = ImmutableBuffer::from_iter( + vertices.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(); + } + + buffer + }, + VertexTypes::VType2(vertices) => { + ImmutableBuffer::from_iter( + vertices.iter().cloned(), + BufferUsage::vertex_buffer(), + self.queue.clone(), + ).unwrap().0 + } + }; - 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/canvas/managed/handles.rs b/src/canvas/managed/handles.rs index d4f41628..b7485217 100644 --- a/src/canvas/managed/handles.rs +++ b/src/canvas/managed/handles.rs @@ -18,7 +18,7 @@ impl Handle for CanvasFontHandle { /// Typed wrapper for a u32 handle #[derive(Clone, Debug, Default, PartialEq, Eq, Hash)] pub struct CanvasTextureHandle { - pub(in crate::canvas) handle: u32 + pub/*(in crate::canvas)*/ handle: u32 } impl Handle for CanvasTextureHandle { diff --git a/src/main.rs b/src/main.rs index 6889542b..02b18678 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,10 +25,11 @@ 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, CanvasFrameTest}; +use crate::canvas::canvas_frame::{CanvasFrame, CanvasFrameTest, DrawableTest}; use crate::compute::managed::compu_sprite::CompuSprite; use std::sync::Arc; -use crate::canvas::managed::handles::CanvasTextureHandle; +use crate::canvas::managed::handles::{CanvasTextureHandle, Handle}; +use crate::canvas::canvas_frame::dynhash::DynHash; pub mod util; @@ -55,22 +56,48 @@ pub mod compute; */ -pub struct ImplVertexData1 { - pub x: i32, - pub y: i32, + +#[derive(Clone)] +pub struct TextureVertex2D { + pub v_position: [f32; 2], + pub ti_position: [f32; 2], +} +vulkano::impl_vertex!(TextureVertex2D, v_position, ti_position); + +#[derive(Clone)] +pub struct ColorVertex2D { + pub v_position: [f32; 2], + pub color: [f32; 4], +} +vulkano::impl_vertex!(ColorVertex2D, v_position, color); + +#[derive(Clone)] +pub struct ImageVertex2D { + pub v_position: [f32; 2], + pub color: [f32; 4], } +vulkano::impl_vertex!(ImageVertex2D, v_position, color); -pub struct ImplVertexData2 { - pub x: i32, - pub y: i32, +#[derive(Clone)] +pub enum VertexTypes { + TextureType(Vec), + ColorType(Vec), + ImageType(Vec), } -impl ImplVertexData1 {} -impl ImplVertexData2 {} +pub struct DrawableTestee { + vertices: VertexTypes, + handle: Arc, +} + +impl, H: Handle + DynHash> DrawableTest for DrawableTestee { + fn get_vertices(&self) -> VTypes { + unimplemented!() + } -enum VertexTypes { - VType1(ImplVertexData1), - VType2(ImplVertexData2), + fn get_handle(&self) -> H { + unimplemented!() + } } pub fn main() { @@ -199,27 +226,30 @@ pub fn main() { break; } -// let dt = DrawableTestee{ -// vertices: vec![ImplVertexData{ x: 0, y: 0 }], -// handle: Arc::new(CanvasTextureHandle{ handle: 0 }) -// }; - let mut cft : CanvasFrameTest = + let sprite = Sprite::new((0.0,0.0), (0.0,0.0)); + + let dt = DrawableTestee { + vertices: VertexTypes::TexturedType(vec![ImplVertexData1 {x:0,y:0}]), + handle: Arc::new(CanvasTextureHandle{ handle: 0 }) + }; + let mut canvas_frame : CanvasFrameTest = CanvasFrameTest{ map: Default::default() }; - // cft.draw(&dt); + canvas_frame.draw(dt.vertices); + canvas_frame.draw(sprite.get_vertices()); let mut compu_frame = CompuFrame::new(); // compu_frame.add(compute_buffer.clone(), compute_kernel.clone()); // compu_frame.add_with_image_swap(compute_buffer.clone(), compute_kernel.clone(), &compu_sprite1); // - let mut canvas = CanvasFrame::new(); - canvas.draw(&funky_sprite); - canvas.draw(&test_polygon); +// let mut canvas = CanvasFrame::new(); +// canvas.draw(&funky_sprite); +// canvas.draw(&test_polygon); { let g = hprof::enter("Run"); processor.run(&surface, - canvas, + canvas_frame, compu_frame); } } diff --git a/src/sprite.rs b/src/sprite.rs index 574e1a8b..091971d6 100644 --- a/src/sprite.rs +++ b/src/sprite.rs @@ -1,13 +1,18 @@ use std::sync::Arc; use crate::util::vertex_3d::Vertex3D; use crate::canvas::*; -use crate::canvas::managed::handles::{CanvasFontHandle, CanvasImageHandle, CanvasTextureHandle}; +use crate::canvas::managed::handles::{CanvasFontHandle, CanvasImageHandle, CanvasTextureHandle, Handle}; use crate::canvas::managed::shader::text_shader::GlyphInstance; use crate::canvas::canvas_frame::{DrawableTest, Drawable}; +use crate::{VertexTypes, ImplVertexData1}; +use crate::canvas::canvas_frame::dynhash::DynHash; /// #[derive(Debug, Clone)] pub struct Sprite { + + pub verts: VertexTypes::TexturedType(vec![]), + pub vertices: [(f32, f32, f32); 6], pub ti_position: [(f32, f32); 6], @@ -98,6 +103,18 @@ impl Sprite { } } } + +impl, H: Handle + DynHash> DrawableTest for Sprite{ + + fn get_vertices(&self) -> V { + VertexTypes::TexturedType(vec![ImplVertexData1{ x: 0, y: 0 }]) + } + + fn get_handle(&self) -> H { + self.texture_handle.unwrap() + } +} + impl Drawable for Sprite { fn get_vertices(&self) -> Vec<(f32, f32, f32)> { self.vertices.to_vec() diff --git a/src/vkprocessor.rs b/src/vkprocessor.rs index 26076842..4b4027b4 100644 --- a/src/vkprocessor.rs +++ b/src/vkprocessor.rs @@ -11,7 +11,7 @@ use winit::Window; use crate::compute::compu_state::CompuState; use vulkano::image::ImageUsage; use crate::compute::compu_frame::CompuFrame; -use crate::canvas::canvas_frame::CanvasFrame; +use crate::canvas::canvas_frame::{CanvasFrame, CanvasFrameTest}; use std::time::Duration; use vulkano::pipeline::depth_stencil::{DynamicStencilValue, StencilFaceFlags}; use vulkano::pipeline::vertex::{OneVertexOneInstanceDefinition, SingleBufferDefinition}; @@ -21,6 +21,7 @@ use crate::canvas::managed::shader::generic_shader::GenericShader; use crate::canvas::managed::shader::text_shader::TextShader; use crate::canvas::managed::handles::{CanvasTextureHandle, CompiledShaderHandle, CanvasFontHandle, CanvasImageHandle}; use crate::compute::managed::handles::{CompuKernelHandle, CompuBufferHandle}; +use crate::{ImplVertexData1, VertexTypes}; /// VKProcessor holds the vulkan instance information, the swapchain, @@ -223,9 +224,10 @@ impl<'a> VkProcessor<'a> { } /// - pub fn run(&mut self, + pub fn run>(&mut self, surface: &'a Arc>, - canvas_frame: CanvasFrame, + //canvas_frame: CanvasFrame, + canvas_frame: CanvasFrameTest, compute_frame: CompuFrame, ) { @@ -268,7 +270,8 @@ impl<'a> VkProcessor<'a> { // take the canvas frame and create the vertex buffers // TODO: This performs gpu buffer creation. Shouldn't be in hotpath?? let g = hprof::enter("Canvas creates GPU buffers"); - self.canvas_state.draw(canvas_frame); + //self.canvas_state.draw(canvas_frame); + } let mut command_buffer = @@ -282,7 +285,9 @@ impl<'a> VkProcessor<'a> { let g = hprof::enter("Push draw commands to command buffer"); // Add the draw commands - let mut command_buffer = self.canvas_state.draw_commands(command_buffer, framebuffers, image_num); + //let mut command_buffer = self.canvas_state.draw_commands(command_buffer, framebuffers, image_num); + let mut command_buffer = + self.canvas_state.draw_commands_test(command_buffer, framebuffers, image_num, canvas_frame); // And build let command_buffer = command_buffer.build().unwrap();