From bf1fc240119779e8667afc4d4f228e50f1ed6ef0 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Sun, 17 Mar 2024 18:46:01 +0900 Subject: [PATCH] Refactor: Split init code into function Previous implementation originally had "init_vulkan" initialization struct creation seperated into a different function, but was merged into "init_vulkan" due to memory issues. More specifically, the struct creation function used pointers to local variables that were not used until the function went out of scope, causing the local variables to be cleaned up and leaving the pointers in the "struct info" struct invalid when they were actually used in a `ffi::Vk...` call. This refactoring ensures that pointers to variables created in the mentioned function does not become invalid when the function ends by stored the pointees in `Pin>` and returning them with the "struct info" struct at the end of the mentioned function. --- src/main.rs | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index bd6aa8c..a389730 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,8 @@ mod math3d; use std::collections::HashSet; use std::ffi::{c_void, CStr, CString}; +use std::ops::Deref; +use std::pin::Pin; use math3d::Vertex; @@ -949,19 +951,8 @@ impl VulkanApp { let shader_stages: [ffi::VkPipelineShaderStageCreateInfo; 2] = [vert_shader_stage_info, frag_shader_stage_info]; - let mut vertex_input_info: ffi::VkPipelineVertexInputStateCreateInfo = - unsafe { std::mem::zeroed() }; - vertex_input_info.sType = - ffi::VkStructureType_VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; - - let bind_desc = Vertex::get_binding_description(); - let attr_descs = Vertex::get_attribute_descriptions(); - - vertex_input_info.vertexBindingDescriptionCount = 1; - vertex_input_info.vertexAttributeDescriptionCount = attr_descs.len() as u32; - - vertex_input_info.pVertexBindingDescriptions = std::ptr::addr_of!(bind_desc); - vertex_input_info.pVertexAttributeDescriptions = attr_descs.as_ptr(); + let (vertex_input_info, _bind_desc, _attr_descs) = + Self::create_vertex_input_state_info_struct()?; let mut input_assembly: ffi::VkPipelineInputAssemblyStateCreateInfo = unsafe { std::mem::zeroed() }; @@ -1729,6 +1720,31 @@ impl VulkanApp { Err(String::from("Failed to find suitable memory type!")) } + + fn create_vertex_input_state_info_struct() -> Result< + ( + ffi::VkPipelineVertexInputStateCreateInfo, + Pin>, + Pin>, + ), + String, + > { + let mut vertex_input_info: ffi::VkPipelineVertexInputStateCreateInfo = + unsafe { std::mem::zeroed() }; + vertex_input_info.sType = + ffi::VkStructureType_VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; + + let bind_desc = Box::pin(Vertex::get_binding_description()); + let attr_descs = Box::pin(Vertex::get_attribute_descriptions()); + + vertex_input_info.vertexBindingDescriptionCount = 1; + vertex_input_info.vertexAttributeDescriptionCount = attr_descs.len() as u32; + + vertex_input_info.pVertexBindingDescriptions = bind_desc.deref(); + vertex_input_info.pVertexAttributeDescriptions = attr_descs.as_ptr(); + + Ok((vertex_input_info, bind_desc, attr_descs)) + } } impl Drop for VulkanApp {