RenderKit / embree

Embree ray tracing kernels repository.
Apache License 2.0
2.32k stars 383 forks source link

Add alignment definitions to RTCHitNt and RTCRayNt #472

Closed dbs4261 closed 1 month ago

dbs4261 commented 5 months ago

Alignment definitions should be added to RTCHitNt and RTCRayNt templates so they match the C definitions provided. The following static assertions demonstrate the issues with using the RTCRayNt templates as they all fail because RTCRayNt has an alignment of 4, equivalent to alignof(float).

static_assert(alignof(embree::RTCRayNt<1>) == alignof(embree::RTCRay));
static_assert(alignof(embree::RTCRayNt<4>) == alignof(embree::RTCRay4));
static_assert(alignof(embree::RTCRayNt<8>) == alignof(embree::RTCRay8));
static_assert(alignof(embree::RTCRayNt<16>) == alignof(embree::RTCRay16));

As the alignment calculation in the PR is nasty as it avoids requiring the alignof keyword, or constexpr functions, this could instead be hidden behind a macro. Maybe RTC_ALIGN_SOA_AT_LEAST(N, MIN)?

The code is equivalent to:

constexpr std::size_t AlignSOAAtLeast(unsigned N, std::size_t min_alignment) noexcept {
    if (std::has_single_bit<unsigned>(N)) {
        return std::max<std::size_t>(min_alignment, N * alignof(float));
    } else {
        return min_alignment;
    }
}
freibold commented 1 month ago

Thanks for the PR! I've merged this into the devel branch manually https://github.com/RenderKit/embree/commit/819aedb442fc05e5b0bacf03208b9279ab207e0b