Fix handling empty shaders with just artificial fields

このコミットが含まれているのは:
Sergio Pedri committed 2023-11-20 21:11:45 +01:00
1 親 777f8537f2
コミット 940cdfdcdc
4ファイル変更
+46 -13

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

@@ -0,0 +1,11 @@
namespace ComputeSharp.SourceGeneration.SyntaxProcessors;
/// <inheritdoc/>
partial class ConstantBufferSyntaxProcessor
{
/// <inheritdoc/>
private static partial bool HasArtificialFields()
{
return false;
}
}
@@ -42,8 +42,8 @@ partial class ConstantBufferSyntaxProcessor
ConstantBufferSyntaxProcessor.generatorName = generatorName;
ConstantBufferSyntaxProcessor.direction = direction;
// If there are no fields, there is no need for a constant buffer type
if (info.Fields.IsEmpty)
// If there are no fields (including artificial ones), there is no need for a constant buffer type
if (info.Fields.IsEmpty && !HasArtificialFields())
{
return;
}
@@ -307,9 +307,22 @@ partial class ConstantBufferSyntaxProcessor
}
callbacks.Add(ConstantBufferCallback);
callbacks.Add(ConstantBufferMarshallerCallback);
// Only emit the marshaller type if there are any non artificial fields.
// Otherwise, it's not needed, as there are either just no fields at all,
// or only artificial ones, which would be assigned directly by callers.
if (!info.Fields.IsEmpty)
{
callbacks.Add(ConstantBufferMarshallerCallback);
}
}
/// <summary>
/// Checks whether any artificial fields are present.
/// </summary>
/// <returns>Whether any artificial fields are present.</returns>
private static partial bool HasArtificialFields();
/// <summary>
/// Appends any artificial fields to the generated constant buffer type.
/// </summary>
+12 -10
ファイルの表示
@@ -63,23 +63,25 @@ partial class ComputeShaderDescriptorGenerator
using (writer.WriteBlock())
{
// If there are no fields, just load an empty buffer
// If there are no fields (ie. there are only artificial ones), just get an uninitialized instance.
// All fields (ie. the thread size values) will be assigned directly inline, with no marshalling.
if (info.Fields.IsEmpty)
{
writer.WriteLine("loader.LoadConstantBuffer(default);");
writer.WriteLine("global::System.Runtime.CompilerServices.Unsafe.SkipInit(out global::ComputeSharp.Generated.ConstantBuffer buffer);");
}
else
{
// Otherwise, pass a span into the marshalled native layout buffer.
// Also load the artificially created fields with dispatch data.
// Otherwise, let the generated marshaller create an initialized instance of the constant buffer type
writer.WriteLine("global::ComputeSharp.Generated.ConstantBufferMarshaller.FromManaged(in shader, out global::ComputeSharp.Generated.ConstantBuffer buffer);");
writer.WriteLine();
writer.WriteLine("buffer.__x = x;");
writer.WriteLine("buffer.__y = y;");
writer.WriteLineIf(!info.IsPixelShaderLike, "buffer.__z = z;");
writer.WriteLineIf(!info.IsPixelShaderLike);
writer.WriteLine("loader.LoadConstantBuffer(new global::System.ReadOnlySpan<byte>(&buffer, sizeof(global::ComputeSharp.Generated.ConstantBuffer)));");
}
// Initialize the artificial fields and finally pass a span over the constant buffer
writer.WriteLine();
writer.WriteLine("buffer.__x = x;");
writer.WriteLine("buffer.__y = y;");
writer.WriteLineIf(!info.IsPixelShaderLike, "buffer.__z = z;");
writer.WriteLineIf(!info.IsPixelShaderLike);
writer.WriteLine("loader.LoadConstantBuffer(new global::System.ReadOnlySpan<byte>(&buffer, sizeof(global::ComputeSharp.Generated.ConstantBuffer)));");
}
}
}
@@ -7,6 +7,12 @@ namespace ComputeSharp.SourceGeneration.SyntaxProcessors;
/// <inheritdoc/>
partial class ConstantBufferSyntaxProcessor
{
/// <inheritdoc/>
private static partial bool HasArtificialFields()
{
return true;
}
/// <inheritdoc/>
static partial void AppendArtificialFields(IConstantBufferInfo info, IndentedTextWriter writer)
{
@@ -22,6 +28,7 @@ partial class ConstantBufferSyntaxProcessor
""", isMultiline: true);
// The Z axis is only present for non pixel-shader-like compute shaders
writer.WriteLineIf(!((ShaderInfo)info).IsPixelShaderLike);
writer.WriteLineIf(
condition: !((ShaderInfo)info).IsPixelShaderLike,
"""