From 73456dc58d88e7ca868d1c39ddbaf4ef08c3c730 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Tue, 10 Sep 2019 22:47:19 -0700 Subject: [PATCH] multi texturing works --- src/canvas.rs | 32 +++++++++++++++++++------------- src/canvas_shader.rs | 3 ++- src/main.rs | 2 +- src/vertex_2d.rs | 3 ++- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/canvas.rs b/src/canvas.rs index fb0154bb..2e0ee5a5 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -1,4 +1,4 @@ -use crate::vertex_2d::{Vertex2D}; +use crate::vertex_2d::Vertex2D; use vulkano::command_buffer::{AutoCommandBufferBuilder, DynamicState}; use std::collections::HashMap; use vulkano::buffer::{BufferAccess, BufferUsage, ImmutableBuffer, CpuAccessibleBuffer}; @@ -35,8 +35,8 @@ pub trait Drawable { fn collect(&self) -> Vec { let color = self.get_color(); - self.get_vertices().iter().zip(self.get_ti_coords().iter()).map(|(a,b)| - Vertex2D{ + self.get_vertices().iter().zip(self.get_ti_coords().iter()).map(|(a, b)| + Vertex2D { v_position: [a.0, a.1], color: [color.0, color.1, color.2, color.3], ti_position: [b.0, b.1], @@ -68,7 +68,6 @@ pub struct CanvasImageHandle { /// Canvas state also contains logic for writing the stored buffers to the command_buffer #[derive(Clone)] pub struct CanvasState { - /// Generated during new() dynamic_state: DynamicState, /// Generated during new() @@ -98,11 +97,9 @@ 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>]) -> Vec> { - let dimensions = images[0].dimensions(); self.dynamic_state.viewports = @@ -126,8 +123,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!( @@ -188,7 +183,6 @@ impl CanvasState { /// Using the dimensions and suggested usage, load a CanvasImage and return it's handle pub fn create_image(&mut self, dimensions: (u32, u32), usage: ImageUsage) -> Arc { - let handle = Arc::new(CanvasImageHandle { handle: self.image_buffers.len() as u32 }); let image = CanvasImage { @@ -281,7 +275,6 @@ impl CanvasState { shader_type: ShaderType, physical: PhysicalDevice, capabilities: Capabilities) -> Option> { - let handle = Arc::new(CanvasShaderHandle { handle: self.shader_buffers.len() as u32 }); @@ -378,10 +371,16 @@ impl CanvasState { { let g = hprof::enter("Textured Vertex Buffer"); for (k, v) in self.textured_drawables.drain() { + let vertex_buffer = v.clone().iter() + .fold(Vec::new(), |mut a: Vec, b| { + a.extend(b); + a + }); + self.textured_vertex_buffer.insert( k.clone(), ImmutableBuffer::from_iter( - v.first().unwrap().iter().cloned(), // I don't think doing a first is the right thing + vertex_buffer.iter().cloned(), BufferUsage::vertex_buffer(), self.queue.clone(), ).unwrap().0, @@ -393,11 +392,16 @@ impl CanvasState { { let g = hprof::enter("Image Vertex Buffer"); for (k, v) in self.image_drawables.drain() { + let vertex_buffer = v.clone().iter() + .fold(Vec::new(), |mut a: Vec, b| { + a.extend(b); + a + }); + self.image_vertex_buffer.insert( k.clone(), ImmutableBuffer::from_iter( - v.iter().fold(Vec::new(), |mut acc, x| [acc.as_slice(), x.as_slice()].concat()).iter(), - //v.first().unwrap().iter().cloned(), + vertex_buffer.iter().cloned(), BufferUsage::vertex_buffer(), self.queue.clone(), ).unwrap().0, @@ -460,6 +464,7 @@ impl CanvasState { command_buffer = command_buffer.draw( shader.get_pipeline().clone(), + // Multiple vertex buffers must have their definition in the pipeline! &self.dynamic_state.clone(), vec![vertex_buffer], vec![descriptor_set], (), ).unwrap(); @@ -480,6 +485,7 @@ impl CanvasState { command_buffer = command_buffer.draw( shader.get_pipeline().clone(), + // Multiple vertex buffers must have their definition in the pipeline! &self.dynamic_state.clone(), vec![vertex_buffer], vec![descriptor_set], (), ).unwrap(); diff --git a/src/canvas_shader.rs b/src/canvas_shader.rs index b7acf51b..795ae66e 100644 --- a/src/canvas_shader.rs +++ b/src/canvas_shader.rs @@ -9,6 +9,7 @@ use vulkano::framebuffer::{Subpass, RenderPassAbstract, Framebuffer, Framebuffer use vulkano::pipeline::shader::{GraphicsShaderType, ShaderModule, SpecializationConstants, SpecializationMapEntry}; use vulkano::swapchain::Capabilities; use crate::vertex_2d::Vertex2D; +use vulkano::pipeline::vertex::SingleBufferDefinition; /// Typed wrapper for a u32 shader handle (index id) #[derive(Clone, Debug, Default, PartialEq, Eq, Hash)] @@ -112,7 +113,7 @@ impl CanvasShader { CanvasShader { graphics_pipeline: Some(Arc::new(GraphicsPipeline::start() - .vertex_input_single_buffer::() + .vertex_input(SingleBufferDefinition::::new()) .vertex_shader(vertex_entry_point.clone().unwrap(), ShaderSpecializationConstants { first_constant: 0, diff --git a/src/main.rs b/src/main.rs index ce464401..604f4f35 100644 --- a/src/main.rs +++ b/src/main.rs @@ -112,7 +112,7 @@ pub fn main() { .expect("Can't find that kernel"); let funky_handle = processor.get_texture_handle(String::from("funky-bird.jpg")).unwrap(); - let sfml_handle = processor.get_texture_handle(String::from("funky-bird.jpg")).unwrap(); + let sfml_handle = processor.get_texture_handle(String::from("sfml.png")).unwrap(); let sprite3 = Sprite::new_with_texture((0.0, -0.5), (0.5, 0.5), funky_handle.clone()); let sprite4 = Sprite::new_with_texture((0.3, 0.5), (0.9, 0.9), sfml_handle.clone()); diff --git a/src/vertex_2d.rs b/src/vertex_2d.rs index ac4e0741..6e1dabb4 100644 --- a/src/vertex_2d.rs +++ b/src/vertex_2d.rs @@ -1,4 +1,5 @@ -#[derive(Default, Debug, Clone)] + +#[derive(Default, Debug, Clone, Copy)] pub struct Vertex2D { pub v_position: [f32; 2], pub color : [f32; 4],