Cycles: Replace __MAX_CLOSURE__ build option with runtime integrator variable
[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 = &branched_state->sd;
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;
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 LocalIntersection *ss_isect = &branched_state->ss_isect;
65                         float bssrdf_u, bssrdf_v;
66                         path_branched_rng_2D(kg,
67                                              bssrdf_rng_hash,
68                                              &branched_state->path_state,
69                                              j,
70                                              num_samples,
71                                              PRNG_BSDF_U,
72                                              &bssrdf_u,
73                                              &bssrdf_v);
74
75                         /* intersection is expensive so avoid doing multiple times for the same input */
76                         if(branched_state->next_hit == 0 && branched_state->next_closure == 0 && branched_state->next_sample == 0) {
77                                 uint lcg_state = branched_state->lcg_state;
78                                 LocalIntersection ss_isect_private;
79
80                                 branched_state->num_hits = subsurface_scatter_multi_intersect(kg,
81                                                                                               &ss_isect_private,
82                                                                                               sd,
83                                                                                               sc,
84                                                                                               &lcg_state,
85                                                                                               bssrdf_u, bssrdf_v,
86                                                                                               true);
87
88                                 branched_state->lcg_state = lcg_state;
89                                 *ss_isect = ss_isect_private;
90                         }
91
92 #ifdef __VOLUME__
93                         Ray volume_ray = branched_state->ray;
94                         bool need_update_volume_stack =
95                                 kernel_data.integrator.use_volumes &&
96                                 sd->object_flag & SD_OBJECT_INTERSECTS_VOLUME;
97 #endif  /* __VOLUME__ */
98
99                         /* compute lighting with the BSDF closure */
100                         for(int hit = branched_state->next_hit; hit < branched_state->num_hits; hit++) {
101                                 ShaderData *bssrdf_sd = kernel_split_sd(sd, ray_index);
102                                 *bssrdf_sd = *sd; /* note: copy happens each iteration of inner loop, this is
103                                                    * important as the indirect path will write into bssrdf_sd */
104
105                                 LocalIntersection ss_isect_private = *ss_isect;
106                                 subsurface_scatter_multi_setup(kg,
107                                                                &ss_isect_private,
108                                                                hit,
109                                                                bssrdf_sd,
110                                                                &branched_state->path_state,
111                                                                branched_state->path_state.flag,
112                                                                sc,
113                                                                true);
114                                 *ss_isect = ss_isect_private;
115
116                                 ccl_global PathState *hit_state = &kernel_split_state.path_state[ray_index];
117                                 *hit_state = branched_state->path_state;
118
119                                 path_state_branch(hit_state, j, num_samples);
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                                         /* this next part is expensive as it does scene intersection so only do once */
128                                         if(branched_state->next_closure == 0 && branched_state->next_sample == 0) {
129                                                 for(int k = 0; k < VOLUME_STACK_SIZE; k++) {
130                                                         branched_state->volume_stack[k] = hit_state->volume_stack[k];
131                                                 }
132
133                                                 kernel_volume_stack_update_for_subsurface(kg,
134                                                                                           emission_sd,
135                                                                                           &volume_ray,
136                                                                                           branched_state->volume_stack);
137                                         }
138
139                                         for(int k = 0; k < VOLUME_STACK_SIZE; k++) {
140                                                 hit_state->volume_stack[k] = branched_state->volume_stack[k];
141                                         }
142                                 }
143 #endif  /* __VOLUME__ */
144
145 #ifdef __EMISSION__
146                                 if(branched_state->next_closure == 0 && branched_state->next_sample == 0) {
147                                         /* direct light */
148                                         if(kernel_data.integrator.use_direct_light) {
149                                                 int all = (kernel_data.integrator.sample_all_lights_direct) ||
150                                                               (branched_state->path_state.flag & PATH_RAY_SHADOW_CATCHER);
151                                                 kernel_branched_path_surface_connect_light(kg,
152                                                                                            bssrdf_sd,
153                                                                                            emission_sd,
154                                                                                            hit_state,
155                                                                                            branched_state->throughput,
156                                                                                            num_samples_inv,
157                                                                                            L,
158                                                                                            all);
159                                         }
160                                 }
161 #endif  /* __EMISSION__ */
162
163                                 /* indirect light */
164                                 if(kernel_split_branched_path_surface_indirect_light_iter(kg,
165                                                                                           ray_index,
166                                                                                           num_samples_inv,
167                                                                                           bssrdf_sd,
168                                                                                           false,
169                                                                                           false))
170                                 {
171                                         branched_state->ss_next_closure = i;
172                                         branched_state->ss_next_sample = j;
173                                         branched_state->next_hit = hit;
174
175                                         return true;
176                                 }
177
178                                 branched_state->next_closure = 0;
179                         }
180
181                         branched_state->next_hit = 0;
182                 }
183
184                 branched_state->ss_next_sample = 0;
185         }
186
187         branched_state->ss_next_closure = sd->num_closure;
188
189         branched_state->waiting_on_shared_samples = (branched_state->shared_sample_count > 0);
190         if(branched_state->waiting_on_shared_samples) {
191                 return true;
192         }
193
194         kernel_split_branched_path_indirect_loop_end(kg, ray_index);
195
196         return false;
197 }
198
199 #endif  /* __BRANCHED_PATH__ && __SUBSURFACE__ */
200
201 ccl_device void kernel_subsurface_scatter(KernelGlobals *kg)
202 {
203         int thread_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
204         if(thread_index == 0) {
205                 /* We will empty both queues in this kernel. */
206                 kernel_split_params.queue_index[QUEUE_ACTIVE_AND_REGENERATED_RAYS] = 0;
207                 kernel_split_params.queue_index[QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS] = 0;
208         }
209
210         int ray_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
211         ray_index = get_ray_index(kg, ray_index,
212                                   QUEUE_ACTIVE_AND_REGENERATED_RAYS,
213                                   kernel_split_state.queue_data,
214                                   kernel_split_params.queue_size,
215                                   1);
216         get_ray_index(kg, thread_index,
217                       QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS,
218                       kernel_split_state.queue_data,
219                       kernel_split_params.queue_size,
220                       1);
221
222 #ifdef __SUBSURFACE__
223         ccl_global char *ray_state = kernel_split_state.ray_state;
224
225         if(IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
226                 ccl_global PathState *state = &kernel_split_state.path_state[ray_index];
227                 PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
228                 ccl_global Ray *ray = &kernel_split_state.ray[ray_index];
229                 ccl_global float3 *throughput = &kernel_split_state.throughput[ray_index];
230                 ccl_global SubsurfaceIndirectRays *ss_indirect = &kernel_split_state.ss_rays[ray_index];
231                 ShaderData *sd = kernel_split_sd(sd, ray_index);
232                 ShaderData *emission_sd = AS_SHADER_DATA(&kernel_split_state.sd_DL_shadow[ray_index]);
233
234                 if(sd->flag & SD_BSSRDF) {
235
236 #ifdef __BRANCHED_PATH__
237                         if(!kernel_data.integrator.branched) {
238 #endif
239                                 if(kernel_path_subsurface_scatter(kg,
240                                                                   sd,
241                                                                   emission_sd,
242                                                                   L,
243                                                                   state,
244                                                                   ray,
245                                                                   throughput,
246                                                                   ss_indirect))
247                                 {
248                                         kernel_split_path_end(kg, ray_index);
249                                 }
250 #ifdef __BRANCHED_PATH__
251                         }
252                         else if(IS_FLAG(ray_state, ray_index, RAY_BRANCHED_INDIRECT)) {
253                                 float bssrdf_u, bssrdf_v;
254                                 path_state_rng_2D(kg,
255                                                   state,
256                                                   PRNG_BSDF_U,
257                                                   &bssrdf_u, &bssrdf_v);
258
259                                 const ShaderClosure *sc = shader_bssrdf_pick(sd, throughput, &bssrdf_u);
260
261                                 /* do bssrdf scatter step if we picked a bssrdf closure */
262                                 if(sc) {
263                                         uint lcg_state = lcg_state_init_addrspace(state, 0x68bc21eb);
264                                         subsurface_scatter_step(kg,
265                                                                 sd,
266                                                                 state,
267                                                                 state->flag,
268                                                                 sc,
269                                                                 &lcg_state,
270                                                                 bssrdf_u, bssrdf_v,
271                                                                 false);
272                                 }
273                         }
274                         else {
275                                 kernel_split_branched_path_subsurface_indirect_light_init(kg, ray_index);
276
277                                 if(kernel_split_branched_path_subsurface_indirect_light_iter(kg, ray_index)) {
278                                         ASSIGN_RAY_STATE(ray_state, ray_index, RAY_REGENERATED);
279                                 }
280                         }
281 #endif
282                 }
283         }
284
285 #  ifdef __BRANCHED_PATH__
286         if(ccl_global_id(0) == 0 && ccl_global_id(1) == 0) {
287                 kernel_split_params.queue_index[QUEUE_SUBSURFACE_INDIRECT_ITER] = 0;
288         }
289
290         /* iter loop */
291         ray_index = get_ray_index(kg, ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0),
292                                   QUEUE_SUBSURFACE_INDIRECT_ITER,
293                                   kernel_split_state.queue_data,
294                                   kernel_split_params.queue_size,
295                                   1);
296
297         if(IS_STATE(ray_state, ray_index, RAY_SUBSURFACE_INDIRECT_NEXT_ITER)) {
298                 /* for render passes, sum and reset indirect light pass variables
299                  * for the next samples */
300                 path_radiance_sum_indirect(&kernel_split_state.path_radiance[ray_index]);
301                 path_radiance_reset_indirect(&kernel_split_state.path_radiance[ray_index]);
302
303                 if(kernel_split_branched_path_subsurface_indirect_light_iter(kg, ray_index)) {
304                         ASSIGN_RAY_STATE(ray_state, ray_index, RAY_REGENERATED);
305                 }
306         }
307 #  endif  /* __BRANCHED_PATH__ */
308
309 #endif  /* __SUBSURFACE__ */
310
311 }
312
313 CCL_NAMESPACE_END