Cycles: Remove few function arguments needed only for the split kernel
[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 Queues :
40  * This kernel only reads from the QUEUE_ACTIVE_AND_REGENERATED_RAYS queue and processes
41  * only the rays of state RAY_ACTIVE; If a ray needs to execute the corresponding shadow_blocked
42  * part, after direct lighting, the ray is marked with RAY_SHADOW_RAY_CAST_DL flag.
43  *
44  * State of queues when this kernel is called :
45  * state of queues QUEUE_ACTIVE_AND_REGENERATED_RAYS and QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS will be same
46  * before and after this kernel call.
47  * QUEUE_SHADOW_RAY_CAST_DL_RAYS queue will be filled with rays for which a shadow_blocked function must be executed, after this
48  * kernel call. Before this kernel call the QUEUE_SHADOW_RAY_CAST_DL_RAYS will be empty.
49  */
50 ccl_device char kernel_direct_lighting(
51         KernelGlobals *kg,
52         ShaderData *sd,                         /* Required for direct lighting */
53         ccl_global uint *rng_coop,              /* Required for direct lighting */
54         ccl_global PathState *PathState_coop,   /* Required for direct lighting */
55         ccl_global int *ISLamp_coop,            /* Required for direct lighting */
56         ccl_global Ray *LightRay_coop,          /* Required for direct lighting */
57         ccl_global BsdfEval *BSDFEval_coop,     /* Required for direct lighting */
58         ccl_global char *ray_state,             /* Denotes the state of each ray */
59         int ray_index)
60 {
61         char enqueue_flag = 0;
62         if(IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
63                 ccl_global PathState *state = &PathState_coop[ray_index];
64
65                 /* direct lighting */
66 #ifdef __EMISSION__
67                 if((kernel_data.integrator.use_direct_light &&
68                     (ccl_fetch(sd, flag) & SD_BSDF_HAS_EVAL)))
69                 {
70                         /* Sample illumination from lights to find path contribution. */
71                         ccl_global RNG* rng = &rng_coop[ray_index];
72                         float light_t = path_state_rng_1D(kg, rng, state, PRNG_LIGHT);
73                         float light_u, light_v;
74                         path_state_rng_2D(kg, rng, state, PRNG_LIGHT_U, &light_u, &light_v);
75
76                         LightSample ls;
77                         light_sample(kg,
78                                      light_t, light_u, light_v,
79                                      ccl_fetch(sd, time),
80                                      ccl_fetch(sd, P),
81                                      state->bounce,
82                                      &ls);
83
84                         Ray light_ray;
85 #ifdef __OBJECT_MOTION__
86                         light_ray.time = ccl_fetch(sd, time);
87 #endif
88
89                         BsdfEval L_light;
90                         bool is_lamp;
91                         if(direct_emission(kg, sd, &ls, state, &light_ray, &L_light, &is_lamp)) {
92                                 /* Write intermediate data to global memory to access from
93                                  * the next kernel.
94                                  */
95                                 LightRay_coop[ray_index] = light_ray;
96                                 BSDFEval_coop[ray_index] = L_light;
97                                 ISLamp_coop[ray_index] = is_lamp;
98                                 /* Mark ray state for next shadow kernel. */
99                                 ADD_RAY_FLAG(ray_state, ray_index, RAY_SHADOW_RAY_CAST_DL);
100                                 enqueue_flag = 1;
101                         }
102                 }
103 #endif  /* __EMISSION__ */
104         }
105         return enqueue_flag;
106 }