using System; using System.Buffers; using System.Text; using ComputeSharp.D2D1.Exceptions; #if !NET6_0_OR_GREATER using ComputeSharp.D2D1.NetStandard.System.Text; #endif using ComputeSharp.D2D1.Shaders.Translation; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; namespace ComputeSharp.D2D1.Interop; /// /// Provides methods to manually compile D2D1 shaders. /// public static class D2D1ShaderCompiler { /// /// Compiles a new HLSL shader from the input source code. /// /// The HLSL source code to compile. /// The entry point of the shader being compiled. /// The shader profile to use to compile the shader. /// The compiler options to use to compile the shader. /// The bytecode for the compiled shader. /// Thrown if the compilation fails. public static ReadOnlyMemory Compile( ReadOnlySpan hlslSource, ReadOnlySpan entryPoint, D2D1ShaderProfile shaderProfile, D2D1CompileOptions options) { // Encode the HLSL source to ASCII int maxSourceLength = Encoding.ASCII.GetMaxByteCount(hlslSource.Length); byte[] sourceBuffer = ArrayPool.Shared.Rent(maxSourceLength); int sourceWrittenBytes = Encoding.ASCII.GetBytes(hlslSource, sourceBuffer); // Encode the entry point to ASCII int maxEntryPointLength = Encoding.ASCII.GetMaxByteCount(entryPoint.Length); byte[] entryPointBuffer = ArrayPool.Shared.Rent(maxEntryPointLength); int entryPointWrittenBytes = Encoding.ASCII.GetBytes(entryPoint, entryPointBuffer); ReadOnlySpan sourceAscii = sourceBuffer.AsSpan(0, sourceWrittenBytes); ReadOnlySpan entryPointAscii = entryPointBuffer.AsSpan(0, entryPointWrittenBytes); return Compile(sourceAscii, entryPointAscii, shaderProfile, options); } /// /// Compiles a new HLSL shader from the input source code. /// /// The HLSL source code to compile (in ASCII). /// The entry point of the shader being compiled (in ASCII). /// The shader profile to use to compile the shader. /// The compiler options to use to compile the shader. /// The bytecode for the compiled shader. /// Thrown if the compilation fails. public static unsafe ReadOnlyMemory Compile( ReadOnlySpan hlslSourceAscii, ReadOnlySpan entryPointAscii, D2D1ShaderProfile shaderProfile, D2D1CompileOptions options) { // Check linking support bool enableLinking = (options & D2D1CompileOptions.EnableLinking) == D2D1CompileOptions.EnableLinking; // Remove the linking flag to make the options blittable to flags options &= ~D2D1CompileOptions.EnableLinking; // Compile the standalone D2D1 full shader using ComPtr d3DBlobFullShader = D3DCompiler.CompileShader( source: hlslSourceAscii, macro: D3DCompiler.ASCII.D2D_FULL_SHADER, d2DEntry: entryPointAscii, entryPoint: entryPointAscii, target: D3DCompiler.ASCII.GetPixelShaderProfile(shaderProfile), flags: (uint)options); if (!enableLinking) { void* blobFullShaderPtr = d3DBlobFullShader.Get()->GetBufferPointer(); nuint blobFullShaderSize = d3DBlobFullShader.Get()->GetBufferSize(); return new Span(blobFullShaderPtr, (int)blobFullShaderSize).ToArray(); } // Compile the export function using ComPtr d3DBlobFunction = D3DCompiler.CompileShader( source: hlslSourceAscii, macro: D3DCompiler.ASCII.D2D_FUNCTION, d2DEntry: entryPointAscii, entryPoint: default, target: D3DCompiler.ASCII.GetLibraryProfile(shaderProfile), flags: (uint)options); // Embed it as private data if requested using ComPtr d3DBlobLinked = D3DCompiler.SetD3DPrivateData(d3DBlobFullShader.Get(), d3DBlobFunction.Get()); void* blobLinkedPtr = d3DBlobLinked.Get()->GetBufferPointer(); nuint blobLinkedSize = d3DBlobLinked.Get()->GetBufferSize(); return new Span(blobLinkedPtr, (int)blobLinkedSize).ToArray(); } }