1M vertices 30 fps geom shader

This commit is contained in:
zomseffen 2024-05-18 10:52:47 +02:00
parent 9ad743cfa2
commit 0b3be29525
5 changed files with 188 additions and 114 deletions

View file

@ -18,6 +18,7 @@ use vulkanalia::prelude::v1_0::*;
use vulkanalia::Version;
use vulkanalia::bytecode::Bytecode;
use core::time;
use std::collections::HashSet;
use std::ffi::CStr;
use std::os::raw::c_void;
@ -27,7 +28,7 @@ use vulkanalia::vk::ExtDebugUtilsExtension;
use vulkanalia::vk::KhrSurfaceExtension;
use vulkanalia::vk::KhrSwapchainExtension;
use cgmath::{vec2, vec3, SquareMatrix};
use cgmath::{vec2, vec3, Matrix, SquareMatrix};
use std::mem::size_of;
use std::ptr::copy_nonoverlapping as memcpy;
@ -50,7 +51,9 @@ const VALIDATION_ENABLED: bool =
const VALIDATION_LAYER: vk::ExtensionName =
vk::ExtensionName::from_bytes(b"VK_LAYER_KHRONOS_validation");
const DEVICE_EXTENSIONS: &[vk::ExtensionName] = &[vk::KHR_SWAPCHAIN_EXTENSION.name];
const DEVICE_EXTENSIONS: &[vk::ExtensionName] = &[
vk::KHR_SWAPCHAIN_EXTENSION.name
];
const MAX_FRAMES_IN_FLIGHT: usize = 2;
@ -112,6 +115,15 @@ fn main() -> Result<()> {
if event.logical_key == "w" {
app.cur_pos += app.view_direction * 0.1;
}
if event.logical_key == "s" {
app.cur_pos -= app.view_direction * 0.1;
}
if event.logical_key == "a" {
app.cur_pos -= app.view_direction.cross(vertex::Vec3::new(0.0, 0.0, 1.0)) * 0.1;
}
if event.logical_key == "d" {
app.cur_pos += app.view_direction.cross(vertex::Vec3::new(0.0, 0.0, 1.0)) * 0.1;
}
},
_ => {}
}
@ -152,11 +164,38 @@ impl App {
vertex::Vertex::new(vec3(8.0, 0.0, 0.0), vec3(1.0, 0.0, 0.0), vec2(0.0, 0.0))
);
data.indices.push(0);
data.vertices.push(
vertex::Vertex::new(vec3(-8.0, 0.0, 0.0), vec3(0.0, 0.0, 1.0), vec2(0.0, 0.0))
);
data.indices.push(1);
data.vertices.push(
vertex::Vertex::new(vec3(0.0, 8.0, 0.0), vec3(1.0, 1.0, 0.0), vec2(0.0, 0.0))
);
data.indices.push(2);
data.vertices.push(
vertex::Vertex::new(vec3(0.0, -8.0, 0.0), vec3(0.0, 1.0, 1.0), vec2(0.0, 0.0))
);
data.indices.push(3);
let grid_size = 1000;
for x_index in -grid_size..grid_size {
for y_index in -grid_size..grid_size {
if !(((x_index as i32).abs() == 8 && y_index == 0) || (x_index == 0 && (y_index as i32).abs() == 8)){
let index = data.indices.len();
let vert = vertex::Vertex::new(
vec3(x_index as f32, y_index as f32, 0.0),
vec3(0.0, 1.0, 0.0),
vec2(0.0, 0.0)
);
data.vertices.push(vert);
data.indices.push(index as u32);
}
}
}
let instance = create_instance(window, &entry, &mut data)?;
data.surface = vk_window::create_surface(&instance, &window, &window)?;
pick_physical_device(&instance, &mut data)?;
@ -200,6 +239,7 @@ impl App {
/// Renders a frame for our Vulkan app.
unsafe fn render(&mut self, window: &Window) -> Result<()> {
let start_time = Instant::now();
let in_flight_fence = self.data.in_flight_fences[self.frame];
self.device.wait_for_fences(&[in_flight_fence], true, u64::MAX)?;
@ -259,6 +299,7 @@ impl App {
self.frame = (self.frame + 1) % MAX_FRAMES_IN_FLIGHT;
println!("{}", 1000000.0 / start_time.elapsed().as_micros() as f32);
Ok(())
}
@ -362,7 +403,7 @@ impl App {
let rot_mat = cgmath::Matrix3::from_angle_y(cgmath::Deg(-self.cam_angle_y)) * cgmath::Matrix3::from_angle_z(cgmath::Deg(self.cam_angle_x));
let rot_mat4 = cgmath::Matrix4::from_angle_y(cgmath::Deg(-self.cam_angle_y)) * cgmath::Matrix4::from_angle_z(cgmath::Deg(self.cam_angle_x));
self.view_direction = rot_mat * vertex::Vec3::new(1.0, 0.0, 0.0);
self.view_direction = rot_mat.transpose() * vertex::Vec3::new(1.0, 0.0, 0.0);
let model = cgmath::Matrix4::from_translation( cgmath::Point3::new(0.0, 0.0, 0.0) - self.cur_pos );
let view = buffer::Mat4::look_to_rh(
@ -385,7 +426,7 @@ impl App {
cgmath::Deg(45.0),
self.data.swapchain_extent.width as f32 / self.data.swapchain_extent.height as f32,
0.1,
10.0,
10000.0,
);
let ubo = buffer::UniformBufferObject { model, geom_rot: rot_mat4, view, proj };
@ -415,7 +456,7 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut app_data::A
.application_version(vk::make_version(1, 0, 0))
.engine_name(b"No Engine\0")
.engine_version(vk::make_version(1, 0, 0))
.api_version(vk::make_version(1, 0, 0));
.api_version(vk::make_version(1, 1, 0));
// Validation layers
@ -562,6 +603,10 @@ unsafe fn check_physical_device_extensions(
if DEVICE_EXTENSIONS.iter().all(|e| extensions.contains(e)) {
Ok(())
} else {
let missing = DEVICE_EXTENSIONS.iter().filter(|e| !extensions.contains(e)).collect::<Vec<&vk::ExtensionName>>();
for missing_extension in missing {
println!("Missing extension: {}", missing_extension);
}
Err(anyhow!(errors::SuitabilityError("Missing required device extensions.")))
}
}