A comprehensive .NET wrapper for the Slang Shader Language compiler, providing seamless integration of shader compilation and reflection capabilities into .NET applications.
$ dotnet add package Slang.SdkNew Updated to recent slang version: 2025.13.2
A comprehensive .NET wrapper for the Slang Shader Language Sdk, providing seamless integration of shader compilation and reflection capabilities into .NET applications.
Slang.Sdk Includes the following: -Compilation API -Reflection API -NEW: 'slangc' CLI API
Update: Release roadmap is now live (please see below). This provides info on releases until v1.0.0.
Note: Slang.Sdk is currently in early development and will remain in alpha until v1.0.0. For experimental use only. Do not include this package for production builds.
Disclaimer: We are the developers and maintainers of this C# wrapper library only, not of the Slang shader language itself. Slang is developed and maintained by NVIDIA. This project provides .NET bindings to make Slang accessible to C# developers.
We will use the latest Slang api version: 2025.13.2 as the underlying native version until after v1.0.0 release. Expect at least one release at the end of every month after v0.5.1:
Note: The release roadmap may change in the future.
Sponsoring Development:
Slang.Sdk is an open-source project maintained by volunteers. Your sponsorships directly support:
If Slang.Sdk adds value to your project or organization, please consider sponsoring our work. Even small contributions make a significant difference.
❤️Make sure to also support the slang team directly so that they can continue to improve the underlying api: Slang Team
Slang is purpose-built to bring modularity, scalability, and developer-friendliness to modern GPU programming. Whether you're building apps, games, or ML pipelines, here's why it stands out:
Slang doesn’t just let you write shaders—it empowers you to design systems. Clean, composable, and powerful. It's the shader language for developers who think architecturally.
Traditional HLSL/GLSL shaders have limitations that Slang addresses:
| Traditional Shaders | Slang Advantages |
|---|---|
| ❌ No code reuse between targets | ✅ Write once, compile to HLSL, GLSL, Metal, etc. |
| ❌ Limited modularity | ✅ True modules and interfaces |
| ❌ No generic programming | ✅ Generics and template metaprogramming |
| ❌ Manual resource binding | ✅ Automatic binding generation |
| ❌ Platform-specific code | ✅ Cross-platform shader source |
Install via NuGet Package Manager:
Install-Package Slang.Sdk
Or via .NET CLI:
dotnet add package Slang.Sdk
Or add to your project file:
<PackageReference Include="Slang.Sdk" Version="0.5.1" />
If you just want to call slangc CLI tooling directly from C#, we got you covered:
Default directory is:
AppDomain.CurrentDomain.BaseDirectory;
It can be changed like this:
CLI.WorkingDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"\Shaders");
using Slang.Sdk;
// Call slangc from C#
var result = CLI.slangc(
target: "hlsl",
profile: "cs_5_0",
entry: "CS",
inputFiles: ["AverageColor.slang"]);
// Prints the compiled shader source
Console.WriteLine(result.StdOut);
using Slang.Sdk;
// Output file will be in the WorkingDirectory
var result = CLI.slangc(
target: "hlsl",
profile: "cs_5_0",
entry: "CS",
outputPath: "output1.hlsl",
inputFiles: ["AverageColor.slang"]);
SlangC_Options.Builder paramsBuilder = new SlangC_Options.Builder()
.SetTarget("hlsl")
.SetProfile("cs_5_0")
.SetEntry("CS")
.AddIncludePaths(Path.Combine(CLI.WorkingDirectory, "AverageColor.slang"));
var cliResult = CLI.slangc(paramsBuilder.Build());
string args = "-target spirv -profile sm_6_6 -stage compute -entry main -O 3 -g -source-embed-style auto -source-embed-language hlsl -source-embed-name MyShader -conformance myType:myInterface=myID -- 'AverageColor.slang'";
// -source-embed-style -source-embed-name flags are not currently suppored SlangC_Options, but can still be used as a raw string
var result = CLI.slangc(args);
Compilation in Slang.Net is quick and easy.
using Slang.Sdk;
using Slang.Sdk.Interop;
// Create the session
Session session = new Session.Builder()
.AddTarget(Targets.Hlsl.cs_5_0)
.Create();
// Load the module from the specified file
Module module = session.LoadModule("AverageColor", $@"{ AppDomain.CurrentDomain.BaseDirectory }Shaders\AverageColor.slang");
// Get the shader program from the module
Program program = module.Program;
// Compile the shader program
var compileResult = program.Targets[Targets.Hlsl.cs_5_0].Compile();
// Alternatively compile from an entrypoint
var entryPoint = program.Targets[Targets.Hlsl.cs_5_0].EntryPoints["CS"];
entryPoint.Compile();
// Print the generated source code length
Console.WriteLine(compileResult.SourceCode);
Multiple graphics backends. You can easily compile them all.
// Compile all targets
var allTargets = program.Targets;
foreach (var target in allTargets)
{
var targetCompileResult = target.Compile();
}
Multiple pipline stages? Use multiple sessions.
// This is required to enable Slang's GLSL support, since it defaults to Vulkan
Session.GlslEnabled = true;
// Vertex Stage Session targeting both DirectX 11 and OpenGL 450
_vsSession = new Session.Builder()
.AddTarget(Targets.Hlsl.vs_5_0)
.AddTarget(Targets.Glsl.v450)
.AddCompilerOption(CompilerOption.Name.AllowGLSL, new(CompilerOption.Value.Kind.Int, 1, 0, null, null))
.Create();
// Pixel Stage Session targeting both DirectX 11 and OpenGL 450
_psSession = new Session.Builder()
.AddTarget(Targets.Hlsl.ps_5_0)
.AddTarget(Targets.Glsl.v450)
.AddCompilerOption(CompilerOption.Name.AllowGLSL, new(CompilerOption.Value.Kind.Int, 1, 0, null, null))
.Create();
If you are a power user, Slang.Sdk is constantly including feature from the underlying API
Using Compile Options
var session = new Session.Builder()
.AddCompilerOption(CompilerOption.Name.WarningsAsErrors, new CompilerOption.Value(CompilerOption.Value.Kind.Int, 0, 0, "all", null))
.AddCompilerOption(CompilerOption.Name.Obfuscate, new CompilerOption.Value(CompilerOption.Value.Kind.Int, 1, 0, null, null))
.AddTarget(Targets.Hlsl.cs_5_0)
.Create();
Using Preprocessor Macros
var session = new Session.Builder()
.AddPreprocessorMacro("ENABLE_LIGHTING", "1")
.AddPreprocessorMacro("MAX_LIGHTS", "16")
.AddPreprocessorMacro("QUALITY_LEVEL", "HIGH")
.AddTarget(Targets.Hlsl.cs_5_0)
.Create();
Module Builders: Beautifully Abstracting CompileRequest
// Create session
var session = new Session.Builder()
.AddTarget(Targets.Hlsl.cs_6_0)
.Create();
// Using the new Module Builder
var fileBuilder = new Module.Builder(session)
.AddCodeGenTarget(Target.CompileTarget.Hlsl)
.AddPreprocessorDefine("MULTI_COMPUTE", "1")
.AddTranslationUnit(SourceLanguage.Slang, "MultiComputeShader", out int unitIndex)
.AddTranslationUnitSourceFile(unitIndex, Path.Combine(_workingDirectory, "MultiComputeShader.slang"));
// Add some compute entry points
fileBuilder.AddEntryPoint(0, "GaussianBlur", Stage.Compute);
fileBuilder.AddEntryPoint(0, "GenerateNoise", Stage.Compute);
// Create a Module from a Builder
var module = fileBuilder.Create();
foreach (var entryPoint in module.EntryPoints)
{
Console.WriteLine($"Index: {entryPoint.Index}, Name: {entryPoint.Name}");
}
module.EntryPoints.Where(entryPoint => entryPoint.Name == "CS").FirstOrDefault();
EntryPoint entryPoint = module.EntryPoints["CS"];
if (module.EntryPoints.TryGetValue("CS", out EntryPoint? entryPoint))
{
Console.WriteLine($"Found entry point: {entryPoint?.Name} at index {entryPoint?.Index}");
}
else
{
Console.WriteLine("Entry point 'CS' not found.");
return;
}
Note As of v0.5.1, all slang reflection types (with the exception of DeclReflection and a few other minor things) has been elegantly abstracted
Reflection Example
using Slang.Sdk;
using Slang.Sdk.Interop;
// Get the shader reflection for the specified target
ShaderReflection reflection = module.Program.Targets[Targets.Hlsl.cs_5_0]GetReflection();
// Get the shader reflection for the specified target
var parameters = reflection.Parameters;
foreach (var parameter in parameters)
{
Console.WriteLine($"Parameter Name: {parameter.Name}");
Console.WriteLine($"Type: {parameter.Type.Name}");
// This can be used for programmatic binding
Console.WriteLine($"BindingIndex: {parameter.BindingIndex}, BindingSpace: {parameter.BindingSpace}");
}
CLI API Sample Compilation API Sample Reflection API Sample Complete Silk.NET Example
As an early-stage project, we welcome community contributions to help Slang.Net grow. Found a bug or want to contribute? Visit our GitHub repository to:
We especially welcome contributions in these areas:
This project is licensed under the same liscense as the project it is wrapping: Apache-2.0 WITH LLVM-exception. See the LICENSE file for details.