Merge branch 'blender2.7'
[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                                                                                               hit_state,
89                                                                                               sc,
90                                                                                               &lcg_state,
91                                                                                               bssrdf_u, bssrdf_v,
92                                                                                               true);
93
94                                 branched_state->lcg_state = lcg_state;
95                                 *ss_isect = ss_isect_private;
96                         }
97
98                         hit_state->rng_offset += PRNG_BOUNCE_NUM;
99
100 #ifdef __VOLUME__
101                         Ray volume_ray = branched_state->ray;
102                         bool need_update_volume_stack =
103                                 kernel_data.integrator.use_volumes &&
104                                 sd->object_flag & SD_OBJECT_INTERSECTS_VOLUME;
105 #endif  /* __VOLUME__ */
106
107                         /* compute lighting with the BSDF closure */
108                         for(int hit = branched_state->next_hit; hit < branched_state->num_hits; hit++) {
109                                 ShaderData *bssrdf_sd = kernel_split_sd(sd, ray_index);
110                                 *bssrdf_sd = *sd; /* note: copy happens each iteration of inner loop, this is
111                                                    * important as the indirect path will write into bssrdf_sd */
112
113                                 Bssrdf *bssrdf = (Bssrdf *)sc;
114                                 ClosureType bssrdf_type = sc->type;
115                                 float bssrdf_roughness = bssrdf->roughness;
116
117                                 LocalIntersection ss_isect_private = *ss_isect;
118                                 subsurface_scatter_multi_setup(kg,
119                                                                &ss_isect_private,
120                                                                hit,
121                                                                bssrdf_sd,
122                                                                hit_state,
123                                                                bssrdf_type,
124                                                                bssrdf_roughness);
125                                 *ss_isect = ss_isect_private;
126
127 #ifdef __VOLUME__
128                                 if(need_update_volume_stack) {
129                                         /* Setup ray from previous surface point to the new one. */
130                                         float3 P = ray_offset(bssrdf_sd->P, -bssrdf_sd->Ng);
131                                         volume_ray.D = normalize_len(P - volume_ray.P, &volume_ray.t);
132
133                                         for(int k = 0; k < VOLUME_STACK_SIZE; k++) {
134                                                 hit_state->volume_stack[k] = branched_state->path_state.volume_stack[k];
135                                         }
136
137                                         kernel_volume_stack_update_for_subsurface(kg,
138                                                                                   emission_sd,
139                                                                                   &volume_ray,
140                                                                                   hit_state->volume_stack);
141                                 }
142 #endif  /* __VOLUME__ */
143
144 #ifdef __EMISSION__
145                                 if(branched_state->next_closure == 0 && branched_state->next_sample == 0) {
146                                         /* direct light */
147                                         if(kernel_data.integrator.use_direct_light) {
148                                                 int all = (kernel_data.integrator.sample_all_lights_direct) ||
149                                                               (hit_state->flag & PATH_RAY_SHADOW_CATCHER);
150                                                 kernel_branched_path_surface_connect_light(kg,
151                                                                                            bssrdf_sd,
152                                                                                            emission_sd,
153                                                                                            hit_state,
154                                                                                            branched_state->throughput,
155                                                                                            num_samples_inv,
156                                                                                            L,
157                                                                                            all);
158                                         }
159                                 }
160 #endif  /* __EMISSION__ */
161
162                                 /* indirect light */
163                                 if(kernel_split_branched_path_surface_indirect_light_iter(kg,
164                                                                                           ray_index,
165                                                                                           num_samples_inv,
166                                                                                           bssrdf_sd,
167                                                                                           false,
168                                                                                           false))
169                                 {
170                                         branched_state->ss_next_closure = i;
171                                         branched_state->ss_next_sample = j;
172                                         branched_state->next_hit = hit;
173
174                                         return true;
175                                 }
176
177                                 branched_state->next_closure = 0;
178                         }
179
180                         branched_state->next_hit = 0;
181                 }
182
183                 branched_state->ss_next_sample = 0;
184         }
185
186         branched_state->ss_next_closure = sd->num_closure;
187
188         branched_state->waiting_on_shared_samples = (branched_state->shared_sample_count > 0);
189         if(branched_state->waiting_on_shared_samples) {
190                 return true;
191         }
192
193         kernel_split_branched_path_indirect_loop_end(kg, ray_index);
194
195         return false;
196 }
197
198 #endif  /* __BRANCHED_PATH__ && __SUBSURFACE__ */
199
200 ccl_device void kernel_subsurface_scatter(KernelGlobals *kg)
201 {
202         int thread_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
203         if(thread_index == 0) {
204                 /* We will empty both queues in this kernel. */
205                 kernel_split_params.queue_index[QUEUE_ACTIVE_AND_REGENERATED_RAYS] = 0;
206                 kernel_split_params.queue_index[QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS] = 0;
207         }
208
209         int ray_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
210         ray_index = get_ray_index(kg, ray_index,
211                                   QUEUE_ACTIVE_AND_REGENERATED_RAYS,
212                                   kernel_split_state.queue_data,
213                                   kernel_split_params.queue_size,
214                                   1);
215         get_ray_index(kg, thread_index,
216                       QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS,
217                       kernel_split_state.queue_data,
218                       kernel_split_params.queue_size,
219                       1);
220
221 #ifdef __SUBSURFACE__
222         ccl_global char *ray_state = kernel_split_state.ray_state;
223
224         if(IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
225                 ccl_global PathState *state = &kernel_split_state.path_state[ray_index];
226                 PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
227                 ccl_global Ray *ray = &kernel_split_state.ray[ray_index];
228                 ccl_global float3 *throughput = &kernel_split_state.throughput[ray_index];
229                 ccl_global SubsurfaceIndirectRays *ss_indirect = &kernel_split_state.ss_rays[ray_index];
230                 ShaderData *sd = kernel_split_sd(sd, ray_index);
231                 ShaderData *emission_sd = AS_SHADER_DATA(&kernel_split_state.sd_DL_shadow[ray_index]);
232
233                 if(sd->flag & SD_BSSRDF) {
234
235 #ifdef __BRANCHED_PATH__
236                         if(!kernel_data.integrator.branched ||
237                            IS_FLAG(ray_state, ray_index, RAY_BRANCHED_INDIRECT))
238                         {
239 #endif
240                                 if(kernel_path_subsurface_scatter(kg,
241                                                                   sd,
242                                                                   emission_sd,
243                                                                   L,
244                                                                   state,
245                                                                   ray,
246                                                                   throughput,
247                                                                   ss_indirect))
248                                 {
249                                         kernel_split_path_end(kg, ray_index);
250                                 }
251 #ifdef __BRANCHED_PATH__
252                         }
253                         else {
254                                 kernel_split_branched_path_subsurface_indirect_light_init(kg, ray_index);
255
256                                 if(kernel_split_branched_path_subsurface_indirect_light_iter(kg, ray_index)) {
257                                         ASSIGN_RAY_STATE(ray_state, ray_index, RAY_REGENERATED);
258                                 }
259                         }
260 #endif
261                 }
262         }
263
264 #  ifdef __BRANCHED_PATH__
265         if(ccl_global_id(0) == 0 && ccl_global_id(1) == 0) {
266                 kernel_split_params.queue_index[QUEUE_SUBSURFACE_INDIRECT_ITER] = 0;
267         }
268
269         /* iter loop */
270         ray_index = get_ray_index(kg, ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0),
271                                   QUEUE_SUBSURFACE_INDIRECT_ITER,
272                                   kernel_split_state.queue_data,
273                                   kernel_split_params.queue_size,
274                                   1);
275
276         if(IS_STATE(ray_state, ray_index, RAY_SUBSURFACE_INDIRECT_NEXT_ITER)) {
277                 /* for render passes, sum and reset indirect light pass variables
278                  * for the next samples */
279                 path_radiance_sum_indirect(&kernel_split_state.path_radiance[ray_index]);
280                 path_radiance_reset_indirect(&kernel_split_state.path_radiance[ray_index]);
281
282                 if(kernel_split_branched_path_subsurface_indirect_light_iter(kg, ray_index)) {
283                         ASSIGN_RAY_STATE(ray_state, ray_index, RAY_REGENERATED);
284                 }
285         }
286 #  endif  /* __BRANCHED_PATH__ */
287
288 #endif  /* __SUBSURFACE__ */
289
290 }
291
292 CCL_NAMESPACE_END