ComputeSharp 2.0.0-alpha.4

A library to run C# code in parallel on the GPU through DX12 and dynamically generated HLSL compute shaders

This is a prerelease version of ComputeSharp.
This package has a SemVer 2.0.0 package version: 2.0.0-alpha.4+33aa291a27d769505340f284109f7dbed63a5b62.
There is a newer prerelease version of this package available.
See the version list below for details.
Install-Package ComputeSharp -Version 2.0.0-alpha.4
dotnet add package ComputeSharp --version 2.0.0-alpha.4
<PackageReference Include="ComputeSharp" Version="2.0.0-alpha.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ComputeSharp --version 2.0.0-alpha.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ComputeSharp, 2.0.0-alpha.4"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install ComputeSharp as a Cake Addin
#addin nuget:?package=ComputeSharp&version=2.0.0-alpha.4&prerelease

// Install ComputeSharp as a Cake Tool
#tool nuget:?package=ComputeSharp&version=2.0.0-alpha.4&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

What is it?

ComputeSharp is a .NET 5 library to run C# code in parallel on the GPU through DX12 and dynamically generated HLSL compute shaders. The available APIs let you access GPU devices, allocate GPU buffers and textures, move data between them and the RAM, write compute shaders entirely in C# and have them run on the GPU. The goal of this project is to make GPU computing easy to use for all .NET developers! 🚀

Quick start

ComputeSharp exposes a Gpu class that acts as entry point for all public APIs. The available Gpu.Default property that lets you access the main GPU device on the current machine, which can be used to allocate buffers and perform operations. If your machine doesn't have a supported GPU (or if it doesn't have a GPU at all), ComputeSharp will automatically create a WARP device instead, which will still let you use the library normally, with shaders running on the CPU instead through an emulation layer. This means that you don't need to manually write a fallback path in case no GPU is available - ComputeSharp will automatically handle this for you.

Let's suppose we want to run a simple compute shader that multiplies all items in a target buffer by two. The first step is to create the GPU buffer and copy our data to it:

// Get some sample data
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
// can allocate a single read-write buffer to work on.
using ReadWriteBuffer<int> buffer = Gpu.Default.AllocateReadWriteBuffer(array);

The AllocateReadWriteBuffer extension takes care of creating a ReadWriteBuffer<T> instance with the same size as the input array and copying its contents to the allocated GPU buffer. There are a number of overloads available as well, to create buffers of different types and with custom length.

Next, we need to define the GPU shader to run. To do this, we'll need to define a struct type implementing the IComputeShader interface. This type will contain the code we want to run on the GPU, as well as fields representing the values we want to capture and pass to the GPU (such as GPU resources, or arbitrary values we need). In this case, we only need to capture the buffer to work on, so the shader type will look like this:

[AutoConstructor]
public readonly partial struct MultiplyByTwo : IComputeShader
{
    public readonly ReadWriteBuffer<int> buffer;

    public void Execute()
    {
        buffer[ThreadIds.X] *= 2;
    }
}

We're using the [AutoConstructor] attribute included in ComputeSharp, which creates a constructor for our type automatically. The shader body is also using a special ThreadIds class, which is one of the available special classes to access dispatch parameters from within a shader body. In this case, ThreadIds lets us access the current invocation index for the shader, just like if we were accessing the classic i variable from within a for loop.

We can now finally run the GPU shader and copy the data back to our array:

// Launch the shader
Gpu.Default.For(buffer.Length, new MultiplyByTwo(buffer));

// Get the data back
buffer.CopyTo(array);

There's more!

For a complete list of all features available in ComputeSharp, check the documentation in the GitHub repo.

What is it?

ComputeSharp is a .NET 5 library to run C# code in parallel on the GPU through DX12 and dynamically generated HLSL compute shaders. The available APIs let you access GPU devices, allocate GPU buffers and textures, move data between them and the RAM, write compute shaders entirely in C# and have them run on the GPU. The goal of this project is to make GPU computing easy to use for all .NET developers! 🚀

Quick start

ComputeSharp exposes a Gpu class that acts as entry point for all public APIs. The available Gpu.Default property that lets you access the main GPU device on the current machine, which can be used to allocate buffers and perform operations. If your machine doesn't have a supported GPU (or if it doesn't have a GPU at all), ComputeSharp will automatically create a WARP device instead, which will still let you use the library normally, with shaders running on the CPU instead through an emulation layer. This means that you don't need to manually write a fallback path in case no GPU is available - ComputeSharp will automatically handle this for you.

Let's suppose we want to run a simple compute shader that multiplies all items in a target buffer by two. The first step is to create the GPU buffer and copy our data to it:

// Get some sample data
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
// can allocate a single read-write buffer to work on.
using ReadWriteBuffer<int> buffer = Gpu.Default.AllocateReadWriteBuffer(array);

The AllocateReadWriteBuffer extension takes care of creating a ReadWriteBuffer<T> instance with the same size as the input array and copying its contents to the allocated GPU buffer. There are a number of overloads available as well, to create buffers of different types and with custom length.

Next, we need to define the GPU shader to run. To do this, we'll need to define a struct type implementing the IComputeShader interface. This type will contain the code we want to run on the GPU, as well as fields representing the values we want to capture and pass to the GPU (such as GPU resources, or arbitrary values we need). In this case, we only need to capture the buffer to work on, so the shader type will look like this:

[AutoConstructor]
public readonly partial struct MultiplyByTwo : IComputeShader
{
    public readonly ReadWriteBuffer<int> buffer;

    public void Execute()
    {
        buffer[ThreadIds.X] *= 2;
    }
}

We're using the [AutoConstructor] attribute included in ComputeSharp, which creates a constructor for our type automatically. The shader body is also using a special ThreadIds class, which is one of the available special classes to access dispatch parameters from within a shader body. In this case, ThreadIds lets us access the current invocation index for the shader, just like if we were accessing the classic i variable from within a for loop.

We can now finally run the GPU shader and copy the data back to our array:

// Launch the shader
Gpu.Default.For(buffer.Length, new MultiplyByTwo(buffer));

// Get the data back
buffer.CopyTo(array);

There's more!

For a complete list of all features available in ComputeSharp, check the documentation in the GitHub repo.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on ComputeSharp:

Package Downloads
ComputeSharp.WinUI
A WinUI 3 library with controls to render DX12 shaders powered by ComputeSharp

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on ComputeSharp:

Repository Stars
Sergio0694/ComputeSharp
A .NET 5 library to run C# code in parallel on the GPU through DX12 and dynamically generated HLSL compute shaders, with the goal of making GPU computing easy to use for all .NET developers! 🚀

Version History

Version Downloads Last updated
2.0.0-alpha.6 150 7/18/2021
2.0.0-alpha.5 69 7/15/2021
2.0.0-alpha.4 49 7/1/2021
2.0.0-alpha.3 103 5/13/2021
2.0.0-alpha.2 106 4/5/2021
2.0.0-alpha.1 106 3/7/2021