From b3a6ba90c387f9f9f7047a0f634e099791b2a658 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 17 Oct 2023 18:23:07 +0200 Subject: [PATCH 1/3] Switch to CsWin32 for DXC APIs in D2D1 generator --- ComputeSharp.sln | 2 - .../ComputeSharp.D2D1.SourceGenerators.csproj | 23 ++++----- ...PixelShaderDescriptorGenerator.EffectId.cs | 16 +++++-- ...lShaderDescriptorGenerator.HlslBytecode.cs | 4 +- .../NativeMethods.json | 10 ++++ .../NativeMethods.txt | 24 ++++++++++ .../Attributes/Enums/D2D1BufferPrecision.cs | 4 ++ .../Attributes/Enums/D2D1ChannelDepth.cs | 4 ++ .../Attributes/Enums/D2D1CompileOptions.cs | 38 ++++++++------- .../Attributes/Enums/D2D1Filter.cs | 4 ++ .../Attributes/Enums/D2D1PixelOptions.cs | 4 ++ .../Helpers/D2D1AssemblyAssociatedMemory.cs | 10 ++-- .../Translation/D3DCompiler.ID3DInclude.cs | 47 +++++++++++-------- .../Shaders/Translation/D3DCompiler.cs | 42 +++++++++++++++-- .../Dxc/Windows.Win32/ComPtr{T}.cs | 16 +++++++ 15 files changed, 185 insertions(+), 63 deletions(-) create mode 100644 src/ComputeSharp.D2D1.SourceGenerators/NativeMethods.json create mode 100644 src/ComputeSharp.D2D1.SourceGenerators/NativeMethods.txt diff --git a/ComputeSharp.sln b/ComputeSharp.sln index 1a592848..078cf036 100644 --- a/ComputeSharp.sln +++ b/ComputeSharp.sln @@ -489,10 +489,8 @@ Global src\ComputeSharp.NetStandard\ComputeSharp.NetStandard.projitems*{bfb003d1-788b-48de-8065-39479c162cb0}*SharedItemsImports = 5 src\ComputeSharp.SourceGeneration\ComputeSharp.SourceGeneration.projitems*{bfb003d1-788b-48de-8065-39479c162cb0}*SharedItemsImports = 5 samples\ComputeSharp.SwapChain.Shaders.Shared\ComputeSharp.SwapChain.Shaders.Shared.projitems*{c12d7ace-98ed-4813-8118-6667c34f484f}*SharedItemsImports = 5 - src\ComputeSharp.NetStandard\ComputeSharp.NetStandard.projitems*{e44053bd-a761-47fb-aa78-087a599672ea}*SharedItemsImports = 5 src\ComputeSharp.SourceGeneration.Hlsl\ComputeSharp.SourceGeneration.Hlsl.projitems*{e44053bd-a761-47fb-aa78-087a599672ea}*SharedItemsImports = 5 src\ComputeSharp.SourceGeneration\ComputeSharp.SourceGeneration.projitems*{e44053bd-a761-47fb-aa78-087a599672ea}*SharedItemsImports = 5 - src\ComputeSharp.Win32.D2D1\ComputeSharp.Win32.D2D1.projitems*{e44053bd-a761-47fb-aa78-087a599672ea}*SharedItemsImports = 5 samples\ComputeSharp.SwapChain.Shaders.Shared\ComputeSharp.SwapChain.Shaders.Shared.projitems*{e7c9247d-016b-4819-af9f-0e9b3591e04b}*SharedItemsImports = 5 samples\ComputeSharp.SwapChain.Shaders.Shared\ComputeSharp.SwapChain.Shaders.Shared.projitems*{e91f4d24-922a-4921-a8d7-9c45a143c45d}*SharedItemsImports = 5 src\ComputeSharp.SourceGeneration.Hlsl\ComputeSharp.SourceGeneration.Hlsl.projitems*{eb52cb67-4162-4722-874b-44ea8da5450e}*SharedItemsImports = 5 diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj index f75c487e..9751343d 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj +++ b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj @@ -1,21 +1,19 @@ netstandard2.0 - $(NoWarn);CS1591;CS0649;CS1574 + $(NoWarn);CS1591;CS0649;CS1574;CS0419;IDE0004 - - @@ -72,8 +70,6 @@ - - @@ -83,13 +79,18 @@ - - - + + + + + + + + @@ -101,12 +102,6 @@ - - - - - - diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.EffectId.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.EffectId.cs index 71e86996..bb8bea54 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.EffectId.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.EffectId.cs @@ -76,7 +76,7 @@ partial class D2DPixelShaderDescriptorGenerator /// /// The input instance. /// The resulting effect id. - private static ImmutableArray CreateDefaultEffectId(INamedTypeSymbol typeSymbol) + private static unsafe ImmutableArray CreateDefaultEffectId(INamedTypeSymbol typeSymbol) { try { @@ -97,15 +97,25 @@ partial class D2DPixelShaderDescriptorGenerator int maxEncodedByteCount = Math.Max(maxTypeNameByteCount, maxAssemblyNameByteCount); byte[] bufferUtf8 = ArrayPool.Shared.Rent(maxEncodedByteCount); + int typeNameBytesWritten; + int assemblyNameBytesWritten; // UTF8 encode the fully qualified name first - int typeNameBytesWritten = Encoding.UTF8.GetBytes(charBuffer.WrittenSpan, bufferUtf8.AsSpan()); + fixed (char* pCharBuffer = charBuffer.WrittenSpan) + fixed (byte* pBufferUtf8 = bufferUtf8) + { + typeNameBytesWritten = Encoding.UTF8.GetBytes(pCharBuffer, charBuffer.Count, pBufferUtf8, bufferUtf8.Length); + } // Append the UTF8 fully qualified name to the MD5 hash incrementalHash.AppendData(bufferUtf8, 0, typeNameBytesWritten); // UTF8 encode the assembly name as well - int assemblyNameBytesWritten = Encoding.UTF8.GetBytes(assemblyName.AsSpan(), bufferUtf8.AsSpan()); + fixed (char* pAssemblyName = assemblyName) + fixed (byte* pBufferUtf8 = bufferUtf8) + { + assemblyNameBytesWritten = Encoding.UTF8.GetBytes(pAssemblyName, assemblyName.Length, pBufferUtf8, bufferUtf8.Length); + } // Append the UTF8 assembly name to the MD5 hash incrementalHash.AppendData(bufferUtf8, 0, assemblyNameBytesWritten); diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs index 496d9798..6ff9b7a0 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs @@ -10,8 +10,8 @@ using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Models; using Microsoft.CodeAnalysis; -using TerraFX.Interop.DirectX; -using TerraFX.Interop.Windows; +using Windows.Win32; +using Windows.Win32.Graphics.Direct3D; using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; namespace ComputeSharp.D2D1.SourceGenerators; diff --git a/src/ComputeSharp.D2D1.SourceGenerators/NativeMethods.json b/src/ComputeSharp.D2D1.SourceGenerators/NativeMethods.json new file mode 100644 index 00000000..f491f43c --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/NativeMethods.json @@ -0,0 +1,10 @@ +{ + "$schema": "https://aka.ms/CsWin32.schema.json", + "allowMarshaling": false, + "useSafeHandles": false, + "comInterop": { + "preserveSigMethods": [ + "*" + ] + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/NativeMethods.txt b/src/ComputeSharp.D2D1.SourceGenerators/NativeMethods.txt new file mode 100644 index 00000000..f4fdf2c2 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/NativeMethods.txt @@ -0,0 +1,24 @@ +D3DCompile +D3DSetBlobPart +D2D1_BUFFER_PRECISION +D2D1_CHANNEL_DEPTH +D2D1_FILTER +D2D1_PIXEL_OPTIONS +D3DCOMPILE_DEBUG +D3DCOMPILE_SKIP_VALIDATION +D3DCOMPILE_SKIP_OPTIMIZATION +D3DCOMPILE_PACK_MATRIX_ROW_MAJOR +D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR +D3DCOMPILE_PARTIAL_PRECISION +D3DCOMPILE_AVOID_FLOW_CONTROL +D3DCOMPILE_ENABLE_STRICTNESS +D3DCOMPILE_IEEE_STRICTNESS +D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY +D3DCOMPILE_OPTIMIZATION_LEVEL0 +D3DCOMPILE_OPTIMIZATION_LEVEL1 +D3DCOMPILE_OPTIMIZATION_LEVEL2 +D3DCOMPILE_OPTIMIZATION_LEVEL3 +D3DCOMPILE_WARNINGS_ARE_ERRORS +IUnknown +S_OK +S_FALSE \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Attributes/Enums/D2D1BufferPrecision.cs b/src/ComputeSharp.D2D1/Attributes/Enums/D2D1BufferPrecision.cs index 14f5326e..f227c4d1 100644 --- a/src/ComputeSharp.D2D1/Attributes/Enums/D2D1BufferPrecision.cs +++ b/src/ComputeSharp.D2D1/Attributes/Enums/D2D1BufferPrecision.cs @@ -1,4 +1,8 @@ +#if SOURCE_GENERATOR +using static Windows.Win32.Graphics.Direct2D.D2D1_BUFFER_PRECISION; +#else using static TerraFX.Interop.DirectX.D2D1_BUFFER_PRECISION; +#endif namespace ComputeSharp.D2D1; diff --git a/src/ComputeSharp.D2D1/Attributes/Enums/D2D1ChannelDepth.cs b/src/ComputeSharp.D2D1/Attributes/Enums/D2D1ChannelDepth.cs index 1d9eff34..6a0e6faf 100644 --- a/src/ComputeSharp.D2D1/Attributes/Enums/D2D1ChannelDepth.cs +++ b/src/ComputeSharp.D2D1/Attributes/Enums/D2D1ChannelDepth.cs @@ -1,4 +1,8 @@ +#if SOURCE_GENERATOR +using static Windows.Win32.Graphics.Direct2D.D2D1_CHANNEL_DEPTH; +#else using static TerraFX.Interop.DirectX.D2D1_CHANNEL_DEPTH; +#endif namespace ComputeSharp.D2D1; diff --git a/src/ComputeSharp.D2D1/Attributes/Enums/D2D1CompileOptions.cs b/src/ComputeSharp.D2D1/Attributes/Enums/D2D1CompileOptions.cs index 525de704..69e0d65d 100644 --- a/src/ComputeSharp.D2D1/Attributes/Enums/D2D1CompileOptions.cs +++ b/src/ComputeSharp.D2D1/Attributes/Enums/D2D1CompileOptions.cs @@ -1,9 +1,13 @@ using System; -#if !SOURCE_GENERATOR +#if SOURCE_GENERATOR +using D3DCOMPILE = Windows.Win32.PInvoke; +#else using ComputeSharp.D2D1.Interop; -#endif using TerraFX.Interop.DirectX; +#pragma warning disable IDE0004 +#endif + namespace ComputeSharp.D2D1; /// @@ -21,7 +25,7 @@ public enum D2D1CompileOptions /// /// This flag maps to D3DCOMPILE_DEBUG and /Zi. /// - Debug = D3DCOMPILE.D3DCOMPILE_DEBUG, + Debug = (int)D3DCOMPILE.D3DCOMPILE_DEBUG, /// /// Directs the compiler not to validate the generated code against known capabilities and constraints. @@ -31,7 +35,7 @@ public enum D2D1CompileOptions /// /// This flag maps to D3DCOMPILE_SKIP_VALIDATION and /Vd. /// - SkipValidation = D3DCOMPILE.D3DCOMPILE_SKIP_VALIDATION, + SkipValidation = (int)D3DCOMPILE.D3DCOMPILE_SKIP_VALIDATION, /// /// Directs the compiler to skip optimization steps during code generation. @@ -40,7 +44,7 @@ public enum D2D1CompileOptions /// /// This flag maps to D3DCOMPILE_SKIP_OPTIMIZATION and /Od. /// - SkipOptimization = D3DCOMPILE.D3DCOMPILE_SKIP_OPTIMIZATION, + SkipOptimization = (int)D3DCOMPILE.D3DCOMPILE_SKIP_OPTIMIZATION, /// /// Directs the compiler to pack matrices in row-major order on input and output from the shader. @@ -49,7 +53,7 @@ public enum D2D1CompileOptions /// This value is needed to have the constant buffer loading from generated shaders map to compiled shaders. /// This flag maps to D3DCOMPILE_PACK_MATRIX_ROW_MAJOR and /Zpr. /// - PackMatrixRowMajor = D3DCOMPILE.D3DCOMPILE_PACK_MATRIX_ROW_MAJOR, + PackMatrixRowMajor = (int)D3DCOMPILE.D3DCOMPILE_PACK_MATRIX_ROW_MAJOR, /// /// Directs the compiler to pack matrices in column-major order on input and output from the shader. @@ -61,7 +65,7 @@ public enum D2D1CompileOptions /// That is, trying to load a constant buffer from a shader processed by the source generator is undefined behavior. /// This flag maps to D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR and /Zpc. /// - PackMatrixColumnMajor = D3DCOMPILE.D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR, + PackMatrixColumnMajor = (int)D3DCOMPILE.D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR, /// /// Directs the compiler to perform all computations with partial precision. @@ -70,7 +74,7 @@ public enum D2D1CompileOptions /// /// This flag maps to D3DCOMPILE_PARTIAL_PRECISION and /Gpp. /// - PartialPrecision = D3DCOMPILE.D3DCOMPILE_PARTIAL_PRECISION, + PartialPrecision = (int)D3DCOMPILE.D3DCOMPILE_PARTIAL_PRECISION, /// /// Directs the compiler to not use flow-control constructs where possible. @@ -78,7 +82,7 @@ public enum D2D1CompileOptions /// /// This flag maps to D3DCOMPILE_AVOID_FLOW_CONTROL and /Gfa. /// - AvoidFlowControl = D3DCOMPILE.D3DCOMPILE_AVOID_FLOW_CONTROL, + AvoidFlowControl = (int)D3DCOMPILE.D3DCOMPILE_AVOID_FLOW_CONTROL, /// /// Forces strict compile, which might not allow for legacy syntax. @@ -87,7 +91,7 @@ public enum D2D1CompileOptions /// /// This flag maps to D3DCOMPILE_ENABLE_STRICTNESS and /Ges. /// - EnableStrictness = D3DCOMPILE.D3DCOMPILE_ENABLE_STRICTNESS, + EnableStrictness = (int)D3DCOMPILE.D3DCOMPILE_ENABLE_STRICTNESS, /// /// Forces the IEEE strict compile which avoids optimizations that may break IEEE rules. @@ -95,7 +99,7 @@ public enum D2D1CompileOptions /// /// This flag maps to D3DCOMPILE_IEEE_STRICTNESS and /Gis. /// - IeeeStrictness = D3DCOMPILE.D3DCOMPILE_IEEE_STRICTNESS, + IeeeStrictness = (int)D3DCOMPILE.D3DCOMPILE_IEEE_STRICTNESS, /// /// Directs the compiler to enable older shaders to compile to 5_0 targets. @@ -103,7 +107,7 @@ public enum D2D1CompileOptions /// /// This flag maps to D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY and /Gec. /// - EnableBackwardsCompatibility = D3DCOMPILE.D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY, + EnableBackwardsCompatibility = (int)D3DCOMPILE.D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY, /// /// Directs the compiler to use the lowest optimization level. If this option is set, the compiler @@ -113,7 +117,7 @@ public enum D2D1CompileOptions /// /// This flag maps to D3DCOMPILE_OPTIMIZATION_LEVEL0 and /O0. /// - OptimizationLevel0 = D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL0, + OptimizationLevel0 = (int)D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL0, /// /// Directs the compiler to use the second lowest optimization level. @@ -121,7 +125,7 @@ public enum D2D1CompileOptions /// /// This flag maps to D3DCOMPILE_OPTIMIZATION_LEVEL1 and /O1. /// - OptimizationLevel1 = D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL1, + OptimizationLevel1 = (int)D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL1, /// /// Directs the compiler to use the second highest optimization level. @@ -129,7 +133,7 @@ public enum D2D1CompileOptions /// /// This flag maps to D3DCOMPILE_OPTIMIZATION_LEVEL2 and /O2. /// - OptimizationLevel2 = D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL2, + OptimizationLevel2 = (int)D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL2, /// /// Directs the compiler to use the highest optimization level. If this option is set, the compiler @@ -139,7 +143,7 @@ public enum D2D1CompileOptions /// /// This flag maps to D3DCOMPILE_OPTIMIZATION_LEVEL3 and /O3. /// - OptimizationLevel3 = D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL3, + OptimizationLevel3 = (int)D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL3, /// /// Directs the compiler to treat all warnings as errors when it compiles the shader code. This @@ -149,7 +153,7 @@ public enum D2D1CompileOptions /// /// This flag maps to D3DCOMPILE_WARNINGS_ARE_ERRORS and /WX. /// - WarningsAreErrors = D3DCOMPILE.D3DCOMPILE_WARNINGS_ARE_ERRORS, + WarningsAreErrors = (int)D3DCOMPILE.D3DCOMPILE_WARNINGS_ARE_ERRORS, /// /// This flag enables the support for shader linking. Specifically, when set, this flag causes the APIs from diff --git a/src/ComputeSharp.D2D1/Attributes/Enums/D2D1Filter.cs b/src/ComputeSharp.D2D1/Attributes/Enums/D2D1Filter.cs index 7b1d38cc..4ccf3066 100644 --- a/src/ComputeSharp.D2D1/Attributes/Enums/D2D1Filter.cs +++ b/src/ComputeSharp.D2D1/Attributes/Enums/D2D1Filter.cs @@ -1,4 +1,8 @@ +#if SOURCE_GENERATOR +using static Windows.Win32.Graphics.Direct2D.D2D1_FILTER; +#else using static TerraFX.Interop.DirectX.D2D1_FILTER; +#endif namespace ComputeSharp.D2D1; diff --git a/src/ComputeSharp.D2D1/Attributes/Enums/D2D1PixelOptions.cs b/src/ComputeSharp.D2D1/Attributes/Enums/D2D1PixelOptions.cs index f0aefc17..f70b5d38 100644 --- a/src/ComputeSharp.D2D1/Attributes/Enums/D2D1PixelOptions.cs +++ b/src/ComputeSharp.D2D1/Attributes/Enums/D2D1PixelOptions.cs @@ -1,5 +1,9 @@ using System; +#if SOURCE_GENERATOR +using static Windows.Win32.Graphics.Direct2D.D2D1_PIXEL_OPTIONS; +#else using static TerraFX.Interop.DirectX.D2D1_PIXEL_OPTIONS; +#endif namespace ComputeSharp.D2D1; diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/D2D1AssemblyAssociatedMemory.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/D2D1AssemblyAssociatedMemory.cs index c783bea8..c0e87818 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/D2D1AssemblyAssociatedMemory.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/D2D1AssemblyAssociatedMemory.cs @@ -1,7 +1,7 @@ -#if NET6_0_OR_GREATER -using System.Runtime.CompilerServices; +#if SOURCE_GENERATOR +using System.Runtime.InteropServices; #else -using RuntimeHelpers = ComputeSharp.NetStandard.RuntimeHelpers; +using System.Runtime.CompilerServices; #endif namespace ComputeSharp.D2D1.Interop; @@ -26,6 +26,10 @@ internal static unsafe class D2D1AssemblyAssociatedMemory // only invoking this API on an empty type, which minimizes the codegen increase. This is still safe, as types are // not unloaded one by one. Rather, the entire assembly they belong to is unloaded. So given all uses of this API // are from within this same assembly, there is no functional difference than associating memory to the various types. +#if SOURCE_GENERATOR + return (void*)Marshal.AllocHGlobal(size); +#else return (void*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(D2D1AssemblyAssociatedMemory), size); +#endif } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.ID3DInclude.cs b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.ID3DInclude.cs index ad7b1c5b..421f2a6c 100644 --- a/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.ID3DInclude.cs +++ b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.ID3DInclude.cs @@ -1,13 +1,15 @@ +#if !SOURCE_GENERATOR using System; +#endif using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using ComputeSharp.D2D1.Interop; -using TerraFX.Interop.DirectX; -using TerraFX.Interop.Windows; -#if NET6_0_OR_GREATER -using MemoryMarshal2 = System.Runtime.InteropServices.MemoryMarshal; +#if SOURCE_GENERATOR +using Windows.Win32.Graphics.Direct3D; +using static Windows.Win32.Foundation.HRESULT; #else -using MemoryMarshal2 = ComputeSharp.NetStandard.MemoryMarshal; +using TerraFX.Interop.DirectX; +using static TerraFX.Interop.Windows.S; #endif namespace ComputeSharp.D2D1.Shaders.Translation; @@ -20,7 +22,7 @@ partial class D3DCompiler /// private unsafe struct ID3DIncludeForD2DHelpers { -#if !NET6_0_OR_GREATER +#if SOURCE_GENERATOR /// [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate int OpenDelegate(ID3DIncludeForD2DHelpers* @this, D3D_INCLUDE_TYPE IncludeType, sbyte* pFileName, void* pParentData, void** ppData, uint* pBytes); @@ -53,12 +55,12 @@ partial class D3DCompiler { void** lpVtbl = (void**)D2D1AssemblyAssociatedMemory.Allocate(sizeof(void*) * 2); -#if NET6_0_OR_GREATER - lpVtbl[0] = (delegate* unmanaged)&Open; - lpVtbl[1] = (delegate* unmanaged)&Close; -#else +#if SOURCE_GENERATOR lpVtbl[0] = (void*)Marshal.GetFunctionPointerForDelegate(OpenWrapper); lpVtbl[1] = (void*)Marshal.GetFunctionPointerForDelegate(CloseWrapper); +#else + lpVtbl[0] = (delegate* unmanaged)&Open; + lpVtbl[1] = (delegate* unmanaged)&Close; #endif return lpVtbl; @@ -73,37 +75,44 @@ partial class D3DCompiler /// Creates and initializes a new instance. /// /// A pointer to a new instance. - public static ID3DIncludeForD2DHelpers* Create() + public static ID3DInclude* Create() { - ID3DIncludeForD2DHelpers* @this = (ID3DIncludeForD2DHelpers*)NativeMemory.Alloc((nuint)sizeof(ID3DIncludeForD2DHelpers)); + ID3DIncludeForD2DHelpers* @this = +#if SOURCE_GENERATOR + (ID3DIncludeForD2DHelpers*)Marshal.AllocHGlobal(sizeof(ID3DIncludeForD2DHelpers)); +#else + (ID3DIncludeForD2DHelpers*)NativeMemory.Alloc((nuint)sizeof(ID3DIncludeForD2DHelpers)); +#endif @this->lpVtbl = Vtbl; - return @this; + return (ID3DInclude*)@this; } /// [UnmanagedCallersOnly] public static int Open(ID3DIncludeForD2DHelpers* @this, D3D_INCLUDE_TYPE IncludeType, sbyte* pFileName, void* pParentData, void** ppData, uint* pBytes) { - ReadOnlySpan fileName = MemoryMarshal2.CreateReadOnlySpanFromNullTerminated((byte*)pFileName); - - if (fileName.SequenceEqual("d2d1effecthelpers.hlsli"u8)) +#if SOURCE_GENERATOR + if (new string(pFileName) == "d2d1effecthelpers.hlsli") +#else + if (MemoryMarshal.CreateReadOnlySpanFromNullTerminated((byte*)pFileName).SequenceEqual("d2d1effecthelpers.hlsli"u8)) +#endif { *ppData = (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(D2D1EffectHelpers.TextUtf8)); *pBytes = (uint)D2D1EffectHelpers.TextUtf8.Length; - return S.S_OK; + return S_OK; } - return S.S_FALSE; + return S_FALSE; } /// [UnmanagedCallersOnly] public static int Close(ID3DIncludeForD2DHelpers* @this, void* pData) { - return S.S_OK; + return S_OK; } } } \ 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 f1317887..71946306 100644 --- a/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs +++ b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs @@ -4,9 +4,17 @@ using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; +#if SOURCE_GENERATOR +using Windows.Win32; +using Windows.Win32.Foundation; +using Windows.Win32.Graphics.Direct3D; +using Windows.Win32.Graphics.Direct3D.Fxc; +using DirectX = Windows.Win32.PInvoke; +#else using ComputeSharp.D2D1.Extensions; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; +#endif namespace ComputeSharp.D2D1.Shaders.Translation; @@ -18,7 +26,7 @@ internal static unsafe partial class D3DCompiler /// /// The instance to load d2d1effecthelpers.hlsli. /// - private static readonly ID3DIncludeForD2DHelpers* D3DIncludeForD2D1EffectHelpers = ID3DIncludeForD2DHelpers.Create(); + private static readonly ID3DInclude* D3DIncludeForD2D1EffectHelpers = ID3DIncludeForD2DHelpers.Create(); /// /// Compiles a new HLSL shader from the input source code. @@ -31,7 +39,17 @@ internal static unsafe partial class D3DCompiler { int maxLength = Encoding.ASCII.GetMaxByteCount(source.Length); byte[] buffer = ArrayPool.Shared.Rent(maxLength); - int writtenBytes = Encoding.ASCII.GetBytes(source, buffer); + int writtenBytes; + +#if SOURCE_GENERATOR + fixed (char* pSource = source) + fixed (byte* pBuffer = buffer) + { + writtenBytes = Encoding.ASCII.GetBytes(pSource, source.Length, pBuffer, buffer.Length); + } +#else + writtenBytes = Encoding.ASCII.GetBytes(source, buffer); +#endif try { @@ -91,7 +109,7 @@ internal static unsafe partial class D3DCompiler using ComPtr d3DBlobBytecode = default; using ComPtr d3DBlobErrors = default; - int hResult; + HRESULT hResult; fixed (byte* sourcePtr = source) fixed (byte* macroPtr = macro) @@ -108,13 +126,23 @@ internal static unsafe partial class D3DCompiler { new() { +#if SOURCE_GENERATOR + Name = new PCSTR(macroPtr), + Definition = new PCSTR((byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.NullTerminator))) +#else Name = (sbyte*)macroPtr, Definition = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.NullTerminator)) +#endif }, new() { +#if SOURCE_GENERATOR + Name = new PCSTR((byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.D2D_ENTRY))), + Definition = new PCSTR(d2DEntryPtr) +#else Name = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.D2D_ENTRY)), Definition = (sbyte*)d2DEntryPtr +#endif }, new() }; @@ -123,11 +151,19 @@ internal static unsafe partial class D3DCompiler hResult = DirectX.D3DCompile( pSrcData: sourcePtr, SrcDataSize: (nuint)source.Length, +#if SOURCE_GENERATOR + pSourceName: (PCSTR)null, + pDefines: macros, + pInclude: D3DIncludeForD2D1EffectHelpers, + pEntrypoint: new PCSTR(entryPointPtr), + pTarget: new PCSTR(targetPtr), +#else pSourceName: null, pDefines: macros, pInclude: D3DIncludeForD2D1EffectHelpers, pEntrypoint: (sbyte*)entryPointPtr, pTarget: (sbyte*)targetPtr, +#endif Flags1: flags, Flags2: 0, ppCode: d3DBlobBytecode.GetAddressOf(), diff --git a/src/ComputeSharp.SourceGenerators/Dxc/Windows.Win32/ComPtr{T}.cs b/src/ComputeSharp.SourceGenerators/Dxc/Windows.Win32/ComPtr{T}.cs index 7f6fbfc1..e7afbe5e 100644 --- a/src/ComputeSharp.SourceGenerators/Dxc/Windows.Win32/ComPtr{T}.cs +++ b/src/ComputeSharp.SourceGenerators/Dxc/Windows.Win32/ComPtr{T}.cs @@ -59,4 +59,20 @@ internal unsafe struct ComPtr : IDisposable { return (T**)Unsafe.AsPointer(ref Unsafe.AsRef(in this)); } + + /// + /// Moves the current instance and resets it without releasing the reference. + /// + /// The moved instance. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ComPtr Move() + { + ComPtr other = default; + + other.ptr = this.ptr; + + this.ptr = null; + + return other; + } } \ No newline at end of file From 896593c8cf47d1aedecc37f9227a4ff4980ee92f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 17 Oct 2023 18:25:29 +0200 Subject: [PATCH 2/3] Remove unnecessary linked files from D2D1 generator --- .../ComputeSharp.D2D1.SourceGenerators.csproj | 13 ------------- .../D2DPixelShaderDescriptorGenerator.HlslSource.cs | 2 +- 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj index 9751343d..a8bca9c0 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj +++ b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj @@ -63,22 +63,9 @@ - - - - - - - - - - - - - diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs index e6c8ba8e..5e8b1864 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs @@ -287,7 +287,7 @@ partial class D2DPixelShaderDescriptorGenerator { IMethodSymbol methodDeclarationSymbol = semanticModel.For(methodDeclaration).GetDeclaredSymbol(methodDeclaration)!; bool isShaderEntryPoint = - methodDeclarationSymbol.Name == nameof(ID2D1PixelShader.Execute) && + methodDeclarationSymbol.Name == "Execute" && methodDeclarationSymbol.ReturnType.HasFullyQualifiedMetadataName("ComputeSharp.Float4") && methodDeclarationSymbol.TypeParameters.Length == 0 && methodDeclarationSymbol.Parameters.Length == 0; From ebeaee4adb4496ce42e9748d0f6be5391b857b83 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 17 Oct 2023 20:15:23 +0200 Subject: [PATCH 3/3] Share NativeMethods.json file with D3D12 generator --- .../ComputeSharp.D2D1.SourceGenerators.csproj | 3 +++ .../NativeMethods.json | 10 ---------- 2 files changed, 3 insertions(+), 10 deletions(-) delete mode 100644 src/ComputeSharp.D2D1.SourceGenerators/NativeMethods.json diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj index a8bca9c0..56552ae9 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj +++ b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj @@ -73,6 +73,9 @@ + + + diff --git a/src/ComputeSharp.D2D1.SourceGenerators/NativeMethods.json b/src/ComputeSharp.D2D1.SourceGenerators/NativeMethods.json deleted file mode 100644 index f491f43c..00000000 --- a/src/ComputeSharp.D2D1.SourceGenerators/NativeMethods.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "$schema": "https://aka.ms/CsWin32.schema.json", - "allowMarshaling": false, - "useSafeHandles": false, - "comInterop": { - "preserveSigMethods": [ - "*" - ] - } -} \ No newline at end of file