Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add VK_EXT_external_memory_metal #2314

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ExternalRevisions/Vulkan-Headers_repo_revision
Original file line number Diff line number Diff line change
@@ -1 +1 @@
fc6c06ac529e4b4b6e34c17cc650a8f62dee2eb0
5a895c363dd689fdd58a4d336cceda77eca3a62f
10 changes: 7 additions & 3 deletions MoltenVK/MoltenVK/GPUObjects/MVKBuffer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,16 @@

void MVKBuffer::initExternalMemory(VkExternalMemoryHandleTypeFlags handleTypes) {
if ( !handleTypes ) { return; }
if (mvkIsOnlyAnyFlagEnabled(handleTypes, VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLBUFFER_BIT_KHR)) {
if (mvkIsOnlyAnyFlagEnabled(handleTypes, VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLBUFFER_BIT_EXT)) {
_externalMemoryHandleTypes = handleTypes;
auto& xmProps = getPhysicalDevice()->getExternalBufferProperties(VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLBUFFER_BIT_KHR);
auto& xmProps = getPhysicalDevice()->getExternalBufferProperties(VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLBUFFER_BIT_EXT);
_requiresDedicatedMemoryAllocation = _requiresDedicatedMemoryAllocation || mvkIsAnyFlagEnabled(xmProps.externalMemoryFeatures, VK_EXTERNAL_MEMORY_FEATURE_DEDICATED_ONLY_BIT);
} else if (mvkIsOnlyAnyFlagEnabled(handleTypes, VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLHEAP_BIT_EXT)) {
_externalMemoryHandleTypes = handleTypes;
auto& xmProps = getPhysicalDevice()->getExternalBufferProperties(VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLHEAP_BIT_EXT);
_requiresDedicatedMemoryAllocation = _requiresDedicatedMemoryAllocation || mvkIsAnyFlagEnabled(xmProps.externalMemoryFeatures, VK_EXTERNAL_MEMORY_FEATURE_DEDICATED_ONLY_BIT);
} else {
setConfigurationResult(reportError(VK_ERROR_FEATURE_NOT_PRESENT, "vkCreateBuffer(): Only external memory handle type VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLBUFFER_BIT_KHR is supported."));
setConfigurationResult(reportError(VK_ERROR_FEATURE_NOT_PRESENT, "vkCreateBuffer(): Only external memory handle type VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLBUFFER_BIT_EXT and VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLHEAP_BIT_EXT are supported."));
}
}

Expand Down
19 changes: 13 additions & 6 deletions MoltenVK/MoltenVK/GPUObjects/MVKDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,37 +336,39 @@ 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);

/** Returns the external memory properties supported for images for the handle type. */
VkExternalMemoryProperties& getExternalImageProperties(VkExternalMemoryHandleTypeFlagBits handleType);
VkExternalMemoryProperties& getExternalImageProperties(VkFormat format, VkExternalMemoryHandleTypeFlagBits handleType);

uint32_t getExternalResourceMemoryTypeBits(VkExternalMemoryHandleTypeFlagBits handleType, void* handle) const;


#pragma mark Metal
Expand Down Expand Up @@ -462,6 +464,7 @@ class MVKPhysicalDevice : public MVKDispatchableVulkanAPIObject {
VkExternalMemoryProperties _hostPointerExternalMemoryProperties;
VkExternalMemoryProperties _mtlBufferExternalMemoryProperties;
VkExternalMemoryProperties _mtlTextureExternalMemoryProperties;
VkExternalMemoryProperties _mtlTextureHeapExternalMemoryProperties;
id<MTLCounterSet> _timestampMTLCounterSet;
MVKSemaphoreStyle _vkSemaphoreStyle;
MTLTimestamp _prevCPUTimestamp = 0;
Expand Down Expand Up @@ -503,6 +506,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; }

