You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Slangpy can generate different types of container to hold the returned results of a Slang function. This is convenient for getting results in a preferred container type, such as a numpy array, texture, or tensor.
5
+
6
+
Let's start by reusing the example from :ref:`firstfunctions`.
7
+
8
+
Shader:
9
+
10
+
.. code-block::
11
+
12
+
// example.slang
13
+
14
+
// A simple function that adds two numbers together
15
+
float add(float a, float b)
16
+
{
17
+
return a + b;
18
+
}
19
+
20
+
In the original :ref:`firstfunctions` python example, we returned the result in a numpy array. Let's return it as a texture instead:
21
+
22
+
.. code-block:: python
23
+
24
+
## main.py
25
+
26
+
# ... initialization here ...
27
+
28
+
# Create a couple of buffers with 128x128 random floats
29
+
a = np.random.rand(128, 128).astype(np.float32)
30
+
b = np.random.rand(128, 128).astype(np.float32)
31
+
32
+
# Call our function and ask for a texture back
33
+
result = module.add(a, b, _result='texture')
34
+
35
+
# Print the first 5x5 values
36
+
print(result.to_numpy()[:5, :5])
37
+
38
+
# Display the result using tev
39
+
spy.tev.show(result, name='add random')
40
+
41
+
Here we use ``_result`` to specify that we want the result to be a texture.
42
+
43
+
The ``_result`` can be ``'numpy'``, ``'texture'``, or ``'tensor'``. You can also use ``_result`` to specify the type directly, like ``numpy.ndarray``, ``slangpy.Texture``, or ``slangpy.Tensor``. Or you can reuse an existing variable of one of those types by passing it directly.
44
+
45
+
You'll see more examples using ``_result`` in the rest of this documentation!
0 commit comments