diff --git a/libs/arm64/dxcompiler.dll b/libs/arm64/dxcompiler.dll index df5dd5e9..8a7928af 100644 Binary files a/libs/arm64/dxcompiler.dll and b/libs/arm64/dxcompiler.dll differ diff --git a/libs/arm64/dxil.dll b/libs/arm64/dxil.dll index 7a4b1f96..da8829e3 100644 Binary files a/libs/arm64/dxil.dll and b/libs/arm64/dxil.dll differ diff --git a/libs/x64/dxcompiler.dll b/libs/x64/dxcompiler.dll index 749fa06c..2f7fef71 100644 Binary files a/libs/x64/dxcompiler.dll and b/libs/x64/dxcompiler.dll differ diff --git a/libs/x64/dxil.dll b/libs/x64/dxil.dll index 4d4ec275..fcfccc5e 100644 Binary files a/libs/x64/dxil.dll and b/libs/x64/dxil.dll differ diff --git a/src/ComputeSharp.SourceGenerators/Dxc/DxcShaderCompiler.cs b/src/ComputeSharp.SourceGenerators/Dxc/DxcShaderCompiler.cs index 255b29a5..274f712b 100644 --- a/src/ComputeSharp.SourceGenerators/Dxc/DxcShaderCompiler.cs +++ b/src/ComputeSharp.SourceGenerators/Dxc/DxcShaderCompiler.cs @@ -22,45 +22,35 @@ internal sealed unsafe class DxcShaderCompiler private static DxcShaderCompiler? instance; /// - /// The instance to use to create the bytecode for HLSL sources. + /// The instance to use to create the bytecode for HLSL sources. /// - private readonly ComPtr dxcCompiler; + private readonly ComPtr dxcCompiler3; /// - /// The instance to use to work with . + /// The instance to use to work with . /// - private readonly ComPtr dxcLibrary; - - /// - /// The instance used to compile shaders with . - /// - private readonly ComPtr dxcIncludeHandler; + private readonly ComPtr dxcUtils; /// /// Creates a new instance. /// private DxcShaderCompiler() { - using ComPtr dxcCompiler = default; - using ComPtr dxcLibrary = default; + using ComPtr dxcCompiler = default; + using ComPtr dxcUtils = default; PInvoke.DxcCreateInstance( PInvoke.CLSID_DxcCompiler, - IDxcCompiler.IID_Guid, + IDxcCompiler3.IID_Guid, out *(void**)dxcCompiler.GetAddressOf()).Assert(); PInvoke.DxcCreateInstance( PInvoke.CLSID_DxcLibrary, - IDxcLibrary.IID_Guid, - out *(void**)dxcLibrary.GetAddressOf()).Assert(); + IDxcUtils.IID_Guid, + out *(void**)dxcUtils.GetAddressOf()).Assert(); - using ComPtr dxcIncludeHandler = default; - - dxcLibrary.Get()->CreateIncludeHandler(dxcIncludeHandler.GetAddressOf()).Assert(); - - this.dxcCompiler = new ComPtr(dxcCompiler.Get()); - this.dxcLibrary = new ComPtr(dxcLibrary.Get()); - this.dxcIncludeHandler = new ComPtr(dxcIncludeHandler.Get()); + this.dxcCompiler3 = new ComPtr(dxcCompiler.Get()); + this.dxcUtils = new ComPtr(dxcUtils.Get()); } /// @@ -68,9 +58,8 @@ internal sealed unsafe class DxcShaderCompiler /// ~DxcShaderCompiler() { - this.dxcCompiler.Dispose(); - this.dxcLibrary.Dispose(); - this.dxcIncludeHandler.Dispose(); + this.dxcCompiler3.Dispose(); + this.dxcUtils.Dispose(); } /// @@ -86,64 +75,78 @@ internal sealed unsafe class DxcShaderCompiler /// The bytecode for the compiled shader. public byte[] Compile(ReadOnlySpan source, CancellationToken token) { - using ComPtr dxcBlobEncoding = default; - using ComPtr dxcOperationResult = default; + using ComPtr dxcResult = default; - // Get the encoded blob from the source code - fixed (char* p = source) - { - this.dxcLibrary.Get()->CreateBlobWithEncodingOnHeapCopy( - pText: p, - size: (uint)source.Length * 2, - codePage: DXC_CP.DXC_CP_UTF16, - pBlobEncoding: dxcBlobEncoding.GetAddressOf()).Assert(); - } - - token.ThrowIfCancellationRequested(); + // We can't use stackalloc here, because the portable Span type has a bug that causes + // the constructor taking a pointer to throw if T is a type that contains a pointer field. + // So we can just rent a buffer from the pool and use that instead, at least for now. + PCWSTR[] arguments = ArrayPool.Shared.Rent(3); // Try to compile the new compute shader + fixed (char* pSourceName = "") + fixed (char* pEntryPoint = "Execute") + fixed (char* pTargetProfile = "cs_6_0") fixed (char* optimization = "-O3") fixed (char* rowMajor = "-Zpr") fixed (char* warningsAsErrors = "-Werror") + fixed (PCWSTR* pArguments = arguments) { - // We can't use stackalloc here, because the portable Span type has a bug that causes - // the constructor taking a pointer to throw if T is a type that contains a pointer field. - // So we can just rent a buffer from the pool and use that instead, at least for now. - PCWSTR[] arguments = ArrayPool.Shared.Rent(3); + pArguments[0] = optimization; + pArguments[1] = rowMajor; + pArguments[2] = warningsAsErrors; - arguments[0] = optimization; - arguments[1] = rowMajor; - arguments[2] = warningsAsErrors; + using ComPtr dxcCompilerArgs = default; - HRESULT hresult = this.dxcCompiler.Get()->Compile( - pSource: (IDxcBlob*)dxcBlobEncoding.Get(), - pSourceName: "", - pEntryPoint: "Execute", - pTargetProfile: "cs_6_0", - pArguments: arguments.AsSpan(0, 3), - pDefines: [], - pIncludeHandler: this.dxcIncludeHandler.Get(), - ppResult: dxcOperationResult.GetAddressOf()); + // Create the arguments to append others to + this.dxcUtils.Get()->BuildArguments( + pSourceName: pSourceName, + pEntryPoint: pEntryPoint, + pTargetProfile: pTargetProfile, + pArguments: pArguments, + argCount: 3, + pDefines: null, + defineCount: 0, + ppArgs: dxcCompilerArgs.GetAddressOf()).Assert(); - ArrayPool.Shared.Return(arguments); + fixed (char* pSource = source) + { + // Prepare the buffer with the HLSL source + DxcBuffer dxcBuffer; + dxcBuffer.Ptr = pSource; + dxcBuffer.Size = (uint)(source.Length * sizeof(char)); + dxcBuffer.Encoding = (uint)DXC_CP.DXC_CP_UTF16; - // Only assert the HRESULT after returning the array from the pool. Since the method itself - // can return a failure but doesn't actually throw, there's no need to throw the buffer away. - hresult.Assert(); + Guid dxcResultIid = IDxcResult.IID_Guid; + + // Try to actually compile the HLSL source + HRESULT hresult = this.dxcCompiler3.Get()->Compile( + pSource: &dxcBuffer, + pArguments: dxcCompilerArgs.Get()->GetArguments(), + argCount: dxcCompilerArgs.Get()->GetCount(), + pIncludeHandler: null, + riid: &dxcResultIid, + ppResult: (void**)dxcResult.GetAddressOf()); + + ArrayPool.Shared.Return(arguments); + + // Only assert the HRESULT after returning the array from the pool. Since the method itself + // can return a failure but doesn't actually throw, there's no need to throw the buffer away. + hresult.Assert(); + } } token.ThrowIfCancellationRequested(); HRESULT status; - dxcOperationResult.Get()->GetStatus(&status).Assert(); + dxcResult.Get()->GetStatus(&status).Assert(); // The compilation was successful, so we can extract the shader bytecode if (status == 0) { using ComPtr dxcBlobBytecode = default; - dxcOperationResult.Get()->GetResult(dxcBlobBytecode.GetAddressOf()).Assert(); + dxcResult.Get()->GetResult(dxcBlobBytecode.GetAddressOf()).Assert(); byte* buffer = (byte*)dxcBlobBytecode.Get()->GetBufferPointer(); int length = checked((int)dxcBlobBytecode.Get()->GetBufferSize()); @@ -151,20 +154,20 @@ internal sealed unsafe class DxcShaderCompiler return new ReadOnlySpan(buffer, length).ToArray(); } - return ThrowHslsCompilationException(dxcOperationResult.Get()); + return ThrowHslsCompilationException(dxcResult.Get()); } /// /// Throws an exception when a shader compilation fails. /// - /// The input (faulting) operation. + /// The input (faulting) operation. /// This method always throws and never actually returs. [MethodImpl(MethodImplOptions.NoInlining)] - private static byte[] ThrowHslsCompilationException(IDxcOperationResult* dxcOperationResult) + private static byte[] ThrowHslsCompilationException(IDxcResult* dxcResult) { using ComPtr dxcBlobEncodingError = default; - dxcOperationResult->GetErrorBuffer(dxcBlobEncodingError.GetAddressOf()).Assert(); + dxcResult->GetErrorBuffer(dxcBlobEncodingError.GetAddressOf()).Assert(); string message = new((sbyte*)dxcBlobEncodingError.Get()->GetBufferPointer()); @@ -175,7 +178,7 @@ internal sealed unsafe class DxcShaderCompiler // note: previous definition is here" // These regex-s try to match the unnecessary headers and remove them, if present. // This doesn't need to be bulletproof, and these regex-s should match all cases anyway. - message = Regex.Replace(message, @"^hlsl\.hlsl:\d+:\d+: (\w+:)", static m => m.Groups[1].Value, RegexOptions.Multiline); + message = Regex.Replace(message.Trim(), @"^hlsl\.hlsl:\d+:\d+: (\w+:)", static m => m.Groups[1].Value, RegexOptions.Multiline).Trim(); // Add a trailing '.' if not present if (message is [.., not '.']) diff --git a/src/ComputeSharp.SourceGenerators/NativeMethods.txt b/src/ComputeSharp.SourceGenerators/NativeMethods.txt index 396fba96..00f34480 100644 --- a/src/ComputeSharp.SourceGenerators/NativeMethods.txt +++ b/src/ComputeSharp.SourceGenerators/NativeMethods.txt @@ -1,7 +1,7 @@ CLSID_DxcCompiler CLSID_DxcLibrary DxcCreateInstance -IDxcCompiler -IDxcLibrary -IDxcIncludeHandler +IDxcCompiler3 +IDxcUtils +IDxcResult IUnknown \ No newline at end of file diff --git a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs index bd9ad46b..01f99b32 100644 --- a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs +++ b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs @@ -550,13 +550,13 @@ namespace ComputeSharp.Tests /// public void Execute() { - float number = this.number; + float x = this.number + ThreadIds.X; - Foo(ref this.buffer[ThreadIds.X], ref number); - Bar(ref this.buffer[ThreadIds.X], ref number); - HelpersForPixelShaderWithScopedParameterInMethods.Baz(in this.buffer[ThreadIds.X], ref number); + Foo(ref this.buffer[ThreadIds.X], ref x); + Bar(ref this.buffer[ThreadIds.X], ref x); + HelpersForPixelShaderWithScopedParameterInMethods.Baz(in this.buffer[ThreadIds.X], ref x); - this.buffer[ThreadIds.X] *= number; + this.buffer[ThreadIds.X] *= x; } } }