Implement generation for ID2D1Shader.GetInputType(uint) API
このコミットが含まれているのは:
1 親
12e2e4cf3f
コミット
6455678056
8ファイル変更
+385
-108
検索条件に一致するファイルはありません
+14
-96
@@ -3,7 +3,6 @@ using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using ComputeSharp.D2D1.SourceGenerators.Diagnostics;
|
||||
using ComputeSharp.D2D1.SourceGenerators.SyntaxRewriters;
|
||||
using ComputeSharp.SourceGeneration.Extensions;
|
||||
using ComputeSharp.SourceGeneration.Helpers;
|
||||
@@ -24,7 +23,7 @@ partial class ID2D1ShaderGenerator
|
||||
/// <summary>
|
||||
/// A helper with all logic to generate the <c>BuildHlslSource</c> method.
|
||||
/// </summary>
|
||||
internal static partial class BuildHlslSource
|
||||
private static partial class BuildHlslSource
|
||||
{
|
||||
/// <summary>
|
||||
/// Gathers all necessary information on a transpiled HLSL source for a given shader type.
|
||||
@@ -33,13 +32,17 @@ partial class ID2D1ShaderGenerator
|
||||
/// <param name="structDeclaration">The <see cref="StructDeclarationSyntax"/> node to process.</param>
|
||||
/// <param name="structDeclarationSymbol">The <see cref="INamedTypeSymbol"/> for <paramref name="structDeclaration"/>.</param>
|
||||
/// <param name="inputCount">The number of inputs for the shader.</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="diagnostics">The resulting diagnostics from the processing operation.</param>
|
||||
/// <returns>The HLSL source for the shader.</returns>
|
||||
public static string GetHlslSource(
|
||||
Compilation compilation,
|
||||
StructDeclarationSyntax structDeclaration,
|
||||
INamedTypeSymbol structDeclarationSymbol,
|
||||
out int inputCount,
|
||||
int inputCount,
|
||||
ImmutableArray<int> inputSimpleIndices,
|
||||
ImmutableArray<int> inputComplexIndices,
|
||||
out ImmutableArray<Diagnostic> diagnostics)
|
||||
{
|
||||
ImmutableArray<Diagnostic>.Builder builder = ImmutableArray.CreateBuilder<Diagnostic>();
|
||||
@@ -62,14 +65,8 @@ partial class ID2D1ShaderGenerator
|
||||
var declaredTypes = GetDeclaredTypes(builder, structDeclarationSymbol, discoveredTypes);
|
||||
var definedConstants = GetDefinedConstants(constantDefinitions);
|
||||
|
||||
// Gather the shader metadata
|
||||
GatherD2D1AttributeInfo(
|
||||
builder,
|
||||
structDeclarationSymbol,
|
||||
out inputCount,
|
||||
out ImmutableArray<int> inputSimpleIndices,
|
||||
out ImmutableArray<int> inputComplexIndices,
|
||||
out bool requiresScenePosition);
|
||||
// Check whether the scene position is required
|
||||
bool requiresScenePosition = GetRequiresScenePositionInfo(structDeclarationSymbol);
|
||||
|
||||
diagnostics = builder.ToImmutable();
|
||||
|
||||
@@ -397,100 +394,21 @@ partial class ID2D1ShaderGenerator
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extracts the metadata definition for the current shader.
|
||||
/// Gets whether or not a given shader requires the scene position.
|
||||
/// </summary>
|
||||
/// <param name="diagnostics">The collection of produced <see cref="Diagnostic"/> instances.</param>
|
||||
/// <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>
|
||||
private static void GatherD2D1AttributeInfo(
|
||||
ImmutableArray<Diagnostic>.Builder diagnostics,
|
||||
INamedTypeSymbol structDeclarationSymbol,
|
||||
out int inputCount,
|
||||
out ImmutableArray<int> inputSimpleIndices,
|
||||
out ImmutableArray<int> inputComplexIndices,
|
||||
out bool requiresScenePosition)
|
||||
/// <remarks>Whether <paramref name="structDeclarationSymbol"/> requires the scene position.</remarks>
|
||||
private static bool GetRequiresScenePositionInfo(INamedTypeSymbol structDeclarationSymbol)
|
||||
{
|
||||
// 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).
|
||||
int rawInputCount = 0;
|
||||
|
||||
inputCount = 0;
|
||||
requiresScenePosition = false;
|
||||
|
||||
ImmutableArray<int>.Builder inputSimpleIndicesBuilder = ImmutableArray.CreateBuilder<int>();
|
||||
ImmutableArray<int>.Builder inputComplexIndicesBuilder = ImmutableArray.CreateBuilder<int>();
|
||||
|
||||
foreach (AttributeData attributeData in structDeclarationSymbol.GetAttributes())
|
||||
{
|
||||
switch (attributeData.AttributeClass?.GetFullMetadataName())
|
||||
if (attributeData.AttributeClass?.GetFullMetadataName() is "ComputeSharp.D2D1.D2DRequiresScenePositionAttribute")
|
||||
{
|
||||
case "ComputeSharp.D2D1.D2DInputCountAttribute":
|
||||
rawInputCount = (int)attributeData.ConstructorArguments[0].Value!;
|
||||
inputCount = Math.Max(rawInputCount, 0);
|
||||
break;
|
||||
case "ComputeSharp.D2D1.D2DInputSimpleAttribute":
|
||||
inputSimpleIndicesBuilder.Add((int)attributeData.ConstructorArguments[0].Value!);
|
||||
break;
|
||||
case "ComputeSharp.D2D1.D2DInputComplexAttribute":
|
||||
inputComplexIndicesBuilder.Add((int)attributeData.ConstructorArguments[0].Value!);
|
||||
break;
|
||||
case "ComputeSharp.D2D1.D2DRequiresScenePositionAttribute":
|
||||
requiresScenePosition = true;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
inputSimpleIndices = inputSimpleIndicesBuilder.ToImmutable();
|
||||
inputComplexIndices = inputComplexIndicesBuilder.ToImmutable();
|
||||
|
||||
// Validate the input count
|
||||
if (rawInputCount is not (>= 0 and <= 8))
|
||||
{
|
||||
diagnostics.Add(InvalidD2DInputCount, structDeclarationSymbol, structDeclarationSymbol);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// All simple indices must be in range
|
||||
if (inputSimpleIndicesBuilder.Concat(inputComplexIndicesBuilder).Any(i => (uint)i >= rawInputCount))
|
||||
{
|
||||
diagnostics.Add(OutOfRangeInputIndex, structDeclarationSymbol, structDeclarationSymbol);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
HashSet<int> inputSimpleIndicesAsSet = new(inputSimpleIndicesBuilder);
|
||||
HashSet<int> inputComplexIndicesAsSet = new(inputComplexIndicesBuilder);
|
||||
|
||||
// All simple indices must be unique
|
||||
if (inputSimpleIndicesAsSet.Count != inputSimpleIndicesBuilder.Count)
|
||||
{
|
||||
diagnostics.Add(RepeatedD2DInputSimpleIndices, structDeclarationSymbol, structDeclarationSymbol);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// All complex indices must be unique
|
||||
if (inputComplexIndicesAsSet.Count != inputComplexIndicesBuilder.Count)
|
||||
{
|
||||
diagnostics.Add(RepeatedD2DInputComplexIndices, structDeclarationSymbol, structDeclarationSymbol);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Simple and complex indices can't have indices in common
|
||||
if (inputSimpleIndicesAsSet.Intersect(inputComplexIndicesAsSet).Any())
|
||||
{
|
||||
diagnostics.Add(InvalidSimpleAndComplexIndicesCombination, structDeclarationSymbol, structDeclarationSymbol);
|
||||
|
||||
return;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ partial class ID2D1ShaderGenerator
|
||||
/// <summary>
|
||||
/// A helper with all logic to generate the <c>GetInputCount</c> method.
|
||||
/// </summary>
|
||||
partial class GetInputCount
|
||||
private static partial class GetInputCount
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a <see cref="MethodDeclarationSyntax"/> instance for the <c>GetInputCount</c> method.
|
||||
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
using System.Collections.Immutable;
|
||||
using ComputeSharp.D2D1.__Internals;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
|
||||
|
||||
#pragma warning disable CS0618
|
||||
|
||||
namespace ComputeSharp.D2D1.SourceGenerators;
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial class ID2D1ShaderGenerator
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
partial class GetInputType
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a <see cref="MethodDeclarationSyntax"/> instance for the <c>GetInputTypeMethod</c> method.
|
||||
/// </summary>
|
||||
/// <param name="inputTypes">The input types for the shader.</param>
|
||||
/// <returns>The resulting <see cref="MethodDeclarationSyntax"/> instance for the <c>GetInputTypeMethod</c> method.</returns>
|
||||
public static MethodDeclarationSyntax GetSyntax(ImmutableArray<uint> inputTypes)
|
||||
{
|
||||
// This code produces a method declaration as follows:
|
||||
//
|
||||
// readonly uint global::ComputeSharp.D2D1.__Internals.ID2D1Shader.GetInputType(uint index)
|
||||
// {
|
||||
// return <INPUT_TYPE>;
|
||||
// }
|
||||
return
|
||||
MethodDeclaration(PredefinedType(Token(SyntaxKind.UIntKeyword)), Identifier("GetInputType"))
|
||||
.WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}")))
|
||||
.AddModifiers(Token(SyntaxKind.ReadOnlyKeyword))
|
||||
.AddParameterListParameters(Parameter(Identifier("index")).WithType(PredefinedType(Token(SyntaxKind.UIntKeyword))))
|
||||
.WithBody(Block(ReturnStatement(GetInputTypesSwitchExpression(inputTypes))));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the expression to get the type of a given shader input from its index.
|
||||
/// </summary>
|
||||
/// <param name="inputTypes">The input types for the shader.</param>
|
||||
/// <returns>The expression to extract the values from <paramref name="inputTypes"/>.</returns>
|
||||
private static ExpressionSyntax GetInputTypesSwitchExpression(ImmutableArray<uint> inputTypes)
|
||||
{
|
||||
// If there are no inputs, just return an unspecified value.
|
||||
if (inputTypes.IsEmpty)
|
||||
{
|
||||
return LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0));
|
||||
}
|
||||
|
||||
ImmutableArray<SwitchExpressionArmSyntax>.Builder switches = ImmutableArray.CreateBuilder<SwitchExpressionArmSyntax>();
|
||||
|
||||
for (int i = 0; i < inputTypes.Length; i++)
|
||||
{
|
||||
// Add a switch branch to map a given input to its serialized type, as follows:
|
||||
//
|
||||
// 0u => 1u
|
||||
switches.Add(
|
||||
SwitchExpressionArm(
|
||||
ConstantPattern(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal((uint)i))),
|
||||
LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(inputTypes[i]))));
|
||||
}
|
||||
|
||||
// Add a default arm
|
||||
switches.Add(
|
||||
SwitchExpressionArm(
|
||||
DiscardPattern(),
|
||||
LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0u))));
|
||||
|
||||
// Build a switch expression as follows:
|
||||
//
|
||||
// index switch
|
||||
// {
|
||||
// <SWITCH_ARMS>
|
||||
// }
|
||||
return
|
||||
SwitchExpression(IdentifierName("index"))
|
||||
.AddArms(switches.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using ComputeSharp.SourceGeneration.Extensions;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using static ComputeSharp.D2D1.SourceGenerators.Diagnostics.DiagnosticDescriptors;
|
||||
|
||||
namespace ComputeSharp.D2D1.SourceGenerators;
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial class ID2D1ShaderGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// A helper with all logic to generate the <c>GetInputTypeMethod</c> method.
|
||||
/// </summary>
|
||||
private static partial class GetInputType
|
||||
{
|
||||
/// <summary>
|
||||
/// Extracts the input info for the current shader.
|
||||
/// </summary>
|
||||
/// <param name="diagnostics">The collection of produced <see cref="Diagnostic"/> instances.</param>
|
||||
/// <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 indices of the complex shader inputs.</param>
|
||||
/// <param name="combinedInputTypes">The combined and serialized input types for each available input.</param>
|
||||
public static void GetInfo(
|
||||
INamedTypeSymbol structDeclarationSymbol,
|
||||
out int inputCount,
|
||||
out ImmutableArray<int> inputSimpleIndices,
|
||||
out ImmutableArray<int> inputComplexIndices,
|
||||
out ImmutableArray<uint> combinedInputTypes,
|
||||
out ImmutableArray<Diagnostic> diagnostics)
|
||||
{
|
||||
ImmutableArray<Diagnostic>.Builder builder = ImmutableArray.CreateBuilder<Diagnostic>();
|
||||
|
||||
// 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).
|
||||
int rawInputCount = 0;
|
||||
|
||||
inputCount = 0;
|
||||
|
||||
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.D2D1.D2DInputCountAttribute":
|
||||
rawInputCount = (int)attributeData.ConstructorArguments[0].Value!;
|
||||
inputCount = Math.Max(rawInputCount, 0);
|
||||
break;
|
||||
case "ComputeSharp.D2D1.D2DInputSimpleAttribute":
|
||||
inputSimpleIndicesBuilder.Add((int)attributeData.ConstructorArguments[0].Value!);
|
||||
break;
|
||||
case "ComputeSharp.D2D1.D2DInputComplexAttribute":
|
||||
inputComplexIndicesBuilder.Add((int)attributeData.ConstructorArguments[0].Value!);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
inputSimpleIndices = inputSimpleIndicesBuilder.ToImmutable();
|
||||
inputComplexIndices = inputComplexIndicesBuilder.ToImmutable();
|
||||
combinedInputTypes = ImmutableArray<uint>.Empty;
|
||||
|
||||
// Validate the input count
|
||||
if (rawInputCount is not (>= 0 and <= 8))
|
||||
{
|
||||
builder.Add(InvalidD2DInputCount, structDeclarationSymbol, structDeclarationSymbol);
|
||||
|
||||
goto End;
|
||||
}
|
||||
|
||||
// All simple indices must be in range
|
||||
if (inputSimpleIndicesBuilder.Concat(inputComplexIndicesBuilder).Any(i => (uint)i >= rawInputCount))
|
||||
{
|
||||
builder.Add(OutOfRangeInputIndex, structDeclarationSymbol, structDeclarationSymbol);
|
||||
|
||||
goto End;
|
||||
}
|
||||
|
||||
HashSet<int> inputSimpleIndicesAsSet = new(inputSimpleIndicesBuilder);
|
||||
HashSet<int> inputComplexIndicesAsSet = new(inputComplexIndicesBuilder);
|
||||
|
||||
// All simple indices must be unique
|
||||
if (inputSimpleIndicesAsSet.Count != inputSimpleIndicesBuilder.Count)
|
||||
{
|
||||
builder.Add(RepeatedD2DInputSimpleIndices, structDeclarationSymbol, structDeclarationSymbol);
|
||||
|
||||
goto End;
|
||||
}
|
||||
|
||||
// All complex indices must be unique
|
||||
if (inputComplexIndicesAsSet.Count != inputComplexIndicesBuilder.Count)
|
||||
{
|
||||
builder.Add(RepeatedD2DInputComplexIndices, structDeclarationSymbol, structDeclarationSymbol);
|
||||
|
||||
goto End;
|
||||
}
|
||||
|
||||
// Simple and complex indices can't have indices in common
|
||||
if (inputSimpleIndicesAsSet.Intersect(inputComplexIndicesAsSet).Any())
|
||||
{
|
||||
builder.Add(InvalidSimpleAndComplexIndicesCombination, structDeclarationSymbol, structDeclarationSymbol);
|
||||
|
||||
goto End;
|
||||
}
|
||||
|
||||
// Build the combined input types list now that inputs have been validated
|
||||
ImmutableArray<uint>.Builder combinedBuilder = ImmutableArray.CreateBuilder<uint>(inputCount);
|
||||
|
||||
for (int i = 0; i < inputCount; i++)
|
||||
{
|
||||
// The D2D_INPUT<N>_SIMPLE define is optional, and if omitted, the corresponding input
|
||||
// will automatically be of complex type. So we just need to check whether the i-th
|
||||
// input is present in the set of explicitly simple inputs. These values will map to
|
||||
// values from D2D1PixelShaderInputType, which has Simple and Complex as members.
|
||||
// So, simple inputs map to 0, and complex inputs map to 1.
|
||||
combinedBuilder.Add(inputSimpleIndicesAsSet.Contains(i) ? 0u : 1u);
|
||||
}
|
||||
|
||||
combinedInputTypes = combinedBuilder.MoveToImmutable();
|
||||
|
||||
End:
|
||||
diagnostics = builder.ToImmutable();
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -22,7 +22,7 @@ namespace ComputeSharp.D2D1.SourceGenerators;
|
||||
partial class ID2D1ShaderGenerator
|
||||
{
|
||||
/// <inheritodoc/>
|
||||
internal static partial class LoadBytecode
|
||||
private static partial class LoadBytecode
|
||||
{
|
||||
/// <summary>
|
||||
/// Extracts the shader profile for the current shader.
|
||||
|
||||
@@ -49,9 +49,9 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
|
||||
.Where(static item => IsD2D1PixelShaderType(item.Left.Symbol, item.Right))
|
||||
.Select(static (item, token) => (item.Left.Syntax, item.Left.Symbol, HierarchyInfo.From(item.Left.Symbol)));
|
||||
|
||||
// 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<(HierarchyInfo Hierarchy, Result<DispatchDataInfo> Dispatch, Result<HlslShaderSourceInfo> Source, int InputCount)> shaderInfoWithErrors =
|
||||
// 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<DispatchDataInfo> Dispatch, Result<InputTypesInfo> InputTypes, Result<HlslShaderSourceInfo> Source)> shaderInfoWithErrors =
|
||||
shaderDeclarations
|
||||
.Combine(context.CompilationProvider)
|
||||
.Select(static (item, token) =>
|
||||
@@ -66,12 +66,25 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
|
||||
|
||||
DispatchDataInfo dispatchDataInfo = new(fieldInfos, root32BitConstantCount);
|
||||
|
||||
// Get the input info for GetInputInfo()
|
||||
GetInputType.GetInfo(
|
||||
item.Left.Symbol,
|
||||
out int inputCount,
|
||||
out ImmutableArray<int> inputSimpleIndices,
|
||||
out ImmutableArray<int> inputComplexIndices,
|
||||
out ImmutableArray<uint> inputTypes,
|
||||
out ImmutableArray<Diagnostic> inputTypesDiagnostics);
|
||||
|
||||
InputTypesInfo inputTypesInfo = new(inputTypes);
|
||||
|
||||
// Get HLSL source for BuildHlslSource()
|
||||
string hlslSource = BuildHlslSource.GetHlslSource(
|
||||
item.Right,
|
||||
item.Left.Syntax,
|
||||
item.Left.Symbol,
|
||||
out int inputCount,
|
||||
inputCount,
|
||||
inputSimpleIndices,
|
||||
inputComplexIndices,
|
||||
out ImmutableArray<Diagnostic> hlslSourceDiagnostics);
|
||||
|
||||
token.ThrowIfCancellationRequested();
|
||||
@@ -92,18 +105,19 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
|
||||
return (
|
||||
item.Left.Hierarchy,
|
||||
new Result<DispatchDataInfo>(dispatchDataInfo, dispatchDataDiagnostics),
|
||||
new Result<HlslShaderSourceInfo>(sourceInfo, hlslSourceDiagnostics),
|
||||
inputCount);
|
||||
new Result<InputTypesInfo>(inputTypesInfo, inputTypesDiagnostics),
|
||||
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.InputTypes.Errors));
|
||||
context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.Source.Errors));
|
||||
|
||||
// Filter items to enable caching for the input count methods
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, int InputCount)> inputCountInfo =
|
||||
shaderInfoWithErrors
|
||||
.Select(static (item, token) => (item.Hierarchy, item.InputCount))
|
||||
.Select(static (item, token) => (item.Hierarchy, item.InputTypes.Value.InputTypes.Length))
|
||||
.WithComparers(HierarchyInfo.Comparer.Default, EqualityComparer<int>.Default);
|
||||
|
||||
// Generate the GetInputCount() methods
|
||||
@@ -116,10 +130,25 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
|
||||
});
|
||||
|
||||
// Filter all items to enable caching at a coarse level, and remove diagnostics
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch, HlslShaderSourceInfo Source)> shaderInfo =
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch, InputTypesInfo InputTypes, HlslShaderSourceInfo Source)> shaderInfo =
|
||||
shaderInfoWithErrors
|
||||
.Select(static (item, token) => (item.Hierarchy, item.Dispatch.Value, item.Source.Value))
|
||||
.WithComparers(HierarchyInfo.Comparer.Default, DispatchDataInfo.Comparer.Default, EqualityComparer<HlslShaderSourceInfo>.Default);
|
||||
.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<HlslShaderSourceInfo>.Default);
|
||||
|
||||
// Get a filtered sequence to enable caching
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, InputTypesInfo InputTypes)> inputTypesInfo =
|
||||
shaderInfo
|
||||
.Select(static (item, token) => (item.Hierarchy, item.InputTypes))
|
||||
.WithComparers(HierarchyInfo.Comparer.Default, InputTypesInfo.Comparer.Default);
|
||||
|
||||
// Generate the GetInputType() methods
|
||||
context.RegisterSourceOutput(inputTypesInfo, static (context, item) =>
|
||||
{
|
||||
MethodDeclarationSyntax getInputTypeMethod = GetInputType.GetSyntax(item.InputTypes.InputTypes);
|
||||
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, getInputTypeMethod, false);
|
||||
|
||||
context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(GetInputType)}", compilationUnit.ToFullString());
|
||||
});
|
||||
|
||||
// Get a filtered sequence to enable caching
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch)> dispatchDataInfo =
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using ComputeSharp.SourceGeneration.Extensions;
|
||||
using ComputeSharp.SourceGeneration.Helpers;
|
||||
|
||||
namespace ComputeSharp.D2D1.SourceGenerators.Models;
|
||||
|
||||
/// <summary>
|
||||
/// A model representing gathered info on a shader input types.
|
||||
/// </summary>
|
||||
/// <param name="InputTypes">The input types for a given shader.</param>
|
||||
internal sealed record InputTypesInfo(ImmutableArray<uint> InputTypes)
|
||||
{
|
||||
/// <summary>
|
||||
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="InputTypesInfo"/>.
|
||||
/// </summary>
|
||||
public sealed class Comparer : Comparer<InputTypesInfo, Comparer>
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
protected override void AddToHashCode(ref HashCode hashCode, InputTypesInfo obj)
|
||||
{
|
||||
hashCode.AddRange(obj.InputTypes);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override bool AreEqual(InputTypesInfo x, InputTypesInfo y)
|
||||
{
|
||||
return x.InputTypes.SequenceEqual(y.InputTypes);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -46,6 +46,29 @@ internal static class IncrementalValuesProviderExtensions
|
||||
return source.WithComparer(new Comparer<T1, T2, T3>(comparer1, comparer2, comparer3));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="IncrementalValuesProvider{TValues}"/> instance with a gven pair of comparers.
|
||||
/// </summary>
|
||||
/// <typeparam name="T1">The type of first items in each tuple.</typeparam>
|
||||
/// <typeparam name="T2">The type of second items in each tuple.</typeparam>
|
||||
/// <typeparam name="T3">The type of third items in each tuple.</typeparam>
|
||||
/// <typeparam name="T4">The type of fourth items in each tuple.</typeparam>
|
||||
/// <param name="source">The input <see cref="IncrementalValuesProvider{TValues}"/> instance.</param>
|
||||
/// <param name="comparer1">An <see cref="IEqualityComparer{T}"/> instance for <typeparamref name="T1"/> items.</param>
|
||||
/// <param name="comparer2">An <see cref="IEqualityComparer{T}"/> instance for <typeparamref name="T2"/> items.</param>
|
||||
/// <param name="comparer3">An <see cref="IEqualityComparer{T}"/> instance for <typeparamref name="T3"/> items.</param>
|
||||
/// <param name="comparer4">An <see cref="IEqualityComparer{T}"/> instance for <typeparamref name="T4"/> items.</param>
|
||||
/// <returns>An <see cref="IncrementalValuesProvider{TValues}"/> with the specified comparers applied to each item.</returns>
|
||||
public static IncrementalValuesProvider<(T1, T2, T3, T4)> WithComparers<T1, T2, T3, T4>(
|
||||
this IncrementalValuesProvider<(T1, T2, T3, T4)> source,
|
||||
IEqualityComparer<T1> comparer1,
|
||||
IEqualityComparer<T2> comparer2,
|
||||
IEqualityComparer<T3> comparer3,
|
||||
IEqualityComparer<T4> comparer4)
|
||||
{
|
||||
return source.WithComparer(new Comparer<T1, T2, T3, T4>(comparer1, comparer2, comparer3, comparer4));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="IEqualityComparer{T}"/> implementation for a value tuple.
|
||||
/// </summary>
|
||||
@@ -140,4 +163,65 @@ internal static class IncrementalValuesProviderExtensions
|
||||
this.comparer3.GetHashCode(obj.Item3));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="IEqualityComparer{T}"/> implementation for a value tuple.
|
||||
/// </summary>
|
||||
public sealed class Comparer<T1, T2, T3, T4> : IEqualityComparer<(T1, T2, T3, T4)>
|
||||
{
|
||||
/// <summary>
|
||||
/// The <typeparamref name="T1"/> comparer.
|
||||
/// </summary>
|
||||
private readonly IEqualityComparer<T1> comparer1;
|
||||
|
||||
/// <summary>
|
||||
/// The <typeparamref name="T2"/> comparer.
|
||||
/// </summary>
|
||||
private readonly IEqualityComparer<T2> comparer2;
|
||||
|
||||
/// <summary>
|
||||
/// The <typeparamref name="T3"/> comparer.
|
||||
/// </summary>
|
||||
private readonly IEqualityComparer<T3> comparer3;
|
||||
|
||||
/// <summary>
|
||||
/// The <typeparamref name="T4"/> comparer.
|
||||
/// </summary>
|
||||
private readonly IEqualityComparer<T4> comparer4;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Comparer{T1, T2, T3, T4}"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="comparer1">The <typeparamref name="T1"/> comparer.</param>
|
||||
/// <param name="comparer2">The <typeparamref name="T2"/> comparer.</param>
|
||||
/// <param name="comparer3">The <typeparamref name="T3"/> comparer.</param>
|
||||
/// <param name="comparer4">The <typeparamref name="T4"/> comparer.</param>
|
||||
public Comparer(IEqualityComparer<T1> comparer1, IEqualityComparer<T2> comparer2, IEqualityComparer<T3> comparer3, IEqualityComparer<T4> comparer4)
|
||||
{
|
||||
this.comparer1 = comparer1;
|
||||
this.comparer2 = comparer2;
|
||||
this.comparer3 = comparer3;
|
||||
this.comparer4 = comparer4;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool Equals((T1, T2, T3, T4) x, (T1, T2, T3, T4) y)
|
||||
{
|
||||
return
|
||||
this.comparer1.Equals(x.Item1, y.Item1) &&
|
||||
this.comparer2.Equals(x.Item2, y.Item2) &&
|
||||
this.comparer3.Equals(x.Item3, y.Item3) &&
|
||||
this.comparer4.Equals(x.Item4, y.Item4);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int GetHashCode((T1, T2, T3, T4) obj)
|
||||
{
|
||||
return HashCode.Combine(
|
||||
this.comparer1.GetHashCode(obj.Item1),
|
||||
this.comparer2.GetHashCode(obj.Item2),
|
||||
this.comparer3.GetHashCode(obj.Item3),
|
||||
this.comparer4.GetHashCode(obj.Item4));
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする