799479ddb6ad0efd68d57bbe75218d9675190f75
[blender-staging.git] / intern / cycles / device / device_split_kernel.cpp
1 /*
2  * Copyright 2011-2016 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 #include "device_split_kernel.h"
18
19 #include "kernel_types.h"
20 #include "kernel_split_data.h"
21
22 #include "util_time.h"
23
24 CCL_NAMESPACE_BEGIN
25
26 static const double alpha = 0.1; /* alpha for rolling average */
27
28 DeviceSplitKernel::DeviceSplitKernel(Device *device) : device(device)
29 {
30         current_max_closure = -1;
31         first_tile = true;
32
33         avg_time_per_sample = 0.0;
34
35         kernel_path_init = NULL;
36         kernel_scene_intersect = NULL;
37         kernel_lamp_emission = NULL;
38         kernel_queue_enqueue = NULL;
39         kernel_background_buffer_update = NULL;
40         kernel_shader_eval = NULL;
41         kernel_holdout_emission_blurring_pathtermination_ao = NULL;
42         kernel_direct_lighting = NULL;
43         kernel_shadow_blocked = NULL;
44         kernel_next_iteration_setup = NULL;
45 }
46
47 DeviceSplitKernel::~DeviceSplitKernel()
48 {
49         device->mem_free(split_data);
50         device->mem_free(ray_state);
51         device->mem_free(use_queues_flag);
52         device->mem_free(queue_index);
53         device->mem_free(work_pool_wgs);
54
55         delete kernel_path_init;
56         delete kernel_scene_intersect;
57         delete kernel_lamp_emission;
58         delete kernel_queue_enqueue;
59         delete kernel_background_buffer_update;
60         delete kernel_shader_eval;
61         delete kernel_holdout_emission_blurring_pathtermination_ao;
62         delete kernel_direct_lighting;
63         delete kernel_shadow_blocked;
64         delete kernel_next_iteration_setup;
65 }
66
67 bool DeviceSplitKernel::load_kernels(const DeviceRequestedFeatures& requested_features)
68 {
69 #define LOAD_KERNEL(name) \
70                 kernel_##name = get_split_kernel_function(#name, requested_features); \
71                 if(!kernel_##name) { \
72                         return false; \
73                 }
74
75         LOAD_KERNEL(path_init);
76         LOAD_KERNEL(scene_intersect);
77         LOAD_KERNEL(lamp_emission);
78         LOAD_KERNEL(queue_enqueue);
79         LOAD_KERNEL(background_buffer_update);
80         LOAD_KERNEL(shader_eval);
81         LOAD_KERNEL(holdout_emission_blurring_pathtermination_ao);
82         LOAD_KERNEL(direct_lighting);
83         LOAD_KERNEL(shadow_blocked);
84         LOAD_KERNEL(next_iteration_setup);
85
86 #undef LOAD_KERNEL
87
88         current_max_closure = requested_features.max_closure;
89
90         return true;
91 }
92
93 size_t DeviceSplitKernel::max_elements_for_max_buffer_size(device_memory& kg, device_memory& data, size_t max_buffer_size)
94 {
95         size_t size_per_element = state_buffer_size(kg, data, 1024) / 1024;
96         return max_buffer_size / size_per_element;
97 }
98
99 bool DeviceSplitKernel::path_trace(DeviceTask *task,
100                                    RenderTile& tile,
101                                    device_memory& kgbuffer,
102                                    device_memory& kernel_data)
103 {
104         if(device->have_error()) {
105                 return false;
106         }
107
108         /* Get local size */
109         size_t local_size[2];
110         {
111                 int2 lsize = split_kernel_local_size();
112                 local_size[0] = lsize[0];
113                 local_size[1] = lsize[1];
114         }
115
116         /* Set gloabl size */
117         size_t global_size[2];
118         {
119                 int2 gsize = split_kernel_global_size(kgbuffer, kernel_data, task);
120
121                 /* Make sure that set work size is a multiple of local
122                  * work size dimensions.
123                  */
124                 global_size[0] = round_up(gsize[0], local_size[0]);
125                 global_size[1] = round_up(gsize[1], local_size[1]);
126         }
127
128         /* Number of elements in the global state buffer */
129         int num_global_elements = global_size[0] * global_size[1];
130
131         /* Allocate all required global memory once. */
132         if(first_tile) {
133                 first_tile = false;
134
135                 /* Calculate max groups */
136
137                 /* Denotes the maximum work groups possible w.r.t. current requested tile size. */
138                 unsigned int max_work_groups = num_global_elements / WORK_POOL_SIZE + 1;
139
140                 /* Allocate work_pool_wgs memory. */
141                 work_pool_wgs.resize(max_work_groups * sizeof(unsigned int));
142                 device->mem_alloc("work_pool_wgs", work_pool_wgs, MEM_READ_WRITE);
143
144                 queue_index.resize(NUM_QUEUES * sizeof(int));
145                 device->mem_alloc("queue_index", queue_index, MEM_READ_WRITE);
146
147                 use_queues_flag.resize(sizeof(char));
148                 device->mem_alloc("use_queues_flag", use_queues_flag, MEM_READ_WRITE);
149
150                 ray_state.resize(num_global_elements);
151                 device->mem_alloc("ray_state", ray_state, MEM_READ_WRITE);
152
153                 split_data.resize(state_buffer_size(kgbuffer, kernel_data, num_global_elements));
154                 device->mem_alloc("split_data", split_data, MEM_READ_WRITE);
155         }
156
157 #define ENQUEUE_SPLIT_KERNEL(name, global_size, local_size) \
158                 if(device->have_error()) { \
159                         return false; \
160                 } \
161                 if(!kernel_##name->enqueue(KernelDimensions(global_size, local_size), kgbuffer, kernel_data)) { \
162                         return false; \
163                 }
164
165         tile.sample = tile.start_sample;
166
167         /* for exponential increase between tile updates */
168         int time_multiplier = 1;
169
170         while(tile.sample < tile.start_sample + tile.num_samples) {
171                 /* to keep track of how long it takes to run a number of samples */
172                 double start_time = time_dt();
173
174                 /* initial guess to start rolling average */
175                 const int initial_num_samples = 1;
176                 /* approx number of samples per second */
177                 int samples_per_second = (avg_time_per_sample > 0.0) ?
178                                          int(double(time_multiplier) / avg_time_per_sample) + 1 : initial_num_samples;
179
180                 RenderTile subtile = tile;
181                 subtile.start_sample = tile.sample;
182                 subtile.num_samples = min(samples_per_second, tile.start_sample + tile.num_samples - tile.sample);
183
184                 if(device->have_error()) {
185                         return false;
186                 }
187
188                 /* reset state memory here as global size for data_init
189                  * kernel might not be large enough to do in kernel
190                  */
191                 device->mem_zero(work_pool_wgs);
192                 device->mem_zero(split_data);
193
194                 if(!enqueue_split_kernel_data_init(KernelDimensions(global_size, local_size),
195                                                            subtile,
196                                                            num_global_elements,
197                                                            kgbuffer,
198                                                            kernel_data,
199                                                            split_data,
200                                                            ray_state,
201                                                            queue_index,
202                                                            use_queues_flag,
203                                                            work_pool_wgs
204                                                            ))
205                 {
206                         return false;
207                 }
208
209                 ENQUEUE_SPLIT_KERNEL(path_init, global_size, local_size);
210
211                 bool activeRaysAvailable = true;
212
213                 while(activeRaysAvailable) {
214                         /* Twice the global work size of other kernels for
215                          * ckPathTraceKernel_shadow_blocked_direct_lighting. */
216                         size_t global_size_shadow_blocked[2];
217                         global_size_shadow_blocked[0] = global_size[0] * 2;
218                         global_size_shadow_blocked[1] = global_size[1];
219
220                         /* Do path-iteration in host [Enqueue Path-iteration kernels. */
221                         for(int PathIter = 0; PathIter < 16; PathIter++) {
222                                 ENQUEUE_SPLIT_KERNEL(scene_intersect, global_size, local_size);
223                                 ENQUEUE_SPLIT_KERNEL(lamp_emission, global_size, local_size);
224                                 ENQUEUE_SPLIT_KERNEL(queue_enqueue, global_size, local_size);
225                                 ENQUEUE_SPLIT_KERNEL(background_buffer_update, global_size, local_size);
226                                 ENQUEUE_SPLIT_KERNEL(shader_eval, global_size, local_size);
227                                 ENQUEUE_SPLIT_KERNEL(holdout_emission_blurring_pathtermination_ao, global_size, local_size);
228                                 ENQUEUE_SPLIT_KERNEL(direct_lighting, global_size, local_size);
229                                 ENQUEUE_SPLIT_KERNEL(shadow_blocked, global_size_shadow_blocked, local_size);
230                                 ENQUEUE_SPLIT_KERNEL(next_iteration_setup, global_size, local_size);
231
232                                 if(task->get_cancel()) {
233                                         return true;
234                                 }
235                         }
236
237                         /* Decide if we should exit path-iteration in host. */
238                         device->mem_copy_from(ray_state, 0, global_size[0] * global_size[1] * sizeof(char), 1, 1);
239
240                         activeRaysAvailable = false;
241
242                         for(int rayStateIter = 0; rayStateIter < global_size[0] * global_size[1]; ++rayStateIter) {
243                                 if(int8_t(ray_state.get_data()[rayStateIter]) != RAY_INACTIVE) {
244                                         /* Not all rays are RAY_INACTIVE. */
245                                         activeRaysAvailable = true;
246                                         break;
247                                 }
248                         }
249
250                         if(task->get_cancel()) {
251                                 return true;
252                         }
253                 }
254
255                 double time_per_sample = ((time_dt()-start_time) / subtile.num_samples);
256
257                 if(avg_time_per_sample == 0.0) {
258                         /* start rolling average */
259                         avg_time_per_sample = time_per_sample;
260                 }
261                 else {
262                         avg_time_per_sample = alpha*time_per_sample + (1.0-alpha)*avg_time_per_sample;
263                 }
264
265 #undef ENQUEUE_SPLIT_KERNEL
266
267                 tile.sample += subtile.num_samples;
268                 task->update_progress(&tile, tile.w*tile.h*subtile.num_samples);
269
270                 time_multiplier = min(time_multiplier << 1, 10);
271
272                 if(task->get_cancel()) {
273                         return true;
274                 }
275         }
276
277         return true;
278 }
279
280 CCL_NAMESPACE_END
281
282