diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs index c2042048..7553f9b2 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 byte global::ComputeSharp.D2D1.__Internals.ID2D1Shader.GetInputCount() + // readonly uint global::ComputeSharp.D2D1.__Internals.ID2D1Shader.GetInputCount() // { // return ; // } return - MethodDeclaration(PredefinedType(Token(SyntaxKind.ByteKeyword)), Identifier("GetInputCount")) + 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))))); diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.Syntax.cs index c22a4582..ce8db4a9 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 byte global::ComputeSharp.D2D1.__Internals.ID2D1Shader.GetInputType(byte index) + // readonly uint global::ComputeSharp.D2D1.__Internals.ID2D1Shader.GetInputType(uint index) // { // return ; // } return - MethodDeclaration(PredefinedType(Token(SyntaxKind.ByteKeyword)), Identifier("GetInputType")) + 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.ByteKeyword)))) + .AddParameterListParameters(Parameter(Identifier("index")).WithType(PredefinedType(Token(SyntaxKind.UIntKeyword)))) .WithBody(Block(ReturnStatement(GetInputTypesSwitchExpression(inputTypes)))); } @@ -48,34 +48,34 @@ partial class ID2D1ShaderGenerator return LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0)); } - ImmutableArray.Builder switches = ImmutableArray.CreateBuilder(); + // 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++) { - // Add a switch branch to map a given input to its serialized type, as follows: - // - // 0 => 1 - switches.Add( - SwitchExpressionArm( - ConstantPattern(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(i))), - LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal((int)inputTypes[i])))); + bitmask |= inputTypes[i] << i; } - // Add a default arm - switches.Add( - SwitchExpressionArm( - DiscardPattern(), - LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0)))); + // If all inputs are simple, also just return 0 + if (bitmask == 0) + { + return LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0)); + } - // Build a switch expression as follows: + // This code produces a bitmask flag extraction expression as follows: // - // index switch - // { - // - // } + // ( >> (int)index) & 1; return - SwitchExpression(IdentifierName("index")) - .AddArms(switches.ToArray()); + BinaryExpression( + SyntaxKind.BitwiseAndExpression, + ParenthesizedExpression( + BinaryExpression( + SyntaxKind.RightShiftExpression, + LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(bitmask)), + CastExpression(PredefinedType(Token(SyntaxKind.IntKeyword)), IdentifierName("index")))), + LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(1))); } } } diff --git a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs index a1cc013b..b9d2593a 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")] - byte GetInputCount(); + uint 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")] - byte GetInputType(byte index); + uint GetInputType(uint 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 7560a7ee..d4dbe25c 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.cs @@ -110,7 +110,7 @@ public static unsafe class D2D1InteropServices ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index), "The input index is outside of range for the target pixel shader type."); } - return (D2D1PixelShaderInputType)shader.GetInputType((byte)index); + return (D2D1PixelShaderInputType)shader.GetInputType(index); } /// diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderInputType.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderInputType.cs index 2945c957..19ae7aec 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 : byte +public enum D2D1PixelShaderInputType { /// /// The input is of simple type.