Cycles: Make shadow catcher an optional feature for OpenCL
[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_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         /* Use various shadow tricks, such as shadow catcher. */
125         bool use_shadow_tricks;
126
127         DeviceRequestedFeatures()
128         {
129                 /* TODO(sergey): Find more meaningful defaults. */
130                 experimental = false;
131                 max_closure = 0;
132                 max_nodes_group = 0;
133                 nodes_features = 0;
134                 use_hair = false;
135                 use_object_motion = false;
136                 use_camera_motion = false;
137                 use_baking = false;
138                 use_subsurface = false;
139                 use_volume = false;
140                 use_integrator_branched = false;
141                 use_patch_evaluation = false;
142                 use_transparent = false;
143                 use_shadow_tricks = false;
144         }
145
146         bool modified(const DeviceRequestedFeatures& requested_features)
147         {
148                 return !(experimental == requested_features.experimental &&
149                          max_closure == requested_features.max_closure &&
150                          max_nodes_group == requested_features.max_nodes_group &&
151                          nodes_features == requested_features.nodes_features &&
152                          use_hair == requested_features.use_hair &&
153                          use_object_motion == requested_features.use_object_motion &&
154                          use_camera_motion == requested_features.use_camera_motion &&
155                          use_baking == requested_features.use_baking &&
156                          use_subsurface == requested_features.use_subsurface &&
157                          use_volume == requested_features.use_volume &&
158                          use_integrator_branched == requested_features.use_integrator_branched &&
159                          use_patch_evaluation == requested_features.use_patch_evaluation &&
160                          use_transparent == requested_features.use_transparent &&
161                          use_shadow_tricks == requested_features.use_shadow_tricks);
162         }
163
164         /* Convert the requested features structure to a build options,
165          * which could then be passed to compilers.
166          */
167         string get_build_options(void) const
168         {
169                 string build_options = "";
170                 if(experimental) {
171                         build_options += "-D__KERNEL_EXPERIMENTAL__ ";
172                 }
173                 build_options += "-D__NODES_MAX_GROUP__=" +
174                         string_printf("%d", max_nodes_group);
175                 build_options += " -D__NODES_FEATURES__=" +
176                         string_printf("%d", nodes_features);
177                 build_options += string_printf(" -D__MAX_CLOSURE__=%d", max_closure);
178                 if(!use_hair) {
179                         build_options += " -D__NO_HAIR__";
180                 }
181                 if(!use_object_motion) {
182                         build_options += " -D__NO_OBJECT_MOTION__";
183                 }
184                 if(!use_camera_motion) {
185                         build_options += " -D__NO_CAMERA_MOTION__";
186                 }
187                 if(!use_baking) {
188                         build_options += " -D__NO_BAKING__";
189                 }
190                 if(!use_volume) {
191                         build_options += " -D__NO_VOLUME__";
192                 }
193                 if(!use_subsurface) {
194                         build_options += " -D__NO_SUBSURFACE__";
195                 }
196                 if(!use_integrator_branched) {
197                         build_options += " -D__NO_BRANCHED_PATH__";
198                 }
199                 if(!use_patch_evaluation) {
200                         build_options += " -D__NO_PATCH_EVAL__";
201                 }
202                 if(!use_transparent && !use_volume) {
203                         build_options += " -D__NO_TRANSPARENT__";
204                 }
205                 if(!use_shadow_tricks) {
206                         build_options += " -D__NO_SHADOW_TRICKS__";
207                 }
208                 return build_options;
209         }
210 };
211
212 std::ostream& operator <<(std::ostream &os,
213                           const DeviceRequestedFeatures& requested_features);
214
215 /* Device */
216
217 struct DeviceDrawParams {
218         function<void(void)> bind_display_space_shader_cb;
219         function<void(void)> unbind_display_space_shader_cb;
220 };
221
222 class Device {
223 protected:
224         Device(DeviceInfo& info_, Stats &stats_, bool background) : background(background), vertex_buffer(0), info(info_), stats(stats_) {}
225
226         bool background;
227         string error_msg;
228
229         /* used for real time display */
230         unsigned int vertex_buffer;
231
232 public:
233         virtual ~Device();
234
235         /* info */
236         DeviceInfo info;
237         virtual const string& error_message() { return error_msg; }
238         bool have_error() { return !error_message().empty(); }
239         virtual void set_error(const string& error)
240         {
241                 if(!have_error()) {
242                         error_msg = error;
243                 }
244                 fprintf(stderr, "%s\n", error.c_str());
245                 fflush(stderr);
246         }
247         virtual bool show_samples() const { return false; }
248
249         /* statistics */
250         Stats &stats;
251
252         /* regular memory */
253         virtual void mem_alloc(const char *name, device_memory& mem, MemoryType type) = 0;
254         virtual void mem_copy_to(device_memory& mem) = 0;
255         virtual void mem_copy_from(device_memory& mem,
256                 int y, int w, int h, int elem) = 0;
257         virtual void mem_zero(device_memory& mem) = 0;
258         virtual void mem_free(device_memory& mem) = 0;
259
260         /* constant memory */
261         virtual void const_copy_to(const char *name, void *host, size_t size) = 0;
262
263         /* texture memory */
264         virtual void tex_alloc(const char * /*name*/,
265                                device_memory& /*mem*/,
266                                InterpolationType interpolation = INTERPOLATION_NONE,
267                                ExtensionType extension = EXTENSION_REPEAT)
268         {
269                 (void)interpolation;  /* Ignored. */
270                 (void)extension;  /* Ignored. */
271         };
272
273         virtual void tex_free(device_memory& /*mem*/) {};
274
275         /* pixel memory */
276         virtual void pixels_alloc(device_memory& mem);
277         virtual void pixels_copy_from(device_memory& mem, int y, int w, int h);
278         virtual void pixels_free(device_memory& mem);
279
280         /* open shading language, only for CPU device */
281         virtual void *osl_memory() { return NULL; }
282
283         /* load/compile kernels, must be called before adding tasks */ 
284         virtual bool load_kernels(
285                 const DeviceRequestedFeatures& /*requested_features*/)
286         { return true; }
287
288         /* tasks */
289         virtual int get_split_task_count(DeviceTask& task) = 0;
290         virtual void task_add(DeviceTask& task) = 0;
291         virtual void task_wait() = 0;
292         virtual void task_cancel() = 0;
293         
294         /* opengl drawing */
295         virtual void draw_pixels(device_memory& mem, int y, int w, int h,
296                 int dx, int dy, int width, int height, bool transparent,
297                 const DeviceDrawParams &draw_params);
298
299 #ifdef WITH_NETWORK
300         /* networking */
301         void server_run();
302 #endif
303
304         /* multi device */
305         virtual void map_tile(Device * /*sub_device*/, RenderTile& /*tile*/) {}
306         virtual int device_number(Device * /*sub_device*/) { return 0; }
307
308         /* static */
309         static Device *create(DeviceInfo& info, Stats &stats, bool background = true);
310
311         static DeviceType type_from_string(const char *name);
312         static string string_from_type(DeviceType type);
313         static vector<DeviceType>& available_types();
314         static vector<DeviceInfo>& available_devices();
315         static string device_capabilities();
316         static DeviceInfo get_multi_device(vector<DeviceInfo> subdevices);
317
318         /* Tag devices lists for update. */
319         static void tag_update();
320
321         static void free_memory();
322 private:
323         /* Indicted whether device types and devices lists were initialized. */
324         static bool need_types_update, need_devices_update;
325         static vector<DeviceType> types;
326         static vector<DeviceInfo> devices;
327 };
328
329 CCL_NAMESPACE_END
330
331 #endif /* __DEVICE_H__ */
332