Fix #34226: cycles shadow pass got incorrectly influenced by world multiple
[blender.git] / intern / cycles / kernel / kernel_passes.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 __device_inline void kernel_write_pass_float(__global float *buffer, int sample, float value)
22 {
23         __global float *buf = buffer;
24         *buf = (sample == 0)? value: *buf + value;
25 }
26
27 __device_inline void kernel_write_pass_float3(__global float *buffer, int sample, float3 value)
28 {
29         __global float3 *buf = (__global float3*)buffer;
30         *buf = (sample == 0)? value: *buf + value;
31 }
32
33 __device_inline void kernel_write_pass_float4(__global float *buffer, int sample, float4 value)
34 {
35         __global float4 *buf = (__global float4*)buffer;
36         *buf = (sample == 0)? value: *buf + value;
37 }
38
39 __device_inline void kernel_write_data_passes(KernelGlobals *kg, __global float *buffer, PathRadiance *L,
40         ShaderData *sd, int sample, int path_flag, float3 throughput)
41 {
42 #ifdef __PASSES__
43         if(!(path_flag & PATH_RAY_CAMERA))
44                 return;
45
46         int flag = kernel_data.film.pass_flag;
47
48         if(!(flag & PASS_ALL))
49                 return;
50         
51         /* todo: add alpha treshold */
52         if(!(path_flag & PATH_RAY_TRANSPARENT)) {
53                 if(sample == 0) {
54                         if(flag & PASS_DEPTH) {
55                                 float depth = camera_distance(kg, sd->P);
56                                 kernel_write_pass_float(buffer + kernel_data.film.pass_depth, sample, depth);
57                         }
58                         if(flag & PASS_OBJECT_ID) {
59                                 float id = object_pass_id(kg, sd->object);
60                                 kernel_write_pass_float(buffer + kernel_data.film.pass_object_id, sample, id);
61                         }
62                         if(flag & PASS_MATERIAL_ID) {
63                                 float id = shader_pass_id(kg, sd);
64                                 kernel_write_pass_float(buffer + kernel_data.film.pass_material_id, sample, id);
65                         }
66                 }
67
68                 if(flag & PASS_NORMAL) {
69                         float3 normal = sd->N;
70                         kernel_write_pass_float3(buffer + kernel_data.film.pass_normal, sample, normal);
71                 }
72                 if(flag & PASS_UV) {
73                         float3 uv = primitive_uv(kg, sd);
74                         kernel_write_pass_float3(buffer + kernel_data.film.pass_uv, sample, uv);
75                 }
76                 if(flag & PASS_MOTION) {
77                         float4 speed = primitive_motion_vector(kg, sd);
78                         kernel_write_pass_float4(buffer + kernel_data.film.pass_motion, sample, speed);
79                         kernel_write_pass_float(buffer + kernel_data.film.pass_motion_weight, sample, 1.0f);
80                 }
81         }
82
83         if(flag & (PASS_DIFFUSE_INDIRECT|PASS_DIFFUSE_COLOR|PASS_DIFFUSE_DIRECT))
84                 L->color_diffuse += shader_bsdf_diffuse(kg, sd)*throughput;
85         if(flag & (PASS_GLOSSY_INDIRECT|PASS_GLOSSY_COLOR|PASS_GLOSSY_DIRECT))
86                 L->color_glossy += shader_bsdf_glossy(kg, sd)*throughput;
87         if(flag & (PASS_TRANSMISSION_INDIRECT|PASS_TRANSMISSION_COLOR|PASS_TRANSMISSION_DIRECT))
88                 L->color_transmission += shader_bsdf_transmission(kg, sd)*throughput;
89 #endif
90 }
91
92 __device_inline void kernel_write_light_passes(KernelGlobals *kg, __global float *buffer, PathRadiance *L, int sample)
93 {
94 #ifdef __PASSES__
95         int flag = kernel_data.film.pass_flag;
96
97         if(!kernel_data.film.use_light_pass)
98                 return;
99         
100         if(flag & PASS_DIFFUSE_INDIRECT)
101                 kernel_write_pass_float3(buffer + kernel_data.film.pass_diffuse_indirect, sample, L->indirect_diffuse);
102         if(flag & PASS_GLOSSY_INDIRECT)
103                 kernel_write_pass_float3(buffer + kernel_data.film.pass_glossy_indirect, sample, L->indirect_glossy);
104         if(flag & PASS_TRANSMISSION_INDIRECT)
105                 kernel_write_pass_float3(buffer + kernel_data.film.pass_transmission_indirect, sample, L->indirect_transmission);
106         if(flag & PASS_DIFFUSE_DIRECT)
107                 kernel_write_pass_float3(buffer + kernel_data.film.pass_diffuse_direct, sample, L->direct_diffuse);
108         if(flag & PASS_GLOSSY_DIRECT)
109                 kernel_write_pass_float3(buffer + kernel_data.film.pass_glossy_direct, sample, L->direct_glossy);
110         if(flag & PASS_TRANSMISSION_DIRECT)
111                 kernel_write_pass_float3(buffer + kernel_data.film.pass_transmission_direct, sample, L->direct_transmission);
112
113         if(flag & PASS_EMISSION)
114                 kernel_write_pass_float3(buffer + kernel_data.film.pass_emission, sample, L->emission);
115         if(flag & PASS_BACKGROUND)
116                 kernel_write_pass_float3(buffer + kernel_data.film.pass_background, sample, L->background);
117         if(flag & PASS_AO)
118                 kernel_write_pass_float3(buffer + kernel_data.film.pass_ao, sample, L->ao);
119
120         if(flag & PASS_DIFFUSE_COLOR)
121                 kernel_write_pass_float3(buffer + kernel_data.film.pass_diffuse_color, sample, L->color_diffuse);
122         if(flag & PASS_GLOSSY_COLOR)
123                 kernel_write_pass_float3(buffer + kernel_data.film.pass_glossy_color, sample, L->color_glossy);
124         if(flag & PASS_TRANSMISSION_COLOR)
125                 kernel_write_pass_float3(buffer + kernel_data.film.pass_transmission_color, sample, L->color_transmission);
126         if(flag & PASS_SHADOW) {
127                 float4 shadow = L->shadow;
128                 shadow.w = kernel_data.film.pass_shadow_scale;
129                 kernel_write_pass_float4(buffer + kernel_data.film.pass_shadow, sample, shadow);
130         }
131 #endif
132 }
133
134 CCL_NAMESPACE_END
135