Update constant buffer marshalling methods in generator

このコミットが含まれているのは:
Sergio Pedri committed 2023-09-30 18:38:36 +02:00
1 親 1a94d017bb
コミット 76df248427
2ファイル変更
+20 -17

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

@@ -21,27 +21,24 @@ partial class ID2D1ShaderGenerator
writer.WriteLine("/// <inheritdoc/>");
writer.WriteGeneratedAttributes(typeof(ID2D1ShaderGenerator));
writer.WriteLine($"readonly {typeName} global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{typeName}>.CreateFromConstantBuffer(global::System.ReadOnlySpan<byte> data)");
writer.WriteLine("[global::System.Runtime.CompilerServices.SkipLocalsInit]");
writer.WriteLine($"readonly unsafe {typeName} global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{typeName}>.CreateFromConstantBuffer(global::System.ReadOnlySpan<byte> data)");
using (writer.WriteBlock())
{
// If there are no fields, the method body is just empty
// If there are no fields, just return an empty shader (TODO: add length check on .NET 8)
if (info.Fields.IsEmpty)
{
writer.WriteLine("return default;");
return;
}
// Insert the fallback for empty shaders. This will generate the following code:
writer.WriteLine("if (data.IsEmpty)");
using (writer.WriteBlock())
{
writer.WriteLine("return;");
}
// Get a reference to the data through the generated native layout type
// Get a reference to the data through the generated native layout type and define the shader
writer.WriteLine($"ref readonly global::ComputeSharp.D2D1.Generated.ConstantBuffer buffer = ref global::System.Runtime.CompilerServices.Unsafe.As<byte, global::ComputeSharp.D2D1.Generated.ConstantBuffer>(ref global::System.Runtime.InteropServices.MemoryMarshal.GetReference(data));");
writer.WriteLine();
writer.WriteLine($"{typeName} shader;");
writer.WriteLine();
writer.WriteLine("ref readonly global::ComputeSharp.D2D1.Generated.ConstantBuffer buffer = ref global::System.Runtime.CompilerServices.Unsafe.As<byte, global::ComputeSharp.D2D1.Generated.ConstantBuffer>(ref global::System.Runtime.InteropServices.MemoryMarshal.GetReference(data));");
// Generate loading statements for each captured field
foreach (FieldInfo fieldInfo in info.Fields)
@@ -51,7 +48,7 @@ partial class ID2D1ShaderGenerator
case FieldInfo.Primitive primitive:
// Read a primitive value
writer.WriteLine($"global::System.Runtime.CompilerServices.Unsafe.AsRef(in this.{string.Join(".", primitive.FieldPath)}) = buffer.{string.Join("_", primitive.FieldPath)};");
writer.WriteLine($"*&shader.{string.Join(".", primitive.FieldPath)} = buffer.{string.Join("_", primitive.FieldPath)};");
break;
case FieldInfo.NonLinearMatrix matrix:
@@ -60,18 +57,22 @@ partial class ID2D1ShaderGenerator
// Read all rows of a given matrix type:
//
// global::System.Runtime.CompilerServices.Unsafe.AsRef(in this.<FIELD_PATH>)[0] = buffer.<CONSTANT_BUFFER_ROW_0_PATH>;
// global::System.Runtime.CompilerServices.Unsafe.AsRef(in this.<FIELD_PATH>)[1] = buffer.<CONSTANT_BUFFER_ROW_1_PATH>;
// (*&shader.<FIELD_PATH>)[0] = buffer.<CONSTANT_BUFFER_ROW_0_PATH>;
// (*&shader.<FIELD_PATH>)[1] = buffer.<CONSTANT_BUFFER_ROW_1_PATH>;
// ...
// global::System.Runtime.CompilerServices.Unsafe.AsRef(in this.<FIELD_PATH>)[N] = buffer.<CONSTANT_BUFFER_ROW_N_PATH>;
// (*&shader.<FIELD_PATH>)[N] = buffer.<CONSTANT_BUFFER_ROW_N_PATH>;
for (int j = 0; j < matrix.Rows; j++)
{
writer.WriteLine($"global::System.Runtime.CompilerServices.Unsafe.AsRef(in this.{fieldPath})[{j}] = buffer.{fieldNamePrefix}_{j};");
writer.WriteLine($"(*&shader.{fieldPath})[{j}] = buffer.{fieldNamePrefix}_{j};");
}
break;
}
}
// Return the populated shader
writer.WriteLine();
writer.WriteLine("return shader;");
}
}
}
@@ -22,6 +22,7 @@ partial class ID2D1ShaderGenerator
writer.WriteLine("/// <inheritdoc/>");
writer.WriteGeneratedAttributes(typeof(ID2D1ShaderGenerator));
writer.WriteLine("[global::System.Runtime.CompilerServices.SkipLocalsInit]");
writer.WriteLine($"readonly unsafe void global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{typeName}>.LoadConstantBuffer<TLoader>(in {typeName} shader, ref TLoader loader)");
using (writer.WriteBlock())
@@ -34,6 +35,7 @@ partial class ID2D1ShaderGenerator
else
{
writer.WriteLine("global::ComputeSharp.D2D1.Generated.ConstantBuffer buffer;");
writer.WriteLine();
// Generate loading statements for each captured field
foreach (FieldInfo fieldInfo in info.Fields)