diff --git a/.gitignore b/.gitignore index b444c010..a49e5f52 100644 --- a/.gitignore +++ b/.gitignore @@ -27,6 +27,10 @@ bld/ [Oo]bj/ [Ll]og/ +# Include the bundled libraries +!**/Libraries/x64/ +!**/Libraries/arm64/ + # Debug view types !**/Resources/Debug diff --git a/src/ComputeSharp.Dynamic/ComputeSharp.Dynamic.csproj b/src/ComputeSharp.Dynamic/ComputeSharp.Dynamic.csproj index 4b638e24..65b4e3bb 100644 --- a/src/ComputeSharp.Dynamic/ComputeSharp.Dynamic.csproj +++ b/src/ComputeSharp.Dynamic/ComputeSharp.Dynamic.csproj @@ -36,6 +36,10 @@ Link="dxil.dll" Visible="False" CopyToOutputDirectory="PreserveNewest" /> + diff --git a/src/ComputeSharp.Dynamic/Libraries/x64/d3dcompiler_47.dll b/src/ComputeSharp.Dynamic/Libraries/x64/d3dcompiler_47.dll new file mode 100644 index 00000000..cbe0765f Binary files /dev/null and b/src/ComputeSharp.Dynamic/Libraries/x64/d3dcompiler_47.dll differ diff --git a/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.cs b/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.cs index b01d9ff3..26f439bc 100644 --- a/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.cs +++ b/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.cs @@ -1,5 +1,8 @@ using System; +using System.Buffers; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Text; using ComputeSharp.Core.Extensions; using ComputeSharp.Exceptions; using TerraFX.Interop.DirectX; @@ -98,6 +101,88 @@ internal sealed unsafe partial class ShaderCompiler } } +#if NET6_0_OR_GREATER + public static ComPtr CompilePixelShader(ReadOnlySpan source) + { + 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; + + fixed (byte* bufferPtr = buffer) + { + // D2D_FULL_SHADER + ReadOnlySpan d2DFullShaderName = new[] + { + (byte)'D', (byte)'2', (byte)'D', (byte)'_', + (byte)'F', (byte)'U', (byte)'L', (byte)'L', (byte)'_', + (byte)'S', (byte)'H', (byte)'A', (byte)'D', (byte)'E', (byte)'R', (byte)'\0' + }; + + // (Empty) + ReadOnlySpan d2DFullShaderValue = new[] { (byte)'\0' }; + + // D2D_ENTRY + ReadOnlySpan d2DEntryName = new[] + { + (byte)'D', (byte)'2', (byte)'D', (byte)'_', + (byte)'E', (byte)'N', (byte)'T', (byte)'R', (byte)'Y', (byte)'\0' + }; + + // Execute + ReadOnlySpan d2DEntryValue = new[] + { + (byte)'E', (byte)'x', (byte)'e', (byte)'c', (byte)'u', (byte)'t', (byte)'e', (byte)'\0' + }; + + D3D_SHADER_MACRO* macros = stackalloc D3D_SHADER_MACRO[] + { + new() + { + Name = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(d2DFullShaderName)), + Definition = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(d2DFullShaderValue)) + }, + new() + { + Name = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(d2DEntryName)), + Definition = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(d2DEntryValue)) + }, + new() + }; + + // ps_4_0 + ReadOnlySpan shaderProfile = new[] + { + (byte)'p', (byte)'s', (byte)'_', (byte)'5', (byte)'_', (byte)'0', (byte)'\0' + }; + + DirectX.D3DCompile( + pSrcData: bufferPtr, + SrcDataSize: (nuint)writtenBytes, + pSourceName: null, + pDefines: macros, + pInclude: null, + pEntrypoint: (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(d2DEntryValue)), + pTarget: (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(shaderProfile)), + Flags1: D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL3 | D3DCOMPILE.D3DCOMPILE_WARNINGS_ARE_ERRORS, + Flags2: 0, + ppCode: d3DBlobBytecode.GetAddressOf(), + ppErrorMsgs: d3DBlobErrors.GetAddressOf()).Assert(); + } + + ArrayPool.Shared.Return(buffer); + + if (d3DBlobErrors.Get() is not null) + { + ThrowHslsCompilationException(d3DBlobErrors.Get()); + } + + return d3DBlobBytecode.Move(); + } +#endif + /// /// Compiles a new HLSL shader from the input source code. /// @@ -156,6 +241,19 @@ internal sealed unsafe partial class ShaderCompiler return ThrowHslsCompilationException(dxcOperationResult); } + /// + /// Throws an exception when a shader compilation fails. + /// + /// The input (faulting) operation. + /// This method always throws and never actually returs. + [MethodImpl(MethodImplOptions.NoInlining)] + private static ComPtr ThrowHslsCompilationException(ID3DBlob* d3DOperationResult) + { + string message = new((sbyte*)d3DOperationResult->GetBufferPointer()); + + throw new HlslCompilationException(message); + } + /// /// Throws an exception when a shader compilation fails. ///