From d5350a04ffbbc07da8363efb8f5f2dd637fe6fb2 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Sun, 30 Jun 2019 21:53:06 -0700 Subject: [PATCH] runtime compiled shader. Annoying behaviour forcing clean builds or whitespace modification before picking up shader changes in compile time shader --- Cargo.toml | 2 +- resources/shaders/add.compute | 44 +++++++++++++++++++-------- src/main.rs | 56 +++++++++++++++++++++++++---------- 3 files changed, 73 insertions(+), 29 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 09245e19..8380ba86 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,5 +16,5 @@ rand = "0.6.5" vulkano = "0.12.0" vulkano-shaders = "0.12.0" time = "0.1.38" - +shade_runner = "0.1.1" diff --git a/resources/shaders/add.compute b/resources/shaders/add.compute index d9601b6c..23788ed3 100644 --- a/resources/shaders/add.compute +++ b/resources/shaders/add.compute @@ -6,9 +6,13 @@ layout(set = 0, binding = 0) buffer Data { int data[]; } data; -//layout(set = 0, binding = 1) buffer Settings { -// int settings[]; -//} settings; +layout(set = 0, binding = 1) buffer Data1 { + int odata[]; +} odata; + +layout(set = 0, binding = 2) buffer Settings { + int settings[]; +} settings; ivec4 separate(int pix){ ivec4 r = ivec4( @@ -26,28 +30,44 @@ uint get_idx(int offset_x, int offset_y){ // uint y = min(settings.settings[1], max(0, gl_GlobalInvocationID.y + offset_y)); // return ((y * settings.settings[0]) + x); - uint x = min(800, max(0, gl_GlobalInvocationID.x + offset_x)); - uint y = min(600, max(0, gl_GlobalInvocationID.y + offset_y)); - return ((y * 800) + x); + uint x = min(settings.settings[0], max(0, gl_GlobalInvocationID.x + offset_x)); + uint y = min(settings.settings[1], max(0, gl_GlobalInvocationID.y + offset_y)); + return ((y * settings.settings[0]) + x); } void main() { uint idx = get_idx(0,0); - ivec4 p = separate(data.data[get_idx(0,0)]); + ivec4 p = separate(odata.odata[get_idx(0,0)]); + + ivec4 p0 = separate(odata.odata[get_idx(0, 1)]); + ivec4 p1 = separate(odata.odata[get_idx(0,-1)]); + + ivec4 p2 = separate(odata.odata[get_idx(1, 1)]); + ivec4 p3 = separate(odata.odata[get_idx(-1,-1)]); + ivec4 p4 = separate(odata.odata[get_idx(1, 0)]); + ivec4 p5 = separate(odata.odata[get_idx(-1,0)]); - ivec4 p0 = separate(data.data[get_idx(2,0)]); - ivec4 p1 = separate(data.data[get_idx(-2,0)]); + ivec4 p6 = separate(odata.odata[get_idx(1,-1)]); + ivec4 p7 = separate(odata.odata[get_idx(-1,1)]); - ivec4 p2 = separate(data.data[get_idx(0,2)]); - ivec4 p3 = separate(data.data[get_idx(0,-2)]); ivec3 d0 = abs(p0.xyz - p1.xyz); ivec3 d1 = abs(p2.xyz - p3.xyz); + ivec3 d2 = abs(p4.xyz - p5.xyz); + ivec3 d3 = abs(p6.xyz - p7.xyz); + + ivec3 m = max(max(max(d0, d1), d2), d3); + + if ((m.x + m.y + m.z) > 150){ + p.x = 0; + p.y = 0; + p.z = 255; + } - p.x = 255;//d0.x + d0.y + d0.z + d1.x + d1.y + d1.z; + // p.z = max(p.z - (d0.x + d0.y + d0.z + d1.x + d1.y + d1.z)/5, 0); data.data[idx] = (data.data[idx] & (~0x000000FF) ) | (p.x); data.data[idx] = (data.data[idx] & (~0x0000FF00) ) | (p.y << 8); diff --git a/src/main.rs b/src/main.rs index 098493e6..ff73d571 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ extern crate nalgebra as na; extern crate quick_xml; extern crate rand; extern crate sfml; +extern crate time; use image::{DynamicImage, GenericImage, GenericImageView, Pixel, SubImage}; use sfml::graphics::*; @@ -29,11 +30,15 @@ use vulkano::sync::GpuFuture; use vulkano::sync; use std::sync::Arc; use std::fs; +use std::path::PathBuf; use crate::input::Input; use crate::slider::Slider; use crate::timer::Timer; use na::DimAdd; +use std::time::{SystemTime, Duration}; +use shade_runner as sr; +use std::ffi::CStr; mod slider; mod timer; @@ -159,20 +164,30 @@ fn main() { let queue = queues.next().unwrap(); println!("Device initialized"); + let project_root = std::env::current_dir().expect("failed to get root directory"); + let mut vert_path = project_root.clone(); + vert_path.push(PathBuf::from("resources/shaders/add.compute")); + + let shader = sr::load_compute(vert_path).expect("Failed to compile"); + let vulkano_entry = sr::parse_compute(&shader).expect("failed to parse"); + + let x = unsafe { + vulkano::pipeline::shader::ShaderModule::from_words(device.clone(), &shader.compute) + }.unwrap(); + + // Compile the shader and add it to a pipeline let pipeline = Arc::new({ - mod cs { - vulkano_shaders::shader!{ - ty: "compute", - path: "resources/shaders/add.compute" - } + unsafe { + ComputePipeline::new(device.clone(), &x.compute_entry_point( + CStr::from_bytes_with_nul_unchecked(b"main\0"), + vulkano_entry.compute_layout), &() + ).unwrap() } - let shader = cs::Shader::load(device.clone()).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/background.jpg").unwrap(); + let mut img = image::open("resources/images/funky-bird.jpg").unwrap(); let xy = img.dimensions(); let data_length = xy.0*xy.1*4; @@ -192,21 +207,30 @@ fn main() { { // Pull out the image data and place it in a sync'd CPU<->GPU buffer - // Raw pixels is a u8 rgb buffer (x*y*3) let data_buffer = { - //let mut buff = img.raw_pixels(); + + let mut buff = image_buffer.iter(); + let data_iter = (0 .. data_length).map(|n| *(buff.next().unwrap())); + CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::all(), data_iter).unwrap() + }; + + let out_buffer = { let mut buff = image_buffer.iter(); let data_iter = (0 .. data_length).map(|n| *(buff.next().unwrap())); CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::all(), data_iter).unwrap() }; let settings_buffer = { - CpuAccessibleBuffer::from_data(device.clone(), BufferUsage::all(), vec![xy.0, xy.1]).unwrap() + let vec = vec![xy.0, xy.1]; + let mut buff = vec.iter(); + let data_iter = (0 .. 2).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) + let mut set = Arc::new(PersistentDescriptorSet::start(pipeline.clone(), 0) .add_buffer(data_buffer.clone()).unwrap() + .add_buffer(out_buffer.clone()).unwrap() .add_buffer(settings_buffer.clone()).unwrap() .build().unwrap() ); @@ -222,19 +246,19 @@ fn main() { .then_signal_fence_and_flush().unwrap(); // I think this is redundant and returns immediately - // future.wait(None).unwrap(); + future.wait(None).unwrap(); // The buffer is sync'd so we can just read straight from the handle let data_buffer_content = data_buffer.read().unwrap(); for y in 0 .. xy.1 { for x in 0 .. 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; - *image_buffer.get_mut(((xy.0 * y + x) * 4 + 0) as usize).unwrap() = r; *image_buffer.get_mut(((xy.0 * y + x) * 4 + 1) as usize).unwrap() = g; *image_buffer.get_mut(((xy.0 * y + x) * 4 + 2) as usize).unwrap() = b; @@ -245,9 +269,9 @@ fn main() { } } - img.save("output9.png"); + img.save(format!("output/{}.png", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs())); - println!("Starting"); + println!("Stdddwafasddfqwefaarting"); let mut window = RenderWindow::new( (900, 900),