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

Proposed VK_EXT_metal_objects extension to expose Metal objects to Vulkan apps #34

Closed
billhollings opened this issue Nov 18, 2021 · 6 comments

Comments

@billhollings
Copy link
Contributor

billhollings commented Nov 18, 2021

Two calls:

  • vkGetMetalObjectsEXT() to retrieve one or more metal objects from the associated Vulkan objects (each 1:1).
  • vkSetMetalObjectsEXT() to set one or more metal objects into the associated Vulkan objects (each 1:1).

Each call makes use of a set of structs chained to the pNext of the VkMetalObjectsInfoEXT, each of which connects one Metal object with a Vulkan object (reading/writing the Metal object from/to the Vulkan object).

Multiple objects can be read/written with each call if multiple pNext structs are included.

#define VK_EXT_metal_objects 1
#ifdef __OBJC__
@protocol MTLDevice;
typedef id<MTLDevice> MTLDevice_id;
#else
typedef void* MTLDevice_id;
#endif

#ifdef __OBJC__
@protocol MTLCommandQueue;
typedef id<MTLCommandQueue> MTLCommandQueue_id;
#else
typedef void* MTLCommandQueue_id;
#endif

#ifdef __OBJC__
@protocol MTLBuffer;
typedef id<MTLBuffer> MTLBuffer_id;
#else
typedef void* MTLBuffer_id;
#endif

#ifdef __OBJC__
@protocol MTLTexture;
typedef id<MTLTexture> MTLTexture_id;
#else
typedef void* MTLTexture_id;
#endif

typedef struct __IOSurface* IOSurfaceRef;
#ifdef __OBJC__
@protocol MTLSharedEvent;
typedef id<MTLSharedEvent> MTLSharedEvent_id;
#else
typedef void* MTLSharedEvent_id;
#endif

#define VK_EXT_METAL_OBJECTS_SPEC_VERSION 1
#define VK_EXT_METAL_OBJECTS_EXTENSION_NAME "VK_EXT_metal_objects"
typedef struct VkGetMetalObjectsInfoEXT {
    VkStructureType    sType;
    const void*        pNext;
} VkGetMetalObjectsInfoEXT;

typedef struct VkGetMetalDeviceInfoEXT {
    VkStructureType    sType;
    const void*        pNext;
    MTLDevice_id       mtlDevice;
} VkGetMetalDeviceInfoEXT;

typedef struct VkGetMetalCommandQueueInfoEXT {
    VkStructureType       sType;
    const void*           pNext;
    VkQueue               queue;
    MTLCommandQueue_id    mtlCommandQueue;
} VkGetMetalCommandQueueInfoEXT;

typedef struct VkGetMetalBufferInfoEXT {
    VkStructureType    sType;
    const void*        pNext;
    VkBuffer           buffer;
    MTLBuffer_id       mtlBuffer;
    VkDeviceSize       mtlBufferOffset;
} VkGetMetalBufferInfoEXT;

typedef struct VkGetMetalTextureInfoEXT {
    VkStructureType       sType;
    const void*           pNext;
    VkImage               image;
    VkImageView           imageView;
    VkBufferView          bufferView;
    VkImageAspectFlags    aspectMask;
    MTLTexture_id         mtlTexture;
} VkGetMetalTextureInfoEXT;

typedef struct VkGetMetalIOSurfaceInfoEXT {
    VkStructureType    sType;
    const void*        pNext;
    VkImage            image;
    IOSurfaceRef       ioSurface;
} VkGetMetalIOSurfaceInfoEXT;

typedef struct VkGetMetalEventInfoEXT {
    VkStructureType      sType;
    const void*          pNext;
    VkEvent              event;
    MTLSharedEvent_id    mtlSharedEvent;
} VkGetMetalEventInfoEXT;

typedef struct VkBufferCreateMetalBufferInfoEXT {
    VkStructureType    sType;
    const void*        pNext;
    MTLBuffer_id       mtlBuffer;
    VkDeviceSize       mtlBufferOffset;
} VkBufferCreateMetalBufferInfoEXT;

typedef struct VkImageCreateMetalTextureInfoEXT {
    VkStructureType       sType;
    const void*           pNext;
    VkImageAspectFlags    aspectMask;
    MTLTexture_id         mtlTexture;
} VkImageCreateMetalTextureInfoEXT;

typedef struct VkImageCreateMetalIOSurfaceInfoEXT {
    VkStructureType    sType;
    const void*        pNext;
    IOSurfaceRef       ioSurface;
} VkImageCreateMetalIOSurfaceInfoEXT;

typedef void (VKAPI_PTR *PFN_vkGetMetalObjectsEXT)(VkDevice device, VkGetMetalObjectsInfoEXT* pMetalObjectsInfo);

#ifndef VK_NO_PROTOTYPES
VKAPI_ATTR void VKAPI_CALL vkGetMetalObjectsEXT(
    VkDevice                                    device,
    VkGetMetalObjectsInfoEXT*                   pMetalObjectsInfo);
#endif
@billhollings
Copy link
Contributor Author

billhollings commented Nov 25, 2021 via email

@kvark
Copy link
Contributor

kvark commented Nov 25, 2021

Are the pointers guaranteed to be valid id<> after these calls?
There are cases where a Vulkan on Metal implementation doesn't have a corresponding object 1:1.
For example, CPU-visible buffer may be sub-allocated, or a CPU-visible texture may be backed by a buffer.

@billhollings
Copy link
Contributor Author

CPU-visible buffer may be sub-allocated

Good point. Thanks. I've added VkMetalBufferInfoEXT:: pMTLBufferOffset to return the offset of the VkBuffer in the returned MTLBuffer.

CPU-visible texture may be backed by a buffer

Not sure if this matters, unless there is a use case for retrieving buffer memory underlying the texture? Current use cases focus on retrieving the MTLTexture in order to be able to have it interact with other Apple API's. Are there use cases where it would matter that it is backed by a buffer?

@billhollings
Copy link
Contributor Author

billhollings commented Nov 25, 2021

Do we need to extract a MTLTexture from VkImageView, in addition to VkImage?

@billhollings
Copy link
Contributor Author

Do we need to extract a MTLTexture from VkImageView, in addition to VkImage?

This has been included now, along with MTLTexture from VkBufferView.

@kanerogers
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants