runtime compiled shader. Annoying behaviour forcing clean builds or whitespace modification before picking up shader changes in compile time shader

master
mitchellhansen 5 years ago
parent 8c22536653
commit d5350a04ff

@ -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"

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

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

Loading…
Cancel
Save