depth buffer compiles and switched to 3d vertices

master
mitchellhansen 5 years ago
parent 73456dc58d
commit f6adbd80d1

@ -1,7 +1,7 @@
#version 450 #version 450
// These come in from the vertex definition // 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 = 1) in vec4 color;
layout(location = 2) in vec2 ti_position; layout(location = 2) in vec2 ti_position;
@ -10,7 +10,7 @@ layout(location = 0) out vec4 out_color;
void main() { void main() {
out_color = color; out_color = color;
gl_Position = vec4(v_position, 0.0, 1.0); gl_Position = vec4(v_position, 1.0);
} }

@ -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);
}

@ -1,7 +0,0 @@
#version 450
layout(location = 0) in vec2 position;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
}

@ -2,7 +2,7 @@
// SIMPLE IMAGE : VERTEX SHADER // SIMPLE IMAGE : VERTEX SHADER
// These come in from the vertex definition // 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 = 1) in vec4 color;
layout(location = 2) in vec2 ti_position; layout(location = 2) in vec2 ti_position;
@ -11,7 +11,7 @@ layout(location = 0) out vec2 img_coords;
void main() { void main() {
gl_Position = vec4(v_position, 0.0, 1.0); gl_Position = vec4(v_position, 1.0);
img_coords = ti_position; img_coords = ti_position;
} }

@ -3,7 +3,7 @@
// These come in from the vertex definition // These come in from the vertex definition
// TODO : Need to add texture coordinate attribute so I can single VBO all these sumbitches // 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 = 1) in vec4 color;
layout(location = 2) in vec2 ti_position; layout(location = 2) in vec2 ti_position;
@ -12,7 +12,7 @@ layout(location = 0) out vec2 tex_coords;
void main() { void main() {
gl_Position = vec4(v_position, 0.0, 1.0); gl_Position = vec4(v_position, 1.0);
tex_coords = ti_position; tex_coords = ti_position;
} }

