Hello Triangle Slang¶
This example shows how to use Slang shaders instead of GLSL in your KDGpu applications. Slang is a shading language developed by NVIDIA that is designed to be a modern and expressive alternative to GLSL and HLSL, while still compiling to SPIR-V for use with Vulkan.
The example uses the KDGpuExample helper API for simplified setup. For comparison, see Hello Triangle which uses GLSL shaders, or Hello Triangle HLSL which uses HLSL shaders.
Overview¶
What this example demonstrates:
- Compiling Slang shaders to SPIR-V using slangc (Slang compiler)
- Loading and using SPIR-V modules compiled from Slang
- Slang syntax and its similarities to HLSL
- Using Slang shader stage attributes
When to use Slang:
- You want a modern, expressive shading language with strong type safety
- You want to share shader code across multiple backends (Vulkan, D3D12, Metal, CUDA)
- You prefer explicit shader stage annotations over file-extension conventions
- You want to use advanced Slang features like generics and interfaces in shaders
Vulkan Requirements¶
- Vulkan Version: 1.0+
- Extensions: None (SPIR-V is Vulkan's native shader format)
- Build Tools: slangc (Slang compiler) for compiling Slang to SPIR-V
Key Concepts¶
Slang vs GLSL:
Slang and GLSL have different syntax for shader inputs, outputs, and resource bindings:
- Entry Points: Slang uses C-style functions annotated with
[shader("vertex")]/[shader("fragment")]; GLSL usesvoid [main()](bindgroup__indexing_2main_8cpp.md#function-main) - Semantics: Slang uses semantics like
SV_Position,COLOR0(similar to HLSL); GLSL useslayout(location = N) - Resource Bindings: Slang uses
[[vk::binding(N)]]attributes; GLSL useslayout(binding = N) - Shader Stages: Slang uses
[shader("vertex")]/[shader("fragment")]annotations; GLSL uses separate files with.vert/.fragextensions
slangc Compilation:
The Slang compiler (slangc) compiles Slang source code directly to SPIR-V bytecode that Vulkan understands.
It is invoked per-stage with -stage vertex or -stage fragment.
For more on Slang: https://shader-slang.com/
Implementation¶
The rendering code is identical to Hello Triangle — the only difference is loading Slang-compiled shaders instead of GLSL-compiled ones:
1 2 3 4 5 | |
Filename: hello_triangle/hello_triangle.cpp
Note that the loaded files are still .spv (SPIR-V bytecode) — the Slang source has already been compiled to SPIR-V at build time.
Build-Time Shader Compilation:
The CMake build system automatically compiles Slang shaders using slangc when you build the project.
The KDGpu_CompileSlangShaderSet CMake function handles this, invoking slangc with:
-target spirv: Generate SPIR-V output-stage vertex/-stage fragment: Specify the shader stage-o: Output file path
Slang Shader Example:
Here's what the Slang vertex shader looks like (compare to GLSL and HLSL):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
See Also¶
- Hello Triangle - The same example using GLSL shaders
- Hello Triangle HLSL - The same example using HLSL shaders
- Slang Language Documentation - Official Slang language reference
- Slang GitHub Repository - Slang compiler source and documentation
- SPIR-V Specification - Vulkan's shader bytecode format
Updated on 2026-03-31 at 00:02:07 +0000