Skip to content

Implement PyMaterialXRenderSlang Python bindings - #59

Open
ppenenko wants to merge 2 commits into
metashade/devfrom
metashade/slang-python-bindings
Open

Implement PyMaterialXRenderSlang Python bindings#59
ppenenko wants to merge 2 commits into
metashade/devfrom
metashade/slang-python-bindings

Conversation

@ppenenko

Copy link
Copy Markdown
Member

Summary

Addresses Phase 2 of #57.

This PR implements the PyMaterialXRenderSlang Python C++ extension module, exposing the native Slang hardware renderer (MaterialXRenderSlang) to Python:

  1. source/PyMaterialX/PyMaterialXRenderSlang/ Module:
    • PyModule.cpp: Defines PYBIND11_MODULE(PyMaterialXRenderSlang, mod).
    • PySlangRenderer.cpp: Exposes SlangRenderer (create, initialize, createProgram, validateInputs, render, renderTextureSpace, captureImage, getProgram).
    • PySlangTextureHandler.cpp: Exposes SlangTextureHandler (create, bindImage, unbindImage, createRenderResources, releaseRenderResources).
    • PyTextureBaker.cpp: Exposes TextureBakerSlang.
    • CMakeLists.txt: Configures module build and installs dependent runtime DLLs.
  2. C++ Fixes for Slang Renderer Module Dependencies:
    • Added MaterialXRenderHw to MaterialXRenderSlang modules to resolve SimpleWindow symbol dependencies.
    • Included SlangFramebuffer.h in TextureBaker.h for complete type definition when instantiated.
    • Reordered add_subdirectory(source/MaterialXRenderHw) in CMakeLists.txt so it is compiled whenever Slang rendering is active.
  3. Windows DLL Search Path Support:
    • Updated python/MaterialX/__init__.py to ensure os.add_dll_directory is called for both the package directory and the installation bin/ directory on Windows, allowing PyMaterialXRenderSlang to find slang.dll automatically.

Verification

  • Built target PyMaterialXRenderSlang and installed into environment via cmake --install build --config Debug.
  • Verified in Python:
    import MaterialX as mx
    import MaterialX.PyMaterialXRenderSlang as mx_render_slang
    renderer = mx_render_slang.SlangRenderer.create(512, 512)
    renderer.initialize()
    Result:
    Layer: (Info): IRHI::createDevice: Debug layer is enabled.
    Successfully created SlangRenderer: <MaterialX.PyMaterialXRenderSlang.SlangRenderer object>
    Successfully initialized SlangRenderer with slang-rhi on GPU!
    

@ppenenko
ppenenko force-pushed the metashade/slang-python-bindings branch from 6715f38 to 97264e6 Compare August 22, 2026 16:57
@ppenenko
ppenenko requested a balanced review from Copilot August 22, 2026 16:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds Python bindings for the Slang renderer and supporting build/runtime integration.

Changes:

  • Adds renderer, texture-handler, and texture-baker bindings.
  • Integrates the module into CMake and documentation targets.
  • Updates renderer dependencies and Windows DLL search paths.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
CMakeLists.txt Enables hardware-renderer support for Slang builds.
python/MaterialX/__init__.py Configures Windows DLL search paths.
source/MaterialXRenderSlang/CMakeLists.txt Adds the hardware-rendering dependency.
source/MaterialXRenderSlang/TextureBaker.h Includes the complete framebuffer type.
source/PyMaterialX/CMakeLists.txt Registers the new Python module.
source/PyMaterialX/PyMaterialXRenderSlang/CMakeLists.txt Builds and installs the extension.
source/PyMaterialX/PyMaterialXRenderSlang/PyModule.cpp Defines module initialization.
source/PyMaterialX/PyMaterialXRenderSlang/PySlangRenderer.cpp Binds SlangRenderer.
source/PyMaterialX/PyMaterialXRenderSlang/PySlangTextureHandler.cpp Binds SlangTextureHandler.
source/PyMaterialX/PyMaterialXRenderSlang/PyTextureBaker.cpp Binds the Slang texture baker.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

.def("render", &mx::SlangRenderer::render)
.def("renderTextureSpace", &mx::SlangRenderer::renderTextureSpace)
.def("captureImage", &mx::SlangRenderer::captureImage)
.def("getProgram", &mx::SlangRenderer::getProgram);
void bindPySlangTextureHandler(py::module& mod)
{
py::class_<mx::SlangTextureHandler, mx::ImageHandler, mx::SlangTextureHandlerPtr>(mod, "SlangTextureHandler")
.def_static("create", &mx::SlangTextureHandler::create)
Comment thread python/MaterialX/__init__.py Outdated
Comment on lines +12 to +13
os.add_dll_directory(mxdir)
except (AttributeError, OSError):
Comment on lines +31 to +33
if(WIN32)
# Install slang-rhi runtime DLL dependencies to python package directory
install(FILES
- Add PySlangProgram binding and register in PyModule
- Remove unusable SlangTextureHandler::create static method
- Retain DLL directory handles in module list to prevent GC cleanup
- Add multi-platform runtime installation for macOS and Linux
- Add default arguments for createImageHandler, captureImage, renderTextureSpace
- Fix constant buffer reflection in SlangProgram to account for trailing u_lightData

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 3 comments.

Suppressed comments (1)

source/PyMaterialX/PyMaterialXRenderSlang/CMakeLists.txt:42

  • The pinned slang-rhi project only copies these compiler libraries into SLANG_RHI_BINARY_DIR on Windows; on Linux and macOS the imported slang target points to the downloaded library in its package directory. Consequently both non-Windows paths here are absent, OPTIONAL silently skips them, and the installed extension cannot load its Slang compiler dependency. Install $<TARGET_FILE:slang> for both non-Windows branches instead of guessing output filenames.
        ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/libslang.dylib

.def("hasUniform", &mx::SlangProgram::hasUniform)
.def("bindUniform", &mx::SlangProgram::bindUniform,
py::arg("name"), py::arg("value"), py::arg("errorIfMissing") = true)
.def("bindMesh", static_cast<void (mx::SlangProgram::*)(mx::MeshPtr)>(&mx::SlangProgram::bindMesh))
target_link_libraries(
PyMaterialXRenderSlang
PUBLIC PyMaterialXRender
MaterialXRenderSlang

void bindPySlangProgram(py::module& mod)
{
py::class_<mx::SlangProgram, mx::SlangProgramPtr>(mod, "SlangProgram")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants