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, CompiledShaders, Entry}; use vulkano::descriptor::descriptor_set::{PersistentDescriptorSetBuf, PersistentDescriptorSetImg, PersistentDescriptorSetSampler}; 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; use image::ImageFormat; use vulkano::image::immutable::ImmutableImage; use vulkano::image::attachment::AttachmentImage; use vulkano::image::{Dimensions, ImageUsage}; use vulkano::format::Format; use vulkano::sampler::{Sampler, Filter, MipmapMode, SamplerAddressMode}; use image::flat::NormalForm::ColumnMajorPacked; mod compute_kernel; use crate::vkprocessor::compute_kernel::ComputeKernel; #[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)] #[derive(Clone)] struct SimpleSpecializationConstants { first_constant: i32, second_constant: u32, third_constant: f32, } unsafe impl SpecializationConstants for SimpleSpecializationConstants { 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 compute_kernel: Option, pub vertex_shader_path: PathBuf, pub fragment_shader_path: PathBuf, pub instance: Arc, pub physical: PhysicalDevice<'a>, pub graphics_pipeline: Option>, pub compute_pipeline: Option>>>, pub device: Arc, pub queues: QueuesIter, pub queue: Arc, pub compute_set: Option>>, ((((), PersistentDescriptorSetBuf>>), PersistentDescriptorSetBuf>>), PersistentDescriptorSetBuf>>)>>>, pub img_set: Option, ((((), PersistentDescriptorSetImg>>), PersistentDescriptorSetSampler), PersistentDescriptorSetImg>)>>>, pub graphics_image_buffer: Option>>, 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 dynamic_state: DynamicState, pub graphics_image_swap_buffer: Option>, } 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 { compute_kernel: Option::None, vertex_shader_path: Default::default(), fragment_shader_path: Default::default(), instance: instance.clone(), physical: physical.clone(), graphics_pipeline: Option::None, compute_pipeline: Option::None, device: device.clone(), queue: queue, queues: queues, compute_set: Option::None, img_set: Option::None, graphics_image_buffer: None, image_buffer: Vec::new(), compute_image_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 }, graphics_image_swap_buffer: None, } } pub fn compile_kernel(&mut self, filename: String) { self.compute_kernel = Some(ComputeKernel::new(filename, self.device.clone())); self.compute_pipeline = Some(self.compute_kernel.clone().unwrap().get_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")); // TODO: better compile message, run til successful compile let shader = sr::load(vertex_shader_path, fragment_shader_path) .expect("Shader didn't 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, SimpleSpecializationConstants { first_constant: 0, second_constant: 0, third_constant: 0.0, }) // The content of the vertex buffer describes a list of triangles. .triangle_fan() // Use a resizable viewport set to draw over the entire window .viewports_dynamic_scissors_irrelevant(1) // See `vertex_shader`. .fragment_shader(frag_entry_point, SimpleSpecializationConstants { first_constant: 0, second_constant: 0, third_constant: 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.graphics_pipeline = 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 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.compute_set = Some(Arc::new(set.build().unwrap())); self.compute_image_buffers.push(write_buffer); self.compute_image_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: [-1.0, -1.0 ] }, tVertex { position: [-1.0, 1.0 ] }, tVertex { position: [ 1.0, 1.0 ] }, tVertex { position: [ 1.0, -1.0 ] }, ].iter().cloned()).unwrap() }; self.vertex_buffer = Some(vertex_buffer); let (texture, tex_future) = { ImmutableImage::from_iter( self.image_buffer.iter().cloned(), Dimensions::Dim2d { width: self.xy.0, height: self.xy.1 }, Format::R8G8B8A8Srgb, self.queue.clone() ).unwrap() }; let attachment_image = { let mut usage = ImageUsage::none(); usage.transfer_destination = true; usage.storage = true; AttachmentImage::with_usage( self.device.clone(), [self.xy.0, self.xy.1], Format::R8G8B8A8Uint, usage) }; let sampler = Sampler::new(self.device.clone(), Filter::Linear, Filter::Linear, MipmapMode::Nearest, SamplerAddressMode::Repeat, SamplerAddressMode::Repeat, SamplerAddressMode::Repeat, 0.0, 1.0, 0.0, 0.0).unwrap(); self.img_set = Some(Arc::new(PersistentDescriptorSet::start(self.graphics_pipeline.clone().unwrap().clone(), 0) .add_sampled_image(texture.clone(), sampler.clone()).unwrap() .add_image(attachment_image.clone().unwrap().clone()).unwrap() .build().unwrap())); self.graphics_image_buffer = Some(texture.clone()); self.graphics_image_swap_buffer = Some(attachment_image.clone().unwrap()); } pub fn run(&mut self, surface: &'a Arc>, mut frame_future: Box) -> Box { 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; // The docs said to call this on each loop. frame_future.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; } // 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.compute_set.clone().unwrap().clone(), ()).unwrap() .copy_buffer_to_image(self.compute_image_buffers.get(0).unwrap().clone(), self.graphics_image_swap_buffer.clone().unwrap()).unwrap() .begin_render_pass(framebuffers[image_num].clone(), false, clear_values) .unwrap() .draw(self.graphics_pipeline.clone().unwrap().clone(), &self.dynamic_state, v, self.img_set.clone().unwrap().clone(), ()) .unwrap() .end_render_pass() .unwrap() .build().unwrap(); let mut data_buffer_content = self.compute_image_buffers.get(0).unwrap().read().unwrap(); let img = ImageBuffer::from_fn(self.xy.0, self.xy.1, |x, y| { 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::Rgba([r, g, b, 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.swapchain.clone().unwrap().clone(), image_num) .then_signal_fence_and_flush(); match future { Ok(future) => { (Box::new(future) as Box<_>) } Err(FlushError::OutOfDate) => { recreate_swapchain = true; (Box::new(sync::now(self.device.clone())) as Box<_>) } Err(e) => { println!("{:?}", e); (Box::new(sync::now(self.device.clone())) as Box<_>) } } } } // 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())); // } }