Simplify constant buffer files in D2D1 generator

このコミットが含まれているのは:
Sergio Pedri committed 2023-11-20 20:51:40 +01:00
1 親 2fc520c965
コミット bec001e3b0
4ファイル変更
+40 -53

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

@@ -11,14 +11,14 @@ namespace ComputeSharp.D2D1.SourceGenerators;
partial class D2DPixelShaderDescriptorGenerator
{
/// <inheritdoc/>
partial class LoadConstantBuffer
partial class ConstantBuffer
{
/// <summary>
/// Writes the <c>LoadConstantBuffer</c> method.
/// </summary>
/// <param name="info">The input <see cref="D2D1ShaderInfo"/> instance with gathered shader info.</param>
/// <param name="writer">The <see cref="IndentedTextWriter"/> instance to write into.</param>
public static void WriteSyntax(D2D1ShaderInfo info, IndentedTextWriter writer)
public static void WriteLoadConstantBufferSyntax(D2D1ShaderInfo info, IndentedTextWriter writer)
{
string typeName = info.Hierarchy.Hierarchy[0].QualifiedName;
@@ -44,6 +44,37 @@ partial class D2DPixelShaderDescriptorGenerator
}
}
/// <summary>
/// Writes the <c>CreateFromConstantBuffer</c> method.
/// </summary>
/// <param name="info">The input <see cref="D2D1ShaderInfo"/> instance with gathered shader info.</param>
/// <param name="writer">The <see cref="IndentedTextWriter"/> instance to write into.</param>
public static void WriteCreateFromConstantBufferSyntax(D2D1ShaderInfo info, IndentedTextWriter writer)
{
string typeName = info.Hierarchy.Hierarchy[0].QualifiedName;
writer.WriteLine("/// <inheritdoc/>");
writer.WriteGeneratedAttributes(GeneratorName);
writer.WriteLine("[global::System.Runtime.CompilerServices.SkipLocalsInit]");
writer.WriteLine($"static unsafe {typeName} global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{typeName}>.CreateFromConstantBuffer(global::System.ReadOnlySpan<byte> data)");
using (writer.WriteBlock())
{
// If there are no fields, just return an empty shader
if (info.Fields.IsEmpty)
{
writer.WriteLine("return default;");
return;
}
// 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.InteropServices.MemoryMarshal.AsRef<global::ComputeSharp.D2D1.Generated.ConstantBuffer>(data);");
writer.WriteLine();
writer.WriteLine("return global::ComputeSharp.D2D1.Generated.ConstantBufferMarshaller.ToManaged(in buffer);");
}
}
/// <summary>
/// Registers a callback to generate additional types, if needed.
/// </summary>
@@ -17,9 +17,9 @@ namespace ComputeSharp.D2D1.SourceGenerators;
partial class D2DPixelShaderDescriptorGenerator
{
/// <summary>
/// A helper with all logic to generate the <c>LoadConstantBuffer</c> method.
/// A helper with all logic to generate the <c>ConstantBuffer</c> methods and additional types.
/// </summary>
private static partial class LoadConstantBuffer
private static partial class ConstantBuffer
{
/// <summary>
/// Explores a given type hierarchy and generates statements to load fields.
@@ -1,44 +0,0 @@
using ComputeSharp.D2D1.SourceGenerators.Models;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
namespace ComputeSharp.D2D1.SourceGenerators;
/// <inheritdoc/>
partial class D2DPixelShaderDescriptorGenerator
{
/// <inheritdoc/>
partial class CreateFromConstantBuffer
{
/// <summary>
/// Writes the <c>CreateFromConstantBuffer</c> method.
/// </summary>
/// <param name="info">The input <see cref="D2D1ShaderInfo"/> instance with gathered shader info.</param>
/// <param name="writer">The <see cref="IndentedTextWriter"/> instance to write into.</param>
public static void WriteSyntax(D2D1ShaderInfo info, IndentedTextWriter writer)
{
string typeName = info.Hierarchy.Hierarchy[0].QualifiedName;
writer.WriteLine("/// <inheritdoc/>");
writer.WriteGeneratedAttributes(GeneratorName);
writer.WriteLine("[global::System.Runtime.CompilerServices.SkipLocalsInit]");
writer.WriteLine($"static unsafe {typeName} global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{typeName}>.CreateFromConstantBuffer(global::System.ReadOnlySpan<byte> data)");
using (writer.WriteBlock())
{
// If there are no fields, just return an empty shader
if (info.Fields.IsEmpty)
{
writer.WriteLine("return default;");
return;
}
// 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.InteropServices.MemoryMarshal.AsRef<global::ComputeSharp.D2D1.Generated.ConstantBuffer>(data);");
writer.WriteLine();
writer.WriteLine("return global::ComputeSharp.D2D1.Generated.ConstantBufferMarshaller.ToManaged(in buffer);");
}
}
}
}
+5 -5
ファイルの表示
@@ -82,8 +82,8 @@ public sealed partial class D2DPixelShaderDescriptorGenerator : IIncrementalGene
using ImmutableArrayBuilder<DiagnosticInfo> diagnostics = new();
// LoadDispatchData() info
ImmutableArray<FieldInfo> fieldInfos = LoadConstantBuffer.GetInfo(
// Constant buffer info
ImmutableArray<FieldInfo> fieldInfos = ConstantBuffer.GetInfo(
diagnostics,
context.SemanticModel.Compilation,
typeSymbol,
@@ -226,13 +226,13 @@ public sealed partial class D2DPixelShaderDescriptorGenerator : IIncrementalGene
declaredMembers.Add(HlslBytecode.WriteCompileOptionsSyntax);
declaredMembers.Add(HlslSource.WriteSyntax);
declaredMembers.Add(HlslBytecode.WriteHlslBytecodeSyntax);
declaredMembers.Add(CreateFromConstantBuffer.WriteSyntax);
declaredMembers.Add(LoadConstantBuffer.WriteSyntax);
declaredMembers.Add(ConstantBuffer.WriteCreateFromConstantBufferSyntax);
declaredMembers.Add(ConstantBuffer.WriteLoadConstantBufferSyntax);
using ImmutableArrayBuilder<IndentedTextWriter.Callback<D2D1ShaderInfo>> additionalTypes = new();
using ImmutableHashSetBuilder<string> usingDirectives = new();
LoadConstantBuffer.RegisterAdditionalTypesSyntax(item, additionalTypes, usingDirectives);
ConstantBuffer.RegisterAdditionalTypesSyntax(item, additionalTypes, usingDirectives);
InputDescriptions.RegisterAdditionalTypeSyntax(item, additionalTypes, usingDirectives);
InputTypes.RegisterAdditionalTypeSyntax(item, additionalTypes, usingDirectives);
HlslBytecode.RegisterAdditionalTypeSyntax(item, additionalTypes, usingDirectives);