diff --git a/filament/backend/include/backend/PixelBufferDescriptor.h b/filament/backend/include/backend/PixelBufferDescriptor.h index 9c19f1942d..2a1a2c6654 100644 --- a/filament/backend/include/backend/PixelBufferDescriptor.h +++ b/filament/backend/include/backend/PixelBufferDescriptor.h @@ -280,7 +280,7 @@ public: } size_t bpr = bpp * stride; - size_t bprAligned = (bpr + (alignment - 1)) & -alignment; + size_t bprAligned = (bpr + (alignment - 1)) & (~alignment + 1); return bprAligned * height; } diff --git a/filament/include/filament/Texture.h b/filament/include/filament/Texture.h index 6247c9bdf7..c005c2b165 100644 --- a/filament/include/filament/Texture.h +++ b/filament/include/filament/Texture.h @@ -327,7 +327,8 @@ public: * PixelBufferDescriptor&& buffer) */ inline void setImage(Engine& engine, size_t level, PixelBufferDescriptor&& buffer) const { - setImage(engine, level, 0, 0, 0, getWidth(level), getHeight(level), 1, std::move(buffer)); + setImage(engine, level, 0, 0, 0, + uint32_t(getWidth(level)), uint32_t(getHeight(level)), 1, std::move(buffer)); } /** diff --git a/libs/filamentapp/include/filamentapp/MeshAssimp.h b/libs/filamentapp/include/filamentapp/MeshAssimp.h index 39a6c8161e..a9b475826e 100644 --- a/libs/filamentapp/include/filamentapp/MeshAssimp.h +++ b/libs/filamentapp/include/filamentapp/MeshAssimp.h @@ -131,8 +131,8 @@ private: mutable std::unordered_map mGltfMaterialCache; filament::Texture* mDefaultMap = nullptr; filament::Texture* mDefaultNormalMap = nullptr; - float mDefaultMetallic = 0.0; - float mDefaultRoughness = 0.4; + float mDefaultMetallic = 0.0f; + float mDefaultRoughness = 0.4f; filament::sRGBColor mDefaultEmissive = filament::sRGBColor({0.0f, 0.0f, 0.0f}); std::vector mRenderables; diff --git a/libs/gltfio/include/gltfio/MaterialProvider.h b/libs/gltfio/include/gltfio/MaterialProvider.h index 3497a7ef9a..289a9cb0bb 100644 --- a/libs/gltfio/include/gltfio/MaterialProvider.h +++ b/libs/gltfio/include/gltfio/MaterialProvider.h @@ -36,8 +36,8 @@ enum class AlphaMode : uint8_t { // The following struct gets hashed so all padding bits should be explicit. // Tell the compiler to emit a warning if it adds any padding. -#pragma clang diagnostic push -#pragma clang diagnostic warning "-Wpadded" +UTILS_WARNING_PUSH +UTILS_WARNING_ENABLE_PADDED /** * \struct MaterialKey MaterialProvider.h gltfio/MaterialProvider.h @@ -98,7 +98,7 @@ struct alignas(4) MaterialKey { static_assert(sizeof(MaterialKey) == 16, "MaterialKey has unexpected size."); -#pragma clang diagnostic pop +UTILS_WARNING_POP bool operator==(const MaterialKey& k1, const MaterialKey& k2); diff --git a/libs/gltfio/include/gltfio/math.h b/libs/gltfio/include/gltfio/math.h index 1482a4b288..dfbe0fca40 100644 --- a/libs/gltfio/include/gltfio/math.h +++ b/libs/gltfio/include/gltfio/math.h @@ -81,7 +81,7 @@ UTILS_PUBLIC inline void decomposeMatrix(const filament::math::mat4f& mat, filam *rotation = clone.toQuaternion(); } else { // Set to identity if close to zero - *rotation = quatf(1); + *rotation = quatf(1.0f); } } diff --git a/libs/image/include/image/ColorTransform.h b/libs/image/include/image/ColorTransform.h index 8986cfd303..f5f8f19536 100644 --- a/libs/image/include/image/ColorTransform.h +++ b/libs/image/include/image/ColorTransform.h @@ -150,7 +150,7 @@ template<> inline filament::math::float3 linearToSRGB(const filament::math::float3& color) { using filament::math::float3; float3 sRGBColor{color}; - #pragma nounroll + UTILS_NOUNROLL for (size_t i = 0; i < sRGBColor.size(); i++) { sRGBColor[i] = (sRGBColor[i] <= 0.0031308f) ? sRGBColor[i] * 12.92f : (powf(sRGBColor[i], 1.0f / 2.4f) * 1.055f) - 0.055f; diff --git a/libs/math/include/math/mat2.h b/libs/math/include/math/mat2.h index 957588a29e..551fe4451e 100644 --- a/libs/math/include/math/mat2.h +++ b/libs/math/include/math/mat2.h @@ -243,7 +243,9 @@ public: uint64_t result = 0; // For some reason clang is not able to vectoize this loop when the number of iteration // is known and constant (!?!?!). Still this is better than operator==. +#if defined(__clang__) #pragma clang loop vectorize_width(2) +#endif for (size_t i = 0; i < sizeof(TMat22) / sizeof(uint64_t); i++) { result |= li[i] ^ ri[i]; } diff --git a/libs/uberz/include/uberz/ReadableArchive.h b/libs/uberz/include/uberz/ReadableArchive.h index 4e5dc2161a..5d78cb2693 100644 --- a/libs/uberz/include/uberz/ReadableArchive.h +++ b/libs/uberz/include/uberz/ReadableArchive.h @@ -30,8 +30,8 @@ namespace filament::uberz { // offset fields into pointers. void convertOffsetsToPointers(struct ReadableArchive* archive); -#pragma clang diagnostic push -#pragma clang diagnostic warning "-Wpadded" +UTILS_WARNING_PUSH +UTILS_WARNING_ENABLE_PADDED // Precompiled set of materials bundled with a list of features flags that each material supports. // This is the readable counterpart to WriteableArchive. @@ -72,7 +72,7 @@ struct ArchiveFlag { ArchiveFeature value; }; -#pragma clang diagnostic pop +UTILS_WARNING_POP } // namespace filament::uberz diff --git a/libs/uberz/include/uberz/WritableArchive.h b/libs/uberz/include/uberz/WritableArchive.h index 2e2121c6fe..e511d95f8d 100644 --- a/libs/uberz/include/uberz/WritableArchive.h +++ b/libs/uberz/include/uberz/WritableArchive.h @@ -34,7 +34,10 @@ namespace filament::uberz { // Users do not need to access this class directly, they should go through gltfio. class WritableArchive { public: - WritableArchive(size_t materialCount) : mMaterials(materialCount) {} + WritableArchive(size_t materialCount) : mMaterials(uint32_t(materialCount)) { + assert(materialCount <= UINT_MAX); + } + void addMaterial(const char* name, const uint8_t* package, size_t packageSize); void addSpecLine(std::string_view line); utils::FixedCapacityVector serialize() const; diff --git a/libs/utils/include/utils/Allocator.h b/libs/utils/include/utils/Allocator.h index 9935008e0c..82d1d1ccee 100644 --- a/libs/utils/include/utils/Allocator.h +++ b/libs/utils/include/utils/Allocator.h @@ -51,7 +51,7 @@ static inline P* align(P* p, size_t alignment) noexcept { template static inline P* align(P* p, size_t alignment, size_t offset) noexcept { P* const r = align(add(p, offset), alignment); - assert(pointermath::add(r, -offset) >= p); + assert(r >= add(p, offset)); return r; } @@ -129,7 +129,9 @@ public: private: void* end() UTILS_RESTRICT noexcept { return pointermath::add(mBegin, mSize); } void* current() UTILS_RESTRICT noexcept { return pointermath::add(mBegin, mCur); } - void set_current(void* p) UTILS_RESTRICT noexcept { mCur = uintptr_t(p) - uintptr_t(mBegin); } + void set_current(void* p) UTILS_RESTRICT noexcept { + mCur = uint32_t(uintptr_t(p) - uintptr_t(mBegin)); + } void* mBegin = nullptr; uint32_t mSize = 0; diff --git a/libs/utils/include/utils/BinaryTreeArray.h b/libs/utils/include/utils/BinaryTreeArray.h index b36d4b35ec..b4e2b1913d 100644 --- a/libs/utils/include/utils/BinaryTreeArray.h +++ b/libs/utils/include/utils/BinaryTreeArray.h @@ -53,7 +53,9 @@ class BinaryTreeArray { public: static size_t count(size_t height) noexcept { return (1u << height) - 1; } static size_t left(size_t i, size_t /*height*/) noexcept { return i + 1; } - static size_t right(size_t i, size_t height) noexcept { return i + (1u << (height - 1)); } + static size_t right(size_t i, size_t height) noexcept { + return i + (size_t(1) << (height - 1)); + } // this builds the depth-first binary tree array top down (post-order) template diff --git a/libs/utils/include/utils/FixedCapacityVector.h b/libs/utils/include/utils/FixedCapacityVector.h index d9d517c2ac..540b42b21d 100644 --- a/libs/utils/include/utils/FixedCapacityVector.h +++ b/libs/utils/include/utils/FixedCapacityVector.h @@ -331,7 +331,7 @@ private: } void construct(iterator first, iterator last, const_reference proto) noexcept { - #pragma nounroll + UTILS_NOUNROLL while (first != last) { storage_traits::construct(allocator(), first++, proto); } @@ -339,7 +339,7 @@ private: // should this be NOINLINE? void construct_non_trivial(iterator first, iterator last) noexcept { - #pragma nounroll + UTILS_NOUNROLL while (first != last) { storage_traits::construct(allocator(), first++); } @@ -355,7 +355,7 @@ private: // should this be NOINLINE? void destroy_non_trivial(iterator first, iterator last) noexcept { - #pragma nounroll + UTILS_NOUNROLL while (first != last) { storage_traits::destroy(allocator(), --last); } diff --git a/libs/utils/include/utils/StructureOfArrays.h b/libs/utils/include/utils/StructureOfArrays.h index 4ee60099ba..7b2d3303df 100644 --- a/libs/utils/include/utils/StructureOfArrays.h +++ b/libs/utils/include/utils/StructureOfArrays.h @@ -525,7 +525,7 @@ private: // hopefully most of this gets unrolled and inlined std::array offsets; offsets[0] = 0; - #pragma unroll + UTILS_UNROLL for (size_t i = 1; i < kArrayCount; i++) { size_t unalignment = (offsets[i - 1] + sizes[i - 1]) % alignments[i]; size_t alignment = unalignment ? (alignments[i] - unalignment) : 0; diff --git a/libs/utils/include/utils/algorithm.h b/libs/utils/include/utils/algorithm.h index 6d12ffc63a..d92555cc99 100644 --- a/libs/utils/include/utils/algorithm.h +++ b/libs/utils/include/utils/algorithm.h @@ -46,10 +46,10 @@ constexpr inline T clz(T x) noexcept { x |= (x >> 4u); x |= (x >> 8u); x |= (x >> 16u); - if (sizeof(T) * CHAR_BIT >= 64) { // just to silence compiler warning + if constexpr (sizeof(T) * CHAR_BIT >= 64) { // just to silence compiler warning x |= (x >> 32u); } - if (sizeof(T) * CHAR_BIT >= 128) { // just to silence compiler warning + if constexpr (sizeof(T) * CHAR_BIT >= 128) { // just to silence compiler warning x |= (x >> 64u); } return T(sizeof(T) * CHAR_BIT) - details::popcount(x); @@ -59,7 +59,13 @@ template::value>> constexpr inline T ctz(T x) noexcept { static_assert(sizeof(T) * CHAR_BIT <= 64, "details::ctz() only support up to 64 bits"); T c = sizeof(T) * CHAR_BIT; - x &= -x; // equivalent to x & (~x + 1) +#if defined(_MSC_VER) + // equivalent to x & -x, but MSVC yield a warning for using unary minus operator on unsigned types + x &= (~x + 1); +#else + // equivalent to x & (~x + 1), but some compilers generate a better sequence on ARM + x &= -x; +#endif if (x) c--; if (sizeof(T) * CHAR_BIT >= 64) { if (x & T(0x00000000FFFFFFFF)) c -= 32; diff --git a/libs/utils/include/utils/compiler.h b/libs/utils/include/utils/compiler.h index 4ada72711a..65b0c4ff11 100644 --- a/libs/utils/include/utils/compiler.h +++ b/libs/utils/include/utils/compiler.h @@ -146,7 +146,7 @@ #define UTILS_PURE #endif -#if __has_attribute(maybe_unused) +#if __has_attribute(maybe_unused) || (defined(_MSC_VER) && _MSC_VER >= 1911) #define UTILS_UNUSED [[maybe_unused]] #define UTILS_UNUSED_IN_RELEASE [[maybe_unused]] #elif __has_attribute(unused) @@ -175,9 +175,11 @@ #if defined(_MSC_VER) // MSVC does not support loop unrolling hints +# define UTILS_UNROLL # define UTILS_NOUNROLL #else // C++11 allows pragmas to be specified as part of defines using the _Pragma syntax. +# define UTILS_UNROLL _Pragma("unroll") # define UTILS_NOUNROLL _Pragma("nounroll") #endif @@ -238,5 +240,18 @@ typedef SSIZE_T ssize_t; #endif +#if defined(_MSC_VER) +# define UTILS_WARNING_PUSH _Pragma("warning( push )") +# define UTILS_WARNING_POP _Pragma("warning( pop )") +# define UTILS_WARNING_ENABLE_PADDED _Pragma("warning(1: 4324)") +#elif defined(__clang__) +# define UTILS_WARNING_PUSH _Pragma("clang diagnostic push") +# define UTILS_WARNING_POP _Pragma("clang diagnostic pop") +# define UTILS_WARNING_ENABLE_PADDED _Pragma("clang diagnostic warning \"-Wpadded\"") +#else +# define UTILS_WARNING_PUSH +# define UTILS_WARNING_POP +# define UTILS_WARNING_ENABLE_PADDED +#endif #endif // TNT_UTILS_COMPILER_H diff --git a/libs/viewer/include/viewer/AutomationEngine.h b/libs/viewer/include/viewer/AutomationEngine.h index 5617d82b6f..a966e753e8 100644 --- a/libs/viewer/include/viewer/AutomationEngine.h +++ b/libs/viewer/include/viewer/AutomationEngine.h @@ -60,7 +60,7 @@ public: * Minimum time that automation waits between applying a settings object and advancing * to the next test case. Specified in seconds. */ - float sleepDuration = 0.2; + float sleepDuration = 0.2f; /** * Similar to sleepDuration, but expressed as a frame count. Both the minimum sleep time