From 651ccb183edd671032e9991f91fab128502e14b6 Mon Sep 17 00:00:00 2001 From: Mathias Agopian Date: Wed, 12 Feb 2020 13:43:50 -0800 Subject: [PATCH] New mathfwd.h header math/mathwfd.h forward declares all {mat|vec}{2|3|4}<> classes, which allows us to remove their respective #include in a lot of our public headers. Our math headers are full of templates, so this should help build times a bit. Also we want to keep the public headers as minimalist as possible. --- .../filament-android/src/main/cpp/Camera.cpp | 2 + .../src/main/cpp/IndirectLight.cpp | 1 + .../src/main/cpp/SurfaceOrientation.cpp | 2 + .../src/main/cpp/TransformManager.cpp | 5 +- filament/include/filament/Camera.h | 12 ++- filament/include/filament/DebugRegistry.h | 6 +- filament/include/filament/IndirectLight.h | 2 +- filament/include/filament/LightManager.h | 4 +- filament/include/filament/Material.h | 2 +- filament/include/filament/MaterialInstance.h | 6 +- filament/include/filament/RenderableManager.h | 4 +- filament/include/filament/TransformManager.h | 5 +- filament/include/filament/View.h | 3 +- filament/include/filament/Viewport.h | 2 +- filament/src/Camera.cpp | 4 + filament/src/DebugRegistry.cpp | 4 + filament/src/IndirectLight.cpp | 1 + filament/src/components/TransformManager.cpp | 6 ++ filament/src/details/IndirectLight.h | 2 + libs/filagui/src/ImGuiExtensions.cpp | 3 +- libs/image/include/image/ColorTransform.h | 1 + libs/math/CMakeLists.txt | 3 +- libs/math/include/math/mathfwd.h | 78 +++++++++++++++++++ libs/math/include/math/scalar.h | 18 +++-- web/filament-js/jsbindings.cpp | 7 +- 25 files changed, 150 insertions(+), 33 deletions(-) create mode 100644 libs/math/include/math/mathfwd.h diff --git a/android/filament-android/src/main/cpp/Camera.cpp b/android/filament-android/src/main/cpp/Camera.cpp index 8efc35e25f..ce612af02d 100644 --- a/android/filament-android/src/main/cpp/Camera.cpp +++ b/android/filament-android/src/main/cpp/Camera.cpp @@ -18,6 +18,8 @@ #include +#include + using namespace filament; extern "C" JNIEXPORT void JNICALL diff --git a/android/filament-android/src/main/cpp/IndirectLight.cpp b/android/filament-android/src/main/cpp/IndirectLight.cpp index 3b3dd07fc5..ebeed60cd6 100644 --- a/android/filament-android/src/main/cpp/IndirectLight.cpp +++ b/android/filament-android/src/main/cpp/IndirectLight.cpp @@ -20,6 +20,7 @@ #include #include #include +#include using namespace filament; diff --git a/android/filament-android/src/main/cpp/SurfaceOrientation.cpp b/android/filament-android/src/main/cpp/SurfaceOrientation.cpp index 6bf17cba9a..1ed1b2be5d 100644 --- a/android/filament-android/src/main/cpp/SurfaceOrientation.cpp +++ b/android/filament-android/src/main/cpp/SurfaceOrientation.cpp @@ -20,6 +20,8 @@ #include "common/NioUtils.h" +#include + using namespace filament; using namespace filament::geometry; using namespace filament::math; diff --git a/android/filament-android/src/main/cpp/TransformManager.cpp b/android/filament-android/src/main/cpp/TransformManager.cpp index af2f58fa07..b329776289 100644 --- a/android/filament-android/src/main/cpp/TransformManager.cpp +++ b/android/filament-android/src/main/cpp/TransformManager.cpp @@ -17,9 +17,12 @@ #include -#include #include +#include + +#include + using namespace utils; using namespace filament; diff --git a/filament/include/filament/Camera.h b/filament/include/filament/Camera.h index ea12582d9a..036829dd63 100644 --- a/filament/include/filament/Camera.h +++ b/filament/include/filament/Camera.h @@ -23,7 +23,7 @@ #include -#include +#include namespace utils { class Entity; @@ -258,7 +258,15 @@ public: */ void lookAt(const math::float3& eye, const math::float3& center, - const math::float3& up = { 0, 1, 0 }) noexcept; + const math::float3& up) noexcept; + + /** Sets the camera's view matrix, assuming up is along the y axis + * + * @param eye The position of the camera in world space. + * @param center The point in world space the camera is looking at. + */ + void lookAt(const math::float3& eye, + const math::float3& center) noexcept; /** Returns the camera's model matrix * diff --git a/filament/include/filament/DebugRegistry.h b/filament/include/filament/DebugRegistry.h index 9506aa4a47..c05a7e19fa 100644 --- a/filament/include/filament/DebugRegistry.h +++ b/filament/include/filament/DebugRegistry.h @@ -23,12 +23,10 @@ #include -#include -#include -#include +#include // FIXME: could we get rid of -#include +#include // for std::pair #include diff --git a/filament/include/filament/IndirectLight.h b/filament/include/filament/IndirectLight.h index 20661c1a6e..4a617f4e40 100644 --- a/filament/include/filament/IndirectLight.h +++ b/filament/include/filament/IndirectLight.h @@ -23,7 +23,7 @@ #include -#include +#include namespace filament { diff --git a/filament/include/filament/LightManager.h b/filament/include/filament/LightManager.h index 6c8ebbb963..8bb5bec2b8 100644 --- a/filament/include/filament/LightManager.h +++ b/filament/include/filament/LightManager.h @@ -23,9 +23,7 @@ #include #include -#include - -#include +#include namespace utils { class Entity; diff --git a/filament/include/filament/Material.h b/filament/include/filament/Material.h index b4b502d2f0..6be3a8df13 100644 --- a/filament/include/filament/Material.h +++ b/filament/include/filament/Material.h @@ -26,7 +26,7 @@ #include -#include +#include #include diff --git a/filament/include/filament/MaterialInstance.h b/filament/include/filament/MaterialInstance.h index 51cb814c0e..bbc867b22d 100644 --- a/filament/include/filament/MaterialInstance.h +++ b/filament/include/filament/MaterialInstance.h @@ -24,11 +24,7 @@ #include -#include -#include -#include -#include -#include +#include namespace filament { diff --git a/filament/include/filament/RenderableManager.h b/filament/include/filament/RenderableManager.h index 6e95d8289d..3c8bf5b397 100644 --- a/filament/include/filament/RenderableManager.h +++ b/filament/include/filament/RenderableManager.h @@ -26,9 +26,7 @@ #include #include -#include -#include -#include +#include #include diff --git a/filament/include/filament/TransformManager.h b/filament/include/filament/TransformManager.h index c5837fc402..a983fc9728 100644 --- a/filament/include/filament/TransformManager.h +++ b/filament/include/filament/TransformManager.h @@ -22,7 +22,7 @@ #include #include -#include +#include #include @@ -126,7 +126,8 @@ public: * * @see destroy() */ - void create(utils::Entity entity, Instance parent = {}, const math::mat4f& localTransform = {}); + void create(utils::Entity entity, Instance parent, const math::mat4f& localTransform); + void create(utils::Entity entity, Instance parent = {}); /** * Destroys this component from the given entity, children are orphaned. diff --git a/filament/include/filament/View.h b/filament/include/filament/View.h index 6b4c5531b0..63d03ec35c 100644 --- a/filament/include/filament/View.h +++ b/filament/include/filament/View.h @@ -26,8 +26,7 @@ #include -#include -#include +#include namespace filament { diff --git a/filament/include/filament/Viewport.h b/filament/include/filament/Viewport.h index 824aa7d638..c514655286 100644 --- a/filament/include/filament/Viewport.h +++ b/filament/include/filament/Viewport.h @@ -24,7 +24,7 @@ #include #include -#include +#include #include #include diff --git a/filament/src/Camera.cpp b/filament/src/Camera.cpp index 9a31f2369d..2c155dc20e 100644 --- a/filament/src/Camera.cpp +++ b/filament/src/Camera.cpp @@ -292,6 +292,10 @@ void Camera::lookAt(const float3& eye, const float3& center, float3 const& up) n upcast(this)->lookAt(eye, center, up); } +void Camera::lookAt(const float3& eye, const float3& center) noexcept { + upcast(this)->lookAt(eye, center, {0, 1, 0}); +} + mat4f Camera::getModelMatrix() const noexcept { return upcast(this)->getModelMatrix(); } diff --git a/filament/src/DebugRegistry.cpp b/filament/src/DebugRegistry.cpp index cff43d3322..2ea711ecad 100644 --- a/filament/src/DebugRegistry.cpp +++ b/filament/src/DebugRegistry.cpp @@ -16,6 +16,10 @@ #include "details/DebugRegistry.h" +#include +#include +#include + #ifndef NDEBUG # define DEBUG_PROPERTIES_WRITABLE true #else diff --git a/filament/src/IndirectLight.cpp b/filament/src/IndirectLight.cpp index 803aca4e87..8af6c5cd19 100644 --- a/filament/src/IndirectLight.cpp +++ b/filament/src/IndirectLight.cpp @@ -27,6 +27,7 @@ #include #include +#include #define IBL_INTEGRATION_PREFILTERED_CUBEMAP 0 #define IBL_INTEGRATION_IMPORTANCE_SAMPLING 1 diff --git a/filament/src/components/TransformManager.cpp b/filament/src/components/TransformManager.cpp index 0ed6d741db..a4da03ba0a 100644 --- a/filament/src/components/TransformManager.cpp +++ b/filament/src/components/TransformManager.cpp @@ -16,6 +16,8 @@ #include "components/TransformManager.h" +#include + using namespace utils; using namespace filament::math; @@ -395,6 +397,10 @@ void TransformManager::create(Entity entity, Instance parent, const mat4f& world upcast(this)->create(entity, parent, worldTransform); } +void TransformManager::create(Entity entity, Instance parent) { + upcast(this)->create(entity, parent, {}); +} + void TransformManager::destroy(Entity e) noexcept { upcast(this)->destroy(e); } diff --git a/filament/src/details/IndirectLight.h b/filament/src/details/IndirectLight.h index 14b7910995..998fe3e4a4 100644 --- a/filament/src/details/IndirectLight.h +++ b/filament/src/details/IndirectLight.h @@ -26,6 +26,8 @@ #include +#include + #include namespace filament { diff --git a/libs/filagui/src/ImGuiExtensions.cpp b/libs/filagui/src/ImGuiExtensions.cpp index bd0c0172fd..cd61e9e00b 100644 --- a/libs/filagui/src/ImGuiExtensions.cpp +++ b/libs/filagui/src/ImGuiExtensions.cpp @@ -22,6 +22,7 @@ #include #include +#include #include using namespace filament::math; @@ -30,7 +31,7 @@ using namespace filament::math; // Maughan's port to ImGui. Thanks Chris! class ArrowWidget { public: - ArrowWidget(float3 direction); + explicit ArrowWidget(float3 direction); bool draw(); float3 getDirection() const; private: diff --git a/libs/image/include/image/ColorTransform.h b/libs/image/include/image/ColorTransform.h index a85c4adb3d..9ff88266fa 100644 --- a/libs/image/include/image/ColorTransform.h +++ b/libs/image/include/image/ColorTransform.h @@ -26,6 +26,7 @@ #include #include +#include #include namespace image { diff --git a/libs/math/CMakeLists.txt b/libs/math/CMakeLists.txt index 906b1dd3f7..cdfafa7e65 100644 --- a/libs/math/CMakeLists.txt +++ b/libs/math/CMakeLists.txt @@ -20,6 +20,7 @@ set(PUBLIC_HDRS include/math/compiler.h include/math/fast.h include/math/half.h + include/math/mathfwd.h include/math/mat2.h include/math/mat3.h include/math/mat4.h @@ -64,7 +65,7 @@ target_link_libraries(test_${TARGET} PRIVATE math gtest) # ================================================================================================== set(BENCHMARK_SRCS - benchmarks/benchmark_fast.cpp) + benchmarks/benchmark_fast.cpp include/math/mathfwd.h) add_executable(benchmark_${TARGET} ${BENCHMARK_SRCS}) diff --git a/libs/math/include/math/mathfwd.h b/libs/math/include/math/mathfwd.h new file mode 100644 index 0000000000..c74e6e4f26 --- /dev/null +++ b/libs/math/include/math/mathfwd.h @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2020 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 MATH_MATHFWD_H_ +#define MATH_MATHFWD_H_ + +#include + +namespace filament { +namespace math { +namespace details { + +template class TVec2; +template class TVec3; +template class TVec4; + +template class TMat22; +template class TMat33; +template class TMat44; + +} // namespace details + +using double2 = details::TVec2; +using float2 = details::TVec2; +using int2 = details::TVec2; +using uint2 = details::TVec2; +using short2 = details::TVec2; +using ushort2 = details::TVec2; +using byte2 = details::TVec2; +using ubyte2 = details::TVec2; +using bool2 = details::TVec2; + +using double3 = details::TVec3; +using float3 = details::TVec3; +using int3 = details::TVec3; +using uint3 = details::TVec3; +using short3 = details::TVec3; +using ushort3 = details::TVec3; +using byte3 = details::TVec3; +using ubyte3 = details::TVec3; +using bool3 = details::TVec3; + +using double4 = details::TVec4; +using float4 = details::TVec4; +using int4 = details::TVec4; +using uint4 = details::TVec4; +using short4 = details::TVec4; +using ushort4 = details::TVec4; +using byte4 = details::TVec4; +using ubyte4 = details::TVec4; +using bool4 = details::TVec4; + +using mat2 = details::TMat22; +using mat2f = details::TMat22; + +using mat3 = details::TMat33; +using mat3f = details::TMat33; + +using mat4 = details::TMat44; +using mat4f = details::TMat44; + +} // namespace math +} // namespace filament + +#endif // MATH_MATHFWD_H_ diff --git a/libs/math/include/math/scalar.h b/libs/math/include/math/scalar.h index 405477733a..ecde716262 100644 --- a/libs/math/include/math/scalar.h +++ b/libs/math/include/math/scalar.h @@ -17,8 +17,6 @@ #ifndef TNT_MATH_SCALAR_H #define TNT_MATH_SCALAR_H -#include -#include #include namespace filament { @@ -39,13 +37,23 @@ constexpr const double F_SQRT2 = 1.41421356237309504880168872420969808; constexpr const double F_SQRT1_2 = 0.707106781186547524400844362104849039; template -inline constexpr T MATH_PURE saturate(T v) noexcept { - return T(std::min(T(1), std::max(T(0), v))); +inline constexpr T MATH_PURE min(T a, T b) noexcept { + return a < b ? a : b; +} + +template +inline constexpr T MATH_PURE max(T a, T b) noexcept { + return a > b ? a : b; } template inline constexpr T MATH_PURE clamp(T v, T min, T max) noexcept { - return T(std::min(max, std::max(min, v))); + return T(math::min(max, math::max(min, v))); +} + +template +inline constexpr T MATH_PURE saturate(T v) noexcept { + return clamp(T(1), T(0), v); } template diff --git a/web/filament-js/jsbindings.cpp b/web/filament-js/jsbindings.cpp index ed4c89c8ed..45c66bcb57 100644 --- a/web/filament-js/jsbindings.cpp +++ b/web/filament-js/jsbindings.cpp @@ -544,7 +544,12 @@ class_("Camera") self->setModelMatrix(m.m); }), allow_raw_pointers()) - .function("lookAt", &Camera::lookAt) + .function("lookAt", EMBIND_LAMBDA(void, (Camera* self, + const math::float3& eye, + const math::float3& center, + const math::float3& up), { + self->lookAt(eye, center, up); + }), allow_raw_pointers()) .function("getModelMatrix", EMBIND_LAMBDA(flatmat4, (Camera* self), { return flatmat4 { self->getModelMatrix() };