Skip to content

Commit

Permalink
Textures example
Browse files Browse the repository at this point in the history
  • Loading branch information
ccummingsNV committed Jan 8, 2025
1 parent 649f4bf commit 69b8015
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ By bringing these 2 technologies together with a simple and flexible Python libr
installation
firstfunctions
buffers
textures

.. toctree::
:hidden:
Expand Down
60 changes: 60 additions & 0 deletions docs/textures.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
Brighten A Texture
==================

In this simple example we'll see the use of SlangPy to read/write a texture, along with
the use of simple broadcasting and inout parameters. `tev <https://github.com/Tom94/tev>`_ is
required to run the example and see results.

The full code for this example can be found `here <>`_

The Slang code is a very simple function that takes a constant and adds it to the value
of an ``inout`` parameter.

.. code-block::
// Add an amount to a given pixel
void brighten(float4 amount, inout float4 pixel)
{
pixel += amount;
}
We'll skip device initialization, and go straight to creating/showing a random texture:

.. code-block:: python
#... device init + module load here ...
# Generate a random image
rand_image = np.random.rand(128*128*4).astype(np.float32)*0.25
tex = device.create_texture(width=128, height=128, format=sgl.Format.rgba32_float,
usage=sgl.ResourceUsage.shader_resource | sgl.ResourceUsage.unordered_access,
data=rand_image)
# Display it with tev
sgl.tev.show(tex, name='photo')
Note the texture as created is both a shader resource and an unordered access resource so it
can be read and written to in a shader.

We can now call the ``brighten`` function and show the result as usual:

.. code-block:: python
# Call the module's add function, passing:
# - a float4 constant that'll be broadcast to every pixel
# - the texture to an inout parameter
module.brighten(sgl.float4(0.5), tex)
# Show the result
sgl.tev.show(tex, name='brighter')
In this case SlangPy infers that this is a `2D` call, because it's passing a 2D texture of float4s
into a function that takes a float4. As the first parameter is a single float4, it gets broadcast
to every thread. Because the second parameter is an inout, SlangPy knows to both read and write
to the texture.

This very simple example shows manipulation of texture pixels and broadcasting. We could equally
have used the same function to add together 2 textures, or buffers, or a buffer and a texture, or
a numpy array to a texture etc etc!


Binary file removed examples/first_function/photo.jpg
Binary file not shown.
7 changes: 7 additions & 0 deletions examples/textures/example.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: Apache-2.0

// Add an amount to a given pixel
void brighten(float4 amount, inout float4 pixel)
{
pixel += amount;
}
34 changes: 34 additions & 0 deletions examples/textures/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# SPDX-License-Identifier: Apache-2.0

import sgl
import slangpy as spy
import pathlib
import numpy as np

# Create an SGL device with the slangpy+local include paths
device = sgl.Device(compiler_options={
"include_paths": [
spy.SHADER_PATH,
pathlib.Path(__file__).parent.absolute(),
],
})

# Load module
module = spy.Module.load_from_file(device, "example.slang")

# Generate a random image
rand_image = np.random.rand(128*128*4).astype(np.float32)*0.25
tex = device.create_texture(width=128, height=128, format=sgl.Format.rgba32_float,
usage=sgl.ResourceUsage.shader_resource | sgl.ResourceUsage.unordered_access,
data=rand_image)

# Display it with tev
sgl.tev.show(tex, name='photo')

# Call the module's add function, passing:
# - a float4 constant that'll be broadcast to every pixel
# - the texture to an inout parameter
module.brighten(sgl.float4(0.5), tex)

# Show the result
sgl.tev.show(tex, name='brighter')

0 comments on commit 69b8015

Please sign in to comment.