diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Polyfills/EncodingExtensions.cs b/src/ComputeSharp.D2D1.SourceGenerators/Polyfills/EncodingExtensions.cs
new file mode 100644
index 00000000..8c985e6d
--- /dev/null
+++ b/src/ComputeSharp.D2D1.SourceGenerators/Polyfills/EncodingExtensions.cs
@@ -0,0 +1,23 @@
+namespace System.Text;
+
+///
+/// Extensions for the type.
+///
+internal static class EncodingExtensions
+{
+ ///
+ /// Encodes into a span of bytes a set of characters from the specified read-only span.
+ ///
+ /// The input instance to use.
+ /// The span containing the set of characters to encode.
+ /// The byte span to hold the encoded bytes.
+ /// The number of encoded bytes.
+ public static unsafe int GetBytes(this Encoding encoding, ReadOnlySpan chars, Span bytes)
+ {
+ fixed (char* charsPtr = chars)
+ fixed (byte* bytesPtr = bytes)
+ {
+ return encoding.GetBytes(charsPtr, chars.Length, bytesPtr, bytes.Length);
+ }
+ }
+}
diff --git a/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs
index 873a1990..7550b9a4 100644
--- a/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs
@@ -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.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 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 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 d3DBlobFunction = Compile(
- source: buffer.AsSpan(0, writtenBytes),
+ source: sourceAscii,
macro: ASCII.D2D_FUNCTION,
d2DEntry: ASCII.Execute,
entryPoint: default,