diff --git a/src/ComputeSharp/Graphics/Interop/InteropServices.cs b/src/ComputeSharp/Graphics/Interop/InteropServices.cs
index a15d9f84..c4aced19 100644
--- a/src/ComputeSharp/Graphics/Interop/InteropServices.cs
+++ b/src/ComputeSharp/Graphics/Interop/InteropServices.cs
@@ -1,4 +1,6 @@
using System;
+using System.ComponentModel;
+using ComputeSharp.Core.Extensions;
using ComputeSharp.Resources;
namespace ComputeSharp.Interop
@@ -21,6 +23,109 @@ namespace ComputeSharp.Interop
///
public static unsafe class InteropServices
{
+ ///
+ /// Gets 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 .
+ /// Thrown if the IUnknown::QueryInterface call doesn't return S_OK.
+ public static void GetID3D12Device(GraphicsDevice device, Guid* riid, void** ppvObject)
+ {
+ device.D3D12Device->QueryInterface(riid, ppvObject).Assert();
+ }
+
+ ///
+ /// Gets 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 .
+ /// Thrown if the IUnknown::QueryInterface call doesn't return S_OK.
+ public static void GetID3D12Resource(Buffer buffer, Guid* riid, void** ppvObject)
+ where T : unmanaged
+ {
+ buffer.D3D12Resource->QueryInterface(riid, ppvObject).Assert();
+ }
+
+ ///
+ /// Gets 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 .
+ /// Thrown if the IUnknown::QueryInterface call doesn't return S_OK.
+ public static void GetID3D12Resource(Texture2D texture, Guid* riid, void** ppvObject)
+ where T : unmanaged
+ {
+ texture.D3D12Resource->QueryInterface(riid, ppvObject).Assert();
+ }
+
+ ///
+ /// Gets 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 .
+ /// Thrown if the IUnknown::QueryInterface call doesn't return S_OK.
+ public static void GetID3D12Resource(Texture3D texture, Guid* riid, void** ppvObject)
+ where T : unmanaged
+ {
+ texture.D3D12Resource->QueryInterface(riid, ppvObject).Assert();
+ }
+
+ ///
+ /// Gets 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 .
+ /// Thrown if the IUnknown::QueryInterface call doesn't return S_OK.
+ public static void GetID3D12Resource(TransferBuffer buffer, Guid* riid, void** ppvObject)
+ where T : unmanaged
+ {
+ buffer.D3D12Resource->QueryInterface(riid, ppvObject).Assert();
+ }
+
+ ///
+ /// Gets 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 .
+ /// Thrown if the IUnknown::QueryInterface call doesn't return S_OK.
+ public static void GetID3D12Resource(TransferTexture2D texture, Guid* riid, void** ppvObject)
+ where T : unmanaged
+ {
+ texture.D3D12Resource->QueryInterface(riid, ppvObject).Assert();
+ }
+
+ ///
+ /// Gets 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 .
+ /// Thrown if the IUnknown::QueryInterface call doesn't return S_OK.
+ public static void GetID3D12Resource(TransferTexture3D texture, Guid* riid, void** ppvObject)
+ where T : unmanaged
+ {
+ texture.D3D12Resource->QueryInterface(riid, ppvObject).Assert();
+ }
+
///
/// Tries to get the underlying COM object for a given device, as a specified interface. This method invokes
/// IUnknown::QueryInterface.
diff --git a/tests/ComputeSharp.Tests/InteropServicesTests.cs b/tests/ComputeSharp.Tests/InteropServicesTests.cs
new file mode 100644
index 00000000..b1362427
--- /dev/null
+++ b/tests/ComputeSharp.Tests/InteropServicesTests.cs
@@ -0,0 +1,188 @@
+using System;
+using ComputeSharp.Interop;
+using ComputeSharp.Resources;
+using ComputeSharp.Tests.Attributes;
+using ComputeSharp.Tests.Extensions;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using TerraFX.Interop;
+using FX = TerraFX.Interop.Windows;
+using static TerraFX.Interop.D3D12_RESOURCE_DIMENSION;
+
+namespace ComputeSharp.Tests
+{
+ [TestClass]
+ [TestCategory("InteropServices")]
+ public unsafe partial class InteropServicesTests
+ {
+ [CombinatorialTestMethod]
+ [AllDevices]
+ public void GetDevice(Device device)
+ {
+ using ComPtr d3D12Device = default;
+
+ InteropServices.GetID3D12Device(device.Get(), FX.__uuidof(), (void**)d3D12Device.GetAddressOf());
+
+ Assert.IsTrue(d3D12Device.Get() != null);
+
+ LUID luid = d3D12Device.Get()->GetAdapterLuid();
+
+ Assert.IsTrue(*(ulong*)&luid != 0);
+
+ d3D12Device.Dispose();
+
+ int hResult = InteropServices.TryGetID3D12Device(device.Get(), FX.__uuidof(), (void**)d3D12Device.GetAddressOf());
+
+ Assert.AreEqual(hResult, FX.S_OK);
+ Assert.IsTrue(d3D12Device.Get() != null);
+
+ luid = d3D12Device.Get()->GetAdapterLuid();
+
+ Assert.IsTrue(*(ulong*)&luid != 0);
+ }
+
+ [CombinatorialTestMethod]
+ [AllDevices]
+ [Resource(typeof(ConstantBuffer<>))]
+ [Resource(typeof(ReadOnlyBuffer<>))]
+ [Resource(typeof(ReadWriteBuffer<>))]
+ public void ResourceFromBuffer(Device device, Type bufferType)
+ {
+ using Buffer buffer = device.Get().AllocateBuffer(bufferType, 128);
+
+ using ComPtr d3D12Resource = default;
+
+ InteropServices.GetID3D12Resource(buffer, FX.__uuidof(), (void**)d3D12Resource.GetAddressOf());
+
+ Assert.IsTrue(d3D12Resource.Get() != null);
+ Assert.AreEqual(d3D12Resource.Get()->GetDesc().Dimension, D3D12_RESOURCE_DIMENSION_BUFFER);
+
+ d3D12Resource.Dispose();
+
+ int hResult = InteropServices.TryGetID3D12Resource(buffer, FX.__uuidof(), (void**)d3D12Resource.GetAddressOf());
+
+ Assert.AreEqual(hResult, FX.S_OK);
+ Assert.IsTrue(d3D12Resource.Get() != null);
+ Assert.AreEqual(d3D12Resource.Get()->GetDesc().Dimension, D3D12_RESOURCE_DIMENSION_BUFFER);
+ }
+
+ [CombinatorialTestMethod]
+ [AllDevices]
+ [Resource(typeof(ReadOnlyTexture2D<>))]
+ [Resource(typeof(ReadWriteTexture2D<>))]
+ public void ResourceFromTexture2D(Device device, Type bufferType)
+ {
+ using Texture2D buffer = device.Get().AllocateTexture2D(bufferType, 16, 16);
+
+ using ComPtr d3D12Resource = default;
+
+ InteropServices.GetID3D12Resource(buffer, FX.__uuidof(), (void**)d3D12Resource.GetAddressOf());
+
+ Assert.IsTrue(d3D12Resource.Get() != null);
+ Assert.AreEqual(d3D12Resource.Get()->GetDesc().Dimension, D3D12_RESOURCE_DIMENSION_TEXTURE2D);
+
+ d3D12Resource.Dispose();
+
+ int hResult = InteropServices.TryGetID3D12Resource(buffer, FX.__uuidof(), (void**)d3D12Resource.GetAddressOf());
+
+ Assert.AreEqual(hResult, FX.S_OK);
+ Assert.IsTrue(d3D12Resource.Get() != null);
+ Assert.AreEqual(d3D12Resource.Get()->GetDesc().Dimension, D3D12_RESOURCE_DIMENSION_TEXTURE2D);
+ }
+
+ [CombinatorialTestMethod]
+ [AllDevices]
+ [Resource(typeof(ReadOnlyTexture3D<>))]
+ [Resource(typeof(ReadWriteTexture3D<>))]
+ public void ResourceFromTexture3D(Device device, Type bufferType)
+ {
+ using Texture3D buffer = device.Get().AllocateTexture3D(bufferType, 16, 16, 4);
+
+ using ComPtr d3D12Resource = default;
+
+ InteropServices.GetID3D12Resource(buffer, FX.__uuidof(), (void**)d3D12Resource.GetAddressOf());
+
+ Assert.IsTrue(d3D12Resource.Get() != null);
+ Assert.AreEqual(d3D12Resource.Get()->GetDesc().Dimension, D3D12_RESOURCE_DIMENSION_TEXTURE3D);
+
+ d3D12Resource.Dispose();
+
+ int hResult = InteropServices.TryGetID3D12Resource(buffer, FX.__uuidof(), (void**)d3D12Resource.GetAddressOf());
+
+ Assert.AreEqual(hResult, FX.S_OK);
+ Assert.IsTrue(d3D12Resource.Get() != null);
+ Assert.AreEqual(d3D12Resource.Get()->GetDesc().Dimension, D3D12_RESOURCE_DIMENSION_TEXTURE3D);
+ }
+
+ [CombinatorialTestMethod]
+ [AllDevices]
+ [Resource(typeof(UploadBuffer<>))]
+ [Resource(typeof(ReadBackBuffer<>))]
+ public void ResourceFromTransferBuffer(Device device, Type bufferType)
+ {
+ using TransferBuffer buffer = device.Get().AllocateTransferBuffer(bufferType, 128);
+
+ using ComPtr d3D12Resource = default;
+
+ InteropServices.GetID3D12Resource(buffer, FX.__uuidof(), (void**)d3D12Resource.GetAddressOf());
+
+ Assert.IsTrue(d3D12Resource.Get() != null);
+ Assert.AreEqual(d3D12Resource.Get()->GetDesc().Dimension, D3D12_RESOURCE_DIMENSION_BUFFER);
+
+ d3D12Resource.Dispose();
+
+ int hResult = InteropServices.TryGetID3D12Resource(buffer, FX.__uuidof(), (void**)d3D12Resource.GetAddressOf());
+
+ Assert.AreEqual(hResult, FX.S_OK);
+ Assert.IsTrue(d3D12Resource.Get() != null);
+ Assert.AreEqual(d3D12Resource.Get()->GetDesc().Dimension, D3D12_RESOURCE_DIMENSION_BUFFER);
+ }
+
+ [CombinatorialTestMethod]
+ [AllDevices]
+ [Resource(typeof(UploadTexture2D<>))]
+ [Resource(typeof(ReadBackTexture2D<>))]
+ public void ResourceFromTransferTexture2D(Device device, Type bufferType)
+ {
+ using TransferTexture2D buffer = device.Get().AllocateTransferTexture2D(bufferType, 16, 16);
+
+ using ComPtr d3D12Resource = default;
+
+ InteropServices.GetID3D12Resource(buffer, FX.__uuidof(), (void**)d3D12Resource.GetAddressOf());
+
+ Assert.IsTrue(d3D12Resource.Get() != null);
+ Assert.AreEqual(d3D12Resource.Get()->GetDesc().Dimension, D3D12_RESOURCE_DIMENSION_BUFFER);
+
+ d3D12Resource.Dispose();
+
+ int hResult = InteropServices.TryGetID3D12Resource(buffer, FX.__uuidof(), (void**)d3D12Resource.GetAddressOf());
+
+ Assert.AreEqual(hResult, FX.S_OK);
+ Assert.IsTrue(d3D12Resource.Get() != null);
+ Assert.AreEqual(d3D12Resource.Get()->GetDesc().Dimension, D3D12_RESOURCE_DIMENSION_BUFFER);
+ }
+
+ [CombinatorialTestMethod]
+ [AllDevices]
+ [Resource(typeof(UploadTexture3D<>))]
+ [Resource(typeof(ReadBackTexture3D<>))]
+ public void ResourceFromTransferTexture3D(Device device, Type bufferType)
+ {
+ using TransferTexture3D buffer = device.Get().AllocateTransferTexture3D(bufferType, 16, 16, 4);
+
+ using ComPtr d3D12Resource = default;
+
+ InteropServices.GetID3D12Resource(buffer, FX.__uuidof(), (void**)d3D12Resource.GetAddressOf());
+
+ Assert.IsTrue(d3D12Resource.Get() != null);
+ Assert.AreEqual(d3D12Resource.Get()->GetDesc().Dimension, D3D12_RESOURCE_DIMENSION_BUFFER);
+
+ d3D12Resource.Dispose();
+
+ int hResult = InteropServices.TryGetID3D12Resource(buffer, FX.__uuidof(), (void**)d3D12Resource.GetAddressOf());
+
+ Assert.AreEqual(hResult, FX.S_OK);
+ Assert.IsTrue(d3D12Resource.Get() != null);
+ Assert.AreEqual(d3D12Resource.Get()->GetDesc().Dimension, D3D12_RESOURCE_DIMENSION_BUFFER);
+ }
+ }
+}