Added draft version of TerraFX GraphicsDevice class

このコミットが含まれているのは:
Sergio Pedri committed 2020-11-14 14:07:30 +01:00
1 親 d1d8cf8c6b
コミット ecce9a6e06
3ファイル変更
+231 -23

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

+7 -23
ファイルの表示
@@ -1,7 +1,6 @@
using System;
using System.Runtime.InteropServices;
using ComputeSharp.Graphics.Helpers;
using System;
using TerraFX.Interop;
using FX = TerraFX.Interop.Windows;
namespace ComputeSharp.Graphics
{
@@ -51,28 +50,13 @@ namespace ComputeSharp.Graphics
/// <param name="device">The <see cref="ID3D12Device"/> instance to use</param>
public DescriptorAllocator2(ID3D12Device* device)
{
D3D12_DESCRIPTOR_HEAP_DESC d3d12DescriptorHeapDesc;
d3d12DescriptorHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE.D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
d3d12DescriptorHeapDesc.NumDescriptors = DescriptorsPerHeap;
d3d12DescriptorHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAGS.D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
d3d12DescriptorHeapDesc.NodeMask = 0;
Guid d3d12DescriptorHeapDescGuid = FX.IID_ID3D12DescriptorHeap;
ID3D12DescriptorHeap* d3d12DescriptorHeap;
int result = device->CreateDescriptorHeap(
&d3d12DescriptorHeapDesc,
&d3d12DescriptorHeapDescGuid,
(void**)&d3d12DescriptorHeap);
if (FX.FAILED(result)) Marshal.ThrowExceptionForHR(result);
D3D12DescriptorHeap = d3d12DescriptorHeap;
D3D12DescriptorHeap = D3D12Helper.CreateDescriptorHeap(device, DescriptorsPerHeap);
this.allocationLock = new object();
this.descriptorSize = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE.D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
this.d3d12CpuDescriptorHandle = d3d12DescriptorHeap->GetCPUDescriptorHandleForHeapStart();
this.d3d12GpuDescriptorHandle = d3d12DescriptorHeap->GetGPUDescriptorHandleForHeapStart();
this.remainingHandles = DescriptorsPerHeap;
this.d3d12CpuDescriptorHandle = D3D12DescriptorHeap->GetCPUDescriptorHandleForHeapStart();
this.d3d12GpuDescriptorHandle = D3D12DescriptorHeap->GetGPUDescriptorHandleForHeapStart();
this.remainingHandles = D3D12DescriptorHeap->GetDesc().NumDescriptors;
}
/// <summary>
@@ -86,9 +70,9 @@ namespace ComputeSharp.Graphics
{
if (this.remainingHandles == 0)
{
this.remainingHandles = D3D12DescriptorHeap->GetDesc().NumDescriptors;
this.d3d12CpuDescriptorHandle = D3D12DescriptorHeap->GetCPUDescriptorHandleForHeapStart();
this.d3d12GpuDescriptorHandle = D3D12DescriptorHeap->GetGPUDescriptorHandleForHeapStart();
this.remainingHandles = D3D12DescriptorHeap->GetDesc().NumDescriptors;
}
d3d12CpuDescriptorHandle = this.d3d12CpuDescriptorHandle;
+121
ファイルの表示
@@ -0,0 +1,121 @@
using ComputeSharp.Core.Interop;
using ComputeSharp.Graphics.Commands;
using ComputeSharp.Graphics.Helpers;
using TerraFX.Interop;
namespace ComputeSharp.Graphics
{
/// <summary>
/// A <see langword="class"/> that represents a DX12.1-compatible GPU device that can be used to run compute shaders.
/// </summary>
public sealed unsafe class GraphicsDevice2 : NativeObject
{
/// <summary>
/// The underlying <see cref="ID3D12Device"/> wrapped by the current instance.
/// </summary>
internal readonly ID3D12Device* D3D12Device;
/// <summary>
/// The <see cref="ID3D12CommandQueue"/> instance to use for compute operations.
/// </summary>
internal readonly ID3D12CommandQueue* D3D12ComputeCommandQueue;
/// <summary>
/// The <see cref="ID3D12CommandQueue"/> instance to use for copy operations.
/// </summary>
internal readonly ID3D12CommandQueue* D3D12CopyCommandQueue;
/// <summary>
/// The <see cref="ID3D12Fence"/> instance used for compute operations.
/// </summary>
internal readonly ID3D12Fence* D3D12ComputeFence;
/// <summary>
/// The <see cref="ID3D12Fence"/> instance used for copy operations.
/// </summary>
internal readonly ID3D12Fence* D3D12CopyFence;
/// <summary>
/// The <see cref="CommandAllocatorPool2"/> instance for compute operations.
/// </summary>
internal readonly CommandAllocatorPool2 ComputeCommandAllocatorPool;
/// <summary>
/// Gets the <see cref="CommandAllocatorPool2"/> instance for copy operations.
/// </summary>
internal readonly CommandAllocatorPool2 CopyCommandAllocatorPool;
/// <summary>
/// The <see cref="DescriptorAllocator2"/> instance to use when allocating new buffers.
/// </summary>
internal readonly DescriptorAllocator2 ShaderResourceViewDescriptorAllocator;
/// <summary>
/// The next fence value for compute operations using <see cref="D3D12ComputeCommandQueue"/>.
/// </summary>
internal long NextD3D12ComputeFenceValue = 1;
/// <summary>
/// The next fence value for copy operations using <see cref="D3D12CopyCommandQueue"/>.
/// </summary>
internal long NextD3D12CopyFenceValue = 1;
/// <summary>
/// Creates a new <see cref="GraphicsDevice2"/> instance for the input <see cref="ID3D12Device"/>.
/// </summary>
/// <param name="device">The <see cref="ID3D12Device"/> to use for the new <see cref="GraphicsDevice2"/> instance.</param>
/// <param name="description">The available info for the new <see cref="GraphicsDevice2"/> instance.</param>
internal GraphicsDevice2(ID3D12Device* d3d12device, DXGI_ADAPTER_DESC1* dxgiDescription1)
{
D3D12Device = d3d12device;
D3D12ComputeCommandQueue = D3D12Helper.CreateCommandQueue(d3d12device, D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_COMPUTE);
D3D12CopyCommandQueue = D3D12Helper.CreateCommandQueue(d3d12device, D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_COPY);
D3D12ComputeFence = D3D12Helper.CreateFence(d3d12device);
D3D12CopyFence = D3D12Helper.CreateFence(d3d12device);
ComputeCommandAllocatorPool = new CommandAllocatorPool2(D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_COMPUTE);
CopyCommandAllocatorPool = new CommandAllocatorPool2(D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_COPY);
ShaderResourceViewDescriptorAllocator = new DescriptorAllocator2(d3d12device);
Name = new string((char*)dxgiDescription1->Description);
MemorySize = dxgiDescription1->DedicatedVideoMemory;
var d3D12Options1Data = D3D12Helper.CheckFeatureSupport<D3D12_FEATURE_DATA_D3D12_OPTIONS1>(d3d12device, D3D12_FEATURE.D3D12_FEATURE_D3D12_OPTIONS1);
ComputeUnits = d3D12Options1Data.TotalLaneCount;
WavefrontSize = d3D12Options1Data.WaveLaneCountMin;
}
/// <summary>
/// Gets the name of the current <see cref="GraphicsDevice"/> instance.
/// </summary>
public string Name { get; }
/// <summary>
/// Gets the size of the dedicated video memory for the current <see cref="GraphicsDevice"/> instance.
/// </summary>
public nuint MemorySize { get; }
/// <summary>
/// Gets the number of total lanes on the current device (eg. CUDA cores on an nVidia GPU).
/// </summary>
public uint ComputeUnits { get; }
/// <summary>
/// Gets the number of lanes in a SIMD wave on the current device (also known as "wavefront size" or "warp width").
/// </summary>
public uint WavefrontSize { get; }
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
D3D12Device->Release();
D3D12ComputeCommandQueue->Release();
D3D12CopyCommandQueue->Release();
D3D12ComputeFence->Release();
D3D12CopyFence->Release();
ComputeCommandAllocatorPool.Dispose();
CopyCommandAllocatorPool.Dispose();
ShaderResourceViewDescriptorAllocator.Dispose();
}
}
}
+103
ファイルの表示
@@ -0,0 +1,103 @@
using System;
using System.Diagnostics.Contracts;
using System.Runtime.InteropServices;
using TerraFX.Interop;
using FX = TerraFX.Interop.Windows;
namespace ComputeSharp.Graphics.Helpers
{
/// <summary>
/// A <see langword="class"/> with helper methods to perform some D3D12 operations.
/// </summary>
internal static unsafe class D3D12Helper
{
/// <summary>
/// Creates a new <see cref="ID3D12CommandQueue"/> of the specified type, for a given device.
/// </summary>
/// <param name="d3d12device">The target <see cref="ID3D12Device"/> to use to create the command queue.</param>
/// <param name="type">The type of command queue to create.</param>
/// <returns>A pointer to the newly allocated <see cref="ID3D12CommandQueue"/> instance.</returns>
/// <exception cref="Exception">Thrown when the creation of the command queue fails.</exception>
public static ID3D12CommandQueue* CreateCommandQueue(ID3D12Device* d3d12device, D3D12_COMMAND_LIST_TYPE type)
{
D3D12_COMMAND_QUEUE_DESC d3d12CommandQueueDesc;
d3d12CommandQueueDesc.Type = D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_COMPUTE;
d3d12CommandQueueDesc.Priority = 0;
d3d12CommandQueueDesc.Flags = D3D12_COMMAND_QUEUE_FLAGS.D3D12_COMMAND_QUEUE_FLAG_NONE;
d3d12CommandQueueDesc.NodeMask = 0;
Guid d3d12CommandQueueDescGuid = FX.IID_ID3D12CommandQueue;
ID3D12CommandQueue* commandQueue;
int result = d3d12device->CreateCommandQueue(
&d3d12CommandQueueDesc,
&d3d12CommandQueueDescGuid,
(void**)&commandQueue);
if (FX.FAILED(result)) Marshal.ThrowExceptionForHR(result);
return commandQueue;
}
/// <summary>
/// Creates a new <see cref="ID3D12Fence"/> for a given device.
/// </summary>
/// <param name="d3d12device">The target <see cref="ID3D12Device"/> to use to create the fence.</param>
/// <returns>A pointer to the newly allocated <see cref="ID3D12Fence"/> instance.</returns>
/// <exception cref="Exception">Thrown when the creation of the command queue fails.</exception>
public static ID3D12Fence* CreateFence(ID3D12Device* d3d12device)
{
Guid d3D12FenceGuid = FX.IID_ID3D12Fence;
ID3D12Fence* d3d12Fence;
int result = d3d12device->CreateFence(
0,
D3D12_FENCE_FLAGS.D3D12_FENCE_FLAG_NONE,
&d3D12FenceGuid,
(void**)&d3d12Fence);
if (FX.FAILED(result)) Marshal.ThrowExceptionForHR(result);
return d3d12Fence;
}
/// <summary>
/// Creates a new <see cref="ID3D12DescriptorHeap"/> for a given device.
/// </summary>
/// <param name="d3d12device">The target <see cref="ID3D12Device"/> to use to create the descriptor heap.</param>
/// <param name="descriptorsCount">The number of descriptors to allocate.</param>
/// <returns>A pointer to the newly allocated <see cref="ID3D12DescriptorHeap"/> instance.</returns>
/// <exception cref="Exception">Thrown when the creation of the command queue fails.</exception>
public static ID3D12DescriptorHeap* CreateDescriptorHeap(ID3D12Device* d3d12device, uint descriptorsCount)
{
D3D12_DESCRIPTOR_HEAP_DESC d3d12DescriptorHeapDesc;
d3d12DescriptorHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE.D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
d3d12DescriptorHeapDesc.NumDescriptors = descriptorsCount;
d3d12DescriptorHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAGS.D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
d3d12DescriptorHeapDesc.NodeMask = 0;
Guid d3d12DescriptorHeapDescGuid = FX.IID_ID3D12DescriptorHeap;
ID3D12DescriptorHeap* d3d12DescriptorHeap;
int result = d3d12device->CreateDescriptorHeap(
&d3d12DescriptorHeapDesc,
&d3d12DescriptorHeapDescGuid,
(void**)&d3d12DescriptorHeap);
if (FX.FAILED(result)) Marshal.ThrowExceptionForHR(result);
return d3d12DescriptorHeap;
}
[Pure]
public static unsafe TFeature CheckFeatureSupport<TFeature>(ID3D12Device* d3d12device, D3D12_FEATURE d3d12feature)
where TFeature : unmanaged
{
TFeature feature;
int result = d3d12device->CheckFeatureSupport(d3d12feature, &feature, (uint)sizeof(TFeature));
if (FX.FAILED(result)) Marshal.ThrowExceptionForHR(result);
return feature;
}
}
}