use vulkano::buffer::{BufferUsage, CpuAccessibleBuffer, DeviceLocalBuffer, ImmutableBuffer, BufferAccess}; use vulkano::command_buffer::{AutoCommandBufferBuilder, DynamicState}; use vulkano::descriptor::descriptor_set::{PersistentDescriptorSet, StdDescriptorPoolAlloc}; use vulkano::device::{Device, DeviceExtensions, QueuesIter, Queue}; use vulkano::instance::{Instance, InstanceExtensions, PhysicalDevice, QueueFamily}; use vulkano::pipeline::{ComputePipeline, GraphicsPipeline, GraphicsPipelineAbstract}; use vulkano::sync::{GpuFuture, FlushError}; use vulkano::sync; use std::time::SystemTime; use std::sync::Arc; use std::ffi::CStr; use std::path::PathBuf; use shade_runner as sr; use image::{DynamicImage, ImageBuffer}; use image::GenericImageView; use vulkano::descriptor::pipeline_layout::PipelineLayout; use image::GenericImage; use shade_runner::{ComputeLayout, CompileError, FragLayout, FragInput, FragOutput, VertInput, VertOutput, VertLayout}; use vulkano::descriptor::descriptor_set::PersistentDescriptorSetBuf; use shaderc::CompileOptions; use vulkano::framebuffer::{Subpass, RenderPass, RenderPassAbstract, Framebuffer, FramebufferAbstract}; use vulkano::pipeline::shader::{GraphicsShaderType, ShaderModule, GraphicsEntryPoint, SpecializationConstants, SpecializationMapEntry}; use vulkano::swapchain::{Swapchain, PresentMode, SurfaceTransform, Surface, SwapchainCreationError, AcquireError}; use vulkano::swapchain::acquire_next_image; use vulkano::image::swapchain::SwapchainImage; use winit::{EventsLoop, WindowBuilder, Window, Event, WindowEvent}; use vulkano_win::VkSurfaceBuild; use vulkano::pipeline::vertex::{SingleBufferDefinition, Vertex}; use vulkano::descriptor::PipelineLayoutAbstract; use std::alloc::Layout; use vulkano::pipeline::viewport::Viewport; #[derive(Default, Debug, Clone)] struct tVertex { position: [f32; 2] } /// This method is called once during initialization, then again whenever the window is resized fn window_size_dependent_setup( images: &[Arc>], render_pass: Arc, dynamic_state: &mut DynamicState, ) -> Vec> { let dimensions = images[0].dimensions(); let viewport = Viewport { origin: [0.0, 0.0], dimensions: [dimensions[0] as f32, dimensions[1] as f32], depth_range: 0.0..1.0, }; dynamic_state.viewports = Some(vec!(viewport)); images.iter().map(|image| { Arc::new( Framebuffer::start(render_pass.clone()) .add(image.clone()).unwrap() .build().unwrap() ) as Arc }).collect::>() } #[repr(C)] struct MySpecConstants { my_integer_constant: i32, a_boolean: u32, floating_point: f32, } unsafe impl SpecializationConstants for MySpecConstants { fn descriptors() -> &'static [SpecializationMapEntry] { static DESCRIPTORS: [SpecializationMapEntry; 3] = [ SpecializationMapEntry { constant_id: 0, offset: 0, size: 4, }, SpecializationMapEntry { constant_id: 1, offset: 4, size: 4, }, SpecializationMapEntry { constant_id: 2, offset: 8, size: 4, }, ]; &DESCRIPTORS } } pub struct VkProcessor<'a> { pub instance: Arc, pub physical: PhysicalDevice<'a>, pub pipeline: Option>, pub compute_pipeline: Option>>>, pub device: Arc, pub queues: QueuesIter, pub queue: Arc, pub set: Option>>, ((((), PersistentDescriptorSetBuf>>), PersistentDescriptorSetBuf>>), PersistentDescriptorSetBuf>>)>>>, pub image_buffer: Vec, pub img_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 dynamic_state: DynamicState, } impl<'a> VkProcessor<'a> { pub fn new(instance: &'a Arc, surface: &'a Arc>) -> VkProcessor<'a> { 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. q.supports_graphics() && surface.is_supported(q).unwrap_or(false) && q.supports_compute() }).unwrap(); let device_ext = DeviceExtensions { khr_swapchain: true, ..DeviceExtensions::none() }; let (device, mut queues) = Device::new(physical, physical.supported_features(), &device_ext, [(queue_family, 0.5)].iter().cloned()).unwrap(); let queue = queues.next().unwrap(); VkProcessor { instance: instance.clone(), physical: physical.clone(), pipeline: Option::None, compute_pipeline: Option::None, device: device, queue: queue, queues: queues, set: Option::None, image_buffer: Vec::new(), img_buffers: Vec::new(), settings_buffer: Option::None, swapchain: Option::None, images: Option::None, xy: (0, 0), render_pass: Option::None, vertex_buffer: Option::None, dynamic_state: DynamicState { line_width: None, viewports: None, scissors: None }, } } pub fn compile_kernel(&mut self, filename: String) { let project_root = std::env::current_dir() .expect("failed to get root directory"); let mut compute_path = project_root.clone(); compute_path.push(PathBuf::from("resources/shaders/")); compute_path.push(PathBuf::from(filename)); let mut options = CompileOptions::new().ok_or(CompileError::CreateCompiler).unwrap(); options.add_macro_definition("SETTING_POS_X", Some("0")); options.add_macro_definition("SETTING_POS_Y", Some("1")); options.add_macro_definition("SETTING_BUCKETS_START", Some("2")); options.add_macro_definition("SETTING_BUCKETS_LEN", Some("2")); let shader = sr::load_compute_with_options(compute_path, options) .expect("Failed to compile"); let vulkano_entry = sr::parse_compute(&shader) .expect("failed to parse"); let x = unsafe { vulkano::pipeline::shader::ShaderModule::from_words(self.device.clone(), &shader.compute) }.unwrap(); let compute_pipeline = Arc::new({ unsafe { ComputePipeline::new(self.device.clone(), &x.compute_entry_point( CStr::from_bytes_with_nul_unchecked(b"main\0"), vulkano_entry.compute_layout), &(), ).unwrap() } }); self.compute_pipeline = Some(compute_pipeline); } pub fn compile_shaders(&mut self, filename: String, surface: &'a Arc>) { // Before we can draw on the surface, we have to create what is called a swapchain. Creating // a swapchain allocates the color buffers that will contain the image that will ultimately // be visible on the screen. These images are returned alongside with the swapchain. 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. return; }; 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.images = Some(images); 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 = project_root.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")); let mut options = CompileOptions::new().ok_or(CompileError::CreateCompiler).unwrap(); options.add_macro_definition("SETTING_POS_X", Some("0")); options.add_macro_definition("SETTING_POS_Y", Some("1")); options.add_macro_definition("SETTING_BUCKETS_START", Some("2")); options.add_macro_definition("SETTING_BUCKETS_LEN", Some("2")); let shader = sr::load(vertex_shader_path, fragment_shader_path) .expect("Failed to compile"); let vulkano_entry = sr::parse(&shader) .expect("failed to parse"); let x1: Arc = unsafe { vulkano::pipeline::shader::ShaderModule::from_words(self.device.clone(), &shader.fragment) }.unwrap(); let x2 = unsafe { vulkano::pipeline::shader::ShaderModule::from_words(self.device.clone(), &shader.vertex) }.unwrap(); let frag_entry_point: GraphicsEntryPoint = unsafe { x1.graphics_entry_point(CStr::from_bytes_with_nul_unchecked(b"main\0"), vulkano_entry.frag_input, vulkano_entry.frag_output, vulkano_entry.frag_layout, GraphicsShaderType::Fragment) }; let vert_entry_point: GraphicsEntryPoint = unsafe { x2.graphics_entry_point(CStr::from_bytes_with_nul_unchecked(b"main\0"), vulkano_entry.vert_input, vulkano_entry.vert_output, vulkano_entry.vert_layout, GraphicsShaderType::Vertex) }; // The next step is to create a *render pass*, which is an object that describes where the // output of the graphics pipeline will go. It describes the layout of the images // where the colors, depth and/or stencil information will be written. let render_pass = Arc::new(vulkano::single_pass_renderpass!( self.device.clone(), attachments: { // `color` is a custom name we give to the first and only attachment. color: { // `load: Clear` means that we ask the GPU to clear the content of this // attachment at the start of the drawing. load: Clear, // `store: Store` means that we ask the GPU to store the output of the draw // in the actual image. We could also ask it to discard the result. store: Store, // `format: ` indicates the type of the format of the image. This has to // 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: self.swapchain.clone().unwrap().clone().format(), // TODO: samples: 1, } }, pass: { // We use the attachment named `color` as the one and only color attachment. color: [color], // No depth-stencil attachment is indicated with empty brackets. depth_stencil: {} } ).unwrap()); self.render_pass = Some(render_pass); // Before we draw we have to create what is called a pipeline. This is similar to an OpenGL // program, but much more specific. let pipeline = GraphicsPipeline::start() // We need to indicate the layout of the vertices. // The type `SingleBufferDefinition` actually contains a template parameter corresponding // to the type of each vertex. But in this code it is automatically inferred. .vertex_input_single_buffer::() // A Vulkan shader can in theory contain multiple entry points, so we have to specify // which one. The `main` word of `main_entry_point` actually corresponds to the name of // the entry point. .vertex_shader(vert_entry_point, MySpecConstants { my_integer_constant: 0, a_boolean: 0, floating_point: 0.0, }) // The content of the vertex buffer describes a list of triangles. .triangle_list() // Use a resizable viewport set to draw over the entire window .viewports_dynamic_scissors_irrelevant(1) // See `vertex_shader`. .fragment_shader(frag_entry_point, MySpecConstants { my_integer_constant: 0, a_boolean: 0, floating_point: 0.0, }) // 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(self.render_pass.clone().unwrap().clone(), 0).unwrap()) // Now that our builder is filled, we call `build()` to obtain an actual pipeline. .build(self.device.clone()) .unwrap(); self.pipeline = Option::Some(Arc::new(pipeline)); } // On resizes we have to recreate the swapchain pub fn recreate_swapchain(&mut self, surface: &'a Arc>) { 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().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.images = Some(new_images); } pub fn run_loop(&mut self, surface: &'a Arc>) { let mut framebuffers = window_size_dependent_setup(&self.images.clone().unwrap().clone(), self.render_pass.clone().unwrap().clone(), &mut self.dynamic_state); let mut recreate_swapchain = false; // In the loop below we are going to submit commands to the GPU. Submitting a command produces // an object that implements the `GpuFuture` trait, which holds the resources for as long as // they are in use by the GPU. // // Destroying the `GpuFuture` blocks until the GPU is finished executing it. In order to avoid // that, we store the submission of the previous frame here. let mut previous_frame_end = Box::new(sync::now(self.device.clone())) as Box; // loop { // It is important to call this function from time to time, otherwise resources will keep // accumulating and you will eventually reach an out of memory error. // Calling this function polls various fences in order to determine what the GPU has // already processed, and frees the resources that are no longer needed. // already processed, and frees the resources that are no longer needed. previous_frame_end.cleanup_finished(); // 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.recreate_swapchain(surface); framebuffers = window_size_dependent_setup(&self.images.clone().unwrap().clone(), self.render_pass.clone().unwrap().clone(), &mut self.dynamic_state); recreate_swapchain = false; } // Before we can draw on the output, we have to *acquire* an image from the swapchain. If // no image is available (which happens if you submit draw commands too quickly), then the // function will block. // This operation returns the index of the image that we are allowed to draw upon. // // 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.swapchain.clone().unwrap().clone(), None) { Ok(r) => r, Err(AcquireError::OutOfDate) => { recreate_swapchain = true; //continue; panic!("Weird thing"); } Err(err) => panic!("{:?}", err) }; // Specify the color to clear the framebuffer with i.e. blue let clear_values = vec!([0.0, 0.0, 1.0, 1.0].into()); { // In order to draw, we have to build a *command buffer*. The command buffer object holds // the list of commands that are going to be executed. // // Building a command buffer is an expensive operation (usually a few hundred // microseconds), but it is known to be a hot path in the driver and is expected to be // optimized. // // Note that we have to pass a queue family when we create the command buffer. The command // buffer will only be executable on that given queue family. let mut v = Vec::new(); v.push(self.vertex_buffer.clone().unwrap().clone()); let command_buffer = AutoCommandBufferBuilder::primary_one_time_submit(self.device.clone(), self.queue.family()) .unwrap() .dispatch([self.xy.0, self.xy.1, 1], self.compute_pipeline.clone().unwrap().clone(), self.set.clone().unwrap().clone(), ()).unwrap() // Before we can draw, we have to *enter a render pass*. There are two methods to do // this: `draw_inline` and `draw_secondary`. The latter is a bit more advanced and is // not covered here. // // The third parameter builds the list of values to clear the attachments with. The API // is similar to the list of attachments when building the framebuffers, except that // only the attachments that use `load: Clear` appear in the list. .begin_render_pass(framebuffers[image_num].clone(), false, clear_values) .unwrap() // We are now inside the first subpass of the render pass. We add a draw command. // // The last two parameters contain the list of resources to pass to the shaders. // Since we used an `EmptyPipeline` object, the objects have to be `()`. .draw(self.pipeline.clone().unwrap().clone(), &self.dynamic_state, v, (), ()) .unwrap() // We leave the render pass by calling `draw_end`. Note that if we had multiple // subpasses we could have called `next_inline` (or `next_secondary`) to jump to the // next subpass. .end_render_pass() .unwrap() // Finish building the command buffer by calling `build`. .build().unwrap(); let future = previous_frame_end.join(acquire_future) .then_execute(self.queue.clone(), command_buffer).unwrap() // The color output is now expected to contain our triangle. But in order to show it on // the screen, we have to *present* the image by calling `present`. // // This function does not actually present the image immediately. Instead it submits a // present command at the end of the queue. This means that it will only be presented once // the GPU has finished executing the command buffer that draws the triangle. .then_swapchain_present(self.queue.clone(), self.swapchain.clone().unwrap().clone(), image_num) .then_signal_fence_and_flush(); match future { Ok(future) => { previous_frame_end = Box::new(future) as Box<_>; } Err(FlushError::OutOfDate) => { recreate_swapchain = true; previous_frame_end = Box::new(sync::now(self.device.clone())) as Box<_>; } Err(e) => { println!("{:?}", e); previous_frame_end = Box::new(sync::now(self.device.clone())) as Box<_>; } } } // Handling the window events in order to close the program when the user wants to close // it. let mut done = true; // events_loop.poll_events(|ev| { // match ev { // Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => done = true, // Event::WindowEvent { event: WindowEvent::Resized(_), .. } => recreate_swapchain = true, // _ => () // } // }); if done { return; } //} } pub fn load_buffers(&mut self, image_filename: String) { let project_root = std::env::current_dir() .expect("failed to get root directory"); let mut compute_path = project_root.clone(); compute_path.push(PathBuf::from("resources/images/")); compute_path.push(PathBuf::from(image_filename)); let img = image::open(compute_path).expect("Couldn't find image"); self.xy = img.dimensions(); let data_length = self.xy.0 * self.xy.1 * 4; let pixel_count = img.raw_pixels().len(); println!("Pixel count {}", pixel_count); if pixel_count != data_length as usize { println!("Creating apha channel..."); for i in img.raw_pixels().iter() { if (self.image_buffer.len() + 1) % 4 == 0 { self.image_buffer.push(255); } self.image_buffer.push(*i); } self.image_buffer.push(255); } else { self.image_buffer = img.raw_pixels(); } println!("Buffer length {}", self.image_buffer.len()); println!("Size {:?}", self.xy); println!("Allocating Buffers..."); // Pull out the image data and place it in a buffer for the kernel to write to and for us to read from let write_buffer = { let mut buff = self.image_buffer.iter(); let data_iter = (0..data_length).map(|n| *(buff.next().unwrap())); CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::all(), data_iter).unwrap() }; // Pull out the image data and place it in a buffer for the kernel to read from let read_buffer = { let mut buff = self.image_buffer.iter(); let data_iter = (0..data_length).map(|n| *(buff.next().unwrap())); CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::all(), data_iter).unwrap() }; // A buffer to hold many i32 values to use as settings let settings_buffer = { let vec = vec![self.xy.0, self.xy.1]; let mut buff = vec.iter(); let data_iter = (0..2).map(|n| *(buff.next().unwrap())); CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::all(), data_iter).unwrap() }; println!("Done"); // Create the data descriptor set for our previously created shader pipeline let mut set = PersistentDescriptorSet::start(self.compute_pipeline.clone().unwrap().clone(), 0) .add_buffer(write_buffer.clone()).unwrap() .add_buffer(read_buffer.clone()).unwrap() .add_buffer(settings_buffer.clone()).unwrap(); self.set = Some(Arc::new(set.build().unwrap())); self.img_buffers.push(write_buffer); self.img_buffers.push(read_buffer); self.settings_buffer = Some(settings_buffer); // We now create a buffer that will store the shape of our triangle. let vertex_buffer = { vulkano::impl_vertex!(tVertex, position); CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::all(), [ tVertex { position: [-0.5, -0.25] }, tVertex { position: [0.0, 0.5] }, tVertex { position: [0.25, -0.1] } ].iter().cloned()).unwrap() }; self.vertex_buffer = Some(vertex_buffer); } // pub fn run_kernel(&mut self) { // // println!("Running Kernel..."); // // // The command buffer I think pretty much serves to define what runs where for how many times // let command_buffer = // AutoCommandBufferBuilder::primary_one_time_submit(self.device.clone(),self.queue.family()).unwrap() // .dispatch([self.xy.0, self.xy.1, 1], // self.compute_pipeline.clone().unwrap().clone(), // self.set.clone().unwrap().clone(), ()).unwrap() // .build().unwrap(); // // // Create a future for running the command buffer and then just fence it // let future = sync::now(self.device.clone()) // .then_execute(self.queue.clone(), command_buffer).unwrap() // .then_signal_fence_and_flush().unwrap(); // // // I think this is redundant and returns immediately // future.wait(None).unwrap(); // println!("Done running kernel"); // } // pub fn read_image(&self) -> Vec { // // // The buffer is sync'd so we can just read straight from the handle // let mut data_buffer_content = self.img_buffers.get(0).unwrap().read().unwrap(); // // println!("Reading output"); // // let mut image_buffer = Vec::new(); // // for y in 0..self.xy.1 { // for x in 0..self.xy.0 { // // let r = data_buffer_content[((self.xy.0 * y + x) * 4 + 0) as usize] as u8; // let g = data_buffer_content[((self.xy.0 * y + x) * 4 + 1) as usize] as u8; // let b = data_buffer_content[((self.xy.0 * y + x) * 4 + 2) as usize] as u8; // let a = data_buffer_content[((self.xy.0 * y + x) * 4 + 3) as usize] as u8; // // image_buffer.push(r); // image_buffer.push(g); // image_buffer.push(b); // image_buffer.push(a); // } // } // // image_buffer // } // pub fn save_image(&self) { // println!("Saving output"); // // let img_data = self.read_image(); // // let img = ImageBuffer::from_fn(self.xy.0, self.xy.1, |x, y| { // // let r = img_data[((self.xy.0 * y + x) * 4 + 0) as usize] as u8; // let g = img_data[((self.xy.0 * y + x) * 4 + 1) as usize] as u8; // let b = img_data[((self.xy.0 * y + x) * 4 + 2) as usize] as u8; // let a = img_data[((self.xy.0 * y + x) * 4 + 3) as usize] as u8; // // image::Rgba([r, g, b, a]) // }); // // img.save(format!("output/{}.png", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs())); // } }