6329f3ae943dc3a2da894a86d95111dc67d81e9a
[blender.git] / intern / cycles / kernel / split / kernel_lamp_emission.h
1 /*
2  * Copyright 2011-2015 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 #include "kernel_split_common.h"
18
19 /* Note on kernel_lamp_emission
20  * This is the 3rd kernel in the ray-tracing logic. This is the second of the
21  * path-iteration kernels. This kernel takes care of the indirect lamp emission logic.
22  * This kernel operates on QUEUE_ACTIVE_AND_REGENERATED_RAYS. It processes rays of state RAY_ACTIVE
23  * and RAY_HIT_BACKGROUND.
24  * We will empty QUEUE_ACTIVE_AND_REGENERATED_RAYS queue in this kernel.
25  * The input/output of the kernel is as follows,
26  * Throughput_coop ------------------------------------|--- kernel_lamp_emission --|--- PathRadiance_coop
27  * Ray_coop -------------------------------------------|                           |--- Queue_data(QUEUE_ACTIVE_AND_REGENERATED_RAYS)
28  * PathState_coop -------------------------------------|                           |--- Queue_index(QUEUE_ACTIVE_AND_REGENERATED_RAYS)
29  * kg (globals) ---------------------------------------|                           |
30  * Intersection_coop ----------------------------------|                           |
31  * ray_state ------------------------------------------|                           |
32  * Queue_data (QUEUE_ACTIVE_AND_REGENERATED_RAYS) -----|                           |
33  * Queue_index (QUEUE_ACTIVE_AND_REGENERATED_RAYS) ----|                           |
34  * queuesize ------------------------------------------|                           |
35  * use_queues_flag ------------------------------------|                           |
36  * sw -------------------------------------------------|                           |
37  * sh -------------------------------------------------|                           |
38  * parallel_samples -----------------------------------|                           |
39  *
40  * note : sd is neither input nor output. Its just filled and consumed in the same, kernel_lamp_emission, kernel.
41  */
42 ccl_device void kernel_lamp_emission(
43         KernelGlobals *kg,
44         ShaderData *sd,                        /* Required for lamp emission */
45         ccl_global float3 *throughput_coop,    /* Required for lamp emission */
46         PathRadiance *PathRadiance_coop,       /* Required for lamp emission */
47         ccl_global Ray *Ray_coop,              /* Required for lamp emission */
48         ccl_global PathState *PathState_coop,  /* Required for lamp emission */
49         Intersection *Intersection_coop,       /* Required for lamp emission */
50         ccl_global char *ray_state,            /* Denotes the state of each ray */
51         int sw, int sh,
52         ccl_global char *use_queues_flag,      /* Used to decide if this kernel should use
53                                                 * queues to fetch ray index
54                                                 */
55         int parallel_samples,                  /* Number of samples to be processed in parallel */
56         int ray_index)
57 {
58         if(IS_STATE(ray_state, ray_index, RAY_ACTIVE) ||
59            IS_STATE(ray_state, ray_index, RAY_HIT_BACKGROUND))
60         {
61                 PathRadiance *L = &PathRadiance_coop[ray_index];
62                 ccl_global PathState *state = &PathState_coop[ray_index];
63
64                 float3 throughput = throughput_coop[ray_index];
65                 Ray ray = Ray_coop[ray_index];
66
67 #ifdef __LAMP_MIS__
68                 if(kernel_data.integrator.use_lamp_mis && !(state->flag & PATH_RAY_CAMERA)) {
69                         /* ray starting from previous non-transparent bounce */
70                         Ray light_ray;
71
72                         light_ray.P = ray.P - state->ray_t*ray.D;
73                         state->ray_t += Intersection_coop[ray_index].t;
74                         light_ray.D = ray.D;
75                         light_ray.t = state->ray_t;
76                         light_ray.time = ray.time;
77                         light_ray.dD = ray.dD;
78                         light_ray.dP = ray.dP;
79                         /* intersect with lamp */
80                         float3 emission;
81
82                         if(indirect_lamp_emission(kg, state, &light_ray, &emission, sd)) {
83                                 path_radiance_accum_emission(L, throughput, emission, state->bounce);
84                         }
85                 }
86 #endif  /* __LAMP_MIS__ */
87
88                 /* __VOLUME__ feature is disabled */
89 #if 0
90 #ifdef __VOLUME__
91                 /* volume attenuation, emission, scatter */
92                 if(state->volume_stack[0].shader != SHADER_NONE) {
93                         Ray volume_ray = ray;
94                         volume_ray.t = (hit)? isect.t: FLT_MAX;
95
96                         bool heterogeneous = volume_stack_is_heterogeneous(kg, state->volume_stack);
97
98 #ifdef __VOLUME_DECOUPLED__
99                         int sampling_method = volume_stack_sampling_method(kg, state->volume_stack);
100                         bool decoupled = kernel_volume_use_decoupled(kg, heterogeneous, true, sampling_method);
101
102                         if(decoupled) {
103                                 /* cache steps along volume for repeated sampling */
104                                 VolumeSegment volume_segment;
105                                 ShaderData volume_sd;
106
107                                 shader_setup_from_volume(kg, &volume_sd, &volume_ray);
108                                 kernel_volume_decoupled_record(kg, state,
109                                         &volume_ray, &volume_sd, &volume_segment, heterogeneous);
110
111                                 volume_segment.sampling_method = sampling_method;
112
113                                 /* emission */
114                                 if(volume_segment.closure_flag & SD_EMISSION)
115                                         path_radiance_accum_emission(&L, throughput, volume_segment.accum_emission, state->bounce);
116
117                                 /* scattering */
118                                 VolumeIntegrateResult result = VOLUME_PATH_ATTENUATED;
119
120                                 if(volume_segment.closure_flag & SD_SCATTER) {
121                                         bool all = false;
122
123                                         /* direct light sampling */
124                                         kernel_branched_path_volume_connect_light(kg, rng, &volume_sd,
125                                                 throughput, state, &L, 1.0f, all, &volume_ray, &volume_segment);
126
127                                         /* indirect sample. if we use distance sampling and take just
128                                          * one sample for direct and indirect light, we could share
129                                          * this computation, but makes code a bit complex */
130                                         float rphase = path_state_rng_1D_for_decision(kg, rng, state, PRNG_PHASE);
131                                         float rscatter = path_state_rng_1D_for_decision(kg, rng, state, PRNG_SCATTER_DISTANCE);
132
133                                         result = kernel_volume_decoupled_scatter(kg,
134                                                 state, &volume_ray, &volume_sd, &throughput,
135                                                 rphase, rscatter, &volume_segment, NULL, true);
136                                 }
137
138                                 if(result != VOLUME_PATH_SCATTERED)
139                                         throughput *= volume_segment.accum_transmittance;
140
141                                 /* free cached steps */
142                                 kernel_volume_decoupled_free(kg, &volume_segment);
143
144                                 if(result == VOLUME_PATH_SCATTERED) {
145                                         if(kernel_path_volume_bounce(kg, rng, &volume_sd, &throughput, state, &L, &ray))
146                                                 continue;
147                                         else
148                                                 break;
149                                 }
150                         }
151                         else
152 #endif  /* __VOLUME_DECOUPLED__ */
153                         {
154                                 /* integrate along volume segment with distance sampling */
155                                 ShaderData volume_sd;
156                                 VolumeIntegrateResult result = kernel_volume_integrate(
157                                         kg, state, &volume_sd, &volume_ray, &L, &throughput, rng, heterogeneous);
158
159 #ifdef __VOLUME_SCATTER__
160                                 if(result == VOLUME_PATH_SCATTERED) {
161                                         /* direct lighting */
162                                         kernel_path_volume_connect_light(kg, rng, &volume_sd, throughput, state, &L);
163
164                                         /* indirect light bounce */
165                                         if(kernel_path_volume_bounce(kg, rng, &volume_sd, &throughput, state, &L, &ray))
166                                                 continue;
167                                         else
168                                                 break;
169                                 }
170 #endif  /* __VOLUME_SCATTER__ */
171                         }
172                 }
173 #endif  /* __VOLUME__ */
174 #endif
175         }
176 }