diff --git a/resources/examples/vulkan_example.rs b/resources/examples/vulkan_example.rs index 8f011e2d..01b390cf 100644 --- a/resources/examples/vulkan_example.rs +++ b/resources/examples/vulkan_example.rs @@ -200,6 +200,8 @@ void main() { .viewports_dynamic_scissors_irrelevant(1) // See `vertex_shader`. .fragment_shader(fs.main_entry_point(), ()) + + .depth_stencil_simple_depth() // We have to indicate which subpass of which render pass this pipeline is going to be used // in. The pipeline will only be usable from this particular subpass. .render_pass(Subpass::from(render_pass.clone(), 0).unwrap()) diff --git a/resources/shaders/simple_image.fragment b/resources/shaders/simple_image.fragment index 6289ade6..b3e9c283 100644 --- a/resources/shaders/simple_image.fragment +++ b/resources/shaders/simple_image.fragment @@ -2,7 +2,7 @@ // SIMPLE TEXTURE : FRAGMENT SHADER // These come in from the previous shader (vertex) -layout(location = 0) in vec2 position; +layout(location = 0) in vec2 image_position; // This goes out to the bound image in window_size_dependent setup layout(location = 0) out vec4 f_color; @@ -15,7 +15,7 @@ void main() { ivec2 pos = ivec2(gl_FragCoord.x, gl_FragCoord.y); - f_color = imageLoad(img, ivec2(position)) / (255.0); + f_color = imageLoad(img, ivec2(image_position)) / (255.0); float gamma = 0.5; f_color.rgb = pow(f_color.rgb, vec3(1.0/gamma)); diff --git a/src/canvas.rs b/src/canvas.rs index e21bbc68..d6133006 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -31,7 +31,7 @@ use crate::vertex_3d::Vertex3D; /// pub trait Drawable { - fn get_vertices(&self) -> Vec<(f32, f32)>; + fn get_vertices(&self) -> Vec<(f32, f32, f32)>; fn get_color(&self) -> (f32, f32, f32, f32); fn get_ti_coords(&self) -> Vec<(f32, f32)>; @@ -43,7 +43,7 @@ pub trait Drawable { let color = self.get_color(); self.get_vertices().iter().zip(self.get_ti_coords().iter()).map(|(a, b)| Vertex3D { - v_position: [a.0, a.1, 0.0], + v_position: [a.0, a.1, a.2], color: [color.0, color.1, color.2, color.3], ti_position: [b.0, b.1], }).collect() @@ -468,17 +468,18 @@ impl CanvasState { ).unwrap(); } - // Textures + + // Images let mut shader = self.shader_buffers.get( - self.get_shader_handle(String::from("simple_texture")) + self.get_shader_handle(String::from("simple_image")) .unwrap().clone().handle as usize ).unwrap(); - if !self.textured_vertex_buffer.is_empty() { - for (texture_handle, vertex_buffer) in self.textured_vertex_buffer.clone() { - let handle = texture_handle.clone().handle as usize; - let descriptor_set = self.texture_buffers.get(handle).clone().unwrap().clone() - .get_descriptor_set(shader.clone(), self.sampler.clone()); + if !self.image_vertex_buffer.is_empty() { + for (image_handle, vertex_buffer) in self.image_vertex_buffer.clone() { + let handle = image_handle.clone().handle as usize; + let descriptor_set = self.image_buffers.get(handle).clone().unwrap().clone() + .get_descriptor_set(shader.clone()); command_buffer = command_buffer.draw( shader.get_pipeline().clone(), @@ -489,17 +490,18 @@ impl CanvasState { } } - // Images + + // Textures let mut shader = self.shader_buffers.get( - self.get_shader_handle(String::from("simple_image")) + self.get_shader_handle(String::from("simple_texture")) .unwrap().clone().handle as usize ).unwrap(); - if !self.image_vertex_buffer.is_empty() { - for (image_handle, vertex_buffer) in self.image_vertex_buffer.clone() { - let handle = image_handle.clone().handle as usize; - let descriptor_set = self.image_buffers.get(handle).clone().unwrap().clone() - .get_descriptor_set(shader.clone()); + if !self.textured_vertex_buffer.is_empty() { + for (texture_handle, vertex_buffer) in self.textured_vertex_buffer.clone() { + let handle = texture_handle.clone().handle as usize; + let descriptor_set = self.texture_buffers.get(handle).clone().unwrap().clone() + .get_descriptor_set(shader.clone(), self.sampler.clone()); command_buffer = command_buffer.draw( shader.get_pipeline().clone(), diff --git a/src/canvas_shader.rs b/src/canvas_shader.rs index 0a22763a..42ab85d5 100644 --- a/src/canvas_shader.rs +++ b/src/canvas_shader.rs @@ -131,6 +131,9 @@ impl CanvasShader { second_constant: 0, third_constant: 0.0, }) + + .depth_stencil_simple_depth() + // We have to indicate which subpass of which render pass this pipeline is going to be used // in. The pipeline will only be usable from this particular subpass. .render_pass(Subpass::from(render_pass.clone(), 0).unwrap()) diff --git a/src/compu_sprite.rs b/src/compu_sprite.rs index da4d97c3..e662a6a1 100644 --- a/src/compu_sprite.rs +++ b/src/compu_sprite.rs @@ -2,29 +2,33 @@ use crate::canvas::{CanvasImageHandle, Drawable, CanvasTextureHandle}; use std::sync::Arc; pub struct CompuSprite { - pub vertices: [(f32, f32); 6], + pub vertices: [(f32, f32, f32); 6], pub ti_position: [(f32, f32); 6], position: (f32, f32), size: (f32, f32), color: (f32, f32, f32, f32), image_handle: Arc, + } impl CompuSprite { pub fn new(position: (f32, f32), size: (f32, f32), + depth: u32, image_size: (f32, f32), image_handle: Arc) -> CompuSprite { + let normalized_depth = (depth as f32 / 255.0); + CompuSprite { vertices: [ - (position.0, position.1), // top left - (position.0, position.1 + size.1), // bottom left - (position.0 + size.0, position.1 + size.1), // bottom right - (position.0, position.1), // top left - (position.0 + size.0, position.1 + size.1), // bottom right - (position.0 + size.0, position.1), // top right + (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 ], ti_position: [ @@ -44,7 +48,7 @@ impl CompuSprite { } impl Drawable for CompuSprite { - fn get_vertices(&self) -> Vec<(f32, f32)> { + fn get_vertices(&self) -> Vec<(f32, f32, f32)> { self.vertices.to_vec() } diff --git a/src/main.rs b/src/main.rs index e0d5e9db..91f5cd39 100644 --- a/src/main.rs +++ b/src/main.rs @@ -86,13 +86,11 @@ pub fn main() { let mut mouse_xy = Vector2i::new(0, 0); - let sprite = Sprite::new_with_color((0., 0.), (0.1, 0.1), (1., 0., 0., 1.)); - let sprite2 = Sprite::new_with_color((-0.3, -0.5), (0.1, 0.1), (0., 1., 0., 1.)); let image_data = load_raw(String::from("funky-bird.jpg")); let image_dimensions_f = ((image_data.1).0 as f32, (image_data.1).1 as f32); let image_dimensions_u = image_data.1; - let compu_sprite1 = CompuSprite::new((-1., -0.5), (1.0, 1.0), image_dimensions_f, + let compu_sprite1 = CompuSprite::new((0.0, -0.5), (0.4, 0.4), 3, image_dimensions_f, // This swap image needs to match the size of the compute processor.new_swap_image(image_dimensions_u)); @@ -104,7 +102,8 @@ pub fn main() { let funky_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 funky_sprite = Sprite::new_with_texture((0.0, -0.5), (0.5, 0.5), 2, funky_handle.clone()); + let sfml_sprite = Sprite::new_with_texture((0.0, -0.5), (0.5, 0.5), 1, sfml_handle.clone()); drop(q2); @@ -171,34 +170,10 @@ pub fn main() { } let mut canvas = CanvasFrame::new(); - canvas.draw(&sprite); - canvas.draw(&sprite2); - 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(&funky_sprite); + canvas.draw(&sfml_sprite); canvas.draw(&compu_sprite1); - canvas.draw(&Sprite::new_with_color(( - (elapsed_time + 0.0).sin()/2.0, (elapsed_time + 0.0).sin()/2.0), - (0.09,0.09), - (1.0,0.2,0.5,1.0) - )); - - canvas.draw(&Sprite::new_with_color(((elapsed_time + 0.0).sin(), 0.0), (0.09,0.09), (1.0,1.0,1.0,1.0))); - canvas.draw(&Sprite::new_with_color(((elapsed_time + 0.1).sin(), 0.1), (0.09,0.09), (1.0,1.0,1.0,1.0))); - canvas.draw(&Sprite::new_with_color(((elapsed_time + 0.2).sin(), 0.2), (0.09,0.09), (1.0,1.0,1.0,1.0))); - canvas.draw(&Sprite::new_with_color(((elapsed_time + 0.3).sin(), 0.3), (0.09,0.09), (1.0,1.0,1.0,1.0))); - canvas.draw(&Sprite::new_with_color(((elapsed_time + 0.4).sin(), 0.4), (0.09,0.09), (1.0,1.0,1.0,1.0))); - canvas.draw(&Sprite::new_with_color(((elapsed_time + 0.5).sin(), 0.5), (0.09,0.09), (1.0,1.0,1.0,1.0))); - canvas.draw(&Sprite::new_with_color(((elapsed_time + 0.6).sin(), 0.6), (0.09,0.09), (1.0,1.0,1.0,1.0))); - canvas.draw(&Sprite::new_with_color(((elapsed_time + 0.7).sin(), 0.7), (0.09,0.09), (1.0,1.0,1.0,1.0))); - canvas.draw(&Sprite::new_with_color(((elapsed_time + 0.8).sin(), 0.8), (0.09,0.09), (1.0,1.0,1.0,1.0))); - canvas.draw(&Sprite::new_with_color(((elapsed_time + 0.9).sin(), 0.9), (0.09,0.09), (1.0,1.0,1.0,1.0))); - { let g = hprof::enter("Run"); processor.run(&surface, diff --git a/src/sprite.rs b/src/sprite.rs index 6e9bbec3..62ef5b3c 100644 --- a/src/sprite.rs +++ b/src/sprite.rs @@ -4,7 +4,7 @@ use std::sync::Arc; #[derive(Debug, Clone)] pub struct Sprite { - pub vertices: [(f32, f32); 6], + pub vertices: [(f32, f32, f32); 6], pub ti_position: [(f32, f32); 6], position: (f32, f32), @@ -20,23 +20,24 @@ pub struct Sprite { impl Sprite { pub fn new(position: (f32, f32), size: (f32, f32)) -> Sprite { - Sprite::new_with_color(position, size, (0.,0.,0.,0.)) + 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 fsize = (size.0 as f32, size.1 as f32); + let normalized_depth = (depth as f32 / 255.0); Sprite { vertices: [ - (position.0, position.1 ), // top left - (position.0, position.1 + fsize.1), // bottom left - (position.0 + fsize.0, position.1 + fsize.1 ), // bottom right - (position.0, position.1 ), // top left - (position.0 + fsize.0, position.1 + fsize.1 ), // bottom right - (position.0 + fsize.0, position.1 ), // top right + (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, @@ -56,18 +57,21 @@ impl Sprite { } /// - pub fn new_with_texture(position: (f32, f32), size: (f32, f32), texture_handle: Arc) -> Sprite { + pub fn new_with_texture(position: (f32, f32), + size: (f32, f32), + depth: u32, + texture_handle: Arc) -> Sprite { - let fsize = (size.0 as f32, size.1 as f32); + let normalized_depth = (depth as f32 / 255.0); Sprite { vertices: [ - (position.0, position.1 ), // top left - (position.0, position.1 + fsize.1), // bottom left - (position.0 + fsize.0, position.1 + fsize.1 ), // bottom right - (position.0, position.1 ), // top left - (position.0 + fsize.0, position.1 + fsize.1 ), // bottom right - (position.0 + fsize.0, position.1 ), // top right + (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: [ @@ -91,7 +95,7 @@ impl Sprite { impl Drawable for Sprite { - fn get_vertices(&self) -> Vec<(f32,f32)> { + fn get_vertices(&self) -> Vec<(f32,f32,f32)> { self.vertices.to_vec() }