ClangFormat: apply to source, most of intern
[blender.git] / intern / cycles / kernel / kernel_emission.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 /* Direction Emission */
20 ccl_device_noinline float3 direct_emissive_eval(KernelGlobals *kg,
21                                                 ShaderData *emission_sd,
22                                                 LightSample *ls,
23                                                 ccl_addr_space PathState *state,
24                                                 float3 I,
25                                                 differential3 dI,
26                                                 float t,
27                                                 float time)
28 {
29   /* setup shading at emitter */
30   float3 eval;
31
32   if (shader_constant_emission_eval(kg, ls->shader, &eval)) {
33     if ((ls->prim != PRIM_NONE) && dot(ls->Ng, I) < 0.0f) {
34       ls->Ng = -ls->Ng;
35     }
36   }
37   else {
38     /* Setup shader data and call shader_eval_surface once, better
39      * for GPU coherence and compile times. */
40 #ifdef __BACKGROUND_MIS__
41     if (ls->type == LIGHT_BACKGROUND) {
42       Ray ray;
43       ray.D = ls->D;
44       ray.P = ls->P;
45       ray.t = 1.0f;
46       ray.time = time;
47       ray.dP = differential3_zero();
48       ray.dD = dI;
49
50       shader_setup_from_background(kg, emission_sd, &ray);
51     }
52     else
53 #endif
54     {
55       shader_setup_from_sample(kg,
56                                emission_sd,
57                                ls->P,
58                                ls->Ng,
59                                I,
60                                ls->shader,
61                                ls->object,
62                                ls->prim,
63                                ls->u,
64                                ls->v,
65                                t,
66                                time,
67                                false,
68                                ls->lamp);
69
70       ls->Ng = emission_sd->Ng;
71     }
72
73     /* No proper path flag, we're evaluating this for all closures. that's
74      * weak but we'd have to do multiple evaluations otherwise. */
75     path_state_modify_bounce(state, true);
76     shader_eval_surface(kg, emission_sd, state, PATH_RAY_EMISSION);
77     path_state_modify_bounce(state, false);
78
79     /* Evaluate closures. */
80 #ifdef __BACKGROUND_MIS__
81     if (ls->type == LIGHT_BACKGROUND) {
82       eval = shader_background_eval(emission_sd);
83     }
84     else
85 #endif
86     {
87       eval = shader_emissive_eval(emission_sd);
88     }
89   }
90
91   eval *= ls->eval_fac;
92
93   return eval;
94 }
95
96 ccl_device_noinline bool direct_emission(KernelGlobals *kg,
97                                          ShaderData *sd,
98                                          ShaderData *emission_sd,
99                                          LightSample *ls,
100                                          ccl_addr_space PathState *state,
101                                          Ray *ray,
102                                          BsdfEval *eval,
103                                          bool *is_lamp,
104                                          float rand_terminate)
105 {
106   if (ls->pdf == 0.0f)
107     return false;
108
109   /* todo: implement */
110   differential3 dD = differential3_zero();
111
112   /* evaluate closure */
113
114   float3 light_eval = direct_emissive_eval(
115       kg, emission_sd, ls, state, -ls->D, dD, ls->t, sd->time);
116
117   if (is_zero(light_eval))
118     return false;
119
120     /* evaluate BSDF at shading point */
121
122 #ifdef __VOLUME__
123   if (sd->prim != PRIM_NONE)
124     shader_bsdf_eval(kg, sd, ls->D, eval, ls->pdf, ls->shader & SHADER_USE_MIS);
125   else {
126     float bsdf_pdf;
127     shader_volume_phase_eval(kg, sd, ls->D, eval, &bsdf_pdf);
128     if (ls->shader & SHADER_USE_MIS) {
129       /* Multiple importance sampling. */
130       float mis_weight = power_heuristic(ls->pdf, bsdf_pdf);
131       light_eval *= mis_weight;
132     }
133   }
134 #else
135   shader_bsdf_eval(kg, sd, ls->D, eval, ls->pdf, ls->shader & SHADER_USE_MIS);
136 #endif
137
138   bsdf_eval_mul3(eval, light_eval / ls->pdf);
139
140 #ifdef __PASSES__
141   /* use visibility flag to skip lights */
142   if (ls->shader & SHADER_EXCLUDE_ANY) {
143     if (ls->shader & SHADER_EXCLUDE_DIFFUSE) {
144       eval->diffuse = make_float3(0.0f, 0.0f, 0.0f);
145       eval->subsurface = make_float3(0.0f, 0.0f, 0.0f);
146     }
147     if (ls->shader & SHADER_EXCLUDE_GLOSSY)
148       eval->glossy = make_float3(0.0f, 0.0f, 0.0f);
149     if (ls->shader & SHADER_EXCLUDE_TRANSMIT)
150       eval->transmission = make_float3(0.0f, 0.0f, 0.0f);
151     if (ls->shader & SHADER_EXCLUDE_SCATTER)
152       eval->scatter = make_float3(0.0f, 0.0f, 0.0f);
153   }
154 #endif
155
156   if (bsdf_eval_is_zero(eval))
157     return false;
158
159   if (kernel_data.integrator.light_inv_rr_threshold > 0.0f
160 #ifdef __SHADOW_TRICKS__
161       && (state->flag & PATH_RAY_SHADOW_CATCHER) == 0
162 #endif
163   ) {
164     float probability = max3(fabs(bsdf_eval_sum(eval))) *
165                         kernel_data.integrator.light_inv_rr_threshold;
166     if (probability < 1.0f) {
167       if (rand_terminate >= probability) {
168         return false;
169       }
170       bsdf_eval_mul(eval, 1.0f / probability);
171     }
172   }
173
174   if (ls->shader & SHADER_CAST_SHADOW) {
175     /* setup ray */
176     bool transmit = (dot(sd->Ng, ls->D) < 0.0f);
177     ray->P = ray_offset(sd->P, (transmit) ? -sd->Ng : sd->Ng);
178
179     if (ls->t == FLT_MAX) {
180       /* distant light */
181       ray->D = ls->D;
182       ray->t = ls->t;
183     }
184     else {
185       /* other lights, avoid self-intersection */
186       ray->D = ray_offset(ls->P, ls->Ng) - ray->P;
187       ray->D = normalize_len(ray->D, &ray->t);
188     }
189
190     ray->dP = sd->dP;
191     ray->dD = differential3_zero();
192   }
193   else {
194     /* signal to not cast shadow ray */
195     ray->t = 0.0f;
196   }
197
198   /* return if it's a lamp for shadow pass */
199   *is_lamp = (ls->prim == PRIM_NONE && ls->type != LIGHT_BACKGROUND);
200
201   return true;
202 }
203
204 /* Indirect Primitive Emission */
205
206 ccl_device_noinline float3 indirect_primitive_emission(
207     KernelGlobals *kg, ShaderData *sd, float t, int path_flag, float bsdf_pdf)
208 {
209   /* evaluate emissive closure */
210   float3 L = shader_emissive_eval(sd);
211
212 #ifdef __HAIR__
213   if (!(path_flag & PATH_RAY_MIS_SKIP) && (sd->flag & SD_USE_MIS) &&
214       (sd->type & PRIMITIVE_ALL_TRIANGLE))
215 #else
216   if (!(path_flag & PATH_RAY_MIS_SKIP) && (sd->flag & SD_USE_MIS))
217 #endif
218   {
219     /* multiple importance sampling, get triangle light pdf,
220      * and compute weight with respect to BSDF pdf */
221     float pdf = triangle_light_pdf(kg, sd, t);
222     float mis_weight = power_heuristic(bsdf_pdf, pdf);
223
224     return L * mis_weight;
225   }
226
227   return L;
228 }
229
230 /* Indirect Lamp Emission */
231
232 ccl_device_noinline bool indirect_lamp_emission(KernelGlobals *kg,
233                                                 ShaderData *emission_sd,
234                                                 ccl_addr_space PathState *state,
235                                                 Ray *ray,
236                                                 float3 *emission)
237 {
238   bool hit_lamp = false;
239
240   *emission = make_float3(0.0f, 0.0f, 0.0f);
241
242   for (int lamp = 0; lamp < kernel_data.integrator.num_all_lights; lamp++) {
243     LightSample ls;
244
245     if (!lamp_light_eval(kg, lamp, ray->P, ray->D, ray->t, &ls))
246       continue;
247
248 #ifdef __PASSES__
249     /* use visibility flag to skip lights */
250     if (ls.shader & SHADER_EXCLUDE_ANY) {
251       if (((ls.shader & SHADER_EXCLUDE_DIFFUSE) && (state->flag & PATH_RAY_DIFFUSE)) ||
252           ((ls.shader & SHADER_EXCLUDE_GLOSSY) &&
253            ((state->flag & (PATH_RAY_GLOSSY | PATH_RAY_REFLECT)) ==
254             (PATH_RAY_GLOSSY | PATH_RAY_REFLECT))) ||
255           ((ls.shader & SHADER_EXCLUDE_TRANSMIT) && (state->flag & PATH_RAY_TRANSMIT)) ||
256           ((ls.shader & SHADER_EXCLUDE_SCATTER) && (state->flag & PATH_RAY_VOLUME_SCATTER)))
257         continue;
258     }
259 #endif
260
261     float3 L = direct_emissive_eval(
262         kg, emission_sd, &ls, state, -ray->D, ray->dD, ls.t, ray->time);
263
264 #ifdef __VOLUME__
265     if (state->volume_stack[0].shader != SHADER_NONE) {
266       /* shadow attenuation */
267       Ray volume_ray = *ray;
268       volume_ray.t = ls.t;
269       float3 volume_tp = make_float3(1.0f, 1.0f, 1.0f);
270       kernel_volume_shadow(kg, emission_sd, state, &volume_ray, &volume_tp);
271       L *= volume_tp;
272     }
273 #endif
274
275     if (!(state->flag & PATH_RAY_MIS_SKIP)) {
276       /* multiple importance sampling, get regular light pdf,
277        * and compute weight with respect to BSDF pdf */
278       float mis_weight = power_heuristic(state->ray_pdf, ls.pdf);
279       L *= mis_weight;
280     }
281
282     *emission += L;
283     hit_lamp = true;
284   }
285
286   return hit_lamp;
287 }
288
289 /* Indirect Background */
290
291 ccl_device_noinline float3 indirect_background(KernelGlobals *kg,
292                                                ShaderData *emission_sd,
293                                                ccl_addr_space PathState *state,
294                                                ccl_addr_space Ray *ray)
295 {
296 #ifdef __BACKGROUND__
297   int shader = kernel_data.background.surface_shader;
298
299   /* Use visibility flag to skip lights. */
300   if (shader & SHADER_EXCLUDE_ANY) {
301     if (((shader & SHADER_EXCLUDE_DIFFUSE) && (state->flag & PATH_RAY_DIFFUSE)) ||
302         ((shader & SHADER_EXCLUDE_GLOSSY) &&
303          ((state->flag & (PATH_RAY_GLOSSY | PATH_RAY_REFLECT)) ==
304           (PATH_RAY_GLOSSY | PATH_RAY_REFLECT))) ||
305         ((shader & SHADER_EXCLUDE_TRANSMIT) && (state->flag & PATH_RAY_TRANSMIT)) ||
306         ((shader & SHADER_EXCLUDE_CAMERA) && (state->flag & PATH_RAY_CAMERA)) ||
307         ((shader & SHADER_EXCLUDE_SCATTER) && (state->flag & PATH_RAY_VOLUME_SCATTER)))
308       return make_float3(0.0f, 0.0f, 0.0f);
309   }
310
311   /* Evaluate background shader. */
312   float3 L;
313   if (!shader_constant_emission_eval(kg, shader, &L)) {
314 #  ifdef __SPLIT_KERNEL__
315     Ray priv_ray = *ray;
316     shader_setup_from_background(kg, emission_sd, &priv_ray);
317 #  else
318     shader_setup_from_background(kg, emission_sd, ray);
319 #  endif
320
321     path_state_modify_bounce(state, true);
322     shader_eval_surface(kg, emission_sd, state, state->flag | PATH_RAY_EMISSION);
323     path_state_modify_bounce(state, false);
324
325     L = shader_background_eval(emission_sd);
326   }
327
328   /* Background MIS weights. */
329 #  ifdef __BACKGROUND_MIS__
330   /* Check if background light exists or if we should skip pdf. */
331   int res_x = kernel_data.integrator.pdf_background_res_x;
332
333   if (!(state->flag & PATH_RAY_MIS_SKIP) && res_x) {
334     /* multiple importance sampling, get background light pdf for ray
335      * direction, and compute weight with respect to BSDF pdf */
336     float pdf = background_light_pdf(kg, ray->P, ray->D);
337     float mis_weight = power_heuristic(state->ray_pdf, pdf);
338
339     return L * mis_weight;
340   }
341 #  endif
342
343   return L;
344 #else
345   return make_float3(0.8f, 0.8f, 0.8f);
346 #endif
347 }
348
349 CCL_NAMESPACE_END