diff --git a/src/ComputeSharp.SourceGenerators/Dxc/DxcShaderCompiler.cs b/src/ComputeSharp.SourceGenerators/Dxc/DxcShaderCompiler.cs
index 255b29a5..98d40a1e 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,20 +75,7 @@ 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;
-
- // 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();
+ using ComPtr dxcResult = default;
// Try to compile the new compute shader
fixed (char* optimization = "-O3")
@@ -115,35 +91,53 @@ internal sealed unsafe class DxcShaderCompiler
arguments[1] = rowMajor;
arguments[2] = warningsAsErrors;
- HRESULT hresult = this.dxcCompiler.Get()->Compile(
- pSource: (IDxcBlob*)dxcBlobEncoding.Get(),
+ using ComPtr dxcCompilerArgs = default;
+
+ // Create the arguments to append others to
+ this.dxcUtils.Get()->BuildArguments(
pSourceName: "",
pEntryPoint: "Execute",
pTargetProfile: "cs_6_0",
pArguments: arguments.AsSpan(0, 3),
pDefines: [],
- pIncludeHandler: this.dxcIncludeHandler.Get(),
- ppResult: dxcOperationResult.GetAddressOf());
+ 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();
+ // Try to actually compile the HLSL source
+ HRESULT hresult = this.dxcCompiler3.Get()->Compile(
+ pSource: in dxcBuffer,
+ pArguments: new ReadOnlySpan(dxcCompilerArgs.Get()->GetArguments(), (int)dxcCompilerArgs.Get()->GetCount()),
+ pIncludeHandler: null,
+ riid: in IDxcResult.IID_Guid,
+ ppResult: out *(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 +145,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 +169,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