Merge pull request #109 from Sergio0694/sample/fsharp

F# sample
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2021-03-11 00:37:13 +01:00
コミット f6e950f42e
8ファイル変更
+170 -21

検索条件に一致するファイルはありません

+6
ファイルの表示
@@ -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
+22
ファイルの表示
@@ -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}
+28 -2
ファイルの表示
@@ -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)
@@ -39,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
@@ -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 1 .. 100 -> (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:
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\ComputeSharp\ComputeSharp.csproj" />
<ProjectReference Include="..\..\src\ComputeSharp.SourceGenerators\ComputeSharp.SourceGenerators.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false"
PrivateAssets="contentfiles;build" />
</ItemGroup>
</Project>
+17
ファイルの表示
@@ -0,0 +1,17 @@
namespace ComputeSharp.Sample.FSharp.Shaders
{
/// <summary>
/// A sample shader thaat just multiplies items in a buffer by two.
/// </summary>
[AutoConstructor]
public readonly partial struct MultiplyByTwo : IComputeShader
{
public readonly ReadWriteBuffer<float> buffer;
/// <inheritdoc/>
public void Execute()
{
buffer[ThreadIds.X] *= 2;
}
}
}
+22
ファイルの表示
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>5.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\ComputeSharp\ComputeSharp.csproj" />
<ProjectReference Include="..\..\src\ComputeSharp.SourceGenerators\ComputeSharp.SourceGenerators.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false"
PrivateAssets="contentfiles;build" />
<ProjectReference Include="..\ComputeSharp.Sample.FSharp.Shaders\ComputeSharp.Sample.FSharp.Shaders.csproj" />
</ItemGroup>
</Project>
+51
ファイルの表示
@@ -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
/// <summary>Prints a matrix in a properly formatted way.</summary>
/// <param name="array">The input <see langword="float"/> array representing the matrix to print.</param>
/// <param name="width">The width of the array to print.</param>
/// <param name="height">The height of the array to print.</param>
/// <param name="name">The name of the matrix to print.</param>
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))
[<EntryPoint>]
let main _ =
let array = [| for i in 1 .. 100 -> (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
+9 -19
ファイルの表示
@@ -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<float> 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");
}
/// <summary>
@@ -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));