ComputeSharp 2.0.0-alpha.1

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
This is a prerelease version of ComputeSharp.
This package has a SemVer 2.0.0 package version: 2.0.0-alpha.1+755eb1472cf84b953e8a0e516b8ae47ede1c4c95.
There is a newer version of this package available.
See the version list below for details.
dotnet add package ComputeSharp --version 2.0.0-alpha.1
NuGet\Install-Package ComputeSharp -Version 2.0.0-alpha.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ComputeSharp" Version="2.0.0-alpha.1" />
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.1
#r "nuget: ComputeSharp, 2.0.0-alpha.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. 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.1&prerelease

// Install ComputeSharp as a Cake Tool
#tool nuget:?package=ComputeSharp&version=2.0.0-alpha.1&prerelease

alternate text is missing from this package README image NuGet NuGet

Major update in the works! 🚀

The currently available NuGet package refers to the 1.x version of ComputeSharp, with the codebase from the master branch. I'm currently working on a new major update targeting .NET 5, featuring a completely rewritten backend using native DirectX bindings and the use of C# source generators to prepare the shader code. This will greatly improve the performance of the library (especially cold start performance, which is over 10x faster already) and use much less memory at runtime. I also plan to introduce support for a number of new features, such as the ability to use custom struct types in shaders. The current status of this work can be seen from the development branches.

Thank you for the support!

What is it?

ComputeSharp is a .NET Standard 2.1 library to run C# code in parallel on the GPU through DX12 and dynamically generated HLSL compute shaders. The available APIs let you allocate GPU buffers and write compute shaders as simple lambda expressions or local methods, with all the captured variables being handled automatically and passed to the running shader.

Table of Contents

Installing from NuGet

To install ComputeSharp, run the following command in the Package Manager Console

Install-Package ComputeSharp

More details available here.

Quick start

ComputeSharp exposes a Gpu class that acts as entry point for all public APIs. It exposes the 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.

The following sample shows how to allocate a writeable buffer, populate it with a compute shader, and read it back.

