Cycles: add Transparent Depth output to Light Path node.
[blender-staging.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
21 ccl_device_noinline float3 direct_emissive_eval(KernelGlobals *kg, float rando,
22         LightSample *ls, float3 I, differential3 dI, float t, float time, int bounce, int transparent_bounce)
23 {
24         /* setup shading at emitter */
25         ShaderData sd;
26         float3 eval;
27
28 #ifdef __BACKGROUND_MIS__
29         if(ls->type == LIGHT_BACKGROUND) {
30                 Ray ray;
31                 ray.D = ls->D;
32                 ray.P = ls->P;
33                 ray.t = 1.0f;
34 #ifdef __OBJECT_MOTION__
35                 ray.time = time;
36 #endif
37                 ray.dP = differential3_zero();
38                 ray.dD = dI;
39
40                 shader_setup_from_background(kg, &sd, &ray, bounce+1, transparent_bounce);
41                 eval = shader_eval_background(kg, &sd, 0, SHADER_CONTEXT_EMISSION);
42         }
43         else
44 #endif
45         {
46                 shader_setup_from_sample(kg, &sd, ls->P, ls->Ng, I, ls->shader, ls->object, ls->prim, ls->u, ls->v, t, time, bounce+1, transparent_bounce);
47
48                 ls->Ng = sd.Ng;
49
50                 /* no path flag, we're evaluating this for all closures. that's weak but
51                  * we'd have to do multiple evaluations otherwise */
52                 shader_eval_surface(kg, &sd, rando, 0, SHADER_CONTEXT_EMISSION);
53
54                 /* evaluate emissive closure */
55                 if(sd.flag & SD_EMISSION)
56                         eval = shader_emissive_eval(kg, &sd);
57                 else
58                         eval = make_float3(0.0f, 0.0f, 0.0f);
59         }
60         
61         eval *= ls->eval_fac;
62
63         return eval;
64 }
65
66 ccl_device_noinline bool direct_emission(KernelGlobals *kg, ShaderData *sd, int lindex,
67         float randt, float rando, float randu, float randv, Ray *ray, BsdfEval *eval,
68         bool *is_lamp, int bounce, int transparent_bounce)
69 {
70         LightSample ls;
71
72 #ifdef __BRANCHED_PATH__
73         if(lindex != LAMP_NONE) {
74                 /* sample position on a specified light */
75                 light_select(kg, lindex, randu, randv, sd->P, &ls);
76         }
77         else
78 #endif
79         {
80                 /* sample a light and position on int */
81                 light_sample(kg, randt, randu, randv, sd->time, sd->P, &ls);
82         }
83
84         if(ls.pdf == 0.0f)
85                 return false;
86
87         /* todo: implement */
88         differential3 dD = differential3_zero();
89
90         /* evaluate closure */
91         float3 light_eval = direct_emissive_eval(kg, rando, &ls, -ls.D, dD, ls.t, sd->time, bounce, transparent_bounce);
92
93         if(is_zero(light_eval))
94                 return false;
95
96         /* evaluate BSDF at shading point */
97         float bsdf_pdf;
98
99 #ifdef __VOLUME__
100         if(sd->prim != PRIM_NONE)
101                 shader_bsdf_eval(kg, sd, ls.D, eval, &bsdf_pdf);
102         else
103                 shader_volume_phase_eval(kg, sd, ls.D, eval, &bsdf_pdf);
104 #else
105         shader_bsdf_eval(kg, sd, ls.D, eval, &bsdf_pdf);
106 #endif
107
108         if(ls.shader & SHADER_USE_MIS) {
109                 /* multiple importance sampling */
110                 float mis_weight = power_heuristic(ls.pdf, bsdf_pdf);
111                 light_eval *= mis_weight;
112         }
113         
114         bsdf_eval_mul(eval, light_eval/ls.pdf);
115
116 #ifdef __PASSES__
117         /* use visibility flag to skip lights */
118         if(ls.shader & SHADER_EXCLUDE_ANY) {
119                 if(ls.shader & SHADER_EXCLUDE_DIFFUSE)
120                         eval->diffuse = make_float3(0.0f, 0.0f, 0.0f);
121                 if(ls.shader & SHADER_EXCLUDE_GLOSSY)
122                         eval->glossy = make_float3(0.0f, 0.0f, 0.0f);
123                 if(ls.shader & SHADER_EXCLUDE_TRANSMIT)
124                         eval->transmission = make_float3(0.0f, 0.0f, 0.0f);
125         }
126 #endif
127
128         if(bsdf_eval_is_zero(eval))
129                 return false;
130
131         if(ls.shader & SHADER_CAST_SHADOW) {
132                 /* setup ray */
133                 bool transmit = (dot(sd->Ng, ls.D) < 0.0f);
134                 ray->P = ray_offset(sd->P, (transmit)? -sd->Ng: sd->Ng);
135
136                 if(ls.t == FLT_MAX) {
137                         /* distant light */
138                         ray->D = ls.D;
139                         ray->t = ls.t;
140                 }
141                 else {
142                         /* other lights, avoid self-intersection */
143                         ray->D = ray_offset(ls.P, ls.Ng) - ray->P;
144                         ray->D = normalize_len(ray->D, &ray->t);
145                 }
146
147                 ray->dP = sd->dP;
148                 ray->dD = differential3_zero();
149         }
150         else {
151                 /* signal to not cast shadow ray */
152                 ray->t = 0.0f;
153         }
154
155         /* return if it's a lamp for shadow pass */
156         *is_lamp = (ls.prim == PRIM_NONE && ls.type != LIGHT_BACKGROUND);
157
158         return true;
159 }
160
161 /* Indirect Primitive Emission */
162
163 ccl_device_noinline float3 indirect_primitive_emission(KernelGlobals *kg, ShaderData *sd, float t, int path_flag, float bsdf_pdf)
164 {
165         /* evaluate emissive closure */
166         float3 L = shader_emissive_eval(kg, sd);
167
168 #ifdef __HAIR__
169         if(!(path_flag & PATH_RAY_MIS_SKIP) && (sd->flag & SD_USE_MIS) && (sd->type & PRIMITIVE_ALL_TRIANGLE)) {
170 #else
171         if(!(path_flag & PATH_RAY_MIS_SKIP) && (sd->flag & SD_USE_MIS)) {
172 #endif
173                 /* multiple importance sampling, get triangle light pdf,
174                  * and compute weight with respect to BSDF pdf */
175                 float pdf = triangle_light_pdf(kg, sd->Ng, sd->I, t);
176                 float mis_weight = power_heuristic(bsdf_pdf, pdf);
177
178                 return L*mis_weight;
179         }
180
181         return L;
182 }
183
184 /* Indirect Lamp Emission */
185
186 ccl_device_noinline bool indirect_lamp_emission(KernelGlobals *kg, Ray *ray, int path_flag, float bsdf_pdf, float randt, float3 *emission, int bounce, int transparent_bounce)
187 {
188         LightSample ls;
189         int lamp = lamp_light_eval_sample(kg, randt);
190
191         if(lamp == LAMP_NONE)
192                 return false;
193
194         if(!lamp_light_eval(kg, lamp, ray->P, ray->D, ray->t, &ls))
195                 return false;
196
197 #ifdef __PASSES__
198         /* use visibility flag to skip lights */
199         if(ls.shader & SHADER_EXCLUDE_ANY) {
200                 if(((ls.shader & SHADER_EXCLUDE_DIFFUSE) && (path_flag & PATH_RAY_DIFFUSE)) ||
201                    ((ls.shader & SHADER_EXCLUDE_GLOSSY) && (path_flag & PATH_RAY_GLOSSY)) ||
202                    ((ls.shader & SHADER_EXCLUDE_TRANSMIT) && (path_flag & PATH_RAY_TRANSMIT)))
203                         return false;
204         }
205 #endif
206
207         float3 L = direct_emissive_eval(kg, 0.0f, &ls, -ray->D, ray->dD, ls.t, ray->time, bounce, transparent_bounce);
208
209         if(!(path_flag & PATH_RAY_MIS_SKIP)) {
210                 /* multiple importance sampling, get regular light pdf,
211                  * and compute weight with respect to BSDF pdf */
212                 float mis_weight = power_heuristic(bsdf_pdf, ls.pdf);
213                 L *= mis_weight;
214         }
215
216         *emission = L;
217         return true;
218 }
219
220 /* Indirect Background */
221
222 ccl_device_noinline float3 indirect_background(KernelGlobals *kg, Ray *ray, int path_flag, float bsdf_pdf, int bounce, int transparent_bounce)
223 {
224 #ifdef __BACKGROUND__
225         int shader = kernel_data.background.surface_shader;
226
227         /* use visibility flag to skip lights */
228         if(shader & SHADER_EXCLUDE_ANY) {
229                 if(((shader & SHADER_EXCLUDE_DIFFUSE) && (path_flag & PATH_RAY_DIFFUSE)) ||
230                    ((shader & SHADER_EXCLUDE_GLOSSY) && (path_flag & PATH_RAY_GLOSSY)) ||
231                    ((shader & SHADER_EXCLUDE_TRANSMIT) && (path_flag & PATH_RAY_TRANSMIT)) ||
232                    ((shader & SHADER_EXCLUDE_CAMERA) && (path_flag & PATH_RAY_CAMERA)))
233                         return make_float3(0.0f, 0.0f, 0.0f);
234         }
235
236         /* evaluate background closure */
237         ShaderData sd;
238         shader_setup_from_background(kg, &sd, ray, bounce+1, transparent_bounce);
239
240         float3 L = shader_eval_background(kg, &sd, path_flag, SHADER_CONTEXT_EMISSION);
241
242 #ifdef __BACKGROUND_MIS__
243         /* check if background light exists or if we should skip pdf */
244         int res = kernel_data.integrator.pdf_background_res;
245
246         if(!(path_flag & PATH_RAY_MIS_SKIP) && res) {
247                 /* multiple importance sampling, get background light pdf for ray
248                  * direction, and compute weight with respect to BSDF pdf */
249                 float pdf = background_light_pdf(kg, ray->D);
250                 float mis_weight = power_heuristic(bsdf_pdf, pdf);
251
252                 return L*mis_weight;
253         }
254 #endif
255
256         return L;
257 #else
258         return make_float3(0.8f, 0.8f, 0.8f);
259 #endif
260 }
261
262 CCL_NAMESPACE_END
263