ComputeSharp 3.1.0-preview1
Prefix ReservedSee the version list below for details.
dotnet add package ComputeSharp --version 3.1.0-preview1
NuGet\Install-Package ComputeSharp -Version 3.1.0-preview1
<PackageReference Include="ComputeSharp" Version="3.1.0-preview1" />
paket add ComputeSharp --version 3.1.0-preview1
#r "nuget: ComputeSharp, 3.1.0-preview1"
// Install ComputeSharp as a Cake Addin #addin nuget:?package=ComputeSharp&version=3.1.0-preview1&prerelease // Install ComputeSharp as a Cake Tool #tool nuget:?package=ComputeSharp&version=3.1.0-preview1&prerelease
Overview 📖
ComputeSharp is a library to write DX12 compute shaders entirely with C# code. 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 GraphicsDevice
class that acts as entry point for all public APIs. The available GraphicsDevice.GetDefault()
method 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)];
// 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 = GraphicsDevice.GetDefault().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 partial struct
type implementing the IComputeShader
interface (note that the partial
modifier is necessary for ComputeSharp to generate additional code to run the shader). 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). Next, we need to add the [ThreadGroupSize]
attribute to configure the dispatching configuration for the shader. This shader only operates on a 1D buffer, so we can use DefaultThreadGroupSizes.X
for this. Lastly, we also need to add the [GeneratedComputeShaderDescriptor]
attribute, to let the source generator bundled with ComputeSharp do its magic. In this case, we only need to capture the buffer to work on, so the shader type will look like this:
[ThreadGroupSize(DefaultThreadGroupSizes.X)]
[GeneratedComputeShaderDescriptor]
public readonly partial struct MultiplyByTwo(ReadWriteBuffer<int> buffer) : IComputeShader
{
public void Execute()
{
buffer[ThreadIds.X] *= 2;
}
}
In this example, we're using a primary constructor (but you can also explicitly declare fields and set them via a constructor). 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
GraphicsDevice.GetDefault().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.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- ComputeSharp.Core (>= 3.1.0-preview1)
NuGet packages (9)
Showing the top 5 NuGet packages that depend on ComputeSharp:
Package | Downloads |
---|---|
ComputeSharp.Uwp
A UWP library with controls to render DX12 shaders powered by ComputeSharp. |
|
ComputeSharp.Dynamic
An extension library for ComputeSharp to enable dynamic compilation of shaders at runtime. |
|
ComputeSharp.WinUI
A WinUI 3 library with controls to render DX12 shaders powered by ComputeSharp. |
|
ComputeSharp.Pix
An extension library for ComputeSharp to enable PIX support to produce debugging information. |
|
ComputeSharp.Dxc
An extension library for ComputeSharp bundling the DXC compiler and enabling shader reflection. |
GitHub repositories (2)
Showing the top 2 popular GitHub repositories that depend on ComputeSharp:
Repository | Stars |
---|---|
Sergio0694/ComputeSharp
A .NET library to run C# code in parallel on the GPU through DX12, D2D1, and dynamically generated HLSL compute and pixel shaders, with the goal of making GPU computing easy to use for all .NET developers! 🚀
|
|
rocksdanister/weather
Windows native weather app powered by DirectX12 animations
|
Version | Downloads | Last updated |
---|---|---|
3.1.0 | 74 | 12/24/2024 |
3.1.0-preview2 | 276 | 10/22/2024 |
3.1.0-preview1 | 480 | 9/19/2024 |
3.0.1 | 2,860 | 6/18/2024 |
3.0.0 | 197 | 6/10/2024 |
3.0.0-preview2 | 734 | 12/17/2023 |
3.0.0-preview1 | 237 | 11/24/2023 |
2.2.0-preview2 | 68 | 10/14/2024 |
2.2.0-preview1 | 303 | 10/10/2023 |
2.1.0 | 3,700 | 9/27/2023 |
2.1.0-preview3 | 320 | 7/9/2023 |
2.1.0-preview2 | 244 | 5/1/2023 |
2.1.0-preview1 | 118 | 4/27/2023 |
2.0.3 | 3,071 | 12/24/2022 |
2.0.2 | 192 | 12/9/2022 |
2.0.0 | 351 | 11/29/2022 |
2.0.0-preview2 | 198 | 10/22/2022 |
2.0.0-preview1 | 252 | 10/8/2022 |
2.0.0-alpha.29 | 163 | 9/19/2022 |
2.0.0-alpha.28 | 168 | 9/6/2022 |
2.0.0-alpha.27 | 602 | 8/22/2022 |
2.0.0-alpha.26 | 497 | 8/1/2022 |
2.0.0-alpha.25 | 331 | 6/6/2022 |
2.0.0-alpha.24 | 234 | 5/24/2022 |
2.0.0-alpha.23 | 279 | 5/12/2022 |
2.0.0-alpha.22 | 273 | 4/24/2022 |
2.0.0-alpha.21 | 5,984 | 4/21/2022 |
2.0.0-alpha.20 | 247 | 4/10/2022 |
2.0.0-alpha.19 | 669 | 3/5/2022 |
2.0.0-alpha.18 | 278 | 2/2/2022 |
2.0.0-alpha.17 | 185 | 1/31/2022 |
2.0.0-alpha.16 | 141 | 1/30/2022 |
2.0.0-alpha.15 | 199 | 1/27/2022 |
2.0.0-alpha.14 | 178 | 1/23/2022 |
2.0.0-alpha.13 | 141 | 1/18/2022 |
2.0.0-alpha.12 | 164 | 1/9/2022 |
2.0.0-alpha.11 | 143 | 1/6/2022 |
2.0.0-alpha.10 | 141 | 1/6/2022 |
2.0.0-alpha.9 | 187 | 12/19/2021 |
2.0.0-alpha.8 | 189 | 11/27/2021 |
2.0.0-alpha.7 | 200 | 10/31/2021 |
2.0.0-alpha.6 | 428 | 7/18/2021 |
2.0.0-alpha.5 | 176 | 7/15/2021 |
2.0.0-alpha.4 | 181 | 7/1/2021 |
2.0.0-alpha.3 | 254 | 5/13/2021 |
2.0.0-alpha.2 | 235 | 4/5/2021 |
2.0.0-alpha.1 | 239 | 3/7/2021 |