From a441ce5e6fa5fd50c8a790b10d861218c0d77947 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 28 Aug 2022 01:55:59 +0200 Subject: [PATCH 1/2] Add double feature support in TransferBuffer ctor --- .../Extensions/GraphicsDeviceExtensions.Resources.cs | 2 ++ .../Graphics/Resources/Abstract/TransferBuffer{T}.cs | 10 ++++++++++ .../Graphics/Resources/ReadBackBuffer{T}.cs | 2 ++ src/ComputeSharp/Graphics/Resources/UploadBuffer{T}.cs | 2 ++ 4 files changed, 16 insertions(+) diff --git a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs index 5d3f3172..b8e91f45 100644 --- a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs +++ b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs @@ -1075,6 +1075,7 @@ partial class GraphicsDeviceExtensions /// The length of the buffer to allocate. /// The allocation mode to use for the new resource. /// A instance of size . + [RequiresUnreferencedCode("This method reads type info of all fields of the resource element type (recursively).")] public static UploadBuffer AllocateUploadBuffer(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { @@ -1126,6 +1127,7 @@ partial class GraphicsDeviceExtensions /// The length of the buffer to allocate. /// The allocation mode to use for the new resource. /// A instance of size . + [RequiresUnreferencedCode("This method reads type info of all fields of the resource element type (recursively).")] public static ReadBackBuffer AllocateReadBackBuffer(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs index b227b2f8..33f3efcf 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs @@ -1,12 +1,15 @@ using System; using System.Buffers; +using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using CommunityToolkit.Diagnostics; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Extensions; +using ComputeSharp.Graphics.Helpers; using ComputeSharp.Interop; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; +using static TerraFX.Interop.DirectX.D3D12_FEATURE; using ResourceType = ComputeSharp.Graphics.Resources.Enums.ResourceType; namespace ComputeSharp.Resources; @@ -42,6 +45,7 @@ public abstract unsafe class TransferBuffer : NativeObject, IGraphicsResource /// The number of items to store in the current buffer. /// The resource type for the current buffer. /// The allocation mode to use for the new resource. + [RequiresUnreferencedCode("This method reads type info of all fields of the resource element type (recursively).")] private protected TransferBuffer(GraphicsDevice device, int length, ResourceType resourceType, AllocationMode allocationMode) { // The maximum length is set such that the aligned buffer size can't exceed uint.MaxValue @@ -51,6 +55,12 @@ public abstract unsafe class TransferBuffer : NativeObject, IGraphicsResource device.ThrowIfDeviceLost(); + if (TypeInfo.IsDoubleOrContainsDoubles && + device.D3D12Device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS).DoublePrecisionFloatShaderOps == 0) + { + UnsupportedDoubleOperationException.Throw(); + } + GraphicsDevice = device; Length = length; diff --git a/src/ComputeSharp/Graphics/Resources/ReadBackBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/ReadBackBuffer{T}.cs index 9ae4cae3..78b8ec2d 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadBackBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadBackBuffer{T}.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using ComputeSharp.Graphics.Resources.Enums; using ComputeSharp.Resources; using ComputeSharp.Resources.Debug; @@ -20,6 +21,7 @@ public sealed class ReadBackBuffer : TransferBuffer /// The associated with the current instance. /// The number of items to store in the current buffer. /// The allocation mode to use for the new resource. + [RequiresUnreferencedCode("This method reads type info of all fields of the resource element type (recursively).")] internal ReadBackBuffer(GraphicsDevice device, int length, AllocationMode allocationMode) : base(device, length, ResourceType.ReadBack, allocationMode) { diff --git a/src/ComputeSharp/Graphics/Resources/UploadBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/UploadBuffer{T}.cs index 61265ee1..1400f410 100644 --- a/src/ComputeSharp/Graphics/Resources/UploadBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/UploadBuffer{T}.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using ComputeSharp.Graphics.Resources.Enums; using ComputeSharp.Resources; using ComputeSharp.Resources.Debug; @@ -20,6 +21,7 @@ public sealed class UploadBuffer : TransferBuffer /// The associated with the current instance. /// The number of items to store in the current buffer. /// The allocation mode to use for the new resource. + [RequiresUnreferencedCode("This method reads type info of all fields of the resource element type (recursively).")] internal UploadBuffer(GraphicsDevice device, int length, AllocationMode allocationMode) : base(device, length, ResourceType.Upload, allocationMode) { From a850b1f2064094cc879611436fec18b97435f70c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 28 Aug 2022 01:56:14 +0200 Subject: [PATCH 2/2] Add unit tests for TransferBuffer with no double support --- tests/ComputeSharp.Tests/BufferTests.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/ComputeSharp.Tests/BufferTests.cs b/tests/ComputeSharp.Tests/BufferTests.cs index 4e468e16..5d84cf8e 100644 --- a/tests/ComputeSharp.Tests/BufferTests.cs +++ b/tests/ComputeSharp.Tests/BufferTests.cs @@ -388,7 +388,7 @@ public partial class BufferTests [Resource(typeof(ReadOnlyBuffer<>))] [Resource(typeof(ReadWriteBuffer<>))] [ExpectedException(typeof(UnsupportedDoubleOperationException))] - public void Dispatch_ReadWriteBuffer_DoublePrecision_ThrowsExceptionIfUnsupported(Device device, Type resourceType) + public void Dispatch_Buffer_DoublePrecision_ThrowsExceptionIfUnsupported(Device device, Type resourceType) { if (device.Get().IsDoublePrecisionSupportAvailable()) { @@ -399,4 +399,21 @@ public partial class BufferTests Assert.Fail(); } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(UploadBuffer<>))] + [Resource(typeof(ReadBackBuffer<>))] + [ExpectedException(typeof(UnsupportedDoubleOperationException))] + public void Dispatch_TransferBuffer_DoublePrecision_ThrowsExceptionIfUnsupported(Device device, Type resourceType) + { + if (device.Get().IsDoublePrecisionSupportAvailable()) + { + Assert.Inconclusive(); + } + + using TransferBuffer buffer = device.Get().AllocateTransferBuffer(resourceType, 32); + + Assert.Fail(); + } }