-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathOwenSampler.h
More file actions
102 lines (91 loc) · 3.69 KB
/
OwenSampler.h
File metadata and controls
102 lines (91 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright (C) 2018-2026 - DevSH Graphics Programming Sp. z O.O.
// This file is part of the "Nabla Engine".
// For conditions of distribution and use, see copyright notice in nabla.h
#ifndef _NBL_CORE_CORE_OWEN_SAMPLER_H_
#define _NBL_CORE_CORE_OWEN_SAMPLER_H_
#include "nbl/core/sampling/RandomSampler.h"
#include "nbl/core/sampling/SobolSampler.h"
namespace nbl::core
{
//! TODO: make the tree sampler/generator configurable and let RandomSampler be default
template<class SequenceSampler=SobolSampler>
class OwenSampler final : protected SequenceSampler
{
// if we don't limit the sample count, then due to IEEE754 precision, we'll get duplicate sample coordinate values, ruining the net property
constexpr static inline uint32_t OUT_BITS = sizeof(uint32_t)*8u;
constexpr static inline uint32_t MAX_SAMPLES_LOG2 = 24u;
constexpr static inline uint32_t MAX_SAMPLES = 0x1u<<MAX_SAMPLES_LOG2;
public:
inline OwenSampler(uint32_t _dimensions, uint32_t _seed) : SequenceSampler(_dimensions), seed(_seed) {}
inline ~OwenSampler() = default;
struct SDimensionSampler final : public core::Unmovable
{
inline uint32_t sample(uint32_t sampleNum) const
{
const uint32_t oldsample = sampler.sample(dimension,sampleNum);
#ifdef _NBL_DEBUG
assert(sampleNum<MAX_SAMPLES);
if (sampleNum)
assert((oldsample&(0x7fffffffu>>hlsl::findMSB(sampleNum))) == 0u);
else
assert(oldsample == 0u);
#endif
constexpr uint32_t lastLevelStart = MAX_SAMPLES/2u-1u;
uint32_t index = oldsample>>(OUT_BITS+1u - MAX_SAMPLES_LOG2);
index += lastLevelStart;
return oldsample^cachedFlip[index];
}
private:
friend class OwenSampler;
inline SDimensionSampler(const SequenceSampler& _sampler, const uint32_t seed, const uint32_t _dimension) : sampler(_sampler), dimension(_dimension),
mersenneTwister(std::hash<uint64_t>()((uint64_t(_dimension)<<32)|seed))
{
cachedFlip.resize(MAX_SAMPLES-1u);
/** NOTES:
- For 64k samples, we can store their positions in uint16_t
- The last leves of Owen Tree can be collapsed to a single node (because trailing bits are always 00000.....)
- The above can be stored in 1x array of sample count uint16_t/uint32_t per Dimension
- We should store samples as uint32_t always because the total amount of memory to fetch is always the same
**/
for (uint32_t i=0u; i<MAX_SAMPLES-1u; i++)
{
uint32_t randMask = (i<(MAX_SAMPLES/2u-1u)) ? 0x80000000u:0xffffffffu;
cachedFlip[i] = mersenneTwister()&(randMask>>getTreeDepth(i));
}
for (uint32_t i=1u; i<MAX_SAMPLES_LOG2; i++)
{
uint32_t previousLevelStart = (0x1u<<(i-1u))-1u;
uint32_t currentLevelStart = (0x1u<<i)-1u;
uint32_t currentLevelSize = 0x1u<<i;
for (uint32_t j=0u; j<currentLevelSize; j++)
cachedFlip[currentLevelStart+j] |= cachedFlip[previousLevelStart+(j>>1u)];
#ifdef _NBL_DEBUG
for (uint32_t j=0u; j<currentLevelSize; j+=2)
{
const uint32_t highBitMask = 0xffffffffu<<(OUT_BITS-i);
uint32_t left = cachedFlip[currentLevelStart+j];
uint32_t right = cachedFlip[currentLevelStart+j+1];
assert(((left^right)&highBitMask)==0u);
assert((left&right&highBitMask)==cachedFlip[previousLevelStart+(j>>1u)]);
}
#endif
}
}
inline uint32_t getTreeDepth(uint32_t sampleNum)
{
return hlsl::findMSB(sampleNum+1u);
}
const SequenceSampler& sampler;
std::mt19937 mersenneTwister;
core::vector<uint32_t> cachedFlip;
const uint32_t dimension;
};
inline SDimensionSampler prepareDimension(const uint64_t dim) const
{
return SDimensionSampler(*this,seed,dim);
}
private:
uint32_t seed;
};
}
#endif