diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.D3DCompile.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.D3DCompile.cs
index c89af0ca..5a5020f7 100644
--- a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.D3DCompile.cs
+++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.D3DCompile.cs
@@ -23,16 +23,26 @@ partial class ID2D1ShaderGenerator
/// Gets a instance with the logic to try to get a compiled shader bytecode.
///
/// The generated HLSL source code (ignoring captured delegates, if present).
+ /// The shader profile to use to compile the shader, if requested.
/// The used to cancel the operation, if needed.
/// The resulting diagnostic from the processing operation, if any.
/// The instance with the compiled shader bytecode.
public static unsafe ImmutableArray GetBytecode(
string hlslSource,
+ D2D1ShaderProfile? shaderProfile,
CancellationToken token,
out DiagnosticInfo? diagnostic)
{
ImmutableArray bytecode = ImmutableArray.Empty;
+ // No embedded shader was requested
+ if (shaderProfile is null)
+ {
+ diagnostic = null;
+
+ goto End;
+ }
+
try
{
token.ThrowIfCancellationRequested();
@@ -59,6 +69,7 @@ partial class ID2D1ShaderGenerator
diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithDxcCompilationException, e.Message);
}
+ End:
return bytecode;
}
}
diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.HlslSource.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.HlslSource.cs
index 7cd13869..534108e9 100644
--- a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.HlslSource.cs
+++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.HlslSource.cs
@@ -34,7 +34,7 @@ partial class ID2D1ShaderGenerator
/// The for .
/// The resulting diagnostics from the processing operation.
/// The resulting info on the processed shader.
- public static string GetHlslSource(
+ public static (string HlslSource, D2D1ShaderProfile? ShaderProfile) GetHlslSource(
Compilation compilation,
StructDeclarationSyntax structDeclaration,
INamedTypeSymbol structDeclarationSymbol,
@@ -58,16 +58,31 @@ partial class ID2D1ShaderGenerator
var definedConstants = GetDefinedConstants(constantDefinitions);
var staticFields = GetStaticFields(builder, semanticModelProvider, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions);
+ // Gather the shader metadata
+ GatherD2D1AttributeInfo(
+ structDeclarationSymbol,
+ out int inputCount,
+ out ImmutableArray inputSimpleIndices,
+ out ImmutableArray inputComplexIndices,
+ out bool requiresScenePosition,
+ out D2D1ShaderProfile? shaderProfile);
+
diagnostics = builder.ToImmutable();
// Get the HLSL source
- return GetHlslSource(
+ string hlslSource = GetHlslSource(
definedConstants,
declaredTypes,
valueFields,
staticFields,
processedMethods,
- entryPoint);
+ entryPoint,
+ inputCount,
+ inputSimpleIndices,
+ inputComplexIndices,
+ requiresScenePosition);
+
+ return (hlslSource, shaderProfile);
}
///
@@ -350,6 +365,58 @@ partial class ID2D1ShaderGenerator
}
}
+ ///
+ /// Extracts the metadata definition for the current shader.
+ ///
+ /// The input instance to process.
+ /// The number of shader inputs to declare.
+ /// The indicess of the simple shader inputs.
+ /// The indicess of the complex shader inputs.
+ /// Whether the shader requires the scene position.
+ /// The shader profile to use to precompile the shader, if requested.
+ private static void GatherD2D1AttributeInfo(
+ INamedTypeSymbol structDeclarationSymbol,
+ out int inputCount,
+ out ImmutableArray inputSimpleIndices,
+ out ImmutableArray inputComplexIndices,
+ out bool requiresScenePosition,
+ out D2D1ShaderProfile? shaderProfile)
+ {
+ inputCount = 0;
+ requiresScenePosition = false;
+ shaderProfile = null;
+
+ ImmutableArray.Builder inputSimpleIndicesBuilder = ImmutableArray.CreateBuilder();
+ ImmutableArray.Builder inputComplexIndicesBuilder = ImmutableArray.CreateBuilder();
+
+ foreach (AttributeData attributeData in structDeclarationSymbol.GetAttributes())
+ {
+ switch (attributeData.AttributeClass?.GetFullMetadataName())
+ {
+ case "ComputeSharp.D2D1Interop.D2DInputCountAttribute":
+ inputCount = (int)attributeData.ConstructorArguments[0].Value!;
+ break;
+ case "ComputeSharp.D2D1Interop.D2DInputSimpleAttribute":
+ inputSimpleIndicesBuilder.Add((int)attributeData.ConstructorArguments[0].Value!);
+ break;
+ case "ComputeSharp.D2D1Interop.D2DInputComplexAttribute":
+ inputComplexIndicesBuilder.Add((int)attributeData.ConstructorArguments[0].Value!);
+ break;
+ case "ComputeSharp.D2D1Interop.D2DRequiresScenePositionAttribute":
+ requiresScenePosition = true;
+ break;
+ case "ComputeSharp.D2D1Interop.D2DEmbeddedBytecodeAttribute":
+ shaderProfile = (D2D1ShaderProfile)attributeData.ConstructorArguments[0].Value!;
+ break;
+ default:
+ break;
+ }
+ }
+
+ inputSimpleIndices = inputSimpleIndicesBuilder.ToImmutable();
+ inputComplexIndices = inputComplexIndicesBuilder.ToImmutable();
+ }
+
///
/// Produces the series of statements to build the current HLSL source.
///
@@ -359,6 +426,10 @@ partial class ID2D1ShaderGenerator
/// The sequence of static fields referenced by the shader.
/// The sequence of processed methods used by the shader.
/// The body of the entry point of the shader.
+ /// The number of shader inputs to declare.
+ /// The indicess of the simple shader inputs.
+ /// The indicess of the complex shader inputs.
+ /// Whether the shader requires the scene position.
/// The series of statements to build the HLSL source to compile to execute the current shader.
private static string GetHlslSource(
ImmutableArray<(string Name, string Value)> definedConstants,
@@ -366,7 +437,11 @@ partial class ID2D1ShaderGenerator
ImmutableArray<(string Name, string HlslType)> valueFields,
ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> staticFields,
ImmutableArray<(string Signature, string Definition)> processedMethods,
- string executeMethod)
+ string executeMethod,
+ int inputCount,
+ ImmutableArray inputSimpleIndices,
+ ImmutableArray inputComplexIndices,
+ bool requiresScenePosition)
{
StringBuilder hlslBuilder = new();
@@ -389,12 +464,26 @@ partial class ID2D1ShaderGenerator
AppendLineAndLF("// See: https://github.com/Sergio0694/ComputeSharp.");
AppendLF();
- // TODO: switch to discovered ones
- AppendLineAndLF("#define D2D_INPUT_COUNT 1");
- AppendLineAndLF("#define D2D_INPUT0_COMPLEX");
- AppendLineAndLF("#define D2D_REQUIRES_SCENE_POSITION");
+ // Shader metadata
+ AppendLineAndLF($"#define D2D_INPUT_COUNT {inputCount}");
+
+ foreach (int simpleInput in inputSimpleIndices)
+ {
+ AppendLineAndLF($"#define D2D_INPUT{simpleInput}_SIMPLE");
+ }
+
+ foreach (int complexInput in inputComplexIndices)
+ {
+ AppendLineAndLF($"#define D2D_INPUT{complexInput}_COMPLEX");
+ }
+
+ if (requiresScenePosition)
+ {
+ AppendLineAndLF("#define D2D_REQUIRES_SCENE_POSITION");
+ }
// Add the "d2d1effecthelpers.hlsli" header
+ AppendLF();
AppendLineAndLF("#include \"d2d1effecthelpers.hlsli\"");
// Define declarations
diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.cs
index 1587f2fe..4879ca3e 100644
--- a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.cs
+++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.cs
@@ -50,7 +50,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
// Get the dispatch data, 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<(Result Dispatch, Result SourceInfo, Result ThreadIds)> shaderInfoWithErrors =
+ IncrementalValuesProvider<(Result Dispatch, Result SourceInfo)> shaderInfoWithErrors =
shaderDeclarations
.Combine(context.CompilationProvider)
.Select(static (item, token) =>
@@ -71,7 +71,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
token.ThrowIfCancellationRequested();
// Get HLSL source for LoadBytecode()
- string hlslSource = LoadBytecode.GetHlslSource(
+ (string hlslSource, D2D1ShaderProfile? shaderProfile) = LoadBytecode.GetHlslSource(
item.Right,
item.Left.Syntax,
item.Left.Symbol,
@@ -81,26 +81,23 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
HlslShaderSourceInfo sourceInfo = new(
item.Left.Hierarchy,
- hlslSource);
-
- // TODO
+ hlslSource,
+ shaderProfile);
return (
new Result(dispatchDataInfo, dispatchDataDiagnostics),
- new Result(sourceInfo, ImmutableArray.Empty),
- new Result(null!, ImmutableArray.Empty));
+ new Result(sourceInfo, hlslSourceDiagnostics));
});
// Output the diagnostics
context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.Dispatch.Errors));
context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.SourceInfo.Errors));
- context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.ThreadIds.Errors));
// Filter all items to enable caching at a coarse level, and remove diagnostics
- IncrementalValuesProvider<(DispatchDataInfo Dispatch, HlslShaderSourceInfo SourceInfo, ThreadIdsInfo ThreadIds)> shaderInfo =
+ IncrementalValuesProvider<(DispatchDataInfo Dispatch, HlslShaderSourceInfo SourceInfo)> shaderInfo =
shaderInfoWithErrors
- .Select(static (item, token) => (item.Dispatch.Value, item.SourceInfo.Value, item.ThreadIds.Value))
- .WithComparers(DispatchDataInfo.Comparer.Default, HlslShaderSourceInfo.Comparer.Default, ThreadIdsInfo.Comparer.Default);
+ .Select(static (item, token) => (item.Dispatch.Value, item.SourceInfo.Value))
+ .WithComparers(DispatchDataInfo.Comparer.Default, HlslShaderSourceInfo.Comparer.Default);
// Get a filtered sequence to enable caching
IncrementalValuesProvider dispatchDataInfo =
@@ -128,7 +125,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
hlslSourceInfo
.Select(static (item, token) =>
{
- ImmutableArray bytecode = LoadBytecode.GetBytecode(item.HlslSource, token, out DiagnosticInfo? diagnostic);
+ ImmutableArray bytecode = LoadBytecode.GetBytecode(item.HlslSource, item.ShaderProfile, token, out DiagnosticInfo? diagnostic);
token.ThrowIfCancellationRequested();
diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/Models/HlslShaderSourceInfo.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/Models/HlslShaderSourceInfo.cs
index aceb8954..0939078a 100644
--- a/src/ComputeSharp.D2D1Interop.SourceGenerators/Models/HlslShaderSourceInfo.cs
+++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/Models/HlslShaderSourceInfo.cs
@@ -8,7 +8,8 @@ namespace ComputeSharp.D2D1Interop.SourceGenerators.Models;
///
/// The hierarchy data for the shader.
/// The HLSL source.
-internal sealed record HlslShaderSourceInfo(HierarchyInfo Hierarchy, string HlslSource)
+/// The shader profile to use to compile the shader, if requested.
+internal sealed record HlslShaderSourceInfo(HierarchyInfo Hierarchy, string HlslSource, D2D1ShaderProfile? ShaderProfile)
{
///
/// An implementation for .
@@ -40,7 +41,8 @@ internal sealed record HlslShaderSourceInfo(HierarchyInfo Hierarchy, string Hlsl
return
HierarchyInfo.Comparer.Default.Equals(x.Hierarchy, y.Hierarchy) &&
- x.HlslSource == y.HlslSource;
+ x.HlslSource == y.HlslSource &&
+ x.ShaderProfile == y.ShaderProfile;
}
///
@@ -50,6 +52,7 @@ internal sealed record HlslShaderSourceInfo(HierarchyInfo Hierarchy, string Hlsl
hashCode.Add(HierarchyInfo.Comparer.Default.GetHashCode(obj.Hierarchy));
hashCode.Add(obj.HlslSource);
+ hashCode.Add(obj.ShaderProfile);
return hashCode.ToHashCode();
}