From 3d5ead664b57a792a38abb426e7c1e0473107306 Mon Sep 17 00:00:00 2001 From: Morteza Mostajab Date: Mon, 21 Aug 2023 14:53:41 +0100 Subject: [PATCH 1/4] Adds MATERIALX_BUILD_IOS option that enables MaterialX Library compile on iOS platform. --- .github/workflows/main.yml | 9 +++++ CMakeLists.txt | 25 ++++++++---- source/MaterialXRenderHw/CMakeLists.txt | 9 ++++- source/MaterialXRenderHw/SimpleWindowIOS.cpp | 40 +++++++++++++++++++ source/MaterialXRenderHw/SimpleWindowMac.cpp | 4 ++ .../MaterialXRenderHw/WindowCocoaWrappers.m | 4 ++ source/MaterialXRenderHw/WindowWrapper.cpp | 4 ++ source/MaterialXRenderMsl/CMakeLists.txt | 9 ++++- 8 files changed, 94 insertions(+), 10 deletions(-) create mode 100644 source/MaterialXRenderHw/SimpleWindowIOS.cpp diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 8787240cd9..b4366ed2a9 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -83,6 +83,15 @@ jobs: python: 3.11 test_shaders: ON + - name: MacOS_iOS_14_Python311 + os: macos-13 + compiler: xcode + compiler_version: "14.3" + python: None + cmake_config: -DMATERIALX_BUILD_IOS=ON -DMATERIALX_BUILD_PYTHON=OFF -DMATERIALX_BUILD_VIEWER=OFF -DMATERIALX_BUILD_GRAPH_EDITOR=OFF + -DCMAKE_OSX_SYSROOT=`xcrun --sdk iphoneos --show-sdk-path` + -DCMAKE_OSX_ARCHITECTURES=arm64 + - name: Windows_VS2019_Win32_Python37 os: windows-2019 architecture: x86 diff --git a/CMakeLists.txt b/CMakeLists.txt index 69e5c8c0f3..aba4fb03d4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,29 +31,38 @@ if (MATERIALX_BUILD_JS) endif() endif() +option(MATERIALX_BUILD_IOS "Build the MaterialX for iOS." OFF) +if (MATERIALX_BUILD_IOS) + set(CMAKE_SYSTEM_NAME iOS) + add_definitions(-DTARGET_OS_IOS=1) +endif() + project(MaterialX) +if(NOT MATERIALX_BUILD_IOS) option(MATERIALX_BUILD_PYTHON "Build the MaterialX Python package from C++ bindings. Requires Python 3.6 or greater." OFF) option(MATERIALX_BUILD_VIEWER "Build the MaterialX Viewer." OFF) option(MATERIALX_BUILD_GRAPH_EDITOR "Build the MaterialX Graph Editor." OFF) option(MATERIALX_BUILD_DOCS "Create HTML documentation using Doxygen. Requires that Doxygen be installed." OFF) - option(MATERIALX_BUILD_GEN_GLSL "Build the GLSL shader generator back-end." ON) option(MATERIALX_BUILD_GEN_OSL "Build the OSL shader generator back-end." ON) option(MATERIALX_BUILD_GEN_MDL "Build the MDL shader generator back-end." ON) -option(MATERIALX_BUILD_GEN_MSL "Build the MSL shader generator back-end." ON) -option(MATERIALX_BUILD_RENDER "Build the MaterialX Render modules." ON) option(MATERIALX_BUILD_OIIO "Build OpenImageIO support for MaterialXRender." OFF) +option(MATERIALX_PYTHON_LTO "Enable link-time optimizations for MaterialX Python." ON) +option(MATERIALX_INSTALL_PYTHON "Install the MaterialX Python package as a third-party library when the install target is built." ON) +option(MATERIALX_DYNAMIC_ANALYSIS "Build MaterialX libraries with dynamic analysis on supporting platforms." OFF) +option(MATERIALX_OSL_LEGACY_CLOSURES "Build OSL shader generation supporting the legacy OSL closures." ON) option(MATERIALX_BUILD_TESTS "Build unit tests." ON) +option(MATERIALX_TEST_RENDER "Run rendering tests for MaterialX Render module. GPU required for graphics validation." ON) +endif() + +option(MATERIALX_BUILD_GEN_MSL "Build the MSL shader generator back-end." ON) +option(MATERIALX_BUILD_RENDER "Build the MaterialX Render modules." ON) option(MATERIALX_BUILD_SHARED_LIBS "Build MaterialX libraries as shared rather than static." OFF) -option(MATERIALX_PYTHON_LTO "Enable link-time optimizations for MaterialX Python." ON) -option(MATERIALX_INSTALL_PYTHON "Install the MaterialX Python package as a third-party library when the install target is built." ON) + option(MATERIALX_INSTALL_RESOURCES "Install the resources folder when building render modules." ON) -option(MATERIALX_TEST_RENDER "Run rendering tests for MaterialX Render module. GPU required for graphics validation." ON) option(MATERIALX_WARNINGS_AS_ERRORS "Interpret all compiler warnings as errors." OFF) -option(MATERIALX_DYNAMIC_ANALYSIS "Build MaterialX libraries with dynamic analysis on supporting platforms." OFF) -option(MATERIALX_OSL_LEGACY_CLOSURES "Build OSL shader generation supporting the legacy OSL closures." ON) set(MATERIALX_PYTHON_VERSION "" CACHE STRING "Python version to be used in building the MaterialX Python package (e.g. '3.9').") diff --git a/source/MaterialXRenderHw/CMakeLists.txt b/source/MaterialXRenderHw/CMakeLists.txt index 07210b12fa..b0383acfd7 100644 --- a/source/MaterialXRenderHw/CMakeLists.txt +++ b/source/MaterialXRenderHw/CMakeLists.txt @@ -4,7 +4,9 @@ file(GLOB materialx_source "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp") file(GLOB materialx_headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h*") if(APPLE) +if (NOT MATERIALX_BUILD_IOS) find_library(COCOA_FRAMEWORK Cocoa) +endif() file(GLOB materialx_source_oc "${CMAKE_CURRENT_SOURCE_DIR}/*.m") message("Objective C files: " ${materialx_source_oc}) set_source_files_properties(${materialx_source_oc} PROPERTIES @@ -26,6 +28,12 @@ add_library(${MATERIALX_MODULE_NAME} ${materialx_source} ${materialx_headers}) add_definitions(-DMATERIALX_RENDERHW_EXPORTS) +if(APPLE AND NOT MATERIALX_BUILD_IOS) +set(CMAKE_DL_LIBS +${CMAKE_DL_LIBS} +"-framework Cocoa") +endif() + # Create version resource if(MATERIALX_BUILD_SHARED_LIBS AND MSVC) configure_file(${CMAKE_SOURCE_DIR}/cmake/modules/MaterialXVersion.rc.in ${CMAKE_CURRENT_BINARY_DIR}/version.rc) @@ -43,7 +51,6 @@ elseif(APPLE) MaterialXRender ${CMAKE_DL_LIBS} "-framework Foundation" - "-framework Cocoa" "-framework Metal") elseif(UNIX) target_link_libraries( diff --git a/source/MaterialXRenderHw/SimpleWindowIOS.cpp b/source/MaterialXRenderHw/SimpleWindowIOS.cpp new file mode 100644 index 0000000000..1d4189b2ab --- /dev/null +++ b/source/MaterialXRenderHw/SimpleWindowIOS.cpp @@ -0,0 +1,40 @@ +// +// Copyright Contributors to the MaterialX Project +// SPDX-License-Identifier: Apache-2.0 +// + +#if defined(__APPLE__) + +#ifdef TARGET_OS_IOS + +#include + +MATERIALX_NAMESPACE_BEGIN + +SimpleWindow::SimpleWindow() : + _width(0), + _height(0) +{ + // Give a unique identifier to this window. + static unsigned int windowCount = 1; + _id = windowCount; + windowCount++; +} + +bool SimpleWindow::initialize(const char* title, + unsigned int width, unsigned int height, + void* /*applicationShell*/) +{ + _windowWrapper = WindowWrapper::create(nullptr); + return true; +} + +SimpleWindow::~SimpleWindow() +{ +} + +MATERIALX_NAMESPACE_END + +#endif + +#endif diff --git a/source/MaterialXRenderHw/SimpleWindowMac.cpp b/source/MaterialXRenderHw/SimpleWindowMac.cpp index 1cf070ac74..a04d1f49ae 100644 --- a/source/MaterialXRenderHw/SimpleWindowMac.cpp +++ b/source/MaterialXRenderHw/SimpleWindowMac.cpp @@ -5,6 +5,8 @@ #if defined(__APPLE__) +#ifndef TARGET_OS_IOS + #include #include @@ -42,3 +44,5 @@ SimpleWindow::~SimpleWindow() MATERIALX_NAMESPACE_END #endif + +#endif diff --git a/source/MaterialXRenderHw/WindowCocoaWrappers.m b/source/MaterialXRenderHw/WindowCocoaWrappers.m index c73de77810..a012966a8f 100644 --- a/source/MaterialXRenderHw/WindowCocoaWrappers.m +++ b/source/MaterialXRenderHw/WindowCocoaWrappers.m @@ -5,6 +5,8 @@ #if defined (__APPLE__) +#ifndef TARGET_OS_IOS + #import #import #import @@ -72,3 +74,5 @@ void NSUtilDisposeWindow(void* pWindow) } #endif + +#endif diff --git a/source/MaterialXRenderHw/WindowWrapper.cpp b/source/MaterialXRenderHw/WindowWrapper.cpp index 315960fb2b..654498d5cd 100644 --- a/source/MaterialXRenderHw/WindowWrapper.cpp +++ b/source/MaterialXRenderHw/WindowWrapper.cpp @@ -84,8 +84,12 @@ WindowWrapper::WindowWrapper(ExternalWindowHandle externalHandle, DisplayHandle display) { _externalHandle = externalHandle; +#ifndef TARGET_OS_IOS // Cache a pointer to the window. _internalHandle = NSUtilGetView(externalHandle); +#else + _internalHandle = nullptr; +#endif } WindowWrapper::~WindowWrapper() diff --git a/source/MaterialXRenderMsl/CMakeLists.txt b/source/MaterialXRenderMsl/CMakeLists.txt index 173a9e9bae..6e7a4f5172 100644 --- a/source/MaterialXRenderMsl/CMakeLists.txt +++ b/source/MaterialXRenderMsl/CMakeLists.txt @@ -11,8 +11,10 @@ if(POLICY CMP0072) endif() if(APPLE) +if(NOT MATERIALX_BUILD_IOS) find_library(COCOA_FRAMEWORK Cocoa) find_package(OpenGL REQUIRED) +endif() file(GLOB_RECURSE materialx_source_oc "${CMAKE_CURRENT_SOURCE_DIR}/*.m") message("Objective C files: " ${materialx_source_oc}) set_source_files_properties(${materialx_source_oc} PROPERTIES @@ -44,6 +46,12 @@ set(COMMON_LIBRARIES MaterialXRenderHw MaterialXGenMsl ${CMAKE_DL_LIBS}) + +if(APPLE AND NOT MATERIALX_BUILD_IOS) +set(COMMON_LIBRARIES + ${COMMON_LIBRARIES} + "-framework Cocoa") +endif() if(MSVC) target_link_libraries( @@ -56,7 +64,6 @@ elseif(APPLE) ${COMMON_LIBRARIES} ${OPENGL_LIBRARIES} "-framework Foundation" - "-framework Cocoa" "-framework Metal") elseif(UNIX) target_link_libraries( From a27b2d2e69ce9a70ec221cfea33637589017caa0 Mon Sep 17 00:00:00 2001 From: Morteza Mostajab Date: Tue, 22 Aug 2023 16:44:30 +0100 Subject: [PATCH 2/4] force disable corresponding cmake properties when MATERIALX_BUILD_IOS is enabled. --- CMakeLists.txt | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index aba4fb03d4..7961f050b3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,38 +31,50 @@ if (MATERIALX_BUILD_JS) endif() endif() -option(MATERIALX_BUILD_IOS "Build the MaterialX for iOS." OFF) -if (MATERIALX_BUILD_IOS) - set(CMAKE_SYSTEM_NAME iOS) - add_definitions(-DTARGET_OS_IOS=1) -endif() - project(MaterialX) -if(NOT MATERIALX_BUILD_IOS) option(MATERIALX_BUILD_PYTHON "Build the MaterialX Python package from C++ bindings. Requires Python 3.6 or greater." OFF) option(MATERIALX_BUILD_VIEWER "Build the MaterialX Viewer." OFF) option(MATERIALX_BUILD_GRAPH_EDITOR "Build the MaterialX Graph Editor." OFF) option(MATERIALX_BUILD_DOCS "Create HTML documentation using Doxygen. Requires that Doxygen be installed." OFF) + option(MATERIALX_BUILD_GEN_GLSL "Build the GLSL shader generator back-end." ON) option(MATERIALX_BUILD_GEN_OSL "Build the OSL shader generator back-end." ON) option(MATERIALX_BUILD_GEN_MDL "Build the MDL shader generator back-end." ON) -option(MATERIALX_BUILD_OIIO "Build OpenImageIO support for MaterialXRender." OFF) -option(MATERIALX_PYTHON_LTO "Enable link-time optimizations for MaterialX Python." ON) -option(MATERIALX_INSTALL_PYTHON "Install the MaterialX Python package as a third-party library when the install target is built." ON) -option(MATERIALX_DYNAMIC_ANALYSIS "Build MaterialX libraries with dynamic analysis on supporting platforms." OFF) -option(MATERIALX_OSL_LEGACY_CLOSURES "Build OSL shader generation supporting the legacy OSL closures." ON) -option(MATERIALX_BUILD_TESTS "Build unit tests." ON) -option(MATERIALX_TEST_RENDER "Run rendering tests for MaterialX Render module. GPU required for graphics validation." ON) -endif() - option(MATERIALX_BUILD_GEN_MSL "Build the MSL shader generator back-end." ON) option(MATERIALX_BUILD_RENDER "Build the MaterialX Render modules." ON) +option(MATERIALX_BUILD_OIIO "Build OpenImageIO support for MaterialXRender." OFF) +option(MATERIALX_BUILD_TESTS "Build unit tests." ON) option(MATERIALX_BUILD_SHARED_LIBS "Build MaterialX libraries as shared rather than static." OFF) - +option(MATERIALX_PYTHON_LTO "Enable link-time optimizations for MaterialX Python." ON) +option(MATERIALX_INSTALL_PYTHON "Install the MaterialX Python package as a third-party library when the install target is built." ON) option(MATERIALX_INSTALL_RESOURCES "Install the resources folder when building render modules." ON) +option(MATERIALX_TEST_RENDER "Run rendering tests for MaterialX Render module. GPU required for graphics validation." ON) option(MATERIALX_WARNINGS_AS_ERRORS "Interpret all compiler warnings as errors." OFF) +option(MATERIALX_DYNAMIC_ANALYSIS "Build MaterialX libraries with dynamic analysis on supporting platforms." OFF) +option(MATERIALX_OSL_LEGACY_CLOSURES "Build OSL shader generation supporting the legacy OSL closures." ON) + +option(MATERIALX_BUILD_IOS "Build the MaterialX for iOS." OFF) +if (MATERIALX_BUILD_IOS) + set(CMAKE_SYSTEM_NAME iOS) + add_definitions(-DTARGET_OS_IOS=1) + set(MATERIALX_BUILD_PYTHON OFF) + set(MATERIALX_BUILD_VIEWER OFF) + set(MATERIALX_BUILD_GRAPH_EDITOR OFF) + set(MATERIALX_BUILD_GEN_GLSL OFF) + set(MATERIALX_BUILD_GEN_OSL OFF) + set(MATERIALX_BUILD_GEN_GLSL OFF) + set(MATERIALX_BUILD_GEN_MDL OFF) + set(MATERIALX_BUILD_OIIO OFF) + set(MATERIALX_PYTHON_LTO OFF) + set(MATERIALX_INSTALL_PYTHON OFF) + set(MATERIALX_DYNAMIC_ANALYSIS OFF) + set(MATERIALX_DYNAMIC_ANALYSIS OFF) + set(MATERIALX_OSL_LEGACY_CLOSURES OFF) + set(MATERIALX_BUILD_TESTS OFF) + set(MATERIALX_TEST_RENDER OFF) +endif() set(MATERIALX_PYTHON_VERSION "" CACHE STRING "Python version to be used in building the MaterialX Python package (e.g. '3.9').") From 9b68c519a9f94018f178b2a920906d0213b7af75 Mon Sep 17 00:00:00 2001 From: Morteza Mostajab Date: Tue, 22 Aug 2023 17:42:06 +0100 Subject: [PATCH 3/4] Consistent name for iOS build --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b4366ed2a9..53111d286d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -83,7 +83,7 @@ jobs: python: 3.11 test_shaders: ON - - name: MacOS_iOS_14_Python311 + - name: iOS_Xcode_14_Python311 os: macos-13 compiler: xcode compiler_version: "14.3" From cb2f7402ef5467d89e080c95d67f969e5f59113e Mon Sep 17 00:00:00 2001 From: Jonathan Stone Date: Tue, 22 Aug 2023 10:25:32 -0700 Subject: [PATCH 4/4] Mark new option as advanced Signed-off-by: Jonathan Stone --- CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7961f050b3..11829bd59d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,7 +55,7 @@ option(MATERIALX_WARNINGS_AS_ERRORS "Interpret all compiler warnings as errors." option(MATERIALX_DYNAMIC_ANALYSIS "Build MaterialX libraries with dynamic analysis on supporting platforms." OFF) option(MATERIALX_OSL_LEGACY_CLOSURES "Build OSL shader generation supporting the legacy OSL closures." ON) -option(MATERIALX_BUILD_IOS "Build the MaterialX for iOS." OFF) +option(MATERIALX_BUILD_IOS "Build MaterialX for iOS." OFF) if (MATERIALX_BUILD_IOS) set(CMAKE_SYSTEM_NAME iOS) add_definitions(-DTARGET_OS_IOS=1) @@ -155,6 +155,7 @@ mark_as_advanced(MATERIALX_INSTALL_LIB_PATH) mark_as_advanced(MATERIALX_INSTALL_STDLIB_PATH) mark_as_advanced(MATERIALX_BUILD_JS) mark_as_advanced(MATERIALX_EMSDK_PATH) +mark_as_advanced(MATERIALX_BUILD_IOS) if (MATERIALX_BUILD_GEN_MDL) mark_as_advanced(MATERIALX_MDLC_EXECUTABLE) mark_as_advanced(MATERIALX_MDL_RENDER_EXECUTABLE)