Remove redundant check for armature mode
[blender.git] / intern / cycles / kernel / kernel_path_state.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 path_state_init(KernelGlobals *kg,
20                                        ShaderData *stack_sd,
21                                        ccl_addr_space PathState *state,
22                                        uint rng_hash,
23                                        int sample,
24                                        ccl_addr_space Ray *ray)
25 {
26         state->flag = PATH_RAY_CAMERA|PATH_RAY_MIS_SKIP|PATH_RAY_TRANSPARENT_BACKGROUND;
27
28         state->rng_hash = rng_hash;
29         state->rng_offset = PRNG_BASE_NUM;
30         state->sample = sample;
31         state->num_samples = kernel_data.integrator.aa_samples;
32         state->branch_factor = 1.0f;
33
34         state->bounce = 0;
35         state->diffuse_bounce = 0;
36         state->glossy_bounce = 0;
37         state->transmission_bounce = 0;
38         state->transparent_bounce = 0;
39
40 #ifdef __DENOISING_FEATURES__
41         if(kernel_data.film.pass_denoising_data) {
42                 state->flag |= PATH_RAY_STORE_SHADOW_INFO;
43                 state->denoising_feature_weight = 1.0f;
44         }
45         else {
46                 state->denoising_feature_weight = 0.0f;
47         }
48 #endif  /* __DENOISING_FEATURES__ */
49
50         state->min_ray_pdf = FLT_MAX;
51         state->ray_pdf = 0.0f;
52 #ifdef __LAMP_MIS__
53         state->ray_t = 0.0f;
54 #endif
55
56 #ifdef __VOLUME__
57         state->volume_bounce = 0;
58
59         if(kernel_data.integrator.use_volumes) {
60                 /* Initialize volume stack with volume we are inside of. */
61                 kernel_volume_stack_init(kg, stack_sd, state, ray, state->volume_stack);
62                 /* Seed RNG for cases where we can't use stratified samples .*/
63                 state->rng_congruential = lcg_init(rng_hash + sample*0x51633e2d);
64         }
65         else {
66                 state->volume_stack[0].shader = SHADER_NONE;
67         }
68 #endif
69 }
70
71 ccl_device_inline void path_state_next(KernelGlobals *kg, ccl_addr_space PathState *state, int label)
72 {
73         /* ray through transparent keeps same flags from previous ray and is
74          * not counted as a regular bounce, transparent has separate max */
75         if(label & LABEL_TRANSPARENT) {
76                 state->flag |= PATH_RAY_TRANSPARENT;
77                 state->transparent_bounce++;
78
79                 if(!kernel_data.integrator.transparent_shadows)
80                         state->flag |= PATH_RAY_MIS_SKIP;
81
82                 /* random number generator next bounce */
83                 state->rng_offset += PRNG_BOUNCE_NUM;
84
85                 return;
86         }
87
88         state->bounce++;
89         state->flag &= ~(PATH_RAY_ALL_VISIBILITY|PATH_RAY_MIS_SKIP);
90
91 #ifdef __VOLUME__
92         if(label & LABEL_VOLUME_SCATTER) {
93                 /* volume scatter */
94                 state->flag |= PATH_RAY_VOLUME_SCATTER;
95                 state->flag &= ~PATH_RAY_TRANSPARENT_BACKGROUND;
96
97                 state->volume_bounce++;
98         }
99         else
100 #endif
101         {
102                 /* surface reflection/transmission */
103                 if(label & LABEL_REFLECT) {
104                         state->flag |= PATH_RAY_REFLECT;
105                         state->flag &= ~PATH_RAY_TRANSPARENT_BACKGROUND;
106
107                         if(label & LABEL_DIFFUSE)
108                                 state->diffuse_bounce++;
109                         else
110                                 state->glossy_bounce++;
111                 }
112                 else {
113                         kernel_assert(label & LABEL_TRANSMIT);
114
115                         state->flag |= PATH_RAY_TRANSMIT;
116
117                         if(!(label & LABEL_TRANSMIT_TRANSPARENT)) {
118                                 state->flag &= ~PATH_RAY_TRANSPARENT_BACKGROUND;
119                         }
120
121                         state->transmission_bounce++;
122                 }
123
124                 /* diffuse/glossy/singular */
125                 if(label & LABEL_DIFFUSE) {
126                         state->flag |= PATH_RAY_DIFFUSE|PATH_RAY_DIFFUSE_ANCESTOR;
127                 }
128                 else if(label & LABEL_GLOSSY) {
129                         state->flag |= PATH_RAY_GLOSSY;
130                 }
131                 else {
132                         kernel_assert(label & LABEL_SINGULAR);
133                         state->flag |= PATH_RAY_GLOSSY|PATH_RAY_SINGULAR|PATH_RAY_MIS_SKIP;
134                 }
135         }
136
137         /* random number generator next bounce */
138         state->rng_offset += PRNG_BOUNCE_NUM;
139
140 #ifdef __DENOISING_FEATURES__
141         if((state->denoising_feature_weight == 0.0f) && !(state->flag & PATH_RAY_SHADOW_CATCHER)) {
142                 state->flag &= ~PATH_RAY_STORE_SHADOW_INFO;
143         }
144 #endif
145 }
146
147 ccl_device_inline uint path_state_ray_visibility(KernelGlobals *kg, ccl_addr_space PathState *state)
148 {
149         uint flag = state->flag & PATH_RAY_ALL_VISIBILITY;
150
151         /* for visibility, diffuse/glossy are for reflection only */
152         if(flag & PATH_RAY_TRANSMIT)
153                 flag &= ~(PATH_RAY_DIFFUSE|PATH_RAY_GLOSSY);
154         /* todo: this is not supported as its own ray visibility yet */
155         if(state->flag & PATH_RAY_VOLUME_SCATTER)
156                 flag |= PATH_RAY_DIFFUSE;
157
158         return flag;
159 }
160
161 ccl_device_inline float path_state_continuation_probability(KernelGlobals *kg,
162                                                             ccl_addr_space PathState *state,
163                                                             const float3 throughput)
164 {
165         if(state->flag & PATH_RAY_TRANSPARENT) {
166                 /* Transparent rays are treated separately with own max bounces. */
167                 if(state->transparent_bounce >= kernel_data.integrator.transparent_max_bounce) {
168                         return 0.0f;
169                 }
170                 /* Do at least one bounce without RR. */
171                 else if(state->transparent_bounce <= 1) {
172                         return 1.0f;
173                 }
174 #ifdef __SHADOW_TRICKS__
175                 /* Exception for shadow catcher not working correctly with RR. */
176                 else if((state->flag & PATH_RAY_SHADOW_CATCHER) && (state->transparent_bounce <= 8)) {
177                         return 1.0f;
178                 }
179 #endif
180         }
181         else {
182                 /* Test max bounces for various ray types. */
183                 if((state->bounce >= kernel_data.integrator.max_bounce) ||
184                    (state->diffuse_bounce >= kernel_data.integrator.max_diffuse_bounce) ||
185                    (state->glossy_bounce >= kernel_data.integrator.max_glossy_bounce) ||
186 #ifdef __VOLUME__
187                    (state->volume_bounce >= kernel_data.integrator.max_volume_bounce) ||
188 #endif
189                    (state->transmission_bounce >= kernel_data.integrator.max_transmission_bounce))
190                 {
191                         return 0.0f;
192                 }
193                 /* Do at least one bounce without RR. */
194                 else if(state->bounce <= 1) {
195                         return 1.0f;
196                 }
197 #ifdef __SHADOW_TRICKS__
198                 /* Exception for shadow catcher not working correctly with RR. */
199                 else if((state->flag & PATH_RAY_SHADOW_CATCHER) && (state->bounce <= 3)) {
200                         return 1.0f;
201                 }
202 #endif
203         }
204
205         /* Probabilistic termination: use sqrt() to roughly match typical view
206          * transform and do path termination a bit later on average. */
207         return min(sqrtf(max3(fabs(throughput)) * state->branch_factor), 1.0f);
208 }
209
210 /* TODO(DingTo): Find more meaningful name for this */
211 ccl_device_inline void path_state_modify_bounce(ccl_addr_space PathState *state, bool increase)
212 {
213         /* Modify bounce temporarily for shader eval */
214         if(increase)
215                 state->bounce += 1;
216         else
217                 state->bounce -= 1;
218 }
219
220 ccl_device_inline bool path_state_ao_bounce(KernelGlobals *kg, ccl_addr_space PathState *state)
221 {
222     if(state->bounce <= kernel_data.integrator.ao_bounces) {
223         return false;
224     }
225
226     int bounce = state->bounce - state->transmission_bounce - (state->glossy_bounce > 0);
227     return (bounce > kernel_data.integrator.ao_bounces);
228 }
229
230 ccl_device_inline void path_state_branch(ccl_addr_space PathState *state,
231                                          int branch,
232                                          int num_branches)
233 {
234         state->rng_offset += PRNG_BOUNCE_NUM;
235
236         if(num_branches > 1) {
237                 /* Path is splitting into a branch, adjust so that each branch
238                  * still gets a unique sample from the same sequence. */
239                 state->sample = state->sample*num_branches + branch;
240                 state->num_samples = state->num_samples*num_branches;
241                 state->branch_factor *= num_branches;
242         }
243 }
244
245 CCL_NAMESPACE_END
246