|
|
|
use vulkano::buffer::{BufferUsage, CpuAccessibleBuffer, DeviceLocalBuffer, ImmutableBuffer, BufferAccess};
|
|
|
|
use vulkano::command_buffer::AutoCommandBufferBuilder;
|
|
|
|
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;
|
|
|
|
use vulkano::sync::GpuFuture;
|
|
|
|
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;
|
|
|
|
use vulkano::descriptor::descriptor_set::PersistentDescriptorSetBuf;
|
|
|
|
|
|
|
|
pub struct VkProcessor<'a> {
|
|
|
|
pub instance: Arc<Instance>,
|
|
|
|
pub physical: PhysicalDevice<'a>,
|
|
|
|
pub pipeline: Option<Arc<ComputePipeline<PipelineLayout<shade_runner::layouts::ComputeLayout>>>>,
|
|
|
|
pub device: Arc<Device>,
|
|
|
|
pub queues: QueuesIter,
|
|
|
|
pub queue: Arc<Queue>,
|
|
|
|
pub set: Option<Arc<PersistentDescriptorSet<std::sync::Arc<ComputePipeline<PipelineLayout<shade_runner::layouts::ComputeLayout>>>, ((((), PersistentDescriptorSetBuf<std::sync::Arc<vulkano::buffer::cpu_access::CpuAccessibleBuffer<[u8]>>>), PersistentDescriptorSetBuf<std::sync::Arc<vulkano::buffer::cpu_access::CpuAccessibleBuffer<[u8]>>>), PersistentDescriptorSetBuf<std::sync::Arc<vulkano::buffer::cpu_access::CpuAccessibleBuffer<[u32]>>>)>>>,
|
|
|
|
pub image_buffer: Vec<u8>,
|
|
|
|
pub img_buffers: Vec<Arc<CpuAccessibleBuffer<[u8]>>>,
|
|
|
|
pub settings_buffer: Option<Arc<CpuAccessibleBuffer<[u32]>>>,
|
|
|
|
pub xy: (u32, u32),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> VkProcessor<'a> {
|
|
|
|
pub fn new(instance : &'a Arc<Instance>) -> VkProcessor<'a> {
|
|
|
|
|
|
|
|
let physical = PhysicalDevice::enumerate(instance).next().unwrap();
|
|
|
|
let queue_family = physical.queue_families().find(|&q| q.supports_compute()).unwrap();
|
|
|
|
let (device, mut queues) = Device::new(physical,
|
|
|
|
physical.supported_features(),
|
|
|
|
&DeviceExtensions::none(),
|
|
|
|
[(queue_family, 0.5)].iter().cloned()).unwrap();
|
|
|
|
|
|
|
|
VkProcessor {
|
|
|
|
instance: instance.clone(),
|
|
|
|
physical: physical.clone(),
|
|
|
|
pipeline: Option::None,
|
|
|
|
device: device,
|
|
|
|
queue: queues.next().unwrap(),
|
|
|
|
queues: queues,
|
|
|
|
set: Option::None,
|
|
|
|
image_buffer: Vec::new(),
|
|
|
|
img_buffers: Vec::new(),
|
|
|
|
settings_buffer: Option::None,
|
|
|
|
xy: (0,0),
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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 shader =
|
|
|
|
sr::load_compute(compute_path)
|
|
|
|
.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 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.pipeline = Some(pipeline);
|
|
|
|
}
|
|
|
|
|
|
|
|
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.pipeline.clone().unwrap().clone(), 0)
|
|
|
|
.add_buffer(write_buffer.clone()).unwrap()
|
|
|
|
.add_buffer(read_buffer).unwrap()
|
|
|
|
.add_buffer(settings_buffer).unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
self.set = Some(Arc::new(set.build().unwrap()));
|
|
|
|
|
|
|
|
self.img_buffers.push(write_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.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<u8> {
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
//self.img.unwrap().put_pixel(x, y, image::Rgba([r, g, b, 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()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|