Fix non-progressive lamps with multiple samples not giving correct intensity after
[blender.git] / intern / cycles / kernel / kernel_accumulate.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 /* BSDF Eval
22  *
23  * BSDF evaluation result, split per BSDF type. This is used to accumulate
24  * render passes separately. */
25
26 __device_inline void bsdf_eval_init(BsdfEval *eval, ClosureType type, float3 value, int use_light_pass)
27 {
28 #ifdef __PASSES__
29         eval->use_light_pass = use_light_pass;
30
31         if(eval->use_light_pass) {
32                 eval->diffuse = make_float3(0.0f, 0.0f, 0.0f);
33                 eval->glossy = make_float3(0.0f, 0.0f, 0.0f);
34                 eval->transmission = make_float3(0.0f, 0.0f, 0.0f);
35                 eval->transparent = make_float3(0.0f, 0.0f, 0.0f);
36
37                 if(type == CLOSURE_BSDF_TRANSPARENT_ID)
38                         eval->transparent = value;
39                 else if(CLOSURE_IS_BSDF_DIFFUSE(type))
40                         eval->diffuse = value;
41                 else if(CLOSURE_IS_BSDF_GLOSSY(type))
42                         eval->glossy = value;
43                 else
44                         eval->transmission = value;
45         }
46         else
47                 eval->diffuse = value;
48 #else
49         *eval = value;
50 #endif
51 }
52
53 __device_inline void bsdf_eval_accum(BsdfEval *eval, ClosureType type, float3 value)
54 {
55 #ifdef __PASSES__
56         if(eval->use_light_pass) {
57                 if(CLOSURE_IS_BSDF_DIFFUSE(type))
58                         eval->diffuse += value;
59                 else if(CLOSURE_IS_BSDF_GLOSSY(type))
60                         eval->glossy += value;
61                 else
62                         eval->transmission += value;
63
64                 /* skipping transparent, this function is used by for eval(), will be zero then */
65         }
66         else
67                 eval->diffuse += value;
68 #else
69         *eval += value;
70 #endif
71 }
72
73 __device_inline bool bsdf_eval_is_zero(BsdfEval *eval)
74 {
75 #ifdef __PASSES__
76         if(eval->use_light_pass) {
77                 return is_zero(eval->diffuse)
78                         && is_zero(eval->glossy)
79                         && is_zero(eval->transmission)
80                         && is_zero(eval->transparent);
81         }
82         else
83                 return is_zero(eval->diffuse);
84 #else
85         return is_zero(*eval);
86 #endif
87 }
88
89 __device_inline void bsdf_eval_mul(BsdfEval *eval, float3 value)
90 {
91 #ifdef __PASSES__
92         if(eval->use_light_pass) {
93                 eval->diffuse *= value;
94                 eval->glossy *= value;
95                 eval->transmission *= value;
96
97                 /* skipping transparent, this function is used by for eval(), will be zero then */
98         }
99         else
100                 eval->diffuse *= value;
101 #else
102         *eval *= value;
103 #endif
104 }
105
106 /* Path Radiance
107  *
108  * We accumulate different render passes separately. After summing at the end
109  * to get the combined result, it should be identical. We definte directly
110  * visible as the first non-transparent hit, while indirectly visible are the
111  * bounces after that. */
112
113 __device_inline void path_radiance_init(PathRadiance *L, int use_light_pass)
114 {
115         /* clear all */
116 #ifdef __PASSES__
117         L->use_light_pass = use_light_pass;
118
119         if(use_light_pass) {
120                 L->indirect = make_float3(0.0f, 0.0f, 0.0f);
121                 L->direct_throughput = make_float3(0.0f, 0.0f, 0.0f);
122                 L->direct_emission = make_float3(0.0f, 0.0f, 0.0f);
123
124                 L->color_diffuse = make_float3(0.0f, 0.0f, 0.0f);
125                 L->color_glossy = make_float3(0.0f, 0.0f, 0.0f);
126                 L->color_transmission = make_float3(0.0f, 0.0f, 0.0f);
127
128                 L->direct_diffuse = make_float3(0.0f, 0.0f, 0.0f);
129                 L->direct_glossy = make_float3(0.0f, 0.0f, 0.0f);
130                 L->direct_transmission = make_float3(0.0f, 0.0f, 0.0f);
131
132                 L->indirect_diffuse = make_float3(0.0f, 0.0f, 0.0f);
133                 L->indirect_glossy = make_float3(0.0f, 0.0f, 0.0f);
134                 L->indirect_transmission = make_float3(0.0f, 0.0f, 0.0f);
135
136                 L->path_diffuse = make_float3(0.0f, 0.0f, 0.0f);
137                 L->path_glossy = make_float3(0.0f, 0.0f, 0.0f);
138                 L->path_transmission = make_float3(0.0f, 0.0f, 0.0f);
139
140                 L->emission = make_float3(0.0f, 0.0f, 0.0f);
141                 L->background = make_float3(0.0f, 0.0f, 0.0f);
142                 L->ao = make_float3(0.0f, 0.0f, 0.0f);
143                 L->shadow = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
144         }
145         else
146                 L->emission = make_float3(0.0f, 0.0f, 0.0f);
147 #else
148         *L = make_float3(0.0f, 0.0f, 0.0f);
149 #endif
150 }
151
152 __device_inline void path_radiance_bsdf_bounce(PathRadiance *L, float3 *throughput,
153         BsdfEval *bsdf_eval, float bsdf_pdf, int bounce, int bsdf_label)
154 {
155         float inverse_pdf = 1.0f/bsdf_pdf;
156
157 #ifdef __PASSES__
158         if(L->use_light_pass) {
159                 if(bounce == 0 && !(bsdf_label & LABEL_TRANSPARENT)) {
160                         /* first on directly visible surface */
161                         float3 value = *throughput*inverse_pdf;
162
163                         L->path_diffuse = bsdf_eval->diffuse*value;
164                         L->path_glossy = bsdf_eval->glossy*value;
165                         L->path_transmission = bsdf_eval->transmission*value;
166
167                         *throughput = L->path_diffuse + L->path_glossy + L->path_transmission;
168                         
169                         L->direct_throughput = *throughput;
170                 }
171                 else {
172                         /* transparent bounce before first hit, or indirectly visible through BSDF */
173                         float3 sum = (bsdf_eval->diffuse + bsdf_eval->glossy + bsdf_eval->transmission + bsdf_eval->transparent)*inverse_pdf;
174                         *throughput *= sum;
175                 }
176         }
177         else
178                 *throughput *= bsdf_eval->diffuse*inverse_pdf;
179 #else
180         *throughput *= *bsdf_eval*inverse_pdf;
181 #endif
182 }
183
184 __device_inline void path_radiance_accum_emission(PathRadiance *L, float3 throughput, float3 value, int bounce)
185 {
186 #ifdef __PASSES__
187         if(L->use_light_pass) {
188                 if(bounce == 0)
189                         L->emission += throughput*value;
190                 else if(bounce == 1)
191                         L->direct_emission += throughput*value;
192                 else
193                         L->indirect += throughput*value;
194         }
195         else
196                 L->emission += throughput*value;
197 #else
198         *L += throughput*value;
199 #endif
200 }
201
202 __device_inline void path_radiance_accum_ao(PathRadiance *L, float3 throughput, float3 bsdf, float3 ao, int bounce)
203 {
204 #ifdef __PASSES__
205         if(L->use_light_pass) {
206                 if(bounce == 0) {
207                         /* directly visible lighting */
208                         L->direct_diffuse += throughput*bsdf*ao;
209                         L->ao += throughput*ao;
210                 }
211                 else {
212                         /* indirectly visible lighting after BSDF bounce */
213                         L->indirect += throughput*bsdf*ao;
214                 }
215         }
216         else
217                 L->emission += throughput*bsdf*ao;
218 #else
219         *L += throughput*bsdf*ao;
220 #endif
221 }
222
223 __device_inline void path_radiance_accum_light(PathRadiance *L, float3 throughput, BsdfEval *bsdf_eval, float3 shadow, float shadow_fac, int bounce, bool is_lamp)
224 {
225 #ifdef __PASSES__
226         if(L->use_light_pass) {
227                 if(bounce == 0) {
228                         /* directly visible lighting */
229                         L->direct_diffuse += throughput*bsdf_eval->diffuse*shadow;
230                         L->direct_glossy += throughput*bsdf_eval->glossy*shadow;
231                         L->direct_transmission += throughput*bsdf_eval->transmission*shadow;
232
233                         if(is_lamp) {
234                                 float3 sum = throughput*(bsdf_eval->diffuse + bsdf_eval->glossy + bsdf_eval->transmission);
235
236                                 L->shadow.x += shadow.x*shadow_fac;
237                                 L->shadow.y += shadow.y*shadow_fac;
238                                 L->shadow.z += shadow.z*shadow_fac;
239                                 L->shadow.w += average(sum);
240                         }
241                 }
242                 else {
243                         /* indirectly visible lighting after BSDF bounce */
244                         float3 sum = bsdf_eval->diffuse + bsdf_eval->glossy + bsdf_eval->transmission;
245                         L->indirect += throughput*sum*shadow;
246                 }
247         }
248         else
249                 L->emission += throughput*bsdf_eval->diffuse*shadow;
250 #else
251         *L += throughput*(*bsdf_eval)*shadow;
252 #endif
253 }
254
255 __device_inline void path_radiance_accum_background(PathRadiance *L, float3 throughput, float3 value, int bounce)
256 {
257 #ifdef __PASSES__
258         if(L->use_light_pass) {
259                 if(bounce == 0)
260                         L->background += throughput*value;
261                 else if(bounce == 1)
262                         L->direct_emission += throughput*value;
263                 else
264                         L->indirect += throughput*value;
265         }
266         else
267                 L->emission += throughput*value;
268 #else
269         *L += throughput*value;
270 #endif
271 }
272
273 __device_inline void path_radiance_sum_indirect(PathRadiance *L)
274 {
275 #ifdef __PASSES__
276         /* this division is a bit ugly, but means we only have to keep track of
277          * only a single throughput further along the path, here we recover just
278          * the indirect parth that is not influenced by any particular BSDF type */
279         if(L->use_light_pass) {
280                 L->direct_emission = safe_divide_color(L->direct_emission, L->direct_throughput);
281                 L->direct_diffuse += L->path_diffuse*L->direct_emission;
282                 L->direct_glossy += L->path_glossy*L->direct_emission;
283                 L->direct_transmission += L->path_transmission*L->direct_emission;
284
285                 L->indirect = safe_divide_color(L->indirect, L->direct_throughput);
286                 L->indirect_diffuse += L->path_diffuse*L->indirect;
287                 L->indirect_glossy += L->path_glossy*L->indirect;
288                 L->indirect_transmission += L->path_transmission*L->indirect;
289         }
290 #endif
291 }
292
293 __device_inline void path_radiance_reset_indirect(PathRadiance *L)
294 {
295 #ifdef __PASSES__
296         if(L->use_light_pass) {
297                 L->path_diffuse = make_float3(0.0f, 0.0f, 0.0f);
298                 L->path_glossy = make_float3(0.0f, 0.0f, 0.0f);
299                 L->path_transmission = make_float3(0.0f, 0.0f, 0.0f);
300
301                 L->direct_emission = make_float3(0.0f, 0.0f, 0.0f);
302                 L->indirect = make_float3(0.0f, 0.0f, 0.0f);
303         }
304 #endif
305 }
306
307 __device_inline float3 path_radiance_sum(KernelGlobals *kg, PathRadiance *L)
308 {
309 #ifdef __PASSES__
310         if(L->use_light_pass) {
311                 path_radiance_sum_indirect(L);
312
313                 float3 L_sum = L->emission
314                         + L->direct_diffuse + L->direct_glossy + L->direct_transmission
315                         + L->indirect_diffuse + L->indirect_glossy + L->indirect_transmission;
316
317                 if(!kernel_data.background.transparent)
318                         L_sum += L->background;
319
320                 return L_sum;
321         }
322         else
323                 return L->emission;
324 #else
325         return *L;
326 #endif
327 }
328
329 __device_inline void path_radiance_clamp(PathRadiance *L, float3 *L_sum, float clamp)
330 {
331         float sum = fabsf((*L_sum).x) + fabsf((*L_sum).y) + fabsf((*L_sum).z);
332
333         if(!isfinite(sum)) {
334                 /* invalid value, reject */
335                 *L_sum = make_float3(0.0f, 0.0f, 0.0f);
336
337 #ifdef __PASSES__
338                 if(L->use_light_pass) {
339                         L->direct_diffuse = make_float3(0.0f, 0.0f, 0.0f);
340                         L->direct_glossy = make_float3(0.0f, 0.0f, 0.0f);
341                         L->direct_transmission = make_float3(0.0f, 0.0f, 0.0f);
342
343                         L->indirect_diffuse = make_float3(0.0f, 0.0f, 0.0f);
344                         L->indirect_glossy = make_float3(0.0f, 0.0f, 0.0f);
345                         L->indirect_transmission = make_float3(0.0f, 0.0f, 0.0f);
346
347                         L->emission = make_float3(0.0f, 0.0f, 0.0f);
348                 }
349 #endif
350         }
351         else if(sum > clamp) {
352                 /* value to high, scale down */
353                 float scale = clamp/sum;
354
355                 *L_sum *= scale;
356
357 #ifdef __PASSES__
358                 if(L->use_light_pass) {
359                         L->direct_diffuse *= scale;
360                         L->direct_glossy *= scale;
361                         L->direct_transmission *= scale;
362
363                         L->indirect_diffuse *= scale;
364                         L->indirect_glossy *= scale;
365                         L->indirect_transmission *= scale;
366
367                         L->emission *= scale;
368                 }
369 #endif
370         }
371 }
372
373 CCL_NAMESPACE_END
374