Examples now properly free their objects

This commit is contained in:
maik klein 2016-12-29 06:02:37 +01:00
parent 7ece1e7d5a
commit 0f52807193
3 changed files with 86 additions and 58 deletions

View file

@ -830,6 +830,7 @@ fn main() {
}; };
base.swapchain_loader.queue_present_khr(base.present_queue, &present_info).unwrap(); base.swapchain_loader.queue_present_khr(base.present_queue, &present_info).unwrap();
}); });
base.device.device_wait_idle().unwrap();
for pipeline in graphics_pipelines { for pipeline in graphics_pipelines {
base.device.destroy_pipeline(pipeline, None); base.device.destroy_pipeline(pipeline, None);
@ -837,10 +838,22 @@ fn main() {
base.device.destroy_pipeline_layout(pipeline_layout, None); base.device.destroy_pipeline_layout(pipeline_layout, None);
base.device.destroy_shader_module(vertex_shader_module, None); base.device.destroy_shader_module(vertex_shader_module, None);
base.device.destroy_shader_module(fragment_shader_module, None); base.device.destroy_shader_module(fragment_shader_module, None);
base.device.free_memory(image_buffer_memory, None);
base.device.destroy_buffer(image_buffer, None);
base.device.free_memory(texture_memory, None);
base.device.destroy_image_view(tex_image_view, None);
base.device.destroy_image(texture_image, None);
base.device.free_memory(index_buffer_memory, None); base.device.free_memory(index_buffer_memory, None);
base.device.destroy_buffer(index_buffer, None); base.device.destroy_buffer(index_buffer, None);
base.device.free_memory(uniform_color_buffer_memory, None);
base.device.destroy_buffer(uniform_color_buffer, None);
base.device.free_memory(vertex_input_buffer_memory, None); base.device.free_memory(vertex_input_buffer_memory, None);
base.device.destroy_buffer(vertex_input_buffer, None); base.device.destroy_buffer(vertex_input_buffer, None);
for &descriptor_set_layout in desc_set_layouts.iter() {
base.device.destroy_descriptor_set_layout(descriptor_set_layout, None);
}
base.device.destroy_descriptor_pool(descriptor_pool, None);
base.device.destroy_sampler(sampler, None);
for framebuffer in framebuffers { for framebuffer in framebuffers {
base.device.destroy_framebuffer(framebuffer, None); base.device.destroy_framebuffer(framebuffer, None);
} }

View file

@ -481,6 +481,7 @@ fn main() {
base.swapchain_loader.queue_present_khr(base.present_queue, &present_info).unwrap(); base.swapchain_loader.queue_present_khr(base.present_queue, &present_info).unwrap();
}); });
base.device.device_wait_idle().unwrap();
for pipeline in graphics_pipelines { for pipeline in graphics_pipelines {
base.device.destroy_pipeline(pipeline, None); base.device.destroy_pipeline(pipeline, None);
} }

View file

