50c83d06140583ff65ae70c1e27f869108326da2
[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  * shader_data --------------------------------------|                             |--- LightRay_coop
34  * ray_state ----------------------------------------|                             |--- ray_state
35  * Queue_data (QUEUE_ACTIVE_AND_REGENERATED_RAYS) ---|                             |
36  * kg (globals + data) ------------------------------|                             |
37  * queuesize ----------------------------------------|                             |
38  *
39  * note on shader_DL : shader_DL is neither input nor output to this kernel; shader_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         ccl_global char *globals,
53         ccl_constant KernelData *data,
54         ccl_global char *shader_data,           /* Required for direct lighting */
55         ccl_global char *shader_DL,             /* Required for direct lighting */
56         ccl_global uint *rng_coop,              /* Required for direct lighting */
57         ccl_global PathState *PathState_coop,   /* Required for direct lighting */
58         ccl_global int *ISLamp_coop,            /* Required for direct lighting */
59         ccl_global Ray *LightRay_coop,          /* Required for direct lighting */
60         ccl_global BsdfEval *BSDFEval_coop,     /* Required for direct lighting */
61         ccl_global char *ray_state,             /* Denotes the state of each ray */
62         int ray_index)
63 {
64         char enqueue_flag = 0;
65         if(IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
66                 /* Load kernel globals structure and ShaderData structure. */
67                 KernelGlobals *kg = (KernelGlobals *)globals;
68                 ShaderData *sd = (ShaderData *)shader_data;
69                 ShaderData *sd_DL  = (ShaderData *)shader_DL;
70
71                 ccl_global PathState *state = &PathState_coop[ray_index];
72
73                 /* direct lighting */
74 #ifdef __EMISSION__
75                 if((kernel_data.integrator.use_direct_light &&
76                     (ccl_fetch(sd, flag) & SD_BSDF_HAS_EVAL)))
77                 {
78                         /* Sample illumination from lights to find path contribution. */
79                         ccl_global RNG* rng = &rng_coop[ray_index];
80                         float light_t = path_state_rng_1D(kg, rng, state, PRNG_LIGHT);
81                         float light_u, light_v;
82                         path_state_rng_2D(kg, rng, state, PRNG_LIGHT_U, &light_u, &light_v);
83
84                         LightSample ls;
85                         light_sample(kg,
86                                      light_t, light_u, light_v,
87                                      ccl_fetch(sd, time),
88                                      ccl_fetch(sd, P),
89                                      state->bounce,
90                                      &ls);
91
92                         Ray light_ray;
93 #ifdef __OBJECT_MOTION__
94                         light_ray.time = ccl_fetch(sd, time);
95 #endif
96
97                         BsdfEval L_light;
98                         bool is_lamp;
99                         if(direct_emission(kg, sd, &ls, &light_ray, &L_light, &is_lamp,
100                                            state->bounce, state->transparent_bounce, sd_DL))
101                         {
102                                 /* Write intermediate data to global memory to access from
103                                  * the next kernel.
104                                  */
105                                 LightRay_coop[ray_index] = light_ray;
106                                 BSDFEval_coop[ray_index] = L_light;
107                                 ISLamp_coop[ray_index] = is_lamp;
108                                 /* Mark ray state for next shadow kernel. */
109                                 ADD_RAY_FLAG(ray_state, ray_index, RAY_SHADOW_RAY_CAST_DL);
110                                 enqueue_flag = 1;
111                         }
112                 }
113 #endif  /* __EMISSION__ */
114         }
115         return enqueue_flag;
116 }