diff --git a/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs b/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs
index f89b5aa1..79f2b1d4 100644
--- a/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs
+++ b/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs
@@ -155,7 +155,7 @@ namespace ComputeSharp.SourceGenerators.Extensions
return ParseTypeName(mappedName!);
}
- return ParseTypeName(typeName.Replace(".", "__"));
+ return ParseTypeName(typeName.ToHlslIdentifierName());
}
}
}
diff --git a/src/ComputeSharp.SourceGenerators/Extensions/StringExtensions.cs b/src/ComputeSharp.SourceGenerators/Extensions/StringExtensions.cs
new file mode 100644
index 00000000..8a84730d
--- /dev/null
+++ b/src/ComputeSharp.SourceGenerators/Extensions/StringExtensions.cs
@@ -0,0 +1,21 @@
+using System.Diagnostics.Contracts;
+
+namespace ComputeSharp.SourceGenerators.Extensions
+{
+ ///
+ /// Extension methods for types.
+ ///
+ internal static class StringExtensions
+ {
+ ///
+ /// Converts a given instance to a valid HLSL identifier name.
+ ///
+ /// The input instance to convert.
+ /// An valid HLSL identifier name from .
+ [Pure]
+ public static string ToHlslIdentifierName(this string text)
+ {
+ return text.Replace('.', '_').Replace('+', '_');
+ }
+ }
+}
diff --git a/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs b/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs
index 3a29bc8d..a047f37f 100644
--- a/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs
+++ b/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs
@@ -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()));
}
///
@@ -98,7 +98,7 @@ namespace ComputeSharp.SourceGenerators.Extensions
return ParseTypeName(mappedName!);
}
- return ParseTypeName(typeName.Replace(".", "__"));
+ return ParseTypeName(typeName.ToHlslIdentifierName());
}
///
diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
index 62943503..23177ccc 100644
--- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
@@ -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(
diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs
index 2522b6ac..812040df 100644
--- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs
+++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs
@@ -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();
}
///
@@ -280,7 +280,7 @@ namespace ComputeSharp.SourceGenerators.Mappings
return mapped;
}
- return elementTypeName.Replace(".", "__");
+ return elementTypeName.ToHlslIdentifierName();
}
///
diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs
index c82554d9..c5c5867d 100644
--- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs
+++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs
@@ -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);
diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
index 55579e2c..d8a70895 100644
--- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
+++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
@@ -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))
{
diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs
index 644bf065..b4c39a0e 100644
--- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs
+++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs
@@ -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);
diff --git a/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs b/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs
index 0d2a7f45..f6b11808 100644
--- a/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs
+++ b/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs
@@ -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;
- }
}
diff --git a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs
index 09faf240..a856dfb6 100644
--- a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs
+++ b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs
@@ -11,11 +11,11 @@ namespace ComputeSharp.Tests
[TestMethod]
public void ReservedKeywords()
{
- ReflectionServices.GetShaderInfo(out _);
+ ReflectionServices.GetShaderInfo(out _);
}
[AutoConstructor]
- public readonly partial struct ReservedKeywords_Kernel : IComputeShader
+ public readonly partial struct ReservedKeywordsShader : IComputeShader
{
public readonly ReadWriteBuffer row_major;
public readonly float dword;
@@ -32,11 +32,11 @@ namespace ComputeSharp.Tests
[TestMethod]
public void SpecialTypeAsReturnType()
{
- ReflectionServices.GetShaderInfo(out _);
+ ReflectionServices.GetShaderInfo(out _);
}
[AutoConstructor]
- public readonly partial struct SpecialTypeAsReturnType_Kernel : IComputeShader
+ public readonly partial struct SpecialTypeAsReturnTypeShader : IComputeShader
{
public readonly ReadWriteBuffer buffer;
@@ -53,11 +53,11 @@ namespace ComputeSharp.Tests
[TestMethod]
public void LocalFunctionInExternalMethods()
{
- ReflectionServices.GetShaderInfo(out _);
+ ReflectionServices.GetShaderInfo(out _);
}
[AutoConstructor]
- public readonly partial struct LocalFunctionInExternalMethods_Kernel : IComputeShader
+ public readonly partial struct LocalFunctionInExternalMethodsShader : IComputeShader
{
public readonly ReadWriteBuffer buffer;
@@ -73,5 +73,31 @@ namespace ComputeSharp.Tests
buffer[ThreadIds.X] = Foo(ThreadIds.X);
}
}
+
+ [TestMethod]
+ public void CapturedNestedStructType()
+ {
+ ReflectionServices.GetShaderInfo(out _);
+ }
+
+ [AutoConstructor]
+ public partial struct CustomStructType
+ {
+ public Float2 a;
+ public int b;
+ }
+
+ [AutoConstructor]
+ public readonly partial struct CapturedNestedStructTypeShader : IComputeShader
+ {
+ public readonly ReadWriteBuffer buffer;
+ public readonly CustomStructType foo;
+
+ ///
+ public void Execute()
+ {
+ buffer[ThreadIds.X] *= foo.a.X + foo.b;
+ }
+ }
}
}