editing the image via vulkan compute shader

master
mitchellhansen 6 years ago
parent 9b266c8d3f
commit 7a9f08c7ec

@ -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;
}

@ -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),

Loading…
Cancel
Save