makes geometry shader an option

This commit is contained in:
zomseffen 2024-06-02 15:25:11 +02:00
parent 5c971d0288
commit a9d4aae1bc
10 changed files with 171 additions and 109 deletions

Binary file not shown.

View file

@ -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[];

View file

@ -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() {
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;
}

Binary file not shown.

View file

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

View file

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

View file

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

1
src/primitives.rs Normal file
View file

@ -0,0 +1 @@
pub mod cube;

130
src/primitives/cube.rs Normal file
View file

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

View file

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