Minor refactoring, extract create_buffer method

This commit is contained in:
Timothy Warren 2023-04-12 13:23:19 -04:00
parent 3dbc718446
commit f7ada5471d
3 changed files with 181 additions and 145 deletions

View File

@ -47,7 +47,8 @@ impl App {
///
/// # Safety
/// Here be Dragons
pub unsafe fn create(window: &Window) -> Result<Self> {
pub fn create(window: &Window) -> Result<Self> {
unsafe {
let loader = LibloadingLoader::new(LIBRARY)?;
let entry = Entry::new(loader).map_err(|b| anyhow!("{}", b))?;
@ -77,6 +78,7 @@ impl App {
resized: false,
})
}
}
/// Destroys our Vulkan app, in reverse order of creation
///
@ -139,7 +141,8 @@ impl App {
///
/// # Safety
/// Here be Dragons
pub unsafe fn render(&mut self, window: &Window) -> Result<()> {
pub fn render(&mut self, window: &Window) -> Result<()> {
unsafe {
let in_flight_fence = self.data.in_flight_fences[self.frame];
self.device
@ -199,6 +202,7 @@ impl App {
}
self.frame = (self.frame + 1) % MAX_FRAMES_IN_FLIGHT;
}
Ok(())
}

View File

@ -37,43 +37,47 @@ extern "system" fn debug_callback(
/// The Vulkan handles and associated properties used by our Vulkan app.
#[derive(Clone, Debug, Default)]
pub struct AppData {
pub(super) struct AppData {
// Debug
pub(crate) messenger: vk::DebugUtilsMessengerEXT,
pub(super) messenger: vk::DebugUtilsMessengerEXT,
// Surface
pub(crate) surface: vk::SurfaceKHR,
pub(super) surface: vk::SurfaceKHR,
// Physical Device / Logical Device
pub(crate) physical_device: vk::PhysicalDevice,
pub(crate) graphics_queue: vk::Queue,
pub(crate) present_queue: vk::Queue,
pub(super) physical_device: vk::PhysicalDevice,
pub(super) graphics_queue: vk::Queue,
pub(super) present_queue: vk::Queue,
// Swapchain
pub(crate) swapchain_format: vk::Format,
pub(crate) swapchain_extent: vk::Extent2D,
pub(crate) swapchain: vk::SwapchainKHR,
pub(crate) swapchain_images: Vec<vk::Image>,
pub(crate) swapchain_image_views: Vec<vk::ImageView>,
pub(super) swapchain_format: vk::Format,
pub(super) swapchain_extent: vk::Extent2D,
pub(super) swapchain: vk::SwapchainKHR,
pub(super) swapchain_images: Vec<vk::Image>,
pub(super) swapchain_image_views: Vec<vk::ImageView>,
// Pipeline
pub(crate) render_pass: vk::RenderPass,
pub(crate) pipeline_layout: vk::PipelineLayout,
pub(crate) pipeline: vk::Pipeline,
pub(super) render_pass: vk::RenderPass,
pub(super) pipeline_layout: vk::PipelineLayout,
pub(super) pipeline: vk::Pipeline,
// Framebuffers
pub(crate) framebuffers: Vec<vk::Framebuffer>,
pub(super) framebuffers: Vec<vk::Framebuffer>,
// Command Pool
pub(crate) command_pool: vk::CommandPool,
pub(super) command_pool: vk::CommandPool,
// Buffers
pub(crate) vertex_buffer: vk::Buffer,
pub(crate) vertex_buffer_memory: vk::DeviceMemory,
pub(super) vertex_buffer: vk::Buffer,
pub(super) vertex_buffer_memory: vk::DeviceMemory,
// Command Buffers
pub(crate) command_buffers: Vec<vk::CommandBuffer>,
pub(super) command_buffers: Vec<vk::CommandBuffer>,
// Sync Objects
pub(crate) image_available_semaphores: Vec<vk::Semaphore>,
pub(crate) render_finished_semaphores: Vec<vk::Semaphore>,
pub(crate) in_flight_fences: Vec<vk::Fence>,
pub(crate) images_in_flight: Vec<vk::Fence>,
pub(super) image_available_semaphores: Vec<vk::Semaphore>,
pub(super) render_finished_semaphores: Vec<vk::Semaphore>,
pub(super) in_flight_fences: Vec<vk::Fence>,
pub(super) images_in_flight: Vec<vk::Fence>,
}
impl AppData {
pub unsafe fn create_instance(&mut self, window: &Window, entry: &Entry) -> Result<Instance> {
pub(super) unsafe fn create_instance(
&mut self,
window: &Window,
entry: &Entry,
) -> Result<Instance> {
// Application Info
let application_info = vk::ApplicationInfo::builder()
.application_name(b"Vulkan Tutorial\0")
@ -534,6 +538,64 @@ impl AppData {
Ok(())
}
//================================================
// Buffers
//================================================
pub(super) unsafe fn create_buffer(
&self,
instance: &Instance,
device: &Device,
size: vk::DeviceSize,
usage: vk::BufferUsageFlags,
properties: vk::MemoryPropertyFlags,
) -> Result<(vk::Buffer, vk::DeviceMemory)> {
let buffer_info = vk::BufferCreateInfo::builder()
.size(size)
.usage(usage)
.sharing_mode(vk::SharingMode::EXCLUSIVE);
let buffer = device.create_buffer(&buffer_info, None)?;
let requirements = device.get_buffer_memory_requirements(buffer);
let memory_info = vk::MemoryAllocateInfo::builder()
.allocation_size(requirements.size)
.memory_type_index(self.get_memory_type_index(instance, properties, requirements)?);
let buffer_memory = device.allocate_memory(&memory_info, None)?;
device.bind_buffer_memory(buffer, buffer_memory, 0)?;
Ok((buffer, buffer_memory))
}
pub(super) unsafe fn create_vertex_buffer(
&mut self,
instance: &Instance,
device: &Device,
) -> Result<()> {
let size = (size_of::<Vertex>() * VERTICES.len()) as u64;
let (vertex_buffer, vertex_buffer_memory) = self.create_buffer(
instance,
device,
size,
vk::BufferUsageFlags::VERTEX_BUFFER,
vk::MemoryPropertyFlags::HOST_COHERENT | vk::MemoryPropertyFlags::HOST_VISIBLE,
)?;
self.vertex_buffer = vertex_buffer;
self.vertex_buffer_memory = vertex_buffer_memory;
let memory =
device.map_memory(vertex_buffer_memory, 0, size, vk::MemoryMapFlags::empty())?;
memcpy(VERTICES.as_ptr(), memory.cast(), VERTICES.len());
device.unmap_memory(vertex_buffer_memory);
Ok(())
}
//================================================
// Command Buffers
//================================================
@ -590,6 +652,10 @@ impl AppData {
Ok(())
}
//================================================
// Sync Objects
//================================================
pub(super) unsafe fn create_sync_objects(&mut self, device: &Device) -> Result<()> {
let semaphore_info = vk::SemaphoreCreateInfo::builder();
let fence_info = vk::FenceCreateInfo::builder().flags(vk::FenceCreateFlags::SIGNALED);
@ -613,43 +679,9 @@ impl AppData {
Ok(())
}
pub(super) unsafe fn create_vertex_buffer(
&mut self,
instance: &Instance,
device: &Device,
) -> Result<()> {
let buffer_info = vk::BufferCreateInfo::builder()
.size((size_of::<Vertex>() * VERTICES.len()) as u64)
.usage(vk::BufferUsageFlags::VERTEX_BUFFER)
.sharing_mode(vk::SharingMode::EXCLUSIVE);
self.vertex_buffer = device.create_buffer(&buffer_info, None)?;
let requirements = device.get_buffer_memory_requirements(self.vertex_buffer);
let memory_info = vk::MemoryAllocateInfo::builder()
.allocation_size(requirements.size)
.memory_type_index(self.get_memory_type_index(
instance,
vk::MemoryPropertyFlags::HOST_COHERENT | vk::MemoryPropertyFlags::HOST_VISIBLE,
requirements,
)?);
self.vertex_buffer_memory = device.allocate_memory(&memory_info, None)?;
device.bind_buffer_memory(self.vertex_buffer, self.vertex_buffer_memory, 0)?;
let memory = device.map_memory(
self.vertex_buffer_memory,
0,
buffer_info.size,
vk::MemoryMapFlags::empty(),
)?;
memcpy(VERTICES.as_ptr(), memory.cast(), VERTICES.len());
device.unmap_memory(self.vertex_buffer_memory);
Ok(())
}
//================================================
// Shared (Other)
//================================================
unsafe fn get_memory_type_index(
&self,

View File

@ -19,7 +19,7 @@ pub fn run() -> Result<()> {
.build(&event_loop)?;
// App
let mut app = unsafe { App::create(&window)? };
let mut app = App::create(&window)?;
let mut destroying = false;
let mut minimized = false;
event_loop.run(move |event, _, control_flow| {
@ -27,7 +27,7 @@ pub fn run() -> Result<()> {
match event {
// Render a frame if our Vulkan app is not being destroyed
Event::MainEventsCleared if !destroying && !minimized => {
unsafe { app.render(&window) }.unwrap()
app.render(&window).unwrap()
}
// Let the app know it has been resized
Event::WindowEvent {