From 034eeb3610292e132b51223920eb6cb12eb60d2f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 16 Dec 2023 16:18:40 +0100 Subject: [PATCH 1/6] Add D2DRequiresDoublePrecisionSupportAttribute --- .../Attributes/D2DCompileOptionsAttribute.cs | 4 ++-- ...RequiresDoublePrecisionSupportAttribute.cs | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 src/ComputeSharp.D2D1/Attributes/D2DRequiresDoublePrecisionSupportAttribute.cs diff --git a/src/ComputeSharp.D2D1/Attributes/D2DCompileOptionsAttribute.cs b/src/ComputeSharp.D2D1/Attributes/D2DCompileOptionsAttribute.cs index 9bd4c07f..bf27291f 100644 --- a/src/ComputeSharp.D2D1/Attributes/D2DCompileOptionsAttribute.cs +++ b/src/ComputeSharp.D2D1/Attributes/D2DCompileOptionsAttribute.cs @@ -24,12 +24,12 @@ namespace ComputeSharp.D2D1; /// This attribute can also be added to a whole assembly, and will be used by default if not overridden by a shader type. /// /// -/// The compiler options to use to compile the shader. +/// The compile options to use to compile the shader. [AttributeUsage(AttributeTargets.Struct | AttributeTargets.Assembly | AttributeTargets.Method, AllowMultiple = false)] public sealed class D2DCompileOptionsAttribute(D2D1CompileOptions options) : Attribute { /// - /// Gets the number of threads in each thread group for the X axis + /// Gets the compile options to use to compile the shader. /// public D2D1CompileOptions Options { get; } = options; } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Attributes/D2DRequiresDoublePrecisionSupportAttribute.cs b/src/ComputeSharp.D2D1/Attributes/D2DRequiresDoublePrecisionSupportAttribute.cs new file mode 100644 index 00000000..dde64edd --- /dev/null +++ b/src/ComputeSharp.D2D1/Attributes/D2DRequiresDoublePrecisionSupportAttribute.cs @@ -0,0 +1,23 @@ +using System; + +namespace ComputeSharp.D2D1; + +/// +/// An attribute for a D2D1 shader indicating that the shader requires support for double precision operations. +/// +/// +/// +/// This attribute does not map to any HLSL feature or compile option directly, but it is needed to make using double +/// precision operations opt-in. It is easy to accidentally introduce them in a shader when that is not intented, and +/// doing so can make the shader run slower and not being compatible with some GPUs (eg. many arm64 GPUs lack support +/// for double precision operations). To avoid this, support is disabled by default, and it is necessary to add this +/// attribute over a shader type to explicitly allow using these operations. +/// +/// +/// Validation can only be performed when the shader is being precompiled. If that is not the case (ie. if the shader +/// is using and not ), then +/// no build time check for double precision operations can be done. Using this attribute in that scenarios is not valid. +/// +/// +[AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)] +public sealed class D2DRequiresDoublePrecisionSupportAttribute : Attribute; \ No newline at end of file From f16dcab8fda93013e3840010b67ab448d41ac666 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 16 Dec 2023 17:15:41 +0100 Subject: [PATCH 2/6] Add check for double precision in D2D1 generator --- .../ComputeSharp.D2D1.SourceGenerators.csproj | 1 + ...lShaderDescriptorGenerator.HlslBytecode.cs | 5 +- .../NativeMethods.txt | 4 ++ .../Shaders/Translation/D3DCompiler.cs | 58 ++++++++++++++++++- .../Models/HlslBytecodeInfo.cs | 12 +++- .../ComputeSharp.SourceGenerators.csproj | 1 + .../Dxc/Windows.Win32/ComPtr{T}.cs | 13 +++++ .../NativeMethods.txt | 3 +- 8 files changed, 92 insertions(+), 5 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj index 1644d727..a17cda56 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj +++ b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj @@ -1,6 +1,7 @@ netstandard2.0 + $(DefineConstants);D2D1_GENERATOR diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs index 5f1420fa..51108f34 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs @@ -205,7 +205,8 @@ partial class D2DPixelShaderDescriptorGenerator using ComPtr dxcBlobBytecode = D3DCompiler.Compile( key.HlslSource.AsSpan(), key.ShaderProfile, - key.CompileOptions); + key.CompileOptions, + out bool requiresDoublePrecisionSupport); token.ThrowIfCancellationRequested(); @@ -216,7 +217,7 @@ partial class D2DPixelShaderDescriptorGenerator ImmutableArray bytecode = Unsafe.As>(ref array); - return new HlslBytecodeInfo.Success(bytecode); + return new HlslBytecodeInfo.Success(bytecode, requiresDoublePrecisionSupport); } catch (Win32Exception e) { diff --git a/src/ComputeSharp.D2D1.SourceGenerators/NativeMethods.txt b/src/ComputeSharp.D2D1.SourceGenerators/NativeMethods.txt index a1c61091..eb4271bf 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/NativeMethods.txt +++ b/src/ComputeSharp.D2D1.SourceGenerators/NativeMethods.txt @@ -1,4 +1,5 @@ D3DCompile +D3DReflect D3DSetBlobPart D3DStripShader D2D1_BUFFER_PRECISION @@ -21,6 +22,9 @@ D3DCOMPILE_OPTIMIZATION_LEVEL2 D3DCOMPILE_OPTIMIZATION_LEVEL3 D3DCOMPILE_WARNINGS_ARE_ERRORS D3DCOMPILER_STRIP_FLAGS +D3D_SHADER_REQUIRES_DOUBLES +D3D_SHADER_REQUIRES_11_1_DOUBLE_EXTENSIONS +ID3D11ShaderReflection IUnknown S_OK S_FALSE \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs index f1ab48c0..2876e53a 100644 --- a/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs +++ b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs @@ -9,6 +9,7 @@ using Windows.Win32; using Windows.Win32.Foundation; using Windows.Win32.Graphics.Direct3D; using Windows.Win32.Graphics.Direct3D.Fxc; +using Windows.Win32.Graphics.Direct3D11; using DirectX = Windows.Win32.PInvoke; #else using ComputeSharp.D2D1.Extensions; @@ -34,7 +35,17 @@ internal static unsafe partial class D3DCompiler /// The shader profile to use to compile the shader. /// The options to use to compile the shader. /// The bytecode for the compiled shader. - public static ComPtr Compile(ReadOnlySpan source, D2D1ShaderProfile shaderProfile, D2D1CompileOptions options) + public static ComPtr Compile( + ReadOnlySpan source, + D2D1ShaderProfile shaderProfile, +#if SOURCE_GENERATOR +#pragma warning disable CS1573 + D2D1CompileOptions options, + out bool requiresDoublePrecisionSupport) // Whether the shader requires support for double precision operations +#pragma warning restore CS1573 +#else + D2D1CompileOptions options) +#endif { int maxLength = Encoding.ASCII.GetMaxByteCount(source.Length); byte[] buffer = ArrayPool.Shared.Rent(maxLength); @@ -58,6 +69,10 @@ internal static unsafe partial class D3DCompiler try { +#if SOURCE_GENERATOR + using ComPtr d3DBlobWithAllParts = default; +#endif + // Compile the standalone D2D1 full shader using ComPtr d3DBlobFullShader = Compile( source: buffer.AsSpan(0, writtenBytes), @@ -66,7 +81,28 @@ internal static unsafe partial class D3DCompiler entryPoint: ASCII.Execute, target: ASCII.GetPixelShaderProfile(shaderProfile), flags: (uint)options, +#if !SOURCE_GENERATOR stripReflectionData: stripReflectionData); +#else + stripReflectionData: stripReflectionData, + d3DBlobWithAllParts: d3DBlobWithAllParts.GetAddressOf()); + + // Check whether double precision operations are required + using (ComPtr d3D11ShaderReflection = default) + { + Guid iidOfID3D11ShaderReflection = ID3D11ShaderReflection.IID_Guid; + + DirectX.D3DReflect( + pSrcData: d3DBlobWithAllParts.Get()->GetBufferPointer(), + SrcDataSize: d3DBlobWithAllParts.Get()->GetBufferSize(), + pInterface: &iidOfID3D11ShaderReflection, + ppReflector: (void**)d3D11ShaderReflection.GetAddressOf()).Assert(); + + ulong doublePrecisionFlags = DirectX.D3D_SHADER_REQUIRES_DOUBLES | DirectX.D3D_SHADER_REQUIRES_11_1_DOUBLE_EXTENSIONS; + + requiresDoublePrecisionSupport = (d3D11ShaderReflection.Get()->GetRequiresFlags() & doublePrecisionFlags) != 0; + } +#endif if (!enableLinking) { @@ -81,7 +117,12 @@ internal static unsafe partial class D3DCompiler entryPoint: default, target: ASCII.GetLibraryProfile(shaderProfile), flags: (uint)options, +#if SOURCE_GENERATOR + stripReflectionData: stripReflectionData, + d3DBlobWithAllParts: null); +#else stripReflectionData: stripReflectionData); +#endif // Embed it as private data if requested using ComPtr d3DBlobLinked = SetD3DPrivateData(d3DBlobFullShader.Get(), d3DBlobFunction.Get()); @@ -113,7 +154,14 @@ internal static unsafe partial class D3DCompiler ReadOnlySpan entryPoint, ReadOnlySpan target, uint flags, +#if SOURCE_GENERATOR +#pragma warning disable CS1573 + bool stripReflectionData, + ID3DBlob** d3DBlobWithAllParts) // The resulting HLSL bytecode with all parts (ie. including reflection info). +#pragma warning restore CS1573 +#else bool stripReflectionData) +#endif { using ComPtr d3DBlobBytecode = default; using ComPtr d3DBlobErrors = default; @@ -191,6 +239,14 @@ internal static unsafe partial class D3DCompiler // there would always be a message), but better safe than sorry. hResult.Assert(); +#if SOURCE_GENERATOR + // If requested, also copy the original HLSL bytecode + if (d3DBlobWithAllParts is not null) + { + d3DBlobBytecode.CopyTo(d3DBlobWithAllParts).Assert(); + } +#endif + // If requested, strip the reflection data and return that bytecode blob if (stripReflectionData) { diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Models/HlslBytecodeInfo.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Models/HlslBytecodeInfo.cs index cb8b9bba..96a5601b 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Models/HlslBytecodeInfo.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Models/HlslBytecodeInfo.cs @@ -11,7 +11,17 @@ internal abstract record HlslBytecodeInfo /// A successfully compiled HLSL shader. /// /// The resulting HLSL bytecode. - public sealed record Success(EquatableArray Bytecode) : HlslBytecodeInfo; + public sealed record Success( +#if D3D12_GENERATOR + EquatableArray Bytecode) +#else + +#pragma warning disable CS1573 + EquatableArray Bytecode, + bool RequiresDoublePrecisionSupport) // Whether the shader requires support for double precision operations. +#pragma warning restore CS1573 +#endif + : HlslBytecodeInfo; /// /// An HLSL shader that failed to compile due to a Win32 error. diff --git a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj index 330353f5..c6ecff10 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj +++ b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj @@ -1,6 +1,7 @@ netstandard2.0 + $(DefineConstants);D3D12_GENERATOR diff --git a/src/ComputeSharp.SourceGenerators/Dxc/Windows.Win32/ComPtr{T}.cs b/src/ComputeSharp.SourceGenerators/Dxc/Windows.Win32/ComPtr{T}.cs index e7afbe5e..34ece11a 100644 --- a/src/ComputeSharp.SourceGenerators/Dxc/Windows.Win32/ComPtr{T}.cs +++ b/src/ComputeSharp.SourceGenerators/Dxc/Windows.Win32/ComPtr{T}.cs @@ -5,6 +5,7 @@ using System; using System.Runtime.CompilerServices; +using Windows.Win32.Foundation; using Windows.Win32.System.Com; namespace Windows.Win32; @@ -43,6 +44,18 @@ internal unsafe struct ComPtr : IDisposable } } + /// Increments the reference count for the current COM object, if any, and copies its address to a target raw pointer. + /// The target raw pointer to copy the address of the current COM object to. + /// This method always returns . + public readonly HRESULT CopyTo(T** ptr) + { + _ = ((IUnknown*)this.ptr)->AddRef(); + + *ptr = this.ptr; + + return HRESULT.S_OK; + } + /// Gets the currently wrapped raw pointer to a COM object. /// The raw pointer wrapped by the current instance. [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/ComputeSharp.SourceGenerators/NativeMethods.txt b/src/ComputeSharp.SourceGenerators/NativeMethods.txt index 00f34480..34636715 100644 --- a/src/ComputeSharp.SourceGenerators/NativeMethods.txt +++ b/src/ComputeSharp.SourceGenerators/NativeMethods.txt @@ -4,4 +4,5 @@ DxcCreateInstance IDxcCompiler3 IDxcUtils IDxcResult -IUnknown \ No newline at end of file +IUnknown +S_OK \ No newline at end of file From 0051423caa6637bfc78b6f7e7a4e31d17896ec4f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 16 Dec 2023 17:32:29 +0100 Subject: [PATCH 3/6] Add diagnostics for incorrect double precision annotations --- .../AnalyzerReleases.Shipped.md | 2 + ...lShaderDescriptorGenerator.HlslBytecode.cs | 40 +++++++++++++++++++ .../D2DPixelShaderDescriptorGenerator.cs | 1 + .../Diagnostics/DiagnosticDescriptors.cs | 32 +++++++++++++++ .../Models/DiagnosticInfo.cs | 20 +++++++--- .../D2D1ReflectionServicesTests.cs | 1 + 6 files changed, 90 insertions(+), 6 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md index 45dfa3bb..8db7381d 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md @@ -86,3 +86,5 @@ CMPSD2D0076 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github CMPSD2D0077 | ComputeSharp.D2D1.Shaders | Info | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0078 | ComputeSharp.D2D1.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0079 | ComputeSharp.D2D1.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0080 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0081 | ComputeSharp.D2D1.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs index 51108f34..6e8f431b 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs @@ -268,5 +268,45 @@ partial class D2DPixelShaderDescriptorGenerator diagnostics.Add(diagnostic); } } + + /// + /// Gets the diagnostics for when double precision support is configured incorrectly. + /// + /// The input instance to process. + /// The source instance. + /// The collection of produced instances. + public static void GetDoublePrecisionSupportDiagnostics( + INamedTypeSymbol structDeclarationSymbol, + HlslBytecodeInfo info, + ImmutableArrayBuilder diagnostics) + { + // If we have no compiled HLSL bytecode, there is nothing more to do + if (info is not HlslBytecodeInfo.Success success) + { + return; + } + + bool hasD2DRequiresDoublePrecisionSupportAttribute = structDeclarationSymbol.TryGetAttributeWithFullyQualifiedMetadataName( + "ComputeSharp.D2D1.D2DRequiresDoublePrecisionSupportAttribute", + out AttributeData? attributeData); + + // Check the two cases where diagnostics are necessary: + // - The shader does not have [D2DRequiresDoublePrecisionSupport], but it needs it + // - The shader has [D2DRequiresDoublePrecisionSupport], but it does not need it + if (!hasD2DRequiresDoublePrecisionSupportAttribute && success.RequiresDoublePrecisionSupport) + { + diagnostics.Add(DiagnosticInfo.Create( + MissingD2DRequiresDoublePrecisionSupportAttribute, + structDeclarationSymbol, + structDeclarationSymbol)); + } + else if (hasD2DRequiresDoublePrecisionSupportAttribute && !success.RequiresDoublePrecisionSupport) + { + diagnostics.Add(DiagnosticInfo.Create( + UnnecessaryD2DRequiresDoublePrecisionSupportAttribute, + attributeData!.GetLocation(), + structDeclarationSymbol)); + } + } } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs index 619708f1..d46dc2c0 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs @@ -169,6 +169,7 @@ public sealed partial class D2DPixelShaderDescriptorGenerator : IIncrementalGene // Append any diagnostic for the shader compilation HlslBytecode.GetInfoDiagnostics(typeSymbol, hlslInfo, diagnostics); + HlslBytecode.GetDoublePrecisionSupportDiagnostics(typeSymbol, hlslInfo, diagnostics); token.ThrowIfCancellationRequested(); diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index e76019d2..47ba5d4d 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -1190,4 +1190,36 @@ partial class DiagnosticDescriptors description: "If an assembly is using [D2DShaderProfile] (meaning that all shaders declared within it will be precompiled), also using [D2DEnableRuntimeCompilation] is unnecessary.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp", customTags: WellKnownDiagnosticTags.CompilationEnd); + + /// + /// Gets a for a shader missing [D2DRequiresDoublePrecisionSupport]. + /// + /// Format: "The shader {0} requires double precision support, but it does not have the [D2DRequiresDoublePrecisionSupport] attribute on it (adding the attribute is necessary to explicitly opt-in into that functionality)". + /// + /// + public static readonly DiagnosticDescriptor MissingD2DRequiresDoublePrecisionSupportAttribute = new( + id: "CMPSD2D0080", + title: "Missing [D2DRequiresDoublePrecisionSupport] attribute", + messageFormat: "The shader {0} requires double precision support, but it does not have the [D2DRequiresDoublePrecisionSupport] attribute on it (adding the attribute is necessary to explicitly opt-in into that functionality)", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "Shaders performing double precision operations must be annotated with [D2DRequiresDoublePrecisionSupport] to explicitly opt-in into that functionality.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for a shader is unnecessarily using [UnnecessaryD2DRequiresDoublePrecisionSupportAttribute]. + /// + /// Format: "The shader {0} does not require double precision support, but it has the [D2DRequiresDoublePrecisionSupport] attribute on it (using the attribute is not needed if the shader is not performing any double precision operations)". + /// + /// + public static readonly DiagnosticDescriptor UnnecessaryD2DRequiresDoublePrecisionSupportAttribute = new( + id: "CMPSD2D0081", + title: "Unnecessary [D2DRequiresDoublePrecisionSupport] attribute", + messageFormat: "The shader {0} does not require double precision support, but it has the [D2DRequiresDoublePrecisionSupport] attribute on it (using the attribute is not needed if the shader is not performing any double precision operations)", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Warning, + isEnabledByDefault: true, + description: "Shaders not performing any double precision operations should not be annotated with [D2DRequiresDoublePrecisionSupport], as the attribute is not needed in that case.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration/Models/DiagnosticInfo.cs b/src/ComputeSharp.SourceGeneration/Models/DiagnosticInfo.cs index de3ae0ed..72970bd5 100644 --- a/src/ComputeSharp.SourceGeneration/Models/DiagnosticInfo.cs +++ b/src/ComputeSharp.SourceGeneration/Models/DiagnosticInfo.cs @@ -33,6 +33,18 @@ internal sealed record DiagnosticInfo( return Diagnostic.Create(Descriptor, null, Arguments.ToArray()); } + /// + /// Creates a new instance with the specified parameters. + /// + /// The input for the diagnostics to create. + /// The location to use for the diagnostic. + /// The optional arguments for the formatted message to include. + /// A new instance with the specified parameters. + public static DiagnosticInfo Create(DiagnosticDescriptor descriptor, Location? location, params object[] args) + { + return new(descriptor, location?.SourceTree, location?.SourceSpan ?? default, args.Select(static arg => arg.ToString()).ToImmutableArray()); + } + /// /// Creates a new instance with the specified parameters. /// @@ -42,9 +54,7 @@ internal sealed record DiagnosticInfo( /// A new instance with the specified parameters. public static DiagnosticInfo Create(DiagnosticDescriptor descriptor, ISymbol symbol, params object[] args) { - Location location = symbol.Locations.First(); - - return new(descriptor, location.SourceTree, location.SourceSpan, args.Select(static arg => arg.ToString()).ToImmutableArray()); + return Create(descriptor, symbol.Locations.First(), args); } /// @@ -56,8 +66,6 @@ internal sealed record DiagnosticInfo( /// A new instance with the specified parameters. public static DiagnosticInfo Create(DiagnosticDescriptor descriptor, SyntaxNode node, params object[] args) { - Location location = node.GetLocation(); - - return new(descriptor, location.SourceTree, location.SourceSpan, args.Select(static arg => arg.ToString()).ToImmutableArray()); + return Create(descriptor, node.GetLocation(), args); } } \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs index d9ce6d07..99dc004d 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs @@ -125,6 +125,7 @@ public partial class D2D1ReflectionServicesTests } [D2DInputCount(1)] + [D2DRequiresDoublePrecisionSupport] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] From ed953c6bf17fc8ca8c336d6211235088b2534843 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 16 Dec 2023 17:52:59 +0100 Subject: [PATCH 4/6] Add diagnostic tests for double precision attribute --- ...DPixelShaderSourceGenerator_Diagnostics.cs | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Diagnostics.cs diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Diagnostics.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Diagnostics.cs new file mode 100644 index 00000000..4a6169d8 --- /dev/null +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Diagnostics.cs @@ -0,0 +1,121 @@ +extern alias Core; +extern alias D2D1; + +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; +using Basic.Reference.Assemblies; +using ComputeSharp.D2D1.SourceGenerators; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.D2D1.Tests.SourceGenerators; + +[TestClass] +public class Test_D2DPixelShaderSourceGenerator_DIagnostics +{ + [TestMethod] + public void MissingD2DRequiresDoublePrecisionSupportAttribute() + { + const string source = """ + using ComputeSharp; + using ComputeSharp.D2D1; + using float4 = global::ComputeSharp.Float4; + + namespace MyNamespace; + + [D2DInputCount(0)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + [D2DGeneratedPixelShaderDescriptor] + internal readonly partial struct MyShader : ID2D1PixelShader + { + private readonly float time; + + public float4 Execute() + { + return (float)(time * 2.0); + } + } + """; + + VerifyGeneratedDiagnostics(source, "CMPSD2D0080"); + } + + [TestMethod] + public void UnnecessaryD2DRequiresDoublePrecisionSupportAttribute() + { + const string source = """ + using ComputeSharp; + using ComputeSharp.D2D1; + using float4 = global::ComputeSharp.Float4; + + namespace MyNamespace; + + [D2DInputCount(0)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + [D2DRequiresDoublePrecisionSupport] + [D2DGeneratedPixelShaderDescriptor] + internal readonly partial struct MyShader : ID2D1PixelShader + { + private readonly float time; + + public float4 Execute() + { + return (float)(time * 2.0f); + } + } + """; + + VerifyGeneratedDiagnostics(source, "CMPSD2D0081"); + } + + /// + /// Verifies the output of a source generator. + /// + /// The input source to process. + /// The expected diagnostics ids to be generated. + /// The task for the operation. + private static void VerifyGeneratedDiagnostics(string source, params string[] diagnosticsIds) + { + // Get all assembly references for the .NET TFM and ComputeSharp + IEnumerable metadataReferences = + [ + .. Net80.References.All, + MetadataReference.CreateFromFile(typeof(Core::ComputeSharp.Hlsl).Assembly.Location), + MetadataReference.CreateFromFile(typeof(D2D1::ComputeSharp.D2D1.ID2D1PixelShader).Assembly.Location) + ]; + + // Parse the source text (C# 12) + SyntaxTree sourceTree = CSharpSyntaxTree.ParseText( + source, + CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp12)); + + // Create the original compilation + CSharpCompilation compilation = CSharpCompilation.Create( + "original", + [sourceTree], + metadataReferences, + new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true)); + + // Create the generator driver with the D2D shader generator + GeneratorDriver driver = CSharpGeneratorDriver.Create(new D2DPixelShaderDescriptorGenerator()).WithUpdatedParseOptions((CSharpParseOptions)sourceTree.Options); + + // Run all source generators on the input source code + _ = driver.RunGeneratorsAndUpdateCompilation(compilation, out Compilation outputCompilation, out ImmutableArray diagnostics); + + Dictionary diagnosticMap = diagnostics.DistinctBy(diagnostic => diagnostic.Id).ToDictionary(diagnostic => diagnostic.Id); + + // Check that the diagnostics match + Assert.IsTrue(diagnosticMap.Keys.ToHashSet().SetEquals(diagnosticsIds), $"Diagnostics didn't match. {string.Join(", ", diagnosticMap.Values)}"); + + // If the compilation was supposed to succeed, ensure that no further errors were generated + if (diagnosticsIds.Length == 0) + { + // Compute diagnostics for the final compiled output (just include errors) + List outputCompilationDiagnostics = outputCompilation.GetDiagnostics().Where(diagnostic => diagnostic.Severity == DiagnosticSeverity.Error).ToList(); + + Assert.IsTrue(outputCompilationDiagnostics.Count == 0, $"resultingIds: {string.Join(", ", outputCompilationDiagnostics)}"); + } + } +} \ No newline at end of file From 76752e2e47cce804f1eee553a1054ae41fab79b6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 16 Dec 2023 18:07:22 +0100 Subject: [PATCH 5/6] Add InvalidD2DRequiresDoublePrecisionSupportAnalyzer and tests --- .../AnalyzerReleases.Shipped.md | 1 + ...DRequiresDoublePrecisionSupportAnalyzer.cs | 66 ++++++++++++++++++ .../Diagnostics/DiagnosticDescriptors.cs | 16 +++++ .../Test_Analyzers.cs | 69 +++++++++++++++++++ .../Test_D2DPixelShaderSourceGenerator.cs | 1 + 5 files changed, 153 insertions(+) create mode 100644 src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DRequiresDoublePrecisionSupportAnalyzer.cs diff --git a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md index 8db7381d..4887a339 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md @@ -88,3 +88,4 @@ CMPSD2D0078 | ComputeSharp.D2D1.Shaders | Warning | [Documentation](https://gith CMPSD2D0079 | ComputeSharp.D2D1.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0080 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0081 | ComputeSharp.D2D1.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0082 | ComputeSharp.D2D1.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DRequiresDoublePrecisionSupportAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DRequiresDoublePrecisionSupportAnalyzer.cs new file mode 100644 index 00000000..fdfe86d1 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DRequiresDoublePrecisionSupportAnalyzer.cs @@ -0,0 +1,66 @@ +using System.Collections.Immutable; +using ComputeSharp.SourceGeneration.Extensions; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; +using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; + +namespace ComputeSharp.D2D1.SourceGenerators; + +/// +/// A diagnostic analyzer that generates diagnostics when [D2DRequiresDoublePrecisionSupport] is used incorrectly. +/// +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class InvalidD2DRequiresDoublePrecisionSupportAnalyzer : DiagnosticAnalyzer +{ + /// + public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(InvalidD2DRequiresDoublePrecisionSupportAttribute); + + /// + public override void Initialize(AnalysisContext context) + { + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); + context.EnableConcurrentExecution(); + + context.RegisterCompilationStartAction(static context => + { + // Get the [D2DRequiresDoublePrecisionSupport], [D2DShaderProfile] and [D2DGeneratedPixelShaderDescriptor] symbols + if (context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DRequiresDoublePrecisionSupportAttribute") is not { } d2DRequiresDoublePrecisionSupportAttributeSymbol || + context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DShaderProfileAttribute") is not { } d2DShaderProfileAttributeSymbol || + context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DGeneratedPixelShaderDescriptorAttribute") is not { } d2DGeneratedPixelShaderDescriptorAttributeSymbol) + { + return; + } + + context.RegisterSymbolAction(context => + { + // Only struct types are possible targets + if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Struct } typeSymbol) + { + return; + } + + // Skip the type if it's not a generated shader descriptor target + if (!typeSymbol.HasAttributeWithType(d2DGeneratedPixelShaderDescriptorAttributeSymbol)) + { + return; + } + + // If the shader is precompiled, there is nothing left to do + if (typeSymbol.HasAttributeWithType(d2DShaderProfileAttributeSymbol) || + typeSymbol.ContainingAssembly.HasAttributeWithType(d2DShaderProfileAttributeSymbol)) + { + return; + } + + // Emit a diagnostic if the type is not precompiled and has [D2DRequiresDoublePrecisionSupport] + if (typeSymbol.TryGetAttributeWithType(d2DRequiresDoublePrecisionSupportAttributeSymbol, out AttributeData? attributeData)) + { + context.ReportDiagnostic(Diagnostic.Create( + InvalidD2DRequiresDoublePrecisionSupportAttribute, + attributeData.GetLocation(), + typeSymbol)); + } + }, SymbolKind.NamedType); + }); + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 47ba5d4d..9d9f0d36 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -1222,4 +1222,20 @@ partial class DiagnosticDescriptors isEnabledByDefault: true, description: "Shaders not performing any double precision operations should not be annotated with [D2DRequiresDoublePrecisionSupport], as the attribute is not needed in that case.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for a shader is using [UnnecessaryD2DRequiresDoublePrecisionSupportAttribute] incorrectly. + /// + /// Format: "The shader {0} has the [D2DRequiresDoublePrecisionSupport] attribute on it, but it is not precompiled, so validation for use of double precision operations cannot be performed". + /// + /// + public static readonly DiagnosticDescriptor InvalidD2DRequiresDoublePrecisionSupportAttribute = new( + id: "CMPSD2D0082", + title: "Invalid [D2DRequiresDoublePrecisionSupport] attribute", + messageFormat: "The shader {0} has the [D2DRequiresDoublePrecisionSupport] attribute on it, but it is not precompiled, so validation for use of double precision operations cannot be performed", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Warning, + isEnabledByDefault: true, + description: "Shaders can only be annotated with [D2DRequiresDoublePrecisionSupport] to perform validation for use of double precision operations if they are precompiled.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_Analyzers.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_Analyzers.cs index cc4e3c5f..903e6549 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_Analyzers.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_Analyzers.cs @@ -346,4 +346,73 @@ public class Test_Analyzers await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); } + + [TestMethod] + public async Task ShaderWithPrecompiledBytecode_FromAssembly_WithD2DRequiresDoublePrecisionSupport_DoesNotWarn() + { + const string source = """ + using ComputeSharp; + using ComputeSharp.D2D1; + + [assembly: D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + + [D2DInputCount(0)] + [D2DRequiresDoublePrecisionSupport] + [D2DGeneratedPixelShaderDescriptor] + internal partial struct MyType : ID2D1PixelShader + { + public Float4 Execute() + { + return 0; + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + public async Task ShaderWithPrecompiledBytecode_FromType_WithD2DRequiresDoublePrecisionSupport_DoesNotWarn() + { + const string source = """ + using ComputeSharp; + using ComputeSharp.D2D1; + + [D2DInputCount(0)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + [D2DRequiresDoublePrecisionSupport] + [D2DGeneratedPixelShaderDescriptor] + internal partial struct MyType : ID2D1PixelShader + { + public Float4 Execute() + { + return 0; + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + public async Task ShaderWithNoPrecompiledBytecode_WithD2DRequiresDoublePrecisionSupport_Warns() + { + const string source = """ + using ComputeSharp; + using ComputeSharp.D2D1; + + [D2DInputCount(0)] + [{|CMPSD2D0082:D2DRequiresDoublePrecisionSupport|}] + [D2DGeneratedPixelShaderDescriptor] + internal partial struct MyType : ID2D1PixelShader + { + public Float4 Execute() + { + return 0; + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } } \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs index d9d60411..ef7fdf3c 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs @@ -1817,6 +1817,7 @@ public class Test_D2DPixelShaderSourceGenerator await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); From 5d58622ddf7121e22102a5f6941db31209a1700d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 17 Dec 2023 02:36:17 +0100 Subject: [PATCH 6/6] Move D2D reflection code to D2D generator only --- ...lShaderDescriptorGenerator.HlslBytecode.cs | 8 ++- .../Fxc/D3DCompiler.cs | 33 +++++++++++ .../Shaders/Translation/D3DCompiler.cs | 58 +------------------ .../Dxc/Windows.Win32/ComPtr{T}.cs | 13 ----- .../NativeMethods.txt | 3 +- 5 files changed, 41 insertions(+), 74 deletions(-) create mode 100644 src/ComputeSharp.D2D1.SourceGenerators/Fxc/D3DCompiler.cs diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs index 6e8f431b..bbe3d69c 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs @@ -205,8 +205,12 @@ partial class D2DPixelShaderDescriptorGenerator using ComPtr dxcBlobBytecode = D3DCompiler.Compile( key.HlslSource.AsSpan(), key.ShaderProfile, - key.CompileOptions, - out bool requiresDoublePrecisionSupport); + key.CompileOptions); + + token.ThrowIfCancellationRequested(); + + // Check whether double precision operations are required + bool requiresDoublePrecisionSupport = D3DCompiler.IsDoublePrecisionSupportRequired(dxcBlobBytecode.Get()); token.ThrowIfCancellationRequested(); diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Fxc/D3DCompiler.cs b/src/ComputeSharp.D2D1.SourceGenerators/Fxc/D3DCompiler.cs new file mode 100644 index 00000000..41d105ca --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/Fxc/D3DCompiler.cs @@ -0,0 +1,33 @@ +using System; +using Windows.Win32; +using Windows.Win32.Graphics.Direct3D; +using Windows.Win32.Graphics.Direct3D11; +using DirectX = Windows.Win32.PInvoke; + +namespace ComputeSharp.D2D1.Shaders.Translation; + +/// +partial class D3DCompiler +{ + /// + /// Checks whether double precision support is required. + /// + /// The input HLSL bytecode to inspect. + /// Whether double precision support is required for . + public static unsafe bool IsDoublePrecisionSupportRequired(ID3DBlob* d3DBlob) + { + using ComPtr d3D11ShaderReflection = default; + + Guid iidOfID3D11ShaderReflection = ID3D11ShaderReflection.IID_Guid; + + DirectX.D3DReflect( + pSrcData: d3DBlob->GetBufferPointer(), + SrcDataSize: d3DBlob->GetBufferSize(), + pInterface: &iidOfID3D11ShaderReflection, + ppReflector: (void**)d3D11ShaderReflection.GetAddressOf()).Assert(); + + ulong doublePrecisionFlags = DirectX.D3D_SHADER_REQUIRES_DOUBLES | DirectX.D3D_SHADER_REQUIRES_11_1_DOUBLE_EXTENSIONS; + + return (d3D11ShaderReflection.Get()->GetRequiresFlags() & doublePrecisionFlags) != 0; + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs index 2876e53a..f1ab48c0 100644 --- a/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs +++ b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs @@ -9,7 +9,6 @@ using Windows.Win32; using Windows.Win32.Foundation; using Windows.Win32.Graphics.Direct3D; using Windows.Win32.Graphics.Direct3D.Fxc; -using Windows.Win32.Graphics.Direct3D11; using DirectX = Windows.Win32.PInvoke; #else using ComputeSharp.D2D1.Extensions; @@ -35,17 +34,7 @@ internal static unsafe partial class D3DCompiler /// The shader profile to use to compile the shader. /// The options to use to compile the shader. /// The bytecode for the compiled shader. - public static ComPtr Compile( - ReadOnlySpan source, - D2D1ShaderProfile shaderProfile, -#if SOURCE_GENERATOR -#pragma warning disable CS1573 - D2D1CompileOptions options, - out bool requiresDoublePrecisionSupport) // Whether the shader requires support for double precision operations -#pragma warning restore CS1573 -#else - D2D1CompileOptions options) -#endif + public static ComPtr Compile(ReadOnlySpan source, D2D1ShaderProfile shaderProfile, D2D1CompileOptions options) { int maxLength = Encoding.ASCII.GetMaxByteCount(source.Length); byte[] buffer = ArrayPool.Shared.Rent(maxLength); @@ -69,10 +58,6 @@ internal static unsafe partial class D3DCompiler try { -#if SOURCE_GENERATOR - using ComPtr d3DBlobWithAllParts = default; -#endif - // Compile the standalone D2D1 full shader using ComPtr d3DBlobFullShader = Compile( source: buffer.AsSpan(0, writtenBytes), @@ -81,28 +66,7 @@ internal static unsafe partial class D3DCompiler entryPoint: ASCII.Execute, target: ASCII.GetPixelShaderProfile(shaderProfile), flags: (uint)options, -#if !SOURCE_GENERATOR stripReflectionData: stripReflectionData); -#else - stripReflectionData: stripReflectionData, - d3DBlobWithAllParts: d3DBlobWithAllParts.GetAddressOf()); - - // Check whether double precision operations are required - using (ComPtr d3D11ShaderReflection = default) - { - Guid iidOfID3D11ShaderReflection = ID3D11ShaderReflection.IID_Guid; - - DirectX.D3DReflect( - pSrcData: d3DBlobWithAllParts.Get()->GetBufferPointer(), - SrcDataSize: d3DBlobWithAllParts.Get()->GetBufferSize(), - pInterface: &iidOfID3D11ShaderReflection, - ppReflector: (void**)d3D11ShaderReflection.GetAddressOf()).Assert(); - - ulong doublePrecisionFlags = DirectX.D3D_SHADER_REQUIRES_DOUBLES | DirectX.D3D_SHADER_REQUIRES_11_1_DOUBLE_EXTENSIONS; - - requiresDoublePrecisionSupport = (d3D11ShaderReflection.Get()->GetRequiresFlags() & doublePrecisionFlags) != 0; - } -#endif if (!enableLinking) { @@ -117,12 +81,7 @@ internal static unsafe partial class D3DCompiler entryPoint: default, target: ASCII.GetLibraryProfile(shaderProfile), flags: (uint)options, -#if SOURCE_GENERATOR - stripReflectionData: stripReflectionData, - d3DBlobWithAllParts: null); -#else stripReflectionData: stripReflectionData); -#endif // Embed it as private data if requested using ComPtr d3DBlobLinked = SetD3DPrivateData(d3DBlobFullShader.Get(), d3DBlobFunction.Get()); @@ -154,14 +113,7 @@ internal static unsafe partial class D3DCompiler ReadOnlySpan entryPoint, ReadOnlySpan target, uint flags, -#if SOURCE_GENERATOR -#pragma warning disable CS1573 - bool stripReflectionData, - ID3DBlob** d3DBlobWithAllParts) // The resulting HLSL bytecode with all parts (ie. including reflection info). -#pragma warning restore CS1573 -#else bool stripReflectionData) -#endif { using ComPtr d3DBlobBytecode = default; using ComPtr d3DBlobErrors = default; @@ -239,14 +191,6 @@ internal static unsafe partial class D3DCompiler // there would always be a message), but better safe than sorry. hResult.Assert(); -#if SOURCE_GENERATOR - // If requested, also copy the original HLSL bytecode - if (d3DBlobWithAllParts is not null) - { - d3DBlobBytecode.CopyTo(d3DBlobWithAllParts).Assert(); - } -#endif - // If requested, strip the reflection data and return that bytecode blob if (stripReflectionData) { diff --git a/src/ComputeSharp.SourceGenerators/Dxc/Windows.Win32/ComPtr{T}.cs b/src/ComputeSharp.SourceGenerators/Dxc/Windows.Win32/ComPtr{T}.cs index 34ece11a..e7afbe5e 100644 --- a/src/ComputeSharp.SourceGenerators/Dxc/Windows.Win32/ComPtr{T}.cs +++ b/src/ComputeSharp.SourceGenerators/Dxc/Windows.Win32/ComPtr{T}.cs @@ -5,7 +5,6 @@ using System; using System.Runtime.CompilerServices; -using Windows.Win32.Foundation; using Windows.Win32.System.Com; namespace Windows.Win32; @@ -44,18 +43,6 @@ internal unsafe struct ComPtr : IDisposable } } - /// Increments the reference count for the current COM object, if any, and copies its address to a target raw pointer. - /// The target raw pointer to copy the address of the current COM object to. - /// This method always returns . - public readonly HRESULT CopyTo(T** ptr) - { - _ = ((IUnknown*)this.ptr)->AddRef(); - - *ptr = this.ptr; - - return HRESULT.S_OK; - } - /// Gets the currently wrapped raw pointer to a COM object. /// The raw pointer wrapped by the current instance. [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/ComputeSharp.SourceGenerators/NativeMethods.txt b/src/ComputeSharp.SourceGenerators/NativeMethods.txt index 34636715..00f34480 100644 --- a/src/ComputeSharp.SourceGenerators/NativeMethods.txt +++ b/src/ComputeSharp.SourceGenerators/NativeMethods.txt @@ -4,5 +4,4 @@ DxcCreateInstance IDxcCompiler3 IDxcUtils IDxcResult -IUnknown -S_OK \ No newline at end of file +IUnknown \ No newline at end of file