6bb65cde2a38d9f26ad7b2534046ae6717a57429
[blender-staging.git] / intern / cycles / device / device.h
1 /*
2  * Copyright 2011-2013 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 #ifndef __DEVICE_H__
18 #define __DEVICE_H__
19
20 #include <stdlib.h>
21
22 #include "device/device_memory.h"
23 #include "device/device_task.h"
24
25 #include "util/util_list.h"
26 #include "util/util_stats.h"
27 #include "util/util_string.h"
28 #include "util/util_thread.h"
29 #include "util/util_texture.h"
30 #include "util/util_types.h"
31 #include "util/util_vector.h"
32
33 CCL_NAMESPACE_BEGIN
34
35 class Progress;
36 class RenderTile;
37
38 /* Device Types */
39
40 enum DeviceType {
41         DEVICE_NONE,
42         DEVICE_CPU,
43         DEVICE_OPENCL,
44         DEVICE_CUDA,
45         DEVICE_NETWORK,
46         DEVICE_MULTI
47 };
48
49 class DeviceInfo {
50 public:
51         DeviceType type;
52         string description;
53         string id; /* used for user preferences, should stay fixed with changing hardware config */
54         int num;
55         bool display_device;
56         bool advanced_shading;
57         bool has_bindless_textures; /* flag for GPU and Multi device */
58         bool has_volume_decoupled;
59         bool has_qbvh;
60         bool has_osl;
61         bool use_split_kernel; /* Denotes if the device is going to run cycles using split-kernel */
62         int cpu_threads;
63         vector<DeviceInfo> multi_devices;
64
65         DeviceInfo()
66         {
67                 type = DEVICE_CPU;
68                 id = "CPU";
69                 num = 0;
70                 cpu_threads = 0;
71                 display_device = false;
72                 advanced_shading = true;
73                 has_bindless_textures = false;
74                 has_volume_decoupled = false;
75                 has_qbvh = false;
76                 has_osl = false;
77                 use_split_kernel = false;
78         }
79
80         bool operator==(const DeviceInfo &info) {
81                 /* Multiple Devices with the same ID would be very bad. */
82                 assert(id != info.id || (type == info.type && num == info.num && description == info.description));
83                 return id == info.id;
84         }
85 };
86
87 class DeviceRequestedFeatures {
88 public:
89         /* Use experimental feature set. */
90         bool experimental;
91
92         /* Maximum number of closures in shader trees. */
93         int max_closure;
94
95         /* Selective nodes compilation. */
96
97         /* Identifier of a node group up to which all the nodes needs to be
98          * compiled in. Nodes from higher group indices will be ignores.
99          */
100         int max_nodes_group;
101
102         /* Features bitfield indicating which features from the requested group
103          * will be compiled in. Nodes which corresponds to features which are not
104          * in this bitfield will be ignored even if they're in the requested group.
105          */
106         int nodes_features;
107
108         /* BVH/sampling kernel features. */
109         bool use_hair;
110         bool use_object_motion;
111         bool use_camera_motion;
112
113         /* Denotes whether baking functionality is needed. */
114         bool use_baking;
115
116         /* Use subsurface scattering materials. */
117         bool use_subsurface;
118
119         /* Use volume materials. */
120         bool use_volume;
121
122         /* Use branched integrator. */
123         bool use_integrator_branched;
124
125         /* Use OpenSubdiv patch evaluation */
126         bool use_patch_evaluation;
127         
128         /* Use Transparent shadows */
129         bool use_transparent;
130
131         /* Use various shadow tricks, such as shadow catcher. */
132         bool use_shadow_tricks;
133
134         /* Per-uber shader usage flags. */
135         bool use_principled;
136
137         /* Denoising features. */
138         bool use_denoising;
139
140         DeviceRequestedFeatures()
141         {
142                 /* TODO(sergey): Find more meaningful defaults. */
143                 experimental = false;
144                 max_closure = 0;
145                 max_nodes_group = 0;
146                 nodes_features = 0;
147                 use_hair = false;
148                 use_object_motion = false;
149                 use_camera_motion = false;
150                 use_baking = false;
151                 use_subsurface = false;
152                 use_volume = false;
153                 use_integrator_branched = false;
154                 use_patch_evaluation = false;
155                 use_transparent = false;
156                 use_shadow_tricks = false;
157                 use_principled = false;
158                 use_denoising = false;
159         }
160
161         bool modified(const DeviceRequestedFeatures& requested_features)
162         {
163                 return !(experimental == requested_features.experimental &&
164                          max_closure == requested_features.max_closure &&
165                          max_nodes_group == requested_features.max_nodes_group &&
166                          nodes_features == requested_features.nodes_features &&
167                          use_hair == requested_features.use_hair &&
168                          use_object_motion == requested_features.use_object_motion &&
169                          use_camera_motion == requested_features.use_camera_motion &&
170                          use_baking == requested_features.use_baking &&
171                          use_subsurface == requested_features.use_subsurface &&
172                          use_volume == requested_features.use_volume &&
173                          use_integrator_branched == requested_features.use_integrator_branched &&
174                          use_patch_evaluation == requested_features.use_patch_evaluation &&
175                          use_transparent == requested_features.use_transparent &&
176                          use_shadow_tricks == requested_features.use_shadow_tricks &&
177                          use_principled == requested_features.use_principled &&
178                          use_denoising == requested_features.use_denoising);
179         }
180
181         /* Convert the requested features structure to a build options,
182          * which could then be passed to compilers.
183          */
184         string get_build_options(void) const
185         {
186                 string build_options = "";
187                 if(experimental) {
188                         build_options += "-D__KERNEL_EXPERIMENTAL__ ";
189                 }
190                 build_options += "-D__NODES_MAX_GROUP__=" +
191                         string_printf("%d", max_nodes_group);
192                 build_options += " -D__NODES_FEATURES__=" +
193                         string_printf("%d", nodes_features);
194                 build_options += string_printf(" -D__MAX_CLOSURE__=%d", max_closure);
195                 if(!use_hair) {
196                         build_options += " -D__NO_HAIR__";
197                 }
198                 if(!use_object_motion) {
199                         build_options += " -D__NO_OBJECT_MOTION__";
200                 }
201                 if(!use_camera_motion) {
202                         build_options += " -D__NO_CAMERA_MOTION__";
203                 }
204                 if(!use_baking) {
205                         build_options += " -D__NO_BAKING__";
206                 }
207                 if(!use_volume) {
208                         build_options += " -D__NO_VOLUME__";
209                 }
210                 if(!use_subsurface) {
211                         build_options += " -D__NO_SUBSURFACE__";
212                 }
213                 if(!use_integrator_branched) {
214                         build_options += " -D__NO_BRANCHED_PATH__";
215                 }
216                 if(!use_patch_evaluation) {
217                         build_options += " -D__NO_PATCH_EVAL__";
218                 }
219                 if(!use_transparent && !use_volume) {
220                         build_options += " -D__NO_TRANSPARENT__";
221                 }
222                 if(!use_shadow_tricks) {
223                         build_options += " -D__NO_SHADOW_TRICKS__";
224                 }
225                 if(!use_principled) {
226                         build_options += " -D__NO_PRINCIPLED__";
227                 }
228                 if(!use_denoising) {
229                         build_options += " -D__NO_DENOISING__";
230                 }
231                 return build_options;
232         }
233 };
234
235 std::ostream& operator <<(std::ostream &os,
236                           const DeviceRequestedFeatures& requested_features);
237
238 /* Device */
239
240 struct DeviceDrawParams {
241         function<void(void)> bind_display_space_shader_cb;
242         function<void(void)> unbind_display_space_shader_cb;
243 };
244
245 class Device {
246         friend class device_sub_ptr;
247 protected:
248         Device(DeviceInfo& info_, Stats &stats_, bool background) : background(background), vertex_buffer(0), info(info_), stats(stats_) {}
249
250         bool background;
251         string error_msg;
252
253         /* used for real time display */
254         unsigned int vertex_buffer;
255
256         virtual device_ptr mem_alloc_sub_ptr(device_memory& /*mem*/, int /*offset*/, int /*size*/)
257         {
258                 /* Only required for devices that implement denoising. */
259                 assert(false);
260                 return (device_ptr) 0;
261         }
262         virtual void mem_free_sub_ptr(device_ptr /*ptr*/) {};
263
264 public:
265         virtual ~Device();
266
267         /* info */
268         DeviceInfo info;
269         virtual const string& error_message() { return error_msg; }
270         bool have_error() { return !error_message().empty(); }
271         virtual void set_error(const string& error)
272         {
273                 if(!have_error()) {
274                         error_msg = error;
275                 }
276                 fprintf(stderr, "%s\n", error.c_str());
277                 fflush(stderr);
278         }
279         virtual bool show_samples() const { return false; }
280
281         /* statistics */
282         Stats &stats;
283
284         /* regular memory */
285         virtual void mem_alloc(device_memory& mem) = 0;
286         virtual void mem_copy_to(device_memory& mem) = 0;
287         virtual void mem_copy_from(device_memory& mem,
288                 int y, int w, int h, int elem) = 0;
289         virtual void mem_zero(device_memory& mem) = 0;
290         virtual void mem_free(device_memory& mem) = 0;
291
292         virtual int mem_address_alignment() { return 16; }
293
294         /* constant memory */
295         virtual void const_copy_to(const char *name, void *host, size_t size) = 0;
296
297         /* texture memory */
298         virtual void tex_alloc(device_memory& /*mem*/) {};
299         virtual void tex_free(device_memory& /*mem*/) {};
300
301         /* pixel memory */
302         virtual void pixels_alloc(device_memory& mem);
303         virtual void pixels_copy_from(device_memory& mem, int y, int w, int h);
304         virtual void pixels_free(device_memory& mem);
305
306         /* open shading language, only for CPU device */
307         virtual void *osl_memory() { return NULL; }
308
309         /* load/compile kernels, must be called before adding tasks */ 
310         virtual bool load_kernels(
311                 const DeviceRequestedFeatures& /*requested_features*/)
312         { return true; }
313
314         /* tasks */
315         virtual int get_split_task_count(DeviceTask& task) = 0;
316         virtual void task_add(DeviceTask& task) = 0;
317         virtual void task_wait() = 0;
318         virtual void task_cancel() = 0;
319         
320         /* opengl drawing */
321         virtual void draw_pixels(device_memory& mem, int y, int w, int h,
322                 int dx, int dy, int width, int height, bool transparent,
323                 const DeviceDrawParams &draw_params);
324
325 #ifdef WITH_NETWORK
326         /* networking */
327         void server_run();
328 #endif
329
330         /* multi device */
331         virtual void map_tile(Device * /*sub_device*/, RenderTile& /*tile*/) {}
332         virtual int device_number(Device * /*sub_device*/) { return 0; }
333         virtual void map_neighbor_tiles(Device * /*sub_device*/, RenderTile * /*tiles*/) {}
334         virtual void unmap_neighbor_tiles(Device * /*sub_device*/, RenderTile * /*tiles*/) {}
335
336         /* static */
337         static Device *create(DeviceInfo& info, Stats &stats, bool background = true);
338
339         static DeviceType type_from_string(const char *name);
340         static string string_from_type(DeviceType type);
341         static vector<DeviceType>& available_types();
342         static vector<DeviceInfo>& available_devices();
343         static string device_capabilities();
344         static DeviceInfo get_multi_device(const vector<DeviceInfo>& subdevices,
345                                            int threads,
346                                            bool background);
347
348         /* Tag devices lists for update. */
349         static void tag_update();
350
351         static void free_memory();
352 private:
353         /* Indicted whether device types and devices lists were initialized. */
354         static bool need_types_update, need_devices_update;
355         static thread_mutex device_mutex;
356         static vector<DeviceType> types;
357         static vector<DeviceInfo> devices;
358 };
359
360 CCL_NAMESPACE_END
361
362 #endif /* __DEVICE_H__ */
363