c75931855b2df9e077632683186a69d483ef5325
[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         uint work_index = 0;
33         /* Get work. */
34         if(!get_next_work(kg, &work_index, ray_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         /* Get the sample associated with the work. */
42         uint sample = get_work_sample(kg, work_index, ray_index) + kernel_split_params.start_sample;
43
44         /* Get pixel and tile position associated with the work. */
45         uint pixel_x, pixel_y, tile_x, tile_y;
46         get_work_pixel_tile_position(kg, &pixel_x, &pixel_y,
47                                      &tile_x, &tile_y,
48                                      work_index,
49                                      ray_index);
50         kernel_split_state.work_array[ray_index] = work_index;
51
52         ccl_global uint *rng_state = kernel_split_params.rng_state;
53         rng_state += kernel_split_params.offset + pixel_x + pixel_y*kernel_split_params.stride;
54
55         ccl_global float *buffer = kernel_split_params.buffer;
56         buffer += (kernel_split_params.offset + pixel_x + pixel_y * kernel_split_params.stride) * kernel_data.film.pass_stride;
57
58         /* Initialize random numbers and ray. */
59         uint rng_hash;
60         kernel_path_trace_setup(kg,
61                                 rng_state,
62                                 sample,
63                                 pixel_x, pixel_y,
64                                 &rng_hash,
65                                 &kernel_split_state.ray[ray_index]);
66
67         if(kernel_split_state.ray[ray_index].t != 0.0f) {
68                 /* Initialize throughput, path radiance, Ray, PathState;
69                  * These rays proceed with path-iteration.
70                  */
71                 kernel_split_state.throughput[ray_index] = make_float3(1.0f, 1.0f, 1.0f);
72                 path_radiance_init(&kernel_split_state.path_radiance[ray_index], kernel_data.film.use_light_pass);
73                 path_state_init(kg,
74                                 &kernel_split_state.sd_DL_shadow[ray_index],
75                                 &kernel_split_state.path_state[ray_index],
76                                 rng_hash,
77                                 sample,
78                                 &kernel_split_state.ray[ray_index]);
79 #ifdef __SUBSURFACE__
80                 kernel_path_subsurface_init_indirect(&kernel_split_state.ss_rays[ray_index]);
81 #endif
82         }
83         else {
84                 /* These rays do not participate in path-iteration. */
85                 float4 L_rad = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
86                 /* Accumulate result in output buffer. */
87                 kernel_write_pass_float4(buffer, sample, L_rad);
88                 ASSIGN_RAY_STATE(kernel_split_state.ray_state, ray_index, RAY_TO_REGENERATE);
89         }
90 }
91
92 CCL_NAMESPACE_END