Skip to content

Commit

Permalink
fix fuzzyEqual
Browse files Browse the repository at this point in the history
- the return value was inverted
- fuzzyEqual could generate alignment faults
- move it out of mat4 and mat2 because it was
  only used in one place.
  • Loading branch information
pixelflinger committed Aug 15, 2023
1 parent 96ed195 commit 132bb7a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 38 deletions.
20 changes: 17 additions & 3 deletions filament/src/Froxelizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ struct Froxelizer::FroxelThreadData :
public std::array<LightGroupType, FROXEL_BUFFER_MAX_ENTRY_COUNT> {
};


// Returns false if the two matrices are different. May return false if they're the
// same, with some elements only differing by +0 or -0. Behaviour is undefined with NaNs.
static bool fuzzyEqual(mat4f const& UTILS_RESTRICT l, mat4f const& UTILS_RESTRICT r) noexcept {
auto const li = reinterpret_cast<uint32_t const*>( reinterpret_cast<char const*>(&l) );
auto const ri = reinterpret_cast<uint32_t const*>( reinterpret_cast<char const*>(&r) );
uint32_t result = 0;
for (size_t i = 0; i < sizeof(mat4f) / sizeof(uint32_t); i++) {
// clang fully vectorizes this
result |= li[i] ^ ri[i];
}
return result == 0;
}


Froxelizer::Froxelizer(FEngine& engine)
: mArena("froxel", PER_FROXELDATA_ARENA_SIZE),
mZLightNear(FROXEL_FIRST_SLICE_DEPTH),
Expand Down Expand Up @@ -144,9 +159,8 @@ void Froxelizer::setViewport(filament::Viewport const& viewport) noexcept {
}

void Froxelizer::setProjection(const mat4f& projection,
float near,
UTILS_UNUSED float far) noexcept {
if (UTILS_UNLIKELY(mat4f::fuzzyEqual(mProjection, projection))) {
float near, UTILS_UNUSED float far) noexcept {
if (UTILS_UNLIKELY(!fuzzyEqual(mProjection, projection))) {
mProjection = projection;
mNear = near;
mDirtyFlags |= PROJECTION_CHANGED;
Expand Down
17 changes: 0 additions & 17 deletions libs/math/include/math/mat2.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,23 +235,6 @@ class MATH_EMPTY_BASES TMat22 :
return r;
}

// returns false if the two matrices are different. May return false if they're the
// same, with some elements only differing by +0 or -0. Behaviour is undefined with NaNs.
static constexpr bool fuzzyEqual(TMat22 l, TMat22 r) noexcept {
uint64_t const* const li = reinterpret_cast<uint64_t const*>(&l);
uint64_t const* const ri = reinterpret_cast<uint64_t const*>(&r);
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];
}
return result != 0;
}

template<typename A>
static constexpr TMat22 translation(const TVec2<A>& t) noexcept {
TMat22 r;
Expand Down
18 changes: 0 additions & 18 deletions libs/math/include/math/mat4.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,24 +272,6 @@ class MATH_EMPTY_BASES TMat44 :
template<typename U, typename V>
constexpr TMat44(const TMat33<U>& matrix, const TVec4<V>& column3) noexcept;

/*
* helpers
*/

// returns false if the two matrices are different. May return false if they're the
// same, with some elements only differing by +0 or -0. Behaviour is undefined with NaNs.
static constexpr bool fuzzyEqual(TMat44 const& l, TMat44 const& r) noexcept {
uint64_t const* const li = reinterpret_cast<uint64_t const*>(&l);
uint64_t const* const ri = reinterpret_cast<uint64_t const*>(&r);
uint64_t result = 0;
// For some reason clang is not able to vectorize this loop when the number of iteration
// is known and constant (!?!?!). Still this is better than operator==.
for (size_t i = 0; i < sizeof(TMat44) / sizeof(uint64_t); i++) {
result |= li[i] ^ ri[i];
}
return result != 0;
}

static constexpr TMat44 ortho(T left, T right, T bottom, T top, T near, T far) noexcept;

static constexpr TMat44 frustum(T left, T right, T bottom, T top, T near, T far) noexcept;
Expand Down

0 comments on commit 132bb7a

Please sign in to comment.