Code cleanup: simplify kernel side work stealing code.
[blender-staging.git] / intern / cycles / kernel / split / kernel_path_init.h
1 /*
2  * Copyright 2011-2017 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 CCL_NAMESPACE_BEGIN
18
19 /* This kernel initializes structures needed in path-iteration kernels.
20  * This is the first kernel in ray-tracing logic.
21  *
22  * Ray state of rays outside the tile-boundary will be marked RAY_INACTIVE
23  */
24 ccl_device void kernel_path_init(KernelGlobals *kg) {
25         int ray_index = ccl_global_id(0) + ccl_global_id(1) * ccl_global_size(0);
26
27         /* This is the first assignment to ray_state;
28          * So we dont use ASSIGN_RAY_STATE macro.
29          */
30         kernel_split_state.ray_state[ray_index] = RAY_ACTIVE;
31
32         /* Get work. */
33         uint work_index;
34         if(!get_next_work(kg, ray_index, &work_index)) {
35                 /* No more work, mark ray as inactive */
36                 kernel_split_state.ray_state[ray_index] = RAY_INACTIVE;
37
38                 return;
39         }
40
41         uint x, y, sample;
42         get_work_pixel(kg, work_index, &x, &y, &sample);
43
44         /* Remap rng_state and buffer to current pixel. */
45         ccl_global uint *rng_state = kernel_split_params.rng_state;
46         rng_state += kernel_split_params.offset + x + y*kernel_split_params.stride;
47
48         /* Store buffer offset for writing to passes. */
49         uint buffer_offset = (kernel_split_params.offset + x + y*kernel_split_params.stride) * kernel_data.film.pass_stride;
50         kernel_split_state.buffer_offset[ray_index] = buffer_offset;
51
52         /* Initialize random numbers and ray. */
53         uint rng_hash;
54         kernel_path_trace_setup(kg,
55                                 rng_state,
56                                 sample,
57                                 x, y,
58                                 &rng_hash,
59                                 &kernel_split_state.ray[ray_index]);
60
61         if(kernel_split_state.ray[ray_index].t != 0.0f) {
62                 /* Initialize throughput, path radiance, Ray, PathState;
63                  * These rays proceed with path-iteration.
64                  */
65                 kernel_split_state.throughput[ray_index] = make_float3(1.0f, 1.0f, 1.0f);
66                 path_radiance_init(&kernel_split_state.path_radiance[ray_index], kernel_data.film.use_light_pass);
67                 path_state_init(kg,
68                                 &kernel_split_state.sd_DL_shadow[ray_index],
69                                 &kernel_split_state.path_state[ray_index],
70                                 rng_hash,
71                                 sample,
72                                 &kernel_split_state.ray[ray_index]);
73 #ifdef __SUBSURFACE__
74                 kernel_path_subsurface_init_indirect(&kernel_split_state.ss_rays[ray_index]);
75 #endif
76         }
77         else {
78                 /* These rays do not participate in path-iteration. */
79                 float4 L_rad = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
80                 /* Accumulate result in output buffer. */
81                 ccl_global float *buffer = kernel_split_params.buffer + buffer_offset;
82                 kernel_write_pass_float4(buffer, sample, L_rad);
83                 ASSIGN_RAY_STATE(kernel_split_state.ray_state, ray_index, RAY_TO_REGENERATE);
84         }
85 }
86
87 CCL_NAMESPACE_END