ComputeSharp

repository·main·Indexed 25 days ago

https://github.com/sergio0694/computesharp

A .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.

Tokens
3.5K
Snippets
7
Records
26
Agent score
76%

What's inside ComputeSharp

  1. Overview of ComputeSharp

    main

    ComputeSharp 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#.
  2. Overview of ComputeSharp.Dxc

    main
    ComputeSharp.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.
  3. Use ComputeSharp.D2D1.Uwp for D2D1 pixel shaders in UWP

    main

    ComputeSharp.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.

  4. Get started with ComputeSharp

    main

    ComputeSharp 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.

  5. Understand the role of ComputeSharp.Core

    main

    ComputeSharp.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.Core directly. 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).

  6. Use ComputeSharp.D2D1.WinUI for WinUI 3 D2D1 effects

    main
    ComputeSharp.D2D1.WinUI is a WinUI 3 library that provides APIs to leverage D2D1 functionality. It allows you to use D2D1 pixel shaders powered by the ComputeSharp.D2D1 package 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.
  7. Configure D3D12MA as the memory allocator for ComputeSharp

    main

    To enable D3D12MA as the memory allocator for graphics resources in ComputeSharp, use the AllocationServices.ConfigureAllocatorFactory method at application startup. Passing a new instance of D3D12MemoryAllocatorFactory to this method ensures that ComputeSharp leverages D3D12MA for all subsequent resource allocations.

    AllocationServices.ConfigureAllocatorFactory(new D3D12MemoryAllocatorFactory());
  8. Write D2D1 pixel shaders with ComputeSharp.D2D1

    main

    You can write D2D1 pixel shaders entirely in C# by implementing the ID2D1PixelShader interface. Use attributes to define input counts, input types, and shader profiles. The Execute method must return a float4 representing 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);
        }
    }