Cycles: Expose image image extension mapping to the image manager
[blender.git] / intern / cycles / kernel / kernel_path_branched.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 #ifdef __BRANCHED_PATH__
20
21 ccl_device void kernel_branched_path_ao(KernelGlobals *kg, ShaderData *sd, PathRadiance *L, PathState *state, RNG *rng, float3 throughput)
22 {
23         int num_samples = kernel_data.integrator.ao_samples;
24         float num_samples_inv = 1.0f/num_samples;
25         float ao_factor = kernel_data.background.ao_factor;
26         float3 ao_N;
27         float3 ao_bsdf = shader_bsdf_ao(kg, sd, ao_factor, &ao_N);
28         float3 ao_alpha = shader_bsdf_alpha(kg, sd);
29
30         for(int j = 0; j < num_samples; j++) {
31                 float bsdf_u, bsdf_v;
32                 path_branched_rng_2D(kg, rng, state, j, num_samples, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
33
34                 float3 ao_D;
35                 float ao_pdf;
36
37                 sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
38
39                 if(dot(ccl_fetch(sd, Ng), ao_D) > 0.0f && ao_pdf != 0.0f) {
40                         Ray light_ray;
41                         float3 ao_shadow;
42
43                         light_ray.P = ray_offset(ccl_fetch(sd, P), ccl_fetch(sd, Ng));
44                         light_ray.D = ao_D;
45                         light_ray.t = kernel_data.background.ao_distance;
46 #ifdef __OBJECT_MOTION__
47                         light_ray.time = ccl_fetch(sd, time);
48 #endif
49                         light_ray.dP = ccl_fetch(sd, dP);
50                         light_ray.dD = differential3_zero();
51
52                         if(!shadow_blocked(kg, state, &light_ray, &ao_shadow))
53                                 path_radiance_accum_ao(L, throughput*num_samples_inv, ao_alpha, ao_bsdf, ao_shadow, state->bounce);
54                 }
55         }
56 }
57
58
59 /* bounce off surface and integrate indirect light */
60 ccl_device_noinline void kernel_branched_path_surface_indirect_light(KernelGlobals *kg,
61         RNG *rng, ShaderData *sd, float3 throughput, float num_samples_adjust,
62         PathState *state, PathRadiance *L)
63 {
64         for(int i = 0; i < ccl_fetch(sd, num_closure); i++) {
65                 const ShaderClosure *sc = &ccl_fetch(sd, closure)[i];
66
67                 if(!CLOSURE_IS_BSDF(sc->type))
68                         continue;
69                 /* transparency is not handled here, but in outer loop */
70                 if(sc->type == CLOSURE_BSDF_TRANSPARENT_ID)
71                         continue;
72
73                 int num_samples;
74
75                 if(CLOSURE_IS_BSDF_DIFFUSE(sc->type))
76                         num_samples = kernel_data.integrator.diffuse_samples;
77                 else if(CLOSURE_IS_BSDF_BSSRDF(sc->type))
78                         num_samples = 1;
79                 else if(CLOSURE_IS_BSDF_GLOSSY(sc->type))
80                         num_samples = kernel_data.integrator.glossy_samples;
81                 else
82                         num_samples = kernel_data.integrator.transmission_samples;
83
84                 num_samples = ceil_to_int(num_samples_adjust*num_samples);
85
86                 float num_samples_inv = num_samples_adjust/num_samples;
87                 RNG bsdf_rng = cmj_hash(*rng, i);
88
89                 for(int j = 0; j < num_samples; j++) {
90                         PathState ps = *state;
91                         float3 tp = throughput;
92                         Ray bsdf_ray;
93
94                         if(!kernel_branched_path_surface_bounce(kg, &bsdf_rng, sd, sc, j, num_samples, &tp, &ps, L, &bsdf_ray))
95                                 continue;
96
97                         kernel_path_indirect(kg, rng, bsdf_ray, tp*num_samples_inv, num_samples, ps, L);
98
99                         /* for render passes, sum and reset indirect light pass variables
100                          * for the next samples */
101                         path_radiance_sum_indirect(L);
102                         path_radiance_reset_indirect(L);
103                 }
104         }
105 }
106
107 #ifdef __SUBSURFACE__
108 ccl_device void kernel_branched_path_subsurface_scatter(KernelGlobals *kg,
109                                                         ShaderData *sd,
110                                                         PathRadiance *L,
111                                                         PathState *state,
112                                                         RNG *rng,
113                                                         Ray *ray,
114                                                         float3 throughput)
115 {
116         for(int i = 0; i < ccl_fetch(sd, num_closure); i++) {
117                 ShaderClosure *sc = &ccl_fetch(sd, closure)[i];
118
119                 if(!CLOSURE_IS_BSSRDF(sc->type))
120                         continue;
121
122                 /* set up random number generator */
123                 uint lcg_state = lcg_state_init(rng, state, 0x68bc21eb);
124                 int num_samples = kernel_data.integrator.subsurface_samples;
125                 float num_samples_inv = 1.0f/num_samples;
126                 RNG bssrdf_rng = cmj_hash(*rng, i);
127
128                 /* do subsurface scatter step with copy of shader data, this will
129                  * replace the BSSRDF with a diffuse BSDF closure */
130                 for(int j = 0; j < num_samples; j++) {
131                         ShaderData bssrdf_sd[BSSRDF_MAX_HITS];
132                         float bssrdf_u, bssrdf_v;
133                         path_branched_rng_2D(kg, &bssrdf_rng, state, j, num_samples, PRNG_BSDF_U, &bssrdf_u, &bssrdf_v);
134                         int num_hits = subsurface_scatter_multi_step(kg, sd, bssrdf_sd, state->flag, sc, &lcg_state, bssrdf_u, bssrdf_v, true);
135 #ifdef __VOLUME__
136                         Ray volume_ray = *ray;
137                         bool need_update_volume_stack = kernel_data.integrator.use_volumes &&
138                                                         ccl_fetch(sd, flag) & SD_OBJECT_INTERSECTS_VOLUME;
139 #endif
140
141                         /* compute lighting with the BSDF closure */
142                         for(int hit = 0; hit < num_hits; hit++) {
143                                 PathState hit_state = *state;
144
145                                 path_state_branch(&hit_state, j, num_samples);
146
147 #ifdef __VOLUME__
148                                 if(need_update_volume_stack) {
149                                         /* Setup ray from previous surface point to the new one. */
150                                         float3 P = ray_offset(bssrdf_sd[hit].P, -bssrdf_sd[hit].Ng);
151                                         volume_ray.D = normalize_len(P - volume_ray.P,
152                                                                      &volume_ray.t);
153
154                                         kernel_volume_stack_update_for_subsurface(
155                                             kg,
156                                             &volume_ray,
157                                             hit_state.volume_stack);
158
159                                         /* Move volume ray forward. */
160                                         volume_ray.P = P;
161                                 }
162 #endif
163
164 #ifdef __EMISSION__
165                                 /* direct light */
166                                 if(kernel_data.integrator.use_direct_light) {
167                                         bool all = kernel_data.integrator.sample_all_lights_direct;
168                                         kernel_branched_path_surface_connect_light(kg, rng,
169                                                 &bssrdf_sd[hit], &hit_state, throughput, num_samples_inv, L, all);
170                                 }
171 #endif
172
173                                 /* indirect light */
174                                 kernel_branched_path_surface_indirect_light(kg, rng,
175                                         &bssrdf_sd[hit], throughput, num_samples_inv,
176                                         &hit_state, L);
177                         }
178                 }
179         }
180 }
181 #endif
182
183 ccl_device float4 kernel_branched_path_integrate(KernelGlobals *kg, RNG *rng, int sample, Ray ray, ccl_global float *buffer)
184 {
185         /* initialize */
186         PathRadiance L;
187         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
188         float L_transparent = 0.0f;
189
190         path_radiance_init(&L, kernel_data.film.use_light_pass);
191
192         PathState state;
193         path_state_init(kg, &state, rng, sample, &ray);
194
195 #ifdef __KERNEL_DEBUG__
196         DebugData debug_data;
197         debug_data_init(&debug_data);
198 #endif
199
200         /* Main Loop
201          * Here we only handle transparency intersections from the camera ray.
202          * Indirect bounces are handled in kernel_branched_path_surface_indirect_light().
203          */
204         for(;;) {
205                 /* intersect scene */
206                 Intersection isect;
207                 uint visibility = path_state_ray_visibility(kg, &state);
208
209 #ifdef __HAIR__
210                 float difl = 0.0f, extmax = 0.0f;
211                 uint lcg_state = 0;
212
213                 if(kernel_data.bvh.have_curves) {
214                         if(kernel_data.cam.resolution == 1) {
215                                 float3 pixdiff = ray.dD.dx + ray.dD.dy;
216                                 /*pixdiff = pixdiff - dot(pixdiff, ray.D)*ray.D;*/
217                                 difl = kernel_data.curve.minimum_width * len(pixdiff) * 0.5f;
218                         }
219
220                         extmax = kernel_data.curve.maximum_width;
221                         lcg_state = lcg_state_init(rng, &state, 0x51633e2d);
222                 }
223
224                 bool hit = scene_intersect(kg, &ray, visibility, &isect, &lcg_state, difl, extmax);
225 #else
226                 bool hit = scene_intersect(kg, &ray, visibility, &isect, NULL, 0.0f, 0.0f);
227 #endif
228
229 #ifdef __KERNEL_DEBUG__
230                 debug_data.num_bvh_traversal_steps += isect.num_traversal_steps;
231                 debug_data.num_bvh_traversed_instances += isect.num_traversed_instances;
232                 debug_data.num_ray_bounces++;
233 #endif
234
235 #ifdef __VOLUME__
236                 /* volume attenuation, emission, scatter */
237                 if(state.volume_stack[0].shader != SHADER_NONE) {
238                         Ray volume_ray = ray;
239                         volume_ray.t = (hit)? isect.t: FLT_MAX;
240                         
241                         bool heterogeneous = volume_stack_is_heterogeneous(kg, state.volume_stack);
242
243 #ifdef __VOLUME_DECOUPLED__
244                         /* decoupled ray marching only supported on CPU */
245
246                         /* cache steps along volume for repeated sampling */
247                         VolumeSegment volume_segment;
248                         ShaderData volume_sd;
249
250                         shader_setup_from_volume(kg, &volume_sd, &volume_ray, state.bounce, state.transparent_bounce);
251                         kernel_volume_decoupled_record(kg, &state,
252                                 &volume_ray, &volume_sd, &volume_segment, heterogeneous);
253
254                         /* direct light sampling */
255                         if(volume_segment.closure_flag & SD_SCATTER) {
256                                 volume_segment.sampling_method = volume_stack_sampling_method(kg, state.volume_stack);
257
258                                 bool all = kernel_data.integrator.sample_all_lights_direct;
259
260                                 kernel_branched_path_volume_connect_light(kg, rng, &volume_sd,
261                                         throughput, &state, &L, all, &volume_ray, &volume_segment);
262
263                                 /* indirect light sampling */
264                                 int num_samples = kernel_data.integrator.volume_samples;
265                                 float num_samples_inv = 1.0f/num_samples;
266
267                                 for(int j = 0; j < num_samples; j++) {
268                                         /* workaround to fix correlation bug in T38710, can find better solution
269                                          * in random number generator later, for now this is done here to not impact
270                                          * performance of rendering without volumes */
271                                         RNG tmp_rng = cmj_hash(*rng, state.rng_offset);
272
273                                         PathState ps = state;
274                                         Ray pray = ray;
275                                         float3 tp = throughput;
276
277                                         /* branch RNG state */
278                                         path_state_branch(&ps, j, num_samples);
279
280                                         /* scatter sample. if we use distance sampling and take just one
281                                          * sample for direct and indirect light, we could share this
282                                          * computation, but makes code a bit complex */
283                                         float rphase = path_state_rng_1D_for_decision(kg, &tmp_rng, &ps, PRNG_PHASE);
284                                         float rscatter = path_state_rng_1D_for_decision(kg, &tmp_rng, &ps, PRNG_SCATTER_DISTANCE);
285
286                                         VolumeIntegrateResult result = kernel_volume_decoupled_scatter(kg,
287                                                 &ps, &pray, &volume_sd, &tp, rphase, rscatter, &volume_segment, NULL, false);
288                                                 
289                                         (void)result;
290                                         kernel_assert(result == VOLUME_PATH_SCATTERED);
291
292                                         if(kernel_path_volume_bounce(kg, rng, &volume_sd, &tp, &ps, &L, &pray)) {
293                                                 kernel_path_indirect(kg, rng, pray, tp*num_samples_inv, num_samples, ps, &L);
294
295                                                 /* for render passes, sum and reset indirect light pass variables
296                                                  * for the next samples */
297                                                 path_radiance_sum_indirect(&L);
298                                                 path_radiance_reset_indirect(&L);
299                                         }
300                                 }
301                         }
302
303                         /* emission and transmittance */
304                         if(volume_segment.closure_flag & SD_EMISSION)
305                                 path_radiance_accum_emission(&L, throughput, volume_segment.accum_emission, state.bounce);
306                         throughput *= volume_segment.accum_transmittance;
307
308                         /* free cached steps */
309                         kernel_volume_decoupled_free(kg, &volume_segment);
310 #else
311                         /* GPU: no decoupled ray marching, scatter probalistically */
312                         int num_samples = kernel_data.integrator.volume_samples;
313                         float num_samples_inv = 1.0f/num_samples;
314
315                         /* todo: we should cache the shader evaluations from stepping
316                          * through the volume, for now we redo them multiple times */
317
318                         for(int j = 0; j < num_samples; j++) {
319                                 PathState ps = state;
320                                 Ray pray = ray;
321                                 ShaderData volume_sd;
322                                 float3 tp = throughput * num_samples_inv;
323
324                                 /* branch RNG state */
325                                 path_state_branch(&ps, j, num_samples);
326
327                                 VolumeIntegrateResult result = kernel_volume_integrate(
328                                         kg, &ps, &volume_sd, &volume_ray, &L, &tp, rng, heterogeneous);
329                                 
330 #ifdef __VOLUME_SCATTER__
331                                 if(result == VOLUME_PATH_SCATTERED) {
332                                         /* todo: support equiangular, MIS and all light sampling.
333                                          * alternatively get decoupled ray marching working on the GPU */
334                                         kernel_path_volume_connect_light(kg, rng, &volume_sd, tp, &state, &L);
335
336                                         if(kernel_path_volume_bounce(kg, rng, &volume_sd, &tp, &ps, &L, &pray)) {
337                                                 kernel_path_indirect(kg, rng, pray, tp, num_samples, ps, &L);
338
339                                                 /* for render passes, sum and reset indirect light pass variables
340                                                  * for the next samples */
341                                                 path_radiance_sum_indirect(&L);
342                                                 path_radiance_reset_indirect(&L);
343                                         }
344                                 }
345 #endif
346                         }
347
348                         /* todo: avoid this calculation using decoupled ray marching */
349                         kernel_volume_shadow(kg, &state, &volume_ray, &throughput);
350 #endif
351                 }
352 #endif
353
354                 if(!hit) {
355                         /* eval background shader if nothing hit */
356                         if(kernel_data.background.transparent) {
357                                 L_transparent += average(throughput);
358
359 #ifdef __PASSES__
360                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
361 #endif
362                                         break;
363                         }
364
365 #ifdef __BACKGROUND__
366                         /* sample background shader */
367                         float3 L_background = indirect_background(kg, &state, &ray);
368                         path_radiance_accum_background(&L, throughput, L_background, state.bounce);
369 #endif
370
371                         break;
372                 }
373
374                 /* setup shading */
375                 ShaderData sd;
376                 shader_setup_from_ray(kg, &sd, &isect, &ray, state.bounce, state.transparent_bounce);
377                 shader_eval_surface(kg, &sd, 0.0f, state.flag, SHADER_CONTEXT_MAIN);
378                 shader_merge_closures(&sd);
379
380                 /* holdout */
381 #ifdef __HOLDOUT__
382                 if(sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK)) {
383                         if(kernel_data.background.transparent) {
384                                 float3 holdout_weight;
385                                 
386                                 if(sd.flag & SD_HOLDOUT_MASK)
387                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
388                                 else
389                                         holdout_weight = shader_holdout_eval(kg, &sd);
390
391                                 /* any throughput is ok, should all be identical here */
392                                 L_transparent += average(holdout_weight*throughput);
393                         }
394
395                         if(sd.flag & SD_HOLDOUT_MASK)
396                                 break;
397                 }
398 #endif
399
400                 /* holdout mask objects do not write data passes */
401                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, &state, throughput);
402
403 #ifdef __EMISSION__
404                 /* emission */
405                 if(sd.flag & SD_EMISSION) {
406                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, state.ray_pdf);
407                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
408                 }
409 #endif
410
411                 /* transparency termination */
412                 if(state.flag & PATH_RAY_TRANSPARENT) {
413                         /* path termination. this is a strange place to put the termination, it's
414                          * mainly due to the mixed in MIS that we use. gives too many unneeded
415                          * shader evaluations, only need emission if we are going to terminate */
416                         float probability = path_state_terminate_probability(kg, &state, throughput);
417
418                         if(probability == 0.0f) {
419                                 break;
420                         }
421                         else if(probability != 1.0f) {
422                                 float terminate = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_TERMINATE);
423
424                                 if(terminate >= probability)
425                                         break;
426
427                                 throughput /= probability;
428                         }
429                 }
430
431 #ifdef __AO__
432                 /* ambient occlusion */
433                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
434                         kernel_branched_path_ao(kg, &sd, &L, &state, rng, throughput);
435                 }
436 #endif
437
438 #ifdef __SUBSURFACE__
439                 /* bssrdf scatter to a different location on the same object */
440                 if(sd.flag & SD_BSSRDF) {
441                         kernel_branched_path_subsurface_scatter(kg, &sd, &L, &state,
442                                                                 rng, &ray, throughput);
443                 }
444 #endif
445
446                 if(!(sd.flag & SD_HAS_ONLY_VOLUME)) {
447                         PathState hit_state = state;
448
449 #ifdef __EMISSION__
450                         /* direct light */
451                         if(kernel_data.integrator.use_direct_light) {
452                                 bool all = kernel_data.integrator.sample_all_lights_direct;
453                                 kernel_branched_path_surface_connect_light(kg, rng,
454                                         &sd, &hit_state, throughput, 1.0f, &L, all);
455                         }
456 #endif
457
458                         /* indirect light */
459                         kernel_branched_path_surface_indirect_light(kg, rng,
460                                 &sd, throughput, 1.0f, &hit_state, &L);
461
462                         /* continue in case of transparency */
463                         throughput *= shader_bsdf_transparency(kg, &sd);
464
465                         if(is_zero(throughput))
466                                 break;
467                 }
468
469                 /* Update Path State */
470                 state.flag |= PATH_RAY_TRANSPARENT;
471                 state.transparent_bounce++;
472
473                 ray.P = ray_offset(sd.P, -sd.Ng);
474                 ray.t -= sd.ray_length; /* clipping works through transparent */
475
476
477 #ifdef __RAY_DIFFERENTIALS__
478                 ray.dP = sd.dP;
479                 ray.dD.dx = -sd.dI.dx;
480                 ray.dD.dy = -sd.dI.dy;
481 #endif
482
483 #ifdef __VOLUME__
484                 /* enter/exit volume */
485                 kernel_volume_stack_enter_exit(kg, &sd, state.volume_stack);
486 #endif
487         }
488
489         float3 L_sum = path_radiance_clamp_and_sum(kg, &L);
490
491         kernel_write_light_passes(kg, buffer, &L, sample);
492
493 #ifdef __KERNEL_DEBUG__
494         kernel_write_debug_passes(kg, buffer, &state, &debug_data, sample);
495 #endif
496
497         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
498 }
499
500 ccl_device void kernel_branched_path_trace(KernelGlobals *kg,
501         ccl_global float *buffer, ccl_global uint *rng_state,
502         int sample, int x, int y, int offset, int stride)
503 {
504         /* buffer offset */
505         int index = offset + x + y*stride;
506         int pass_stride = kernel_data.film.pass_stride;
507
508         rng_state += index;
509         buffer += index*pass_stride;
510
511         /* initialize random numbers and ray */
512         RNG rng;
513         Ray ray;
514
515         kernel_path_trace_setup(kg, rng_state, sample, x, y, &rng, &ray);
516
517         /* integrate */
518         float4 L;
519
520         if(ray.t != 0.0f)
521                 L = kernel_branched_path_integrate(kg, &rng, sample, ray, buffer);
522         else
523                 L = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
524
525         /* accumulate result in output buffer */
526         kernel_write_pass_float4(buffer, sample, L);
527
528         path_rng_end(kg, rng_state, rng);
529 }
530
531 #endif  /* __BRANCHED_PATH__ */
532
533 CCL_NAMESPACE_END
534