Cycles: Remove few function arguments needed only for the split kernel
[blender.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 ccl_device_inline void kernel_write_pass_float(ccl_global float *buffer, int sample, float value)
20 {
21         ccl_global float *buf = buffer;
22 #if defined(__SPLIT_KERNEL__) && defined(__WORK_STEALING__)
23         atomic_add_float(buf, value);
24 #else
25         *buf = (sample == 0)? value: *buf + value;
26 #endif // __SPLIT_KERNEL__ && __WORK_STEALING__
27 }
28
29 ccl_device_inline void kernel_write_pass_float3(ccl_global float *buffer, int sample, float3 value)
30 {
31 #if defined(__SPLIT_KERNEL__) && defined(__WORK_STEALING__)
32         ccl_global float *buf_x = buffer + 0;
33         ccl_global float *buf_y = buffer + 1;
34         ccl_global float *buf_z = buffer + 2;
35
36         atomic_add_float(buf_x, value.x);
37         atomic_add_float(buf_y, value.y);
38         atomic_add_float(buf_z, value.z);
39 #else
40         ccl_global float3 *buf = (ccl_global float3*)buffer;
41         *buf = (sample == 0)? value: *buf + value;
42 #endif // __SPLIT_KERNEL__ && __WORK_STEALING__
43 }
44
45 ccl_device_inline void kernel_write_pass_float4(ccl_global float *buffer, int sample, float4 value)
46 {
47 #if defined(__SPLIT_KERNEL__) && defined(__WORK_STEALING__)
48         ccl_global float *buf_x = buffer + 0;
49         ccl_global float *buf_y = buffer + 1;
50         ccl_global float *buf_z = buffer + 2;
51         ccl_global float *buf_w = buffer + 3;
52
53         atomic_add_float(buf_x, value.x);
54         atomic_add_float(buf_y, value.y);
55         atomic_add_float(buf_z, value.z);
56         atomic_add_float(buf_w, value.w);
57 #else
58         ccl_global float4 *buf = (ccl_global float4*)buffer;
59         *buf = (sample == 0)? value: *buf + value;
60 #endif // __SPLIT_KERNEL__ && __WORK_STEALING__
61 }
62
63 ccl_device_inline void kernel_write_data_passes(KernelGlobals *kg, ccl_global float *buffer, PathRadiance *L,
64         ShaderData *sd, int sample, ccl_addr_space PathState *state, float3 throughput)
65 {
66 #ifdef __PASSES__
67         int path_flag = state->flag;
68
69         if(!(path_flag & PATH_RAY_CAMERA))
70                 return;
71
72         int flag = kernel_data.film.pass_flag;
73
74         if(!(flag & PASS_ALL))
75                 return;
76         
77         if(!(path_flag & PATH_RAY_SINGLE_PASS_DONE)) {
78                 if(!(ccl_fetch(sd, flag) & SD_TRANSPARENT) ||
79                    kernel_data.film.pass_alpha_threshold == 0.0f ||
80                    average(shader_bsdf_alpha(kg, sd)) >= kernel_data.film.pass_alpha_threshold)
81                 {
82
83                         if(sample == 0) {
84                                 if(flag & PASS_DEPTH) {
85                                         float depth = camera_distance(kg, ccl_fetch(sd, P));
86                                         kernel_write_pass_float(buffer + kernel_data.film.pass_depth, sample, depth);
87                                 }
88                                 if(flag & PASS_OBJECT_ID) {
89                                         float id = object_pass_id(kg, ccl_fetch(sd, object));
90                                         kernel_write_pass_float(buffer + kernel_data.film.pass_object_id, sample, id);
91                                 }
92                                 if(flag & PASS_MATERIAL_ID) {
93                                         float id = shader_pass_id(kg, sd);
94                                         kernel_write_pass_float(buffer + kernel_data.film.pass_material_id, sample, id);
95                                 }
96                         }
97
98                         if(flag & PASS_NORMAL) {
99                                 float3 normal = ccl_fetch(sd, N);
100                                 kernel_write_pass_float3(buffer + kernel_data.film.pass_normal, sample, normal);
101                         }
102                         if(flag & PASS_UV) {
103                                 float3 uv = primitive_uv(kg, sd);
104                                 kernel_write_pass_float3(buffer + kernel_data.film.pass_uv, sample, uv);
105                         }
106                         if(flag & PASS_MOTION) {
107                                 float4 speed = primitive_motion_vector(kg, sd);
108                                 kernel_write_pass_float4(buffer + kernel_data.film.pass_motion, sample, speed);
109                                 kernel_write_pass_float(buffer + kernel_data.film.pass_motion_weight, sample, 1.0f);
110                         }
111
112                         state->flag |= PATH_RAY_SINGLE_PASS_DONE;
113                 }
114         }
115
116         if(flag & (PASS_DIFFUSE_INDIRECT|PASS_DIFFUSE_COLOR|PASS_DIFFUSE_DIRECT))
117                 L->color_diffuse += shader_bsdf_diffuse(kg, sd)*throughput;
118         if(flag & (PASS_GLOSSY_INDIRECT|PASS_GLOSSY_COLOR|PASS_GLOSSY_DIRECT))
119                 L->color_glossy += shader_bsdf_glossy(kg, sd)*throughput;
120         if(flag & (PASS_TRANSMISSION_INDIRECT|PASS_TRANSMISSION_COLOR|PASS_TRANSMISSION_DIRECT))
121                 L->color_transmission += shader_bsdf_transmission(kg, sd)*throughput;
122         if(flag & (PASS_SUBSURFACE_INDIRECT|PASS_SUBSURFACE_COLOR|PASS_SUBSURFACE_DIRECT))
123                 L->color_subsurface += shader_bsdf_subsurface(kg, sd)*throughput;
124
125         if(flag & PASS_MIST) {
126                 /* bring depth into 0..1 range */
127                 float mist_start = kernel_data.film.mist_start;
128                 float mist_inv_depth = kernel_data.film.mist_inv_depth;
129
130                 float depth = camera_distance(kg, ccl_fetch(sd, P));
131                 float mist = saturate((depth - mist_start)*mist_inv_depth);
132
133                 /* falloff */
134                 float mist_falloff = kernel_data.film.mist_falloff;
135
136                 if(mist_falloff == 1.0f)
137                         ;
138                 else if(mist_falloff == 2.0f)
139                         mist = mist*mist;
140                 else if(mist_falloff == 0.5f)
141                         mist = sqrtf(mist);
142                 else
143                         mist = powf(mist, mist_falloff);
144
145                 /* modulate by transparency */
146                 float3 alpha = shader_bsdf_alpha(kg, sd);
147                 L->mist += (1.0f - mist)*average(throughput*alpha);
148         }
149 #endif
150 }
151
152 ccl_device_inline void kernel_write_light_passes(KernelGlobals *kg, ccl_global float *buffer, PathRadiance *L, int sample)
153 {
154 #ifdef __PASSES__
155         int flag = kernel_data.film.pass_flag;
156
157         if(!kernel_data.film.use_light_pass)
158                 return;
159         
160         if(flag & PASS_DIFFUSE_INDIRECT)
161                 kernel_write_pass_float3(buffer + kernel_data.film.pass_diffuse_indirect, sample, L->indirect_diffuse);
162         if(flag & PASS_GLOSSY_INDIRECT)
163                 kernel_write_pass_float3(buffer + kernel_data.film.pass_glossy_indirect, sample, L->indirect_glossy);
164         if(flag & PASS_TRANSMISSION_INDIRECT)
165                 kernel_write_pass_float3(buffer + kernel_data.film.pass_transmission_indirect, sample, L->indirect_transmission);
166         if(flag & PASS_SUBSURFACE_INDIRECT)
167                 kernel_write_pass_float3(buffer + kernel_data.film.pass_subsurface_indirect, sample, L->indirect_subsurface);
168         if(flag & PASS_DIFFUSE_DIRECT)
169                 kernel_write_pass_float3(buffer + kernel_data.film.pass_diffuse_direct, sample, L->direct_diffuse);
170         if(flag & PASS_GLOSSY_DIRECT)
171                 kernel_write_pass_float3(buffer + kernel_data.film.pass_glossy_direct, sample, L->direct_glossy);
172         if(flag & PASS_TRANSMISSION_DIRECT)
173                 kernel_write_pass_float3(buffer + kernel_data.film.pass_transmission_direct, sample, L->direct_transmission);
174         if(flag & PASS_SUBSURFACE_DIRECT)
175                 kernel_write_pass_float3(buffer + kernel_data.film.pass_subsurface_direct, sample, L->direct_subsurface);
176
177         if(flag & PASS_EMISSION)
178                 kernel_write_pass_float3(buffer + kernel_data.film.pass_emission, sample, L->emission);
179         if(flag & PASS_BACKGROUND)
180                 kernel_write_pass_float3(buffer + kernel_data.film.pass_background, sample, L->background);
181         if(flag & PASS_AO)
182                 kernel_write_pass_float3(buffer + kernel_data.film.pass_ao, sample, L->ao);
183
184         if(flag & PASS_DIFFUSE_COLOR)
185                 kernel_write_pass_float3(buffer + kernel_data.film.pass_diffuse_color, sample, L->color_diffuse);
186         if(flag & PASS_GLOSSY_COLOR)
187                 kernel_write_pass_float3(buffer + kernel_data.film.pass_glossy_color, sample, L->color_glossy);
188         if(flag & PASS_TRANSMISSION_COLOR)
189                 kernel_write_pass_float3(buffer + kernel_data.film.pass_transmission_color, sample, L->color_transmission);
190         if(flag & PASS_SUBSURFACE_COLOR)
191                 kernel_write_pass_float3(buffer + kernel_data.film.pass_subsurface_color, sample, L->color_subsurface);
192         if(flag & PASS_SHADOW) {
193                 float4 shadow = L->shadow;
194                 shadow.w = kernel_data.film.pass_shadow_scale;
195                 kernel_write_pass_float4(buffer + kernel_data.film.pass_shadow, sample, shadow);
196         }
197         if(flag & PASS_MIST)
198                 kernel_write_pass_float(buffer + kernel_data.film.pass_mist, sample, 1.0f - L->mist);
199 #endif
200 }
201
202 CCL_NAMESPACE_END
203