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