Code refactor: tweaks in SSS code to prepare for coming changes.
[blender.git] / intern / cycles / kernel / split / kernel_subsurface_scatter.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 #if defined(__BRANCHED_PATH__) && defined(__SUBSURFACE__)
20
21 ccl_device_inline void kernel_split_branched_path_subsurface_indirect_light_init(KernelGlobals *kg, int ray_index)
22 {
23         kernel_split_branched_path_indirect_loop_init(kg, ray_index);
24
25         SplitBranchedState *branched_state = &kernel_split_state.branched_state[ray_index];
26
27         branched_state->ss_next_closure = 0;
28         branched_state->ss_next_sample = 0;
29
30         branched_state->num_hits = 0;
31         branched_state->next_hit = 0;
32
33         ADD_RAY_FLAG(kernel_split_state.ray_state, ray_index, RAY_BRANCHED_SUBSURFACE_INDIRECT);
34 }
35
36 ccl_device_noinline bool kernel_split_branched_path_subsurface_indirect_light_iter(KernelGlobals *kg, int ray_index)
37 {
38         SplitBranchedState *branched_state = &kernel_split_state.branched_state[ray_index];
39
40         ShaderData *sd = kernel_split_sd(branched_state_sd, ray_index);
41         PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
42         ShaderData *emission_sd = AS_SHADER_DATA(&kernel_split_state.sd_DL_shadow[ray_index]);
43
44         for(int i = branched_state->ss_next_closure; i < sd->num_closure; i++) {
45                 ShaderClosure *sc = &sd->closure[i];
46
47                 if(!CLOSURE_IS_BSSRDF(sc->type))
48                         continue;
49
50                 /* set up random number generator */
51                 if(branched_state->ss_next_sample == 0 && branched_state->next_hit == 0 &&
52                    branched_state->next_closure == 0 && branched_state->next_sample == 0)
53                 {
54                         branched_state->lcg_state = lcg_state_init_addrspace(&branched_state->path_state,
55                                                                              0x68bc21eb);
56                 }
57                 int num_samples = kernel_data.integrator.subsurface_samples * 3;
58                 float num_samples_inv = 1.0f/num_samples;
59                 uint bssrdf_rng_hash = cmj_hash(branched_state->path_state.rng_hash, i);
60
61                 /* do subsurface scatter step with copy of shader data, this will
62                  * replace the BSSRDF with a diffuse BSDF closure */
63                 for(int j = branched_state->ss_next_sample; j < num_samples; j++) {
64                         ccl_global PathState *hit_state = &kernel_split_state.path_state[ray_index];
65                         *hit_state = branched_state->path_state;
66                         hit_state->rng_hash = bssrdf_rng_hash;
67                         path_state_branch(hit_state, j, num_samples);
68
69                         ccl_global LocalIntersection *ss_isect = &branched_state->ss_isect;
70                         float bssrdf_u, bssrdf_v;
71                         path_branched_rng_2D(kg,
72                                              bssrdf_rng_hash,
73                                              hit_state,
74                                              j,
75                                              num_samples,
76                                              PRNG_BSDF_U,
77                                              &bssrdf_u,
78                                              &bssrdf_v);
79
80                         /* intersection is expensive so avoid doing multiple times for the same input */
81                         if(branched_state->next_hit == 0 && branched_state->next_closure == 0 && branched_state->next_sample == 0) {
82                                 uint lcg_state = branched_state->lcg_state;
83                                 LocalIntersection ss_isect_private;
84
85                                 branched_state->num_hits = subsurface_scatter_multi_intersect(kg,
86                                                                                               &ss_isect_private,
87                                                                                               sd,
88                                                                                               sc,
89                                                                                               &lcg_state,
90                                                                                               bssrdf_u, bssrdf_v,
91                                                                                               true);
92
93                                 branched_state->lcg_state = lcg_state;
94                                 *ss_isect = ss_isect_private;
95                         }
96
97                         hit_state->rng_offset += PRNG_BOUNCE_NUM;
98
99 #ifdef __VOLUME__
100                         Ray volume_ray = branched_state->ray;
101                         bool need_update_volume_stack =
102                                 kernel_data.integrator.use_volumes &&
103                                 sd->object_flag & SD_OBJECT_INTERSECTS_VOLUME;
104 #endif  /* __VOLUME__ */
105
106                         /* compute lighting with the BSDF closure */
107                         for(int hit = branched_state->next_hit; hit < branched_state->num_hits; hit++) {
108                                 ShaderData *bssrdf_sd = kernel_split_sd(sd, ray_index);
109                                 *bssrdf_sd = *sd; /* note: copy happens each iteration of inner loop, this is
110                                                    * important as the indirect path will write into bssrdf_sd */
111
112                                 LocalIntersection ss_isect_private = *ss_isect;
113                                 subsurface_scatter_multi_setup(kg,
114                                                                &ss_isect_private,
115                                                                hit,
116                                                                bssrdf_sd,
117                                                                hit_state,
118                                                                sc);
119                                 *ss_isect = ss_isect_private;
120
121 #ifdef __VOLUME__
122                                 if(need_update_volume_stack) {
123                                         /* Setup ray from previous surface point to the new one. */
124                                         float3 P = ray_offset(bssrdf_sd->P, -bssrdf_sd->Ng);
125                                         volume_ray.D = normalize_len(P - volume_ray.P, &volume_ray.t);
126
127                                         for(int k = 0; k < VOLUME_STACK_SIZE; k++) {
128                                                 hit_state->volume_stack[k] = branched_state->path_state.volume_stack[k];
129                                         }
130
131                                         kernel_volume_stack_update_for_subsurface(kg,
132                                                                                   emission_sd,
133                                                                                   &volume_ray,
134                                                                                   hit_state->volume_stack);
135                                 }
136 #endif  /* __VOLUME__ */
137
138 #ifdef __EMISSION__
139                                 if(branched_state->next_closure == 0 && branched_state->next_sample == 0) {
140                                         /* direct light */
141                                         if(kernel_data.integrator.use_direct_light) {
142                                                 int all = (kernel_data.integrator.sample_all_lights_direct) ||
143                                                               (hit_state->flag & PATH_RAY_SHADOW_CATCHER);
144                                                 kernel_branched_path_surface_connect_light(kg,
145                                                                                            bssrdf_sd,
146                                                                                            emission_sd,
147                                                                                            hit_state,
148                                                                                            branched_state->throughput,
149                                                                                            num_samples_inv,
150                                                                                            L,
151                                                                                            all);
152                                         }
153                                 }
154 #endif  /* __EMISSION__ */
155
156                                 /* indirect light */
157                                 if(kernel_split_branched_path_surface_indirect_light_iter(kg,
158                                                                                           ray_index,
159                                                                                           num_samples_inv,
160                                                                                           bssrdf_sd,
161                                                                                           false,
162                                                                                           false))
163                                 {
164                                         branched_state->ss_next_closure = i;
165                                         branched_state->ss_next_sample = j;
166                                         branched_state->next_hit = hit;
167
168                                         return true;
169                                 }
170
171                                 branched_state->next_closure = 0;
172                         }
173
174                         branched_state->next_hit = 0;
175                 }
176
177                 branched_state->ss_next_sample = 0;
178         }
179
180         branched_state->ss_next_closure = sd->num_closure;
181
182         branched_state->waiting_on_shared_samples = (branched_state->shared_sample_count > 0);
183         if(branched_state->waiting_on_shared_samples) {
184                 return true;
185         }
186
187         kernel_split_branched_path_indirect_loop_end(kg, ray_index);
188
189         return false;
190 }
191
192 #endif  /* __BRANCHED_PATH__ && __SUBSURFACE__ */
193
194 ccl_device void kernel_subsurface_scatter(KernelGlobals *kg)
195 {
196         int thread_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
197         if(thread_index == 0) {
198                 /* We will empty both queues in this kernel. */
199                 kernel_split_params.queue_index[QUEUE_ACTIVE_AND_REGENERATED_RAYS] = 0;
200                 kernel_split_params.queue_index[QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS] = 0;
201         }
202
203         int ray_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
204         ray_index = get_ray_index(kg, ray_index,
205                                   QUEUE_ACTIVE_AND_REGENERATED_RAYS,
206                                   kernel_split_state.queue_data,
207                                   kernel_split_params.queue_size,
208                                   1);
209         get_ray_index(kg, thread_index,
210                       QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS,
211                       kernel_split_state.queue_data,
212                       kernel_split_params.queue_size,
213                       1);
214
215 #ifdef __SUBSURFACE__
216         ccl_global char *ray_state = kernel_split_state.ray_state;
217
218         if(IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
219                 ccl_global PathState *state = &kernel_split_state.path_state[ray_index];
220                 PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
221                 ccl_global Ray *ray = &kernel_split_state.ray[ray_index];
222                 ccl_global float3 *throughput = &kernel_split_state.throughput[ray_index];
223                 ccl_global SubsurfaceIndirectRays *ss_indirect = &kernel_split_state.ss_rays[ray_index];
224                 ShaderData *sd = kernel_split_sd(sd, ray_index);
225                 ShaderData *emission_sd = AS_SHADER_DATA(&kernel_split_state.sd_DL_shadow[ray_index]);
226
227                 if(sd->flag & SD_BSSRDF) {
228
229 #ifdef __BRANCHED_PATH__
230                         if(!kernel_data.integrator.branched) {
231 #endif
232                                 if(kernel_path_subsurface_scatter(kg,
233                                                                   sd,
234                                                                   emission_sd,
235                                                                   L,
236                                                                   state,
237                                                                   ray,
238                                                                   throughput,
239                                                                   ss_indirect))
240                                 {
241                                         kernel_split_path_end(kg, ray_index);
242                                 }
243 #ifdef __BRANCHED_PATH__
244                         }
245                         else if(IS_FLAG(ray_state, ray_index, RAY_BRANCHED_INDIRECT)) {
246                                 float bssrdf_u, bssrdf_v;
247                                 path_state_rng_2D(kg,
248                                                   state,
249                                                   PRNG_BSDF_U,
250                                                   &bssrdf_u, &bssrdf_v);
251
252                                 const ShaderClosure *sc = shader_bssrdf_pick(sd, throughput, &bssrdf_u);
253
254                                 /* do bssrdf scatter step if we picked a bssrdf closure */
255                                 if(sc) {
256                                         uint lcg_state = lcg_state_init_addrspace(state, 0x68bc21eb);
257                                         subsurface_scatter_step(kg,
258                                                                 sd,
259                                                                 state,
260                                                                 sc,
261                                                                 &lcg_state,
262                                                                 bssrdf_u, bssrdf_v,
263                                                                 false);
264                                 }
265                         }
266                         else {
267                                 kernel_split_branched_path_subsurface_indirect_light_init(kg, ray_index);
268
269                                 if(kernel_split_branched_path_subsurface_indirect_light_iter(kg, ray_index)) {
270                                         ASSIGN_RAY_STATE(ray_state, ray_index, RAY_REGENERATED);
271                                 }
272                         }
273 #endif
274                 }
275         }
276
277 #  ifdef __BRANCHED_PATH__
278         if(ccl_global_id(0) == 0 && ccl_global_id(1) == 0) {
279                 kernel_split_params.queue_index[QUEUE_SUBSURFACE_INDIRECT_ITER] = 0;
280         }
281
282         /* iter loop */
283         ray_index = get_ray_index(kg, ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0),
284                                   QUEUE_SUBSURFACE_INDIRECT_ITER,
285                                   kernel_split_state.queue_data,
286                                   kernel_split_params.queue_size,
287                                   1);
288
289         if(IS_STATE(ray_state, ray_index, RAY_SUBSURFACE_INDIRECT_NEXT_ITER)) {
290                 /* for render passes, sum and reset indirect light pass variables
291                  * for the next samples */
292                 path_radiance_sum_indirect(&kernel_split_state.path_radiance[ray_index]);
293                 path_radiance_reset_indirect(&kernel_split_state.path_radiance[ray_index]);
294
295                 if(kernel_split_branched_path_subsurface_indirect_light_iter(kg, ray_index)) {
296                         ASSIGN_RAY_STATE(ray_state, ray_index, RAY_REGENERATED);
297                 }
298         }
299 #  endif  /* __BRANCHED_PATH__ */
300
301 #endif  /* __SUBSURFACE__ */
302
303 }
304
305 CCL_NAMESPACE_END