Merge pull request #264 from Sergio0694/feature/d2d1-get-pixel-input-type
Add D2D1InteropServices.GetInputType API
このコミットが含まれているのは:
13ファイル変更
+487
-109
検索条件に一致するファイルはありません
+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));
|
||||
}
|
||||
|
||||
// In order to avoid repeated branches, we can build a bitmask that stores in each
|
||||
// bit whether the corresponding pixel shader input is simple or complex. This will
|
||||
// allow the logic to be branchless and to just extract the target bit from the mask.
|
||||
uint bitmask = 0;
|
||||
|
||||
for (int i = 0; i < inputTypes.Length; i++)
|
||||
{
|
||||
bitmask |= inputTypes[i] << i;
|
||||
}
|
||||
|
||||
// If all inputs are simple, also just return 0
|
||||
if (bitmask == 0)
|
||||
{
|
||||
return LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0));
|
||||
}
|
||||
|
||||
// This code produces a bitmask flag extraction expression as follows:
|
||||
//
|
||||
// (<BITMASK> >> (int)index) & 1;
|
||||
return
|
||||
BinaryExpression(
|
||||
SyntaxKind.BitwiseAndExpression,
|
||||
ParenthesizedExpression(
|
||||
BinaryExpression(
|
||||
SyntaxKind.RightShiftExpression,
|
||||
LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(bitmask)),
|
||||
CastExpression(PredefinedType(Token(SyntaxKind.IntKeyword)), IdentifierName("index")))),
|
||||
LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(1)));
|
||||
}
|
||||
}
|
||||
}
|
||||
+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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -45,6 +45,18 @@ internal static partial class ThrowHelper
|
||||
throw new ArgumentOutOfRangeException(name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws a new <see cref="ArgumentOutOfRangeException"/>.
|
||||
/// </summary>
|
||||
/// <param name="name">The argument name.</param>
|
||||
/// <param name="message">The message to include in the exception.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown with the specified parameter.</exception>
|
||||
[DoesNotReturn]
|
||||
public static void ThrowArgumentOutOfRangeException(string? name, string? message)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(name, message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws a new <see cref="InvalidOperationException"/>.
|
||||
/// </summary>
|
||||
|
||||
@@ -27,6 +27,16 @@ public interface ID2D1Shader
|
||||
[Obsolete("This method is not intended to be used directly by user code")]
|
||||
uint GetInputCount();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the input type for the input at the input index.
|
||||
/// </summary>
|
||||
/// <param name="index">The index of the shader input to get the type for.</param>
|
||||
/// <returns>The type of input at the specified index.</returns>
|
||||
/// <remarks>The return value if the input is out of range is undefined.</remarks>
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
[Obsolete("This method is not intended to be used directly by user code")]
|
||||
uint GetInputType(uint index);
|
||||
|
||||
/// <summary>
|
||||
/// Loads the dispatch data for the shader.
|
||||
/// </summary>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.D2D1.Helpers;
|
||||
using ComputeSharp.D2D1.Shaders.Dispatching;
|
||||
using ComputeSharp.D2D1.Shaders.Interop.Buffers;
|
||||
using TerraFX.Interop.DirectX;
|
||||
@@ -92,6 +93,26 @@ public static unsafe class D2D1InteropServices
|
||||
return shader.GetInputCount();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the type of a given input for a D2D1 pixel shader.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of D2D1 pixel shader to get the input type for.</typeparam>
|
||||
/// <param name="index">The index of the input to get the type for.</param>
|
||||
/// <returns>The type of the input of the target D2D1 pixel shader at the specified index.</returns>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="index"/> is not in range for the available inputs for the shader type.</exception>
|
||||
public static D2D1PixelShaderInputType GetPixelShaderInputType<T>(uint index)
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
Unsafe.SkipInit(out T shader);
|
||||
|
||||
if (index >= shader.GetInputCount())
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index), "The input index is outside of range for the target pixel shader type.");
|
||||
}
|
||||
|
||||
return (D2D1PixelShaderInputType)shader.GetInputType(index);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the constant buffer from an input D2D1 pixel shader.
|
||||
/// </summary>
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
namespace ComputeSharp.D2D1.Interop;
|
||||
|
||||
/// <summary>
|
||||
/// Indicates the type of a given input for a D2D1 pixel shader.
|
||||
/// </summary>
|
||||
public enum D2D1PixelShaderInputType
|
||||
{
|
||||
/// <summary>
|
||||
/// The input is of simple type.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// A pixel shader is of simple type when the consuming pixel shader requires only a single input value to perform its computation. This value would normally
|
||||
/// come from sampling an input texture at the texture coordinate emitted by the vertex shader. Such a pixel shader is said to perform simple sampling.
|
||||
/// </para>
|
||||
/// <para>For more info, see <see href="https://docs.microsoft.com/windows/win32/direct2d/effect-shader-linking"/>.</para>
|
||||
/// </remarks>
|
||||
Simple,
|
||||
|
||||
/// <summary>
|
||||
/// The input is of complex type.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Some pixel shaders, such as a Gaussian blur, compute their output from multiple input samples
|
||||
/// rather than just a single sample. Such a pixel shader is said to perform complex sampling.
|
||||
/// </para>
|
||||
/// <para>For more info, see <see href="https://docs.microsoft.com/windows/win32/direct2d/effect-shader-linking"/>.</para>
|
||||
/// </remarks>
|
||||
Complex
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ namespace ComputeSharp.D2D1.Tests;
|
||||
|
||||
[TestClass]
|
||||
[TestCategory("D2D1InteropServices")]
|
||||
public class D2D1InteropServicesTests
|
||||
public partial class D2D1InteropServicesTests
|
||||
{
|
||||
[TestMethod]
|
||||
public unsafe void GetInputCount()
|
||||
@@ -14,5 +14,32 @@ public class D2D1InteropServicesTests
|
||||
Assert.AreEqual(D2D1InteropServices.GetPixelShaderInputCount<InvertEffect>(), 1u);
|
||||
Assert.AreEqual(D2D1InteropServices.GetPixelShaderInputCount<PixelateEffect.Shader>(), 1u);
|
||||
Assert.AreEqual(D2D1InteropServices.GetPixelShaderInputCount<ZonePlateEffect>(), 0u);
|
||||
Assert.AreEqual(D2D1InteropServices.GetPixelShaderInputCount<ShaderWithMultipleInputs>(), 7u);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public unsafe void GetInputType()
|
||||
{
|
||||
Assert.AreEqual(D2D1InteropServices.GetPixelShaderInputType<ShaderWithMultipleInputs>(0), D2D1PixelShaderInputType.Simple);
|
||||
Assert.AreEqual(D2D1InteropServices.GetPixelShaderInputType<ShaderWithMultipleInputs>(1), D2D1PixelShaderInputType.Complex);
|
||||
Assert.AreEqual(D2D1InteropServices.GetPixelShaderInputType<ShaderWithMultipleInputs>(2), D2D1PixelShaderInputType.Simple);
|
||||
Assert.AreEqual(D2D1InteropServices.GetPixelShaderInputType<ShaderWithMultipleInputs>(3), D2D1PixelShaderInputType.Complex);
|
||||
Assert.AreEqual(D2D1InteropServices.GetPixelShaderInputType<ShaderWithMultipleInputs>(4), D2D1PixelShaderInputType.Complex);
|
||||
Assert.AreEqual(D2D1InteropServices.GetPixelShaderInputType<ShaderWithMultipleInputs>(5), D2D1PixelShaderInputType.Complex);
|
||||
}
|
||||
|
||||
[D2DInputCount(7)]
|
||||
[D2DInputSimple(0)]
|
||||
[D2DInputSimple(2)]
|
||||
[D2DInputComplex(1)]
|
||||
[D2DInputComplex(3)]
|
||||
[D2DInputComplex(5)]
|
||||
[D2DInputSimple(6)]
|
||||
partial struct ShaderWithMultipleInputs : ID2D1PixelShader
|
||||
{
|
||||
public Float4 Execute()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする