--- title: Your first Slang shader layout: page description: Your first Slang shader permalink: "/docs/first-slang-shader" intro_image_absolute: true intro_image_hide_on_mobile: false --- In this tutorial, we demonstrate how to write a simple compute shader in Slang that adds numbers from two buffers and writes the results into a third buffer. To start, create a text file named `hello-world.slang` in any directory, and paste the following content in the newly created file: ```hlsl // hello-world.slang StructuredBuffer buffer0; StructuredBuffer buffer1; RWStructuredBuffer result; [shader("compute")] [numthreads(1,1,1)] void computeMain(uint3 threadId : SV_DispatchThreadID) { uint index = threadId.x; result[index] = buffer0[index] + buffer1[index]; } ``` > #### Note #### > Slang has official language extension support for both [Visual Studio](https://marketplace.visualstudio.com/items?itemName=shader-slang.slang-vs-extension) and [Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=shader-slang.slang-language-extension). The extensions are powered by the Slang compiler to support a wide range of > assisting features including auto-completion, function signature hinting, semantic highlighting and more. As you can see, `hello-world.slang` is no different from a normal HLSL shader file. In fact, Slang is compatible with most HLSL code you would write. On top of HLSL, Slang has added many new language and compiler features that simplifies various tasks with shader code, which we will cover in future chapters. For now we will demonstrate one key feature of Slang: cross-compiling to different platforms. Slang supports compiling shaders into many different targets including Direct3D 11, Direct3D 12, Vulkan, CUDA and C++ (for execution on CPU). You can run `slangc` with the following command line to compile `hello-world.slang` into Vulkan SPIR-V: ```bat .\slangc.exe hello-world.slang -profile glsl_450 -target spirv -o hello-world.spv -entry computeMain ``` If you would like to see the equivalent GLSL of the generated SPIR-V code, simply change the `-target` argument to `glsl`: ```bat .\slangc.exe hello-world.slang -profile glsl_450 -target glsl -o hello-world.glsl -entry computeMain ``` The resulting `hello-world.glsl` generated by `slangc` is shown below: ```glsl // hello-world.glsl (generated by slangc) #version 450 layout(row_major) uniform; layout(row_major) buffer; #line 2 0 layout(std430, binding = 0) readonly buffer _S1 { float _data[]; } buffer0_0; #line 3 layout(std430, binding = 1) readonly buffer _S2 { float _data[]; } buffer1_0; #line 4 layout(std430, binding = 2) buffer _S3 { float _data[]; } result_0; layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in; void main() { #line 10 uint index_0 = gl_GlobalInvocationID.x; float _S4 = ((buffer0_0)._data[(index_0)]); #line 11 float _S5 = ((buffer1_0)._data[(index_0)]); #line 11 float _S6 = _S4 + _S5; #line 11 ((result_0)._data[(index_0)]) = _S6; #line 8 return; } ``` > #### Note #### > You can check out how this hello world shader compiles to different targets on [Slang Playground](/slang-playground/?target=SPIRV&code=eJyFjz0LwjAQhvf7FUenih9V3Iw6SBcHFxUdSpHYpDYQE8mHFsT_bu2Hm3g33fM-B3dRhAWXUg8f2kg2spKqC-yc8ZnzhrOVz3Nu5rnU1C3xXE9j8keYENgefymGWy8dAUhsQRk3YZDp6807HvRSSJS_usJwymw4GVRdsbsWDFtnQ4UKvVBuio22ZjjD3eEUC3ujLiv2DY178ASs6uOiUIyXuPiujEpSh80pSR2nVd6-14F-904LCLzedkNpAg). As you can see, things are being translated just as expected to GLSL: the HLSL `StructuredBuffer` and `RWStructuredBuffer` types are mapped to shader storage objects and the `[numthreads]` attribute are translated into proper `layout(...) in` qualifier on the `main` entry-point. Note that in the generated GLSL code, all shader parameters are qualified with explicit binding layouts. This is because Slang provides a guarantee that all parameters will have fixed bindings regardless of shader optimization. Without generating explicit binding layout qualifiers, the downstream compiler in the driver may change the binding of a parameter depending on whether any preceding parameters are eliminated during optimization passes. In practice, this causes a pain in application code, where developers will need to rely on run-time reflection to determine the binding location of a compiled shader kernel. The issue gets harder to manage when the application also needs to deal with shader specializations. Since Slang will always generate explicit binding locations in its output on all targets as if no parameters are eliminated, the user is assured that parameters always gets a deterministic binding location without having to write any manual binding qualifiers in the Slang code themselves. In fact, we strongly encourage users not to qualify their Slang code with explicit binding qualifiers and let the Slang compiler do its work to properly lay out parameters. This is best practice to maintain code modularity and avoid potential binding location conflicts between different shader modules. ## The full example The full Vulkan example that sets up and runs the `hello-world.slang` shader in located in the [/examples/hello-world](https://github.com/shader-slang/slang/tree/master/examples/hello-world) directory of the Slang repository. The example code initializes a Vulkan context and runs the compiled SPIR-V code. The example code demonstrates how to use the Slang API to load and compile shaders. ## Further Reading See how to write vertex and fragment shaders in this [Slang Playground Demo](/slang-playground/?target=SPIRV&code=eJyVU01r20AQvetXDD7Z4MqJm0OJUh_iXHoIDdgEijFlI41tkdWu2J11bUr_e2c_vJHpqUKg1czozXszT7MZrA-tBTyJrpcIDXZaWTKC0MJB_wLSHNu1CuGIhvAEQjWwM2LfoSKwB9GgAT6ac69bRbYsZgmSbwG17vpW4iet5PnSZApWAx3QoK9RGrSj3hGj6s7HoZfivDfaqSag_dAOaqHAWQzp0TKCjuDNEWnlOaY-nB-I8dzb3Y4bMVUSZo-eX8HyXE3wGvQUvwvgaye1oM_Qa9tSq1U1DCptOiEHoTm4Y1X8yUjPPC3TCpmweAwg5Bs2eo0ncga_qQZPVc5FvOvcB9iqRoUJaRVCXNU8Oq_j4dJpAV062Yib0OZPQPFkN7fzL9sh8FJ0aMRQ793pDrpjH4pehOE8Yz5KXb8_BBYLsP5RFUvvCaEosYhIC16Kf1bFKozbrIg5gR28VB_D_u7oqnUeNdzD6vXny_8NfhONNx5FU44m28J3SB59Fq0ax_XywNlak9Q61ES3RdR4LjOXr9A5OY66Sh7NNLEdB5hcN4Xb8mYyucKIZBkhlg65pwp3zFkvw2cM8q5UpjRUdvnHvLY0skso6vNaoro4wnUweFIqkS5O4KZhjWV2RnzNDtrcbMt__bqtMlCtpTYMkwDKuO_xcNPTLGxypSx86oX9Be1nf3g).