master
mitchellhansen 6 years ago
parent 7a9f08c7ec
commit e8d94e4ba7

@ -1,6 +1,6 @@
#version 450 #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 { layout(set = 0, binding = 0) buffer Data {
uint data[]; uint data[];
@ -8,5 +8,5 @@ layout(set = 0, binding = 0) buffer Data {
void main() { void main() {
uint idx = gl_GlobalInvocationID.x; uint idx = gl_GlobalInvocationID.x;
data.data[idx] += 30; data.data[idx] += 100;
} }

@ -28,6 +28,7 @@ use vulkano::pipeline::ComputePipeline;
use vulkano::sync::GpuFuture; use vulkano::sync::GpuFuture;
use vulkano::sync; use vulkano::sync;
use std::sync::Arc; use std::sync::Arc;
use std::fs;
use crate::input::Input; use crate::input::Input;
use crate::slider::Slider; use crate::slider::Slider;
@ -118,6 +119,7 @@ impl<'t> Effect for Edge<'t> {
"edge post-effect" "edge post-effect"
} }
} }
// =================================================
fn surrounding_pixels(x: u32, y: u32, img: &DynamicImage) -> Vec<image::Rgba<u8>> { fn surrounding_pixels(x: u32, y: u32, img: &DynamicImage) -> Vec<image::Rgba<u8>> {
@ -145,6 +147,7 @@ fn surrounding_pixels(x: u32, y: u32, img: &DynamicImage) -> Vec<image::Rgba<u8>
fn main() { fn main() {
// Create the vulkan instance, device, and device queue
let instance = Instance::new(None, &InstanceExtensions::none(), None).unwrap(); 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 queue_family = physical.queue_families().find(|&q| q.supports_compute()).unwrap();
@ -153,9 +156,9 @@ fn main() {
&DeviceExtensions::none(), &DeviceExtensions::none(),
[(queue_family, 0.5)].iter().cloned()).unwrap(); [(queue_family, 0.5)].iter().cloned()).unwrap();
let queue = queues.next().unwrap(); let queue = queues.next().unwrap();
println!("Device initialized"); println!("Device initialized");
// Compile the shader and add it to a pipeline
let pipeline = Arc::new({ let pipeline = Arc::new({
mod cs { mod cs {
vulkano_shaders::shader!{ vulkano_shaders::shader!{
@ -167,47 +170,62 @@ fn main() {
ComputePipeline::new(device.clone(), &shader.main_entry_point(), &()).unwrap() 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 mut img = image::open("resources/images/funky-bird.jpg").unwrap();
let xy = img.dimensions(); let xy = img.dimensions();
let data_length = xy.0*xy.1*3; let data_length = xy.0*xy.1*3;
println!("Buffer length {}", data_length); println!("Buffer length {}", data_length);
{ {
// Pull out the image data and place it in a sync'd CPU<->GPU buffer
let data_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())); let data_iter = (0 .. data_length).map(|n| *(buff.next().unwrap()));
CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::all(), data_iter).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) let set = Arc::new(PersistentDescriptorSet::start(pipeline.clone(), 0)
.add_buffer(data_buffer.clone()).unwrap() .add_buffer(data_buffer.clone()).unwrap()
.build().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() let command_buffer = AutoCommandBufferBuilder::primary_one_time_submit(device.clone(), queue.family()).unwrap()
.dispatch([1024, 1, 1], pipeline.clone(), set.clone(), ()).unwrap() .dispatch([1024, 1, 1], pipeline.clone(), set.clone(), ()).unwrap()
.build().unwrap(); .build().unwrap();
// Create a future for running the command buffer and then just fence it
let future = sync::now(device.clone()) let future = sync::now(device.clone())
.then_execute(queue.clone(), command_buffer).unwrap() .then_execute(queue.clone(), command_buffer).unwrap()
.then_signal_fence_and_flush().unwrap(); .then_signal_fence_and_flush().unwrap();
// I think this is redundant and returns immediately
future.wait(None).unwrap(); future.wait(None).unwrap();
let data_buffer_content = data_buffer.read().unwrap();
for x in 0 .. xy.0 - 1 { // The buffer is sync'd so we can just read straight from the handle
for y in 0 .. xy.1 - 1 { 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]; for x in 0 .. xy.0 {
let b = data_buffer_content[((xy.0 * y + x) * 3 + 2) as usize]; for y in 0 .. xy.1 {
//let a = data_buffer_content[((xy.0 * y + x) * 4 + 3) as usize];
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); 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"); println!("Starting");
@ -224,6 +242,7 @@ fn main() {
//========================================== //==========================================
let font = Font::from_file("resources/fonts/sansation.ttf").unwrap(); let font = Font::from_file("resources/fonts/sansation.ttf").unwrap();
let mut bg_texture = Texture::from_file("resources/images/sfml.png").unwrap(); let mut bg_texture = Texture::from_file("resources/images/sfml.png").unwrap();
bg_texture.set_smooth(true); bg_texture.set_smooth(true);
@ -291,7 +310,7 @@ fn main() {
window.clear(&Color::BLACK); window.clear(&Color::BLACK);
window.draw(effects[current].as_drawable()); // window.draw(effects[current].as_drawable());
window.draw(&slider); window.draw(&slider);
window.display(); window.display();

Loading…
Cancel
Save