From 6805940bbfb13377e44f277910927af8c6a48e7b Mon Sep 17 00:00:00 2001
From: daemyung jang
Date: Thu, 13 Jan 2022 04:06:47 +0900
Subject: [PATCH] Compute morphing on GPU (#4999)
---
.../filament/filamat/MaterialBuilder.java | 16 +-
.../src/main/cpp/RenderableManager.cpp | 4 +-
.../android/filament/RenderableManager.java | 8 +-
.../google/android/filament/VertexBuffer.java | 16 +-
docs/webgl/reference.html | 8 -
filament/CMakeLists.txt | 3 +
filament/include/filament/Engine.h | 2 +
filament/include/filament/MorphTargetBuffer.h | 119 +++++++++
filament/include/filament/RenderableManager.h | 17 +-
filament/src/Allocators.h | 4 +-
filament/src/Engine.cpp | 13 +
filament/src/Material.cpp | 4 +
filament/src/MorphTargetBuffer.cpp | 225 ++++++++++++++++++
filament/src/RenderPass.cpp | 15 ++
filament/src/RenderPass.h | 13 +-
filament/src/RenderPrimitive.cpp | 4 +
filament/src/RenderPrimitive.h | 5 +
filament/src/Renderer.cpp | 3 +
filament/src/Scene.cpp | 6 +-
filament/src/ShadowMapManager.cpp | 2 +
filament/src/View.cpp | 14 ++
filament/src/components/RenderableManager.cpp | 70 +++++-
filament/src/components/RenderableManager.h | 39 ++-
filament/src/details/Engine.h | 4 +
filament/src/details/MorphTargetBuffer.h | 69 ++++++
filament/src/details/Scene.h | 4 +-
filament/src/details/View.h | 4 +
.../include/filament/MaterialEnums.h | 14 +-
.../include/private/filament/EngineEnums.h | 22 +-
.../include/private/filament/SibGenerator.h | 10 +
.../include/private/filament/UibStructs.h | 10 +-
libs/filabridge/src/SibGenerator.cpp | 15 ++
libs/filamat/src/UibGenerator.cpp | 10 +-
libs/filamat/src/UibGenerator.h | 1 +
libs/filamat/src/shaders/ShaderGenerator.cpp | 14 +-
libs/gltfio/include/gltfio/FilamentAsset.h | 10 +
libs/gltfio/src/Animator.cpp | 6 +-
libs/gltfio/src/AssetLoader.cpp | 36 +--
libs/gltfio/src/FFilamentAsset.h | 9 +-
libs/gltfio/src/FilamentAsset.cpp | 23 ++
libs/gltfio/src/MorphHelper.cpp | 142 ++++-------
libs/gltfio/src/MorphHelper.h | 27 +--
libs/gltfio/src/ResourceLoader.cpp | 15 +-
libs/viewer/include/viewer/SimpleViewer.h | 3 +
libs/viewer/src/SimpleViewer.cpp | 22 ++
shaders/src/getters.vs | 43 +++-
shaders/src/main.vs | 10 +-
web/filament-js/filament.d.ts | 8 -
web/filament-js/jsbindings.cpp | 7 +-
web/filament-js/jsenums.cpp | 10 +-
50 files changed, 853 insertions(+), 305 deletions(-)
create mode 100644 filament/include/filament/MorphTargetBuffer.h
create mode 100644 filament/src/MorphTargetBuffer.cpp
create mode 100644 filament/src/details/MorphTargetBuffer.h
diff --git a/android/filamat-android/src/main/java/com/google/android/filament/filamat/MaterialBuilder.java b/android/filamat-android/src/main/java/com/google/android/filament/filamat/MaterialBuilder.java
index 5017f18fc5..c52cd51e0c 100644
--- a/android/filamat-android/src/main/java/com/google/android/filament/filamat/MaterialBuilder.java
+++ b/android/filamat-android/src/main/java/com/google/android/filament/filamat/MaterialBuilder.java
@@ -114,14 +114,14 @@ public class MaterialBuilder {
BONE_INDICES, // indices of 4 bones (uvec4)
BONE_WEIGHTS, // weights of the 4 bones (normalized float4)
UNUSED, // reserved for future use
- CUSTOM0, // custom or MORPH_POSITION_0
- CUSTOM1, // custom or MORPH_POSITION_1
- CUSTOM2, // custom or MORPH_POSITION_2
- CUSTOM3, // custom or MORPH_POSITION_3
- CUSTOM4, // custom or MORPH_TANGENTS_0
- CUSTOM5, // custom or MORPH_TANGENTS_1
- CUSTOM6, // custom or MORPH_TANGENTS_2
- CUSTOM7 // custom or MORPH_TANGENTS_3
+ CUSTOM0, // custom
+ CUSTOM1, // custom
+ CUSTOM2, // custom
+ CUSTOM3, // custom
+ CUSTOM4, // custom
+ CUSTOM5, // custom
+ CUSTOM6, // custom
+ CUSTOM7 // custom
}
public enum BlendingMode {
diff --git a/android/filament-android/src/main/cpp/RenderableManager.cpp b/android/filament-android/src/main/cpp/RenderableManager.cpp
index cba710637c..8d3d43dcdd 100644
--- a/android/filament-android/src/main/cpp/RenderableManager.cpp
+++ b/android/filament-android/src/main/cpp/RenderableManager.cpp
@@ -270,9 +270,9 @@ Java_com_google_android_filament_RenderableManager_nSetMorphWeights(JNIEnv* env,
jlong nativeRenderableManager, jint instance, jfloatArray weights) {
RenderableManager *rm = (RenderableManager *) nativeRenderableManager;
jfloat* vec = env->GetFloatArrayElements(weights, NULL);
- math::float4 floatvec(vec[0], vec[1], vec[2], vec[3]);
+ jsize count = env->GetArrayLength(weights);
+ rm->setMorphWeights((RenderableManager::Instance)instance, vec, count);
env->ReleaseFloatArrayElements(weights, vec, JNI_ABORT);
- rm->setMorphWeights((RenderableManager::Instance)instance, floatvec);
}
extern "C" JNIEXPORT void JNICALL
diff --git a/android/filament-android/src/main/java/com/google/android/filament/RenderableManager.java b/android/filament-android/src/main/java/com/google/android/filament/RenderableManager.java
index db4fdb8c83..1a5785ebe7 100644
--- a/android/filament-android/src/main/java/com/google/android/filament/RenderableManager.java
+++ b/android/filament-android/src/main/java/com/google/android/filament/RenderableManager.java
@@ -402,9 +402,6 @@ public class RenderableManager {
/**
* Controls if the renderable has vertex morphing targets, false by default.
*
- * This is required to enable GPU morphing for up to 4 attributes. The attached VertexBuffer
- * must provide data in the appropriate VertexAttribute slots (MORPH_POSITION_0 etc).
- *
* See also {@link RenderableManager#setMorphWeights}, which can be called on a per-frame basis
* to advance the animation.
*/
@@ -497,14 +494,11 @@ public class RenderableManager {
/**
* Updates the vertex morphing weights on a renderable, all zeroes by default.
*
- * This is specified using a 4-tuple, one float per morph target. If the renderable has fewer
- * than 4 morph targets, then clients should fill the unused components with zeroes.
- *
* The renderable must be built with morphing enabled.
*
* @see Builder#morphing
*/
- public void setMorphWeights(@EntityInstance int i, @NonNull @Size(min = 4) float[] weights) {
+ public void setMorphWeights(@EntityInstance int i, @NonNull float[] weights) {
nSetMorphWeights(mNativeObject, i, weights);
}
diff --git a/android/filament-android/src/main/java/com/google/android/filament/VertexBuffer.java b/android/filament-android/src/main/java/com/google/android/filament/VertexBuffer.java
index d3494e81ef..359505d369 100644
--- a/android/filament-android/src/main/java/com/google/android/filament/VertexBuffer.java
+++ b/android/filament-android/src/main/java/com/google/android/filament/VertexBuffer.java
@@ -66,14 +66,14 @@ public class VertexBuffer {
BONE_INDICES, // indices of 4 bones (uvec4)
BONE_WEIGHTS, // weights of the 4 bones (normalized float4)
UNUSED, // reserved for future use
- CUSTOM0, // custom or MORPH_POSITION_0
- CUSTOM1, // custom or MORPH_POSITION_1
- CUSTOM2, // custom or MORPH_POSITION_2
- CUSTOM3, // custom or MORPH_POSITION_3
- CUSTOM4, // custom or MORPH_TANGENTS_0
- CUSTOM5, // custom or MORPH_TANGENTS_1
- CUSTOM6, // custom or MORPH_TANGENTS_2
- CUSTOM7 // custom or MORPH_TANGENTS_3
+ CUSTOM0, // custom
+ CUSTOM1, // custom
+ CUSTOM2, // custom
+ CUSTOM3, // custom
+ CUSTOM4, // custom
+ CUSTOM5, // custom
+ CUSTOM6, // custom
+ CUSTOM7 // custom
}
public enum AttributeType {
diff --git a/docs/webgl/reference.html b/docs/webgl/reference.html
index 3fd55c7de6..6b7510906d 100644
--- a/docs/webgl/reference.html
+++ b/docs/webgl/reference.html
@@ -1438,14 +1438,6 @@ This defines the following functions:
CUSTOM5
CUSTOM6
CUSTOM7
-MORPH_POSITION_0
-MORPH_POSITION_1
-MORPH_POSITION_2
-MORPH_POSITION_3
-MORPH_TANGENTS_0
-MORPH_TANGENTS_1
-MORPH_TANGENTS_2
-MORPH_TANGENTS_3
diff --git a/filament/CMakeLists.txt b/filament/CMakeLists.txt
index 4049d39130..c9c996a9d3 100644
--- a/filament/CMakeLists.txt
+++ b/filament/CMakeLists.txt
@@ -28,6 +28,7 @@ set(PUBLIC_HDRS
include/filament/LightManager.h
include/filament/Material.h
include/filament/MaterialInstance.h
+ include/filament/MorphTargetBuffer.h
include/filament/Options.h
include/filament/RenderTarget.h
include/filament/RenderableManager.h
@@ -69,6 +70,7 @@ set(SRCS
src/Material.cpp
src/MaterialInstance.cpp
src/MaterialParser.cpp
+ src/MorphTargetBuffer.cpp
src/PerViewUniforms.cpp
src/PostProcessManager.cpp
src/RenderPass.cpp
@@ -140,6 +142,7 @@ set(PRIVATE_HDRS
src/details/IndirectLight.h
src/details/Material.h
src/details/MaterialInstance.h
+ src/details/MorphTargetBuffer.h
src/details/RenderTarget.h
src/details/Renderer.h
src/details/Scene.h
diff --git a/filament/include/filament/Engine.h b/filament/include/filament/Engine.h
index 4db5ae7576..0a31013f66 100644
--- a/filament/include/filament/Engine.h
+++ b/filament/include/filament/Engine.h
@@ -39,6 +39,7 @@ class SkinningBuffer;
class IndirectLight;
class Material;
class MaterialInstance;
+class MorphTargetBuffer;
class Renderer;
class RenderTarget;
class Scene;
@@ -421,6 +422,7 @@ public:
bool destroy(const Fence* p); //!< Destroys a Fence object.
bool destroy(const IndexBuffer* p); //!< Destroys an IndexBuffer object.
bool destroy(const SkinningBuffer* p); //!< Destroys a SkinningBuffer object.
+ bool destroy(const MorphTargetBuffer* p); //!< Destroys a MorphTargetBuffer object.
bool destroy(const IndirectLight* p); //!< Destroys an IndirectLight object.
/**
diff --git a/filament/include/filament/MorphTargetBuffer.h b/filament/include/filament/MorphTargetBuffer.h
new file mode 100644
index 0000000000..f3c0877039
--- /dev/null
+++ b/filament/include/filament/MorphTargetBuffer.h
@@ -0,0 +1,119 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef TNT_FILAMENT_MORPHTARGETBUFFER_H
+#define TNT_FILAMENT_MORPHTARGETBUFFER_H
+
+#include
+
+#include
+
+#include