Implement D2D1 attribute parsing logic

このコミットが含まれているのは:
Sergio Pedri committed 2022-04-24 21:03:46 +02:00
1 親 e64f32b528
コミット 69ffa16e8c
4ファイル変更
+122 -22

検索条件に一致するファイルはありません

@@ -23,16 +23,26 @@ partial class ID2D1ShaderGenerator
/// Gets a <see cref="BlockSyntax"/> instance with the logic to try to get a compiled shader bytecode.
/// </summary>
/// <param name="hlslSource">The generated HLSL source code (ignoring captured delegates, if present).</param>
/// <param name="shaderProfile">The shader profile to use to compile the shader, if requested.</param>
/// <param name="token">The <see cref="CancellationToken"/> used to cancel the operation, if needed.</param>
/// <param name="diagnostic">The resulting diagnostic from the processing operation, if any.</param>
/// <returns>The <see cref="ImmutableArray{T}"/> instance with the compiled shader bytecode.</returns>
public static unsafe ImmutableArray<byte> GetBytecode(
string hlslSource,
D2D1ShaderProfile? shaderProfile,
CancellationToken token,
out DiagnosticInfo? diagnostic)
{
ImmutableArray<byte> bytecode = ImmutableArray<byte>.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;
}
}
@@ -34,7 +34,7 @@ partial class ID2D1ShaderGenerator
/// <param name="structDeclarationSymbol">The <see cref="INamedTypeSymbol"/> for <paramref name="structDeclaration"/>.</param>
/// <param name="diagnostics">The resulting diagnostics from the processing operation.</param>
/// <returns>The resulting info on the processed shader.</returns>
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<int> inputSimpleIndices,
out ImmutableArray<int> 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);
}
/// <summary>
@@ -350,6 +365,58 @@ partial class ID2D1ShaderGenerator
}
}
/// <summary>
/// Extracts the metadata definition for the current shader.
/// </summary>
/// <param name="structDeclarationSymbol">The input <see cref="INamedTypeSymbol"/> instance to process.</param>
/// <param name="inputCount">The number of shader inputs to declare.</param>
/// <param name="inputSimpleIndices">The indicess of the simple shader inputs.</param>
/// <param name="inputComplexIndices">The indicess of the complex shader inputs.</param>
/// <param name="requiresScenePosition">Whether the shader requires the scene position.</param>
/// <param name="shaderProfile">The shader profile to use to precompile the shader, if requested.</param>
private static void GatherD2D1AttributeInfo(
INamedTypeSymbol structDeclarationSymbol,
out int inputCount,
out ImmutableArray<int> inputSimpleIndices,
out ImmutableArray<int> inputComplexIndices,
out bool requiresScenePosition,
out D2D1ShaderProfile? shaderProfile)
{
inputCount = 0;
requiresScenePosition = false;
shaderProfile = null;
ImmutableArray<int>.Builder inputSimpleIndicesBuilder = ImmutableArray.CreateBuilder<int>();
ImmutableArray<int>.Builder inputComplexIndicesBuilder = ImmutableArray.CreateBuilder<int>();
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();
}
/// <summary>
/// Produces the series of statements to build the current HLSL source.
/// </summary>
@@ -359,6 +426,10 @@ partial class ID2D1ShaderGenerator
/// <param name="staticFields">The sequence of static fields referenced by the shader.</param>
/// <param name="processedMethods">The sequence of processed methods used by the shader.</param>
/// <param name="executeMethod">The body of the entry point of the shader.</param>
/// <param name="inputCount">The number of shader inputs to declare.</param>
/// <param name="inputSimpleIndices">The indicess of the simple shader inputs.</param>
/// <param name="inputComplexIndices">The indicess of the complex shader inputs.</param>
/// <param name="requiresScenePosition">Whether the shader requires the scene position.</param>
/// <returns>The series of statements to build the HLSL source to compile to execute the current shader.</returns>
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<int> inputSimpleIndices,
ImmutableArray<int> 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
+9 -12
ファイルの表示
@@ -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<DispatchDataInfo> Dispatch, Result<HlslShaderSourceInfo> SourceInfo, Result<ThreadIdsInfo> ThreadIds)> shaderInfoWithErrors =
IncrementalValuesProvider<(Result<DispatchDataInfo> Dispatch, Result<HlslShaderSourceInfo> 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>(dispatchDataInfo, dispatchDataDiagnostics),
new Result<HlslShaderSourceInfo>(sourceInfo, ImmutableArray<Diagnostic>.Empty),
new Result<ThreadIdsInfo>(null!, ImmutableArray<Diagnostic>.Empty));
new Result<HlslShaderSourceInfo>(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> dispatchDataInfo =
@@ -128,7 +125,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
hlslSourceInfo
.Select(static (item, token) =>
{
ImmutableArray<byte> bytecode = LoadBytecode.GetBytecode(item.HlslSource, token, out DiagnosticInfo? diagnostic);
ImmutableArray<byte> bytecode = LoadBytecode.GetBytecode(item.HlslSource, item.ShaderProfile, token, out DiagnosticInfo? diagnostic);
token.ThrowIfCancellationRequested();
+5 -2
ファイルの表示
@@ -8,7 +8,8 @@ namespace ComputeSharp.D2D1Interop.SourceGenerators.Models;
/// </summary>
/// <param name="Hierarchy">The hierarchy data for the shader.</param>
/// <param name="HlslSource">The HLSL source.</param>
internal sealed record HlslShaderSourceInfo(HierarchyInfo Hierarchy, string HlslSource)
/// <param name="ShaderProfile">The shader profile to use to compile the shader, if requested.</param>
internal sealed record HlslShaderSourceInfo(HierarchyInfo Hierarchy, string HlslSource, D2D1ShaderProfile? ShaderProfile)
{
/// <summary>
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="HlslShaderSourceInfo"/>.
@@ -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;
}
/// <inheritdoc/>
@@ -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();
}