Skip to content

Commit

Permalink
Merge pull request #654 from Sergio0694/dev/generated-collection-expr…
Browse files Browse the repository at this point in the history
…essions

Use collection expressions in generated code
  • Loading branch information
Sergio0694 authored Nov 20, 2023
2 parents 4ecf0b2 + 74ccc5e commit 341c40f
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public static void WriteSyntax(D2D1ShaderInfo info, IndentedTextWriter writer)

using (writer.WriteBlock())
{
writer.WriteLine("global::System.ReadOnlySpan<byte> bytes = new byte[]");
writer.WriteLine("{");
writer.WriteLine("global::System.ReadOnlySpan<byte> bytes =");
writer.WriteLine("[");
writer.IncreaseIndent();

// Write the bytes like so:
Expand Down Expand Up @@ -60,7 +60,7 @@ public static void WriteSyntax(D2D1ShaderInfo info, IndentedTextWriter writer)
writer.WriteLine($"{SyntaxFormattingHelper.GetByteExpression(info.EffectId[14])},");
writer.WriteLine(SyntaxFormattingHelper.GetByteExpression(info.EffectId[15]));
writer.DecreaseIndent();
writer.WriteLine("};");
writer.WriteLine("];");

writer.WriteLine();
writer.WriteLine("return ref global::System.Runtime.CompilerServices.Unsafe.As<byte, global::System.Guid>(ref global::System.Runtime.InteropServices.MemoryMarshal.GetReference(bytes));");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ static void Callback(D2D1ShaderInfo info, IndentedTextWriter writer)
// RVA field (with the compiled HLSL bytecode, on a single line)
writer.WriteLine();
writer.WriteLine("/// <summary>The RVA data with the HLSL bytecode.</summary>");
writer.Write("private static ReadOnlySpan<byte> Data => new byte[] { ");
writer.Write("private static ReadOnlySpan<byte> Data => [");

SyntaxFormattingHelper.WriteByteArrayInitializationExpressions(((HlslBytecodeInfo.Success)info.HlslInfo).Bytecode.AsSpan(), writer);

writer.WriteLine(" };");
writer.WriteLine("];");
writer.WriteLine();

// Add the remaining members for the memory manager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ private static void RegisterAdditionalDataMemberSyntax(D2D1ShaderInfo info, Immu
static void Callback(D2D1ShaderInfo info, IndentedTextWriter writer)
{
writer.WriteLine("""/// <summary>The singleton <see cref="D2D1InputDescription"/> array instance.</summary>""");
writer.WriteLine("""public static readonly D2D1InputDescription[] InputDescriptions = """);
writer.WriteLine("""{""");
writer.WriteLine("""public static readonly D2D1InputDescription[] InputDescriptions =""");
writer.WriteLine("""[""");
writer.IncreaseIndent();

// Initialize all input descriptions
Expand All @@ -136,7 +136,7 @@ static void Callback(D2D1ShaderInfo info, IndentedTextWriter writer)

writer.DecreaseIndent();
writer.WriteLine();
writer.WriteLine("};");
writer.WriteLine("];");
}

callbacks.Add(Callback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ static void Callback(D2D1ShaderInfo info, IndentedTextWriter writer)

// RVA field
writer.WriteLine();
writer.WriteLine("/// <summary>The data with the input type info.</summary>");
writer.WriteLine("private static D2D1PixelShaderInputType[] Data = new[]");
writer.WriteLine("{");
writer.WriteLine("/// <summary>The RVA data with the input type info.</summary>");
writer.WriteLine("private static ReadOnlySpan<D2D1PixelShaderInputType> Data =>");
writer.WriteLine("[");
writer.IncreaseIndent();

// Input types, one per line in the RVA field initializer
Expand All @@ -90,15 +90,15 @@ static void Callback(D2D1ShaderInfo info, IndentedTextWriter writer)

writer.DecreaseIndent();
writer.WriteLine();
writer.WriteLine("};");
writer.WriteLine("];");
writer.WriteLine();

// Add the remaining members for the memory manager
writer.WriteLine("""
/// <inheritdoc/>
public override unsafe Span<D2D1PixelShaderInputType> GetSpan()
{
return Data;
return new(Unsafe.AsPointer(ref MemoryMarshal.GetReference(Data)), Data.Length);
}
/// <inheritdoc/>
Expand All @@ -111,9 +111,7 @@ public override Memory<D2D1PixelShaderInputType> Memory
/// <inheritdoc/>
public override unsafe MemoryHandle Pin(int elementIndex)
{
GCHandle handle = GCHandle.Alloc(Data, GCHandleType.Pinned);
return new(Unsafe.AsPointer(ref Data[elementIndex]), handle);
return new(Unsafe.AsPointer(ref Unsafe.AsRef(in Data[elementIndex])), pinnable: this);
}
/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static void Callback(D2D1ShaderInfo info, IndentedTextWriter writer)
{
writer.WriteLine("""/// <summary>The singleton <see cref="D2D1ResourceTextureDescription"/> array instance.</summary>""");
writer.WriteLine("""public static readonly D2D1ResourceTextureDescription[] ResourceTextureDescriptions =""");
writer.WriteLine("""{""");
writer.WriteLine("""[""");
writer.IncreaseIndent();

// Initialize all resource texture descriptions
Expand All @@ -79,7 +79,7 @@ static void Callback(D2D1ShaderInfo info, IndentedTextWriter writer)

writer.DecreaseIndent();
writer.WriteLine();
writer.WriteLine("};");
writer.WriteLine("];");
}

callbacks.Add(Callback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ private static partial class Execute
/// <returns>The HLSL source to compile, if present.</returns>
public static string? GetInvalidReturnType(ImmutableArrayBuilder<DiagnosticInfo> diagnostics, IMethodSymbol methodSymbol)
{
if (!(methodSymbol.ReturnType is INamedTypeSymbol
if (methodSymbol.ReturnType is not INamedTypeSymbol
{
Name: "ReadOnlySpan",
ContainingNamespace.Name: "System",
IsGenericType: true,
TypeParameters.Length: 1
} returnType && returnType.TypeArguments[0].SpecialType == SpecialType.System_Byte))
TypeArguments: [{ SpecialType: SpecialType.System_Byte }]
})
{
diagnostics.Add(
InvalidD2DPixelShaderSourceMethodReturnType,
Expand Down Expand Up @@ -191,11 +191,11 @@ public static void WriteSyntax(D2D1PixelShaderSourceInfo info, IndentedTextWrite
{
if (info.HlslInfo is HlslBytecodeInfo.Success success)
{
writer.Write("return new byte[] { ");
writer.Write("return [");

SyntaxFormattingHelper.WriteByteArrayInitializationExpressions(success.Bytecode.AsSpan(), writer);

writer.WriteLine(" };");
writer.WriteLine("];");
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ static void Callback(ShaderInfo info, IndentedTextWriter writer)
// RVA field (with the compiled HLSL bytecode, on a single line)
writer.WriteLine();
writer.WriteLine("/// <summary>The RVA data with the HLSL bytecode.</summary>");
writer.Write("private static ReadOnlySpan<byte> Data => new byte[] { ");
writer.Write("private static ReadOnlySpan<byte> Data => [");

SyntaxFormattingHelper.WriteByteArrayInitializationExpressions(((HlslBytecodeInfo.Success)info.HlslInfo).Bytecode.AsSpan(), writer);

writer.WriteLine(" };");
writer.WriteLine("];");
writer.WriteLine();

// Add the remaining members for the memory manager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static void Callback(ShaderInfo info, IndentedTextWriter writer)
{
writer.WriteLine("""/// <summary>The singleton <see cref="ResourceDescriptorRange"/> array instance.</summary>""");
writer.WriteLine("""public static readonly ResourceDescriptorRange[] ResourceDescriptorRanges =""");
writer.WriteLine("""{""");
writer.WriteLine("""[""");
writer.IncreaseIndent();

// Initialize all resource descriptor ranges
Expand All @@ -85,7 +85,7 @@ static void Callback(ShaderInfo info, IndentedTextWriter writer)

writer.DecreaseIndent();
writer.WriteLine();
writer.WriteLine("};");
writer.WriteLine("];");
}
}

Expand Down

0 comments on commit 341c40f

Please sign in to comment.