Skip to content

Commit

Permalink
Change Vulkan frame image creation logic
Browse files Browse the repository at this point in the history
- Handle the case when Vulkan image needs dedicated memory
- Fixes maximbaz#81
  • Loading branch information
ar7eniyan committed Jul 27, 2023
1 parent 54874c1 commit 41d657f
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions src/frame/vulkan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,43 @@ impl Vulkan {

let frame_image = unsafe { self.device.create_image(&frame_image_create_info, None)? };

let frame_image_memory_req_info = vk::ImageMemoryRequirementsInfo2::builder()
.image(frame_image);

// Prepare the structures to get memory requirements into, then get the requirements
let mut frame_image_mem_dedicated_req = vk::MemoryDedicatedRequirements::default();
let mut frame_image_mem_req = vk::MemoryRequirements2::builder()
.push_next(&mut frame_image_mem_dedicated_req)
.build();
unsafe {
self.device.get_image_memory_requirements2(&frame_image_memory_req_info, &mut frame_image_mem_req);
}

// Construct the memory alloctation info accordind to the requirements
// If the image needs dedicated memory, add MemoryDedicatedAllocateInfo to the info chain
let mut frame_import_memory_info = vk::ImportMemoryFdInfoKHR::builder()
.handle_type(vk::ExternalMemoryHandleTypeFlags::DMA_BUF_EXT)
.fd(frame.fds[0]);

let frame_image_allocate_info = vk::MemoryAllocateInfo::builder()
.push_next(&mut frame_import_memory_info)
.allocation_size(frame.sizes[0].into())
.memory_type_index(0);
let mut frame_image_memory_dedicated_info = vk::MemoryDedicatedAllocateInfo::builder()
.image(frame_image);

let frame_image_allocate_info = {
if frame_image_mem_dedicated_req.prefers_dedicated_allocation == vk::TRUE {
vk::MemoryAllocateInfo::builder()
.push_next(&mut frame_import_memory_info)
.push_next(&mut frame_image_memory_dedicated_info)
.allocation_size(frame_image_mem_req.memory_requirements.size)
.memory_type_index(0)
} else {
vk::MemoryAllocateInfo::builder()
.push_next(&mut frame_import_memory_info)
.allocation_size(frame_image_mem_req.memory_requirements.size)
.memory_type_index(0)
}
};

// Allocate the memory and bind it to the image
let frame_image_memory = unsafe {
self.device
.allocate_memory(&frame_image_allocate_info, None)?
Expand Down

0 comments on commit 41d657f

Please sign in to comment.