Cycles: ray visibility options now work for lamps and mesh lights, with and without
[blender.git] / intern / cycles / kernel / kernel_emission.h
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 CCL_NAMESPACE_BEGIN
20
21 /* Direction Emission */
22
23 __device_noinline float3 direct_emissive_eval(KernelGlobals *kg, float rando,
24         LightSample *ls, float u, float v, float3 I, differential3 dI, float t, float time)
25 {
26         /* setup shading at emitter */
27         ShaderData sd;
28         float3 eval;
29
30 #ifdef __BACKGROUND_MIS__
31         if(ls->type == LIGHT_BACKGROUND) {
32                 Ray ray;
33                 ray.D = ls->D;
34                 ray.P = ls->P;
35                 ray.t = 1.0f;
36 #ifdef __OBJECT_MOTION__
37                 ray.time = time;
38 #endif
39                 ray.dP = differential3_zero();
40                 ray.dD = dI;
41 #ifdef __CAMERA_MOTION__
42                 ray.time = time;
43 #endif
44                 shader_setup_from_background(kg, &sd, &ray);
45                 eval = shader_eval_background(kg, &sd, 0, SHADER_CONTEXT_EMISSION);
46         }
47         else
48 #endif
49         {
50 #ifdef __HAIR__
51                 if(ls->type == LIGHT_STRAND)
52                         shader_setup_from_sample(kg, &sd, ls->P, ls->Ng, I, ls->shader, ls->object, ls->prim, u, v, t, time, ls->prim);
53                 else
54 #endif
55                         shader_setup_from_sample(kg, &sd, ls->P, ls->Ng, I, ls->shader, ls->object, ls->prim, u, v, t, time, ~0);
56
57                 ls->Ng = sd.Ng;
58
59                 /* no path flag, we're evaluating this for all closures. that's weak but
60                  * we'd have to do multiple evaluations otherwise */
61                 shader_eval_surface(kg, &sd, rando, 0, SHADER_CONTEXT_EMISSION);
62
63                 /* evaluate emissive closure */
64                 if(sd.flag & SD_EMISSION)
65                         eval = shader_emissive_eval(kg, &sd);
66                 else
67                         eval = make_float3(0.0f, 0.0f, 0.0f);
68         }
69         
70         eval *= ls->eval_fac;
71
72         return eval;
73 }
74
75 __device_noinline bool direct_emission(KernelGlobals *kg, ShaderData *sd, int lindex,
76         float randt, float rando, float randu, float randv, Ray *ray, BsdfEval *eval,
77         bool *is_lamp)
78 {
79         LightSample ls;
80
81 #ifdef __NON_PROGRESSIVE__
82         if(lindex != -1) {
83                 /* sample position on a specified light */
84                 light_select(kg, lindex, randu, randv, sd->P, &ls);
85         }
86         else
87 #endif
88         {
89                 /* sample a light and position on int */
90                 light_sample(kg, randt, randu, randv, sd->time, sd->P, &ls);
91         }
92
93         if(ls.pdf == 0.0f)
94                 return false;
95
96         /* todo: implement */
97         differential3 dD = differential3_zero();
98
99         /* evaluate closure */
100         float3 light_eval = direct_emissive_eval(kg, rando, &ls, randu, randv, -ls.D, dD, ls.t, sd->time);
101
102         if(is_zero(light_eval))
103                 return false;
104
105         /* evaluate BSDF at shading point */
106         float bsdf_pdf;
107
108         shader_bsdf_eval(kg, sd, ls.D, eval, &bsdf_pdf);
109
110         if(ls.shader & SHADER_USE_MIS) {
111                 /* multiple importance sampling */
112                 float mis_weight = power_heuristic(ls.pdf, bsdf_pdf);
113                 light_eval *= mis_weight;
114         }
115         
116         bsdf_eval_mul(eval, light_eval/ls.pdf);
117
118 #ifdef __PASSES__
119         /* use visibility flag to skip lights */
120         if(ls.shader & SHADER_EXCLUDE_ANY) {
121                 if(ls.shader & SHADER_EXCLUDE_DIFFUSE)
122                         eval->diffuse = make_float3(0.0f, 0.0f, 0.0f);
123                 if(ls.shader & SHADER_EXCLUDE_GLOSSY)
124                         eval->glossy = make_float3(0.0f, 0.0f, 0.0f);
125                 if(ls.shader & SHADER_EXCLUDE_TRANSMIT)
126                         eval->transmission = make_float3(0.0f, 0.0f, 0.0f);
127         }
128 #endif
129
130         if(bsdf_eval_is_zero(eval))
131                 return false;
132
133         if(ls.shader & SHADER_CAST_SHADOW) {
134                 /* setup ray */
135                 bool transmit = (dot(sd->Ng, ls.D) < 0.0f);
136                 ray->P = ray_offset(sd->P, (transmit)? -sd->Ng: sd->Ng);
137
138                 if(ls.t == FLT_MAX) {
139                         /* distant light */
140                         ray->D = ls.D;
141                         ray->t = ls.t;
142                 }
143                 else {
144                         /* other lights, avoid self-intersection */
145                         ray->D = ray_offset(ls.P, ls.Ng) - ray->P;
146                         ray->D = normalize_len(ray->D, &ray->t);
147                 }
148
149                 ray->dP = sd->dP;
150                 ray->dD = differential3_zero();
151         }
152         else {
153                 /* signal to not cast shadow ray */
154                 ray->t = 0.0f;
155         }
156
157         /* return if it's a lamp for shadow pass */
158         *is_lamp = (ls.prim == ~0 && ls.type != LIGHT_BACKGROUND);
159
160         return true;
161 }
162
163 /* Indirect Primitive Emission */
164
165 __device_noinline float3 indirect_primitive_emission(KernelGlobals *kg, ShaderData *sd, float t, int path_flag, float bsdf_pdf)
166 {
167         /* evaluate emissive closure */
168         float3 L = shader_emissive_eval(kg, sd);
169
170 #ifdef __HAIR__
171         if(!(path_flag & PATH_RAY_MIS_SKIP) && (sd->flag & SD_SAMPLE_AS_LIGHT) && (sd->segment == ~0)) {
172 #else
173         if(!(path_flag & PATH_RAY_MIS_SKIP) && (sd->flag & SD_SAMPLE_AS_LIGHT)) {
174 #endif
175                 /* multiple importance sampling, get triangle light pdf,
176                  * and compute weight with respect to BSDF pdf */
177                 float pdf = triangle_light_pdf(kg, sd->Ng, sd->I, t);
178                 float mis_weight = power_heuristic(bsdf_pdf, pdf);
179
180                 return L*mis_weight;
181         }
182
183         return L;
184 }
185
186 /* Indirect Lamp Emission */
187
188 __device_noinline bool indirect_lamp_emission(KernelGlobals *kg, Ray *ray, int path_flag, float bsdf_pdf, float randt, float3 *emission)
189 {
190         LightSample ls;
191         int lamp = lamp_light_eval_sample(kg, randt);
192
193         if(lamp == ~0)
194                 return false;
195
196         if(!lamp_light_eval(kg, lamp, ray->P, ray->D, ray->t, &ls))
197                 return false;
198
199 #ifdef __PASSES__
200         /* use visibility flag to skip lights */
201         if(ls.shader & SHADER_EXCLUDE_ANY) {
202                 if((ls.shader & SHADER_EXCLUDE_DIFFUSE) && (path_flag & PATH_RAY_DIFFUSE))
203                         return false;
204                 if((ls.shader & SHADER_EXCLUDE_GLOSSY) && (path_flag & PATH_RAY_GLOSSY))
205                         return false;
206                 if((ls.shader & SHADER_EXCLUDE_TRANSMIT) && (path_flag & PATH_RAY_TRANSMIT))
207                         return false;
208         }
209 #endif
210
211         /* todo: missing texture coordinates */
212         float u = 0.0f;
213         float v = 0.0f;
214         float3 L = direct_emissive_eval(kg, 0.0f, &ls, u, v, -ray->D, ray->dD, ls.t, ray->time);
215
216         if(!(path_flag & PATH_RAY_MIS_SKIP)) {
217                 /* multiple importance sampling, get regular light pdf,
218                  * and compute weight with respect to BSDF pdf */
219                 float mis_weight = power_heuristic(bsdf_pdf, ls.pdf);
220                 L *= mis_weight;
221         }
222
223         *emission = L;
224         return true;
225 }
226
227 /* Indirect Background */
228
229 __device_noinline float3 indirect_background(KernelGlobals *kg, Ray *ray, int path_flag, float bsdf_pdf)
230 {
231 #ifdef __BACKGROUND__
232         /* evaluate background closure */
233         ShaderData sd;
234         shader_setup_from_background(kg, &sd, ray);
235         float3 L = shader_eval_background(kg, &sd, path_flag, SHADER_CONTEXT_EMISSION);
236
237 #ifdef __BACKGROUND_MIS__
238         /* check if background light exists or if we should skip pdf */
239         int res = kernel_data.integrator.pdf_background_res;
240
241         if(!(path_flag & PATH_RAY_MIS_SKIP) && res) {
242                 /* multiple importance sampling, get background light pdf for ray
243                  * direction, and compute weight with respect to BSDF pdf */
244                 float pdf = background_light_pdf(kg, ray->D);
245                 float mis_weight = power_heuristic(bsdf_pdf, pdf);
246
247                 return L*mis_weight;
248         }
249 #endif
250
251         return L;
252 #else
253         return make_float3(0.8f, 0.8f, 0.8f);
254 #endif
255 }
256
257 CCL_NAMESPACE_END
258