diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index fd9a1775..a9589170 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -292,6 +292,10 @@ jobs: msbuild samples\ComputeSharp.SwapChain.D2D1.Cli\ComputeSharp.SwapChain.D2D1.Cli.csproj /restore -t:publish /p:Configuration=Release /p:Platform=${{matrix.platform}} /p:RuntimeIdentifier=win-${{matrix.platform}} + # Publish the native library as a NativeAOT shared library + - name: Publish ComputeSharp.NativeLibrary + run: dotnet publish samples\ComputeSharp.NativeLibrary\ComputeSharp.NativeLibrary.csproj -r win-${{matrix.platform}} /p:NativeLib=Shared -v n + # Download the NuGet packages generated in the previous job and use them # to build and run the sample project referencing them. This is used as # a test to ensure the NuGet packages work in a consuming project. diff --git a/ComputeSharp.sln b/ComputeSharp.sln index ee5e503e..6f92e011 100644 --- a/ComputeSharp.sln +++ b/ComputeSharp.sln @@ -164,6 +164,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "arm64", "arm64", "{B4F6A41C libs\arm64\WinPixEventRuntime_UAP.dll = libs\arm64\WinPixEventRuntime_UAP.dll EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputeSharp.NativeLibrary", "samples\ComputeSharp.NativeLibrary\ComputeSharp.NativeLibrary.csproj", "{1A3E20B3-F711-41C0-82EA-3B53A575955C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -444,6 +446,14 @@ Global {9B4448B1-200F-4966-8A13-A508691B3003}.Release|ARM64.Build.0 = Release|Any CPU {9B4448B1-200F-4966-8A13-A508691B3003}.Release|x64.ActiveCfg = Release|Any CPU {9B4448B1-200F-4966-8A13-A508691B3003}.Release|x64.Build.0 = Release|Any CPU + {1A3E20B3-F711-41C0-82EA-3B53A575955C}.Debug|ARM64.ActiveCfg = Debug|Any CPU + {1A3E20B3-F711-41C0-82EA-3B53A575955C}.Debug|ARM64.Build.0 = Debug|Any CPU + {1A3E20B3-F711-41C0-82EA-3B53A575955C}.Debug|x64.ActiveCfg = Debug|Any CPU + {1A3E20B3-F711-41C0-82EA-3B53A575955C}.Debug|x64.Build.0 = Debug|Any CPU + {1A3E20B3-F711-41C0-82EA-3B53A575955C}.Release|ARM64.ActiveCfg = Release|Any CPU + {1A3E20B3-F711-41C0-82EA-3B53A575955C}.Release|ARM64.Build.0 = Release|Any CPU + {1A3E20B3-F711-41C0-82EA-3B53A575955C}.Release|x64.ActiveCfg = Release|Any CPU + {1A3E20B3-F711-41C0-82EA-3B53A575955C}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -481,6 +491,7 @@ Global {59A17380-24A0-4BD7-9012-B105A9E7D46F} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7} {79FD7862-4707-4DE0-8777-3872558E4C1C} = {A5013F9B-92EC-419F-B0B3-3D6B721E7104} {B4F6A41C-AA9A-4302-92B3-AC64CF2E30F5} = {A5013F9B-92EC-419F-B0B3-3D6B721E7104} + {1A3E20B3-F711-41C0-82EA-3B53A575955C} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {4664C5E3-0340-4E22-BCFD-98AAEDF5F2DC} diff --git a/samples/ComputeSharp.NativeLibrary/ComputeSharp.NativeLibrary.csproj b/samples/ComputeSharp.NativeLibrary/ComputeSharp.NativeLibrary.csproj new file mode 100644 index 00000000..d6645972 --- /dev/null +++ b/samples/ComputeSharp.NativeLibrary/ComputeSharp.NativeLibrary.csproj @@ -0,0 +1,30 @@ + + + net8.0 + AnyCPU;x64;ARM64 + + + + + true + true + true + Size + false + + + false + false + + + true + true + + + + + + + + + diff --git a/samples/ComputeSharp.NativeLibrary/NativeLibrary.cs b/samples/ComputeSharp.NativeLibrary/NativeLibrary.cs new file mode 100644 index 00000000..ce1c77a3 --- /dev/null +++ b/samples/ComputeSharp.NativeLibrary/NativeLibrary.cs @@ -0,0 +1,84 @@ +using System; +using System.Runtime.InteropServices; +using System.Runtime.Versioning; +using ComputeSharp; +using ComputeSharp.D2D1; +using ComputeSharp.D2D1.Interop; + +[assembly: SupportedOSPlatform("windows6.1")] + +/// +/// Native exports for the library. +/// +public static unsafe class Library +{ + /// + /// Registers the effect into a target ID2D1Factory1 instance. + /// + /// + /// + /// The HRESULT for the operation. + [UnmanagedCallersOnly(EntryPoint = nameof(RegisterHelloWorldEffect))] + public static int RegisterHelloWorldEffect(void* d2D1Factory1, Guid* effectId) + { + try + { + D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory1, out *effectId); + + return 0; + } + catch (Exception e) + { + return Marshal.GetHRForException(e); + } + } + + /// + /// Sets the constant buffer for the input ID2D1Effect (assumed to be ). + /// + /// + /// + /// The horizontal dispatch size for the current output. + /// The vertical dispatch size for the current output. + /// + [UnmanagedCallersOnly(EntryPoint = nameof(SetHelloWorldEffectConstantBuffer))] + public static int SetHelloWorldEffectConstantBuffer(void* d2D1Effect, float time, int dispatchWidth, int dispatchHeight) + { + try + { + D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(d2D1Effect, new HelloWorld(time, new int2(dispatchWidth, dispatchHeight))); + + return 0; + } + catch (Exception e) + { + return Marshal.GetHRForException(e); + } + } +} + +/// +/// A hello world effect that displays a color gradient. +/// +/// The current time since the start of the application. +/// The dispatch size for the current output. +[D2DEffectDisplayName(nameof(HelloWorld))] +[D2DEffectDescription("A hello world effect that displays a color gradient.")] +[D2DEffectCategory("Render")] +[D2DEffectAuthor("ComputeSharp.D2D1")] +[D2DInputCount(0)] +[D2DRequiresScenePosition] +[D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] +[D2DGeneratedPixelShaderDescriptor] +internal readonly partial struct HelloWorld(float time, int2 dispatchSize) : ID2D1PixelShader +{ + /// + public float4 Execute() + { + int2 xy = (int2)D2D.GetScenePosition().XY; + float2 uv = xy / (float2)dispatchSize; + float3 color = 0.5f + (0.5f * Hlsl.Cos(time + new float3(uv, uv.X) + new float3(0, 2, 4))); + + return new(color, 1f); + } +} \ No newline at end of file