From 9143b5f704592213bef0106db7913df125c1e624 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 16 Sep 2019 19:17:43 +0200 Subject: [PATCH] Added custom exception on shader compiler error --- .../Exceptions/HlslCompilationException.cs | 31 +++++++++++++++++++ .../Translation/ShaderCompiler.cs | 6 ++-- 2 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 src/ComputeSharp.Shaders/Exceptions/HlslCompilationException.cs diff --git a/src/ComputeSharp.Shaders/Exceptions/HlslCompilationException.cs b/src/ComputeSharp.Shaders/Exceptions/HlslCompilationException.cs new file mode 100644 index 00000000..13a95b6d --- /dev/null +++ b/src/ComputeSharp.Shaders/Exceptions/HlslCompilationException.cs @@ -0,0 +1,31 @@ +using System; +using System.Diagnostics.Contracts; +using System.Text; + +namespace ComputeSharp.Exceptions +{ + /// + /// A custom that indicates when mismatched devices are being used + /// + public sealed class HlslCompilationException : Exception + { + /// + /// Creates a new instance + /// + /// The error message produced by the Dxc compiler + internal HlslCompilationException(string error) : base(GetExceptionMessage(error)) { } + + // Gets a proper exception message for a given compilation error + [Pure] + private static string GetExceptionMessage(string error) + { + StringBuilder builder = new StringBuilder(); + builder.Append("The Dxc compiler encountered an error while trying to compile the shader."); + 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.AppendLine("If you're sure that your C# shader code is valid, please open an issue an include a working repro and the following error message:"); + builder.AppendLine(error.TrimEnd()); + + return builder.ToString(); + } + } +} diff --git a/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs b/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs index 0cedbcaf..ec7b9c0c 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs @@ -1,5 +1,5 @@ -using System; -using System.Diagnostics.Contracts; +using System.Diagnostics.Contracts; +using ComputeSharp.Exceptions; using Vortice.Direct3D12; using Vortice.Dxc; @@ -43,7 +43,7 @@ namespace ComputeSharp.Shaders.Translation // Compile error string resultText = Dxc.GetStringFromBlob(Library, result.GetErrors()); - throw new Exception(resultText); + throw new HlslCompilationException(resultText); } } }