// Define a simple shader
public readonly struct MyShader : IComputeShader
{
    public readonly ReadWriteBuffer<float> buffer;

    public MyShader(ReadWriteBuffer<float> buffer)
    {
        this.buffer = buffer;
    }

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

// Allocate a writeable buffer on the GPU, with the contents of the array
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(1000);

// Run the shader
Gpu.Default.For(1000, new MyShader(buffer));

// Get the data back
float[] array = buffer.GetData();

Capturing variables

If the shader in C# is capturing some local variable, those will be automatically copied over to the GPU, so that the HLSL shader will be able to access them just like you'd expect. Additionally, ComputeSharp can also resolve static fields being used in a shader. The captured variables need to be convertible to valid HLSL types: either scalar types (int, uint, float, etc.) or known HLSL structs (eg. Vector3). Here is a list of the variable types currently supported by the library:

✅ .NET scalar types: bool, int, uint, float, double

✅ .NET vector types: System.Numerics.Vector2, Vector3, Vector4

✅ HLSL types: Bool, Bool2, Bool3, Bool4, Float2, Float3, Float4, Int2, Int3, Int4, UInt2, Uint3, etc.

✅ static fields of both scalar, vector or buffer types

✅ static properties, same as with fields

✅ Func<T>s or delegates with a valid HLSL signature, with the target method being static

✅ Func<T>s, local methods or delegates with no captured variables

✅ static fields and properties with a supported delegate type

Allocating GPU buffers

There are a number of extension APIs for the GraphicsDevice class that can be used to allocate GPU buffers of three types: ConstantBuffer<T>, ReadOnlyBuffer<T> and ReadWriteBuffer<T>. The first is packed to 16 bytes and provides the fastest possible access for buffer elements, but it has a limited maximum size (around 64KB) and requires additional overhead when copying data to and from it if the size of each element is not a multiple of 16. The other buffer types are tightly packed and work great for all kinds of operations, and can be thought of the HLSL equivalent of T[] arrays in C#. If you're in doubt about which buffer type to use, just use either ReadOnlyBuffer<T> or ReadWriteBuffer<T>, depending on whether or not you also need write access to that buffer on the GPU side.

NOTE: although the APIs to allocate buffers are simply generic methods with a T : unmanaged constrain, they should only be used with C# types that are fully mapped to HLSL types. That means either int, uint, float, double, .NET vector types or HLSL types. The bool type should not be used in buffers due to C#/HLSL differences: use the Bool type instead.

Advanced usage

ComputeSharp lets you dispatch compute shaders over thread groups from 1 to 3 dimensions, includes supports for constant and readonly buffers, and more. Additionally, most of the HLSL intrinsic functions are available through the Hlsl class. Here is a more advanced sample showcasing all these features.

// Define a class with some utility static functions
public static class Activations
{
    public static float Sigmoid(float x) => 1 / (1 + Hlsl.Exp(-x));
}

// Define the shader
public readonly struct ActivationShader : IComputeShader
{
    private readonly int width;

    public readonly ReadOnlyBuffer<float> x;
    public readonly ReadWriteBuffer<float> y;

    public ActivationShader(
        int width,
        ReadOnlyBuffer<float> x,
        ReadWriteBuffer<float> y)
    {
        this.width = width;
        this.x = x;
        this.y = y;
    }

    public void Execute(ThreadIds ids)
    {
        int offset = ids.X + ids.Y * width;
        float pow = Hlsl.Pow(x[offset], 2);
        y[offset] = Activations.Sigmoid(pow);
    }
}

int height = 10, width = 10;
float[] x = new float[height * width]; // Array to sum to y
float[] y = new float[height * width]; // Result array (assume both had some values)

using ReadOnlyBuffer<float> xBuffer = Gpu.Default.AllocateReadOnlyBuffer(x); 
using ReadWriteBuffer<float> yBuffer = Gpu.Default.AllocateReadWriteBuffer(y);

// Run the shader
Gpu.Default.For(width, height, new ActivationShader(width, xBuffer, yBuffer));

// Get the data back and write it to the y array
yBuffer.GetData(y);

Requirements

The ComputeSharp library requires .NET Standard 2.1 support, and it is available for applications targeting:

  • .NET Core >= 3.0
  • Windows (x86 or x64)

Additionally, you need an IDE with .NET Core 3.1 and C# 8.0 support to compile the library and samples on your PC.

Special thanks

The ComputeSharp library is based on some of the code from the DX12GameEngine repository by Amin Delavar. Additionally, ComputeSharp uses NuGet packages from the following repositories (excluding those from Microsoft):

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (8)

Showing the top 5 NuGet packages that depend on ComputeSharp:

Package Downloads
ComputeSharp.Uwp The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

A UWP library with controls to render DX12 shaders powered by ComputeSharp.

ComputeSharp.Dynamic The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

An extension library for ComputeSharp to enable dynamic compilation of shaders at runtime.

ComputeSharp.WinUI The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

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

ComputeSharp.Pix The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

An extension library for ComputeSharp to enable PIX support to produce debugging information.

ComputeSharp.Dxc The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

An extension library for ComputeSharp bundling the DXC compiler and enabling shader reflection.

GitHub repositories (3)

Showing the top 3 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
sebas77/Svelto.MiniExamples
Svelto.ECS and Svelto.Tasks Mini Examples for Unity
Version Downloads Last updated
3.0.0-preview2 394 12/17/2023
3.0.0-preview1 197 11/24/2023
2.2.0-preview1 251 10/10/2023
2.1.0 2,003 9/27/2023
2.1.0-preview3 277 7/9/2023
2.1.0-preview2 211 5/1/2023
2.1.0-preview1 88 4/27/2023
2.0.3 2,862 12/24/2022
2.0.2 156 12/9/2022
2.0.0 236 11/29/2022
2.0.0-preview2 160 10/22/2022
2.0.0-preview1 209 10/8/2022
2.0.0-alpha.29 140 9/19/2022
2.0.0-alpha.28 145 9/6/2022
2.0.0-alpha.27 566 8/22/2022
2.0.0-alpha.26 451 8/1/2022
2.0.0-alpha.25 307 6/6/2022
2.0.0-alpha.24 208 5/24/2022
2.0.0-alpha.23 252 5/12/2022
2.0.0-alpha.22 247 4/24/2022
2.0.0-alpha.21 5,949 4/21/2022
2.0.0-alpha.20 219 4/10/2022
2.0.0-alpha.19 626 3/5/2022
2.0.0-alpha.18 238 2/2/2022
2.0.0-alpha.17 161 1/31/2022
2.0.0-alpha.16 119 1/30/2022
2.0.0-alpha.15 175 1/27/2022
2.0.0-alpha.14 153 1/23/2022
2.0.0-alpha.13 122 1/18/2022
2.0.0-alpha.12 145 1/9/2022
2.0.0-alpha.11 122 1/6/2022
2.0.0-alpha.10 122 1/6/2022
2.0.0-alpha.9 172 12/19/2021
2.0.0-alpha.8 170 11/27/2021
2.0.0-alpha.7 180 10/31/2021
2.0.0-alpha.6 404 7/18/2021
2.0.0-alpha.5 157 7/15/2021
2.0.0-alpha.4 162 7/1/2021
2.0.0-alpha.3 234 5/13/2021
2.0.0-alpha.2 220 4/5/2021
2.0.0-alpha.1 219 3/7/2021

First public preview of ComputeSharp 2.0