@ -80,9 +80,9 @@ pub fn record_submit_commandbuffer<F: FnOnce(&Device, vk::CommandBuffer)>(device
#[cfg(all(unix, not(target_os = "android")))] #[cfg(all(unix, not(target_os = "android")))]
unsafe fn create_surface(instance: &Instance, unsafe fn create_surface(instance: &Instance,
entry: &Entry, entry: &Entry,
window: &winit::Window) window: &winit::Window)
-> Result<vk::SurfaceKHR, vk::Result> { -> Result<vk::SurfaceKHR, vk::Result> {
use winit::os::unix::WindowExt; use winit::os::unix::WindowExt;
let x11_display = window.get_xlib_display().unwrap(); let x11_display = window.get_xlib_display().unwrap();
let x11_window = window.get_xlib_window().unwrap(); let x11_window = window.get_xlib_window().unwrap();
@ -100,9 +100,9 @@ unsafe fn create_surface(instance: &Instance,
#[cfg(windows)] #[cfg(windows)]
unsafe fn create_surface(instance: &Instance, unsafe fn create_surface(instance: &Instance,
entry: &Entry, entry: &Entry,
window: &winit::Window) window: &winit::Window)
-> Result<vk::SurfaceKHR, vk::Result> { -> Result<vk::SurfaceKHR, vk::Result> {
use winit::os::windows::WindowExt; use winit::os::windows::WindowExt;
let hwnd = window.get_hwnd() as *mut winapi::windef::HWND__; let hwnd = window.get_hwnd() as *mut winapi::windef::HWND__;
let hinstance = unsafe { user32::GetWindow(hwnd, 0) as *const () }; let hinstance = unsafe { user32::GetWindow(hwnd, 0) as *const () };
@ -141,19 +141,45 @@ unsafe extern "system" fn vulkan_debug_callback(_: vk::DebugReportFlagsEXT,
1 1
} }
pub fn find_memorytype_index(memory_req: &vk::MemoryRequirements, pub fn find_memorytype_index(memory_req: &vk::MemoryRequirements,
memory_prop: &vk::PhysicalDeviceMemoryProperties, memory_prop: &vk::PhysicalDeviceMemoryProperties,
flags: vk::MemoryPropertyFlags) flags: vk::MemoryPropertyFlags)
-> Option<u32> { -> Option<u32> {
// Try to find an exactly matching memory flag
let best_suitable_index =
find_memorytype_index_f(memory_req,
memory_prop,
flags,
|property_flags, flags| property_flags == flags);
if best_suitable_index.is_some() {
return best_suitable_index;
}
// Otherwise find a memory flag that works
find_memorytype_index_f(memory_req,
memory_prop,
flags,
|property_flags, flags| property_flags & flags == flags)
}
pub fn find_memorytype_index_f<F: Fn(vk::MemoryPropertyFlags, vk::MemoryPropertyFlags) -> bool>
(memory_req: &vk::MemoryRequirements,
memory_prop: &vk::PhysicalDeviceMemoryProperties,
flags: vk::MemoryPropertyFlags,
f: F)
-> Option<u32> {
let mut memory_type_bits = memory_req.memory_type_bits; let mut memory_type_bits = memory_req.memory_type_bits;
for (index, ref memory_type) in memory_prop.memory_types.iter().enumerate() { for (index, ref memory_type) in memory_prop.memory_types.iter().enumerate() {
if (memory_type.property_flags & flags) == flags { if memory_type_bits & 1 == 1 {
return Some(index as u32); if f(memory_type.property_flags, flags) {
return Some(index as u32);
}
} }
memory_type_bits = memory_type_bits >> 1; memory_type_bits = memory_type_bits >> 1;
} }
None None
} }
fn resize_callback(width: u32, height: u32) { fn resize_callback(width: u32, height: u32) {
println!("Window resized to {}x{}", width, height); println!("Window resized to {}x{}", width, height);
} }
@ -254,8 +280,9 @@ impl ExampleBase {
}; };
let debug_report_loader = DebugReport::new(&entry, &instance) let debug_report_loader = DebugReport::new(&entry, &instance)
.expect("Unable to load debug report"); .expect("Unable to load debug report");
let debug_call_back = debug_report_loader.create_debug_report_callback_ext(&debug_info, None) let debug_call_back =
.unwrap(); debug_report_loader.create_debug_report_callback_ext(&debug_info, None)
.unwrap();
let surface = create_surface(&instance, &entry, &window).unwrap(); let surface = create_surface(&instance, &entry, &window).unwrap();
let pdevices = instance.enumerate_physical_devices().expect("Physical device error"); let pdevices = instance.enumerate_physical_devices().expect("Physical device error");
let surface_loader = Surface::new(&entry, &instance) let surface_loader = Surface::new(&entry, &instance)
@ -379,7 +406,8 @@ impl ExampleBase {
p_queue_family_indices: ptr::null(), p_queue_family_indices: ptr::null(),
queue_family_index_count: 0, queue_family_index_count: 0,
}; };
let swapchain = swapchain_loader.create_swapchain_khr(&swapchain_create_info, None).unwrap(); let swapchain = swapchain_loader.create_swapchain_khr(&swapchain_create_info, None)
.unwrap();
let pool_create_info = vk::CommandPoolCreateInfo { let pool_create_info = vk::CommandPoolCreateInfo {
s_type: vk::StructureType::CommandPoolCreateInfo, s_type: vk::StructureType::CommandPoolCreateInfo,
p_next: ptr::null(), p_next: ptr::null(),
@ -473,54 +501,40 @@ impl ExampleBase {
p_inheritance_info: ptr::null(), p_inheritance_info: ptr::null(),
flags: vk::COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, flags: vk::COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
}; };
device.begin_command_buffer(setup_command_buffer, &command_buffer_begin_info).unwrap(); record_submit_commandbuffer(&device,
let layout_transition_barrier = vk::ImageMemoryBarrier { setup_command_buffer,
s_type: vk::StructureType::ImageMemoryBarrier, present_queue,
p_next: ptr::null(), &[vk::PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT],
src_access_mask: Default::default(),
dst_access_mask: vk::ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
vk::ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
old_layout: vk::ImageLayout::Undefined,
new_layout: vk::ImageLayout::DepthStencilAttachmentOptimal,
src_queue_family_index: vk::VK_QUEUE_FAMILY_IGNORED,
dst_queue_family_index: vk::VK_QUEUE_FAMILY_IGNORED,
image: depth_image,
subresource_range: vk::ImageSubresourceRange {
aspect_mask: vk::IMAGE_ASPECT_DEPTH_BIT,
base_mip_level: 0,
level_count: 1,
base_array_layer: 0,
layer_count: 1,
},
};
device.cmd_pipeline_barrier(setup_command_buffer,
vk::PIPELINE_STAGE_TOP_OF_PIPE_BIT,
vk::PIPELINE_STAGE_TOP_OF_PIPE_BIT,
vk::DependencyFlags::empty(),
&[], &[],
&[], &[],
&[layout_transition_barrier]); |device, setup_command_buffer| {
let wait_stage_mask = [vk::PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT]; let layout_transition_barrier = vk::ImageMemoryBarrier {
let fence_create_info = vk::FenceCreateInfo { s_type: vk::StructureType::ImageMemoryBarrier,
s_type: vk::StructureType::FenceCreateInfo, p_next: ptr::null(),
p_next: ptr::null(), src_access_mask: Default::default(),
flags: vk::FenceCreateFlags::empty(), dst_access_mask: vk::ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
}; vk::ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
let submit_fence = device.create_fence(&fence_create_info, None).unwrap(); old_layout: vk::ImageLayout::Undefined,
let submit_info = vk::SubmitInfo { new_layout: vk::ImageLayout::DepthStencilAttachmentOptimal,
s_type: vk::StructureType::SubmitInfo, src_queue_family_index: vk::VK_QUEUE_FAMILY_IGNORED,
p_next: ptr::null(), dst_queue_family_index: vk::VK_QUEUE_FAMILY_IGNORED,
wait_semaphore_count: 0, image: depth_image,
p_wait_semaphores: ptr::null(), subresource_range: vk::ImageSubresourceRange {
signal_semaphore_count: 0, aspect_mask: vk::IMAGE_ASPECT_DEPTH_BIT,
p_signal_semaphores: ptr::null(), base_mip_level: 0,
p_wait_dst_stage_mask: wait_stage_mask.as_ptr(), level_count: 1,
command_buffer_count: 1, base_array_layer: 0,
p_command_buffers: &setup_command_buffer, layer_count: 1,
}; },
device.end_command_buffer(setup_command_buffer).unwrap(); };
device.queue_submit(present_queue, &[submit_info], submit_fence).unwrap(); device.cmd_pipeline_barrier(setup_command_buffer,
device.wait_for_fences(&[submit_fence], true, std::u64::MAX).unwrap(); vk::PIPELINE_STAGE_TOP_OF_PIPE_BIT,
vk::PIPELINE_STAGE_TOP_OF_PIPE_BIT,
vk::DependencyFlags::empty(),
&[],
&[],
&[layout_transition_barrier]);
});
let depth_image_view_info = vk::ImageViewCreateInfo { let depth_image_view_info = vk::ImageViewCreateInfo {
s_type: vk::StructureType::ImageViewCreateInfo, s_type: vk::StructureType::ImageViewCreateInfo,
p_next: ptr::null(), p_next: ptr::null(),
@ -589,9 +603,9 @@ impl Drop for ExampleBase {
self.device.device_wait_idle().unwrap(); self.device.device_wait_idle().unwrap();
self.device.destroy_semaphore(self.present_complete_semaphore, None); self.device.destroy_semaphore(self.present_complete_semaphore, None);
self.device.destroy_semaphore(self.rendering_complete_semaphore, None); self.device.destroy_semaphore(self.rendering_complete_semaphore, None);
self.device.free_memory(self.depth_image_memory, None);
self.device.destroy_image_view(self.depth_image_view, None); self.device.destroy_image_view(self.depth_image_view, None);
self.device.destroy_image(self.depth_image, None); self.device.destroy_image(self.depth_image, None);
self.device.free_memory(self.depth_image_memory, None);
for &image_view in self.present_image_views.iter() { for &image_view in self.present_image_views.iter() {
self.device.destroy_image_view(image_view, None); self.device.destroy_image_view(image_view, None);
} }