From fc9c61ca6a4c4ed07c6a21606c73bef966d108a3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 6 Sep 2019 21:43:04 +0200 Subject: [PATCH] Added custom exception when using buffers from other devices --- .../Commands/CommandList.cs | 3 ++ .../GraphicsDeviceMismatchException.cs | 33 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs diff --git a/src/ComputeSharp.Graphics/Commands/CommandList.cs b/src/ComputeSharp.Graphics/Commands/CommandList.cs index 52128f17..413af34e 100644 --- a/src/ComputeSharp.Graphics/Commands/CommandList.cs +++ b/src/ComputeSharp.Graphics/Commands/CommandList.cs @@ -1,4 +1,5 @@ using System; +using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Graphics.Commands.Abstract; using Vortice.Direct3D12; @@ -61,6 +62,8 @@ namespace ComputeSharp.Graphics.Commands public void SetComputeRootDescriptorTable(int rootParameterIndex, GraphicsResource resource) { if (resource.NativeGpuDescriptorHandle == null) throw new InvalidOperationException("Invalid graphics resource GPU descriptor"); + if (resource.GraphicsDevice != GraphicsDevice) throw new GraphicsDeviceMismatchException(GraphicsDevice, resource); + NativeCommandList.SetComputeRootDescriptorTable(rootParameterIndex, resource.NativeGpuDescriptorHandle.Value); } diff --git a/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs b/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs new file mode 100644 index 00000000..b11d8512 --- /dev/null +++ b/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs @@ -0,0 +1,33 @@ +using System; +using System.Diagnostics.Contracts; +using System.Text; +using ComputeSharp.Graphics; +using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Graphics.Extensions; + +namespace ComputeSharp.Exceptions +{ + /// + /// A custom that indicates when mismatched devices are being used + /// + public sealed class GraphicsDeviceMismatchException : InvalidOperationException + { + /// + /// Creates a new instance + /// + internal GraphicsDeviceMismatchException(GraphicsDevice device, GraphicsResource resource) : base(GetExceptionMessage(device, resource)) { } + + // Gets a proper exception message for a pair of device and resource + [Pure] + private static string GetExceptionMessage(GraphicsDevice device, GraphicsResource resource) + { + StringBuilder builder = new StringBuilder(); + builder.AppendLine("Invalid pairing of graphics devices used to run a compute shader and allocate memory buffers."); + builder.AppendLine($"The target device to run the compute shader is \"{device.Name}\" (id: {device.NativeDevice.AdapterLuid})."); + builder.AppendLine($"The buffer of type {resource.GetType().ToFriendlyString()} was allocated on device \"{resource.GraphicsDevice.Name}\" (id: {resource.GraphicsDevice.NativeDevice.AdapterLuid})."); + builder.AppendLine("Make sure to always allocate buffers on the same device used to actually run the code that accesses them."); + + return builder.ToString(); + } + } +}