@ -1,4 +1,3 @@
use crate::vertex_2d::Vertex2D;
use vulkano::command_buffer::{AutoCommandBufferBuilder, DynamicState}; use vulkano::command_buffer::{AutoCommandBufferBuilder, DynamicState};
use std::collections::HashMap; use std::collections::HashMap;
use vulkano::buffer::{BufferAccess, BufferUsage, ImmutableBuffer, CpuAccessibleBuffer}; use vulkano::buffer::{BufferAccess, BufferUsage, ImmutableBuffer, CpuAccessibleBuffer};
@ -23,21 +22,28 @@ use crate::canvas_frame::CanvasFrame;
use std::hash::Hash; use std::hash::Hash;
use crate::canvas_shader::{CanvasShader, CanvasShaderHandle}; use crate::canvas_shader::{CanvasShader, CanvasShaderHandle};
use crate::canvas_buffer::{CanvasImage, CanvasTexture}; use crate::canvas_buffer::{CanvasImage, CanvasTexture};
use crate::vertex_3d::Vertex3D;
/// A drawable object can be passed into a CanvasFrame to be rendered /// 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 { pub trait Drawable {
fn get_vertices(&self) -> Vec<(f32, f32)>; fn get_vertices(&self) -> Vec<(f32, f32)>;
fn get_color(&self) -> (f32, f32, f32, f32); fn get_color(&self) -> (f32, f32, f32, f32);
fn get_ti_coords(&self) -> Vec<(f32, f32)>; fn get_ti_coords(&self) -> Vec<(f32, f32)>;
fn get_texture_handle(&self) -> Option<Arc<CanvasTextureHandle>>; fn get_texture_handle(&self) -> Option<Arc<CanvasTextureHandle>>;
fn get_image_handle(&self) -> Option<Arc<CanvasImageHandle>>; fn get_image_handle(&self) -> Option<Arc<CanvasImageHandle>>;
// fn get_text_handle(&self) -> Option<Arc<CanvasTextHandle>>;
fn collect(&self) -> Vec<Vertex2D> { fn collect(&self) -> Vec<Vertex3D> {
let color = self.get_color(); let color = self.get_color();
self.get_vertices().iter().zip(self.get_ti_coords().iter()).map(|(a, b)| self.get_vertices().iter().zip(self.get_ti_coords().iter()).map(|(a, b)|
Vertex2D { Vertex3D {
v_position: [a.0, a.1], v_position: [a.0, a.1, 0.0],
color: [color.0, color.1, color.2, color.3], color: [color.0, color.1, color.2, color.3],
ti_position: [b.0, b.1], ti_position: [b.0, b.1],
}).collect() }).collect()
@ -80,13 +86,13 @@ pub struct CanvasState {
// Hold onto the vertices we get from the Compu and Canvas Frames // Hold onto the vertices we get from the Compu and Canvas Frames
// When the run comes around, push the vertices to the GPU // When the run comes around, push the vertices to the GPU
colored_drawables: Vec<Vertex2D>, colored_drawables: Vec<Vertex3D>,
colored_vertex_buffer: Vec<Arc<(dyn BufferAccess + std::marker::Send + std::marker::Sync)>>, colored_vertex_buffer: Vec<Arc<(dyn BufferAccess + std::marker::Send + std::marker::Sync)>>,
textured_drawables: HashMap<Arc<CanvasTextureHandle>, Vec<Vec<Vertex2D>>>, textured_drawables: HashMap<Arc<CanvasTextureHandle>, Vec<Vec<Vertex3D>>>,
textured_vertex_buffer: HashMap<Arc<CanvasTextureHandle>, Arc<(dyn BufferAccess + std::marker::Send + std::marker::Sync)>>, textured_vertex_buffer: HashMap<Arc<CanvasTextureHandle>, Arc<(dyn BufferAccess + std::marker::Send + std::marker::Sync)>>,
image_drawables: HashMap<Arc<CanvasImageHandle>, Vec<Vec<Vertex2D>>>, image_drawables: HashMap<Arc<CanvasImageHandle>, Vec<Vec<Vertex3D>>>,
image_vertex_buffer: HashMap<Arc<CanvasImageHandle>, Arc<(dyn BufferAccess + std::marker::Send + std::marker::Sync)>>, image_vertex_buffer: HashMap<Arc<CanvasImageHandle>, Arc<(dyn BufferAccess + std::marker::Send + std::marker::Sync)>>,
// Looks like we gotta hold onto the queue for managing textures // Looks like we gotta hold onto the queue for managing textures
@ -109,10 +115,14 @@ impl CanvasState {
depth_range: 0.0..1.0, 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| { images.iter().map(|image| {
Arc::new( Arc::new(
Framebuffer::start(self.render_pass.clone()) Framebuffer::start(self.render_pass.clone())
.add(image.clone()).unwrap() .add(image.clone()).unwrap()
.add(depth_buffer.clone()).unwrap()
.build().unwrap() .build().unwrap()
) as Arc<dyn FramebufferAbstract + Send + Sync> ) as Arc<dyn FramebufferAbstract + Send + Sync>
}).collect::<Vec<_>>() }).collect::<Vec<_>>()
@ -143,16 +153,21 @@ impl CanvasState {
// of your structs that implements the `FormatDesc` trait). Here we use the // of your structs that implements the `FormatDesc` trait). Here we use the
// same format as the swapchain. // same format as the swapchain.
format: format, 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, samples: 1,
} }
}, },
pass: { pass: {
// We use the attachment named `color` as the one and only color attachment. // We use the attachment named `color` as the one and only color attachment.
color: [color], color: [color],
//color: [],
// No depth-stencil attachment is indicated with empty brackets. // No depth-stencil attachment is indicated with empty brackets.
depth_stencil: {} depth_stencil: {depth}
} }
).unwrap()); ).unwrap());
@ -372,7 +387,7 @@ impl CanvasState {
let g = hprof::enter("Textured Vertex Buffer"); let g = hprof::enter("Textured Vertex Buffer");
for (k, v) in self.textured_drawables.drain() { for (k, v) in self.textured_drawables.drain() {
let vertex_buffer = v.clone().iter() let vertex_buffer = v.clone().iter()
.fold(Vec::new(), |mut a: Vec<Vertex2D>, b| { .fold(Vec::new(), |mut a: Vec<Vertex3D>, b| {
a.extend(b); a.extend(b);
a a
}); });
@ -393,7 +408,7 @@ impl CanvasState {
let g = hprof::enter("Image Vertex Buffer"); let g = hprof::enter("Image Vertex Buffer");
for (k, v) in self.image_drawables.drain() { for (k, v) in self.image_drawables.drain() {
let vertex_buffer = v.clone().iter() let vertex_buffer = v.clone().iter()
.fold(Vec::new(), |mut a: Vec<Vertex2D>, b| { .fold(Vec::new(), |mut a: Vec<Vertex3D>, b| {
a.extend(b); a.extend(b);
a a
}); });
@ -427,7 +442,10 @@ impl CanvasState {
image_num: usize) -> AutoCommandBufferBuilder { image_num: usize) -> AutoCommandBufferBuilder {
// Specify the color to clear the framebuffer with i.e. blue // 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( let mut command_buffer = command_buffer.begin_render_pass(
framebuffers[image_num].clone(), false, clear_values.clone(), framebuffers[image_num].clone(), false, clear_values.clone(),

@ -1,15 +1,18 @@
use crate::vertex_2d::{Vertex2D}; use crate::vertex_3d::{Vertex3D};
use std::sync::Arc; use std::sync::Arc;
use std::collections::HashMap; use std::collections::HashMap;
use crate::canvas::{Drawable, CanvasTextureHandle, CanvasImageHandle}; use crate::canvas::{Drawable, CanvasTextureHandle, CanvasImageHandle};
///
pub struct CanvasFrame { pub struct CanvasFrame {
pub colored_drawables: Vec<Vertex2D>, pub colored_drawables: Vec<Vertex3D>,
pub textured_drawables: HashMap<Arc<CanvasTextureHandle>, Vec<Vec<Vertex2D>>>, pub textured_drawables: HashMap<Arc<CanvasTextureHandle>, Vec<Vec<Vertex3D>>>,
pub image_drawables: HashMap<Arc<CanvasImageHandle>, Vec<Vec<Vertex2D>>>, pub image_drawables: HashMap<Arc<CanvasImageHandle>, Vec<Vec<Vertex3D>>>,
} }
impl CanvasFrame { impl CanvasFrame {
/// Creates a bare canvas frame with empty accumulators
pub fn new() -> CanvasFrame { pub fn new() -> CanvasFrame {
CanvasFrame { CanvasFrame {
colored_drawables: vec![], 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) { pub fn draw(&mut self, drawable: &dyn Drawable) {
match drawable.get_texture_handle() { match drawable.get_texture_handle() {
Some(handle) => { Some(handle) => {

@ -10,6 +10,7 @@ use vulkano::pipeline::shader::{GraphicsShaderType, ShaderModule, Specialization
use vulkano::swapchain::Capabilities; use vulkano::swapchain::Capabilities;
use crate::vertex_2d::Vertex2D; use crate::vertex_2d::Vertex2D;
use vulkano::pipeline::vertex::SingleBufferDefinition; use vulkano::pipeline::vertex::SingleBufferDefinition;
use crate::vertex_3d::Vertex3D;
/// Typed wrapper for a u32 shader handle (index id) /// Typed wrapper for a u32 shader handle (index id)
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)] #[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
@ -113,7 +114,7 @@ impl CanvasShader {
CanvasShader { CanvasShader {
graphics_pipeline: Some(Arc::new(GraphicsPipeline::start() graphics_pipeline: Some(Arc::new(GraphicsPipeline::start()
.vertex_input(SingleBufferDefinition::<Vertex2D>::new()) .vertex_input(SingleBufferDefinition::<Vertex3D>::new())
.vertex_shader(vertex_entry_point.clone().unwrap(), ShaderSpecializationConstants { .vertex_shader(vertex_entry_point.clone().unwrap(), ShaderSpecializationConstants {
first_constant: 0, first_constant: 0,

@ -46,16 +46,6 @@ pub mod compu_sprite;
pub mod compu_kernel; pub mod compu_kernel;
pub mod compu_buffer; 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 /// Main Entry
pub fn main() { pub fn main() {
hprof::start_frame(); hprof::start_frame();
@ -115,7 +105,6 @@ pub fn main() {
let sfml_handle = processor.get_texture_handle(String::from("sfml.png")).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 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); drop(q2);
@ -184,9 +173,13 @@ pub fn main() {
let mut canvas = CanvasFrame::new(); let mut canvas = CanvasFrame::new();
canvas.draw(&sprite); canvas.draw(&sprite);
canvas.draw(&sprite2); canvas.draw(&sprite2);
canvas.draw(&sprite4);
canvas.draw(&sprite3); 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(&compu_sprite1);
canvas.draw(&Sprite::new_with_color(( canvas.draw(&Sprite::new_with_color((

@ -16,6 +16,7 @@ pub struct Sprite {
} }
/// Container class which implements drawable.
impl Sprite { impl Sprite {
pub fn new(position: (f32, f32), size: (f32, f32)) -> 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<CanvasTextureHandle>) -> Sprite { pub fn new_with_texture(position: (f32, f32), size: (f32, f32), texture_handle: Arc<CanvasTextureHandle>) -> Sprite {
let fsize = (size.0 as f32, size.1 as f32); let fsize = (size.0 as f32, size.1 as f32);
@ -88,6 +90,7 @@ impl Sprite {
} }
impl Drawable for Sprite { impl Drawable for Sprite {
fn get_vertices(&self) -> Vec<(f32,f32)> { fn get_vertices(&self) -> Vec<(f32,f32)> {
self.vertices.to_vec() self.vertices.to_vec()
} }

@ -1,4 +1,6 @@
/// Generic vertex 2d with vertex position, texture position and a 32bit color
#[derive(Default, Debug, Clone, Copy)] #[derive(Default, Debug, Clone, Copy)]
pub struct Vertex2D { pub struct Vertex2D {
pub v_position: [f32; 2], pub v_position: [f32; 2],

@ -1,15 +1,10 @@
#[derive(Default, Debug, Clone)] #[derive(Default, Debug, Clone, Copy)]
pub struct Vertex3D { pub struct Vertex3D {
position: [f32; 3] pub v_position: [f32; 3],
pub color : [f32; 4],
pub ti_position: [f32; 2],
} }
#[derive(Default, Debug, Clone)] vulkano::impl_vertex!(Vertex3D, v_position, color, ti_position);
pub struct ColoredVertex3D {
position: [f32; 3],
color : [u8; 4],
}
vulkano::impl_vertex!(ColoredVertex3D, position, color);
vulkano::impl_vertex!(Vertex3D, position);

Loading…
Cancel
Save