diff --git a/python/Scripts/generateshader.py b/python/Scripts/generateshader.py index e56b27b4a6..86a67d96a8 100644 --- a/python/Scripts/generateshader.py +++ b/python/Scripts/generateshader.py @@ -10,13 +10,18 @@ import MaterialX.PyMaterialXGenGlsl as mx_gen_glsl import MaterialX.PyMaterialXGenOsl as mx_gen_osl import MaterialX.PyMaterialXGenMdl as mx_gen_mdl +import MaterialX.PyMaterialXGenMsl as mx_gen_msl def validateCode(sourceCodeFile, codevalidator, codevalidatorArgs): if codevalidator: - cmd = codevalidator + ' ' + sourceCodeFile + cmd = codevalidator.split() + cmd.append(sourceCodeFile) if codevalidatorArgs: - cmd += ' ' + codevalidatorArgs - print('----- Run Validator: '+ cmd) + cmd.append(codevalidatorArgs) + cmd_flatten ='----- Run Validator: ' + for c in cmd: + cmd_flatten += c + ' ' + print(cmd_flatten) try: output = subprocess.check_output(cmd, stderr=subprocess.STDOUT) result = output.decode(encoding='utf-8') @@ -96,6 +101,8 @@ def main(): shadergen = mx_gen_glsl.EsslShaderGenerator.create() elif gentarget == 'vulkan': shadergen = mx_gen_glsl.VkShaderGenerator.create() + elif gentarget == 'msl': + shadergen = mx_gen_msl.MslShaderGenerator.create() else: shadergen = mx_gen_glsl.GlslShaderGenerator.create() @@ -153,7 +160,7 @@ def main(): if shader: # Use extension of .vert and .frag as it's type is # recognized by glslangValidator - if gentarget in ['glsl', 'essl', 'vulkan']: + if gentarget in ['glsl', 'essl', 'vulkan', 'msl']: pixelSource = shader.getSourceCode(mx_gen_shader.PIXEL_STAGE) filename = pathPrefix + "/" + shader.getName() + "." + gentarget + ".frag" print('--- Wrote pixel shader to: ' + filename) diff --git a/source/PyMaterialX/CMakeLists.txt b/source/PyMaterialX/CMakeLists.txt index c98da971ce..58f254c88c 100644 --- a/source/PyMaterialX/CMakeLists.txt +++ b/source/PyMaterialX/CMakeLists.txt @@ -49,6 +49,9 @@ if (MATERIALX_BUILD_GEN_GLSL OR MATERIALX_BUILD_GEN_OSL OR MATERIALX_BUILD_GEN_M if (MATERIALX_BUILD_GEN_GLSL) add_subdirectory(PyMaterialXGenGlsl) endif() + if (MATERIALX_BUILD_GEN_MSL) + add_subdirectory(PyMaterialXGenMsl) + endif() if (MATERIALX_BUILD_GEN_OSL) add_subdirectory(PyMaterialXGenOsl) endif() diff --git a/source/PyMaterialX/PyMaterialXGenMsl/CMakeLists.txt b/source/PyMaterialX/PyMaterialXGenMsl/CMakeLists.txt new file mode 100644 index 0000000000..88e21687bc --- /dev/null +++ b/source/PyMaterialX/PyMaterialXGenMsl/CMakeLists.txt @@ -0,0 +1,28 @@ +file(GLOB pymaterialxgenmsl_source "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp") +file(GLOB pymaterialxgenmsl_headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h") + +pybind11_add_module(PyMaterialXGenMsl SHARED ${PYBIND11_MODULE_FLAGS} ${pymaterialxgenmsl_source} ${pymaterialxgenmsl_headers}) + +if(APPLE) + set_target_properties(PyMaterialXGenMsl PROPERTIES CXX_VISIBILITY_PRESET "default") +endif() + +set_target_properties( + PyMaterialXGenMsl + PROPERTIES + OUTPUT_NAME PyMaterialXGenMsl + COMPILE_FLAGS "${EXTERNAL_COMPILE_FLAGS}" + LINK_FLAGS "${EXTERNAL_LINK_FLAGS}" + INSTALL_RPATH "${MATERIALX_UP_TWO_RPATH}" + VERSION "${MATERIALX_LIBRARY_VERSION}" + SOVERSION "${MATERIALX_MAJOR_VERSION}" + DEBUG_POSTFIX "${MATERIALX_PYTHON_DEBUG_POSTFIX}") + +target_link_libraries( + PyMaterialXGenMsl + PUBLIC PyMaterialXGenShader + MaterialXGenMsl + PRIVATE ${CMAKE_DL_LIBS}) + +install(TARGETS PyMaterialXGenMsl + DESTINATION "python/MaterialX") diff --git a/source/PyMaterialX/PyMaterialXGenMsl/PyModule.cpp b/source/PyMaterialX/PyMaterialXGenMsl/PyModule.cpp new file mode 100644 index 0000000000..6eda44871b --- /dev/null +++ b/source/PyMaterialX/PyMaterialXGenMsl/PyModule.cpp @@ -0,0 +1,19 @@ +// +// Copyright Contributors to the MaterialX Project +// SPDX-License-Identifier: Apache-2.0 +// + +#include + +namespace py = pybind11; + +void bindPyMslShaderGenerator(py::module& mod); +void bindPyMslResourceBindingContext(py::module &mod); + +PYBIND11_MODULE(PyMaterialXGenMsl, mod) +{ + mod.doc() = "Module containing Python bindings for the MaterialXGenMsl library"; + + bindPyMslShaderGenerator(mod); + bindPyMslResourceBindingContext(mod); +} diff --git a/source/PyMaterialX/PyMaterialXGenMsl/PyMslShaderGenerator.cpp b/source/PyMaterialX/PyMaterialXGenMsl/PyMslShaderGenerator.cpp new file mode 100644 index 0000000000..870b9bc02c --- /dev/null +++ b/source/PyMaterialX/PyMaterialXGenMsl/PyMslShaderGenerator.cpp @@ -0,0 +1,37 @@ +// +// Copyright Contributors to the MaterialX Project +// SPDX-License-Identifier: Apache-2.0 +// + +#include + +#include +#include +#include +#include + +#include + +namespace py = pybind11; +namespace mx = MaterialX; + +// MSL shader generator bindings + +void bindPyMslShaderGenerator(py::module& mod) +{ + py::class_(mod, "MslShaderGenerator") + .def_static("create", &mx::MslShaderGenerator::create) + .def(py::init<>()) + .def("generate", &mx::MslShaderGenerator::generate) + .def("getTarget", &mx::MslShaderGenerator::getTarget) + .def("getVersion", &mx::MslShaderGenerator::getVersion); +} + +void bindPyMslResourceBindingContext(py::module &mod) +{ + py::class_(mod, "MslResourceBindingContext") + .def_static("create", &mx::MslResourceBindingContext::create) + .def(py::init()) + .def("emitDirectives", &mx::MslResourceBindingContext::emitDirectives) + .def("emitResourceBindings", &mx::MslResourceBindingContext::emitResourceBindings); +}