diff --git a/Cargo.toml b/Cargo.toml index 8380ba86..5cd50bfa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,10 +6,8 @@ edition = "2018" [dependencies] sfml = "0.14.0" -quick-xml = "0.14.0" cgmath = "0.17.0" simple-stopwatch="0.1.4" -ncollide2d = "0.19.1" nalgebra = "0.18.0" image = "0.21.2" rand = "0.6.5" diff --git a/src/main.rs b/src/main.rs index 274c992f..b4991251 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,6 @@ extern crate cgmath; extern crate image; extern crate nalgebra as na; -extern crate quick_xml; extern crate rand; extern crate sfml; extern crate time; @@ -43,18 +42,17 @@ use vulkano::sync::GpuFuture; mod slider; mod timer; mod input; -mod util; mod vkprocessor; fn main() { let instance = Instance::new(None, &InstanceExtensions::none(), None).unwrap(); let mut processor = vkprocessor::VkProcessor::new(&instance); - processor.compile_kernel(); - processor.load_buffers(); + processor.compile_kernel(String::from("simple-edge.compute")); + processor.load_buffers(String::from("test2.png")); processor.run_kernel(); - - return; + processor.read_image(); + processor.save_image(); let mut window = RenderWindow::new( (900, 900), @@ -70,18 +68,16 @@ fn main() { let xy = processor.xy; let mut bg_texture = Texture::new(xy.0, xy.1).unwrap(); - bg_texture.update_from_pixels(processor.image_buffer.as_slice(), xy.0, xy.1, 0, 0); + bg_texture.update_from_pixels(processor.read_image().as_slice(), xy.0, xy.1, 0, 0); let mut background_sprite = Sprite::with_texture(&bg_texture); background_sprite.set_position((0., 0.)); let mut slider = Slider::new(40.0, None); - let mut selected_colors = Vec::new(); selected_colors.push(RectangleShape::with_size(Vector2f::new(30.0, 30.0))); - let step_size: f32 = 0.005; let mut elapsed_time: f32; let mut delta_time: f32; @@ -128,10 +124,6 @@ fn main() { window.draw(&background_sprite); -// for i in selected_colors { -// -// } - window.draw(&slider); diff --git a/src/util.rs b/src/util.rs index b37c9aad..e69de29b 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,76 +0,0 @@ -use quick_xml::Reader; -use quick_xml::events::Event as xmlEvent; -use std::collections::HashMap; -use std::borrow::Cow; -use std::str::FromStr; -use sfml::graphics::IntRect; - -pub fn read_spritesheet(filename: String) -> HashMap> { - - let mut reader = Reader::from_file(filename).unwrap(); - reader.trim_text(true); - - let mut buf = Vec::new(); - - let mut t : HashMap> = HashMap::new(); - - loop { - match reader.read_event(&mut buf) { - Ok(xmlEvent::Start(ref e)) => { - match e.name() { - _ => (), - } - }, - Ok(xmlEvent::Empty(d)) => { - - let mut map_entry: HashMap = HashMap::new(); - - let mut name: String = String::new(); - - for i in d.attributes() { - - let attr = i.expect("Couldn't grab attribute"); - - let key = String::from_utf8_lossy(attr.key); - - if key == "name" { - - let value = match attr.value { - Cow::Borrowed(r) => String::from_utf8_lossy(&r), - Cow::Owned(_) => break - }; - name = value.to_string(); - } else { - - let value = match attr.value { - Cow::Borrowed(r) => String::from_utf8_lossy(&r), - Cow::Owned(_) => break - }; - - map_entry.insert(String::from(key), FromStr::from_str(&value[..]).expect("")); - } - } - - t.insert(name,map_entry); - }, - Ok(xmlEvent::Eof) => break, - _ => (), - } - - // if we don't keep a borrow elsewhere, we can clear the buffer to keep memory usage low - buf.clear(); - } - - return t; -} - -pub fn grab_sheet_rec(spritename: String, spritesheet: &HashMap>) -> IntRect { - - let block_desc = spritesheet.get(&spritename).expect("Can't load sprite"); - IntRect::new( - *block_desc.get("x").unwrap(), - *block_desc.get("y").unwrap(), - *block_desc.get("width").unwrap(), - *block_desc.get("height").unwrap() - ) -} diff --git a/src/vkprocessor.rs b/src/vkprocessor.rs index 479f164a..00a710f3 100644 --- a/src/vkprocessor.rs +++ b/src/vkprocessor.rs @@ -58,19 +58,28 @@ impl<'a> VkProcessor<'a> { } - pub fn compile_kernel(&mut self) { - let project_root = std::env::current_dir().expect("failed to get root directory"); + pub fn compile_kernel(&mut self, filename: String) { + + let project_root = + std::env::current_dir() + .expect("failed to get root directory"); + let mut compute_path = project_root.clone(); - compute_path.push(PathBuf::from("resources/shaders/simple-homogenize.compute")); + compute_path.push(PathBuf::from("resources/shaders/")); + compute_path.push(PathBuf::from(filename)); + + let shader = + sr::load_compute(compute_path) + .expect("Failed to compile"); - let shader = sr::load_compute(compute_path).expect("Failed to compile"); - let vulkano_entry = sr::parse_compute(&shader).expect("failed to parse"); + let vulkano_entry = + sr::parse_compute(&shader) + .expect("failed to parse"); let x = unsafe { vulkano::pipeline::shader::ShaderModule::from_words(self.device.clone(), &shader.compute) }.unwrap(); - // Compile the shader and add it to a pipeline let pipeline = Arc::new({ unsafe { ComputePipeline::new(self.device.clone(), &x.compute_entry_point( @@ -83,10 +92,17 @@ impl<'a> VkProcessor<'a> { self.pipeline = Some(pipeline); } - pub fn load_buffers(&mut self) + pub fn load_buffers(&mut self, image_filename: String) { + let project_root = + std::env::current_dir() + .expect("failed to get root directory"); - let img = image::open("resources/images/funky-bird.jpg").unwrap(); + let mut compute_path = project_root.clone(); + compute_path.push(PathBuf::from("resources/images/")); + compute_path.push(PathBuf::from(image_filename)); + + let img = image::open(compute_path).expect("Couldn't find image"); self.xy = img.dimensions(); @@ -145,12 +161,14 @@ impl<'a> VkProcessor<'a> { // Create the data descriptor set for our previously created shader pipeline let mut set = PersistentDescriptorSet::start(self.pipeline.clone().unwrap().clone(), 0) - .add_buffer(write_buffer).unwrap() + .add_buffer(write_buffer.clone()).unwrap() .add_buffer(read_buffer).unwrap() .add_buffer(settings_buffer).unwrap(); self.set = Some(Arc::new(set.build().unwrap())); + + self.img_buffers.push(write_buffer); } pub fn run_kernel(&mut self) {