Merge pull request #83 from Sergio0694/feature/warp-fallback

Added WARP device option
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2021-01-03 14:15:35 +01:00
コミット 6bfd6d8a98
17ファイル変更
+318 -124

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

+12 -3
ファイルの表示
@@ -1,5 +1,6 @@
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using ComputeSharp.Graphics;
namespace ComputeSharp.Benchmark
{
@@ -53,6 +54,7 @@ namespace ComputeSharp.Benchmark
/// <summary>
/// Executes the forward pass on a fully connected layer on the GPU.
/// </summary>
/// <param name="device">The device to run the computation on.</param>
/// <param name="c">The number of samples in the input tensor.</param>
/// <param name="n">The number of rows in the input matrix.</param>
/// <param name="m">The number of columns in the input matrix.</param>
@@ -63,10 +65,17 @@ namespace ComputeSharp.Benchmark
/// <param name="y">The result tensor.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void FullyConnectedForwardGpu(
int c, int n, int m, int p,
ReadOnlyBuffer<float> x, ReadOnlyBuffer<float> w, ReadOnlyBuffer<float> b, ReadWriteBuffer<float> y)
GraphicsDevice device,
int c,
int n,
int m,
int p,
ReadOnlyBuffer<float> x,
ReadOnlyBuffer<float> w,
ReadOnlyBuffer<float> b,
ReadWriteBuffer<float> y)
{
Gpu.Default.For(c, n, p, new FullyConnectedForwardKernel(n, m, p, x, w, b, y));
device.For(c, n, p, new FullyConnectedForwardKernel(n, m, p, x, w, b, y));
}
/// <summary>
+19 -24
ファイルの表示
@@ -1,6 +1,5 @@
using System;
using System.Buffers;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Engines;
@@ -54,22 +53,22 @@ namespace ComputeSharp.Benchmark
private float[] Y;
/// <summary>
/// The input tensor.
/// The input tensor (GPU).
/// </summary>
private ReadOnlyBuffer<float> BufferX;
/// <summary>
/// The weights tensor.
/// The weights tensor (GPU).
/// </summary>
private ReadOnlyBuffer<float> BufferW;
/// <summary>
/// The bias tensor.
/// The bias tensor (GPU).
/// </summary>
private ReadOnlyBuffer<float> BufferB;
/// <summary>
/// The result tensor.
/// The result tensor (GPU).
/// </summary>
private ReadWriteBuffer<float> BufferY;
@@ -84,6 +83,19 @@ namespace ComputeSharp.Benchmark
[GlobalSetup]
public void Setup()
{
// Creates a new random normalized float[] array of a given size
float[] CreateRandomArray(int size)
{
float[] array = ArrayPool<float>.Shared.Rent(size);
ref float r = ref array[0];
for (int i = 0; i < size; i++)
{
Unsafe.Add(ref r, i) = (float)Random.NextDouble();
}
return array;
}
X = CreateRandomArray(C * N * M);
W = CreateRandomArray(M * P);
B = CreateRandomArray(P);
@@ -112,23 +124,6 @@ namespace ComputeSharp.Benchmark
BufferY.Dispose();
}
/// <summary>
/// Creates a new <see langword="float"/> array of the specified size.
/// </summary>
/// <param name="size">The size of the new random array to create.</param>
[Pure]
private float[] CreateRandomArray(int size)
{
float[] array = ArrayPool<float>.Shared.Rent(size);
ref float r = ref array[0];
for (int i = 0; i < size; i++)
{
Unsafe.Add(ref r, i) = (float)Random.NextDouble();
}
return array;
}
/// <summary>
/// Runs a fully connected forward operation on the CPU.
/// </summary>
@@ -139,7 +134,7 @@ namespace ComputeSharp.Benchmark
/// Runs a fully connected forward operation on the GPU.
/// </summary>
[Benchmark]
public void GpuWithNoTemporaryBuffers() => Dnn.FullyConnectedForwardGpu(C, N, M, P, BufferX, BufferW, BufferB, BufferY);
public void GpuWithNoTemporaryBuffers() => Dnn.FullyConnectedForwardGpu(Gpu.Default, C, N, M, P, BufferX, BufferW, BufferB, BufferY);
/// <summary>
/// Runs a fully connected forward operation on the GPU, creating temporary GPU buffers to perform the operations.
@@ -152,7 +147,7 @@ namespace ComputeSharp.Benchmark
using ReadOnlyBuffer<float> b = Gpu.Default.AllocateReadOnlyBuffer(B);
using ReadWriteBuffer<float> y = Gpu.Default.AllocateReadWriteBuffer(Y);
Dnn.FullyConnectedForwardGpu(C, N, M, P, x, w, b, y);
Dnn.FullyConnectedForwardGpu(Gpu.Default, C, N, M, P, x, w, b, y);
y.GetData(Y);
}
+1
ファイルの表示
@@ -3,3 +3,4 @@
[assembly: InternalsVisibleTo("ComputeSharp.Graphics")]
[assembly: InternalsVisibleTo("ComputeSharp.Shaders")]
[assembly: InternalsVisibleTo("ComputeSharp.SourceGenerators")]
[assembly: InternalsVisibleTo("ComputeSharp.Tests.Internals")]
+1 -1
ファイルの表示
@@ -64,7 +64,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
GraphicsDevice = device;
Length = length;
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, (ulong)SizeInBytes);
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, (ulong)SizeInBytes, device.IsCacheCoherentUMA);
device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle);
+70 -36
ファイルの表示
@@ -37,28 +37,45 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
Guard.IsInRange(offset, 0, Length, nameof(offset));
Guard.IsLessThanOrEqualTo(offset + size, Length, nameof(size));
nint
byteOffset = (nint)offset * sizeof(T),
byteSize = size * sizeof(T);
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, (ulong)byteSize);
using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY))
if (GraphicsDevice.IsCacheCoherentUMA)
{
copyCommandList.CopyBufferRegion(d3D12Resource.Get(), 0, D3D12Resource, (ulong)byteOffset,(ulong)byteSize);
copyCommandList.ExecuteAndWaitForCompletion();
using ID3D12ResourceMap resource = D3D12Resource->Map();
fixed (void* destinationPointer = &destination)
{
MemoryHelper.Copy(
resource.Pointer,
(uint)offset,
(uint)size,
(uint)sizeof(T),
destinationPointer);
}
}
using ID3D12ResourceMap resource = d3D12Resource.Get()->Map();
fixed (void* destinationPointer = &destination)
else
{
MemoryHelper.Copy(
resource.Pointer,
0u,
(uint)size,
(uint)sizeof(T),
destinationPointer);
nint
byteOffset = (nint)offset * sizeof(T),
byteSize = size * sizeof(T);
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, (ulong)byteSize, false);
using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY))
{
copyCommandList.CopyBufferRegion(d3D12Resource.Get(), 0, D3D12Resource, (ulong)byteOffset, (ulong)byteSize);
copyCommandList.ExecuteAndWaitForCompletion();
}
using ID3D12ResourceMap resource = d3D12Resource.Get()->Map();
fixed (void* destinationPointer = &destination)
{
MemoryHelper.Copy(
resource.Pointer,
0u,
(uint)size,
(uint)sizeof(T),
destinationPointer);
}
}
}
@@ -72,27 +89,44 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
Guard.IsInRange(offset, 0, Length, nameof(offset));
Guard.IsLessThanOrEqualTo(offset + size, Length, nameof(size));
nint
byteOffset = (nint)offset * sizeof(T),
byteSize = size * sizeof(T);
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, (ulong)byteSize);
using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
fixed (void* sourcePointer = &source)
if (GraphicsDevice.IsCacheCoherentUMA)
{
MemoryHelper.Copy(
sourcePointer,
0u,
(uint)size,
(uint)sizeof(T),
resource.Pointer);
using ID3D12ResourceMap resource = D3D12Resource->Map();
fixed (void* sourcePointer = &source)
{
MemoryHelper.Copy(
sourcePointer,
(uint)offset,
(uint)size,
(uint)sizeof(T),
resource.Pointer);
}
}
else
{
nint
byteOffset = (nint)offset * sizeof(T),
byteSize = size * sizeof(T);
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, (ulong)byteSize, false);
copyCommandList.CopyBufferRegion(D3D12Resource, (ulong)byteOffset, d3D12Resource.Get(), 0,(ulong)byteSize);
copyCommandList.ExecuteAndWaitForCompletion();
using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
fixed (void* sourcePointer = &source)
{
MemoryHelper.Copy(
sourcePointer,
0u,
(uint)size,
(uint)sizeof(T),
resource.Pointer);
}
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
copyCommandList.CopyBufferRegion(D3D12Resource, (ulong)byteOffset, d3D12Resource.Get(), 0, (ulong)byteSize);
copyCommandList.ExecuteAndWaitForCompletion();
}
}
/// <inheritdoc/>
+3 -2
ファイルの表示
@@ -78,6 +78,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
DXGIFormatHelper.GetForType<T>(),
(uint)width,
(uint)height,
device.IsCacheCoherentUMA,
out this.d3D12ResourceState);
this.d3D12CommandListType = this.d3D12ResourceState == D3D12_RESOURCE_STATE_COMMON
@@ -148,7 +149,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
out ulong rowSizeInBytes,
out ulong totalSizeInBytes);
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, totalSizeInBytes);
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA);
using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType))
{
@@ -220,7 +221,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
out ulong rowSizeInBytes,
out ulong totalSizeInBytes);
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, totalSizeInBytes);
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA);
using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
fixed (void* sourcePointer = &source)
+3 -2
ファイルの表示
@@ -82,6 +82,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
(uint)width,
(uint)height,
(ushort)depth,
device.IsCacheCoherentUMA,
out this.d3D12ResourceState);
this.d3D12CommandListType = this.d3D12ResourceState == D3D12_RESOURCE_STATE_COMMON
@@ -162,7 +163,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
out ulong rowSizeInBytes,
out ulong totalSizeInBytes);
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, totalSizeInBytes);
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA);
using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType))
{
@@ -241,7 +242,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
out ulong rowSizeInBytes,
out ulong totalSizeInBytes);
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, totalSizeInBytes);
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA);
using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
fixed (void* sourcePointer = &source)
+49 -10
ファイルの表示
@@ -102,11 +102,13 @@ namespace ComputeSharp.Graphics.Extensions
/// <param name="d3D12Device">The <see cref="ID3D12Device"/> instance in use.</param>
/// <param name="resourceType">The resource type currently in use.</param>
/// <param name="sizeInBytes">The size in bytes of the current buffer.</param>
/// <param name="isCacheCoherentUMA">Indicates whether or not the current device has a cache coherent UMA architecture.</param>
/// <returns>An <see cref="ID3D12Resource"/> reference for the current buffer.</returns>
public static ComPtr<ID3D12Resource> CreateCommittedResource(
this ref ID3D12Device d3D12Device,
ResourceType resourceType,
ulong sizeInBytes)
ulong sizeInBytes,
bool isCacheCoherentUMA)
{
(D3D12_HEAP_TYPE d3D12HeapType,
D3D12_RESOURCE_FLAGS d3D12ResourceFlags,
@@ -123,11 +125,22 @@ namespace ComputeSharp.Graphics.Extensions
using ComPtr<ID3D12Resource> d3D12Resource = default;
D3D12_HEAP_PROPERTIES d3D12HeapProperties;
d3D12HeapProperties.Type = d3D12HeapType;
d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
d3D12HeapProperties.CreationNodeMask = 1;
d3D12HeapProperties.VisibleNodeMask = 1;
if (isCacheCoherentUMA)
{
d3D12HeapProperties.Type = D3D12_HEAP_TYPE_CUSTOM;
d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_WRITE_BACK;
d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_L0;
}
else
{
d3D12HeapProperties.Type = d3D12HeapType;
d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
}
D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Buffer(sizeInBytes, d3D12ResourceFlags);
d3D12Device.CreateCommittedResource(
@@ -150,6 +163,7 @@ namespace ComputeSharp.Graphics.Extensions
/// <param name="dxgiFormat">The <see cref="DXGI_FORMAT"/> value to use.</param>
/// <param name="width">The width of the texture resource.</param>
/// <param name="height">The height of the texture resource.</param>
/// <param name="isCacheCoherentUMA">Indicates whether or not the current device has a cache coherent UMA architecture.</param>
/// <param name="d3D12ResourceStates">The default <see cref="D3D12_RESOURCE_STATES"/> value for the resource.</param>
/// <returns>An <see cref="ID3D12Resource"/> reference for the current texture.</returns>
public static ComPtr<ID3D12Resource> CreateCommittedResource(
@@ -158,6 +172,7 @@ namespace ComputeSharp.Graphics.Extensions
DXGI_FORMAT dxgiFormat,
uint width,
uint height,
bool isCacheCoherentUMA,
out D3D12_RESOURCE_STATES d3D12ResourceStates)
{
D3D12_RESOURCE_FLAGS d3D12ResourceFlags;
@@ -172,11 +187,22 @@ namespace ComputeSharp.Graphics.Extensions
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;
if (isCacheCoherentUMA)
{
d3D12HeapProperties.Type = D3D12_HEAP_TYPE_CUSTOM;
d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_WRITE_BACK;
d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_L0;
}
else
{
d3D12HeapProperties.Type = D3D12_HEAP_TYPE_DEFAULT;
d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
}
D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex2D(dxgiFormat, width, height, flags: d3D12ResourceFlags);
d3D12Device.CreateCommittedResource(
@@ -200,6 +226,7 @@ namespace ComputeSharp.Graphics.Extensions
/// <param name="width">The width of the texture resource.</param>
/// <param name="height">The height of the texture resource.</param>
/// <param name="depth">The depth of the texture resource.</param>
/// <param name="isCacheCoherentUMA">Indicates whether or not the current device has a cache coherent UMA architecture.</param>
/// <param name="d3D12ResourceStates">The default <see cref="D3D12_RESOURCE_STATES"/> value for the resource.</param>
/// <returns>An <see cref="ID3D12Resource"/> reference for the current texture.</returns>
public static ComPtr<ID3D12Resource> CreateCommittedResource(
@@ -209,6 +236,7 @@ namespace ComputeSharp.Graphics.Extensions
uint width,
uint height,
ushort depth,
bool isCacheCoherentUMA,
out D3D12_RESOURCE_STATES d3D12ResourceStates)
{
D3D12_RESOURCE_FLAGS d3D12ResourceFlags;
@@ -223,11 +251,22 @@ namespace ComputeSharp.Graphics.Extensions
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;
if (isCacheCoherentUMA)
{
d3D12HeapProperties.Type = D3D12_HEAP_TYPE_CUSTOM;
d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_WRITE_BACK;
d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_L0;
}
else
{
d3D12HeapProperties.Type = D3D12_HEAP_TYPE_DEFAULT;
d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
}
D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex3D(dxgiFormat, width, height, depth, flags: d3D12ResourceFlags);
d3D12Device.CreateCommittedResource(
+26 -3
ファイルの表示
@@ -12,6 +12,7 @@ using TerraFX.Interop;
using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE;
using static TerraFX.Interop.D3D12_FEATURE;
using static TerraFX.Interop.D3D12_FORMAT_SUPPORT1;
using static TerraFX.Interop.DXGI_ADAPTER_FLAG;
namespace ComputeSharp.Graphics
{
@@ -90,12 +91,18 @@ namespace ComputeSharp.Graphics
Luid = Luid.FromLUID(dxgiDescription1->AdapterLuid);
Name = new string((char*)dxgiDescription1->Description);
MemorySize = dxgiDescription1->DedicatedVideoMemory;
DedicatedMemorySize = dxgiDescription1->DedicatedVideoMemory;
SharedMemorySize = dxgiDescription1->SharedSystemMemory;
IsHardwareAccelerated = (dxgiDescription1->Flags & (uint)DXGI_ADAPTER_FLAG_SOFTWARE) == 0;
var d3D12Options1Data = d3D12Device.Get()->CheckFeatureSupport<D3D12_FEATURE_DATA_D3D12_OPTIONS1>(D3D12_FEATURE_D3D12_OPTIONS1);
ComputeUnits = d3D12Options1Data.TotalLaneCount;
WavefrontSize = d3D12Options1Data.WaveLaneCountMin;
var d3D12Architecture1Data = d3D12Device.Get()->CheckFeatureSupport<D3D12_FEATURE_DATA_ARCHITECTURE1>(D3D12_FEATURE_ARCHITECTURE1);
IsCacheCoherentUMA = d3D12Architecture1Data.CacheCoherentUMA != 0;
}
/// <summary>
@@ -109,9 +116,20 @@ namespace ComputeSharp.Graphics
public string Name { get; }
/// <summary>
/// Gets the size of the dedicated video memory for the current <see cref="GraphicsDevice"/> instance.
/// Gets the size of the dedicated memory for the current device.
/// </summary>
public nuint MemorySize { get; }
public nuint DedicatedMemorySize { get; }
/// <summary>
/// Gets the size of the shared system memory for the current device.
/// </summary>
public nuint SharedMemorySize { get; }
/// <summary>
/// Gets whether or not the current device is hardware accelerated.
/// This value is <see langword="false"/> for software fallback devices.
/// </summary>
public bool IsHardwareAccelerated { get; }
/// <summary>
/// Gets the number of total lanes on the current device (eg. CUDA cores on an nVidia GPU).
@@ -128,6 +146,11 @@ namespace ComputeSharp.Graphics
/// </summary>
internal ID3D12Device* D3D12Device => this.d3D12Device;
/// <summary>
/// Gets whether or not the current device has a cache coherent UMA architecture.
/// </summary>
internal bool IsCacheCoherentUMA { get; }
/// <summary>
/// Checks whether the current device supports the creation of <see cref="Buffers.Abstract.Texture2D{T}"/>
/// resources for a specified type <typeparamref name="T"/>.
+17 -3
ファイルの表示
@@ -1,4 +1,5 @@
using TerraFX.Interop;
using static TerraFX.Interop.DXGI_ADAPTER_FLAG;
namespace ComputeSharp.Graphics
{
@@ -15,7 +16,9 @@ namespace ComputeSharp.Graphics
{
Luid = Luid.FromLUID(dxgiDescription1->AdapterLuid);
Name = new string((char*)dxgiDescription1->Description);
MemorySize = dxgiDescription1->DedicatedVideoMemory;
DedicatedMemorySize = dxgiDescription1->DedicatedVideoMemory;
SharedMemorySize = dxgiDescription1->SharedSystemMemory;
IsHardwareAccelerated = (dxgiDescription1->Flags & (uint)DXGI_ADAPTER_FLAG_SOFTWARE) == 0;
}
/// <summary>
@@ -29,8 +32,19 @@ namespace ComputeSharp.Graphics
public string Name { get; }
/// <summary>
/// Gets the size of the dedicated video memory for the associated device.
/// Gets the size of the dedicated memory for the associated device.
/// </summary>
public nuint MemorySize { get; }
public nuint DedicatedMemorySize { get; }
/// <summary>
/// Gets the size of the shared system memory for the associated device.
/// </summary>
public nuint SharedMemorySize { get; }
/// <summary>
/// Gets whether or not the associated device is hardware accelerated.
/// This value is <see langword="false"/> for software fallback devices.
/// </summary>
public bool IsHardwareAccelerated { get; }
}
}
+64 -26
ファイルの表示
@@ -68,6 +68,11 @@ namespace ComputeSharp.Graphics.Helpers
/// </summary>
private uint index;
/// <summary>
/// Indicates whether or not the enumerator has completed the enumeration of all possible devices.
/// </summary>
private bool isCompleted;
/// <summary>
/// The current <see cref="GraphicsDevice"/> instance to return.
/// </summary>
@@ -103,47 +108,80 @@ namespace ComputeSharp.Graphics.Helpers
}
}
if (this.isCompleted) return false;
while (true)
{
using ComPtr<IDXGIAdapter1> dxgiAdapter1 = default;
HRESULT enumAdapters1Result = dxgiFactory4.Get()->EnumAdapters1(this.index, dxgiAdapter1.GetAddressOf());
HRESULT enumAdapters1Result = this.dxgiFactory4.Get()->EnumAdapters1(this.index, dxgiAdapter1.GetAddressOf());
if (enumAdapters1Result == FX.DXGI_ERROR_NOT_FOUND)
{
return false;
}
this.dxgiFactory4.Get()->EnumWarpAdapter(FX.__uuidof<IDXGIAdapter1>(), dxgiAdapter1.GetVoidAddressOf()).Assert();
enumAdapters1Result.Assert();
DXGI_ADAPTER_DESC1 dxgiDescription1;
this.index++;
dxgiAdapter1.Get()->GetDesc1(&dxgiDescription1).Assert();
DXGI_ADAPTER_DESC1 dxgiDescription1;
dxgiAdapter1.Get()->GetDesc1(&dxgiDescription1).Assert();
if (dxgiDescription1.DedicatedVideoMemory == 0) continue;
HRESULT createDeviceResult = FX.D3D12CreateDevice(
dxgiAdapter1.AsIUnknown().Get(),
D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0,
FX.__uuidof<ID3D12Device>(),
null);
if (FX.SUCCEEDED(createDeviceResult) &&
this.predicate(new GraphicsDeviceInfo(&dxgiDescription1)))
{
using ComPtr<ID3D12Device> d3D12Device = default;
FX.D3D12CreateDevice(
HRESULT createDeviceResult = FX.D3D12CreateDevice(
dxgiAdapter1.AsIUnknown().Get(),
D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0,
FX.__uuidof<ID3D12Device>(),
d3D12Device.GetVoidAddressOf()).Assert();
null);
this.graphicsDevice = GetOrCreateDevice(d3D12Device.Move(), &dxgiDescription1);
if (FX.SUCCEEDED(createDeviceResult) &&
this.predicate(new GraphicsDeviceInfo(&dxgiDescription1)))
{
using ComPtr<ID3D12Device> d3D12Device = default;
return true;
FX.D3D12CreateDevice(
dxgiAdapter1.AsIUnknown().Get(),
D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0,
FX.__uuidof<ID3D12Device>(),
d3D12Device.GetVoidAddressOf()).Assert();
this.graphicsDevice = GetOrCreateDevice(d3D12Device.Move(), &dxgiDescription1);
this.isCompleted = true;
return true;
}
return false;
}
else
{
enumAdapters1Result.Assert();
this.index++;
DXGI_ADAPTER_DESC1 dxgiDescription1;
dxgiAdapter1.Get()->GetDesc1(&dxgiDescription1).Assert();
if (dxgiDescription1.DedicatedVideoMemory == 0) continue;
HRESULT createDeviceResult = FX.D3D12CreateDevice(
dxgiAdapter1.AsIUnknown().Get(),
D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0,
FX.__uuidof<ID3D12Device>(),
null);
if (FX.SUCCEEDED(createDeviceResult) &&
this.predicate(new GraphicsDeviceInfo(&dxgiDescription1)))
{
using ComPtr<ID3D12Device> d3D12Device = default;
FX.D3D12CreateDevice(
dxgiAdapter1.AsIUnknown().Get(),
D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0,
FX.__uuidof<ID3D12Device>(),
d3D12Device.GetVoidAddressOf()).Assert();
this.graphicsDevice = GetOrCreateDevice(d3D12Device.Move(), &dxgiDescription1);
return true;
}
}
}
}
+33 -3
ファイルの表示
@@ -23,7 +23,7 @@ namespace ComputeSharp.Graphics.Helpers
{
DXGI_ADAPTER_DESC1 dxgiDescription1;
return TryGetDefaultDevice(null, &dxgiDescription1);
return TryGetDefaultDevice(null, &dxgiDescription1) || TryGetWarpDevice(null, &dxgiDescription1);
}
/// <summary>
@@ -36,7 +36,7 @@ namespace ComputeSharp.Graphics.Helpers
{
DXGI_ADAPTER_DESC1 dxgiDescription1;
_ = TryGetDefaultDevice(null, &dxgiDescription1);
_ = TryGetDefaultDevice(null, &dxgiDescription1) || TryGetWarpDevice(null, &dxgiDescription1);
return Luid.FromLUID(dxgiDescription1.AdapterLuid);
}
@@ -52,7 +52,8 @@ namespace ComputeSharp.Graphics.Helpers
DXGI_ADAPTER_DESC1 dxgiDescription1;
if (TryGetDefaultDevice(d3D12Device.GetAddressOf(), &dxgiDescription1))
if (TryGetDefaultDevice(d3D12Device.GetAddressOf(), &dxgiDescription1) ||
TryGetWarpDevice(d3D12Device.GetAddressOf(), &dxgiDescription1))
{
return GetOrCreateDevice(d3D12Device.Move(), &dxgiDescription1);
}
@@ -106,6 +107,35 @@ namespace ComputeSharp.Graphics.Helpers
}
}
/// <summary>
/// Tries to check or create a warp <see cref="ID3D12Device"/> object.
/// </summary>
/// <param name="d3D12Device">A pointer to the <see cref="ID3D12Device"/> object to create, or <see langword="null"/>.</param>
/// <param name="dxgiDescription1">A pointer to the <see cref="DXGI_ADAPTER_DESC1"/> value for the device found.</param>
/// <returns>Whether a warp device was created successfully.</returns>
private static unsafe bool TryGetWarpDevice(ID3D12Device** d3D12Device, DXGI_ADAPTER_DESC1* dxgiDescription1)
{
using ComPtr<IDXGIFactory4> dxgiFactory4 = default;
EnableDebugMode();
FX.CreateDXGIFactory2(IDXGIFactoryCreationFlags, FX.__uuidof<IDXGIFactory4>(), dxgiFactory4.GetVoidAddressOf()).Assert();
using ComPtr<IDXGIAdapter1> dxgiAdapter1 = default;
dxgiFactory4.Get()->EnumWarpAdapter(FX.__uuidof<IDXGIAdapter1>(), dxgiAdapter1.GetVoidAddressOf()).Assert();
dxgiAdapter1.Get()->GetDesc1(dxgiDescription1).Assert();
HRESULT createDeviceResult = FX.D3D12CreateDevice(
dxgiAdapter1.AsIUnknown().Get(),
D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0,
FX.__uuidof<ID3D12Device>(),
(void**)d3D12Device);
return FX.SUCCEEDED(createDeviceResult);
}
/// <summary>
/// The creation flags for <see cref="IDXGIFactory"/> instances.
/// </summary>
+1 -1
ファイルの表示
@@ -1,4 +1,4 @@
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.Core.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComputeSharp.Tests.Internals
+3 -3
ファイルの表示
@@ -218,7 +218,7 @@ namespace ComputeSharp.Tests
}
[AutoConstructor]
private readonly partial struct ConstantBufferKernel : IComputeShader
internal readonly partial struct ConstantBufferKernel : IComputeShader
{
public readonly ConstantBuffer<int> source;
public readonly ReadWriteBuffer<int> destination;
@@ -245,7 +245,7 @@ namespace ComputeSharp.Tests
}
[AutoConstructor]
private readonly partial struct ReadOnlyBufferKernel : IComputeShader
internal readonly partial struct ReadOnlyBufferKernel : IComputeShader
{
public readonly ReadOnlyBuffer<int> source;
public readonly ReadWriteBuffer<int> destination;
@@ -272,7 +272,7 @@ namespace ComputeSharp.Tests
}
[AutoConstructor]
private readonly partial struct ReadWriteBufferKernel : IComputeShader
internal readonly partial struct ReadWriteBufferKernel : IComputeShader
{
public readonly ReadWriteBuffer<int> source;
public readonly ReadWriteBuffer<int> destination;
+12 -3
ファイルの表示
@@ -20,7 +20,16 @@ namespace ComputeSharp.Tests
{
Assert.IsTrue(Gpu.Default.Luid != default);
Assert.IsTrue(Gpu.Default.Name is { Length: > 0 });
Assert.IsTrue(Gpu.Default.MemorySize != 0);
if (Gpu.Default.IsHardwareAccelerated)
{
Assert.IsTrue(Gpu.Default.DedicatedMemorySize != 0);
}
else
{
Assert.IsTrue(Gpu.Default.SharedMemorySize != 0);
}
Assert.IsTrue(Gpu.Default.ComputeUnits != 0);
Assert.IsTrue(Gpu.Default.WavefrontSize != 0);
}
@@ -59,7 +68,7 @@ namespace ComputeSharp.Tests
{
int i = 0;
foreach (GraphicsDevice device in Gpu.QueryDevices(info => info.MemorySize >= 1024))
foreach (GraphicsDevice device in Gpu.QueryDevices(info => info.DedicatedMemorySize >= 1024))
{
if (i++ == 0) Assert.AreSame(Gpu.Default, device);
@@ -74,7 +83,7 @@ namespace ComputeSharp.Tests
}
[AutoConstructor]
private readonly partial struct SampleShader : IComputeShader
internal readonly partial struct SampleShader : IComputeShader
{
public readonly ReadWriteBuffer<int> buffer;
+2 -2
ファイルの表示
@@ -154,7 +154,7 @@ namespace ComputeSharp.Tests
}
[AutoConstructor]
private readonly partial struct ReadOnlyTexture2DKernel : IComputeShader
internal readonly partial struct ReadOnlyTexture2DKernel : IComputeShader
{
public readonly ReadOnlyTexture2D<int> source;
public readonly ReadWriteBuffer<int> destination;
@@ -183,7 +183,7 @@ namespace ComputeSharp.Tests
}
[AutoConstructor]
private readonly partial struct ReadWriteTexture2DKernel : IComputeShader
internal readonly partial struct ReadWriteTexture2DKernel : IComputeShader
{
public readonly ReadWriteTexture2D<int> source;
public readonly ReadWriteTexture2D<int> destination;
+2 -2
ファイルの表示
@@ -188,7 +188,7 @@ namespace ComputeSharp.Tests
}
[AutoConstructor]
private readonly partial struct ReadOnlyTexture3DKernel : IComputeShader
internal readonly partial struct ReadOnlyTexture3DKernel : IComputeShader
{
public readonly ReadOnlyTexture3D<int> source;
public readonly ReadWriteBuffer<int> destination;
@@ -217,7 +217,7 @@ namespace ComputeSharp.Tests
}
[AutoConstructor]
private readonly partial struct ReadWriteTexture3DKernel : IComputeShader
internal readonly partial struct ReadWriteTexture3DKernel : IComputeShader
{
public readonly ReadWriteTexture3D<int> source;
public readonly ReadWriteTexture3D<int> destination;