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