diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj index a430c4ca..ef124a54 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj +++ b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj @@ -83,8 +83,8 @@ - - + + diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs index 2c24a603..a9454d6b 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs @@ -92,7 +92,7 @@ partial class ID2D1ShaderGenerator token.ThrowIfCancellationRequested(); // Compile the shader bytecode - using ComPtr dxcBlobBytecode = D2D1ShaderCompiler.Compile( + using ComPtr dxcBlobBytecode = D3DCompiler.Compile( sourceInfo.HlslSource.AsSpan(), sourceInfo.ShaderProfile.Value, sourceInfo.IsLinkingSupported); diff --git a/src/ComputeSharp.D2D1/Shaders/Translation/D2D1ShaderCompiler.ID3DInclude.cs b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.ID3DInclude.cs similarity index 99% rename from src/ComputeSharp.D2D1/Shaders/Translation/D2D1ShaderCompiler.ID3DInclude.cs rename to src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.ID3DInclude.cs index 7e59f3ab..23455d02 100644 --- a/src/ComputeSharp.D2D1/Shaders/Translation/D2D1ShaderCompiler.ID3DInclude.cs +++ b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.ID3DInclude.cs @@ -14,7 +14,7 @@ using UnmanagedCallersOnlyAttribute = ComputeSharp.NetStandard.System.Runtime.In namespace ComputeSharp.D2D1.Shaders.Translation; /// -partial class D2D1ShaderCompiler +partial class D3DCompiler { /// /// A custom fallback implementation to use on systems with no support for it. diff --git a/src/ComputeSharp.D2D1/Shaders/Translation/D2D1ShaderCompiler.cs b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs similarity index 70% rename from src/ComputeSharp.D2D1/Shaders/Translation/D2D1ShaderCompiler.cs rename to src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs index 9b879275..9a486caf 100644 --- a/src/ComputeSharp.D2D1/Shaders/Translation/D2D1ShaderCompiler.cs +++ b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs @@ -20,7 +20,7 @@ namespace ComputeSharp.D2D1.Shaders.Translation; /// /// A that uses the FXC APIs to compile D2D1 pixel shaders. /// -internal static unsafe partial class D2D1ShaderCompiler +internal static unsafe partial class D3DCompiler { /// /// The instance to load d2d1effecthelpers.hlsli. @@ -36,77 +36,106 @@ internal static unsafe partial class D2D1ShaderCompiler /// The bytecode for the compiled shader. public static ComPtr Compile(ReadOnlySpan source, D2D1ShaderProfile shaderProfile, bool enableLinkingSupport) { - // Compile the standalone D2D1 full shader - using ComPtr d3DBlobFullShader = CompileD2DFullShader(source, shaderProfile); - - if (!enableLinkingSupport) - { - return d3DBlobFullShader.Move(); - } - - // Compile the export function and embed it as private data if requested - using ComPtr d3DBlobFunction = CompileD2DFunction(source, shaderProfile); - using ComPtr d3DBlobLinked = EmbedD2DFunctionPrivateData(d3DBlobFullShader.Get(), d3DBlobFunction.Get()); - - return d3DBlobLinked.Move(); - } - - /// - /// Compiles a D2D1 pixel shader with D2D_FULL_SHADER. - /// - /// The HLSL source code to compile. - /// The shader profile to use to compile the shader. - /// The bytecode for the compiled shader. - private static ComPtr CompileD2DFullShader(ReadOnlySpan source, D2D1ShaderProfile shaderProfile) - { - // Encode the HLSL source to ASCII int maxLength = Encoding.ASCII.GetMaxByteCount(source.Length); byte[] buffer = ArrayPool.Shared.Rent(maxLength); int writtenBytes = Encoding.ASCII.GetBytes(source, buffer); + try + { + // Compile the standalone D2D1 full shader + using ComPtr d3DBlobFullShader = CompileShader( + buffer.AsSpan(0, writtenBytes), + ASCII.D2D_FULL_SHADER, + ASCII.Execute, + ASCII.GetPixelShaderProfile(shaderProfile), + D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL3 | D3DCOMPILE.D3DCOMPILE_WARNINGS_ARE_ERRORS | D3DCOMPILE.D3DCOMPILE_PACK_MATRIX_ROW_MAJOR); + + if (!enableLinkingSupport) + { + return d3DBlobFullShader.Move(); + } + + // Compile the export function + using ComPtr d3DBlobFunction = CompileShader( + buffer.AsSpan(0, writtenBytes), + ASCII.D2D_FUNCTION, + ASCII.Execute, + ASCII.GetLibraryProfile(shaderProfile), + D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL3 | D3DCOMPILE.D3DCOMPILE_WARNINGS_ARE_ERRORS | D3DCOMPILE.D3DCOMPILE_PACK_MATRIX_ROW_MAJOR); + + // Embed it as private data if requested + using ComPtr d3DBlobLinked = SetD3DPrivateData(d3DBlobFullShader.Get(), d3DBlobFunction.Get()); + + return d3DBlobLinked.Move(); + } + finally + { + ArrayPool.Shared.Return(buffer); + } + } + + /// + /// Compiles a D2D1 pixel shader with the specified parameters. + /// + /// The HLSL source code to compile (in ASCII). + /// The target macro to define for the shader (either D2D_FULL_SHADER or D2D_FUNCTION). + /// The entry point for the shader. + /// The shader target to use to compile the input source. + /// The compilationm flag to use. + /// The bytecode for the compiled shader. + /// Thrown if the compilation fails. + public static ComPtr CompileShader( + ReadOnlySpan source, + ReadOnlySpan macro, + ReadOnlySpan entryPoint, + ReadOnlySpan target, + uint flags) + { using ComPtr d3DBlobBytecode = default; using ComPtr d3DBlobErrors = default; int hResult; - fixed (byte* bufferPtr = buffer) + fixed (byte* sourcePtr = source) + fixed (byte* macroPtr = macro) + fixed (byte* entryPointPtr = entryPoint) + fixed (byte* targetPtr = target) { // Prepare the macros for full shader compilation: // - // -D D2D_FULL_SHADER - // -D D2D_ENTRY=Execute + // -D + // -D D2D_ENTRY= + // D3D_SHADER_MACRO* macros = stackalloc D3D_SHADER_MACRO[] { new() { - Name = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.D2D_FULL_SHADER)), + Name = (sbyte*)macroPtr, Definition = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.NullTerminator)) }, new() { Name = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.D2D_ENTRY)), - Definition = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.Execute)) + Definition = (sbyte*)entryPointPtr }, new() }; - // Compile the shader with -ps_5_0 -O3 -We -Zpr + // Compile the shader hResult = DirectX.D3DCompile( - pSrcData: bufferPtr, - SrcDataSize: (nuint)writtenBytes, + pSrcData: sourcePtr, + SrcDataSize: (nuint)source.Length, pSourceName: null, pDefines: macros, pInclude: D3DIncludeForD2D1EffectHelpers, - pEntrypoint: (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.Execute)), - pTarget: (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.GetPixelShaderProfile(shaderProfile))), - Flags1: D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL3 | D3DCOMPILE.D3DCOMPILE_WARNINGS_ARE_ERRORS | D3DCOMPILE.D3DCOMPILE_PACK_MATRIX_ROW_MAJOR, + pEntrypoint: (sbyte*)entryPointPtr, + pTarget: (sbyte*)targetPtr, + Flags1: flags, Flags2: 0, ppCode: d3DBlobBytecode.GetAddressOf(), ppErrorMsgs: d3DBlobErrors.GetAddressOf()); } - ArrayPool.Shared.Return(buffer); - // Throw if an error was retrieved, then also double check the HRESULT if (d3DBlobErrors.Get() is not null) { @@ -118,79 +147,13 @@ internal static unsafe partial class D2D1ShaderCompiler return d3DBlobBytecode.Move(); } - /// - /// Compiles a D2D1 pixel shader with D2D_FUNCTION. - /// - /// The HLSL source code to compile. - /// The shader profile to use to compile the shader. - /// The bytecode for the compiled shader. - private static ComPtr CompileD2DFunction(ReadOnlySpan source, D2D1ShaderProfile shaderProfile) - { - int maxLength = Encoding.ASCII.GetMaxByteCount(source.Length); - byte[] buffer = ArrayPool.Shared.Rent(maxLength); - int writtenBytes = Encoding.ASCII.GetBytes(source, buffer); - - using ComPtr d3DBlobBytecode = default; - using ComPtr d3DBlobErrors = default; - - int hResult; - - fixed (byte* bufferPtr = buffer) - { - // Prepare the macros for full shader compilation: - // - // -D D2D_FUNCTION - // -D D2D_ENTRY=Execute - D3D_SHADER_MACRO* macros = stackalloc D3D_SHADER_MACRO[] - { - new() - { - Name = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.D2D_FUNCTION)), - Definition = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.NullTerminator)) - }, - new() - { - Name = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.D2D_ENTRY)), - Definition = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.Execute)) - }, - new() - }; - - // Compile the shader with -lib_5_0 -O3 -We -Zpr - hResult = DirectX.D3DCompile( - pSrcData: bufferPtr, - SrcDataSize: (nuint)writtenBytes, - pSourceName: null, - pDefines: macros, - pInclude: D3DIncludeForD2D1EffectHelpers, - pEntrypoint: null, - pTarget: (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.GetLibraryProfile(shaderProfile))), - Flags1: D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL3 | D3DCOMPILE.D3DCOMPILE_WARNINGS_ARE_ERRORS | D3DCOMPILE.D3DCOMPILE_PACK_MATRIX_ROW_MAJOR, - Flags2: 0, - ppCode: d3DBlobBytecode.GetAddressOf(), - ppErrorMsgs: d3DBlobErrors.GetAddressOf()); - } - - ArrayPool.Shared.Return(buffer); - - // Check for errors and double check like above - if (d3DBlobErrors.Get() is not null) - { - ThrowHslsCompilationException(d3DBlobErrors.Get()); - } - - hResult.Assert(); - - return d3DBlobBytecode.Move(); - } - /// /// Embeds the bytecode for an exported shader as private data into another shader bytecode. /// - /// The bytecode produced by . - /// The bytecode produced by . + /// The bytecode for the full shader. + /// The bytecode for the shader function to export. /// An instance with the combined data of and . - private static ComPtr EmbedD2DFunctionPrivateData(ID3DBlob* shaderBlob, ID3DBlob* exportBlob) + private static ComPtr SetD3DPrivateData(ID3DBlob* shaderBlob, ID3DBlob* exportBlob) { void* shaderPtr = shaderBlob->GetBufferPointer(); nuint shaderSize = shaderBlob->GetBufferSize(); diff --git a/src/ComputeSharp.D2D1/Shaders/Translation/__Internals/D2D1ShaderCompiler.cs b/src/ComputeSharp.D2D1/Shaders/Translation/__Internals/D2D1ShaderCompiler.cs index 47867a3a..deae7cd7 100644 --- a/src/ComputeSharp.D2D1/Shaders/Translation/__Internals/D2D1ShaderCompiler.cs +++ b/src/ComputeSharp.D2D1/Shaders/Translation/__Internals/D2D1ShaderCompiler.cs @@ -28,7 +28,7 @@ public static class D2D1ShaderCompiler { Unsafe.AsRef(in shader).BuildHlslSource(out string hlslSource); - using ComPtr d3DBlobBytecode = Shaders.Translation.D2D1ShaderCompiler.Compile( + using ComPtr d3DBlobBytecode = Shaders.Translation.D3DCompiler.Compile( hlslSource.AsSpan(), shaderProfile ?? D2D1ShaderProfile.PixelShader50, enableLinkingSupport);