Fixed HLSL compilation when using nested struct types

このコミットが含まれているのは:
Sergio Pedri committed 2021-03-11 23:12:09 +01:00
1 親 cd8fb59593
コミット 9d41cbf94c
10ファイル変更
+104 -57

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

+1 -1
ファイルの表示
@@ -155,7 +155,7 @@ namespace ComputeSharp.SourceGenerators.Extensions
return ParseTypeName(mappedName!);
}
return ParseTypeName(typeName.Replace(".", "__"));
return ParseTypeName(typeName.ToHlslIdentifierName());
}
}
}
+21
ファイルの表示
@@ -0,0 +1,21 @@
using System.Diagnostics.Contracts;
namespace ComputeSharp.SourceGenerators.Extensions
{
/// <summary>
/// Extension methods for <see cref="string"/> types.
/// </summary>
internal static class StringExtensions
{
/// <summary>
/// Converts a given <see cref="string"/> instance to a valid HLSL identifier name.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to convert.</param>
/// <returns>An valid HLSL identifier name from <paramref name="text"/>.</returns>
[Pure]
public static string ToHlslIdentifierName(this string text)
{
return text.Replace('.', '_').Replace('+', '_');
}
}
}
+2 -2
ファイルの表示
@@ -75,7 +75,7 @@ namespace ComputeSharp.SourceGenerators.Extensions
return node.ReplaceNode(targetType, newType);
}
return node.ReplaceNode(targetType, ParseTypeName(typeName.Replace(".", "__")));
return node.ReplaceNode(targetType, ParseTypeName(typeName.ToHlslIdentifierName()));
}
/// <summary>
@@ -98,7 +98,7 @@ namespace ComputeSharp.SourceGenerators.Extensions
return ParseTypeName(mappedName!);
}
return ParseTypeName(typeName.Replace(".", "__"));
return ParseTypeName(typeName.ToHlslIdentifierName());
}
/// <summary>
+3 -3
ファイルの表示
@@ -373,7 +373,7 @@ namespace ComputeSharp.SourceGenerators
{
foreach (var constant in constantDefinitions)
{
var ownerTypeName = ((INamedTypeSymbol)constant.Key.ContainingSymbol).ToDisplayString().Replace(".", "__");
var ownerTypeName = ((INamedTypeSymbol)constant.Key.ContainingSymbol).ToDisplayString().ToHlslIdentifierName();
var constantName = $"__{ownerTypeName}__{constant.Key.Name}";
yield return new string[] { constantName, constant.Value };
@@ -389,7 +389,7 @@ namespace ComputeSharp.SourceGenerators
{
foreach (var type in HlslKnownTypes.GetCustomTypes(types))
{
var structType = type.GetFullMetadataName().Replace(".", "__");
var structType = type.GetFullMetadataName().ToHlslIdentifierName();
var structDeclaration = StructDeclaration(structType);
// Declare the fields of the current type
@@ -400,7 +400,7 @@ namespace ComputeSharp.SourceGenerators
// Convert the name to the fully qualified HLSL version
if (!HlslKnownTypes.TryGetMappedName(fieldType.GetFullMetadataName(), out string? mapped))
{
mapped = fieldType.GetFullMetadataName().Replace(".", "__");
mapped = fieldType.GetFullMetadataName().ToHlslIdentifierName();
}
structDeclaration = structDeclaration.AddMembers(
+3 -3
ファイルの表示
@@ -230,7 +230,7 @@ namespace ComputeSharp.SourceGenerators.Mappings
// If the current type is a custom type, format it as needed
if (!KnownHlslTypes.TryGetValue(genericArgumentName, out string? mappedElementType))
{
mappedElementType = genericArgumentName.Replace(".", "__");
mappedElementType = genericArgumentName.ToHlslIdentifierName();
}
// Construct the HLSL type name
@@ -262,7 +262,7 @@ namespace ComputeSharp.SourceGenerators.Mappings
}
// The captured field is of a custom struct type
return typeName.Replace(".", "__");
return typeName.ToHlslIdentifierName();
}
/// <summary>
@@ -280,7 +280,7 @@ namespace ComputeSharp.SourceGenerators.Mappings
return mapped;
}
return elementTypeName.Replace(".", "__");
return elementTypeName.ToHlslIdentifierName();
}
/// <summary>
+1 -1
ファイルの表示
@@ -260,7 +260,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
{
ConstantDefinitions[operation.Field] = ((IFormattable)operation.Field.ConstantValue!).ToString(null, CultureInfo.InvariantCulture);
var ownerTypeName = ((INamedTypeSymbol)operation.Field.ContainingSymbol).ToDisplayString().Replace(".", "__");
var ownerTypeName = ((INamedTypeSymbol)operation.Field.ContainingSymbol).ToDisplayString().ToHlslIdentifierName();
var constantName = $"__{ownerTypeName}__{operation.Field.Name}";
return IdentifierName(constantName);
+2 -2
ファイルの表示
@@ -260,7 +260,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
{
ConstantDefinitions[fieldOperation.Field] = ((IFormattable)fieldOperation.Field.ConstantValue!).ToString(null, CultureInfo.InvariantCulture);
var ownerTypeName = ((INamedTypeSymbol)fieldOperation.Field.ContainingSymbol).ToDisplayString().Replace(".", "__");
var ownerTypeName = ((INamedTypeSymbol)fieldOperation.Field.ContainingSymbol).ToDisplayString().ToHlslIdentifierName();
var constantName = $"__{ownerTypeName}__{fieldOperation.Field.Name}";
return IdentifierName(constantName);
@@ -398,7 +398,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
// type as well, as they will be processed by the generator in a different path.
if (!SymbolEqualityComparer.Default.Equals(this.shaderType, method.ContainingType))
{
var methodIdentifier = method.GetFullMetadataName().Replace(".", "__");
var methodIdentifier = method.GetFullMetadataName().ToHlslIdentifierName();
if (!this.staticMethods.ContainsKey(method))
{
+1 -1
ファイルの表示
@@ -60,7 +60,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
{
ConstantDefinitions[fieldOperation.Field] = ((IFormattable)fieldOperation.Field.ConstantValue!).ToString(null, CultureInfo.InvariantCulture);
var ownerTypeName = ((INamedTypeSymbol)fieldOperation.Field.ContainingSymbol).ToDisplayString().Replace(".", "__");
var ownerTypeName = ((INamedTypeSymbol)fieldOperation.Field.ContainingSymbol).ToDisplayString().ToHlslIdentifierName();
var constantName = $"__{ownerTypeName}__{fieldOperation.Field.Name}";
return IdentifierName(constantName);
+38 -38
ファイルの表示
@@ -177,6 +177,19 @@ namespace ComputeSharp.Tests.Internals
Assert.AreEqual(9999, *(int*)&p1[120]);
}
[AutoConstructor]
public partial struct SimpleTypes
{
public Float2x3 f2x3;
public int a;
public Int1x3 i1x3;
public Double2 d2;
public int c;
public Int1x2 i1x2;
public Int2x2 i2x2;
public int d;
}
[AutoConstructor]
public partial struct FlatCustomTypeShader : IComputeShader
{
@@ -236,6 +249,31 @@ namespace ComputeSharp.Tests.Internals
Assert.AreEqual(9999, *(int*)&p1[120]);
}
[AutoConstructor]
public partial struct CustomType1
{
public Float3 a;
public SimpleTypes b;
public int c;
public CustomType2 d;
public CustomType3 e;
}
[AutoConstructor]
public partial struct CustomType2
{
public int a;
public Float2 b;
public CustomType3 c;
}
[AutoConstructor]
public partial struct CustomType3
{
public int a;
public Float2 b;
}
[AutoConstructor]
public partial struct NestedCustomTypesShader : IComputeShader
{
@@ -320,42 +358,4 @@ namespace ComputeSharp.Tests.Internals
Assert.AreEqual(444.4f, *(float*)&p1[184]);
}
}
[AutoConstructor]
public partial struct SimpleTypes
{
public Float2x3 f2x3;
public int a;
public Int1x3 i1x3;
public Double2 d2;
public int c;
public Int1x2 i1x2;
public Int2x2 i2x2;
public int d;
}
[AutoConstructor]
public partial struct CustomType1
{
public Float3 a;
public SimpleTypes b;
public int c;
public CustomType2 d;
public CustomType3 e;
}
[AutoConstructor]
public partial struct CustomType2
{
public int a;
public Float2 b;
public CustomType3 c;
}
[AutoConstructor]
public partial struct CustomType3
{
public int a;
public Float2 b;
}
}
+32 -6
ファイルの表示
@@ -11,11 +11,11 @@ namespace ComputeSharp.Tests
[TestMethod]
public void ReservedKeywords()
{
ReflectionServices.GetShaderInfo<ReservedKeywords_Kernel>(out _);
ReflectionServices.GetShaderInfo<ReservedKeywordsShader>(out _);
}
[AutoConstructor]
public readonly partial struct ReservedKeywords_Kernel : IComputeShader
public readonly partial struct ReservedKeywordsShader : IComputeShader
{
public readonly ReadWriteBuffer<float> row_major;
public readonly float dword;
@@ -32,11 +32,11 @@ namespace ComputeSharp.Tests
[TestMethod]
public void SpecialTypeAsReturnType()
{
ReflectionServices.GetShaderInfo<SpecialTypeAsReturnType_Kernel>(out _);
ReflectionServices.GetShaderInfo<SpecialTypeAsReturnTypeShader>(out _);
}
[AutoConstructor]
public readonly partial struct SpecialTypeAsReturnType_Kernel : IComputeShader
public readonly partial struct SpecialTypeAsReturnTypeShader : IComputeShader
{
public readonly ReadWriteBuffer<Float2> buffer;
@@ -53,11 +53,11 @@ namespace ComputeSharp.Tests
[TestMethod]
public void LocalFunctionInExternalMethods()
{
ReflectionServices.GetShaderInfo<LocalFunctionInExternalMethods_Kernel>(out _);
ReflectionServices.GetShaderInfo<LocalFunctionInExternalMethodsShader>(out _);
}
[AutoConstructor]
public readonly partial struct LocalFunctionInExternalMethods_Kernel : IComputeShader
public readonly partial struct LocalFunctionInExternalMethodsShader : IComputeShader
{
public readonly ReadWriteBuffer<Float2> buffer;
@@ -73,5 +73,31 @@ namespace ComputeSharp.Tests
buffer[ThreadIds.X] = Foo(ThreadIds.X);
}
}
[TestMethod]
public void CapturedNestedStructType()
{
ReflectionServices.GetShaderInfo<CapturedNestedStructTypeShader>(out _);
}
[AutoConstructor]
public partial struct CustomStructType
{
public Float2 a;
public int b;
}
[AutoConstructor]
public readonly partial struct CapturedNestedStructTypeShader : IComputeShader
{
public readonly ReadWriteBuffer<float> buffer;
public readonly CustomStructType foo;
/// <inheritdoc/>
public void Execute()
{
buffer[ThreadIds.X] *= foo.a.X + foo.b;
}
}
}
}