Fix T54105: random walk SSS missing in branched indirect paths.
[blender-staging.git] / intern / cycles / kernel / split / kernel_indirect_subsurface.h
1 /*
2  * Copyright 2011-2017 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 void kernel_indirect_subsurface(KernelGlobals *kg)
20 {
21         int thread_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
22         if(thread_index == 0) {
23                 /* We will empty both queues in this kernel. */
24                 kernel_split_params.queue_index[QUEUE_ACTIVE_AND_REGENERATED_RAYS] = 0;
25                 kernel_split_params.queue_index[QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS] = 0;
26         }
27
28         int ray_index;
29         get_ray_index(kg, thread_index,
30                       QUEUE_ACTIVE_AND_REGENERATED_RAYS,
31                       kernel_split_state.queue_data,
32                       kernel_split_params.queue_size,
33                       1);
34         ray_index = get_ray_index(kg, thread_index,
35                                   QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS,
36                                   kernel_split_state.queue_data,
37                                   kernel_split_params.queue_size,
38                                   1);
39
40 #ifdef __SUBSURFACE__
41
42         if(ray_index == QUEUE_EMPTY_SLOT) {
43                 return;
44         }
45
46         ccl_global char *ray_state = kernel_split_state.ray_state;
47         ccl_global PathState *state = &kernel_split_state.path_state[ray_index];
48         PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
49         ccl_global Ray *ray = &kernel_split_state.ray[ray_index];
50         ccl_global float3 *throughput = &kernel_split_state.throughput[ray_index];
51
52         if(IS_STATE(ray_state, ray_index, RAY_UPDATE_BUFFER)) {
53                 ccl_addr_space SubsurfaceIndirectRays *ss_indirect = &kernel_split_state.ss_rays[ray_index];
54
55                 /* Trace indirect subsurface rays by restarting the loop. this uses less
56                  * stack memory than invoking kernel_path_indirect.
57                  */
58                 if(ss_indirect->num_rays) {
59                         kernel_path_subsurface_setup_indirect(kg,
60                                                                                                   ss_indirect,
61                                                                                                   state,
62                                                                                                   ray,
63                                                                                                   L,
64                                                                                                   throughput);
65                         ASSIGN_RAY_STATE(ray_state, ray_index, RAY_REGENERATED);
66                 }
67         }
68
69 #endif  /* __SUBSURFACE__ */
70
71 }
72
73 CCL_NAMESPACE_END