d2e2ffaca91df1fcb43175f0f02b843e968e1c99
[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
25 ccl_device void kernel_path_init(KernelGlobals *kg) {
26         int ray_index = ccl_global_id(0) + ccl_global_id(1) * ccl_global_size(0);
27
28         /* This is the first assignment to ray_state;
29          * So we dont use ASSIGN_RAY_STATE macro.
30          */
31         kernel_split_state.ray_state[ray_index] = RAY_ACTIVE;
32
33         unsigned int my_sample;
34         unsigned int pixel_x;
35         unsigned int pixel_y;
36         unsigned int tile_x;
37         unsigned int tile_y;
38
39         unsigned int work_index = 0;
40         /* Get work. */
41         if(!get_next_work(kg, &work_index, ray_index)) {
42                 /* No more work, mark ray as inactive */
43                 kernel_split_state.ray_state[ray_index] = RAY_INACTIVE;
44
45                 return;
46         }
47
48         /* Get the sample associated with the work. */
49         my_sample = get_work_sample(kg, work_index, ray_index) + kernel_split_params.start_sample;
50
51         /* Get pixel and tile position associated with the work. */
52         get_work_pixel_tile_position(kg, &pixel_x, &pixel_y,
53                                 &tile_x, &tile_y,
54                                 work_index,
55                                 ray_index);
56         kernel_split_state.work_array[ray_index] = work_index;
57
58         ccl_global uint *rng_state = kernel_split_params.rng_state;
59         rng_state += kernel_split_params.offset + pixel_x + pixel_y*kernel_split_params.stride;
60
61         ccl_global float *buffer = kernel_split_params.buffer;
62         buffer += (kernel_split_params.offset + pixel_x + pixel_y * kernel_split_params.stride) * kernel_data.film.pass_stride;
63
64         /* Initialize random numbers and ray. */
65         kernel_path_trace_setup(kg,
66                                 rng_state,
67                                 my_sample,
68                                 pixel_x, pixel_y,
69                                 &kernel_split_state.rng[ray_index],
70                                 &kernel_split_state.ray[ray_index]);
71
72         if(kernel_split_state.ray[ray_index].t != 0.0f) {
73                 /* Initialize throughput, L_transparent, Ray, PathState;
74                  * These rays proceed with path-iteration.
75                  */
76                 kernel_split_state.throughput[ray_index] = make_float3(1.0f, 1.0f, 1.0f);
77                 kernel_split_state.L_transparent[ray_index] = 0.0f;
78                 path_radiance_init(&kernel_split_state.path_radiance[ray_index], kernel_data.film.use_light_pass);
79                 path_state_init(kg,
80                                 &kernel_split_state.sd_DL_shadow[ray_index],
81                                 &kernel_split_state.path_state[ray_index],
82                                 &kernel_split_state.rng[ray_index],
83                                 my_sample,
84                                 &kernel_split_state.ray[ray_index]);
85 #ifdef __KERNEL_DEBUG__
86                 debug_data_init(&kernel_split_state.debug_data[ray_index]);
87 #endif
88         }
89         else {
90                 /* These rays do not participate in path-iteration. */
91                 float4 L_rad = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
92                 /* Accumulate result in output buffer. */
93                 kernel_write_pass_float4(buffer, my_sample, L_rad);
94                 path_rng_end(kg, rng_state, kernel_split_state.rng[ray_index]);
95                 ASSIGN_RAY_STATE(kernel_split_state.ray_state, ray_index, RAY_TO_REGENERATE);
96         }
97 }
98
99 CCL_NAMESPACE_END
100