b6a856baf2416bd64aae2ecc20ffd8359a5ae92d
[blender.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         /* setup ray */
103         ray->P = sd->P;
104         ray->D = phase_omega_in;
105         ray->t = FLT_MAX;
106
107 #ifdef __RAY_DIFFERENTIALS__
108         ray->dP = sd->dP;
109         ray->dD = phase_domega_in;
110 #endif
111
112         return true;
113 }
114
115 #ifndef __SPLIT_KERNEL__
116 ccl_device void kernel_branched_path_volume_connect_light(
117         KernelGlobals *kg,
118         ShaderData *sd,
119         ShaderData *emission_sd,
120         float3 throughput,
121         ccl_addr_space PathState *state,
122         PathRadiance *L,
123         bool sample_all_lights,
124         Ray *ray,
125         const VolumeSegment *segment)
126 {
127 #ifdef __EMISSION__
128         if(!kernel_data.integrator.use_direct_light)
129                 return;
130
131         Ray light_ray;
132         BsdfEval L_light;
133         bool is_lamp;
134
135         light_ray.time = sd->time;
136
137         if(sample_all_lights) {
138                 /* lamp sampling */
139                 for(int i = 0; i < kernel_data.integrator.num_all_lights; i++) {
140                         if(UNLIKELY(light_select_reached_max_bounces(kg, i, state->bounce)))
141                                 continue;
142
143                         int num_samples = light_select_num_samples(kg, i);
144                         float num_samples_inv = 1.0f/(num_samples*kernel_data.integrator.num_all_lights);
145                         uint lamp_rng_hash = cmj_hash(state->rng_hash, i);
146
147                         for(int j = 0; j < num_samples; j++) {
148                                 /* sample random position on given light */
149                                 float light_u, light_v;
150                                 path_branched_rng_2D(kg, lamp_rng_hash, state, j, num_samples, PRNG_LIGHT_U, &light_u, &light_v);
151
152                                 LightSample ls;
153                                 lamp_light_sample(kg, i, light_u, light_v, ray->P, &ls);
154
155                                 float3 tp = throughput;
156
157                                 /* sample position on volume segment */
158                                 float rphase = path_branched_rng_1D(kg, state->rng_hash, state, j, num_samples, PRNG_PHASE_CHANNEL);
159                                 float rscatter = path_branched_rng_1D(kg, state->rng_hash, state, j, num_samples, PRNG_SCATTER_DISTANCE);
160
161                                 VolumeIntegrateResult result = kernel_volume_decoupled_scatter(kg,
162                                         state, ray, sd, &tp, rphase, rscatter, segment, (ls.t != FLT_MAX)? &ls.P: NULL, false);
163
164                                 /* todo: split up light_sample so we don't have to call it again with new position */
165                                 if(result == VOLUME_PATH_SCATTERED &&
166                                    lamp_light_sample(kg, i, light_u, light_v, sd->P, &ls)) {
167                                         if(kernel_data.integrator.pdf_triangles != 0.0f)
168                                                 ls.pdf *= 2.0f;
169
170                                         float terminate = path_branched_rng_light_termination(kg, state->rng_hash, state, j, num_samples);
171                                         if(direct_emission(kg, sd, emission_sd, &ls, state, &light_ray, &L_light, &is_lamp, terminate)) {
172                                                 /* trace shadow ray */
173                                                 float3 shadow;
174
175                                                 if(!shadow_blocked(kg, sd, emission_sd, state, &light_ray, &shadow)) {
176                                                         /* accumulate */
177                                                         path_radiance_accum_light(L, state, tp*num_samples_inv, &L_light, shadow, num_samples_inv, is_lamp);
178                                                 }
179                                         }
180                                 }
181                         }
182                 }
183
184                 /* mesh light sampling */
185                 if(kernel_data.integrator.pdf_triangles != 0.0f) {
186                         int num_samples = kernel_data.integrator.mesh_light_samples;
187                         float num_samples_inv = 1.0f/num_samples;
188
189                         for(int j = 0; j < num_samples; j++) {
190                                 /* sample random position on random triangle */
191                                 float light_u, light_v;
192                                 path_branched_rng_2D(kg, state->rng_hash, state, j, num_samples, PRNG_LIGHT_U, &light_u, &light_v);
193
194                                 /* only sample triangle lights */
195                                 if(kernel_data.integrator.num_all_lights)
196                                         light_u = 0.5f*light_u;
197
198                                 LightSample ls;
199                                 light_sample(kg, light_u, light_v, sd->time, ray->P, state->bounce, &ls);
200
201                                 float3 tp = throughput;
202
203                                 /* sample position on volume segment */
204                                 float rphase = path_branched_rng_1D(kg, state->rng_hash, state, j, num_samples, PRNG_PHASE_CHANNEL);
205                                 float rscatter = path_branched_rng_1D(kg, state->rng_hash, state, j, num_samples, PRNG_SCATTER_DISTANCE);
206
207                                 VolumeIntegrateResult result = kernel_volume_decoupled_scatter(kg,
208                                         state, ray, sd, &tp, rphase, rscatter, segment, (ls.t != FLT_MAX)? &ls.P: NULL, false);
209                                         
210                                 /* todo: split up light_sample so we don't have to call it again with new position */
211                                 if(result == VOLUME_PATH_SCATTERED &&
212                                    light_sample(kg, light_u, light_v, sd->time, sd->P, state->bounce, &ls)) {
213                                         if(kernel_data.integrator.num_all_lights)
214                                                 ls.pdf *= 2.0f;
215
216                                         float terminate = path_branched_rng_light_termination(kg, state->rng_hash, state, j, num_samples);
217                                         if(direct_emission(kg, sd, emission_sd, &ls, state, &light_ray, &L_light, &is_lamp, terminate)) {
218                                                 /* trace shadow ray */
219                                                 float3 shadow;
220
221                                                 if(!shadow_blocked(kg, sd, emission_sd, state, &light_ray, &shadow)) {
222                                                         /* accumulate */
223                                                         path_radiance_accum_light(L, state, tp*num_samples_inv, &L_light, shadow, num_samples_inv, is_lamp);
224                                                 }
225                                         }
226                                 }
227                         }
228                 }
229         }
230         else {
231                 /* sample random position on random light */
232                 float light_u, light_v;
233                 path_state_rng_2D(kg, state, PRNG_LIGHT_U, &light_u, &light_v);
234
235                 LightSample ls;
236                 light_sample(kg, light_u, light_v, sd->time, ray->P, state->bounce, &ls);
237
238                 float3 tp = throughput;
239
240                 /* sample position on volume segment */
241                 float rphase = path_state_rng_1D(kg, state, PRNG_PHASE_CHANNEL);
242                 float rscatter = path_state_rng_1D(kg, state, PRNG_SCATTER_DISTANCE);
243
244                 VolumeIntegrateResult result = kernel_volume_decoupled_scatter(kg,
245                         state, ray, sd, &tp, rphase, rscatter, segment, (ls.t != FLT_MAX)? &ls.P: NULL, false);
246                         
247                 /* todo: split up light_sample so we don't have to call it again with new position */
248                 if(result == VOLUME_PATH_SCATTERED &&
249                    light_sample(kg, light_u, light_v, sd->time, sd->P, state->bounce, &ls)) {
250                         /* sample random light */
251                         float terminate = path_state_rng_light_termination(kg, state);
252                         if(direct_emission(kg, sd, emission_sd, &ls, state, &light_ray, &L_light, &is_lamp, terminate)) {
253                                 /* trace shadow ray */
254                                 float3 shadow;
255
256                                 if(!shadow_blocked(kg, sd, emission_sd, state, &light_ray, &shadow)) {
257                                         /* accumulate */
258                                         path_radiance_accum_light(L, state, tp, &L_light, shadow, 1.0f, is_lamp);
259                                 }
260                         }
261                 }
262         }
263 #endif /* __EMISSION__ */
264 }
265 #endif /* __SPLIT_KERNEL__ */
266
267 #endif /* __VOLUME_SCATTER__ */
268
269 CCL_NAMESPACE_END
270