Skip to content

Commit

Permalink
Add iOS support to Filament (google#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
bejado authored Oct 18, 2018
1 parent 356c3a2 commit 29119c7
Show file tree
Hide file tree
Showing 48 changed files with 2,995 additions and 38 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ if (WEBGL)
add_subdirectory(${EXTERNAL}/stb/tnt)
endif()

if (NOT ANDROID AND NOT WEBGL)
if (NOT ANDROID AND NOT WEBGL AND NOT IOS)
add_subdirectory(${FILAMENT}/samples)

add_subdirectory(${LIBRARIES}/bluegl)
Expand Down Expand Up @@ -336,7 +336,7 @@ if (NOT ANDROID AND NOT WEBGL)
add_subdirectory(${TOOLS}/specular-color)
endif()

# Generate exported executables for cross-compiled builds (Android and WebGL)
# Generate exported executables for cross-compiled builds (Android, WebGL, and iOS)
if (NOT CMAKE_CROSSCOMPILING)
export(TARGETS matc cmgen filamesh mipgen FILE ${IMPORT_EXECUTABLES})
endif()
29 changes: 25 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Filament

<img alt="Android" src="build/img/android.png" width="20px" height="20px" hspace="2px"/>[![Android Build Status](https://filament-build.storage.googleapis.com/badges/build_status_android.svg)](https://filament-build.storage.googleapis.com/badges/build_link_android.html)
<img alt="iOS" src="build/img/macos.png" width="20px" height="20px" hspace="2px"/>[![iOS Build Status](https://filament-build.storage.googleapis.com/badges/build_status_ios.svg)](https://filament-build.storage.googleapis.com/badges/build_link_ios.html)
<img alt="Linux" src="build/img/linux.png" width="20px" height="20px" hspace="2px"/>[![Linux Build Status](https://filament-build.storage.googleapis.com/badges/build_status_linux.svg)](https://filament-build.storage.googleapis.com/badges/build_link_linux.html)
<img alt="macOS" src="build/img/macos.png" width="20px" height="20px" hspace="2px"/>[![MacOS Build Status](https://filament-build.storage.googleapis.com/badges/build_status_mac.svg)](https://filament-build.storage.googleapis.com/badges/build_link_mac.html)
<img alt="Windows" src="build/img/windows.png" width="20px" height="20px" hspace="2px"/>[![Windows Build Status](https://filament-build.storage.googleapis.com/badges/build_status_windows.svg)](https://filament-build.storage.googleapis.com/badges/build_link_windows.html)
<img alt="Web" src="build/img/web.png" width="20px" height="20px" hspace="2px"/>[![Web Build Status](https://filament-build.storage.googleapis.com/badges/build_status_web.svg)](https://filament-build.storage.googleapis.com/badges/build_link_web.html)

Filament is a real-time physically based rendering engine for Android, Linux, macOS, Windows, and
WebGL. It is designed to be as small as possible and as efficient as possible on Android.
Filament is a real-time physically based rendering engine for Android, iOS, Linux, macOS, Windows,
and WebGL. It is designed to be as small as possible and as efficient as possible on Android.

Filament is currently used in the
[Sceneform](https://developers.google.com/ar/develop/java/sceneform/) library both at runtime on
Expand Down Expand Up @@ -58,8 +59,8 @@ Here are a few sample materials rendered with Filament:
### Backends

- OpenGL 4.1+ for Linux, macOS and Windows
- OpenGL ES 3.0+ for Android
- Vulkan 1.0 for Android, Linux, macOS (with MoltenVk) and Windows
- OpenGL ES 3.0+ for Android and iOS
- Vulkan 1.0 for Android, Linux, macOS and iOS (with MoltenVk), and Windows
- WebGL 2.0 for all platforms

### Rendering
Expand Down Expand Up @@ -109,6 +110,7 @@ Many other features have been either prototyped or planned:
- `math`: Mathematica notebooks used to explore BRDFs, equations, etc.
- `filament`: Filament engine
- `ide`: Configuration files for IDEs (CLion, etc.)
- `ios`: Sample projects for iOS
- `java`: Java bindings for Filament libraries
- `libs`: Libraries
- `bluegl`: OpenGL bindings for macOS, Linux and Windows
Expand Down Expand Up @@ -301,6 +303,17 @@ $ cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../release/fi
$ ninja
```

### iOS

The easiest way to build Filament for iOS is to use `build.sh` and the
`-p ios` flag. For instance to build the debug target:

```
$ ./build.sh -p ios debug
```

See [ios/samples/README.md](./ios/samples/README.md) for more information.

### Windows

The following instructions have been tested on a machine running Windows 10. They should take you
Expand Down Expand Up @@ -842,6 +855,14 @@ package `com.google.android.filament.android`. All you need to do is set a rende
helper and attach your `SurfaceView` or `TextureView` to it. You are still responsible for
creating the swap chain in the `onNativeWindowChanged()` callback.
### iOS
See `ios/samples` for examples of using Filament on iOS.
Filament on iOS is largely the same as native rendering with C++. A `CAEAGLLayer` or `CAMetalLayer`
is passed to the `createSwapChain` method. Filament for iOS supports both OpenGL ES and Vulkan via
MoltenVK.
## Generating C++ documentation
To generate the documentation you must first install `doxygen`, then run the following commands:
Expand Down
97 changes: 93 additions & 4 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ set -e

# NDK API level
API_LEVEL=24
# Host tools required by Android and WebGL builds
# Host tools required by Android, WebGL, and iOS builds
HOST_TOOLS="matc cmgen filamesh mipgen"
IOS_TOOLCHAIN_URL="https://opensource.apple.com/source/clang/clang-800.0.38/src/cmake/platforms/iOS.cmake"

function print_help {
local SELF_NAME=`basename $0`
Expand All @@ -26,10 +27,10 @@ function print_help {
echo " Do not compile desktop Java projects"
echo " -m"
echo " Compile with make instead of ninja."
echo " -p [desktop|android|webgl|all]"
echo " -p [desktop|android|ios|webgl|all]"
echo " Platform(s) to build, defaults to desktop."
echo " Building android will automatically generate the toolchains if needed and"
echo " perform a partial desktop build."
echo " Building for Android or iOS will automatically generate / download"
echo " the toolchains if needed and perform a partial desktop build."
echo " -r"
echo " Restrict the number of make/ninja jobs."
echo " -t"
Expand Down Expand Up @@ -71,6 +72,7 @@ ISSUE_DEBUG_BUILD=false
ISSUE_RELEASE_BUILD=false

ISSUE_ANDROID_BUILD=false
ISSUE_IOS_BUILD=false
ISSUE_DESKTOP_BUILD=true
ISSUE_WEBGL_BUILD=false

Expand Down Expand Up @@ -352,6 +354,79 @@ function build_android {
cd ../..
}

function ensure_ios_toolchain {
local TOOLCHAIN_PATH="build/toolchain-arm64-mac-ios.cmake"
if [ -e ${TOOLCHAIN_PATH} ]; then
echo "iOS arm64 toolchain file exists."
return 0
fi
echo
echo "iOS arm64 toolchain file does not exist."
echo "It will automatically be downloaded from http://opensource.apple.com."
read -p "Continue? (y/n) " -n 1 -r
echo
if [[ ! "$REPLY" =~ ^[Yy]$ ]]; then
echo "Toolchain file must be downloaded to continue."
exit 1
fi
curl -o "${TOOLCHAIN_PATH}" "${IOS_TOOLCHAIN_URL}" || {
echo "Error downloading iOS toolchain file."
exit 1
}
cat build/toolchain-arm64-mac-ios.filament.cmake >> ${TOOLCHAIN_PATH}
echo "Successfully downloaded iOS toolchain file and appended Filament-specific settings."
}

function build_ios_target {
local LC_TARGET=`echo $1 | tr '[:upper:]' '[:lower:]'`
local ARCH=$2

echo "Building iOS $LC_TARGET ($ARCH)..."
mkdir -p out/cmake-ios-${LC_TARGET}-${ARCH}

cd out/cmake-ios-${LC_TARGET}-${ARCH}

if [ ! -d "CMakeFiles" ] || [ "$ISSUE_CMAKE_ALWAYS" == "true" ]; then
cmake \
-G "$BUILD_GENERATOR" \
-DCMAKE_BUILD_TYPE=${LC_TARGET} \
-DCMAKE_INSTALL_PREFIX=../ios-${LC_TARGET}/filament \
-DCMAKE_TOOLCHAIN_FILE=../../build/toolchain-arm64-mac-ios.cmake \
-DIOS=1 \
../..
fi

${BUILD_COMMAND} install

if [ -d "../ios-${LC_TARGET}/filament" ]; then
if [ "$ISSUE_ARCHIVES" == "true" ]; then
echo "Generating out/filament-${LC_TARGET}-ios.tgz..."
cd ../ios-${LC_TARGET}
tar -czvf ../filament-${LC_TARGET}-ios.tgz filament
fi
fi

cd ../..
}

function build_ios {
# Supress intermediate desktop tools install
OLD_INSTALL_COMMAND=${INSTALL_COMMAND}
INSTALL_COMMAND=
build_desktop "${HOST_TOOLS}"
INSTALL_COMMAND=${OLD_INSTALL_COMMAND}

ensure_ios_toolchain

if [ "$ISSUE_DEBUG_BUILD" == "true" ]; then
build_ios_target "Debug" "arm64"
fi

if [ "$ISSUE_RELEASE_BUILD" == "true" ]; then
build_ios_target "Release" "arm64"
fi
}

function validate_build_command {
set +e
# Make sure CMake is installed
Expand Down Expand Up @@ -441,21 +516,31 @@ while getopts ":hacfijmp:tuv" opt; do
case $OPTARG in
desktop)
ISSUE_ANDROID_BUILD=false
ISSUE_IOS_BUILD=false
ISSUE_DESKTOP_BUILD=true
ISSUE_WEBGL_BUILD=false
;;
android)
ISSUE_ANDROID_BUILD=true
ISSUE_IOS_BUILD=false
ISSUE_DESKTOP_BUILD=false
ISSUE_WEBGL_BUILD=false
;;
ios)
ISSUE_ANDROID_BUILD=false
ISSUE_IOS_BUILD=true
ISSUE_DESKTOP_BUILD=false
ISSUE_WEBGL_BUILD=false
;;
webgl)
ISSUE_ANDROID_BUILD=false
ISSUE_IOS_BUILD=false
ISSUE_DESKTOP_BUILD=false
ISSUE_WEBGL_BUILD=true
;;
all)
ISSUE_ANDROID_BUILD=true
ISSUE_IOS_BUILD=true
ISSUE_DESKTOP_BUILD=true
ISSUE_WEBGL_BUILD=false
;;
Expand Down Expand Up @@ -528,6 +613,10 @@ if [ "$ISSUE_ANDROID_BUILD" == "true" ]; then
build_android
fi

if [ "$ISSUE_IOS_BUILD" == "true" ]; then
build_ios
fi

if [ "$ISSUE_WEBGL_BUILD" == "true" ]; then
build_webgl
fi
Expand Down
2 changes: 2 additions & 0 deletions build/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# File is generated by ../build.sh and should not be tracked
toolchain-arm64-mac-ios.cmake
22 changes: 22 additions & 0 deletions build/ios/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

# Usage: the first argument selects the build type:
# - release, to build release only
# - debug, to build debug only
# - continuous, to build release and debug
# - presubmit, for presubmit builds
#
# The default is release

set -e
set -x

source `dirname $0`/../common/ci-common.sh
source `dirname $0`/ci-common.sh
source `dirname $0`/../common/build-common.sh

pushd `dirname $0`/../.. > /dev/null

# build.sh prompts the user to download Apple's iOS toolchain
yes | ./build.sh -p ios -c $GENERATE_ARCHIVES $BUILD_DEBUG $BUILD_RELEASE

6 changes: 6 additions & 0 deletions build/ios/ci-common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

curl -OL https://github.com/ninja-build/ninja/releases/download/v1.8.2/ninja-mac.zip
unzip -q ninja-mac.zip
chmod +x ninja
export PATH="$PWD:$PATH"
10 changes: 10 additions & 0 deletions build/ios/common.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Format: //devtools/kokoro/config/proto/build.proto

action {
define_artifacts {
regex: "github/filament/out/*.tgz"
regex: "github/filament/out/*.aar"
strip_prefix: "github/filament/out"
regex: "**/*sponge_log.xml"
}
}
3 changes: 3 additions & 0 deletions build/ios/continuous.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Format: //devtools/kokoro/config/proto/build.proto

build_file: "filament/build/ios/build.sh"
3 changes: 3 additions & 0 deletions build/ios/presubmit.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Format: //devtools/kokoro/config/proto/build.proto

build_file: "filament/build/ios/build.sh"
3 changes: 3 additions & 0 deletions build/ios/release.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Format: //devtools/kokoro/config/proto/build.proto

build_file: "filament/build/ios/build.sh"
18 changes: 18 additions & 0 deletions build/toolchain-arm64-mac-ios.filament.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# ==================================================================================================
# Filament-specific settings
# The rest of the toolchain is downloaded from Apple Open Source and appended.
# ==================================================================================================

# In theory, we could support older architectures, but only arm64 Apple devices support Metal
set(IOS_ARCH arm64)

set(CMAKE_OSX_ARCHITECTURES ${IOS_ARCH} CACHE STRING "Build architecture for iOS")

# Necessary for correct install location
set(DIST_ARCH arm64)

add_definitions(-DIOS)

# ==================================================================================================
# End Filament-specific settings
# ==================================================================================================
Loading

0 comments on commit 29119c7

Please sign in to comment.