Skip to content

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 uses void [main()](bindgroup__indexing_2main_8cpp.md#function-main)
  • Semantics: Slang uses semantics like SV_Position, COLOR0 (similar to HLSL); GLSL uses layout(location = N)
  • Resource Bindings: Slang uses [[vk::binding(N)]] attributes; GLSL uses layout(binding = N)
  • Shader Stages: Slang uses [shader("vertex")] / [shader("fragment")] annotations; GLSL uses separate files with .vert / .frag extensions

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
    auto vertexShaderPath = KDGpuExample::assetDir().file("shaders/examples/hello_triangle_slang/hello_triangle.vert.spv");
    auto vertexShader = m_device.createShaderModule(KDGpuExample::readShaderFile(vertexShaderPath));

    auto fragmentShaderPath = KDGpuExample::assetDir().file("shaders/examples/hello_triangle_slang/hello_triangle.frag.spv");
    auto fragmentShader = m_device.createShaderModule(KDGpuExample::readShaderFile(fragmentShaderPath));

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
[[vk::binding(0)]]
cbuffer Transform { float4x4 model; };

struct VSInput { float3 position : POSITION; float3 color : COLOR0; };
struct VSOutput { float4 position : SV_Position; float3 color : COLOR0; };

[shader("vertex")]
VSOutput vertexMain(VSInput input)
{
    VSOutput output;
    output.position = mul(model, float4(input.position, 1.0f));
    output.color = input.color;
    return output;
}

See Also


Updated on 2026-03-31 at 00:02:07 +0000