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 103 104
|
// Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "../math/vec.isph"
struct RandomSampler
{
unsigned int s;
};
inline unsigned int MurmurHash3_mix(unsigned int hash, unsigned int k)
{
const unsigned int c1 = 0xcc9e2d51;
const unsigned int c2 = 0x1b873593;
const unsigned int r1 = 15;
const unsigned int r2 = 13;
const unsigned int m = 5;
const unsigned int n = 0xe6546b64;
k *= c1;
k = (k << r1) | (k >> (32 - r1));
k *= c2;
hash ^= k;
hash = ((hash << r2) | (hash >> (32 - r2))) * m + n;
return hash;
}
inline unsigned int MurmurHash3_finalize(unsigned int hash)
{
hash ^= hash >> 16;
hash *= 0x85ebca6b;
hash ^= hash >> 13;
hash *= 0xc2b2ae35;
hash ^= hash >> 16;
return hash;
}
inline unsigned int LCG_next(unsigned int value)
{
const unsigned int m = 1664525;
const unsigned int n = 1013904223;
return value * m + n;
}
inline void RandomSampler_init(RandomSampler& self, int id)
{
unsigned int hash = 0;
hash = MurmurHash3_mix(hash, id);
hash = MurmurHash3_finalize(hash);
self.s = hash;
}
inline void RandomSampler_init(RandomSampler& self, int pixelId, int sampleId)
{
unsigned int hash = 0;
hash = MurmurHash3_mix(hash, pixelId);
hash = MurmurHash3_mix(hash, sampleId);
hash = MurmurHash3_finalize(hash);
self.s = hash;
}
inline void RandomSampler_init(RandomSampler& self, int x, int y, int sampleId)
{
RandomSampler_init(self, x | (y << 16), sampleId);
}
inline int RandomSampler_getInt(RandomSampler& self) {
self.s = LCG_next(self.s); return self.s >> 1;
}
inline unsigned int RandomSampler_getUInt(RandomSampler& self) {
self.s = LCG_next(self.s); return self.s;
}
inline float RandomSampler_getFloat(RandomSampler& self) {
return (float)RandomSampler_getInt(self) * 4.656612873077392578125e-10f;
}
inline float RandomSampler_get1D(RandomSampler& self) {
return RandomSampler_getFloat(self);
}
inline Vec2f RandomSampler_get2D(RandomSampler& self)
{
const float u = RandomSampler_get1D(self);
const float v = RandomSampler_get1D(self);
return make_Vec2f(u,v);
}
inline Vec3fa RandomSampler_get3D(RandomSampler& self)
{
const float u = RandomSampler_get1D(self);
const float v = RandomSampler_get1D(self);
const float w = RandomSampler_get1D(self);
return make_Vec3fa(u,v,w);
}
|