From ae9f574324d7b43accd0fb5f332552b3a59d03d0 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 10 Mar 2021 23:27:55 +0100 Subject: [PATCH 1/4] Added F# sample --- ComputeSharp.sln | 22 ++++++++ .../ComputeSharp.Sample.FSharp.Shaders.csproj | 15 ++++++ .../MultiplyByTwo.cs | 17 +++++++ .../ComputeSharp.Sample.FSharp.fsproj | 22 ++++++++ samples/ComputeSharp.Sample.FSharp/Program.fs | 51 +++++++++++++++++++ 5 files changed, 127 insertions(+) create mode 100644 samples/ComputeSharp.Sample.FSharp.Shaders/ComputeSharp.Sample.FSharp.Shaders.csproj create mode 100644 samples/ComputeSharp.Sample.FSharp.Shaders/MultiplyByTwo.cs create mode 100644 samples/ComputeSharp.Sample.FSharp/ComputeSharp.Sample.FSharp.fsproj create mode 100644 samples/ComputeSharp.Sample.FSharp/Program.fs diff --git a/ComputeSharp.sln b/ComputeSharp.sln index 591e85c2..4d5b566a 100644 --- a/ComputeSharp.sln +++ b/ComputeSharp.sln @@ -43,6 +43,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Tests.NativeLi EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.SwapChain", "samples\ComputeSharp.SwapChain\ComputeSharp.SwapChain.csproj", "{E7C9247D-016B-4819-AF9F-0E9B3591E04B}" EndProject +Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "ComputeSharp.Sample.FSharp", "samples\ComputeSharp.Sample.FSharp\ComputeSharp.Sample.FSharp.fsproj", "{7C625EC4-F599-4350-A915-F046DF2406D3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputeSharp.Sample.FSharp.Shaders", "samples\ComputeSharp.Sample.FSharp.Shaders\ComputeSharp.Sample.FSharp.Shaders.csproj", "{1F37A704-3CDF-45BB-8114-ED9137E5F2CF}" +EndProject Global GlobalSection(SharedMSBuildProjectFiles) = preSolution samples\ComputeSharp.Sample.Shared\ComputeSharp.Sample.Shared.projitems*{7048deea-0a6f-46a0-b951-b682a8f1cb75}*SharedItemsImports = 5 @@ -152,6 +156,22 @@ Global {E7C9247D-016B-4819-AF9F-0E9B3591E04B}.Release|Any CPU.Build.0 = Release|Any CPU {E7C9247D-016B-4819-AF9F-0E9B3591E04B}.Release|x64.ActiveCfg = Release|Any CPU {E7C9247D-016B-4819-AF9F-0E9B3591E04B}.Release|x64.Build.0 = Release|Any CPU + {7C625EC4-F599-4350-A915-F046DF2406D3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7C625EC4-F599-4350-A915-F046DF2406D3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7C625EC4-F599-4350-A915-F046DF2406D3}.Debug|x64.ActiveCfg = Debug|Any CPU + {7C625EC4-F599-4350-A915-F046DF2406D3}.Debug|x64.Build.0 = Debug|Any CPU + {7C625EC4-F599-4350-A915-F046DF2406D3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7C625EC4-F599-4350-A915-F046DF2406D3}.Release|Any CPU.Build.0 = Release|Any CPU + {7C625EC4-F599-4350-A915-F046DF2406D3}.Release|x64.ActiveCfg = Release|Any CPU + {7C625EC4-F599-4350-A915-F046DF2406D3}.Release|x64.Build.0 = Release|Any CPU + {1F37A704-3CDF-45BB-8114-ED9137E5F2CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1F37A704-3CDF-45BB-8114-ED9137E5F2CF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1F37A704-3CDF-45BB-8114-ED9137E5F2CF}.Debug|x64.ActiveCfg = Debug|Any CPU + {1F37A704-3CDF-45BB-8114-ED9137E5F2CF}.Debug|x64.Build.0 = Debug|Any CPU + {1F37A704-3CDF-45BB-8114-ED9137E5F2CF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1F37A704-3CDF-45BB-8114-ED9137E5F2CF}.Release|Any CPU.Build.0 = Release|Any CPU + {1F37A704-3CDF-45BB-8114-ED9137E5F2CF}.Release|x64.ActiveCfg = Release|Any CPU + {1F37A704-3CDF-45BB-8114-ED9137E5F2CF}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -168,6 +188,8 @@ Global {BBBD0ADD-F2F9-49A3-BD5E-D223F0735D8B} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7} {BF52326F-E415-49E6-8595-62762216762B} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7} {E7C9247D-016B-4819-AF9F-0E9B3591E04B} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1} + {7C625EC4-F599-4350-A915-F046DF2406D3} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1} + {1F37A704-3CDF-45BB-8114-ED9137E5F2CF} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {4664C5E3-0340-4E22-BCFD-98AAEDF5F2DC} diff --git a/samples/ComputeSharp.Sample.FSharp.Shaders/ComputeSharp.Sample.FSharp.Shaders.csproj b/samples/ComputeSharp.Sample.FSharp.Shaders/ComputeSharp.Sample.FSharp.Shaders.csproj new file mode 100644 index 00000000..ab7cef29 --- /dev/null +++ b/samples/ComputeSharp.Sample.FSharp.Shaders/ComputeSharp.Sample.FSharp.Shaders.csproj @@ -0,0 +1,15 @@ + + + + net5.0 + + + + + + + + diff --git a/samples/ComputeSharp.Sample.FSharp.Shaders/MultiplyByTwo.cs b/samples/ComputeSharp.Sample.FSharp.Shaders/MultiplyByTwo.cs new file mode 100644 index 00000000..747bd11b --- /dev/null +++ b/samples/ComputeSharp.Sample.FSharp.Shaders/MultiplyByTwo.cs @@ -0,0 +1,17 @@ +namespace ComputeSharp.Sample.FSharp.Shaders +{ + /// + /// A sample shader thaat just multiplies items in a buffer by two. + /// + [AutoConstructor] + public readonly partial struct MultiplyByTwo : IComputeShader + { + public readonly ReadWriteBuffer buffer; + + /// + public void Execute() + { + buffer[ThreadIds.X] *= 2; + } + } +} diff --git a/samples/ComputeSharp.Sample.FSharp/ComputeSharp.Sample.FSharp.fsproj b/samples/ComputeSharp.Sample.FSharp/ComputeSharp.Sample.FSharp.fsproj new file mode 100644 index 00000000..60b0d1f7 --- /dev/null +++ b/samples/ComputeSharp.Sample.FSharp/ComputeSharp.Sample.FSharp.fsproj @@ -0,0 +1,22 @@ + + + + Exe + net5.0 + 5.0 + + + + + + + + + + + + + diff --git a/samples/ComputeSharp.Sample.FSharp/Program.fs b/samples/ComputeSharp.Sample.FSharp/Program.fs new file mode 100644 index 00000000..e07e00f8 --- /dev/null +++ b/samples/ComputeSharp.Sample.FSharp/Program.fs @@ -0,0 +1,51 @@ +// Learn more about F# at http://docs.microsoft.com/dotnet/fsharp + +open System +open System.Globalization +open System.Linq +open ComputeSharp +open ComputeSharp.Sample.FSharp.Shaders + +/// Prints a matrix in a properly formatted way. +/// The input array representing the matrix to print. +/// The width of the array to print. +/// The height of the array to print. +/// The name of the matrix to print. +let printMatrix (array : float32[]) width height (name : string) = + let pad = 48 - name.Length + let title = $"%s{new string('=', pad / 2)} %s{name} %s{new string('=', (pad + 1) / 2)}" + let numberWidth = Math.Max(array.Max().ToString(CultureInfo.InvariantCulture).Length, 4) + + printfn "%s" title + + for i in 0 .. height - 1 do + let row = array.[i * width .. (i + 1) * width - 1] + let text = String.Join(",", [for x in row -> x.ToString(CultureInfo.InvariantCulture).PadLeft(numberWidth)]) + + printfn "%s" text + + printfn "%s" (new string('=', 50)) + +[] +let main _ = + + let array = [| for i in 0 .. 99 -> (float32)i |] + + // Create the graphics buffer + use buffer = Gpu.Default.AllocateReadWriteBuffer(array) + + let shader = new MultiplyByTwo(buffer) + + // Run the shader (passed by reference) + Gpu.Default.For(100, &shader) + + // Print the initial matrix + printMatrix array 10 10 "BEFORE" + + // Get the data back + buffer.CopyTo array + + // Print the updated matrix + printMatrix array 10 10 "AFTER" + + 0 From 19e19a3853aea3e861d88e4677690957141ea59c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 10 Mar 2021 23:36:24 +0100 Subject: [PATCH 2/4] Added readme section on F# support --- README.md | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bfad5e65..6ba4bd06 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ - [Working with images](#working-with-images) - [Shader metaprogramming](#shader-metaprogramming) - [Requirements](#requirements) +- [F# support](#f-support) - [Sponsors](#sponsors) - [Special thanks](#special-thanks) @@ -291,6 +292,31 @@ The **ComputeSharp** library has the following requirements: Additionally, in order to compile the library or a project using it, you need a recent build of Visual Studio 2019 or another IDE that has support for Roslyn source generators, as **ComputeSharp** relies on this feature to create the HLSL shader sources and to extract other shader metadata that is necessary to setup and execute the code at runtime. You can learn more about source generators [here](https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/). +# F# support + +If you're wondering whether it is possible to use **ComputeSharp** from an F# project, it is! + +There is a caveat though: since the HLSL shader rewriter specifically works on C# syntax, it is necessary to write the actual shaders in C#. A simple way to do this is to have a small C# project with just the shader types, and then reference it from an F# project that will contain all the actual logic: every API to create GPU devices, allocate buffers and invoke shaders will work perfectly fine from F# as well. + +Assuming we had the `MultiplyByTwo` shader defined in the [Quick start](#quick-start) paragraph referenced from a C# project, here is how we can actually use it directly from our F# project, with the same sample described above: + +```fsharp +let array = [| for i in 0 .. 99 -> (float32)i |] + +// Create the graphics buffer +use buffer = Gpu.Default.AllocateReadWriteBuffer(array) + +let shader = new MultiplyByTwo(buffer) + +// Run the shader (passed by reference) +Gpu.Default.For(100, &shader) + +// Get the data back +buffer.CopyTo array +``` + +For a complete example, check out the F# sample [here](https://github.com/Sergio0694/ComputeSharp/blob/main/samples/ComputeSharp.Sample.FSharp/Program.fs). + # Sponsors Huge thanks to these sponsors for directly supporting my work on **ComputeSharp**, it means a lot! 🙌 @@ -301,7 +327,7 @@ Huge thanks to these sponsors for directly supporting my work on **ComputeSharp* # Special thanks -The **ComputeSharp** library was originally based on some of the code from the [DX12GameEngine](https://github.com/Aminator/DirectX12GameEngine) repository by [Amin Delavar](https://github.com/Aminator). +**ComputeSharp** was originally based on some of the code from the [DX12GameEngine](https://github.com/Aminator/DirectX12GameEngine) project by [Amin Delavar](https://github.com/Aminator). Additionally, **ComputeSharp** uses NuGet packages from the following repositories: From bf42e6d398dff54cd35814917a4a7ce8eefaa2a9 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 10 Mar 2021 23:37:34 +0100 Subject: [PATCH 3/4] Added F# sample to GitHub Actions pipeline --- .github/workflows/dotnet.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index 3303c106..6b8b859d 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -68,6 +68,12 @@ jobs: - name: Run ComputeSharp.Sample.Local run: dotnet samples\ComputeSharp.Sample.Local\bin\Release\net5.0\ComputeSharp.Sample.Local.dll shell: cmd + - name: Build ComputeSharp.Sample.FSharp + run: dotnet build samples\ComputeSharp.Sample.FSharp\ComputeSharp.Sample.FSharp.fsproj -c Release + shell: cmd + - name: Run ComputeSharp.Sample.FSharp + run: dotnet samples\ComputeSharp.Sample.FSharp\bin\Release\net5.0\ComputeSharp.Sample.FSharp.dll + shell: cmd - name: Build ComputeSharp.ImageProcessing.csproj run: dotnet build samples\ComputeSharp.ImageProcessing\ComputeSharp.ImageProcessing.csproj -c Release shell: cmd From 2a45fbcf057567227b47ca6b73a71d6071aa7a84 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 11 Mar 2021 00:24:28 +0100 Subject: [PATCH 4/4] Minor tweaks to the C# and F# local samples --- README.md | 4 +-- samples/ComputeSharp.Sample.FSharp/Program.fs | 2 +- samples/ComputeSharp.Sample.Shared/Program.cs | 28 ++++++------------- 3 files changed, 12 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 6ba4bd06..783fcff8 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ Let's suppose we want to run a simple compute shader that multiplies all items i ```csharp // Get some sample data -int[] array = Enumerable.Range(0, 100).ToArray(); +int[] array = Enumerable.Range(1, 100).ToArray(); // Allocate a GPU buffer and copy the data to it. // We want the shader to modify the items in-place, so we @@ -301,7 +301,7 @@ There is a caveat though: since the HLSL shader rewriter specifically works on C Assuming we had the `MultiplyByTwo` shader defined in the [Quick start](#quick-start) paragraph referenced from a C# project, here is how we can actually use it directly from our F# project, with the same sample described above: ```fsharp -let array = [| for i in 0 .. 99 -> (float32)i |] +let array = [| for i in 1 .. 100 -> (float32)i |] // Create the graphics buffer use buffer = Gpu.Default.AllocateReadWriteBuffer(array) diff --git a/samples/ComputeSharp.Sample.FSharp/Program.fs b/samples/ComputeSharp.Sample.FSharp/Program.fs index e07e00f8..397d0079 100644 --- a/samples/ComputeSharp.Sample.FSharp/Program.fs +++ b/samples/ComputeSharp.Sample.FSharp/Program.fs @@ -29,7 +29,7 @@ let printMatrix (array : float32[]) width height (name : string) = [] let main _ = - let array = [| for i in 0 .. 99 -> (float32)i |] + let array = [| for i in 1 .. 100 -> (float32)i |] // Create the graphics buffer use buffer = Gpu.Default.AllocateReadWriteBuffer(array) diff --git a/samples/ComputeSharp.Sample.Shared/Program.cs b/samples/ComputeSharp.Sample.Shared/Program.cs index ee01f2a1..f87c1678 100644 --- a/samples/ComputeSharp.Sample.Shared/Program.cs +++ b/samples/ComputeSharp.Sample.Shared/Program.cs @@ -1,7 +1,6 @@ using System; using System.Globalization; using System.Linq; -using System.Runtime.CompilerServices; namespace ComputeSharp.Sample { @@ -9,28 +8,22 @@ namespace ComputeSharp.Sample { static void Main() { - // Create the graphics buffer - int width = 10; - int height = 10; - float[] array = new float[width * height]; - for (int i = 0; i < array.Length; i++) - { - array[i] = i + 1; - } + float[] array = Enumerable.Range(1, 100).Select(static i => (float)i).ToArray(); + // Create the graphics buffer using ReadWriteBuffer gpuBuffer = Gpu.Default.AllocateReadWriteBuffer(array); // Run the shader Gpu.Default.For(100, new MainKernel(gpuBuffer)); // Print the initial matrix - PrintMatrix(array, width, height, "BEFORE"); + PrintMatrix(array, 10, 10, "BEFORE"); // Get the data back gpuBuffer.CopyTo(array); // Print the updated matrix - PrintMatrix(array, width, height, "AFTER"); + PrintMatrix(array, 10, 10, "AFTER"); } /// @@ -62,17 +55,14 @@ namespace ComputeSharp.Sample Console.WriteLine(title); - int length = width * height; - int numberWidth = array.Max().ToString(CultureInfo.InvariantCulture).Length; - ref float r = ref array[0]; + int numberWidth = Math.Max(array.Max().ToString(CultureInfo.InvariantCulture).Length, 4); - for (int i = 0; i < length; i++) + for (int i = 0; i < height; i++) { - Console.Write(r.ToString(CultureInfo.InvariantCulture).PadLeft(numberWidth)); - r = ref Unsafe.Add(ref r, 1); + var row = array[(i * width)..((i + 1) * width)]; + var text = string.Join(",", row.Select(x => x.ToString(CultureInfo.InvariantCulture).PadLeft(numberWidth))); - if (i < length - 1) Console.Write(", "); - if (i > 0 && (i + 1) % width == 0) Console.WriteLine(); + Console.WriteLine(text); } Console.WriteLine(new string('=', 50));