MUDA
Loading...
Searching...
No Matches
morton_code.h
1#pragma once
2#include <vector_types.h>
3#include <cuda_runtime.h>
4#include <cstdint>
5
6namespace muda::lbvh
7{
8MUDA_GENERIC MUDA_INLINE std::uint32_t expand_bits(std::uint32_t v) noexcept
9{
10 v = (v * 0x00010001u) & 0xFF0000FFu;
11 v = (v * 0x00000101u) & 0x0F00F00Fu;
12 v = (v * 0x00000011u) & 0xC30C30C3u;
13 v = (v * 0x00000005u) & 0x49249249u;
14 return v;
15}
16
17// Calculates a 30-bit Morton code for the
18// given 3D point located within the unit cube [0,1].
19MUDA_GENERIC MUDA_INLINE std::uint32_t morton_code(float4 xyz, float resolution = 1024.0f) noexcept
20{
21 xyz.x = ::fminf(::fmaxf(xyz.x * resolution, 0.0f), resolution - 1.0f);
22 xyz.y = ::fminf(::fmaxf(xyz.y * resolution, 0.0f), resolution - 1.0f);
23 xyz.z = ::fminf(::fmaxf(xyz.z * resolution, 0.0f), resolution - 1.0f);
24 const std::uint32_t xx = expand_bits(static_cast<std::uint32_t>(xyz.x));
25 const std::uint32_t yy = expand_bits(static_cast<std::uint32_t>(xyz.y));
26 const std::uint32_t zz = expand_bits(static_cast<std::uint32_t>(xyz.z));
27 return xx * 4 + yy * 2 + zz;
28}
29
30MUDA_GENERIC MUDA_INLINE std::uint32_t morton_code(double4 xyz, double resolution = 1024.0) noexcept
31{
32 xyz.x = ::fmin(::fmax(xyz.x * resolution, 0.0), resolution - 1.0);
33 xyz.y = ::fmin(::fmax(xyz.y * resolution, 0.0), resolution - 1.0);
34 xyz.z = ::fmin(::fmax(xyz.z * resolution, 0.0), resolution - 1.0);
35 const std::uint32_t xx = expand_bits(static_cast<std::uint32_t>(xyz.x));
36 const std::uint32_t yy = expand_bits(static_cast<std::uint32_t>(xyz.y));
37 const std::uint32_t zz = expand_bits(static_cast<std::uint32_t>(xyz.z));
38 return xx * 4 + yy * 2 + zz;
39}
40
41__device__ MUDA_INLINE int common_upper_bits(const unsigned int lhs, const unsigned int rhs) noexcept
42{
43 return ::__clz(lhs ^ rhs);
44}
45__device__ MUDA_INLINE int common_upper_bits(const unsigned long long int lhs,
46 const unsigned long long int rhs) noexcept
47{
48 return ::__clzll(lhs ^ rhs);
49}
50
51} // namespace muda::lbvh