diff --git a/ComputeSharp.sln b/ComputeSharp.sln index fe3ef9c6..f1f78326 100644 --- a/ComputeSharp.sln +++ b/ComputeSharp.sln @@ -75,6 +75,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Dynamic", "src EndProject Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "TerraFX.Interop.Windows.Dynamic", "src\TerraFX.Interop.Windows.Dynamic\TerraFX.Interop.Windows.Dynamic.shproj", "{158F984D-CD96-4062-BD02-2268946031A0}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.D2D1Interop", "src\ComputeSharp.D2D1Interop\ComputeSharp.D2D1Interop.csproj", "{4D7D9BA8-A821-4B1C-9EE2-CBCF5849906E}" +EndProject Global GlobalSection(SharedMSBuildProjectFiles) = preSolution samples\ComputeSharp.SwapChain.Core.Shared\ComputeSharp.SwapChain.Core.Shared.projitems*{0071701e-b9c5-4098-be5f-ef556c4553c6}*SharedItemsImports = 5 @@ -337,6 +339,18 @@ Global {76C66257-FDAA-47AB-85E7-EF757EAB10C0}.Release|ARM64.Build.0 = Release|ARM64 {76C66257-FDAA-47AB-85E7-EF757EAB10C0}.Release|x64.ActiveCfg = Release|x64 {76C66257-FDAA-47AB-85E7-EF757EAB10C0}.Release|x64.Build.0 = Release|x64 + {4D7D9BA8-A821-4B1C-9EE2-CBCF5849906E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4D7D9BA8-A821-4B1C-9EE2-CBCF5849906E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4D7D9BA8-A821-4B1C-9EE2-CBCF5849906E}.Debug|ARM64.ActiveCfg = Debug|Any CPU + {4D7D9BA8-A821-4B1C-9EE2-CBCF5849906E}.Debug|ARM64.Build.0 = Debug|Any CPU + {4D7D9BA8-A821-4B1C-9EE2-CBCF5849906E}.Debug|x64.ActiveCfg = Debug|Any CPU + {4D7D9BA8-A821-4B1C-9EE2-CBCF5849906E}.Debug|x64.Build.0 = Debug|Any CPU + {4D7D9BA8-A821-4B1C-9EE2-CBCF5849906E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4D7D9BA8-A821-4B1C-9EE2-CBCF5849906E}.Release|Any CPU.Build.0 = Release|Any CPU + {4D7D9BA8-A821-4B1C-9EE2-CBCF5849906E}.Release|ARM64.ActiveCfg = Release|Any CPU + {4D7D9BA8-A821-4B1C-9EE2-CBCF5849906E}.Release|ARM64.Build.0 = Release|Any CPU + {4D7D9BA8-A821-4B1C-9EE2-CBCF5849906E}.Release|x64.ActiveCfg = Release|Any CPU + {4D7D9BA8-A821-4B1C-9EE2-CBCF5849906E}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -361,6 +375,7 @@ Global {751BB05B-563D-4B3A-94C6-15E2D79D35EA} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1} {4A8FCE22-851C-442A-8BC9-3EC3FD00B637} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7} {158F984D-CD96-4062-BD02-2268946031A0} = {8DCE343F-BA78-4FE1-B562-B57C603A8BAA} + {6892B4F9-163D-4F08-9910-114C2A3B986F} = {8DCE343F-BA78-4FE1-B562-B57C603A8BAA} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {4664C5E3-0340-4E22-BCFD-98AAEDF5F2DC} diff --git a/src/ComputeSharp.D2D1Interop/ComputeSharp.D2D1Interop.csproj b/src/ComputeSharp.D2D1Interop/ComputeSharp.D2D1Interop.csproj new file mode 100644 index 00000000..16e8e2af --- /dev/null +++ b/src/ComputeSharp.D2D1Interop/ComputeSharp.D2D1Interop.csproj @@ -0,0 +1,17 @@ + + + net6.0 + bin\Release + ComputeSharp.D2D1Interop.xml + AnyCPU;x64;ARM64 + + + + + true + true + + + + + diff --git a/src/ComputeSharp.D2D1Interop/Extensions/ComPtrExtensions.cs b/src/ComputeSharp.D2D1Interop/Extensions/ComPtrExtensions.cs new file mode 100644 index 00000000..2d473c06 --- /dev/null +++ b/src/ComputeSharp.D2D1Interop/Extensions/ComPtrExtensions.cs @@ -0,0 +1,28 @@ +using System.Runtime.CompilerServices; +using TerraFX.Interop.Windows; + +namespace ComputeSharp.D2D1Interop.Extensions; + +/// +/// Helper methods for working with the type. +/// +/// Trimmed down version of the same file in ComputeSharp. +internal static class ComPtrExtensions +{ + /// + /// Moves the current instance and resets it without releasing the reference. + /// + /// The type to wrap in the current instance. + /// The input instance to move. + /// The moved instance. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static unsafe ComPtr Move(this in ComPtr ptr) + where T : unmanaged, IUnknown.Interface + { + ComPtr copy = default; + + Unsafe.AsRef(in ptr).Swap(ref copy); + + return copy; + } +} diff --git a/src/ComputeSharp.D2D1Interop/Extensions/HResultExtensions.cs b/src/ComputeSharp.D2D1Interop/Extensions/HResultExtensions.cs new file mode 100644 index 00000000..39d82d56 --- /dev/null +++ b/src/ComputeSharp.D2D1Interop/Extensions/HResultExtensions.cs @@ -0,0 +1,53 @@ +using System.ComponentModel; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using TerraFX.Interop.Windows; + +namespace ComputeSharp.D2D1Interop.Extensions; + +/// +/// Helper methods to efficiently throw exceptions. +/// +/// Trimmed down version of the same file in ComputeSharp. +[DebuggerStepThrough] +internal static class HResultExtensions +{ + /// + /// Throws a if represents an error. + /// + /// The input to check. + /// Thrown if represents an error. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void Assert(this HRESULT result) + { + if ((int)result < 0) + { + ThrowWin32Exception(result); + } + } + + /// + /// Throws a if represents an error. + /// + /// The input to check. + /// Thrown if represents an error. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void Assert(this int result) + { + if (result < 0) + { + ThrowWin32Exception(result); + } + } + + /// + /// Throws a . + /// + /// The input return code. + [DoesNotReturn] + private static void ThrowWin32Exception(int result) + { + throw new Win32Exception(result); + } +} diff --git a/src/ComputeSharp.D2D1Interop/Shaders/Exceptions/FxcCompilationException.cs b/src/ComputeSharp.D2D1Interop/Shaders/Exceptions/FxcCompilationException.cs new file mode 100644 index 00000000..97e4a3bc --- /dev/null +++ b/src/ComputeSharp.D2D1Interop/Shaders/Exceptions/FxcCompilationException.cs @@ -0,0 +1,38 @@ +using System; +using System.Text; + +namespace ComputeSharp.D2D1Interop.Exceptions; + +/// +/// A custom type that indicates when a shader compilation with the FXC compiler has failed. +/// +public sealed class FxcCompilationException : Exception +{ + /// + /// Creates a new instance. + /// + /// The error message produced by the DXC compiler. + internal FxcCompilationException(string error) + : base(GetExceptionMessage(error)) + { + } + + /// + /// Gets a formatted exception message for a given compilation error. + /// + /// The input compilatin error message from the FXC compiler. + /// A formatted error message for a new instance. + private static string GetExceptionMessage(string error) + { + StringBuilder builder = new(512); + + 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(); + } +} diff --git a/src/ComputeSharp.Dynamic/Shaders/Translation/D2D1ShaderCompiler.cs b/src/ComputeSharp.D2D1Interop/Shaders/Translation/D2D1ShaderCompiler.cs similarity index 97% rename from src/ComputeSharp.Dynamic/Shaders/Translation/D2D1ShaderCompiler.cs rename to src/ComputeSharp.D2D1Interop/Shaders/Translation/D2D1ShaderCompiler.cs index f7b7bafd..c0f9e910 100644 --- a/src/ComputeSharp.Dynamic/Shaders/Translation/D2D1ShaderCompiler.cs +++ b/src/ComputeSharp.D2D1Interop/Shaders/Translation/D2D1ShaderCompiler.cs @@ -3,17 +3,14 @@ using System.Buffers; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; -using ComputeSharp.Core.Extensions; -using ComputeSharp.Exceptions; +using ComputeSharp.D2D1Interop.Exceptions; +using ComputeSharp.D2D1Interop.Extensions; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; -#if !NET6_0_OR_GREATER -using DirectX = TerraFX.Interop.DirectX.DirectX2; -#endif #pragma warning disable IDE1006 -namespace ComputeSharp.Shaders.Translation; +namespace ComputeSharp.D2D1Interop.Shaders.Translation; /// /// A that uses the FXC APIs to compile D2D1 pixel shaders. @@ -212,7 +209,7 @@ internal static unsafe class D2D1ShaderCompiler { string message = new((sbyte*)d3DOperationResult->GetBufferPointer()); - throw new HlslCompilationException(message); + throw new FxcCompilationException(message); } /// diff --git a/src/ComputeSharp.Dynamic/ComputeSharp.Dynamic.csproj b/src/ComputeSharp.Dynamic/ComputeSharp.Dynamic.csproj index 4b638e24..12d74cec 100644 --- a/src/ComputeSharp.Dynamic/ComputeSharp.Dynamic.csproj +++ b/src/ComputeSharp.Dynamic/ComputeSharp.Dynamic.csproj @@ -12,7 +12,7 @@ true - + diff --git a/src/ComputeSharp.Dynamic/Shaders/Exceptions/HlslCompilationException.cs b/src/ComputeSharp.Dynamic/Shaders/Exceptions/DxcCompilationException.cs similarity index 85% rename from src/ComputeSharp.Dynamic/Shaders/Exceptions/HlslCompilationException.cs rename to src/ComputeSharp.Dynamic/Shaders/Exceptions/DxcCompilationException.cs index f0b3aa06..3c315dd7 100644 --- a/src/ComputeSharp.Dynamic/Shaders/Exceptions/HlslCompilationException.cs +++ b/src/ComputeSharp.Dynamic/Shaders/Exceptions/DxcCompilationException.cs @@ -6,13 +6,13 @@ namespace ComputeSharp.Exceptions; /// /// A custom type that indicates when a shader compilation with the DXC compiler has failed. /// -public sealed class HlslCompilationException : Exception +public sealed class DxcCompilationException : Exception { /// - /// Creates a new instance. + /// Creates a new instance. /// /// The error message produced by the DXC compiler. - internal HlslCompilationException(string error) + internal DxcCompilationException(string error) : base(GetExceptionMessage(error)) { } @@ -21,7 +21,7 @@ public sealed class HlslCompilationException : Exception /// Gets a formatted exception message for a given compilation error. /// /// The input compilatin error message from the DXC compiler. - /// A formatted error message for a new instance. + /// A formatted error message for a new instance. private static string GetExceptionMessage(string error) { StringBuilder builder = new(512); diff --git a/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.cs b/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.cs index 817c071d..baf233fe 100644 --- a/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.cs +++ b/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.cs @@ -151,6 +151,6 @@ internal sealed unsafe partial class ShaderCompiler string message = new((sbyte*)dxcBlobEncodingError.Get()->GetBufferPointer()); - throw new HlslCompilationException(message); + throw new DxcCompilationException(message); } } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj index bfd2581e..e55b7953 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj +++ b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj @@ -73,7 +73,7 @@ - + diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 6f41eb53..7f6e4f12 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -639,7 +639,7 @@ internal static class DiagnosticDescriptors /// Format: "The shader of type {{0}} failed to compile due to an HLSL compiler error (Message: "{1}")". /// /// - public static readonly DiagnosticDescriptor EmbeddedBytecodeFailedWithHlslCompilationException = new DiagnosticDescriptor( + public static readonly DiagnosticDescriptor EmbeddedBytecodeFailedWithDxcCompilationException = new DiagnosticDescriptor( id: "CMPS0046", title: "Embedded bytecode compilation failed due to an HLSL compiler error", messageFormat: "The shader of type {0} failed to compile due to an HLSL compiler error (Message: \"{1}\")", diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs index 9352dd52..7002e87b 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs @@ -188,9 +188,9 @@ public sealed partial class IShaderGenerator { diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithWin32Exception, e.HResult, e.Message); } - catch (HlslCompilationException e) + catch (DxcCompilationException e) { - diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithHlslCompilationException, e.Message); + diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithDxcCompilationException, e.Message); } End: diff --git a/src/ComputeSharp/Properties/AssemblyInfo.cs b/src/ComputeSharp/Properties/AssemblyInfo.cs index 80e60132..ebbe6d87 100644 --- a/src/ComputeSharp/Properties/AssemblyInfo.cs +++ b/src/ComputeSharp/Properties/AssemblyInfo.cs @@ -3,6 +3,7 @@ [module: SkipLocalsInit] [assembly: InternalsVisibleTo("ComputeSharp.Dynamic")] +[assembly: InternalsVisibleTo("ComputeSharp.D2D1Interop")] [assembly: InternalsVisibleTo("ComputeSharp.Uwp")] [assembly: InternalsVisibleTo("ComputeSharp.WinUI")] diff --git a/tests/ComputeSharp.Tests.Internals/ShaderCompilerTests.cs b/tests/ComputeSharp.Tests.Internals/ShaderCompilerTests.cs index 7fd41eff..b7744888 100644 --- a/tests/ComputeSharp.Tests.Internals/ShaderCompilerTests.cs +++ b/tests/ComputeSharp.Tests.Internals/ShaderCompilerTests.cs @@ -37,7 +37,7 @@ public class ShaderCompilerTests } [TestMethod] - [ExpectedException(typeof(HlslCompilationException))] + [ExpectedException(typeof(DxcCompilationException))] public void CompileTest_Fail() { var faultyShader = ShaderSource.Replace("ids.x", "ids.X");