Code cleanup: move rng into path state.
[blender-staging.git] / intern / cycles / kernel / kernel_path_volume.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 CCL_NAMESPACE_BEGIN
18
19 #ifdef __VOLUME_SCATTER__
20
21 ccl_device_inline void kernel_path_volume_connect_light(
22         KernelGlobals *kg,
23         ShaderData *sd,
24         ShaderData *emission_sd,
25         float3 throughput,
26         ccl_addr_space PathState *state,
27         PathRadiance *L)
28 {
29 #ifdef __EMISSION__
30         if(!kernel_data.integrator.use_direct_light)
31                 return;
32
33         /* sample illumination from lights to find path contribution */
34         float light_t = path_state_rng_1D(kg, state, PRNG_LIGHT);
35         float light_u, light_v;
36         path_state_rng_2D(kg, state, PRNG_LIGHT_U, &light_u, &light_v);
37
38         Ray light_ray;
39         BsdfEval L_light;
40         LightSample ls;
41         bool is_lamp;
42
43         /* connect to light from given point where shader has been evaluated */
44 #  ifdef __OBJECT_MOTION__
45         light_ray.time = sd->time;
46 #  endif
47
48         if(light_sample(kg, light_t, light_u, light_v, sd->time, sd->P, state->bounce, &ls))
49         {
50                 float terminate = path_state_rng_light_termination(kg, state);
51                 if(direct_emission(kg, sd, emission_sd, &ls, state, &light_ray, &L_light, &is_lamp, terminate)) {
52                         /* trace shadow ray */
53                         float3 shadow;
54
55                         if(!shadow_blocked(kg, emission_sd, state, &light_ray, &shadow)) {
56                                 /* accumulate */
57                                 path_radiance_accum_light(L, state, throughput, &L_light, shadow, 1.0f, is_lamp);
58                         }
59                 }
60         }
61 #endif /* __EMISSION__ */
62 }
63
64 #ifdef __KERNEL_GPU__
65 ccl_device_noinline
66 #else
67 ccl_device
68 #endif
69 bool kernel_path_volume_bounce(
70     KernelGlobals *kg,
71     ShaderData *sd,
72     ccl_addr_space float3 *throughput,
73     ccl_addr_space PathState *state,
74     PathRadiance *L,
75     ccl_addr_space Ray *ray)
76 {
77         /* sample phase function */
78         float phase_pdf;
79         BsdfEval phase_eval;
80         float3 phase_omega_in;
81         differential3 phase_domega_in;
82         float phase_u, phase_v;
83         path_state_rng_2D(kg, state, PRNG_PHASE_U, &phase_u, &phase_v);
84         int label;
85
86         label = shader_volume_phase_sample(kg, sd, phase_u, phase_v, &phase_eval,
87                 &phase_omega_in, &phase_domega_in, &phase_pdf);
88
89         if(phase_pdf == 0.0f || bsdf_eval_is_zero(&phase_eval))
90                 return false;
91         
92         /* modify throughput */
93         path_radiance_bsdf_bounce(L, throughput, &phase_eval, phase_pdf, state->bounce, label);
94
95         /* set labels */
96         state->ray_pdf = phase_pdf;
97 #ifdef __LAMP_MIS__
98         state->ray_t = 0.0f;
99 #endif
100         state->min_ray_pdf = fminf(phase_pdf, state->min_ray_pdf);
101
102         /* update path state */
103         path_state_next(kg, state, label);
104
105         /* setup ray */
106         ray->P = sd->P;
107         ray->D = phase_omega_in;
108         ray->t = FLT_MAX;
109
110 #ifdef __RAY_DIFFERENTIALS__
111         ray->dP = sd->dP;
112         ray->dD = phase_domega_in;
113 #endif
114
115         return true;
116 }
117
118 #ifndef __SPLIT_KERNEL__
119 ccl_device void kernel_branched_path_volume_connect_light(
120         KernelGlobals *kg,
121         ShaderData *sd,
122         ShaderData *emission_sd,
123         float3 throughput,
124         ccl_addr_space PathState *state,
125         PathRadiance *L,
126         bool sample_all_lights,
127         Ray *ray,
128         const VolumeSegment *segment)
129 {
130 #ifdef __EMISSION__
131         if(!kernel_data.integrator.use_direct_light)
132                 return;
133
134         Ray light_ray;
135         BsdfEval L_light;
136         bool is_lamp;
137
138 #  ifdef __OBJECT_MOTION__
139         light_ray.time = sd->time;
140 #  endif
141
142         if(sample_all_lights) {
143                 /* lamp sampling */
144                 for(int i = 0; i < kernel_data.integrator.num_all_lights; i++) {
145                         if(UNLIKELY(light_select_reached_max_bounces(kg, i, state->bounce)))
146                                 continue;
147
148                         int num_samples = light_select_num_samples(kg, i);
149                         float num_samples_inv = 1.0f/(num_samples*kernel_data.integrator.num_all_lights);
150                         uint lamp_rng_hash = cmj_hash(state->rng_hash, i);
151
152                         for(int j = 0; j < num_samples; j++) {
153                                 /* sample random position on given light */
154                                 float light_u, light_v;
155                                 path_branched_rng_2D(kg, lamp_rng_hash, state, j, num_samples, PRNG_LIGHT_U, &light_u, &light_v);
156
157                                 LightSample ls;
158                                 lamp_light_sample(kg, i, light_u, light_v, ray->P, &ls);
159
160                                 float3 tp = throughput;
161
162                                 /* sample position on volume segment */
163                                 float rphase = path_branched_rng_1D_for_decision(kg, state->rng_hash, state, j, num_samples, PRNG_PHASE);
164                                 float rscatter = path_branched_rng_1D_for_decision(kg, state->rng_hash, state, j, num_samples, PRNG_SCATTER_DISTANCE);
165
166                                 VolumeIntegrateResult result = kernel_volume_decoupled_scatter(kg,
167                                         state, ray, sd, &tp, rphase, rscatter, segment, (ls.t != FLT_MAX)? &ls.P: NULL, false);
168
169                                 (void)result;
170                                 kernel_assert(result == VOLUME_PATH_SCATTERED);
171
172                                 /* todo: split up light_sample so we don't have to call it again with new position */
173                                 if(lamp_light_sample(kg, i, light_u, light_v, sd->P, &ls)) {
174                                         if(kernel_data.integrator.pdf_triangles != 0.0f)
175                                                 ls.pdf *= 2.0f;
176
177                                         float terminate = path_branched_rng_light_termination(kg, state->rng_hash, state, j, num_samples);
178                                         if(direct_emission(kg, sd, emission_sd, &ls, state, &light_ray, &L_light, &is_lamp, terminate)) {
179                                                 /* trace shadow ray */
180                                                 float3 shadow;
181
182                                                 if(!shadow_blocked(kg, emission_sd, state, &light_ray, &shadow)) {
183                                                         /* accumulate */
184                                                         path_radiance_accum_light(L, state, tp*num_samples_inv, &L_light, shadow, num_samples_inv, is_lamp);
185                                                 }
186                                         }
187                                 }
188                         }
189                 }
190
191                 /* mesh light sampling */
192                 if(kernel_data.integrator.pdf_triangles != 0.0f) {
193                         int num_samples = kernel_data.integrator.mesh_light_samples;
194                         float num_samples_inv = 1.0f/num_samples;
195
196                         for(int j = 0; j < num_samples; j++) {
197                                 /* sample random position on random triangle */
198                                 float light_t = path_branched_rng_1D_for_decision(kg, state->rng_hash, state, j, num_samples, PRNG_LIGHT);
199                                 float light_u, light_v;
200                                 path_branched_rng_2D(kg, state->rng_hash, state, j, num_samples, PRNG_LIGHT_U, &light_u, &light_v);
201
202                                 /* only sample triangle lights */
203                                 if(kernel_data.integrator.num_all_lights)
204                                         light_t = 0.5f*light_t;
205
206                                 LightSample ls;
207                                 light_sample(kg, light_t, light_u, light_v, sd->time, ray->P, state->bounce, &ls);
208
209                                 float3 tp = throughput;
210
211                                 /* sample position on volume segment */
212                                 float rphase = path_branched_rng_1D_for_decision(kg, state->rng_hash, state, j, num_samples, PRNG_PHASE);
213                                 float rscatter = path_branched_rng_1D_for_decision(kg, state->rng_hash, state, j, num_samples, PRNG_SCATTER_DISTANCE);
214
215                                 VolumeIntegrateResult result = kernel_volume_decoupled_scatter(kg,
216                                         state, ray, sd, &tp, rphase, rscatter, segment, (ls.t != FLT_MAX)? &ls.P: NULL, false);
217                                         
218                                 (void)result;
219                                 kernel_assert(result == VOLUME_PATH_SCATTERED);
220
221                                 /* todo: split up light_sample so we don't have to call it again with new position */
222                                 if(light_sample(kg, light_t, light_u, light_v, sd->time, sd->P, state->bounce, &ls)) {
223                                         if(kernel_data.integrator.num_all_lights)
224                                                 ls.pdf *= 2.0f;
225
226                                         float terminate = path_branched_rng_light_termination(kg, state->rng_hash, state, j, num_samples);
227                                         if(direct_emission(kg, sd, emission_sd, &ls, state, &light_ray, &L_light, &is_lamp, terminate)) {
228                                                 /* trace shadow ray */
229                                                 float3 shadow;
230
231                                                 if(!shadow_blocked(kg, emission_sd, state, &light_ray, &shadow)) {
232                                                         /* accumulate */
233                                                         path_radiance_accum_light(L, state, tp*num_samples_inv, &L_light, shadow, num_samples_inv, is_lamp);
234                                                 }
235                                         }
236                                 }
237                         }
238                 }
239         }
240         else {
241                 /* sample random position on random light */
242                 float light_t = path_state_rng_1D(kg, state, PRNG_LIGHT);
243                 float light_u, light_v;
244                 path_state_rng_2D(kg, state, PRNG_LIGHT_U, &light_u, &light_v);
245
246                 LightSample ls;
247                 light_sample(kg, light_t, light_u, light_v, sd->time, ray->P, state->bounce, &ls);
248
249                 float3 tp = throughput;
250
251                 /* sample position on volume segment */
252                 float rphase = path_state_rng_1D_for_decision(kg, state, PRNG_PHASE);
253                 float rscatter = path_state_rng_1D_for_decision(kg, state, PRNG_SCATTER_DISTANCE);
254
255                 VolumeIntegrateResult result = kernel_volume_decoupled_scatter(kg,
256                         state, ray, sd, &tp, rphase, rscatter, segment, (ls.t != FLT_MAX)? &ls.P: NULL, false);
257                         
258                 (void)result;
259                 kernel_assert(result == VOLUME_PATH_SCATTERED);
260
261                 /* todo: split up light_sample so we don't have to call it again with new position */
262                 if(light_sample(kg, light_t, light_u, light_v, sd->time, sd->P, state->bounce, &ls)) {
263                         /* sample random light */
264                         float terminate = path_state_rng_light_termination(kg, state);
265                         if(direct_emission(kg, sd, emission_sd, &ls, state, &light_ray, &L_light, &is_lamp, terminate)) {
266                                 /* trace shadow ray */
267                                 float3 shadow;
268
269                                 if(!shadow_blocked(kg, emission_sd, state, &light_ray, &shadow)) {
270                                         /* accumulate */
271                                         path_radiance_accum_light(L, state, tp, &L_light, shadow, 1.0f, is_lamp);
272                                 }
273                         }
274                 }
275         }
276 #endif /* __EMISSION__ */
277 }
278 #endif /* __SPLIT_KERNEL__ */
279
280 #endif /* __VOLUME_SCATTER__ */
281
282 CCL_NAMESPACE_END
283