diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs index 113327c2..f0ea920a 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs @@ -1,4 +1,3 @@ -using System; using ComputeSharp.SourceGeneration.Extensions; using Microsoft.CodeAnalysis; @@ -76,83 +75,12 @@ partial class D2DPixelShaderDescriptorGenerator /// Gets the effective compile options to use. /// /// The requested compile options. - /// Whether the input shader supports linking. /// The effective compile options. - public static D2D1CompileOptions GetEffectiveCompileOptions(D2D1CompileOptions? compileOptions, bool isLinkingSupported) + public static D2D1CompileOptions GetEffectiveCompileOptions(D2D1CompileOptions? compileOptions) { // If an explicit set of compile options is provided, use that directly. Otherwise, use the default // options plus the option to enable linking only if the shader can potentially support it. - return compileOptions ?? (D2D1CompileOptions.Default | (isLinkingSupported ? D2D1CompileOptions.EnableLinking : 0)); - } - - /// - /// Extracts the metadata definition for the current shader. - /// - /// The input object currently in use. - /// The input instance to process. - /// The number of inputs for the shader. - /// Whether the shader only has simple inputs. - public static bool IsSimpleInputShader( - Compilation compilation, - INamedTypeSymbol structDeclarationSymbol, - int inputCount) - { - return IsSimpleInputShader( - structDeclarationSymbol, - compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DInputSimpleAttribute")!, - inputCount); - } - - /// - /// Extracts the metadata definition for the current shader. - /// - /// The input instance to process. - /// The symbol for the [D2DInputSimple] attribute. - /// The number of inputs for the shader. - /// Whether the shader only has simple inputs. - public static bool IsSimpleInputShader( - INamedTypeSymbol structDeclarationSymbol, - INamedTypeSymbol d2DInputSimpleSymbolAttributeSymbol, - int inputCount) - { - // We cannot trust the input count to be valid at this point (it may be invalid and - // with diagnostic already emitted for it). So first, just clamp it in the right range. - inputCount = Math.Max(0, Math.Min(8, inputCount)); - - // If there are no inputs, the shader is as if only had simple inputs - if (inputCount == 0) - { - return true; - } - - // Build a map of all simple inputs (unmarked inputs default to being complex). - // We can never have more than 8 inputs, and if there are it means the shader is - // not valid. Just ignore them, and the generator will emit a separate diagnostic. - Span simpleInputsMap = stackalloc bool[8]; - - // We first start with all inputs marked as complex (ie. not simple) - simpleInputsMap.Clear(); - - foreach (AttributeData attributeData in structDeclarationSymbol.GetAttributes()) - { - // Only retrieve indices of simple inputs that are in range. If an input is out of - // range, the diagnostic for it will already be emitted by a previous generator step. - if (SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, d2DInputSimpleSymbolAttributeSymbol) && - attributeData.ConstructorArguments is [{ Value: >= 0 and < 8 and int index }]) - { - simpleInputsMap[index] = true; - } - } - - bool isSimpleInputShader = true; - - // Validate all inputs in our range (filtered by the allowed one) - foreach (bool isSimpleInput in simpleInputsMap[..inputCount]) - { - isSimpleInputShader &= isSimpleInput; - } - - return isSimpleInputShader; + return compileOptions ?? D2D1CompileOptions.Default; } } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs index 62251b17..e3b47480 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs @@ -128,11 +128,10 @@ public sealed partial class D2DPixelShaderDescriptorGenerator : IIncrementalGene token.ThrowIfCancellationRequested(); // Get the shader profile and linking info for the HLSL bytecode properties - bool isLinkingSupported = HlslBytecode.IsSimpleInputShader(context.SemanticModel.Compilation, typeSymbol, inputCount); D2D1ShaderProfile? requestedShaderProfile = HlslBytecode.GetRequestedShaderProfile(typeSymbol); D2D1CompileOptions? requestedCompileOptions = HlslBytecode.GetRequestedCompileOptions(typeSymbol); D2D1ShaderProfile effectiveShaderProfile = HlslBytecode.GetEffectiveShaderProfile(requestedShaderProfile, out bool isCompilationEnabled); - D2D1CompileOptions effectiveCompileOptions = HlslBytecode.GetEffectiveCompileOptions(requestedCompileOptions, isLinkingSupported); + D2D1CompileOptions effectiveCompileOptions = HlslBytecode.GetEffectiveCompileOptions(requestedCompileOptions); token.ThrowIfCancellationRequested(); diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer.cs deleted file mode 100644 index 99e1c13c..00000000 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer.cs +++ /dev/null @@ -1,67 +0,0 @@ -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 an error whenever [D2DCompileOptions] is used on a shader type to request linking when not supported. -/// -[DiagnosticAnalyzer(LanguageNames.CSharp)] -public sealed class InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer : DiagnosticAnalyzer -{ - /// - public override ImmutableArray SupportedDiagnostics { get; } = [InvalidD2D1CompileOptionsEnableLinkingOnShaderType]; - - /// - public override void Initialize(AnalysisContext context) - { - context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); - context.EnableConcurrentExecution(); - - context.RegisterCompilationStartAction(static context => - { - // Get the [D2DCompileOptions], [D2DInputCount] and [D2DInputSimple] symbols - if (context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DCompileOptionsAttribute") is not { } d2DCompileOptionsAttributeSymbol || - context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DInputCountAttribute") is not { } d2DInputCountAttributeSymbol || - context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DInputSimpleAttribute") is not { } d2DInputSimpleAttributeSymbol) - { - return; - } - - context.RegisterSymbolAction(context => - { - // Only struct types are possible targets - if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Struct } typeSymbol) - { - return; - } - - // If the type is not using [D2DCompileOptions] with D2D1CompileOptions.EnableLinking, there's nothing to do - if (!typeSymbol.TryGetAttributeWithType(d2DCompileOptionsAttributeSymbol, out AttributeData? compileOptionsAttribute) || - !((D2D1CompileOptions)compileOptionsAttribute.ConstructorArguments[0].Value!).HasFlag(D2D1CompileOptions.EnableLinking)) - { - return; - } - - // Make sure we have the [D2DInputCount] (if not present, the shader is invalid anyway) and with a valid value - if (!typeSymbol.TryGetAttributeWithType(d2DInputCountAttributeSymbol, out AttributeData? inputCountAttribute) || - inputCountAttribute.ConstructorArguments is not [{ Value: >= 0 and < 8 and int inputCount }]) - { - return; - } - - // Emit a diagnostic if the compile options are not valid for the shader type - if (!D2DPixelShaderDescriptorGenerator.HlslBytecode.IsSimpleInputShader(typeSymbol, d2DInputSimpleAttributeSymbol, inputCount)) - { - context.ReportDiagnostic(Diagnostic.Create( - InvalidD2D1CompileOptionsEnableLinkingOnShaderType, - compileOptionsAttribute.GetLocation(), - typeSymbol)); - } - }, SymbolKind.NamedType); - }); - } -} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2D1PixelOptionsTrivialSamplingOnShaderTypeAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2D1PixelOptionsTrivialSamplingOnShaderTypeAnalyzer.cs new file mode 100644 index 00000000..855370d4 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2D1PixelOptionsTrivialSamplingOnShaderTypeAnalyzer.cs @@ -0,0 +1,120 @@ +using System; +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 an error whenever [D2DPixelOptions] is used on a shader type to incorrectly request trivial sampling. +/// +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class InvalidD2D1PixelOptionsTrivialSamplingOnShaderTypeAnalyzer : DiagnosticAnalyzer +{ + /// + public override ImmutableArray SupportedDiagnostics { get; } = [InvalidD2D1PixelOptionsTrivialSamplingOnShaderType]; + + /// + public override void Initialize(AnalysisContext context) + { + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); + context.EnableConcurrentExecution(); + + context.RegisterCompilationStartAction(static context => + { + // Get the [D2DPixelOptions], [D2DInputCount] and [D2DInputSimple] symbols + if (context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DPixelOptionsAttribute") is not { } d2DPixelOptionsAttributeSymbol || + context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DInputCountAttribute") is not { } d2DInputCountAttributeSymbol || + context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DInputSimpleAttribute") is not { } d2DInputSimpleAttributeSymbol) + { + return; + } + + context.RegisterSymbolAction(context => + { + // Only struct types are possible targets + if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Struct } typeSymbol) + { + return; + } + + // If the type is not using [D2DPixelOptions] with D2D1PixelOptions.TrivialSampling, there's nothing to do + if (!typeSymbol.TryGetAttributeWithType(d2DPixelOptionsAttributeSymbol, out AttributeData? pixelOptionsAttribute) || + !((D2D1PixelOptions)pixelOptionsAttribute.ConstructorArguments[0].Value!).HasFlag(D2D1PixelOptions.TrivialSampling)) + { + return; + } + + // Make sure we have the [D2DInputCount] (if not present, the shader is invalid anyway) and with a valid value + if (!typeSymbol.TryGetAttributeWithType(d2DInputCountAttributeSymbol, out AttributeData? inputCountAttribute) || + inputCountAttribute.ConstructorArguments is not [{ Value: >= 0 and < 8 and int inputCount }]) + { + return; + } + + // Emit a diagnostic if the compile options are not valid for the shader type + if (!IsSimpleInputShader(typeSymbol, d2DInputSimpleAttributeSymbol, inputCount)) + { + context.ReportDiagnostic(Diagnostic.Create( + InvalidD2D1PixelOptionsTrivialSamplingOnShaderType, + pixelOptionsAttribute.GetLocation(), + typeSymbol)); + } + }, SymbolKind.NamedType); + }); + } + + /// + /// Checks whether a given shader only has simple inputs. + /// + /// The input instance to process. + /// The symbol for the [D2DInputSimple] attribute. + /// The number of inputs for the shader. + /// Whether the shader only has simple inputs. + private static bool IsSimpleInputShader( + INamedTypeSymbol structDeclarationSymbol, + INamedTypeSymbol d2DInputSimpleSymbolAttributeSymbol, + int inputCount) + { + // We cannot trust the input count to be valid at this point (it may be invalid and + // with diagnostic already emitted for it). So first, just clamp it in the right range. + inputCount = Math.Max(0, Math.Min(8, inputCount)); + + // If there are no inputs, the shader is as if only had simple inputs + if (inputCount == 0) + { + return true; + } + + // Build a map of all simple inputs (unmarked inputs default to being complex). + // We can never have more than 8 inputs, and if there are it means the shader is + // not valid. Just ignore them, and the generator will emit a separate diagnostic. + Span simpleInputsMap = stackalloc bool[8]; + + // We first start with all inputs marked as complex (ie. not simple) + simpleInputsMap.Clear(); + + foreach (AttributeData attributeData in structDeclarationSymbol.GetAttributes()) + { + // Only retrieve indices of simple inputs that are in range. If an input is out of + // range, the diagnostic for it will already be emitted by a previous generator step. + if (SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, d2DInputSimpleSymbolAttributeSymbol) && + attributeData.ConstructorArguments is [{ Value: >= 0 and < 8 and int index }]) + { + simpleInputsMap[index] = true; + } + } + + bool isSimpleInputShader = true; + + // Validate all inputs in our range (filtered by the allowed one) + foreach (bool isSimpleInput in simpleInputsMap[..inputCount]) + { + isSimpleInputShader &= isSimpleInput; + } + + return isSimpleInputShader; + } +} \ 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 2e615f6c..23d1d418 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -1024,19 +1024,19 @@ partial class DiagnosticDescriptors helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); /// - /// Gets a for an invalid use of [D2DCompileOptions] requesting to enable linking. + /// Gets a for an invalid use of [D2DPixelOptions] indicating trivial sampling. /// - /// Format: "The D2D1 shader of type {0} cannot use D2D1CompileOptions.EnableLinking in its [D2DCompileOptions] attribute, as it doesn't support linking (only D2D1 shaders with no complex inputs can use this option)". + /// Format: "The D2D1 shader of type {0} shouldn't use D2D1PixelOptions.TrivialSampling in its [D2DPixelOptions] attribute, as it has one or more complex inputs (either mark the inputs as simple, or remove the trivial sampling option)". /// /// - public static readonly DiagnosticDescriptor InvalidD2D1CompileOptionsEnableLinkingOnShaderType = new( + public static readonly DiagnosticDescriptor InvalidD2D1PixelOptionsTrivialSamplingOnShaderType = new( id: "CMPSD2D0069", - title: "Invalid [D2DResourceTextureIndex] use", - messageFormat: """The D2D1 shader of type {0} cannot use D2D1CompileOptions.EnableLinking in its [D2DCompileOptions] attribute, as it doesn't support linking (only D2D1 shaders with no complex inputs can use this option)""", + title: "Invalid [D2DPixelOptions] use", + messageFormat: """The D2D1 shader of type {0} shouldn't use D2D1PixelOptions.TrivialSampling in its [D2DPixelOptions] attribute, as it has one or more complex inputs (either mark the inputs as simple, or remove the trivial sampling option)""", category: "ComputeSharp.D2D1.Shaders", defaultSeverity: DiagnosticSeverity.Warning, isEnabledByDefault: true, - description: "A D2D1 shader cannot use D2D1CompileOptions.EnableLinking in its [D2DCompileOptions] attribute if it doesn't support linking (only D2D1 shaders with no complex inputs can use this option).", + description: "A D2D1 shader shouldn't use D2D1PixelOptions.TrivialSampling in its [D2DPixelOptions] attribute if it has one or more complex inputs (because trivial sampling shaders can only sample pixels at the same scene coordinate as the output pixel, a shader using this option should only have simple inputs).", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); /// diff --git a/src/ComputeSharp.D2D1/Attributes/Enums/D2D1CompileOptions.cs b/src/ComputeSharp.D2D1/Attributes/Enums/D2D1CompileOptions.cs index 62d90117..0f3bc559 100644 --- a/src/ComputeSharp.D2D1/Attributes/Enums/D2D1CompileOptions.cs +++ b/src/ComputeSharp.D2D1/Attributes/Enums/D2D1CompileOptions.cs @@ -176,14 +176,28 @@ public enum D2D1CompileOptions EnableLinking = 1 << 31, /// - /// The default options for shaders compiled by ComputeSharp.D2D1. Specifically, this combination - /// of options is the one used by precompiled shaders (ie. when using ), - /// and when using or any of the overloads. + /// The default options for shaders compiled by ComputeSharp.D2D1. Specifically, this combination of options + /// is the one used by precompiled shaders (ie. when using ), and when + /// using or any of the overloads, if not manually set. /// /// - /// This option does not include , which is instead automatically enabled by shaders - /// compiled using any of the APIs mentioned above. When manually compiling shaders from source using the APIs - /// from instance, should be manually used when needed. + /// + /// This combination is also meant to provide the maximum speed possible without potentially introducing visual artifacts or altering + /// the shader outputs in any way. For instance, is not included, because it might affect all computations + /// inside the shader on some GPU devices, making issues hard to investigate. As such, it can be considered equivalent to "OptimizeForSpeed", + /// which is not available on specifically because it would be identical to this. + /// + /// + /// In order to ensure the best performance possible, this combination also includes . This will include an + /// export function for all shaders, to allow D2D to opportunistically link them for better performance whenever possible. Doing so + /// incurs a small binary size cost, as this option will also enable linking for shaders that D2D might never actually link. For most + /// scenarios, this cost is minimal and not noticeable. If size is critical, consider using instead. + /// /// - Default = OptimizationLevel3 | WarningsAreErrors | PackMatrixRowMajor + Default = OptimizationLevel3 | WarningsAreErrors | PackMatrixRowMajor | EnableLinking, + + /// + /// A combination of options to optimize for minimum size. + /// + OptimizeForSize = OptimizationLevel3 | WarningsAreErrors | PackMatrixRowMajor | StripReflectionData } \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator.cs index f772dbcc..26f0f706 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator.cs @@ -166,7 +166,7 @@ public class Test_D2DPixelShaderDescriptorGenerator [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.D2DPixelShaderDescriptorGenerator", )] [global::System.Diagnostics.DebuggerNonUserCode] [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] - static ComputeSharp.D2D1.D2D1CompileOptions global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.CompileOptions => global::ComputeSharp.D2D1.D2D1CompileOptions.Default | global::ComputeSharp.D2D1.D2D1CompileOptions.EnableLinking; + static ComputeSharp.D2D1.D2D1CompileOptions global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.CompileOptions => global::ComputeSharp.D2D1.D2D1CompileOptions.Default; /// [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.D2DPixelShaderDescriptorGenerator", )] @@ -466,7 +466,7 @@ public class Test_D2DPixelShaderDescriptorGenerator [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.D2DPixelShaderDescriptorGenerator", )] [global::System.Diagnostics.DebuggerNonUserCode] [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] - static ComputeSharp.D2D1.D2D1CompileOptions global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.CompileOptions => global::ComputeSharp.D2D1.D2D1CompileOptions.Default | global::ComputeSharp.D2D1.D2D1CompileOptions.EnableLinking; + static ComputeSharp.D2D1.D2D1CompileOptions global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.CompileOptions => global::ComputeSharp.D2D1.D2D1CompileOptions.Default; /// [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.D2DPixelShaderDescriptorGenerator", )] @@ -818,7 +818,7 @@ public class Test_D2DPixelShaderDescriptorGenerator [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.D2DPixelShaderDescriptorGenerator", )] [global::System.Diagnostics.DebuggerNonUserCode] [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] - static ComputeSharp.D2D1.D2D1CompileOptions global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.CompileOptions => global::ComputeSharp.D2D1.D2D1CompileOptions.Default | global::ComputeSharp.D2D1.D2D1CompileOptions.EnableLinking; + static ComputeSharp.D2D1.D2D1CompileOptions global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.CompileOptions => global::ComputeSharp.D2D1.D2D1CompileOptions.Default; /// [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.D2DPixelShaderDescriptorGenerator", )] @@ -1116,7 +1116,7 @@ public class Test_D2DPixelShaderDescriptorGenerator [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.D2DPixelShaderDescriptorGenerator", )] [global::System.Diagnostics.DebuggerNonUserCode] [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] - static ComputeSharp.D2D1.D2D1CompileOptions global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.CompileOptions => global::ComputeSharp.D2D1.D2D1CompileOptions.Default | global::ComputeSharp.D2D1.D2D1CompileOptions.EnableLinking; + static ComputeSharp.D2D1.D2D1CompileOptions global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.CompileOptions => global::ComputeSharp.D2D1.D2D1CompileOptions.Default; /// [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.D2DPixelShaderDescriptorGenerator", )] @@ -1694,7 +1694,7 @@ public class Test_D2DPixelShaderDescriptorGenerator [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.D2DPixelShaderDescriptorGenerator", )] [global::System.Diagnostics.DebuggerNonUserCode] [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] - static ComputeSharp.D2D1.D2D1CompileOptions global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.CompileOptions => global::ComputeSharp.D2D1.D2D1CompileOptions.Default | global::ComputeSharp.D2D1.D2D1CompileOptions.EnableLinking; + static ComputeSharp.D2D1.D2D1CompileOptions global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.CompileOptions => global::ComputeSharp.D2D1.D2D1CompileOptions.Default; /// [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.D2DPixelShaderDescriptorGenerator", )] @@ -1784,7 +1784,7 @@ public class Test_D2DPixelShaderDescriptorGenerator 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); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs index 31ea0031..12cf8f56 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs @@ -1117,4 +1117,69 @@ public class Test_D2DPixelShaderDescriptorGenerator_Analyzers await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); } + + [TestMethod] + public async Task TrivialSamplingWithComplexInput_Warns() + { + const string source = """ + using ComputeSharp.D2D1; + using float4 = ComputeSharp.Float4; + + [D2DInputCount(1)] + [{|CMPSD2D0069:D2DPixelOptions(D2D1PixelOptions.TrivialSampling)|}] + internal readonly partial struct MyType : ID2D1PixelShader + { + public float4 Execute() + { + return 0; + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + public async Task TrivialSamplingWithExplicitComplexInput_Warns() + { + const string source = """ + using ComputeSharp.D2D1; + using float4 = ComputeSharp.Float4; + + [D2DInputCount(1)] + [D2DInputComplex(1)] + [{|CMPSD2D0069:D2DPixelOptions(D2D1PixelOptions.TrivialSampling)|}] + internal readonly partial struct MyType : ID2D1PixelShader + { + public float4 Execute() + { + return 0; + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + public async Task TrivialSamplingWithLeftoverComplexInput_Warns() + { + const string source = """ + using ComputeSharp.D2D1; + using float4 = ComputeSharp.Float4; + + [D2DInputCount(2)] + [D2DInputSimple(1)] + [{|CMPSD2D0069:D2DPixelOptions(D2D1PixelOptions.TrivialSampling)|}] + internal readonly 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_InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer.cs deleted file mode 100644 index 6cd4b336..00000000 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer.cs +++ /dev/null @@ -1,117 +0,0 @@ -using System.Threading.Tasks; -using ComputeSharp.D2D1.SourceGenerators; -using ComputeSharp.Tests.SourceGenerators.Helpers; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace ComputeSharp.D2D1.Tests.SourceGenerators; - -[TestClass] -public class Test_InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer -{ - [TestMethod] - public async Task NoCompileOptionsDoesNotWarn() - { - const string source = """ - using ComputeSharp; - using ComputeSharp.D2D1; - - public partial class Foo - { - [D2DInputCount(0)] - private partial struct MyShader : ID2D1PixelShader - { - public Float4 Execute() => 0; - } - } - """; - - await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); - } - - [TestMethod] - public async Task ValidCompileOptionsDoesNotWarn() - { - const string source = """ - using ComputeSharp; - using ComputeSharp.D2D1; - - public partial class Foo - { - [D2DInputCount(0)] - [D2DCompileOptions(D2D1CompileOptions.PackMatrixRowMajor)] - private partial struct MyShader : ID2D1PixelShader - { - public Float4 Execute() => 0; - } - } - """; - - await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); - } - - [TestMethod] - public async Task EnableLinkingWithNoInputCountDoesNotWarn() - { - const string source = """ - using ComputeSharp; - using ComputeSharp.D2D1; - - public partial class Foo - { - [D2DInputComplex(0)] - [D2DCompileOptions(D2D1CompileOptions.EnableLinking)] - private partial struct MyShader : ID2D1PixelShader - { - public Float4 Execute() => 0; - } - } - """; - - await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); - } - - [TestMethod] - public async Task EnableLinkingWithImplicitComplexInputWarns() - { - const string source = """ - using ComputeSharp; - using ComputeSharp.D2D1; - - public partial class Foo - { - [D2DInputCount(2)] - [D2DInputSimple(1)] - [{|CMPSD2D0069:D2DCompileOptions(D2D1CompileOptions.Default | D2D1CompileOptions.EnableLinking)|}] - private partial struct MyShader : ID2D1PixelShader - { - public Float4 Execute() => 0; - } - } - """; - - await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); - } - - [TestMethod] - public async Task EnableLinkingWithExplicitComplexInputWarns() - { - const string source = """ - using ComputeSharp; - using ComputeSharp.D2D1; - - public partial class Foo - { - [D2DInputCount(2)] - [D2DInputSimple(0)] - [D2DInputComplex(1)] - [{|CMPSD2D0069:D2DCompileOptions(D2D1CompileOptions.Default | D2D1CompileOptions.EnableLinking)|}] - private partial struct MyShader : ID2D1PixelShader - { - public Float4 Execute() => 0; - } - } - """; - - await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); - } -} \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs index dea905b7..fd0c2545 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs @@ -417,13 +417,13 @@ namespace ComputeSharp.D2D1.Tests Assert.IsTrue(manager!.GetType().Name.Contains("HlslBytecodeMemoryManager")); // Matching compile options - bytecode = D2D1PixelShader.LoadBytecode(D2D1CompileOptions.Default | D2D1CompileOptions.EnableLinking); + bytecode = D2D1PixelShader.LoadBytecode(D2D1CompileOptions.Default); Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); Assert.IsTrue(manager!.GetType().Name.Contains("HlslBytecodeMemoryManager")); // Matching shader profile and compile options - bytecode = D2D1PixelShader.LoadBytecode(D2D1ShaderProfile.PixelShader40Level91, D2D1CompileOptions.Default | D2D1CompileOptions.EnableLinking); + bytecode = D2D1PixelShader.LoadBytecode(D2D1ShaderProfile.PixelShader40Level91, D2D1CompileOptions.Default); Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); Assert.IsTrue(manager!.GetType().Name.Contains("HlslBytecodeMemoryManager")); @@ -534,7 +534,7 @@ namespace ComputeSharp.D2D1.Tests _ = D2D1PixelShader.LoadBytecode(out D2D1ShaderProfile shaderProfile, out D2D1CompileOptions compileOptions); Assert.AreEqual(shaderProfile, D2D1ShaderProfile.PixelShader40Level91); - Assert.AreEqual(compileOptions, D2D1CompileOptions.Default | D2D1CompileOptions.EnableLinking); + Assert.AreEqual(compileOptions, D2D1CompileOptions.Default); _ = D2D1PixelShader.LoadBytecode(out shaderProfile, out compileOptions); @@ -598,7 +598,7 @@ namespace ComputeSharp.D2D1.Tests { ReadOnlyMemory hlslBytecode = D2D1PixelShader.LoadBytecode(out _, out D2D1CompileOptions compileOptions); - Assert.AreEqual(D2D1CompileOptions.OptimizationLevel3 | D2D1CompileOptions.PackMatrixRowMajor, compileOptions); + Assert.AreEqual(D2D1CompileOptions.Default & ~D2D1CompileOptions.WarningsAreErrors, compileOptions); Assert.IsTrue(hlslBytecode.Length > 0); } @@ -618,11 +618,11 @@ namespace ComputeSharp.D2D1.Tests [TestMethod] public void LoadBytecode_EnableLinkingIsAppliedCorrectly() { - ReadOnlyMemory hlslBytecode1 = D2D1PixelShader.LoadBytecode(out _, out D2D1CompileOptions compileOptions1); - ReadOnlyMemory hlslBytecode2 = D2D1PixelShader.LoadBytecode(out _, out D2D1CompileOptions compileOptions2); + ReadOnlyMemory hlslBytecode1 = D2D1PixelShader.LoadBytecode(out _, out D2D1CompileOptions compileOptions1); + ReadOnlyMemory hlslBytecode2 = D2D1PixelShader.LoadBytecode(out _, out D2D1CompileOptions compileOptions2); - Assert.AreEqual(D2D1CompileOptions.Default | D2D1CompileOptions.PackMatrixRowMajor, compileOptions1); - Assert.AreEqual(D2D1CompileOptions.Default | D2D1CompileOptions.EnableLinking | D2D1CompileOptions.PackMatrixRowMajor, compileOptions2); + Assert.AreEqual(D2D1CompileOptions.Default & ~D2D1CompileOptions.EnableLinking, compileOptions1); + Assert.AreEqual(D2D1CompileOptions.Default, compileOptions2); Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(hlslBytecode1, out MemoryManager? manager1)); Assert.IsTrue(manager1!.GetType().Name.Contains("HlslBytecodeMemoryManager")); @@ -630,9 +630,9 @@ namespace ComputeSharp.D2D1.Tests Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(hlslBytecode2, out MemoryManager? manager2)); Assert.IsTrue(manager2!.GetType().Name.Contains("HlslBytecodeMemoryManager")); - // Same checks as in D2D1ShaderCompilerTests.CompileInvertEffectWithDefaultOptionsAndLinking + // Very similar checks to those in D2D1ShaderCompilerTests.CompileInvertEffectWithDefaultOptionsAndLinking Assert.IsTrue(hlslBytecode1.Length > 700); - Assert.IsTrue(hlslBytecode2.Length > 1400); + Assert.IsTrue(hlslBytecode2.Length > 1500); Assert.IsTrue(hlslBytecode2.Length > hlslBytecode1.Length); Assert.IsTrue((hlslBytecode2.Length - hlslBytecode1.Length) > 700); } @@ -656,9 +656,9 @@ namespace ComputeSharp.D2D1.Tests [D2DInputCount(1)] [D2DInputSimple(0)] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] - [D2DCompileOptions(D2D1CompileOptions.Default | D2D1CompileOptions.EnableLinking)] + [D2DCompileOptions(D2D1CompileOptions.Default & ~D2D1CompileOptions.EnableLinking)] [D2DGeneratedPixelShaderDescriptor] - public readonly partial struct ReferenceShaderWithEnableLinking : ID2D1PixelShader + public readonly partial struct ReferenceShaderWithDefaultCompileOptionsAndNoLinking : ID2D1PixelShader { public float4 Execute() { @@ -675,8 +675,8 @@ namespace ComputeSharp.D2D1.Tests ReadOnlyMemory hlslBytecode1 = D2D1PixelShader.LoadBytecode(out _, out D2D1CompileOptions compileOptions1); ReadOnlyMemory hlslBytecode2 = D2D1PixelShader.LoadBytecode(out _, out D2D1CompileOptions compileOptions2); - Assert.AreEqual(D2D1CompileOptions.Default | D2D1CompileOptions.PackMatrixRowMajor, compileOptions1); - Assert.AreEqual(D2D1CompileOptions.Default | D2D1CompileOptions.StripReflectionData | D2D1CompileOptions.PackMatrixRowMajor, compileOptions2); + Assert.AreEqual(D2D1CompileOptions.Default, compileOptions1); + Assert.AreEqual(D2D1CompileOptions.Default | D2D1CompileOptions.StripReflectionData, compileOptions2); Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(hlslBytecode1, out MemoryManager? manager1)); Assert.IsTrue(manager1!.GetType().Name.Contains("HlslBytecodeMemoryManager")); diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1ShaderCompilerTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1ShaderCompilerTests.cs index 2337dd38..577ca088 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1ShaderCompilerTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1ShaderCompilerTests.cs @@ -80,13 +80,13 @@ public partial class D2D1ShaderCompilerTests source.AsSpan(), "PSMain".AsSpan(), D2D1ShaderProfile.PixelShader40Level93, - D2D1CompileOptions.Default); + D2D1CompileOptions.Default & ~D2D1CompileOptions.EnableLinking); ReadOnlyMemory bytecodeWithLinking = D2D1ShaderCompiler.Compile( source.AsSpan(), "PSMain".AsSpan(), D2D1ShaderProfile.PixelShader40Level93, - D2D1CompileOptions.Default | D2D1CompileOptions.EnableLinking); + D2D1CompileOptions.Default); Assert.IsTrue(bytecode.Length > 800); Assert.IsTrue(bytecodeWithLinking.Length > 1600); @@ -94,6 +94,39 @@ public partial class D2D1ShaderCompilerTests Assert.IsTrue((bytecodeWithLinking.Length - bytecode.Length) > 800); } + [TestMethod] + public void CompileInvertEffectWithDefaultOptionsAndOptimizeForSize() + { + const string source = """ + #define D2D_INPUT_COUNT 1 + #define D2D_INPUT0_SIMPLE + + #include "d2d1effecthelpers.hlsli" + + D2D_PS_ENTRY(PSMain) + { + float4 color = D2DGetInput(0); + float3 rgb = saturate(1.0 - color.rgb); + return float4(rgb, 1); + } + """; + + ReadOnlyMemory bytecode = D2D1ShaderCompiler.Compile( + source.AsSpan(), + "PSMain".AsSpan(), + D2D1ShaderProfile.PixelShader40Level93, + D2D1CompileOptions.Default); + + ReadOnlyMemory bytecodeWithOptimizeForSize = D2D1ShaderCompiler.Compile( + source.AsSpan(), + "PSMain".AsSpan(), + D2D1ShaderProfile.PixelShader40Level93, + D2D1CompileOptions.OptimizeForSize); + + Assert.IsTrue(bytecode.Length > 1800); + Assert.IsTrue(bytecodeWithOptimizeForSize.Length < 600); + } + [TestMethod] public void CompileInvertEffectWithDefaultOptionsAndStripReflectionData() {