Merge pull request #572 from Sergio0694/dev/resource-texture-descriptions-memory

Generate D2D1 resource texture descriptions as file local ReadOnlyMemory<T>
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2023-09-28 00:09:43 +02:00
コミット 586e3c1f8c
17ファイル変更
+194 -337

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

+1 -1
ファイルの表示
@@ -80,12 +80,12 @@
<Compile Include="..\ComputeSharp.D2D1\Interfaces\ID2D1PixelShader.cs" Link="ComputeSharp.D2D1\Interfaces\ID2D1PixelShader.cs" />
<Compile Include="..\ComputeSharp.D2D1\Interfaces\__Internals\ID2D1BytecodeLoader.cs" Link="ComputeSharp.D2D1\Interfaces\__Internals\ID2D1BytecodeLoader.cs" />
<Compile Include="..\ComputeSharp.D2D1\Interfaces\__Internals\ID2D1DispatchDataLoader.cs" Link="ComputeSharp.D2D1\Interfaces\__Internals\ID2D1DispatchDataLoader.cs" />
<Compile Include="..\ComputeSharp.D2D1\Interfaces\__Internals\ID2D1ResourceTextureDescriptionsLoader.cs" Link="ComputeSharp.D2D1\Interfaces\__Internals\ID2D1ResourceTextureDescriptionsLoader.cs" />
<Compile Include="..\ComputeSharp.D2D1\Interfaces\__Internals\ID2D1Shader.cs" Link="ComputeSharp.D2D1\Interfaces\__Internals\ID2D1Shader.cs" />
<Compile Include="..\ComputeSharp.D2D1\Intrinsics\D2D.cs" Link="ComputeSharp.D2D1\Intrinsics\D2D.cs" />
<Compile Include="..\ComputeSharp.D2D1\Shaders\Exceptions\FxcCompilationException.cs" Link="ComputeSharp.D2D1\Shaders\Exceptions\FxcCompilationException.cs" />
<Compile Include="..\ComputeSharp.D2D1\Shaders\Interop\D2D1InputDescription.cs" Link="ComputeSharp.D2D1\Shaders\Interop\D2D1InputDescription.cs" />
<Compile Include="..\ComputeSharp.D2D1\Shaders\Interop\D2D1PixelShaderInputType.cs" Link="ComputeSharp.D2D1\Shaders\Interop\D2D1PixelShaderInputType.cs" />
<Compile Include="..\ComputeSharp.D2D1\Shaders\Interop\D2D1ResourceTextureDescription.cs" Link="ComputeSharp.D2D1\Shaders\Interop\D2D1ResourceTextureDescription.cs" />
<Compile Include="..\ComputeSharp.D2D1\Shaders\Translation\D3DCompiler.cs" Link="ComputeSharp.D2D1\Shaders\Translation\D3DCompiler.cs" />
<Compile Include="..\ComputeSharp.D2D1\Shaders\Translation\D3DCompiler.ID3DInclude.cs" Link="ComputeSharp.D2D1\Shaders\Translation\D3DCompiler.ID3DInclude.cs" />
<Compile Include="..\ComputeSharp.D2D1\Shaders\Translation\Headers\d2d1effecthelpers.hlsli.cs" Link="ComputeSharp.D2D1\Shaders\Translation\Headers\d2d1effecthelpers.hlsli.cs" />
@@ -46,10 +46,7 @@ partial class ID2D1ShaderGenerator
// This code produces a method declaration as follows:
//
// readonly void global::ComputeSharp.D2D1.__Internals.ID2D1Shader.LoadInputDescriptions<TLoader>(ref TLoader loader)
// {
// <BODY>
// }
// readonly global::System.ReadOnlyMemory<global::ComputeSharp.D2D1.Interop.D2D1InputDescription> global::ComputeSharp.D2D1.__Internals.ID2D1Shader.InputDescriptions => <EXPRESSION>;
return
PropertyDeclaration(
GenericName(Identifier("global::System.ReadOnlyMemory"))
@@ -110,7 +107,7 @@ partial class ID2D1ShaderGenerator
// Declare the singleton property to get the memory instance:
//
// /// <summary>The singleton <see cref="global::System.ReadOnlyMemory{T}"/> instance for the memory manager.</summary>
// /// <summary>The singleton <see cref="global::ComputeSharp.D2D1.Interop.D2D1InputDescription"/> array instance.</summary>
// public static readonly global::ComputeSharp.D2D1.Interop.D2D1InputDescription[] InputDescriptions = { <INPUT_DESCRIPTIONS> };
FieldDeclarationSyntax fieldDeclaration =
FieldDeclaration(
@@ -1,140 +0,0 @@
using System.Collections.Immutable;
using ComputeSharp.D2D1.__Internals;
using ComputeSharp.D2D1.SourceGenerators.Models;
using ComputeSharp.SourceGeneration.Helpers;
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
{
/// <inheritoc/>
private static partial class LoadResourceTextureDescriptions
{
/// <summary>
/// Creates a <see cref="MethodDeclarationSyntax"/> instance for the <c>LoadResourceTextureDescriptions</c> method.
/// </summary>
/// <param name="resourceTextureDescriptionsInfo">The resource texture descriptions info gathered for the current shader.</param>
/// <returns>The resulting <see cref="MethodDeclarationSyntax"/> instance for the <c>LoadResourceTextureDescriptions</c> method.</returns>
public static MethodDeclarationSyntax GetSyntax(ResourceTextureDescriptionsInfo resourceTextureDescriptionsInfo)
{
// This code produces a method declaration as follows:
//
// readonly void global::ComputeSharp.D2D1.__Internals.ID2D1Shader.LoadResourceTextureDescriptions<TLoader>(ref TLoader loader)
// {
// <BODY>
// }
return
MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier(nameof(LoadResourceTextureDescriptions)))
.WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}")))
.AddModifiers(Token(SyntaxKind.ReadOnlyKeyword))
.AddTypeParameterListParameters(TypeParameter(Identifier("TLoader")))
.AddParameterListParameters(Parameter(Identifier("loader")).AddModifiers(Token(SyntaxKind.RefKeyword)).WithType(IdentifierName("TLoader")))
.WithBody(Block(GetInputDescriptionsLoadingStatements(resourceTextureDescriptionsInfo.ResourceTextureDescriptions)));
}
/// <summary>
/// Gets a sequence of statements to load the resource texture descriptions for a given shader.
/// </summary>
/// <param name="resourceTextureDescriptionsInfo">The array of <see cref="ResourceTextureDescriptionsInfo"/> values for all available resource texture descriptions.</param>
/// <returns>The sequence of <see cref="StatementSyntax"/> instances to load the resource texture descriptions data.</returns>
private static ImmutableArray<StatementSyntax> GetInputDescriptionsLoadingStatements(ImmutableArray<ResourceTextureDescription> resourceTextureDescriptionsInfo)
{
// If there are no resource texture descriptions available, just load an empty buffer
if (resourceTextureDescriptionsInfo.IsEmpty)
{
// loader.LoadResourceTextureDescriptions(default);
return
ImmutableArray.Create<StatementSyntax>(
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("loader"),
IdentifierName("LoadResourceTextureDescriptions")))
.AddArgumentListArguments(Argument(
LiteralExpression(
SyntaxKind.DefaultLiteralExpression,
Token(SyntaxKind.DefaultKeyword))))));
}
using ImmutableArrayBuilder<StatementSyntax> statements = ImmutableArrayBuilder<StatementSyntax>.Rent();
// The size of the buffer with the resource texture descriptions is the number of resurce textures descriptions,
// times the size of each input description, which is a struct containing two int-sized fields (index and rank).
int resourceTextureDescriptionSizeInBytes = resourceTextureDescriptionsInfo.Length * sizeof(int) * 2;
// global::System.Span<byte> data = stackalloc byte[<RESOURCE_TEXTURE_DESCRIPTIONS_SIZE>];
statements.Add(
LocalDeclarationStatement(
VariableDeclaration(
GenericName(Identifier("global::System.Span"))
.AddTypeArgumentListArguments(PredefinedType(Token(SyntaxKind.ByteKeyword))))
.AddVariables(
VariableDeclarator(Identifier("data"))
.WithInitializer(EqualsValueClause(
StackAllocArrayCreationExpression(
ArrayType(PredefinedType(Token(SyntaxKind.ByteKeyword)))
.AddRankSpecifiers(
ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(
LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(resourceTextureDescriptionSizeInBytes)))))))))));
// ref byte r0 = ref data[0];
statements.Add(
LocalDeclarationStatement(
VariableDeclaration(RefType(PredefinedType(Token(SyntaxKind.ByteKeyword))))
.AddVariables(
VariableDeclarator(Identifier("r0"))
.WithInitializer(EqualsValueClause(
RefExpression(
ElementAccessExpression(IdentifierName("data"))
.AddArgumentListArguments(Argument(
LiteralExpression(
SyntaxKind.NumericLiteralExpression,
Literal(0))))))))));
int offset = 0;
// Generate loading statements for each resource texture description
foreach (ResourceTextureDescription resourceTextureDescription in resourceTextureDescriptionsInfo)
{
// Write the index of the current resource texture description:
//
// global::System.Runtime.CompilerServices.Unsafe.As<byte, uint>(ref global::System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref r0, (nint)<OFFSET>)) = <INDEX>;
statements.Add(ExpressionStatement(
AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
ParseExpression($"global::System.Runtime.CompilerServices.Unsafe.As<byte, uint>(ref global::System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref r0, (nint){offset}))"),
LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal((int)resourceTextureDescription.Index)))));
// Write the rank of the current resource texture description:
//
// global::System.Runtime.CompilerServices.Unsafe.As<byte, uint>(ref global::System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref r0, (nint)<OFFSET> + 4)) = <FILTER>;
statements.Add(ExpressionStatement(
AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
ParseExpression($"global::System.Runtime.CompilerServices.Unsafe.As<byte, uint>(ref global::System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref r0, (nint){offset + 4}))"),
LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal((int)resourceTextureDescription.Rank)))));
offset += sizeof(int) * 2;
}
// loader.LoadInputDescriptions(data);
statements.Add(
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("loader"),
IdentifierName("LoadResourceTextureDescriptions")))
.AddArgumentListArguments(Argument(IdentifierName("data")))));
return statements.ToImmutable();
}
}
}
@@ -0,0 +1,135 @@
using System;
using ComputeSharp.D2D1.__Internals;
using ComputeSharp.D2D1.SourceGenerators.Models;
using ComputeSharp.SourceGeneration.Helpers;
using Microsoft.CodeAnalysis;
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
{
/// <inheritoc/>
private static partial class ResourceTextureDescriptions
{
/// <summary>
/// Creates a <see cref="PropertyDeclarationSyntax"/> instance for the <c>ResourceTextureDescriptions</c> property.
/// </summary>
/// <param name="resourceTextureDescriptionsInfo">The resource texture descriptions info gathered for the current shader.</param>
/// <param name="additionalTypes">Any additional <see cref="TypeDeclarationSyntax"/> instances needed by the generated code, if needed.</param>
/// <returns>The resulting <see cref="PropertyDeclarationSyntax"/> instance for the <c>ResourceTextureDescriptions</c> property.</returns>
public static PropertyDeclarationSyntax GetSyntax(ResourceTextureDescriptionsInfo resourceTextureDescriptionsInfo, out TypeDeclarationSyntax[] additionalTypes)
{
ExpressionSyntax memoryExpression;
// If there are no resource texture descriptions, just return a default expression.
// Otherwise, declare the shared array and return it from the property.
if (resourceTextureDescriptionsInfo.ResourceTextureDescriptions.Length == 0)
{
memoryExpression = LiteralExpression(SyntaxKind.DefaultLiteralExpression, Token(SyntaxKind.DefaultKeyword));
additionalTypes = Array.Empty<TypeDeclarationSyntax>();
}
else
{
memoryExpression = MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("Data"),
IdentifierName(nameof(ResourceTextureDescriptions)));
additionalTypes = new[] { GetArrayDeclaration(resourceTextureDescriptionsInfo) };
}
// This code produces a method declaration as follows:
//
// readonly global::System.ReadOnlyMemory<global::ComputeSharp.D2D1.Interop.D2D1ResourceTextureDescription> global::ComputeSharp.D2D1.__Internals.ID2D1Shader.ResourceTextureDescriptions => <EXPRESSION>;
return
PropertyDeclaration(
GenericName(Identifier("global::System.ReadOnlyMemory"))
.AddTypeArgumentListArguments(IdentifierName("global::ComputeSharp.D2D1.Interop.D2D1ResourceTextureDescription")),
Identifier(nameof(ResourceTextureDescriptions)))
.WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}")))
.AddModifiers(Token(SyntaxKind.ReadOnlyKeyword))
.WithExpressionBody(ArrowExpressionClause(memoryExpression))
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
}
/// <summary>
/// Gets the array declaration for the given resource texture descriptions.
/// </summary>
/// <param name="resourceTextureDescriptionsInfo">The resource texture descriptions info gathered for the current shader.</param>
/// <returns>The array declaration for the given resource texture descriptions.</returns>
private static TypeDeclarationSyntax GetArrayDeclaration(ResourceTextureDescriptionsInfo resourceTextureDescriptionsInfo)
{
using ImmutableArrayBuilder<ExpressionSyntax> resourceTextureDescriptionExpressions = ImmutableArrayBuilder<ExpressionSyntax>.Rent();
foreach (ResourceTextureDescription resourceTextureDescription in resourceTextureDescriptionsInfo.ResourceTextureDescriptions)
{
// Create the description expression:
//
// new(<INDEX>, <RANK>)
resourceTextureDescriptionExpressions.Add(
ImplicitObjectCreationExpression()
.AddArgumentListArguments(
Argument(LiteralExpression(
SyntaxKind.NumericLiteralExpression,
Literal(resourceTextureDescription.Index))),
Argument(LiteralExpression(
SyntaxKind.NumericLiteralExpression,
Literal(resourceTextureDescription.Rank)))));
}
// Declare the singleton property to get the memory instance:
//
// /// <summary>The singleton <see cref="global::ComputeSharp.D2D1.Interop.D2D1ResourceTextureDescription"/> array instance.</summary>
// public static readonly global::ComputeSharp.D2D1.Interop.D2D1ResourceTextureDescription[] ResourceTextureDescriptions = { <RESOURCE_TEXTURE_DESCRIPTIONS> };
FieldDeclarationSyntax fieldDeclaration =
FieldDeclaration(
VariableDeclaration(
ArrayType(IdentifierName("global::ComputeSharp.D2D1.Interop.D2D1ResourceTextureDescription"))
.AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(OmittedArraySizeExpression()))))
.AddVariables(
VariableDeclarator(Identifier(nameof(ResourceTextureDescriptions)))
.WithInitializer(EqualsValueClause(
InitializerExpression(SyntaxKind.ArrayInitializerExpression)
.AddExpressions(resourceTextureDescriptionExpressions.ToArray())))))
.AddModifiers(
Token(SyntaxKind.PublicKeyword),
Token(SyntaxKind.StaticKeyword),
Token(SyntaxKind.ReadOnlyKeyword))
.WithLeadingTrivia(Comment("""/// <summary>The singleton <see cref="global::ComputeSharp.D2D1.Interop.D2D1ResourceTextureDescription"/> array instance.</summary>"""));
// Create the container type declaration:
//
// /// <summary>
// /// A container type for input descriptions.
// /// </summary>
// [global::System.CodeDom.Compiler.GeneratedCode("...", "...")]
// [global::System.Diagnostics.DebuggerNonUserCode]
// [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
// file static class Data
// {
// <FIELD_DECLARATION>
// }
return
ClassDeclaration("Data")
.AddModifiers(Token(SyntaxKind.FileKeyword), Token(SyntaxKind.StaticKeyword))
.AddAttributeLists(
AttributeList(SingletonSeparatedList(
Attribute(IdentifierName("global::System.CodeDom.Compiler.GeneratedCode")).AddArgumentListArguments(
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(ID2D1ShaderGenerator).FullName))),
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(ID2D1ShaderGenerator).Assembly.GetName().Version.ToString())))))),
AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.DebuggerNonUserCode")))),
AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage")))))
.AddMembers(fieldDeclaration)
.WithLeadingTrivia(
Comment("/// <summary>"),
Comment("/// A container type for resource texture descriptions."),
Comment("/// </summary>"));
}
}
}
@@ -14,9 +14,9 @@ namespace ComputeSharp.D2D1.SourceGenerators;
partial class ID2D1ShaderGenerator
{
/// <summary>
/// A helper with all logic to generate the <c>LoadResourceTextureDescriptions</c> method.
/// A helper with all logic to generate the <c>ResourceTextureDescriptions</c> property.
/// </summary>
private static partial class LoadResourceTextureDescriptions
private static partial class ResourceTextureDescriptions
{
/// <summary>
/// Extracts the resource texture descriptions for the current shader.
@@ -76,7 +76,7 @@ partial class ID2D1ShaderGenerator
{
foreach ((int? index, int rank) in resourceTextureInfos.WrittenSpan)
{
resourceTextureDescriptionsBuilder.Add(new ResourceTextureDescription((uint)(index ?? 0), (uint)rank));
resourceTextureDescriptionsBuilder.Add(new ResourceTextureDescription(index ?? 0, rank));
}
resourceTextureDescriptions = resourceTextureDescriptionsBuilder.ToImmutable();
+8 -8
ファイルの表示
@@ -100,8 +100,8 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
out ImmutableArray<int> inputComplexIndices,
out ImmutableArray<uint> inputTypes);
// Get the resource texture info for LoadResourceTextureDescriptions()
LoadResourceTextureDescriptions.GetInfo(
// Get the resource texture info for ResourceTextureDescriptions
ResourceTextureDescriptions.GetInfo(
diagnostics,
typeSymbol,
inputCount,
@@ -265,24 +265,24 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
// Generate the InputTypes properties
context.RegisterSourceOutput(inputTypesInfo, static (context, item) =>
{
MemberDeclarationSyntax inputTypesProperty = InputTypes.GetSyntax(item.InputTypes.InputTypes, out TypeDeclarationSyntax[] additionalTypes);
PropertyDeclarationSyntax inputTypesProperty = InputTypes.GetSyntax(item.InputTypes.InputTypes, out TypeDeclarationSyntax[] additionalTypes);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, inputTypesProperty, canUseSkipLocalsInit: false, additionalMemberDeclarations: additionalTypes);
context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(InputTypes)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
});
// Get the LoadResourceTextureDescriptions() info (hierarchy and resource texture descriptions)
// Get the ResourceTextureDescriptions info (hierarchy and resource texture descriptions)
IncrementalValuesProvider<(HierarchyInfo Hierarchy, ResourceTextureDescriptionsInfo ResourceTextureDescriptions)> resourceTextureDescriptionsInfo =
shaderInfoWithErrors
.Select(static (item, _) => (item.Hierarchy, item.ResourceTextureDescriptions));
// Generate the LoadResourceTextureDescriptions() methods
// Generate the ResourceTextureDescriptions properties
context.RegisterSourceOutput(resourceTextureDescriptionsInfo, static (context, item) =>
{
MethodDeclarationSyntax getInputTypeMethod = LoadResourceTextureDescriptions.GetSyntax(item.ResourceTextureDescriptions);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, getInputTypeMethod, canUseSkipLocalsInit: false);
PropertyDeclarationSyntax resourceTextureDescriptionsProperty = ResourceTextureDescriptions.GetSyntax(item.ResourceTextureDescriptions, out TypeDeclarationSyntax[] additionalTypes);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, resourceTextureDescriptionsProperty, canUseSkipLocalsInit: false, additionalMemberDeclarations: additionalTypes);
context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(LoadResourceTextureDescriptions)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(ResourceTextureDescriptions)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
});
// Get the info for InitializeFromDispatchData() (hierarchy and dispatch data)
+1 -1
ファイルの表示
@@ -5,4 +5,4 @@ namespace ComputeSharp.D2D1.SourceGenerators.Models;
/// </summary>
/// <param name="Index">The index of the resource texture resource the description is for.</param>
/// <param name="Rank">The rank of the resource texture.</param>
internal sealed record ResourceTextureDescription(uint Index, uint Rank);
internal sealed record ResourceTextureDescription(int Index, int Rank);
-2
ファイルの表示
@@ -37,8 +37,6 @@
<Compile Include="$(MSBuildThisFileDirectory)Extensions\IUnknownExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\RcwMarshaller.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\ResourceManager.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Loaders\D2D1ImmutableArrayResourceTextureDescriptionsLoader.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Loaders\D2D1IndexBitmaskResourceTextureDescriptionsLoader.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PixelShaderEffect{T}.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PixelShaderEffect{T}.ICanvasEffect.cs" />
@@ -1,53 +0,0 @@
using System;
using System.Collections.Immutable;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using ComputeSharp.D2D1.__Internals;
using ComputeSharp.D2D1.Interop;
#pragma warning disable CS0618
namespace ComputeSharp.D2D1.Shaders.Loaders;
/// <summary>
/// A resource texture index array loader for D2D1 pixel shaders.
/// </summary>
internal struct D2D1ImmutableArrayResourceTextureDescriptionsLoader : ID2D1ResourceTextureDescriptionsLoader
{
/// <summary>
/// The array of indices for resource textures for the current effect.
/// </summary>
private ImmutableArray<int> indices;
/// <summary>
/// Gets the resulting array of indices for resource textures for the current effect.
/// </summary>
/// <returns>An array of indices for resource textures for the current effect.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ImmutableArray<int> GetResultingIndices()
{
return this.indices;
}
/// <inheritdoc/>
void ID2D1ResourceTextureDescriptionsLoader.LoadResourceTextureDescriptions(ReadOnlySpan<byte> data)
{
if (data.IsEmpty)
{
this.indices = ImmutableArray<int>.Empty;
}
else
{
ReadOnlySpan<D2D1ResourceTextureDescription> descriptions = MemoryMarshal.Cast<byte, D2D1ResourceTextureDescription>(data);
int[] indices = new int[descriptions.Length];
for (int i = 0; i < descriptions.Length; i++)
{
indices[i] = descriptions[i].Index;
}
this.indices = Unsafe.As<int[], ImmutableArray<int>>(ref indices);
}
}
}
@@ -1,43 +0,0 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using ComputeSharp.D2D1.__Internals;
using ComputeSharp.D2D1.Interop;
#pragma warning disable CS0618
namespace ComputeSharp.D2D1.Shaders.Loaders;
/// <summary>
/// A resource texture index bitmask loader for D2D1 pixel shaders.
/// </summary>
internal struct D2D1IndexBitmaskResourceTextureDescriptionsLoader : ID2D1ResourceTextureDescriptionsLoader
{
/// <summary>
/// The bitmask of indices for resource textures in the target shader.
/// </summary>
private int indexBitmask;
/// <summary>
/// Gets the resulting bitmask of indices for resource textures in the target shader.
/// </summary>
/// <returns>A bitmask of indices for resource textures in the target shader.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly int GetResultingIndexBitmask()
{
return this.indexBitmask;
}
/// <inheritdoc/>
void ID2D1ResourceTextureDescriptionsLoader.LoadResourceTextureDescriptions(ReadOnlySpan<byte> data)
{
int indexBitmask = 0;
foreach (ref readonly D2D1ResourceTextureDescription description in MemoryMarshal.Cast<byte, D2D1ResourceTextureDescription>(data))
{
indexBitmask |= 1 << description.Index;
}
this.indexBitmask = indexBitmask;
}
}
@@ -5,7 +5,6 @@ using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using ComputeSharp.D2D1.Interop;
using ComputeSharp.D2D1.Shaders.Loaders;
#if WINDOWS_UWP
using ComputeSharp.D2D1.Uwp.Buffers;
using ComputeSharp.D2D1.Uwp.Collections;
@@ -233,13 +232,16 @@ partial class PixelShaderEffect<T>
/// <returns>The bitmask of indices for resource textures in the target shader.</returns>
private static int GetIndexBitmask()
{
D2D1IndexBitmaskResourceTextureDescriptionsLoader bitmaskLoader = default;
Unsafe.SkipInit(out T shader);
shader.LoadResourceTextureDescriptions(ref bitmaskLoader);
int indexBitmask = 0;
return bitmaskLoader.GetResultingIndexBitmask();
foreach (ref readonly D2D1ResourceTextureDescription description in shader.ResourceTextureDescriptions.Span)
{
indexBitmask |= 1 << description.Index;
}
return indexBitmask;
}
/// <summary>
@@ -248,13 +250,24 @@ partial class PixelShaderEffect<T>
/// <returns>The collection of valid indices for the current effect.</returns>
private static ImmutableArray<int> GetIndices()
{
D2D1ImmutableArrayResourceTextureDescriptionsLoader indicesLoader = default;
Unsafe.SkipInit(out T shader);
shader.LoadResourceTextureDescriptions(ref indicesLoader);
ReadOnlySpan<D2D1ResourceTextureDescription> descriptions = shader.ResourceTextureDescriptions.Span;
return indicesLoader.GetResultingIndices();
// Skip the array allocation if there are no resource texture descriptions
if (descriptions.IsEmpty)
{
return ImmutableArray<int>.Empty;
}
int[] indices = new int[descriptions.Length];
for (int i = 0; i < descriptions.Length; i++)
{
indices[i] = descriptions[i].Index;
}
return Unsafe.As<int[], ImmutableArray<int>>(ref indices);
}
}
}
@@ -1,18 +0,0 @@
using System;
using System.ComponentModel;
namespace ComputeSharp.D2D1.__Internals;
/// <summary>
/// A base <see langword="interface"/> representing a loader for resource texture descriptions in a D2D1 shader.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This interface is not intended to be used directly by user code")]
public interface ID2D1ResourceTextureDescriptionsLoader
{
/// <summary>
/// Loads the available resource texture descriptions for the shader.
/// </summary>
/// <param name="data">The sequence of serialized resource texture descriptions.</param>
void LoadResourceTextureDescriptions(ReadOnlySpan<byte> data);
}
+2 -5
ファイルの表示
@@ -92,14 +92,11 @@ public interface ID2D1Shader
ReadOnlyMemory<D2D1InputDescription> InputDescriptions { get; }
/// <summary>
/// Loads the resource texture descriptions for the shader, if any.
/// Gets the resource texture descriptions for the shader.
/// </summary>
/// <typeparam name="TLoader">The type of resource texture descriptions loader being used.</typeparam>
/// <param name="loader">The <typeparamref name="TLoader"/> instance to use to load the resource texture descriptions.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This method is not intended to be used directly by user code")]
void LoadResourceTextureDescriptions<TLoader>(ref TLoader loader)
where TLoader : struct, ID2D1ResourceTextureDescriptionsLoader;
ReadOnlyMemory<D2D1ResourceTextureDescription> ResourceTextureDescriptions { get; }
/// <summary>
/// Gets the output buffer precision and depth for the shader.
+1 -5
ファイルの表示
@@ -292,13 +292,9 @@ public static class D2D1PixelShader
public static ReadOnlyMemory<D2D1ResourceTextureDescription> GetResourceTextureDescriptions<T>()
where T : unmanaged, ID2D1PixelShader
{
D2D1ByteArrayResourceTextureDescriptionsLoader resourceTextureDescriptionsLoader = default;
Unsafe.SkipInit(out T shader);
shader.LoadResourceTextureDescriptions(ref resourceTextureDescriptionsLoader);
return resourceTextureDescriptionsLoader.GetResultingResourceTextureDescriptions();
return shader.ResourceTextureDescriptions;
}
/// <summary>
+11
ファイルの表示
@@ -19,6 +19,17 @@ public readonly struct D2D1ResourceTextureDescription
/// </summary>
private readonly int dimensions;
/// <summary>
/// Creates a new <see cref="D2D1ResourceTextureDescription"/> instance with the specified parameters.
/// </summary>
/// <param name="index">The index of the resource texture the description belongs to.</param>
/// <param name="dimensions">The number of dimensions of the resource texture the description belongs to.</param>
public D2D1ResourceTextureDescription(int index, int dimensions)
{
this.index = index;
this.dimensions = dimensions;
}
/// <summary>
/// Gets the index of the resource texture the description belongs to.
/// </summary>
@@ -1,36 +0,0 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using ComputeSharp.D2D1.__Internals;
using ComputeSharp.D2D1.Interop;
#pragma warning disable CS0618
namespace ComputeSharp.D2D1.Shaders.Loaders;
/// <summary>
/// A resource texture descriptions loader for D2D1 pixel shaders.
/// </summary>
internal struct D2D1ByteArrayResourceTextureDescriptionsLoader : ID2D1ResourceTextureDescriptionsLoader
{
/// <summary>
/// The resulting <see cref="D2D1ResourceTextureDescription"/> array.
/// </summary>
private D2D1ResourceTextureDescription[]? resourceTextureDescriptions;
/// <summary>
/// Gets the resulting resource texture descriptions.
/// </summary>
/// <returns>A <see cref="D2D1ResourceTextureDescription"/> array with the available resource texture descriptions.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly D2D1ResourceTextureDescription[] GetResultingResourceTextureDescriptions()
{
return this.resourceTextureDescriptions!;
}
/// <inheritdoc/>
void ID2D1ResourceTextureDescriptionsLoader.LoadResourceTextureDescriptions(ReadOnlySpan<byte> data)
{
this.resourceTextureDescriptions = MemoryMarshal.Cast<byte, D2D1ResourceTextureDescription>(data).ToArray();
}
}
+8 -8
ファイルの表示
@@ -222,16 +222,16 @@ public partial class D2D1PixelShaderTests
ReadOnlySpan<D2D1ResourceTextureDescription> span = resourceTextureDescriptions.Span;
Assert.AreEqual(span[0].Index, 5);
Assert.AreEqual(span[0].Index, 4);
Assert.AreEqual(span[0].Dimensions, 1);
Assert.AreEqual(span[1].Index, 6);
Assert.AreEqual(span[1].Index, 5);
Assert.AreEqual(span[1].Dimensions, 2);
Assert.AreEqual(span[2].Index, 7);
Assert.AreEqual(span[2].Index, 6);
Assert.AreEqual(span[2].Dimensions, 3);
Assert.AreEqual(span[3].Index, 8);
Assert.AreEqual(span[3].Index, 7);
Assert.AreEqual(span[3].Dimensions, 2);
}
@@ -246,16 +246,16 @@ public partial class D2D1PixelShaderTests
{
float number;
[D2DResourceTextureIndex(5)]
[D2DResourceTextureIndex(4)]
D2D1ResourceTexture1D<float4> myTexture1;
[D2DResourceTextureIndex(6)]
[D2DResourceTextureIndex(5)]
D2D1ResourceTexture2D<float4> myTexture2;
[D2DResourceTextureIndex(7)]
[D2DResourceTextureIndex(6)]
D2D1ResourceTexture3D<float4> myTexture3;
[D2DResourceTextureIndex(8)]
[D2DResourceTextureIndex(7)]
D2D1ResourceTexture2D<float> myTexture4;
public Float4 Execute()