From 9d16d7e90ec92703e3a9f7b052b31f6cebb3f090 Mon Sep 17 00:00:00 2001 From: Aitor Camacho Date: Tue, 20 Aug 2024 16:00:20 +0900 Subject: [PATCH] Add VK_EXT_external_memory_metal --- appendices/VK_EXT_external_memory_metal.adoc | 27 +++ .../device_memory.adoc | 159 ++++++++++++++++++ chapters/capabilities.adoc | 13 ++ chapters/memory.adoc | 27 ++- chapters/resources.adoc | 17 +- proposals/VK_EXT_external_memory_metal.adoc | 113 +++++++++++++ scripts/genvk.py | 3 +- scripts/xml_consistency.py | 1 + xml/vk.xml | 53 ++++++ 9 files changed, 401 insertions(+), 12 deletions(-) create mode 100644 appendices/VK_EXT_external_memory_metal.adoc create mode 100644 chapters/VK_EXT_external_memory_metal/device_memory.adoc create mode 100644 proposals/VK_EXT_external_memory_metal.adoc diff --git a/appendices/VK_EXT_external_memory_metal.adoc b/appendices/VK_EXT_external_memory_metal.adoc new file mode 100644 index 000000000..668020084 --- /dev/null +++ b/appendices/VK_EXT_external_memory_metal.adoc @@ -0,0 +1,27 @@ +// Copyright 2024 The Khronos Group Inc. +// +// SPDX-License-Identifier: CC-BY-4.0 + +include::{generated}/meta/{refprefix}VK_EXT_external_memory_metal.adoc[] + +=== Other Extension Metadata + +*Last Modified Date*:: + 2024-07-18 +*IP Status*:: + No known IP claims. +*Contributors*:: + - Aitor Camacho Larrondo, LunarG Inc. + +=== Description + +An application may wish to reference device memory in multiple Vulkan device instances, in multiple processes, and/or in Metal API. +This extension enables an application to export non-Vulkan handles from Vulkan memory objects such that the underlying resources can +be referenced outside the scope of the Vulkan device instance that created them. + +include::{generated}/interfaces/VK_EXT_external_memory_metal.adoc[] + +=== Version History + + * Revision 1, 2024-07-18 (Aitor Camacho Larrondo) + ** Initial revision diff --git a/chapters/VK_EXT_external_memory_metal/device_memory.adoc b/chapters/VK_EXT_external_memory_metal/device_memory.adoc new file mode 100644 index 000000000..d1ab7d968 --- /dev/null +++ b/chapters/VK_EXT_external_memory_metal/device_memory.adoc @@ -0,0 +1,159 @@ +// Copyright 2024 The Khronos Group Inc. +// +// SPDX-License-Identifier: CC-BY-4.0 + +=== Metal External Memory + +[open,refpage='MTLResource_id',desc='Metal MTLResource type reference',type='basetypes'] +-- +The type `id` is defined in Apple's Metal framework as the +parent type of `id` and `id`. We use `id` +to simplify usage and provide a single common type for both resources. To +remove an unnecessary compile time dependency, an incomplete type definition +of basetype:MTLResource_id is provided in the Vulkan headers: + +include::{generated}/api/basetypes/MTLResource_id.adoc[] +-- + + +[open,refpage='VkImportMemoryMetalHandleInfoEXT',desc='Import Metal memory created on the same physical device',type='structs'] +-- +To import memory from a Metal handle, add a +slink:VkImportMemoryMetalHandleInfoEXT structure to the pname:pNext chain of +the slink:VkMemoryAllocateInfo structure. + +The sname:VkImportMemoryMetalHandleInfoEXT structure is defined as: + +include::{generated}/api/structs/VkImportMemoryMetalHandleInfoEXT.adoc[] + + * pname:sType is a elink:VkStructureType value identifying this structure. + * pname:pNext is `NULL` or a pointer to a structure extending this + structure. + * pname:handleType is a elink:VkExternalMemoryHandleTypeFlagBits value + specifying the type of pname:handle or pname:name. + * pname:handle is `NULL` or the external handle to import. + +Importing memory object payloads from Metal handles shares the +ownership of the handle to the Vulkan implementation. + +Applications can: import the same payload into multiple instances of Vulkan, +into the same instance from which it was exported, and multiple times into a +given Vulkan instance. +In all cases, each import operation must: create a distinct +sname:VkDeviceMemory object. + +.Valid Usage +**** + * If pname:handleType is not `0`, it must: be supported for import, as + reported by slink:VkExternalImageFormatProperties or + slink:VkExternalBufferProperties + * The memory from which pname:handle was exported must: have been created + on the same underlying physical device as pname:device + * If pname:handleType is not `0`, it must: be + ename:VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLBUFFER_BIT_EXT or + ename:VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLTEXTURE_BIT_EXT + * If pname:handleType is not `0` , pname:handle must: be a valid non-NULL + handle of the type specified by pname:handleType + * pname:handle must: obey any requirements listed for pname:handleType in + <> +**** + +include::{generated}/validity/structs/VkImportMemoryMetalHandleInfoEXT.adoc[] +-- + +[open,refpage='vkGetMemoryMetalHandleEXT',desc='Get a Metal handle for a memory object',type='protos'] +-- +To export a Metal handle representing the payload of a Vulkan device +memory object, call: + +include::{generated}/api/protos/vkGetMemoryMetalHandleEXT.adoc[] + + * pname:device is the logical device that created the device memory being + exported. + * pname:pGetMetalHandleInfo is a pointer to a + slink:VkMemoryGetMetalHandleInfoEXT structure containing parameters of + the export operation. + * pname:pHandle will return the Metal handle representing the payload of + the device memory object. + +The lifespan of the returned handle object is the same as the associated `VkDeviceMemory` object. +The app must: retain the handle object if it intends to use it beyond that lifespan. + +include::{generated}/validity/protos/vkGetMemoryMetalHandleEXT.adoc[] +-- + +[open,refpage='VkMemoryGetMetalHandleInfoEXT',desc='Structure describing a Metal handle memory export operation',type='structs'] +-- +The sname:VkMemoryGetMetalHandleInfoEXT structure is defined as: + +include::{generated}/api/structs/VkMemoryGetMetalHandleInfoEXT.adoc[] + + * pname:sType is a elink:VkStructureType value identifying this structure. + * pname:pNext is `NULL` or a pointer to a structure extending this + structure. + * pname:memory is the memory object from which the handle will be + exported. + * pname:handleType is a elink:VkExternalMemoryHandleTypeFlagBits value + specifying the type of handle requested. + +The properties of the handle returned depend on the value of +pname:handleType. +See elink:VkExternalMemoryHandleTypeFlagBits for a description of the +properties of the defined external memory handle types. + +.Valid Usage +**** + * pname:handleType must: have been included in + slink:VkExportMemoryAllocateInfo::pname:handleTypes when pname:memory + was created + * pname:handleType it must: be + ename:VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLBUFFER_BIT_EXT or + ename:VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLTEXTURE_BIT_EXT +**** + +include::{generated}/validity/structs/VkMemoryGetMetalHandleInfoEXT.adoc[] +-- + +[open,refpage='vkGetMemoryMetalHandlePropertiesEXT',desc='Get Properties of External Memory Metal Handles',type='protos'] +-- +Metal memory handles compatible with Vulkan may: also be created by +non-Vulkan APIs using methods beyond the scope of this specification. +To determine the correct parameters to use when importing such handles, +call: + +include::{generated}/api/protos/vkGetMemoryMetalHandlePropertiesEXT.adoc[] + + * pname:device is the logical device that will be importing pname:handle. + * pname:handleType is a elink:VkExternalMemoryHandleTypeFlagBits value + specifying the type of the handle pname:handle. + * pname:handle is the handle which will be imported. + * pname:pMemoryMetalHandleProperties is a pointer to a + slink:VkMemoryMetalHandlePropertiesEXT structure in which properties of + pname:handle are returned. + +.Valid Usage +**** + * pname:handle must: point to a valid MTLResource_id + * pname:handleType it must: be + ename:VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLBUFFER_BIT_EXT or + ename:VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLTEXTURE_BIT_EXT +**** + +include::{generated}/validity/protos/vkGetMemoryMetalHandlePropertiesEXT.adoc[] +-- + +[open,refpage='VkMemoryMetalHandlePropertiesEXT',desc='Properties of External Memory Metal Handles',type='structs'] +-- +The sname:VkMemoryMetalHandlePropertiesEXT structure returned is defined as: + +include::{generated}/api/structs/VkMemoryMetalHandlePropertiesEXT.adoc[] + + * pname:sType is a elink:VkStructureType value identifying this structure. + * pname:pNext is `NULL` or a pointer to a structure extending this + structure. + * pname:memoryTypeBits is a bitmask containing one bit set for every + memory type which the specified Metal handle can: be imported as. + +include::{generated}/validity/structs/VkMemoryMetalHandlePropertiesEXT.adoc[] +-- \ No newline at end of file diff --git a/chapters/capabilities.adoc b/chapters/capabilities.adoc index 5d0beec49..e7732454b 100644 --- a/chapters/capabilities.adoc +++ b/chapters/capabilities.adoc @@ -528,6 +528,12 @@ ifdef::VK_QNX_external_memory_screen_buffer[] See <> for more details of this handle type. endif::VK_QNX_external_memory_screen_buffer[] +ifdef::VK_EXT_external_memory_metal[] + * ename:VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLBUFFER_BIT_EXT is a handle to + a `MTLResource` holding a `MTLBuffer`. + * ename:VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLTEXTURE_BIT_EXT is a handle to + a `MTLResource` holding a `MTLTexture`. +endif::VK_EXT_external_memory_metal[] <<< @@ -568,6 +574,10 @@ endif::VK_NV_external_memory_sci_buf[] ifdef::VK_QNX_external_memory_screen_buffer[] | ename:VK_EXTERNAL_MEMORY_HANDLE_TYPE_SCREEN_BUFFER_BIT_QNX | No restriction | No restriction endif::VK_QNX_external_memory_screen_buffer[] +ifdef::VK_EXT_external_memory_metal[] +| ename:VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLBUFFER_BIT_EXT | Must match | Must match +| ename:VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLTEXTURE_BIT_EXT | Must match | Must match +endif::VK_EXT_external_memory_metal[] |==== ifdef::VK_EXT_external_memory_host[] @@ -707,6 +717,9 @@ ifdef::VK_QNX_external_memory_screen_buffer[] * ename:VK_EXTERNAL_MEMORY_HANDLE_TYPE_SCREEN_BUFFER_BIT_QNX for images only endif::VK_QNX_external_memory_screen_buffer[] +ifdef::VK_EXT_external_memory_metal[] + * ename:VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLTEXTURE_BIT_EXT +endif::VK_EXT_external_memory_metal[] ifdef::VK_ANDROID_external_memory_android_hardware_buffer[] Implementations must: not report diff --git a/chapters/memory.adoc b/chapters/memory.adoc index e01b5448a..6e3009a54 100644 --- a/chapters/memory.adoc +++ b/chapters/memory.adoc @@ -1204,7 +1204,7 @@ include::{generated}/api/structs/VkMemoryAllocateInfo.adoc[] The internal data of an allocated device memory object must: include a reference to implementation-specific resources, referred to as the memory object's _payload_. -ifdef::VK_KHR_external_memory_win32,VK_KHR_external_memory_fd,VK_EXT_external_memory_host,VK_ANDROID_external_memory_android_hardware_buffer,VK_FUCHSIA_external_memory,VK_NV_external_memory_sci_buf,VK_QNX_external_memory_screen_buffer[] +ifdef::VK_KHR_external_memory_win32,VK_KHR_external_memory_fd,VK_EXT_external_memory_host,VK_ANDROID_external_memory_android_hardware_buffer,VK_FUCHSIA_external_memory,VK_NV_external_memory_sci_buf,VK_QNX_external_memory_screen_buffer,VK_EXT_external_memory_metal[] Applications can: also import and export that internal data to and from device memory objects to share data between Vulkan instances and other compatible APIs. @@ -1240,6 +1240,10 @@ endif::VK_NV_external_memory_sci_buf[] ifdef::VK_QNX_external_memory_screen_buffer[] * slink:VkImportScreenBufferInfoQNX with a non-`NULL` pname:buffer value endif::VK_QNX_external_memory_screen_buffer[] +ifdef::VK_EXT_external_memory_metal[] + * slink:VkImportMemoryMetalHandleInfoEXT with a non-zero pname:handleType + value +endif::VK_EXT_external_memory_metal[] ifdef::VK_KHR_external_memory_win32[] If the parameters define an import operation and the external handle type is @@ -1258,6 +1262,13 @@ The implementation must: query the size of this allocation from the stext:NvSciBufAttrList associated with the external stext:NvSciBufObj. endif::VK_NV_external_memory_sci_buf[] +ifdef::VK_EXT_external_memory_metal[] +If the parameters define an import operation and the external handle type is +ename:VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLTEXTURE_BIT_EXT, +pname:allocationSize is ignored. +The implementation must: query the size of these allocations from the OS. +endif::VK_EXT_external_memory_metal[] + Whether device memory objects constructed via a memory import operation hold a reference to their payload depends on the properties of the handle type used to perform the import, as defined below for each valid handle type. @@ -1310,17 +1321,17 @@ This is because for an export operation, there is currently no way for the application to know the memory type index before allocating. endif::VK_ANDROID_external_memory_android_hardware_buffer[] -endif::VK_KHR_external_memory_win32,VK_KHR_external_memory_fd,VK_EXT_external_memory_host,VK_ANDROID_external_memory_android_hardware_buffer,VK_FUCHSIA_external_memory,VK_NV_external_memory_sci_buf,VK_QNX_external_memory_screen_buffer[] +endif::VK_KHR_external_memory_win32,VK_KHR_external_memory_fd,VK_EXT_external_memory_host,VK_ANDROID_external_memory_android_hardware_buffer,VK_FUCHSIA_external_memory,VK_NV_external_memory_sci_buf,VK_QNX_external_memory_screen_buffer,VK_EXT_external_memory_metal[] .Valid Usage **** * [[VUID-VkMemoryAllocateInfo-allocationSize-07897]] -ifdef::VK_KHR_external_memory_win32,VK_KHR_external_memory_fd,VK_EXT_external_memory_host,VK_ANDROID_external_memory_android_hardware_buffer,VK_FUCHSIA_external_memory,VK_NV_external_memory_sci_buf,VK_QNX_external_memory_screen_buffer[] +ifdef::VK_KHR_external_memory_win32,VK_KHR_external_memory_fd,VK_EXT_external_memory_host,VK_ANDROID_external_memory_android_hardware_buffer,VK_FUCHSIA_external_memory,VK_NV_external_memory_sci_buf,VK_QNX_external_memory_screen_buffer,VK_EXT_external_memory_metal[] If the parameters do not define an <>, -endif::VK_KHR_external_memory_win32,VK_KHR_external_memory_fd,VK_EXT_external_memory_host,VK_ANDROID_external_memory_android_hardware_buffer,VK_FUCHSIA_external_memory,VK_NV_external_memory_sci_buf,VK_QNX_external_memory_screen_buffer[] +endif::VK_KHR_external_memory_win32,VK_KHR_external_memory_fd,VK_EXT_external_memory_host,VK_ANDROID_external_memory_android_hardware_buffer,VK_FUCHSIA_external_memory,VK_NV_external_memory_sci_buf,VK_QNX_external_memory_screen_buffer,VK_EXT_external_memory_metal[] pname:allocationSize must: be greater than `0` -ifdef::VK_KHR_external_memory_win32,VK_KHR_external_memory_fd,VK_EXT_external_memory_host,VK_ANDROID_external_memory_android_hardware_buffer,VK_FUCHSIA_external_memory,VK_NV_external_memory_sci_buf,VK_QNX_external_memory_screen_buffer[] +ifdef::VK_KHR_external_memory_win32,VK_KHR_external_memory_fd,VK_EXT_external_memory_host,VK_ANDROID_external_memory_android_hardware_buffer,VK_FUCHSIA_external_memory,VK_NV_external_memory_sci_buf,VK_QNX_external_memory_screen_buffer,VK_EXT_external_memory_metal[] * [[VUID-VkMemoryAllocateInfo-None-06657]] The parameters must: not define more than one <> @@ -1331,7 +1342,7 @@ ifdef::VK_ANDROID_external_memory_android_hardware_buffer[] ename:VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID endif::VK_ANDROID_external_memory_android_hardware_buffer[] , pname:allocationSize must: be greater than `0` -endif::VK_KHR_external_memory_win32,VK_KHR_external_memory_fd,VK_EXT_external_memory_host,VK_ANDROID_external_memory_android_hardware_buffer,VK_FUCHSIA_external_memory,VK_NV_external_memory_sci_buf,VK_QNX_external_memory_screen_buffer[] +endif::VK_KHR_external_memory_win32,VK_KHR_external_memory_fd,VK_EXT_external_memory_host,VK_ANDROID_external_memory_android_hardware_buffer,VK_FUCHSIA_external_memory,VK_NV_external_memory_sci_buf,VK_QNX_external_memory_screen_buffer,VK_EXT_external_memory_metal[] ifdef::VK_FUCHSIA_buffer_collection[] * [[VUID-VkMemoryAllocateInfo-buffer-06380]] If the parameters define an import operation from an @@ -3246,6 +3257,10 @@ ifdef::VK_QNX_external_memory_screen_buffer[] include::{chapters}/VK_QNX_external_memory_screen_buffer/device_memory.adoc[] endif::VK_QNX_external_memory_screen_buffer[] +ifdef::VK_EXT_external_memory_metal[] +include::{chapters}/VK_EXT_external_memory_metal/device_memory.adoc[] +endif::VK_EXT_external_memory_metal[] + ifdef::VK_VERSION_1_1,VK_KHR_device_group[] === Device Group Memory Allocations diff --git a/chapters/resources.adoc b/chapters/resources.adoc index 87a8f1a6a..6c9e4fac3 100644 --- a/chapters/resources.adoc +++ b/chapters/resources.adoc @@ -8321,13 +8321,20 @@ flink:vkGetBufferMemoryRequirements or flink:vkGetBufferMemoryRequirements2 with slink:VkBuffer before it has been bound to memory. endif::VK_ANDROID_external_memory_android_hardware_buffer[] +ifdef::VK_KHR_external_memory_win32,VK_EXT_external_memory_metal[] +The value of pname:size has no meaning and should: be ignored +if the resource being queried was created with the any of the +following external memory handle types: + ifdef::VK_KHR_external_memory_win32[] -If the resource being queried was created with the -ename:VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_TEXTURE_BIT, -ename:VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_TEXTURE_KMT_BIT, or -ename:VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D12_RESOURCE_BIT external memory -handle type, the value of pname:size has no meaning and should: be ignored. + * ename:VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_TEXTURE_BIT + * ename:VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_TEXTURE_KMT_BIT + * ename:VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D12_RESOURCE_BIT endif::VK_KHR_external_memory_win32[] +ifdef::VK_EXT_external_memory_metal[] + * ename:VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLTEXTURE_BIT_EXT +endif::VK_EXT_external_memory_metal[] +endif::VK_KHR_external_memory_win32,VK_EXT_external_memory_metal[] The implementation guarantees certain properties about the memory requirements returned by diff --git a/proposals/VK_EXT_external_memory_metal.adoc b/proposals/VK_EXT_external_memory_metal.adoc new file mode 100644 index 000000000..b932ec3a8 --- /dev/null +++ b/proposals/VK_EXT_external_memory_metal.adoc @@ -0,0 +1,113 @@ +// Copyright 2021-2024 The Khronos Group Inc. +// +// SPDX-License-Identifier: CC-BY-4.0 + += VK_EXT_external_memory_metal +:toc: left +:refpage: https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/ +:sectnums: + +This extension allows memory sharing through imports from or export to Metal resource handles. + +== Problem Statement + +An application may wish to reference device memory in multiple Vulkan device instances, in multiple processes, and/or in Metal API. +This extension enables an application to export non-Vulkan handles from Vulkan memory objects such that the underlying resources can +be referenced outside the scope of the Vulkan device instance that created them. + +While link:{refpage}VK_EXT_metal_objects.html[VK_EXT_metal_objects] provides a way to expose underlying Metal resources, +when importing an image from an `id` handle, said images and their backing memory will be imported at +link:{refpage}VkImage.html[VkImage] creation. +According to point link:https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#resources-association[12.8 Memory Resource Association] +resources are created as _virtual allocations_ with no backing memory. This leads to having to treat imported images through +link:{refpage}VK_EXT_metal_objects.html[VK_EXT_metal_objects] differently. + +This extension aims to import both resources through link:{refpage}VkDeviceMemory.html[VkDeviceMemory] so that later can be bound +to a existing link:{refpage}VkImage.html[VkImage] or link:{refpage}VkBuffer.html[VkBuffer] respectively. + +== Proposal + +This extension aims to standardize memory imports and exports of Vulkan resources using +link:{refpage}VK_KHR_external_memory.html[VK_KHR_external_memory] as baseline to conform to what other platforms have done with +extensions like link:{refpage}VK_KHR_external_memory_win32.html[VK_KHR_external_memory_win32] and +link:{refpage}VK_ANDROID_external_memory_android_hardware_buffer.html[VK_ANDROID_external_memory_android_hardware_buffer] to name a few. + +This means that external Metal resources will be imported through link:{refpage}VkDeviceMemory.html[VkDeviceMemory] to later be bound. + +A new type will be exposed in the extension `MTLResource_id` which will be equivalent to `id` when compiled in Objective-C +and `void*` when compiled in C. This handle will allow mapping to `id` and `id` to have a seamless import and +export operations from Vulkan to Metal and vice-versa. + +2 new bit values will be added to link:{refpage}VkExternalMemoryHandleTypeFlagBits.html[VkExternalMemoryHandleTypeFlagBits]: + + - `VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLBUFFER_BIT_EXT` to accommodate for Metal buffers (MTLBuffer) + - `VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLTEXTURE_BIT_EXT` to accommodate for Metal textures (MTLTexture) + +Due to MTLTexture's semantics in Metal API roughly align with that of an image with a dedicated allocation in Vulkan, +implementations will be required to report `VK_EXTERNAL_MEMORY_FEATURE_DEDICATED_ONLY_BIT` for +`VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLTEXTURE_BIT_EXT`. When importing a MTLTexture through the creation of a +link:{refpage}VkDeviceMemory.html[VkDeviceMemory], the link:{refpage}VkImage.html[VkImage] it will be bound to +must have the same creation parameters as the MTLTexture used at import. + +== Examples + +=== Importing a Metal texture from a id + +[source,c] +---- +MTLResource_id metalTexture = /* Initialized to id */; +VkMemoryMetalHandlePropertiesEXT handleProperties; +VkResult result = vkGetMemoryMetalHandlePropertiesEXT( + device, + VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLTEXTURE_BIT_EXT, + metalTexture, + &handleProperties); + +VkImageCreateInfo imageCreateInfo = { + // This image creation parameters should match MTLTexture's, or the exported VkImage's +}; +VkImage image; +result = vkCreateImage( + device, + &imageCreateInfo, + NULL, // pAllocator + &image); + +// Import image memory +VkImportMemoryMetalHandleInfoEXT importInfo = { + .sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_METAL_HANDLE_INFO_EXT, + .pNext = NULL, + .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLTEXTURE_BIT_EXT, + .handle = metalTexture +}; +VkMemoryDedicatedAllocateInfoKHR dedicatedInfo = { + .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR, + .pNext = &importInfo, + .image = image, + .buffer = VK_NULL_HANDLE, +}; +VkMemoryAllocateInfo allocateInfo = { + .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, + .pNext = &dedicatedInfo, + .allocationSize = /* Not 0 value */, // Ignored. The allocation will have the size of the MTLResource_id + .memoryTypeIndex = /* Select preferred memory type from handleProperties.memoryTypeBits */ +}; +// If the MTLResource_id cannot be used for the VkImage we created, implementations will return VK_ERROR_INVALID_EXTERNAL_HANDLE +VkDeviceMemory memory; +result = vkAllocateMemory( + device, + &allocateInfo, + NULL, + &memory); + +if (result == VK_SUCCESS) { + // Bind memory to image +} else { + // Handle failure +} + +---- + +== Issues + +None identified. \ No newline at end of file diff --git a/scripts/genvk.py b/scripts/genvk.py index 03eb9758c..1d2b43dbf 100755 --- a/scripts/genvk.py +++ b/scripts/genvk.py @@ -451,7 +451,8 @@ def makeGenOpts(args): [ 'vulkan_directfb.h', [ 'VK_EXT_directfb_surface' ], commonSuppressExtensions ], [ 'vulkan_xlib_xrandr.h', [ 'VK_EXT_acquire_xlib_display' ], commonSuppressExtensions ], [ 'vulkan_metal.h', [ 'VK_EXT_metal_surface', - 'VK_EXT_metal_objects' ], commonSuppressExtensions ], + 'VK_EXT_metal_objects', + 'VK_EXT_external_memory_metal' ], commonSuppressExtensions ], [ 'vulkan_screen.h', [ 'VK_QNX_screen_surface', 'VK_QNX_external_memory_screen_buffer' ], commonSuppressExtensions ], [ 'vulkan_sci.h', [ 'VK_NV_external_sci_sync', diff --git a/scripts/xml_consistency.py b/scripts/xml_consistency.py index f1db1edce..191a888cc 100755 --- a/scripts/xml_consistency.py +++ b/scripts/xml_consistency.py @@ -64,6 +64,7 @@ 'MTLDevice_id', 'MTLSharedEvent_id', 'MTLTexture_id', + 'MTLResource_id', 'VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE', 'VkFlags64', 'VkPipelineCacheCreateFlagBits', diff --git a/xml/vk.xml b/xml/vk.xml index 0e92812c4..beeb1a8e6 100755 --- a/xml/vk.xml +++ b/xml/vk.xml @@ -264,6 +264,12 @@ typedef void* MTLTexture_id; typedef __unsafe_unretained id<MTLSharedEvent> MTLSharedEvent_id; #else typedef void* MTLSharedEvent_id; +#endif + #ifdef __OBJC__ +@protocol MTLResource; +typedef __unsafe_unretained id<MTLResource> MTLResource_id; +#else +typedef void* MTLResource_id; #endif typedef struct __IOSurface* IOSurfaceRef; @@ -2758,6 +2764,23 @@ typedef void* MTLSharedEvent_id; const VkDeviceMemory* pReleaseSyncs const uint64_t* pReleaseKeys + + VkStructureType sType + const void* pNext + VkExternalMemoryHandleTypeFlagBits handleType + MTLResource_id handle + + + VkStructureType sType + void* pNext + uint32_t memoryTypeBits + + + VkStructureType sType + const void* pNext + VkDeviceMemory memory + VkExternalMemoryHandleTypeFlagBits handleType + VkStructureType sType const void* pNext @@ -15450,6 +15473,19 @@ typedef void* MTLSharedEvent_id; VkCommandBuffer commandBuffer const VkRenderingInputAttachmentIndexInfoKHR* pInputAttachmentIndexInfo + + VkResult vkGetMemoryMetalHandleEXT + VkDevice device + const VkMemoryGetMetalHandleInfoEXT* pGetMetalHandleInfo + MTLResource_id* pHandle + + + VkResult vkGetMemoryMetalHandlePropertiesEXT + VkDevice device + VkExternalMemoryHandleTypeFlagBits handleType + MTLResource_id handle + VkMemoryMetalHandlePropertiesEXT* pMemoryMetalHandleProperties + @@ -24640,6 +24676,23 @@ typedef void* MTLSharedEvent_id; + + + + + + + + + + + + + + + + +