diff --git a/src/canvas/canvas_state.rs b/src/canvas/canvas_state.rs index df687fe7..1af6d9d3 100644 --- a/src/canvas/canvas_state.rs +++ b/src/canvas/canvas_state.rs @@ -256,7 +256,6 @@ impl CanvasState { /// Takes physical and capabilities as we don't store that in Canvas pub fn load_shader(&mut self, filename: String, - physical: PhysicalDevice, capabilities: Capabilities) -> Option> where T: CompiledShader, V: Vertex { let handle = Arc::new(CompiledShaderHandle { diff --git a/src/main.rs b/src/main.rs index 779ddc0f..d60c21fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,39 +4,38 @@ extern crate cgmath; +extern crate hprof; extern crate image; extern crate nalgebra as na; extern crate rand; extern crate time; -extern crate hprof; -use vulkano::sync; -use crate::util::timer::Timer; +use std::sync::Arc; + +use vulkano::instance::debug::DebugCallback; use vulkano::instance::Instance; +use vulkano::sync; use vulkano::sync::GpuFuture; use vulkano_win::VkSurfaceBuild; +use winit::dpi::LogicalSize; +use winit::event::{DeviceEvent, ElementState, Event, VirtualKeyCode, WindowEvent}; +use winit::event_loop::{EventLoop, ControlFlow}; +use winit::platform::unix::WindowBuilderExtUnix; +use winit::window::WindowBuilder; -use crate::util::load_raw; - -use vulkano::instance::debug::DebugCallback; -use crate::compute::compu_frame::CompuFrame; use crate::canvas::canvas_frame::{CanvasFrame, Drawable}; -use std::sync::Arc; -use crate::canvas::managed::handles::{CanvasTextureHandle, Handle, CanvasFontHandle}; -use crate::util::vertex::{VertexTypes, TextureVertex3D}; +use crate::canvas::managed::handles::{CanvasFontHandle, CanvasTextureHandle, Handle}; +use crate::compute::compu_frame::CompuFrame; use crate::compute::managed::handles::{CompuBufferHandle, CompuKernelHandle}; -use crate::drawables::sprite::Sprite; -use crate::drawables::rect::Rect; use crate::drawables::compu_sprite::CompuSprite; +use crate::drawables::rect::Rect; +use crate::drawables::sprite::Sprite; use crate::drawables::text::Text; -use winit::window::{WindowBuilder}; -use winit::dpi::LogicalSize; -use winit::event_loop::EventLoop; -use winit::event::{Event, WindowEvent, DeviceEvent, VirtualKeyCode, ElementState}; -use winit::platform::unix::WindowBuilderExtUnix; +use crate::util::load_raw; +use crate::util::timer::Timer; +use crate::util::vertex::{TextureVertex3D, VertexTypes}; use crate::vkprocessor::VkProcessor; - pub mod util; pub mod vkprocessor; pub mod drawables; @@ -60,15 +59,14 @@ pub fn main() { let mut events_loop = EventLoop::new(); let mut surface = WindowBuilder::new() - .with_inner_size(LogicalSize::new(800, 800)); - // Some weird namespacing issue here - let mut surface = VkSurfaceBuild::build_vk_surface(surface.clone(), &events_loop, instance.clone()).unwrap(); + .with_inner_size(LogicalSize::new(800, 800)) + .build_vk_surface(&events_loop, instance.clone()).unwrap(); - let mut processor = VkProcessor::new(&instance, surface.clone()); + let mut processor = VkProcessor::new(instance.clone(), surface.clone()); { let g = hprof::enter("vulkan preload"); - processor.create_swapchain(surface.clone()); + processor.create_swapchain(instance.clone(), surface.clone()); processor.preload_kernels(); processor.preload_shaders(); @@ -129,77 +127,72 @@ pub fn main() { let mut count = 0; - while let true = processor.is_open() { - - // Take care of our timing - { - elapsed_time = timer.elap_time(); - delta_time = elapsed_time - current_time; - current_time = elapsed_time; - if delta_time > 0.02 { - delta_time = 0.02; + // while let true = processor.is_open() { + // + // // Take care of our timing + // { + // elapsed_time = timer.elap_time(); + // delta_time = elapsed_time - current_time; + // current_time = elapsed_time; + // if delta_time > 0.02 { + // delta_time = 0.02; + // } + // accumulator_time += delta_time; + // } + // + // while (accumulator_time - step_size) >= step_size { + // accumulator_time -= step_size; + // } + + + // Events loop is borrowed from the surface + events_loop.run(move |event, _, control_flow| { + match event { + Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => + { + *control_flow = ControlFlow::Exit + } + Event::WindowEvent { event: WindowEvent::Resized(_), .. } => { + processor.swapchain_recreate_needed = true; } - accumulator_time += delta_time; - } + Event::MainEventsCleared => { + let mut canvas_frame = CanvasFrame::default(); + canvas_frame.draw(&funky_sprite); + canvas_frame.draw(&text_sprite); +// canvas_frame.draw(&rect); - while (accumulator_time - step_size) >= step_size { - accumulator_time -= step_size; - } + let mut compu_frame = CompuFrame::new(); + //compu_frame.add(compute_buffer.clone(), compute_kernel.clone()); + compu_frame.add_with_image_swap(compute_buffer.clone(), compute_kernel.clone(), &compu_sprite1); - // Events loop is borrowed from the surface - events_loop.run(move |event, _, control_flow| { - match event { - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => - { - exit = true; - } - Event::WindowEvent { event: WindowEvent::Resized(_), .. } => { - processor.swapchain_recreate_needed = true; + { + let g = hprof::enter("Run"); + processor.run(&surface.clone(), + canvas_frame, + compu_frame); } - Event::DeviceEvent { event: DeviceEvent::Key(keyboard_input), .. } => { - match keyboard_input.virtual_keycode.unwrap() { - VirtualKeyCode::A => { - if keyboard_input.state == ElementState::Pressed { - // processor.save_edges_image(); - } + } + Event::DeviceEvent { event: DeviceEvent::Key(keyboard_input), .. } => { + match keyboard_input.virtual_keycode.unwrap() { + VirtualKeyCode::A => { + if keyboard_input.state == ElementState::Pressed { + // processor.save_edges_image(); } - _ => () } + _ => () } + } // Event::DeviceEvent { event: DeviceEvent::Button(mouse_input), .. } => { // mouse_xy.x // }, - _ => () - } - }); - - if exit { - break; - } - - let mut canvas_frame = CanvasFrame::default(); - canvas_frame.draw(&funky_sprite); - canvas_frame.draw(&text_sprite); -// canvas_frame.draw(&rect); - - let mut compu_frame = CompuFrame::new(); - //compu_frame.add(compute_buffer.clone(), compute_kernel.clone()); - compu_frame.add_with_image_swap(compute_buffer.clone(), compute_kernel.clone(), &compu_sprite1); - - - { - let g = hprof::enter("Run"); - processor.run(&surface.clone(), - canvas_frame, - compu_frame); + _ => () } - } + }); drop(l); hprof::end_frame(); hprof::profiler().print_timing(); - } diff --git a/src/vkprocessor.rs b/src/vkprocessor.rs index 1995cfe7..14668122 100644 --- a/src/vkprocessor.rs +++ b/src/vkprocessor.rs @@ -30,9 +30,9 @@ use winit::event_loop::EventLoop; /// VKProcessor holds the vulkan instance information, the swapchain, /// and the compute and canvas states -pub struct VkProcessor<'a> { +pub struct VkProcessor { // Vulkan state fields - pub physical: PhysicalDevice<'a>, + //pub physical: PhysicalDevice<'a>, pub device: Arc, pub queues: QueuesIter, pub queue: Arc, @@ -52,12 +52,12 @@ pub struct VkProcessor<'a> { } -impl<'a> VkProcessor<'a> { +impl VkProcessor { /// Creates a new VkProcessor from an instance and surface /// This includes the physical device, queues, compute and canvas state - pub fn new(instance: &'a Arc, surface: Arc>) -> VkProcessor<'a> { + pub fn new(instance: Arc, surface: Arc>) -> VkProcessor { - let physical = PhysicalDevice::enumerate(instance).next().unwrap(); + let physical = PhysicalDevice::enumerate(&instance).next().unwrap(); let queue_family = physical.queue_families().find(|&q| { // We take the first queue that supports drawing to our window. @@ -78,7 +78,7 @@ impl<'a> VkProcessor<'a> { VkProcessor { - physical: physical.clone(), + //physical: physical.clone(), device: device.clone(), queue: queue.clone(), queues: queues, @@ -98,13 +98,15 @@ impl<'a> VkProcessor<'a> { } /// Using the surface, we calculate the surface capabilities and create the swapchain and swapchain images - pub fn create_swapchain(&mut self, surface: Arc>) { + pub fn create_swapchain(&mut self, instance: Arc, surface: Arc>) { let (mut swapchain, images) = { - let capabilities = surface.capabilities(self.physical).unwrap(); + let physical = PhysicalDevice::enumerate(&instance).next().unwrap(); + 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; + let colorspace = capabilities.supported_formats[0].1; // Set the swapchains window dimensions let initial_dimensions = if let dimensions = surface.window().inner_size() { @@ -122,13 +124,13 @@ impl<'a> VkProcessor<'a> { format, initial_dimensions, 1, // Layers - usage, + ImageUsage::color_attachment(), &self.queue, SurfaceTransform::Identity, alpha, PresentMode::Immediate, FullscreenExclusive::Default, true, - ColorSpace::PassThrough).unwrap() + colorspace).unwrap() }; self.swapchain = Some(swapchain); @@ -136,7 +138,7 @@ impl<'a> VkProcessor<'a> { } /// On screen resizes, the swapchain and images must be recreated - pub fn recreate_swapchain(&mut self, surface: &'a Arc>) { + pub fn recreate_swapchain(&mut self, surface: &Arc>) { let dimensions = if let dimensions = surface.window().inner_size() { let dimensions: (u32, u32) = dimensions.to_logical::(surface.window().scale_factor()).into(); [dimensions.0, dimensions.1] @@ -174,10 +176,10 @@ impl<'a> VkProcessor<'a> { /// A hardcoded list of shaders which can be preloaded from this function pub fn preload_shaders(&mut self) { - self.canvas_state.load_shader::(String::from("color-passthrough"), self.physical.clone(), self.capabilities.clone()); - self.canvas_state.load_shader::(String::from("simple_texture"), self.physical.clone(), self.capabilities.clone()); - self.canvas_state.load_shader::(String::from("simple_image"), self.physical.clone(), self.capabilities.clone()); - self.canvas_state.load_shader::(String::from("simple_text"), self.physical.clone(), self.capabilities.clone()); + self.canvas_state.load_shader::(String::from("color-passthrough"), self.capabilities.clone()); + self.canvas_state.load_shader::(String::from("simple_texture"), self.capabilities.clone()); + self.canvas_state.load_shader::(String::from("simple_image"), self.capabilities.clone()); + self.canvas_state.load_shader::(String::from("simple_text"), self.capabilities.clone()); } /// A hardcoded list of shaders which can be proloaded from this function @@ -230,7 +232,7 @@ impl<'a> VkProcessor<'a> { /// Run the VKprocessor for a single frame, consuming the Canvas/Compu Frames pub fn run(&mut self, - surface: &'a Arc>, + surface: &Arc>, canvas_frame: CanvasFrame, compute_frame: CompuFrame, ) {