diff --git a/samples/ComputeSharp.Benchmark/Dnn.cs b/samples/ComputeSharp.Benchmark/Dnn.cs index c6a3cc53..3f8087ba 100644 --- a/samples/ComputeSharp.Benchmark/Dnn.cs +++ b/samples/ComputeSharp.Benchmark/Dnn.cs @@ -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 /// /// Executes the forward pass on a fully connected layer on the GPU. /// + /// The device to run the computation on. /// The number of samples in the input tensor. /// The number of rows in the input matrix. /// The number of columns in the input matrix. @@ -63,10 +65,17 @@ namespace ComputeSharp.Benchmark /// The result tensor. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void FullyConnectedForwardGpu( - int c, int n, int m, int p, - ReadOnlyBuffer x, ReadOnlyBuffer w, ReadOnlyBuffer b, ReadWriteBuffer y) + GraphicsDevice device, + int c, + int n, + int m, + int p, + ReadOnlyBuffer x, + ReadOnlyBuffer w, + ReadOnlyBuffer b, + ReadWriteBuffer 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)); } /// diff --git a/samples/ComputeSharp.Benchmark/DnnBenchmark.cs b/samples/ComputeSharp.Benchmark/DnnBenchmark.cs index 1b163220..186d119f 100644 --- a/samples/ComputeSharp.Benchmark/DnnBenchmark.cs +++ b/samples/ComputeSharp.Benchmark/DnnBenchmark.cs @@ -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; /// - /// The input tensor. + /// The input tensor (GPU). /// private ReadOnlyBuffer BufferX; /// - /// The weights tensor. + /// The weights tensor (GPU). /// private ReadOnlyBuffer BufferW; /// - /// The bias tensor. + /// The bias tensor (GPU). /// private ReadOnlyBuffer BufferB; /// - /// The result tensor. + /// The result tensor (GPU). /// private ReadWriteBuffer 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.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(); } - /// - /// Creates a new array of the specified size. - /// - /// The size of the new random array to create. - [Pure] - private float[] CreateRandomArray(int size) - { - float[] array = ArrayPool.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; - } - /// /// Runs a fully connected forward operation on the CPU. /// @@ -139,7 +134,7 @@ namespace ComputeSharp.Benchmark /// Runs a fully connected forward operation on the GPU. /// [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); /// /// 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 b = Gpu.Default.AllocateReadOnlyBuffer(B); using ReadWriteBuffer 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); } diff --git a/src/ComputeSharp.Core/Properties/AssemblyInfo.cs b/src/ComputeSharp.Core/Properties/AssemblyInfo.cs index 4f59d117..7c536786 100644 --- a/src/ComputeSharp.Core/Properties/AssemblyInfo.cs +++ b/src/ComputeSharp.Core/Properties/AssemblyInfo.cs @@ -3,3 +3,4 @@ [assembly: InternalsVisibleTo("ComputeSharp.Graphics")] [assembly: InternalsVisibleTo("ComputeSharp.Shaders")] [assembly: InternalsVisibleTo("ComputeSharp.SourceGenerators")] +[assembly: InternalsVisibleTo("ComputeSharp.Tests.Internals")] diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs index bf9be010..c502dbf9 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs @@ -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); diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs index 37686b47..3e73a1c7 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs @@ -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 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 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 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 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(); + } } /// diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs index 963c3b5f..d8849e0f 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs @@ -78,6 +78,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract DXGIFormatHelper.GetForType(), (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 d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, totalSizeInBytes); + using ComPtr 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 d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, totalSizeInBytes); + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) fixed (void* sourcePointer = &source) diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs index 6539319a..abe74bc4 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs @@ -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 d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, totalSizeInBytes); + using ComPtr 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 d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, totalSizeInBytes); + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) fixed (void* sourcePointer = &source) diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs index 8a6f278c..239e2369 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs @@ -102,11 +102,13 @@ namespace ComputeSharp.Graphics.Extensions /// The instance in use. /// The resource type currently in use. /// The size in bytes of the current buffer. + /// Indicates whether or not the current device has a cache coherent UMA architecture. /// An reference for the current buffer. public static ComPtr 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 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 /// The value to use. /// The width of the texture resource. /// The height of the texture resource. + /// Indicates whether or not the current device has a cache coherent UMA architecture. /// The default value for the resource. /// An reference for the current texture. public static ComPtr 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 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 /// The width of the texture resource. /// The height of the texture resource. /// The depth of the texture resource. + /// Indicates whether or not the current device has a cache coherent UMA architecture. /// The default value for the resource. /// An reference for the current texture. public static ComPtr 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 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( diff --git a/src/ComputeSharp.Graphics/GraphicsDevice.cs b/src/ComputeSharp.Graphics/GraphicsDevice.cs index 92503ee7..ff2dfd47 100644 --- a/src/ComputeSharp.Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp.Graphics/GraphicsDevice.cs @@ -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_D3D12_OPTIONS1); ComputeUnits = d3D12Options1Data.TotalLaneCount; WavefrontSize = d3D12Options1Data.WaveLaneCountMin; + + var d3D12Architecture1Data = d3D12Device.Get()->CheckFeatureSupport(D3D12_FEATURE_ARCHITECTURE1); + + IsCacheCoherentUMA = d3D12Architecture1Data.CacheCoherentUMA != 0; } /// @@ -109,9 +116,20 @@ namespace ComputeSharp.Graphics public string Name { get; } /// - /// Gets the size of the dedicated video memory for the current instance. + /// Gets the size of the dedicated memory for the current device. /// - public nuint MemorySize { get; } + public nuint DedicatedMemorySize { get; } + + /// + /// Gets the size of the shared system memory for the current device. + /// + public nuint SharedMemorySize { get; } + + /// + /// Gets whether or not the current device is hardware accelerated. + /// This value is for software fallback devices. + /// + public bool IsHardwareAccelerated { get; } /// /// Gets the number of total lanes on the current device (eg. CUDA cores on an nVidia GPU). @@ -128,6 +146,11 @@ namespace ComputeSharp.Graphics /// internal ID3D12Device* D3D12Device => this.d3D12Device; + /// + /// Gets whether or not the current device has a cache coherent UMA architecture. + /// + internal bool IsCacheCoherentUMA { get; } + /// /// Checks whether the current device supports the creation of /// resources for a specified type . diff --git a/src/ComputeSharp.Graphics/GraphicsDeviceInfo.cs b/src/ComputeSharp.Graphics/GraphicsDeviceInfo.cs index 4ea257dc..ad07c130 100644 --- a/src/ComputeSharp.Graphics/GraphicsDeviceInfo.cs +++ b/src/ComputeSharp.Graphics/GraphicsDeviceInfo.cs @@ -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; } /// @@ -29,8 +32,19 @@ namespace ComputeSharp.Graphics public string Name { get; } /// - /// Gets the size of the dedicated video memory for the associated device. + /// Gets the size of the dedicated memory for the associated device. /// - public nuint MemorySize { get; } + public nuint DedicatedMemorySize { get; } + + /// + /// Gets the size of the shared system memory for the associated device. + /// + public nuint SharedMemorySize { get; } + + /// + /// Gets whether or not the associated device is hardware accelerated. + /// This value is for software fallback devices. + /// + public bool IsHardwareAccelerated { get; } } } diff --git a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Enumerator.cs b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Enumerator.cs index daefc4ec..81a3e914 100644 --- a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Enumerator.cs +++ b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Enumerator.cs @@ -68,6 +68,11 @@ namespace ComputeSharp.Graphics.Helpers /// private uint index; + /// + /// Indicates whether or not the enumerator has completed the enumeration of all possible devices. + /// + private bool isCompleted; + /// /// The current instance to return. /// @@ -103,47 +108,80 @@ namespace ComputeSharp.Graphics.Helpers } } + if (this.isCompleted) return false; + while (true) { using ComPtr 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(), 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(), - null); - - if (FX.SUCCEEDED(createDeviceResult) && - this.predicate(new GraphicsDeviceInfo(&dxgiDescription1))) - { - using ComPtr d3D12Device = default; - - FX.D3D12CreateDevice( + HRESULT createDeviceResult = FX.D3D12CreateDevice( dxgiAdapter1.AsIUnknown().Get(), D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0, FX.__uuidof(), - d3D12Device.GetVoidAddressOf()).Assert(); + null); - this.graphicsDevice = GetOrCreateDevice(d3D12Device.Move(), &dxgiDescription1); + if (FX.SUCCEEDED(createDeviceResult) && + this.predicate(new GraphicsDeviceInfo(&dxgiDescription1))) + { + using ComPtr d3D12Device = default; - return true; + FX.D3D12CreateDevice( + dxgiAdapter1.AsIUnknown().Get(), + D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0, + FX.__uuidof(), + 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(), + null); + + if (FX.SUCCEEDED(createDeviceResult) && + this.predicate(new GraphicsDeviceInfo(&dxgiDescription1))) + { + using ComPtr d3D12Device = default; + + FX.D3D12CreateDevice( + dxgiAdapter1.AsIUnknown().Get(), + D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0, + FX.__uuidof(), + d3D12Device.GetVoidAddressOf()).Assert(); + + this.graphicsDevice = GetOrCreateDevice(d3D12Device.Move(), &dxgiDescription1); + + return true; + } } } } diff --git a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs index 4f4e08fe..036f771a 100644 --- a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs @@ -23,7 +23,7 @@ namespace ComputeSharp.Graphics.Helpers { DXGI_ADAPTER_DESC1 dxgiDescription1; - return TryGetDefaultDevice(null, &dxgiDescription1); + return TryGetDefaultDevice(null, &dxgiDescription1) || TryGetWarpDevice(null, &dxgiDescription1); } /// @@ -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 } } + /// + /// Tries to check or create a warp object. + /// + /// A pointer to the object to create, or . + /// A pointer to the value for the device found. + /// Whether a warp device was created successfully. + private static unsafe bool TryGetWarpDevice(ID3D12Device** d3D12Device, DXGI_ADAPTER_DESC1* dxgiDescription1) + { + using ComPtr dxgiFactory4 = default; + + EnableDebugMode(); + + FX.CreateDXGIFactory2(IDXGIFactoryCreationFlags, FX.__uuidof(), dxgiFactory4.GetVoidAddressOf()).Assert(); + + using ComPtr dxgiAdapter1 = default; + + dxgiFactory4.Get()->EnumWarpAdapter(FX.__uuidof(), 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(), + (void**)d3D12Device); + + return FX.SUCCEEDED(createDeviceResult); + } + /// /// The creation flags for instances. /// diff --git a/tests/ComputeSharp.Tests.Internals/AlignmentHelperTests.cs b/tests/ComputeSharp.Tests.Internals/AlignmentHelperTests.cs index 90868229..9886d70c 100644 --- a/tests/ComputeSharp.Tests.Internals/AlignmentHelperTests.cs +++ b/tests/ComputeSharp.Tests.Internals/AlignmentHelperTests.cs @@ -1,4 +1,4 @@ -using ComputeSharp.Graphics.Helpers; +using ComputeSharp.Core.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.Tests.Internals diff --git a/tests/ComputeSharp.Tests/BufferTests.cs b/tests/ComputeSharp.Tests/BufferTests.cs index 6731b5fa..30f97b6e 100644 --- a/tests/ComputeSharp.Tests/BufferTests.cs +++ b/tests/ComputeSharp.Tests/BufferTests.cs @@ -218,7 +218,7 @@ namespace ComputeSharp.Tests } [AutoConstructor] - private readonly partial struct ConstantBufferKernel : IComputeShader + internal readonly partial struct ConstantBufferKernel : IComputeShader { public readonly ConstantBuffer source; public readonly ReadWriteBuffer destination; @@ -245,7 +245,7 @@ namespace ComputeSharp.Tests } [AutoConstructor] - private readonly partial struct ReadOnlyBufferKernel : IComputeShader + internal readonly partial struct ReadOnlyBufferKernel : IComputeShader { public readonly ReadOnlyBuffer source; public readonly ReadWriteBuffer destination; @@ -272,7 +272,7 @@ namespace ComputeSharp.Tests } [AutoConstructor] - private readonly partial struct ReadWriteBufferKernel : IComputeShader + internal readonly partial struct ReadWriteBufferKernel : IComputeShader { public readonly ReadWriteBuffer source; public readonly ReadWriteBuffer destination; diff --git a/tests/ComputeSharp.Tests/InitializationTests.cs b/tests/ComputeSharp.Tests/InitializationTests.cs index 1b3a19e2..586b5b4e 100644 --- a/tests/ComputeSharp.Tests/InitializationTests.cs +++ b/tests/ComputeSharp.Tests/InitializationTests.cs @@ -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 buffer; diff --git a/tests/ComputeSharp.Tests/Texture2DTests.cs b/tests/ComputeSharp.Tests/Texture2DTests.cs index cf23220f..d9ce8fa5 100644 --- a/tests/ComputeSharp.Tests/Texture2DTests.cs +++ b/tests/ComputeSharp.Tests/Texture2DTests.cs @@ -154,7 +154,7 @@ namespace ComputeSharp.Tests } [AutoConstructor] - private readonly partial struct ReadOnlyTexture2DKernel : IComputeShader + internal readonly partial struct ReadOnlyTexture2DKernel : IComputeShader { public readonly ReadOnlyTexture2D source; public readonly ReadWriteBuffer destination; @@ -183,7 +183,7 @@ namespace ComputeSharp.Tests } [AutoConstructor] - private readonly partial struct ReadWriteTexture2DKernel : IComputeShader + internal readonly partial struct ReadWriteTexture2DKernel : IComputeShader { public readonly ReadWriteTexture2D source; public readonly ReadWriteTexture2D destination; diff --git a/tests/ComputeSharp.Tests/Texture3DTests.cs b/tests/ComputeSharp.Tests/Texture3DTests.cs index b68e02f6..5b771f42 100644 --- a/tests/ComputeSharp.Tests/Texture3DTests.cs +++ b/tests/ComputeSharp.Tests/Texture3DTests.cs @@ -188,7 +188,7 @@ namespace ComputeSharp.Tests } [AutoConstructor] - private readonly partial struct ReadOnlyTexture3DKernel : IComputeShader + internal readonly partial struct ReadOnlyTexture3DKernel : IComputeShader { public readonly ReadOnlyTexture3D source; public readonly ReadWriteBuffer destination; @@ -217,7 +217,7 @@ namespace ComputeSharp.Tests } [AutoConstructor] - private readonly partial struct ReadWriteTexture3DKernel : IComputeShader + internal readonly partial struct ReadWriteTexture3DKernel : IComputeShader { public readonly ReadWriteTexture3D source; public readonly ReadWriteTexture3D destination;