ComputeSharp
repository·main·Indexed 25 days ago
https://github.com/sergio0694/computesharpA .NET library for high-performance GPU computing that allows developers to write C# code executed as HLSL shaders via DirectX 12 and Direct2D. It provides APIs for accessing GPU devices, allocating buffers and textures, and moving data between RAM and GPU. The ecosystem includes specialized packages for WinUI 3, UWP, D2D1 pixel shaders, PIX debugging, and DXC shader reflection.
What's inside ComputeSharp
- ComputeSharp.WinUI is a WinUI 3 library that provides specialized controls for rendering DX12 shaders. It leverages the core ComputeSharp engine to bridge high-performance GPU compute capabilities with the WinUI 3 application framework.
Overview of ComputeSharp
mainComputeSharp is a .NET library designed to run C# code in parallel on the GPU using DX12, D2D1, and dynamically generated HLSL compute and pixel shaders.
Key capabilities include:
- Accessing GPU devices.
- Allocating GPU buffers and textures.
- Moving data between GPU and RAM.
- Writing compute shaders entirely in C#.
Overview of ComputeSharp.Dxc
mainComputeSharp.Dxc is an extension library for ComputeSharp that bundles the DXC (DirectX Shader Compiler) and enables shader reflection. It is designed for users who need to inspect shaders generated by ComputeSharp, providing access to the compiled HLSL code and statistics exposed by DirectX 12 reflection APIs.Overview of ComputeSharp.Uwp
mainComputeSharp.Uwp is a Universal Windows Platform (UWP) library that provides specialized controls for rendering DX12 shaders. It leverages the core ComputeSharp engine to enable high-performance GPU computing and shader rendering within UWP applications.Use ComputeSharp.D2D1.Uwp for D2D1 pixel shaders in UWP
mainComputeSharp.D2D1.Uwp is a UWP library that provides APIs to leverage D2D1 functionality. It allows you to use D2D1 pixel shaders powered by ComputeSharp.D2D1 within Universal Windows Platform applications.
To implement custom effects, you should follow the Win2D patterns for custom effects. For detailed implementation guidance, refer to the official Win2D documentation on custom effects.
Get started with ComputeSharp
mainComputeSharp allows you to write DX12 compute shaders entirely in C#. It provides APIs to access GPU devices, allocate buffers and textures, and move data between RAM and GPU.
If a supported GPU is not found, ComputeSharp automatically falls back to a WARP device, which emulates GPU execution on the CPU, ensuring your code runs without manual fallback logic.
Use ComputeSharp.Pix for PIX debugging support
mainComputeSharp.Pix is an extension library for ComputeSharp designed to enable PIX support. This allows developers to produce debugging information for ComputeSharp workloads using the PIX tool.Understand the role of ComputeSharp.Core
mainComputeSharp.Core is the foundational package containing basic primitives and shared infrastructure for the entire ComputeSharp ecosystem.
Important Note: This package is intended to be a transitive dependency. Developers should not reference
ComputeSharp.Coredirectly. Instead, you should install and use the specific ComputeSharp packages that provide the functionality you need (e.g., for GPU acceleration or specific compute tasks).Use ComputeSharp.D2D1.WinUI for WinUI 3 D2D1 effects
mainComputeSharp.D2D1.WinUI is a WinUI 3 library that provides APIs to leverage D2D1 functionality. It allows you to use D2D1 pixel shaders powered by theComputeSharp.D2D1package within WinUI 3 applications. This is primarily used for implementing custom effects. For implementation details on custom effects, refer to the Win2D documentation on custom effects.Configure D3D12MA as the memory allocator for ComputeSharp
mainTo enable D3D12MA as the memory allocator for graphics resources in ComputeSharp, use the
AllocationServices.ConfigureAllocatorFactorymethod at application startup. Passing a new instance ofD3D12MemoryAllocatorFactoryto this method ensures that ComputeSharp leverages D3D12MA for all subsequent resource allocations.AllocationServices.ConfigureAllocatorFactory(new D3D12MemoryAllocatorFactory());Write D2D1 pixel shaders with ComputeSharp.D2D1
mainYou can write D2D1 pixel shaders entirely in C# by implementing the
ID2D1PixelShaderinterface. Use attributes to define input counts, input types, and shader profiles. TheExecutemethod must return afloat4representing the pixel color.Required attributes:
[D2DInputCount(n)]: Specifies the number of inputs.[D2DInputSimple(index)]: Specifies a simple input at a specific index.[D2DShaderProfile(D2D1ShaderProfile.ProfileName)]: Sets the shader profile (e.g.,D2D1ShaderProfile.PixelShader50).[D2DGeneratedPixelShaderDescriptor]: Required for generating the descriptor.
[D2DInputCount(1)] [D2DInputSimple(0)] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] public readonly partial struct DifferenceEffect(float amount) : ID2D1PixelShader { public float4 Execute() { float4 color = D2D.GetInput(0); float3 rgb = Hlsl.Saturate(this.amount - color.RGB); return new(rgb, 1); } }Install ComputeSharp preview builds
mainIf you need to use preview builds, you can access them via the following NuGet source:
https://pkgs.computesharp.dev/index.json(Preview builds are also available on GitHub Packages).