From 57e862832a8da05dc8cc639bce241e6595da0ed0 Mon Sep 17 00:00:00 2001 From: Steffen Temme Date: Sun, 22 Dec 2024 11:13:05 +0100 Subject: [PATCH] linux shader building, complete side renaming, esc to quit --- build.rs | 22 ++++ shaders/compile.sh | 11 ++ src/main.rs | 6 + src/primitives/cube.rs | 2 +- src/scene.rs | 241 ++++++++++++++++++++--------------------- 5 files changed, 160 insertions(+), 122 deletions(-) create mode 100755 shaders/compile.sh diff --git a/build.rs b/build.rs index e06ed04..42858e0 100644 --- a/build.rs +++ b/build.rs @@ -39,6 +39,28 @@ fn main() { io::stderr().write_all(&output.stderr).unwrap(); assert!(output.status.success()); + } else if std::env::consts::OS == "linux" { + #[allow(unused_must_use)] + std::fs::remove_file("shaders/geo_cube.spv"); + #[allow(unused_must_use)] + std::fs::remove_file("shaders/frag_cube.spv"); + #[allow(unused_must_use)] + std::fs::remove_file("shaders/vert_cube.spv"); + #[allow(unused_must_use)] + std::fs::remove_file("shaders/geo_cuboid.spv"); + #[allow(unused_must_use)] + std::fs::remove_file("shaders/frag_cuboid.spv"); + #[allow(unused_must_use)] + std::fs::remove_file("shaders/vert_cuboid.spv"); + #[warn(unused_must_use)] + // probably need to check the os and have different versions + let mut command = Command::new("./shaders/compile.sh"); + let output = command.output().expect("Failed to execute command"); + println!("status: {}", output.status); + io::stdout().write_all(&output.stdout).unwrap(); + io::stderr().write_all(&output.stderr).unwrap(); + + assert!(output.status.success()); } assert!(Path::new("shaders/geo_cube.spv").exists()); diff --git a/shaders/compile.sh b/shaders/compile.sh new file mode 100755 index 0000000..46bcd3a --- /dev/null +++ b/shaders/compile.sh @@ -0,0 +1,11 @@ +#!/bin/sh +glslc shaders/cube.vert -o shaders/vert_cube.spv +glslc shaders/cube.frag -o shaders/frag_cube.spv +glslc shaders/cube.geom -o shaders/geo_cube.spv + +glslc shaders/cuboid.vert -o shaders/vert_cuboid.spv +glslc shaders/cuboid.frag -o shaders/frag_cuboid.spv +glslc shaders/cuboid.geom -o shaders/geo_cuboid.spv + +glslc shaders/rt_quad.vert -o shaders/vert_rt_quad.spv +glslc shaders/rt_quad.frag -o shaders/frag_rt_quad.spv \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 1118eb0..8228225 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,7 @@ use log::*; use winit::dpi::{LogicalSize, LogicalPosition}; use winit::event::{Event, WindowEvent}; use winit::event_loop::EventLoop; +use winit::keyboard::NamedKey; use winit::window::{Window, WindowBuilder}; use vulkanalia::loader::{LibloadingLoader, LIBRARY}; @@ -131,6 +132,11 @@ fn main() -> Result<()> { if event.logical_key == "d" { app.cur_pos += app.view_direction.cross(vertex::Vec3::new(0.0, 0.0, 1.0)) * 0.1; } + if event.logical_key == NamedKey::Escape { + elwt.exit(); + unsafe { app.device.device_wait_idle().unwrap(); } + unsafe { app.destroy(); } + } }, _ => {} } diff --git a/src/primitives/cube.rs b/src/primitives/cube.rs index bf439ad..fd27a1e 100644 --- a/src/primitives/cube.rs +++ b/src/primitives/cube.rs @@ -11,7 +11,7 @@ pub struct Cube{ pub tex_coord: vertex::Vec2 } -const cube_size: f32 = 0.5; +const cube_size: f32 = 0.48; impl Drawable for Cube { fn draw(& self, topology: &vk::PrimitiveTopology, start_index: usize, scene: &mut Scene) { diff --git a/src/scene.rs b/src/scene.rs index b9b317e..c231b71 100644 --- a/src/scene.rs +++ b/src/scene.rs @@ -5,7 +5,6 @@ use anyhow::Result; use cgmath::{vec2, vec3, Vector3}; use std::cell::RefCell; -use std::intrinsics::size_of; use std::rc::Rc; use std::time::Instant; @@ -652,26 +651,26 @@ struct EmptyVolume { pub position: Vector3, - pub color_left_rn: Vec>, - pub color_right_rn: Vec>, + pub color_left: Vec>, + pub color_right: Vec>, pub color_top: Vec>, pub color_bottom: Vec>, - pub color_back_rn: Vec>, - pub color_front_rn: Vec>, + pub color_back: Vec>, + pub color_front: Vec>, - pub roughness_left_rn: Vec>, - pub roughness_right_rn: Vec>, + pub roughness_left: Vec>, + pub roughness_right: Vec>, pub roughness_top: Vec>, pub roughness_bottom: Vec>, - pub roughness_back_rn: Vec>, - pub roughness_front_rn: Vec>, + pub roughness_back: Vec>, + pub roughness_front: Vec>, - pub neighbor_left_rn: Vec>>>, - pub neighbor_right_rn: Vec>>>, + pub neighbor_left: Vec>>>, + pub neighbor_right: Vec>>>, pub neighbor_top: Vec>>>, pub neighbor_bottom: Vec>>>, - pub neighbor_back_rn: Vec>>>, - pub neighbor_front_rn: Vec>>>, + pub neighbor_back: Vec>>>, + pub neighbor_front: Vec>>>, } impl EmptyVolume { @@ -773,24 +772,24 @@ impl EmptyVolume { size_y: y_size + 1, size_z: z_size + 1, position: Vector3{x: x_index, y: y_index, z: z_index}, - color_left_rn: vec![], - color_right_rn: vec![], + color_left: vec![], + color_right: vec![], color_top: vec![], color_bottom: vec![], - color_back_rn: vec![], - color_front_rn: vec![], - roughness_left_rn: vec![], - roughness_right_rn: vec![], + color_back: vec![], + color_front: vec![], + roughness_left: vec![], + roughness_right: vec![], roughness_top: vec![], roughness_bottom: vec![], - roughness_back_rn: vec![], - roughness_front_rn: vec![], - neighbor_left_rn: vec![], - neighbor_right_rn: vec![], + roughness_back: vec![], + roughness_front: vec![], + neighbor_left: vec![], + neighbor_right: vec![], neighbor_top: vec![], neighbor_bottom: vec![], - neighbor_back_rn: vec![], - neighbor_front_rn: vec![], + neighbor_back: vec![], + neighbor_front: vec![], }; println!("adding neighbor references"); //fill in info in the neighbor octtree @@ -887,111 +886,111 @@ impl EmptyVolume { } // back face of the volume - let mut back_colors_rn = vec![]; - let mut back_roughness_rn = vec![]; - let mut back_elements_num_rn = 0; + let mut back_colors = vec![]; + let mut back_roughness = vec![]; + let mut back_elements_num = 0; for x in 0..x_size+1 { for z in 0..z_size+1 { if let Some(c) = tree.get_element(reference.borrow().position.x + x, y_max_pos, reference.borrow().position.z + z) { - back_elements_num_rn += 1; + back_elements_num += 1; let u8_color = Vector3 {x: (c.color * 255.0).x.min(255.0).max(0.0) as u8, y: (c.color * 255.0).y.min(255.0).max(0.0) as u8, z: (c.color * 255.0).z.min(255.0).max(0.0) as u8}; - back_colors_rn.push(u8_color); - back_roughness_rn.push(Vector3 {x: 128, y: 128, z: 128}); + back_colors.push(u8_color); + back_roughness.push(Vector3 {x: 128, y: 128, z: 128}); } else { - back_colors_rn.push(Vector3 { x: 255, y: 255, z: 255 }); - back_roughness_rn.push(Vector3 {x: 255, y: 255, z: 255}); + back_colors.push(Vector3 { x: 255, y: 255, z: 255 }); + back_roughness.push(Vector3 {x: 255, y: 255, z: 255}); } } } - if back_elements_num_rn > 0 { - reference.borrow_mut().color_back_rn = back_colors_rn; - reference.borrow_mut().roughness_back_rn = back_roughness_rn; + if back_elements_num > 0 { + reference.borrow_mut().color_back = back_colors; + reference.borrow_mut().roughness_back = back_roughness; } else { - reference.borrow_mut().color_back_rn= vec![]; - reference.borrow_mut().roughness_back_rn= vec![]; + reference.borrow_mut().color_back= vec![]; + reference.borrow_mut().roughness_back= vec![]; } // front face of the volume - let mut front_colors_rn = vec![]; - let mut front_roughness_rn = vec![]; - let mut front_elements_num_rn = 0; + let mut front_colors = vec![]; + let mut front_roughness = vec![]; + let mut front_elements_num = 0; for x in 0..x_size+1 { for z in 0..z_size+1 { if let Some(c) = tree.get_element(reference.borrow().position.x + x, y_min_pos, reference.borrow().position.z + z) { - front_elements_num_rn += 1; + front_elements_num += 1; let u8_color = Vector3 {x: (c.color * 255.0).x.min(255.0).max(0.0) as u8, y: (c.color * 255.0).y.min(255.0).max(0.0) as u8, z: (c.color * 255.0).z.min(255.0).max(0.0) as u8}; - front_colors_rn.push(u8_color); - front_roughness_rn.push(Vector3 {x: 128, y: 128, z: 128}); + front_colors.push(u8_color); + front_roughness.push(Vector3 {x: 128, y: 128, z: 128}); } else { - front_colors_rn.push(Vector3 { x: 255, y: 255, z: 255 }); - front_roughness_rn.push(Vector3 {x: 255, y: 255, z: 255}); + front_colors.push(Vector3 { x: 255, y: 255, z: 255 }); + front_roughness.push(Vector3 {x: 255, y: 255, z: 255}); } } } - if front_elements_num_rn > 0 { - reference.borrow_mut().color_front_rn = front_colors_rn; - reference.borrow_mut().roughness_front_rn = front_roughness_rn; + if front_elements_num > 0 { + reference.borrow_mut().color_front = front_colors; + reference.borrow_mut().roughness_front = front_roughness; } else { - reference.borrow_mut().color_front_rn= vec![]; - reference.borrow_mut().roughness_front_rn= vec![]; + reference.borrow_mut().color_front= vec![]; + reference.borrow_mut().roughness_front= vec![]; } // front face of the volume - let mut left_colors_rn = vec![]; - let mut left_roughness_rn = vec![]; - let mut left_elements_num_rn = 0; + let mut left_colors = vec![]; + let mut left_roughness = vec![]; + let mut left_elements_num = 0; for y in 0..y_size+1 { for z in 0..z_size+1 { if let Some(c) = tree.get_element(x_min_pos, reference.borrow().position.y + y, reference.borrow().position.z + z) { - left_elements_num_rn += 1; + left_elements_num += 1; let u8_color = Vector3 {x: (c.color * 255.0).x.min(255.0).max(0.0) as u8, y: (c.color * 255.0).y.min(255.0).max(0.0) as u8, z: (c.color * 255.0).z.min(255.0).max(0.0) as u8}; - left_colors_rn.push(u8_color); - left_roughness_rn.push(Vector3 {x: 128, y: 128, z: 128}); + left_colors.push(u8_color); + left_roughness.push(Vector3 {x: 128, y: 128, z: 128}); } else { - left_colors_rn.push(Vector3 { x: 255, y: 255, z: 255 }); - left_roughness_rn.push(Vector3 {x: 255, y: 255, z: 255}); + left_colors.push(Vector3 { x: 255, y: 255, z: 255 }); + left_roughness.push(Vector3 {x: 255, y: 255, z: 255}); } } } - if left_elements_num_rn > 0 { - reference.borrow_mut().color_left_rn = left_colors_rn; - reference.borrow_mut().roughness_left_rn = left_roughness_rn; + if left_elements_num > 0 { + reference.borrow_mut().color_left = left_colors; + reference.borrow_mut().roughness_left = left_roughness; } else { - reference.borrow_mut().color_left_rn= vec![]; - reference.borrow_mut().roughness_left_rn= vec![]; + reference.borrow_mut().color_left= vec![]; + reference.borrow_mut().roughness_left= vec![]; } // back face of the volume - let mut right_colors_rn = vec![]; - let mut right_roughness_rn = vec![]; - let mut right_elements_num_rn = 0; + let mut right_colors = vec![]; + let mut right_roughness = vec![]; + let mut right_elements_num = 0; for y in 0..y_size+1 { for z in 0..z_size+1 { if let Some(c) = tree.get_element(x_max_pos, reference.borrow().position.y + y, reference.borrow().position.z + z) { - right_elements_num_rn += 1; + right_elements_num += 1; let u8_color = Vector3 {x: (c.color * 255.0).x.min(255.0).max(0.0) as u8, y: (c.color * 255.0).y.min(255.0).max(0.0) as u8, z: (c.color * 255.0).z.min(255.0).max(0.0) as u8}; - right_colors_rn.push(u8_color); - right_roughness_rn.push(Vector3 {x: 128, y: 128, z: 128}); + right_colors.push(u8_color); + right_roughness.push(Vector3 {x: 128, y: 128, z: 128}); } else { - right_colors_rn.push(Vector3 { x: 255, y: 255, z: 255 }); - right_roughness_rn.push(Vector3 {x: 255, y: 255, z: 255}); + right_colors.push(Vector3 { x: 255, y: 255, z: 255 }); + right_roughness.push(Vector3 {x: 255, y: 255, z: 255}); } } } - if right_elements_num_rn > 0 { - reference.borrow_mut().color_right_rn = right_colors_rn; - reference.borrow_mut().roughness_right_rn = right_roughness_rn; + if right_elements_num > 0 { + reference.borrow_mut().color_right = right_colors; + reference.borrow_mut().roughness_right = right_roughness; } else { - reference.borrow_mut().color_right_rn= vec![]; - reference.borrow_mut().roughness_right_rn= vec![]; + reference.borrow_mut().color_right= vec![]; + reference.borrow_mut().roughness_right= vec![]; } println!("new volume done"); @@ -1075,91 +1074,91 @@ impl EmptyVolume { } // back face of the volume - let mut back_neighbors_rn = vec![]; - let mut back_elements_num_rn = 0; + let mut back_neighbors = vec![]; + let mut back_elements_num = 0; for x in 0..reference.borrow().size_x { for z in 0..reference.borrow().size_z { if let Some(c) = neighbors.get_element(reference.borrow().position.x + x, y_max_pos, reference.borrow().position.z + z) { - back_elements_num_rn += 1; - back_neighbors_rn.push(Some(c.clone())); + back_elements_num += 1; + back_neighbors.push(Some(c.clone())); } else { - back_neighbors_rn.push(None); + back_neighbors.push(None); } } } - if back_elements_num_rn > 0 { - reference.borrow_mut().neighbor_back_rn = back_neighbors_rn; + if back_elements_num > 0 { + reference.borrow_mut().neighbor_back = back_neighbors; } else { - reference.borrow_mut().neighbor_back_rn = vec![None]; + reference.borrow_mut().neighbor_back = vec![None]; } // front face of the volume - let mut front_neighbors_rn = vec![]; - let mut front_elements_num_rn = 0; + let mut front_neighbors = vec![]; + let mut front_elements_num = 0; if y_min_pos != 0{ for x in 0..reference.borrow().size_x { for z in 0..reference.borrow().size_z { if let Some(c) = neighbors.get_element(reference.borrow().position.x + x, y_min_pos, reference.borrow().position.z + z) { - front_elements_num_rn += 1; - front_neighbors_rn.push(Some(c.clone())); + front_elements_num += 1; + front_neighbors.push(Some(c.clone())); } else { - front_neighbors_rn.push(None); + front_neighbors.push(None); } } } } - if front_elements_num_rn > 0 { - reference.borrow_mut().neighbor_front_rn = front_neighbors_rn; + if front_elements_num > 0 { + reference.borrow_mut().neighbor_front = front_neighbors; } else { - reference.borrow_mut().neighbor_front_rn = vec![None]; + reference.borrow_mut().neighbor_front = vec![None]; } // left face of the volume - let mut left_neighbors_rn = vec![]; - let mut left_elements_num_rn = 0; + let mut left_neighbors = vec![]; + let mut left_elements_num = 0; if x_min_pos != 0 { for y in 0..reference.borrow().size_y { for z in 0..reference.borrow().size_z { if let Some(c) = neighbors.get_element(x_min_pos, reference.borrow().position.y + y, reference.borrow().position.z + z) { - left_elements_num_rn += 1; - left_neighbors_rn.push(Some(c.clone())); + left_elements_num += 1; + left_neighbors.push(Some(c.clone())); } else { - left_neighbors_rn.push(None); + left_neighbors.push(None); } } } } - if left_elements_num_rn > 0 { - reference.borrow_mut().neighbor_left_rn = left_neighbors_rn; + if left_elements_num > 0 { + reference.borrow_mut().neighbor_left = left_neighbors; } else { - reference.borrow_mut().neighbor_left_rn = vec![None]; + reference.borrow_mut().neighbor_left = vec![None]; } // right face of the volume - let mut right_neighbors_rn = vec![]; - let mut right_elements_num_rn = 0; + let mut right_neighbors = vec![]; + let mut right_elements_num = 0; for y in 0..reference.borrow().size_y { for z in 0..reference.borrow().size_z { if let Some(c) = neighbors.get_element(x_max_pos, reference.borrow().position.y + y, reference.borrow().position.z + z) { - right_elements_num_rn += 1; - right_neighbors_rn.push(Some(c.clone())); + right_elements_num += 1; + right_neighbors.push(Some(c.clone())); } else { - right_neighbors_rn.push(None); + right_neighbors.push(None); } } } - if right_elements_num_rn > 0 { - reference.borrow_mut().neighbor_right_rn = right_neighbors_rn; + if right_elements_num > 0 { + reference.borrow_mut().neighbor_right = right_neighbors; } else { - reference.borrow_mut().neighbor_right_rn = vec![None]; + reference.borrow_mut().neighbor_right = vec![None]; } } println!("volume creation took {} s", start_time.elapsed().as_millis() as f32 / 1000.0); @@ -1220,11 +1219,11 @@ impl EmptyVolume { for x in 0..self.size_x { for z in 0..self.size_z { let index = x * self.size_z + z; - if self.color_front_rn.len() == 0 { + if self.color_front.len() == 0 { continue; } - if self.neighbor_front_rn.len() > index { - if let Some(_) = self.neighbor_front_rn[index] { + if self.neighbor_front.len() > index { + if let Some(_) = self.neighbor_front[index] { continue; } } @@ -1244,11 +1243,11 @@ impl EmptyVolume { for x in 0..self.size_x { for z in 0..self.size_z { let index = x * self.size_z + z; - if self.color_back_rn.len() == 0 { + if self.color_back.len() == 0 { continue; } - if self.neighbor_back_rn.len() > index { - if let Some(_) = self.neighbor_back_rn[index] { + if self.neighbor_back.len() > index { + if let Some(_) = self.neighbor_back[index] { continue; } } @@ -1268,11 +1267,11 @@ impl EmptyVolume { for y in 0..self.size_y { for z in 0..self.size_z { let index = y * self.size_z + z; - if self.color_left_rn.len() == 0 { + if self.color_left.len() == 0 { continue; } - if self.neighbor_left_rn.len() > index { - if let Some(_) = self.neighbor_left_rn[index] { + if self.neighbor_left.len() > index { + if let Some(_) = self.neighbor_left[index] { continue; } } @@ -1292,11 +1291,11 @@ impl EmptyVolume { for y in 0..self.size_y { for z in 0..self.size_z { let index = y * self.size_z + z; - if self.color_right_rn.len() == 0 { + if self.color_right.len() == 0 { continue; } - if self.neighbor_right_rn.len() > index { - if let Some(_) = self.neighbor_right_rn[index] { + if self.neighbor_right.len() > index { + if let Some(_) = self.neighbor_right[index] { continue; } }