diff --git a/Directory.Build.props b/Directory.Build.props index 32b44f4..d9998fd 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -14,7 +14,7 @@ true $(MSBuildThisFileDirectory)NuGet.config - 1.9.5 + 1.9.6 true diff --git a/src/Vortice.Win32.Graphics.Direct3D.Fxc/Apis.cs b/src/Vortice.Win32.Graphics.Direct3D.Fxc/Apis.cs index 6fb0329..dd3bf32 100644 --- a/src/Vortice.Win32.Graphics.Direct3D.Fxc/Apis.cs +++ b/src/Vortice.Win32.Graphics.Direct3D.Fxc/Apis.cs @@ -12,23 +12,69 @@ public static unsafe partial class Apis { public static ID3DInclude* D3D_COMPILE_STANDARD_FILE_INCLUDE => (ID3DInclude*)(nuint)1; + public static ComPtr D3DCompile( + string source, + string entryPoint, + string target, + CompileFlags flags = CompileFlags.None) + { + byte[] sourceUtf8 = Encoding.UTF8.GetBytes(source); + byte[] entryPointUtf8 = Encoding.UTF8.GetBytes(entryPoint); + byte[] targetUtf8 = Encoding.UTF8.GetBytes(target); + + using ComPtr d3dBlobBytecode = default; + using ComPtr d3dBlobErrors = default; + + fixed (byte* sourcePtr = sourceUtf8) + fixed (byte* entryPointPtr = entryPointUtf8) + fixed (byte* targetPtr = targetUtf8) + { + HResult hr = D3DCompile( + pSrcData: sourcePtr, + SrcDataSize: (nuint)sourceUtf8.Length, + pSourceName: null, + pDefines: null, + pInclude: D3D_COMPILE_STANDARD_FILE_INCLUDE, + pEntrypoint: (sbyte*)entryPointPtr, + pTarget: (sbyte*)targetPtr, + Flags1: flags, + Flags2: 0u, + ppCode: d3dBlobBytecode.GetAddressOf(), + ppErrorMsgs: d3dBlobErrors.GetAddressOf() + ); + + if (hr.Failure) + { + // Throw if an error was retrieved, then also double check the HRESULT + if (d3dBlobErrors.Get() is not null) + { + ThrowHslsCompilationException(d3dBlobErrors.Get()); + } + } + + ThrowIfFailed(hr); + + return d3dBlobBytecode.Move(); + } + } + public static ComPtr D3DCompile( ReadOnlySpan source, ReadOnlySpan entryPoint, ReadOnlySpan target, CompileFlags flags = CompileFlags.None) { - int maxLength = Encoding.ASCII.GetMaxByteCount(source.Length); + int maxLength = Encoding.UTF8.GetMaxByteCount(source.Length); byte[] sourceBuffer = ArrayPool.Shared.Rent(maxLength); - int writtenBytes = Encoding.ASCII.GetBytes(source, sourceBuffer); + int writtenBytes = Encoding.UTF8.GetBytes(source, sourceBuffer); - maxLength = Encoding.ASCII.GetMaxByteCount(entryPoint.Length); + maxLength = Encoding.UTF8.GetMaxByteCount(entryPoint.Length); byte[] entryPointBuffer = ArrayPool.Shared.Rent(maxLength); - int entryPointWrittenBytes = Encoding.ASCII.GetBytes(entryPoint, entryPointBuffer); + int entryPointWrittenBytes = Encoding.UTF8.GetBytes(entryPoint, entryPointBuffer); - maxLength = Encoding.ASCII.GetMaxByteCount(target.Length); + maxLength = Encoding.UTF8.GetMaxByteCount(target.Length); byte[] targetBuffer = ArrayPool.Shared.Rent(maxLength); - int targetWrittenBytes = Encoding.ASCII.GetBytes(target, targetBuffer); + int targetWrittenBytes = Encoding.UTF8.GetBytes(target, targetBuffer); try { diff --git a/src/Vortice.Win32.Graphics.Direct3D.Fxc/FxcCompilationException.cs b/src/Vortice.Win32.Graphics.Direct3D.Fxc/FxcCompilationException.cs index da36e19..c7237ae 100644 --- a/src/Vortice.Win32.Graphics.Direct3D.Fxc/FxcCompilationException.cs +++ b/src/Vortice.Win32.Graphics.Direct3D.Fxc/FxcCompilationException.cs @@ -31,9 +31,6 @@ public sealed class FxcCompilationException : Exception builder.AppendLine("The FXC compiler encountered one or more errors while trying to compile the shader:"); builder.AppendLine(); builder.AppendLine(error.Trim()); - builder.AppendLine(); - builder.AppendLine("Make sure to only be using supported features by checking the README file in the ComputeSharp repository: https://github.com/Sergio0694/ComputeSharp."); - builder.Append("If you're sure that your C# shader code is valid, please open an issue an include a working repro and this error message."); return builder.ToString(); }