diff --git a/src/ComputeSharp.Graphics/Interop/InteropServices.cs b/src/ComputeSharp.Graphics/Interop/InteropServices.cs new file mode 100644 index 00000000..a15d9f84 --- /dev/null +++ b/src/ComputeSharp.Graphics/Interop/InteropServices.cs @@ -0,0 +1,148 @@ +using System; +using ComputeSharp.Resources; + +namespace ComputeSharp.Interop +{ + /// + /// Provides methods to interoperate with the native APIs and the managed types in this library. + /// + /// None of the APIs in perform input validation, and it is responsibility + /// of consumers to ensure the input arguments are in a correct state to be used (eg. not disposed). + /// + /// + /// Manually interfering with the underlying COM objects for any of these types can result in issues if + /// the operations are not done correctly, which can prevent other APIs from functioning as expected. + /// Consumers should ensure the executed operations do not result in any errors. Furthermore, even when + /// the reference count to the returned COM object is incremented, consumers should ensure that the owning + /// objects will remain alive as long as the returned pointers are in use, to avoid unexpected issues. This + /// is because the lifecycle of certain COM objects (eg. resources) is delegated to an internal allocator + /// that relies on resources being disposed as soon as the relative allocation object is disposed. + /// + /// + public static unsafe class InteropServices + { + /// + /// Tries to get the underlying COM object for a given device, as a specified interface. This method invokes + /// IUnknown::QueryInterface. + /// + /// The input instance in use. + /// A reference to the interface identifier (IID) of the device interface being queried for. + /// The address of a pointer to an interface with the IID specified in . + /// + /// S_OK if the interface is supported, and E_NOINTERFACE otherwise. + /// If ppvObject (the address) is nullptr, then this method returns E_POINTER. + /// + public static int TryGetID3D12Device(GraphicsDevice device, Guid* riid, void** ppvObject) + { + return device.D3D12Device->QueryInterface(riid, ppvObject); + } + + /// + /// Tries to get the underlying COM object for a given resource, as a specified interface. This method invokes + /// IUnknown::QueryInterface. + /// + /// The type of items stored on the buffer. + /// The input instance in use. + /// A reference to the interface identifier (IID) of the resource interface being queried for. + /// The address of a pointer to an interface with the IID specified in . + /// + /// S_OK if the interface is supported, and E_NOINTERFACE otherwise. + /// If ppvObject (the address) is nullptr, then this method returns E_POINTER. + /// + public static int TryGetID3D12Resource(Buffer buffer, Guid* riid, void** ppvObject) + where T : unmanaged + { + return buffer.D3D12Resource->QueryInterface(riid, ppvObject); + } + + /// + /// Tries to get the underlying COM object for a given resource, as a specified interface. This method invokes + /// IUnknown::QueryInterface. + /// + /// The type of items stored on the texture. + /// The input instance in use. + /// A reference to the interface identifier (IID) of the resource interface being queried for. + /// The address of a pointer to an interface with the IID specified in . + /// + /// S_OK if the interface is supported, and E_NOINTERFACE otherwise. + /// If ppvObject (the address) is nullptr, then this method returns E_POINTER. + /// + public static int TryGetID3D12Resource(Texture2D texture, Guid* riid, void** ppvObject) + where T : unmanaged + { + return texture.D3D12Resource->QueryInterface(riid, ppvObject); + } + + /// + /// Tries to get the underlying COM object for a given resource, as a specified interface. This method invokes + /// IUnknown::QueryInterface. + /// + /// The type of items stored on the texture. + /// The input instance in use. + /// A reference to the interface identifier (IID) of the resource interface being queried for. + /// The address of a pointer to an interface with the IID specified in . + /// + /// S_OK if the interface is supported, and E_NOINTERFACE otherwise. + /// If ppvObject (the address) is nullptr, then this method returns E_POINTER. + /// + public static int TryGetID3D12Resource(Texture3D texture, Guid* riid, void** ppvObject) + where T : unmanaged + { + return texture.D3D12Resource->QueryInterface(riid, ppvObject); + } + + /// + /// Tries to get the underlying COM object for a given resource, as a specified interface. This method invokes + /// IUnknown::QueryInterface. + /// + /// The type of items stored on the buffer. + /// The input instance in use. + /// A reference to the interface identifier (IID) of the resource interface being queried for. + /// The address of a pointer to an interface with the IID specified in . + /// + /// S_OK if the interface is supported, and E_NOINTERFACE otherwise. + /// If ppvObject (the address) is nullptr, then this method returns E_POINTER. + /// + public static int TryGetID3D12Resource(TransferBuffer buffer, Guid* riid, void** ppvObject) + where T : unmanaged + { + return buffer.D3D12Resource->QueryInterface(riid, ppvObject); + } + + /// + /// Tries to get the underlying COM object for a given resource, as a specified interface. This method invokes + /// IUnknown::QueryInterface. + /// + /// The type of items stored on the texture. + /// The input instance in use. + /// A reference to the interface identifier (IID) of the resource interface being queried for. + /// The address of a pointer to an interface with the IID specified in . + /// + /// S_OK if the interface is supported, and E_NOINTERFACE otherwise. + /// If ppvObject (the address) is nullptr, then this method returns E_POINTER. + /// + public static int TryGetID3D12Resource(TransferTexture2D texture, Guid* riid, void** ppvObject) + where T : unmanaged + { + return texture.D3D12Resource->QueryInterface(riid, ppvObject); + } + + /// + /// Tries to get the underlying COM object for a given resource, as a specified interface. This method invokes + /// IUnknown::QueryInterface. + /// + /// The type of items stored on the texture. + /// The input instance in use. + /// A reference to the interface identifier (IID) of the resource interface being queried for. + /// The address of a pointer to an interface with the IID specified in . + /// + /// S_OK if the interface is supported, and E_NOINTERFACE otherwise. + /// If ppvObject (the address) is nullptr, then this method returns E_POINTER. + /// + public static int TryGetID3D12Resource(TransferTexture3D texture, Guid* riid, void** ppvObject) + where T : unmanaged + { + return texture.D3D12Resource->QueryInterface(riid, ppvObject); + } + } +}