diff --git a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md
index 89d1c79e..72d09bf1 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md
+++ b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md
@@ -50,3 +50,4 @@ CMPSD2D0040 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github
CMPSD2D0041 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
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)
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj
index ef124a54..e53c3b74 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj
@@ -67,8 +67,10 @@
+
+
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
index 9a54a56e..0e8167b3 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
@@ -618,4 +618,20 @@ internal static class DiagnosticDescriptors
isEnabledByDefault: true,
description: "A D2D1 shader must only have unique indices for all of its [D2DInputDescription] attributes.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
+
+ ///
+ /// Gets a for the PackMatrixColumnMajor option being used.
+ ///
+ /// Format: "The D2D1 shader of type {0} is using the PackMatrixColumnMajor option in its [D2DCompileOptions] attribute".
+ ///
+ ///
+ public static readonly DiagnosticDescriptor InvalidPackMatrixColumnMajorOption = new DiagnosticDescriptor(
+ id: "CMPSD2D0044",
+ title: "Invalid PackMatrixColumnMajor compile option",
+ messageFormat: "The D2D1 shader of type {0} is using the PackMatrixColumnMajor option in its [D2DCompileOptions] attribute",
+ category: "ComputeSharp.D2D1.Shaders",
+ defaultSeverity: DiagnosticSeverity.Error,
+ 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");
}
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs
index 577a87c4..350e30f0 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs
@@ -28,27 +28,25 @@ partial class ID2D1ShaderGenerator
///
/// Gathers all necessary information on a transpiled HLSL source for a given shader type.
///
+ /// The collection of produced instances.
/// The input object currently in use.
/// The node to process.
/// The for .
/// The number of inputs for the shader.
/// The indicess of the simple shader inputs.
/// The indicess of the complex shader inputs.
- /// The resulting diagnostics from the processing operation.
/// The HLSL source for the shader.
public static string GetHlslSource(
+ ImmutableArray.Builder diagnostics,
Compilation compilation,
StructDeclarationSyntax structDeclaration,
INamedTypeSymbol structDeclarationSymbol,
int inputCount,
ImmutableArray inputSimpleIndices,
- ImmutableArray inputComplexIndices,
- out ImmutableArray diagnostics)
+ ImmutableArray inputComplexIndices)
{
- ImmutableArray.Builder builder = ImmutableArray.CreateBuilder();
-
// Properties are not supported
- DetectAndReportInvalidPropertyDeclarations(builder, structDeclarationSymbol);
+ DetectAndReportInvalidPropertyDeclarations(diagnostics, structDeclarationSymbol);
// We need to sets to track all discovered custom types and static methods
HashSet discoveredTypes = new(SymbolEqualityComparer.Default);
@@ -57,19 +55,17 @@ partial class ID2D1ShaderGenerator
// Explore the syntax tree and extract the processed info
var semanticModelProvider = new SemanticModelProvider(compilation);
- var valueFields = GetInstanceFields(builder, structDeclarationSymbol, discoveredTypes);
- var (entryPoint, processedMethods) = GetProcessedMethods(builder, structDeclaration, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, constantDefinitions);
- var staticFields = GetStaticFields(builder, semanticModelProvider, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions);
+ var valueFields = GetInstanceFields(diagnostics, structDeclarationSymbol, discoveredTypes);
+ var (entryPoint, processedMethods) = GetProcessedMethods(diagnostics, structDeclaration, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, constantDefinitions);
+ var staticFields = GetStaticFields(diagnostics, semanticModelProvider, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions);
// Process the discovered types and constants
- var declaredTypes = GetDeclaredTypes(builder, structDeclarationSymbol, discoveredTypes);
+ var declaredTypes = GetDeclaredTypes(diagnostics, structDeclarationSymbol, discoveredTypes);
var definedConstants = GetDefinedConstants(constantDefinitions);
// Check whether the scene position is required
bool requiresScenePosition = GetRequiresScenePositionInfo(structDeclarationSymbol);
- diagnostics = builder.ToImmutable();
-
// Get the HLSL source
return GetHlslSource(
definedConstants,
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs
index 5c45d3a8..06c844fb 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs
@@ -19,22 +19,20 @@ partial class ID2D1ShaderGenerator
///
/// Extracts the input info for the current shader.
///
+ /// The collection of produced instances.
/// The input instance to process.
/// The number of shader inputs to declare.
/// The indicess of the simple shader inputs.
/// The indices of the complex shader inputs.
/// The combined and serialized input types for each available input.
- /// The collection of produced instances.
public static void GetInfo(
+ ImmutableArray.Builder diagnostics,
INamedTypeSymbol structDeclarationSymbol,
out int inputCount,
out ImmutableArray inputSimpleIndices,
out ImmutableArray inputComplexIndices,
- out ImmutableArray combinedInputTypes,
- out ImmutableArray diagnostics)
+ out ImmutableArray combinedInputTypes)
{
- ImmutableArray.Builder builder = ImmutableArray.CreateBuilder();
-
// We need a separate local here so we can keep the original value to apply
// diagnostics to, but without returning invalid values to the caller which
// might cause generator errors (eg. -1 would cause other code to just throw).
@@ -71,17 +69,17 @@ partial class ID2D1ShaderGenerator
// Validate the input count
if (rawInputCount is not (>= 0 and <= 8))
{
- builder.Add(InvalidD2DInputCount, structDeclarationSymbol, structDeclarationSymbol);
+ diagnostics.Add(InvalidD2DInputCount, structDeclarationSymbol, structDeclarationSymbol);
- goto End;
+ return;
}
// All simple indices must be in range
if (inputSimpleIndicesBuilder.Concat(inputComplexIndicesBuilder).Any(i => (uint)i >= rawInputCount))
{
- builder.Add(OutOfRangeInputIndex, structDeclarationSymbol, structDeclarationSymbol);
+ diagnostics.Add(OutOfRangeInputIndex, structDeclarationSymbol, structDeclarationSymbol);
- goto End;
+ return;
}
HashSet inputSimpleIndicesAsSet = new(inputSimpleIndicesBuilder);
@@ -90,25 +88,25 @@ partial class ID2D1ShaderGenerator
// All simple indices must be unique
if (inputSimpleIndicesAsSet.Count != inputSimpleIndicesBuilder.Count)
{
- builder.Add(RepeatedD2DInputSimpleIndices, structDeclarationSymbol, structDeclarationSymbol);
+ diagnostics.Add(RepeatedD2DInputSimpleIndices, structDeclarationSymbol, structDeclarationSymbol);
- goto End;
+ return;
}
// All complex indices must be unique
if (inputComplexIndicesAsSet.Count != inputComplexIndicesBuilder.Count)
{
- builder.Add(RepeatedD2DInputComplexIndices, structDeclarationSymbol, structDeclarationSymbol);
+ diagnostics.Add(RepeatedD2DInputComplexIndices, structDeclarationSymbol, structDeclarationSymbol);
- goto End;
+ return;
}
// Simple and complex indices can't have indices in common
if (inputSimpleIndicesAsSet.Intersect(inputComplexIndicesAsSet).Any())
{
- builder.Add(InvalidSimpleAndComplexIndicesCombination, structDeclarationSymbol, structDeclarationSymbol);
+ diagnostics.Add(InvalidSimpleAndComplexIndicesCombination, structDeclarationSymbol, structDeclarationSymbol);
- goto End;
+ return;
}
// Build the combined input types list now that inputs have been validated
@@ -125,9 +123,6 @@ partial class ID2D1ShaderGenerator
}
combinedInputTypes = combinedBuilder.MoveToImmutable();
-
- End:
- diagnostics = builder.ToImmutable();
}
}
}
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs
index fcb97fa9..fcaa7ce2 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs
@@ -1,5 +1,6 @@
using System;
using System.Globalization;
+using System.Linq;
using ComputeSharp.__Internals;
using ComputeSharp.D2D1.__Internals;
using ComputeSharp.D2D1.SourceGenerators.Models;
@@ -39,7 +40,7 @@ partial class ID2D1ShaderGenerator
// This code produces a method declaration as follows:
//
- // readonly void global::ComputeSharp.D2D1.__Internals.ID2D1Shader.LoadBytecode(ref TLoader loader, global::ComputeSharp.D2D1.D2D1ShaderProfile? shaderProfile)
+ // readonly void global::ComputeSharp.D2D1.__Internals.ID2D1Shader.LoadBytecode(ref TLoader loader, global::ComputeSharp.D2D1.D2D1ShaderProfile? shaderProfile, global::ComputeSharp.D2D1.D2D1CompileOptions? options)
// {
//
// }
@@ -50,7 +51,8 @@ partial class ID2D1ShaderGenerator
.AddTypeParameterListParameters(TypeParameter(Identifier("TLoader")))
.AddParameterListParameters(
Parameter(Identifier("loader")).AddModifiers(Token(SyntaxKind.RefKeyword)).WithType(IdentifierName("TLoader")),
- Parameter(Identifier("shaderProfile")).WithType(NullableType(IdentifierName("global::ComputeSharp.D2D1.D2D1ShaderProfile"))))
+ Parameter(Identifier("shaderProfile")).WithType(NullableType(IdentifierName("global::ComputeSharp.D2D1.D2D1ShaderProfile"))),
+ Parameter(Identifier("options")).WithType(NullableType(IdentifierName("global::ComputeSharp.D2D1.D2D1CompileOptions"))))
.WithBody(block);
}
@@ -62,21 +64,56 @@ partial class ID2D1ShaderGenerator
/// The instance trying to retrieve the precompiled shader.
private static unsafe BlockSyntax GetShaderBytecodeBody(EmbeddedBytecodeInfo bytecodeInfo, out string? bytecodeLiterals)
{
+ // This method needs to handle several different scenarions, which are as follows:
+ // 1) If [EmbeddedBytecode] is used, then the shader is precompiled and stored as a ReadOnlySpan. In this case,
+ // this bytecode would have been generated with some specific D2D1ShaderProfile and D2D1CompileOptions values. So,
+ // a branch is generated to try to retrieve this precompiled bytecode, which can be returned if and only if the
+ // requested shader profile and compile options are either null, or either matches the values that have been used
+ // to generate that bytecode. If this is the case, then no further work is done and that binary data is returned.
+ // 2) If either the requested shader profile or options do not match the values used to create the shader bytecode,
+ // then a call to the internal D2D1ShaderCompiler.LoadDynamicBytecode API is used, to create the shader on the fly.
+ // 3) If [EmbeddedBytecode] is not used, then regardless of the requested profile and options, a new shader is compiled.
+ // 4) If this is the case and either the shader profile or the options are null, then this method will manually set the
+ // value being null to the default one to use. That is, D2D1ShaderProfile.PixelShader50 will be used as the target,
+ // and the options will be set to D2D1CompileOptions.Default (and optionally, D2D1CompileOptions.EnableLinking too).
+
+ // Get a formatted representation of the compile options being used
+ ExpressionSyntax optionsExpression =
+ ParseExpression(
+ bytecodeInfo.CompileOptions
+ .GetValueOrDefault()
+ .ToString(CultureInfo.InvariantCulture)
+ .Split(',')
+ .Select(static name => $"global::ComputeSharp.D2D1.D2D1CompileOptions.{name.Trim()}")
+ .Aggregate("", static (left, right) => left.Length > 0 ? $"{left} | {right}" : right));
+
+ // Add parantheses if needed
+ if (optionsExpression is BinaryExpressionSyntax)
+ {
+ optionsExpression = ParenthesizedExpression(optionsExpression);
+ }
+
// This code produces a method declaration as follows:
//
- // ComputeSharp.D2D1.__Internals.D2D1ShaderCompiler.LoadDynamicBytecode(ref loader, in this, shaderProfile, );
+ // global::ComputeSharp.D2D1.__Internals.D2D1ShaderCompiler.LoadDynamicBytecode(ref loader, in this, shaderProfile ?? , options ?? );
ExpressionStatementSyntax dynamicLoadingStatement =
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
- IdentifierName("ComputeSharp.D2D1.__Internals.D2D1ShaderCompiler"),
+ IdentifierName("global::ComputeSharp.D2D1.__Internals.D2D1ShaderCompiler"),
IdentifierName("LoadDynamicBytecode")))
.AddArgumentListArguments(
Argument(IdentifierName("loader")).WithRefKindKeyword(Token(SyntaxKind.RefKeyword)),
Argument(ThisExpression()).WithRefKindKeyword(Token(SyntaxKind.InKeyword)),
- Argument(IdentifierName("shaderProfile")),
- Argument(LiteralExpression(bytecodeInfo.IsLinkingSupported ? SyntaxKind.TrueLiteralExpression : SyntaxKind.FalseLiteralExpression))));
+ Argument(BinaryExpression(
+ SyntaxKind.CoalesceExpression,
+ IdentifierName("shaderProfile"),
+ IdentifierName($"global::ComputeSharp.D2D1.D2D1ShaderProfile.{D2D1ShaderProfile.PixelShader50}"))),
+ Argument(BinaryExpression(
+ SyntaxKind.CoalesceExpression,
+ IdentifierName("options"),
+ optionsExpression))));
// If there is no precompiled bytecode, just return the dynamic path
if (bytecodeInfo.Bytecode.IsDefaultOrEmpty)
@@ -90,7 +127,7 @@ partial class ID2D1ShaderGenerator
// This code produces a branch as follows:
//
- // if (shaderProfile is null or )
+ // if (shaderProfile is null or && options is null or )
// {
// global::System.ReadOnlySpan bytecode = new byte[] { __EMBEDDED_SHADER_BYTECODE };
// loader.LoadEmbeddedBytecode(bytecode);
@@ -101,16 +138,24 @@ partial class ID2D1ShaderGenerator
// having to create/parse tens of thousands of literal expression nodes for every shader.
IfStatementSyntax embeddedBranch =
IfStatement(
- IsPatternExpression(
- IdentifierName("shaderProfile"),
- BinaryPattern(
- SyntaxKind.OrPattern,
- ConstantPattern(LiteralExpression(SyntaxKind.NullLiteralExpression)),
- ConstantPattern(
- MemberAccessExpression(
- SyntaxKind.SimpleMemberAccessExpression,
- IdentifierName("global::ComputeSharp.D2D1.D2D1ShaderProfile"),
- IdentifierName(bytecodeInfo.ShaderProfile.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)))))),
+ BinaryExpression(
+ SyntaxKind.LogicalAndExpression,
+ IsPatternExpression(
+ IdentifierName("shaderProfile"),
+ BinaryPattern(
+ SyntaxKind.OrPattern,
+ ConstantPattern(LiteralExpression(SyntaxKind.NullLiteralExpression)),
+ ConstantPattern(
+ MemberAccessExpression(
+ SyntaxKind.SimpleMemberAccessExpression,
+ IdentifierName("global::ComputeSharp.D2D1.D2D1ShaderProfile"),
+ IdentifierName(bytecodeInfo.ShaderProfile.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)))))),
+ IsPatternExpression(
+ IdentifierName("options"),
+ BinaryPattern(
+ SyntaxKind.OrPattern,
+ ConstantPattern(LiteralExpression(SyntaxKind.NullLiteralExpression)),
+ ConstantPattern(optionsExpression)))),
Block(
LocalDeclarationStatement(
VariableDeclaration(
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs
index a9454d6b..7f29ba5d 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs
@@ -41,6 +41,33 @@ partial class ID2D1ShaderGenerator
return null;
}
+ ///
+ /// Extracts the compile options for the current shader.
+ ///
+ /// The collection of produced instances.
+ /// The input instance to process.
+ /// The compile options to use to compile the shader, if present.
+ public static D2D1CompileOptions? GetCompileOptions(ImmutableArray.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol)
+ {
+ if (structDeclarationSymbol.TryGetAttributeWithFullMetadataName("ComputeSharp.D2D1.D2DCompileOptionsAttribute", out AttributeData? attributeData))
+ {
+ D2D1CompileOptions options = (D2D1CompileOptions)attributeData!.ConstructorArguments[0].Value!;
+
+ if ((options & D2D1CompileOptions.PackMatrixColumnMajor) != 0)
+ {
+ diagnostics.Add(
+ InvalidPackMatrixColumnMajorOption,
+ structDeclarationSymbol,
+ structDeclarationSymbol);
+ }
+
+ // PackMatrixRowMajor is always automatically enabled
+ return options | D2D1CompileOptions.PackMatrixRowMajor;
+ }
+
+ return null;
+ }
+
///
/// Extracts the metadata definition for the current shader.
///
@@ -70,18 +97,23 @@ partial class ID2D1ShaderGenerator
///
/// The source info for the shader to compile.
/// The used to cancel the operation, if needed.
+ /// The effective compile options used to create the shader bytecode.
/// The resulting diagnostic from the processing operation, if any.
/// The instance with the compiled shader bytecode.
- public static unsafe ImmutableArray GetBytecode(HlslShaderSourceInfo sourceInfo, CancellationToken token, out DiagnosticInfo? diagnostic)
+ public static unsafe ImmutableArray GetBytecode(
+ HlslShaderSourceInfo sourceInfo,
+ CancellationToken token,
+ out D2D1CompileOptions options,
+ out DiagnosticInfo? diagnostic)
{
ImmutableArray bytecode = ImmutableArray.Empty;
// 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 })
{
+ options = default;
diagnostic = null;
goto End;
@@ -91,11 +123,15 @@ partial class ID2D1ShaderGenerator
{
token.ThrowIfCancellationRequested();
- // Compile the shader bytecode
+ // 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.
+ options = sourceInfo.CompileOptions ?? (D2D1CompileOptions.Default | (sourceInfo.IsLinkingSupported ? D2D1CompileOptions.EnableLinking : 0));
+
+ // Compile the shader bytecode using the requested parameters
using ComPtr dxcBlobBytecode = D3DCompiler.Compile(
sourceInfo.HlslSource.AsSpan(),
sourceInfo.ShaderProfile.Value,
- sourceInfo.IsLinkingSupported);
+ options);
token.ThrowIfCancellationRequested();
@@ -109,10 +145,12 @@ partial class ID2D1ShaderGenerator
}
catch (Win32Exception e)
{
+ options = default;
diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithWin32Exception, e.HResult, FixupExceptionMessage(e.Message));
}
catch (FxcCompilationException e)
{
+ options = default;
diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithDxcCompilationException, FixupExceptionMessage(e.Message));
}
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.cs
index a6e21e6f..f68e9c16 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.cs
@@ -23,14 +23,14 @@ partial class ID2D1ShaderGenerator
///
/// Explores a given type hierarchy and generates statements to load fields.
///
+ /// The collection of produced instances.
/// The current shader type being explored.
/// The total number of needed 32 bit constants in the shader root signature.
- /// The resulting diagnostics from the processing operation.
/// The sequence of instances for all captured resources and values.
public static ImmutableArray GetInfo(
+ ImmutableArray.Builder diagnostics,
ITypeSymbol structDeclarationSymbol,
- out int root32BitConstantCount,
- out ImmutableArray diagnostics)
+ out int root32BitConstantCount)
{
// Helper method that uses boxes instead of ref-s (illegal in enumerators)
static IEnumerable GetCapturedFieldInfos(
@@ -78,8 +78,6 @@ partial class ID2D1ShaderGenerator
}
}
- ImmutableArray.Builder builder = ImmutableArray.CreateBuilder();
-
// Setup the resource and byte offsets for tracking
StrongBox rawDataOffsetAsBox = new();
@@ -107,11 +105,9 @@ partial class ID2D1ShaderGenerator
if (rootSignatureDwordSize > 64)
{
- builder.Add(ShaderDispatchDataSizeExceeded, structDeclarationSymbol, structDeclarationSymbol);
+ diagnostics.Add(ShaderDispatchDataSizeExceeded, structDeclarationSymbol, structDeclarationSymbol);
}
- diagnostics = builder.ToImmutable();
-
return fieldInfos;
}
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs
index 41f8e262..9005fb51 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs
@@ -51,16 +51,18 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
// Get the dispatch data, input types, HLSL source and embedded bytecode info. This info is computed
// on the same step as parts are shared in following sub-branches in the incremental generator pipeline.
- IncrementalValuesProvider<(HierarchyInfo Hierarchy, Result Dispatch, Result InputTypes, Result Source)> shaderInfoWithErrors =
+ IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch, InputTypesInfo InputTypes, HlslShaderSourceInfo Source, ImmutableArray Diagnostics)> shaderInfoWithErrors =
shaderDeclarations
.Combine(context.CompilationProvider)
.Select(static (item, token) =>
{
+ ImmutableArray.Builder diagnostics = ImmutableArray.CreateBuilder();
+
// LoadDispatchData() info
ImmutableArray fieldInfos = LoadDispatchData.GetInfo(
+ diagnostics,
item.Left.Symbol,
- out int root32BitConstantCount,
- out ImmutableArray dispatchDataDiagnostics);
+ out int root32BitConstantCount);
token.ThrowIfCancellationRequested();
@@ -68,56 +70,62 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
// Get the input info for GetInputInfo()
GetInputType.GetInfo(
+ diagnostics,
item.Left.Symbol,
out int inputCount,
out ImmutableArray inputSimpleIndices,
out ImmutableArray inputComplexIndices,
- out ImmutableArray inputTypes,
- out ImmutableArray inputTypesDiagnostics);
+ out ImmutableArray inputTypes);
InputTypesInfo inputTypesInfo = new(inputTypes);
// Get HLSL source for BuildHlslSource()
string hlslSource = BuildHlslSource.GetHlslSource(
+ diagnostics,
item.Right,
item.Left.Syntax,
item.Left.Symbol,
inputCount,
inputSimpleIndices,
- inputComplexIndices,
- out ImmutableArray hlslSourceDiagnostics);
+ inputComplexIndices);
token.ThrowIfCancellationRequested();
// Get the shader profile and linking info for LoadBytecode()
D2D1ShaderProfile? shaderProfile = LoadBytecode.GetShaderProfile(item.Left.Symbol);
- bool hasErrors = !dispatchDataDiagnostics.IsDefaultOrEmpty || !hlslSourceDiagnostics.IsDefaultOrEmpty;
- bool isLinkingSupported = !hasErrors && LoadBytecode.IsSimpleInputShader(item.Left.Symbol, inputCount);
+ D2D1CompileOptions? compileOptions = LoadBytecode.GetCompileOptions(diagnostics, item.Left.Symbol);
+ bool isLinkingSupported = LoadBytecode.IsSimpleInputShader(item.Left.Symbol, inputCount);
HlslShaderSourceInfo sourceInfo = new(
hlslSource,
shaderProfile,
+ compileOptions,
isLinkingSupported,
- hasErrors);
+ HasErrors: diagnostics.Count > 0);
token.ThrowIfCancellationRequested();
return (
item.Left.Hierarchy,
- new Result(dispatchDataInfo, dispatchDataDiagnostics),
- new Result(inputTypesInfo, inputTypesDiagnostics),
- new Result(sourceInfo, hlslSourceDiagnostics));
+ dispatchDataInfo,
+ inputTypesInfo,
+ sourceInfo,
+ diagnostics.ToImmutableArray());
});
// Output the diagnostics
- context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.Dispatch.Errors));
- context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.InputTypes.Errors));
- context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.Source.Errors));
+ context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.Diagnostics));
+
+ // Filter all items to enable caching at a coarse level, and remove diagnostics
+ IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch, InputTypesInfo InputTypes, HlslShaderSourceInfo Source)> shaderInfo =
+ shaderInfoWithErrors
+ .Select(static (item, token) => (item.Hierarchy, item.Dispatch, item.InputTypes, item.Source))
+ .WithComparers(HierarchyInfo.Comparer.Default, DispatchDataInfo.Comparer.Default, InputTypesInfo.Comparer.Default, EqualityComparer.Default);
// Filter items to enable caching for the input count methods
IncrementalValuesProvider<(HierarchyInfo Hierarchy, int InputCount)> inputCountInfo =
- shaderInfoWithErrors
- .Select(static (item, token) => (item.Hierarchy, item.InputTypes.Value.InputTypes.Length))
+ shaderInfo
+ .Select(static (item, token) => (item.Hierarchy, item.InputTypes.InputTypes.Length))
.WithComparers(HierarchyInfo.Comparer.Default, EqualityComparer.Default);
// Generate the GetInputCount() methods
@@ -129,12 +137,6 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(GetInputCount)}", compilationUnit.ToFullString());
});
- // Filter all items to enable caching at a coarse level, and remove diagnostics
- IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch, InputTypesInfo InputTypes, HlslShaderSourceInfo Source)> shaderInfo =
- shaderInfoWithErrors
- .Select(static (item, token) => (item.Hierarchy, item.Dispatch.Value, item.InputTypes.Value, item.Source.Value))
- .WithComparers(HierarchyInfo.Comparer.Default, DispatchDataInfo.Comparer.Default, InputTypesInfo.Comparer.Default, EqualityComparer.Default);
-
// Get a filtered sequence to enable caching
IncrementalValuesProvider<(HierarchyInfo Hierarchy, InputTypesInfo InputTypes)> inputTypesInfo =
shaderInfo
@@ -200,11 +202,19 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
shaderBytecodeInfo
.Select(static (item, token) =>
{
- ImmutableArray bytecode = LoadBytecode.GetBytecode(item.Source, token, out DiagnosticInfo? diagnostic);
+ ImmutableArray bytecode = LoadBytecode.GetBytecode(
+ item.Source,
+ token,
+ out D2D1CompileOptions options,
+ out DiagnosticInfo? diagnostic);
token.ThrowIfCancellationRequested();
- EmbeddedBytecodeInfo bytecodeInfo = new(item.Source.HlslSource, item.Source.ShaderProfile, item.Source.IsLinkingSupported, bytecode);
+ EmbeddedBytecodeInfo bytecodeInfo = new(
+ item.Source.HlslSource,
+ item.Source.ShaderProfile,
+ options,
+ bytecode);
return (item.Hierarchy, bytecodeInfo, diagnostic);
});
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/EmbeddedBytecodeInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/EmbeddedBytecodeInfo.cs
index 385b0144..532a839e 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/Models/EmbeddedBytecodeInfo.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/EmbeddedBytecodeInfo.cs
@@ -11,9 +11,9 @@ namespace ComputeSharp.D2D1.SourceGenerators.Models;
///
/// The HLSL source for the shader.
/// The shader profile to use to compile the shader, if requested.
-/// Whether linking is supported for the current shader.
+/// The compile options to use to compile the shader.
/// The compiled shader bytecode, if available.
-internal sealed record EmbeddedBytecodeInfo(string HlslSource, D2D1ShaderProfile? ShaderProfile, bool IsLinkingSupported, ImmutableArray Bytecode)
+internal sealed record EmbeddedBytecodeInfo(string HlslSource, D2D1ShaderProfile? ShaderProfile, D2D1CompileOptions? CompileOptions, ImmutableArray Bytecode)
{
///
/// An implementation for .
@@ -25,7 +25,7 @@ internal sealed record EmbeddedBytecodeInfo(string HlslSource, D2D1ShaderProfile
{
hashCode.Add(obj.HlslSource);
hashCode.Add(obj.ShaderProfile);
- hashCode.Add(obj.IsLinkingSupported);
+ hashCode.Add(obj.CompileOptions);
hashCode.AddBytes(obj.Bytecode.AsSpan());
}
@@ -35,7 +35,7 @@ internal sealed record EmbeddedBytecodeInfo(string HlslSource, D2D1ShaderProfile
return
x.HlslSource == y.HlslSource &&
x.ShaderProfile == y.ShaderProfile &&
- x.IsLinkingSupported == y.IsLinkingSupported &&
+ x.CompileOptions == y.CompileOptions &&
x.Bytecode.SequenceEqual(y.Bytecode);
}
}
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslShaderSourceInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslShaderSourceInfo.cs
index 76f7c60d..efb0eb1a 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslShaderSourceInfo.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslShaderSourceInfo.cs
@@ -5,6 +5,12 @@
///
/// The HLSL source.
/// The shader profile to use to compile the shader, if requested.
+/// The compile options to use to compile the shader.
/// Whether the shader supports linking.
/// Whether any errors have been detected, and therefore the shader compilation should be skipped.
-internal sealed record HlslShaderSourceInfo(string HlslSource, D2D1ShaderProfile? ShaderProfile, bool IsLinkingSupported, bool HasErrors);
+internal sealed record HlslShaderSourceInfo(
+ string HlslSource,
+ D2D1ShaderProfile? ShaderProfile,
+ D2D1CompileOptions? CompileOptions,
+ bool IsLinkingSupported,
+ bool HasErrors);
diff --git a/src/ComputeSharp.D2D1/Attributes/D2DCompileOptionsAttribute.cs b/src/ComputeSharp.D2D1/Attributes/D2DCompileOptionsAttribute.cs
new file mode 100644
index 00000000..385aacee
--- /dev/null
+++ b/src/ComputeSharp.D2D1/Attributes/D2DCompileOptionsAttribute.cs
@@ -0,0 +1,40 @@
+using System;
+
+namespace ComputeSharp.D2D1;
+
+///
+/// An attribute that indicates the compile options that should be used when compiling a given D2D1 shader.
+/// This applies when the shader is precompiled (through ) as well.
+///
+/// This attribute can be used to annotate shader types as follows:
+///
+/// [D2DCompileOptions(D2D1CompileOptions.IeeeStrictness | D2D1CompileOptions.OptimizationLevel3)]
+/// struct MyShader : ID2D1PixelShader
+/// {
+/// }
+///
+///
+///
+///
+/// Note that the is always enabled automatically and does not need to be
+/// specified. This option is mandatory, as the generated code to load the constant buffer from a shader assumes the layout
+/// for matrix types is row major. For the same reason, using is not
+/// allowed, as it would cause the constant buffer retrieved from a shader to be potentially incorrect.
+///
+[AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)]
+public sealed class D2DCompileOptionsAttribute : Attribute
+{
+ ///
+ /// Creates a new instance with the specified parameters.
+ ///
+ /// The compiler options to use to compile the shader.
+ public D2DCompileOptionsAttribute(D2D1CompileOptions options)
+ {
+ Options = options;
+ }
+
+ ///
+ /// Gets the number of threads in each thread group for the X axis
+ ///
+ public D2D1CompileOptions Options { get; }
+}
diff --git a/src/ComputeSharp.D2D1/Attributes/D2DEmbeddedBytecodeAttribute.cs b/src/ComputeSharp.D2D1/Attributes/D2DEmbeddedBytecodeAttribute.cs
index 1325ad6c..d3fab2d6 100644
--- a/src/ComputeSharp.D2D1/Attributes/D2DEmbeddedBytecodeAttribute.cs
+++ b/src/ComputeSharp.D2D1/Attributes/D2DEmbeddedBytecodeAttribute.cs
@@ -8,7 +8,6 @@ namespace ComputeSharp.D2D1;
///
/// This attribute can be used to annotate shader types as follows:
///
-/// // A compute shader that is dispatched on a target buffer
/// [D2DEmbeddedBytecode(D2D1ShaderProfile.PixelShader50)]
/// struct MyShader : ID2D1PixelShader
/// {
diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderCompilerOptions.cs b/src/ComputeSharp.D2D1/Attributes/Enums/D2D1CompileOptions.cs
similarity index 98%
rename from src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderCompilerOptions.cs
rename to src/ComputeSharp.D2D1/Attributes/Enums/D2D1CompileOptions.cs
index a1cacc59..a0209386 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderCompilerOptions.cs
+++ b/src/ComputeSharp.D2D1/Attributes/Enums/D2D1CompileOptions.cs
@@ -1,7 +1,10 @@
using System;
+#if !SOURCE_GENERATOR
+using ComputeSharp.D2D1.Interop;
+#endif
using TerraFX.Interop.DirectX;
-namespace ComputeSharp.D2D1.Interop;
+namespace ComputeSharp.D2D1;
///
/// Flags to control the options that can be used to compile a D2D1 shader.
@@ -10,7 +13,7 @@ namespace ComputeSharp.D2D1.Interop;
/// For more info, see For more info, see .
///
[Flags]
-public enum D2D1ShaderCompilerOptions
+public enum D2D1CompileOptions
{
///
/// Directs the compiler to insert debug file/line/type/symbol information into the output code.
diff --git a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs
index 0c4fe02c..cfc6814f 100644
--- a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs
+++ b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs
@@ -88,9 +88,10 @@ public interface ID2D1Shader
/// The type of bytecode loader being used.
/// The instance to use to load the bytecode.
/// The shader profile to use to get the shader bytecode (if , the precompiled shader will be used).
+ /// The compile options to use to get the shader bytecode (if , the precompiled shader will be used).
/// Thrown if a precompiled bytecode was requested ( is ), but it wasn't availablle.
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This method is not intended to be used directly by user code")]
- void LoadBytecode(ref TLoader loader, D2D1ShaderProfile? shaderProfile)
+ void LoadBytecode(ref TLoader loader, D2D1ShaderProfile? shaderProfile, D2D1CompileOptions? options)
where TLoader : struct, ID2D1BytecodeLoader;
}
diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs
index afcb0287..644f16c7 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs
@@ -30,7 +30,7 @@ public static class D2D1PixelShader
public static ReadOnlyMemory LoadBytecode()
where T : unmanaged, ID2D1PixelShader
{
- return LoadOrCompileBytecode(null);
+ return LoadOrCompileBytecode(null, null);
}
///
@@ -40,13 +40,65 @@ public static class D2D1PixelShader
/// The shader profile to use to get the shader bytecode.
/// A instance with the resulting shader bytecode.
///
+ ///
+ /// If precompiled shader for the profile does not exist, the shader will be compiled with either the custom compile options specified on the shader
+ /// type (through ), or using otherwise.
+ ///
+ ///
+ /// Additionally, in case no custom compile options are specified and the the shader type
+ /// supports linking, will also be automatically added.
+ ///
+ ///
/// If the input shader was precompiled, the returned instance will wrap a pinned memory buffer (from the PE section).
/// If the shader was compiled at runtime, the returned instance will wrap a array with the bytecode.
+ ///
///
public static ReadOnlyMemory LoadBytecode(D2D1ShaderProfile shaderProfile)
where T : unmanaged, ID2D1PixelShader
{
- return LoadOrCompileBytecode(shaderProfile);
+ return LoadOrCompileBytecode(shaderProfile, null);
+ }
+
+ ///
+ /// Loads the bytecode from an input D2D1 pixel shader.
+ ///
+ /// The type of D2D1 pixel shader to load the bytecode for.
+ /// The compile options to use to get the shader bytecode.
+ /// A instance with the resulting shader bytecode.
+ ///
+ ///
+ /// If precompiled shader with the requested options does not exist, the shader will be compiled with the input options. If additional compile
+ /// options have been specified on the shader type (through ), they will be ignored.
+ ///
+ ///
+ /// If the shader needs to be recompiled, the shader profile that will be used is .
+ ///
+ ///
+ /// If the input shader was precompiled, the returned instance will wrap a pinned memory buffer (from the PE section).
+ /// If the shader was compiled at runtime, the returned instance will wrap a array with the bytecode.
+ ///
+ ///
+ public static ReadOnlyMemory LoadBytecode(D2D1CompileOptions options)
+ where T : unmanaged, ID2D1PixelShader
+ {
+ return LoadOrCompileBytecode(null, options);
+ }
+
+ ///
+ /// Loads the bytecode from an input D2D1 pixel shader.
+ ///
+ /// The type of D2D1 pixel shader to load the bytecode for.
+ /// The shader profile to use to get the shader bytecode.
+ /// The compile options to use to get the shader bytecode.
+ /// A instance with the resulting shader bytecode.
+ ///
+ /// If the input shader was precompiled, the returned instance will wrap a pinned memory buffer (from the PE section).
+ /// If the shader was compiled at runtime, the returned instance will wrap a array with the bytecode.
+ ///
+ public static ReadOnlyMemory LoadBytecode(D2D1ShaderProfile shaderProfile, D2D1CompileOptions options)
+ where T : unmanaged, ID2D1PixelShader
+ {
+ return LoadOrCompileBytecode(shaderProfile, options);
}
///
@@ -54,16 +106,17 @@ public static class D2D1PixelShader
///
/// The type of D2D1 pixel shader to load the bytecode for.
/// The shader profile to use to get the shader bytecode.
+ /// The compile options to use to get the shader bytecode.
/// A instance with the resulting shader bytecode.
/// Thrown if the input shader has not been precompiled.
- private static unsafe ReadOnlyMemory LoadOrCompileBytecode(D2D1ShaderProfile? shaderProfile)
+ private static unsafe ReadOnlyMemory LoadOrCompileBytecode(D2D1ShaderProfile? shaderProfile, D2D1CompileOptions? options)
where T : unmanaged, ID2D1PixelShader
{
D2D1ShaderBytecodeLoader bytecodeLoader = default;
Unsafe.SkipInit(out T shader);
- shader.LoadBytecode(ref bytecodeLoader, shaderProfile);
+ shader.LoadBytecode(ref bytecodeLoader, shaderProfile, options);
using ComPtr dynamicBytecode = bytecodeLoader.GetResultingShaderBytecode(out ReadOnlySpan precompiledBytecode);
diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ReflectionServices.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ReflectionServices.cs
index f82b577c..ed93b63e 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ReflectionServices.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ReflectionServices.cs
@@ -29,7 +29,7 @@ public static class D2D1ReflectionServices
D2D1ShaderBytecodeLoader bytecodeLoader = default;
- shader.LoadBytecode(ref bytecodeLoader, D2D1ShaderProfile.PixelShader50);
+ shader.LoadBytecode(ref bytecodeLoader, D2D1ShaderProfile.PixelShader50, D2D1CompileOptions.Default);
using ComPtr dynamicBytecode = bytecodeLoader.GetResultingShaderBytecode(out ReadOnlySpan precompiledBytecode);
diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderCompiler.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderCompiler.cs
index c52b142a..ac459e8b 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderCompiler.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderCompiler.cs
@@ -29,7 +29,7 @@ public static class D2D1ShaderCompiler
ReadOnlySpan hlslSource,
ReadOnlySpan entryPoint,
D2D1ShaderProfile shaderProfile,
- D2D1ShaderCompilerOptions options)
+ D2D1CompileOptions options)
{
// Encode the HLSL source to ASCII
int maxSourceLength = Encoding.ASCII.GetMaxByteCount(hlslSource.Length);
@@ -60,13 +60,13 @@ public static class D2D1ShaderCompiler
ReadOnlySpan hlslSourceAscii,
ReadOnlySpan entryPointAscii,
D2D1ShaderProfile shaderProfile,
- D2D1ShaderCompilerOptions options)
+ D2D1CompileOptions options)
{
// Check linking support
- bool enableLinking = (options & D2D1ShaderCompilerOptions.EnableLinking) == D2D1ShaderCompilerOptions.EnableLinking;
+ bool enableLinking = (options & D2D1CompileOptions.EnableLinking) == D2D1CompileOptions.EnableLinking;
// Remove the linking flag to make the options blittable to flags
- options &= ~D2D1ShaderCompilerOptions.EnableLinking;
+ options &= ~D2D1CompileOptions.EnableLinking;
// Compile the standalone D2D1 full shader
using ComPtr d3DBlobFullShader = D3DCompiler.CompileShader(
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..0802f677 100644
--- a/src/ComputeSharp.D2D1/Shaders/Translation/__Internals/D2D1ShaderCompiler.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Translation/__Internals/D2D1ShaderCompiler.cs
@@ -21,8 +21,8 @@ public static class D2D1ShaderCompiler
/// The instance to use to load the bytecode.
/// The input instance representing the D2D1 shader to run.
/// The shader profile to use to compile the shader.
- /// Whether to enable linking support for the shader.
- public static unsafe void LoadDynamicBytecode(ref TLoader loader, in T shader, D2D1ShaderProfile? shaderProfile, bool enableLinkingSupport)
+ /// The compiler options to use to compile the shader.
+ public static unsafe void LoadDynamicBytecode(ref TLoader loader, in T shader, D2D1ShaderProfile shaderProfile, D2D1CompileOptions options)
where TLoader : struct, ID2D1BytecodeLoader
where T : struct, ID2D1Shader
{
@@ -30,8 +30,8 @@ public static class D2D1ShaderCompiler
using ComPtr d3DBlobBytecode = Shaders.Translation.D3DCompiler.Compile(
hlslSource.AsSpan(),
- shaderProfile ?? D2D1ShaderProfile.PixelShader50,
- enableLinkingSupport);
+ shaderProfile,
+ options);
loader.LoadDynamicBytecode((IntPtr)d3DBlobBytecode.Get());
}
diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1ShaderCompilerTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1ShaderCompilerTests.cs
index 06c208bf..9a7424b9 100644
--- a/tests/ComputeSharp.D2D1.Tests/D2D1ShaderCompilerTests.cs
+++ b/tests/ComputeSharp.D2D1.Tests/D2D1ShaderCompilerTests.cs
@@ -31,7 +31,7 @@ public partial class D2D1ShaderCompilerTests
source,
"PSMain",
D2D1ShaderProfile.PixelShader40Level93,
- D2D1ShaderCompilerOptions.Default);
+ D2D1CompileOptions.Default);
Assert.IsTrue(bytecode.Length > 0);
}
@@ -57,7 +57,7 @@ public partial class D2D1ShaderCompilerTests
source,
"PSMain",
D2D1ShaderProfile.PixelShader40Level93,
- D2D1ShaderCompilerOptions.Default | D2D1ShaderCompilerOptions.EnableLinking);
+ D2D1CompileOptions.Default | D2D1CompileOptions.EnableLinking);
Assert.IsTrue(bytecode.Length > 0);
}
@@ -84,7 +84,7 @@ public partial class D2D1ShaderCompilerTests
source,
"Execute",
D2D1ShaderProfile.PixelShader40Level93,
- D2D1ShaderCompilerOptions.Default);
+ D2D1CompileOptions.Default);
Assert.IsTrue(bytecode.Length > 0);
}
@@ -111,7 +111,7 @@ public partial class D2D1ShaderCompilerTests
source,
"PSMain",
D2D1ShaderProfile.PixelShader40Level93,
- D2D1ShaderCompilerOptions.Default);
+ D2D1CompileOptions.Default);
Assert.IsTrue(bytecode.Length > 0);
}
@@ -158,7 +158,7 @@ public partial class D2D1ShaderCompilerTests
source,
"PSMain",
D2D1ShaderProfile.PixelShader40,
- D2D1ShaderCompilerOptions.Default);
+ D2D1CompileOptions.Default);
Assert.IsTrue(bytecode.Length > 0);
}