Expand Down Expand Up @@ -817,6 +822,8 @@ class MVKDevice : public MVKDispatchableVulkanAPIObject {
/** Returns the Metal objects underpinning the Vulkan objects indicated in the pNext chain of pMetalObjectsInfo. */
void getMetalObjects(VkExportMetalObjectsInfoEXT* pMetalObjectsInfo);

void* getResourceIdFromHandle(const VkMemoryGetMetalHandleInfoEXT* pGetMetalHandleInfo) const;


#pragma mark Construction

Expand Down
77 changes: 69 additions & 8 deletions MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1312,7 +1312,7 @@
for (auto* nextProps = (VkBaseOutStructure*)pImageFormatProperties->pNext; nextProps; nextProps = nextProps->pNext) {
if (nextProps->sType == VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES) {
auto* pExtImgFmtProps = (VkExternalImageFormatProperties*)nextProps;
pExtImgFmtProps->externalMemoryProperties = getExternalImageProperties(pExtImgFmtInfo->handleType);
pExtImgFmtProps->externalMemoryProperties = getExternalImageProperties(pImageFormatInfo->format, pExtImgFmtInfo->handleType);
}
}
break;
Expand Down Expand Up @@ -1370,25 +1370,59 @@
case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT:
case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_MAPPED_FOREIGN_MEMORY_BIT_EXT:
return _hostPointerExternalMemoryProperties;
case VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLBUFFER_BIT_KHR:
case VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLBUFFER_BIT_EXT:
return _mtlBufferExternalMemoryProperties;
default:
return _emptyExtMemProps;
}
}

