20d1728f9de216b8689aac7fd8fcd020c041579a
[blender.git] / intern / cycles / kernel / split / kernel_direct_lighting.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_direct_lighting kernel.
20  * This is the eighth kernel in the ray tracing logic. This is the seventh
21  * of the path iteration kernels. This kernel takes care of direct lighting
22  * logic. However, the "shadow ray cast" part of direct lighting is handled
23  * in the next kernel.
24  *
25  * This kernels determines the rays for which a shadow_blocked() function associated with direct lighting should be executed.
26  * Those rays for which a shadow_blocked() function for direct-lighting must be executed, are marked with flag RAY_SHADOW_RAY_CAST_DL and
27  * enqueued into the queue QUEUE_SHADOW_RAY_CAST_DL_RAYS
28  *
29  * The input and output are as follows,
30  *
31  * rng_coop -----------------------------------------|--- kernel_direct_lighting --|--- BSDFEval_coop
32  * PathState_coop -----------------------------------|                             |--- ISLamp_coop
33  * sd -----------------------------------------------|                             |--- LightRay_coop
34  * ray_state ----------------------------------------|                             |--- ray_state
35  * Queue_data (QUEUE_ACTIVE_AND_REGENERATED_RAYS) ---|                             |
36  * kg (globals) -------------------------------------|                             |
37  * queuesize ----------------------------------------|                             |
38  *
39  * note on sd_DL : sd_DL is neither input nor output to this kernel; sd_DL is filled and consumed in this kernel itself.
40  * Note on Queues :
41  * This kernel only reads from the QUEUE_ACTIVE_AND_REGENERATED_RAYS queue and processes
42  * only the rays of state RAY_ACTIVE; If a ray needs to execute the corresponding shadow_blocked
43  * part, after direct lighting, the ray is marked with RAY_SHADOW_RAY_CAST_DL flag.
44  *
45  * State of queues when this kernel is called :
46  * state of queues QUEUE_ACTIVE_AND_REGENERATED_RAYS and QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS will be same
47  * before and after this kernel call.
48  * QUEUE_SHADOW_RAY_CAST_DL_RAYS queue will be filled with rays for which a shadow_blocked function must be executed, after this
49  * kernel call. Before this kernel call the QUEUE_SHADOW_RAY_CAST_DL_RAYS will be empty.
50  */
51 ccl_device char kernel_direct_lighting(
52         KernelGlobals *kg,
53         ShaderData *sd,                         /* Required for direct lighting */
54         ShaderData *sd_DL,                      /* Required for direct lighting */
55         ccl_global uint *rng_coop,              /* Required for direct lighting */
56         ccl_global PathState *PathState_coop,   /* Required for direct lighting */
57         ccl_global int *ISLamp_coop,            /* Required for direct lighting */
58         ccl_global Ray *LightRay_coop,          /* Required for direct lighting */
59         ccl_global BsdfEval *BSDFEval_coop,     /* Required for direct lighting */
60         ccl_global char *ray_state,             /* Denotes the state of each ray */
61         int ray_index)
62 {
63         char enqueue_flag = 0;
64         if(IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
65                 ccl_global PathState *state = &PathState_coop[ray_index];
66
67                 /* direct lighting */
68 #ifdef __EMISSION__
69                 if((kernel_data.integrator.use_direct_light &&
70                     (ccl_fetch(sd, flag) & SD_BSDF_HAS_EVAL)))
71                 {
72                         /* Sample illumination from lights to find path contribution. */
73                         ccl_global RNG* rng = &rng_coop[ray_index];
74                         float light_t = path_state_rng_1D(kg, rng, state, PRNG_LIGHT);
75                         float light_u, light_v;
76                         path_state_rng_2D(kg, rng, state, PRNG_LIGHT_U, &light_u, &light_v);
77
78                         LightSample ls;
79                         light_sample(kg,
80                                      light_t, light_u, light_v,
81                                      ccl_fetch(sd, time),
82                                      ccl_fetch(sd, P),
83                                      state->bounce,
84                                      &ls);
85
86                         Ray light_ray;
87 #ifdef __OBJECT_MOTION__
88                         light_ray.time = ccl_fetch(sd, time);
89 #endif
90
91                         BsdfEval L_light;
92                         bool is_lamp;
93                         if(direct_emission(kg, sd, &ls, state, &light_ray, &L_light, &is_lamp,
94                                            sd_DL))
95                         {
96                                 /* Write intermediate data to global memory to access from
97                                  * the next kernel.
98                                  */
99                                 LightRay_coop[ray_index] = light_ray;
100                                 BSDFEval_coop[ray_index] = L_light;
101                                 ISLamp_coop[ray_index] = is_lamp;
102                                 /* Mark ray state for next shadow kernel. */
103                                 ADD_RAY_FLAG(ray_state, ray_index, RAY_SHADOW_RAY_CAST_DL);
104                                 enqueue_flag = 1;
105                         }
106                 }
107 #endif  /* __EMISSION__ */
108         }
109         return enqueue_flag;
110 }