using System.Collections.Immutable;
using ComputeSharp.D2D1.SourceGenerators.Models;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Models;
using ComputeSharp.SourceGeneration.SyntaxProcessors;
using Microsoft.CodeAnalysis;
namespace ComputeSharp.D2D1.SourceGenerators;
///
partial class D2DPixelShaderDescriptorGenerator
{
///
/// A helper with all logic to generate the constant buffer marshalling methods and additional types.
///
private static class ConstantBuffer
{
///
///
///
///
///
public static void GetInfo(
Compilation compilation,
ITypeSymbol structDeclarationSymbol,
out int constantBufferSizeInBytes,
out ImmutableArray fields)
{
constantBufferSizeInBytes = 0;
ConstantBufferSyntaxProcessor.GetInfo(
compilation,
structDeclarationSymbol,
ref constantBufferSizeInBytes,
out fields);
}
///
/// Writes the LoadConstantBuffer method.
///
/// The input instance with gathered shader info.
/// The instance to write into.
public static void WriteLoadConstantBufferSyntax(D2D1ShaderInfo info, IndentedTextWriter writer)
{
string typeName = info.Hierarchy.Hierarchy[0].QualifiedName;
writer.WriteLine("/// ");
writer.WriteGeneratedAttributes(GeneratorName);
writer.WriteLine("[global::System.Runtime.CompilerServices.SkipLocalsInit]");
// Write the method declaration (we only need unsafe blocks if we have any values in the constant buffer)
writer.Write("static ");
writer.WriteIf(!info.Fields.IsEmpty, "unsafe ");
writer.WriteLine($"void global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{typeName}>.LoadConstantBuffer(in {typeName} shader, ref TLoader loader)");
using (writer.WriteBlock())
{
// If there are no fields, just load an empty buffer
if (info.Fields.IsEmpty)
{
writer.WriteLine("loader.LoadConstantBuffer(default);");
}
else
{
// Because D2D (and possibly other consumers) will do a bitwise check on the constant buffer to check for changes,
// it is important that the padding space between aligned elements in the generated constant buffer type are also
// cleared, and not filled with random data (which might be the case if no special care is taken). We cannot just
// rely on the default zero-ing performed by the JIT, because there is no guarantee that the padding space in types
// representing unions will also be zero-ed. So instead we create an uninitialized instance and manually clear it
// with 'NativeMemory.Clear' (which can also be unrolled by the JIT). This still maintains good performance, and
// makes it possible for downstream consumers to further improve performance by implementing better caching strategies.
writer.WriteLine("global::System.Runtime.CompilerServices.Unsafe.SkipInit(out global::ComputeSharp.D2D1.Generated.ConstantBuffer buffer);");
writer.WriteLine();
writer.WriteLine("global::System.Runtime.InteropServices.NativeMemory.Clear(&buffer, (uint)sizeof(global::ComputeSharp.D2D1.Generated.ConstantBuffer));");
writer.WriteLine();
// Otherwise, pass a span into the marshalled native layout buffer
writer.WriteLine("global::ComputeSharp.D2D1.Generated.ConstantBufferMarshaller.FromManaged(in shader, out buffer);");
writer.WriteLine();
writer.WriteLine("loader.LoadConstantBuffer(new global::System.ReadOnlySpan(&buffer, sizeof(global::ComputeSharp.D2D1.Generated.ConstantBuffer)));");
}
}
}
///
/// Writes the CreateFromConstantBuffer method.
///
/// The input instance with gathered shader info.
/// The instance to write into.
public static void WriteCreateFromConstantBufferSyntax(D2D1ShaderInfo info, IndentedTextWriter writer)
{
string typeName = info.Hierarchy.Hierarchy[0].QualifiedName;
writer.WriteLine("/// ");
writer.WriteGeneratedAttributes(GeneratorName);
writer.WriteLine("[global::System.Runtime.CompilerServices.SkipLocalsInit]");
writer.WriteLine($"static {typeName} global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{typeName}>.CreateFromConstantBuffer(global::System.ReadOnlySpan 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(data);");
writer.WriteLine();
writer.WriteLine("return global::ComputeSharp.D2D1.Generated.ConstantBufferMarshaller.ToManaged(in buffer);");
}
}
}
}