From 125f79e5e028ed7adbeea1949f934280173ce588 Mon Sep 17 00:00:00 2001 From: Philip Rideout Date: Wed, 12 Dec 2018 16:54:39 -0800 Subject: [PATCH] Allow Java / Kotlin to enable the experimental Vulkan backend. We still do not compile the Vulkan backend for Android by default, this simply makes it possible to use the samples on Vulkan with a one-line change, which is useful for testing purposes. This change also makes it so that the materials used for the samples include SPIR-V. This makes them fatter but they are merely samples. I still consider Vulkan on Android to be experimental, there are some features that need to be implemented. --- android/build/filament-tasks.gradle | 2 +- .../filament-android/src/main/cpp/Engine.cpp | 4 ++-- .../com/google/android/filament/Engine.java | 20 ++++++++++++++++--- build.sh | 2 +- filament/src/Engine.cpp | 2 -- 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/android/build/filament-tasks.gradle b/android/build/filament-tasks.gradle index 199b46b771..2a93de5e3f 100644 --- a/android/build/filament-tasks.gradle +++ b/android/build/filament-tasks.gradle @@ -96,7 +96,7 @@ class MaterialCompiler extends DefaultTask { standardOutput out errorOutput err executable "${matcPath}" - args('-p', 'mobile', '-o', getOutputFile(file), file) + args('-a', 'all', '-p', 'mobile', '-o', getOutputFile(file), file) } } diff --git a/android/filament-android/src/main/cpp/Engine.cpp b/android/filament-android/src/main/cpp/Engine.cpp index eeb5a049ce..e508e4a614 100644 --- a/android/filament-android/src/main/cpp/Engine.cpp +++ b/android/filament-android/src/main/cpp/Engine.cpp @@ -22,9 +22,9 @@ using namespace filament; using namespace utils; extern "C" JNIEXPORT jlong JNICALL -Java_com_google_android_filament_Engine_nCreateEngine(JNIEnv*, jclass, +Java_com_google_android_filament_Engine_nCreateEngine(JNIEnv*, jclass, jlong backend, jlong sharedContext) { - return (jlong) Engine::create(Engine::Backend::OPENGL, nullptr, (void*) sharedContext); + return (jlong) Engine::create((Engine::Backend) backend, nullptr, (void*) sharedContext); } extern "C" JNIEXPORT void JNICALL diff --git a/android/filament-android/src/main/java/com/google/android/filament/Engine.java b/android/filament-android/src/main/java/com/google/android/filament/Engine.java index 00c044466e..a04e92c4d8 100644 --- a/android/filament-android/src/main/java/com/google/android/filament/Engine.java +++ b/android/filament-android/src/main/java/com/google/android/filament/Engine.java @@ -24,6 +24,13 @@ public class Engine { @NonNull private final LightManager mLightManager; @NonNull private final RenderableManager mRenderableManager; + public enum Backend { + DEFAULT, // Automatically selects an appropriate driver for the platform. + OPENGL, // Selects the OpenGL ES driver. + VULKAN, // Selects the experimental Vulkan driver. + NOOP, // Selects the no-op driver for testing purposes. + } + private Engine(long nativeEngine) { mNativeObject = nativeEngine; mTransformManager = new TransformManager(nGetTransformManager(nativeEngine)); @@ -33,7 +40,14 @@ public class Engine { @NonNull public static Engine create() { - long nativeEngine = nCreateEngine(0); + long nativeEngine = nCreateEngine(0, 0); + if (nativeEngine == 0) throw new IllegalStateException("Couldn't create Engine"); + return new Engine(nativeEngine); + } + + @NonNull + public static Engine create(@NonNull Backend backend) { + long nativeEngine = nCreateEngine(backend.ordinal(), 0); if (nativeEngine == 0) throw new IllegalStateException("Couldn't create Engine"); return new Engine(nativeEngine); } @@ -46,7 +60,7 @@ public class Engine { @NonNull public static Engine create(@NonNull Object sharedContext) { if (Platform.get().validateSharedContext(sharedContext)) { - long nativeEngine = nCreateEngine( + long nativeEngine = nCreateEngine(0, Platform.get().getSharedContextNativeHandle(sharedContext)); if (nativeEngine == 0) throw new IllegalStateException("Couldn't create Engine"); return new Engine(nativeEngine); @@ -263,7 +277,7 @@ public class Engine { mNativeObject = 0; } - private static native long nCreateEngine(long sharedContext); + private static native long nCreateEngine(long backend, long sharedContext); private static native void nDestroyEngine(long nativeEngine); private static native long nCreateSwapChain(long nativeEngine, Object nativeWindow, long flags); private static native long nCreateSwapChainFromRawPointer(long nativeEngine, long pointer, long flags); diff --git a/build.sh b/build.sh index 42e4a7f32a..2d1fedad2b 100755 --- a/build.sh +++ b/build.sh @@ -600,7 +600,7 @@ while getopts ":hacfijmp:tuvs" opt; do echo "To switch your application to Vulkan, in Android Studio go to " echo "File > Settings > Build > Compiler. In the command-line options field, " echo "add -Pextra_cmake_args=-DFILAMENT_SUPPORTS_VULKAN=ON." - echo "Also be sure to pass Backend::VULKAN to Engine::create." + echo "Also be sure to pass Engine.Backend.VULKAN to Engine.create." echo "" ;; s) diff --git a/filament/src/Engine.cpp b/filament/src/Engine.cpp index 02e259c044..64ea019dcd 100644 --- a/filament/src/Engine.cpp +++ b/filament/src/Engine.cpp @@ -356,10 +356,8 @@ int FEngine::loop() { if (platform == nullptr) { platform = Platform::create(&mBackend); mPlatform = platform; -#if !defined(NDEBUG) slog.d << "FEngine resolved backend: " << (mBackend == driver::Backend::VULKAN ? "Vulkan" : "OpenGL") << io::endl; -#endif } mDriver = platform->createDriver(mSharedGLContext); mDriverBarrier.latch();