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