From e8d94e4ba77d74ee9d699500b4c9bdfd01615123 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Thu, 13 Jun 2019 00:56:52 -0700 Subject: [PATCH] fiddling --- resources/shaders/add.compute | 4 ++-- src/main.rs | 45 +++++++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/resources/shaders/add.compute b/resources/shaders/add.compute index 4a3b9a6c..691d95b3 100644 --- a/resources/shaders/add.compute +++ b/resources/shaders/add.compute @@ -1,6 +1,6 @@ #version 450 -layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in; +layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in; layout(set = 0, binding = 0) buffer Data { uint data[]; @@ -8,5 +8,5 @@ layout(set = 0, binding = 0) buffer Data { void main() { uint idx = gl_GlobalInvocationID.x; - data.data[idx] += 30; + data.data[idx] += 100; } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 21620c0d..30319a62 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,6 +28,7 @@ use vulkano::pipeline::ComputePipeline; use vulkano::sync::GpuFuture; use vulkano::sync; use std::sync::Arc; +use std::fs; use crate::input::Input; use crate::slider::Slider; @@ -118,6 +119,7 @@ impl<'t> Effect for Edge<'t> { "edge post-effect" } } +// ================================================= fn surrounding_pixels(x: u32, y: u32, img: &DynamicImage) -> Vec> { @@ -145,6 +147,7 @@ fn surrounding_pixels(x: u32, y: u32, img: &DynamicImage) -> Vec fn main() { + // Create the vulkan instance, device, and device queue let instance = Instance::new(None, &InstanceExtensions::none(), None).unwrap(); let physical = PhysicalDevice::enumerate(&instance).next().unwrap(); let queue_family = physical.queue_families().find(|&q| q.supports_compute()).unwrap(); @@ -153,9 +156,9 @@ fn main() { &DeviceExtensions::none(), [(queue_family, 0.5)].iter().cloned()).unwrap(); let queue = queues.next().unwrap(); - println!("Device initialized"); + // Compile the shader and add it to a pipeline let pipeline = Arc::new({ mod cs { vulkano_shaders::shader!{ @@ -167,47 +170,62 @@ fn main() { ComputePipeline::new(device.clone(), &shader.main_entry_point(), &()).unwrap() }); + // Load up the input image, determine some details let mut img = image::open("resources/images/funky-bird.jpg").unwrap(); let xy = img.dimensions(); let data_length = xy.0*xy.1*3; println!("Buffer length {}", data_length); { + // Pull out the image data and place it in a sync'd CPU<->GPU buffer let data_buffer = { - let mut buff = img.as_flat_samples().samples.iter(); + let mut buff = img.raw_pixels(); + let mut buff = buff.iter(); let data_iter = (0 .. data_length).map(|n| *(buff.next().unwrap())); CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::all(), data_iter).unwrap() }; + + // Create the data descriptor set for our previously created shader pipeline let set = Arc::new(PersistentDescriptorSet::start(pipeline.clone(), 0) .add_buffer(data_buffer.clone()).unwrap() .build().unwrap() ); + + // 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(device.clone(), queue.family()).unwrap() .dispatch([1024, 1, 1], pipeline.clone(), set.clone(), ()).unwrap() .build().unwrap(); + + // Create a future for running the command buffer and then just fence it let future = sync::now(device.clone()) .then_execute(queue.clone(), command_buffer).unwrap() .then_signal_fence_and_flush().unwrap(); + + // I think this is redundant and returns immediately future.wait(None).unwrap(); - let data_buffer_content = data_buffer.read().unwrap(); - for x in 0 .. xy.0 - 1 { - for y in 0 .. xy.1 - 1 { + // The buffer is sync'd so we can just read straight from the handle + let data_buffer_content = data_buffer.read().unwrap(); - let r = data_buffer_content[((xy.0 * y + x) * 3 + 0) as usize]; - let g = data_buffer_content[((xy.0 * y + x) * 3 + 1) as usize]; - let b = data_buffer_content[((xy.0 * y + x) * 3 + 2) as usize]; - //let a = data_buffer_content[((xy.0 * y + x) * 4 + 3) as usize]; + // + for x in 0 .. xy.0 { + for y in 0 .. xy.1 { + let r = data_buffer_content[((xy.0 * y + x) * 3 + 0) as usize] as u8; + let g = data_buffer_content[((xy.0 * y + x) * 3 + 1) as usize] as u8; + let b = data_buffer_content[((xy.0 * y + x) * 3 + 2) as usize] as u8; + // let a = data_buffer_content[((xy.0 * y + x) * 4 + 3) as usize] as u8; let old = img.get_pixel(x, y); - img.put_pixel(x, y, image::Rgba([r, g, b, 255])); - + img.put_pixel(x, y, image::Rgba([r, g, b, 0])); } } } - img.save("output.jpg"); + fs::remove_file("output.jpg"); + img.save("output14.jpg"); + + return; println!("Starting"); @@ -224,6 +242,7 @@ fn main() { //========================================== let font = Font::from_file("resources/fonts/sansation.ttf").unwrap(); + let mut bg_texture = Texture::from_file("resources/images/sfml.png").unwrap(); bg_texture.set_smooth(true); @@ -291,7 +310,7 @@ fn main() { window.clear(&Color::BLACK); - window.draw(effects[current].as_drawable()); + // window.draw(effects[current].as_drawable()); window.draw(&slider); window.display();