Slang Function
Compute node compiling and executing Slang code
Slang node, when executed, runs a compute Slang function. Node’s input, output, and state attributes define variables that can be referenced in the Slang code. The code of the node’s function can be edited in the Slang Code editor and is saved in the USDA file as a node’s Code attribute.
Installation
To use this Node, you must enable omni.slangnode
in the Extension Manager.
Inputs
Name |
Type |
Description |
Default |
---|---|---|---|
Code (code) |
|
String containing the Slang source code |
|
Instance count (instanceCount) |
|
Number of instances running the compute function |
1 |
What is Slang
Slang is a shading language backward compatible with HLSL that makes it easier to build and maintain large shader codebases in a modular and extensible fashion, while also maintaining the highest possible performance on modern GPUs and graphics APIs. Slang is based on years of collaboration between researchers at NVIDIA, Carnegie Mellon University, and Stanford.
For better understanding of language usage and features, please refer to the User’s Guide provided by Slang developers.
When should I use Slang Node
The node allows users to write their own functions executed in OmniGraph. Slang code is compiled once during the stage load or on user request in the Code Editor. The node execution does not bring any additional overhead during the OmniGraph updates.
The current implementation only runs the Slang code single-threaded on the CPU target. Multithreaded and GPU support will be added in future releases.
How to use Slang node in OmniGraph
Video Tutorial
The example code from the tutorial is listed here.
Slang Function
When you use Slang Node in a Push Graph, it only has the Code token and Instance count input attributes. When you use it in an Action Graph, it also has Exec In and Exec Out pins. You can preview the Slang function’s code in the Slang Code Editor by clicking Edit in the node’s Property window next to the Code attribute. You can add or remove node attributes for variables used in the function by clicking the dedicated buttons in the Add and Remove Attributes section in the top part of the Property window.
Code
The code attribute should always contain at least an empty compute
function.
1void compute(uint instanceId)
2{
3}
Important
Defining non-static global variables in a Slang function is not allowed. You must create a node attribute to declare a resource buffer.
Instance Count
Slang node’s Instance count attribute input specifies how many times the function will be executed and the size of an output array. The uint instanceId
parameter of the main compute function then defines the position in the output array where the computed result can be stored (an example of the usage).
Important
Arrays are zero-indexed bounds checked which means, if an access to array is out of bounds, the value at the zero index is returned. An empty array thus always contains at least one element with a zero value by default.
Node Attributes
User-added node attributes can be used in a Slang function as variables. The attribute types are input, output, and state. Input attributes are read-only. Output and state can be accessed for both reads and writes. You can find a conversion table between OGN, USD, and Slang data types here.
To create a node from the demo scene shown in the tutorial video, follow the next steps:
Drop a node called Slang Function in the Action Graph
In the Property window of the node, click Add button
Create input attribute of type double and name it time
Create output attribute of type double3 and name it position
Continue with opening the Slang Code Editor
Slang Code Editor
To use the node’s attribute as a variable in the code, the attribute name has to be adjusted by a simple pattern. Please refer to the Variables from Attributes Syntax section to see examples of this pattern.
An example of a compute function from the tutorial can be copy pasted into the editor and compiled by hitting Save & Compile. The green tick next to the button indicates that the compilation was successful. Any errors, in case of failed compilation, are displayed in the Console window and a red cross appears next to the compile button. Handling compilation errors is described further in the text.
1void compute(uint instanceId)
2{
3 double time = inputs_time_get();
4
5 double k = 0.1f;
6 double r = 100.f + 10.f * sin(time);
7 double x = 100.f * sin(k * time);
8 double y = 100.f * cos(k * time);
9
10 double3 pos = double(x, 50.f, y);
11
12 outputs_position_set(pos);
13
14 outputs_execOut_set(EXEC_OUT_ENABLED);
15}
Slang Settings
Use Slang LLVM
Slang code is compiled by Slang LLVM compiler. The library is included in the extension and the compiler is used to compile Slang code by default. When turned off in Slang Settings, Slang looks for a default system C++ compiler.
Multithreaded Compilation
Each node would be compiled on a single thread to accelerate the initialization of multiple Slang nodes in the Stage during the scene load. This is only relevant when Slang LLVM is off.
Show Generated Code
This toggle shows a separate editor tab after switching to Generated Slang Code where the user can preview what functions are auto-generated from the node’s attribute before the actual code compilation is run.
Also, when a compilation error occurs, the line number refers to a position in the generated code not in the Compute Function tab.
The code can be edited and compiled even while the OmniGraph is running. The Slang node won’t be updated and does not output any data in case of failed compilation.
Slang Function Examples
Reading and writing arrays
1void compute(uint instanceId) 2{ 3 float3 pos = inputs_pointsIn_get(instanceId); 4 5 float time = float(inputs_time_get()); 6 7 pos.x += 10.f * cos(time); 8 pos.z += 10.f * sin(time); 9 10 outputs_pointsOut_set(instanceId, pos); 11 12 outputs_execOut_set(EXEC_OUT_ENABLED); 13}