diff --git a/Cargo.toml b/Cargo.toml index 8fd201d6..f0dc92fc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,15 +10,17 @@ simple-stopwatch="0.1.4" nalgebra = "0.18.0" image = "0.21.2" rand = "0.6.5" -vulkano = "0.13.0" -#vulkano = {path = "../vulkano/vulkano"} -vulkano-shaders = "0.13.0" -vulkano-win = "0.13.0" +#vulkano = "0.13.0" +vulkano = {path = "../vulkano/vulkano"} +#vulkano-shaders = "0.14.0" +vulkano-shaders = {path = "../vulkano-shaders"} +#vulkano-win = "0.14.0" +vulkano-win= {path = "../vulkano-win"} time = "0.1.38" -shaderc = "0.5.0" +shaderc = "0.6.1" #shade_runner = {version = "0.1.1", git = "https://github.com/MitchellHansen/shade_runner"} shade_runner = {path = "../shade_runner"} winit = "0.19.1" #criterion = "0.3.0" hprof = "0.1.3" -rusttype = { version = "0.7.0", features = ["gpu_cache"] } \ No newline at end of file +rusttype = { version = "0.7.0", features = ["gpu_cache"] } diff --git a/src/canvas.rs b/src/canvas.rs index 8649c229..e3edcb80 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -24,6 +24,7 @@ use crate::canvas_text::{CanvasText, CanvasTextHandle}; use crate::canvas_shader::{CanvasShader, CanvasShaderHandle}; use crate::canvas_buffer::{CanvasImage, CanvasTexture, CanvasTextCache}; use crate::vertex_3d::Vertex3D; +use vulkano::pipeline::depth_stencil::{StencilFaceFlags, DynamicStencilValue}; /// A drawable object can be passed into a CanvasFrame to be rendered /// Very generic implementation. (N % 2 == 0) vertices, ditto for texture coords, and rgba color @@ -175,7 +176,23 @@ impl CanvasState { CanvasState { - dynamic_state: DynamicState { line_width: None, viewports: None, scissors: None }, + dynamic_state: DynamicState { + line_width: None, + viewports: None, + scissors: None, + compare_mask: Some(DynamicStencilValue { + face: StencilFaceFlags::StencilFrontAndBack, + value: 0xFF, + }), + write_mask: Some(DynamicStencilValue { + face: StencilFaceFlags::StencilFrontAndBack, + value: 0xFF, + }), + reference: Some(DynamicStencilValue { + face: StencilFaceFlags::StencilFrontAndBack, + value: 0xFF, + }), + }, sampler: Sampler::new(device.clone(), Filter::Linear, Filter::Linear, MipmapMode::Nearest, @@ -332,10 +349,12 @@ impl CanvasState { shader_type: ShaderType, physical: PhysicalDevice, capabilities: Capabilities) -> Option> { + let handle = Arc::new(CanvasShaderHandle { handle: self.shader_buffers.len() as u32 }); + // TODO : what is even going on here let shader = match shader_type { ShaderType::SOLID => { Arc::new(CanvasShader::new( @@ -478,7 +497,7 @@ impl CanvasState { /// Pushes the draw commands to the command buffer. Requires the framebuffers and /// image number to be passed in as they are taken care of by the vkprocessor - pub fn draw_commands(&self, + pub fn draw_commands(&mut self, mut command_buffer: AutoCommandBufferBuilder, framebuffers: Vec>, image_num: usize) -> AutoCommandBufferBuilder { @@ -486,9 +505,16 @@ impl CanvasState { // 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]), - ClearValue::DepthStencil((1.0, 0)), + ClearValue::DepthStencil((1.0, 0x00)), ); +// self.dynamic_state = DynamicState { +// line_width: None, +// viewports: self.dynamic_state.viewports.clone(), +// scissors: None, +// compare_mask: Some(StencilMask{ face: StencilFaceFlags::StencilFaceFrontBit, mask: 0xFF }), +// }; + let mut command_buffer = command_buffer.begin_render_pass( framebuffers[image_num].clone(), false, clear_values.clone(), ).unwrap(); @@ -501,6 +527,7 @@ impl CanvasState { // This looks a little weird as colored_vertex_buffer is a vec of GPU allocated vecs. // But we can pass in multiple vertex buffers + if !self.colored_vertex_buffer.is_empty() { command_buffer = command_buffer.draw( shader.get_pipeline().clone(), @@ -510,7 +537,6 @@ impl CanvasState { ).unwrap(); } - // Images let mut shader = self.shader_buffers.get( self.get_shader_handle(String::from("simple_image")) @@ -529,10 +555,10 @@ impl CanvasState { &self.dynamic_state.clone(), vec![vertex_buffer], vec![descriptor_set], (), ).unwrap(); + } } - // Textures let mut shader = self.shader_buffers.get( self.get_shader_handle(String::from("simple_texture")) diff --git a/src/canvas_shader.rs b/src/canvas_shader.rs index 8e218a94..14895488 100644 --- a/src/canvas_shader.rs +++ b/src/canvas_shader.rs @@ -12,6 +12,51 @@ use crate::vertex_2d::Vertex2D; use vulkano::pipeline::vertex::SingleBufferDefinition; use crate::vertex_3d::Vertex3D; use vulkano::pipeline::depth_stencil::{DepthStencil, Stencil, StencilOp, Compare, DepthBounds}; +use std::collections::HashSet; + +/// Typed wrapper for a u32 handle +#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)] +pub struct CompiledGraphicsPipelineHandle { + pub handle: u32 +} + +pub trait CompiledGraphicsPipeline { + fn get_paths(&self, filename: String) -> Vec<(ShaderType, PathBuf)> { + + let project_root = + std::env::current_dir() + .expect("failed to get root directory"); + + let mut shader_path = project_root.clone(); + shader_path.push(PathBuf::from("resources/shaders/")); + + + + let mut vertex_shader_path = shader_path.clone(); + vertex_shader_path.push(PathBuf::from("resources/shaders/")); + vertex_shader_path.push(PathBuf::from(filename.clone() + ".vertex")); + + let mut fragment_shader_path = project_root.clone(); + fragment_shader_path.push(PathBuf::from("resources/shaders/")); + fragment_shader_path.push(PathBuf::from(filename.clone() + ".fragment")); + + (vertex_shader_path, fragment_shader_path) + } + fn new(filename: String, shaders: HashSet) -> Box; + fn get_name() -> String; + fn get_handle() -> Arc; + fn get_pipeline() -> Arc; + fn recompile(); +} + +/// Legacy ShaderType enum for single type shaders. +#[derive(PartialEq, Eq, Hash, Clone)] +pub enum ShaderType { + VERTEX = 0, + FRAGMENT = 1, + GEOMETRY = 2, + TESSELATION = 3, +} /// Typed wrapper for a u32 shader handle (index id) @@ -31,9 +76,8 @@ pub struct CanvasShader { pub(crate) name: String, } -impl CanvasShader { - /// Takes the filename of a .vertex .fragment shader combo in resources/shaders/ - /// Returns pathbuffer of that vertex and fragment shader +impl CompiledGraphicsPipeline for CanvasShader { + fn get_path(filename: String) -> (PathBuf, PathBuf) { let project_root = std::env::current_dir() @@ -53,6 +97,30 @@ impl CanvasShader { (vertex_shader_path, fragment_shader_path) } + fn new(filename: String, shaders: HashSet) -> Box { + + } + + fn get_name() -> String { + unimplemented!() + } + + fn get_handle() -> Arc<_> { + unimplemented!() + } + + fn get_pipeline() -> Arc { + unimplemented!() + } + + fn recompile() { + unimplemented!() + } +} + +impl CanvasShader { + + /// Clone and returns the compiled graphics pipeline pub fn get_pipeline(&self) -> Arc { self.graphics_pipeline.clone().unwrap() @@ -119,16 +187,25 @@ impl CanvasShader { depth_bounds_test: DepthBounds::Disabled, stencil_front: Stencil { compare: Compare::Equal, - pass_op: StencilOp::Invert, - fail_op: StencilOp::Invert, + pass_op: StencilOp::IncrementAndWrap, + fail_op: StencilOp::DecrementAndClamp, depth_fail_op: StencilOp::Keep, - compare_mask: Some(0x01), - write_mask: Some(0xFF), - reference: Some(0x01), + compare_mask: None, + write_mask: None, + reference: None, + }, + stencil_back: Stencil { + compare: Compare::Equal, + pass_op: StencilOp::Invert, + fail_op: StencilOp::Zero, + depth_fail_op: StencilOp::Zero, + compare_mask: None, + write_mask: None, + reference: None, }, - stencil_back: Default::default() }; + CanvasShader { graphics_pipeline: Some(Arc::new(GraphicsPipeline::start() diff --git a/src/main.rs b/src/main.rs index 042cd3ef..a5782319 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,6 +25,7 @@ use crate::compu_buffer::CompuBuffers; use crate::util::load_raw; use crate::canvas_frame::CanvasFrame; use crate::sprite::Poly; +use vulkano::instance::debug::DebugCallback; pub mod util; @@ -58,6 +59,10 @@ pub fn main() { Instance::new(None, &extensions, None).unwrap() }; + let _callback = DebugCallback::errors_and_warnings(&instance, |msg| { + println!("Debug callback: {:?}", msg.description); + }).ok(); + let mut events_loop = EventsLoop::new(); let mut surface = WindowBuilder::new() .with_dimensions(LogicalSize::from((800, 800))) @@ -106,7 +111,6 @@ pub fn main() { let test_polygon = Poly::new_with_color((-0.5, -0.5), (0.5, 0.5), 1, (1.0,0.0,0.0,0.0)); - drop(q2); drop(q1); @@ -186,6 +190,8 @@ pub fn main() { drop(l); + + hprof::end_frame(); hprof::profiler().print_timing(); } diff --git a/src/sprite.rs b/src/sprite.rs index d3be69c5..908b7ffa 100644 --- a/src/sprite.rs +++ b/src/sprite.rs @@ -153,32 +153,65 @@ impl Poly { Poly { vertices: vec![ - (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 + (-0.5 , -0.5 , normalized_depth), + (-1.0 , 1.0 , normalized_depth), + (-0.25 , 0.0 , normalized_depth), - (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 + (-0.25 , 0.0 , normalized_depth), + (-1.0 , 1.0 , normalized_depth), + (0.0 , 0.5 , normalized_depth), + + (0.25 , 0.0 , normalized_depth), + (-1.0 , 1.0 , normalized_depth), + (0.0 , 0.5 , normalized_depth), + + (0.5 , -0.5 , normalized_depth), + (-1.0 , 1.0 , normalized_depth), + (0.25 , 0.0 , normalized_depth), + + (0.25 , -0.5 , normalized_depth), + (-1.0 , 1.0 , normalized_depth), + (0.5 , -0.5 , normalized_depth), + + (0.25 , -0.5 , normalized_depth), + (-1.0 , 1.0 , normalized_depth), + (0.0 , -0.1 , normalized_depth), + + (-0.25 , -0.5 , normalized_depth), + (-1.0 , 1.0 , normalized_depth), + (0.0 , -0.1 , normalized_depth), - (position.0 - 0.1, position.1 , normalized_depth), // top left - (position.0 - 0.1 + size.0, position.1 + size.1, normalized_depth), // bottom right - (position.0 - 0.1 + size.0, position.1 , normalized_depth), // top right + (-0.5 , -0.5 , normalized_depth), + (-1.0 , 1.0 , normalized_depth), + (-0.25 , -0.5 , normalized_depth), ], position: position, ti_position: vec![ - (-0.0, -0.0), // top left - (-0.0, 1.0), // bottom left - ( 1.0, 1.0), // bottom right - - (-0.0, -0.0), // top left - ( 1.0, 1.0), // bottom right - ( 1.0, -0.0), // top right - - (-0.0, -0.0), // top left - ( 1.0, 1.0), // bottom right - ( 1.0, -0.0), // top right + (0.0,0.0), + (0.0,0.0), + (0.0,0.0), + (0.0,0.0), + (0.0,0.0), + (0.0,0.0), + (0.0,0.0), + (0.0,0.0), + (0.0,0.0), + (0.0,0.0), + (0.0,0.0), + (0.0,0.0), + (0.0,0.0), + (0.0,0.0), + (0.0,0.0), + (0.0,0.0), + (0.0,0.0), + (0.0,0.0), + (0.0,0.0), + (0.0,0.0), + (0.0,0.0), + (0.0,0.0), + (0.0,0.0), + (0.0,0.0), ], size: size, color: color, diff --git a/src/vkprocessor.rs b/src/vkprocessor.rs index b6867610..4994c1ec 100644 --- a/src/vkprocessor.rs +++ b/src/vkprocessor.rs @@ -17,6 +17,7 @@ use crate::compu_kernel::{CompuKernel, CompuKernelHandle}; use crate::compu_buffer::{CompuBuffers, CompuBufferHandle}; use std::time::Duration; use crate::canvas_shader::CanvasShaderHandle; +use vulkano::pipeline::depth_stencil::{DynamicStencilValue, StencilFaceFlags}; /// VKProcessor holds the vulkan instance information, the swapchain, and the compute and canvas states /// @@ -27,7 +28,6 @@ pub struct VkProcessor<'a> { pub device: Arc, pub queues: QueuesIter, pub queue: Arc, - pub dynamic_state: DynamicState, pub swapchain: Option>>, pub swapchain_images: Option>>>, @@ -72,7 +72,6 @@ impl<'a> VkProcessor<'a> { device: device.clone(), queue: queue.clone(), queues: queues, - dynamic_state: DynamicState { line_width: None, viewports: None, scissors: None }, swapchain: None, swapchain_images: None, swapchain_recreate_needed: false,