Cycles: option to make background visible through glass transparent.
[blender-staging.git] / intern / cycles / kernel / kernel_passes.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 #if defined(__SPLIT_KERNEL__) || defined(__KERNEL_CUDA__)
20 #define __ATOMIC_PASS_WRITE__
21 #endif
22
23 ccl_device_inline void kernel_write_pass_float(ccl_global float *buffer, float value)
24 {
25         ccl_global float *buf = buffer;
26 #ifdef __ATOMIC_PASS_WRITE__
27         atomic_add_and_fetch_float(buf, value);
28 #else
29         *buf += value;
30 #endif
31 }
32
33 ccl_device_inline void kernel_write_pass_float3(ccl_global float *buffer, float3 value)
34 {
35 #ifdef __ATOMIC_PASS_WRITE__
36         ccl_global float *buf_x = buffer + 0;
37         ccl_global float *buf_y = buffer + 1;
38         ccl_global float *buf_z = buffer + 2;
39
40         atomic_add_and_fetch_float(buf_x, value.x);
41         atomic_add_and_fetch_float(buf_y, value.y);
42         atomic_add_and_fetch_float(buf_z, value.z);
43 #else
44         ccl_global float3 *buf = (ccl_global float3*)buffer;
45         *buf += value;
46 #endif
47 }
48
49 ccl_device_inline void kernel_write_pass_float4(ccl_global float *buffer, float4 value)
50 {
51 #ifdef __ATOMIC_PASS_WRITE__
52         ccl_global float *buf_x = buffer + 0;
53         ccl_global float *buf_y = buffer + 1;
54         ccl_global float *buf_z = buffer + 2;
55         ccl_global float *buf_w = buffer + 3;
56
57         atomic_add_and_fetch_float(buf_x, value.x);
58         atomic_add_and_fetch_float(buf_y, value.y);
59         atomic_add_and_fetch_float(buf_z, value.z);
60         atomic_add_and_fetch_float(buf_w, value.w);
61 #else
62         ccl_global float4 *buf = (ccl_global float4*)buffer;
63         *buf += value;
64 #endif
65 }
66
67 #ifdef __DENOISING_FEATURES__
68 ccl_device_inline void kernel_write_pass_float_variance(ccl_global float *buffer, float value)
69 {
70         kernel_write_pass_float(buffer, value);
71
72         /* The online one-pass variance update that's used for the megakernel can't easily be implemented
73          * with atomics, so for the split kernel the E[x^2] - 1/N * (E[x])^2 fallback is used. */
74         kernel_write_pass_float(buffer+1, value*value);
75 }
76
77 #  ifdef __ATOMIC_PASS_WRITE__
78 #    define kernel_write_pass_float3_unaligned kernel_write_pass_float3
79 #  else
80 ccl_device_inline void kernel_write_pass_float3_unaligned(ccl_global float *buffer, float3 value)
81 {
82         buffer[0] += value.x;
83         buffer[1] += value.y;
84         buffer[2] += value.z;
85 }
86 #  endif
87
88 ccl_device_inline void kernel_write_pass_float3_variance(ccl_global float *buffer, float3 value)
89 {
90         kernel_write_pass_float3_unaligned(buffer, value);
91         kernel_write_pass_float3_unaligned(buffer+3, value*value);
92 }
93
94 ccl_device_inline void kernel_write_denoising_shadow(KernelGlobals *kg, ccl_global float *buffer,
95         int sample, float path_total, float path_total_shaded)
96 {
97         if(kernel_data.film.pass_denoising_data == 0)
98                 return;
99
100         buffer += (sample & 1)? DENOISING_PASS_SHADOW_B : DENOISING_PASS_SHADOW_A;
101
102         path_total = ensure_finite(path_total);
103         path_total_shaded = ensure_finite(path_total_shaded);
104
105         kernel_write_pass_float(buffer, path_total);
106         kernel_write_pass_float(buffer+1, path_total_shaded);
107
108         float value = path_total_shaded / max(path_total, 1e-7f);
109         kernel_write_pass_float(buffer+2, value*value);
110 }
111 #endif /* __DENOISING_FEATURES__ */
112
113 ccl_device_inline void kernel_update_denoising_features(KernelGlobals *kg,
114                                                         ShaderData *sd,
115                                                         ccl_addr_space PathState *state,
116                                                         PathRadiance *L)
117 {
118 #ifdef __DENOISING_FEATURES__
119         if(state->denoising_feature_weight == 0.0f) {
120                 return;
121         }
122
123         L->denoising_depth += ensure_finite(state->denoising_feature_weight * sd->ray_length);
124
125         /* Skip implicitly transparent surfaces. */
126         if(sd->flag & SD_HAS_ONLY_VOLUME) {
127                 return;
128         }
129
130         float3 normal = make_float3(0.0f, 0.0f, 0.0f);
131         float3 albedo = make_float3(0.0f, 0.0f, 0.0f);
132         float sum_weight = 0.0f, sum_nonspecular_weight = 0.0f;
133
134         for(int i = 0; i < sd->num_closure; i++) {
135                 ShaderClosure *sc = &sd->closure[i];
136
137                 if(!CLOSURE_IS_BSDF_OR_BSSRDF(sc->type))
138                         continue;
139
140                 /* All closures contribute to the normal feature, but only diffuse-like ones to the albedo. */
141                 normal += sc->N * sc->sample_weight;
142                 sum_weight += sc->sample_weight;
143                 if(bsdf_get_roughness_squared(sc) > sqr(0.075f)) {
144                         albedo += sc->weight;
145                         sum_nonspecular_weight += sc->sample_weight;
146                 }
147         }
148
149         /* Wait for next bounce if 75% or more sample weight belongs to specular-like closures. */
150         if((sum_weight == 0.0f) || (sum_nonspecular_weight*4.0f > sum_weight)) {
151                 if(sum_weight != 0.0f) {
152                         normal /= sum_weight;
153                 }
154                 L->denoising_normal += ensure_finite3(state->denoising_feature_weight * normal);
155                 L->denoising_albedo += ensure_finite3(state->denoising_feature_weight * albedo);
156
157                 state->denoising_feature_weight = 0.0f;
158         }
159 #else
160         (void) kg;
161         (void) sd;
162         (void) state;
163         (void) L;
164 #endif  /* __DENOISING_FEATURES__ */
165 }
166
167 #ifdef __KERNEL_DEBUG__
168 ccl_device_inline void kernel_write_debug_passes(KernelGlobals *kg,
169                                                  ccl_global float *buffer,
170                                                  PathRadiance *L)
171 {
172         int flag = kernel_data.film.pass_flag;
173         if(flag & PASSMASK(BVH_TRAVERSED_NODES)) {
174                 kernel_write_pass_float(buffer + kernel_data.film.pass_bvh_traversed_nodes,
175                                         L->debug_data.num_bvh_traversed_nodes);
176         }
177         if(flag & PASSMASK(BVH_TRAVERSED_INSTANCES)) {
178                 kernel_write_pass_float(buffer + kernel_data.film.pass_bvh_traversed_instances,
179                                         L->debug_data.num_bvh_traversed_instances);
180         }
181         if(flag & PASSMASK(BVH_INTERSECTIONS)) {
182                 kernel_write_pass_float(buffer + kernel_data.film.pass_bvh_intersections,
183                                         L->debug_data.num_bvh_intersections);
184         }
185         if(flag & PASSMASK(RAY_BOUNCES)) {
186                 kernel_write_pass_float(buffer + kernel_data.film.pass_ray_bounces,
187                                         L->debug_data.num_ray_bounces);
188         }
189 }
190 #endif /* __KERNEL_DEBUG__ */
191
192 ccl_device_inline void kernel_write_data_passes(KernelGlobals *kg, ccl_global float *buffer, PathRadiance *L,
193         ShaderData *sd, ccl_addr_space PathState *state, float3 throughput)
194 {
195 #ifdef __PASSES__
196         int path_flag = state->flag;
197
198         if(!(path_flag & PATH_RAY_CAMERA))
199                 return;
200
201         int flag = kernel_data.film.pass_flag;
202         int light_flag = kernel_data.film.light_pass_flag;
203
204         if(!((flag | light_flag) & PASS_ANY))
205                 return;
206         
207         if(!(path_flag & PATH_RAY_SINGLE_PASS_DONE)) {
208                 if(!(sd->flag & SD_TRANSPARENT) ||
209                    kernel_data.film.pass_alpha_threshold == 0.0f ||
210                    average(shader_bsdf_alpha(kg, sd)) >= kernel_data.film.pass_alpha_threshold)
211                 {
212                         if(state->sample == 0) {
213                                 if(flag & PASSMASK(DEPTH)) {
214                                         float depth = camera_distance(kg, sd->P);
215                                         kernel_write_pass_float(buffer + kernel_data.film.pass_depth, depth);
216                                 }
217                                 if(flag & PASSMASK(OBJECT_ID)) {
218                                         float id = object_pass_id(kg, sd->object);
219                                         kernel_write_pass_float(buffer + kernel_data.film.pass_object_id, id);
220                                 }
221                                 if(flag & PASSMASK(MATERIAL_ID)) {
222                                         float id = shader_pass_id(kg, sd);
223                                         kernel_write_pass_float(buffer + kernel_data.film.pass_material_id, id);
224                                 }
225                         }
226
227                         if(flag & PASSMASK(NORMAL)) {
228                                 float3 normal = shader_bsdf_average_normal(kg, sd);
229                                 kernel_write_pass_float3(buffer + kernel_data.film.pass_normal, normal);
230                         }
231                         if(flag & PASSMASK(UV)) {
232                                 float3 uv = primitive_uv(kg, sd);
233                                 kernel_write_pass_float3(buffer + kernel_data.film.pass_uv, uv);
234                         }
235                         if(flag & PASSMASK(MOTION)) {
236                                 float4 speed = primitive_motion_vector(kg, sd);
237                                 kernel_write_pass_float4(buffer + kernel_data.film.pass_motion, speed);
238                                 kernel_write_pass_float(buffer + kernel_data.film.pass_motion_weight, 1.0f);
239                         }
240
241                         state->flag |= PATH_RAY_SINGLE_PASS_DONE;
242                 }
243         }
244
245         if(light_flag & PASSMASK_COMPONENT(DIFFUSE))
246                 L->color_diffuse += shader_bsdf_diffuse(kg, sd)*throughput;
247         if(light_flag & PASSMASK_COMPONENT(GLOSSY))
248                 L->color_glossy += shader_bsdf_glossy(kg, sd)*throughput;
249         if(light_flag & PASSMASK_COMPONENT(TRANSMISSION))
250                 L->color_transmission += shader_bsdf_transmission(kg, sd)*throughput;
251         if(light_flag & PASSMASK_COMPONENT(SUBSURFACE))
252                 L->color_subsurface += shader_bsdf_subsurface(kg, sd)*throughput;
253
254         if(light_flag & PASSMASK(MIST)) {
255                 /* bring depth into 0..1 range */
256                 float mist_start = kernel_data.film.mist_start;
257                 float mist_inv_depth = kernel_data.film.mist_inv_depth;
258
259                 float depth = camera_distance(kg, sd->P);
260                 float mist = saturate((depth - mist_start)*mist_inv_depth);
261
262                 /* falloff */
263                 float mist_falloff = kernel_data.film.mist_falloff;
264
265                 if(mist_falloff == 1.0f)
266                         ;
267                 else if(mist_falloff == 2.0f)
268                         mist = mist*mist;
269                 else if(mist_falloff == 0.5f)
270                         mist = sqrtf(mist);
271                 else
272                         mist = powf(mist, mist_falloff);
273
274                 /* modulate by transparency */
275                 float3 alpha = shader_bsdf_alpha(kg, sd);
276                 L->mist += (1.0f - mist)*average(throughput*alpha);
277         }
278 #endif
279 }
280
281 ccl_device_inline void kernel_write_light_passes(KernelGlobals *kg, ccl_global float *buffer, PathRadiance *L)
282 {
283 #ifdef __PASSES__
284         int light_flag = kernel_data.film.light_pass_flag;
285
286         if(!kernel_data.film.use_light_pass)
287                 return;
288         
289         if(light_flag & PASSMASK(DIFFUSE_INDIRECT))
290                 kernel_write_pass_float3(buffer + kernel_data.film.pass_diffuse_indirect, L->indirect_diffuse);
291         if(light_flag & PASSMASK(GLOSSY_INDIRECT))
292                 kernel_write_pass_float3(buffer + kernel_data.film.pass_glossy_indirect, L->indirect_glossy);
293         if(light_flag & PASSMASK(TRANSMISSION_INDIRECT))
294                 kernel_write_pass_float3(buffer + kernel_data.film.pass_transmission_indirect, L->indirect_transmission);
295         if(light_flag & PASSMASK(SUBSURFACE_INDIRECT))
296                 kernel_write_pass_float3(buffer + kernel_data.film.pass_subsurface_indirect, L->indirect_subsurface);
297         if(light_flag & PASSMASK(VOLUME_INDIRECT))
298                 kernel_write_pass_float3(buffer + kernel_data.film.pass_volume_indirect, L->indirect_scatter);
299         if(light_flag & PASSMASK(DIFFUSE_DIRECT))
300                 kernel_write_pass_float3(buffer + kernel_data.film.pass_diffuse_direct, L->direct_diffuse);
301         if(light_flag & PASSMASK(GLOSSY_DIRECT))
302                 kernel_write_pass_float3(buffer + kernel_data.film.pass_glossy_direct, L->direct_glossy);
303         if(light_flag & PASSMASK(TRANSMISSION_DIRECT))
304                 kernel_write_pass_float3(buffer + kernel_data.film.pass_transmission_direct, L->direct_transmission);
305         if(light_flag & PASSMASK(SUBSURFACE_DIRECT))
306                 kernel_write_pass_float3(buffer + kernel_data.film.pass_subsurface_direct, L->direct_subsurface);
307         if(light_flag & PASSMASK(VOLUME_DIRECT))
308                 kernel_write_pass_float3(buffer + kernel_data.film.pass_volume_direct, L->direct_scatter);
309
310         if(light_flag & PASSMASK(EMISSION))
311                 kernel_write_pass_float3(buffer + kernel_data.film.pass_emission, L->emission);
312         if(light_flag & PASSMASK(BACKGROUND))
313                 kernel_write_pass_float3(buffer + kernel_data.film.pass_background, L->background);
314         if(light_flag & PASSMASK(AO))
315                 kernel_write_pass_float3(buffer + kernel_data.film.pass_ao, L->ao);
316
317         if(light_flag & PASSMASK(DIFFUSE_COLOR))
318                 kernel_write_pass_float3(buffer + kernel_data.film.pass_diffuse_color, L->color_diffuse);
319         if(light_flag & PASSMASK(GLOSSY_COLOR))
320                 kernel_write_pass_float3(buffer + kernel_data.film.pass_glossy_color, L->color_glossy);
321         if(light_flag & PASSMASK(TRANSMISSION_COLOR))
322                 kernel_write_pass_float3(buffer + kernel_data.film.pass_transmission_color, L->color_transmission);
323         if(light_flag & PASSMASK(SUBSURFACE_COLOR))
324                 kernel_write_pass_float3(buffer + kernel_data.film.pass_subsurface_color, L->color_subsurface);
325         if(light_flag & PASSMASK(SHADOW)) {
326                 float4 shadow = L->shadow;
327                 shadow.w = kernel_data.film.pass_shadow_scale;
328                 kernel_write_pass_float4(buffer + kernel_data.film.pass_shadow, shadow);
329         }
330         if(light_flag & PASSMASK(MIST))
331                 kernel_write_pass_float(buffer + kernel_data.film.pass_mist, 1.0f - L->mist);
332 #endif
333 }
334
335 ccl_device_inline void kernel_write_result(KernelGlobals *kg,
336                                            ccl_global float *buffer,
337                                            int sample,
338                                            PathRadiance *L)
339 {
340         float alpha;
341         float3 L_sum = path_radiance_clamp_and_sum(kg, L, &alpha);
342
343         kernel_write_pass_float4(buffer, make_float4(L_sum.x, L_sum.y, L_sum.z, alpha));
344
345         kernel_write_light_passes(kg, buffer, L);
346
347 #ifdef __DENOISING_FEATURES__
348         if(kernel_data.film.pass_denoising_data) {
349 #  ifdef __SHADOW_TRICKS__
350                 kernel_write_denoising_shadow(kg,
351                                               buffer + kernel_data.film.pass_denoising_data,
352                                               sample,
353                                               average(L->path_total),
354                                               average(L->path_total_shaded));
355 #  else
356                 kernel_write_denoising_shadow(kg,
357                                               buffer + kernel_data.film.pass_denoising_data,
358                                               sample,
359                                               0.0f, 0.0f);
360 #  endif
361                 if(kernel_data.film.pass_denoising_clean) {
362                         float3 noisy, clean;
363                         path_radiance_split_denoising(kg, L, &noisy, &clean);
364                         kernel_write_pass_float3_variance(
365                                 buffer + kernel_data.film.pass_denoising_data + DENOISING_PASS_COLOR,
366                                 noisy);
367                         kernel_write_pass_float3_unaligned(
368                                 buffer + kernel_data.film.pass_denoising_clean,
369                                 clean);
370                 }
371                 else {
372                         kernel_write_pass_float3_variance(buffer + kernel_data.film.pass_denoising_data + DENOISING_PASS_COLOR,
373                                                             ensure_finite3(L_sum));
374                 }
375
376                 kernel_write_pass_float3_variance(
377                         buffer + kernel_data.film.pass_denoising_data + DENOISING_PASS_NORMAL,
378                         L->denoising_normal);
379                 kernel_write_pass_float3_variance(
380                         buffer + kernel_data.film.pass_denoising_data + DENOISING_PASS_ALBEDO,
381                         L->denoising_albedo);
382                 kernel_write_pass_float_variance(
383                         buffer + kernel_data.film.pass_denoising_data + DENOISING_PASS_DEPTH,
384                         L->denoising_depth);
385         }
386 #endif  /* __DENOISING_FEATURES__ */
387
388
389 #ifdef __KERNEL_DEBUG__
390         kernel_write_debug_passes(kg, buffer, L);
391 #endif
392 }
393
394 CCL_NAMESPACE_END
395