From bb1f78216807ee4539e5b0007177043383107408 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Mon, 5 Aug 2019 22:09:21 -0700 Subject: [PATCH] pulled swapchain out of shaderkernels --- resources/shaders/color-passthrough.fragment | 11 +- resources/shaders/color-passthrough.vertex | 2 - src/canvas.rs | 45 ++++++++ src/main.rs | 3 +- src/vkprocessor.rs | 80 +++++++++++-- src/vkprocessor/shader_kernels.rs | 111 ++++++++++--------- 6 files changed, 172 insertions(+), 80 deletions(-) create mode 100644 src/canvas.rs diff --git a/resources/shaders/color-passthrough.fragment b/resources/shaders/color-passthrough.fragment index 69fef39f..cd2ee6df 100644 --- a/resources/shaders/color-passthrough.fragment +++ b/resources/shaders/color-passthrough.fragment @@ -8,14 +8,5 @@ layout(set = 0, binding = 0) uniform sampler2D tex; layout(set = 0, binding = 1, rgba32ui) readonly uniform uimage2D img; void main() { - - ivec2 pos = ivec2(gl_FragCoord.x, gl_FragCoord.y); - - f_color = imageLoad(img, ivec2(pos)) / (255.0); - - float gamma = 0.5; - f_color.rgb = pow(f_color.rgb, vec3(1.0/gamma)); - - //f_color = out_color; - // f_color = texture(tex, tex_coords); + f_color = out_color; } \ No newline at end of file diff --git a/resources/shaders/color-passthrough.vertex b/resources/shaders/color-passthrough.vertex index b4dbeee7..9be947e5 100644 --- a/resources/shaders/color-passthrough.vertex +++ b/resources/shaders/color-passthrough.vertex @@ -7,7 +7,5 @@ layout(location = 1) out vec4 out_color; void main() { out_color = color; - gl_Position = vec4(position, 0.0, 1.0); - tex_coords = position; } diff --git a/src/canvas.rs b/src/canvas.rs new file mode 100644 index 00000000..8c2266d7 --- /dev/null +++ b/src/canvas.rs @@ -0,0 +1,45 @@ + + +// Canvas is the accumulator of Sprites for drawing + +// Needs to know: +// textured? +// colored? +// vertices + +/* + +If it is textured. It needs to be rendered with the texture shader which requires a separate + graphics pipeline. Might as well have a new render pass as well. + +Need to pull recreate swapchain out of shader_kernels.rs + +I need a second version of shaderkernels + + + +*/ + + +trait Drawable { + fn draw() { + + } +} + +pub struct Canvas { + + +} + +impl Canvas { + pub fn new() -> Canvas { + + Canvas { + + } + + } + + pub fn draw() -> +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 821cedb9..663f2d65 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,7 +42,6 @@ use vulkano::descriptor::pipeline_layout::PipelineLayoutAbstract; use vulkano::sync::GpuFuture; use shaderc::CompileOptions; use shade_runner::CompileError; -use crate::workpiece::{WorkpieceLoader, Workpiece}; use winit::{EventsLoop, WindowBuilder, WindowEvent, Event, DeviceEvent, VirtualKeyCode, ElementState}; use winit::dpi::LogicalSize; use vulkano_win::VkSurfaceBuild; @@ -54,7 +53,6 @@ mod input; mod vkprocessor; mod util; mod button; -mod workpiece; mod vertex_2d; mod vertex_3d; mod sprite; @@ -76,6 +74,7 @@ fn main() { processor.compile_kernel(String::from("simple-edge.compute")); processor.compile_shaders(String::from("simple_texture"), &surface); processor.load_buffers(String::from("background.jpg")); + processor.create_swapchain(&surface); let mut timer = Timer::new(); let mut frame_future = Box::new(sync::now(processor.device.clone())) as Box; diff --git a/src/vkprocessor.rs b/src/vkprocessor.rs index 19043c8a..586122bc 100644 --- a/src/vkprocessor.rs +++ b/src/vkprocessor.rs @@ -125,10 +125,8 @@ pub struct VkProcessor<'a> { pub image_buffer: Vec, pub compute_image_buffers: Vec>>, pub settings_buffer: Option>>, -// pub swapchain: Option>>, -// pub images: Option>>>, + pub xy: (u32, u32), - pub render_pass: Option>, pub vertex_buffer: Option>, pub vertex_buffer2: Option>, pub dynamic_state: DynamicState, @@ -139,8 +137,9 @@ pub struct VkProcessor<'a> { pub image_buffer_store : Vec>, pub compute_image: Option, + pub swapchain: Option>>, + pub swapchain_images: Option>>>, - // pub image_buffers_obj : ImageBuffers, } @@ -183,7 +182,6 @@ impl<'a> VkProcessor<'a> { compute_image_buffers: Vec::new(), settings_buffer: Option::None, xy: (0, 0), - render_pass: Option::None, vertex_buffer: Option::None, vertex_buffer2: None, @@ -193,7 +191,10 @@ impl<'a> VkProcessor<'a> { textures: vec![], image_buffer_store: vec![], - compute_image: None + compute_image: None, + + swapchain: None, + swapchain_images: None } } @@ -212,9 +213,64 @@ impl<'a> VkProcessor<'a> { ); } + pub fn create_swapchain(&mut self, surface: &'a Arc>) { + + let (mut swapchain, images) = { + let capabilities = surface.capabilities(self.physical).unwrap(); + let usage = capabilities.supported_usage_flags; + let alpha = capabilities.supported_composite_alpha.iter().next().unwrap(); + // Choosing the internal format that the images will have. + let format = capabilities.supported_formats[0].0; + + // Set the swapchains window dimensions + let initial_dimensions = if let Some(dimensions) = surface.window().get_inner_size() { + // convert to physical pixels + let dimensions: (u32, u32) = dimensions.to_physical(surface.window().get_hidpi_factor()).into(); + [dimensions.0, dimensions.1] + } else { + // The window no longer exists so exit the application. + panic!("window closed"); + }; + + Swapchain::new(self.device.clone(), + surface.clone(), + capabilities.min_image_count, + format, + initial_dimensions, + 1, // Layers + usage, + &self.queue, + SurfaceTransform::Identity, + alpha, + PresentMode::Fifo, true, None).unwrap() + }; + + self.swapchain = Some(swapchain); + self.swapchain_images = Some(images); + + } + // On resizes we have to recreate the swapchain pub fn recreate_swapchain(&mut self, surface: &'a Arc>) { - self.shader_kernels = Some(self.shader_kernels.take().unwrap().recreate_swapchain(surface)); + + let dimensions = if let Some(dimensions) = surface.window().get_inner_size() { + let dimensions: (u32, u32) = dimensions.to_physical(surface.window().get_hidpi_factor()).into(); + [dimensions.0, dimensions.1] + } else { + return; + }; + + let (new_swapchain, new_images) = match self.swapchain.clone().unwrap().clone().recreate_with_dimension(dimensions) { + Ok(r) => r, + // This error tends to happen when the user is manually resizing the window. + // Simply restarting the loop is the easiest way to fix this issue. + Err(SwapchainCreationError::UnsupportedDimensions) => panic!("Uh oh"), + Err(err) => panic!("{:?}", err) + }; + + self.swapchain = Some(new_swapchain); + self.swapchain_images = Some(new_images); + } fn get_texture_from_file(image_filename: String, queue: Arc) -> Arc> { @@ -376,7 +432,7 @@ impl<'a> VkProcessor<'a> { pub fn run(&mut self, surface: &'a Arc>, mut frame_future: Box) -> Box { - let mut framebuffers = window_size_dependent_setup(&self.shader_kernels.clone().unwrap().swapchain_images.clone(), + let mut framebuffers = window_size_dependent_setup(&self.swapchain_images.clone().unwrap().clone(), self.shader_kernels.clone().unwrap().render_pass.clone(), &mut self.dynamic_state); @@ -388,8 +444,8 @@ impl<'a> VkProcessor<'a> { // Whenever the window resizes we need to recreate everything dependent on the window size. // In this example that includes the swapchain, the framebuffers and the dynamic state viewport. if recreate_swapchain { - self.shader_kernels = Some(self.shader_kernels.clone().unwrap().recreate_swapchain(surface)); - framebuffers = window_size_dependent_setup(&self.shader_kernels.clone().unwrap().swapchain_images.clone(), + self.recreate_swapchain(surface); + framebuffers = window_size_dependent_setup(&self.swapchain_images.clone().unwrap().clone(), self.shader_kernels.clone().unwrap().render_pass.clone(), //self.render_pass.clone().unwrap().clone(), &mut self.dynamic_state); @@ -398,7 +454,7 @@ impl<'a> VkProcessor<'a> { // This function can block if no image is available. The parameter is an optional timeout // after which the function call will return an error. - let (image_num, acquire_future) = match vulkano::swapchain::acquire_next_image(self.shader_kernels.clone().unwrap().swapchain.clone(), None) { + let (image_num, acquire_future) = match vulkano::swapchain::acquire_next_image(self.swapchain.clone().unwrap().clone(), None) { Ok(r) => r, Err(AcquireError::OutOfDate) => { recreate_swapchain = true; @@ -450,7 +506,7 @@ impl<'a> VkProcessor<'a> { // Wait on the previous frame, then execute the command buffer and present the image let future = frame_future.join(acquire_future) .then_execute(self.queue.clone(), command_buffer).unwrap() - .then_swapchain_present(self.queue.clone(), self.shader_kernels.clone().unwrap().swapchain.clone(), image_num) + .then_swapchain_present(self.queue.clone(), self.swapchain.clone().unwrap().clone(), image_num) .then_signal_fence_and_flush(); match future { diff --git a/src/vkprocessor/shader_kernels.rs b/src/vkprocessor/shader_kernels.rs index 4bfe81e4..c998c112 100644 --- a/src/vkprocessor/shader_kernels.rs +++ b/src/vkprocessor/shader_kernels.rs @@ -49,8 +49,8 @@ struct EntryPoint<'a> { #[derive(Clone)] pub struct ShaderKernels { - pub swapchain : Arc>, - pub swapchain_images: Vec>>, // Surface which is drawn to +// pub swapchain : Arc>, +// pub swapchain_images: Vec>>, // Surface which is drawn to pub render_pass: Arc, pub graphics_pipeline: Option>, @@ -93,26 +93,26 @@ impl ShaderKernels { } // On resizes we have to recreate the swapchain - pub fn recreate_swapchain(mut self, surface: &Arc>) -> Self { - let dimensions = if let Some(dimensions) = surface.window().get_inner_size() { - let dimensions: (u32, u32) = dimensions.to_physical(surface.window().get_hidpi_factor()).into(); - [dimensions.0, dimensions.1] - } else { - return self; - }; - - let (new_swapchain, new_images) = match self.swapchain.clone().recreate_with_dimension(dimensions) { - Ok(r) => r, - // This error tends to happen when the user is manually resizing the window. - // Simply restarting the loop is the easiest way to fix this issue. - Err(SwapchainCreationError::UnsupportedDimensions) => panic!("Uh oh"), - Err(err) => panic!("{:?}", err) - }; - - self.swapchain = new_swapchain; - self.swapchain_images = new_images; - self - } +// pub fn recreate_swapchain(mut self, surface: &Arc>) -> Self { +// let dimensions = if let Some(dimensions) = surface.window().get_inner_size() { +// let dimensions: (u32, u32) = dimensions.to_physical(surface.window().get_hidpi_factor()).into(); +// [dimensions.0, dimensions.1] +// } else { +// return self; +// }; +// +// let (new_swapchain, new_images) = match self.swapchain.clone().recreate_with_dimension(dimensions) { +// Ok(r) => r, +// // This error tends to happen when the user is manually resizing the window. +// // Simply restarting the loop is the easiest way to fix this issue. +// Err(SwapchainCreationError::UnsupportedDimensions) => panic!("Uh oh"), +// Err(err) => panic!("{:?}", err) +// }; +// +// self.swapchain = new_swapchain; +// self.swapchain_images = new_images; +// self +// } pub fn new(filename: String, surface: &Arc>, @@ -120,35 +120,38 @@ impl ShaderKernels { physical: PhysicalDevice, device: Arc) -> ShaderKernels { - let (mut swapchain, images) = { - let capabilities = surface.capabilities(physical).unwrap(); - let usage = capabilities.supported_usage_flags; - let alpha = capabilities.supported_composite_alpha.iter().next().unwrap(); - // Choosing the internal format that the images will have. - let format = capabilities.supported_formats[0].0; - - // Set the swapchains window dimensions - let initial_dimensions = if let Some(dimensions) = surface.window().get_inner_size() { - // convert to physical pixels - let dimensions: (u32, u32) = dimensions.to_physical(surface.window().get_hidpi_factor()).into(); - [dimensions.0, dimensions.1] - } else { - // The window no longer exists so exit the application. - panic!("window closed"); - }; - - Swapchain::new(device.clone(), - surface.clone(), - capabilities.min_image_count, - format, - initial_dimensions, - 1, // Layers - usage, - &queue, - SurfaceTransform::Identity, - alpha, - PresentMode::Fifo, true, None).unwrap() - }; +// let (mut swapchain, images) = { +// let capabilities = surface.capabilities(physical).unwrap(); +// let usage = capabilities.supported_usage_flags; +// let alpha = capabilities.supported_composite_alpha.iter().next().unwrap(); +// // Choosing the internal format that the images will have. +// let format = capabilities.supported_formats[0].0; +// +// // Set the swapchains window dimensions +// let initial_dimensions = if let Some(dimensions) = surface.window().get_inner_size() { +// // convert to physical pixels +// let dimensions: (u32, u32) = dimensions.to_physical(surface.window().get_hidpi_factor()).into(); +// [dimensions.0, dimensions.1] +// } else { +// // The window no longer exists so exit the application. +// panic!("window closed"); +// }; +// +// Swapchain::new(device.clone(), +// surface.clone(), +// capabilities.min_image_count, +// format, +// initial_dimensions, +// 1, // Layers +// usage, +// &queue, +// SurfaceTransform::Identity, +// alpha, +// PresentMode::Fifo, true, None).unwrap() +// }; + + let capabilities = surface.capabilities(physical).unwrap(); + let format = capabilities.supported_formats[0].0; let filenames = ShaderKernels::get_path(filename.clone()); @@ -208,7 +211,7 @@ impl ShaderKernels { // be one of the types of the `vulkano::format` module (or alternatively one // of your structs that implements the `FormatDesc` trait). Here we use the // same format as the swapchain. - format: swapchain.clone().format(), + format: format, // TODO: samples: 1, } @@ -224,8 +227,8 @@ impl ShaderKernels { ShaderKernels { - swapchain: swapchain, - swapchain_images: images, +// swapchain: swapchain, +// swapchain_images: images, //physical: physical, //options: CompileOptions::new().ok_or(CompileError::CreateCompiler).unwrap(),