From a9d4aae1bc50b115fd26030e333edd5b6f3ac6e6 Mon Sep 17 00:00:00 2001 From: zomseffen Date: Sun, 2 Jun 2024 15:25:11 +0200 Subject: [PATCH] makes geometry shader an option --- shaders/geo.spv | Bin 12612 -> 12712 bytes shaders/shader.geom | 1 + shaders/shader.vert | 7 ++- shaders/vert.spv | Bin 1992 -> 2524 bytes src/app_data.rs | 1 + src/buffer.rs | 1 + src/main.rs | 27 +++++++-- src/primitives.rs | 1 + src/primitives/cube.rs | 130 +++++++++++++++++++++++++++++++++++++++++ src/scene.rs | 112 +++-------------------------------- 10 files changed, 171 insertions(+), 109 deletions(-) create mode 100644 src/primitives.rs create mode 100644 src/primitives/cube.rs diff --git a/shaders/geo.spv b/shaders/geo.spv index 14191492af6057ada65ec7bb46d38d4a497c703b..b9be273a3f47f39536c3a8e94f7a7cd26eaa61a5 100644 GIT binary patch literal 12712 zcmZ{q2bf(|6@_2Q3?Y!vi%2j5u>bSzyz|in`d(GtVS#9g)q`KkJH6v3)xuB1Vyyrgdbq;F`uNxg3Zx2kY z85|iLpJ=tz=cBJ{kF6gVADbMU93Jx~`&wJHwr5;5I=psleEqRgYuC2NSFS#T^_kJv z&5>@c9~)|qF!uAudi{)R0jF!dzq5whXJ<`cH;j*+(P+(j?s;mWJg^%%IX^*}Ipv3_u5VD;ddGrQWJXw6N9*55h+ZQaPg3GMNd+vAh%bD-UQs{1iF zHZk12<9_Z}y}N();nB$xhtI=l=QKI*fjLV?hBqu7o|qgQUDKY4dPNZq~L zz{2hF+u6kdMm(YFYvBZ7m$(?sJ5!X_y=lPxI_2kamNW?s~ zdY=7|=gFOS9TD@?HqUGB$LG;H8*v$3aW;1qHy2K@_m@$xFxYOcrN2YVi)S%t6VR3XKPs(z5MhmB&e3ZvV)R;GBj!FB+-hw+ zD(3DDQ;YeI)_VVsAl$c}?9b?R)b`+;IDj#HH)pu!fsB5)=JiAFdtS}!djAIzeu5ti z&iTXOdg40vUcHX{IRf3B`*GdJGP+lF*Of0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(#`J`(j27?bw|0z067YRygCE!v3v_FH8Oa#&WcY literal 1992 zcmZ9LTTj$r5QZzTfG8j$2NiJv@q{ACL5%T;X1Uk|4C?I$b_@APx80O3qF4L{{YA!K z<&BB&({?tMlG&MgXWnn-oB8POn(Ont3*IGf(0lG>--y>G#1qz=>PmIDx_F#47FXAn z$Qbgv(?oKHmBrU1Y3{iYdr&ejA$eJ{CV3|5)?b(OZxMUE0k2Z6RkpV))ep6Lt5rKr zz6E|QaG_soxY(~J?!+grzmVs`deCkZ6H3Y`)-I`MIyww$JAVAdj}!k?uFwp}CKesL z#6_XdzBHe^q__){UH4s;;bbv-R%|nHt@rLY*$eA_C&na*wS%A&E9suhfUl%8Mx;N7 z?jVYr@7f0ke!RVZr2FZNQW_(#88!Ssnm)--`|+b@Eshf6mP;=u&i`7(=%E!yM_|+* zm2U4xvP7HDeLGaj_az6Q|n^6=uQXV^WVF&WIgl604 ztz@5J^bdwMjNZZM*D!1_c34pao4hR{%z>{YA>aJD6#XC5BlfPOc^QwoMIJu9HxfGw z>^zU!MIJu9Gl}uQ&hxlOk%tfOm&ABr#^bKshqEfWD?6K&@R>i8xv4)dd%EDW!=k^O z``igW`y*~XyThm6sD9Bi>VclH$7S_Omn9S8( 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); } }