Merge branch 'master' into blender2.8
[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 "bvh/bvh_params.h"
23
24 #include "device/device_memory.h"
25 #include "device/device_task.h"
26
27 #include "util/util_list.h"
28 #include "util/util_stats.h"
29 #include "util/util_string.h"
30 #include "util/util_thread.h"
31 #include "util/util_texture.h"
32 #include "util/util_types.h"
33 #include "util/util_vector.h"
34
35 CCL_NAMESPACE_BEGIN
36
37 class Progress;
38 class RenderTile;
39
40 /* Device Types */
41
42 enum DeviceType {
43         DEVICE_NONE,
44         DEVICE_CPU,
45         DEVICE_OPENCL,
46         DEVICE_CUDA,
47         DEVICE_NETWORK,
48         DEVICE_MULTI
49 };
50
51 class DeviceInfo {
52 public:
53         DeviceType type;
54         string description;
55         string id; /* used for user preferences, should stay fixed with changing hardware config */
56         int num;
57         bool display_device;            /* GPU is used as a display device. */
58         bool advanced_shading;          /* Supports full shading system. */
59         bool has_half_images;           /* Support half-float textures. */
60         bool has_volume_decoupled;      /* Decoupled volume shading. */
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_half_images = false;
75                 has_volume_decoupled = 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         /* Selective nodes compilation. */
93
94         /* Identifier of a node group up to which all the nodes needs to be
95          * compiled in. Nodes from higher group indices will be ignores.
96          */
97         int max_nodes_group;
98
99         /* Features bitfield indicating which features from the requested group
100          * will be compiled in. Nodes which corresponds to features which are not
101          * in this bitfield will be ignored even if they're in the requested group.
102          */
103         int nodes_features;
104
105         /* BVH/sampling kernel features. */
106         bool use_hair;
107         bool use_object_motion;
108         bool use_camera_motion;
109
110         /* Denotes whether baking functionality is needed. */
111         bool use_baking;
112
113         /* Use subsurface scattering materials. */
114         bool use_subsurface;
115
116         /* Use volume materials. */
117         bool use_volume;
118
119         /* Use branched integrator. */
120         bool use_integrator_branched;
121
122         /* Use OpenSubdiv patch evaluation */
123         bool use_patch_evaluation;
124
125         /* Use Transparent shadows */
126         bool use_transparent;
127
128         /* Use various shadow tricks, such as shadow catcher. */
129         bool use_shadow_tricks;
130
131         /* Per-uber shader usage flags. */
132         bool use_principled;
133
134         /* Denoising features. */
135         bool use_denoising;
136
137         /* Use raytracing in shaders. */
138         bool use_shader_raytrace;
139
140         DeviceRequestedFeatures()
141         {
142                 /* TODO(sergey): Find more meaningful defaults. */
143                 experimental = false;
144                 max_nodes_group = 0;
145                 nodes_features = 0;
146                 use_hair = false;
147                 use_object_motion = false;
148                 use_camera_motion = false;
149                 use_baking = false;
150                 use_subsurface = false;
151                 use_volume = false;
152                 use_integrator_branched = false;
153                 use_patch_evaluation = false;
154                 use_transparent = false;
155                 use_shadow_tricks = false;
156                 use_principled = false;
157                 use_denoising = false;
158                 use_shader_raytrace = false;
159         }
160
161         bool modified(const DeviceRequestedFeatures& requested_features)
162         {
163                 return !(experimental == requested_features.experimental &&
164                          max_nodes_group == requested_features.max_nodes_group &&
165                          nodes_features == requested_features.nodes_features &&
166                          use_hair == requested_features.use_hair &&
167                          use_object_motion == requested_features.use_object_motion &&
168                          use_camera_motion == requested_features.use_camera_motion &&
169                          use_baking == requested_features.use_baking &&
170                          use_subsurface == requested_features.use_subsurface &&
171                          use_volume == requested_features.use_volume &&
172                          use_integrator_branched == requested_features.use_integrator_branched &&
173                          use_patch_evaluation == requested_features.use_patch_evaluation &&
174                          use_transparent == requested_features.use_transparent &&
175                          use_shadow_tricks == requested_features.use_shadow_tricks &&
176                          use_principled == requested_features.use_principled &&
177                          use_denoising == requested_features.use_denoising &&
178                          use_shader_raytrace == requested_features.use_shader_raytrace);
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                 if(!use_hair) {
195                         build_options += " -D__NO_HAIR__";
196                 }
197                 if(!use_object_motion) {
198                         build_options += " -D__NO_OBJECT_MOTION__";
199                 }
200                 if(!use_camera_motion) {
201                         build_options += " -D__NO_CAMERA_MOTION__";
202                 }
203                 if(!use_baking) {
204                         build_options += " -D__NO_BAKING__";
205                 }
206                 if(!use_volume) {
207                         build_options += " -D__NO_VOLUME__";
208                 }
209                 if(!use_subsurface) {
210                         build_options += " -D__NO_SUBSURFACE__";
211                 }
212                 if(!use_integrator_branched) {
213                         build_options += " -D__NO_BRANCHED_PATH__";
214                 }
215                 if(!use_patch_evaluation) {
216                         build_options += " -D__NO_PATCH_EVAL__";
217                 }
218                 if(!use_transparent && !use_volume) {
219                         build_options += " -D__NO_TRANSPARENT__";
220                 }
221                 if(!use_shadow_tricks) {
222                         build_options += " -D__NO_SHADOW_TRICKS__";
223                 }
224                 if(!use_principled) {
225                         build_options += " -D__NO_PRINCIPLED__";
226                 }
227                 if(!use_denoising) {
228                         build_options += " -D__NO_DENOISING__";
229                 }
230                 if(!use_shader_raytrace) {
231                         build_options += " -D__NO_SHADER_RAYTRACE__";
232                 }
233                 return build_options;
234         }
235 };
236
237 std::ostream& operator <<(std::ostream &os,
238                           const DeviceRequestedFeatures& requested_features);
239
240 /* Device */
241
242 struct DeviceDrawParams {
243         function<void(void)> bind_display_space_shader_cb;
244         function<void(void)> unbind_display_space_shader_cb;
245 };
246
247 class Device {
248         friend class device_sub_ptr;
249 protected:
250         enum {
251                 FALLBACK_SHADER_STATUS_NONE = 0,
252                 FALLBACK_SHADER_STATUS_ERROR,
253                 FALLBACK_SHADER_STATUS_SUCCESS,
254         };
255
256         Device(DeviceInfo& info_, Stats &stats_, bool background) : background(background),
257             vertex_buffer(0),
258             fallback_status(FALLBACK_SHADER_STATUS_NONE), fallback_shader_program(0),
259             info(info_), stats(stats_) {}
260
261         bool background;
262         string error_msg;
263
264         /* used for real time display */
265         unsigned int vertex_buffer;
266         int fallback_status, fallback_shader_program;
267         int image_texture_location, fullscreen_location;
268
269         bool bind_fallback_display_space_shader(const float width, const float height);
270
271         virtual device_ptr mem_alloc_sub_ptr(device_memory& /*mem*/, int /*offset*/, int /*size*/)
272         {
273                 /* Only required for devices that implement denoising. */
274                 assert(false);
275                 return (device_ptr) 0;
276         }
277         virtual void mem_free_sub_ptr(device_ptr /*ptr*/) {};
278
279 public:
280         virtual ~Device();
281
282         /* info */
283         DeviceInfo info;
284         virtual const string& error_message() { return error_msg; }
285         bool have_error() { return !error_message().empty(); }
286         virtual void set_error(const string& error)
287         {
288                 if(!have_error()) {
289                         error_msg = error;
290                 }
291                 fprintf(stderr, "%s\n", error.c_str());
292                 fflush(stderr);
293         }
294         virtual bool show_samples() const { return false; }
295         virtual BVHLayoutMask get_bvh_layout_mask() const = 0;
296
297         /* statistics */
298         Stats &stats;
299
300         /* memory alignment */
301         virtual int mem_sub_ptr_alignment() { return MIN_ALIGNMENT_CPU_DATA_TYPES; }
302
303         /* constant memory */
304         virtual void const_copy_to(const char *name, void *host, size_t size) = 0;
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,
322             int w, int h, int width, int height,
323             int dx, int dy, int dw, int dh,
324             bool transparent, const DeviceDrawParams &draw_params);
325
326 #ifdef WITH_NETWORK
327         /* networking */
328         void server_run();
329 #endif
330
331         /* multi device */
332         virtual void map_tile(Device * /*sub_device*/, RenderTile& /*tile*/) {}
333         virtual int device_number(Device * /*sub_device*/) { return 0; }
334         virtual void map_neighbor_tiles(Device * /*sub_device*/, RenderTile * /*tiles*/) {}
335         virtual void unmap_neighbor_tiles(Device * /*sub_device*/, RenderTile * /*tiles*/) {}
336
337         /* static */
338         static Device *create(DeviceInfo& info, Stats &stats, bool background = true);
339
340         static DeviceType type_from_string(const char *name);
341         static string string_from_type(DeviceType type);
342         static vector<DeviceType>& available_types();
343         static vector<DeviceInfo>& available_devices();
344         static string device_capabilities();
345         static DeviceInfo get_multi_device(const vector<DeviceInfo>& subdevices,
346                                            int threads,
347                                            bool background);
348
349         /* Tag devices lists for update. */
350         static void tag_update();
351
352         static void free_memory();
353
354 protected:
355         /* Memory allocation, only accessed through device_memory. */
356         friend class MultiDevice;
357         friend class DeviceServer;
358         friend class device_memory;
359
360         virtual void mem_alloc(device_memory& mem) = 0;
361         virtual void mem_copy_to(device_memory& mem) = 0;
362         virtual void mem_copy_from(device_memory& mem,
363                 int y, int w, int h, int elem) = 0;
364         virtual void mem_zero(device_memory& mem) = 0;
365         virtual void mem_free(device_memory& mem) = 0;
366
367 private:
368         /* Indicted whether device types and devices lists were initialized. */
369         static bool need_types_update, need_devices_update;
370         static thread_mutex device_mutex;
371         static vector<DeviceType> types;
372         static vector<DeviceInfo> devices;
373 };
374
375 CCL_NAMESPACE_END
376
377 #endif /* __DEVICE_H__ */