NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Random.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2011, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #pragma once
29 #include "base/Math.hpp"
30 #include "io/Stream.hpp"
31 
32 namespace FW
33 {
34 //------------------------------------------------------------------------
35 
36 class Random : public InputStream
37 {
38 public:
39  Random (void) { initImpl(); reset(); }
40  explicit Random (U32 seed) { initImpl(); reset(seed); }
41  Random (const Random& other) { initImpl(); reset(other); }
42  ~Random (void) { deinitImpl(); }
43 
44  void reset (void);
45  void reset (U32 seed);
46  void reset (const Random& other);
47 
48  virtual int read (void* ptr, int size);
49  Random& operator= (const Random& other) { reset(other); return *this; }
50 
51  U32 getU32 (void) { return getImpl(); }
52  U32 getU32 (U32 hi) { return (hi == 0) ? 0 : (getU32() % hi); }
53  U32 getU32 (U32 lo, U32 hi) { return (hi <= lo) ? lo : (getU32() % (hi - lo) + lo); }
54  S32 getS32 (void) { return getImpl(); }
55  S32 getS32 (S32 hi) { return (hi <= 0) ? 0 : (getU32() % (U32)hi); }
56  S32 getS32 (S32 lo, S32 hi) { return (hi <= lo) ? lo : (getU32() % (U32)(hi - lo) + lo); }
57  F32 getF32 (void) { return (F32)getU32() * (1.0f / 4294967296.0f); }
58  F32 getF32 (F32 lo, F32 hi) { return getF32() * (hi - lo) + lo; }
59  U64 getU64 (void) { return getU32() | ((U64)getU32() << 32); }
60  U64 getU64 (U64 hi) { return (hi == 0) ? 0 : (getU64() % hi); }
61  U64 getU64 (U64 lo, U64 hi) { return (hi <= lo) ? lo : (getU64() % (hi - lo) + lo); }
62  S64 getS64 (S64 hi) { return (hi <= 0) ? 0 : (getU64() % (U64)hi); }
63  S64 getS64 (S64 lo, S64 hi) { return (hi <= lo) ? lo : (getU64() % (U64)(hi - lo) + lo); }
64  F64 getF64 (void) { return (F64)getU64() * (1.0 / 18446744073709551616.0); }
65  F64 getF64 (F64 lo, F64 hi) { return getF64() * (hi - lo) + lo; }
66 
67  F32 getF32Exp (void) { return -log(getF32() + (1.0f / 4294967296.0f)); }
68  F32 getF32Exp (F32 deviation) { return getF32Exp() * deviation; }
69  F32 getF32Exp (F32 mean, F32 deviation) { return getF32Exp() * deviation + mean; }
70  F64 getF64Exp (void) { return -log(getF64() + (1.0 / 18446744073709551616.0)); }
71  F64 getF64Exp (F64 deviation) { return getF64Exp() * deviation; }
72  F64 getF64Exp (F64 mean, F64 deviation) { return getF64Exp() * deviation + mean; }
73 
74  F32 getF32Normal (void);
75  F32 getF32Normal (F32 deviation) { return getF32Normal() * deviation; }
76  F32 getF32Normal (F32 mean, F32 deviation) { return getF32Normal() * deviation + mean; }
77  F64 getF64Normal (void);
78  F64 getF64Normal (F64 deviation) { return getF64Normal() * deviation; }
79  F64 getF64Normal (F64 mean, F64 deviation) { return getF64Normal() * deviation + mean; }
80 
81  Vec2f getVec2f (void) { return Vec2f(getF32(), getF32()); }
82  Vec2f getVec2f (F32 lo, F32 hi) { return Vec2f(getF32(lo, hi), getF32(lo, hi)); }
83  Vec3f getVec3f (void) { return Vec3f(getF32(), getF32(), getF32()); }
84  Vec3f getVec3f (F32 lo, F32 hi) { return Vec3f(getF32(lo, hi), getF32(lo, hi), getF32(lo, hi)); }
85  Vec4f getVec4f (void) { return Vec4f(getF32(), getF32(), getF32(), getF32()); }
86  Vec4f getVec4f (F32 lo, F32 hi) { return Vec4f(getF32(lo, hi), getF32(lo, hi), getF32(lo, hi), getF32(lo, hi)); }
87 
88  Vec2d getVec2d (void) { return Vec2d(getF64(), getF64()); }
89  Vec2d getVec2d (F64 lo, F64 hi) { return Vec2d(getF64(lo, hi), getF64(lo, hi)); }
90  Vec3d getVec3d (void) { return Vec3d(getF64(), getF64(), getF64()); }
91  Vec3d getVec3d (F64 lo, F64 hi) { return Vec3d(getF64(lo, hi), getF64(lo, hi), getF64(lo, hi)); }
92  Vec4d getVec4d (void) { return Vec4d(getF64(), getF64(), getF64(), getF64()); }
93  Vec4d getVec4d (F64 lo, F64 hi) { return Vec4d(getF64(lo, hi), getF64(lo, hi), getF64(lo, hi), getF64(lo, hi)); }
94 
95 private:
96  void initImpl (void);
97  void deinitImpl (void);
98  void resetImpl (U32 seed);
99  void assignImpl (const Random& other);
100  U32 getImpl (void);
101 
102 private:
103  void* m_impl;
104  bool m_normalF32Valid;
105  F32 m_normalF32;
106  bool m_normalF64Valid;
107  F64 m_normalF64;
108 };
109 
110 //------------------------------------------------------------------------
111 }
Random(const Random &other)
Definition: Random.hpp:41
U64 getU64(U64 lo, U64 hi)
Definition: Random.hpp:61
U64 getU64(U64 hi)
Definition: Random.hpp:60
U32 getU32(U32 lo, U32 hi)
Definition: Random.hpp:53
Random & operator=(const Random &other)
Definition: Random.hpp:49
F32 getF32Normal(void)
Definition: Random.cpp:141
F32 getF32Exp(F32 mean, F32 deviation)
Definition: Random.hpp:69
void ** ptr
Definition: DLLImports.cpp:74
Vec4d getVec4d(F64 lo, F64 hi)
Definition: Random.hpp:93
U64 getU64(void)
Definition: Random.hpp:59
F32 getF32Exp(void)
Definition: Random.hpp:67
unsigned __int64 U64
Definition: Defs.hpp:97
double F64
Definition: Defs.hpp:90
F32 getF32Normal(F32 deviation)
Definition: Random.hpp:75
Vec2f getVec2f(void)
Definition: Random.hpp:81
virtual int read(void *ptr, int size)
Definition: Random.cpp:132
Random(U32 seed)
Definition: Random.hpp:40
Vec4f getVec4f(void)
Definition: Random.hpp:85
~Random(void)
Definition: Random.hpp:42
F64 getF64(void)
Definition: Random.hpp:64
S32 getS32(void)
Definition: Random.hpp:54
F64 getF64Normal(F64 deviation)
Definition: Random.hpp:78
float F32
Definition: Defs.hpp:89
F64 getF64Exp(void)
Definition: Random.hpp:70
U32 getU32(U32 hi)
Definition: Random.hpp:52
signed int S32
Definition: Defs.hpp:88
Vec2d getVec2d(void)
Definition: Random.hpp:88
F32 getF32Normal(F32 mean, F32 deviation)
Definition: Random.hpp:76
S32 getS32(S32 lo, S32 hi)
Definition: Random.hpp:56
F32 getF32(void)
Definition: Random.hpp:57
Vec3d getVec3d(void)
Definition: Random.hpp:90
F64 getF64Normal(void)
Definition: Random.cpp:163
signed __int64 S64
Definition: Defs.hpp:98
S64 getS64(S64 lo, S64 hi)
Definition: Random.hpp:63
F32 getF32(F32 lo, F32 hi)
Definition: Random.hpp:58
unsigned int U32
Definition: Defs.hpp:85
Vec3f getVec3f(F32 lo, F32 hi)
Definition: Random.hpp:84
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction f
Definition: DLLImports.inl:88
F32 getF32Exp(F32 deviation)
Definition: Random.hpp:68
FW_CUDA_FUNC F64 log(F64 a)
Definition: Math.hpp:47
U32 getU32(void)
Definition: Random.hpp:51
Vec2d getVec2d(F64 lo, F64 hi)
Definition: Random.hpp:89
S64 getS64(S64 hi)
Definition: Random.hpp:62
void reset(void)
Definition: Random.cpp:98
F64 getF64Normal(F64 mean, F64 deviation)
Definition: Random.hpp:79
S32 getS32(S32 hi)
Definition: Random.hpp:55
Vec2f getVec2f(F32 lo, F32 hi)
Definition: Random.hpp:82
Vec4f getVec4f(F32 lo, F32 hi)
Definition: Random.hpp:86
Vec3d getVec3d(F64 lo, F64 hi)
Definition: Random.hpp:91
Vec4d getVec4d(void)
Definition: Random.hpp:92
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr size
Definition: DLLImports.inl:319
F64 getF64Exp(F64 deviation)
Definition: Random.hpp:71
Random(void)
Definition: Random.hpp:39
F64 getF64(F64 lo, F64 hi)
Definition: Random.hpp:65
F64 getF64Exp(F64 mean, F64 deviation)
Definition: Random.hpp:72
Vec3f getVec3f(void)
Definition: Random.hpp:83