diff --git a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md
index 72d09bf1..7bd22acc 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md
+++ b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md
@@ -51,3 +51,4 @@ CMPSD2D0041 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github
CMPSD2D0042 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0043 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0044 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
+CMPSD2D0045 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
index 0e8167b3..ff19b8e0 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
@@ -634,4 +634,20 @@ internal static class DiagnosticDescriptors
isEnabledByDefault: true,
description: "A D2D1 shader generated with ComputeSharp.D2D1 cannot use the PackMatrixColumnMajor option, as that is not compatible with the generated code used to load shader constant buffers.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
+
+ ///
+ /// Gets a for the EnableLinking option being used.
+ ///
+ /// Format: "The D2D1 shader of type {0} is using the EnableLinking option in its [D2DCompileOptions] attribute for a non-linkable shader".
+ ///
+ ///
+ public static readonly DiagnosticDescriptor InvalidEnableLinkingOption = new DiagnosticDescriptor(
+ id: "CMPSD2D0045",
+ title: "Invalid EnableLinking compile option",
+ messageFormat: "The D2D1 shader of type {0} is using the EnableLinking option in its [D2DCompileOptions] attribute for a non-linkable shader",
+ category: "ComputeSharp.D2D1.Shaders",
+ defaultSeverity: DiagnosticSeverity.Error,
+ isEnabledByDefault: true,
+ description: "A D2D1 shader generated with ComputeSharp.D2D1 cannot use the EnableLinking option if the shader is non-linkable (ie. if it has any complex inputs).",
+ helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
}
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs
index 417ce8e9..973418f7 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs
@@ -46,8 +46,12 @@ partial class ID2D1ShaderGenerator
///
/// The collection of produced instances.
/// The input instance to process.
+ /// Whether or not linking is supported for the current shader.
/// The compile options to use to compile the shader, if present.
- public static D2D1CompileOptions? GetCompileOptions(ImmutableArray.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol)
+ public static D2D1CompileOptions? GetCompileOptions(
+ ImmutableArray.Builder diagnostics,
+ INamedTypeSymbol structDeclarationSymbol,
+ bool isLinkingSupported)
{
if (structDeclarationSymbol.TryGetAttributeWithFullMetadataName("ComputeSharp.D2D1.D2DCompileOptionsAttribute", out AttributeData? attributeData))
{
@@ -61,7 +65,16 @@ partial class ID2D1ShaderGenerator
structDeclarationSymbol);
}
- return options;
+ if ((options & D2D1CompileOptions.EnableLinking) != 0 && !isLinkingSupported)
+ {
+ diagnostics.Add(
+ InvalidEnableLinkingOption,
+ structDeclarationSymbol,
+ structDeclarationSymbol);
+ }
+
+ // PackMatrixRowMajor is always automatically enabled
+ return options | D2D1CompileOptions.PackMatrixRowMajor;
}
return null;
@@ -105,8 +118,7 @@ partial class ID2D1ShaderGenerator
// No embedded shader was requested, or there were some errors earlier in the pipeline.
// In this case, skip the compilation, as diagnostic will be emitted for those anyway.
// Compiling would just add overhead and result in more errors, as the HLSL would be invalid.
- if (sourceInfo.HasErrors ||
- sourceInfo.ShaderProfile is null)
+ if (sourceInfo is { HasErrors: true } or { ShaderProfile: null })
{
diagnostic = null;
@@ -121,7 +133,7 @@ partial class ID2D1ShaderGenerator
using ComPtr dxcBlobBytecode = D3DCompiler.Compile(
sourceInfo.HlslSource.AsSpan(),
sourceInfo.ShaderProfile.Value,
- sourceInfo.IsLinkingSupported);
+ sourceInfo.CompileOptions ?? D2D1CompileOptions.Default);
token.ThrowIfCancellationRequested();
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs
index e79cf32c..3599fac4 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs
@@ -92,9 +92,9 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
token.ThrowIfCancellationRequested();
// Get the shader profile and linking info for LoadBytecode()
+ bool isLinkingSupported = LoadBytecode.IsSimpleInputShader(item.Left.Symbol, inputCount);
D2D1ShaderProfile? shaderProfile = LoadBytecode.GetShaderProfile(item.Left.Symbol);
- D2D1CompileOptions? compileOptions = LoadBytecode.GetCompileOptions(diagnostics, item.Left.Symbol);
- bool isLinkingSupported = diagnostics.Count == 0 && LoadBytecode.IsSimpleInputShader(item.Left.Symbol, inputCount);
+ D2D1CompileOptions? compileOptions = LoadBytecode.GetCompileOptions(diagnostics, item.Left.Symbol, isLinkingSupported);
HlslShaderSourceInfo sourceInfo = new(
hlslSource,
diff --git a/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs
index e9d9617d..c8856937 100644
--- a/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs
@@ -32,9 +32,9 @@ internal static unsafe partial class D3DCompiler
///
/// The HLSL source code to compile.
/// The shader profile to use to compile the shader.
- /// Whether to enable linking for the shader.
+ /// The options to use to compile the shader.
/// The bytecode for the compiled shader.
- public static ComPtr Compile(ReadOnlySpan source, D2D1ShaderProfile shaderProfile, bool enableLinking)
+ public static ComPtr Compile(ReadOnlySpan source, D2D1ShaderProfile shaderProfile, D2D1CompileOptions options)
{
int maxLength = Encoding.ASCII.GetMaxByteCount(source.Length);
byte[] buffer = ArrayPool.Shared.Rent(maxLength);
@@ -49,9 +49,9 @@ internal static unsafe partial class D3DCompiler
d2DEntry: ASCII.Execute,
entryPoint: ASCII.Execute,
target: ASCII.GetPixelShaderProfile(shaderProfile),
- flags: D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL3 | D3DCOMPILE.D3DCOMPILE_WARNINGS_ARE_ERRORS | D3DCOMPILE.D3DCOMPILE_PACK_MATRIX_ROW_MAJOR);
+ flags: (uint)(options & ~D2D1CompileOptions.EnableLinking));
- if (!enableLinking)
+ if ((options & D2D1CompileOptions.EnableLinking) == 0)
{
return d3DBlobFullShader.Move();
}
@@ -63,7 +63,7 @@ internal static unsafe partial class D3DCompiler
d2DEntry: ASCII.Execute,
entryPoint: default,
target: ASCII.GetLibraryProfile(shaderProfile),
- flags: D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL3 | D3DCOMPILE.D3DCOMPILE_WARNINGS_ARE_ERRORS | D3DCOMPILE.D3DCOMPILE_PACK_MATRIX_ROW_MAJOR);
+ flags: (uint)(options & ~D2D1CompileOptions.EnableLinking));
// Embed it as private data if requested
using ComPtr d3DBlobLinked = SetD3DPrivateData(d3DBlobFullShader.Get(), d3DBlobFunction.Get());
diff --git a/src/ComputeSharp.D2D1/Shaders/Translation/__Internals/D2D1ShaderCompiler.cs b/src/ComputeSharp.D2D1/Shaders/Translation/__Internals/D2D1ShaderCompiler.cs
index deae7cd7..3d5cfbbd 100644
--- a/src/ComputeSharp.D2D1/Shaders/Translation/__Internals/D2D1ShaderCompiler.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Translation/__Internals/D2D1ShaderCompiler.cs
@@ -31,7 +31,7 @@ public static class D2D1ShaderCompiler
using ComPtr d3DBlobBytecode = Shaders.Translation.D3DCompiler.Compile(
hlslSource.AsSpan(),
shaderProfile ?? D2D1ShaderProfile.PixelShader50,
- enableLinkingSupport);
+ D2D1CompileOptions.Default | (enableLinkingSupport ? D2D1CompileOptions.EnableLinking : 0));
loader.LoadDynamicBytecode((IntPtr)d3DBlobBytecode.Get());
}