Blender  V2.93
kernel_direct_lighting.h
Go to the documentation of this file.
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 
18 
19 /* This kernel takes care of direct lighting logic.
20  * However, the "shadow ray cast" part of direct lighting is handled
21  * in the next kernel.
22  *
23  * This kernels determines the rays for which a shadow_blocked() function
24  * associated with direct lighting should be executed. Those rays for which
25  * a shadow_blocked() function for direct-lighting must be executed, are
26  * marked with flag RAY_SHADOW_RAY_CAST_DL and enqueued into the queue
27  * QUEUE_SHADOW_RAY_CAST_DL_RAYS
28  *
29  * Note on Queues:
30  * This kernel only reads from the QUEUE_ACTIVE_AND_REGENERATED_RAYS queue
31  * and processes only the rays of state RAY_ACTIVE; If a ray needs to execute
32  * the corresponding shadow_blocked part, after direct lighting, the ray is
33  * marked with RAY_SHADOW_RAY_CAST_DL flag.
34  *
35  * State of queues when this kernel is called:
36  * - State of queues QUEUE_ACTIVE_AND_REGENERATED_RAYS and
37  * QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS will be same before and after this
38  * kernel call.
39  * - QUEUE_SHADOW_RAY_CAST_DL_RAYS queue will be filled with rays for which a
40  * shadow_blocked function must be executed, after this kernel call
41  * Before this kernel call the QUEUE_SHADOW_RAY_CAST_DL_RAYS will be empty.
42  */
43 ccl_device void kernel_direct_lighting(KernelGlobals *kg,
44  ccl_local_param unsigned int *local_queue_atomics)
45 {
46  if (ccl_local_id(0) == 0 && ccl_local_id(1) == 0) {
47  *local_queue_atomics = 0;
48  }
50 
51  char enqueue_flag = 0;
52  int ray_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
53  ray_index = get_ray_index(kg,
54  ray_index,
56  kernel_split_state.queue_data,
57  kernel_split_params.queue_size,
58  0);
59 
60  if (IS_STATE(kernel_split_state.ray_state, ray_index, RAY_ACTIVE)) {
61  ccl_global PathState *state = &kernel_split_state.path_state[ray_index];
62  ShaderData *sd = kernel_split_sd(sd, ray_index);
63 
64  /* direct lighting */
65 #ifdef __EMISSION__
66  bool flag = (kernel_data.integrator.use_direct_light && (sd->flag & SD_BSDF_HAS_EVAL));
67 
68 # ifdef __BRANCHED_PATH__
69  if (flag && kernel_data.integrator.branched) {
70  flag = false;
71  enqueue_flag = 1;
72  }
73 # endif /* __BRANCHED_PATH__ */
74 
75 # ifdef __SHADOW_TRICKS__
76  if (flag && state->flag & PATH_RAY_SHADOW_CATCHER) {
77  flag = false;
78  enqueue_flag = 1;
79  }
80 # endif /* __SHADOW_TRICKS__ */
81 
82  if (flag) {
83  /* Sample illumination from lights to find path contribution. */
84  float light_u, light_v;
85  path_state_rng_2D(kg, state, PRNG_LIGHT_U, &light_u, &light_v);
86  float terminate = path_state_rng_light_termination(kg, state);
87 
88  LightSample ls;
89  if (light_sample(kg, -1, light_u, light_v, sd->time, sd->P, state->bounce, &ls)) {
90  Ray light_ray;
91  light_ray.time = sd->time;
92 
93  BsdfEval L_light;
94  bool is_lamp;
95  if (direct_emission(kg,
96  sd,
97  AS_SHADER_DATA(&kernel_split_state.sd_DL_shadow[ray_index]),
98  &ls,
99  state,
100  &light_ray,
101  &L_light,
102  &is_lamp,
103  terminate)) {
104  /* Write intermediate data to global memory to access from
105  * the next kernel.
106  */
107  kernel_split_state.light_ray[ray_index] = light_ray;
108  kernel_split_state.bsdf_eval[ray_index] = L_light;
109  kernel_split_state.is_lamp[ray_index] = is_lamp;
110  /* Mark ray state for next shadow kernel. */
111  enqueue_flag = 1;
112  }
113  }
114  }
115 #endif /* __EMISSION__ */
116  }
117 
118 #ifdef __EMISSION__
119  /* Enqueue RAY_SHADOW_RAY_CAST_DL rays. */
120  enqueue_ray_index_local(ray_index,
122  enqueue_flag,
123  kernel_split_params.queue_size,
124  local_queue_atomics,
125  kernel_split_state.queue_data,
126  kernel_split_params.queue_index);
127 #endif
128 
129 #ifdef __BRANCHED_PATH__
130  /* Enqueue RAY_LIGHT_INDIRECT_NEXT_ITER rays
131  * this is the last kernel before next_iteration_setup that uses local atomics so we do this here
132  */
134  if (ccl_local_id(0) == 0 && ccl_local_id(1) == 0) {
135  *local_queue_atomics = 0;
136  }
138 
139  ray_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
141  ray_index,
144  kernel_split_params.queue_size,
145  local_queue_atomics,
146  kernel_split_state.queue_data,
147  kernel_split_params.queue_index);
148 
149 #endif /* __BRANCHED_PATH__ */
150 }
151 
#define kernel_data
#define ccl_global_id(d)
#define ccl_global_size(d)
#define ccl_local_param
#define ccl_device
ccl_device_inline uint ccl_local_id(uint d)
#define ccl_global
#define CCL_NAMESPACE_END
CCL_NAMESPACE_BEGIN ccl_device void kernel_direct_lighting(KernelGlobals *kg, ccl_local_param unsigned int *local_queue_atomics)
ccl_device_noinline_cpu bool direct_emission(KernelGlobals *kg, ShaderData *sd, ShaderData *emission_sd, LightSample *ls, ccl_addr_space PathState *state, Ray *ray, BsdfEval *eval, bool *is_lamp, float rand_terminate)
ccl_device_noinline bool light_sample(KernelGlobals *kg, int lamp, float randu, float randv, float time, float3 P, int bounce, LightSample *ls)
Definition: kernel_light.h:663
ccl_device int get_ray_index(KernelGlobals *kg, int thread_index, int queue_number, ccl_global int *queues, int queuesize, int empty_queue)
Definition: kernel_queues.h:53
ccl_device void enqueue_ray_index_local(int ray_index, int queue_number, char enqueue_flag, int queuesize, ccl_local_param unsigned int *local_queue_atomics, ccl_global int *Queue_data, ccl_global int *Queue_index)
Definition: kernel_queues.h:71
ccl_device_inline void path_state_rng_2D(KernelGlobals *kg, const ccl_addr_space PathState *state, int dimension, float *fx, float *fy)
ccl_device_inline float path_state_rng_light_termination(KernelGlobals *kg, const ccl_addr_space PathState *state)
#define kernel_split_params
#define kernel_split_sd(sd, ray_index)
#define kernel_split_state
@ SD_BSDF_HAS_EVAL
Definition: kernel_types.h:849
#define AS_SHADER_DATA(shader_data_tiny_storage)
#define IS_STATE(ray_state, ray_index, state)
@ PRNG_LIGHT_U
Definition: kernel_types.h:242
@ PATH_RAY_SHADOW_CATCHER
Definition: kernel_types.h:306
ShaderData
@ RAY_ACTIVE
@ RAY_LIGHT_INDIRECT_NEXT_ITER
@ QUEUE_LIGHT_INDIRECT_ITER
@ QUEUE_SHADOW_RAY_CAST_DL_RAYS
@ QUEUE_ACTIVE_AND_REGENERATED_RAYS
static ulong state[N]
float time
Definition: kernel_types.h:650
#define CCL_LOCAL_MEM_FENCE
Definition: util_atomic.h:32
#define ccl_barrier(flags)
Definition: util_atomic.h:33