Code cleanup: store branch factor in PathState.
[blender.git] / intern / cycles / kernel / kernel_random.h
1 /*
2  * Copyright 2011-2013 Blender Foundation
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 #include "kernel/kernel_jitter.h"
18
19 CCL_NAMESPACE_BEGIN
20
21 /* Pseudo random numbers, uncomment this for debugging correlations. Only run
22  * this single threaded on a CPU for repeatable resutls. */
23 //#define __DEBUG_CORRELATION__
24
25
26 /* High Dimensional Sobol.
27  *
28  * Multidimensional sobol with generator matrices. Dimension 0 and 1 are equal
29  * to classic Van der Corput and Sobol sequences. */
30
31 #ifdef __SOBOL__
32
33 /* Skip initial numbers that are not as well distributed, especially the
34  * first sequence is just 0 everywhere, which can be problematic for e.g.
35  * path termination.
36  */
37 #define SOBOL_SKIP 64
38
39 ccl_device uint sobol_dimension(KernelGlobals *kg, int index, int dimension)
40 {
41         uint result = 0;
42         uint i = index;
43         for(uint j = 0; i; i >>= 1, j++) {
44                 if(i & 1) {
45                         result ^= kernel_tex_fetch(__sobol_directions, 32*dimension + j);
46                 }
47         }
48         return result;
49 }
50
51 #endif /* __SOBOL__ */
52
53
54 ccl_device_forceinline float path_rng_1D(KernelGlobals *kg,
55                                          uint rng_hash,
56                                          int sample, int num_samples,
57                                          int dimension)
58 {
59 #ifdef __DEBUG_CORRELATION__
60         return (float)drand48();
61 #endif
62
63 #ifdef __CMJ__
64 #  ifdef __SOBOL__
65         if(kernel_data.integrator.sampling_pattern == SAMPLING_PATTERN_CMJ)
66 #  endif
67         {
68                 /* Correlated multi-jitter. */
69                 int p = rng_hash + dimension;
70                 return cmj_sample_1D(sample, num_samples, p);
71         }
72 #endif
73
74 #ifdef __SOBOL__
75         /* Sobol sequence value using direction vectors. */
76         uint result = sobol_dimension(kg, sample + SOBOL_SKIP, dimension);
77         float r = (float)result * (1.0f/(float)0xFFFFFFFF);
78
79         /* Cranly-Patterson rotation using rng seed */
80         float shift;
81
82         /* Hash rng with dimension to solve correlation issues.
83          * See T38710, T50116.
84          */
85         uint tmp_rng = cmj_hash_simple(dimension, rng_hash);
86         shift = tmp_rng * (1.0f/(float)0xFFFFFFFF);
87
88         return r + shift - floorf(r + shift);
89 #endif
90 }
91
92 ccl_device_forceinline void path_rng_2D(KernelGlobals *kg,
93                                         uint rng_hash,
94                                         int sample, int num_samples,
95                                         int dimension,
96                                         float *fx, float *fy)
97 {
98 #ifdef __DEBUG_CORRELATION__
99         *fx = (float)drand48();
100         *fy = (float)drand48();
101         return;
102 #endif
103
104 #ifdef __CMJ__
105 #  ifdef __SOBOL__
106         if(kernel_data.integrator.sampling_pattern == SAMPLING_PATTERN_CMJ)
107 #  endif
108         {
109                 /* Correlated multi-jitter. */
110                 int p = rng_hash + dimension;
111                 cmj_sample_2D(sample, num_samples, p, fx, fy);
112                 return;
113         }
114 #endif
115
116 #ifdef __SOBOL__
117         /* Sobol. */
118         *fx = path_rng_1D(kg, rng_hash, sample, num_samples, dimension);
119         *fy = path_rng_1D(kg, rng_hash, sample, num_samples, dimension + 1);
120 #endif
121 }
122
123 ccl_device_inline void path_rng_init(KernelGlobals *kg,
124                                      ccl_global uint *rng_state,
125                                      int sample, int num_samples,
126                                      uint *rng_hash,
127                                      int x, int y,
128                                      float *fx, float *fy)
129 {
130         /* load state */
131         *rng_hash = *rng_state;
132         *rng_hash ^= kernel_data.integrator.seed;
133
134 #ifdef __DEBUG_CORRELATION__
135         srand48(*rng_hash + sample);
136 #endif
137
138         if(sample == 0) {
139                 *fx = 0.5f;
140                 *fy = 0.5f;
141         }
142         else {
143                 path_rng_2D(kg, *rng_hash, sample, num_samples, PRNG_FILTER_U, fx, fy);
144         }
145 }
146
147 /* Linear Congruential Generator */
148
149 ccl_device uint lcg_step_uint(uint *rng)
150 {
151         /* implicit mod 2^32 */
152         *rng = (1103515245*(*rng) + 12345);
153         return *rng;
154 }
155
156 ccl_device float lcg_step_float(uint *rng)
157 {
158         /* implicit mod 2^32 */
159         *rng = (1103515245*(*rng) + 12345);
160         return (float)*rng * (1.0f/(float)0xFFFFFFFF);
161 }
162
163 ccl_device uint lcg_init(uint seed)
164 {
165         uint rng = seed;
166         lcg_step_uint(&rng);
167         return rng;
168 }
169
170 /* Path Tracing Utility Functions
171  *
172  * For each random number in each step of the path we must have a unique
173  * dimension to avoid using the same sequence twice.
174  *
175  * For branches in the path we must be careful not to reuse the same number
176  * in a sequence and offset accordingly.
177  */
178
179 ccl_device_inline float path_state_rng_1D(KernelGlobals *kg,
180                                           const ccl_addr_space PathState *state,
181                                           int dimension)
182 {
183         return path_rng_1D(kg,
184                            state->rng_hash,
185                            state->sample, state->num_samples,
186                            state->rng_offset + dimension);
187 }
188
189 ccl_device_inline float path_state_rng_1D_for_decision(
190         KernelGlobals *kg,
191         const ccl_addr_space PathState *state,
192         int dimension)
193 {
194         /* The rng_offset is not increased for transparent bounces. if we do then
195          * fully transparent objects can become subtly visible by the different
196          * sampling patterns used where the transparent object is.
197          *
198          * however for some random numbers that will determine if we next bounce
199          * is transparent we do need to increase the offset to avoid always making
200          * the same decision. */
201         const int rng_offset = state->rng_offset + state->transparent_bounce * PRNG_BOUNCE_NUM;
202         return path_rng_1D(kg,
203                            state->rng_hash,
204                            state->sample, state->num_samples,
205                            rng_offset + dimension);
206 }
207
208 ccl_device_inline void path_state_rng_2D(KernelGlobals *kg,
209                                          const ccl_addr_space PathState *state,
210                                          int dimension,
211                                          float *fx, float *fy)
212 {
213         path_rng_2D(kg,
214                     state->rng_hash,
215                     state->sample, state->num_samples,
216                     state->rng_offset + dimension,
217                     fx, fy);
218 }
219
220 ccl_device_inline float path_branched_rng_1D(
221         KernelGlobals *kg,
222         uint rng_hash,
223         const ccl_addr_space PathState *state,
224         int branch,
225         int num_branches,
226         int dimension)
227 {
228         return path_rng_1D(kg,
229                            rng_hash,
230                            state->sample * num_branches + branch,
231                            state->num_samples * num_branches,
232                            state->rng_offset + dimension);
233 }
234
235 ccl_device_inline float path_branched_rng_1D_for_decision(
236         KernelGlobals *kg,
237         uint rng_hash,
238         const ccl_addr_space PathState *state,
239         int branch,
240         int num_branches,
241         int dimension)
242 {
243         const int rng_offset = state->rng_offset + state->transparent_bounce * PRNG_BOUNCE_NUM;
244         return path_rng_1D(kg,
245                            rng_hash,
246                            state->sample * num_branches + branch,
247                            state->num_samples * num_branches,
248                            rng_offset + dimension);
249 }
250
251 ccl_device_inline void path_branched_rng_2D(
252         KernelGlobals *kg,
253         uint rng_hash,
254         const ccl_addr_space PathState *state,
255         int branch,
256         int num_branches,
257         int dimension,
258         float *fx, float *fy)
259 {
260         path_rng_2D(kg,
261                     rng_hash,
262                     state->sample * num_branches + branch,
263                     state->num_samples * num_branches,
264                     state->rng_offset + dimension,
265                     fx, fy);
266 }
267
268 /* Utitility functions to get light termination value,
269  * since it might not be needed in many cases.
270  */
271 ccl_device_inline float path_state_rng_light_termination(
272         KernelGlobals *kg,
273         const ccl_addr_space PathState *state)
274 {
275         if(kernel_data.integrator.light_inv_rr_threshold > 0.0f) {
276                 return path_state_rng_1D_for_decision(kg, state, PRNG_LIGHT_TERMINATE);
277         }
278         return 0.0f;
279 }
280
281 ccl_device_inline float path_branched_rng_light_termination(
282         KernelGlobals *kg,
283         uint rng_hash,
284         const ccl_addr_space PathState *state,
285         int branch,
286         int num_branches)
287 {
288         if(kernel_data.integrator.light_inv_rr_threshold > 0.0f) {
289                 return path_branched_rng_1D_for_decision(kg,
290                                                          rng_hash,
291                                                          state,
292                                                          branch,
293                                                          num_branches,
294                                                          PRNG_LIGHT_TERMINATE);
295         }
296         return 0.0f;
297 }
298
299 ccl_device_inline uint lcg_state_init(PathState *state,
300                                       uint scramble)
301 {
302         return lcg_init(state->rng_hash + state->rng_offset + state->sample*scramble);
303 }
304
305 ccl_device_inline uint lcg_state_init_addrspace(ccl_addr_space PathState *state,
306                                                 uint scramble)
307 {
308         return lcg_init(state->rng_hash + state->rng_offset + state->sample*scramble);
309 }
310
311
312 ccl_device float lcg_step_float_addrspace(ccl_addr_space uint *rng)
313 {
314         /* Implicit mod 2^32 */
315         *rng = (1103515245*(*rng) + 12345);
316         return (float)*rng * (1.0f/(float)0xFFFFFFFF);
317 }
318
319 CCL_NAMESPACE_END
320