diff --git a/resources/shaders/color-passthrough.vertex b/resources/shaders/color-passthrough.vertex index bdc2ca9e..0530d5e4 100644 --- a/resources/shaders/color-passthrough.vertex +++ b/resources/shaders/color-passthrough.vertex @@ -1,7 +1,7 @@ #version 450 // These come in from the vertex definition -layout(location = 0) in vec2 v_position; +layout(location = 0) in vec3 v_position; layout(location = 1) in vec4 color; layout(location = 2) in vec2 ti_position; @@ -10,7 +10,7 @@ layout(location = 0) out vec4 out_color; void main() { out_color = color; - gl_Position = vec4(v_position, 0.0, 1.0); + gl_Position = vec4(v_position, 1.0); } diff --git a/resources/shaders/simple.fragment b/resources/shaders/simple.fragment deleted file mode 100644 index f40b64bc..00000000 --- a/resources/shaders/simple.fragment +++ /dev/null @@ -1,7 +0,0 @@ -#version 450 - -layout(location = 0) out vec4 f_color; - -void main() { - f_color = vec4(1.0, 0.0, 0.0, 1.0); -} \ No newline at end of file diff --git a/resources/shaders/simple.vertex b/resources/shaders/simple.vertex deleted file mode 100644 index eca24008..00000000 --- a/resources/shaders/simple.vertex +++ /dev/null @@ -1,7 +0,0 @@ -#version 450 - -layout(location = 0) in vec2 position; - -void main() { - gl_Position = vec4(position, 0.0, 1.0); -} \ No newline at end of file diff --git a/resources/shaders/simple_image.vertex b/resources/shaders/simple_image.vertex index 2dd7081e..e2b62f18 100644 --- a/resources/shaders/simple_image.vertex +++ b/resources/shaders/simple_image.vertex @@ -2,7 +2,7 @@ // SIMPLE IMAGE : VERTEX SHADER // These come in from the vertex definition -layout(location = 0) in vec2 v_position; +layout(location = 0) in vec3 v_position; layout(location = 1) in vec4 color; layout(location = 2) in vec2 ti_position; @@ -11,7 +11,7 @@ layout(location = 0) out vec2 img_coords; void main() { - gl_Position = vec4(v_position, 0.0, 1.0); + gl_Position = vec4(v_position, 1.0); img_coords = ti_position; } diff --git a/resources/shaders/simple_texture.vertex b/resources/shaders/simple_texture.vertex index 15511f39..83a06f4e 100644 --- a/resources/shaders/simple_texture.vertex +++ b/resources/shaders/simple_texture.vertex @@ -3,7 +3,7 @@ // These come in from the vertex definition // TODO : Need to add texture coordinate attribute so I can single VBO all these sumbitches -layout(location = 0) in vec2 v_position; +layout(location = 0) in vec3 v_position; layout(location = 1) in vec4 color; layout(location = 2) in vec2 ti_position; @@ -12,7 +12,7 @@ layout(location = 0) out vec2 tex_coords; void main() { - gl_Position = vec4(v_position, 0.0, 1.0); + gl_Position = vec4(v_position, 1.0); tex_coords = ti_position; } diff --git a/src/canvas.rs b/src/canvas.rs index 2e0ee5a5..e21bbc68 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -1,4 +1,3 @@ -use crate::vertex_2d::Vertex2D; use vulkano::command_buffer::{AutoCommandBufferBuilder, DynamicState}; use std::collections::HashMap; use vulkano::buffer::{BufferAccess, BufferUsage, ImmutableBuffer, CpuAccessibleBuffer}; @@ -23,21 +22,28 @@ use crate::canvas_frame::CanvasFrame; use std::hash::Hash; use crate::canvas_shader::{CanvasShader, CanvasShaderHandle}; use crate::canvas_buffer::{CanvasImage, CanvasTexture}; +use crate::vertex_3d::Vertex3D; /// A drawable object can be passed into a CanvasFrame to be rendered -/// Allows Texture or Image drawing via their handles +/// Very generic implementation. (N % 2 == 0) vertices, ditto for texture coords, and rgba color +/// Provides Image and Texture handles for drawing +/// Split out to two drawables? +/// pub trait Drawable { + fn get_vertices(&self) -> Vec<(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 collect(&self) -> Vec { + fn collect(&self) -> Vec { let color = self.get_color(); self.get_vertices().iter().zip(self.get_ti_coords().iter()).map(|(a, b)| - Vertex2D { - v_position: [a.0, a.1], + Vertex3D { + v_position: [a.0, a.1, 0.0], color: [color.0, color.1, color.2, color.3], ti_position: [b.0, b.1], }).collect() @@ -80,13 +86,13 @@ pub struct CanvasState { // Hold onto the vertices we get from the Compu and Canvas Frames // When the run comes around, push the vertices to the GPU - colored_drawables: Vec, + colored_drawables: Vec, colored_vertex_buffer: Vec>, - textured_drawables: HashMap, Vec>>, + textured_drawables: HashMap, Vec>>, textured_vertex_buffer: HashMap, Arc<(dyn BufferAccess + std::marker::Send + std::marker::Sync)>>, - image_drawables: HashMap, Vec>>, + image_drawables: HashMap, Vec>>, image_vertex_buffer: HashMap, Arc<(dyn BufferAccess + std::marker::Send + std::marker::Sync)>>, // Looks like we gotta hold onto the queue for managing textures @@ -109,10 +115,14 @@ impl CanvasState { depth_range: 0.0..1.0, }]); + let dimensions = [dimensions.width(), dimensions.height()]; + let depth_buffer = AttachmentImage::transient(self.device.clone(), dimensions, Format::D16Unorm).unwrap(); + images.iter().map(|image| { Arc::new( Framebuffer::start(self.render_pass.clone()) .add(image.clone()).unwrap() + .add(depth_buffer.clone()).unwrap() .build().unwrap() ) as Arc }).collect::>() @@ -143,16 +153,21 @@ impl CanvasState { // of your structs that implements the `FormatDesc` trait). Here we use the // same format as the swapchain. format: format, - // TODO: + samples: 1, + }, + // `color` is a custom name we give to the first and only attachment. + depth: { + load: Clear, + store: DontCare, + format: Format::D16Unorm, samples: 1, } }, pass: { // We use the attachment named `color` as the one and only color attachment. color: [color], - //color: [], // No depth-stencil attachment is indicated with empty brackets. - depth_stencil: {} + depth_stencil: {depth} } ).unwrap()); @@ -372,7 +387,7 @@ 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| { + .fold(Vec::new(), |mut a: Vec, b| { a.extend(b); a }); @@ -393,7 +408,7 @@ 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| { + .fold(Vec::new(), |mut a: Vec, b| { a.extend(b); a }); @@ -427,7 +442,10 @@ impl CanvasState { image_num: usize) -> AutoCommandBufferBuilder { // 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])); + let clear_values = vec!( + ClearValue::Float([0.0, 0.0, 1.0, 1.0]), + 1f32.into() + ); let mut command_buffer = command_buffer.begin_render_pass( framebuffers[image_num].clone(), false, clear_values.clone(), diff --git a/src/canvas_frame.rs b/src/canvas_frame.rs index 77b6e3b2..c216e09e 100644 --- a/src/canvas_frame.rs +++ b/src/canvas_frame.rs @@ -1,15 +1,18 @@ -use crate::vertex_2d::{Vertex2D}; +use crate::vertex_3d::{Vertex3D}; use std::sync::Arc; use std::collections::HashMap; use crate::canvas::{Drawable, CanvasTextureHandle, CanvasImageHandle}; +/// pub struct CanvasFrame { - pub colored_drawables: Vec, - pub textured_drawables: HashMap, Vec>>, - pub image_drawables: HashMap, Vec>>, + pub colored_drawables: Vec, + pub textured_drawables: HashMap, Vec>>, + pub image_drawables: HashMap, Vec>>, } impl CanvasFrame { + + /// Creates a bare canvas frame with empty accumulators pub fn new() -> CanvasFrame { CanvasFrame { colored_drawables: vec![], @@ -18,7 +21,7 @@ impl CanvasFrame { } } - // Accumulates the drawables vertices and colors + /// Accumulates the drawables collected Vertex2D's pub fn draw(&mut self, drawable: &dyn Drawable) { match drawable.get_texture_handle() { Some(handle) => { diff --git a/src/canvas_shader.rs b/src/canvas_shader.rs index 795ae66e..0a22763a 100644 --- a/src/canvas_shader.rs +++ b/src/canvas_shader.rs @@ -10,6 +10,7 @@ use vulkano::pipeline::shader::{GraphicsShaderType, ShaderModule, Specialization use vulkano::swapchain::Capabilities; use crate::vertex_2d::Vertex2D; use vulkano::pipeline::vertex::SingleBufferDefinition; +use crate::vertex_3d::Vertex3D; /// Typed wrapper for a u32 shader handle (index id) #[derive(Clone, Debug, Default, PartialEq, Eq, Hash)] @@ -113,7 +114,7 @@ impl CanvasShader { CanvasShader { graphics_pipeline: Some(Arc::new(GraphicsPipeline::start() - .vertex_input(SingleBufferDefinition::::new()) + .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 604f4f35..e0d5e9db 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,16 +46,6 @@ pub mod compu_sprite; pub mod compu_kernel; pub mod compu_buffer; - -/* - -Alright, what the hell do I do next... - -Canvas works, but I want to use CPU accessible buffer instead of immutable buffer - I think it would be faster if we reuse fewer oversized buffers than vis versa -*/ - - /// Main Entry pub fn main() { hprof::start_frame(); @@ -115,7 +105,6 @@ pub fn main() { 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()); drop(q2); @@ -184,9 +173,13 @@ pub fn main() { let mut canvas = CanvasFrame::new(); canvas.draw(&sprite); canvas.draw(&sprite2); - - canvas.draw(&sprite4); canvas.draw(&sprite3); + + canvas.draw(&Sprite::new_with_texture( + (0.3, -1.0), + (1.0 + elapsed_time.sin()/2.0, 1.0 + elapsed_time.sin()/2.0), + sfml_handle.clone())); + canvas.draw(&compu_sprite1); canvas.draw(&Sprite::new_with_color(( diff --git a/src/sprite.rs b/src/sprite.rs index 5ec2e6d3..6e9bbec3 100644 --- a/src/sprite.rs +++ b/src/sprite.rs @@ -16,6 +16,7 @@ pub struct Sprite { } +/// Container class which implements drawable. impl Sprite { pub fn new(position: (f32, f32), size: (f32, f32)) -> Sprite { @@ -54,6 +55,7 @@ impl Sprite { } } + /// pub fn new_with_texture(position: (f32, f32), size: (f32, f32), texture_handle: Arc) -> Sprite { let fsize = (size.0 as f32, size.1 as f32); @@ -88,6 +90,7 @@ impl Sprite { } impl Drawable for Sprite { + fn get_vertices(&self) -> Vec<(f32,f32)> { self.vertices.to_vec() } diff --git a/src/vertex_2d.rs b/src/vertex_2d.rs index 6e1dabb4..507477e9 100644 --- a/src/vertex_2d.rs +++ b/src/vertex_2d.rs @@ -1,4 +1,6 @@ + +/// 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], diff --git a/src/vertex_3d.rs b/src/vertex_3d.rs index b1afc5b5..76ceb79e 100644 --- a/src/vertex_3d.rs +++ b/src/vertex_3d.rs @@ -1,15 +1,10 @@ -#[derive(Default, Debug, Clone)] +#[derive(Default, Debug, Clone, Copy)] pub struct Vertex3D { - position: [f32; 3] + pub v_position: [f32; 3], + pub color : [f32; 4], + pub ti_position: [f32; 2], } -#[derive(Default, Debug, Clone)] -pub struct ColoredVertex3D { - position: [f32; 3], - color : [u8; 4], -} - -vulkano::impl_vertex!(ColoredVertex3D, position, color); -vulkano::impl_vertex!(Vertex3D, position); +vulkano::impl_vertex!(Vertex3D, v_position, color, ti_position);