-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampler.cppm
More file actions
72 lines (59 loc) · 1.72 KB
/
Copy pathSampler.cppm
File metadata and controls
72 lines (59 loc) · 1.72 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
module;
#include <algorithm>
#include <cmath>
#include <cstdint>
export module Kairo.Foundation.RayTracer.Sampler;
export namespace kairo::foundation::raytracer
{
struct PixelSample final
{
float dx = 0.5f;
float dy = 0.5f;
};
[[nodiscard]]
inline float Hash01(
std::uint32_t value) noexcept
{
value ^= value >> 16u;
value *= 0x7feb352du;
value ^= value >> 15u;
value *= 0x846ca68bu;
value ^= value >> 16u;
return static_cast<float>(value & 0x00ffffffu) /
static_cast<float>(0x01000000u);
}
[[nodiscard]]
inline PixelSample StratifiedPixelSample(
std::uint32_t x,
std::uint32_t y,
std::uint32_t sampleIndex,
std::uint32_t samplesPerPixel,
std::uint32_t seedOffset = 0) noexcept
{
if (samplesPerPixel <= 1u)
{
return {};
}
const std::uint32_t side =
static_cast<std::uint32_t>(
std::ceil(std::sqrt(static_cast<float>(samplesPerPixel))));
const std::uint32_t sx =
sampleIndex % side;
const std::uint32_t sy =
sampleIndex / side;
const std::uint32_t seed =
x * 73856093u ^
y * 19349663u ^
sampleIndex * 83492791u ^
seedOffset * 2654435761u;
const float jitterX =
Hash01(seed);
const float jitterY =
Hash01(seed ^ 0x9e3779b9u);
return
{
std::clamp((static_cast<float>(sx) + jitterX) / static_cast<float>(side), 0.0f, 0.999999f),
std::clamp((static_cast<float>(sy) + jitterY) / static_cast<float>(side), 0.0f, 0.999999f)
};
}
}