diff --git a/ComputeSharp.sln b/ComputeSharp.sln index 01e8f1c0..066d9426 100644 --- a/ComputeSharp.sln +++ b/ComputeSharp.sln @@ -17,8 +17,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Benchmark", "s EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.ImageProcessing", "samples\ComputeSharp.ImageProcessing\ComputeSharp.ImageProcessing.csproj", "{524CC5DD-1DEE-4A30-9515-AB90965E5207}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Sample", "samples\ComputeSharp.Sample\ComputeSharp.Sample.csproj", "{7A48A85D-8866-4DED-A404-653ED09BF396}" -EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Tests", "tests\ComputeSharp.Tests\ComputeSharp.Tests.csproj", "{E91F4D24-922A-4921-A8D7-9C45A143C45D}" @@ -39,7 +37,22 @@ Project("{13B669BE-BB05-4DDF-9536-439F39A36129}") = "ComputeSharp.Package", "src EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "NuGet", "NuGet", "{FB17EF84-9208-47AC-921F-680CB1DAD8BA}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Sample.Local", "samples\ComputeSharp.Sample.Local\ComputeSharp.Sample.Local.csproj", "{7048DEEA-0A6F-46A0-B951-B682A8F1CB75}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Sample.NuGet", "samples\ComputeSharp.Sample.NuGet\ComputeSharp.Sample.NuGet.csproj", "{C423C061-64F7-4C12-8E5F-75C5351D6308}" +EndProject +Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "ComputeSharp.Sample.Shared", "samples\ComputeSharp.Sample.Shared\ComputeSharp.Sample.Shared.shproj", "{8CEFC324-CA6C-4845-9AD9-70B92BD87057}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Tests.Internals.SourceGenerators", "tests\ComputeSharp.Tests.Internals.SourceGenerators\ComputeSharp.Tests.Internals.SourceGenerators.csproj", "{BBBD0ADD-F2F9-49A3-BD5E-D223F0735D8B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputeSharp.Tests.NativeLibrariesResolver", "tests\ComputeSharp.Tests.NativeLibrariesResolver\ComputeSharp.Tests.NativeLibrariesResolver.csproj", "{BF52326F-E415-49E6-8595-62762216762B}" +EndProject Global + GlobalSection(SharedMSBuildProjectFiles) = preSolution + samples\ComputeSharp.Sample.Shared\ComputeSharp.Sample.Shared.projitems*{7048deea-0a6f-46a0-b951-b682a8f1cb75}*SharedItemsImports = 5 + samples\ComputeSharp.Sample.Shared\ComputeSharp.Sample.Shared.projitems*{8cefc324-ca6c-4845-9ad9-70b92bd87057}*SharedItemsImports = 13 + samples\ComputeSharp.Sample.Shared\ComputeSharp.Sample.Shared.projitems*{c423c061-64f7-4c12-8e5f-75c5351d6308}*SharedItemsImports = 5 + EndGlobalSection GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Debug|x64 = Debug|x64 @@ -95,14 +108,6 @@ Global {524CC5DD-1DEE-4A30-9515-AB90965E5207}.Release|Any CPU.Build.0 = Release|Any CPU {524CC5DD-1DEE-4A30-9515-AB90965E5207}.Release|x64.ActiveCfg = Release|Any CPU {524CC5DD-1DEE-4A30-9515-AB90965E5207}.Release|x64.Build.0 = Release|Any CPU - {7A48A85D-8866-4DED-A404-653ED09BF396}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {7A48A85D-8866-4DED-A404-653ED09BF396}.Debug|Any CPU.Build.0 = Debug|Any CPU - {7A48A85D-8866-4DED-A404-653ED09BF396}.Debug|x64.ActiveCfg = Debug|Any CPU - {7A48A85D-8866-4DED-A404-653ED09BF396}.Debug|x64.Build.0 = Debug|Any CPU - {7A48A85D-8866-4DED-A404-653ED09BF396}.Release|Any CPU.ActiveCfg = Release|Any CPU - {7A48A85D-8866-4DED-A404-653ED09BF396}.Release|Any CPU.Build.0 = Release|Any CPU - {7A48A85D-8866-4DED-A404-653ED09BF396}.Release|x64.ActiveCfg = Release|Any CPU - {7A48A85D-8866-4DED-A404-653ED09BF396}.Release|x64.Build.0 = Release|Any CPU {E91F4D24-922A-4921-A8D7-9C45A143C45D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {E91F4D24-922A-4921-A8D7-9C45A143C45D}.Debug|Any CPU.Build.0 = Debug|Any CPU {E91F4D24-922A-4921-A8D7-9C45A143C45D}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -135,6 +140,38 @@ Global {0A6AE752-2A1D-40F8-B9E7-46B60F5321A3}.Release|Any CPU.Build.0 = Release|Any CPU {0A6AE752-2A1D-40F8-B9E7-46B60F5321A3}.Release|x64.ActiveCfg = Release|Any CPU {0A6AE752-2A1D-40F8-B9E7-46B60F5321A3}.Release|x64.Build.0 = Release|Any CPU + {7048DEEA-0A6F-46A0-B951-B682A8F1CB75}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7048DEEA-0A6F-46A0-B951-B682A8F1CB75}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7048DEEA-0A6F-46A0-B951-B682A8F1CB75}.Debug|x64.ActiveCfg = Debug|Any CPU + {7048DEEA-0A6F-46A0-B951-B682A8F1CB75}.Debug|x64.Build.0 = Debug|Any CPU + {7048DEEA-0A6F-46A0-B951-B682A8F1CB75}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7048DEEA-0A6F-46A0-B951-B682A8F1CB75}.Release|Any CPU.Build.0 = Release|Any CPU + {7048DEEA-0A6F-46A0-B951-B682A8F1CB75}.Release|x64.ActiveCfg = Release|Any CPU + {7048DEEA-0A6F-46A0-B951-B682A8F1CB75}.Release|x64.Build.0 = Release|Any CPU + {C423C061-64F7-4C12-8E5F-75C5351D6308}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C423C061-64F7-4C12-8E5F-75C5351D6308}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C423C061-64F7-4C12-8E5F-75C5351D6308}.Debug|x64.ActiveCfg = Debug|Any CPU + {C423C061-64F7-4C12-8E5F-75C5351D6308}.Debug|x64.Build.0 = Debug|Any CPU + {C423C061-64F7-4C12-8E5F-75C5351D6308}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C423C061-64F7-4C12-8E5F-75C5351D6308}.Release|Any CPU.Build.0 = Release|Any CPU + {C423C061-64F7-4C12-8E5F-75C5351D6308}.Release|x64.ActiveCfg = Release|Any CPU + {C423C061-64F7-4C12-8E5F-75C5351D6308}.Release|x64.Build.0 = Release|Any CPU + {BBBD0ADD-F2F9-49A3-BD5E-D223F0735D8B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BBBD0ADD-F2F9-49A3-BD5E-D223F0735D8B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BBBD0ADD-F2F9-49A3-BD5E-D223F0735D8B}.Debug|x64.ActiveCfg = Debug|Any CPU + {BBBD0ADD-F2F9-49A3-BD5E-D223F0735D8B}.Debug|x64.Build.0 = Debug|Any CPU + {BBBD0ADD-F2F9-49A3-BD5E-D223F0735D8B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BBBD0ADD-F2F9-49A3-BD5E-D223F0735D8B}.Release|Any CPU.Build.0 = Release|Any CPU + {BBBD0ADD-F2F9-49A3-BD5E-D223F0735D8B}.Release|x64.ActiveCfg = Release|Any CPU + {BBBD0ADD-F2F9-49A3-BD5E-D223F0735D8B}.Release|x64.Build.0 = Release|Any CPU + {BF52326F-E415-49E6-8595-62762216762B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BF52326F-E415-49E6-8595-62762216762B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BF52326F-E415-49E6-8595-62762216762B}.Debug|x64.ActiveCfg = Debug|Any CPU + {BF52326F-E415-49E6-8595-62762216762B}.Debug|x64.Build.0 = Debug|Any CPU + {BF52326F-E415-49E6-8595-62762216762B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BF52326F-E415-49E6-8595-62762216762B}.Release|Any CPU.Build.0 = Release|Any CPU + {BF52326F-E415-49E6-8595-62762216762B}.Release|x64.ActiveCfg = Release|Any CPU + {BF52326F-E415-49E6-8595-62762216762B}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -142,10 +179,14 @@ Global GlobalSection(NestedProjects) = preSolution {34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1} {524CC5DD-1DEE-4A30-9515-AB90965E5207} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1} - {7A48A85D-8866-4DED-A404-653ED09BF396} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1} {E91F4D24-922A-4921-A8D7-9C45A143C45D} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7} {E889E6D6-A814-4950-AAC1-68F1ECFC5BD6} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7} {0A6AE752-2A1D-40F8-B9E7-46B60F5321A3} = {FB17EF84-9208-47AC-921F-680CB1DAD8BA} + {7048DEEA-0A6F-46A0-B951-B682A8F1CB75} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1} + {C423C061-64F7-4C12-8E5F-75C5351D6308} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1} + {8CEFC324-CA6C-4845-9AD9-70B92BD87057} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1} + {BBBD0ADD-F2F9-49A3-BD5E-D223F0735D8B} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7} + {BF52326F-E415-49E6-8595-62762216762B} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {4664C5E3-0340-4E22-BCFD-98AAEDF5F2DC} diff --git a/nuget.config b/nuget.config index af57fd7e..a06b48ea 100644 --- a/nuget.config +++ b/nuget.config @@ -3,5 +3,6 @@ + diff --git a/samples/ComputeSharp.Sample/ComputeSharp.Sample.csproj b/samples/ComputeSharp.Sample.Local/ComputeSharp.Sample.Local.csproj similarity index 74% rename from samples/ComputeSharp.Sample/ComputeSharp.Sample.csproj rename to samples/ComputeSharp.Sample.Local/ComputeSharp.Sample.Local.csproj index 8b2bf452..7c96952c 100644 --- a/samples/ComputeSharp.Sample/ComputeSharp.Sample.csproj +++ b/samples/ComputeSharp.Sample.Local/ComputeSharp.Sample.Local.csproj @@ -10,7 +10,9 @@ + PrivateAssets="contentfiles;build" /> + + diff --git a/samples/ComputeSharp.Sample.NuGet/ComputeSharp.Sample.NuGet.csproj b/samples/ComputeSharp.Sample.NuGet/ComputeSharp.Sample.NuGet.csproj new file mode 100644 index 00000000..bdf283bf --- /dev/null +++ b/samples/ComputeSharp.Sample.NuGet/ComputeSharp.Sample.NuGet.csproj @@ -0,0 +1,14 @@ + + + + Exe + net5.0 + + + + + + + + + diff --git a/samples/ComputeSharp.Sample.Shared/ComputeSharp.Sample.Shared.projitems b/samples/ComputeSharp.Sample.Shared/ComputeSharp.Sample.Shared.projitems new file mode 100644 index 00000000..f30f1996 --- /dev/null +++ b/samples/ComputeSharp.Sample.Shared/ComputeSharp.Sample.Shared.projitems @@ -0,0 +1,14 @@ + + + + $(MSBuildAllProjects);$(MSBuildThisFileFullPath) + true + 8cefc324-ca6c-4845-9ad9-70b92bd87057 + + + ComputeSharp.Sample.Shared + + + + + \ No newline at end of file diff --git a/samples/ComputeSharp.Sample.Shared/ComputeSharp.Sample.Shared.shproj b/samples/ComputeSharp.Sample.Shared/ComputeSharp.Sample.Shared.shproj new file mode 100644 index 00000000..51735cd3 --- /dev/null +++ b/samples/ComputeSharp.Sample.Shared/ComputeSharp.Sample.Shared.shproj @@ -0,0 +1,13 @@ + + + + 8cefc324-ca6c-4845-9ad9-70b92bd87057 + 14.0 + + + + + + + + diff --git a/samples/ComputeSharp.Sample/Program.cs b/samples/ComputeSharp.Sample.Shared/Program.cs similarity index 100% rename from samples/ComputeSharp.Sample/Program.cs rename to samples/ComputeSharp.Sample.Shared/Program.cs diff --git a/src/ComputeSharp.Core/Attributes/ShaderMethodSourceAttribute.cs b/src/ComputeSharp.Core/Attributes/ShaderMethodSourceAttribute.cs index 236d9f78..594e01ff 100644 --- a/src/ComputeSharp.Core/Attributes/ShaderMethodSourceAttribute.cs +++ b/src/ComputeSharp.Core/Attributes/ShaderMethodSourceAttribute.cs @@ -5,7 +5,6 @@ using System.Diagnostics.Contracts; using System.Reflection; using ComputeSharp.Core.Extensions; using ComputeSharp.Exceptions; -using Microsoft.Toolkit.Diagnostics; namespace ComputeSharp { @@ -79,7 +78,10 @@ namespace ComputeSharp [Pure] internal static ShaderMethodSourceAttribute GetForDelegate(Delegate function) { - Guard.IsTrue(function.Method.IsStatic, "Captured delegates need to wrap static methods"); + if (!function.Method.IsStatic) + { + ThrowArgumentExceptionForNonStaticShader(); + } var attributes = function.Method.DeclaringType!.Assembly.GetCustomAttributes(); string methodName = function.Method.GetFullName(); @@ -94,5 +96,13 @@ namespace ComputeSharp return MissingMethodSourceException.Throw(function); } + + /// + /// Throws an when a given is not static. + /// + private static void ThrowArgumentExceptionForNonStaticShader() + { + throw new ArgumentException("Captured delegates in a shader need to wrap a static method.", "function"); + } } } \ No newline at end of file diff --git a/src/ComputeSharp.Core/ComputeSharp.Core.csproj b/src/ComputeSharp.Core/ComputeSharp.Core.csproj index c820d0b9..85cab145 100644 --- a/src/ComputeSharp.Core/ComputeSharp.Core.csproj +++ b/src/ComputeSharp.Core/ComputeSharp.Core.csproj @@ -3,26 +3,11 @@ netstandard2.0;net5.0 true - 9.0 - - - - - - false - true - - false + 9.0 - - - - + diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs index 57cf2a07..963c3b5f 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs @@ -9,6 +9,7 @@ using ComputeSharp.Graphics.Interop; using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; +using static TerraFX.Interop.D3D12_FORMAT_SUPPORT1; using static TerraFX.Interop.D3D12_RESOURCE_STATES; using static TerraFX.Interop.D3D12_SRV_DIMENSION; using static TerraFX.Interop.D3D12_UAV_DIMENSION; @@ -63,6 +64,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract Guard.IsBetweenOrEqualTo(width, 1, FX.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION, nameof(width)); Guard.IsBetweenOrEqualTo(height, 1, FX.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION, nameof(height)); + if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE2D)) + { + UnsupportedTextureTypeException.ThrowForTexture2D(); + } + GraphicsDevice = device; Width = width; Height = height; diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs index db758741..6539319a 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs @@ -9,6 +9,7 @@ using ComputeSharp.Graphics.Interop; using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; +using static TerraFX.Interop.D3D12_FORMAT_SUPPORT1; using static TerraFX.Interop.D3D12_RESOURCE_STATES; using static TerraFX.Interop.D3D12_SRV_DIMENSION; using static TerraFX.Interop.D3D12_UAV_DIMENSION; @@ -65,6 +66,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract Guard.IsBetweenOrEqualTo(height, 1, FX.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION, nameof(height)); Guard.IsBetweenOrEqualTo(depth, 1, FX.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION, nameof(depth)); + if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE2D)) + { + UnsupportedTextureTypeException.ThrowForTexture2D(); + } + GraphicsDevice = device; Width = width; Height = height; diff --git a/src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj b/src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj index d5fab37a..8c720d45 100644 --- a/src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj +++ b/src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj @@ -6,7 +6,8 @@ - + + diff --git a/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs b/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs index ef4e927d..6790ec38 100644 --- a/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs +++ b/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs @@ -52,7 +52,6 @@ namespace ComputeSharp.Exceptions /// The type of values in the input buffer. /// The input that was used. /// The target instance that was used. - [Pure] internal static void Throw(Buffer buffer, GraphicsDevice device) where T : unmanaged { @@ -65,7 +64,6 @@ namespace ComputeSharp.Exceptions /// The type of values in the input buffer. /// The input that was used. /// The target instance that was used. - [Pure] internal static void Throw(Texture2D texture, GraphicsDevice device) where T : unmanaged { @@ -78,7 +76,6 @@ namespace ComputeSharp.Exceptions /// The type of values in the input buffer. /// The input that was used. /// The target instance that was used. - [Pure] internal static void Throw(Texture3D texture, GraphicsDevice device) where T : unmanaged { diff --git a/src/ComputeSharp.Graphics/Exceptions/UnsupportedTextureTypeException.cs b/src/ComputeSharp.Graphics/Exceptions/UnsupportedTextureTypeException.cs new file mode 100644 index 00000000..e7f1e242 --- /dev/null +++ b/src/ComputeSharp.Graphics/Exceptions/UnsupportedTextureTypeException.cs @@ -0,0 +1,70 @@ +using System; +using System.Diagnostics.Contracts; +using System.Text; +using ComputeSharp.Graphics; +using Microsoft.Toolkit.Diagnostics; + +namespace ComputeSharp.Exceptions +{ + /// + /// A custom that indicates when a texture was attempted to be created with an unsupported type. + /// + public sealed class UnsupportedTextureTypeException : InvalidOperationException + { + /// + /// Creates a new instance. + /// + /// The message that describes the error. + private UnsupportedTextureTypeException(string message) + : base(message) + { + } + + /// + /// Creates a new instance from the specified parameters. + /// + /// The rank of the texture that couldn't be created. + /// The element type of texture that was being created. + /// A new instance with a formatted error message. + [Pure] + 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 + { + 2 => $"{nameof(GraphicsDevice.IsTexture2DSupportedForType)}().", + 3 => $"{nameof(GraphicsDevice.IsTexture3DSupportedForType)}().", + _ => ThrowHelper.ThrowArgumentException("Invalid texture rank") + }); + + builder.AppendLine($"As a possible workaround on older devices, consider using a texture type of lower rank, or a linear buffer."); + builder.ToString(); + + return new(builder.ToString()); + } + + /// + /// Throws a new instance from the specified parameters. + /// + /// The type of values in the texture that couldn't be created. + internal static void ThrowForTexture2D() + where T : unmanaged + { + throw Create(2, typeof(T)); + } + + /// + /// Throws a new instance from the specified parameters. + /// + /// The type of values in the texture that couldn't be created. + internal static void ThrowForTexture3D() + where T : unmanaged + { + throw Create(3, typeof(T)); + } + } +} \ No newline at end of file diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs index 5e3d17f6..8a6f278c 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs @@ -9,6 +9,7 @@ using static TerraFX.Interop.D3D12_COMMAND_QUEUE_PRIORITY; using static TerraFX.Interop.D3D12_CPU_PAGE_PROPERTY; using static TerraFX.Interop.D3D12_DESCRIPTOR_HEAP_FLAGS; using static TerraFX.Interop.D3D12_DESCRIPTOR_HEAP_TYPE; +using static TerraFX.Interop.D3D12_FEATURE; using static TerraFX.Interop.D3D12_FENCE_FLAGS; using static TerraFX.Interop.D3D12_MEMORY_POOL; using static TerraFX.Interop.D3D12_HEAP_FLAGS; @@ -448,5 +449,23 @@ namespace ComputeSharp.Graphics.Extensions return feature; } + + /// + /// Checks whether or not a given DXGI format is supported for the specified resource type. + /// + /// The target to use to check features for. + /// The type to check support for. + /// The resource type to check support for. + /// Whether or not the input device supports the requested format for the specified resource type. + [Pure] + public static unsafe bool IsDxgiFormatSupported(this ref ID3D12Device d3D12Device, DXGI_FORMAT dxgiFormat, D3D12_FORMAT_SUPPORT1 d3D12FormatSupport1) + { + D3D12_FEATURE_DATA_FORMAT_SUPPORT d3D12FeatureDataFormatSupport = default; + d3D12FeatureDataFormatSupport.Format = dxgiFormat; + + d3D12Device.CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &d3D12FeatureDataFormatSupport, (uint)sizeof(D3D12_FEATURE_DATA_FORMAT_SUPPORT)).Assert(); + + return (d3D12FeatureDataFormatSupport.Support1 & d3D12FormatSupport1) != 0; + } } } diff --git a/src/ComputeSharp.Graphics/GraphicsDevice.cs b/src/ComputeSharp.Graphics/GraphicsDevice.cs index a5e18802..92503ee7 100644 --- a/src/ComputeSharp.Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp.Graphics/GraphicsDevice.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using ComputeSharp.Core.Extensions; using ComputeSharp.Graphics.Commands; @@ -10,6 +11,7 @@ using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; using static TerraFX.Interop.D3D12_FEATURE; +using static TerraFX.Interop.D3D12_FORMAT_SUPPORT1; namespace ComputeSharp.Graphics { @@ -126,6 +128,36 @@ namespace ComputeSharp.Graphics /// internal ID3D12Device* D3D12Device => this.d3D12Device; + /// + /// Checks whether the current device supports the creation of + /// resources for a specified type . + /// + /// The type of values to check support for. + /// Whether instances can be created by the current device. + [Pure] + public bool IsTexture2DSupportedForType() + where T : unmanaged + { + ThrowIfDisposed(); + + return this.d3D12Device.Get()->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE2D); + } + + /// + /// Checks whether the current device supports the creation of + /// resources for a specified type . + /// + /// The type of values to check support for. + /// Whether instances can be created by the current device. + [Pure] + public bool IsTexture3DSupportedForType() + where T : unmanaged + { + ThrowIfDisposed(); + + return this.d3D12Device.Get()->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE3D); + } + /// internal void RentShaderResourceViewDescriptorHandles( out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, diff --git a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Enumerator.cs b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Enumerator.cs index 0c6b73cb..daefc4ec 100644 --- a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Enumerator.cs +++ b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Enumerator.cs @@ -126,7 +126,7 @@ namespace ComputeSharp.Graphics.Helpers HRESULT createDeviceResult = FX.D3D12CreateDevice( dxgiAdapter1.AsIUnknown().Get(), - D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_12_0, + D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0, FX.__uuidof(), null); @@ -137,7 +137,7 @@ namespace ComputeSharp.Graphics.Helpers FX.D3D12CreateDevice( dxgiAdapter1.AsIUnknown().Get(), - D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_12_0, + D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0, FX.__uuidof(), d3D12Device.GetVoidAddressOf()).Assert(); diff --git a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs index 86c880b7..4f4e08fe 100644 --- a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs @@ -95,7 +95,7 @@ namespace ComputeSharp.Graphics.Helpers HRESULT createDeviceResult = FX.D3D12CreateDevice( dxgiAdapter1.AsIUnknown().Get(), - D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_12_0, + D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0, FX.__uuidof(), (void**)d3D12Device); diff --git a/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs b/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs index d5e62752..78c5b1ea 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs @@ -1,7 +1,10 @@ using System; using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; +using System.IO; +using System.Reflection; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using ComputeSharp.Core.Extensions; using ComputeSharp.Exceptions; using TerraFX.Interop; @@ -34,6 +37,8 @@ namespace ComputeSharp.Shaders.Translation /// static ShaderCompiler() { + InitializeDxcLibrariesLoading(); + using ComPtr dxcCompiler = default; using ComPtr dxcLibrary = default; using ComPtr dxcIncludeHandler = default; @@ -51,6 +56,64 @@ namespace ComputeSharp.Shaders.Translation DxcIncludeHandler = dxcIncludeHandler.Move(); } + /// + /// Initializes the DLL resolvers for the dxcompiler.dll and dxil.dll libraries. + /// + private static void InitializeDxcLibrariesLoading() + { + // Test whether the native libraries are present in the same folder of the executable + // (which is the case when the program was built with a runtime identifier), or whether + // they are in the "runtimes\win-x64\native" folder in the executable directory. + string nugetNativeLibsPath = Path.Combine(AppContext.BaseDirectory, @"runtimes\win-x64\native"); + bool isNuGetRuntimeLibrariesDirectoryPresent = Directory.Exists(nugetNativeLibsPath); + + // Register a custom library resolver for the two DXC libraries. We need to either manually load the two + // libraries from the NuGet directory, if an RID is not in use, or we need to ensure that dxil.dll is + // loaded correctly in case the program was executed with the host being in another directory. + // This happens when doing eg. "dotnet bin\Debug\net5.0\MyApp.dll", which would crash at runtime. + FX.ResolveLibrary += (n, a, s) => ResolveLibrary(n, a, s, isNuGetRuntimeLibrariesDirectoryPresent); + } + + /// + /// A custom resolver to override the default library resolution behavior for the DXC and DXIL libraries. + /// This either loads the libraries from the NuGet directory, or just ensures that DXIL is always loaded. + /// + /// + private static IntPtr ResolveLibrary(string libraryName, Assembly assembly, DllImportSearchPath? searchPath, bool isNuGetRuntimeLibrariesDirectoryPresent) + { + if (libraryName is not "dxcompiler") return IntPtr.Zero; + + if (isNuGetRuntimeLibrariesDirectoryPresent) + { + string + dxcompilerPath = Path.Combine(AppContext.BaseDirectory, @"runtimes\win-x64\native\dxcompiler.dll"), + dxilPath = Path.Combine(AppContext.BaseDirectory, @"runtimes\win-x64\native\dxil.dll"); + + // Load DXIL first so that DXC doesn't fail to load it, and then DXIL, both from the NuGet path + if (NativeLibrary.TryLoad(dxilPath, out _) && NativeLibrary.TryLoad(dxcompilerPath, out IntPtr handle)) + { + return handle; + } + } + else + { + // Even when the two libraries are correctly copied next to the executable in use, we load them + // manually to ensure the operation is successful. This is to avoid failures in cases such as when + // doing "dotnet bin\MyApp.dll", ie. when the host is in another path than the executable in use. + // This is probably because DXIL is a native dependency for DXC, but the way Windows loads these + // libraries doesn't take into account the .NET concepts of "app directory": neither the current "bin" + // directory nor the "process directory", which is "C:\Program Files\dotnet", actually contain the + // native library we need, hence the runtime crash. Manually loading the library this way solves this. + if (NativeLibrary.TryLoad("dxil", assembly, searchPath, out _) && + NativeLibrary.TryLoad("dxcompiler", assembly, searchPath, out IntPtr handle)) + { + return handle; + } + } + + return IntPtr.Zero; + } + /// /// Compiles a new HLSL shader from the input source code. /// @@ -76,7 +139,7 @@ namespace ComputeSharp.Shaders.Translation // Try to compile the new compute shader fixed (char* shaderName = "") fixed (char* entryPoint = nameof(IComputeShader.Execute)) - fixed (char* shaderProfile = "cs_6_1") + fixed (char* shaderProfile = "cs_6_0") fixed (char* optimization = "-O3") { DxcCompiler.Get()->Compile( diff --git a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj index 467bb2d1..d2b96445 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj +++ b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj @@ -10,13 +10,42 @@ false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - diff --git a/src/ComputeSharp/ComputeSharp.csproj b/src/ComputeSharp/ComputeSharp.csproj index 3954a5b5..9d310d55 100644 --- a/src/ComputeSharp/ComputeSharp.csproj +++ b/src/ComputeSharp/ComputeSharp.csproj @@ -3,11 +3,7 @@ net5.0 9.0 - true - - - - lib\$(TargetFramework) + true diff --git a/tests/ComputeSharp.Tests.Internals.SourceGenerators/ComputeSharp.Tests.Internals.SourceGenerators.csproj b/tests/ComputeSharp.Tests.Internals.SourceGenerators/ComputeSharp.Tests.Internals.SourceGenerators.csproj new file mode 100644 index 00000000..b0e8280b --- /dev/null +++ b/tests/ComputeSharp.Tests.Internals.SourceGenerators/ComputeSharp.Tests.Internals.SourceGenerators.csproj @@ -0,0 +1,19 @@ + + + + net5.0 + false + + + + + + + + + + + + + + diff --git a/tests/ComputeSharp.Tests.Internals/SourceGeneratorTests.cs b/tests/ComputeSharp.Tests.Internals.SourceGenerators/SourceGeneratorTests.cs similarity index 100% rename from tests/ComputeSharp.Tests.Internals/SourceGeneratorTests.cs rename to tests/ComputeSharp.Tests.Internals.SourceGenerators/SourceGeneratorTests.cs diff --git a/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj b/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj index 3515164a..5fd0b827 100644 --- a/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj +++ b/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj @@ -9,11 +9,9 @@ - - diff --git a/tests/ComputeSharp.Tests.NativeLibrariesResolver/ComputeSharp.Tests.NativeLibrariesResolver.csproj b/tests/ComputeSharp.Tests.NativeLibrariesResolver/ComputeSharp.Tests.NativeLibrariesResolver.csproj new file mode 100644 index 00000000..36d9d5ca --- /dev/null +++ b/tests/ComputeSharp.Tests.NativeLibrariesResolver/ComputeSharp.Tests.NativeLibrariesResolver.csproj @@ -0,0 +1,16 @@ + + + + net5.0 + 9.0 + false + + + + + + + + + + diff --git a/tests/ComputeSharp.Tests.NativeLibrariesResolver/NativeLibrariesResolverTests.cs b/tests/ComputeSharp.Tests.NativeLibrariesResolver/NativeLibrariesResolverTests.cs new file mode 100644 index 00000000..6f1add7c --- /dev/null +++ b/tests/ComputeSharp.Tests.NativeLibrariesResolver/NativeLibrariesResolverTests.cs @@ -0,0 +1,196 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.ComponentModel; +using System.Diagnostics; +using System.IO; + +namespace ComputeSharp.Tests.NativeLibrariesResolver +{ + [TestClass] + public class NativeLibrariesResolverTests + { + static NativeLibrariesResolverTests() + { + string path = Path.GetDirectoryName(typeof(NativeLibrariesResolverTests).Assembly.Location); + + while (Path.GetFileName(path) is not "ComputeSharp") + { + path = Path.GetDirectoryName(path); + } + + SampleProjectDirectory = Path.Combine(path, "samples", "ComputeSharp.Sample.NuGet"); + + string + sampleProjectPath = Path.Combine(SampleProjectDirectory, "ComputeSharp.Sample.NuGet.csproj"), + packagingProjectPath = Path.Combine(path, "src", "ComputeSharp.Package", "ComputeSharp.Package.msbuildproj"); + + // Run dotnet pack and on the packaging project, to ensure the local NuGet package is + // available. Then run dotnet restore on the sample project. This is done so that + // other tests can skip the restore step to run faster, as there's lots of them. + Process.Start("dotnet", $"pack {packagingProjectPath} -c Release").WaitForExit(); + Process.Start("dotnet", $"restore {sampleProjectPath}").WaitForExit(); + } + + /// + /// Gets the directory of the ComputeSharp.Sample.NuGet project. + /// + private static string SampleProjectDirectory { get; } + + [TestMethod] + [DataRow(Configuration.Debug, RID.None)] + [DataRow(Configuration.Debug, RID.Win_x64)] + [DataRow(Configuration.Release, RID.None)] + [DataRow(Configuration.Release, RID.Win_x64)] + public void DotnetRunWorks(Configuration configuration, RID rid) + { + // "dotnet run" fails with "--no-restore", so in this case we restore packages as well + Assert.AreEqual(0, Exec(SampleProjectDirectory, "dotnet", $"run -c {configuration} {ToOption(rid)}")); + } + + [TestMethod] + [DataRow(Configuration.Debug, RID.None)] + [DataRow(Configuration.Debug, RID.Win_x64)] + [DataRow(Configuration.Release, RID.None)] + [DataRow(Configuration.Release, RID.Win_x64)] + public void DotnetBuildWithRunningDotnetHostFromProjectDirectoryWorks(Configuration configuration, RID rid) + { + BuildSampleProject(configuration, rid); + + string realtivePathToDll = Path.Combine("bin", $"{configuration}", "net5.0", $"{ToDirectory(rid)}", "ComputeSharp.Sample.NuGet.dll"); + + Assert.AreEqual(0, Exec(SampleProjectDirectory, "dotnet", realtivePathToDll)); + } + + [TestMethod] + [DataRow(Configuration.Debug, RID.None)] + [DataRow(Configuration.Debug, RID.Win_x64)] + [DataRow(Configuration.Release, RID.None)] + [DataRow(Configuration.Release, RID.Win_x64)] + public void DotnetBuildWithRunningDotnetHostDirectlyWorks(Configuration configuration, RID rid) + { + BuildSampleProject(configuration, rid); + + string pathToDllDirectory = Path.Combine(SampleProjectDirectory, "bin", $"{configuration}", "net5.0", $"{ToDirectory(rid)}"); + + Assert.AreEqual(0, Exec(pathToDllDirectory, "dotnet", "ComputeSharp.Sample.NuGet.dll")); + } + + [TestMethod] + [DataRow(Configuration.Debug, RID.None)] + [DataRow(Configuration.Debug, RID.Win_x64)] + [DataRow(Configuration.Release, RID.None)] + [DataRow(Configuration.Release, RID.Win_x64)] + public void DotnetBuildWithRunningAppHostFromProjectDirectoryWorks(Configuration configuration, RID rid) + { + BuildSampleProject(configuration, rid); + + string relativePathToAppHost = Path.Combine("bin", $"{configuration}", "net5.0", $"{ToDirectory(rid)}", "ComputeSharp.Sample.NuGet.exe"); + + Assert.AreEqual(0, Exec(SampleProjectDirectory, relativePathToAppHost, "")); + } + + [TestMethod] + [DataRow(Configuration.Debug, RID.None)] + [DataRow(Configuration.Debug, RID.Win_x64)] + [DataRow(Configuration.Release, RID.None)] + [DataRow(Configuration.Release, RID.Win_x64)] + public void DotnetBuildWithRunningAppHostDirectlyWorks(Configuration configuration, RID rid) + { + BuildSampleProject(configuration, rid); + + string pathToAppHostDirectory = Path.Combine(SampleProjectDirectory, "bin", $"{configuration}", "net5.0", $"{ToDirectory(rid)}"); + + Assert.AreEqual(0, Exec(pathToAppHostDirectory, "ComputeSharp.Sample.NuGet.exe", "")); + } + + [TestMethod] + [DataRow(Configuration.Debug)] + [DataRow(Configuration.Release)] + public void DotnetPublishWorks(Configuration configuration) + { + // Publishing without specifying an RID is not supported + Exec(SampleProjectDirectory, "dotnet", $"publish -c {configuration} -r win-x64 --no-restore"); + + string pathToAppHost = Path.Combine("bin", $"{configuration}", "net5.0", "win-x64", "publish", "ComputeSharp.Sample.NuGet.exe"); + + Assert.AreEqual(0, Exec(SampleProjectDirectory, pathToAppHost, "")); + } + + /// + /// Builds the sample project with a specific configuration and target runtime. + /// + /// The configuration to use to build the project. + /// The RID to use to build the project. + private static void BuildSampleProject(Configuration configuration, RID rid) + { + Exec(SampleProjectDirectory, "dotnet", $"build -c {configuration} {ToOption(rid)} --no-restore"); + } + + /// + /// Executes a specified process from the command line and returns the exit code. + /// + /// The working directory to execute the process. + /// The target file path for the process to execute. + /// The arguments to invoke the process. + /// The exit code for the executed process. + private static int Exec(string workingDirectory, string filePath, string arguments) + { + ProcessStartInfo startInfo = new() + { + UseShellExecute = true, + CreateNoWindow = true, + WindowStyle = ProcessWindowStyle.Hidden, + FileName = filePath, + Arguments = arguments, + WorkingDirectory = workingDirectory + }; + + Process process = Process.Start(startInfo); + + process.WaitForExit(); + + return process.ExitCode; + } + + /// + /// Gets a text representation of a cmd option for the dotnet tool, for the given . + /// + /// The input value. + /// A text representation for . + private static string ToOption(RID rid) => rid switch + { + RID.None => "", + RID.Win_x64 => "-r win-x64", + _ => throw new InvalidEnumArgumentException(nameof(rid), (int)rid, typeof(RID)) + }; + + /// + /// Gets a text representation of the build folder for the given . + /// + /// The input value. + /// A text representation for . + private static string ToDirectory(RID rid) => rid switch + { + RID.None => "", + RID.Win_x64 => "win-x64", + _ => throw new InvalidEnumArgumentException(nameof(rid), (int)rid, typeof(RID)) + }; + + /// + /// A build configuration. + /// + public enum Configuration + { + Debug, + Release + } + + /// + /// A runtime identifier. + /// + public enum RID + { + None, + Win_x64, + } + } +}