D3D11↔D3D12 ゼロコピー連携のための共有リソース API を追加
このコミットが含まれているのは:
1 親
c3e9ad4faa
コミット
5620f5f4e1
10ファイル変更
+713
-1
検索条件に一致するファイルはありません
@@ -217,6 +217,13 @@ internal unsafe partial struct ID3D12Device : IComObject
|
||||
return ((delegate* unmanaged[MemberFunction]<ID3D12Device*, D3D12_RESOURCE_DESC*, D3D12_RESOURCE_STATES, D3D12_CLEAR_VALUE*, Guid*, void**, int>)(lpVtbl[30]))((ID3D12Device*)Unsafe.AsPointer(ref this), pDesc, InitialState, pOptimizedClearValue, riid, ppvResource);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(31)]
|
||||
public HRESULT CreateSharedHandle(IUnknown* pObject, [NativeTypeName("const SECURITY_ATTRIBUTES *")] SECURITY_ATTRIBUTES* pAttributes, [NativeTypeName("DWORD")] uint Access, [NativeTypeName("LPCWSTR")] ushort* Name, HANDLE* pHandle)
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ID3D12Device*, IUnknown*, SECURITY_ATTRIBUTES*, uint, ushort*, HANDLE*, int>)(lpVtbl[31]))((ID3D12Device*)Unsafe.AsPointer(ref this), pObject, pAttributes, Access, Name, pHandle);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(32)]
|
||||
public HRESULT OpenSharedHandle(HANDLE NTHandle, [NativeTypeName("const IID &")] Guid* riid, void** ppvObj)
|
||||
|
||||
@@ -12,4 +12,7 @@ internal static partial class Windows
|
||||
|
||||
[NativeTypeName("#define GENERIC_WRITE (0x40000000L)")]
|
||||
public const int GENERIC_WRITE = (0x40000000);
|
||||
|
||||
[NativeTypeName("#define GENERIC_ALL (0x10000000L)")]
|
||||
public const uint GENERIC_ALL = (0x10000000);
|
||||
}
|
||||
@@ -270,6 +270,106 @@ internal static unsafe class ID3D12DeviceExtensions
|
||||
return d3D12Resource.Move();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// クロス API 共有が可能なコミット型の 2D テクスチャ リソースを生成します。
|
||||
/// </summary>
|
||||
/// <param name="d3D12Device">使用する <see cref="ID3D12Device"/> インスタンス。</param>
|
||||
/// <param name="resourceType">生成するリソースの種別。</param>
|
||||
/// <param name="dxgiFormat">使用する <see cref="DXGI_FORMAT"/> 値。</param>
|
||||
/// <param name="width">テクスチャの幅。</param>
|
||||
/// <param name="height">テクスチャの高さ。</param>
|
||||
/// <param name="d3D12ResourceStates">生成されたリソースの初期 <see cref="D3D12_RESOURCE_STATES"/> 値。</param>
|
||||
/// <returns>共有可能な <see cref="ID3D12Resource"/> への参照。</returns>
|
||||
public static ComPtr<ID3D12Resource> CreateSharedCommittedResource(
|
||||
this ref ID3D12Device d3D12Device,
|
||||
ResourceType resourceType,
|
||||
DXGI_FORMAT dxgiFormat,
|
||||
uint width,
|
||||
uint height,
|
||||
out D3D12_RESOURCE_STATES d3D12ResourceStates)
|
||||
{
|
||||
D3D12_RESOURCE_FLAGS d3D12ResourceFlags = resourceType switch
|
||||
{
|
||||
ResourceType.ReadOnly => D3D12_RESOURCE_FLAG_NONE,
|
||||
ResourceType.ReadWrite => D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS | D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS,
|
||||
_ => default(ArgumentException).Throw<D3D12_RESOURCE_FLAGS>(nameof(resourceType))
|
||||
};
|
||||
|
||||
d3D12ResourceStates = D3D12_RESOURCE_STATE_COMMON;
|
||||
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = default;
|
||||
|
||||
D3D12_HEAP_PROPERTIES d3D12HeapProperties;
|
||||
d3D12HeapProperties.Type = D3D12_HEAP_TYPE_DEFAULT;
|
||||
d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
|
||||
d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
|
||||
d3D12HeapProperties.CreationNodeMask = 1;
|
||||
d3D12HeapProperties.VisibleNodeMask = 1;
|
||||
|
||||
D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex2D(dxgiFormat, width, height, mipLevels: 1, flags: d3D12ResourceFlags);
|
||||
|
||||
d3D12Device.CreateCommittedResource(
|
||||
&d3D12HeapProperties,
|
||||
D3D12_HEAP_FLAG_SHARED,
|
||||
&d3D12ResourceDescription,
|
||||
d3D12ResourceStates,
|
||||
null,
|
||||
Windows.__uuidof<ID3D12Resource>(),
|
||||
(void**)d3D12Resource.GetAddressOf()).Assert();
|
||||
|
||||
return d3D12Resource.Move();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 他 API と共有可能な <see cref="ID3D12Fence"/> を生成します。
|
||||
/// </summary>
|
||||
/// <param name="d3D12Device">使用する <see cref="ID3D12Device"/> インスタンス。</param>
|
||||
/// <returns>共有可能な <see cref="ID3D12Fence"/> への参照。</returns>
|
||||
public static ComPtr<ID3D12Fence> CreateSharedFence(this ref ID3D12Device d3D12Device)
|
||||
{
|
||||
using ComPtr<ID3D12Fence> d3D12Fence = default;
|
||||
|
||||
d3D12Device.CreateFence(
|
||||
0,
|
||||
D3D12_FENCE_FLAG_SHARED,
|
||||
Windows.__uuidof<ID3D12Fence>(),
|
||||
(void**)d3D12Fence.GetAddressOf()).Assert();
|
||||
|
||||
return d3D12Fence.Move();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 指定した COM オブジェクトに対する共有 NT ハンドルを生成します。
|
||||
/// </summary>
|
||||
/// <param name="d3D12Device">使用する <see cref="ID3D12Device"/> インスタンス。</param>
|
||||
/// <param name="pObject">共有ハンドルを生成する対象の COM オブジェクト。</param>
|
||||
/// <returns>生成された共有 NT ハンドル。呼び出し側が <c>CloseHandle</c> で解放する責任を持ちます。</returns>
|
||||
public static HANDLE CreateSharedHandle(this ref ID3D12Device d3D12Device, IUnknown* pObject)
|
||||
{
|
||||
HANDLE handle;
|
||||
|
||||
d3D12Device.CreateSharedHandle(pObject, null, Windows.GENERIC_ALL, null, &handle).Assert();
|
||||
|
||||
return handle;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 指定した共有 NT ハンドルから <typeparamref name="T"/> 型の COM オブジェクトを開きます。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">開く COM オブジェクトの型。</typeparam>
|
||||
/// <param name="d3D12Device">使用する <see cref="ID3D12Device"/> インスタンス。</param>
|
||||
/// <param name="handle">開く対象の共有 NT ハンドル。</param>
|
||||
/// <returns>開かれた <typeparamref name="T"/> への参照。</returns>
|
||||
public static ComPtr<T> OpenSharedHandle<T>(this ref ID3D12Device d3D12Device, HANDLE handle)
|
||||
where T : unmanaged, IComObject
|
||||
{
|
||||
using ComPtr<T> obj = default;
|
||||
|
||||
d3D12Device.OpenSharedHandle(handle, Windows.__uuidof<T>(), (void**)obj.GetAddressOf()).Assert();
|
||||
|
||||
return obj.Move();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ID3D12InfoQueue"/> for a given device.
|
||||
/// </summary>
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
using ComputeSharp.Core.Extensions;
|
||||
using ComputeSharp.Graphics.Extensions;
|
||||
using ComputeSharp.Win32;
|
||||
using ResourceType = ComputeSharp.Graphics.Resources.Enums.ResourceType;
|
||||
|
||||
namespace ComputeSharp;
|
||||
|
||||
/// <inheritdoc/>
|
||||
unsafe partial class GraphicsDevice
|
||||
{
|
||||
/// <summary>
|
||||
/// クロス API 共有が可能な 2D テクスチャ リソースを生成します。
|
||||
/// </summary>
|
||||
/// <param name="resourceType">生成するリソースの種別。</param>
|
||||
/// <param name="dxgiFormat">使用する <see cref="DXGI_FORMAT"/> 値。</param>
|
||||
/// <param name="width">テクスチャの幅。</param>
|
||||
/// <param name="height">テクスチャの高さ。</param>
|
||||
/// <param name="d3D12Resource">生成された <see cref="ID3D12Resource"/> オブジェクト。</param>
|
||||
/// <param name="d3D12ResourceStates">生成されたリソースの初期 <see cref="D3D12_RESOURCE_STATES"/> 値。</param>
|
||||
internal void CreateSharedResource(
|
||||
ResourceType resourceType,
|
||||
DXGI_FORMAT dxgiFormat,
|
||||
uint width,
|
||||
uint height,
|
||||
out ComPtr<ID3D12Resource> d3D12Resource,
|
||||
out D3D12_RESOURCE_STATES d3D12ResourceStates)
|
||||
{
|
||||
d3D12Resource = this.d3D12Device.Get()->CreateSharedCommittedResource(resourceType, dxgiFormat, width, height, out d3D12ResourceStates);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 指定した COM オブジェクトに対する共有 NT ハンドルを生成します。
|
||||
/// </summary>
|
||||
/// <param name="pObject">共有ハンドルを生成する対象の COM オブジェクト。</param>
|
||||
/// <returns>生成された共有 NT ハンドル。</returns>
|
||||
internal HANDLE CreateSharedHandle(IUnknown* pObject)
|
||||
{
|
||||
return this.d3D12Device.Get()->CreateSharedHandle(pObject);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 指定した共有 NT ハンドルから <see cref="ID3D12Resource"/> を開きます。
|
||||
/// </summary>
|
||||
/// <param name="handle">開く対象の共有 NT ハンドル。</param>
|
||||
/// <returns>開かれた <see cref="ID3D12Resource"/> への参照。</returns>
|
||||
internal ComPtr<ID3D12Resource> OpenSharedResource(HANDLE handle)
|
||||
{
|
||||
return this.d3D12Device.Get()->OpenSharedHandle<ID3D12Resource>(handle);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 他 API と共有可能な <see cref="ID3D12Fence"/> を生成します。
|
||||
/// </summary>
|
||||
/// <returns>共有可能な <see cref="ID3D12Fence"/> への参照。</returns>
|
||||
internal ComPtr<ID3D12Fence> CreateSharedFence()
|
||||
{
|
||||
return this.d3D12Device.Get()->CreateSharedFence();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 指定した共有 NT ハンドルから <see cref="ID3D12Fence"/> を開きます。
|
||||
/// </summary>
|
||||
/// <param name="handle">開く対象の共有 NT ハンドル。</param>
|
||||
/// <returns>開かれた <see cref="ID3D12Fence"/> への参照。</returns>
|
||||
internal ComPtr<ID3D12Fence> OpenSharedFence(HANDLE handle)
|
||||
{
|
||||
return this.d3D12Device.Get()->OpenSharedHandle<ID3D12Fence>(handle);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// コンピュート キュー上で指定した共有フェンスに値をシグナルします。
|
||||
/// </summary>
|
||||
/// <param name="d3D12Fence">対象の <see cref="ID3D12Fence"/>。</param>
|
||||
/// <param name="value">シグナルする値。</param>
|
||||
internal void SignalSharedFence(ID3D12Fence* d3D12Fence, ulong value)
|
||||
{
|
||||
this.d3D12ComputeCommandQueue.Get()->Signal(d3D12Fence, value).Assert();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// コンピュート キュー上で指定した共有フェンスが目標値に達するまで待機するよう登録します。
|
||||
/// </summary>
|
||||
/// <param name="d3D12Fence">対象の <see cref="ID3D12Fence"/>。</param>
|
||||
/// <param name="value">待機する目標値。</param>
|
||||
internal void WaitForSharedFence(ID3D12Fence* d3D12Fence, ulong value)
|
||||
{
|
||||
this.d3D12ComputeCommandQueue.Get()->Wait(d3D12Fence, value).Assert();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
using System;
|
||||
using ComputeSharp.Core.Extensions;
|
||||
using ComputeSharp.Resources;
|
||||
using ComputeSharp.Win32;
|
||||
|
||||
namespace ComputeSharp.Interop;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public static unsafe partial class InteropServices
|
||||
{
|
||||
/// <summary>
|
||||
/// クロス API 共有が可能な <see cref="ReadWriteTexture2D{T}"/> を確保します。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">テクスチャに格納する要素の型。</typeparam>
|
||||
/// <param name="device">テクスチャの確保に使用する <see cref="GraphicsDevice"/>。</param>
|
||||
/// <param name="width">テクスチャの幅。</param>
|
||||
/// <param name="height">テクスチャの高さ。</param>
|
||||
/// <returns>共有可能な <see cref="ReadWriteTexture2D{T}"/> インスタンス。</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="device"/> が <see langword="null"/> の場合にスローされます。</exception>
|
||||
/// <remarks>
|
||||
/// 返されるテクスチャは <see cref="CreateSharedHandle{T}(Texture2D{T})"/> で共有 NT ハンドルをエクスポートでき、
|
||||
/// D3D11 など他 API から <c>OpenSharedResource1</c> で開けます。クロス API の同期は共有フェンスで行ってください。
|
||||
/// </remarks>
|
||||
public static ReadWriteTexture2D<T> AllocateSharedReadWriteTexture2D<T>(GraphicsDevice device, int width, int height)
|
||||
where T : unmanaged
|
||||
{
|
||||
default(ArgumentNullException).ThrowIfNull(device);
|
||||
|
||||
return new ReadWriteTexture2D<T>(device, width, height);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// クロス API 共有が可能な <see cref="ReadOnlyTexture2D{T}"/> を確保します。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">テクスチャに格納する要素の型。</typeparam>
|
||||
/// <param name="device">テクスチャの確保に使用する <see cref="GraphicsDevice"/>。</param>
|
||||
/// <param name="width">テクスチャの幅。</param>
|
||||
/// <param name="height">テクスチャの高さ。</param>
|
||||
/// <returns>共有可能な <see cref="ReadOnlyTexture2D{T}"/> インスタンス。</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="device"/> が <see langword="null"/> の場合にスローされます。</exception>
|
||||
public static ReadOnlyTexture2D<T> AllocateSharedReadOnlyTexture2D<T>(GraphicsDevice device, int width, int height)
|
||||
where T : unmanaged
|
||||
{
|
||||
default(ArgumentNullException).ThrowIfNull(device);
|
||||
|
||||
return new ReadOnlyTexture2D<T>(device, width, height);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 外部 API が所有する共有 NT ハンドルを <see cref="ReadWriteTexture2D{T}"/> として開きます。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">テクスチャに格納する要素の型。</typeparam>
|
||||
/// <param name="device">リソースを開くために使用する <see cref="GraphicsDevice"/>。</param>
|
||||
/// <param name="handle">開く対象の共有 NT ハンドル。</param>
|
||||
/// <returns>共有リソースをラップする <see cref="ReadWriteTexture2D{T}"/> インスタンス。</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="device"/> が <see langword="null"/> の場合にスローされます。</exception>
|
||||
/// <exception cref="ArgumentException">共有リソースが 2D テクスチャでない、またはフォーマットが <typeparamref name="T"/> と一致しない場合にスローされます。</exception>
|
||||
/// <remarks>
|
||||
/// インポートしたリソースを <see cref="ReadWriteTexture2D{T}"/> として書き込み用に使用する場合、元のリソースは UAV 対応かつ
|
||||
/// 同時アクセス可能 (simultaneous access) として作成されている必要があります。クロス API の同期は共有フェンスで行ってください。
|
||||
/// </remarks>
|
||||
public static ReadWriteTexture2D<T> OpenSharedReadWriteTexture2D<T>(GraphicsDevice device, nint handle)
|
||||
where T : unmanaged
|
||||
{
|
||||
default(ArgumentNullException).ThrowIfNull(device);
|
||||
|
||||
using ReferenceTracker.Lease _0 = device.GetReferenceTracker().GetLease();
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = device.OpenSharedResource(new HANDLE((void*)handle));
|
||||
|
||||
return new ReadWriteTexture2D<T>(device, d3D12Resource.Get());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 外部 API が所有する共有 NT ハンドルを <see cref="ReadOnlyTexture2D{T}"/> として開きます。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">テクスチャに格納する要素の型。</typeparam>
|
||||
/// <param name="device">リソースを開くために使用する <see cref="GraphicsDevice"/>。</param>
|
||||
/// <param name="handle">開く対象の共有 NT ハンドル。</param>
|
||||
/// <returns>共有リソースをラップする <see cref="ReadOnlyTexture2D{T}"/> インスタンス。</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="device"/> が <see langword="null"/> の場合にスローされます。</exception>
|
||||
/// <exception cref="ArgumentException">共有リソースが 2D テクスチャでない、またはフォーマットが <typeparamref name="T"/> と一致しない場合にスローされます。</exception>
|
||||
public static ReadOnlyTexture2D<T> OpenSharedReadOnlyTexture2D<T>(GraphicsDevice device, nint handle)
|
||||
where T : unmanaged
|
||||
{
|
||||
default(ArgumentNullException).ThrowIfNull(device);
|
||||
|
||||
using ReferenceTracker.Lease _0 = device.GetReferenceTracker().GetLease();
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = device.OpenSharedResource(new HANDLE((void*)handle));
|
||||
|
||||
return new ReadOnlyTexture2D<T>(device, d3D12Resource.Get());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 共有可能なテクスチャの基になる <see cref="ID3D12Resource"/> に対する共有 NT ハンドルをエクスポートします。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">テクスチャに格納する要素の型。</typeparam>
|
||||
/// <param name="texture">共有ハンドルをエクスポートする対象の <see cref="Texture2D{T}"/>。</param>
|
||||
/// <returns>エクスポートされた共有 NT ハンドル。呼び出し側が <c>CloseHandle</c> で解放する責任を持ちます。</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="texture"/> が <see langword="null"/> の場合にスローされます。</exception>
|
||||
/// <remarks>
|
||||
/// このメソッドは、<see cref="AllocateSharedReadWriteTexture2D{T}(GraphicsDevice, int, int)"/> など共有可能として
|
||||
/// 作成されたテクスチャに対してのみ成功します。共有用に作成されていないテクスチャに対しては失敗します。
|
||||
/// </remarks>
|
||||
public static nint CreateSharedHandle<T>(Texture2D<T> texture)
|
||||
where T : unmanaged
|
||||
{
|
||||
default(ArgumentNullException).ThrowIfNull(texture);
|
||||
|
||||
using ReferenceTracker.Lease _0 = texture.GetReferenceTracker().GetLease();
|
||||
|
||||
HANDLE handle = texture.GraphicsDevice.CreateSharedHandle((IUnknown*)texture.D3D12Resource);
|
||||
|
||||
return (nint)handle.Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// クロス API 同期に使用する共有フェンスを生成し、要求されたインターフェイスとして取得します。
|
||||
/// </summary>
|
||||
/// <param name="device">フェンスの生成に使用する <see cref="GraphicsDevice"/>。</param>
|
||||
/// <param name="riid">取得するフェンス インターフェイスの識別子 (IID) への参照。</param>
|
||||
/// <param name="ppvFence"><paramref name="riid"/> で指定したインターフェイスへのポインターのアドレス。</param>
|
||||
/// <param name="sharedHandle">生成されたフェンスの共有 NT ハンドルを書き込む先のアドレス。呼び出し側が <c>CloseHandle</c> で解放する責任を持ちます。</param>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="device"/> が <see langword="null"/> の場合にスローされます。</exception>
|
||||
/// <exception cref="System.ComponentModel.Win32Exception">フェンスの生成、ハンドルのエクスポート、またはインターフェイスの取得に失敗した場合にスローされます。</exception>
|
||||
/// <remarks>
|
||||
/// エクスポートされた <paramref name="sharedHandle"/> は、D3D11 側で <c>ID3D11Device5::OpenSharedFence</c> により開けます。
|
||||
/// 取得した <paramref name="ppvFence"/> は <see cref="SignalSharedFence(GraphicsDevice, void*, ulong)"/> および
|
||||
/// <see cref="WaitForSharedFence(GraphicsDevice, void*, ulong)"/> に渡して、コンピュート キュー上での同期に使用できます。
|
||||
/// </remarks>
|
||||
public static void CreateSharedFence(GraphicsDevice device, Guid* riid, void** ppvFence, nint* sharedHandle)
|
||||
{
|
||||
default(ArgumentNullException).ThrowIfNull(device);
|
||||
|
||||
using ReferenceTracker.Lease _0 = device.GetReferenceTracker().GetLease();
|
||||
using ComPtr<ID3D12Fence> d3D12Fence = device.CreateSharedFence();
|
||||
|
||||
HANDLE handle = device.CreateSharedHandle((IUnknown*)d3D12Fence.Get());
|
||||
|
||||
*sharedHandle = (nint)handle.Value;
|
||||
|
||||
d3D12Fence.Get()->QueryInterface(riid, ppvFence).Assert();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 共有 NT ハンドルから他 API が生成した共有フェンスを開き、要求されたインターフェイスとして取得します。
|
||||
/// </summary>
|
||||
/// <param name="device">フェンスを開くために使用する <see cref="GraphicsDevice"/>。</param>
|
||||
/// <param name="handle">開く対象の共有 NT ハンドル。</param>
|
||||
/// <param name="riid">取得するフェンス インターフェイスの識別子 (IID) への参照。</param>
|
||||
/// <param name="ppvFence"><paramref name="riid"/> で指定したインターフェイスへのポインターのアドレス。</param>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="device"/> が <see langword="null"/> の場合にスローされます。</exception>
|
||||
/// <exception cref="System.ComponentModel.Win32Exception">フェンスを開く、またはインターフェイスの取得に失敗した場合にスローされます。</exception>
|
||||
public static void OpenSharedFence(GraphicsDevice device, nint handle, Guid* riid, void** ppvFence)
|
||||
{
|
||||
default(ArgumentNullException).ThrowIfNull(device);
|
||||
|
||||
using ReferenceTracker.Lease _0 = device.GetReferenceTracker().GetLease();
|
||||
using ComPtr<ID3D12Fence> d3D12Fence = device.OpenSharedFence(new HANDLE((void*)handle));
|
||||
|
||||
d3D12Fence.Get()->QueryInterface(riid, ppvFence).Assert();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// コンピュート キュー上で指定した共有フェンスに値をシグナルします。
|
||||
/// </summary>
|
||||
/// <param name="device">操作を発行する <see cref="GraphicsDevice"/>。</param>
|
||||
/// <param name="d3D12Fence">対象の <c>ID3D12Fence</c> COM オブジェクトへのポインター。</param>
|
||||
/// <param name="value">シグナルする値。</param>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="device"/> が <see langword="null"/> の場合にスローされます。</exception>
|
||||
/// <exception cref="System.ComponentModel.Win32Exception">シグナルの発行に失敗した場合にスローされます。</exception>
|
||||
public static void SignalSharedFence(GraphicsDevice device, void* d3D12Fence, ulong value)
|
||||
{
|
||||
default(ArgumentNullException).ThrowIfNull(device);
|
||||
|
||||
using ReferenceTracker.Lease _0 = device.GetReferenceTracker().GetLease();
|
||||
|
||||
device.SignalSharedFence((ID3D12Fence*)d3D12Fence, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// コンピュート キュー上で指定した共有フェンスが目標値に達するまで待機するよう登録します。
|
||||
/// </summary>
|
||||
/// <param name="device">操作を発行する <see cref="GraphicsDevice"/>。</param>
|
||||
/// <param name="d3D12Fence">対象の <c>ID3D12Fence</c> COM オブジェクトへのポインター。</param>
|
||||
/// <param name="value">待機する目標値。</param>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="device"/> が <see langword="null"/> の場合にスローされます。</exception>
|
||||
/// <exception cref="System.ComponentModel.Win32Exception">待機の登録に失敗した場合にスローされます。</exception>
|
||||
public static void WaitForSharedFence(GraphicsDevice device, void* d3D12Fence, ulong value)
|
||||
{
|
||||
default(ArgumentNullException).ThrowIfNull(device);
|
||||
|
||||
using ReferenceTracker.Lease _0 = device.GetReferenceTracker().GetLease();
|
||||
|
||||
device.WaitForSharedFence((ID3D12Fence*)d3D12Fence, value);
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ namespace ComputeSharp.Interop;
|
||||
/// that relies on resources being disposed as soon as the relative allocation object is disposed.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static unsafe class InteropServices
|
||||
public static unsafe partial class InteropServices
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the underlying COM object for a given device, as a specified interface. This method invokes
|
||||
|
||||
@@ -11,6 +11,7 @@ using ComputeSharp.Interop.Allocation;
|
||||
using ComputeSharp.Resources.Interop;
|
||||
using ComputeSharp.Win32;
|
||||
using static ComputeSharp.Win32.D3D12_COMMAND_LIST_TYPE;
|
||||
using static ComputeSharp.Win32.D3D12_RESOURCE_DIMENSION;
|
||||
using static ComputeSharp.Win32.D3D12_RESOURCE_STATES;
|
||||
using static ComputeSharp.Win32.D3D12_SRV_DIMENSION;
|
||||
using static ComputeSharp.Win32.D3D12_UAV_DIMENSION;
|
||||
@@ -127,6 +128,141 @@ public abstract unsafe partial class Texture2D<T> : IReferenceTrackedObject, IGr
|
||||
this.d3D12Resource.Get()->SetName(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// クロス API 共有が可能な <see cref="Texture2D{T}"/> インスタンスを生成します。
|
||||
/// </summary>
|
||||
/// <param name="device">現在のインスタンスに関連付ける <see cref="ComputeSharp.GraphicsDevice"/>。</param>
|
||||
/// <param name="width">テクスチャの幅。</param>
|
||||
/// <param name="height">テクスチャの高さ。</param>
|
||||
/// <param name="resourceType">現在のテクスチャのリソース種別。</param>
|
||||
/// <param name="d3D12FormatSupport">現在のテクスチャ型のフォーマット サポート。</param>
|
||||
private protected Texture2D(GraphicsDevice device, int width, int height, ResourceType resourceType, D3D12_FORMAT_SUPPORT1 d3D12FormatSupport)
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = ReferenceTracker.Create(this, out this.referenceTracker);
|
||||
|
||||
default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION);
|
||||
default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION);
|
||||
|
||||
using ReferenceTracker.Lease _1 = device.GetReferenceTracker().GetLease();
|
||||
|
||||
device.ThrowIfDeviceLost();
|
||||
|
||||
if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType<T>(), d3D12FormatSupport))
|
||||
{
|
||||
UnsupportedTextureTypeException.ThrowForTexture2D<T>();
|
||||
}
|
||||
|
||||
GraphicsDevice = device;
|
||||
|
||||
device.CreateSharedResource(
|
||||
resourceType,
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
out this.d3D12Resource,
|
||||
out this.d3D12ResourceState);
|
||||
|
||||
this.d3D12CommandListType = this.d3D12ResourceState == D3D12_RESOURCE_STATE_COMMON
|
||||
? D3D12_COMMAND_LIST_TYPE_COPY
|
||||
: D3D12_COMMAND_LIST_TYPE_COMPUTE;
|
||||
|
||||
device.D3D12Device->GetCopyableFootprint(
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
out this.d3D12PlacedSubresourceFootprint,
|
||||
out _,
|
||||
out _);
|
||||
|
||||
device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles);
|
||||
|
||||
switch (resourceType)
|
||||
{
|
||||
case ResourceType.ReadOnly:
|
||||
device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType<T>(), D3D12_SRV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
|
||||
break;
|
||||
case ResourceType.ReadWrite:
|
||||
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType<T>(), D3D12_UAV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
|
||||
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType<T>(), D3D12_UAV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible);
|
||||
break;
|
||||
}
|
||||
|
||||
this.d3D12Resource.Get()->SetName(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 外部 API が所有する共有リソースをラップする <see cref="Texture2D{T}"/> インスタンスを生成します。
|
||||
/// </summary>
|
||||
/// <param name="device">現在のインスタンスに関連付ける <see cref="ComputeSharp.GraphicsDevice"/>。</param>
|
||||
/// <param name="d3D12Resource">ラップ対象の、共有ハンドルから開かれた <see cref="ID3D12Resource"/>。</param>
|
||||
/// <param name="resourceType">現在のテクスチャのリソース種別。</param>
|
||||
/// <param name="d3D12FormatSupport">現在のテクスチャ型のフォーマット サポート。</param>
|
||||
private protected Texture2D(GraphicsDevice device, ID3D12Resource* d3D12Resource, ResourceType resourceType, D3D12_FORMAT_SUPPORT1 d3D12FormatSupport)
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = ReferenceTracker.Create(this, out this.referenceTracker);
|
||||
|
||||
using ReferenceTracker.Lease _1 = device.GetReferenceTracker().GetLease();
|
||||
|
||||
device.ThrowIfDeviceLost();
|
||||
|
||||
if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType<T>(), d3D12FormatSupport))
|
||||
{
|
||||
UnsupportedTextureTypeException.ThrowForTexture2D<T>();
|
||||
}
|
||||
|
||||
D3D12_RESOURCE_DESC d3D12ResourceDescription = d3D12Resource->GetDesc();
|
||||
|
||||
if (d3D12ResourceDescription.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D)
|
||||
{
|
||||
static void Throw() => throw new ArgumentException("The shared resource is not a 2D texture.", "handle");
|
||||
|
||||
Throw();
|
||||
}
|
||||
|
||||
if (d3D12ResourceDescription.Format != DXGIFormatHelper.GetForType<T>())
|
||||
{
|
||||
static void Throw() => throw new ArgumentException($"The shared resource format does not match the texture element type '{typeof(T)}'.", "handle");
|
||||
|
||||
Throw();
|
||||
}
|
||||
|
||||
int width = (int)d3D12ResourceDescription.Width;
|
||||
int height = (int)d3D12ResourceDescription.Height;
|
||||
|
||||
default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION);
|
||||
default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION);
|
||||
|
||||
GraphicsDevice = device;
|
||||
|
||||
this.d3D12Resource = new ComPtr<ID3D12Resource>(d3D12Resource);
|
||||
this.d3D12ResourceState = D3D12_RESOURCE_STATE_COMMON;
|
||||
|
||||
this.d3D12CommandListType = D3D12_COMMAND_LIST_TYPE_COPY;
|
||||
|
||||
device.D3D12Device->GetCopyableFootprint(
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
out this.d3D12PlacedSubresourceFootprint,
|
||||
out _,
|
||||
out _);
|
||||
|
||||
device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles);
|
||||
|
||||
switch (resourceType)
|
||||
{
|
||||
case ResourceType.ReadOnly:
|
||||
device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType<T>(), D3D12_SRV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
|
||||
break;
|
||||
case ResourceType.ReadWrite:
|
||||
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType<T>(), D3D12_UAV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
|
||||
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType<T>(), D3D12_UAV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible);
|
||||
break;
|
||||
}
|
||||
|
||||
this.d3D12Resource.Get()->SetName(this);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public GraphicsDevice GraphicsDevice { get; }
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ using System.Diagnostics;
|
||||
using ComputeSharp.Graphics.Resources.Enums;
|
||||
using ComputeSharp.Resources;
|
||||
using ComputeSharp.Resources.Debug;
|
||||
using ComputeSharp.Win32;
|
||||
using static ComputeSharp.Win32.D3D12_FORMAT_SUPPORT1;
|
||||
|
||||
namespace ComputeSharp;
|
||||
@@ -27,6 +28,27 @@ public sealed class ReadOnlyTexture2D<T> : Texture2D<T>
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// クロス API 共有が可能な <see cref="ReadOnlyTexture2D{T}"/> インスタンスを生成します。
|
||||
/// </summary>
|
||||
/// <param name="device">現在のインスタンスに関連付ける <see cref="GraphicsDevice"/>。</param>
|
||||
/// <param name="width">テクスチャの幅。</param>
|
||||
/// <param name="height">テクスチャの高さ。</param>
|
||||
internal ReadOnlyTexture2D(GraphicsDevice device, int width, int height)
|
||||
: base(device, width, height, ResourceType.ReadOnly, D3D12_FORMAT_SUPPORT1_TEXTURE2D)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 外部 API が所有する共有リソースをラップする <see cref="ReadOnlyTexture2D{T}"/> インスタンスを生成します。
|
||||
/// </summary>
|
||||
/// <param name="device">現在のインスタンスに関連付ける <see cref="GraphicsDevice"/>。</param>
|
||||
/// <param name="d3D12Resource">ラップ対象の、共有ハンドルから開かれた <see cref="ID3D12Resource"/>。</param>
|
||||
internal unsafe ReadOnlyTexture2D(GraphicsDevice device, ID3D12Resource* d3D12Resource)
|
||||
: base(device, d3D12Resource, ResourceType.ReadOnly, D3D12_FORMAT_SUPPORT1_TEXTURE2D)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a single <typeparamref name="T"/> value from the current readonly texture.
|
||||
/// </summary>
|
||||
|
||||
@@ -2,6 +2,7 @@ using System.Diagnostics;
|
||||
using ComputeSharp.Graphics.Resources.Enums;
|
||||
using ComputeSharp.Resources;
|
||||
using ComputeSharp.Resources.Debug;
|
||||
using ComputeSharp.Win32;
|
||||
using static ComputeSharp.Win32.D3D12_FORMAT_SUPPORT1;
|
||||
|
||||
namespace ComputeSharp;
|
||||
@@ -27,6 +28,27 @@ public sealed partial class ReadWriteTexture2D<T> : Texture2D<T>
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// クロス API 共有が可能な <see cref="ReadWriteTexture2D{T}"/> インスタンスを生成します。
|
||||
/// </summary>
|
||||
/// <param name="device">現在のインスタンスに関連付ける <see cref="GraphicsDevice"/>。</param>
|
||||
/// <param name="width">テクスチャの幅。</param>
|
||||
/// <param name="height">テクスチャの高さ。</param>
|
||||
internal ReadWriteTexture2D(GraphicsDevice device, int width, int height)
|
||||
: base(device, width, height, ResourceType.ReadWrite, D3D12_FORMAT_SUPPORT1_TEXTURE2D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 外部 API が所有する共有リソースをラップする <see cref="ReadWriteTexture2D{T}"/> インスタンスを生成します。
|
||||
/// </summary>
|
||||
/// <param name="device">現在のインスタンスに関連付ける <see cref="GraphicsDevice"/>。</param>
|
||||
/// <param name="d3D12Resource">ラップ対象の、共有ハンドルから開かれた <see cref="ID3D12Resource"/>。</param>
|
||||
internal unsafe ReadWriteTexture2D(GraphicsDevice device, ID3D12Resource* d3D12Resource)
|
||||
: base(device, d3D12Resource, ResourceType.ReadWrite, D3D12_FORMAT_SUPPORT1_TEXTURE2D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a single <typeparamref name="T"/> value from the current writeable texture.
|
||||
/// </summary>
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using ComputeSharp.Interop;
|
||||
using ComputeSharp.Tests.Attributes;
|
||||
using ComputeSharp.Tests.Extensions;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using TerraFX.Interop.DirectX;
|
||||
using TerraFX.Interop.Windows;
|
||||
|
||||
namespace ComputeSharp.Tests;
|
||||
|
||||
public unsafe partial class InteropServicesTests
|
||||
{
|
||||
[DllImport("kernel32", ExactSpelling = true)]
|
||||
private static extern int CloseHandle(nint hObject);
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
public void SharedReadWriteTexture2D_ComputeShaderRoundTrip(Device device)
|
||||
{
|
||||
using ReadWriteTexture2D<float> texture = InteropServices.AllocateSharedReadWriteTexture2D<float>(device.Get(), 16, 16);
|
||||
|
||||
device.Get().For(16, 16, new SharedTextureFillShader(texture));
|
||||
|
||||
float[] result = new float[16 * 16];
|
||||
|
||||
texture.CopyTo(result);
|
||||
|
||||
for (int y = 0; y < 16; y++)
|
||||
{
|
||||
for (int x = 0; x < 16; x++)
|
||||
{
|
||||
Assert.AreEqual(x + y, result[(y * 16) + x]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
public void SharedReadWriteTexture2D_RoundTripThroughSharedHandle(Device device)
|
||||
{
|
||||
using ReadWriteTexture2D<float> source = InteropServices.AllocateSharedReadWriteTexture2D<float>(device.Get(), 16, 16);
|
||||
|
||||
device.Get().For(16, 16, new SharedTextureFillShader(source));
|
||||
|
||||
nint handle = InteropServices.CreateSharedHandle(source);
|
||||
|
||||
Assert.AreNotEqual(0, handle);
|
||||
|
||||
try
|
||||
{
|
||||
using ReadWriteTexture2D<float> opened = InteropServices.OpenSharedReadWriteTexture2D<float>(device.Get(), handle);
|
||||
|
||||
Assert.AreEqual(16, opened.Width);
|
||||
Assert.AreEqual(16, opened.Height);
|
||||
|
||||
float[] result = new float[16 * 16];
|
||||
|
||||
opened.CopyTo(result);
|
||||
|
||||
for (int y = 0; y < 16; y++)
|
||||
{
|
||||
for (int x = 0; x < 16; x++)
|
||||
{
|
||||
Assert.AreEqual(x + y, result[(y * 16) + x]);
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_ = CloseHandle(handle);
|
||||
}
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
public void SharedReadOnlyTexture2D_AllocateAndExportHandle(Device device)
|
||||
{
|
||||
using ReadOnlyTexture2D<float> texture = InteropServices.AllocateSharedReadOnlyTexture2D<float>(device.Get(), 16, 16);
|
||||
|
||||
Assert.AreEqual(16, texture.Width);
|
||||
Assert.AreEqual(16, texture.Height);
|
||||
|
||||
nint handle = InteropServices.CreateSharedHandle(texture);
|
||||
|
||||
Assert.AreNotEqual(0, handle);
|
||||
|
||||
_ = CloseHandle(handle);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
public void SharedFence_SignalReachesValueOnComputeQueue(Device device)
|
||||
{
|
||||
using ComPtr<ID3D12Fence> d3D12Fence = default;
|
||||
|
||||
nint handle = 0;
|
||||
|
||||
InteropServices.CreateSharedFence(device.Get(), Windows.__uuidof<ID3D12Fence>(), (void**)d3D12Fence.GetAddressOf(), &handle);
|
||||
|
||||
Assert.IsTrue(d3D12Fence.Get() != null);
|
||||
Assert.AreNotEqual(0, handle);
|
||||
|
||||
try
|
||||
{
|
||||
Assert.AreEqual(0u, d3D12Fence.Get()->GetCompletedValue());
|
||||
|
||||
InteropServices.SignalSharedFence(device.Get(), d3D12Fence.Get(), 7);
|
||||
|
||||
int spin = 0;
|
||||
|
||||
while (d3D12Fence.Get()->GetCompletedValue() < 7 && spin++ < 10000)
|
||||
{
|
||||
Thread.Sleep(1);
|
||||
}
|
||||
|
||||
Assert.AreEqual(7u, d3D12Fence.Get()->GetCompletedValue());
|
||||
}
|
||||
finally
|
||||
{
|
||||
_ = CloseHandle(handle);
|
||||
}
|
||||
}
|
||||
|
||||
[AutoConstructor]
|
||||
[ThreadGroupSize(DefaultThreadGroupSizes.XY)]
|
||||
[GeneratedComputeShaderDescriptor]
|
||||
internal readonly partial struct SharedTextureFillShader : IComputeShader
|
||||
{
|
||||
public readonly ReadWriteTexture2D<float> texture;
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
this.texture[ThreadIds.XY] = ThreadIds.X + ThreadIds.Y;
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする