diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/ConstantBufferSyntaxProcessor.Generation.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/ConstantBufferSyntaxProcessor.Generation.cs
index 116c13bf..9a26260e 100644
--- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/ConstantBufferSyntaxProcessor.Generation.cs
+++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/ConstantBufferSyntaxProcessor.Generation.cs
@@ -126,20 +126,21 @@ partial class ConstantBufferSyntaxProcessor
using (writer.WriteBlock())
{
- // Define the ToManaged method (native constant buffer to managed shader type)
+ // Define the FromManaged method (managed shader type to native constant buffer)
writer.WriteLine($"""/// """);
- writer.WriteLine($"""/// Marshals native constant buffer data to managed instances.""");
+ writer.WriteLine($"""/// Marshals managed instances to native constant buffer data.""");
writer.WriteLine($"""/// """);
writer.WriteLine($"""/// The input native constant buffer.""");
writer.WriteLine($"""/// The marshalled instance.""");
writer.WriteLine($"""[MethodImpl(MethodImplOptions.AggressiveInlining)]""");
writer.WriteLine($"""[SkipLocalsInit]""");
- writer.WriteLine($"public static {fullyQualifiedTypeName} ToManaged(in ConstantBuffer buffer)");
+ writer.WriteLine($"public static void FromManaged(in {fullyQualifiedTypeName} shader, out ConstantBuffer buffer)");
using (writer.WriteBlock())
{
- writer.WriteLine($"Unsafe.SkipInit(out {fullyQualifiedTypeName} shader);");
- writer.WriteLine();
+ // Initialize any artificial fields, if needed. This is necessary to avoid compiler errors due to
+ // the resulting constant buffer not being fully initialized, in case it had some extra fields.
+ InitializeArtificialFields(info, writer);
// Generate loading statements for each captured field
foreach (FieldInfo fieldInfo in info.Fields)
@@ -148,9 +149,10 @@ partial class ConstantBufferSyntaxProcessor
{
case FieldInfo.Primitive primitive:
- // Read a nested primitive value
+ // Assign a primitive value
+ writer.Write($"buffer.{string.Join("_", primitive.FieldPath.Select(static path => path.Name))} = ");
writer.WriteFieldAccessExpression(fieldInfo);
- writer.WriteLine($" = buffer.{string.Join("_", primitive.FieldPath.Select(static path => path.Name))};");
+ writer.WriteLine(";");
break;
case FieldInfo.NonLinearMatrix matrix:
@@ -166,37 +168,37 @@ partial class ConstantBufferSyntaxProcessor
writer.WriteLine(";");
writer.WriteLine();
- // Read all rows of a given matrix type
+ // Assign all rows of a given matrix type
for (int j = 0; j < matrix.Rows; j++)
{
- writer.WriteLine($"Unsafe.As<{primitiveTypeName}, {rowTypeName}>(ref __{fieldNamePrefix}.M{j + 1}1) = buffer.{fieldNamePrefix}_{j};");
+ writer.WriteLine($"buffer.{fieldNamePrefix}_{j} = Unsafe.As<{primitiveTypeName}, {rowTypeName}>(ref __{fieldNamePrefix}.M{j + 1}1);");
}
writer.WriteLine();
break;
}
}
-
- writer.WriteLine(skipIfPresent: true);
- writer.WriteLine("return shader;");
}
// Only generate the other direction if requested
if (ConstantBufferSyntaxProcessor.direction == BindingDirection.TwoWay)
{
- // Define the FromManaged method (managed shader type to native constant buffer)
+ // Define the ToManaged method (native constant buffer to managed shader type)
writer.WriteLine();
writer.WriteLine($"""/// """);
- writer.WriteLine($"""/// Marshals managed instances to native constant buffer data.""");
+ writer.WriteLine($"""/// Marshals native constant buffer data to managed instances.""");
writer.WriteLine($"""/// """);
writer.WriteLine($"""/// The input native constant buffer.""");
writer.WriteLine($"""/// The marshalled instance.""");
writer.WriteLine($"""[MethodImpl(MethodImplOptions.AggressiveInlining)]""");
writer.WriteLine($"""[SkipLocalsInit]""");
- writer.WriteLine($"public static void FromManaged(in {fullyQualifiedTypeName} shader, out ConstantBuffer buffer)");
+ writer.WriteLine($"public static {fullyQualifiedTypeName} ToManaged(in ConstantBuffer buffer)");
using (writer.WriteBlock())
{
+ writer.WriteLine($"Unsafe.SkipInit(out {fullyQualifiedTypeName} shader);");
+ writer.WriteLine();
+
// Generate loading statements for each captured field
foreach (FieldInfo fieldInfo in info.Fields)
{
@@ -204,10 +206,9 @@ partial class ConstantBufferSyntaxProcessor
{
case FieldInfo.Primitive primitive:
- // Assign a primitive value
- writer.Write($"buffer.{string.Join("_", primitive.FieldPath.Select(static path => path.Name))} = ");
+ // Read a nested primitive value
writer.WriteFieldAccessExpression(fieldInfo);
- writer.WriteLine(";");
+ writer.WriteLine($" = buffer.{string.Join("_", primitive.FieldPath.Select(static path => path.Name))};");
break;
case FieldInfo.NonLinearMatrix matrix:
@@ -223,16 +224,19 @@ partial class ConstantBufferSyntaxProcessor
writer.WriteLine(";");
writer.WriteLine();
- // Assign all rows of a given matrix type
+ // Read all rows of a given matrix type
for (int j = 0; j < matrix.Rows; j++)
{
- writer.WriteLine($"buffer.{fieldNamePrefix}_{j} = Unsafe.As<{primitiveTypeName}, {rowTypeName}>(ref __{fieldNamePrefix}.M{j + 1}1);");
+ writer.WriteLine($"Unsafe.As<{primitiveTypeName}, {rowTypeName}>(ref __{fieldNamePrefix}.M{j + 1}1) = buffer.{fieldNamePrefix}_{j};");
}
writer.WriteLine();
break;
}
}
+
+ writer.WriteLine(skipIfPresent: true);
+ writer.WriteLine("return shader;");
}
}
@@ -313,6 +317,14 @@ partial class ConstantBufferSyntaxProcessor
/// The instance to write into.
/// This method is responsible for leaving trailing blanklines, if it adds any fields.
static partial void AppendArtificialFields(IConstantBufferInfo info, IndentedTextWriter writer);
+
+ ///
+ /// Initializes any artificial fields that have been generated into the constant buffer.
+ ///
+ /// The instance to use to extract info.
+ /// The instance to write into.
+ /// This method is responsible for leaving trailing blanklines, if it emits any statements.
+ static partial void InitializeArtificialFields(IConstantBufferInfo info, IndentedTextWriter writer);
}
///
diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ConstantBuffer.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ConstantBuffer.cs
index 48e18ec3..670a07c8 100644
--- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ConstantBuffer.cs
+++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ConstantBuffer.cs
@@ -59,7 +59,7 @@ partial class ComputeShaderDescriptorGenerator
writer.WriteLine("/// ");
writer.WriteGeneratedAttributes(GeneratorName);
writer.WriteLine("[global::System.Runtime.CompilerServices.SkipLocalsInit]");
- writer.WriteLine($"static void global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{typeName}>.LoadConstantBuffer(in {typeName} shader, ref TLoader loader, int x, int y, int z)");
+ writer.WriteLine($"static unsafe void global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{typeName}>.LoadConstantBuffer(in {typeName} shader, ref TLoader loader, int x, int y, int z)");
using (writer.WriteBlock())
{
diff --git a/src/ComputeSharp.SourceGenerators/SyntaxProcessors/ConstantBufferSyntaxProcessor.ArtificialFields.cs b/src/ComputeSharp.SourceGenerators/SyntaxProcessors/ConstantBufferSyntaxProcessor.ArtificialFields.cs
index 1db0d28f..fbe5c168 100644
--- a/src/ComputeSharp.SourceGenerators/SyntaxProcessors/ConstantBufferSyntaxProcessor.ArtificialFields.cs
+++ b/src/ComputeSharp.SourceGenerators/SyntaxProcessors/ConstantBufferSyntaxProcessor.ArtificialFields.cs
@@ -33,4 +33,14 @@ partial class ConstantBufferSyntaxProcessor
// Leave a trailing blank line if the shader has any fields
writer.WriteLineIf(!info.Fields.IsEmpty);
}
+
+ ///
+ static partial void InitializeArtificialFields(IConstantBufferInfo info, IndentedTextWriter writer)
+ {
+ // Ignore the artificial fields (they will be set separately)
+ writer.WriteLine("Unsafe.SkipInit(out buffer.__x);");
+ writer.WriteLine("Unsafe.SkipInit(out buffer.__y);");
+ writer.WriteLineIf(!((ShaderInfo)info).IsPixelShaderLike, "Unsafe.SkipInit(out buffer.__z);");
+ writer.WriteLineIf(!info.Fields.IsEmpty);
+ }
}
\ No newline at end of file
diff --git a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs
index 79a8b2a8..bd9ad46b 100644
--- a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs
+++ b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs
@@ -293,7 +293,7 @@ namespace ComputeSharp.Tests
static int C() => 1;
- int D() => A() + E() + F();
+ public int D() => A() + E() + F();
int E() => 1;
diff --git a/tests/ComputeSharp.Tests/Shaders/Compute/TriangleGridContouring.cs b/tests/ComputeSharp.Tests/Shaders/Compute/TriangleGridContouring.cs
index de1e0b2c..f75fb189 100644
--- a/tests/ComputeSharp.Tests/Shaders/Compute/TriangleGridContouring.cs
+++ b/tests/ComputeSharp.Tests/Shaders/Compute/TriangleGridContouring.cs
@@ -31,12 +31,6 @@ internal readonly partial struct TriangleGridContouring : IComputeShader
return new(c, -s, s, c);
}
- // IQ's float2 to float hash.
- private static float Hash21(float2 p)
- {
- return Hlsl.Frac(Hlsl.Sin(Hlsl.Dot(p, new float2(27.619f, 57.583f))) * 43758.5453f);
- }
-
// float2 to float2 hash.
private float2 Hash22(float2 p)
{