Merge pull request #266 from Sergio0694/feature/d2d-output-buffer-attribute

Add [D2DOutputBuffer] attribute and infrastructure
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2022-05-08 00:17:11 +02:00
コミット df9454caf2
34ファイル変更
+493 -39

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

+3 -1
ファイルの表示
@@ -64,7 +64,9 @@
<Compile Include="..\ComputeSharp\Shaders\Dispatching\__Internals\ArrayPoolStringBuilder.cs" Link="ComputeSharp\Shaders\Dispatching\__Internals\ArrayPoolStringBuilder.cs" />
<!-- ComputeSharp.D2D1 APIs -->
<Compile Include="..\ComputeSharp.D2D1\Attributes\Profiles\D2D1ShaderProfile.cs" Link="ComputeSharp.D2D1\Attributes\Profiles\D2D1ShaderProfile.cs" />
<Compile Include="..\ComputeSharp.D2D1\Attributes\Enums\D2D1ShaderProfile.cs" Link="ComputeSharp.D2D1\Attributes\Enums\D2D1ShaderProfile.cs" />
<Compile Include="..\ComputeSharp.D2D1\Attributes\Enums\D2D1BufferPrecision.cs" Link="ComputeSharp.D2D1\Attributes\Enums\D2D1BufferPrecision.cs" />
<Compile Include="..\ComputeSharp.D2D1\Attributes\Enums\D2D1ChannelDepth.cs" Link="ComputeSharp.D2D1\Attributes\Enums\D2D1ChannelDepth.cs" />
<Compile Include="..\ComputeSharp.D2D1\Attributes\D2DEmbeddedBytecodeAttribute.cs" Link="ComputeSharp.D2D1\Attributes\D2DEmbeddedBytecodeAttribute.cs" />
<Compile Include="..\ComputeSharp.D2D1\Attributes\D2DInputComplexAttribute.cs" Link="ComputeSharp.D2D1\Attributes\D2DInputComplexAttribute.cs" />
<Compile Include="..\ComputeSharp.D2D1\Attributes\D2DInputCountAttribute.cs" Link="ComputeSharp.D2D1\Attributes\D2DInputCountAttribute.cs" />
@@ -27,7 +27,7 @@ partial class ID2D1ShaderGenerator
// hlslSource = <HLSL_SOURCE>;
// }
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(
@@ -400,15 +400,7 @@ partial class ID2D1ShaderGenerator
/// <remarks>Whether <paramref name="structDeclarationSymbol"/> requires the scene position.</remarks>
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 _);
}
/// <summary>
@@ -29,7 +29,7 @@ partial class ID2D1ShaderGenerator
// return <INPUT_COUNT>;
// }
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)))));
@@ -28,7 +28,7 @@ partial class ID2D1ShaderGenerator
// return <INPUT_TYPE>;
// }
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))))
@@ -12,7 +12,7 @@ namespace ComputeSharp.D2D1.SourceGenerators;
partial class ID2D1ShaderGenerator
{
/// <summary>
/// A helper with all logic to generate the <c>GetInputTypeMethod</c> method.
/// A helper with all logic to generate the <c>GetInputType</c> method.
/// </summary>
private static partial class GetInputType
{
@@ -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;
/// <inheritdoc/>
partial class ID2D1ShaderGenerator
{
/// <inheritdoc/>
partial class GetOutputBuffer
{
/// <summary>
/// Creates a <see cref="MethodDeclarationSyntax"/> instance for the <c>GetOutputBuffer</c> method.
/// </summary>
/// <param name="info">The input info for the shader output buffer.</param>
/// <returns>The resulting <see cref="MethodDeclarationSyntax"/> instance for the <c>GetOutputBuffer</c> method.</returns>
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 = <BUFFER_PRECISION>;
// depth = <CHANNEL_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))))));
}
}
}
@@ -0,0 +1,62 @@
using ComputeSharp.SourceGeneration.Extensions;
using Microsoft.CodeAnalysis;
namespace ComputeSharp.D2D1.SourceGenerators;
/// <inheritdoc/>
partial class ID2D1ShaderGenerator
{
/// <summary>
/// A helper with all logic to generate the <c>GetOutputBuffer</c> method.
/// </summary>
private static partial class GetOutputBuffer
{
/// <summary>
/// Extracts the output buffer info for the current shader.
/// </summary>
/// <param name="structDeclarationSymbol">The input <see cref="INamedTypeSymbol"/> instance to process.</param>
/// <param name="bufferPrecision">The output buffer precision for the shader.</param>
/// <param name="channelDepth">The output buffer channel depth for the shader.</param>
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;
}
}
}
}
@@ -29,7 +29,7 @@ partial class ID2D1ShaderGenerator
// <BODY>
// }
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"))
@@ -44,9 +44,7 @@ partial class ID2D1ShaderGenerator
// <BODY>
// }
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")))
@@ -21,7 +21,9 @@ namespace ComputeSharp.D2D1.SourceGenerators;
/// <inheritdoc/>
partial class ID2D1ShaderGenerator
{
/// <inheritodoc/>
/// <summary>
/// A helper with all logic to generate the <c>LoadBytecode</c> method.
/// </summary>
private static partial class LoadBytecode
{
/// <summary>
@@ -29,7 +29,7 @@ partial class ID2D1ShaderGenerator
// <BODY>
// }
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")))
+26
ファイルの表示
@@ -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<OutputBufferInfo>.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());
});
}
}
+8
ファイルの表示
@@ -0,0 +1,8 @@
namespace ComputeSharp.D2D1.SourceGenerators.Models;
/// <summary>
/// A model representing the output buffer info for a shader.
/// </summary>
/// <param name="BufferPrecision">The buffer precision for the resulting output buffer.</param>
/// <param name="ChannelDepth">The channel depth for the resulting output buffer.</param>
internal sealed record OutputBufferInfo(D2D1BufferPrecision BufferPrecision, D2D1ChannelDepth ChannelDepth);
+1 -14
ファイルの表示
@@ -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
/// <param name="shaderProfile">The target shader profile to use to compile the shader.</param>
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<D2D1ShaderProfile>(nameof(shaderProfile), "Invalid shader profile value.")
};
#endif
ShaderProfile = shaderProfile;
}
/// <summary>
+2 -1
ファイルの表示
@@ -8,7 +8,8 @@ namespace ComputeSharp.D2D1;
/// </summary>
/// <remarks>
/// <para>This attribute directly maps to <c>#define D2D_INPUT&lt;N&gt;_COMPLEX</c>.</para>
/// <para>For more info, see <see href="https://docs.microsoft.com/en-us/windows/win32/direct2d/hlsl-helpers"/>.</para></remarks>
/// <para>For more info, see <see href="https://docs.microsoft.com/en-us/windows/win32/direct2d/hlsl-helpers"/>.</para>
/// </remarks>
[AttributeUsage(AttributeTargets.Struct, AllowMultiple = true)]
public sealed class D2DInputComplexAttribute : Attribute
{
+2 -1
ファイルの表示
@@ -8,7 +8,8 @@ namespace ComputeSharp.D2D1;
/// </summary>
/// <remarks>
/// <para>This attribute directly maps to <c>#define D2D_INPUT_COUNT &lt;N&gt;</c>.</para>
/// <para>For more info, see <see href="https://docs.microsoft.com/en-us/windows/win32/direct2d/hlsl-helpers"/>.</para></remarks>
/// <para>For more info, see <see href="https://docs.microsoft.com/en-us/windows/win32/direct2d/hlsl-helpers"/>.</para>
/// </remarks>
[AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)]
public sealed class D2DInputCountAttribute : Attribute
{
+2 -1
ファイルの表示
@@ -8,7 +8,8 @@ namespace ComputeSharp.D2D1;
/// </summary>
/// <remarks>
/// <para>This attribute directly maps to <c>#define D2D_INPUT&lt;N&gt;_SIMPLE</c>.</para>
/// <para>For more info, see <see href="https://docs.microsoft.com/en-us/windows/win32/direct2d/hlsl-helpers"/>.</para></remarks>
/// <para>For more info, see <see href="https://docs.microsoft.com/en-us/windows/win32/direct2d/hlsl-helpers"/>.</para>
/// </remarks>
[AttributeUsage(AttributeTargets.Struct, AllowMultiple = true)]
public sealed class D2DInputSimpleAttribute : Attribute
{
+53
ファイルの表示
@@ -0,0 +1,53 @@
using System;
namespace ComputeSharp.D2D1;
/// <summary>
/// An attribute for a D2D1 shader with a description on the resulting output buffer.
/// Using this attribute is optional when defining a D2D1 shader.
/// </summary>
/// <remarks>
/// For more info, see <see href="https://docs.microsoft.com/en-us/windows/win32/direct2d/precision-and-clipping-in-effect-graphs"/>.
/// </remarks>
[AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)]
public sealed class D2DOutputBufferAttribute : Attribute
{
/// <summary>
/// Creates a new instance of the <see cref="D2DOutputBufferAttribute"/> type with the specified arguments.
/// </summary>
/// <param name="bufferPrecision">The buffer precision for the resulting output buffer.</param>
public D2DOutputBufferAttribute(D2D1BufferPrecision bufferPrecision)
{
BufferPrecision = bufferPrecision;
}
/// <summary>
/// Creates a new instance of the <see cref="D2DOutputBufferAttribute"/> type with the specified arguments.
/// </summary>
/// <param name="channelDepth">The channel depth for the resulting output buffer.</param>
public D2DOutputBufferAttribute(D2D1ChannelDepth channelDepth)
{
ChannelDepth = channelDepth;
}
/// <summary>
/// Creates a new instance of the <see cref="D2DOutputBufferAttribute"/> type with the specified arguments.
/// </summary>
/// <param name="bufferPrecision">The buffer precision for the resulting output buffer.</param>
/// <param name="channelDepth">The channel depth for the resulting output buffer.</param>
public D2DOutputBufferAttribute(D2D1BufferPrecision bufferPrecision, D2D1ChannelDepth channelDepth)
{
BufferPrecision = bufferPrecision;
ChannelDepth = channelDepth;
}
/// <summary>
/// Gets the buffer precision for the resulting output buffer.
/// </summary>
public D2D1BufferPrecision BufferPrecision { get; }
/// <summary>
/// Gets the channel depth for the resulting output buffer.
/// </summary>
public D2D1ChannelDepth ChannelDepth { get; }
}
+2 -1
ファイルの表示
@@ -9,7 +9,8 @@ namespace ComputeSharp.D2D1;
/// </summary>
/// <remarks>
/// <para>This attribute directly maps to <c>#define D2D_REQUIRES_SCENE_POSITION</c>.</para>
/// <para>For more info, see <see href="https://docs.microsoft.com/en-us/windows/win32/direct2d/hlsl-helpers"/>.</para></remarks>
/// <para>For more info, see <see href="https://docs.microsoft.com/en-us/windows/win32/direct2d/hlsl-helpers"/>.</para>
/// </remarks>
[AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)]
public sealed class D2DRequiresScenePositionAttribute : Attribute
{
+43
ファイルの表示
@@ -0,0 +1,43 @@
using static TerraFX.Interop.DirectX.D2D1_BUFFER_PRECISION;
namespace ComputeSharp.D2D1;
/// <summary>
/// Indicates the bit depth to use in the imaging pipeline in Direct2D to produce the result of a D2D1 shader.
/// </summary>
/// <remarks>
/// <para>This type exposes the available values in <see href="https://docs.microsoft.com/windows/win32/api/d2d1_1/ne-d2d1_1-d2d1_buffer_precision"/>.</para>
/// <para>For more info, see <see href="https://docs.microsoft.com/windows/win32/direct2d/precision-and-clipping-in-effect-graphs"/>.</para>
/// </remarks>
public enum D2D1BufferPrecision
{
/// <summary>
/// The buffer precision is not specified.
/// </summary>
Unknown = (int)D2D1_BUFFER_PRECISION_UNKNOWN,
/// <summary>
/// The buffer precision uses 8-bit normalized integer values per channel.
/// </summary>
UInt8Normalized = (int)D2D1_BUFFER_PRECISION_8BPC_UNORM,
/// <summary>
/// The buffer precision uses 8-bit normalized integer values per channel with standard RGB data.
/// </summary>
UInt8NormalizedSrgb = (int)D2D1_BUFFER_PRECISION_8BPC_UNORM_SRGB,
/// <summary>
/// The buffer precision uses 16-bit normalized integer values per channel.
/// </summary>
UInt16Normalized = (int)D2D1_BUFFER_PRECISION_16BPC_UNORM,
/// <summary>
/// The buffer precision uses 16-bit float values per channel.
/// </summary>
Float16 = (int)D2D1_BUFFER_PRECISION_16BPC_FLOAT,
/// <summary>
/// The buffer precision uses 32-bit float values per channel.
/// </summary>
Float32 = (int)D2D1_BUFFER_PRECISION_32BPC_FLOAT
}
+28
ファイルの表示
@@ -0,0 +1,28 @@
using static TerraFX.Interop.DirectX.D2D1_CHANNEL_DEPTH;
namespace ComputeSharp.D2D1;
/// <summary>
/// Indicates the channel depth of a stage in a Direct2D rendering pipeline.
/// </summary>
/// <remarks>
/// <para>This type exposes the available values in <see href="https://docs.microsoft.com/windows/win32/api/d2d1effectauthor/ne-d2d1effectauthor-d2d1_channel_depth"/>.</para>
/// <para>For more info, see <see href="https://docs.microsoft.com/windows/win32/direct2d/precision-and-clipping-in-effect-graphs"/>.</para>
/// </remarks>
public enum D2D1ChannelDepth
{
/// <summary>
/// The channel depth is the default, and it will be inherited from the inputs.
/// </summary>
Default = (int)D2D1_CHANNEL_DEPTH_DEFAULT,
/// <summary>
/// The channel depth is 1.
/// </summary>
One = (int)D2D1_CHANNEL_DEPTH_1,
/// <summary>
/// The channel depth is 4.
/// </summary>
Four = (int)D2D1_CHANNEL_DEPTH_4
}
差分なしでファイル名を変更。
+9
ファイルの表示
@@ -37,6 +37,15 @@ public interface ID2D1Shader
[Obsolete("This method is not intended to be used directly by user code")]
uint GetInputType(uint index);
/// <summary>
/// Gets the output buffer precision and depth for the shader.
/// </summary>
/// <param name="precision">The output buffer precision.</param>
/// <param name="depth">The output buffer channel depth.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This method is not intended to be used directly by user code")]
void GetOutputBuffer(out uint precision, out uint depth);
/// <summary>
/// Loads the dispatch data for the shader.
/// </summary>
+30
ファイルの表示
@@ -113,6 +113,36 @@ public static class D2D1PixelShader
return (D2D1PixelShaderInputType)shader.GetInputType((uint)index);
}
/// <summary>
/// Gets the buffer precision for the output buffer of a D2D1 pixel shader.
/// </summary>
/// <typeparam name="T">The type of D2D1 pixel shader to get the output buffer precision for.</typeparam>
/// <returns>The output buffer precision for the D2D1 pixel shader of type <typeparamref name="T"/>.</returns>
public static D2D1BufferPrecision GetOutputBufferPrecision<T>()
where T : unmanaged, ID2D1PixelShader
{
Unsafe.SkipInit(out T shader);
shader.GetOutputBuffer(out uint precision, out _);
return (D2D1BufferPrecision)precision;
}
/// <summary>
/// Gets the channel depth for the output buffer of a D2D1 pixel shader.
/// </summary>
/// <typeparam name="T">The type of D2D1 pixel shader to get the output buffer channel depth for.</typeparam>
/// <returns>The output buffer channel depth for the D2D1 pixel shader of type <typeparamref name="T"/>.</returns>
public static D2D1ChannelDepth GetOutputBufferChannelDepth<T>()
where T : unmanaged, ID2D1PixelShader
{
Unsafe.SkipInit(out T shader);
shader.GetOutputBuffer(out _, out uint depth);
return (D2D1ChannelDepth)depth;
}
/// <summary>
/// Gets the constant buffer from an input D2D1 pixel shader.
/// </summary>
+16
ファイルの表示
@@ -64,6 +64,16 @@ internal unsafe partial struct PixelShaderEffect
/// </summary>
private static int bytecodeSize;
/// <summary>
/// The buffer precision for the resulting output buffer.
/// </summary>
private static D2D1BufferPrecision bufferPrecision;
/// <summary>
/// The channel depth for the resulting output buffer.
/// </summary>
private static D2D1ChannelDepth channelDepth;
/// <summary>
/// The factory of <see cref="ID2D1TransformMapper{T}"/> instances to use for each created effect.
/// </summary>
@@ -97,6 +107,8 @@ internal unsafe partial struct PixelShaderEffect
ReadOnlyMemory<byte> buffer = D2D1PixelShader.LoadBytecode<T>();
int bytecodeSize = buffer.Length;
byte* bytecode = (byte*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(For<T>), bytecodeSize);
D2D1BufferPrecision bufferPrecision = D2D1PixelShader.GetOutputBufferPrecision<T>();
D2D1ChannelDepth channelDepth = D2D1PixelShader.GetOutputBufferChannelDepth<T>();
// Prepare the inputs info
int inputCount = D2D1PixelShader.GetInputCount<T>();
@@ -116,6 +128,8 @@ internal unsafe partial struct PixelShaderEffect
For<T>.inputTypes = inputTypes;
For<T>.bytecode = bytecode;
For<T>.bytecodeSize = bytecodeSize;
For<T>.bufferPrecision = bufferPrecision;
For<T>.channelDepth = channelDepth;
For<T>.d2D1DrawTransformMapperFactory = d2D1DrawTransformMapperFactory;
isInitialized = true;
@@ -195,6 +209,8 @@ internal unsafe partial struct PixelShaderEffect
inputTypes,
bytecode,
bytecodeSize,
bufferPrecision,
channelDepth,
d2D1TransformMapper,
effectImpl);
}
@@ -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;
}
}
}
+16
ファイルの表示
@@ -171,6 +171,16 @@ internal unsafe partial struct PixelShaderEffect
/// </summary>
private int bytecodeSize;
/// <summary>
/// The buffer precision for the resulting output buffer.
/// </summary>
private D2D1BufferPrecision bufferPrecision;
/// <summary>
/// The channel depth for the resulting output buffer.
/// </summary>
private D2D1ChannelDepth channelDepth;
/// <summary>
/// The handle for the <see cref="D2D1TransformMapper"/> instance in use, if any.
/// </summary>
@@ -189,6 +199,8 @@ internal unsafe partial struct PixelShaderEffect
/// <param name="inputTypes">The buffer with the types of inputs for the shader.</param>
/// <param name="bytecode">The shader bytecode.</param>
/// <param name="bytecodeSize">The size of <paramref name="bytecode"/>.</param>
/// <param name="bufferPrecision">The buffer precision for the resulting output buffer.</param>
/// <param name="channelDepth">The channel depth for the resulting output buffer.</param>
/// <param name="d2D1TransformMapper">The <see cref="D2D1TransformMapper"/> instance to use for the effect.</param>
/// <param name="effectImpl">The resulting effect instance.</param>
/// <returns>This always returns <c>0</c>.</returns>
@@ -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;
+11
ファイルの表示
@@ -40,6 +40,17 @@ internal static class ISymbolExtensions
return symbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat);
}
/// <summary>
/// Checks whether or not a given type symbol has a specified full name.
/// </summary>
/// <param name="symbol">The input <see cref="ISymbol"/> instance to check.</param>
/// <param name="name">The full name to check.</param>
/// <returns>Whether <paramref name="symbol"/> has a full name equals to <paramref name="name"/>.</returns>
public static bool HasFullyQualifiedName(this ISymbol symbol, string name)
{
return symbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat) == name;
}
/// <summary>
/// Gets a valid filename for a given <see cref="ITypeSymbol"/> instance.
/// </summary>
+18
ファイルの表示
@@ -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
}
}
+15
ファイルの表示
@@ -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
}
}
+7
ファイルの表示
@@ -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]<ID2D1DrawInfo*, D2D1_BUFFER_PRECISION, D2D1_CHANNEL_DEPTH, int>)(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)
+2
ファイルの表示
@@ -19,6 +19,8 @@
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1effectauthor\ID2D1Transform.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1effectauthor\ID2D1TransformGraph.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1effectauthor\ID2D1TransformNode.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1_1\D2D1_CHANNEL_DEPTH.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1_1\D2D1_BUFFER_PRECISION.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1_1\D2D1_PROPERTY_TYPE.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1_1\ID2D1DeviceContext.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1_1\ID2D1Effect.cs" />
+57
ファイルの表示
@@ -42,4 +42,61 @@ public partial class D2D1InteropServicesTests
return 0;
}
}
[TestMethod]
public unsafe void GetOutputBufferPrecision()
{
Assert.AreEqual(D2D1PixelShader.GetOutputBufferPrecision<ShaderWithMultipleInputs>(), D2D1BufferPrecision.Unknown);
Assert.AreEqual(D2D1PixelShader.GetOutputBufferPrecision<OnlyBufferPrecisionShader>(), D2D1BufferPrecision.UInt16Normalized);
Assert.AreEqual(D2D1PixelShader.GetOutputBufferPrecision<OnlyChannelDepthShader>(), D2D1BufferPrecision.Unknown);
Assert.AreEqual(D2D1PixelShader.GetOutputBufferPrecision<CustomBufferOutputShader>(), D2D1BufferPrecision.UInt8NormalizedSrgb);
}
[TestMethod]
public unsafe void GetOutputBufferChannelDepth()
{
Assert.AreEqual(D2D1PixelShader.GetOutputBufferChannelDepth<ShaderWithMultipleInputs>(), D2D1ChannelDepth.Default);
Assert.AreEqual(D2D1PixelShader.GetOutputBufferChannelDepth<OnlyBufferPrecisionShader>(), D2D1ChannelDepth.Default);
Assert.AreEqual(D2D1PixelShader.GetOutputBufferChannelDepth<OnlyChannelDepthShader>(), D2D1ChannelDepth.Four);
Assert.AreEqual(D2D1PixelShader.GetOutputBufferChannelDepth<CustomBufferOutputShader>(), D2D1ChannelDepth.One);
}
[D2DInputCount(0)]
partial struct EmptyShader : ID2D1PixelShader
{
public Float4 Execute()
{
return 0;
}
}
[D2DInputCount(0)]
[D2DOutputBuffer(D2D1BufferPrecision.UInt16Normalized)]
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.UInt8NormalizedSrgb, D2D1ChannelDepth.One)]
partial struct CustomBufferOutputShader : ID2D1PixelShader
{
public Float4 Execute()
{
return 0;
}
}
}