Code style
C++
- Classes: PascalCase | Functions/variables: snake_case | Members:
m_ prefix
- Use nanobind patterns for Python bindings
- C++ tests use doctest framework
Python
- Classes: PascalCase | Functions/variables: snake_case | Public Members: no prefix | Private Members:
_ prefix
- All arguments must have type annotations
- Black formatter, line-length 100
- Use
typing_extensions for backward-compatible type hints (Python >= 3.9)
Slang (.slang files)
[shader("compute")] attribute marks GPU entry points
StructuredBuffer<T> / RWStructuredBuffer<T> for typed GPU arrays
uint3 tid : SV_DispatchThreadID for thread indexing
- Generics via
<T>, interfaces via interface IFoo, conformance via struct Foo : IFoo
- Differentiable functions:
[Differentiable] float foo(float x) with bwd_diff(foo) for backprop
Development workflow
Adding new Python APIs
- Implement in
slangpy/ (core/, bindings/, builtin/, or appropriate subpackage)
- Add type annotations to all function arguments
- Write tests in
slangpy/tests/
- Build and run tests
- Run
pre-commit run --all-files
Adding a new type to the functional API
- Create a Marshall in
slangpy/bindings/ or slangpy/builtin/ -- subclass Marshall and implement resolve_types(), resolve_dimensionality(), gen_calldata(). See existing marshalls (e.g., TensorMarshall) for the pattern.
- Register in
slangpy/bindings/typeregistry.py -- add entry to PYTHON_TYPES dict.
- (Optional) Native signature -- for performance, add a type signature handler in
NativeCallDataCache constructor (src/slangpy_ext/utils/slangpyfunction.cpp).
Modifying the C++ binding layer
- Edits in
src/slangpy_ext/ affect the nanobind interface
- Changes in
src/sgl/ affect the core GPU abstraction
- Always rebuild after C++ changes:
cmake --build --preset linux-gcc-debug
Modifying kernel generation
slangpy/core/calldata.py -- the Phase 2 pipeline (type resolution, vectorization, code generation, compilation)
slangpy/bindings/codegen.py -- Slang compute kernel source generation
- Set
SLANGPY_PRINT_GENERATED_SHADERS=1 to inspect generated kernels
Workflow
- Create a branch:
git checkout -b feature/description
- Make changes, keeping them minimal and focused
- Write/update tests -- new APIs require tests in
slangpy/tests/
- Build:
cmake --build --preset linux-gcc-debug or pip install -e .
- Test:
pytest slangpy/tests -v
- Format:
pre-commit run --all-files (re-run if it modifies files)
- Commit with descriptive message (do not mention Claude)
- Push and create PR against
shader-slang/slangpy:main
Autonomy: Proceed through format, test, and commit without asking for confirmation. Only stop and notify the user if tests fail and the failure is not self-fixable.
Documentation style
C++ (Doxygen)
/// Description.
void do_something();
/// @param v Float values.
/// @return Result.
uint32_t my_func(float2 v);
Python (Sphinx)
def myfunc(x: int, y: int) -> int:
"""
Description.
:param x: Some parameter.
:param y: Some parameter.
:return: Some return value.
"""
Error handling patterns
- Python-layer errors: standard exceptions (
ValueError, TypeError, SlangPyError)
- C++ errors: translated to Python via nanobind
- Shader compile errors: exceptions with Slang diagnostic text
- GPU errors (device lost, OOM): propagate from RHI layer
From project
AGENTS.md -- architecture (3 layers, 3 phases), key files, key classes, adding new types, type resolution, code style
CLAUDE.md -- references AGENTS.md, key rules (type annotations, tests, pre-commit)
CONTRIBUTING.md -- PR process, branch workflow, testing requirements
1---2name: slangpy-code-writer3description: Implement changes in SlangPy. Edit code, write tests, format, commit.4license: MIT5---67## Code style89### C++1011- **Classes**: PascalCase | **Functions/variables**: snake_case | **Members**: `m_` prefix12- Use nanobind patterns for Python bindings13- C++ tests use doctest framework1415### Python1617- **Classes**: PascalCase | **Functions/variables**: snake_case | **Public Members**: no prefix | **Private Members**: `_` prefix18- **All arguments must have type annotations**19- Black formatter, line-length 10020- Use `typing_extensions` for backward-compatible type hints (Python >= 3.9)2122### Slang (.slang files)2324- `[shader("compute")]` attribute marks GPU entry points25- `StructuredBuffer<T>` / `RWStructuredBuffer<T>` for typed GPU arrays26- `uint3 tid : SV_DispatchThreadID` for thread indexing27- Generics via `<T>`, interfaces via `interface IFoo`, conformance via `struct Foo : IFoo`28- Differentiable functions: `[Differentiable] float foo(float x)` with `bwd_diff(foo)` for backprop2930## Development workflow3132### Adding new Python APIs33341. Implement in `slangpy/` (core/, bindings/, builtin/, or appropriate subpackage)352. Add type annotations to all function arguments363. Write tests in `slangpy/tests/`374. Build and run tests385. Run `pre-commit run --all-files`3940### Adding a new type to the functional API41421. **Create a Marshall** in `slangpy/bindings/` or `slangpy/builtin/` -- subclass `Marshall` and implement `resolve_types()`, `resolve_dimensionality()`, `gen_calldata()`. See existing marshalls (e.g., `TensorMarshall`) for the pattern.432. **Register** in `slangpy/bindings/typeregistry.py` -- add entry to `PYTHON_TYPES` dict.443. **(Optional) Native signature** -- for performance, add a type signature handler in `NativeCallDataCache` constructor (`src/slangpy_ext/utils/slangpyfunction.cpp`).4546### Modifying the C++ binding layer4748- Edits in `src/slangpy_ext/` affect the nanobind interface49- Changes in `src/sgl/` affect the core GPU abstraction50- Always rebuild after C++ changes: `cmake --build --preset linux-gcc-debug`5152### Modifying kernel generation5354- `slangpy/core/calldata.py` -- the Phase 2 pipeline (type resolution, vectorization, code generation, compilation)55- `slangpy/bindings/codegen.py` -- Slang compute kernel source generation56- Set `SLANGPY_PRINT_GENERATED_SHADERS=1` to inspect generated kernels5758## Workflow59601. Create a branch: `git checkout -b feature/description`612. Make changes, keeping them minimal and focused623. Write/update tests -- new APIs require tests in `slangpy/tests/`634. Build: `cmake --build --preset linux-gcc-debug` or `pip install -e .`645. Test: `pytest slangpy/tests -v`656. Format: `pre-commit run --all-files` (re-run if it modifies files)667. Commit with descriptive message (do not mention Claude)678. Push and create PR against `shader-slang/slangpy:main`6869**Autonomy:** Proceed through format, test, and commit without asking for confirmation. Only stop and notify the user if tests fail and the failure is not self-fixable.7071## Documentation style7273### C++ (Doxygen)7475```cpp76/// Description.77void do_something();7879/// @param v Float values.80/// @return Result.81uint32_t my_func(float2 v);82```8384### Python (Sphinx)8586```python87def myfunc(x: int, y: int) -> int:88 """89 Description.9091 :param x: Some parameter.92 :param y: Some parameter.93 :return: Some return value.94 """95```9697## Error handling patterns9899- Python-layer errors: standard exceptions (`ValueError`, `TypeError`, `SlangPyError`)100- C++ errors: translated to Python via nanobind101- Shader compile errors: exceptions with Slang diagnostic text102- GPU errors (device lost, OOM): propagate from RHI layer103104## From project105106- `AGENTS.md` -- architecture (3 layers, 3 phases), key files, key classes, adding new types, type resolution, code style107- `CLAUDE.md` -- references AGENTS.md, key rules (type annotations, tests, pre-commit)108- `CONTRIBUTING.md` -- PR process, branch workflow, testing requirements