From 659cd98a1f539df312eac3f6d4a283a80a89f531 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Wed, 12 Feb 2020 00:42:30 -0800 Subject: [PATCH] moved over to the enum method of drawing. Not flexible, but type safe --- notes/Home.txt | 8 +- notes/VKProcessor.txt | 6 +- .../CanvasContainerClasses.txt | 1 + notes/{ => VKProcessor}/CanvasFont.txt | 0 notes/{ => VKProcessor}/CanvasFrame.txt | 0 notes/{ => VKProcessor}/CanvasImage.txt | 0 notes/{ => VKProcessor}/CanvasState.txt | 2 +- notes/{ => VKProcessor}/CanvasTexture.txt | 0 notes/{ => VKProcessor}/CompuState.txt | 0 notes/{ => VKProcessor}/DynamicVertex.txt | 0 src/canvas/canvas_frame.rs | 24 +- src/canvas/canvas_state.rs | 232 ++++---------- src/canvas/managed/canvas_text.rs | 1 - src/canvas/managed/handles.rs | 9 + src/canvas/managed/shader/generic_shader.rs | 1 - src/canvas/managed/shader/text_shader.rs | 1 - src/main.rs | 65 +--- src/sprite.rs | 283 +----------------- src/util/mod.rs | 3 +- src/util/vertex.rs | 78 +++++ src/util/vertex_2d.rs | 10 - src/util/vertex_3d.rs | 20 -- src/vkprocessor.rs | 16 +- 23 files changed, 191 insertions(+), 569 deletions(-) rename notes/{ => VKProcessor}/CanvasContainerClasses.txt (99%) rename notes/{ => VKProcessor}/CanvasFont.txt (100%) rename notes/{ => VKProcessor}/CanvasFrame.txt (100%) rename notes/{ => VKProcessor}/CanvasImage.txt (100%) rename notes/{ => VKProcessor}/CanvasState.txt (97%) rename notes/{ => VKProcessor}/CanvasTexture.txt (100%) rename notes/{ => VKProcessor}/CompuState.txt (100%) rename notes/{ => VKProcessor}/DynamicVertex.txt (100%) create mode 100644 src/util/vertex.rs delete mode 100644 src/util/vertex_2d.rs delete mode 100644 src/util/vertex_3d.rs diff --git a/notes/Home.txt b/notes/Home.txt index efd51566..cb02a6b7 100644 --- a/notes/Home.txt +++ b/notes/Home.txt @@ -8,13 +8,13 @@ Creation-Date: 2020-02-03T22:11:42-08:00 Main Systems: [[~/source/Trac3r-rust/doc/sfml_rust/sprite/index.html|Spri]][[~/source/Trac3r-rust/doc/sfml_rust/sprite/index.html|te]] - [[CanvasContainerClasses]] + [[VKProcessor:CanvasContainerClasses]] Docs [[VkProcessor]] [[~/source/Trac3r-rust/doc/sfml_rust/vkprocessor/struct.VkProcessor.html|===========]] - [[CanvasState]] [[~/source/Trac3r-rust/doc/sfml_rust/canvas/canvas_state/index.html|===========]] - [[DynamicVertex]] - [[CompuState]] [[~/source/Trac3r-rust/doc/sfml_rust/compute/compu_state/struct.CompuState.html|===========]] + [[VKProcessor:CanvasState]] [[~/source/Trac3r-rust/doc/sfml_rust/canvas/canvas_state/index.html|===========]] + [[VKProcessor:DynamicVertex]] + [[VKProcessor:CompuState]] [[~/source/Trac3r-rust/doc/sfml_rust/compute/compu_state/struct.CompuState.html|===========]] -------------------- diff --git a/notes/VKProcessor.txt b/notes/VKProcessor.txt index 3e937d31..a915328e 100644 --- a/notes/VKProcessor.txt +++ b/notes/VKProcessor.txt @@ -26,13 +26,13 @@ Vk Processors is a do_all class for interaction with the render window, vulkan s queue **Owns:** - [[CanvasState]] - [[CompuState]] + [[+CanvasState]] + [[+CompuState]] -------------------- ===== CanvasState ===== -<[[CanvasState]]>:[[~/source/Trac3r-rust/doc/sfml_rust/canvas/canvas_state/index.html|docs]] +<[[+CanvasState]]>:[[~/source/Trac3r-rust/doc/sfml_rust/canvas/canvas_state/index.html|docs]] * Is used for storage of texture and image buffers in addition to vertex buffers * Also contains logic for writing the stored buffers to the command_buffer diff --git a/notes/CanvasContainerClasses.txt b/notes/VKProcessor/CanvasContainerClasses.txt similarity index 99% rename from notes/CanvasContainerClasses.txt rename to notes/VKProcessor/CanvasContainerClasses.txt index 6afe828a..453bb5a7 100644 --- a/notes/CanvasContainerClasses.txt +++ b/notes/VKProcessor/CanvasContainerClasses.txt @@ -18,6 +18,7 @@ All buffers will have a coupled handle type stored in the [[/src/canvas/mod.rs|c ===== CanvasTexture ===== + ===== CanvasFont ===== diff --git a/notes/CanvasFont.txt b/notes/VKProcessor/CanvasFont.txt similarity index 100% rename from notes/CanvasFont.txt rename to notes/VKProcessor/CanvasFont.txt diff --git a/notes/CanvasFrame.txt b/notes/VKProcessor/CanvasFrame.txt similarity index 100% rename from notes/CanvasFrame.txt rename to notes/VKProcessor/CanvasFrame.txt diff --git a/notes/CanvasImage.txt b/notes/VKProcessor/CanvasImage.txt similarity index 100% rename from notes/CanvasImage.txt rename to notes/VKProcessor/CanvasImage.txt diff --git a/notes/CanvasState.txt b/notes/VKProcessor/CanvasState.txt similarity index 97% rename from notes/CanvasState.txt rename to notes/VKProcessor/CanvasState.txt index 2a7d2aea..ff8430a9 100644 --- a/notes/CanvasState.txt +++ b/notes/VKProcessor/CanvasState.txt @@ -37,7 +37,7 @@ Creation-Date: 2020-02-03T23:30:41-08:00 **Owns:** render_pass [[CanvasImage]] - [[CanvasTexture]] + [[VKProcessor:CanvasTexture]] [[CanvasFont]] CompiledGraphicsPipeline colored_vertex_buffer diff --git a/notes/CanvasTexture.txt b/notes/VKProcessor/CanvasTexture.txt similarity index 100% rename from notes/CanvasTexture.txt rename to notes/VKProcessor/CanvasTexture.txt diff --git a/notes/CompuState.txt b/notes/VKProcessor/CompuState.txt similarity index 100% rename from notes/CompuState.txt rename to notes/VKProcessor/CompuState.txt diff --git a/notes/DynamicVertex.txt b/notes/VKProcessor/DynamicVertex.txt similarity index 100% rename from notes/DynamicVertex.txt rename to notes/VKProcessor/DynamicVertex.txt diff --git a/src/canvas/canvas_frame.rs b/src/canvas/canvas_frame.rs index e67cebd8..950ff814 100644 --- a/src/canvas/canvas_frame.rs +++ b/src/canvas/canvas_frame.rs @@ -1,4 +1,3 @@ -use crate::util::vertex_3d::Vertex3D; use std::sync::Arc; use std::collections::HashMap; use std::hash::Hash; @@ -10,11 +9,7 @@ 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, H: Handle + DynHash> { - fn get_vertices(&self) -> VTypes; - fn get_handle(&self) -> H; -} + pub mod dynhash { use std::any::Any; @@ -74,14 +69,19 @@ 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, VTypes>, + +pub trait DrawableTest { + fn get(&self) -> VertexTypes; +} + +#[derive(Default)] +pub struct CanvasFrameTest { + pub map: Vec, } -impl CanvasFrameTest { - pub fn draw(&mut self, drawable: VTypes) { - self.map.insert(Box::new(10), drawable); +impl CanvasFrameTest { + pub fn draw(&mut self, drawable: &dyn DrawableTest) { + self.map.push(drawable.get()); } } diff --git a/src/canvas/canvas_state.rs b/src/canvas/canvas_state.rs index 0db23002..5138ac9d 100644 --- a/src/canvas/canvas_state.rs +++ b/src/canvas/canvas_state.rs @@ -20,7 +20,6 @@ use vulkano::pipeline::viewport::Viewport; use vulkano::descriptor::descriptor::DescriptorDescTy::TexelBuffer; 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}; use vulkano::memory::pool::PotentialDedicatedAllocation::Generic; use std::borrow::Borrow; @@ -29,11 +28,12 @@ use std::io::Read; use rusttype::{Font, PositionedGlyph, Scale, Rect, point, GlyphId, Line, Curve, Segment}; use vulkano::pipeline::vertex::VertexDefinition; use crate::canvas::managed::shader::dynamic_vertex::RuntimeVertexDef; -use crate::canvas::managed::handles::{CanvasTextureHandle, CanvasImageHandle, CanvasFontHandle, CompiledShaderHandle, Handle}; +use crate::canvas::managed::handles::{CanvasTextureHandle, CanvasImageHandle, CanvasFontHandle, CompiledShaderHandle, Handle, DrawableHandle}; 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; +use crate::util::vertex::{TextVertex3D, TextureVertex2D, ImageVertex2D, ColorVertex2D, CanvasFrameAllocation}; /// Canvas state is used for storage of texture and image buffers in addition to vertex buffers @@ -53,16 +53,6 @@ pub struct CanvasState { // Compiled Graphics pipelines have a handle which self describe their position in this vector shader_buffers: Vec>>, - // Hold onto the vertices we get from the Compu and Canvas Frames - // When the run comes around, push the vertices to the GPU - colored_vertex_buffer: Vec>, - - textured_vertex_buffer: HashMap, Arc<(dyn BufferAccess + Send + Sync)>>, - - image_vertex_buffer: HashMap, Arc<(dyn BufferAccess + Send + Sync)>>, - - text_instances: HashMap, Arc<(dyn BufferAccess + Send + Sync)>>, - // Looks like we gotta hold onto the queue for managing textures queue: Arc, device: Arc, @@ -70,7 +60,6 @@ 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>]) @@ -102,7 +91,6 @@ 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!( @@ -172,11 +160,6 @@ impl CanvasState { shader_buffers: vec![], font_buffers: vec![], - colored_vertex_buffer: vec![], - textured_vertex_buffer: Default::default(), - image_vertex_buffer: Default::default(), - text_instances: HashMap::default(), - queue: queue.clone(), device: device.clone(), render_pass: render_pass.clone(), @@ -393,91 +376,6 @@ impl CanvasState { } } - /// Scrape all the values from the CanvasFrame and then allocate the vertex buffers - pub fn draw(&mut self, canvas_frame: CanvasFrame) { - - // Consume the canvas frame - let mut textured_drawables = canvas_frame.textured_drawables; - let mut colored_drawables = canvas_frame.colored_drawables; - let mut image_drawables = canvas_frame.image_drawables; - let mut text_drawables = canvas_frame.text_drawables; - - // Walk through the consumed items and allocate them to GPU buffers - - self.colored_vertex_buffer.clear(); - { - let g = hprof::enter("Colored Vertex Buffer"); - self.colored_vertex_buffer.push( - ImmutableBuffer::from_iter( - colored_drawables.iter().cloned(), - BufferUsage::vertex_buffer(), - self.queue.clone(), - ).unwrap().0 - ); - } - - self.textured_vertex_buffer.clear(); - { - let g = hprof::enter("Textured Vertex Buffer"); - for (k, v) in textured_drawables.drain() { - let vertex_buffer = v.clone().get(0).unwrap().clone(); - // TODO -// v.clone().iter() -// .fold(Vec::new(), |mut a: Vec, b| { -// a.extend(b); -// a -// }); - - self.textured_vertex_buffer.insert( - k.clone(), - ImmutableBuffer::from_iter( - vertex_buffer.iter().cloned(), - BufferUsage::vertex_buffer(), - self.queue.clone(), - ).unwrap().0, - ); - } - } - - self.image_vertex_buffer.clear(); - { - let g = hprof::enter("Image Vertex Buffer"); - for (k, v) in image_drawables.drain() { - let vertex_buffer = v.clone().get(0).unwrap().clone(); - // TODO -// v.clone().iter() -// .fold(Vec::new(), |mut a: Vec<&RuntimeVertexDef>, b| { -// a.extend(b); -// a -// }); - - self.image_vertex_buffer.insert( - k.clone(), - ImmutableBuffer::from_iter( - vertex_buffer.iter().cloned(), - BufferUsage::vertex_buffer(), - self.queue.clone(), - ).unwrap().0, - ); - } - } - - self.text_instances.clear(); - { - let g = hprof::enter("Text Instance Vertex Buffer"); - for (k, v) in text_drawables.drain() { - self.text_instances.insert( - k.clone(), - ImmutableBuffer::from_iter( - v.iter().cloned(), - BufferUsage::all(), - self.queue.clone(), - ).unwrap().0, - ); - } - } - } - /// Builds the descriptor set for solid colors using the input kernel (needs to support solid colors) fn get_solid_color_descriptor_set(&self, kernel: Arc) -> Box { let o: Box = Box::new( @@ -487,79 +385,58 @@ impl CanvasState { o } + /// Consume and allocated the canvas frame data to the GPU + pub fn allocate(&mut self, canvas_frame: CanvasFrameTest) -> CanvasFrameAllocation { - // 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, - mut command_buffer: AutoCommandBufferBuilder, - framebuffers: Vec>, - image_num: usize, - canvas_frame: CanvasFrameTest) -> AutoCommandBufferBuilder { + let mut colored_vertex_buffer: Vec = Vec::default(); + let mut textured_vertex_buffer: HashMap, Vec> = HashMap::new(); + let mut image_vertex_buffer: HashMap, Vec> = HashMap::new(); + let mut text_instances: HashMap, Vec> = HashMap::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)), - ); - - 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(); - - - - for (k,v) in canvas_frame.map { - - 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 + for value in canvas_frame.map { + match value { + VertexTypes::TextureType(vertices, handle) => { + textured_vertex_buffer.entry(handle).or_insert(vertices.clone()).extend(vertices); + } + VertexTypes::ImageType(vertices, handle) => { + image_vertex_buffer.entry(handle).or_insert(vertices.clone()).extend(vertices); } + VertexTypes::ColorType(vertices) => { + colored_vertex_buffer.extend(vertices); + } + VertexTypes::ThreeDType(vertices) => {} }; + }; - + let mut allocated_colored_buffer: Vec> = Vec::new(); + if !colored_vertex_buffer.is_empty() { + allocated_colored_buffer.push(ImmutableBuffer::from_iter( + colored_vertex_buffer.iter().cloned(), + BufferUsage::vertex_buffer(), + self.queue.clone(), + ).unwrap().0); } - command_buffer - + CanvasFrameAllocation { + colored_vertex_buffer: allocated_colored_buffer, + textured_vertex_buffer: textured_vertex_buffer.into_iter().map(|(k, v)| { + (k, + ImmutableBuffer::from_iter( + v.iter().cloned(), + BufferUsage::vertex_buffer(), + self.queue.clone(), + ).unwrap().0 as Arc<(dyn BufferAccess + Send + Sync)>) + }).collect(), + image_vertex_buffer: image_vertex_buffer.into_iter().map(|(k, v)| { + (k, + ImmutableBuffer::from_iter( + v.iter().cloned(), + BufferUsage::vertex_buffer(), + self.queue.clone(), + ).unwrap().0 as Arc<(dyn BufferAccess + Send + Sync)>) + }).collect(), + text_instances: Default::default(), + } } /// Pushes the draw commands to the command buffer. Requires the framebuffers and @@ -567,7 +444,8 @@ impl CanvasState { pub fn draw_commands(&mut self, mut command_buffer: AutoCommandBufferBuilder, framebuffers: Vec>, - image_num: usize) -> AutoCommandBufferBuilder { + image_num: usize, + allocated_buffers: CanvasFrameAllocation) -> AutoCommandBufferBuilder { // Specify the color to clear the framebuffer with i.e. blue let clear_values = vec!( @@ -597,11 +475,11 @@ impl CanvasState { // This looks a little weird as colored_vertex_buffer is a vec of GPU allocated vecs. // But we can pass in multiple vertex buffers - if !self.colored_vertex_buffer.is_empty() { + if allocated_buffers.colored_vertex_buffer.is_empty() { command_buffer = command_buffer.draw( shader.get_pipeline().clone(), &self.dynamic_state.clone(), - self.colored_vertex_buffer.clone(), + allocated_buffers.colored_vertex_buffer.clone(), (), (), ).unwrap(); } @@ -612,8 +490,8 @@ impl CanvasState { .unwrap().clone().get_handle() as usize ).unwrap(); - if !self.image_vertex_buffer.is_empty() { - for (image_handle, vertex_buffer) in self.image_vertex_buffer.clone() { + if !allocated_buffers.image_vertex_buffer.is_empty() { + for (image_handle, vertex_buffer) in allocated_buffers.image_vertex_buffer.clone() { let handle = image_handle.clone().get_handle() as usize; let descriptor_set = self.image_buffers.get(handle).clone().unwrap().clone() .get_descriptor_set(shader.get_pipeline().clone()); @@ -633,8 +511,8 @@ impl CanvasState { .unwrap().clone().get_handle() as usize ).unwrap(); - if !self.textured_vertex_buffer.is_empty() { - for (texture_handle, vertex_buffer) in self.textured_vertex_buffer.clone() { + if !allocated_buffers.textured_vertex_buffer.is_empty() { + for (texture_handle, vertex_buffer) in allocated_buffers.textured_vertex_buffer.clone() { let handle = texture_handle.clone().get_handle() as usize; let descriptor_set = self.texture_buffers.get(handle).clone().unwrap().clone() .get_descriptor_set(shader.get_pipeline(), self.sampler.clone()); diff --git a/src/canvas/managed/canvas_text.rs b/src/canvas/managed/canvas_text.rs index 121f33e8..e583e8b7 100644 --- a/src/canvas/managed/canvas_text.rs +++ b/src/canvas/managed/canvas_text.rs @@ -10,7 +10,6 @@ use vulkano::format::ClearValue; use vulkano::format::Format::R8Unorm; use std::fs::File; use std::io::Read; -use crate::util::vertex_3d::{Vertex3D, TextVertex3D}; //pub struct Glyph {} diff --git a/src/canvas/managed/handles.rs b/src/canvas/managed/handles.rs index b7485217..6f6f8f24 100644 --- a/src/canvas/managed/handles.rs +++ b/src/canvas/managed/handles.rs @@ -3,6 +3,15 @@ pub trait Handle { fn get_handle(&self) -> u32; } + +pub enum DrawableHandle { + Texture(CanvasTextureHandle), + Image(CanvasImageHandle), + Font(CanvasFontHandle), +} + + + /// Typed wrapper for a u32 handle #[derive(Clone, Debug, Default, PartialEq, Eq, Hash)] pub struct CanvasFontHandle { diff --git a/src/canvas/managed/shader/generic_shader.rs b/src/canvas/managed/shader/generic_shader.rs index 730362f7..a3e428e9 100644 --- a/src/canvas/managed/shader/generic_shader.rs +++ b/src/canvas/managed/shader/generic_shader.rs @@ -11,7 +11,6 @@ use std::ffi::CStr; use std::marker::PhantomData; use vulkano::pipeline::depth_stencil::{DepthStencil, Compare, DepthBounds, Stencil, StencilOp}; use vulkano::pipeline::vertex::{SingleBufferDefinition, VertexDefinition}; -use crate::util::vertex_3d::Vertex3D; use shade_runner as sr; use vulkano::memory::pool::PotentialDedicatedAllocation::Generic; use vulkano::SafeDeref; diff --git a/src/canvas/managed/shader/text_shader.rs b/src/canvas/managed/shader/text_shader.rs index 53349852..f1ac5608 100644 --- a/src/canvas/managed/shader/text_shader.rs +++ b/src/canvas/managed/shader/text_shader.rs @@ -10,7 +10,6 @@ use std::ffi::CStr; use std::marker::PhantomData; use vulkano::pipeline::depth_stencil::{DepthStencil, Compare, DepthBounds, Stencil, StencilOp}; use vulkano::pipeline::vertex::{SingleBufferDefinition, OneVertexOneInstanceDefinition}; -use crate::util::vertex_3d::Vertex3D; use shade_runner as sr; use crate::canvas::managed::shader::shader_common::{ShaderType, CompiledGraphicsPipelineResources, CompiledGraphicsPipeline}; use crate::canvas::managed::handles::CompiledShaderHandle; diff --git a/src/main.rs b/src/main.rs index 02b18678..1c5e0165 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,6 @@ use sprite::Sprite; 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, DrawableTest}; @@ -30,6 +29,7 @@ use crate::compute::managed::compu_sprite::CompuSprite; use std::sync::Arc; use crate::canvas::managed::handles::{CanvasTextureHandle, Handle}; use crate::canvas::canvas_frame::dynhash::DynHash; +use crate::util::vertex::{VertexTypes, TextureVertex2D}; pub mod util; @@ -57,48 +57,6 @@ pub mod compute; */ -#[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); - -#[derive(Clone)] -pub enum VertexTypes { - TextureType(Vec), - ColorType(Vec), - ImageType(Vec), -} - -pub struct DrawableTestee { - vertices: VertexTypes, - handle: Arc, -} - -impl, H: Handle + DynHash> DrawableTest for DrawableTestee { - fn get_vertices(&self) -> VTypes { - unimplemented!() - } - - fn get_handle(&self) -> H { - unimplemented!() - } -} pub fn main() { hprof::start_frame(); @@ -162,12 +120,12 @@ pub fn main() { let sfml_handle = processor.get_texture_handle(String::from("sfml.png")).unwrap(); let font_handle = processor.get_font_handle(String::from("sansation.ttf")).unwrap(); - let funky_sprite = Sprite::new_with_texture((0.0, -0.5), (0.5, 0.5), 0, funky_handle.clone()); - let sfml_sprite = Sprite::new_with_texture((0.0, -0.5), (0.5, 0.5), 1, sfml_handle.clone()); - let text_sprite = Text::new((-0.1,-0.1), (10.0, 10.0), font_handle.clone()); + let funky_sprite = Sprite::new((0.0, -0.5), (0.5, 0.5), 0, funky_handle.clone()); + let sfml_sprite = Sprite::new((0.0, -0.5), (0.5, 0.5), 1, sfml_handle.clone()); + //let text_sprite = Text::new((-0.1,-0.1), (10.0, 10.0), font_handle.clone()); - let test_polygon = Poly::new_with_color((-0.5, -0.5), (0.5, 0.5), 1, (1.0,0.0,0.0,0.0)); + //let test_polygon = Poly::new_with_color((-0.5, -0.5), (0.5, 0.5), 1, (1.0,0.0,0.0,0.0)); drop(q2); drop(q1); @@ -226,17 +184,12 @@ pub fn main() { break; } - 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() }; + let funky_sprite = Sprite::new((0.0, -0.5), (0.5, 0.5), 0, funky_handle.clone()); - canvas_frame.draw(dt.vertices); - canvas_frame.draw(sprite.get_vertices()); + let mut canvas_frame = CanvasFrameTest::default(); + canvas_frame.draw(&funky_sprite); + canvas_frame.draw(&sfml_sprite); let mut compu_frame = CompuFrame::new(); // compu_frame.add(compute_buffer.clone(), compute_kernel.clone()); diff --git a/src/sprite.rs b/src/sprite.rs index 091971d6..bfd6e362 100644 --- a/src/sprite.rs +++ b/src/sprite.rs @@ -1,306 +1,45 @@ use std::sync::Arc; -use crate::util::vertex_3d::Vertex3D; use crate::canvas::*; 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; +use crate::util::vertex::{VertexTypes, TextureVertex2D, Vertex3D}; /// #[derive(Debug, Clone)] pub struct Sprite { - pub verts: VertexTypes::TexturedType(vec![]), - - pub vertices: [(f32, f32, f32); 6], - pub ti_position: [(f32, f32); 6], + pub verts: VertexTypes, position: (f32, f32), size: (f32, f32), - color: (f32, f32, f32, f32), - - textured: bool, - texture_handle: Option>, - - value: GlyphInstance, } /// Container class which implements drawable. impl Sprite { - pub fn new(position: (f32, f32), size: (f32, f32)) -> Sprite { - Sprite::new_with_color(position, size, 0, (0., 0., 0., 0.)) - } - - pub fn new_with_color(position: (f32, f32), - size: (f32, f32), - depth: u32, - color: (f32, f32, f32, f32)) -> Sprite { - let normalized_depth = (depth as f32 / 255.0); - - Sprite { - vertices: [ - (position.0, position.1, normalized_depth), // top left - (position.0, position.1 + size.1, normalized_depth), // bottom left - (position.0 + size.0, position.1 + size.1, normalized_depth), // bottom right - (position.0, position.1, normalized_depth), // top left - (position.0 + size.0, position.1 + size.1, normalized_depth), // bottom right - (position.0 + size.0, position.1, normalized_depth), // top right - ], - - position: position, - ti_position: [ - (-0.0, -0.0), // top left - (-0.0, 1.0), // bottom left - (1.0, 1.0), // bottom right - (-0.0, -0.0), // top left - (1.0, 1.0), // bottom right - (1.0, -0.0), // top right - ], - size: size, - color: color, - textured: false, - texture_handle: None, - value: GlyphInstance { - screen_position: (0.0, 0.0), - atlas_position: (0.0, 0.0), - atlas_size: (0.0, 0.0), - scale: 0.0, - }, - } - } /// - pub fn new_with_texture(position: (f32, f32), - size: (f32, f32), - depth: u32, - texture_handle: Arc) -> Sprite { - let normalized_depth = (depth as f32 / 255.0); - - Sprite { - vertices: [ - (position.0, position.1, normalized_depth), // top left - (position.0, position.1 + size.1, normalized_depth), // bottom left - (position.0 + size.0, position.1 + size.1, normalized_depth), // bottom right - (position.0, position.1, normalized_depth), // top left - (position.0 + size.0, position.1 + size.1, normalized_depth), // bottom right - (position.0 + size.0, position.1, normalized_depth), // top right - ], - position: position, - ti_position: [ - (-0.0, -0.0), // top left - (-0.0, 1.0), // bottom left - (1.0, 1.0), // bottom right - (-0.0, -0.0), // top left - (1.0, 1.0), // bottom right - (1.0, -0.0), // top right - ], - size: size, - color: (1.0, 0.0, 0.0, 1.0), - textured: true, - texture_handle: Some(texture_handle.clone()), - value: Default::default(), - } - } -} - -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() - } - - fn get_color(&self) -> (f32, f32, f32, f32) { - self.color.clone() - } - - fn get_ti_coords(&self) -> Vec<(f32, f32)> { - self.ti_position.to_vec() - } - - fn get_texture_handle(&self) -> Option> { - match self.textured { - true => { - self.texture_handle.clone() - } - false => None, - } - } - - fn get_image_handle(&self) -> Option> { - None - } -} - + pub fn new(position: (f32, f32), + size: (f32, f32), + depth: u32, + texture_handle: Arc) -> Sprite { -#[derive(Debug, Clone)] -pub struct Poly { - pub vertices: Vec<(f32, f32, f32)>, - pub ti_position: Vec<(f32, f32)>, - - position: (f32, f32), - size: (f32, f32), - color: (f32, f32, f32, f32), - - textured: bool, - texture_handle: Option>, - - // ================================== -} - -/// Container class which implements drawable. -impl Poly { - pub fn new(position: (f32, f32), size: (f32, f32)) -> Poly { - Poly::new_with_color(position, size, 0, (0., 0., 0., 0.)) - } - - pub fn new_with_color(position: (f32, f32), - size: (f32, f32), - depth: u32, - color: (f32, f32, f32, f32)) -> Poly { let normalized_depth = (depth as f32 / 255.0); - Poly { - vertices: vec![ - (-0.5, -0.5, normalized_depth), - (-1.0, 1.0, normalized_depth), - (-0.25, 0.0, normalized_depth), - (-0.25, 0.0, normalized_depth), - (-1.0, 1.0, normalized_depth), - (0.0, 0.5, normalized_depth), - (0.25, 0.0, normalized_depth), - (-1.0, 1.0, normalized_depth), - (0.0, 0.5, normalized_depth), - (0.5, -0.5, normalized_depth), - (-1.0, 1.0, normalized_depth), - (0.25, 0.0, normalized_depth), - (0.25, -0.5, normalized_depth), - (-1.0, 1.0, normalized_depth), - (0.5, -0.5, normalized_depth), - (0.25, -0.5, normalized_depth), - (-1.0, 1.0, normalized_depth), - (0.0, -0.1, normalized_depth), - (-0.25, -0.5, normalized_depth), - (-1.0, 1.0, normalized_depth), - (0.0, -0.1, normalized_depth), - (-0.5, -0.5, normalized_depth), - (-1.0, 1.0, normalized_depth), - (-0.25, -0.5, normalized_depth), - ], - + Sprite { + verts: VertexTypes::TextureType(Vec::new(), texture_handle), position: position, - ti_position: vec![ - (0.0, 0.0), - (0.0, 0.0), - (0.0, 0.0), - (0.0, 0.0), - (0.0, 0.0), - (0.0, 0.0), - (0.0, 0.0), - (0.0, 0.0), - (0.0, 0.0), - (0.0, 0.0), - (0.0, 0.0), - (0.0, 0.0), - (0.0, 0.0), - (0.0, 0.0), - (0.0, 0.0), - (0.0, 0.0), - (0.0, 0.0), - (0.0, 0.0), - (0.0, 0.0), - (0.0, 0.0), - (0.0, 0.0), - (0.0, 0.0), - (0.0, 0.0), - (0.0, 0.0), - ], size: size, - color: color, - textured: false, - texture_handle: None, - } - } -} - -impl Drawable for Poly { - fn get_vertices(&self) -> Vec<(f32, f32, f32)> { - self.vertices.to_vec() - } - - fn get_color(&self) -> (f32, f32, f32, f32) { - self.color.clone() - } - - fn get_ti_coords(&self) -> Vec<(f32, f32)> { - self.ti_position.to_vec() - } - - fn get_texture_handle(&self) -> Option> { - match self.textured { - true => { - self.texture_handle.clone() - } - false => None, } } - - fn get_image_handle(&self) -> Option> { - None - } -} - - -#[derive(Debug, Clone)] -pub struct Text { - position: (f32, f32, f32), - scale: f32, - color: (f32, f32, f32, f32), - - text_handle: Arc, - } - -/// Container class which implements drawable. -impl Text { - pub fn new(position: (f32, f32), size: (f32, f32), font_handle: Arc) -> Text { - Text::new_with_color(position, size, 0, (0., 0., 0., 0.), font_handle) +impl DrawableTest for Sprite{ + fn get(&self) -> VertexTypes { + self.verts.clone() } - pub fn new_with_color(position: (f32, f32), - size: (f32, f32), - depth: u32, - color: (f32, f32, f32, f32), - handle: Arc) -> Text { - let normalized_depth = (depth as f32 / 255.0); - - Text { - position: (position.0, position.1, normalized_depth), - scale: 0.0, - color, - text_handle: handle, - } - } } -pub trait TextHandle { - fn do_nothing() -> u32; -} -pub trait TextInstance { - fn get_thing() -> Vec<(u32, u32, u32)>; -} -pub trait TextVertex { - fn get_vertices() -> Vec<(u32, u32, u32)>; -} diff --git a/src/util/mod.rs b/src/util/mod.rs index ade26498..5c4a0bf1 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -4,8 +4,7 @@ use std::ffi::CStr; use std::path::PathBuf; pub mod timer; -pub mod vertex_2d; -pub mod vertex_3d; +pub mod vertex; pub fn load_raw(filename: String) -> (Vec, (u32,u32)) { diff --git a/src/util/vertex.rs b/src/util/vertex.rs new file mode 100644 index 00000000..ed4eca52 --- /dev/null +++ b/src/util/vertex.rs @@ -0,0 +1,78 @@ +use crate::canvas::managed::handles::{CanvasTextureHandle, CanvasImageHandle, CanvasFontHandle}; +use std::sync::Arc; +use vulkano::buffer::BufferAccess; +use std::collections::HashMap; + +#[derive(Default, Debug, Clone, Copy)] +pub struct TextureVertex2D { + pub v_position: [f32; 2], + pub ti_position: [f32; 2], +} +vulkano::impl_vertex!(TextureVertex2D, v_position, ti_position); + +#[derive(Default, Debug, Clone, Copy)] +pub struct ColorVertex2D { + pub v_position: [f32; 2], + pub color: [f32; 4], +} +vulkano::impl_vertex!(ColorVertex2D, v_position, color); + +#[derive(Default, Debug, Clone, Copy)] +pub struct ImageVertex2D { + pub v_position: [f32; 2], + pub color: [f32; 4], +} +vulkano::impl_vertex!(ImageVertex2D, v_position, color); + +#[derive(Default, Debug, Clone, Copy)] +pub struct Vertex3D { + pub v_position: [f32; 3], + pub color : [f32; 4], + pub ti_position: [f32; 2], +} +vulkano::impl_vertex!(Vertex3D, v_position, color, ti_position); + +/// Text vertex 3d with vertex position +#[derive(Default, Debug, Clone, Copy)] +pub struct TextVertex3D { + pub position: [f32; 3], +} + +vulkano::impl_vertex!(TextVertex3D, position); + +// ============================================================================== + +#[derive(Debug, Clone)] +pub enum VertexTypes { + TextureType(Vec, Arc), + ImageType(Vec, Arc), + ColorType(Vec), + ThreeDType(Vec), + +} + +#[derive(Clone)] +pub struct CanvasFrameAllocation { + pub colored_vertex_buffer: Vec>, + pub textured_vertex_buffer: HashMap, Arc<(dyn BufferAccess + Send + Sync)>>, + pub image_vertex_buffer: HashMap, Arc<(dyn BufferAccess + Send + Sync)>>, + pub text_instances: HashMap, Arc<(dyn BufferAccess + Send + Sync)>>, +} + + + + + + + + + + + + + + + + + + diff --git a/src/util/vertex_2d.rs b/src/util/vertex_2d.rs deleted file mode 100644 index c6ecd0f3..00000000 --- a/src/util/vertex_2d.rs +++ /dev/null @@ -1,10 +0,0 @@ - -/// Generic vertex 2d with vertex position, texture position and a 32bit color -#[derive(Default, Debug, Clone, Copy)] -pub struct Vertex2D { - pub v_position: [f32; 2], - pub color : [f32; 4], - pub ti_position: [f32; 2], -} - -vulkano::impl_vertex!(Vertex2D, v_position, color, ti_position); \ No newline at end of file diff --git a/src/util/vertex_3d.rs b/src/util/vertex_3d.rs deleted file mode 100644 index 17214121..00000000 --- a/src/util/vertex_3d.rs +++ /dev/null @@ -1,20 +0,0 @@ - -/// Generic vertex 3d with vertex position, texture position and a 32bit color -#[derive(Default, Debug, Clone, Copy)] -pub struct Vertex3D { - pub v_position: [f32; 3], - pub color : [f32; 4], - pub ti_position: [f32; 2], -} - - -vulkano::impl_vertex!(Vertex3D, v_position, color, ti_position); - -/// Text vertex 3d with vertex position -#[derive(Default, Debug, Clone, Copy)] -pub struct TextVertex3D { - pub position: [f32; 3], -} - -vulkano::impl_vertex!(TextVertex3D, position); - diff --git a/src/vkprocessor.rs b/src/vkprocessor.rs index 4b4027b4..1c5af22c 100644 --- a/src/vkprocessor.rs +++ b/src/vkprocessor.rs @@ -15,13 +15,12 @@ use crate::canvas::canvas_frame::{CanvasFrame, CanvasFrameTest}; use std::time::Duration; use vulkano::pipeline::depth_stencil::{DynamicStencilValue, StencilFaceFlags}; use vulkano::pipeline::vertex::{OneVertexOneInstanceDefinition, SingleBufferDefinition}; -use crate::util::vertex_3d::Vertex3D; use crate::canvas::canvas_state::CanvasState; 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}; +use crate::util::vertex::VertexTypes; /// VKProcessor holds the vulkan instance information, the swapchain, @@ -224,10 +223,10 @@ impl<'a> VkProcessor<'a> { } /// - pub fn run>(&mut self, + pub fn run(&mut self, surface: &'a Arc>, //canvas_frame: CanvasFrame, - canvas_frame: CanvasFrameTest, + canvas_frame: CanvasFrameTest, compute_frame: CompuFrame, ) { @@ -266,13 +265,12 @@ impl<'a> VkProcessor<'a> { drop(g); - { + let allocated_buffers = { // 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.allocate(canvas_frame) + }; let mut command_buffer = AutoCommandBufferBuilder::primary_one_time_submit(self.device.clone(), self.queue.family()).unwrap(); @@ -287,7 +285,7 @@ impl<'a> VkProcessor<'a> { // 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_test(command_buffer, framebuffers, image_num, canvas_frame); + self.canvas_state.draw_commands(command_buffer, framebuffers, image_num, allocated_buffers); // And build let command_buffer = command_buffer.build().unwrap();