diff --git a/shaders/geo.spv b/shaders/geo.spv index 1419149..b9be273 100644 Binary files a/shaders/geo.spv and b/shaders/geo.spv differ diff --git a/shaders/shader.geom b/shaders/shader.geom index ce74298..5e127e5 100644 --- a/shaders/shader.geom +++ b/shaders/shader.geom @@ -8,6 +8,7 @@ layout(binding = 0) uniform UniformBufferObject { mat4 geom_rot; mat4 view; mat4 proj; + bool[16] use_geom_shader; } ubo; layout(location = 0) in vec3 geoColor[]; diff --git a/shaders/shader.vert b/shaders/shader.vert index 1ffef71..9e5c24b 100644 --- a/shaders/shader.vert +++ b/shaders/shader.vert @@ -5,6 +5,7 @@ layout(binding = 0) uniform UniformBufferObject { mat4 geom_rot; mat4 view; mat4 proj; + bool[16] use_geom_shader; } ubo; @@ -16,7 +17,11 @@ layout(location = 0) out vec3 geoColor; layout(location = 1) out vec2 geoTexCoord; void main() { - gl_Position = ubo.proj * ubo.view * ubo.geom_rot * ubo.model * vec4(inPosition, 1.0); + if (ubo.use_geom_shader[0]) { + gl_Position = ubo.geom_rot * ubo.model * vec4(inPosition, 1.0); + } else { + gl_Position = ubo.proj * ubo.view * ubo.geom_rot * ubo.model * vec4(inPosition, 1.0); + } geoColor = inColor; geoTexCoord = inTexCoord; } \ No newline at end of file diff --git a/shaders/vert.spv b/shaders/vert.spv index d3b1d59..7c214fa 100644 Binary files a/shaders/vert.spv and b/shaders/vert.spv differ diff --git a/src/app_data.rs b/src/app_data.rs index a70a3c0..9cc625e 100644 --- a/src/app_data.rs +++ b/src/app_data.rs @@ -52,4 +52,5 @@ pub struct AppData { pub color_image: vk::Image, pub color_image_memory: vk::DeviceMemory, pub color_image_view: vk::ImageView, + pub use_geometry_shader: bool, } \ No newline at end of file diff --git a/src/buffer.rs b/src/buffer.rs index 1efb613..0b56a18 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -181,6 +181,7 @@ pub struct UniformBufferObject { pub geom_rot: Mat4, pub view: Mat4, pub proj: Mat4, + pub use_geom_shader: [bool; 16], } pub unsafe fn create_descriptor_set_layout( diff --git a/src/main.rs b/src/main.rs index 0fb944e..67005a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,6 +45,7 @@ pub mod command_buffer; pub mod depth_buffer; pub mod load_model; pub mod scene; +pub mod primitives; const PORTABILITY_MACOS_VERSION: Version = Version::new(1, 3, 216); const VALIDATION_ENABLED: bool = @@ -70,6 +71,7 @@ fn main() -> Result<()> { .build(&event_loop)?; //window.set_cursor_visible(false); + // event_loop.set_control_flow(winit::event_loop::ControlFlow::Poll); // App let mut app = unsafe { App::create(&window)? }; @@ -160,6 +162,7 @@ impl App { let loader = LibloadingLoader::new(LIBRARY)?; let entry = Entry::new(loader).map_err(|b| anyhow!("{}", b))?; let mut data = app_data::AppData::default(); + data.use_geometry_shader = false; let mut scene_handler = scene::Scene::default(); //load_model::load_model(&mut data)?; @@ -201,7 +204,7 @@ impl App { last_pos: LogicalPosition::new(-1 as f32, -1 as f32), view_direction: vertex::Vec3::new(0.0, 0.0, 0.0), cur_pos: cgmath::point3(0.0, 0.0, 0.0), - scene_handler, + scene_handler }) } @@ -393,7 +396,8 @@ impl App { 10000.0, ); - let ubo = buffer::UniformBufferObject { model, geom_rot: rot_mat4, view, proj }; + let ubo = buffer::UniformBufferObject { model, geom_rot: rot_mat4, view, proj, + use_geom_shader: [self.data.use_geometry_shader, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false]}; let memory = self.device.map_memory( self.data.uniform_buffers_memory[image_index], @@ -668,8 +672,13 @@ unsafe fn create_pipeline(device: &Device, data: &mut app_data::AppData) -> Resu .vertex_binding_descriptions(binding_descriptions) .vertex_attribute_descriptions(&attribute_descriptions); + let mut topology = vk::PrimitiveTopology::TRIANGLE_LIST; + if data.use_geometry_shader { + topology = vk::PrimitiveTopology::POINT_LIST; + } + let input_assembly_state = vk::PipelineInputAssemblyStateCreateInfo::builder() - .topology(vk::PrimitiveTopology::TRIANGLE_LIST) + .topology(topology) .primitive_restart_enable(false); let viewport = vk::Viewport::builder() @@ -736,8 +745,9 @@ unsafe fn create_pipeline(device: &Device, data: &mut app_data::AppData) -> Resu data.pipeline_layout = device.create_pipeline_layout(&layout_info, None)?; let stages = &[vert_stage, frag_stage]; - let info = vk::GraphicsPipelineCreateInfo::builder() - .stages(stages) + let stages_geom = &[vert_stage, geo_stage,frag_stage]; + + let mut info = vk::GraphicsPipelineCreateInfo::builder() .vertex_input_state(&vertex_input_state) .input_assembly_state(&input_assembly_state) .viewport_state(&viewport_state) @@ -749,6 +759,13 @@ unsafe fn create_pipeline(device: &Device, data: &mut app_data::AppData) -> Resu .render_pass(data.render_pass) .subpass(0); + if data.use_geometry_shader { + info = info.stages(stages_geom); + } + else { + info = info.stages(stages); + } + data.pipeline = device.create_graphics_pipelines( vk::PipelineCache::null(), &[info], None)?.0[0]; diff --git a/src/primitives.rs b/src/primitives.rs new file mode 100644 index 0000000..887bbb1 --- /dev/null +++ b/src/primitives.rs @@ -0,0 +1 @@ +pub mod cube; \ No newline at end of file diff --git a/src/primitives/cube.rs b/src/primitives/cube.rs new file mode 100644 index 0000000..8666888 --- /dev/null +++ b/src/primitives/cube.rs @@ -0,0 +1,130 @@ +use vulkanalia::prelude::v1_0::*; +use cgmath::{vec2, vec3, Matrix, SquareMatrix}; +use crate::vertex; +use crate::scene::Scene; + +#[derive(Clone, Debug)] +pub struct Cube{ + pub pos: vertex::Vec3, + pub color: vertex::Vec3, + pub tex_coord: vertex::Vec2 +} + +impl Cube { + pub fn draw(& self, topology: &vk::PrimitiveTopology, start_index: usize, scene: &mut Scene) { + if *topology == vk::PrimitiveTopology::TRIANGLE_LIST { + // 0 top left far + scene.vertices.push(vertex::Vertex::new( + vec3(self.pos.x as f32 - 0.5, self.pos.y as f32 + 0.5, self.pos.z as f32 + 0.5), + self.color, + self.tex_coord + )); + // 1 top right far + scene.vertices.push(vertex::Vertex::new( + vec3(self.pos.x as f32 + 0.5, self.pos.y as f32 + 0.5, self.pos.z as f32 + 0.5), + self.color, + self.tex_coord + )); + // 2 top left near + scene.vertices.push(vertex::Vertex::new( + vec3(self.pos.x as f32 - 0.5, self.pos.y as f32 - 0.5, self.pos.z as f32 + 0.5), + self.color, + self.tex_coord + )); + // 3 top right near + scene.vertices.push(vertex::Vertex::new( + vec3(self.pos.x as f32 + 0.5, self.pos.y as f32 - 0.5, self.pos.z as f32 + 0.5), + self.color, + self.tex_coord + )); + + // 4 bottom left far + scene.vertices.push(vertex::Vertex::new( + vec3(self.pos.x as f32 - 0.5, self.pos.y as f32 + 0.5, self.pos.z as f32 - 0.5), + self.color, + self.tex_coord + )); + // 5 bottom right far + scene.vertices.push(vertex::Vertex::new( + vec3(self.pos.x as f32 + 0.5, self.pos.y as f32 + 0.5, self.pos.z as f32 - 0.5), + self.color, + self.tex_coord + )); + // 6 bottom left near + scene.vertices.push(vertex::Vertex::new( + vec3(self.pos.x as f32 - 0.5, self.pos.y as f32 - 0.5, self.pos.z as f32 - 0.5), + self.color, + self.tex_coord + )); + // 7 bottom right near + scene.vertices.push(vertex::Vertex::new( + vec3(self.pos.x as f32 + 0.5, self.pos.y as f32 - 0.5, self.pos.z as f32 - 0.5), + self.color, + self.tex_coord + )); + + + // top + scene.indices.push(start_index as u32 + 3); + scene.indices.push(start_index as u32 + 0); + scene.indices.push(start_index as u32 + 2); + + scene.indices.push(start_index as u32 + 3); + scene.indices.push(start_index as u32 + 1); + scene.indices.push(start_index as u32 + 0); + + // bottom + scene.indices.push(start_index as u32 + 6); + scene.indices.push(start_index as u32 + 4); + scene.indices.push(start_index as u32 + 7); + + scene.indices.push(start_index as u32 + 4); + scene.indices.push(start_index as u32 + 5); + scene.indices.push(start_index as u32 + 7); + + // left + scene.indices.push(start_index as u32 + 0); + scene.indices.push(start_index as u32 + 4); + scene.indices.push(start_index as u32 + 2); + + scene.indices.push(start_index as u32 + 6); + scene.indices.push(start_index as u32 + 2); + scene.indices.push(start_index as u32 + 4); + + // right + scene.indices.push(start_index as u32 + 1); + scene.indices.push(start_index as u32 + 3); + scene.indices.push(start_index as u32 + 5); + + scene.indices.push(start_index as u32 + 5); + scene.indices.push(start_index as u32 + 3); + scene.indices.push(start_index as u32 + 7); + + // near + scene.indices.push(start_index as u32 + 6); + scene.indices.push(start_index as u32 + 3); + scene.indices.push(start_index as u32 + 2); + + scene.indices.push(start_index as u32 + 3); + scene.indices.push(start_index as u32 + 6); + scene.indices.push(start_index as u32 + 7); + + // far + scene.indices.push(start_index as u32 + 0); + scene.indices.push(start_index as u32 + 1); + scene.indices.push(start_index as u32 + 4); + + scene.indices.push(start_index as u32 + 5); + scene.indices.push(start_index as u32 + 4); + scene.indices.push(start_index as u32 + 1); + } + if *topology == vk::PrimitiveTopology::POINT_LIST { + scene.vertices.push(vertex::Vertex::new( + self.pos, + self.color, + self.tex_coord + )); + scene.indices.push(start_index as u32); + } + } +} \ No newline at end of file diff --git a/src/scene.rs b/src/scene.rs index e553bb3..6f08f70 100644 --- a/src/scene.rs +++ b/src/scene.rs @@ -1,3 +1,5 @@ +use std::thread::yield_now; + use vulkanalia::prelude::v1_0::*; use anyhow::{anyhow, Result}; @@ -6,6 +8,7 @@ use cgmath::{vec2, vec3, Matrix, SquareMatrix}; use crate::app_data::AppData; use crate::buffer; use crate::vertex; +use crate::primitives::cube::Cube; #[derive(Clone, Debug, Default)] pub struct Scene { @@ -28,110 +31,13 @@ impl Scene { for x_index in -grid_size..grid_size { for y_index in -grid_size..grid_size { let index = self.vertices.len(); - // 0 top left far - self.vertices.push(vertex::Vertex::new( - vec3(x_index as f32 - 0.5, y_index as f32 + 0.5, 0.5), - vec3(0.0, 1.0, 0.0), - vec2(0.0, 0.0) - )); - // 1 top right far - self.vertices.push(vertex::Vertex::new( - vec3(x_index as f32 + 0.5, y_index as f32 + 0.5, 0.5), - vec3(0.0, 1.0, 0.0), - vec2(0.0, 0.0) - )); - // 2 top left near - self.vertices.push(vertex::Vertex::new( - vec3(x_index as f32 - 0.5, y_index as f32 - 0.5, 0.5), - vec3(0.0, 1.0, 0.0), - vec2(0.0, 0.0) - )); - // 3 top right near - self.vertices.push(vertex::Vertex::new( - vec3(x_index as f32 + 0.5, y_index as f32 - 0.5, 0.5), - vec3(0.0, 1.0, 0.0), - vec2(0.0, 0.0) - )); + let cube = Cube { + pos: vec3(x_index as f32, y_index as f32, 0.0), + color: vec3(0.0, 1.0, 0.0), + tex_coord: vec2(0.0, 0.0) + }; - // 4 bottom left far - self.vertices.push(vertex::Vertex::new( - vec3(x_index as f32 - 0.5, y_index as f32 + 0.5, -0.5), - vec3(0.0, 1.0, 0.0), - vec2(0.0, 0.0) - )); - // 5 bottom right far - self.vertices.push(vertex::Vertex::new( - vec3(x_index as f32 + 0.5, y_index as f32 + 0.5, -0.5), - vec3(0.0, 1.0, 0.0), - vec2(0.0, 0.0) - )); - // 6 bottom left near - self.vertices.push(vertex::Vertex::new( - vec3(x_index as f32 - 0.5, y_index as f32 - 0.5, -0.5), - vec3(0.0, 1.0, 0.0), - vec2(0.0, 0.0) - )); - // 7 bottom right near - self.vertices.push(vertex::Vertex::new( - vec3(x_index as f32 + 0.5, y_index as f32 - 0.5, -0.5), - vec3(0.0, 1.0, 0.0), - vec2(0.0, 0.0) - )); - - - // top - self.indices.push(index as u32 + 3); - self.indices.push(index as u32 + 0); - self.indices.push(index as u32 + 2); - - self.indices.push(index as u32 + 3); - self.indices.push(index as u32 + 1); - self.indices.push(index as u32 + 0); - - // bottom - self.indices.push(index as u32 + 6); - self.indices.push(index as u32 + 4); - self.indices.push(index as u32 + 7); - - self.indices.push(index as u32 + 4); - self.indices.push(index as u32 + 5); - self.indices.push(index as u32 + 7); - - // left - self.indices.push(index as u32 + 0); - self.indices.push(index as u32 + 4); - self.indices.push(index as u32 + 2); - - self.indices.push(index as u32 + 6); - self.indices.push(index as u32 + 2); - self.indices.push(index as u32 + 4); - - // right - self.indices.push(index as u32 + 1); - self.indices.push(index as u32 + 3); - self.indices.push(index as u32 + 5); - - self.indices.push(index as u32 + 5); - self.indices.push(index as u32 + 3); - self.indices.push(index as u32 + 7); - - // near - self.indices.push(index as u32 + 6); - self.indices.push(index as u32 + 3); - self.indices.push(index as u32 + 2); - - self.indices.push(index as u32 + 3); - self.indices.push(index as u32 + 6); - self.indices.push(index as u32 + 7); - - // far - self.indices.push(index as u32 + 0); - self.indices.push(index as u32 + 1); - self.indices.push(index as u32 + 4); - - self.indices.push(index as u32 + 5); - self.indices.push(index as u32 + 4); - self.indices.push(index as u32 + 1); + cube.draw(&vk::PrimitiveTopology::TRIANGLE_LIST, index, self); } }