OWL
OptiX7 Wrapper Library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
random.h
Go to the documentation of this file.
1 // ======================================================================== //
2 // Copyright 2018-2020 Ingo Wald //
3 // //
4 // Licensed under the Apache License, Version 2.0 (the "License"); //
5 // you may not use this file except in compliance with the License. //
6 // You may obtain a copy of the License at //
7 // //
8 // http://www.apache.org/licenses/LICENSE-2.0 //
9 // //
10 // Unless required by applicable law or agreed to in writing, software //
11 // distributed under the License is distributed on an "AS IS" BASIS, //
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
13 // See the License for the specific language governing permissions and //
14 // limitations under the License. //
15 // ======================================================================== //
16 
17 /* pieces originally taken from optixPathTracer/random.h example,
18  under following license */
19 
20 /*
21  * Copyright (c) 2018 NVIDIA CORPORATION. All rights reserved.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * * Redistributions of source code must retain the above copyright
27  * notice, this list of conditions and the following disclaimer.
28  * * Redistributions in binary form must reproduce the above copyright
29  * notice, this list of conditions and the following disclaimer in the
30  * documentation and/or other materials provided with the distribution.
31  * * Neither the name of NVIDIA CORPORATION nor the names of its
32  * contributors may be used to endorse or promote products derived
33  * from this software without specific prior written permission.
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
36  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
39  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
40  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
41  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
42  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
43  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46  */
47 
48 #pragma once
49 
50 #include "owl/common/math/vec.h"
51 
52 namespace owl {
53  namespace common {
54 
56  template<unsigned int N=4>
57  struct LCG {
58 
59  inline __both__ LCG()
60  { /* intentionally empty so we can use it in device vars that
61  don't allow dynamic initialization (ie, PRD) */
62  }
63  inline __both__ LCG(unsigned int val0, unsigned int val1)
64  { init(val0,val1); }
65 
66  inline __both__ LCG(const vec2i &seed)
67  { init((unsigned)seed.x,(unsigned)seed.y); }
68  inline __both__ LCG(const vec2ui &seed)
69  { init(seed.x,seed.y); }
70 
71  inline __both__ void init(unsigned int val0, unsigned int val1)
72  {
73  unsigned int v0 = val0;
74  unsigned int v1 = val1;
75  unsigned int s0 = 0;
76 
77  for (unsigned int n = 0; n < N; n++) {
78  s0 += 0x9e3779b9;
79  v0 += ((v1<<4)+0xa341316c)^(v1+s0)^((v1>>5)+0xc8013ea4);
80  v1 += ((v0<<4)+0xad90777d)^(v0+s0)^((v0>>5)+0x7e95761e);
81  }
82  state = v0;
83  }
84 
85  // Generate random unsigned int in [0, 2^24)
86  inline __both__ float operator() ()
87  {
88  const uint32_t LCG_A = 1664525u;
89  const uint32_t LCG_C = 1013904223u;
90  state = (LCG_A * state + LCG_C);
91  return ldexpf(float(state), -32);
92  // return (state & 0x00FFFFFF) / (float) 0x01000000;
93  }
94 
95  uint32_t state;
96  };
97 
98 
102  struct DRand48
103  {
106  inline __both__ void init(int seed = 0)
107  {
108  state = seed;
109  for (int warmUp=0;warmUp<10;warmUp++)
110  (*this)();
111  }
112 
114  inline __both__ float operator() ()
115  {
116  const uint64_t a = 0x5DEECE66DULL;
117  const uint64_t c = 0xBULL;
118  const uint64_t mask = 0xFFFFFFFFFFFFULL;
119  state = a*state + c;
120  return float(state & mask) / float(mask+1ULL);
121  //return ldexpf(float(state & mask), -24);
122  }
123 
124  uint64_t state;
125  };
126 
127  } // ::owl::common
128 } // ::owl
owl::common::LCG::LCG
__both__ LCG(const vec2ui &seed)
Definition: random.h:68
owl::common::DRand48::operator()
__both__ float operator()()
Definition: random.h:114
owl::common::DRand48
Definition: random.h:103
owl::common::LCG
Definition: random.h:57
owl::common::LCG::LCG
__both__ LCG(const vec2i &seed)
Definition: random.h:66
owl::common::LCG::LCG
__both__ LCG(unsigned int val0, unsigned int val1)
Definition: random.h:63
owl::common::LCG::init
__both__ void init(unsigned int val0, unsigned int val1)
Definition: random.h:71
owl::common::LCG::operator()
__both__ float operator()()
Definition: random.h:86
owl::common::DRand48::init
__both__ void init(int seed=0)
Definition: random.h:106
owl::common::LCG::state
uint32_t state
Definition: random.h:95
owl::common::LCG::LCG
__both__ LCG()
Definition: random.h:59
owl::common::DRand48::state
uint64_t state
Definition: random.h:124
vec.h
owl
Definition: APIContext.cpp:36
__both__
#define __both__
Definition: owl-common.h:102