Correctly generate accessors for nested/unaccessible fields
このコミットが含まれているのは:
1 親
99a0c2b8b6
コミット
2961612f47
15ファイル変更
+181
-109
検索条件に一致するファイルはありません
+89
-45
@@ -1,4 +1,3 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using ComputeSharp.D2D1.SourceGenerators.Models;
|
||||
using ComputeSharp.SourceGeneration.Extensions;
|
||||
@@ -93,11 +92,11 @@ partial class D2DPixelShaderDescriptorGenerator
|
||||
return;
|
||||
}
|
||||
|
||||
usingDirectives.Add("global::System.CodeDom.Compiler");
|
||||
usingDirectives.Add("global::System.Diagnostics");
|
||||
usingDirectives.Add("global::System.Diagnostics.CodeAnalysis");
|
||||
usingDirectives.Add("global::System.Runtime.CompilerServices");
|
||||
usingDirectives.Add("global::System.Runtime.InteropServices");
|
||||
_ = usingDirectives.Add("global::System.CodeDom.Compiler");
|
||||
_ = usingDirectives.Add("global::System.Diagnostics");
|
||||
_ = usingDirectives.Add("global::System.Diagnostics.CodeAnalysis");
|
||||
_ = usingDirectives.Add("global::System.Runtime.CompilerServices");
|
||||
_ = usingDirectives.Add("global::System.Runtime.InteropServices");
|
||||
|
||||
// Declare the ConstantBuffer type
|
||||
static void ConstantBufferCallback(D2D1ShaderInfo info, IndentedTextWriter writer)
|
||||
@@ -121,27 +120,27 @@ partial class D2DPixelShaderDescriptorGenerator
|
||||
case FieldInfo.Primitive { TypeName: "System.Boolean" } primitive:
|
||||
|
||||
// Append a field as a global::ComputeSharp.Bool value (will use the implicit conversion from bool values)
|
||||
writer.WriteLine($"""/// <inheritdoc cref="{fullyQualifiedTypeName}.{string.Join(".", primitive.FieldPath)}"/>""");
|
||||
writer.WriteLine($"""/// <inheritdoc cref="{fullyQualifiedTypeName}.{string.Join(".", primitive.FieldPath.Select(static path => path.Name))}"/>""");
|
||||
writer.WriteLine($"""[FieldOffset({primitive.Offset})]""");
|
||||
writer.WriteLine($"""public global::ComputeSharp.Bool {string.Join("_", primitive.FieldPath)};""");
|
||||
writer.WriteLine($"""public global::ComputeSharp.Bool {string.Join("_", primitive.FieldPath.Select(static path => path.Name))};""");
|
||||
break;
|
||||
case FieldInfo.Primitive primitive:
|
||||
|
||||
// Append primitive fields of other types with their mapped names
|
||||
writer.WriteLine($"""/// <inheritdoc cref="{fullyQualifiedTypeName}.{string.Join(".", primitive.FieldPath)}"/>""");
|
||||
writer.WriteLine($"""/// <inheritdoc cref="{fullyQualifiedTypeName}.{string.Join(".", primitive.FieldPath.Select(static path => path.Name))}"/>""");
|
||||
writer.WriteLine($"""[FieldOffset({primitive.Offset})]""");
|
||||
writer.WriteLine($"""public {HlslKnownTypes.GetMappedName(primitive.TypeName)} {string.Join("_", primitive.FieldPath)};""");
|
||||
writer.WriteLine($"""public {HlslKnownTypes.GetMappedName(primitive.TypeName)} {string.Join("_", primitive.FieldPath.Select(static path => path.Name))};""");
|
||||
break;
|
||||
|
||||
case FieldInfo.NonLinearMatrix matrix:
|
||||
string rowTypeName = HlslKnownTypes.GetMappedName($"ComputeSharp.{matrix.ElementName}{matrix.Columns}");
|
||||
string fieldNamePrefix = string.Join("_", matrix.FieldPath);
|
||||
string fieldNamePrefix = string.Join("_", matrix.FieldPath.Select(static path => path.Name));
|
||||
|
||||
// Declare a field for every row of the matrix type
|
||||
for (int j = 0; j < matrix.Rows; j++)
|
||||
{
|
||||
writer.WriteLineIf(j > 0);
|
||||
writer.WriteLine($"""/// <summary>Row {j} of <see cref="{fullyQualifiedTypeName}.{string.Join(".", matrix.FieldPath)}"/>.</summary>""");
|
||||
writer.WriteLine($"""/// <summary>Row {j} of <see cref="{fullyQualifiedTypeName}.{string.Join(".", matrix.FieldPath.Select(static path => path.Name))}"/>.</summary>""");
|
||||
writer.WriteLine($"""[FieldOffset({matrix.Offsets[j]})]""");
|
||||
writer.WriteLine($"""public {rowTypeName} {fieldNamePrefix}_{j};""");
|
||||
}
|
||||
@@ -186,22 +185,20 @@ partial class D2DPixelShaderDescriptorGenerator
|
||||
switch (fieldInfo)
|
||||
{
|
||||
case FieldInfo.Primitive primitive:
|
||||
string fieldName = primitive.FieldPath[0];
|
||||
|
||||
// Read a nested primitive value
|
||||
writer.WriteLine($"{fieldName}(ref shader){string.Join(".", primitive.FieldPath.Skip(1))} = buffer.{string.Join("_", primitive.FieldPath)};");
|
||||
writer.WriteLine($"{GetFieldAccessExpression(fieldInfo, true)} = buffer.{string.Join("_", primitive.FieldPath.Select(static path => path.Name))};");
|
||||
break;
|
||||
|
||||
case FieldInfo.NonLinearMatrix matrix:
|
||||
string primitiveTypeName = matrix.ElementName.ToLowerInvariant();
|
||||
string matrixHlslTypeName = $"{primitiveTypeName}{matrix.Rows}x{matrix.Columns}";
|
||||
string rowTypeName = HlslKnownTypes.GetMappedName($"ComputeSharp.{matrix.ElementName}{matrix.Columns}");
|
||||
string fieldPath = string.Join(".", matrix.FieldPath);
|
||||
string fieldNamePrefix = string.Join("_", matrix.FieldPath);
|
||||
string fieldNamePrefix = string.Join("_", matrix.FieldPath.Select(static path => path.Name));
|
||||
|
||||
// Get a reference to the whole matrix field, just once for all rows
|
||||
writer.WriteLine(skipIfPresent: true);
|
||||
writer.WriteLine($"ref {matrixHlslTypeName} __{fieldNamePrefix} = ref {matrix.FieldPath[0]}(ref shader){string.Join(".", matrix.FieldPath.Skip(1))};");
|
||||
writer.WriteLine($"ref {matrixHlslTypeName} __{fieldNamePrefix} = ref {GetFieldAccessExpression(fieldInfo, true)};");
|
||||
writer.WriteLine();
|
||||
|
||||
// Read all rows of a given matrix type
|
||||
@@ -240,19 +237,18 @@ partial class D2DPixelShaderDescriptorGenerator
|
||||
case FieldInfo.Primitive primitive:
|
||||
|
||||
// Assign a primitive value
|
||||
writer.WriteLine($"buffer.{string.Join("_", primitive.FieldPath)} = {primitive.FieldPath[0]}(in shader){string.Join(".", primitive.FieldPath.Skip(1))};");
|
||||
writer.WriteLine($"buffer.{string.Join("_", primitive.FieldPath.Select(static path => path.Name))} = {GetFieldAccessExpression(fieldInfo, false)};");
|
||||
break;
|
||||
|
||||
case FieldInfo.NonLinearMatrix matrix:
|
||||
string primitiveTypeName = matrix.ElementName.ToLowerInvariant();
|
||||
string matrixHlslTypeName = $"{primitiveTypeName}{matrix.Rows}x{matrix.Columns}";
|
||||
string rowTypeName = HlslKnownTypes.GetMappedName($"ComputeSharp.{matrix.ElementName}{matrix.Columns}");
|
||||
string fieldPath = string.Join(".", matrix.FieldPath);
|
||||
string fieldNamePrefix = string.Join("_", matrix.FieldPath);
|
||||
string fieldNamePrefix = string.Join("_", matrix.FieldPath.Select(static path => path.Name));
|
||||
|
||||
// Get a reference to the whole matrix field, just once for all rows
|
||||
writer.WriteLine(skipIfPresent: true);
|
||||
writer.WriteLine($"ref {matrixHlslTypeName} __{fieldNamePrefix} = ref {matrix.FieldPath[0]}(in shader){string.Join(".", matrix.FieldPath.Skip(1))};");
|
||||
writer.WriteLine($"ref {matrixHlslTypeName} __{fieldNamePrefix} = ref {GetFieldAccessExpression(fieldInfo, true)};");
|
||||
writer.WriteLine();
|
||||
|
||||
// Assign all rows of a given matrix type
|
||||
@@ -267,36 +263,55 @@ partial class D2DPixelShaderDescriptorGenerator
|
||||
}
|
||||
}
|
||||
|
||||
using ImmutableArrayBuilder<string> topLevelFieldNames = new();
|
||||
using ImmutableHashSetBuilder<(string ContainerType, string FieldName)> generatedAccessors = new();
|
||||
|
||||
// Define all field accessors
|
||||
foreach (FieldInfo fieldInfo in info.Fields)
|
||||
{
|
||||
string fieldName = fieldInfo.FieldPath[0];
|
||||
|
||||
// Only generate accessors for top level fields, just once. That is, if they have
|
||||
// multiple nested items, only generate one accessor for the top level field. We
|
||||
// can simply use a linear scan here, as we expect fields to not be that many.
|
||||
if (topLevelFieldNames.WrittenSpan.IndexOf(fieldName) >= 0)
|
||||
for (int i = 0; i < fieldInfo.FieldPath.Length; i++)
|
||||
{
|
||||
continue;
|
||||
FieldPathPart pathPart = fieldInfo.FieldPath[i];
|
||||
|
||||
// Only generate field accessors for fields that are not accessible
|
||||
if (pathPart.IsAccessible)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get the friendly type name for the field:
|
||||
// - If the part path is a nested struct, the type name is embedded
|
||||
// - Otherwise, get the correct HLSL mapped type name for the field
|
||||
string typeName = pathPart switch
|
||||
{
|
||||
FieldPathPart.Nested nested => nested.TypeName,
|
||||
FieldPathPart.Leaf when fieldInfo.TypeName is "System.Boolean" => "global::ComputeSharp.Bool",
|
||||
_ => HlslKnownTypes.GetMappedName(fieldInfo.TypeName)
|
||||
};
|
||||
|
||||
// Get the friendly type name for the field. If this nested field is the
|
||||
// first path, then the parent is the shader type itself. Otherwise, the
|
||||
// container path is the type of the previous containing type of the field.
|
||||
string containingTypeName = i == 0
|
||||
? fullyQualifiedTypeName
|
||||
: ((FieldPathPart.Nested)fieldInfo.FieldPath[i - 1]).TypeName;
|
||||
|
||||
// Make sure to skip repeated field accessors. It's possible for some accessors to end up
|
||||
// being required, in case a shader type had multiple fields of the same nested struct type.
|
||||
// To avoid so, we just add the containing type and name to an hashset that we update.
|
||||
if (!generatedAccessors.Add((containingTypeName, pathPart.Name)))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
writer.WriteLine();
|
||||
writer.WriteLine($"""
|
||||
/// <inheritdoc cref="{containingTypeName}.{pathPart.Name}"/>
|
||||
/// <param name="shader">The input <see cref="{containingTypeName}"/> shader instance.</param>
|
||||
/// <returns>A mutable reference to <see cref="{containingTypeName}.{pathPart.Name}"/>.</returns>
|
||||
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "{pathPart.Name}")]
|
||||
private static extern ref readonly {typeName} {pathPart.Name}(ref readonly {containingTypeName} shader);
|
||||
""", isMultiline: true);
|
||||
}
|
||||
|
||||
topLevelFieldNames.Add(fieldName);
|
||||
|
||||
// Get the friendly type name for the field
|
||||
string typeName = fieldInfo.TypeName is "System.Boolean"
|
||||
? "global::ComputeSharp.Bool"
|
||||
: HlslKnownTypes.GetMappedName(fieldInfo.TypeName);
|
||||
|
||||
writer.WriteLine();
|
||||
writer.WriteLine($"""
|
||||
/// <inheritdoc cref="{fullyQualifiedTypeName}.{fieldName}"/>
|
||||
/// <param name="shader">The input <see cref="{fullyQualifiedTypeName}"/> shader instance.</param>
|
||||
/// <returns>A mutable reference to <see cref="{fullyQualifiedTypeName}.{fieldName}"/>.</returns>
|
||||
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "{fieldName}")]
|
||||
private static extern ref {typeName} {fieldName}(ref readonly {fullyQualifiedTypeName} shader);
|
||||
""", isMultiline: true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -304,5 +319,34 @@ partial class D2DPixelShaderDescriptorGenerator
|
||||
callbacks.Add(ConstantBufferCallback);
|
||||
callbacks.Add(ConstantBufferMarshallerCallback);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an expression to access a given field.
|
||||
/// </summary>
|
||||
/// <param name="fieldInfo">The input field to generate the expression for.</param>
|
||||
/// <param name="isRef">Whether to return the expression as a mutable <see langword="ref"/>.</param>
|
||||
/// <returns>An expression to access a given field.</returns>
|
||||
private static string GetFieldAccessExpression(FieldInfo fieldInfo, bool isRef)
|
||||
{
|
||||
string fieldExpression = "shader";
|
||||
|
||||
// Format the field access expression, there's two options:
|
||||
// - If the field is accessible, just access it directly
|
||||
// - If the field is not accessible, use the unsafe accessor
|
||||
foreach (FieldPathPart part in fieldInfo.FieldPath)
|
||||
{
|
||||
fieldExpression = part.IsAccessible
|
||||
? $"{fieldExpression}.{part.Name}"
|
||||
: $"{part.Name}(in {fieldExpression})";
|
||||
}
|
||||
|
||||
// Return a mutable ref if needed (the expression is always readonly here)
|
||||
if (isRef)
|
||||
{
|
||||
return $"Unsafe.AsRef(in {fieldExpression})";
|
||||
}
|
||||
|
||||
return fieldExpression;
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
-5
@@ -39,7 +39,7 @@ partial class D2DPixelShaderDescriptorGenerator
|
||||
static IEnumerable<FieldInfo> GetCapturedFieldInfos(
|
||||
Compilation compilation,
|
||||
ITypeSymbol currentTypeSymbol,
|
||||
ImmutableArray<string> fieldPath,
|
||||
ImmutableArray<FieldPathPart> fieldPath,
|
||||
StrongBox<int> rawDataOffset)
|
||||
{
|
||||
bool isFirstField = true;
|
||||
@@ -58,6 +58,7 @@ partial class D2DPixelShaderDescriptorGenerator
|
||||
|
||||
string fieldName = fieldSymbol.Name;
|
||||
string typeName = fieldSymbol.Type.GetFullyQualifiedMetadataName();
|
||||
bool isAccessible = fieldSymbol.IsAccessibleFromCompilationAssembly(compilation);
|
||||
|
||||
// The first item in each nested struct needs to be aligned to 16 bytes
|
||||
if (isFirstField && fieldPath.Length > 0)
|
||||
@@ -69,12 +70,17 @@ partial class D2DPixelShaderDescriptorGenerator
|
||||
|
||||
if (HlslKnownTypes.IsKnownHlslType(typeName))
|
||||
{
|
||||
yield return GetHlslKnownTypeFieldInfo(fieldPath.Add(fieldName), typeName, ref rawDataOffset.Value);
|
||||
ImmutableArray<FieldPathPart> nestedFieldPath = fieldPath.Add(new FieldPathPart.Leaf(fieldName, isAccessible));
|
||||
|
||||
yield return GetHlslKnownTypeFieldInfo(nestedFieldPath, typeName, ref rawDataOffset.Value);
|
||||
}
|
||||
else if (fieldSymbol.Type.IsUnmanagedType)
|
||||
{
|
||||
string nestedTypeName = fieldSymbol.Type.GetFullyQualifiedName(includeGlobal: true);
|
||||
ImmutableArray<FieldPathPart> nestedFieldPath = fieldPath.Add(new FieldPathPart.Nested(fieldName, isAccessible, nestedTypeName));
|
||||
|
||||
// Custom struct type defined by the user
|
||||
foreach (FieldInfo fieldInfo in GetCapturedFieldInfos(compilation, fieldSymbol.Type, fieldPath.Add(fieldName), rawDataOffset))
|
||||
foreach (FieldInfo fieldInfo in GetCapturedFieldInfos(compilation, fieldSymbol.Type, nestedFieldPath, rawDataOffset))
|
||||
{
|
||||
yield return fieldInfo;
|
||||
}
|
||||
@@ -89,7 +95,7 @@ partial class D2DPixelShaderDescriptorGenerator
|
||||
ImmutableArray<FieldInfo> fieldInfos = GetCapturedFieldInfos(
|
||||
compilation,
|
||||
structDeclarationSymbol,
|
||||
ImmutableArray<string>.Empty,
|
||||
ImmutableArray<FieldPathPart>.Empty,
|
||||
rawDataOffsetAsBox).ToImmutableArray();
|
||||
|
||||
constantBufferSizeInBytes = rawDataOffsetAsBox.Value;
|
||||
@@ -114,7 +120,7 @@ partial class D2DPixelShaderDescriptorGenerator
|
||||
/// <param name="typeName">The type name currently being read.</param>
|
||||
/// <param name="rawDataOffset">The current offset within the loaded data buffer.</param>
|
||||
private static FieldInfo GetHlslKnownTypeFieldInfo(
|
||||
ImmutableArray<string> fieldPath,
|
||||
ImmutableArray<FieldPathPart> fieldPath,
|
||||
string typeName,
|
||||
ref int rawDataOffset)
|
||||
{
|
||||
|
||||
+7
-7
@@ -85,13 +85,13 @@ partial class D2DPixelShaderDescriptorGenerator
|
||||
return;
|
||||
}
|
||||
|
||||
usingDirectives.Add("global::System");
|
||||
usingDirectives.Add("global::System.Buffers");
|
||||
usingDirectives.Add("global::System.CodeDom.Compiler");
|
||||
usingDirectives.Add("global::System.Diagnostics");
|
||||
usingDirectives.Add("global::System.Diagnostics.CodeAnalysis");
|
||||
usingDirectives.Add("global::System.Runtime.CompilerServices");
|
||||
usingDirectives.Add("global::System.Runtime.InteropServices");
|
||||
_ = usingDirectives.Add("global::System");
|
||||
_ = usingDirectives.Add("global::System.Buffers");
|
||||
_ = usingDirectives.Add("global::System.CodeDom.Compiler");
|
||||
_ = usingDirectives.Add("global::System.Diagnostics");
|
||||
_ = usingDirectives.Add("global::System.Diagnostics.CodeAnalysis");
|
||||
_ = usingDirectives.Add("global::System.Runtime.CompilerServices");
|
||||
_ = usingDirectives.Add("global::System.Runtime.InteropServices");
|
||||
|
||||
// Declare the HlslBytecodeMemoryManager custom memory manager type
|
||||
static void Callback(D2D1ShaderInfo info, IndentedTextWriter writer)
|
||||
|
||||
+5
-5
@@ -53,9 +53,9 @@ partial class D2DPixelShaderDescriptorGenerator
|
||||
return;
|
||||
}
|
||||
|
||||
usingDirectives.Add("global::System.CodeDom.Compiler");
|
||||
usingDirectives.Add("global::System.Diagnostics");
|
||||
usingDirectives.Add("global::System.Diagnostics.CodeAnalysis");
|
||||
_ = usingDirectives.Add("global::System.CodeDom.Compiler");
|
||||
_ = usingDirectives.Add("global::System.Diagnostics");
|
||||
_ = usingDirectives.Add("global::System.Diagnostics.CodeAnalysis");
|
||||
|
||||
AddAdditionalDataMemberUsingDirectives(info, usingDirectives);
|
||||
ResourceTextureDescriptions.AddAdditionalDataMemberUsingDirectives(info, usingDirectives);
|
||||
@@ -103,8 +103,8 @@ partial class D2DPixelShaderDescriptorGenerator
|
||||
return;
|
||||
}
|
||||
|
||||
usingDirectives.Add("global::ComputeSharp.D2D1");
|
||||
usingDirectives.Add("global::ComputeSharp.D2D1.Interop");
|
||||
_ = usingDirectives.Add("global::ComputeSharp.D2D1");
|
||||
_ = usingDirectives.Add("global::ComputeSharp.D2D1.Interop");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
+8
-8
@@ -50,14 +50,14 @@ partial class D2DPixelShaderDescriptorGenerator
|
||||
return;
|
||||
}
|
||||
|
||||
usingDirectives.Add("global::System");
|
||||
usingDirectives.Add("global::System.Buffers");
|
||||
usingDirectives.Add("global::System.CodeDom.Compiler");
|
||||
usingDirectives.Add("global::System.Diagnostics");
|
||||
usingDirectives.Add("global::System.Diagnostics.CodeAnalysis");
|
||||
usingDirectives.Add("global::System.Runtime.CompilerServices");
|
||||
usingDirectives.Add("global::System.Runtime.InteropServices");
|
||||
usingDirectives.Add("global::ComputeSharp.D2D1.Interop");
|
||||
_ = usingDirectives.Add("global::System");
|
||||
_ = usingDirectives.Add("global::System.Buffers");
|
||||
_ = usingDirectives.Add("global::System.CodeDom.Compiler");
|
||||
_ = usingDirectives.Add("global::System.Diagnostics");
|
||||
_ = usingDirectives.Add("global::System.Diagnostics.CodeAnalysis");
|
||||
_ = usingDirectives.Add("global::System.Runtime.CompilerServices");
|
||||
_ = usingDirectives.Add("global::System.Runtime.InteropServices");
|
||||
_ = usingDirectives.Add("global::ComputeSharp.D2D1.Interop");
|
||||
|
||||
// Declare the InputTypesMemoryManager custom memory manager type
|
||||
static void Callback(D2D1ShaderInfo info, IndentedTextWriter writer)
|
||||
|
||||
+1
-1
@@ -47,7 +47,7 @@ partial class D2DPixelShaderDescriptorGenerator
|
||||
return;
|
||||
}
|
||||
|
||||
usingDirectives.Add("global::ComputeSharp.D2D1.Interop");
|
||||
_ = usingDirectives.Add("global::ComputeSharp.D2D1.Interop");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace ComputeSharp.SourceGeneration.Models;
|
||||
/// </summary>
|
||||
/// <param name="FieldPath">The path of the field with respect to the shader instance.</param>
|
||||
/// <param name="TypeName">The full metadata name for the primitive type.</param>
|
||||
internal abstract record FieldInfo(EquatableArray<string> FieldPath, string TypeName)
|
||||
internal abstract record FieldInfo(EquatableArray<FieldPathPart> FieldPath, string TypeName)
|
||||
{
|
||||
/// <summary>
|
||||
/// A captured primitive value (either a scalar, a vector, or a linear matrix).
|
||||
@@ -15,7 +15,7 @@ internal abstract record FieldInfo(EquatableArray<string> FieldPath, string Type
|
||||
/// <param name="FieldPath"><inheritdoc cref="FieldInfo(EquatableArray{string}, string)" path="/param[@name='FieldPath']/node()"/></param>
|
||||
/// <param name="TypeName"><inheritdoc cref="FieldInfo(EquatableArray{string}, string)" path="/param[@name='TypeName']/node()"/></param>
|
||||
/// <param name="Offset">The byte offset of the value within the root signature.</param>
|
||||
public sealed record Primitive(EquatableArray<string> FieldPath, string TypeName, int Offset) : FieldInfo(FieldPath, TypeName);
|
||||
public sealed record Primitive(EquatableArray<FieldPathPart> FieldPath, string TypeName, int Offset) : FieldInfo(FieldPath, TypeName);
|
||||
|
||||
/// <summary>
|
||||
/// A captured non linear matrix value.
|
||||
@@ -27,10 +27,33 @@ internal abstract record FieldInfo(EquatableArray<string> FieldPath, string Type
|
||||
/// <param name="Columns">The number of columns in the matrix.</param>
|
||||
/// <param name="Offsets">The sequence of byte offsets for each row within the root signature.</param>
|
||||
public sealed record NonLinearMatrix(
|
||||
EquatableArray<string> FieldPath,
|
||||
EquatableArray<FieldPathPart> FieldPath,
|
||||
string TypeName,
|
||||
string ElementName,
|
||||
int Rows,
|
||||
int Columns,
|
||||
EquatableArray<int> Offsets) : FieldInfo(FieldPath, TypeName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A model for a part of a field path, ie. the name of the immediate parent field, and whether it's accessible.
|
||||
/// </summary>
|
||||
/// <param name="Name">The name of the immediate parent field to access (or self).</param>
|
||||
/// <param name="IsAccessible">Whether the current path part is accessible from the compilation.</param>
|
||||
internal abstract record FieldPathPart(string Name, bool IsAccessible)
|
||||
{
|
||||
/// <summary>
|
||||
/// A model for a leaf part of a field path.
|
||||
/// </summary>
|
||||
/// <param name="Name"><inheritdoc cref="FieldPathPart(string, bool)" path="/param[@name='Name']/node()"/></param>
|
||||
/// <param name="IsAccessible"><inheritdoc cref="FieldPathPart(string, bool)" path="/param[@name='IsAccessible']/node()"/></param>
|
||||
public sealed record Leaf(string Name, bool IsAccessible) : FieldPathPart(Name, IsAccessible);
|
||||
|
||||
/// <summary>
|
||||
/// A model for a nested field part path (ie. going through a nested struct type).
|
||||
/// </summary>
|
||||
/// <param name="Name"><inheritdoc cref="FieldPathPart(string, bool)" path="/param[@name='Name']/node()"/></param>
|
||||
/// <param name="IsAccessible"><inheritdoc cref="FieldPathPart(string, bool)" path="/param[@name='IsAccessible']/node()"/></param>
|
||||
/// <param name="TypeName">The fully qualified type name of the containing type.</param>
|
||||
public sealed record Nested(string Name, bool IsAccessible, string TypeName) : FieldPathPart(Name, IsAccessible);
|
||||
}
|
||||
@@ -49,10 +49,13 @@ internal static class ISymbolExtensions
|
||||
/// Gets the fully qualified name for a given symbol.
|
||||
/// </summary>
|
||||
/// <param name="symbol">The input <see cref="ISymbol"/> instance.</param>
|
||||
/// <param name="includeGlobal">Whether to include the <c>global::</c> prefix.</param>
|
||||
/// <returns>The fully qualified name for <paramref name="symbol"/>.</returns>
|
||||
public static string GetFullyQualifiedName(this ISymbol symbol)
|
||||
public static string GetFullyQualifiedName(this ISymbol symbol, bool includeGlobal = false)
|
||||
{
|
||||
return symbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat);
|
||||
return includeGlobal
|
||||
? symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)
|
||||
: symbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -44,9 +44,9 @@ internal struct ImmutableHashSetBuilder<T> : IDisposable
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ImmutableArray{T}.Builder.Add(T)"/>
|
||||
public readonly void Add(T item)
|
||||
public readonly bool Add(T item)
|
||||
{
|
||||
_ = this.set!.Add(item);
|
||||
return this.set!.Add(item);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
+5
-4
@@ -1,3 +1,4 @@
|
||||
using System.Linq;
|
||||
using ComputeSharp.SourceGeneration.Extensions;
|
||||
using ComputeSharp.SourceGeneration.Helpers;
|
||||
using ComputeSharp.SourceGeneration.Models;
|
||||
@@ -44,19 +45,19 @@ partial class ComputeShaderDescriptorGenerator
|
||||
// Read a boolean value and cast it to Bool first, which will apply the correct size expansion
|
||||
writer.WriteLine(
|
||||
$"global::System.Runtime.CompilerServices.Unsafe.As<byte, global::ComputeSharp.Bool>(ref span[{primitive.Offset}]) = " +
|
||||
$"(global::ComputeSharp.Bool)shader.{string.Join(".", primitive.FieldPath)};");
|
||||
$"(global::ComputeSharp.Bool)shader.{string.Join(".", primitive.FieldPath.Select(static path => path.Name))};");
|
||||
break;
|
||||
case FieldInfo.Primitive primitive:
|
||||
|
||||
// Read a primitive value and serialize it into the target buffer
|
||||
writer.WriteLine(
|
||||
$"global::System.Runtime.CompilerServices.Unsafe.As<byte, global::{primitive.TypeName}>(ref span[{primitive.Offset}]) = " +
|
||||
$"shader.{string.Join(".", primitive.FieldPath)};");
|
||||
$"shader.{string.Join(".", primitive.FieldPath.Select(static path => path.Name))};");
|
||||
break;
|
||||
|
||||
case FieldInfo.NonLinearMatrix matrix:
|
||||
string rowTypeName = $"global::ComputeSharp.{matrix.ElementName}{matrix.Columns}";
|
||||
string rowLocalName = $"__{string.Join("_", matrix.FieldPath)}__row0";
|
||||
string rowLocalName = $"__{string.Join("_", matrix.FieldPath.Select(static path => path.Name))}__row0";
|
||||
|
||||
// Declare a local to index into individual rows. This will generate:
|
||||
//
|
||||
@@ -64,7 +65,7 @@ partial class ComputeShaderDescriptorGenerator
|
||||
// ref global::System.Runtime.CompilerServices.Unsafe.AsRef(in <FIELD_PATH>));
|
||||
writer.WriteLine(
|
||||
$"ref {rowTypeName} {rowLocalName} = ref global::System.Runtime.CompilerServices.Unsafe.As<global::{matrix.TypeName}, {rowTypeName}>(" +
|
||||
$"ref global::System.Runtime.CompilerServices.Unsafe.AsRef(in shader.{string.Join(".", matrix.FieldPath)}));");
|
||||
$"ref global::System.Runtime.CompilerServices.Unsafe.AsRef(in shader.{string.Join(".", matrix.FieldPath.Select(static path => path.Name))}));");
|
||||
|
||||
// Generate the loading code for each individual row, with proper alignment.
|
||||
// This will result in the following (assuming Float2x3 m):
|
||||
|
||||
+6
-5
@@ -43,7 +43,7 @@ partial class ComputeShaderDescriptorGenerator
|
||||
static void GetInfo(
|
||||
Compilation compilation,
|
||||
ITypeSymbol currentTypeSymbol,
|
||||
ImmutableArray<string> fieldPath,
|
||||
ImmutableArray<FieldPathPart> fieldPath,
|
||||
ref int resourceOffset,
|
||||
ref int rawDataOffset,
|
||||
ImmutableArrayBuilder<FieldInfo> fields,
|
||||
@@ -82,12 +82,13 @@ partial class ComputeShaderDescriptorGenerator
|
||||
}
|
||||
else if (HlslKnownTypes.IsKnownHlslType(typeName))
|
||||
{
|
||||
fields.Add(GetHlslKnownTypeFieldInfo(fieldPath.Add(fieldName), typeName, ref rawDataOffset));
|
||||
// TODO: share the logic and fix the field path parts (here they just always return accessible leaves for now)
|
||||
fields.Add(GetHlslKnownTypeFieldInfo(fieldPath.Add(new FieldPathPart.Leaf(fieldName, true)), typeName, ref rawDataOffset));
|
||||
}
|
||||
else if (fieldSymbol.Type.IsUnmanagedType)
|
||||
{
|
||||
// Custom struct type defined by the user
|
||||
GetInfo(compilation, fieldSymbol.Type, fieldPath.Add(fieldName), ref resourceOffset, ref rawDataOffset, fields, resources);
|
||||
GetInfo(compilation, fieldSymbol.Type, fieldPath.Add(new FieldPathPart.Leaf(fieldName, true)), ref resourceOffset, ref rawDataOffset, fields, resources);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -104,7 +105,7 @@ partial class ComputeShaderDescriptorGenerator
|
||||
GetInfo(
|
||||
compilation,
|
||||
structDeclarationSymbol,
|
||||
ImmutableArray<string>.Empty,
|
||||
ImmutableArray<FieldPathPart>.Empty,
|
||||
ref resourceOffset,
|
||||
ref rawDataOffset,
|
||||
fieldBuilder,
|
||||
@@ -144,7 +145,7 @@ partial class ComputeShaderDescriptorGenerator
|
||||
/// <param name="typeName">The type name currently being read.</param>
|
||||
/// <param name="rawDataOffset">The current offset within the loaded data buffer.</param>
|
||||
private static FieldInfo GetHlslKnownTypeFieldInfo(
|
||||
ImmutableArray<string> fieldPath,
|
||||
ImmutableArray<FieldPathPart> fieldPath,
|
||||
string typeName,
|
||||
ref int rawDataOffset)
|
||||
{
|
||||
|
||||
+7
-7
@@ -50,13 +50,13 @@ partial class ComputeShaderDescriptorGenerator
|
||||
return;
|
||||
}
|
||||
|
||||
usingDirectives.Add("global::System");
|
||||
usingDirectives.Add("global::System.Buffers");
|
||||
usingDirectives.Add("global::System.CodeDom.Compiler");
|
||||
usingDirectives.Add("global::System.Diagnostics");
|
||||
usingDirectives.Add("global::System.Diagnostics.CodeAnalysis");
|
||||
usingDirectives.Add("global::System.Runtime.CompilerServices");
|
||||
usingDirectives.Add("global::System.Runtime.InteropServices");
|
||||
_ = usingDirectives.Add("global::System");
|
||||
_ = usingDirectives.Add("global::System.Buffers");
|
||||
_ = usingDirectives.Add("global::System.CodeDom.Compiler");
|
||||
_ = usingDirectives.Add("global::System.Diagnostics");
|
||||
_ = usingDirectives.Add("global::System.Diagnostics.CodeAnalysis");
|
||||
_ = usingDirectives.Add("global::System.Runtime.CompilerServices");
|
||||
_ = usingDirectives.Add("global::System.Runtime.InteropServices");
|
||||
|
||||
// Declare the HlslBytecodeMemoryManager custom memory manager type
|
||||
static void Callback(ShaderInfo info, IndentedTextWriter writer)
|
||||
|
||||
+4
-4
@@ -49,10 +49,10 @@ partial class ComputeShaderDescriptorGenerator
|
||||
return;
|
||||
}
|
||||
|
||||
usingDirectives.Add("global::System.CodeDom.Compiler");
|
||||
usingDirectives.Add("global::System.Diagnostics");
|
||||
usingDirectives.Add("global::System.Diagnostics.CodeAnalysis");
|
||||
usingDirectives.Add("global::ComputeSharp.Interop");
|
||||
_ = usingDirectives.Add("global::System.CodeDom.Compiler");
|
||||
_ = usingDirectives.Add("global::System.Diagnostics");
|
||||
_ = usingDirectives.Add("global::System.Diagnostics.CodeAnalysis");
|
||||
_ = usingDirectives.Add("global::ComputeSharp.Interop");
|
||||
|
||||
// Declare the additional data type and the shared array with resource descriptor ranges
|
||||
static void Callback(ShaderInfo info, IndentedTextWriter writer)
|
||||
|
||||
@@ -314,6 +314,7 @@ public partial class D2D1PixelShaderTests
|
||||
float4 pixel6 = this.myTexture3.Sample(0.4f, 0.5f, 0.6f);
|
||||
float number1 = this.myTexture4[0, 2];
|
||||
float number2 = this.myTexture4.Sample(0.4f, 0.6f);
|
||||
float number3 = this.number;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -574,7 +574,7 @@ public partial class D2D1ResourceTextureManagerTests
|
||||
|
||||
D2D1PixelShaderEffect.CreateFromD2D1DeviceContext<CopyFromResourceTexture1DShader>(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf());
|
||||
|
||||
CopyFromResourceTexture1DShader shader = new(width);
|
||||
CopyFromResourceTexture1DShader shader = new();
|
||||
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(d2D1Effect.Get(), in shader);
|
||||
|
||||
@@ -626,8 +626,6 @@ public partial class D2D1ResourceTextureManagerTests
|
||||
[AutoConstructor]
|
||||
internal partial struct CopyFromResourceTexture1DShader : ID2D1PixelShader
|
||||
{
|
||||
private int width;
|
||||
|
||||
[D2DResourceTextureIndex(0)]
|
||||
private D2D1ResourceTexture1D<float> source;
|
||||
|
||||
@@ -663,7 +661,7 @@ public partial class D2D1ResourceTextureManagerTests
|
||||
|
||||
D2D1PixelShaderEffect.CreateFromD2D1DeviceContext<CopyFromResourceTexture2DShader>(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf());
|
||||
|
||||
CopyFromResourceTexture2DShader shader = new(width, height);
|
||||
CopyFromResourceTexture2DShader shader = new();
|
||||
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(d2D1Effect.Get(), in shader);
|
||||
|
||||
@@ -735,7 +733,7 @@ public partial class D2D1ResourceTextureManagerTests
|
||||
|
||||
D2D1PixelShaderEffect.CreateFromD2D1DeviceContext<CopyFromResourceTexture2DShader>(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf());
|
||||
|
||||
CopyFromResourceTexture2DShader shader = new(width, height);
|
||||
CopyFromResourceTexture2DShader shader = new();
|
||||
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(d2D1Effect.Get(), in shader);
|
||||
|
||||
@@ -796,9 +794,6 @@ public partial class D2D1ResourceTextureManagerTests
|
||||
[AutoConstructor]
|
||||
internal partial struct CopyFromResourceTexture2DShader : ID2D1PixelShader
|
||||
{
|
||||
private int width;
|
||||
private int height;
|
||||
|
||||
[D2DResourceTextureIndex(0)]
|
||||
private D2D1ResourceTexture2D<float> source;
|
||||
|
||||
@@ -839,7 +834,7 @@ public partial class D2D1ResourceTextureManagerTests
|
||||
|
||||
D2D1PixelShaderEffect.CreateFromD2D1DeviceContext<CopyFromResourceTexture3DShader>(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf());
|
||||
|
||||
CopyFromResourceTexture3DShader shader = new(width, height, depth);
|
||||
CopyFromResourceTexture3DShader shader = new(height);
|
||||
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(d2D1Effect.Get(), in shader);
|
||||
|
||||
@@ -903,9 +898,7 @@ public partial class D2D1ResourceTextureManagerTests
|
||||
[AutoConstructor]
|
||||
internal partial struct CopyFromResourceTexture3DShader : ID2D1PixelShader
|
||||
{
|
||||
private int width;
|
||||
private int height;
|
||||
private int depth;
|
||||
|
||||
[D2DResourceTextureIndex(0)]
|
||||
private D2D1ResourceTexture3D<float> source;
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする