fixed depth buffering for images

master
mitchellhansen 5 years ago
parent f6adbd80d1
commit df2543bc8d

@ -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())

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

@ -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(),

@ -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())

@ -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<CanvasImageHandle>,
}
impl CompuSprite {
pub fn new(position: (f32, f32),
size: (f32, f32),
depth: u32,
image_size: (f32, f32),
image_handle: Arc<CanvasImageHandle>) -> 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()
}

@ -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,

@ -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<CanvasTextureHandle>) -> Sprite {
pub fn new_with_texture(position: (f32, f32),
size: (f32, f32),
depth: u32,
texture_handle: Arc<CanvasTextureHandle>) -> 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()
}

Loading…
Cancel
Save