Compiling

In order to compile SlangPy from source, the following prerequisites are required:

  • A C++20 compliant compiler (tested with Visual Studio 2022, GCC 11 and Clang 14)

  • Xcode >= 16, 16.4 recommended (on macOS)

  • Python >= 3.9

  • git

Optionally:

  • CUDA Toolkit >= 11.8; 12.8 recommended (on Windows/Linux, for cuda acceleration)

  • PyTorch >= 2.7.1 (for optional integration)

Tip

We strongly recommend using a Python virtual environment (anaconda on Windows, venv on Linux/macOS)

Cloning the repository

The first step is to clone the repository. This can be done by running the following command:

git clone https://github.com/shader-slang/slangpy.git --recursive

Setup

To make it easy to build slangpy reliably, an additional setup step is required:

# Install Python build prerequisites
pip install -r requirements-dev.txt

# On Windows
setup.bat

# On Linux and macOS
./setup.sh

This will do the following:

  • Make sure all git submodules are initialized and up-to-date.

  • On the first run, setup a .vscode directory with initial settings for VS Code.

This script can be run anytime to ensure that both git submodules and host tools are up-to-date.

Windows

To build on Windows, make sure you have a recent version of Visual Studio 2022 installed.

Open x64 Native Tools Command Prompt for VS 2022 and use the following commands to build the project:

# Configure
cmake --preset windows-msvc

# Build "Debug" configuration
cmake --build --preset windows-msvc-debug

# Build "Release" configuration
cmake --build --preset windows-msvc-release

The build artifacts are placed in build\windows-msvc\bin\Debug or build\windows-msvc\bin\Release.

Alternatively you can use the windows-vs2022 preset to configure the project as a Visual Studio 2022 solution stored in build\windows-vs2022.

Tested on:

  • Windows 10 (build 19045)

  • Visual Studio 2022 (Version 17.8.0)

  • CMake 3.27.7

  • Ninja 1.11.1

Linux

To build on Linux, make sure you have the required build tools and dependencies installed. The following commands can be used to install the required build tools and dependencies:

# Install build tools
sudo apt install build-essential

# Install required build dependencies
sudo apt install libxinerama-dev libxcursor-dev xorg-dev libglu1-mesa-dev pkg-config

Then use the following commands to build the project:

# Configure
cmake --preset linux-gcc

# Build "Debug" configuration
cmake --build --preset linux-gcc-debug

# Build "Release" configuration
cmake --build --preset linux-gcc-release

The build artifacts are placed in build\linux-gcc\bin\Debug or build\linux-gcc\bin\Release.

Alternativaly you can also use the linux-clang preset to use the Clang compiler.

Tested on:

  • Ubuntu 22.04

  • GCC 11.4.0

  • CMake 3.27.7

  • Ninja 1.11.1

Building without GLFW

SGL_ENABLE_GLFW defaults to AUTO, enabling window support when its build dependencies are available. Set it to ON to require GLFW support or OFF to omit it, for example:

cmake --preset linux-gcc -DSGL_ENABLE_GLFW=OFF
cmake --build --preset linux-gcc-debug

Compute and offscreen rendering remain available, but creating a Window raises an error. On Linux, this avoids the need for X11 development headers.

macOS

To build on macOS, make sure you have a recent version of XCode installed. You also need to install the XCode command line tools by running the following command:

xcode-select --install

Some additional command line build tools are also required. An easy way to install these is to install brew, and then use the following commands:

brew install cmake ninja pkg-config git-lfs
git lfs install

If git-lfs wasn’t installed before you cloned SlangPy, you will need to use the following commands to retrieve and check out the files stored in LFS:

git submodule foreach --recursive git lfs fetch
git submodule foreach --recursive git lfs checkout

Then open a new shell and use the following commands to build the project:

# Configure
cmake --preset macos-arm64-clang

# Build "Debug" configuration
cmake --build --preset macos-arm64-clang-debug

# Build "Release" configuration
cmake --build --preset macos-arm64-clang-release

The build artifacts are placed in build\macos-arm64-clang\Debug or build\macos-arm64-clang\Release.

To build for the x64 architecture, use the macos-x64-clang preset.

Tested on:

  • macOS 15.5

  • Xcode 16.4 (clang 17.0.0)

  • CMake 4.0.3

  • Ninja 1.13.1

  • pkg-config 2.5.1

Configuration options

SlangPy can be configured using the following CMake options. These options can be specified on the command line when running CMake, for example:

cmake --preset windows-msvc -DSGL_BUILD_DOC=ON -DSGL_BUILD_EXAMPLES=OFF -DSGL_BUILD_TESTS=OFF

The following table lists the available configuration options:

Option

Default

Description

SGL_BUILD_PYTHON

ON

Build sgl Python extension

SGL_BUILD_EXAMPLES

ON

Build sgl examples

SGL_BUILD_TESTS

ON

Build sgl tests

SGL_BUILD_DOC

OFF

Build sgl documentation

SGL_USE_DYNAMIC_CUDA

ON

Load CUDA driver API dynamically

SGL_DISABLE_ASSERTS

OFF

Disable asserts

SGL_ENABLE_PCH

OFF

Enable precompiled headers

SGL_ENABLE_ASAN

OFF

Enable AddressSanitizer (Clang only)

