diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs index e7ec8eab..797a7e72 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs @@ -1,5 +1,4 @@ using System; -using System.Buffers; using System.Collections.Immutable; using System.ComponentModel; using System.Linq; @@ -134,41 +133,7 @@ partial class ID2D1ShaderGenerator // Add square brackets around error headers message = Regex.Replace(message, @"((?:error|warning) \w+):", static m => $"[{m.Groups[1].Value}]:"); - char[] buffer = ArrayPool.Shared.Rent(message.Length); - int index = 0; - char lastCharacter = '\0'; - - foreach (char newCharacter in message) - { - char pendingCharacter = newCharacter; - - // Ignore '\r' characters - if (pendingCharacter is '\r') - { - continue; - } - - // Convert newlines to spaces (this avoids VS droppping multiline diagnostics) - if (pendingCharacter is '\n') - { - pendingCharacter = ' '; - } - - // Drop duplicate spaces - if (pendingCharacter is ' ' && - lastCharacter is ' ') - { - continue; - } - - buffer[index++] = lastCharacter = pendingCharacter; - } - - string text = buffer.AsSpan(0, index).ToString(); - - ArrayPool.Shared.Return(buffer); - - return text; + return message.NormalizeToSingleLine(); } } } diff --git a/src/ComputeSharp.D2D1/Shaders/Translation/D2D1ShaderCompiler.cs b/src/ComputeSharp.D2D1/Shaders/Translation/D2D1ShaderCompiler.cs index cd931470..9b879275 100644 --- a/src/ComputeSharp.D2D1/Shaders/Translation/D2D1ShaderCompiler.cs +++ b/src/ComputeSharp.D2D1/Shaders/Translation/D2D1ShaderCompiler.cs @@ -226,7 +226,7 @@ internal static unsafe partial class D2D1ShaderCompiler // The error message will be in a format like this: // "C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Shader@0x0000019AD1B4BA70(22,32-35): error X3004: undeclared identifier 'this'" // This regex tries to match the unnecessary header and remove it, if present. This doesn't need to be bulletproof, and this regex should match all cases anyway. - message = Regex.Replace(message, @"[A-Z]:\\[^:]+: (\w+ \w+:)", static m => m.Groups[1].Value).Trim(); + message = Regex.Replace(message, @"^[A-Z]:\\[^:]+: (\w+ \w+:)", static m => m.Groups[1].Value, RegexOptions.Multiline).Trim(); // Add a trailing '.' if not present if (message is { Length: > 0 } && diff --git a/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.cs b/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.cs index baf233fe..aff3c606 100644 --- a/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.cs +++ b/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.cs @@ -1,5 +1,6 @@ using System; using System.Runtime.CompilerServices; +using System.Text.RegularExpressions; using ComputeSharp.Core.Extensions; using ComputeSharp.Exceptions; using TerraFX.Interop.DirectX; @@ -151,6 +152,22 @@ internal sealed unsafe partial class ShaderCompiler string message = new((sbyte*)dxcBlobEncodingError.Get()->GetBufferPointer()); + // The error message will be in a format like this: + // "hlsl.hlsl:11:20: error: redefinition of 'float1' as different kind of symbol + // static const float float1 = asfloat(0xFFC00000); + // ^ + // note: previous definition is here" + // These regex-s try to match the unnecessary headers and remove them, if present. + // This doesn't need to be bulletproof, and these regex-s should match all cases anyway. + message = Regex.Replace(message, @"^hlsl\.hlsl:\d+:\d+: (\w+:)", static m => m.Groups[1].Value, RegexOptions.Multiline); + + // Add a trailing '.' if not present + if (message is { Length: > 0 } && + message[message.Length - 1] != '.') + { + message += '.'; + } + throw new DxcCompilationException(message); } } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Extensions/StringExtensions.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Extensions/StringExtensions.cs index f0847fa0..f01e6d86 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Extensions/StringExtensions.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Extensions/StringExtensions.cs @@ -1,4 +1,7 @@ -namespace ComputeSharp.SourceGeneration.Extensions; +using System; +using System.Buffers; + +namespace ComputeSharp.SourceGeneration.Extensions; /// /// Extension methods for types. @@ -14,4 +17,53 @@ internal static class StringExtensions { return text.Replace('.', '_').Replace('+', '_'); } + + /// + /// Normalizes a given instance to make it single line and with no repeated spaces. + /// + /// The input instance to normalize. + /// The normalized version of . + public static string NormalizeToSingleLine(this string text) + { + if (text is "") + { + return text; + } + + char[] buffer = ArrayPool.Shared.Rent(text.Length); + int index = 0; + char lastCharacter = '\0'; + + foreach (char newCharacter in text) + { + char pendingCharacter = newCharacter; + + // Ignore '\r' characters + if (pendingCharacter is '\r') + { + continue; + } + + // Convert newlines to spaces (this avoids VS droppping multiline diagnostics) + if (pendingCharacter is '\n') + { + pendingCharacter = ' '; + } + + // Drop duplicate spaces + if (pendingCharacter is ' ' && + lastCharacter is ' ') + { + continue; + } + + buffer[index++] = lastCharacter = pendingCharacter; + } + + text = buffer.AsSpan(0, index).ToString(); + + ArrayPool.Shared.Return(buffer); + + return text; + } } diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs index dc01d486..f535b721 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using System.Text.RegularExpressions; using System.Threading; using ComputeSharp.Exceptions; using ComputeSharp.Shaders.Translation; @@ -187,17 +188,36 @@ partial class IShaderGenerator } catch (Win32Exception e) { - diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithWin32Exception, e.HResult, e.Message); + diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithWin32Exception, e.HResult, FixupExceptionMessage(e.Message)); } catch (DxcCompilationException e) { - diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithDxcCompilationException, e.Message); + diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithDxcCompilationException, FixupExceptionMessage(e.Message)); } End: return bytecode; } + /// + /// Fixes up an exception message to improve the way it's displayed in VS. + /// + /// The input exception message. + /// The updated exception message. + private static string FixupExceptionMessage(string message) + { + // Add square brackets around error headers + message = Regex.Replace(message, @"^(error|warning):", static m => $"[{m.Groups[1].Value}]:", RegexOptions.Multiline); + + // Remove lines with notes + message = Regex.Replace(message, @"^note:.+", string.Empty, RegexOptions.Multiline); + + // Remove syntax error indicators + message = Regex.Replace(message, @"^ +\^", string.Empty, RegexOptions.Multiline); + + return message.NormalizeToSingleLine(); + } + /// /// Extracts and loads the dxcompiler.dll and dxil.dll libraries. ///