Use ImmutableArrayBuilder<T> to build HLSL

このコミットが含まれているのは:
Sergio Pedri committed 2023-10-09 12:21:31 +02:00
1 親 875d6da509
コミット 26934553f6
3ファイル変更
+27 -16

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

+4 -5
ファイルの表示
@@ -2,7 +2,6 @@ using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Mappings;
@@ -527,17 +526,17 @@ partial class D2DPixelShaderDescriptorGenerator
ImmutableArray<int> inputComplexIndices,
bool requiresScenePosition)
{
StringBuilder hlslBuilder = new();
using ImmutableArrayBuilder<char> hlslBuilder = ImmutableArrayBuilder<char>.Rent();
void AppendLF()
{
_ = hlslBuilder.Append('\n');
hlslBuilder.Add('\n');
}
void AppendLineAndLF(string text)
{
_ = hlslBuilder.Append(text);
_ = hlslBuilder.Append('\n');
hlslBuilder.AddRange(text.AsSpan());
hlslBuilder.Add('\n');
}
// Header
+15 -1
ファイルの表示
@@ -89,6 +89,12 @@ internal struct ImmutableArrayBuilder<T> : IDisposable
this.writer!.AddRange(items);
}
/// <inheritdoc cref="ImmutableArray{T}.Builder.Clear"/>
public readonly void Clear()
{
this.writer!.Clear();
}
/// <summary>
/// Inserts an item to the builder at the specified index.
/// </summary>
@@ -203,6 +209,12 @@ internal struct ImmutableArrayBuilder<T> : IDisposable
this.index += items.Length;
}
/// <inheritdoc cref="ImmutableArrayBuilder{T}.Clear"/>
public void Clear(ReadOnlySpan<T> items)
{
this.index = 0;
}
/// <inheritdoc cref="ImmutableArrayBuilder{T}.Insert"/>
public void Insert(int index, T item)
{
@@ -227,7 +239,9 @@ internal struct ImmutableArrayBuilder<T> : IDisposable
/// </summary>
public void Clear()
{
if (typeof(T) != typeof(char))
if (typeof(T) != typeof(byte) &&
typeof(T) != typeof(char) &&
typeof(T) != typeof(int))
{
this.array.AsSpan(0, this.index).Clear();
}
+8 -10
ファイルの表示
@@ -2,7 +2,6 @@ using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Mappings;
@@ -566,38 +565,37 @@ partial class IShaderGenerator
bool isComputeShader,
string? implicitTextureType,
bool isSamplerUsed,
string executeMethod)
{
StringBuilder hlslBuilder = new();
using ImmutableArrayBuilder<char> hlslBuilder = ImmutableArrayBuilder<char>.Rent();
void AppendLF()
{
_ = hlslBuilder.Append('\n');
hlslBuilder.Add('\n');
}
void AppendLine(string text)
{
_ = hlslBuilder.Append(text);
hlslBuilder.AddRange(text.AsSpan());
}
void AppendLineAndLF(string text)
{
_ = hlslBuilder.Append(text);
_ = hlslBuilder.Append('\n');
hlslBuilder.AddRange(text.AsSpan());
hlslBuilder.Add('\n');
}
void AppendCharacterAndLF(char c)
{
_ = hlslBuilder.Append(c);
_ = hlslBuilder.Append('\n');
hlslBuilder.Add(c);
hlslBuilder.Add('\n');
}
string FlushText()
{
string text = hlslBuilder.ToString();
_ = hlslBuilder.Clear();
hlslBuilder.Clear();
return text;
}