From e7c4288adfce4d116b81860de64b61937f0e3a02 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 6 May 2022 19:32:29 +0200 Subject: [PATCH] Add D2D1InteropServices.GetPixelShaderInputType API --- ...erator.CreateGetInputCountMethod.Syntax.cs | 4 ++-- ...nerator.CreateGetInputTypeMethod.Syntax.cs | 14 ++++++------- src/ComputeSharp.D2D1/Helpers/ThrowHelper.cs | 12 +++++++++++ .../Interfaces/__Internals/ID2D1Shader.cs | 4 ++-- .../Shaders/Interop/D2D1InteropServices.cs | 21 +++++++++++++++++++ .../Interop/D2D1PixelShaderInputType.cs | 2 +- 6 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs index 7553f9b2..c2042048 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs @@ -24,12 +24,12 @@ partial class ID2D1ShaderGenerator { // This code produces a method declaration as follows: // - // readonly uint global::ComputeSharp.D2D1.__Internals.ID2D1Shader.GetInputCount() + // readonly byte global::ComputeSharp.D2D1.__Internals.ID2D1Shader.GetInputCount() // { // return ; // } return - MethodDeclaration(PredefinedType(Token(SyntaxKind.UIntKeyword)), Identifier("GetInputCount")) + MethodDeclaration(PredefinedType(Token(SyntaxKind.ByteKeyword)), Identifier("GetInputCount")) .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) .WithBody(Block(ReturnStatement(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(inputCount))))); diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.Syntax.cs index da74241a..c22a4582 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.Syntax.cs @@ -23,15 +23,15 @@ partial class ID2D1ShaderGenerator { // This code produces a method declaration as follows: // - // readonly uint global::ComputeSharp.D2D1.__Internals.ID2D1Shader.GetInputType(uint index) + // readonly byte global::ComputeSharp.D2D1.__Internals.ID2D1Shader.GetInputType(byte index) // { // return ; // } return - MethodDeclaration(PredefinedType(Token(SyntaxKind.UIntKeyword)), Identifier("GetInputType")) + MethodDeclaration(PredefinedType(Token(SyntaxKind.ByteKeyword)), Identifier("GetInputType")) .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) - .AddParameterListParameters(Parameter(Identifier("index")).WithType(PredefinedType(Token(SyntaxKind.UIntKeyword)))) + .AddParameterListParameters(Parameter(Identifier("index")).WithType(PredefinedType(Token(SyntaxKind.ByteKeyword)))) .WithBody(Block(ReturnStatement(GetInputTypesSwitchExpression(inputTypes)))); } @@ -54,18 +54,18 @@ partial class ID2D1ShaderGenerator { // Add a switch branch to map a given input to its serialized type, as follows: // - // 0u => 1u + // 0 => 1 switches.Add( SwitchExpressionArm( - ConstantPattern(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal((uint)i))), - LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(inputTypes[i])))); + ConstantPattern(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(i))), + LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal((int)inputTypes[i])))); } // Add a default arm switches.Add( SwitchExpressionArm( DiscardPattern(), - LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0u)))); + LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0)))); // Build a switch expression as follows: // diff --git a/src/ComputeSharp.D2D1/Helpers/ThrowHelper.cs b/src/ComputeSharp.D2D1/Helpers/ThrowHelper.cs index 012eca14..a2163e54 100644 --- a/src/ComputeSharp.D2D1/Helpers/ThrowHelper.cs +++ b/src/ComputeSharp.D2D1/Helpers/ThrowHelper.cs @@ -45,6 +45,18 @@ internal static partial class ThrowHelper throw new ArgumentOutOfRangeException(name); } + /// + /// Throws a new . + /// + /// The argument name. + /// The message to include in the exception. + /// Thrown with the specified parameter. + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeException(string? name, string? message) + { + throw new ArgumentOutOfRangeException(name, message); + } + /// /// Throws a new . /// diff --git a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs index b9d2593a..a1cc013b 100644 --- a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs +++ b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs @@ -25,7 +25,7 @@ public interface ID2D1Shader /// The number of inputs for the current shader. [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("This method is not intended to be used directly by user code")] - uint GetInputCount(); + byte GetInputCount(); /// /// Gets the input type for the input at the input index. @@ -35,7 +35,7 @@ public interface ID2D1Shader /// The return value if the input is out of range is undefined. [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("This method is not intended to be used directly by user code")] - uint GetInputType(uint index); + byte GetInputType(byte index); /// /// Loads the dispatch data for the shader. diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.cs index b9540bfb..7560a7ee 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.cs @@ -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(); } + /// + /// Gets the type of a given input for a D2D1 pixel shader. + /// + /// The type of D2D1 pixel shader to get the input type for. + /// The index of the input to get the type for. + /// The type of the input of the target D2D1 pixel shader at the specified index. + /// Thrown if is not in range for the available inputs for the shader type. + public static D2D1PixelShaderInputType GetPixelShaderInputType(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((byte)index); + } + /// /// Gets the constant buffer from an input D2D1 pixel shader. /// diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderInputType.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderInputType.cs index 19ae7aec..2945c957 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderInputType.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderInputType.cs @@ -3,7 +3,7 @@ /// /// Indicates the type of a given input for a D2D1 pixel shader. /// -public enum D2D1PixelShaderInputType +public enum D2D1PixelShaderInputType : byte { /// /// The input is of simple type.