How to use Slang node in OmniGraph

Video Tutorial

Video Tutorial on configuring simple Slang node in the action graph.

The example code from the tutorial is listed here.

Slang Function

Slang node, when dropped into a push graph, has only an input attribute for Code token and Instance count, while the node dropped into an action graph also has execution Exec In and Exec Out pins. The Slang function’s code can be previewed in the Slang Code Editor by clicking Edit in the node’s Property window next to the Code attribute. Node attributes for variables used in the function can be added or removed via dedicated buttons in the Add and Remove Attributes section in the top part of the Property window.

Action Graph Node

Code

The code attribute should always contain at least an empty compute function.

1void compute(uint instanceId)
2{
3}

Important

Defining Slang resources (buffers, textures) in the compute function is not allowed. The user needs to create a node attribute to declare a resource buffer for Slang function.

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. Type of attributes is input, output, and state. Input attributes are read-only while output and state are can be accessed for both reads and writes. Conversion table between OGN, USD and Slang data types can be found on a separate page:

Create Attribute

Execution attributes can be added or removed arbitrary. Constants which can be passed to the output execution attributes are:

EXEC_OUT_DISABLED
EXEC_OUT_ENABLED
EXEC_OUT_ENABLED_AND_PUSH
EXEC_OUT_LATENT_PUSH
EXEC_OUT_LATENT_FINISH

To create a node from the demo scene shown in a tutorial video, follow the next steps:

  1. Drop a node called Slang Function in the Action Graph

  2. In the Property window of the node, click Add button

  3. Create input attribute of type double and name it time

  4. Create output attribute of type double3 and name it position

  5. 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.”

Code Editor

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}

Note

Slang nodes authored in Create 2022.3.0 use deprecated type uint64_t for token attributes. By hitting Upgrade button in the bottom right of the Code editor, the node will be upgraded to the latest version and user needs to replace uint64_t types for tokens in their code by a new Slang Token type.

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. Only relevant for use with Slang LLVM 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.

    Generated Code

    Also, when a compilation error occurs, the line number refers to a position in the generated code not in the Compute Function tab.

    Generated Code Error

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 Functions 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}