Skip to content

Commit

Permalink
Adds python bindings for Metal Shading Language generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Morteeza committed Mar 8, 2023
1 parent 6f41c26 commit ac037ce
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 4 deletions.
15 changes: 11 additions & 4 deletions python/Scripts/generateshader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions source/PyMaterialX/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
28 changes: 28 additions & 0 deletions source/PyMaterialX/PyMaterialXGenMsl/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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")
19 changes: 19 additions & 0 deletions source/PyMaterialX/PyMaterialXGenMsl/PyModule.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Copyright Contributors to the MaterialX Project
// SPDX-License-Identifier: Apache-2.0
//

#include <PyMaterialX/PyMaterialX.h>

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);
}
37 changes: 37 additions & 0 deletions source/PyMaterialX/PyMaterialXGenMsl/PyMslShaderGenerator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// Copyright Contributors to the MaterialX Project
// SPDX-License-Identifier: Apache-2.0
//

#include <PyMaterialX/PyMaterialX.h>

#include <MaterialXGenMsl/MslShaderGenerator.h>
#include <MaterialXGenMsl/MslResourceBindingContext.h>
#include <MaterialXGenShader/Shader.h>
#include <MaterialXGenShader/ShaderGenerator.h>

#include <string>

namespace py = pybind11;
namespace mx = MaterialX;

// MSL shader generator bindings

void bindPyMslShaderGenerator(py::module& mod)
{
py::class_<mx::MslShaderGenerator, mx::HwShaderGenerator, mx::MslShaderGeneratorPtr>(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_<mx::MslResourceBindingContext, mx::HwResourceBindingContext, mx::MslResourceBindingContextPtr>(mod, "MslResourceBindingContext")
.def_static("create", &mx::MslResourceBindingContext::create)
.def(py::init<size_t, size_t>())
.def("emitDirectives", &mx::MslResourceBindingContext::emitDirectives)
.def("emitResourceBindings", &mx::MslResourceBindingContext::emitResourceBindings);
}

0 comments on commit ac037ce

Please sign in to comment.