From afd4649a5554adb5709919398975fa53b5e2b930 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 4 Oct 2023 00:14:16 +0200 Subject: [PATCH] Simplify code in all exception types --- .../InvalidExecutionContextException.cs | 4 ++-- .../Exceptions/FxcCompilationException.cs | 19 ++++++++------- .../Exceptions/DxcCompilationException.cs | 19 ++++++++------- .../GraphicsDeviceMismatchException.cs | 15 +++++------- .../UnsupportedDoubleOperationsException.cs | 13 ++++------- .../UnsupportedTextureTypeException.cs | 23 ++++++++----------- 6 files changed, 41 insertions(+), 52 deletions(-) diff --git a/src/ComputeSharp.Core/Exceptions/InvalidExecutionContextException.cs b/src/ComputeSharp.Core/Exceptions/InvalidExecutionContextException.cs index 269e1c65..063169b9 100644 --- a/src/ComputeSharp.Core/Exceptions/InvalidExecutionContextException.cs +++ b/src/ComputeSharp.Core/Exceptions/InvalidExecutionContextException.cs @@ -10,9 +10,9 @@ public sealed class InvalidExecutionContextException : InvalidOperationException /// /// Creates a new instance. /// - /// The member name of the caller API. + /// The member name of the invoked API. internal InvalidExecutionContextException(string memberName) - : base($"The API {memberName} can only be used from within an HLSL shader.") + : base("The invoked API can only be used from within an HLSL shader.") { MemberName = memberName; } diff --git a/src/ComputeSharp.D2D1/Shaders/Exceptions/FxcCompilationException.cs b/src/ComputeSharp.D2D1/Shaders/Exceptions/FxcCompilationException.cs index c7b2b7f3..3c5f8bbd 100644 --- a/src/ComputeSharp.D2D1/Shaders/Exceptions/FxcCompilationException.cs +++ b/src/ComputeSharp.D2D1/Shaders/Exceptions/FxcCompilationException.cs @@ -1,5 +1,4 @@ using System; -using System.Text; namespace ComputeSharp.D2D1.Exceptions; @@ -24,15 +23,15 @@ public sealed class FxcCompilationException : Exception /// A formatted error message for a new instance. private static string GetExceptionMessage(string error) { - StringBuilder builder = new(512); +#if NET6_0_OR_GREATER + ReadOnlySpan message = error.AsSpan().Trim(); +#else + string message = error.Trim(); +#endif - _ = builder.AppendLine("The FXC compiler encountered one or more errors while trying to compile the shader:"); - _ = builder.AppendLine(); - _ = builder.AppendLine(error.Trim()); - _ = builder.AppendLine(); - _ = builder.AppendLine("Make sure to only be using supported features by checking the README file in the ComputeSharp repository: https://github.com/Sergio0694/ComputeSharp."); - _ = builder.Append("If you're sure that your C# shader code is valid, please open an issue an include a working repro and this error message."); - - return builder.ToString(); + return + $"""The FXC compiler encountered one or more errors while trying to compile the shader: "{message}". """ + + $"""Make sure to only be using supported features by checking the README file in the ComputeSharp repository: https://github.com/Sergio0694/ComputeSharp. """ + + $"""If you're sure that your C# shader code is valid, please open an issue an include a working repro and this error message."""; } } \ No newline at end of file diff --git a/src/ComputeSharp.Dynamic/Shaders/Exceptions/DxcCompilationException.cs b/src/ComputeSharp.Dynamic/Shaders/Exceptions/DxcCompilationException.cs index 634b84dd..c43f14ec 100644 --- a/src/ComputeSharp.Dynamic/Shaders/Exceptions/DxcCompilationException.cs +++ b/src/ComputeSharp.Dynamic/Shaders/Exceptions/DxcCompilationException.cs @@ -1,5 +1,4 @@ using System; -using System.Text; namespace ComputeSharp.Exceptions; @@ -24,15 +23,15 @@ public sealed class DxcCompilationException : Exception /// A formatted error message for a new instance. private static string GetExceptionMessage(string error) { - StringBuilder builder = new(512); +#if NET6_0_OR_GREATER + ReadOnlySpan message = error.AsSpan().Trim(); +#else + string message = error.Trim(); +#endif - _ = builder.AppendLine("The DXC compiler encountered one or more errors while trying to compile the shader:"); - _ = builder.AppendLine(); - _ = builder.AppendLine(error.Trim()); - _ = builder.AppendLine(); - _ = builder.AppendLine("Make sure to only be using supported features by checking the README file in the ComputeSharp repository: https://github.com/Sergio0694/ComputeSharp."); - _ = builder.Append("If you're sure that your C# shader code is valid, please open an issue an include a working repro and this error message."); - - return builder.ToString(); + return + $"""The DXC compiler encountered one or more errors while trying to compile the shader: "{message}". """ + + $"""Make sure to only be using supported features by checking the README file in the ComputeSharp repository: https://github.com/Sergio0694/ComputeSharp. """ + + $"""If you're sure that your C# shader code is valid, please open an issue an include a working repro and this error message."""; } } \ No newline at end of file diff --git a/src/ComputeSharp/Graphics/Exceptions/GraphicsDeviceMismatchException.cs b/src/ComputeSharp/Graphics/Exceptions/GraphicsDeviceMismatchException.cs index 34c3fcde..1659e0f1 100644 --- a/src/ComputeSharp/Graphics/Exceptions/GraphicsDeviceMismatchException.cs +++ b/src/ComputeSharp/Graphics/Exceptions/GraphicsDeviceMismatchException.cs @@ -1,5 +1,4 @@ using System; -using System.Text; using ComputeSharp.Interop; using ComputeSharp.Resources; @@ -32,15 +31,13 @@ public sealed class GraphicsDeviceMismatchException : InvalidOperationException /// private static GraphicsDeviceMismatchException Create(IReferenceTrackedObject resource, GraphicsDevice sourceDevice, GraphicsDevice destinationDevice) { - StringBuilder builder = new(512); + string message = + $"""Invalid pairing of graphics devices used to run a compute shader and allocate memory buffers. """ + + $"""The target device to run the compute shader is "{destinationDevice}". """ + + $"""The buffer of type {resource.GetType()} was allocated on device "{sourceDevice}". """ + + $"""Make sure to always allocate buffers on the same device used to actually run the code that accesses them."""; - _ = 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 \"{destinationDevice}\"."); - _ = builder.AppendLine($"The buffer of type {resource.GetType()} was allocated on device \"{sourceDevice}\"."); - _ = builder.Append("Make sure to always allocate buffers on the same device used to actually run the code that accesses them."); - _ = builder.ToString(); - - return new(builder.ToString()); + return new(message); } /// diff --git a/src/ComputeSharp/Graphics/Exceptions/UnsupportedDoubleOperationsException.cs b/src/ComputeSharp/Graphics/Exceptions/UnsupportedDoubleOperationsException.cs index 0c4b8fdb..60f8085c 100644 --- a/src/ComputeSharp/Graphics/Exceptions/UnsupportedDoubleOperationsException.cs +++ b/src/ComputeSharp/Graphics/Exceptions/UnsupportedDoubleOperationsException.cs @@ -1,5 +1,4 @@ using System; -using System.Text; namespace ComputeSharp.Exceptions; @@ -24,14 +23,12 @@ public sealed class UnsupportedDoubleOperationException : NotSupportedException /// A new instance with a formatted error message. private static UnsupportedDoubleOperationException Create(Type type) { - StringBuilder builder = new(256); + string message = + $"The device in use does not support creating resources of type {type}, as it does not support using double precision floating point numbers. " + + $"As a possible workaround, consider replacing usage of the double type with single precision floating point numbers. " + + $"Note that double precision operations are only partially supported on GPU devices, and are far slower than single precision ones."; - _ = builder.AppendLine($"The device in use does not support creating resources of type {type}, as it does not support using double precision floating point numbers."); - _ = builder.AppendLine("As a possible workaround, consider replacing usage of the double type with single precision floating point numbers."); - _ = builder.AppendLine("Note that double precision operations are only partially supported on GPU devices, and are far slower than single precision ones."); - _ = builder.ToString(); - - return new(builder.ToString()); + return new(message); } /// diff --git a/src/ComputeSharp/Graphics/Exceptions/UnsupportedTextureTypeException.cs b/src/ComputeSharp/Graphics/Exceptions/UnsupportedTextureTypeException.cs index 25ab893d..5566a370 100644 --- a/src/ComputeSharp/Graphics/Exceptions/UnsupportedTextureTypeException.cs +++ b/src/ComputeSharp/Graphics/Exceptions/UnsupportedTextureTypeException.cs @@ -1,5 +1,4 @@ using System; -using System.Text; namespace ComputeSharp.Exceptions; @@ -25,22 +24,20 @@ public sealed class UnsupportedTextureTypeException : NotSupportedException /// A new instance with a formatted error message. private static UnsupportedTextureTypeException Create(int rank, Type type) { - StringBuilder builder = new(256); - - _ = builder.AppendLine($"The device in use does not support creating {rank}D textures of type {type}."); - _ = builder.Append($"Make sure to check the support at runtime by using {nameof(GraphicsDevice)}."); - - _ = builder.AppendLine(rank switch + string suggestedMethods = rank switch { - 1 => $"{nameof(GraphicsDevice.IsReadOnlyTexture1DSupportedForType)}() or {nameof(GraphicsDevice)}.{nameof(GraphicsDevice.IsReadWriteTexture1DSupportedForType)}().", - 2 => $"{nameof(GraphicsDevice.IsReadOnlyTexture2DSupportedForType)}() or {nameof(GraphicsDevice)}.{nameof(GraphicsDevice.IsReadWriteTexture2DSupportedForType)}().", - 3 => $"{nameof(GraphicsDevice.IsReadOnlyTexture3DSupportedForType)}() or {nameof(GraphicsDevice)}.{nameof(GraphicsDevice.IsReadWriteTexture3DSupportedForType)}().", + 1 => $"{nameof(GraphicsDevice)}.{nameof(GraphicsDevice.IsReadOnlyTexture1DSupportedForType)}() or {nameof(GraphicsDevice)}.{nameof(GraphicsDevice.IsReadWriteTexture1DSupportedForType)}()", + 2 => $"{nameof(GraphicsDevice)}.{nameof(GraphicsDevice.IsReadOnlyTexture2DSupportedForType)}() or {nameof(GraphicsDevice)}.{nameof(GraphicsDevice.IsReadWriteTexture2DSupportedForType)}()", + 3 => $"{nameof(GraphicsDevice)}.{nameof(GraphicsDevice.IsReadOnlyTexture3DSupportedForType)}() or {nameof(GraphicsDevice)}.{nameof(GraphicsDevice.IsReadWriteTexture3DSupportedForType)}()", _ => default(ArgumentException).Throw(nameof(rank)) - }); + }; - _ = builder.Append("As a possible workaround on older devices, consider using a texture type of lower rank, or a linear buffer."); + string message = + $"The device in use does not support creating {rank}D textures of type {type}. " + + $"Make sure to check the support at runtime by using {suggestedMethods}. " + + $"As a possible workaround on older devices, consider using a texture type of lower rank, or a linear buffer."; - return new(builder.ToString()); + return new(message); } ///