diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj index 1644d727..a17cda56 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj +++ b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj @@ -1,6 +1,7 @@ netstandard2.0 + $(DefineConstants);D2D1_GENERATOR diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs index 5f1420fa..51108f34 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs @@ -205,7 +205,8 @@ partial class D2DPixelShaderDescriptorGenerator using ComPtr dxcBlobBytecode = D3DCompiler.Compile( key.HlslSource.AsSpan(), key.ShaderProfile, - key.CompileOptions); + key.CompileOptions, + out bool requiresDoublePrecisionSupport); token.ThrowIfCancellationRequested(); @@ -216,7 +217,7 @@ partial class D2DPixelShaderDescriptorGenerator 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.D2D1.SourceGenerators/NativeMethods.txt b/src/ComputeSharp.D2D1.SourceGenerators/NativeMethods.txt index a1c61091..eb4271bf 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/NativeMethods.txt +++ b/src/ComputeSharp.D2D1.SourceGenerators/NativeMethods.txt @@ -1,4 +1,5 @@ D3DCompile +D3DReflect D3DSetBlobPart D3DStripShader D2D1_BUFFER_PRECISION @@ -21,6 +22,9 @@ D3DCOMPILE_OPTIMIZATION_LEVEL2 D3DCOMPILE_OPTIMIZATION_LEVEL3 D3DCOMPILE_WARNINGS_ARE_ERRORS D3DCOMPILER_STRIP_FLAGS +D3D_SHADER_REQUIRES_DOUBLES +D3D_SHADER_REQUIRES_11_1_DOUBLE_EXTENSIONS +ID3D11ShaderReflection IUnknown S_OK S_FALSE \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs index f1ab48c0..2876e53a 100644 --- a/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs +++ b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs @@ -9,6 +9,7 @@ using Windows.Win32; using Windows.Win32.Foundation; using Windows.Win32.Graphics.Direct3D; using Windows.Win32.Graphics.Direct3D.Fxc; +using Windows.Win32.Graphics.Direct3D11; using DirectX = Windows.Win32.PInvoke; #else using ComputeSharp.D2D1.Extensions; @@ -34,7 +35,17 @@ internal static unsafe partial class D3DCompiler /// The shader profile to use to compile the shader. /// The options to use to compile the shader. /// The bytecode for the compiled shader. - public static ComPtr Compile(ReadOnlySpan source, D2D1ShaderProfile shaderProfile, D2D1CompileOptions options) + public static ComPtr Compile( + ReadOnlySpan source, + D2D1ShaderProfile shaderProfile, +#if SOURCE_GENERATOR +#pragma warning disable CS1573 + D2D1CompileOptions options, + out bool requiresDoublePrecisionSupport) // Whether the shader requires support for double precision operations +#pragma warning restore CS1573 +#else + D2D1CompileOptions options) +#endif { int maxLength = Encoding.ASCII.GetMaxByteCount(source.Length); byte[] buffer = ArrayPool.Shared.Rent(maxLength); @@ -58,6 +69,10 @@ internal static unsafe partial class D3DCompiler try { +#if SOURCE_GENERATOR + using ComPtr d3DBlobWithAllParts = default; +#endif + // Compile the standalone D2D1 full shader using ComPtr d3DBlobFullShader = Compile( source: buffer.AsSpan(0, writtenBytes), @@ -66,7 +81,28 @@ internal static unsafe partial class D3DCompiler entryPoint: ASCII.Execute, target: ASCII.GetPixelShaderProfile(shaderProfile), flags: (uint)options, +#if !SOURCE_GENERATOR stripReflectionData: stripReflectionData); +#else + stripReflectionData: stripReflectionData, + d3DBlobWithAllParts: d3DBlobWithAllParts.GetAddressOf()); + + // Check whether double precision operations are required + using (ComPtr d3D11ShaderReflection = default) + { + Guid iidOfID3D11ShaderReflection = ID3D11ShaderReflection.IID_Guid; + + DirectX.D3DReflect( + pSrcData: d3DBlobWithAllParts.Get()->GetBufferPointer(), + SrcDataSize: d3DBlobWithAllParts.Get()->GetBufferSize(), + pInterface: &iidOfID3D11ShaderReflection, + ppReflector: (void**)d3D11ShaderReflection.GetAddressOf()).Assert(); + + ulong doublePrecisionFlags = DirectX.D3D_SHADER_REQUIRES_DOUBLES | DirectX.D3D_SHADER_REQUIRES_11_1_DOUBLE_EXTENSIONS; + + requiresDoublePrecisionSupport = (d3D11ShaderReflection.Get()->GetRequiresFlags() & doublePrecisionFlags) != 0; + } +#endif if (!enableLinking) { @@ -81,7 +117,12 @@ internal static unsafe partial class D3DCompiler entryPoint: default, target: ASCII.GetLibraryProfile(shaderProfile), flags: (uint)options, +#if SOURCE_GENERATOR + stripReflectionData: stripReflectionData, + d3DBlobWithAllParts: null); +#else stripReflectionData: stripReflectionData); +#endif // Embed it as private data if requested using ComPtr d3DBlobLinked = SetD3DPrivateData(d3DBlobFullShader.Get(), d3DBlobFunction.Get()); @@ -113,7 +154,14 @@ internal static unsafe partial class D3DCompiler ReadOnlySpan entryPoint, ReadOnlySpan target, uint flags, +#if SOURCE_GENERATOR +#pragma warning disable CS1573 + bool stripReflectionData, + ID3DBlob** d3DBlobWithAllParts) // The resulting HLSL bytecode with all parts (ie. including reflection info). +#pragma warning restore CS1573 +#else bool stripReflectionData) +#endif { using ComPtr d3DBlobBytecode = default; using ComPtr d3DBlobErrors = default; @@ -191,6 +239,14 @@ internal static unsafe partial class D3DCompiler // there would always be a message), but better safe than sorry. hResult.Assert(); +#if SOURCE_GENERATOR + // If requested, also copy the original HLSL bytecode + if (d3DBlobWithAllParts is not null) + { + d3DBlobBytecode.CopyTo(d3DBlobWithAllParts).Assert(); + } +#endif + // If requested, strip the reflection data and return that bytecode blob if (stripReflectionData) { diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Models/HlslBytecodeInfo.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Models/HlslBytecodeInfo.cs index cb8b9bba..96a5601b 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Models/HlslBytecodeInfo.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Models/HlslBytecodeInfo.cs @@ -11,7 +11,17 @@ internal abstract record HlslBytecodeInfo /// A successfully compiled HLSL shader. /// /// The resulting HLSL bytecode. - public sealed record Success(EquatableArray Bytecode) : HlslBytecodeInfo; + 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; /// /// An HLSL shader that failed to compile due to a Win32 error. diff --git a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj index 330353f5..c6ecff10 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj +++ b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj @@ -1,6 +1,7 @@ netstandard2.0 + $(DefineConstants);D3D12_GENERATOR diff --git a/src/ComputeSharp.SourceGenerators/Dxc/Windows.Win32/ComPtr{T}.cs b/src/ComputeSharp.SourceGenerators/Dxc/Windows.Win32/ComPtr{T}.cs index e7afbe5e..34ece11a 100644 --- a/src/ComputeSharp.SourceGenerators/Dxc/Windows.Win32/ComPtr{T}.cs +++ b/src/ComputeSharp.SourceGenerators/Dxc/Windows.Win32/ComPtr{T}.cs @@ -5,6 +5,7 @@ using System; using System.Runtime.CompilerServices; +using Windows.Win32.Foundation; using Windows.Win32.System.Com; namespace Windows.Win32; @@ -43,6 +44,18 @@ internal unsafe struct ComPtr : IDisposable } } + /// Increments the reference count for the current COM object, if any, and copies its address to a target raw pointer. + /// The target raw pointer to copy the address of the current COM object to. + /// This method always returns . + public readonly HRESULT CopyTo(T** ptr) + { + _ = ((IUnknown*)this.ptr)->AddRef(); + + *ptr = this.ptr; + + return HRESULT.S_OK; + } + /// Gets the currently wrapped raw pointer to a COM object. /// The raw pointer wrapped by the current instance. [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/ComputeSharp.SourceGenerators/NativeMethods.txt b/src/ComputeSharp.SourceGenerators/NativeMethods.txt index 00f34480..34636715 100644 --- a/src/ComputeSharp.SourceGenerators/NativeMethods.txt +++ b/src/ComputeSharp.SourceGenerators/NativeMethods.txt @@ -4,4 +4,5 @@ DxcCreateInstance IDxcCompiler3 IDxcUtils IDxcResult -IUnknown \ No newline at end of file +IUnknown +S_OK \ No newline at end of file