Simplify example memory selection (#329)

According to specs for Device Memory (section 10.2), memory types are
topologically sorted by their property flags so the two-pass memory
type selection is unnecessary.

Co-authored-by: Steve Wooster <s.f.m.wooster@gmail.com>
This commit is contained in:
Steve Wooster 2020-10-19 03:11:57 -07:00 committed by GitHub
parent ef8b5490c5
commit 61476184e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -122,34 +122,15 @@ pub fn find_memorytype_index(
memory_prop: &vk::PhysicalDeviceMemoryProperties,
flags: vk::MemoryPropertyFlags,
) -> 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;
for (index, ref memory_type) in memory_prop.memory_types.iter().enumerate() {
if memory_type_bits & 1 == 1 && f(memory_type.property_flags, flags) {
return Some(index as u32);
}
memory_type_bits >>= 1;
}
None
memory_prop
.memory_types
.iter()
.enumerate()
.find(|(index, memory_type)| {
(1 << index) & memory_req.memory_type_bits != 0
&& memory_type.property_flags & flags == flags
})
.map(|(index, _memory_type)| index as _)
}
pub struct ExampleBase {