Handle member accesses for static shader fields

このコミットが含まれているのは:
Sergio Pedri committed 2023-12-06 21:12:42 +01:00
1 親 f844452072
コミット c0f1ad6a1e
2ファイル変更
+29 -8

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

+18 -8
ファイルの表示
@@ -368,12 +368,22 @@ internal sealed partial class ShaderSourceRewriter(
return updatedNode.Name;
}
// Handle static fields, but only if they're not in the shader type itself. If that is the case, they will already
// be discovered separately, when crawling all members of the shader type. Here we only gather external fields.
if (fieldOperation.Field.IsStatic &&
!SymbolEqualityComparer.Default.Equals(fieldOperation.Field.ContainingType, this.shaderType))
// Handle static fields, which can be either in the shader type itself, or in external types
if (fieldOperation.Field.IsStatic)
{
if (!StaticFieldDefinitions.TryGetValue(fieldOperation.Field, out (string Name, string, string?) info))
(string Name, string, string?) fieldInfo;
// Special case static fields in the shader type itself. Those are already discovered separately, when crawling
// all members of the shader type. But, if this path was hit, it means that they are accessed via a member access
// expression, which means they need to be rewritten to match the simple name they will have in the shader. This
// is often the case if they're accessed from external types. So we just map the name and use that expression.
if (SymbolEqualityComparer.Default.Equals(fieldOperation.Field.ContainingType, this.shaderType))
{
_ = HlslKnownKeywords.TryGetMappedName(fieldOperation.Field.Name, out string? mappedFieldName);
return IdentifierName(mappedFieldName ?? fieldOperation.Field.Name);
}
else if (!StaticFieldDefinitions.TryGetValue(fieldOperation.Field, out fieldInfo))
{
// Execute the same logic as for shader static fields, to process them and extract the relevant info.
// In this case, we use the fully qualified name of the field as identifier, not just the field name.
@@ -391,9 +401,9 @@ internal sealed partial class ShaderSourceRewriter(
out string? assignmentExpression,
out _))
{
info.Name = fieldOperation.Field.GetFullyQualifiedMetadataName().ToHlslIdentifierName();
fieldInfo.Name = fieldOperation.Field.GetFullyQualifiedMetadataName().ToHlslIdentifierName();
StaticFieldDefinitions.Add(fieldOperation.Field, (info.Name, typeDeclaration, assignmentExpression));
StaticFieldDefinitions.Add(fieldOperation.Field, (fieldInfo.Name, typeDeclaration, assignmentExpression));
}
else
{
@@ -403,7 +413,7 @@ internal sealed partial class ShaderSourceRewriter(
}
}
return IdentifierName(info.Name);
return IdentifierName(fieldInfo.Name);
}
}
+11
ファイルの表示
@@ -507,6 +507,8 @@ public partial class ShaderRewriterTests
Assert.AreEqual(444, results[9]);
Assert.AreEqual(123, results[10]);
Assert.AreEqual(666, results[11]);
Assert.AreEqual(3.14f, results[12]);
Assert.AreEqual(3.14f, results[13]);
}
[AutoConstructor]
@@ -545,6 +547,10 @@ public partial class ShaderRewriterTests
buffer[9] = C;
buffer[10] = ExternalType.B;
buffer[11] = ExternalType.C;
// Access shader fields via a member access
buffer[12] = MixedStaticFieldsInExternalTypesShader.PI;
buffer[13] = ExternalType.ReadPI();
}
}
@@ -554,6 +560,11 @@ public partial class ShaderRewriterTests
public static readonly int A = 42;
public static int B;
public static int C = 333;
public static float ReadPI()
{
return MixedStaticFieldsInExternalTypesShader.PI;
}
}
// See https://github.com/Sergio0694/ComputeSharp/issues/278