Inline
Advantages of Inline Functions
Readability: Inline functions promote a clean and organized code structure by encapsulating logic, making shaders easier to read and understand.
Reusability: Encapsulating repeated code snippets in inline functions reduces redundancy and allows for straightforward reuse across multiple parts of the shader.
Maintainability: Updates or bug fixes can be applied to the function definition once, automatically propagating improvements throughout the shader.
Performance: Although the entire code of an inline function is inserted wherever it is called, minimizing function overhead can improve runtime performance in some cases.
Example
Here's a basic example of an inline function that adds two numbers:
In this example, add
is an inline function that takes two arguments, x
and y
, and returns their sum. This function can be called multiple times throughout your shader code to perform addition operations.
Functions as arguments
In osl you can use a function as arguments for other functions, methods and commands. This allows for powerful new syntax such as the .map()
method which iterates over each item in an array and overwrites it based on the function it is passed as argument.
You can also define a function earlier and then pass it as input
Last updated