SGL_ENABLE_UBSAN

OFF

Enable UndefinedBehaviorSanitizer (Clang only)

SGL_ENABLE_HEADER_VALIDATION

OFF

Enable header validation

Sanitizer builds

SlangPy supports AddressSanitizer (ASan) and UndefinedBehaviorSanitizer (UBSan) with Clang. Enabling these options instruments the native SlangPy libraries, the Python extension, C++ tests, examples, and the embedded slang-rhi library. Prebuilt dependencies such as Slang, Python, and GPU drivers are not instrumented.

Configure and build a sanitizer-enabled RelWithDebInfo build with the platform’s Clang preset. For example, on Linux use:

cmake --preset linux-clang --fresh -DSGL_ENABLE_ASAN=ON -DSGL_ENABLE_UBSAN=ON
cmake --build --preset linux-clang-relwithdebinfo

On Windows, use Clang 22 or newer and a RelWithDebInfo build. Clang 22 includes the fix for incorrect ASan instrumentation of C++ exception catch parameters on Windows. Configure both sanitizers with:

cmake --preset windows-clang --fresh -DSGL_ENABLE_ASAN=ON -DSGL_ENABLE_UBSAN=ON
cmake --build --preset windows-clang-relwithdebinfo

RelWithDebInfo is required on Windows because LLVM’s UBSan C++ runtime uses the release iterator ABI. It retains symbols while avoiding an ABI mismatch with the Debug C++ runtime. The vptr check is disabled on Windows because Clang ships its support library with a static-CRT ABI that is incompatible with SlangPy’s dynamic-CRT dependencies; SlangPy explicitly links the compatible core runtime and the remaining UBSan checks are enabled.

On Apple Silicon, use macos-arm64-clang and macos-arm64-clang-relwithdebinfo with both sanitizer options.

On Linux, the native extension is loaded by an ordinary Python executable, so test processes must preload the Clang sanitizer runtimes. The helper below prints the required environment assignments for local use and writes them directly to the GitHub Actions environment when GITHUB_ENV is present:

python tools/setup-sanitizer-env.py \
    --os linux \
    --sanitizers address,undefined \
    --binary-dir build/linux-clang/RelWithDebInfo

On Linux, apply the printed environment assignments and run the C++ and Python tests normally. Pass the generated leak reports through tools/filter-lsan-reports.py. The filter fails for leaks originating in SlangPy or slang-rhi and ignores reports whose allocation site is external code such as Python or a GPU driver.

On Windows and macOS, loading the sanitizer runtimes only when the SlangPy extension is imported is too late for CPython’s existing allocations. ASan builds on these platforms therefore also produce slangpy_sanitizer_python (.exe on Windows). This small executable links the runtimes before it initializes stock CPython; CPython itself does not need to be rebuilt. Use it in place of the ordinary Python executable when running tests. On Windows, the environment helper also copies the matching runtime DLL beside the build output and adds its directory to PATH. For example:

build\windows-clang\RelWithDebInfo\slangpy_sanitizer_python.exe tools\ci.py unit-test-python --disable-torch

On macOS, use the corresponding host without the .exe suffix:

build/macos-arm64-clang/RelWithDebInfo/slangpy_sanitizer_python \
    tools/ci.py unit-test-python --disable-torch

Only instrumented SlangPy and slang-rhi code receives complete ASan coverage; prebuilt Python and GPU driver code does not. Sanitizer tests make installed torch and slangpy_torch packages unavailable because they load separately built native libraries into the instrumented process. Tests that require them are skipped in the same way as in an environment where the packages are not installed.

Updating the API Reference

SlangPy uses a vendored copy of pybind11_mkdoc to extract documentation strings from the C++ source code. These comments are then used by nanobind to generate Python documentation comments, which are in turn used when building the API Reference document.

The generator lives in external/pybind11_mkdoc; its README.slangpy.md records the upstream revision and local changes. It is invoked through tools/generate_pydoc.py and does not use an installed pybind11_mkdoc package. Install LLVM/libclang and matching Python Clang bindings (version 20.1.5 or later). If libclang cannot be located, set LIBCLANG_PATH to the library file.

To regenerate docstrings, specify the slangpy_pydoc target when invoking cmake:

# Install Python build prerequisites
pip install -r requirements-dev.txt

# Install Python documentation build prerequisites
pip install -r docs/requirements.txt

# Install Python bindings matching the installed LLVM/libclang version
pip install "clang>=20.1.5"

# Configure
cmake --preset windows-msvc

# Regenerate the docstring header and rebuild the Python extension
cmake --build --preset windows-msvc-release --target slangpy_pydoc
cmake --build --preset windows-msvc-release

The generated API Reference page can then be updated by invoking the html build of the SlangPy docs. (It’s regenerated as part of running sphinx-build.) The API generator converts Google-style sections such as Args: into reStructuredText using Sphinx’s bundled Napoleon parser. Existing reStructuredText docstrings are supported as well.

Install the Pandoc executable and make it available on PATH for the notebook tutorials, then build the complete HTML documentation:

python -m sphinx -b html docs build/docs/html

Tested on:

  • Windows 10 (build 19045)

  • Visual Studio 2022 (Version 17.13.6)

  • CMake 4.0.2

  • Ninja 1.12.1

VS Code

TBD