Skip to content

Commit

Permalink
Add support for MaterialX builds on iOS (#1435)
Browse files Browse the repository at this point in the history
Thanks to @ikuznetcova for the contribution
  • Loading branch information
Morteeza committed Aug 22, 2023
1 parent 6918d5d commit c5b376c
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 2 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ jobs:
python: 3.11
test_shaders: ON

- name: iOS_Xcode_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
Expand Down
22 changes: 22 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,27 @@ 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 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').")
set(MATERIALX_PYTHON_EXECUTABLE "" CACHE FILEPATH
Expand Down Expand Up @@ -134,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)
Expand Down
9 changes: 8 additions & 1 deletion source/MaterialXRenderHw/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -43,7 +51,6 @@ elseif(APPLE)
MaterialXRender
${CMAKE_DL_LIBS}
"-framework Foundation"
"-framework Cocoa"
"-framework Metal")
elseif(UNIX)
target_link_libraries(
Expand Down
40 changes: 40 additions & 0 deletions source/MaterialXRenderHw/SimpleWindowIOS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// Copyright Contributors to the MaterialX Project
// SPDX-License-Identifier: Apache-2.0
//

#if defined(__APPLE__)

#ifdef TARGET_OS_IOS

#include <MaterialXRenderHw/SimpleWindow.h>

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
4 changes: 4 additions & 0 deletions source/MaterialXRenderHw/SimpleWindowMac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#if defined(__APPLE__)

#ifndef TARGET_OS_IOS

#include <MaterialXRenderHw/SimpleWindow.h>
#include <MaterialXRenderHw/WindowCocoaWrappers.h>

Expand Down Expand Up @@ -42,3 +44,5 @@ SimpleWindow::~SimpleWindow()
MATERIALX_NAMESPACE_END

#endif

#endif
4 changes: 4 additions & 0 deletions source/MaterialXRenderHw/WindowCocoaWrappers.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#if defined (__APPLE__)

#ifndef TARGET_OS_IOS

#import <Cocoa/Cocoa.h>
#import <AppKit/NSApplication.h>
#import <MaterialXRenderHw/WindowCocoaWrappers.h>
Expand Down Expand Up @@ -72,3 +74,5 @@ void NSUtilDisposeWindow(void* pWindow)
}

#endif

#endif
4 changes: 4 additions & 0 deletions source/MaterialXRenderHw/WindowWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 8 additions & 1 deletion source/MaterialXRenderMsl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -56,7 +64,6 @@ elseif(APPLE)
${COMMON_LIBRARIES}
${OPENGL_LIBRARIES}
"-framework Foundation"
"-framework Cocoa"
"-framework Metal")
elseif(UNIX)
target_link_libraries(
Expand Down

0 comments on commit c5b376c

Please sign in to comment.