From f8b05d070ce80dac6f42f63916b72fd0589ff68a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 7 May 2022 17:03:45 +0200 Subject: [PATCH 1/6] Add D2DOutputBuffer attribute and types --- .../ComputeSharp.D2D1.SourceGenerators.csproj | 2 +- .../D2DEmbeddedBytecodeAttribute.cs | 15 +----- .../Attributes/D2DInputComplexAttribute.cs | 3 +- .../Attributes/D2DInputCountAttribute.cs | 3 +- .../Attributes/D2DInputSimpleAttribute.cs | 3 +- .../Attributes/D2DOutputBuffer.cs | 53 +++++++++++++++++++ .../D2DRequiresScenePositionAttribute.cs | 3 +- .../Attributes/Enums/D2D1BufferPrecision.cs | 41 ++++++++++++++ .../Attributes/Enums/D2D1ChannelDepth.cs | 26 +++++++++ .../{Profiles => Enums}/D2D1ShaderProfile.cs | 0 10 files changed, 130 insertions(+), 19 deletions(-) create mode 100644 src/ComputeSharp.D2D1/Attributes/D2DOutputBuffer.cs create mode 100644 src/ComputeSharp.D2D1/Attributes/Enums/D2D1BufferPrecision.cs create mode 100644 src/ComputeSharp.D2D1/Attributes/Enums/D2D1ChannelDepth.cs rename src/ComputeSharp.D2D1/Attributes/{Profiles => Enums}/D2D1ShaderProfile.cs (100%) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj index 254db83b..b68b1087 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj +++ b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj @@ -64,7 +64,7 @@ - + diff --git a/src/ComputeSharp.D2D1/Attributes/D2DEmbeddedBytecodeAttribute.cs b/src/ComputeSharp.D2D1/Attributes/D2DEmbeddedBytecodeAttribute.cs index ab9ab4c7..1325ad6c 100644 --- a/src/ComputeSharp.D2D1/Attributes/D2DEmbeddedBytecodeAttribute.cs +++ b/src/ComputeSharp.D2D1/Attributes/D2DEmbeddedBytecodeAttribute.cs @@ -1,7 +1,4 @@ using System; -#if !SOURCE_GENERATOR -using ComputeSharp.D2D1.Helpers; -#endif namespace ComputeSharp.D2D1; @@ -32,17 +29,7 @@ public sealed class D2DEmbeddedBytecodeAttribute : Attribute /// The target shader profile to use to compile the shader. public D2DEmbeddedBytecodeAttribute(D2D1ShaderProfile shaderProfile) { -#if !SOURCE_GENERATOR - ShaderProfile = shaderProfile switch - { - D2D1ShaderProfile.PixelShader40 or - D2D1ShaderProfile.PixelShader40Level91 or - D2D1ShaderProfile.PixelShader40Level93 or - D2D1ShaderProfile.PixelShader41 or - D2D1ShaderProfile.PixelShader50 => shaderProfile, - _ => ThrowHelper.ThrowArgumentException(nameof(shaderProfile), "Invalid shader profile value.") - }; -#endif + ShaderProfile = shaderProfile; } /// diff --git a/src/ComputeSharp.D2D1/Attributes/D2DInputComplexAttribute.cs b/src/ComputeSharp.D2D1/Attributes/D2DInputComplexAttribute.cs index 59f8188b..c3d5717c 100644 --- a/src/ComputeSharp.D2D1/Attributes/D2DInputComplexAttribute.cs +++ b/src/ComputeSharp.D2D1/Attributes/D2DInputComplexAttribute.cs @@ -8,7 +8,8 @@ namespace ComputeSharp.D2D1; /// /// /// This attribute directly maps to #define D2D_INPUT<N>_COMPLEX. -/// For more info, see . +/// For more info, see . +/// [AttributeUsage(AttributeTargets.Struct, AllowMultiple = true)] public sealed class D2DInputComplexAttribute : Attribute { diff --git a/src/ComputeSharp.D2D1/Attributes/D2DInputCountAttribute.cs b/src/ComputeSharp.D2D1/Attributes/D2DInputCountAttribute.cs index e578bf23..92e1f84f 100644 --- a/src/ComputeSharp.D2D1/Attributes/D2DInputCountAttribute.cs +++ b/src/ComputeSharp.D2D1/Attributes/D2DInputCountAttribute.cs @@ -8,7 +8,8 @@ namespace ComputeSharp.D2D1; /// /// /// This attribute directly maps to #define D2D_INPUT_COUNT <N>. -/// For more info, see . +/// For more info, see . +/// [AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)] public sealed class D2DInputCountAttribute : Attribute { diff --git a/src/ComputeSharp.D2D1/Attributes/D2DInputSimpleAttribute.cs b/src/ComputeSharp.D2D1/Attributes/D2DInputSimpleAttribute.cs index 7a23e2ac..4845e32b 100644 --- a/src/ComputeSharp.D2D1/Attributes/D2DInputSimpleAttribute.cs +++ b/src/ComputeSharp.D2D1/Attributes/D2DInputSimpleAttribute.cs @@ -8,7 +8,8 @@ namespace ComputeSharp.D2D1; /// /// /// This attribute directly maps to #define D2D_INPUT<N>_SIMPLE. -/// For more info, see . +/// For more info, see . +/// [AttributeUsage(AttributeTargets.Struct, AllowMultiple = true)] public sealed class D2DInputSimpleAttribute : Attribute { diff --git a/src/ComputeSharp.D2D1/Attributes/D2DOutputBuffer.cs b/src/ComputeSharp.D2D1/Attributes/D2DOutputBuffer.cs new file mode 100644 index 00000000..615d8a4d --- /dev/null +++ b/src/ComputeSharp.D2D1/Attributes/D2DOutputBuffer.cs @@ -0,0 +1,53 @@ +using System; + +namespace ComputeSharp.D2D1; + +/// +/// An attribute for a D2D1 shader with a description on the resulting output buffer. +/// Using this attribute is optional when defining a D2D1 shader. +/// +/// +/// For more info, see . +/// +[AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)] +public sealed class D2DOutputBufferAttribute : Attribute +{ + /// + /// Creates a new instance of the type with the specified arguments. + /// + /// The buffer precision for the resulting output buffer. + public D2DOutputBufferAttribute(D2D1BufferPrecision bufferPrecision) + { + BufferPrecision = bufferPrecision; + } + + /// + /// Creates a new instance of the type with the specified arguments. + /// + /// The channel depth for the resulting output buffer. + public D2DOutputBufferAttribute(D2D1ChannelDepth channelDepth) + { + ChannelDepth = channelDepth; + } + + /// + /// Creates a new instance of the type with the specified arguments. + /// + /// The buffer precision for the resulting output buffer. + /// The channel depth for the resulting output buffer. + public D2DOutputBufferAttribute(D2D1BufferPrecision bufferPrecision, D2D1ChannelDepth channelDepth) + { + BufferPrecision = bufferPrecision; + ChannelDepth = channelDepth; + } + + /// + /// Gets the buffer precision for the resulting output buffer. + /// + public D2D1BufferPrecision BufferPrecision { get; } + + /// + /// Gets the channel depth for the resulting output buffer. + /// + public D2D1ChannelDepth ChannelDepth { get; } +} diff --git a/src/ComputeSharp.D2D1/Attributes/D2DRequiresScenePositionAttribute.cs b/src/ComputeSharp.D2D1/Attributes/D2DRequiresScenePositionAttribute.cs index f61dbc35..f351bb93 100644 --- a/src/ComputeSharp.D2D1/Attributes/D2DRequiresScenePositionAttribute.cs +++ b/src/ComputeSharp.D2D1/Attributes/D2DRequiresScenePositionAttribute.cs @@ -9,7 +9,8 @@ namespace ComputeSharp.D2D1; /// /// /// This attribute directly maps to #define D2D_REQUIRES_SCENE_POSITION. -/// For more info, see . +/// For more info, see . +/// [AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)] public sealed class D2DRequiresScenePositionAttribute : Attribute { diff --git a/src/ComputeSharp.D2D1/Attributes/Enums/D2D1BufferPrecision.cs b/src/ComputeSharp.D2D1/Attributes/Enums/D2D1BufferPrecision.cs new file mode 100644 index 00000000..8b1bc9a8 --- /dev/null +++ b/src/ComputeSharp.D2D1/Attributes/Enums/D2D1BufferPrecision.cs @@ -0,0 +1,41 @@ +namespace ComputeSharp.D2D1; + +/// +/// Indicates the bit depth to use in the imaging pipeline in Direct2D to produce the result of a D2D1 shader. +/// +/// +/// This type exposes the available values in . +/// For more info, see . +/// +public enum D2D1BufferPrecision +{ + /// + /// The buffer precision is not specified. + /// + Unknown, + + /// + /// The buffer precision uses 8-bit normalized integer values per channel. + /// + Int8Normalized, + + /// + /// The buffer precision uses 8-bit normalized integer values per channel with standard RGB data. + /// + Int8NormalizedSRGB, + + /// + /// The buffer precision uses 16-bit normalized integer values per channel. + /// + Int16Normalized, + + /// + /// The buffer precision uses 16-bit float values per channel. + /// + Float16, + + /// + /// The buffer precision uses 32-bit float values per channel. + /// + Float32 +} diff --git a/src/ComputeSharp.D2D1/Attributes/Enums/D2D1ChannelDepth.cs b/src/ComputeSharp.D2D1/Attributes/Enums/D2D1ChannelDepth.cs new file mode 100644 index 00000000..73abb928 --- /dev/null +++ b/src/ComputeSharp.D2D1/Attributes/Enums/D2D1ChannelDepth.cs @@ -0,0 +1,26 @@ +namespace ComputeSharp.D2D1; + +/// +/// Indicates the channel depth of a stage in a Direct2D rendering pipeline. +/// +/// +/// This type exposes the available values in . +/// For more info, see . +/// +public enum D2D1ChannelDepth +{ + /// + /// The channel depth is the default, and it will be inherited by the inputs. + /// + Default, + + /// + /// The channel depth is 1. + /// + One, + + /// + /// The channel depth is 4. + /// + Four +} diff --git a/src/ComputeSharp.D2D1/Attributes/Profiles/D2D1ShaderProfile.cs b/src/ComputeSharp.D2D1/Attributes/Enums/D2D1ShaderProfile.cs similarity index 100% rename from src/ComputeSharp.D2D1/Attributes/Profiles/D2D1ShaderProfile.cs rename to src/ComputeSharp.D2D1/Attributes/Enums/D2D1ShaderProfile.cs From c554f56bfa40a73e83f8d0a6ed22092d3930e40d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 7 May 2022 17:09:30 +0200 Subject: [PATCH 2/6] Add ID2D1Shader and D2D1PixelShader output buffer APIs --- .../Interfaces/__Internals/ID2D1Shader.cs | 9 ++++++ .../Shaders/Interop/D2D1PixelShader.cs | 30 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs index b9d2593a..035d7b52 100644 --- a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs +++ b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs @@ -37,6 +37,15 @@ public interface ID2D1Shader [Obsolete("This method is not intended to be used directly by user code")] uint GetInputType(uint index); + /// + /// Gets the output buffer precision and depth for the shader. + /// + /// The output buffer precision. + /// The output buffer channel depth. + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This method is not intended to be used directly by user code")] + void GetOutputBuffer(out uint precision, out uint depth); + /// /// Loads the dispatch data for the shader. /// diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs index 54b4a683..1082ba7a 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs @@ -113,6 +113,36 @@ public static class D2D1PixelShader return (D2D1PixelShaderInputType)shader.GetInputType((uint)index); } + /// + /// Gets the buffer precision for the output buffer of a D2D1 pixel shader. + /// + /// The type of D2D1 pixel shader to get the output buffer precision for. + /// The output buffer precision for the D2D1 pixel shader of type . + public static D2D1BufferPrecision GetOutputBufferPrecision() + where T : unmanaged, ID2D1PixelShader + { + Unsafe.SkipInit(out T shader); + + shader.GetOutputBuffer(out uint precision, out _); + + return (D2D1BufferPrecision)precision; + } + + /// + /// Gets the channel depth for the output buffer of a D2D1 pixel shader. + /// + /// The type of D2D1 pixel shader to get the output buffer channel depth for. + /// The output buffer channel depth for the D2D1 pixel shader of type . + public static D2D1ChannelDepth GetOutputBufferChannelDepth() + where T : unmanaged, ID2D1PixelShader + { + Unsafe.SkipInit(out T shader); + + shader.GetOutputBuffer(out _, out uint depth); + + return (D2D1ChannelDepth)depth; + } + /// /// Gets the constant buffer from an input D2D1 pixel shader. /// From 80fea25b5ad0768435e3e6161c2637dbb368e2b9 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 7 May 2022 20:09:59 +0200 Subject: [PATCH 3/6] Enable generation for ID2D1Shader.GetOutputBuffer --- .../ComputeSharp.D2D1.SourceGenerators.csproj | 2 + ...ator.CreateBuildHlslSourceMethod.Syntax.cs | 2 +- ...erGenerator.CreateBuildHlslSourceMethod.cs | 10 +-- ...erator.CreateGetInputCountMethod.Syntax.cs | 2 +- ...nerator.CreateGetInputTypeMethod.Syntax.cs | 2 +- ...haderGenerator.CreateGetInputTypeMethod.cs | 2 +- ...ator.CreateGetOutputBufferMethod.Syntax.cs | 51 +++++++++++++++ ...erGenerator.CreateGetOutputBufferMethod.cs | 62 +++++++++++++++++++ ...InitializeFromDispatchDataMethod.Syntax.cs | 2 +- ...nerator.CreateLoadBytecodeMethod.Syntax.cs | 4 +- ...haderGenerator.CreateLoadBytecodeMethod.cs | 4 +- ...tor.CreateLoadDispatchDataMethod.Syntax.cs | 2 +- .../ID2D1ShaderGenerator.cs | 26 ++++++++ .../Models/OutputBufferInfo.cs | 8 +++ .../Extensions/ISymbolExtensions.cs | 11 ++++ 15 files changed, 171 insertions(+), 19 deletions(-) create mode 100644 src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetOutputBufferMethod.Syntax.cs create mode 100644 src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetOutputBufferMethod.cs create mode 100644 src/ComputeSharp.D2D1.SourceGenerators/Models/OutputBufferInfo.cs diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj index b68b1087..3ca380e3 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj +++ b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj @@ -65,6 +65,8 @@ + + diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs index 9676c21f..df18aab4 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs @@ -27,7 +27,7 @@ partial class ID2D1ShaderGenerator // hlslSource = ; // } return - MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier("BuildHlslSource")) + MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier(nameof(BuildHlslSource))) .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) .AddParameterListParameters( diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs index 86dcf802..577a87c4 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs @@ -400,15 +400,7 @@ partial class ID2D1ShaderGenerator /// Whether requires the scene position. private static bool GetRequiresScenePositionInfo(INamedTypeSymbol structDeclarationSymbol) { - foreach (AttributeData attributeData in structDeclarationSymbol.GetAttributes()) - { - if (attributeData.AttributeClass?.GetFullMetadataName() is "ComputeSharp.D2D1.D2DRequiresScenePositionAttribute") - { - return true; - } - } - - return false; + return structDeclarationSymbol.TryGetAttributeWithFullMetadataName("ComputeSharp.D2D1.D2DRequiresScenePositionAttribute", out _); } /// diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs index 7553f9b2..80028804 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs @@ -29,7 +29,7 @@ partial class ID2D1ShaderGenerator // return ; // } return - MethodDeclaration(PredefinedType(Token(SyntaxKind.UIntKeyword)), Identifier("GetInputCount")) + MethodDeclaration(PredefinedType(Token(SyntaxKind.UIntKeyword)), Identifier(nameof(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 ce8db4a9..fcf9c5cc 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.Syntax.cs @@ -28,7 +28,7 @@ partial class ID2D1ShaderGenerator // return ; // } return - MethodDeclaration(PredefinedType(Token(SyntaxKind.UIntKeyword)), Identifier("GetInputType")) + MethodDeclaration(PredefinedType(Token(SyntaxKind.UIntKeyword)), Identifier(nameof(GetInputType))) .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) .AddParameterListParameters(Parameter(Identifier("index")).WithType(PredefinedType(Token(SyntaxKind.UIntKeyword)))) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs index 926345cb..f4798fdf 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs @@ -12,7 +12,7 @@ namespace ComputeSharp.D2D1.SourceGenerators; partial class ID2D1ShaderGenerator { /// - /// A helper with all logic to generate the GetInputTypeMethod method. + /// A helper with all logic to generate the GetInputType method. /// private static partial class GetInputType { diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetOutputBufferMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetOutputBufferMethod.Syntax.cs new file mode 100644 index 00000000..427d89b7 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetOutputBufferMethod.Syntax.cs @@ -0,0 +1,51 @@ +using ComputeSharp.D2D1.__Internals; +using ComputeSharp.D2D1.SourceGenerators.Models; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +#pragma warning disable CS0618 + +namespace ComputeSharp.D2D1.SourceGenerators; + +/// +partial class ID2D1ShaderGenerator +{ + /// + partial class GetOutputBuffer + { + /// + /// Creates a instance for the GetOutputBuffer method. + /// + /// The input info for the shader output buffer. + /// The resulting instance for the GetOutputBuffer method. + public static MethodDeclarationSyntax GetSyntax(OutputBufferInfo info) + { + // This code produces a method declaration as follows: + // + // readonly void global::ComputeSharp.D2D1.__Internals.ID2D1Shader.GetOutputBuffer(out uint precision, out uint depth) + // { + // precision = ; + // depth = ; + // } + return + MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier(nameof(GetOutputBuffer))) + .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) + .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) + .AddParameterListParameters( + Parameter(Identifier("precision")).AddModifiers(Token(SyntaxKind.OutKeyword)).WithType(PredefinedType(Token(SyntaxKind.UIntKeyword))), + Parameter(Identifier("depth")).AddModifiers(Token(SyntaxKind.OutKeyword)).WithType(PredefinedType(Token(SyntaxKind.UIntKeyword)))) + .WithBody(Block( + ExpressionStatement( + AssignmentExpression( + SyntaxKind.SimpleAssignmentExpression, + IdentifierName("precision"), + LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal((int)info.BufferPrecision)))), + ExpressionStatement( + AssignmentExpression( + SyntaxKind.SimpleAssignmentExpression, + IdentifierName("depth"), + LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal((int)info.ChannelDepth)))))); + } + } +} diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetOutputBufferMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetOutputBufferMethod.cs new file mode 100644 index 00000000..f0af8afe --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetOutputBufferMethod.cs @@ -0,0 +1,62 @@ +using ComputeSharp.SourceGeneration.Extensions; +using Microsoft.CodeAnalysis; + +namespace ComputeSharp.D2D1.SourceGenerators; + +/// +partial class ID2D1ShaderGenerator +{ + /// + /// A helper with all logic to generate the GetOutputBuffer method. + /// + private static partial class GetOutputBuffer + { + /// + /// Extracts the output buffer info for the current shader. + /// + /// The input instance to process. + /// The output buffer precision for the shader. + /// The output buffer channel depth for the shader. + public static void GetInfo( + INamedTypeSymbol structDeclarationSymbol, + out D2D1BufferPrecision bufferPrecision, + out D2D1ChannelDepth channelDepth) + { + // Try to get the [D2DOutputBuffer] attribute + if (!structDeclarationSymbol.TryGetAttributeWithFullMetadataName("ComputeSharp.D2D1.D2DOutputBufferAttribute", out AttributeData? attributeData)) + { + bufferPrecision = D2D1BufferPrecision.Unknown; + channelDepth = D2D1ChannelDepth.Default; + + return; + } + + // Extract the buffer precision and channel depth, if available + if (attributeData!.ConstructorArguments.Length == 1) + { + if (attributeData.AttributeConstructor is IMethodSymbol { Parameters.Length: 1 } constructorSymbol && + constructorSymbol.Parameters[0].Type.HasFullyQualifiedName("ComputeSharp.D2D1.D2D1BufferPrecision")) + { + bufferPrecision = (D2D1BufferPrecision)attributeData.ConstructorArguments[0].Value!; + channelDepth = D2D1ChannelDepth.Default; + } + else + { + bufferPrecision = D2D1BufferPrecision.Unknown; + channelDepth = (D2D1ChannelDepth)attributeData.ConstructorArguments[0].Value!; + } + } + else if (attributeData.ConstructorArguments.Length == 2) + { + bufferPrecision = (D2D1BufferPrecision)attributeData.ConstructorArguments[0].Value!; + channelDepth = (D2D1ChannelDepth)attributeData.ConstructorArguments[1].Value!; + } + else + { + // The syntax tree is likely invalid, so just ignore it for now + bufferPrecision = D2D1BufferPrecision.Unknown; + channelDepth = D2D1ChannelDepth.Default; + } + } + } +} diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInitializeFromDispatchDataMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInitializeFromDispatchDataMethod.Syntax.cs index 9e6816a5..ec17de8f 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInitializeFromDispatchDataMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInitializeFromDispatchDataMethod.Syntax.cs @@ -29,7 +29,7 @@ partial class ID2D1ShaderGenerator // // } return - MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier("InitializeFromDispatchData")) + MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier(nameof(InitializeFromDispatchData))) .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) .AddParameterListParameters(Parameter(Identifier("data")).WithType( GenericName(Identifier("global::System.ReadOnlySpan")) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs index 069e1ec0..fcb97fa9 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs @@ -44,9 +44,7 @@ partial class ID2D1ShaderGenerator // // } return - MethodDeclaration( - PredefinedType(Token(SyntaxKind.VoidKeyword)), - Identifier("LoadBytecode")) + MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier(nameof(LoadBytecode))) .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) .AddTypeParameterListParameters(TypeParameter(Identifier("TLoader"))) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs index ca1ba01a..2c24a603 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs @@ -21,7 +21,9 @@ namespace ComputeSharp.D2D1.SourceGenerators; /// partial class ID2D1ShaderGenerator { - /// + /// + /// A helper with all logic to generate the LoadBytecode method. + /// private static partial class LoadBytecode { /// diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs index c167d7b6..0904600b 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs @@ -29,7 +29,7 @@ partial class ID2D1ShaderGenerator // // } return - MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier("LoadDispatchData")) + MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier(nameof(LoadDispatchData))) .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) .AddTypeParameterListParameters(TypeParameter(Identifier("TLoader"))) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 12cd2d3b..a972cbf5 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -243,5 +243,31 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(LoadBytecode)}", text); }); + + // Get the output buffer data, which can be computed separately from all other generation steps. + // Caching at a fine grained level can be enabled immediately, as both models are comparable. + IncrementalValuesProvider<(HierarchyInfo Hierarchy, OutputBufferInfo Info)> outputBufferInfo = + shaderDeclarations + .Select(static (item, token) => + { + // GetOutputBuffer() info + GetOutputBuffer.GetInfo(item.Symbol, out D2D1BufferPrecision bufferPrecision, out D2D1ChannelDepth channelDepth); + + token.ThrowIfCancellationRequested(); + + OutputBufferInfo outputBufferInfo = new(bufferPrecision, channelDepth); + + return (item.Hierarchy, outputBufferInfo); + }) + .WithComparers(HierarchyInfo.Comparer.Default, EqualityComparer.Default); + + // Generate the GetOutputBuffer() methods + context.RegisterSourceOutput(outputBufferInfo, static (context, item) => + { + MethodDeclarationSyntax getOutputBufferMethod = GetOutputBuffer.GetSyntax(item.Info); + CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, getOutputBufferMethod, false); + + context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(GetOutputBuffer)}", compilationUnit.ToFullString()); + }); } } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/OutputBufferInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/OutputBufferInfo.cs new file mode 100644 index 00000000..fc821cae --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/OutputBufferInfo.cs @@ -0,0 +1,8 @@ +namespace ComputeSharp.D2D1.SourceGenerators.Models; + +/// +/// A model representing the output buffer info for a shader. +/// +/// The buffer precision for the resulting output buffer. +/// The channel depth for the resulting output buffer. +internal sealed record OutputBufferInfo(D2D1BufferPrecision BufferPrecision, D2D1ChannelDepth ChannelDepth); diff --git a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs index 404a9da3..d12cb721 100644 --- a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs +++ b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs @@ -40,6 +40,17 @@ internal static class ISymbolExtensions return symbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat); } + /// + /// Checks whether or not a given type symbol has a specified full name. + /// + /// The input instance to check. + /// The full name to check. + /// Whether has a full name equals to . + public static bool HasFullyQualifiedName(this ISymbol symbol, string name) + { + return symbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat) == name; + } + /// /// Gets a valid filename for a given instance. /// From e292eb6d9d96fd55ca72f819cca771c9577f5fd1 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 7 May 2022 20:18:20 +0200 Subject: [PATCH 4/6] Add unit tests for new buffer output APIs --- .../D2D1InteropServicesTests.cs | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1InteropServicesTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1InteropServicesTests.cs index c797a527..fa89d21e 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1InteropServicesTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1InteropServicesTests.cs @@ -42,4 +42,61 @@ public partial class D2D1InteropServicesTests return 0; } } + + [TestMethod] + public unsafe void GetOutputBufferPrecision() + { + Assert.AreEqual(D2D1PixelShader.GetOutputBufferPrecision(), D2D1BufferPrecision.Unknown); + Assert.AreEqual(D2D1PixelShader.GetOutputBufferPrecision(), D2D1BufferPrecision.Int16Normalized); + Assert.AreEqual(D2D1PixelShader.GetOutputBufferPrecision(), D2D1BufferPrecision.Unknown); + Assert.AreEqual(D2D1PixelShader.GetOutputBufferPrecision(), D2D1BufferPrecision.Int8NormalizedSRGB); + } + + [TestMethod] + public unsafe void GetOutputBufferChannelDepth() + { + Assert.AreEqual(D2D1PixelShader.GetOutputBufferChannelDepth(), D2D1ChannelDepth.Default); + Assert.AreEqual(D2D1PixelShader.GetOutputBufferChannelDepth(), D2D1ChannelDepth.Default); + Assert.AreEqual(D2D1PixelShader.GetOutputBufferChannelDepth(), D2D1ChannelDepth.Four); + Assert.AreEqual(D2D1PixelShader.GetOutputBufferChannelDepth(), D2D1ChannelDepth.One); + } + + [D2DInputCount(0)] + partial struct EmptyShader : ID2D1PixelShader + { + public Float4 Execute() + { + return 0; + } + } + + [D2DInputCount(0)] + [D2DOutputBuffer(D2D1BufferPrecision.Int16Normalized)] + partial struct OnlyBufferPrecisionShader : ID2D1PixelShader + { + public Float4 Execute() + { + return 0; + } + } + + [D2DInputCount(0)] + [D2DOutputBuffer(D2D1ChannelDepth.Four)] + partial struct OnlyChannelDepthShader : ID2D1PixelShader + { + public Float4 Execute() + { + return 0; + } + } + + [D2DInputCount(0)] + [D2DOutputBuffer(D2D1BufferPrecision.Int8NormalizedSRGB, D2D1ChannelDepth.One)] + partial struct CustomBufferOutputShader : ID2D1PixelShader + { + public Float4 Execute() + { + return 0; + } + } } \ No newline at end of file From 966f5ee06dd6ccb88c96f1ca0ac1312889d37585 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 7 May 2022 22:34:27 +0200 Subject: [PATCH 5/6] Enable output buffer configuration in D2D1PixelShaderEffect --- .../Attributes/Enums/D2D1ChannelDepth.cs | 6 +++--- .../Effects/PixelShaderEffect.For{T}.cs | 16 ++++++++++++++++ ...lShaderEffect.ID2D1DrawTransformMethods.cs | 19 ++++++++++++++++++- .../Interop/Effects/PixelShaderEffect.cs | 16 ++++++++++++++++ .../um/d2d1_1/D2D1_BUFFER_PRECISION.cs | 18 ++++++++++++++++++ .../DirectX/um/d2d1_1/D2D1_CHANNEL_DEPTH.cs | 15 +++++++++++++++ .../um/d2d1effectauthor/ID2D1DrawInfo.cs | 7 +++++++ .../TerraFX.Interop.Windows.D2D1.projitems | 2 ++ 8 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/D2D1_BUFFER_PRECISION.cs create mode 100644 src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/D2D1_CHANNEL_DEPTH.cs diff --git a/src/ComputeSharp.D2D1/Attributes/Enums/D2D1ChannelDepth.cs b/src/ComputeSharp.D2D1/Attributes/Enums/D2D1ChannelDepth.cs index 73abb928..e712cce0 100644 --- a/src/ComputeSharp.D2D1/Attributes/Enums/D2D1ChannelDepth.cs +++ b/src/ComputeSharp.D2D1/Attributes/Enums/D2D1ChannelDepth.cs @@ -12,15 +12,15 @@ public enum D2D1ChannelDepth /// /// The channel depth is the default, and it will be inherited by the inputs. /// - Default, + Default = 0, /// /// The channel depth is 1. /// - One, + One = 1, /// /// The channel depth is 4. /// - Four + Four = 4 } diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs index 1afb2a41..576b567b 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs @@ -64,6 +64,16 @@ internal unsafe partial struct PixelShaderEffect /// private static int bytecodeSize; + /// + /// The buffer precision for the resulting output buffer. + /// + private static D2D1BufferPrecision bufferPrecision; + + /// + /// The channel depth for the resulting output buffer. + /// + private static D2D1ChannelDepth channelDepth; + /// /// The factory of instances to use for each created effect. /// @@ -97,6 +107,8 @@ internal unsafe partial struct PixelShaderEffect ReadOnlyMemory buffer = D2D1PixelShader.LoadBytecode(); int bytecodeSize = buffer.Length; byte* bytecode = (byte*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(For), bytecodeSize); + D2D1BufferPrecision bufferPrecision = D2D1PixelShader.GetOutputBufferPrecision(); + D2D1ChannelDepth channelDepth = D2D1PixelShader.GetOutputBufferChannelDepth(); // Prepare the inputs info int inputCount = D2D1PixelShader.GetInputCount(); @@ -116,6 +128,8 @@ internal unsafe partial struct PixelShaderEffect For.inputTypes = inputTypes; For.bytecode = bytecode; For.bytecodeSize = bytecodeSize; + For.bufferPrecision = bufferPrecision; + For.channelDepth = channelDepth; For.d2D1DrawTransformMapperFactory = d2D1DrawTransformMapperFactory; isInitialized = true; @@ -195,6 +209,8 @@ internal unsafe partial struct PixelShaderEffect inputTypes, bytecode, bytecodeSize, + bufferPrecision, + channelDepth, d2D1TransformMapper, effectImpl); } diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs index 211cefcd..fbfbb0c5 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs @@ -323,7 +323,24 @@ partial struct PixelShaderEffect @this->d2D1DrawInfo = drawInfo; - return drawInfo->SetPixelShader(&@this->shaderId); + // Set the pixel shader for the effect + HRESULT hresult = drawInfo->SetPixelShader(&@this->shaderId); + + if (hresult != S.S_OK) + { + return hresult; + } + + // If a custom buffer precision or channel depth is requested, set it + if (@this->bufferPrecision != D2D1BufferPrecision.Unknown || + @this->channelDepth != D2D1ChannelDepth.Default) + { + return drawInfo->SetOutputBuffer( + (D2D1_BUFFER_PRECISION)@this->bufferPrecision, + (D2D1_CHANNEL_DEPTH)@this->channelDepth); + } + + return S.S_OK; } } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs index 8cbf824e..af5e1537 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs @@ -171,6 +171,16 @@ internal unsafe partial struct PixelShaderEffect /// private int bytecodeSize; + /// + /// The buffer precision for the resulting output buffer. + /// + private D2D1BufferPrecision bufferPrecision; + + /// + /// The channel depth for the resulting output buffer. + /// + private D2D1ChannelDepth channelDepth; + /// /// The handle for the instance in use, if any. /// @@ -189,6 +199,8 @@ internal unsafe partial struct PixelShaderEffect /// The buffer with the types of inputs for the shader. /// The shader bytecode. /// The size of . + /// The buffer precision for the resulting output buffer. + /// The channel depth for the resulting output buffer. /// The instance to use for the effect. /// The resulting effect instance. /// This always returns 0. @@ -198,6 +210,8 @@ internal unsafe partial struct PixelShaderEffect D2D1PixelShaderInputType* inputTypes, byte* bytecode, int bytecodeSize, + D2D1BufferPrecision bufferPrecision, + D2D1ChannelDepth channelDepth, D2D1TransformMapper? d2D1TransformMapper, IUnknown** effectImpl) { @@ -213,6 +227,8 @@ internal unsafe partial struct PixelShaderEffect @this->inputTypes = inputTypes; @this->bytecode = bytecode; @this->bytecodeSize = bytecodeSize; + @this->bufferPrecision = bufferPrecision; + @this->channelDepth = channelDepth; @this->d2D1TransformMapperHandle = GCHandle.Alloc(d2D1TransformMapper); *effectImpl = (IUnknown*)@this; diff --git a/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/D2D1_BUFFER_PRECISION.cs b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/D2D1_BUFFER_PRECISION.cs new file mode 100644 index 00000000..f8aab853 --- /dev/null +++ b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/D2D1_BUFFER_PRECISION.cs @@ -0,0 +1,18 @@ +// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +// Ported from um/d2d1_1.h in the Windows SDK for Windows 10.0.22000.0 +// Original source is Copyright © Microsoft. All rights reserved. + +namespace TerraFX.Interop.DirectX +{ + internal enum D2D1_BUFFER_PRECISION : uint + { + D2D1_BUFFER_PRECISION_UNKNOWN = 0, + D2D1_BUFFER_PRECISION_8BPC_UNORM = 1, + D2D1_BUFFER_PRECISION_8BPC_UNORM_SRGB = 2, + D2D1_BUFFER_PRECISION_16BPC_UNORM = 3, + D2D1_BUFFER_PRECISION_16BPC_FLOAT = 4, + D2D1_BUFFER_PRECISION_32BPC_FLOAT = 5, + D2D1_BUFFER_PRECISION_FORCE_DWORD = 0xffffffff + } +} \ No newline at end of file diff --git a/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/D2D1_CHANNEL_DEPTH.cs b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/D2D1_CHANNEL_DEPTH.cs new file mode 100644 index 00000000..3189cb39 --- /dev/null +++ b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/D2D1_CHANNEL_DEPTH.cs @@ -0,0 +1,15 @@ +// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +// Ported from um/d2d1_1.h in the Windows SDK for Windows 10.0.22000.0 +// Original source is Copyright © Microsoft. All rights reserved. + +namespace TerraFX.Interop.DirectX +{ + internal enum D2D1_CHANNEL_DEPTH : uint + { + D2D1_CHANNEL_DEPTH_DEFAULT = 0, + D2D1_CHANNEL_DEPTH_1 = 1, + D2D1_CHANNEL_DEPTH_4 = 4, + D2D1_CHANNEL_DEPTH_FORCE_DWORD = 0xffffffff + } +} \ No newline at end of file diff --git a/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1effectauthor/ID2D1DrawInfo.cs b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1effectauthor/ID2D1DrawInfo.cs index 2c94d473..76105584 100644 --- a/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1effectauthor/ID2D1DrawInfo.cs +++ b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1effectauthor/ID2D1DrawInfo.cs @@ -20,6 +20,13 @@ namespace TerraFX.Interop.DirectX { public void** lpVtbl; + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [VtblIndex(4)] + public HRESULT SetOutputBuffer(D2D1_BUFFER_PRECISION bufferPrecision, D2D1_CHANNEL_DEPTH channelDepth) + { + return ((delegate* unmanaged[Stdcall])(lpVtbl[4]))((ID2D1DrawInfo*)Unsafe.AsPointer(ref this), bufferPrecision, channelDepth); + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] [VtblIndex(7)] public HRESULT SetPixelShaderConstantBuffer([NativeTypeName("const BYTE *")] byte* buffer, [NativeTypeName("UINT32")] uint bufferCount) diff --git a/src/TerraFX.Interop.Windows.D2D1/TerraFX.Interop.Windows.D2D1.projitems b/src/TerraFX.Interop.Windows.D2D1/TerraFX.Interop.Windows.D2D1.projitems index 942865b5..70a7135d 100644 --- a/src/TerraFX.Interop.Windows.D2D1/TerraFX.Interop.Windows.D2D1.projitems +++ b/src/TerraFX.Interop.Windows.D2D1/TerraFX.Interop.Windows.D2D1.projitems @@ -19,6 +19,8 @@ + + From e81a39d27aced6e052ab8424d8fa9622a88d683e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 7 May 2022 23:45:41 +0200 Subject: [PATCH 6/6] Set D2D1 enum values to be the same as D2D1 ones Also some minor tweaks to the enums --- .../Attributes/Enums/D2D1BufferPrecision.cs | 16 +++++++++------- .../Attributes/Enums/D2D1ChannelDepth.cs | 12 +++++++----- .../D2D1InteropServicesTests.cs | 8 ++++---- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/ComputeSharp.D2D1/Attributes/Enums/D2D1BufferPrecision.cs b/src/ComputeSharp.D2D1/Attributes/Enums/D2D1BufferPrecision.cs index 8b1bc9a8..55c0bfd4 100644 --- a/src/ComputeSharp.D2D1/Attributes/Enums/D2D1BufferPrecision.cs +++ b/src/ComputeSharp.D2D1/Attributes/Enums/D2D1BufferPrecision.cs @@ -1,4 +1,6 @@ -namespace ComputeSharp.D2D1; +using static TerraFX.Interop.DirectX.D2D1_BUFFER_PRECISION; + +namespace ComputeSharp.D2D1; /// /// Indicates the bit depth to use in the imaging pipeline in Direct2D to produce the result of a D2D1 shader. @@ -12,30 +14,30 @@ public enum D2D1BufferPrecision /// /// The buffer precision is not specified. /// - Unknown, + Unknown = (int)D2D1_BUFFER_PRECISION_UNKNOWN, /// /// The buffer precision uses 8-bit normalized integer values per channel. /// - Int8Normalized, + UInt8Normalized = (int)D2D1_BUFFER_PRECISION_8BPC_UNORM, /// /// The buffer precision uses 8-bit normalized integer values per channel with standard RGB data. /// - Int8NormalizedSRGB, + UInt8NormalizedSrgb = (int)D2D1_BUFFER_PRECISION_8BPC_UNORM_SRGB, /// /// The buffer precision uses 16-bit normalized integer values per channel. /// - Int16Normalized, + UInt16Normalized = (int)D2D1_BUFFER_PRECISION_16BPC_UNORM, /// /// The buffer precision uses 16-bit float values per channel. /// - Float16, + Float16 = (int)D2D1_BUFFER_PRECISION_16BPC_FLOAT, /// /// The buffer precision uses 32-bit float values per channel. /// - Float32 + Float32 = (int)D2D1_BUFFER_PRECISION_32BPC_FLOAT } diff --git a/src/ComputeSharp.D2D1/Attributes/Enums/D2D1ChannelDepth.cs b/src/ComputeSharp.D2D1/Attributes/Enums/D2D1ChannelDepth.cs index e712cce0..a83794ec 100644 --- a/src/ComputeSharp.D2D1/Attributes/Enums/D2D1ChannelDepth.cs +++ b/src/ComputeSharp.D2D1/Attributes/Enums/D2D1ChannelDepth.cs @@ -1,4 +1,6 @@ -namespace ComputeSharp.D2D1; +using static TerraFX.Interop.DirectX.D2D1_CHANNEL_DEPTH; + +namespace ComputeSharp.D2D1; /// /// Indicates the channel depth of a stage in a Direct2D rendering pipeline. @@ -10,17 +12,17 @@ public enum D2D1ChannelDepth { /// - /// The channel depth is the default, and it will be inherited by the inputs. + /// The channel depth is the default, and it will be inherited from the inputs. /// - Default = 0, + Default = (int)D2D1_CHANNEL_DEPTH_DEFAULT, /// /// The channel depth is 1. /// - One = 1, + One = (int)D2D1_CHANNEL_DEPTH_1, /// /// The channel depth is 4. /// - Four = 4 + Four = (int)D2D1_CHANNEL_DEPTH_4 } diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1InteropServicesTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1InteropServicesTests.cs index fa89d21e..3e6d1919 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1InteropServicesTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1InteropServicesTests.cs @@ -47,9 +47,9 @@ public partial class D2D1InteropServicesTests public unsafe void GetOutputBufferPrecision() { Assert.AreEqual(D2D1PixelShader.GetOutputBufferPrecision(), D2D1BufferPrecision.Unknown); - Assert.AreEqual(D2D1PixelShader.GetOutputBufferPrecision(), D2D1BufferPrecision.Int16Normalized); + Assert.AreEqual(D2D1PixelShader.GetOutputBufferPrecision(), D2D1BufferPrecision.UInt16Normalized); Assert.AreEqual(D2D1PixelShader.GetOutputBufferPrecision(), D2D1BufferPrecision.Unknown); - Assert.AreEqual(D2D1PixelShader.GetOutputBufferPrecision(), D2D1BufferPrecision.Int8NormalizedSRGB); + Assert.AreEqual(D2D1PixelShader.GetOutputBufferPrecision(), D2D1BufferPrecision.UInt8NormalizedSrgb); } [TestMethod] @@ -71,7 +71,7 @@ public partial class D2D1InteropServicesTests } [D2DInputCount(0)] - [D2DOutputBuffer(D2D1BufferPrecision.Int16Normalized)] + [D2DOutputBuffer(D2D1BufferPrecision.UInt16Normalized)] partial struct OnlyBufferPrecisionShader : ID2D1PixelShader { public Float4 Execute() @@ -91,7 +91,7 @@ public partial class D2D1InteropServicesTests } [D2DInputCount(0)] - [D2DOutputBuffer(D2D1BufferPrecision.Int8NormalizedSRGB, D2D1ChannelDepth.One)] + [D2DOutputBuffer(D2D1BufferPrecision.UInt8NormalizedSrgb, D2D1ChannelDepth.One)] partial struct CustomBufferOutputShader : ID2D1PixelShader { public Float4 Execute()