From 4f409de59e4ab66239d220d90e338c252d2fc28c Mon Sep 17 00:00:00 2001 From: msiglreith Date: Thu, 21 Dec 2017 15:31:05 +0100 Subject: [PATCH] Implement vkAllocateMemory and vkFreeMemory --- libportability-gfx/Cargo.toml | 2 -- libportability-gfx/src/impls.rs | 22 ++++++++++++++++++---- libportability-gfx/src/lib.rs | 7 +------ libportability/src/lib.rs | 8 ++++++++ native/test.cpp | 7 +++++++ 5 files changed, 34 insertions(+), 12 deletions(-) diff --git a/libportability-gfx/Cargo.toml b/libportability-gfx/Cargo.toml index b9052b2..b33a7b1 100644 --- a/libportability-gfx/Cargo.toml +++ b/libportability-gfx/Cargo.toml @@ -10,12 +10,10 @@ name = "portability_gfx" lazy_static = "1.0" [dependencies.gfx-hal] -#path = "../gfx/src/hal" git = "https://github.com/gfx-rs/gfx" branch = "portable" [dependencies.gfx-backend-vulkan] -#path = "../gfx/src/backend/vulkan" git = "https://github.com/gfx-rs/gfx" branch = "portable" features = ["portable"] diff --git a/libportability-gfx/src/impls.rs b/libportability-gfx/src/impls.rs index 37f0555..14bd5d4 100644 --- a/libportability-gfx/src/impls.rs +++ b/libportability-gfx/src/impls.rs @@ -262,11 +262,25 @@ pub extern fn gfxAllocateMemory( _pAllocator: *const VkAllocationCallbacks, pMemory: *mut VkDeviceMemory, ) -> VkResult { - unimplemented!() + let info = unsafe { &*pAllocateInfo }; + let memory = gpu + .device + .allocate_memory( + hal::MemoryTypeId(info.memoryTypeIndex as _), + info.allocationSize, + ) + .unwrap(); // TODO: + + unsafe { *pMemory = Handle::new(memory); } + VkResult::VK_SUCCESS } -extern "C" { - pub fn vkFreeMemory(device: VkDevice, memory: VkDeviceMemory, - pAllocator: *const VkAllocationCallbacks); +#[inline] +pub extern fn gfxFreeMemory( + gpu: VkDevice, + memory: VkDeviceMemory, + pAllocator: *const VkAllocationCallbacks, +) { + gpu.device.free_memory(*memory.unwrap()); } extern "C" { pub fn vkMapMemory(device: VkDevice, memory: VkDeviceMemory, diff --git a/libportability-gfx/src/lib.rs b/libportability-gfx/src/lib.rs index 33b6f35..57bd3ea 100644 --- a/libportability-gfx/src/lib.rs +++ b/libportability-gfx/src/lib.rs @@ -26,6 +26,7 @@ pub type VkPhysicalDevice = Handle>; pub type VkDevice = Handle>; pub type VkCommandPool = Handle<::CommandPool>; pub type VkCommandBuffer = Handle<::CommandBuffer>; +pub type VkDeviceMemory = Handle<::Memory>; pub enum Image { Image(B::Image), @@ -512,12 +513,6 @@ pub struct VkFence_T { pub type VkFence = *mut VkFence_T; #[repr(C)] #[derive(Debug, Copy, Clone)] -pub struct VkDeviceMemory_T { - _unused: [u8; 0], -} -pub type VkDeviceMemory = *mut VkDeviceMemory_T; -#[repr(C)] -#[derive(Debug, Copy, Clone)] pub struct VkBuffer_T { _unused: [u8; 0], } diff --git a/libportability/src/lib.rs b/libportability/src/lib.rs index 15a37ac..b3e884c 100644 --- a/libportability/src/lib.rs +++ b/libportability/src/lib.rs @@ -64,6 +64,14 @@ pub extern fn vkAllocateMemory( gfxAllocateMemory(device, pAllocateInfo, pAllocator, pMemory) } #[no_mangle] +pub extern fn vkFreeMemory( + device: VkDevice, + memory: VkDeviceMemory, + pAllocator: *const VkAllocationCallbacks, +) { + gfxFreeMemory(device, memory, pAllocator) +} +#[no_mangle] pub extern fn vkBindImageMemory( device: VkDevice, image: VkImage, diff --git a/native/test.cpp b/native/test.cpp index a5beb93..36230bf 100644 --- a/native/test.cpp +++ b/native/test.cpp @@ -311,6 +311,11 @@ int main() { &mem_alloc.memoryTypeIndex); assert(pass); + VkDeviceMemory depth_memory = 0; + res = vkAllocateMemory(device, &mem_alloc, NULL, &depth_memory); + printf("\tvkAllocateMemory: res=%d\n", res); + assert(!res); + VkCommandPool cmd_pool = 0; VkCommandPoolCreateInfo cmd_pool_info = {}; cmd_pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; @@ -339,6 +344,8 @@ int main() { } + vkFreeMemory(device, depth_memory, NULL); + printf("\tvkFreeMemory\n"); for(auto view : swapchain_views) { vkDestroyImageView(device, view, NULL); printf("\tvkDestroyImageView\n");