makes geometry shader an option
This commit is contained in:
parent
5c971d0288
commit
a9d4aae1bc
10 changed files with 171 additions and 109 deletions
BIN
shaders/geo.spv
BIN
shaders/geo.spv
Binary file not shown.
|
@ -8,6 +8,7 @@ layout(binding = 0) uniform UniformBufferObject {
|
||||||
mat4 geom_rot;
|
mat4 geom_rot;
|
||||||
mat4 view;
|
mat4 view;
|
||||||
mat4 proj;
|
mat4 proj;
|
||||||
|
bool[16] use_geom_shader;
|
||||||
} ubo;
|
} ubo;
|
||||||
|
|
||||||
layout(location = 0) in vec3 geoColor[];
|
layout(location = 0) in vec3 geoColor[];
|
||||||
|
|
|
@ -5,6 +5,7 @@ layout(binding = 0) uniform UniformBufferObject {
|
||||||
mat4 geom_rot;
|
mat4 geom_rot;
|
||||||
mat4 view;
|
mat4 view;
|
||||||
mat4 proj;
|
mat4 proj;
|
||||||
|
bool[16] use_geom_shader;
|
||||||
} ubo;
|
} ubo;
|
||||||
|
|
||||||
|
|
||||||
|
@ -16,7 +17,11 @@ layout(location = 0) out vec3 geoColor;
|
||||||
layout(location = 1) out vec2 geoTexCoord;
|
layout(location = 1) out vec2 geoTexCoord;
|
||||||
|
|
||||||
void main() {
|
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;
|
geoColor = inColor;
|
||||||
geoTexCoord = inTexCoord;
|
geoTexCoord = inTexCoord;
|
||||||
}
|
}
|
BIN
shaders/vert.spv
BIN
shaders/vert.spv
Binary file not shown.
|
@ -52,4 +52,5 @@ pub struct AppData {
|
||||||
pub color_image: vk::Image,
|
pub color_image: vk::Image,
|
||||||
pub color_image_memory: vk::DeviceMemory,
|
pub color_image_memory: vk::DeviceMemory,
|
||||||
pub color_image_view: vk::ImageView,
|
pub color_image_view: vk::ImageView,
|
||||||
|
pub use_geometry_shader: bool,
|
||||||
}
|
}
|
|
@ -181,6 +181,7 @@ pub struct UniformBufferObject {
|
||||||
pub geom_rot: Mat4,
|
pub geom_rot: Mat4,
|
||||||
pub view: Mat4,
|
pub view: Mat4,
|
||||||
pub proj: Mat4,
|
pub proj: Mat4,
|
||||||
|
pub use_geom_shader: [bool; 16],
|
||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe fn create_descriptor_set_layout(
|
pub unsafe fn create_descriptor_set_layout(
|
||||||
|
|
27
src/main.rs
27
src/main.rs
|
@ -45,6 +45,7 @@ pub mod command_buffer;
|
||||||
pub mod depth_buffer;
|
pub mod depth_buffer;
|
||||||
pub mod load_model;
|
pub mod load_model;
|
||||||
pub mod scene;
|
pub mod scene;
|
||||||
|
pub mod primitives;
|
||||||
|
|
||||||
const PORTABILITY_MACOS_VERSION: Version = Version::new(1, 3, 216);
|
const PORTABILITY_MACOS_VERSION: Version = Version::new(1, 3, 216);
|
||||||
const VALIDATION_ENABLED: bool =
|
const VALIDATION_ENABLED: bool =
|
||||||
|
@ -70,6 +71,7 @@ fn main() -> Result<()> {
|
||||||
.build(&event_loop)?;
|
.build(&event_loop)?;
|
||||||
//window.set_cursor_visible(false);
|
//window.set_cursor_visible(false);
|
||||||
|
|
||||||
|
// event_loop.set_control_flow(winit::event_loop::ControlFlow::Poll);
|
||||||
// App
|
// App
|
||||||
|
|
||||||
let mut app = unsafe { App::create(&window)? };
|
let mut app = unsafe { App::create(&window)? };
|
||||||
|
@ -160,6 +162,7 @@ impl App {
|
||||||
let loader = LibloadingLoader::new(LIBRARY)?;
|
let loader = LibloadingLoader::new(LIBRARY)?;
|
||||||
let entry = Entry::new(loader).map_err(|b| anyhow!("{}", b))?;
|
let entry = Entry::new(loader).map_err(|b| anyhow!("{}", b))?;
|
||||||
let mut data = app_data::AppData::default();
|
let mut data = app_data::AppData::default();
|
||||||
|
data.use_geometry_shader = false;
|
||||||
let mut scene_handler = scene::Scene::default();
|
let mut scene_handler = scene::Scene::default();
|
||||||
|
|
||||||
//load_model::load_model(&mut data)?;
|
//load_model::load_model(&mut data)?;
|
||||||
|
@ -201,7 +204,7 @@ impl App {
|
||||||
last_pos: LogicalPosition::new(-1 as f32, -1 as f32),
|
last_pos: LogicalPosition::new(-1 as f32, -1 as f32),
|
||||||
view_direction: vertex::Vec3::new(0.0, 0.0, 0.0),
|
view_direction: vertex::Vec3::new(0.0, 0.0, 0.0),
|
||||||
cur_pos: cgmath::point3(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,
|
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(
|
let memory = self.device.map_memory(
|
||||||
self.data.uniform_buffers_memory[image_index],
|
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_binding_descriptions(binding_descriptions)
|
||||||
.vertex_attribute_descriptions(&attribute_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()
|
let input_assembly_state = vk::PipelineInputAssemblyStateCreateInfo::builder()
|
||||||
.topology(vk::PrimitiveTopology::TRIANGLE_LIST)
|
.topology(topology)
|
||||||
.primitive_restart_enable(false);
|
.primitive_restart_enable(false);
|
||||||
|
|
||||||
let viewport = vk::Viewport::builder()
|
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)?;
|
data.pipeline_layout = device.create_pipeline_layout(&layout_info, None)?;
|
||||||
|
|
||||||
let stages = &[vert_stage, frag_stage];
|
let stages = &[vert_stage, frag_stage];
|
||||||
let info = vk::GraphicsPipelineCreateInfo::builder()
|
let stages_geom = &[vert_stage, geo_stage,frag_stage];
|
||||||
.stages(stages)
|
|
||||||
|
let mut info = vk::GraphicsPipelineCreateInfo::builder()
|
||||||
.vertex_input_state(&vertex_input_state)
|
.vertex_input_state(&vertex_input_state)
|
||||||
.input_assembly_state(&input_assembly_state)
|
.input_assembly_state(&input_assembly_state)
|
||||||
.viewport_state(&viewport_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)
|
.render_pass(data.render_pass)
|
||||||
.subpass(0);
|
.subpass(0);
|
||||||
|
|
||||||
|
if data.use_geometry_shader {
|
||||||
|
info = info.stages(stages_geom);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
info = info.stages(stages);
|
||||||
|
}
|
||||||
|
|
||||||
data.pipeline = device.create_graphics_pipelines(
|
data.pipeline = device.create_graphics_pipelines(
|
||||||
vk::PipelineCache::null(), &[info], None)?.0[0];
|
vk::PipelineCache::null(), &[info], None)?.0[0];
|
||||||
|
|
||||||
|
|
1
src/primitives.rs
Normal file
1
src/primitives.rs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
pub mod cube;
|
130
src/primitives/cube.rs
Normal file
130
src/primitives/cube.rs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
112
src/scene.rs
112
src/scene.rs
|
@ -1,3 +1,5 @@
|
||||||
|
use std::thread::yield_now;
|
||||||
|
|
||||||
use vulkanalia::prelude::v1_0::*;
|
use vulkanalia::prelude::v1_0::*;
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
|
|
||||||
|
@ -6,6 +8,7 @@ use cgmath::{vec2, vec3, Matrix, SquareMatrix};
|
||||||
use crate::app_data::AppData;
|
use crate::app_data::AppData;
|
||||||
use crate::buffer;
|
use crate::buffer;
|
||||||
use crate::vertex;
|
use crate::vertex;
|
||||||
|
use crate::primitives::cube::Cube;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default)]
|
#[derive(Clone, Debug, Default)]
|
||||||
pub struct Scene {
|
pub struct Scene {
|
||||||
|
@ -28,110 +31,13 @@ impl Scene {
|
||||||
for x_index in -grid_size..grid_size {
|
for x_index in -grid_size..grid_size {
|
||||||
for y_index in -grid_size..grid_size {
|
for y_index in -grid_size..grid_size {
|
||||||
let index = self.vertices.len();
|
let index = self.vertices.len();
|
||||||
// 0 top left far
|
let cube = Cube {
|
||||||
self.vertices.push(vertex::Vertex::new(
|
pos: vec3(x_index as f32, y_index as f32, 0.0),
|
||||||
vec3(x_index as f32 - 0.5, y_index as f32 + 0.5, 0.5),
|
color: vec3(0.0, 1.0, 0.0),
|
||||||
vec3(0.0, 1.0, 0.0),
|
tex_coord: vec2(0.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)
|
|
||||||
));
|
|
||||||
|
|
||||||
// 4 bottom left far
|
cube.draw(&vk::PrimitiveTopology::TRIANGLE_LIST, index, self);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue