f07e1b1fc649261dba7e7f7ac5bbabba65843817
[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
90         if(flag & PASS_MIST) {
91                 /* bring depth into 0..1 range */
92                 float mist_start = kernel_data.film.mist_start;
93                 float mist_inv_depth = kernel_data.film.mist_inv_depth;
94
95                 float depth = camera_distance(kg, sd->P);
96                 float mist = clamp((depth - mist_start)*mist_inv_depth, 0.0f, 1.0f);
97
98                 /* falloff */
99                 float mist_falloff = kernel_data.film.mist_falloff;
100
101                 if(mist_falloff == 1.0f)
102                         ;
103                 else if(mist_falloff == 2.0f)
104                         mist = mist*mist;
105                 else if(mist_falloff == 0.5f)
106                         mist = sqrtf(mist);
107                 else
108                         mist = powf(mist, mist_falloff);
109
110                 /* modulate by transparency */
111                 float3 alpha = throughput*(make_float3(1.0f, 1.0f, 1.0f) - shader_bsdf_transparency(kg, sd));
112                 L->mist += (1.0f - mist)*average(alpha);
113         }
114 #endif
115 }
116
117 __device_inline void kernel_write_light_passes(KernelGlobals *kg, __global float *buffer, PathRadiance *L, int sample)
118 {
119 #ifdef __PASSES__
120         int flag = kernel_data.film.pass_flag;
121
122         if(!kernel_data.film.use_light_pass)
123                 return;
124         
125         if(flag & PASS_DIFFUSE_INDIRECT)
126                 kernel_write_pass_float3(buffer + kernel_data.film.pass_diffuse_indirect, sample, L->indirect_diffuse);
127         if(flag & PASS_GLOSSY_INDIRECT)
128                 kernel_write_pass_float3(buffer + kernel_data.film.pass_glossy_indirect, sample, L->indirect_glossy);
129         if(flag & PASS_TRANSMISSION_INDIRECT)
130                 kernel_write_pass_float3(buffer + kernel_data.film.pass_transmission_indirect, sample, L->indirect_transmission);
131         if(flag & PASS_DIFFUSE_DIRECT)
132                 kernel_write_pass_float3(buffer + kernel_data.film.pass_diffuse_direct, sample, L->direct_diffuse);
133         if(flag & PASS_GLOSSY_DIRECT)
134                 kernel_write_pass_float3(buffer + kernel_data.film.pass_glossy_direct, sample, L->direct_glossy);
135         if(flag & PASS_TRANSMISSION_DIRECT)
136                 kernel_write_pass_float3(buffer + kernel_data.film.pass_transmission_direct, sample, L->direct_transmission);
137
138         if(flag & PASS_EMISSION)
139                 kernel_write_pass_float3(buffer + kernel_data.film.pass_emission, sample, L->emission);
140         if(flag & PASS_BACKGROUND)
141                 kernel_write_pass_float3(buffer + kernel_data.film.pass_background, sample, L->background);
142         if(flag & PASS_AO)
143                 kernel_write_pass_float3(buffer + kernel_data.film.pass_ao, sample, L->ao);
144
145         if(flag & PASS_DIFFUSE_COLOR)
146                 kernel_write_pass_float3(buffer + kernel_data.film.pass_diffuse_color, sample, L->color_diffuse);
147         if(flag & PASS_GLOSSY_COLOR)
148                 kernel_write_pass_float3(buffer + kernel_data.film.pass_glossy_color, sample, L->color_glossy);
149         if(flag & PASS_TRANSMISSION_COLOR)
150                 kernel_write_pass_float3(buffer + kernel_data.film.pass_transmission_color, sample, L->color_transmission);
151         if(flag & PASS_SHADOW) {
152                 float4 shadow = L->shadow;
153                 shadow.w = kernel_data.film.pass_shadow_scale;
154                 kernel_write_pass_float4(buffer + kernel_data.film.pass_shadow, sample, shadow);
155         }
156         if(flag & PASS_MIST)
157                 kernel_write_pass_float(buffer + kernel_data.film.pass_mist, sample, 1.0f - L->mist);
158 #endif
159 }
160
161 CCL_NAMESPACE_END
162