Skip to content

Commit

Permalink
Align Fragment allocation in AVX builder.
Browse files Browse the repository at this point in the history
Not doing so results in immediate crashes on Linux.

Don't explicitly align Fragment allocation on MSVC,
since the code apparently works anyway.
  • Loading branch information
eloj committed Nov 8, 2024
1 parent b38d6e2 commit a137a79
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions tiny_bvh.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ THE SOFTWARE.
#define ALIGNED( x ) __declspec( align( x ) )
#define ALIGNED_MALLOC( x ) ( ( x ) == 0 ? 0 : _aligned_malloc( ( x ), 64 ) )
#define ALIGNED_FREE( x ) _aligned_free( x )
#define OPERATOR_NEW_ALIGN(x)
#define OPERATOR_DELETE_ALIGN(x)
#else
// gcc / clang
#include <cstdlib>
Expand All @@ -106,6 +108,8 @@ THE SOFTWARE.
#define ALIGNED_MALLOC( x ) ( ( x ) == 0 ? 0 : aligned_alloc( 64, ( x ) ) )
#define ALIGNED_FREE( x ) free( x )
#endif
#define OPERATOR_NEW_ALIGN(x) (std::align_val_t(x))
#define OPERATOR_DELETE_ALIGN(x) ,(std::align_val_t(x))
#endif
// generic
#ifdef BVH_USEAVX
Expand Down Expand Up @@ -335,7 +339,7 @@ class BVH
{
ALIGNED_FREE( bvhNode );
delete[] triIdx;
delete[] fragment;
operator delete[](fragment OPERATOR_DELETE_ALIGN(32));
bvhNode = 0, triIdx = 0, fragment = 0;
}
float SAHCost( const unsigned int nodeIdx = 0 ) const
Expand Down Expand Up @@ -1012,7 +1016,7 @@ void BVH::BuildAVX( const bvhvec4* vertices, const unsigned int primCount )
triIdx = new unsigned int[triCount];
bvhNode = (BVHNode*)ALIGNED_MALLOC( triCount * 2 * sizeof( BVHNode ) );
memset( &bvhNode[1], 0, 32 ); // avoid crash in refit.
fragment = new Fragment[triCount];
fragment = new OPERATOR_NEW_ALIGN(32) Fragment[triCount];
idxCount = triCount;
}
else assert( triCount == primCount ); // don't change triangle count between builds.
Expand Down Expand Up @@ -1433,4 +1437,4 @@ int BVH::Intersect_AltSoA( Ray& ray ) const

#endif // TINYBVH_IMPLEMENTATION

#endif // TINY_BVH_H_
#endif // TINY_BVH_H_

0 comments on commit a137a79

Please sign in to comment.