forked from microsoft/Xbox-ATG-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSampler.cpp
62 lines (50 loc) · 1.61 KB
/
Sampler.cpp
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
//--------------------------------------------------------------------------------------
// D3D12RaytracingAO.h
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "Sampler.h"
using namespace DirectX;
// Random generator.
std::random_device Sampler::m_randomDevice;
std::mt19937 Sampler::m_generator(m_randomDevice());
// [0, 1] distribution.
std::uniform_real_distribution<float> Sampler::m_dis(
0.f, std::nextafter(1.f, std::numeric_limits<float>::max()));
// [-1, 1] distribution.
std::uniform_real_distribution<float> Sampler::m_signedDis(
-1.f, std::nextafter(1.f, std::numeric_limits<float>::max()));
// Choose a float with uniform distribution from [0, 1].
float Sampler::RandFloat()
{
return m_dis(m_generator);
}
// Choose a float with uniform distribution from [-1, 1].
float Sampler::RandSignedFloat()
{
return m_signedDis(m_generator);
}
void Sampler::SetSeed(unsigned int seed)
{
m_generator = std::mt19937(seed);
}
// Default Sample.
XMFLOAT3 Sampler::Sample()
{
return Sample(RandFloat(), RandFloat(), RandFloat());
}
// Generate a set of samples.
// Note that numSamples does not necessarily
// equal the number of data points returned (varies based on sample method).
// float4 is used for packing reasons.
void Sampler::Sample(XMFLOAT4* data, unsigned int numSamples)
{
XMFLOAT3 sample;
for (unsigned int i = 0; i < numSamples; i++)
{
sample = Sample();
data[i] = { sample.x, sample.y, sample.z, 0 };
}
}