Merge pull request #249 from Sergio0694/feature/get-input-count-method

Add D2D1InteropServices.GetPixelShaderInputCount<T>() API
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2022-04-28 00:53:50 +02:00
コミット 295772682e
6ファイル変更
+97 -4

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

@@ -0,0 +1,38 @@
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
{
/// <summary>
/// A helper with all logic to generate the <c>GetInputCount</c> method.
/// </summary>
partial class GetInputCount
{
/// <summary>
/// Creates a <see cref="MethodDeclarationSyntax"/> instance for the <c>GetInputCount</c> method.
/// </summary>
/// <param name="inputCount">The number of inputs for the shader.</param>
/// <returns>The resulting <see cref="MethodDeclarationSyntax"/> instance for the <c>GetInputCount</c> method.</returns>
public static MethodDeclarationSyntax GetSyntax(int inputCount)
{
// This code produces a method declaration as follows:
//
// readonly uint global::ComputeSharp.D2D1.__Internals.ID2D1Shader.GetInputCount()
// {
// return <INPUT_COUNT>;
// }
return
MethodDeclaration(PredefinedType(Token(SyntaxKind.UIntKeyword)), Identifier("GetInputCount"))
.WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}")))
.AddModifiers(Token(SyntaxKind.ReadOnlyKeyword))
.WithBody(Block(ReturnStatement(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(inputCount)))));
}
}
}
+19 -3
ファイルの表示
@@ -51,7 +51,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<(HierarchyInfo Hierarchy, Result<DispatchDataInfo> Dispatch, Result<HlslShaderSourceInfo> Source)> shaderInfoWithErrors =
IncrementalValuesProvider<(HierarchyInfo Hierarchy, Result<DispatchDataInfo> Dispatch, Result<HlslShaderSourceInfo> Source, int InputCount)> shaderInfoWithErrors =
shaderDeclarations
.Combine(context.CompilationProvider)
.Select(static (item, token) =>
@@ -92,13 +92,29 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
return (
item.Left.Hierarchy,
new Result<DispatchDataInfo>(dispatchDataInfo, dispatchDataDiagnostics),
new Result<HlslShaderSourceInfo>(sourceInfo, hlslSourceDiagnostics));
new Result<HlslShaderSourceInfo>(sourceInfo, hlslSourceDiagnostics),
inputCount);
});
// Output the diagnostics
context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.Dispatch.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))
.WithComparers(HierarchyInfo.Comparer.Default, EqualityComparer<int>.Default);
// Generate the GetInputCount() methods
context.RegisterSourceOutput(inputCountInfo, static (context, item) =>
{
MethodDeclarationSyntax getInputCountMethod = GetInputCount.GetSyntax(item.InputCount);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, getInputCountMethod, false);
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, HlslShaderSourceInfo Source)> shaderInfo =
shaderInfoWithErrors
@@ -106,7 +122,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
.WithComparers(HierarchyInfo.Comparer.Default, DispatchDataInfo.Comparer.Default, EqualityComparer<HlslShaderSourceInfo>.Default);
// Get a filtered sequence to enable caching
IncrementalValuesProvider< (HierarchyInfo Hierarchy, DispatchDataInfo Dispatch)> dispatchDataInfo =
IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch)> dispatchDataInfo =
shaderInfo
.Select(static (item, token) => (item.Hierarchy, item.Dispatch))
.WithComparers(HierarchyInfo.Comparer.Default, DispatchDataInfo.Comparer.Default);
+8
ファイルの表示
@@ -19,6 +19,14 @@ public interface ID2D1Shader
[Obsolete("This method is not intended to be called directly by user code")]
void InitializeFromDispatchData(ReadOnlySpan<byte> data);
/// <summary>
/// Gets the number of inputs for the current shader.
/// </summary>
/// <returns>The number of inputs for the current shader.</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This method is not intended to be used directly by user code")]
uint GetInputCount();
/// <summary>
/// Loads the dispatch data for the shader.
/// </summary>
+13
ファイルの表示
@@ -79,6 +79,19 @@ public static unsafe class D2D1InteropServices
return new ReadOnlySpan<byte>(bytecodePtr, bytecodeSize).ToArray();
}
/// <summary>
/// Gets the number of inputs from an input D2D1 pixel shader.
/// </summary>
/// <typeparam name="T">The type of D2D1 pixel shader to get the input count for.</typeparam>
/// <returns>The number of inputs for the D2D1 pixel shader of type <typeparamref name="T"/>.</returns>
public static uint GetPixelShaderInputCount<T>()
where T : unmanaged, ID2D1PixelShader
{
Unsafe.SkipInit(out T shader);
return shader.GetInputCount();
}
/// <summary>
/// Gets the constant buffer from an input D2D1 pixel shader.
/// </summary>
+1 -1
ファイルの表示
@@ -89,7 +89,7 @@ internal unsafe partial struct PixelShaderEffect
{
// Load all shader properties
Guid guid = typeof(T).GUID;
int inputCount = ((D2DInputCountAttribute[])typeof(T).GetCustomAttributes(typeof(D2DInputCountAttribute), false))[0].Count;
int inputCount = (int)D2D1InteropServices.GetPixelShaderInputCount<T>();
ReadOnlyMemory<byte> buffer = D2D1InteropServices.LoadShaderBytecode<T>();
byte* typeAssociatedMemory = (byte*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(For<T>), buffer.Length);
+18
ファイルの表示
@@ -0,0 +1,18 @@
using ComputeSharp.D2D1.Interop;
using ComputeSharp.D2D1.Tests.Effects;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComputeSharp.D2D1.Tests;
[TestClass]
[TestCategory("D2D1InteropServices")]
public class D2D1InteropServicesTests
{
[TestMethod]
public unsafe void GetInputCount()
{
Assert.AreEqual(D2D1InteropServices.GetPixelShaderInputCount<InvertEffect>(), 1u);
Assert.AreEqual(D2D1InteropServices.GetPixelShaderInputCount<PixelateEffect.Shader>(), 1u);
Assert.AreEqual(D2D1InteropServices.GetPixelShaderInputCount<ZonePlateEffect>(), 0u);
}
}