From 9bad2e99dbd0ddf92638a08c125fdc89cc3a9e06 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 17 Dec 2023 02:55:27 +0100 Subject: [PATCH] Add double precision support check to DX12 generator --- .../ComputeSharp.D2D1.SourceGenerators.csproj | 1 - .../Fxc/D3DCompiler.cs | 2 +- .../Models/HlslBytecodeInfo.cs | 13 ++------- ...eShaderDescriptorGenerator.HlslBytecode.cs | 7 ++++- .../ComputeSharp.SourceGenerators.csproj | 1 - .../Dxc/DxcShaderCompiler.cs | 27 +++++++++++++++++++ .../NativeMethods.txt | 3 +++ 7 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj index a17cda56..1644d727 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj +++ b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj @@ -1,7 +1,6 @@ netstandard2.0 - $(DefineConstants);D2D1_GENERATOR diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Fxc/D3DCompiler.cs b/src/ComputeSharp.D2D1.SourceGenerators/Fxc/D3DCompiler.cs index 41d105ca..0709fd22 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Fxc/D3DCompiler.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Fxc/D3DCompiler.cs @@ -26,7 +26,7 @@ partial class D3DCompiler pInterface: &iidOfID3D11ShaderReflection, ppReflector: (void**)d3D11ShaderReflection.GetAddressOf()).Assert(); - ulong doublePrecisionFlags = DirectX.D3D_SHADER_REQUIRES_DOUBLES | DirectX.D3D_SHADER_REQUIRES_11_1_DOUBLE_EXTENSIONS; + const ulong doublePrecisionFlags = DirectX.D3D_SHADER_REQUIRES_DOUBLES | DirectX.D3D_SHADER_REQUIRES_11_1_DOUBLE_EXTENSIONS; return (d3D11ShaderReflection.Get()->GetRequiresFlags() & doublePrecisionFlags) != 0; } diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Models/HlslBytecodeInfo.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Models/HlslBytecodeInfo.cs index 96a5601b..0ae53ca5 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Models/HlslBytecodeInfo.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Models/HlslBytecodeInfo.cs @@ -11,17 +11,8 @@ internal abstract record HlslBytecodeInfo /// A successfully compiled HLSL shader. /// /// The resulting HLSL bytecode. - public sealed record Success( -#if D3D12_GENERATOR - EquatableArray Bytecode) -#else - -#pragma warning disable CS1573 - EquatableArray Bytecode, - bool RequiresDoublePrecisionSupport) // Whether the shader requires support for double precision operations. -#pragma warning restore CS1573 -#endif - : HlslBytecodeInfo; + /// Whether the shader requires support for double precision operations. + public sealed record Success(EquatableArray Bytecode, bool RequiresDoublePrecisionSupport) : HlslBytecodeInfo; /// /// An HLSL shader that failed to compile due to a Win32 error. diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.cs index 25652d20..2d44b94d 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.cs @@ -62,6 +62,11 @@ partial class ComputeShaderDescriptorGenerator token.ThrowIfCancellationRequested(); + // Check whether double precision operations are required + bool requiresDoublePrecisionSupport = DxcShaderCompiler.Instance.IsDoublePrecisionSupportRequired(dxcBlob.Get()); + + token.ThrowIfCancellationRequested(); + byte* buffer = (byte*)dxcBlob.Get()->GetBufferPointer(); int length = checked((int)dxcBlob.Get()->GetBufferSize()); @@ -69,7 +74,7 @@ partial class ComputeShaderDescriptorGenerator ImmutableArray bytecode = Unsafe.As>(ref array); - return new HlslBytecodeInfo.Success(bytecode); + return new HlslBytecodeInfo.Success(bytecode, requiresDoublePrecisionSupport); } catch (Win32Exception e) { diff --git a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj index c6ecff10..330353f5 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj +++ b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj @@ -1,7 +1,6 @@ netstandard2.0 - $(DefineConstants);D3D12_GENERATOR diff --git a/src/ComputeSharp.SourceGenerators/Dxc/DxcShaderCompiler.cs b/src/ComputeSharp.SourceGenerators/Dxc/DxcShaderCompiler.cs index fef2d180..6b82a99f 100644 --- a/src/ComputeSharp.SourceGenerators/Dxc/DxcShaderCompiler.cs +++ b/src/ComputeSharp.SourceGenerators/Dxc/DxcShaderCompiler.cs @@ -6,6 +6,8 @@ using System.Threading; using Windows.Win32; using Windows.Win32.Foundation; using Windows.Win32.Graphics.Direct3D.Dxc; +using Windows.Win32.Graphics.Direct3D12; +using DirectX = Windows.Win32.PInvoke; namespace ComputeSharp.SourceGenerators.Dxc; @@ -247,6 +249,31 @@ internal sealed unsafe class DxcShaderCompiler return dxcBlobBytecode.Move(); } + /// + /// Checks whether double precision support is required. + /// + /// The input HLSL bytecode to inspect. + /// Whether double precision support is required for . + public bool IsDoublePrecisionSupportRequired(IDxcBlob* dxcBlob) + { + using ComPtr d3D12ShaderReflection = default; + + Guid iidOfID3D12ShaderReflection = ID3D12ShaderReflection.IID_Guid; + + DxcBuffer dxcBuffer = default; + dxcBuffer.Ptr = dxcBlob->GetBufferPointer(); + dxcBuffer.Size = dxcBlob->GetBufferSize(); + + this.dxcUtils.Get()->CreateReflection( + &dxcBuffer, + &iidOfID3D12ShaderReflection, + (void**)d3D12ShaderReflection.GetAddressOf()).Assert(); + + const ulong doublePrecisionFlags = DirectX.D3D_SHADER_REQUIRES_DOUBLES | DirectX.D3D_SHADER_REQUIRES_11_1_DOUBLE_EXTENSIONS; + + return (d3D12ShaderReflection.Get()->GetRequiresFlags() & doublePrecisionFlags) != 0; + } + /// /// Throws an exception when a shader compilation fails. /// diff --git a/src/ComputeSharp.SourceGenerators/NativeMethods.txt b/src/ComputeSharp.SourceGenerators/NativeMethods.txt index 00f34480..1c482b3e 100644 --- a/src/ComputeSharp.SourceGenerators/NativeMethods.txt +++ b/src/ComputeSharp.SourceGenerators/NativeMethods.txt @@ -1,6 +1,9 @@ CLSID_DxcCompiler CLSID_DxcLibrary DxcCreateInstance +D3D_SHADER_REQUIRES_DOUBLES +D3D_SHADER_REQUIRES_11_1_DOUBLE_EXTENSIONS +ID3D12ShaderReflection IDxcCompiler3 IDxcUtils IDxcResult