Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
DeterministicSeeder.h
Go to the documentation of this file.
1#pragma once
2
3#include "Utility/utility.h"
4#include "Math/hash.h"
5
6#include <Common/assertion.h>
7#include <Common/primitive_type.h>
8
9#include <atomic>
10#include <climits>
11#include <type_traits>
12
13namespace ph { class EngineInitSettings; }
14
15namespace ph::math
16{
17
22{
23public:
24 template<typename T>
25 static T nextSeed();
26
27 static void init(const EngineInitSettings& settings);
28
29private:
30 static uint32 nextUInt32Number();
31
32 static std::atomic<uint32> s_numberSource;
33 static uint32 s_step;
34};
35
36template<typename T>
38{
39 static_assert(CHAR_BIT == 8);
40
41 // The goal here is to generate values that will not repeat themselves unless this method is
42 // called a large amount of times (e.g., more than 2^32 times), and the values should look
43 // uncorrelated. MurmurHash3 generates unique hash values for <= 32-bit inputs and is used
44 // here (seed values used here are randomly chosen prime numbers).
45
46 const auto number = nextUInt32Number();
47 if constexpr(std::is_same_v<T, uint32>)
48 {
49 return murmur3_32(number, 1236161);
50 }
51 else if constexpr(std::is_same_v<T, uint64>)
52 {
53 const auto lower32 = uint64(murmur3_32(number, 2237617));
54 const auto upper32 = uint64(murmur3_32(number, 3237557)) << 32;
55 return upper32 | lower32;
56 }
57 else
58 {
59 PH_STATIC_ASSERT_DEPENDENT_FALSE(T,
60 "Unsupported seed type `T`. You can provide your own implementation.");
61 }
62}
63
64inline uint32 DeterministicSeeder::nextUInt32Number()
65{
66 PH_ASSERT_NE(s_step, 0);
67
68 return s_numberSource.fetch_add(s_step, std::memory_order_relaxed);
69}
70
71}// end namespace ph::math
Options for initializing core engine. These settings are loaded on engine startup and remains constan...
Definition EngineInitSettings.h:20
Convenient thread-safe seed provider for RNGs. Do not use this for cryptography.
Definition DeterministicSeeder.h:22
static void init(const EngineInitSettings &settings)
Definition DeterministicSeeder.cpp:20
static T nextSeed()
Definition DeterministicSeeder.h:37
Math functions and utilities.
Definition TransformInfo.h:10
uint32 murmur3_32(const T &data, uint32 seed)
Generate 32-bit hash values using MurmurHash3. Note that there are no collisions when T has <= 32 bit...
Definition hash.ipp:119
The root for all renderer implementations.
Definition EEngineProject.h:6