Skip to content

DominicMaas/SlangShaderSharp

Repository files navigation

SlangShaderSharp

An in-development C# binding for the Slang shading language that targets .NET 8+ built on-top of Source Generated COM Wrappers.

Now available on NuGet: https://www.nuget.org/packages/SlangShaderSharp/

Most bindings have been implemented, but not fully tested. If you discover a missing or broken binding, please raise an issue on GitHub.

Versioning

The versioning of this library follows the versioning of the underlying Slang library where the Revision part is used to indicate changes in the bindings.

For Example:

SlangShaderSharp 2025.21.2.0 corresponds to Slang library version 2025.21.2. SlangShaderSharp 2025.21.2.1 corresponds to Slang library version 2025.21.2 with changes in the bindings only.

Usage

Loading a Module and Getting WGSL Code for a Compute Shader

// 1. Create Global Session

Slang.CreateGlobalSession(Slang.ApiVersion, out var globalSession).Succeeded.ShouldBeTrue();

// 2. Create Session

var sessionDesc = new SessionDesc
{
    Targets = [new TargetDesc { Format = SlangCompileTarget.Wgsl }],

    // Slang supports using the preprocessor.
    PreprocessorMacros = [
        new PreprocessorMacroDesc("BIAS_VALUE", "1138"),
        new PreprocessorMacroDesc("OTHER_MACRO", "float")
    ],
};

globalSession.CreateSession(sessionDesc, out var session).Succeeded.ShouldBeTrue();

// 3. Load module

var module = session.LoadModuleFromSource("test", "test.slang", Slang.CreateBlob("""
    StructuredBuffer<float> buffer0;
    StructuredBuffer<float> buffer1;
    RWStructuredBuffer<float> result;

    [shader("compute")]
    [numthreads(1,1,1)]
    void computeMain(uint3 threadId : SV_DispatchThreadID)
    {
        uint index = threadId.x;
        result[index] = buffer0[index] + buffer1[index];
    }
    """u8), out var moduleLoadError);

module.ShouldNotBeNull(moduleLoadError?.AsString ?? "Unknown Error");

// 4. Query Entry Points

module.FindEntryPointByName("computeMain", out var entryPoint).Succeeded.ShouldBeTrue();

// 5. Compose Modules + Entry Points

session.CreateCompositeComponentType([module, entryPoint], out var composedProgram, out _).Succeeded.ShouldBeTrue();

// 6. Link

composedProgram.Link(out _, out var linkError).Succeeded.ShouldBeTrue(linkError?.AsString ?? "Unknown Error");

// 7. Get Target Kernel Code

composedProgram.GetEntryPointCode(0, 0, out var wgslCode, out _).Succeeded.ShouldBeTrue();

// Output

_ = wgslCode.AsString;

// Done

Slang.Shutdown();

Extensions

ISlangBlob

  • ReadOnlySpan<byte> Buffer - View the blob data as a safe read-only span.
  • string AsString - Converts the blob data to a UTF-8 string.

Updating Bindings

Eventually this should be moved to a GitHub Action.

  1. Update update_slang.ps1 to point towards the desired Slang version.
  2. Run update_slang.ps1.
  3. Adjust any bindings as needed to incorporate any changes in the header files (diff compare the header files to make this easy!)

About

C# bindings for the Slang Shader Language compiler: https://github.com/shader-slang/slang

Topics

Resources

License

Stars

Watchers

Forks

Contributors 2

  •  
  •  

Languages