Cycles: Replace __MAX_CLOSURE__ build option with runtime integrator variable
[blender.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/device_split_kernel.h"
18
19 #include "kernel/kernel_types.h"
20 #include "kernel/split/kernel_split_data_types.h"
21
22 #include "util/util_logging.h"
23 #include "util/util_time.h"
24
25 CCL_NAMESPACE_BEGIN
26
27 static const double alpha = 0.1; /* alpha for rolling average */
28
29 DeviceSplitKernel::DeviceSplitKernel(Device *device)
30 : device(device),
31   split_data(device, "split_data"),
32   ray_state(device, "ray_state", MEM_READ_WRITE),
33   queue_index(device, "queue_index"),
34   use_queues_flag(device, "use_queues_flag"),
35   work_pool_wgs(device, "work_pool_wgs")
36 {
37         first_tile = true;
38
39         avg_time_per_sample = 0.0;
40
41         kernel_path_init = NULL;
42         kernel_scene_intersect = NULL;
43         kernel_lamp_emission = NULL;
44         kernel_do_volume = NULL;
45         kernel_queue_enqueue = NULL;
46         kernel_indirect_background = NULL;
47         kernel_shader_setup = NULL;
48         kernel_shader_sort = NULL;
49         kernel_shader_eval = NULL;
50         kernel_holdout_emission_blurring_pathtermination_ao = NULL;
51         kernel_subsurface_scatter = NULL;
52         kernel_direct_lighting = NULL;
53         kernel_shadow_blocked_ao = NULL;
54         kernel_shadow_blocked_dl = NULL;
55         kernel_enqueue_inactive = NULL;
56         kernel_next_iteration_setup = NULL;
57         kernel_indirect_subsurface = NULL;
58         kernel_buffer_update = NULL;
59 }
60
61 DeviceSplitKernel::~DeviceSplitKernel()
62 {
63         split_data.free();
64         ray_state.free();
65         use_queues_flag.free();
66         queue_index.free();
67         work_pool_wgs.free();
68
69         delete kernel_path_init;
70         delete kernel_scene_intersect;
71         delete kernel_lamp_emission;
72         delete kernel_do_volume;
73         delete kernel_queue_enqueue;
74         delete kernel_indirect_background;
75         delete kernel_shader_setup;
76         delete kernel_shader_sort;
77         delete kernel_shader_eval;
78         delete kernel_holdout_emission_blurring_pathtermination_ao;
79         delete kernel_subsurface_scatter;
80         delete kernel_direct_lighting;
81         delete kernel_shadow_blocked_ao;
82         delete kernel_shadow_blocked_dl;
83         delete kernel_enqueue_inactive;
84         delete kernel_next_iteration_setup;
85         delete kernel_indirect_subsurface;
86         delete kernel_buffer_update;
87 }
88
89 bool DeviceSplitKernel::load_kernels(const DeviceRequestedFeatures& requested_features)
90 {
91 #define LOAD_KERNEL(name) \
92                 kernel_##name = get_split_kernel_function(#name, requested_features); \
93                 if(!kernel_##name) { \
94                         return false; \
95                 }
96
97         LOAD_KERNEL(path_init);
98         LOAD_KERNEL(scene_intersect);
99         LOAD_KERNEL(lamp_emission);
100         LOAD_KERNEL(do_volume);
101         LOAD_KERNEL(queue_enqueue);
102         LOAD_KERNEL(indirect_background);
103         LOAD_KERNEL(shader_setup);
104         LOAD_KERNEL(shader_sort);
105         LOAD_KERNEL(shader_eval);
106         LOAD_KERNEL(holdout_emission_blurring_pathtermination_ao);
107         LOAD_KERNEL(subsurface_scatter);
108         LOAD_KERNEL(direct_lighting);
109         LOAD_KERNEL(shadow_blocked_ao);
110         LOAD_KERNEL(shadow_blocked_dl);
111         LOAD_KERNEL(enqueue_inactive);
112         LOAD_KERNEL(next_iteration_setup);
113         LOAD_KERNEL(indirect_subsurface);
114         LOAD_KERNEL(buffer_update);
115
116 #undef LOAD_KERNEL
117
118         return true;
119 }
120
121 size_t DeviceSplitKernel::max_elements_for_max_buffer_size(device_memory& kg, device_memory& data, uint64_t max_buffer_size)
122 {
123         uint64_t size_per_element = state_buffer_size(kg, data, 1024) / 1024;
124         VLOG(1) << "Split state element size: "
125                 << string_human_readable_number(size_per_element) << " bytes. ("
126                 << string_human_readable_size(size_per_element) << ").";
127         return max_buffer_size / size_per_element;
128 }
129
130 bool DeviceSplitKernel::path_trace(DeviceTask *task,
131                                    RenderTile& tile,
132                                    device_memory& kgbuffer,
133                                    device_memory& kernel_data)
134 {
135         if(device->have_error()) {
136                 return false;
137         }
138
139         /* Get local size */
140         size_t local_size[2];
141         {
142                 int2 lsize = split_kernel_local_size();
143                 local_size[0] = lsize[0];
144                 local_size[1] = lsize[1];
145         }
146
147         /* Number of elements in the global state buffer */
148         int num_global_elements = global_size[0] * global_size[1];
149
150         /* Allocate all required global memory once. */
151         if(first_tile) {
152                 first_tile = false;
153
154                 /* Set gloabl size */
155                 {
156                         int2 gsize = split_kernel_global_size(kgbuffer, kernel_data, task);
157
158                         /* Make sure that set work size is a multiple of local
159                          * work size dimensions.
160                          */
161                         global_size[0] = round_up(gsize[0], local_size[0]);
162                         global_size[1] = round_up(gsize[1], local_size[1]);
163                 }
164
165                 num_global_elements = global_size[0] * global_size[1];
166                 assert(num_global_elements % WORK_POOL_SIZE == 0);
167
168                 /* Calculate max groups */
169
170                 /* Denotes the maximum work groups possible w.r.t. current requested tile size. */
171                 unsigned int work_pool_size = (device->info.type == DEVICE_CPU) ? WORK_POOL_SIZE_CPU : WORK_POOL_SIZE_GPU;
172                 unsigned int max_work_groups = num_global_elements / work_pool_size + 1;
173
174                 /* Allocate work_pool_wgs memory. */
175                 work_pool_wgs.alloc_to_device(max_work_groups);
176                 queue_index.alloc_to_device(NUM_QUEUES);
177                 use_queues_flag.alloc_to_device(1);
178                 split_data.alloc_to_device(state_buffer_size(kgbuffer, kernel_data, num_global_elements));
179                 ray_state.alloc(num_global_elements);
180         }
181
182 #define ENQUEUE_SPLIT_KERNEL(name, global_size, local_size) \
183                 if(device->have_error()) { \
184                         return false; \
185                 } \
186                 if(!kernel_##name->enqueue(KernelDimensions(global_size, local_size), kgbuffer, kernel_data)) { \
187                         return false; \
188                 }
189
190         tile.sample = tile.start_sample;
191
192         /* for exponential increase between tile updates */
193         int time_multiplier = 1;
194
195         while(tile.sample < tile.start_sample + tile.num_samples) {
196                 /* to keep track of how long it takes to run a number of samples */
197                 double start_time = time_dt();
198
199                 /* initial guess to start rolling average */
200                 const int initial_num_samples = 1;
201                 /* approx number of samples per second */
202                 int samples_per_second = (avg_time_per_sample > 0.0) ?
203                                          int(double(time_multiplier) / avg_time_per_sample) + 1 : initial_num_samples;
204
205                 RenderTile subtile = tile;
206                 subtile.start_sample = tile.sample;
207                 subtile.num_samples = min(samples_per_second, tile.start_sample + tile.num_samples - tile.sample);
208
209                 if(device->have_error()) {
210                         return false;
211                 }
212
213                 /* reset state memory here as global size for data_init
214                  * kernel might not be large enough to do in kernel
215                  */
216                 work_pool_wgs.zero_to_device();
217                 split_data.zero_to_device();
218                 ray_state.zero_to_device();
219
220                 if(!enqueue_split_kernel_data_init(KernelDimensions(global_size, local_size),
221                                                    subtile,
222                                                    num_global_elements,
223                                                    kgbuffer,
224                                                    kernel_data,
225                                                    split_data,
226                                                    ray_state,
227                                                    queue_index,
228                                                    use_queues_flag,
229                                                    work_pool_wgs))
230                 {
231                         return false;
232                 }
233
234                 ENQUEUE_SPLIT_KERNEL(path_init, global_size, local_size);
235
236                 bool activeRaysAvailable = true;
237                 double cancel_time = DBL_MAX;
238
239                 while(activeRaysAvailable) {
240                         /* Do path-iteration in host [Enqueue Path-iteration kernels. */
241                         for(int PathIter = 0; PathIter < 16; PathIter++) {
242                                 ENQUEUE_SPLIT_KERNEL(scene_intersect, global_size, local_size);
243                                 ENQUEUE_SPLIT_KERNEL(lamp_emission, global_size, local_size);
244                                 ENQUEUE_SPLIT_KERNEL(do_volume, global_size, local_size);
245                                 ENQUEUE_SPLIT_KERNEL(queue_enqueue, global_size, local_size);
246                                 ENQUEUE_SPLIT_KERNEL(indirect_background, global_size, local_size);
247                                 ENQUEUE_SPLIT_KERNEL(shader_setup, global_size, local_size);
248                                 ENQUEUE_SPLIT_KERNEL(shader_sort, global_size, local_size);
249                                 ENQUEUE_SPLIT_KERNEL(shader_eval, global_size, local_size);
250                                 ENQUEUE_SPLIT_KERNEL(holdout_emission_blurring_pathtermination_ao, global_size, local_size);
251                                 ENQUEUE_SPLIT_KERNEL(subsurface_scatter, global_size, local_size);
252                                 ENQUEUE_SPLIT_KERNEL(queue_enqueue, global_size, local_size);
253                                 ENQUEUE_SPLIT_KERNEL(direct_lighting, global_size, local_size);
254                                 ENQUEUE_SPLIT_KERNEL(shadow_blocked_ao, global_size, local_size);
255                                 ENQUEUE_SPLIT_KERNEL(shadow_blocked_dl, global_size, local_size);
256                                 ENQUEUE_SPLIT_KERNEL(enqueue_inactive, global_size, local_size);
257                                 ENQUEUE_SPLIT_KERNEL(next_iteration_setup, global_size, local_size);
258                                 ENQUEUE_SPLIT_KERNEL(indirect_subsurface, global_size, local_size);
259                                 ENQUEUE_SPLIT_KERNEL(queue_enqueue, global_size, local_size);
260                                 ENQUEUE_SPLIT_KERNEL(buffer_update, global_size, local_size);
261
262                                 if(task->get_cancel() && cancel_time == DBL_MAX) {
263                                         /* Wait up to twice as many seconds for current samples to finish 
264                                          * to avoid artifacts in render result from ending too soon.
265                                          */
266                                         cancel_time = time_dt() + 2.0 * time_multiplier;
267                                 }
268
269                                 if(time_dt() > cancel_time) {
270                                         return true;
271                                 }
272                         }
273
274                         /* Decide if we should exit path-iteration in host. */
275                         ray_state.copy_from_device(0, global_size[0] * global_size[1], 1);
276
277                         activeRaysAvailable = false;
278
279                         for(int rayStateIter = 0; rayStateIter < global_size[0] * global_size[1]; ++rayStateIter) {
280                                 if(!IS_STATE(ray_state.data(), rayStateIter, RAY_INACTIVE)) {
281                                         if(IS_STATE(ray_state.data(), rayStateIter, RAY_INVALID)) {
282                                                 /* Something went wrong, abort to avoid looping endlessly. */
283                                                 device->set_error("Split kernel error: invalid ray state");
284                                                 return false;
285                                         }
286
287                                         /* Not all rays are RAY_INACTIVE. */
288                                         activeRaysAvailable = true;
289                                         break;
290                                 }
291                         }
292
293                         if(time_dt() > cancel_time) {
294                                 return true;
295                         }
296                 }
297
298                 double time_per_sample = ((time_dt()-start_time) / subtile.num_samples);
299
300                 if(avg_time_per_sample == 0.0) {
301                         /* start rolling average */
302                         avg_time_per_sample = time_per_sample;
303                 }
304                 else {
305                         avg_time_per_sample = alpha*time_per_sample + (1.0-alpha)*avg_time_per_sample;
306                 }
307
308 #undef ENQUEUE_SPLIT_KERNEL
309
310                 tile.sample += subtile.num_samples;
311                 task->update_progress(&tile, tile.w*tile.h*subtile.num_samples);
312
313                 time_multiplier = min(time_multiplier << 1, 10);
314
315                 if(task->get_cancel()) {
316                         return true;
317                 }
318         }
319
320         return true;
321 }
322
323 CCL_NAMESPACE_END
324
325