Add DeviceMemory::dedicated_alloc

This commit is contained in:
Pierre Krieger 2017-07-13 20:41:50 +02:00
parent d623663266
commit c8d48cc681
6 changed files with 81 additions and 4 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "vk-sys"
version = "0.2.5"
version = "0.2.6"
authors = ["Pierre Krieger <pierre.krieger1708@gmail.com>"]
repository = "https://github.com/tomaka/vulkano"
description = "Bindings for the Vulkan graphics API"

View File

@ -180,6 +180,8 @@ pub const STRUCTURE_TYPE_PHYSICAL_DEVICE_SPARSE_IMAGE_FORMAT_INFO_2_KHR: u32 = 1
pub const STRUCTURE_TYPE_VI_SURFACE_CREATE_INFO_NN: u32 = 1000062000;
pub const STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR: u32 = 1000080000;
pub const STRUCTURE_TYPE_DESCRIPTOR_UPDATE_TEMPLATE_CREATE_INFO_KHR: u32 = 1000085000;
pub const STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS_KHR: u32 = 1000127000;
pub const STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR: u32 = 1000127001;
pub type SystemAllocationScope = u32;
pub const SYSTEM_ALLOCATION_SCOPE_COMMAND: u32 = 0;
@ -2584,6 +2586,21 @@ pub struct DescriptorUpdateTemplateCreateInfoKHR {
pub set: u32,
}
#[repr(C)]
pub struct MemoryDedicatedRequirementsKHR {
pub sType: StructureType,
pub pNext: *const c_void,
pub prefersDedicatedAllocation: Bool32,
pub requiresDedicatedAllocation: Bool32,
}
#[repr(C)]
pub struct MemoryDedicatedAllocateInfoKHR {
pub sType: StructureType,
pub pNext: *const c_void,
pub image: Image,
pub buffer: Buffer,
}
macro_rules! ptrs {
($struct_name:ident, { $($name:ident => ($($param_n:ident: $param_ty:ty),*) -> $ret:ty,)+ }) => (

View File

@ -15,4 +15,4 @@ fnv = "1.0.5"
shared_library = "0.1.5"
smallvec = "0.3.1"
lazy_static = "0.2.2"
vk-sys = { version = "0.2.5", path = "../vk-sys" }
vk-sys = { version = "0.2.6", path = "../vk-sys" }

View File

@ -356,6 +356,7 @@ device_extensions! {
khr_display_swapchain => b"VK_KHR_display_swapchain",
khr_sampler_mirror_clamp_to_edge => b"VK_KHR_sampler_mirror_clamp_to_edge",
khr_maintenance1 => b"VK_KHR_maintenance1",
khr_dedicated_allocation => b"VK_KHR_dedicated_allocation",
}
/// Error that can happen when loading the list of layers.

View File

@ -25,6 +25,7 @@ use device::Device;
use device::DeviceOwned;
use instance::MemoryType;
use memory::Content;
use memory::DedicatedAlloc;
use vk;
/// Represents memory that has been allocated.
@ -63,6 +64,20 @@ impl DeviceMemory {
#[inline]
pub fn alloc(device: Arc<Device>, memory_type: MemoryType, size: usize)
-> Result<DeviceMemory, DeviceMemoryAllocError> {
DeviceMemory::dedicated_alloc(device, memory_type, size, DedicatedAlloc::None)
}
/// Same as `alloc`, but allows specifying a resource that we will be bound to the memory.
///
/// If `resource` is different from `None`, then the returned memory must not be bound to a
/// different buffer or image.
///
/// If the `VK_KHR_dedicated_allocation` extension is enabled on the device, then it will be
/// used by this method. Otherwise the `resource` parameter will be ignored.
#[inline]
pub fn dedicated_alloc(device: Arc<Device>, memory_type: MemoryType, size: usize,
resource: DedicatedAlloc)
-> Result<DeviceMemory, DeviceMemoryAllocError> {
assert!(size >= 1);
assert_eq!(device.physical_device().internal_object(),
memory_type.physical_device().internal_object());
@ -82,9 +97,36 @@ impl DeviceMemory {
}
let vk = device.pointers();
// Decide whether we are going to pass a `vkMemoryDedicatedAllocateInfoKHR`.
let dedicated_alloc_info = if device.loaded_extensions().khr_dedicated_allocation {
match resource {
DedicatedAlloc::Buffer(buffer) => {
Some(vk::MemoryDedicatedAllocateInfoKHR {
sType: vk::STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR,
pNext: ptr::null(),
image: 0,
buffer: buffer.internal_object(),
})
},
DedicatedAlloc::Image(image) => {
Some(vk::MemoryDedicatedAllocateInfoKHR {
sType: vk::STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR,
pNext: ptr::null(),
image: image.internal_object(),
buffer: 0,
})
},
DedicatedAlloc::None => {
None
}
}
} else {
None
};
let infos = vk::MemoryAllocateInfo {
sType: vk::STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
pNext: ptr::null(),
pNext: dedicated_alloc_info.as_ref().map(|i| i as *const vk::MemoryDedicatedAllocateInfoKHR).unwrap_or(ptr::null()) as *const _,
allocationSize: size as u64,
memoryTypeIndex: memory_type.id(),
};
@ -113,12 +155,20 @@ impl DeviceMemory {
/// - Panics if `memory_type` doesn't belong to the same physical device as `device`.
/// - Panics if the memory type is not host-visible.
///
#[inline]
pub fn alloc_and_map(device: Arc<Device>, memory_type: MemoryType, size: usize)
-> Result<MappedDeviceMemory, DeviceMemoryAllocError> {
DeviceMemory::dedicated_alloc_and_map(device, memory_type, size, DedicatedAlloc::None)
}
/// Equivalent of `dedicated_alloc` for `alloc_and_map`.
pub fn dedicated_alloc_and_map(device: Arc<Device>, memory_type: MemoryType, size: usize,
resource: DedicatedAlloc)
-> Result<MappedDeviceMemory, DeviceMemoryAllocError> {
let vk = device.pointers();
assert!(memory_type.is_host_visible());
let mem = DeviceMemory::alloc(device.clone(), memory_type, size)?;
let mem = DeviceMemory::dedicated_alloc(device.clone(), memory_type, size, resource)?;
let coherent = memory_type.is_host_coherent();

View File

@ -89,6 +89,8 @@ use std::mem;
use std::os::raw::c_void;
use std::slice;
use buffer::sys::UnsafeBuffer;
use image::sys::UnsafeImage;
use vk;
pub use self::device_memory::CpuAccess;
@ -127,6 +129,13 @@ impl From<vk::MemoryRequirements> for MemoryRequirements {
}
}
#[derive(Debug, Copy, Clone)]
pub enum DedicatedAlloc<'a> {
None,
Buffer(&'a UnsafeBuffer),
Image(&'a UnsafeImage),
}
/// Trait for types of data that can be mapped.
// TODO: move to `buffer` module
pub unsafe trait Content {