Code refactor: add WorkTile struct for passing work to kernel.
[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         ccl_global uint *work_pools = kernel_split_params.work_pools;
34         uint total_work_size = kernel_split_params.total_work_size;
35         uint work_index;
36
37         if(!get_next_work(kg, work_pools, total_work_size, ray_index, &work_index)) {
38                 /* No more work, mark ray as inactive */
39                 kernel_split_state.ray_state[ray_index] = RAY_INACTIVE;
40
41                 return;
42         }
43
44         ccl_global WorkTile *tile = &kernel_split_params.tile;
45         uint x, y, sample;
46         get_work_pixel(tile, work_index, &x, &y, &sample);
47
48         /* Remap rng_state and buffer to current pixel. */
49         ccl_global uint *rng_state = kernel_split_params.tile.rng_state;
50         rng_state += tile->offset + x + y*tile->stride;
51
52         /* Store buffer offset for writing to passes. */
53         uint buffer_offset = (tile->offset + x + y*tile->stride) * kernel_data.film.pass_stride;
54         ccl_global float *buffer = tile->buffer + buffer_offset;
55         kernel_split_state.buffer_offset[ray_index] = buffer_offset;
56
57         /* Initialize random numbers and ray. */
58         uint rng_hash;
59         kernel_path_trace_setup(kg,
60                                 rng_state,
61                                 sample,
62                                 x, y,
63                                 &rng_hash,
64                                 &kernel_split_state.ray[ray_index]);
65
66         if(kernel_split_state.ray[ray_index].t != 0.0f) {
67                 /* Initialize throughput, path radiance, Ray, PathState;
68                  * These rays proceed with path-iteration.
69                  */
70                 kernel_split_state.throughput[ray_index] = make_float3(1.0f, 1.0f, 1.0f);
71                 path_radiance_init(&kernel_split_state.path_radiance[ray_index], kernel_data.film.use_light_pass);
72                 path_state_init(kg,
73                                 &kernel_split_state.sd_DL_shadow[ray_index],
74                                 &kernel_split_state.path_state[ray_index],
75                                 rng_hash,
76                                 sample,
77                                 &kernel_split_state.ray[ray_index]);
78 #ifdef __SUBSURFACE__
79                 kernel_path_subsurface_init_indirect(&kernel_split_state.ss_rays[ray_index]);
80 #endif
81         }
82         else {
83                 /* These rays do not participate in path-iteration. */
84                 float4 L_rad = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
85                 /* Accumulate result in output buffer. */
86                 kernel_write_pass_float4(buffer, sample, L_rad);
87                 ASSIGN_RAY_STATE(kernel_split_state.ray_state, ray_index, RAY_TO_REGENERATE);
88         }
89 }
90
91 CCL_NAMESPACE_END