Merge pull request #730 from Sergio0694/dev/computesharp-bool-fields

Fix support for ComputeSharp.Bool shader fields
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2023-12-21 19:43:59 +00:00
コミット 4f0b5ea4c4
3ファイル変更
+43 -4

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

@@ -81,13 +81,15 @@ partial class ConstantBufferSyntaxProcessor
{
switch (field)
{
case FieldInfo.Primitive { TypeName: "System.Boolean" } primitive:
case FieldInfo.Primitive { TypeName: "System.Boolean" or "ComputeSharp.Bool" } primitive:
// Append a field as a global::ComputeSharp.Bool value (will use the implicit conversion from bool values)
// Append a field as a Bool value for the native constant buffer layout. This is the case both for
// fields of type System.Boolean (they will use the implicit conversion from bool values), as well
// as for fields that were originally declared as Bool from the start (not recommended, but supported).
writer.WriteFieldXmlSummary(field, fullyQualifiedTypeName);
writer.WriteFieldXmlRemarks(field);
writer.WriteLine($"""[FieldOffset({primitive.Offset})]""");
writer.WriteLine($"""public global::ComputeSharp.Bool {string.Join("_", primitive.FieldPath.Select(static path => path.Name))};""");
writer.WriteLine($"""public Bool {string.Join("_", primitive.FieldPath.Select(static path => path.Name))};""");
break;
case FieldInfo.Primitive primitive:
@@ -266,6 +268,14 @@ partial class ConstantBufferSyntaxProcessor
_ => HlslKnownTypes.GetMappedName(fieldInfo.TypeName)
};
// Special case: if the field was actually of type ComputeSharp.Bool, and not System.Boolean,
// we need to preserve the original type name, and we cannot use the mapped type name (bool),
// as it would cause the generated field accessor to fail (the field type would not match).
if (fieldInfo.TypeName == "ComputeSharp.Bool")
{
typeName = "Bool";
}
// 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.
+29
ファイルの表示
@@ -1232,5 +1232,34 @@ namespace ComputeSharp.D2D1.Tests
// Just a sanity check that the generated code is present
Assert.AreEqual(0, D2D1PixelShader.GetInputCount<ContainingClass.ContainingRecord.ContainingStruct.ContainingRecordStruct.IContainingInterface.DeeplyNestedShader>());
}
// https://github.com/Sergio0694/ComputeSharp/issues/727
[TestMethod]
public unsafe void ShaderWithComputeSharpBoolInstanceField_IsMarshalledCorrectly()
{
Assert.AreEqual(4, D2D1PixelShader.GetConstantBufferSize<ShaderWithComputeSharpBoolInstanceField>());
ShaderWithComputeSharpBoolInstanceField shader = new(true);
ReadOnlyMemory<byte> memory = D2D1PixelShader.GetConstantBuffer(in shader);
ShaderWithComputeSharpBoolInstanceField roundTrip = D2D1PixelShader.CreateFromConstantBuffer<ShaderWithComputeSharpBoolInstanceField>(memory.Span);
Assert.IsTrue(shader.value);
}
[D2DInputCount(0)]
[D2DShaderProfile(D2D1ShaderProfile.PixelShader50)]
[D2DGeneratedPixelShaderDescriptor]
[AutoConstructor]
internal readonly partial struct ShaderWithComputeSharpBoolInstanceField : ID2D1PixelShader
{
public readonly Bool value;
public float4 Execute()
{
return Hlsl.BoolToFloat(this.value);
}
}
}
}
+1 -1
ファイルの表示
@@ -122,7 +122,7 @@ public partial class ShaderMembersTests
public readonly bool g;
public readonly bool2 h;
public readonly int i;
public readonly bool j;
public readonly ComputeSharp.Bool j; // See https://github.com/Sergio0694/ComputeSharp/issues/727
public void Execute()
{