--- title: Migrating to Slang from HLSL layout: page description: Migrating to Slang from HLSL permalink: "/docs/coming-from-hlsl/" intro_image_absolute: true intro_image_hide_on_mobile: false --- ### Overview This guide provides a comprehensive overview of the primary syntax and feature differences between HLSL and Slang. It offers essential adjustments needed for a smooth transition and tips to enhance debugging and performance. While the languages share similarities, paying close attention to specific syntax and function conventions will ensure a seamless migration of HLSL shaders to Slang. ### Key Syntax and Feature Differences #### `enum` is scoped in Slang In HLSL, `enum` is unscoped, which means the enum values can be referred to without the enum's name. In Slang, `enum` is scoped, requiring explicit reference to the enum's name along with its values.
| HLSL shader | Slang shader |
| {% capture somemarkdown %} ```hlsl enum MyEnum { MyEnum_A, MyEnum_B, MyEnum_C }; int MyFunc() { return int(MyEnum_A); } ``` {% endcapture %} {{ somemarkdown | markdownify }} | {% capture somemarkdown %} ```hlsl enum MyEnum { A, B, C }; int MyFunc() { return int(MyEnum::A); } ``` {% endcapture %} {{ somemarkdown | markdownify }} |
| HLSL shader | Slang shader |
| {% capture somemarkdown %} ```hlsl struct Counter { int count; void increment() { count++; } }; ``` {% endcapture %} {{ somemarkdown | markdownify }} | {% capture somemarkdown %} ```hlsl struct Counter { int count; [mutating] void increment() { count++; } }; ``` {% endcapture %} {{ somemarkdown | markdownify }} |
| HLSL shader | Slang shader |
|
{% capture somemarkdown %}
```hlsl
template |
{% capture somemarkdown %}
```hlsl
__generic |
| HLSL shader | Slang shader |
|
{% capture somemarkdown %}
```hlsl
template |
{% capture somemarkdown %}
```hlsl
__generic |
| HLSL shader | Slang shader |
| {% capture somemarkdown %} ```hlsl struct MyStruct { MyStruct operator+(MyStruct rhs) { MyStruct tmp; tmp.value = value + rhs.value; return tmp; } float value; }; ``` {% endcapture %} {{ somemarkdown | markdownify }} | {% capture somemarkdown %} ```hlsl struct MyStruct { MyStruct operator_ADD(MyStruct rhs) { MyStruct tmp; tmp.value = value + rhs.value; return tmp; } float value; }; MyStruct operator+(MyStruct lhs, MyStruct rhs) { return lhs.operator_ADD(rhs); } ``` {% endcapture %} {{ somemarkdown | markdownify }} |
| HLSL shader | Slang shader |
| {% capture somemarkdown %} ```hlsl struct MyType { float values[12]; float operator[](int index) { return values[index]; } } ``` {% endcapture %} {{ somemarkdown | markdownify }} | {% capture somemarkdown %} ```hlsl struct MyType { float values[12]; __subscript(int index) -> float { get { return val[index]; } set { val[index] = newValue; } } } ``` {% endcapture %} {{ somemarkdown | markdownify }} |