From 7a9f08c7ecfe61a79afb70b33c95b5ee65873b23 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Wed, 12 Jun 2019 22:30:10 -0700 Subject: [PATCH] editing the image via vulkan compute shader --- resources/shaders/add.compute | 2 +- src/main.rs | 66 +++++++++++++++++++++-------------- 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/resources/shaders/add.compute b/resources/shaders/add.compute index cf24845d..4a3b9a6c 100644 --- a/resources/shaders/add.compute +++ b/resources/shaders/add.compute @@ -8,5 +8,5 @@ layout(set = 0, binding = 0) buffer Data { void main() { uint idx = gl_GlobalInvocationID.x; - data.data[idx] *= 12; + data.data[idx] += 30; } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 15576b65..21620c0d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -166,38 +166,50 @@ fn main() { let shader = cs::Shader::load(device.clone()).unwrap(); ComputePipeline::new(device.clone(), &shader.main_entry_point(), &()).unwrap() }); - let data_buffer = { - let data_iter = (0 .. 65536u32).map(|n| n); - CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::all(), data_iter).unwrap() - }; - let set = Arc::new(PersistentDescriptorSet::start(pipeline.clone(), 0) - .add_buffer(data_buffer.clone()).unwrap() - .build().unwrap() - ); - let command_buffer = AutoCommandBufferBuilder::primary_one_time_submit(device.clone(), queue.family()).unwrap() - .dispatch([1024, 1, 1], pipeline.clone(), set.clone(), ()).unwrap() - .build().unwrap(); - let future = sync::now(device.clone()) - .then_execute(queue.clone(), command_buffer).unwrap() - .then_signal_fence_and_flush().unwrap(); - future.wait(None).unwrap(); - let data_buffer_content = data_buffer.read().unwrap(); - for n in 0 .. 65536u32 { - assert_eq!(data_buffer_content[n as usize], n * 12); - } 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); + + { + let data_buffer = { + let mut buff = img.as_flat_samples().samples.iter(); + let data_iter = (0 .. data_length).map(|n| *(buff.next().unwrap())); + CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::all(), data_iter).unwrap() + }; + let set = Arc::new(PersistentDescriptorSet::start(pipeline.clone(), 0) + .add_buffer(data_buffer.clone()).unwrap() + .build().unwrap() + ); + let command_buffer = AutoCommandBufferBuilder::primary_one_time_submit(device.clone(), queue.family()).unwrap() + .dispatch([1024, 1, 1], pipeline.clone(), set.clone(), ()).unwrap() + .build().unwrap(); + let future = sync::now(device.clone()) + .then_execute(queue.clone(), command_buffer).unwrap() + .then_signal_fence_and_flush().unwrap(); + 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 { + + 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]; + + + let old = img.get_pixel(x, y); + img.put_pixel(x, y, image::Rgba([r, g, b, 255])); + + } + } + } + + img.save("output.jpg"); println!("Starting"); -// for x in 0..xy.0 { -// for y in 0..xy.1 { -// let mut pixel = img.get_pixel(x, y); -// img.put_pixel(x, y, pixel); -// } -// } - // println!("Ending"); - // img.save("fractal.png").unwrap(); let mut window = RenderWindow::new( (512, 512),