OpenQMC API
Loading...
Searching...
No Matches
state.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright Contributors to the OpenQMC Project.
3
6
7#pragma once
8
9#include "encode.h"
10#include "gpu.h"
11#include "pcg.h"
12
13#include <cassert>
14#include <cstdint>
15
16namespace oqmc
17{
18
28{
29 static constexpr auto maxIndexBitSize = 16;
30 static constexpr auto maxIndexSize = 1 << 16;
31 static constexpr auto spatialEncodeBitSizeX = 8;
32 static constexpr auto spatialEncodeBitSizeY = 8;
33
35 "Encoding must have equal resolution in x and y");
36
42 /*AUTO_DEFINED*/ State64Bit() = default;
43
54 OQMC_HOST_DEVICE State64Bit(int x, int y, int frame, int index);
55
64
67
70 int index) const;
71
73 OQMC_HOST_DEVICE State64Bit newDomainDistrib(int key, int index) const;
74
76 template <int Size>
77 OQMC_HOST_DEVICE void drawRnd(std::uint32_t rnd[Size]) const;
78
79 std::uint32_t patternId;
80 std::uint16_t sampleId;
81 std::uint16_t pixelId;
82};
83
92OQMC_HOST_DEVICE constexpr int computeIndexKey(int index)
93{
94 constexpr auto offset = State64Bit::maxIndexBitSize;
95 return index >> offset;
96}
97
106OQMC_HOST_DEVICE constexpr int computeIndexId(int index)
107{
108 constexpr auto mask = State64Bit::maxIndexSize - 1;
109 return index & mask;
110}
111
112inline State64Bit::State64Bit(int x, int y, int frame, int index)
113{
114 assert(index >= 0);
115
116 const auto indexKey = computeIndexKey(index);
117 const auto indexId = computeIndexId(index);
118
119 constexpr auto xBits = State64Bit::spatialEncodeBitSizeX;
120 constexpr auto yBits = State64Bit::spatialEncodeBitSizeY;
121
122 const auto pixelId = encodeBits16<xBits, yBits, 0>({x, y, 0});
123
124 this->patternId = pcg::init(frame + indexKey);
125 this->sampleId = indexId;
126 this->pixelId = pixelId;
127}
128
130{
131 return newDomain(pixelId);
132}
133
135{
136 auto ret = *this;
137 ret.patternId = pcg::stateTransition(patternId + key);
138
139 return ret;
140}
141
142inline State64Bit State64Bit::newDomainSplit(int key, int size, int index) const
143{
144 assert(size > 0);
145 assert(index >= 0);
146
147 const auto indexKey = computeIndexKey(sampleId * size + index);
148 const auto indexId = computeIndexId(sampleId * size + index);
149
150 auto ret = newDomain(key).newDomain(indexKey);
151 ret.sampleId = indexId;
152
153 return ret;
154}
155
156inline State64Bit State64Bit::newDomainDistrib(int key, int index) const
157{
158 assert(index >= 0);
159
160 const auto indexKey = computeIndexKey(index);
161 const auto indexId = computeIndexId(index);
162
164 ret.sampleId = indexId;
165
166 return ret;
167}
168
169template <int Size>
170void State64Bit::drawRnd(std::uint32_t rnd[Size]) const
171{
172 static_assert(Size >= 0, "Draw size greater or equal to zero.");
173
174 auto rngState = patternId + sampleId;
175
176 for(int i = 0; i < Size; ++i)
177 {
179 }
180}
181
182static_assert(sizeof(State64Bit) == 8, "State64Bit must be 8 bytes in size.");
183
184} // namespace oqmc
#define OQMC_HOST_DEVICE
Definition gpu.h:13
constexpr std::uint32_t stateTransition(std::uint32_t state)
State transition function.
Definition pcg.h:79
constexpr std::uint32_t init()
Default initialise the PRNG state.
Definition pcg.h:117
constexpr std::uint32_t rng(std::uint32_t &state)
Compute a random number from the PRNG sequence.
Definition pcg.h:162
EncodeKey decodeBits16(std::uint16_t value)
Decode a value back into a key.
Definition encode.h:81
Definition bntables.h:21
constexpr int computeIndexKey(int index)
Compute 16-bit key from index.
Definition state.h:92
constexpr int computeIndexId(int index)
Compute new 16-bit index from index.
Definition state.h:106
Generic sampler state type.
Definition state.h:28
static constexpr auto spatialEncodeBitSizeY
256 pixels in y.
Definition state.h:32
State64Bit()=default
Construct an invalid object.
static constexpr auto maxIndexSize
2^16 index upper limit.
Definition state.h:30
std::uint16_t sampleId
Identifier for sample index.
Definition state.h:80
std::uint16_t pixelId
Identifier for pixel position.
Definition state.h:81
State64Bit newDomainDistrib(int key, int index) const
Derive a split sampler object with a local distribution.
Definition state.h:156
State64Bit newDomainSplit(int key, int size, int index) const
Derive a split sampler object with a local and a global distribution.
Definition state.h:142
void drawRnd(std::uint32_t rnd[Size]) const
Draw integer pseudo random values from domain.
Definition state.h:170
std::uint32_t patternId
Identifier for domain pattern.
Definition state.h:79
static constexpr auto spatialEncodeBitSizeX
256 pixels in x.
Definition state.h:31
static constexpr auto maxIndexBitSize
2^16 index upper limit.
Definition state.h:29
State64Bit newDomain(int key) const
Derive a sampler object as a new domain.
Definition state.h:134
State64Bit pixelDecorrelate() const
Decorrelate state between pixels.
Definition state.h:129