Merge branch 'blender2.7'
[blender.git] / intern / cycles / app / cycles_server.cpp
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 #include <stdio.h>
18
19 #include "device/device.h"
20
21 #include "util/util_args.h"
22 #include "util/util_foreach.h"
23 #include "util/util_path.h"
24 #include "util/util_stats.h"
25 #include "util/util_string.h"
26 #include "util/util_task.h"
27 #include "util/util_logging.h"
28
29 using namespace ccl;
30
31 int main(int argc, const char **argv)
32 {
33         util_logging_init(argv[0]);
34         path_init();
35
36         /* device types */
37         string devicelist = "";
38         string devicename = "cpu";
39         bool list = false, debug = false;
40         int threads = 0, verbosity = 1;
41
42         vector<DeviceType>& types = Device::available_types();
43
44         foreach(DeviceType type, types) {
45                 if(devicelist != "")
46                         devicelist += ", ";
47
48                 devicelist += Device::string_from_type(type);
49         }
50
51         /* parse options */
52         ArgParse ap;
53
54         ap.options ("Usage: cycles_server [options]",
55                 "--device %s", &devicename, ("Devices to use: " + devicelist).c_str(),
56                 "--list-devices", &list, "List information about all available devices",
57                 "--threads %d", &threads, "Number of threads to use for CPU device",
58 #ifdef WITH_CYCLES_LOGGING
59                 "--debug", &debug, "Enable debug logging",
60                 "--verbose %d", &verbosity, "Set verbosity of the logger",
61 #endif
62                 NULL);
63
64         if(ap.parse(argc, argv) < 0) {
65                 fprintf(stderr, "%s\n", ap.geterror().c_str());
66                 ap.usage();
67                 exit(EXIT_FAILURE);
68         }
69
70         if(debug) {
71                 util_logging_start();
72                 util_logging_verbosity_set(verbosity);
73         }
74
75         if(list) {
76                 vector<DeviceInfo>& devices = Device::available_devices();
77
78                 printf("Devices:\n");
79
80                 foreach(DeviceInfo& info, devices) {
81                         printf("    %s%s\n",
82                                 info.description.c_str(),
83                                 (info.display_device)? " (display)": "");
84                 }
85
86                 exit(EXIT_SUCCESS);
87         }
88
89         /* find matching device */
90         DeviceType device_type = Device::type_from_string(devicename.c_str());
91         vector<DeviceInfo>& devices = Device::available_devices();
92         DeviceInfo device_info;
93
94         foreach(DeviceInfo& device, devices) {
95                 if(device_type == device.type) {
96                         device_info = device;
97                         break;
98                 }
99         }
100
101         TaskScheduler::init(threads);
102
103         while(1) {
104                 Stats stats;
105                 Device *device = Device::create(device_info, stats, true);
106                 printf("Cycles Server with device: %s\n", device->info.description.c_str());
107                 device->server_run();
108                 delete device;
109         }
110
111         TaskScheduler::exit();
112
113         return 0;
114 }