From ba02ae0901ce84a43cecea5ad29d73539fda8767 Mon Sep 17 00:00:00 2001 From: Aitor Camacho Date: Mon, 26 Aug 2024 10:33:50 +0900 Subject: [PATCH] Implement vkGetMemoryMetalHandlePropertiesEXT --- MoltenVK/MoltenVK/GPUObjects/MVKDevice.h | 14 ++++++++----- MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm | 24 +++++++++++++++++++++++ MoltenVK/MoltenVK/Vulkan/vulkan.mm | 3 ++- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKDevice.h b/MoltenVK/MoltenVK/GPUObjects/MVKDevice.h index e74ef60bb..88b9d1277 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKDevice.h +++ b/MoltenVK/MoltenVK/GPUObjects/MVKDevice.h @@ -336,31 +336,31 @@ class MVKPhysicalDevice : public MVKDispatchableVulkanAPIObject { * Returns a bit mask of all memory type indices. * Each bit [0..31] in the returned bit mask indicates a distinct memory type. */ - uint32_t getAllMemoryTypes() { return _allMemoryTypes; } + uint32_t getAllMemoryTypes() const { return _allMemoryTypes; } /** * Returns a bit mask of all memory type indices that allow host visibility to the memory. * Each bit [0..31] in the returned bit mask indicates a distinct memory type. */ - uint32_t getHostVisibleMemoryTypes() { return _hostVisibleMemoryTypes; } + uint32_t getHostVisibleMemoryTypes() const { return _hostVisibleMemoryTypes; } /** * Returns a bit mask of all memory type indices that are coherent between host and device. * Each bit [0..31] in the returned bit mask indicates a distinct memory type. */ - uint32_t getHostCoherentMemoryTypes() { return _hostCoherentMemoryTypes; } + uint32_t getHostCoherentMemoryTypes() const { return _hostCoherentMemoryTypes; } /** * Returns a bit mask of all memory type indices that do NOT allow host visibility to the memory. * Each bit [0..31] in the returned bit mask indicates a distinct memory type. */ - uint32_t getPrivateMemoryTypes() { return _privateMemoryTypes; } + uint32_t getPrivateMemoryTypes() const { return _privateMemoryTypes; } /** * Returns a bit mask of all memory type indices that are lazily allocated. * Each bit [0..31] in the returned bit mask indicates a distinct memory type. */ - uint32_t getLazilyAllocatedMemoryTypes() { return _lazilyAllocatedMemoryTypes; } + uint32_t getLazilyAllocatedMemoryTypes() const { return _lazilyAllocatedMemoryTypes; } /** Returns the external memory properties supported for buffers for the handle type. */ VkExternalMemoryProperties& getExternalBufferProperties(VkExternalMemoryHandleTypeFlagBits handleType); @@ -368,6 +368,8 @@ class MVKPhysicalDevice : public MVKDispatchableVulkanAPIObject { /** Returns the external memory properties supported for images for the handle type. */ VkExternalMemoryProperties& getExternalImageProperties(VkFormat format, VkExternalMemoryHandleTypeFlagBits handleType); + uint32_t getExternalResourceMemoryTypeBits(MTLResource_id handle) const; + #pragma mark Metal @@ -503,6 +505,8 @@ class MVKDevice : public MVKDispatchableVulkanAPIObject { /** Returns a pointer to the Vulkan instance. */ MVKInstance* getInstance() override { return _physicalDevice->_mvkInstance; } + const MVKPhysicalDevice* getPhysicalDevice() const { return _physicalDevice; } + /** Returns the name of this device. */ const char* getName() { return _physicalDevice->_properties.deviceName; } diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm b/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm index 7d3df7565..b57b2ddc1 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm +++ b/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm @@ -1395,6 +1395,30 @@ } } +uint32_t MVKPhysicalDevice::getExternalResourceMemoryTypeBits(MTLResource_id handle) const { + uint32_t memoryTypeBits = 0u; + switch (handle.storageMode) { + case MTLStorageModeShared: + memoryTypeBits = _hostCoherentMemoryTypes; + break; +#if !MVK_IOS + case MTLStorageModeManaged: + memoryTypeBits = _hostVisibleMemoryTypes; + break; +#endif + case MTLStorageModePrivate: + memoryTypeBits = _privateMemoryTypes; + break; + case MTLStorageModeMemoryless: + memoryTypeBits = _lazilyAllocatedMemoryTypes; + break; + default: + // This should never be reached, but just to be future-proof + break; + }; + return memoryTypeBits; +} + static const VkExternalFenceProperties _emptyExtFenceProps = {VK_STRUCTURE_TYPE_EXTERNAL_FENCE_PROPERTIES, nullptr, 0, 0, 0}; void MVKPhysicalDevice::getExternalFenceProperties(const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo, diff --git a/MoltenVK/MoltenVK/Vulkan/vulkan.mm b/MoltenVK/MoltenVK/Vulkan/vulkan.mm index 120a283f4..0e1323341 100644 --- a/MoltenVK/MoltenVK/Vulkan/vulkan.mm +++ b/MoltenVK/MoltenVK/Vulkan/vulkan.mm @@ -3056,7 +3056,8 @@ MVK_PUBLIC_VULKAN_SYMBOL VkResult vkGetMemoryMetalHandlePropertiesEXT( VkMemoryMetalHandlePropertiesEXT* pMemoryMetalHandleProperties) { MVKTraceVulkanCallStart(); - // TODO + MVKDevice* mvkDvc = MVKDevice::getMVKDevice(device); + pMemoryMetalHandleProperties->memoryTypeBits = mvkDvc->getPhysicalDevice()->getExternalResourceMemoryTypeBits(handle); MVKTraceVulkanCallEnd(); return VK_SUCCESS; }