moves vertex and index buffer handling to scene
This commit is contained in:
parent
0b3be29525
commit
32577d548b
6 changed files with 111 additions and 79 deletions
|
@ -1,14 +1,10 @@
|
|||
use vulkanalia::prelude::v1_0::*;
|
||||
|
||||
use crate::vertex;
|
||||
use crate::scene;
|
||||
|
||||
// The Vulkan handles and associated properties used by our Vulkan app.
|
||||
// The Vulkan handles and associated properties used by our Vulkan app.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct AppData {
|
||||
|
||||
pub vertices: Vec<vertex::Vertex>,
|
||||
pub indices: Vec<u32>,
|
||||
|
||||
pub surface: vk::SurfaceKHR,
|
||||
pub messenger: vk::DebugUtilsMessengerEXT,
|
||||
pub physical_device: vk::PhysicalDevice,
|
||||
|
@ -35,12 +31,6 @@ pub struct AppData {
|
|||
|
||||
pub in_flight_fences: Vec<vk::Fence>,
|
||||
pub images_in_flight: Vec<vk::Fence>,
|
||||
|
||||
pub vertex_buffer: vk::Buffer,
|
||||
pub vertex_buffer_memory: vk::DeviceMemory,
|
||||
|
||||
pub index_buffer: vk::Buffer,
|
||||
pub index_buffer_memory: vk::DeviceMemory,
|
||||
|
||||
pub uniform_buffers: Vec<vk::Buffer>,
|
||||
pub uniform_buffers_memory: Vec<vk::DeviceMemory>,
|
||||
|
|
|
@ -47,9 +47,10 @@ pub unsafe fn create_buffer(
|
|||
pub unsafe fn create_vertex_buffer(
|
||||
instance: &Instance,
|
||||
device: &Device,
|
||||
data: &mut app_data::AppData,
|
||||
) -> Result<()> {
|
||||
let size = (size_of::<vertex::Vertex>() * data.vertices.len()) as u64;
|
||||
data: &app_data::AppData,
|
||||
vertices: &Vec<vertex::Vertex>
|
||||
) -> Result<(vk::Buffer, vk::DeviceMemory)> {
|
||||
let size = (size_of::<vertex::Vertex>() * vertices.len()) as u64;
|
||||
|
||||
let (staging_buffer, staging_buffer_memory) = create_buffer(
|
||||
instance,
|
||||
|
@ -67,7 +68,7 @@ pub unsafe fn create_vertex_buffer(
|
|||
vk::MemoryMapFlags::empty(),
|
||||
)?;
|
||||
|
||||
memcpy(data.vertices.as_ptr(), memory.cast(), data.vertices.len());
|
||||
memcpy(vertices.as_ptr(), memory.cast(), vertices.len());
|
||||
|
||||
device.unmap_memory(staging_buffer_memory);
|
||||
|
||||
|
@ -80,15 +81,15 @@ pub unsafe fn create_vertex_buffer(
|
|||
vk::MemoryPropertyFlags::DEVICE_LOCAL,
|
||||
)?;
|
||||
|
||||
data.vertex_buffer = vertex_buffer;
|
||||
data.vertex_buffer_memory = vertex_buffer_memory;
|
||||
let vertex_buffer: vk::Buffer = vertex_buffer;
|
||||
let vertex_buffer_memory: vk::DeviceMemory = vertex_buffer_memory;
|
||||
|
||||
copy_buffer(device, data, staging_buffer, vertex_buffer, size)?;
|
||||
|
||||
device.destroy_buffer(staging_buffer, None);
|
||||
device.free_memory(staging_buffer_memory, None);
|
||||
|
||||
Ok(())
|
||||
Ok((vertex_buffer, vertex_buffer_memory))
|
||||
}
|
||||
|
||||
pub unsafe fn get_memory_type_index(
|
||||
|
@ -128,9 +129,10 @@ pub unsafe fn copy_buffer(
|
|||
pub unsafe fn create_index_buffer(
|
||||
instance: &Instance,
|
||||
device: &Device,
|
||||
data: &mut app_data::AppData,
|
||||
) -> Result<()> {
|
||||
let size = (size_of::<u32>() * data.indices.len()) as u64;
|
||||
data: &app_data::AppData,
|
||||
indices: &Vec<u32>,
|
||||
) -> Result<(vk::Buffer, vk::DeviceMemory)> {
|
||||
let size = (size_of::<u32>() * indices.len()) as u64;
|
||||
|
||||
let (staging_buffer, staging_buffer_memory) = create_buffer(
|
||||
instance,
|
||||
|
@ -148,7 +150,7 @@ pub unsafe fn create_index_buffer(
|
|||
vk::MemoryMapFlags::empty(),
|
||||
)?;
|
||||
|
||||
memcpy(data.indices.as_ptr(), memory.cast(), data.indices.len());
|
||||
memcpy(indices.as_ptr(), memory.cast(), indices.len());
|
||||
|
||||
device.unmap_memory(staging_buffer_memory);
|
||||
|
||||
|
@ -161,15 +163,15 @@ pub unsafe fn create_index_buffer(
|
|||
vk::MemoryPropertyFlags::DEVICE_LOCAL,
|
||||
)?;
|
||||
|
||||
data.index_buffer = index_buffer;
|
||||
data.index_buffer_memory = index_buffer_memory;
|
||||
let index_buffer = index_buffer;
|
||||
let index_buffer_memory = index_buffer_memory;
|
||||
|
||||
copy_buffer(device, data, staging_buffer, index_buffer, size)?;
|
||||
|
||||
device.destroy_buffer(staging_buffer, None);
|
||||
device.free_memory(staging_buffer_memory, None);
|
||||
|
||||
Ok(())
|
||||
Ok((index_buffer, index_buffer_memory))
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
|
|
|
@ -5,6 +5,7 @@ use vulkanalia::prelude::v1_0::*;
|
|||
|
||||
use crate::app_data;
|
||||
use crate::queue_family_indices;
|
||||
use crate::scene;
|
||||
|
||||
pub unsafe fn create_command_pool(
|
||||
instance: &Instance,
|
||||
|
@ -22,7 +23,7 @@ pub unsafe fn create_command_pool(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub unsafe fn create_command_buffers(device: &Device, data: &mut app_data::AppData) -> Result<()> {
|
||||
pub unsafe fn create_command_buffers(device: &Device, data: &mut app_data::AppData, scene_handler: &scene::Scene) -> Result<()> {
|
||||
let allocate_info = vk::CommandBufferAllocateInfo::builder()
|
||||
.command_pool(data.command_pool)
|
||||
.level(vk::CommandBufferLevel::PRIMARY)
|
||||
|
@ -69,8 +70,8 @@ pub unsafe fn create_command_buffers(device: &Device, data: &mut app_data::AppDa
|
|||
device.cmd_bind_pipeline(
|
||||
*command_buffer, vk::PipelineBindPoint::GRAPHICS, data.pipeline);
|
||||
|
||||
device.cmd_bind_vertex_buffers(*command_buffer, 0, &[data.vertex_buffer], &[0]);
|
||||
device.cmd_bind_index_buffer(*command_buffer, data.index_buffer, 0, vk::IndexType::UINT32);
|
||||
device.cmd_bind_vertex_buffers(*command_buffer, 0, &[scene_handler.vertex_buffer], &[0]);
|
||||
device.cmd_bind_index_buffer(*command_buffer, scene_handler.index_buffer, 0, vk::IndexType::UINT32);
|
||||
|
||||
device.cmd_bind_descriptor_sets(
|
||||
*command_buffer,
|
||||
|
@ -81,7 +82,7 @@ pub unsafe fn create_command_buffers(device: &Device, data: &mut app_data::AppDa
|
|||
&[],
|
||||
);
|
||||
|
||||
device.cmd_draw_indexed(*command_buffer, data.indices.len() as u32, 1, 0, 0, 0);
|
||||
device.cmd_draw_indexed(*command_buffer, scene_handler.indices.len() as u32, 1, 0, 0, 0);
|
||||
|
||||
device.cmd_end_render_pass(*command_buffer);
|
||||
|
||||
|
|
|
@ -5,11 +5,12 @@ use std::collections::HashMap;
|
|||
use std::io::BufReader;
|
||||
|
||||
use crate::app_data;
|
||||
use crate::scene;
|
||||
use crate::vertex;
|
||||
|
||||
use cgmath::{vec3, vec2};
|
||||
|
||||
pub fn load_model(data: &mut app_data::AppData) -> Result<()> {
|
||||
pub fn load_model(data: &mut app_data::AppData, scene_handler: &mut scene::Scene) -> Result<()> {
|
||||
// Model
|
||||
|
||||
let mut reader = BufReader::new(File::open("./resources/viking_room.obj").expect("file not found"));
|
||||
|
@ -46,12 +47,12 @@ pub fn load_model(data: &mut app_data::AppData) -> Result<()> {
|
|||
};
|
||||
|
||||
if let Some(index) = unique_vertices.get(&vertex) {
|
||||
data.indices.push(*index as u32);
|
||||
scene_handler.indices.push(*index as u32);
|
||||
} else {
|
||||
let index = data.vertices.len();
|
||||
let index = scene_handler.vertices.len();
|
||||
unique_vertices.insert(vertex, index);
|
||||
data.vertices.push(vertex);
|
||||
data.indices.push(index as u32);
|
||||
scene_handler.vertices.push(vertex);
|
||||
scene_handler.indices.push(index as u32);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
52
src/main.rs
52
src/main.rs
|
@ -44,6 +44,7 @@ pub mod image;
|
|||
pub mod command_buffer;
|
||||
pub mod depth_buffer;
|
||||
pub mod load_model;
|
||||
pub mod scene;
|
||||
|
||||
const PORTABILITY_MACOS_VERSION: Version = Version::new(1, 3, 216);
|
||||
const VALIDATION_ENABLED: bool =
|
||||
|
@ -150,6 +151,7 @@ struct App {
|
|||
last_pos: LogicalPosition<f32>,
|
||||
view_direction: vertex::Vec3,
|
||||
cur_pos: cgmath::Point3<f32>,
|
||||
scene_handler: scene::Scene,
|
||||
}
|
||||
|
||||
impl App {
|
||||
|
@ -158,43 +160,9 @@ impl App {
|
|||
let loader = LibloadingLoader::new(LIBRARY)?;
|
||||
let entry = Entry::new(loader).map_err(|b| anyhow!("{}", b))?;
|
||||
let mut data = app_data::AppData::default();
|
||||
let mut scene_handler = scene::Scene::default();
|
||||
|
||||
//load_model::load_model(&mut data)?;
|
||||
data.vertices.push(
|
||||
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)?;
|
||||
|
@ -218,14 +186,13 @@ impl App {
|
|||
image::create_texture_image_view(&device, &mut data)?;
|
||||
image::create_texture_sampler(&device, &mut data)?;
|
||||
|
||||
buffer::create_vertex_buffer(&instance, &device, &mut data)?;
|
||||
buffer::create_index_buffer(&instance, &device, &mut data)?;
|
||||
scene_handler.prepare_data(&instance, &device, &data);
|
||||
|
||||
buffer::create_uniform_buffers(&instance, &device, &mut data)?;
|
||||
buffer::create_descriptor_pool(&device, &mut data)?;
|
||||
buffer::create_descriptor_sets(&device, &mut data)?;
|
||||
|
||||
command_buffer::create_command_buffers(&device, &mut data)?;
|
||||
command_buffer::create_command_buffers(&device, &mut data, &scene_handler)?;
|
||||
|
||||
create_sync_objects(&device, &mut data)?;
|
||||
|
||||
|
@ -234,6 +201,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,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -316,11 +284,7 @@ impl App {
|
|||
|
||||
self.device.destroy_descriptor_set_layout(self.data.descriptor_set_layout, None);
|
||||
|
||||
self.device.destroy_buffer(self.data.index_buffer, None);
|
||||
self.device.free_memory(self.data.index_buffer_memory, None);
|
||||
|
||||
self.device.destroy_buffer(self.data.vertex_buffer, None);
|
||||
self.device.free_memory(self.data.vertex_buffer_memory, None);
|
||||
self.scene_handler.destroy(&self.device);
|
||||
|
||||
self.data.in_flight_fences
|
||||
.iter()
|
||||
|
@ -355,7 +319,7 @@ impl App {
|
|||
create_framebuffers(&self.device, &mut self.data)?;
|
||||
buffer::create_uniform_buffers(&self.instance, &self.device, &mut self.data)?;
|
||||
buffer::create_descriptor_sets(&self.device, &mut self.data)?;
|
||||
command_buffer::create_command_buffers(&self.device, &mut self.data)?;
|
||||
command_buffer::create_command_buffers(&self.device, &mut self.data, &self.scene_handler)?;
|
||||
self.data
|
||||
.images_in_flight
|
||||
.resize(self.data.swapchain_images.len(), vk::Fence::null());
|
||||
|
|
74
src/scene.rs
Normal file
74
src/scene.rs
Normal file
|
@ -0,0 +1,74 @@
|
|||
use vulkanalia::prelude::v1_0::*;
|
||||
use anyhow::{anyhow, Result};
|
||||
|
||||
use cgmath::{vec2, vec3, Matrix, SquareMatrix};
|
||||
|
||||
use crate::app_data::AppData;
|
||||
use crate::buffer;
|
||||
use crate::vertex;
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct Scene {
|
||||
pub vertices: Vec<vertex::Vertex>,
|
||||
pub indices: Vec<u32>,
|
||||
|
||||
pub vertex_buffer: vk::Buffer,
|
||||
pub vertex_buffer_memory: vk::DeviceMemory,
|
||||
|
||||
pub index_buffer: vk::Buffer,
|
||||
pub index_buffer_memory: vk::DeviceMemory,
|
||||
}
|
||||
|
||||
impl Scene {
|
||||
pub unsafe fn prepare_data(&mut self, instance: &vulkanalia::Instance, device: &vulkanalia::Device, data: &AppData) -> Result<()> {
|
||||
self.vertices.push(
|
||||
vertex::Vertex::new(vec3(8.0, 0.0, 0.0), vec3(1.0, 0.0, 0.0), vec2(0.0, 0.0))
|
||||
);
|
||||
self.indices.push(0);
|
||||
|
||||
self.vertices.push(
|
||||
vertex::Vertex::new(vec3(-8.0, 0.0, 0.0), vec3(0.0, 0.0, 1.0), vec2(0.0, 0.0))
|
||||
);
|
||||
self.indices.push(1);
|
||||
|
||||
self.vertices.push(
|
||||
vertex::Vertex::new(vec3(0.0, 8.0, 0.0), vec3(1.0, 1.0, 0.0), vec2(0.0, 0.0))
|
||||
);
|
||||
self.indices.push(2);
|
||||
|
||||
self.vertices.push(
|
||||
vertex::Vertex::new(vec3(0.0, -8.0, 0.0), vec3(0.0, 1.0, 1.0), vec2(0.0, 0.0))
|
||||
);
|
||||
self.indices.push(3);
|
||||
|
||||
let grid_size = 1;
|
||||
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 = self.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)
|
||||
);
|
||||
self.vertices.push(vert);
|
||||
self.indices.push(index as u32);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
(self.vertex_buffer, self.vertex_buffer_memory) = buffer::create_vertex_buffer(instance, device, &data, &self.vertices)?;
|
||||
(self.index_buffer, self.index_buffer_memory) = buffer::create_index_buffer(&instance, &device, &data, &self.indices)?;
|
||||
|
||||
Ok(())
|
||||
|
||||
}
|
||||
|
||||
pub unsafe fn destroy(&mut self, device: &vulkanalia::Device) {
|
||||
device.destroy_buffer(self.index_buffer, None);
|
||||
device.free_memory(self.index_buffer_memory, None);
|
||||
|
||||
device.destroy_buffer(self.vertex_buffer, None);
|
||||
device.free_memory(self.vertex_buffer_memory, None);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue