-
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #721 from Sergio0694/dev/d2d-requires-double-preci…
…sion-support Add [D2DRequiresDoublePrecisionSupport] attribute
- Loading branch information
Showing
17 changed files
with
445 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...ourceGenerators/Diagnostics/Analyzers/InvalidD2DRequiresDoublePrecisionSupportAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
/// <summary> | ||
/// A diagnostic analyzer that generates diagnostics when [D2DRequiresDoublePrecisionSupport] is used incorrectly. | ||
/// </summary> | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class InvalidD2DRequiresDoublePrecisionSupportAnalyzer : DiagnosticAnalyzer | ||
{ | ||
/// <inheritdoc/> | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(InvalidD2DRequiresDoublePrecisionSupportAttribute); | ||
|
||
/// <inheritdoc/> | ||
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); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
/// <inheritdoc/> | ||
partial class D3DCompiler | ||
{ | ||
/// <summary> | ||
/// Checks whether double precision support is required. | ||
/// </summary> | ||
/// <param name="d3DBlob">The input HLSL bytecode to inspect.</param> | ||
/// <returns>Whether double precision support is required for <paramref name="d3DBlob"/>.</returns> | ||
public static unsafe bool IsDoublePrecisionSupportRequired(ID3DBlob* d3DBlob) | ||
{ | ||
using ComPtr<ID3D11ShaderReflection> 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/ComputeSharp.D2D1/Attributes/D2DRequiresDoublePrecisionSupportAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
|
||
namespace ComputeSharp.D2D1; | ||
|
||
/// <summary> | ||
/// An attribute for a D2D1 shader indicating that the shader requires support for double precision operations. | ||
/// </summary> | ||
/// <remarks> | ||
/// <para> | ||
/// 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. | ||
/// </para> | ||
/// <para> | ||
/// Validation can only be performed when the shader is being precompiled. If that is not the case (ie. if the shader | ||
/// is using <see cref="D2DEnableRuntimeCompilationAttribute"/> and not <see cref="D2DShaderProfileAttribute"/>), then | ||
/// no build time check for double precision operations can be done. Using this attribute in that scenarios is not valid. | ||
/// </para> | ||
/// </remarks> | ||
[AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)] | ||
public sealed class D2DRequiresDoublePrecisionSupportAttribute : Attribute; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.