diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInitializeFromDispatchDataMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInitializeFromDispatchDataMethod.Syntax.cs
index 67398f84..ec970a6e 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInitializeFromDispatchDataMethod.Syntax.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInitializeFromDispatchDataMethod.Syntax.cs
@@ -21,27 +21,24 @@ partial class ID2D1ShaderGenerator
writer.WriteLine("/// ");
writer.WriteGeneratedAttributes(typeof(ID2D1ShaderGenerator));
- writer.WriteLine($"readonly {typeName} global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{typeName}>.CreateFromConstantBuffer(global::System.ReadOnlySpan data)");
+ writer.WriteLine("[global::System.Runtime.CompilerServices.SkipLocalsInit]");
+ writer.WriteLine($"readonly unsafe {typeName} global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{typeName}>.CreateFromConstantBuffer(global::System.ReadOnlySpan 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(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(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.)[0] = buffer.;
- // global::System.Runtime.CompilerServices.Unsafe.AsRef(in this.)[1] = buffer.;
+ // (*&shader.)[0] = buffer.;
+ // (*&shader.)[1] = buffer.;
// ...
- // global::System.Runtime.CompilerServices.Unsafe.AsRef(in this.)[N] = buffer.;
+ // (*&shader.)[N] = buffer.;
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;");
}
}
}
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs
index f4301f36..f327dded 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs
@@ -22,6 +22,7 @@ partial class ID2D1ShaderGenerator
writer.WriteLine("/// ");
writer.WriteGeneratedAttributes(typeof(ID2D1ShaderGenerator));
+ writer.WriteLine("[global::System.Runtime.CompilerServices.SkipLocalsInit]");
writer.WriteLine($"readonly unsafe void global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{typeName}>.LoadConstantBuffer(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)