Added non-try InteropServices APIs and unit tests

このコミットが含まれているのは:
Sergio Pedri committed 2021-03-12 17:04:17 +01:00
1 親 a94e5559a0
コミット d9298a93e8
2ファイル変更
+293

検索条件に一致するファイルはありません

+105
ファイルの表示
@@ -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
/// </summary>
public static unsafe class InteropServices
{
/// <summary>
/// Gets the underlying COM object for a given device, as a specified interface. This method invokes
/// <see href="https://docs.microsoft.com/windows/win32/api/unknwn/nf-unknwn-iunknown-queryinterface(refiid_void)">IUnknown::QueryInterface</see>.
/// </summary>
/// <param name="device">The input <see cref="GraphicsDevice"/> instance in use.</param>
/// <param name="riid">A reference to the interface identifier (IID) of the device interface being queried for.</param>
/// <param name="ppvObject">The address of a pointer to an interface with the IID specified in <paramref name="riid"/>.</param>
/// <exception cref="Win32Exception">Thrown if the <c>IUnknown::QueryInterface</c> call doesn't return <c>S_OK</c>.</exception>
public static void GetID3D12Device(GraphicsDevice device, Guid* riid, void** ppvObject)
{
device.D3D12Device->QueryInterface(riid, ppvObject).Assert();
}
/// <summary>
/// Gets the underlying COM object for a given resource, as a specified interface. This method invokes
/// <see href="https://docs.microsoft.com/windows/win32/api/unknwn/nf-unknwn-iunknown-queryinterface(refiid_void)">IUnknown::QueryInterface</see>.
/// </summary>
/// <typeparam name="T">The type of items stored on the buffer.</typeparam>
/// <param name="buffer">The input <see cref="Buffer{T}"/> instance in use.</param>
/// <param name="riid">A reference to the interface identifier (IID) of the resource interface being queried for.</param>
/// <param name="ppvObject">The address of a pointer to an interface with the IID specified in <paramref name="riid"/>.</param>
/// <exception cref="Win32Exception">Thrown if the <c>IUnknown::QueryInterface</c> call doesn't return <c>S_OK</c>.</exception>
public static void GetID3D12Resource<T>(Buffer<T> buffer, Guid* riid, void** ppvObject)
where T : unmanaged
{
buffer.D3D12Resource->QueryInterface(riid, ppvObject).Assert();
}
/// <summary>
/// Gets the underlying COM object for a given resource, as a specified interface. This method invokes
/// <see href="https://docs.microsoft.com/windows/win32/api/unknwn/nf-unknwn-iunknown-queryinterface(refiid_void)">IUnknown::QueryInterface</see>.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The input <see cref="Texture2D{T}"/> instance in use.</param>
/// <param name="riid">A reference to the interface identifier (IID) of the resource interface being queried for.</param>
/// <param name="ppvObject">The address of a pointer to an interface with the IID specified in <paramref name="riid"/>.</param>
/// <exception cref="Win32Exception">Thrown if the <c>IUnknown::QueryInterface</c> call doesn't return <c>S_OK</c>.</exception>
public static void GetID3D12Resource<T>(Texture2D<T> texture, Guid* riid, void** ppvObject)
where T : unmanaged
{
texture.D3D12Resource->QueryInterface(riid, ppvObject).Assert();
}
/// <summary>
/// Gets the underlying COM object for a given resource, as a specified interface. This method invokes
/// <see href="https://docs.microsoft.com/windows/win32/api/unknwn/nf-unknwn-iunknown-queryinterface(refiid_void)">IUnknown::QueryInterface</see>.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The input <see cref="Texture3D{T}"/> instance in use.</param>
/// <param name="riid">A reference to the interface identifier (IID) of the resource interface being queried for.</param>
/// <param name="ppvObject">The address of a pointer to an interface with the IID specified in <paramref name="riid"/>.</param>
/// <exception cref="Win32Exception">Thrown if the <c>IUnknown::QueryInterface</c> call doesn't return <c>S_OK</c>.</exception>
public static void GetID3D12Resource<T>(Texture3D<T> texture, Guid* riid, void** ppvObject)
where T : unmanaged
{
texture.D3D12Resource->QueryInterface(riid, ppvObject).Assert();
}
/// <summary>
/// Gets the underlying COM object for a given resource, as a specified interface. This method invokes
/// <see href="https://docs.microsoft.com/windows/win32/api/unknwn/nf-unknwn-iunknown-queryinterface(refiid_void)">IUnknown::QueryInterface</see>.
/// </summary>
/// <typeparam name="T">The type of items stored on the buffer.</typeparam>
/// <param name="buffer">The input <see cref="TransferBuffer{T}"/> instance in use.</param>
/// <param name="riid">A reference to the interface identifier (IID) of the resource interface being queried for.</param>
/// <param name="ppvObject">The address of a pointer to an interface with the IID specified in <paramref name="riid"/>.</param>
/// <exception cref="Win32Exception">Thrown if the <c>IUnknown::QueryInterface</c> call doesn't return <c>S_OK</c>.</exception>
public static void GetID3D12Resource<T>(TransferBuffer<T> buffer, Guid* riid, void** ppvObject)
where T : unmanaged
{
buffer.D3D12Resource->QueryInterface(riid, ppvObject).Assert();
}
/// <summary>
/// Gets the underlying COM object for a given resource, as a specified interface. This method invokes
/// <see href="https://docs.microsoft.com/windows/win32/api/unknwn/nf-unknwn-iunknown-queryinterface(refiid_void)">IUnknown::QueryInterface</see>.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The input <see cref="TransferTexture2D{T}"/> instance in use.</param>
/// <param name="riid">A reference to the interface identifier (IID) of the resource interface being queried for.</param>
/// <param name="ppvObject">The address of a pointer to an interface with the IID specified in <paramref name="riid"/>.</param>
/// <exception cref="Win32Exception">Thrown if the <c>IUnknown::QueryInterface</c> call doesn't return <c>S_OK</c>.</exception>
public static void GetID3D12Resource<T>(TransferTexture2D<T> texture, Guid* riid, void** ppvObject)
where T : unmanaged
{
texture.D3D12Resource->QueryInterface(riid, ppvObject).Assert();
}
/// <summary>
/// Gets the underlying COM object for a given resource, as a specified interface. This method invokes
/// <see href="https://docs.microsoft.com/windows/win32/api/unknwn/nf-unknwn-iunknown-queryinterface(refiid_void)">IUnknown::QueryInterface</see>.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The input <see cref="TransferTexture3D{T}"/> instance in use.</param>
/// <param name="riid">A reference to the interface identifier (IID) of the resource interface being queried for.</param>
/// <param name="ppvObject">The address of a pointer to an interface with the IID specified in <paramref name="riid"/>.</param>
/// <exception cref="Win32Exception">Thrown if the <c>IUnknown::QueryInterface</c> call doesn't return <c>S_OK</c>.</exception>
public static void GetID3D12Resource<T>(TransferTexture3D<T> texture, Guid* riid, void** ppvObject)
where T : unmanaged
{
texture.D3D12Resource->QueryInterface(riid, ppvObject).Assert();
}
/// <summary>
/// Tries to get the underlying COM object for a given device, as a specified interface. This method invokes
/// <see href="https://docs.microsoft.com/windows/win32/api/unknwn/nf-unknwn-iunknown-queryinterface(refiid_void)">IUnknown::QueryInterface</see>.
+188
ファイルの表示
@@ -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<ID3D12Device> d3D12Device = default;
InteropServices.GetID3D12Device(device.Get(), FX.__uuidof<ID3D12Device>(), (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<ID3D12Device>(), (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<float> buffer = device.Get().AllocateBuffer<float>(bufferType, 128);
using ComPtr<ID3D12Resource> d3D12Resource = default;
InteropServices.GetID3D12Resource(buffer, FX.__uuidof<ID3D12Resource>(), (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<ID3D12Resource>(), (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<float> buffer = device.Get().AllocateTexture2D<float>(bufferType, 16, 16);
using ComPtr<ID3D12Resource> d3D12Resource = default;
InteropServices.GetID3D12Resource(buffer, FX.__uuidof<ID3D12Resource>(), (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<ID3D12Resource>(), (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<float> buffer = device.Get().AllocateTexture3D<float>(bufferType, 16, 16, 4);
using ComPtr<ID3D12Resource> d3D12Resource = default;
InteropServices.GetID3D12Resource(buffer, FX.__uuidof<ID3D12Resource>(), (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<ID3D12Resource>(), (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<float> buffer = device.Get().AllocateTransferBuffer<float>(bufferType, 128);
using ComPtr<ID3D12Resource> d3D12Resource = default;
InteropServices.GetID3D12Resource(buffer, FX.__uuidof<ID3D12Resource>(), (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<ID3D12Resource>(), (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<float> buffer = device.Get().AllocateTransferTexture2D<float>(bufferType, 16, 16);
using ComPtr<ID3D12Resource> d3D12Resource = default;
InteropServices.GetID3D12Resource(buffer, FX.__uuidof<ID3D12Resource>(), (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<ID3D12Resource>(), (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<float> buffer = device.Get().AllocateTransferTexture3D<float>(bufferType, 16, 16, 4);
using ComPtr<ID3D12Resource> d3D12Resource = default;
InteropServices.GetID3D12Resource(buffer, FX.__uuidof<ID3D12Resource>(), (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<ID3D12Resource>(), (void**)d3D12Resource.GetAddressOf());
Assert.AreEqual(hResult, FX.S_OK);
Assert.IsTrue(d3D12Resource.Get() != null);
Assert.AreEqual(d3D12Resource.Get()->GetDesc().Dimension, D3D12_RESOURCE_DIMENSION_BUFFER);
}
}
}