Add Encoding.GetBytes polyfill to simplify D3DCompiler

このコミットが含まれているのは:
Sergio Pedri committed 2023-12-17 12:48:27 +01:00
1 親 963ec0ecb4
コミット ef821be2b2
2ファイル変更
+28 -13

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

+23
ファイルの表示
@@ -0,0 +1,23 @@
namespace System.Text;
/// <summary>
/// Extensions for the <see cref="Encoding"/> type.
/// </summary>
internal static class EncodingExtensions
{
/// <summary>
/// Encodes into a span of bytes a set of characters from the specified read-only span.
/// </summary>
/// <param name="encoding">The input <see cref="Encoding"/> instance to use.</param>
/// <param name="chars">The span containing the set of characters to encode.</param>
/// <param name="bytes">The byte span to hold the encoded bytes.</param>
/// <returns>The number of encoded bytes.</returns>
public static unsafe int GetBytes(this Encoding encoding, ReadOnlySpan<char> chars, Span<byte> bytes)
{
fixed (char* charsPtr = chars)
fixed (byte* bytesPtr = bytes)
{
return encoding.GetBytes(charsPtr, chars.Length, bytesPtr, bytes.Length);
}
}
}
+5 -13
ファイルの表示
@@ -47,19 +47,11 @@ internal static unsafe partial class D3DCompiler
D2D1CompileOptions options)
#endif
{
// Transcode the input HLSL source from Unicode to ASCII encoding
int maxLength = Encoding.ASCII.GetMaxByteCount(source.Length);
byte[] buffer = ArrayPool<byte>.Shared.Rent(maxLength);
int writtenBytes;
#if SOURCE_GENERATOR
fixed (char* pSource = source)
fixed (byte* pBuffer = buffer)
{
writtenBytes = Encoding.ASCII.GetBytes(pSource, source.Length, pBuffer, buffer.Length);
}
#else
writtenBytes = Encoding.ASCII.GetBytes(source, buffer);
#endif
int writtenBytes = Encoding.ASCII.GetBytes(source, buffer);
ReadOnlySpan<byte> sourceAscii = new(buffer, 0, writtenBytes);
bool enableLinking = (options & D2D1CompileOptions.EnableLinking) == D2D1CompileOptions.EnableLinking;
bool stripReflectionData = (options & D2D1CompileOptions.StripReflectionData) == D2D1CompileOptions.StripReflectionData;
@@ -75,7 +67,7 @@ internal static unsafe partial class D3DCompiler
// Compile the standalone D2D1 full shader
using ComPtr<ID3DBlob> d3DBlobFullShader = Compile(
source: buffer.AsSpan(0, writtenBytes),
source: sourceAscii,
macro: ASCII.D2D_FULL_SHADER,
d2DEntry: ASCII.Execute,
entryPoint: ASCII.Execute,
@@ -94,7 +86,7 @@ internal static unsafe partial class D3DCompiler
// Compile the export function
using ComPtr<ID3DBlob> d3DBlobFunction = Compile(
source: buffer.AsSpan(0, writtenBytes),
source: sourceAscii,
macro: ASCII.D2D_FUNCTION,
d2DEntry: ASCII.Execute,
entryPoint: default,