Skip to content

Commit 78fb064

Browse files
committed
Add documentation on using _result=
Adds documentation page on how to use the _result argument to specify the return type for a non-void slang function. Fixes #69
1 parent 705876e commit 78fb064

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ If you use SlangPy in a research project leading to a publication, please cite t
5656
:caption: Basics
5757

5858
src/basics/firstfunctions
59+
src/basics/returntype
5960
src/basics/buffers
6061
src/basics/textures
6162
src/basics/nested

docs/src/basics/returntype.rst

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
Return Types
2+
============
3+
4+
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

Comments
 (0)