Merge pull request #729 from Sergio0694/dev/this-expressions

Support 'this.' expressions within custom types
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2023-12-21 18:46:51 +00:00
コミット 4a016feffa
4ファイル変更
+101 -6

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

+12 -2
ファイルの表示
@@ -383,10 +383,20 @@ internal sealed partial class ShaderSourceRewriter(
return IdentifierName(constantName);
}
// If the member access is a this.<FIELD> access, rewrite it to strip "this."
// Special handling for 'this.' expressions, which are not always allowed in HLSL
if (node.Expression.IsKind(SyntaxKind.ThisExpression))
{
return updatedNode.Name;
// If the member access is a this.<FIELD> access, rewrite it to strip 'this.', but only if accessing instance
// fields from the shader type itself (as using 'this.' in that case is not allowed in HLSL, and also it's not
// really useful anyway given that those fields are readonly, so less likely to cause observable conflicts).
if (SymbolEqualityComparer.Default.Equals(fieldOperation.Field.ContainingType, this.shaderType))
{
return updatedNode.Name;
}
// For all other cases, we can keep it in, as HLSL does allow using it to access instance fields of custom
// struct types. This is allowed by both FXC and DXC, on all shader profiles, so it's always safe to do.
return updatedNode;
}
// Handle static fields, which can be either in the shader type itself, or in external types
+2 -2
ファイルの表示
@@ -6,9 +6,9 @@ public struct ColorWrapperUsingExternalType
{
private float4 value;
public ColorWrapperUsingExternalType(float4 color)
public ColorWrapperUsingExternalType(float4 value)
{
this.value = color;
this.value = value;
}
public readonly float4 Invert()
+2 -2
ファイルの表示
@@ -6,9 +6,9 @@ public struct ColorWrapper
{
private float4 value;
public ColorWrapper(float4 color)
public ColorWrapper(float4 value)
{
this.value = color;
this.value = value;
}
public float4 Invert()
+85
ファイルの表示
@@ -1193,6 +1193,91 @@ namespace ComputeSharp.Tests
return x * 4;
}
}
// See https://github.com/Sergio0694/ComputeSharp/issues/726
[TestMethod]
public void ShaderUsingThisExpressions_IsProcessedCorrectly()
{
ShaderInfo info = ReflectionServices.GetShaderInfo<ShaderUsingThisExpressions>();
Assert.AreEqual(
"""
// ================================================
// AUTO GENERATED
// ================================================
// This shader was created by ComputeSharp.
// See: https://github.com/Sergio0694/ComputeSharp.
#define __GroupSize__get_X 64
#define __GroupSize__get_Y 1
#define __GroupSize__get_Z 1
struct ComputeSharp_Tests_ShaderCompilerTests_ShaderUsingThisExpressions_Data;
struct ComputeSharp_Tests_ShaderCompilerTests_ShaderUsingThisExpressions_Data
{
int value;
void SetValue(int value);
};
cbuffer _ : register(b0)
{
uint __x;
uint __y;
uint __z;
float alpha;
}
RWStructuredBuffer<float4> __reserved__buffer : register(u0);
void ComputeSharp_Tests_ShaderCompilerTests_ShaderUsingThisExpressions_Data::SetValue(int value)
{
this.value = value;
}
[NumThreads(__GroupSize__get_X, __GroupSize__get_Y, __GroupSize__get_Z)]
void Execute(uint3 ThreadIds : SV_DispatchThreadID)
{
if (ThreadIds.x < __x && ThreadIds.y < __y && ThreadIds.z < __z)
{
ComputeSharp_Tests_ShaderCompilerTests_ShaderUsingThisExpressions_Data data = (ComputeSharp_Tests_ShaderCompilerTests_ShaderUsingThisExpressions_Data)0;
data.SetValue(3);
__reserved__buffer[ThreadIds.x] = float4(data.value, data.value, data.value, alpha);
}
}
""",
info.HlslSource);
}
[AutoConstructor]
[ThreadGroupSize(DefaultThreadGroupSizes.X)]
[GeneratedComputeShaderDescriptor]
internal readonly partial struct ShaderUsingThisExpressions : IComputeShader
{
public readonly ReadWriteBuffer<float4> buffer;
public readonly float alpha;
private struct Data
{
public int value;
public void SetValue(int value)
{
this.value = value;
}
}
public void Execute()
{
Data data = default;
data.SetValue(3);
// The 'this.' expression for shader captured values should be stripped.
// The one for accessing struct instance members, however, should not be.
this.buffer[ThreadIds.X] = new float4(data.value, data.value, data.value, this.alpha);
}
}
}
}