diff --git a/README.md b/README.md index 4e2dc0b4..7dfbcafb 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ ![](https://user-images.githubusercontent.com/10199417/108635546-3512ea00-7480-11eb-8172-99bc59f4eb6f.png) -![.NET](https://github.com/Sergio0694/ComputeSharp/workflows/.NET/badge.svg) [![NuGet](https://img.shields.io/nuget/v/ComputeSharp.svg)](https://www.nuget.org/packages/ComputeSharp/) [![NuGet](https://img.shields.io/nuget/dt/ComputeSharp.svg)](https://www.nuget.org/stats/packages/ComputeSharp?groupby=Version) +![.NET](https://github.com/Sergio0694/ComputeSharp/workflows/.NET/badge.svg) [![NuGet](https://img.shields.io/nuget/vpre/ComputeSharp.svg)](https://www.nuget.org/packages/ComputeSharp/) [![NuGet](https://img.shields.io/nuget/dt/ComputeSharp.svg)](https://www.nuget.org/stats/packages/ComputeSharp?groupby=Version) # What is it? @@ -45,7 +45,7 @@ int[] array = Enumerable.Range(0, 100).ToArray(); using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(array); ``` -The `AllocateReadWriteBuffer` extension takes care of creating a `ReadWriteBuffer` instance with the same size and contents of the input array. There are a number of overloads available as well, to create buffers of different types and with custom length. +The `AllocateReadWriteBuffer` extension takes care of creating a `ReadWriteBuffer` 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: @@ -76,7 +76,7 @@ buffer.CopyTo(array); ## Capturing variables -Shaders can store either GPU resources or custom values in their fields, so that they can be accessed when running on the GPU as well. This can be useful to pass some extra parameters to a shader (eg. some factor to multiply values by), that don't belong to a GPU buffer of their own. The captured variables need to be of a supported scalar or vector type so that they can be correctly used by the GPU shader in HLSL. Here is a list of the variable types currently supported by the library: +Shaders can store either GPU resources or custom values in their fields, so that they can be accessed when running on the GPU as well. This can be useful to pass some extra parameters to a shader (eg. some factor to multiply values by), that don't belong to a GPU buffer of their own. The captured variables need to be of a supported scalar or vector type so that they can be correctly used by the GPU shader in HLSL. Here is a list of the variable types currently supported: ✅ .NET scalar types: `bool`, `int`, `uint`, `float`, `double` @@ -92,14 +92,14 @@ Shaders can store either GPU resources or custom values in their fields, so that There are a number of extension APIs for the `GraphicsDevice` class that can be used to allocate GPU resources. Here is a breakdown of the main resource types that are available: -- `ReadWriteBuffer`: this type can be viewed as the equivalent of the `T[]` type, and can contain a writeable sequence of any of the supported types mentioned above. It is very flexible and works well in most situations. If you're just getting started with this library and are not sure about what kind of buffer to use, this is usually a good choice. +- `ReadWriteBuffer`: this type can be viewed as the equivalent of the `T[]` type, and can contain a writeable sequence of any of the supported types mentioned above. It is very flexible and works well in most situations. If you're just getting started and are not sure about what kind of buffer to use, this is usually a good choice. - `ReadOnlyBuffer`: this type represents a sequence of items that the GPU cannot write to. It is particularly useful to declare intent from within a compute shader and to avoid accidentally writing to data that is not supposed to change during the execution of a shader. - `ConstantBuffer`: this type is meant to be used for small sequences of items that never change during the execution of a shader. Compared to `ReadOnlyBuffer` is has more constraints, but can benefit from better caching on the GPU side (it is recommended to verify with proper benchmarking that this type is appropriate to use). Items within a `ConstantBuffer` instance are packed to 16 bytes, which helps the GPU to have a particularly fast access time to them, but the total size of the buffer is limited to around 64KB. Copying to and from this buffer can also have additional overhead, as the GPU needs to account for the possible padding for each item (as the 16 bytes alignment is not present on the CPU side). If you're in doubt about which buffer type to use, just use either `ReadOnlyBuffer` or `ReadWriteBuffer`, depending on whether or not you also need write access to that buffer on the GPU side. -- `ReadOnlyTexture2D` and `ReadWriteTexture2D`: these types represent a 2D texture with elements of a specific type. Note that textures are not just 2D arrays, and have additional characteristics and limitations. Items in a texture are stored with a tiled layout instead of with the classic row-major order that .NET `T[,]` arrays have, and this allows them to be extremely fast when accessing small areas of neighbouring items (due to better cache locality). This can offer a big performance speedup in operations that have a similar memory access pattern, such as blur effect or convolutions in general. Textures also have limitations in the type of items they can contain (eg. custom `struct` types are not supported), and you can check if a specific type is supported at runtime with the `GraphicsDevice.IsReadOnlyTexture2DSupportedForType()` and `IsReadWriteTexture2DSupportedForType()` methods. +- `ReadOnlyTexture2D` and `ReadWriteTexture2D`: these types represent a 2D texture with elements of a specific type. Note that textures are not just 2D arrays, and have additional characteristics and limitations. Items in a texture are stored with a tiled layout instead of with the classic row-major order that .NET `T[,]` arrays have, and this allows them to be extremely fast when accessing small areas of neighbouring items (due to better cache locality). This can offer a big performance speedup in operations that have a similar memory access pattern, such as blur effect or convolutions in general. Textures also have limitations in the type of items they can contain (eg. custom `struct` types are not supported), and you can check if a specific type is supported at runtime with `GraphicsDevice.IsReadOnlyTexture2DSupportedForType()` and `IsReadWriteTexture2DSupportedForType()`. - `ReadOnlyTexture3D` and `ReadWriteTexture3D`: these are just like 2D textures, but in 3 dimensions. The same characteristics and limitations apply, with the addition of the fact that the depth axis has a much smaller limit on the size it can have. The `GraphicsDevice.IsReadOnlyTexture3DSupportedForType()` and `IsReadWriteTexture3DSupportedForType()` methods can be used to check for type support at runtime. - `ReadOnlyTexture2D` and `ReadWriteTexture2D`: these texture types are particularly useful when doing image processing from a compute shader, as they allow the CPU to perform pixel format conversion automatically. The two type parameters indicate the type of items in a texture when exposed on the CPU side (as `T` items) or on the GPU side (as `TPixel` items). The type parameter `T` can be any of the existing pixel formats available in **ComputeSharp** (such as `Rgba32` and `Bgra32`), while the `TPixel` parameter represents the type of elements the GPU will be working with (for instance, `Float4` for `Rgba32`). These texture types have a number of benefits such as lower memory usage and reduced overhead on the CPU (as there is no need to manually do the pixel format conversion when copying data back and forth from the GPU). - `ReadOnlyTexture3D` and `ReadWriteTexture3D`: these types are just like their 2D equivalent types, with the same support for automatic pixel format conversion on the GPU side. -- `UploadBuffer`: this type can be used in more advanced scenarios where performance is particularly important. It represents a buffer that can be copied directly to a GPU buffer (such as a `ReadOnlyBuffer`), without the need to create a temporary transfer buffer to do so. If there is a large number of copy operations being performed to GPU buffers, it can be beneficial to create a single `UploadBuffer` instance to load data to copy to the GPU, and execute the copy from there. +- `UploadBuffer`: this type can be used in more advanced scenarios where performance is particularly important. It represents a buffer that can be copied directly to a GPU structured buffer (either `ReadWriteBuffer` or `ReadOnlyBuffer`), without the need to first create a temporary transfer buffer to do so. If there is a large number of copy operations being performed to GPU buffers, it can be beneficial to create a single `UploadBuffer` instance to load data to copy to the GPU, and execute a single copy from there. - `ReadBackBuffer`: this type is another advanced buffer type that is analogous to `UploadBuffer`, but that can be used to copy data back from a GPU buffer. In particular, this buffer can also be accessed quickly by the GPU when reading or writing data from it, so it can also be used for further processing of data on the CPU side, without the need to copy the data onto another buffer first (such as a .NET array). - `UploadTexture2D`, `UploadTexture3D`, `ReadBackTexture2D` and `ReadBackTexture3D`: these types are conceptually similar to `UploadBuffer` and `ReadBackBuffer`, with the main difference being that they can be used to copy data back and forth from 2D and 3D textures respectively. @@ -124,11 +124,11 @@ Float4 diagonal = matrix[M11, M22, M33, M44]; Float4 vertices = matrix[M11, M14, M44, M41]; ``` -Matrix types also include a number of built-in operators to work with vector types, and the `Hlsl` class detailed below (see [HLSL intrinsics](#hlsl-intrinsics)) also includes several overloads for the available methods to work on both matrix and vector types at the same time (eg. for row/matrix multiplication). +Matrix types also include a number of built-in operators to work with vector types, and the `Hlsl` class detailed below (see [HLSL intrinsics](#hlsl-intrinsics)) also includes several overloads for the available methods to work on both matrix and vector types at the same time (eg. for row/matrix multiplication and other common linear algebra operations). ## HLSL intrinsics -**ComputeSharp** offers support for all HLSL intrinsics that can be used from compute shaders. These are special functions (usually representing mathematical operations) that are optimized on the GPU and can execute very efficiently. Some of them can be mapped automatically from methods in the `System.Math` type (such as `Math.Sin` and `Math.Cos`), but if you want to have direct access to all of them you can just use the methods in the `Hlsl` class from a compute shader, and **ComputeSharp** will rewrite all those calls to use the native intrinsics in the compute shaders where those are used. +**ComputeSharp** offers support for all HLSL intrinsics that can be used from compute shaders. These are special functions (usually representing mathematical operations) that are optimized on the GPU and can execute very efficiently. Some of them can be mapped automatically from methods in the `System.Math` type, but if you want to have direct access to all of them you can just use the methods in the `Hlsl` class from a compute shader, and **ComputeSharp** will rewrite all those calls to use the native intrinsics in the compute shaders where those are used. Here's an example of a shader that applies the [softmax function](https://en.wikipedia.org/wiki/Rectifier_(neural_networks)) to all the items in a given buffer: