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         bool has_profiling;             /* Supports runtime collection of profiling info. */
64         int cpu_threads;
65         vector<DeviceInfo> multi_devices;
66
67         DeviceInfo()
68         {
69                 type = DEVICE_CPU;
70                 id = "CPU";
71                 num = 0;
72                 cpu_threads = 0;
73                 display_device = false;
74                 advanced_shading = true;
75                 has_half_images = false;
76                 has_volume_decoupled = false;
77                 has_osl = false;
78                 use_split_kernel = false;
79                 has_profiling = 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         /* Selective nodes compilation. */
95
96         /* Identifier of a node group up to which all the nodes needs to be
97          * compiled in. Nodes from higher group indices will be ignores.
98          */
99         int max_nodes_group;
100
101         /* Features bitfield indicating which features from the requested group
102          * will be compiled in. Nodes which corresponds to features which are not
103          * in this bitfield will be ignored even if they're in the requested group.
104          */
105         int nodes_features;
106
107         /* BVH/sampling kernel features. */
108         bool use_hair;
109         bool use_object_motion;
110         bool use_camera_motion;
111
112         /* Denotes whether baking functionality is needed. */
113         bool use_baking;
114
115         /* Use subsurface scattering materials. */
116         bool use_subsurface;
117
118         /* Use volume materials. */
119         bool use_volume;
120
121         /* Use branched integrator. */
122         bool use_integrator_branched;
123
124         /* Use OpenSubdiv patch evaluation */
125         bool use_patch_evaluation;
126
127         /* Use Transparent shadows */
128         bool use_transparent;
129
130         /* Use various shadow tricks, such as shadow catcher. */
131         bool use_shadow_tricks;
132
133         /* Per-uber shader usage flags. */
134         bool use_principled;
135
136         /* Denoising features. */
137         bool use_denoising;
138
139         /* Use raytracing in shaders. */
140         bool use_shader_raytrace;
141
142         DeviceRequestedFeatures()
143         {
144                 /* TODO(sergey): Find more meaningful defaults. */
145                 experimental = false;
146                 max_nodes_group = 0;
147                 nodes_features = 0;
148                 use_hair = false;
149                 use_object_motion = false;
150                 use_camera_motion = false;
151                 use_baking = false;
152                 use_subsurface = false;
153                 use_volume = false;
154                 use_integrator_branched = false;
155                 use_patch_evaluation = false;
156                 use_transparent = false;
157                 use_shadow_tricks = false;
158                 use_principled = false;
159                 use_denoising = false;
160                 use_shader_raytrace = false;
161         }
162
163         bool modified(const DeviceRequestedFeatures& requested_features)
164         {
165                 return !(experimental == requested_features.experimental &&
166                          max_nodes_group == requested_features.max_nodes_group &&
167                          nodes_features == requested_features.nodes_features &&
168                          use_hair == requested_features.use_hair &&
169                          use_object_motion == requested_features.use_object_motion &&
170                          use_camera_motion == requested_features.use_camera_motion &&
171                          use_baking == requested_features.use_baking &&
172                          use_subsurface == requested_features.use_subsurface &&
173                          use_volume == requested_features.use_volume &&
174                          use_integrator_branched == requested_features.use_integrator_branched &&
175                          use_patch_evaluation == requested_features.use_patch_evaluation &&
176                          use_transparent == requested_features.use_transparent &&
177                          use_shadow_tricks == requested_features.use_shadow_tricks &&
178                          use_principled == requested_features.use_principled &&
179                          use_denoising == requested_features.use_denoising &&
180                          use_shader_raytrace == requested_features.use_shader_raytrace);
181         }
182
183         /* Convert the requested features structure to a build options,
184          * which could then be passed to compilers.
185          */
186         string get_build_options() const
187         {
188                 string build_options = "";
189                 if(experimental) {
190                         build_options += "-D__KERNEL_EXPERIMENTAL__ ";
191                 }
192                 build_options += "-D__NODES_MAX_GROUP__=" +
193                         string_printf("%d", max_nodes_group);
194                 build_options += " -D__NODES_FEATURES__=" +
195                         string_printf("%d", nodes_features);
196                 if(!use_hair) {
197                         build_options += " -D__NO_HAIR__";
198                 }
199                 if(!use_object_motion) {
200                         build_options += " -D__NO_OBJECT_MOTION__";
201                 }
202                 if(!use_camera_motion) {
203                         build_options += " -D__NO_CAMERA_MOTION__";
204                 }
205                 if(!use_baking) {
206                         build_options += " -D__NO_BAKING__";
207                 }
208                 if(!use_volume) {
209                         build_options += " -D__NO_VOLUME__";
210                 }
211                 if(!use_subsurface) {
212                         build_options += " -D__NO_SUBSURFACE__";
213                 }
214                 if(!use_integrator_branched) {
215                         build_options += " -D__NO_BRANCHED_PATH__";
216                 }
217                 if(!use_patch_evaluation) {
218                         build_options += " -D__NO_PATCH_EVAL__";
219                 }
220                 if(!use_transparent && !use_volume) {
221                         build_options += " -D__NO_TRANSPARENT__";
222                 }
223                 if(!use_shadow_tricks) {
224                         build_options += " -D__NO_SHADOW_TRICKS__";
225                 }
226                 if(!use_principled) {
227                         build_options += " -D__NO_PRINCIPLED__";
228                 }
229                 if(!use_denoising) {
230                         build_options += " -D__NO_DENOISING__";
231                 }
232                 if(!use_shader_raytrace) {
233                         build_options += " -D__NO_SHADER_RAYTRACE__";
234                 }
235                 return build_options;
236         }
237 };
238
239 std::ostream& operator <<(std::ostream &os,
240                           const DeviceRequestedFeatures& requested_features);
241
242 /* Device */
243
244 struct DeviceDrawParams {
245         function<void()> bind_display_space_shader_cb;
246         function<void()> unbind_display_space_shader_cb;
247 };
248
249 class Device {
250         friend class device_sub_ptr;
251 protected:
252         enum {
253                 FALLBACK_SHADER_STATUS_NONE = 0,
254                 FALLBACK_SHADER_STATUS_ERROR,
255                 FALLBACK_SHADER_STATUS_SUCCESS,
256         };
257
258         Device(DeviceInfo& info_, Stats &stats_, Profiler &profiler_, bool background) : background(background),
259             vertex_buffer(0),
260             fallback_status(FALLBACK_SHADER_STATUS_NONE), fallback_shader_program(0),
261             info(info_), stats(stats_), profiler(profiler_) {}
262
263         bool background;
264         string error_msg;
265
266         /* used for real time display */
267         unsigned int vertex_buffer;
268         int fallback_status, fallback_shader_program;
269         int image_texture_location, fullscreen_location;
270
271         bool bind_fallback_display_space_shader(const float width, const float height);
272
273         virtual device_ptr mem_alloc_sub_ptr(device_memory& /*mem*/, int /*offset*/, int /*size*/)
274         {
275                 /* Only required for devices that implement denoising. */
276                 assert(false);
277                 return (device_ptr) 0;
278         }
279         virtual void mem_free_sub_ptr(device_ptr /*ptr*/) {};
280
281 public:
282         virtual ~Device();
283
284         /* info */
285         DeviceInfo info;
286         virtual const string& error_message() { return error_msg; }
287         bool have_error() { return !error_message().empty(); }
288         virtual void set_error(const string& error)
289         {
290                 if(!have_error()) {
291                         error_msg = error;
292                 }
293                 fprintf(stderr, "%s\n", error.c_str());
294                 fflush(stderr);
295         }
296         virtual bool show_samples() const { return false; }
297         virtual BVHLayoutMask get_bvh_layout_mask() const = 0;
298
299         /* statistics */
300         Stats &stats;
301         Profiler &profiler;
302
303         /* memory alignment */
304         virtual int mem_sub_ptr_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, Profiler& profiler, 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__ */