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