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