interesting behaviour on the .clone()ing of options and arcs causing compiler errors
This commit is contained in:
25
src/main.rs
25
src/main.rs
@@ -10,24 +10,14 @@ extern crate rand;
|
||||
extern crate sfml;
|
||||
extern crate time;
|
||||
|
||||
use image::{DynamicImage, GenericImage, GenericImageView, Pixel, SubImage};
|
||||
use sfml::graphics::*;
|
||||
use sfml::graphics::{
|
||||
Color, RenderTarget, RenderWindow,
|
||||
};
|
||||
use sfml::system::*;
|
||||
use sfml::system::Vector2 as sfVec2;
|
||||
use sfml::window::*;
|
||||
use sfml::window::{Event, Key, Style};
|
||||
use sfml::window::mouse::Button;
|
||||
|
||||
use vulkano::buffer::{BufferUsage, CpuAccessibleBuffer, DeviceLocalBuffer, ImmutableBuffer, BufferAccess};
|
||||
use vulkano::command_buffer::AutoCommandBufferBuilder;
|
||||
use vulkano::descriptor::descriptor_set::PersistentDescriptorSet;
|
||||
use vulkano::device::{Device, DeviceExtensions};
|
||||
use vulkano::instance::{Instance, InstanceExtensions, PhysicalDevice};
|
||||
use vulkano::pipeline::ComputePipeline;
|
||||
use vulkano::sync::GpuFuture;
|
||||
use vulkano::sync;
|
||||
use std::sync::Arc;
|
||||
use std::{fs, mem, iter, ptr};
|
||||
@@ -42,6 +32,13 @@ use std::time::{SystemTime, Duration};
|
||||
use shade_runner as sr;
|
||||
use std::ffi::CStr;
|
||||
use std::ptr::write;
|
||||
use vulkano::buffer::{BufferUsage, CpuAccessibleBuffer, DeviceLocalBuffer, ImmutableBuffer, BufferAccess};
|
||||
use vulkano::command_buffer::AutoCommandBufferBuilder;
|
||||
use vulkano::descriptor::descriptor_set::PersistentDescriptorSet;
|
||||
use vulkano::device::{Device, DeviceExtensions};
|
||||
use vulkano::instance::{Instance, InstanceExtensions, PhysicalDevice};
|
||||
use vulkano::pipeline::ComputePipeline;
|
||||
use vulkano::sync::GpuFuture;
|
||||
|
||||
mod slider;
|
||||
mod timer;
|
||||
@@ -51,11 +48,13 @@ mod vkprocessor;
|
||||
|
||||
fn main() {
|
||||
|
||||
let mut processor = vkprocessor::VkProcessor::new();
|
||||
let instance = Instance::new(None, &InstanceExtensions::none(), None).unwrap();
|
||||
let mut processor = vkprocessor::VkProcessor::new(&instance);
|
||||
processor.compile_kernel();
|
||||
processor.load_buffers();
|
||||
processor.run_kernel();
|
||||
|
||||
|
||||
return;
|
||||
|
||||
let mut window = RenderWindow::new(
|
||||
(900, 900),
|
||||
@@ -69,7 +68,7 @@ fn main() {
|
||||
|
||||
let font = Font::from_file("resources/fonts/sansation.ttf").unwrap();
|
||||
|
||||
let xy = processor.img.unwrap().dimensions();
|
||||
let xy = processor.xy;
|
||||
let mut bg_texture = Texture::new(xy.0, xy.1).unwrap();
|
||||
bg_texture.update_from_pixels(processor.image_buffer.as_slice(), xy.0, xy.1, 0, 0);
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ use std::sync::Arc;
|
||||
use std::ffi::CStr;
|
||||
use std::path::PathBuf;
|
||||
use shade_runner as sr;
|
||||
use image::DynamicImage;
|
||||
use image::{DynamicImage, ImageBuffer};
|
||||
use image::GenericImageView;
|
||||
use vulkano::descriptor::pipeline_layout::PipelineLayout;
|
||||
use image::GenericImage;
|
||||
@@ -21,43 +21,39 @@ use vulkano::descriptor::descriptor_set::PersistentDescriptorSetBuf;
|
||||
pub struct VkProcessor<'a> {
|
||||
pub instance: Arc<Instance>,
|
||||
pub physical: PhysicalDevice<'a>,
|
||||
pub queue_family: QueueFamily<'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 img: Option<DynamicImage>,
|
||||
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() -> VkProcessor<'a> {
|
||||
pub fn new(instance : &'a Arc<Instance>) -> VkProcessor<'a> {
|
||||
|
||||
let instance = Instance::new(None, &InstanceExtensions::none(), None).unwrap();
|
||||
let physical = PhysicalDevice::enumerate(&instance).next().unwrap();
|
||||
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();
|
||||
|
||||
// Self referential struct problem
|
||||
VkProcessor {
|
||||
instance: instance.clone(),
|
||||
physical: physical.clone(),
|
||||
queue_family: physical.queue_families().find(|&q| q.supports_compute()).unwrap(),
|
||||
pipeline: Option::None,
|
||||
device: device,
|
||||
queues: queues,
|
||||
queue: queues.next().unwrap(),
|
||||
img: Option::None,
|
||||
queues: queues,
|
||||
set: Option::None,
|
||||
image_buffer: Vec::new(),
|
||||
img_buffers: Vec::new(),
|
||||
settings_buffer: Option::None,
|
||||
xy: (0,0),
|
||||
}
|
||||
|
||||
}
|
||||
@@ -84,20 +80,23 @@ impl<'a> VkProcessor<'a> {
|
||||
}
|
||||
});
|
||||
|
||||
self.pipeline = Some(pipeline);
|
||||
}
|
||||
|
||||
pub fn load_buffers(&mut self) {
|
||||
pub fn load_buffers(&mut self)
|
||||
{
|
||||
|
||||
self.img = Option::Some(image::open("resources/images/funky-bird.jpg").unwrap());
|
||||
let img = image::open("resources/images/funky-bird.jpg").unwrap();
|
||||
|
||||
let xy = self.img.unwrap().dimensions();
|
||||
let data_length = xy.0 * xy.1 * 4;
|
||||
let pixel_count = self.img.unwrap().raw_pixels().len();
|
||||
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 self.img.unwrap().raw_pixels().iter() {
|
||||
for i in img.raw_pixels().iter() {
|
||||
if (self.image_buffer.len() + 1) % 4 == 0 {
|
||||
self.image_buffer.push(255);
|
||||
}
|
||||
@@ -105,11 +104,11 @@ impl<'a> VkProcessor<'a> {
|
||||
}
|
||||
self.image_buffer.push(255);
|
||||
} else {
|
||||
self.image_buffer = self.img.unwrap().raw_pixels();
|
||||
self.image_buffer = img.raw_pixels();
|
||||
}
|
||||
|
||||
println!("Buffer length {}", self.image_buffer.len());
|
||||
println!("Size {:?}", xy);
|
||||
println!("Size {:?}", self.xy);
|
||||
|
||||
println!("Allocating Buffers...");
|
||||
|
||||
@@ -119,7 +118,7 @@ impl<'a> VkProcessor<'a> {
|
||||
let data_iter = (0..data_length).map(|n| *(buff.next().unwrap()));
|
||||
CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::all(), data_iter).unwrap()
|
||||
};
|
||||
self.img_buffers.push(write_buffer);
|
||||
|
||||
|
||||
// Pull out the image data and place it in a buffer for the kernel to read from
|
||||
let read_buffer = {
|
||||
@@ -127,36 +126,41 @@ impl<'a> VkProcessor<'a> {
|
||||
let data_iter = (0..data_length).map(|n| *(buff.next().unwrap()));
|
||||
CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::all(), data_iter).unwrap()
|
||||
};
|
||||
self.img_buffers.push(read_buffer);
|
||||
|
||||
|
||||
// A buffer to hold many i32 values to use as settings
|
||||
let settings_buffer = {
|
||||
let vec = vec![xy.0, xy.1];
|
||||
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()
|
||||
let data_iter =
|
||||
(0..2).map(|n| *(buff.next().unwrap()));
|
||||
CpuAccessibleBuffer::from_iter(self.device.clone(),
|
||||
BufferUsage::all(),
|
||||
data_iter).unwrap()
|
||||
};
|
||||
self.settings_buffer = Some(settings_buffer);
|
||||
|
||||
|
||||
println!("Done");
|
||||
|
||||
// Create the data descriptor set for our previously created shader pipeline
|
||||
let mut set = PersistentDescriptorSet::start(self.pipeline.unwrap().clone(), 0)
|
||||
.add_buffer(write_buffer.clone()).unwrap()
|
||||
.add_buffer(read_buffer.clone()).unwrap()
|
||||
.add_buffer(settings_buffer.clone()).unwrap();
|
||||
let mut set =
|
||||
PersistentDescriptorSet::start(self.pipeline.clone().unwrap().clone(), 0)
|
||||
.add_buffer(write_buffer).unwrap()
|
||||
.add_buffer(read_buffer).unwrap()
|
||||
.add_buffer(settings_buffer).unwrap();
|
||||
|
||||
// self.set = Some(Arc::new(set.build().unwrap()));
|
||||
|
||||
self.set = Some(Arc::new(set.build().unwrap()));
|
||||
}
|
||||
|
||||
pub fn run_kernel(&mut self) {
|
||||
|
||||
println!("Running Kernel...");
|
||||
let xy = self.img.unwrap().dimensions();
|
||||
|
||||
// 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([xy.0, xy.1, 1], self.pipeline.unwrap().clone(), self.set.unwrap().clone(), ()).unwrap()
|
||||
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
|
||||
@@ -171,8 +175,6 @@ impl<'a> VkProcessor<'a> {
|
||||
|
||||
pub fn read_image(&self) -> Vec<u8> {
|
||||
|
||||
let xy = self.img.unwrap().dimensions();
|
||||
|
||||
// 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();
|
||||
|
||||
@@ -180,20 +182,20 @@ impl<'a> VkProcessor<'a> {
|
||||
|
||||
let mut image_buffer = Vec::new();
|
||||
|
||||
for y in 0..xy.1 {
|
||||
for x in 0..xy.0 {
|
||||
for y in 0..self.xy.1 {
|
||||
for x in 0..self.xy.0 {
|
||||
|
||||
let r = data_buffer_content[((xy.0 * y + x) * 4 + 0) as usize] as u8;
|
||||
let g = data_buffer_content[((xy.0 * y + x) * 4 + 1) as usize] as u8;
|
||||
let b = data_buffer_content[((xy.0 * y + x) * 4 + 2) as usize] as u8;
|
||||
let a = data_buffer_content[((xy.0 * y + x) * 4 + 3) as usize] as u8;
|
||||
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]))
|
||||
//self.img.unwrap().put_pixel(x, y, image::Rgba([r, g, b, a]))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,7 +204,20 @@ impl<'a> VkProcessor<'a> {
|
||||
|
||||
pub fn save_image(&self) {
|
||||
println!("Saving output");
|
||||
self.img.unwrap().save(format!("output/{}.png", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs()));
|
||||
|
||||
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()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user