RenderManAPI  24.0
RixRNGInline.h
Go to the documentation of this file.
1 /*
2 # ------------------------------------------------------------------------------
3 #
4 # Copyright (c) 2020 Pixar. All rights reserved.
5 #
6 # The information in this file (the "Software") is provided for the exclusive
7 # use of the software licensees of Pixar ("Licensees"). Licensees have the
8 # right to incorporate the Software into other products for use by other
9 # authorized software licensees of Pixar, without fee. Except as expressly
10 # permitted herein, the Software may not be disclosed to third parties, copied
11 # or duplicated in any form, in whole or in part, without the prior written
12 # permission of Pixar.
13 #
14 # The copyright notices in the Software and this entire statement, including the
15 # above license grant, this restriction and the following disclaimer, must be
16 # included in all copies of the Software, in whole or in part, and all permitted
17 # derivative works of the Software, unless such copies or derivative works are
18 # solely in the form of machine-executable object code generated by a source
19 # language processor.
20 #
21 # PIXAR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL PIXAR BE
23 # LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
24 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
25 # OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
26 # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. IN NO CASE WILL
27 # PIXAR'S TOTAL LIABILITY FOR ALL DAMAGES ARISING OUT OF OR IN CONNECTION WITH
28 # THE USE OR PERFORMANCE OF THIS SOFTWARE EXCEED $50.
29 #
30 # Pixar
31 # 1200 Park Ave
32 # Emeryville CA 94608
33 #
34 # ------------------------------------------------------------------------------
35 */
36 
37 #ifndef RixRNGInline_h
38 #define RixRNGInline_h
39 
40 // Convenience functions that can be used to generate sample values.
41 // This file is included by RixRNG.h.
42 
43 #include "prmanapi.h" // for PRMAN_INLINE
44 
45 namespace RixRNGUtils
46 {
47 
48 // Compute a repeatable random float in [0,1) based on value and scramble
49 PRMAN_INLINE float
50 HashToRandom(const unsigned value, const unsigned scramble)
51 {
52  // clang-format off
53  unsigned result = value;
54  result ^= scramble;
55  result ^= result >> 17;
56  result ^= result >> 10; result *= 0xb36534e5;
57  result ^= result >> 12;
58  result ^= result >> 21; result *= 0x93fc4795;
59  result ^= 0xdf6e307f;
60  result ^= result >> 17; result *= 1 | scramble >> 18;
61  return static_cast<float>(result) / 4298115584.0f;
62  // clang-format on
63 }
64 
65 PRMAN_INLINE unsigned
67  const unsigned pattern,
68  const unsigned scramble1 = 0xb2182ef5,
69  const unsigned scramble2 = 0x1e1897a7)
70 {
71  unsigned npattern = pattern * ((scramble1 << 1) | 1);
72  // All odds are coprime with 2^16.
73  npattern += scramble2;
74  return npattern;
75 }
76 
77 } // namespace RixRNGUtils
78 
79 #endif
#define PRMAN_INLINE
Definition: prmanapi.h:99
Definition: RixRNGInline.h:45
PRMAN_INLINE float HashToRandom(const unsigned value, const unsigned scramble)
Definition: RixRNGInline.h:50
PRMAN_INLINE unsigned shufflePattern(const unsigned pattern, const unsigned scramble1=0xb2182ef5, const unsigned scramble2=0x1e1897a7)
Definition: RixRNGInline.h:66