VkExternalMemoryProperties& MVKPhysicalDevice::getExternalImageProperties(VkExternalMemoryHandleTypeFlagBits handleType) {
VkExternalMemoryProperties& MVKPhysicalDevice::getExternalImageProperties(VkFormat format, VkExternalMemoryHandleTypeFlagBits handleType) {
switch (handleType) {
case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT:
case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_MAPPED_FOREIGN_MEMORY_BIT_EXT:
return _hostPointerExternalMemoryProperties;
case VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLTEXTURE_BIT_KHR:
case VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLTEXTURE_BIT_EXT:
// We cannot export images that have no Metal counterparts. This is because we are emulating them via multiple MTLTextures
// and we would require to export multiple MTLTextures. We let them export them as a heap whenever possible.
if (_pixelFormats.getChromaSubsamplingPlaneCount(format) > 1u)
return _mtlTextureHeapExternalMemoryProperties;
aitor-lunarg marked this conversation as resolved.
Show resolved Hide resolved
return _mtlTextureExternalMemoryProperties;
case VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLHEAP_BIT_EXT:
return _mtlTextureHeapExternalMemoryProperties;
default:
return _emptyExtMemProps;
}
}

uint32_t MVKPhysicalDevice::getExternalResourceMemoryTypeBits(VkExternalMemoryHandleTypeFlagBits handleType,
void* handle) const {
// MTLBuffer and MTLTextures are resources. MTLHeap is not according to Metal
const bool isResource = handleType != VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLHEAP_BIT_EXT;
const MTLStorageMode storageMode = isResource ? ((id<MTLResource>)handle).storageMode : ((id<MTLHeap>)handle).storageMode;
uint32_t memoryTypeBits = 0u;
switch (storageMode) {
case MTLStorageModeShared:
memoryTypeBits = _hostCoherentMemoryTypes;
break;
#if !MVK_IOS && !MVK_TVOS
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,
Expand Down Expand Up @@ -3325,15 +3359,23 @@ static uint32_t mvkGetEntryProperty(io_registry_entry_t entry, CFStringRef prope
// Buffers
_mtlBufferExternalMemoryProperties.externalMemoryFeatures = (VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT |
VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT);
_mtlBufferExternalMemoryProperties.exportFromImportedHandleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLBUFFER_BIT_KHR;
_mtlBufferExternalMemoryProperties.compatibleHandleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLBUFFER_BIT_KHR;
_mtlBufferExternalMemoryProperties.exportFromImportedHandleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLBUFFER_BIT_EXT;
_mtlBufferExternalMemoryProperties.compatibleHandleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLBUFFER_BIT_EXT;

// Images
_mtlTextureExternalMemoryProperties.externalMemoryFeatures = (VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT |
VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT |
VK_EXTERNAL_MEMORY_FEATURE_DEDICATED_ONLY_BIT);
_mtlTextureExternalMemoryProperties.exportFromImportedHandleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLTEXTURE_BIT_KHR;
_mtlTextureExternalMemoryProperties.compatibleHandleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLTEXTURE_BIT_KHR;
_mtlTextureExternalMemoryProperties.exportFromImportedHandleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLTEXTURE_BIT_EXT;
_mtlTextureExternalMemoryProperties.compatibleHandleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLTEXTURE_BIT_EXT;

if (_metalFeatures.placementHeaps) {
_mtlTextureHeapExternalMemoryProperties = _mtlTextureExternalMemoryProperties;
_mtlTextureHeapExternalMemoryProperties.exportFromImportedHandleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLHEAP_BIT_EXT;
_mtlTextureHeapExternalMemoryProperties.compatibleHandleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLHEAP_BIT_EXT;
} else {
_mtlTextureHeapExternalMemoryProperties = _emptyExtMemProps;
}
}

void MVKPhysicalDevice::initExtensions() {
Expand Down Expand Up @@ -4784,6 +4826,25 @@ static uint32_t mvkGetEntryProperty(io_registry_entry_t entry, CFStringRef prope
}
}

void* MVKDevice::getResourceIdFromHandle(const VkMemoryGetMetalHandleInfoEXT* pGetMetalHandleInfo) const
{
void* handle = nil;
MVKDeviceMemory* memory = (MVKDeviceMemory*)pGetMetalHandleInfo->memory;
switch (pGetMetalHandleInfo->handleType) {
case VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLBUFFER_BIT_EXT:
handle = memory->getMTLBuffer();
break;
case VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLTEXTURE_BIT_EXT:
handle = memory->getMTLTexture();
break;
case VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLHEAP_BIT_EXT:
handle = memory->getMTLHeap();
default:
break;
}
return handle;
}


#pragma mark Construction

Expand Down
22 changes: 14 additions & 8 deletions MoltenVK/MoltenVK/GPUObjects/MVKDeviceMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@

class MVKImageMemoryBinding;

// TODO: These are inoperable placeholders until VK_KHR_external_memory_metal defines them properly
static const VkExternalMemoryHandleTypeFlagBits VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLBUFFER_BIT_KHR = VK_EXTERNAL_MEMORY_HANDLE_TYPE_FLAG_BITS_MAX_ENUM;
static const VkExternalMemoryHandleTypeFlagBits VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLTEXTURE_BIT_KHR = VK_EXTERNAL_MEMORY_HANDLE_TYPE_FLAG_BITS_MAX_ENUM;


#pragma mark MVKDeviceMemory

typedef struct MVKMappedMemoryRange {
Expand Down Expand Up @@ -128,6 +123,8 @@ class MVKDeviceMemory : public MVKVulkanAPIDeviceObject {
/** Returns the Metal resource options used by this memory allocation. */
inline MTLResourceOptions getMTLResourceOptions() { return mvkMTLResourceOptions(_mtlStorageMode, _mtlCPUCacheMode); }

/** Returns the Metal texture underlying this memory allocation. */
inline id<MTLTexture> getMTLTexture() { return _mtlTexture; }

#pragma mark Construction

Expand All @@ -140,6 +137,7 @@ class MVKDeviceMemory : public MVKVulkanAPIDeviceObject {

protected:
friend class MVKBuffer;
friend class MVKImage;
friend class MVKImageMemoryBinding;
friend class MVKImagePlane;

Expand All @@ -154,14 +152,22 @@ class MVKDeviceMemory : public MVKVulkanAPIDeviceObject {
bool ensureHostMemory();
void freeHostMemory();
MVKResource* getDedicatedResource();
void initExternalMemory(VkExternalMemoryHandleTypeFlags handleTypes);
void initExternalMemory(MVKImage* dedicatedImage);

MVKSmallVector<MVKBuffer*, 4> _buffers;
MVKSmallVector<MVKImageMemoryBinding*, 4> _imageMemoryBindings;
std::mutex _rezLock;
VkDeviceSize _allocationSize = 0;
MVKMappedMemoryRange _mappedRange;
id<MTLBuffer> _mtlBuffer = nil;
// Resource object that spans the whole VkDeviceMemory or supposedly does for the user.
// Due to MVKImages allocating the memory they'll use differently based on some criteria,
// we have no access to that memory unless we store a reference to that MTLTexture.
// This allows us to be able to export said texture when the user requests so from a
// VkDeviceMemory object.
union {
id<MTLBuffer> _mtlBuffer = nil;
id<MTLTexture> _mtlTexture;
};
id<MTLHeap> _mtlHeap = nil;
void* _pMemory = nullptr;
void* _pHostMemory = nullptr;
Expand All @@ -171,6 +177,6 @@ class MVKDeviceMemory : public MVKVulkanAPIDeviceObject {
MTLCPUCacheMode _mtlCPUCacheMode;
bool _isDedicated = false;
bool _isHostMemImported = false;

VkExternalMemoryHandleTypeFlags _externalMemoryHandleType = 0u;
};

Loading
Loading