diff --git a/docs/coming-from-hlsl.md b/docs/coming-from-hlsl.md index c5a425f9..20f29f99 100644 --- a/docs/coming-from-hlsl.md +++ b/docs/coming-from-hlsl.md @@ -1,3 +1,306 @@ # Migrating to Slang from HLSL -[//]: # (TODO: write documentation on HLSL onramp here, and update link in docs/index.md) \ No newline at end of file +## 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 |
| + +```hlsl +enum MyEnum +{ + MyEnum_A, + MyEnum_B, + MyEnum_C +}; + +int MyFunc() +{ + return int(MyEnum_A); +} +``` + | + +```hlsl +enum MyEnum +{ + A, + B, + C +}; + +int MyFunc() +{ + return int(MyEnum::A); +} +``` + |
| HLSL shader | Slang shader |
| + +```hlsl +struct Counter +{ + int count; + void increment() { count++; } +}; +``` + | + +```hlsl +struct Counter +{ + int count; + + [mutating] + void increment() { count++; } +}; +``` + |
| HLSL shader | Slang shader |
|
+
+```hlsl
+template |
+
+```hlsl
+__generic |
| HLSL shader | Slang shader |
|
+
+```hlsl
+template |
+
+```hlsl
+__generic |
| HLSL shader | Slang shader |
| + +```hlsl +struct MyStruct +{ + MyStruct operator+(MyStruct rhs) + { + MyStruct tmp; + tmp.value = value + rhs.value; + return tmp; + } + + float value; +}; +``` + | + +```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); +} +``` + |
| HLSL shader | Slang shader |
| + +```hlsl +struct MyType +{ + float values[12]; + + float operator[](int index) + { + return values[index]; + } +} +``` + | + +```hlsl +struct MyType +{ + float values[12]; + + __subscript(int index) -> float + { + get { return val[index]; } + set { val[index] = newValue; } + } +} +``` + |