37 #ifndef RixRNGProgressive_h
38 #define RixRNGProgressive_h
76 #define N_PMJ_SEQS (256*256) // number of different shuffles+scrambles of pmj sequences
77 #define N_PMJ_SAMPS 4096 // number of samples in the pmj sequence
89 PRMAN_INLINE
static float
90 HashToRandom(
const unsigned value,
const unsigned scramble)
93 unsigned result = value;
95 result ^= result >> 17;
96 result ^= result >> 10; result *= 0xb36534e5;
97 result ^= result >> 12;
98 result ^= result >> 21; result *= 0x93fc4795;
100 result ^= result >> 17; result *= 1 | scramble >> 18;
101 return static_cast<float>(result) / 4298115584.0f;
109 PRMAN_INLINE
static unsigned int
110 HashToRandomUInt(
unsigned int const value,
unsigned int const scramble)
113 unsigned int result = value;
115 result ^= result >> 17;
116 result ^= result >> 10; result *= 0xb36534e5;
117 result ^= result >> 12;
118 result ^= result >> 21; result *= 0x93fc4795;
119 result ^= 0xdf6e307f;
120 result ^= result >> 17; result *= 1 | scramble >> 18;
138 PRMAN_INLINE
unsigned int
139 shuffle(
const unsigned int pattern,
140 const unsigned int sample)
const
143 unsigned short* shuffleTable = (
unsigned short*)tables[2];
144 unsigned int t1, t2, s;
148 if (sample < 4)
return sample;
151 t2 = (pattern >> 8) & 0xff;
152 assert(sample < nSamps);
162 s = shuffleTable[t1 * nSamps + s];
163 s = shuffleTable[t2 * nSamps + s];
165 assert(s/64 == sample/64);
172 PRMAN_INLINE
static float
173 fpScramble(
float f,
unsigned int scramble)
181 f2i.i ^= (scramble >> 9);
189 PRMAN_INLINE
float progressive1DSample(
190 const unsigned int pattern,
191 const unsigned int sample)
const
194 float* sampleTable = (
float*)tables[0];
195 unsigned int* shuffleScrambleTable = (
unsigned int*)tables[1];
197 unsigned int t, shift, s, ss;
198 unsigned int shuffleBitsZ, scrambleBitsZ;
202 if (sample > nSeqs * nSamps)
206 result = HashToRandom(sample, pattern * 0x51633e2d);
215 shift = pattern & 0xffff;
220 t += 2579 * (sample / nSamps);
226 s = shuffle(pattern, s);
229 shuffleBitsZ = shuffleScrambleTable[5*t+3];
230 scrambleBitsZ = shuffleScrambleTable[5*t+4];
233 assert(shuffleBitsZ < 64);
234 ss = s ^ shuffleBitsZ;
238 result = sampleTable[3*ss+2];
239 assert(0.0f <= result && result < 1.0f);
242 result = fpScramble(result, scrambleBitsZ);
248 result = std::min(result, 0.999990f);
249 assert(0.0f <= result && result < 1.0f);
260 PRMAN_INLINE RtFloat2 progressive2DSample(
261 const unsigned int pattern,
262 const unsigned int sample)
const
265 float* sampleTable = (
float*)tables[0];
266 unsigned int* shuffleScrambleTable = (
unsigned int*)tables[1];
268 unsigned int t, shift, s, ss;
269 unsigned int shuffleBits, scrambleBitsX, scrambleBitsY;
273 if (sample > nSeqs * nSamps)
277 result.x = HashToRandom(sample, pattern * 0x51633e2d);
278 result.y = HashToRandom(sample, pattern * 0x68bc21eb);
287 shift = pattern & 0xffff;
292 t += 2579 * (sample / nSamps);
303 s = shuffle(pattern, s);
306 shuffleBits = shuffleScrambleTable[5*t];
307 scrambleBitsX = shuffleScrambleTable[5*t+1];
308 scrambleBitsY = shuffleScrambleTable[5*t+2];
311 assert(shuffleBits < 256);
312 ss = s ^ shuffleBits;
316 result.x = sampleTable[3*ss];
317 result.y = sampleTable[3*ss+1];
318 assert(0.0f <= result.x && result.x < 1.0f);
319 assert(0.0f <= result.y && result.y < 1.0f);
322 result.x = fpScramble(result.x, scrambleBitsX);
323 result.y = fpScramble(result.y, scrambleBitsY);
332 result.x = std::min(result.x, 0.999990f);
333 result.y = std::min(result.y, 0.999990f);
334 assert(0.0f <= result.x && result.x < 1.0f);
335 assert(0.0f <= result.y && result.y < 1.0f);
347 PRMAN_INLINE RtFloat2 progressive2DScrambledSample(
348 const unsigned int pattern,
349 const unsigned int sample)
const
352 float* sampleTable = (
float*)tables[0];
353 unsigned int* shuffleScrambleTable = (
unsigned int*)tables[1];
355 unsigned int t, s, ss;
356 unsigned int shuffleBits, pattern1, pattern2;
360 if (sample > nSeqs * nSamps)
364 result.x = HashToRandom(sample, pattern * 0x51633e2d);
365 result.y = HashToRandom(sample, pattern * 0x68bc21eb);
375 t += 2579 * (sample / nSamps);
386 s = shuffle(pattern, s);
389 shuffleBits = shuffleScrambleTable[5*t];
392 ss = s ^ shuffleBits;
396 result.x = sampleTable[3*ss];
397 result.y = sampleTable[3*ss+1];
398 assert(0.0f <= result.x && result.x < 1.0f);
399 assert(0.0f <= result.y && result.y < 1.0f);
402 pattern1 = HashToRandomUInt(pattern, 0x51633e2d);
403 pattern2 = HashToRandomUInt(pattern, 0x68bc21eb);
404 result.x = fpScramble(result.x, pattern1);
405 result.y = fpScramble(result.y, pattern2);
408 result.x = std::min(result.x, 0.999990f);
409 result.y = std::min(result.y, 0.999990f);
410 assert(0.0f <= result.x && result.x < 1.0f);
411 assert(0.0f <= result.y && result.y < 1.0f);
420 PRMAN_INLINE RtFloat3 progressive3DSample(
421 const unsigned int pattern,
422 const unsigned int sample)
const
425 float* sampleTable = (
float*)tables[0];
426 unsigned int* shuffleScrambleTable = (
unsigned int*)tables[1];
428 unsigned int t, shift, s, ss, sz;
429 unsigned int shuffleBits, scrambleBitsX, scrambleBitsY, shuffleBitsZ, scrambleBitsZ;
433 if (sample > nSeqs * nSamps)
437 result.x = HashToRandom(sample, pattern * 0x51633e2d);
438 result.y = HashToRandom(sample, pattern * 0x68bc21eb);
439 result.z = HashToRandom(sample, pattern * 0x02e5be93);
448 shift = pattern & 0xffff;
453 t += 2579 * (sample / nSamps);
459 s = shuffle(pattern, s);
462 shuffleBits = shuffleScrambleTable[5*t];
463 scrambleBitsX = shuffleScrambleTable[5*t+1];
464 scrambleBitsY = shuffleScrambleTable[5*t+2];
465 shuffleBitsZ = shuffleScrambleTable[5*t+3];
466 scrambleBitsZ = shuffleScrambleTable[5*t+4];
469 ss = s ^ shuffleBits;
471 sz = s ^ shuffleBitsZ;
475 result.x = sampleTable[3*ss];
476 result.y = sampleTable[3*ss+1];
477 result.z = sampleTable[3*sz+2];
478 assert(0.0f <= result.x && result.x < 1.0f);
479 assert(0.0f <= result.y && result.y < 1.0f);
480 assert(0.0f <= result.z && result.z < 1.0f);
483 result.x = fpScramble(result.x, scrambleBitsX);
484 result.y = fpScramble(result.y, scrambleBitsY);
485 result.z = fpScramble(result.z, scrambleBitsZ);
493 result.x = std::min(result.x, 0.999990f);
494 result.y = std::min(result.y, 0.999990f);
495 result.z = std::min(result.z, 0.999990f);
496 assert(0.0f <= result.x && result.x < 1.0f);
497 assert(0.0f <= result.y && result.y < 1.0f);
498 assert(0.0f <= result.z && result.z < 1.0f);
509 virtual float Sample1D(
const SampleCtx& rctx,
unsigned i)
const
512 return progressive1DSample(rctx.patternid, rctx.sampleid);
515 virtual RtFloat2
Sample2D(
const SampleCtx& rctx,
unsigned i)
const
518 return progressive2DSample(rctx.patternid, rctx.sampleid);
524 return progressive2DScrambledSample(rctx.patternid, rctx.sampleid);
527 virtual RtFloat3
Sample3D(
const SampleCtx& rctx,
unsigned i)
const
530 return progressive3DSample(rctx.patternid, rctx.sampleid);
538 const SampleCtx* rctx,
541 for (
unsigned int i = 0; i < n; ++i)
543 xis[i] = progressive1DSample(rctx[i].patternid, rctx[i].sampleid);
549 const SampleCtx* rctx,
552 for (
unsigned int i = 0; i < n; ++i)
554 xis[i] = progressive2DSample(rctx[i].patternid, rctx[i].sampleid);
560 const SampleCtx* rctx,
563 for (
unsigned int i = 0; i < n; ++i)
565 xis[i] = progressive2DScrambledSample(rctx[i].patternid, rctx[i].sampleid);
571 const SampleCtx* rctx,
574 for (
unsigned int i = 0; i < n; ++i)
576 xis[i] = progressive3DSample(rctx[i].patternid, rctx[i].sampleid);