Blender V5.3
cycles_standalone.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include <cstdio>
6
7#include "device/device.h"
8#include "scene/camera.h"
9#include "scene/integrator.h"
10#include "scene/scene.h"
11#include "session/buffers.h"
12#include "session/session.h"
13
14#include "util/args.h"
15#include "util/log.h"
16#include "util/path.h"
17#include "util/progress.h"
18#include "util/string.h"
19#include "util/system.h"
20#ifdef WITH_CYCLES_STANDALONE_GUI
21# include "util/time.h"
22# include "util/transform.h"
23#endif
24#include "util/unique_ptr.h"
25#include "util/version.h"
26
27#ifdef WITH_USD
28# include "hydra/file_reader.h"
29#endif
30
31#include "app/cycles_xml.h"
33
34#ifdef WITH_CYCLES_STANDALONE_GUI
36# include "opengl/window.h"
37#endif
38
40
53
54static void session_print(const string &str)
55{
56 /* print with carriage return to overwrite previous */
57 printf("\r%s", str.c_str());
58
59 /* add spaces to overwrite longer previous print */
60 static int maxlen = 0;
61 const int len = str.size();
62 maxlen = max(len, maxlen);
63
64 for (int i = len; i < maxlen; i++) {
65 printf(" ");
66 }
67
68 /* flush because we don't write an end of line */
69 fflush(stdout);
70}
71
73{
74 string status;
75 string substatus;
76
77 /* get status */
78 const double progress = options.session->progress.get_progress();
79 options.session->progress.get_status(status, substatus);
80
81 if (!substatus.empty()) {
82 status += ": " + substatus;
83 }
84
85 /* print status */
86 status = string_printf("Progress %05.2f %s", progress * 100, status.c_str());
87 session_print(status);
88}
89
91{
92 static BufferParams buffer_params;
93 buffer_params.width = options.width;
94 buffer_params.height = options.height;
95 buffer_params.full_width = options.width;
96 buffer_params.full_height = options.height;
97
98 return buffer_params;
99}
100
101static void scene_init()
102{
103 options.scene = options.session->scene.get();
104
105 /* Read XML or USD */
106#ifdef WITH_USD
108 HD_CYCLES_NS::HdCyclesFileReader::read(options.session.get(), options.filepath.c_str());
109 }
110 else
111#endif
112 {
114 }
115
116 /* Camera width/height override? */
117 if (!(options.width == 0 || options.height == 0)) {
118 options.scene->camera->set_full_width(options.width);
119 options.scene->camera->set_full_height(options.height);
120 }
121 else {
122 options.width = options.scene->camera->get_full_width();
123 options.height = options.scene->camera->get_full_height();
124 }
125
126 /* Calculate Viewplane */
128}
129
130static void session_init()
131{
132 options.output_pass = "combined";
133 options.session = make_unique<Session>(options.session_params, options.scene_params);
134
135#ifdef WITH_CYCLES_STANDALONE_GUI
137 options.session->set_display_driver(make_unique<OpenGLDisplayDriver>(
139 }
140#endif
141
142 if (!options.output_filepath.empty()) {
143 options.session->set_output_driver(make_unique<OIIOOutputDriver>(
145 }
146
148 options.session->progress.set_update_callback([] { session_print_status(); });
149 }
150#ifdef WITH_CYCLES_STANDALONE_GUI
151 else {
152 options.session->progress.set_update_callback([] { window_redraw(); });
153 }
154#endif
155
156 /* load scene */
157 scene_init();
158
159 /* add pass for output. */
160 Pass *pass = options.scene->create_node<Pass>();
161 pass->set_name(ustring(options.output_pass.c_str()));
162 pass->set_type(PASS_COMBINED);
163
165 options.session->start();
166}
167
168static void session_exit()
169{
170 if (options.session) {
171 options.session.reset();
172 }
173
175 session_print("Finished Rendering.");
176 printf("\n");
177 }
178}
179
180#ifdef WITH_CYCLES_STANDALONE_GUI
181static void display_info(Progress &progress)
182{
183 static double latency = 0.0;
184 static double last = 0;
185 const double elapsed = time_dt();
186 string str;
187 string interactive;
188
189 latency = (elapsed - last);
190 last = elapsed;
191
192 double total_time;
193 double sample_time;
194 string status;
195 string substatus;
196
197 progress.get_time(total_time, sample_time);
198 progress.get_status(status, substatus);
199 const double progress_val = progress.get_progress();
200
201 if (!substatus.empty()) {
202 status += ": " + substatus;
203 }
204
205 interactive = options.interactive ? "On" : "Off";
206
208 "%s"
209 " Time: %.2f"
210 " Latency: %.4f"
211 " Progress: %05.2f"
212 " Average: %.4f"
213 " Interactive: %s",
214 status.c_str(),
215 total_time,
216 latency,
217 progress_val * 100,
218 sample_time,
219 interactive.c_str());
220
221 window_display_info(str.c_str());
222
223 if (options.show_help) {
225 }
226}
227
228static void display()
229{
230 options.session->draw();
231
232 display_info(options.session->progress);
233}
234
235static void motion(const int x, const int y, int button)
236{
237 if (options.interactive) {
238 Transform matrix = options.session->scene->camera->get_matrix();
239
240 /* Translate */
241 if (button == 0) {
242 const float3 translate = make_float3(x * 0.01f, -(y * 0.01f), 0.0f);
243 matrix = matrix * transform_translate(translate);
244 }
245
246 /* Rotate */
247 else if (button == 2) {
248 const float4 r1 = make_float4((float)x * 0.1f, 0.0f, 1.0f, 0.0f);
249 matrix = matrix * transform_rotate(DEG2RADF(r1.x), make_float3(r1.y, r1.z, r1.w));
250
251 const float4 r2 = make_float4(y * 0.1f, 1.0f, 0.0f, 0.0f);
252 matrix = matrix * transform_rotate(DEG2RADF(r2.x), make_float3(r2.y, r2.z, r2.w));
253 }
254
255 /* Update and Reset */
256 options.session->scene->camera->set_matrix(matrix);
257 options.session->scene->camera->need_flags_update = true;
258 options.session->scene->camera->need_device_update = true;
259
261 }
262}
263
264static void resize(const int width, const int height)
265{
266 options.width = width;
267 options.height = height;
268
269 if (options.session) {
270 /* Update camera */
271 options.session->scene->camera->set_full_width(options.width);
272 options.session->scene->camera->set_full_height(options.height);
273 options.session->scene->camera->compute_auto_viewplane();
274 options.session->scene->camera->need_flags_update = true;
275 options.session->scene->camera->need_device_update = true;
276
278 }
279}
280
281static void keyboard(unsigned char key)
282{
283 /* Toggle help */
284 if (key == 'h') {
286
287 /* Reset */
288 }
289 else if (key == 'r') {
291
292 /* Cancel */
293 }
294 else if (key == 27) { // escape
295 options.session->progress.set_cancel("Canceled");
296
297 /* Pause */
298 }
299 else if (key == 'p') {
301 options.session->set_pause(options.pause);
302 }
303
304 /* Interactive Mode */
305 else if (key == 'i') {
307
308 /* Navigation */
309 }
310 else if (options.interactive && (key == 'w' || key == 'a' || key == 's' || key == 'd')) {
311 Transform matrix = options.session->scene->camera->get_matrix();
313
314 if (key == 'w') {
315 translate = make_float3(0.0f, 0.0f, 0.1f);
316 }
317 else if (key == 's') {
318 translate = make_float3(0.0f, 0.0f, -0.1f);
319 }
320 else if (key == 'a') {
321 translate = make_float3(-0.1f, 0.0f, 0.0f);
322 }
323 else if (key == 'd') {
324 translate = make_float3(0.1f, 0.0f, 0.0f);
325 }
326
327 matrix = matrix * transform_translate(translate);
328
329 /* Update and Reset */
330 options.session->scene->camera->set_matrix(matrix);
331 options.session->scene->camera->need_flags_update = true;
332 options.session->scene->camera->need_device_update = true;
333
335 }
336
337 /* Set Max Bounces */
338 else if (options.interactive && (key == '0' || key == '1' || key == '2' || key == '3')) {
339 int bounce;
340 switch (key) {
341 case '0':
342 bounce = 0;
343 break;
344 case '1':
345 bounce = 1;
346 break;
347 case '2':
348 bounce = 2;
349 break;
350 case '3':
351 bounce = 3;
352 break;
353 default:
354 bounce = 0;
355 break;
356 }
357
358 options.session->scene->integrator->set_max_bounce(bounce);
359
361 }
362}
363#endif
364
365static void parse_int(OIIO::cspan<const char *> argv, int *i)
366{
367 assert(argv.size() == 2);
368 *i = atoi(argv[1]);
369}
370
371static void parse_string(OIIO::cspan<const char *> argv, std::string *s)
372{
373 assert(argv.size() == 2);
374 *s = argv[1];
375}
376
377static void options_parse(const int argc, const char **argv)
378{
379 options.width = 1024;
380 options.height = 512;
381 options.filepath = "";
382 options.session = nullptr;
383 options.quiet = false;
386
387 /* device names */
388 string device_names;
389 string devicename = "CPU";
390 bool list = false;
391
392 /* List devices for which support is compiled in. */
394 for (const DeviceType type : types) {
395 if (!device_names.empty()) {
396 device_names += ", ";
397 }
398
399 device_names += Device::string_from_type(type);
400 }
401
402 /* shading system */
403 string ssname = "svm";
404
405 /* parse options */
406 ArgParse ap;
407 bool help = false;
408 bool profile = false;
409 bool version = false;
410 string log_level;
411
412 ap.usage("cycles [options] file.xml");
413 ap.arg("filename").hidden().action([&](auto argv) { options.filepath = argv[0]; });
414 ap.arg("--device %s:DEVICE").help("Devices to use: " + device_names).action([&](auto argv) {
415 parse_string(argv, &devicename);
416 });
417#ifdef WITH_OSL
418 ap.arg("--shadingsys %s:SHADINGSYSTEM")
419 .help("Shading system to use: svm, osl")
420 .action([&](auto argv) { parse_string(argv, &ssname); });
421#endif
422 ap.arg("--background", &options.session_params.background)
423 .help("Render in background, without user interface");
424 ap.arg("--quiet", &options.quiet).help("In background mode, don't print progress messages");
425 ap.arg("--samples %d:SAMPLES").help("Number of samples to render").action([&](auto argv) {
427 });
428 ap.arg("--output %s:OUTPUT").help("File path to write output image").action([&](auto argv) {
430 });
431 ap.arg("--threads %d:THREADS").help("CPU Rendering Threads").action([&](auto argv) {
433 });
434 ap.arg("--width %d:WIDTH").help("Image width in pixelx").action([&](auto argv) {
435 parse_int(argv, &options.width);
436 });
437 ap.arg("--height %d:HEIGHT").help("Image height in pixel").action([&](auto argv) {
438 parse_int(argv, &options.height);
439 });
440 ap.arg("--tile-size %d:TILE_SIZE").help("Tile size in pixels").action([&](auto argv) {
442 });
443 ap.arg("--list-devices", &list).help("List information about all available devices");
444 ap.arg("--profile", &profile).help("Enable profile logging");
445 ap.arg("--log-level %s:LEVEL")
446 .help("Log verbosity: fatal, error, warning, info, stats, debug")
447 .action([&](auto argv) { parse_string(argv, &log_level); });
448 ap.arg("--help", &help).help("Print help message");
449 ap.arg("--version", &version).help("Print version number");
450
451 if (ap.parse_args(argc, argv) < 0) {
452 fprintf(stderr, "%s\n", ap.geterror().c_str());
453 ap.print_help();
454 exit(EXIT_FAILURE);
455 }
456
457 if (!log_level.empty()) {
458 log_level_set(log_level);
459 }
460
461 if (list) {
463 printf("Devices:\n");
464
465 for (const DeviceInfo &info : devices) {
466 printf(" %-10s%s%s%s\n",
467 Device::string_from_type(info.type).c_str(),
468 info.description.c_str(),
469 (info.display_device) ? " (display)" : "",
470 (info.meets_driver_requirement) ? "" : " (driver outdated)");
471 }
472
473 exit(EXIT_SUCCESS);
474 }
475 else if (version) {
477 exit(EXIT_SUCCESS);
478 }
479 else if (help || options.filepath.empty()) {
480 ap.print_help();
481 exit(EXIT_SUCCESS);
482 }
483
485
486 if (ssname == "osl") {
488 }
489 else if (ssname == "svm") {
491 }
492
493#ifndef WITH_CYCLES_STANDALONE_GUI
495#endif
496
499 }
500
501 /* find matching device */
502 const DeviceType device_type = Device::type_from_string(devicename.c_str());
503
504 bool device_available = false;
505 bool device_driver_outdated = false;
506 for (const DeviceInfo &info : Device::available_devices(DEVICE_MASK(device_type))) {
507 if (info.meets_driver_requirement) {
509 device_available = true;
510 break;
511 }
512 device_driver_outdated = true;
513 }
514
515 /* handle invalid configurations */
516 if (options.session_params.device.type == DEVICE_NONE || !device_available) {
517 if (device_driver_outdated) {
518 fprintf(stderr, "Device driver outdated: %s\n", devicename.c_str());
519 }
520 else {
521 fprintf(stderr, "Unknown device: %s\n", devicename.c_str());
522 }
523 exit(EXIT_FAILURE);
524 }
525#ifdef WITH_OSL
526 else if (!(ssname == "osl" || ssname == "svm")) {
527 fprintf(stderr, "Unknown shading system: %s\n", ssname.c_str());
528 exit(EXIT_FAILURE);
529 }
532 {
533 fprintf(stderr, "OSL shading system only works with CPU device\n");
534 exit(EXIT_FAILURE);
535 }
536#endif
537 else if (options.session_params.samples < 0) {
538 fprintf(stderr, "Invalid number of samples: %d\n", options.session_params.samples);
539 exit(EXIT_FAILURE);
540 }
541 else if (options.filepath.empty()) {
542 fprintf(stderr, "No file path specified\n");
543 exit(EXIT_FAILURE);
544 }
545}
546
548
549using namespace ccl;
550
551int main(const int argc, const char **argv)
552{
553 log_init(nullptr);
554 path_init();
556 options_parse(argc, argv);
557
558#ifdef WITH_CYCLES_STANDALONE_GUI
560#endif
561 session_init();
562 options.session->wait();
563 session_exit();
564#ifdef WITH_CYCLES_STANDALONE_GUI
565 }
566 else {
567 const string title = "Cycles: " + path_filename(options.filepath);
568
569 /* init/exit are callback so they run while GL is initialized */
570 window_main_loop(title.c_str(),
575 resize,
576 display,
577 keyboard,
578 motion);
579 }
580#endif
581
582 return 0;
583}
#define DEG2RADF(_deg)
btScalar max
int full_width
Definition buffers.h:85
int full_height
Definition buffers.h:86
NODE_DECLARE int width
Definition buffers.h:70
void compute_auto_viewplane()
DeviceType type
static DeviceType type_from_string(const char *name)
static vector< DeviceType > available_types()
static string string_from_type(DeviceType type)
static vector< DeviceInfo > available_devices(const uint device_type_mask=DEVICE_MASK_ALL)
Definition pass.h:50
void get_status(string &status_, string &substatus_) const
Definition progress.h:290
void get_time(double &total_time_, double &render_time_) const
Definition progress.h:153
double get_progress() const
Definition progress.h:185
ShadingSystem shadingsystem
Definition scene.h:65
Definition scene.h:133
T * create_node(Args &&...)=delete
Camera * camera
Definition scene.h:145
DeviceInfo device
#define printf
static void session_init()
static void options_parse(const int argc, const char **argv)
static void scene_init()
static BufferParams & session_buffer_params()
static void session_print_status()
static void session_exit()
static void parse_string(OIIO::cspan< const char * > argv, std::string *s)
CCL_NAMESPACE_BEGIN struct Options options
static void session_print(const string &str)
static void parse_int(OIIO::cspan< const char * > argv, int *i)
void xml_read_file(Scene *scene, const char *filepath)
#define CCL_NAMESPACE_END
#define DEVICE_MASK(type)
DeviceType
@ DEVICE_NONE
@ DEVICE_CPU
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define str(s)
#define assert(assertion)
#define main()
float4x4 translate(float4x4 mat, float3 translation)
@ PASS_COMBINED
void log_init(const LogFunction func)
Definition log.cpp:72
void log_level_set(const LogLevel level)
Definition log.cpp:78
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
string path_filename(const string &path)
Definition path.cpp:399
void path_init(const string &path, const string &user_path)
Definition path.cpp:355
#define make_float4
@ SHADINGSYSTEM_OSL
@ SHADINGSYSTEM_SVM
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition string.cpp:25
bool string_endswith(const string_view s, const string_view end)
Definition string.cpp:122
string string_to_lower(const string &s)
Definition string.cpp:217
SceneParams scene_params
SessionParams session_params
unique_ptr< Session > session
string output_filepath
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225
void system_max_open_files_ensure()
Definition system.cpp:287
CCL_NAMESPACE_BEGIN double time_dt()
Definition time.cpp:31
ccl_device_inline Transform transform_rotate(const float angle, float3 axis)
Definition transform.h:323
ccl_device_inline Transform transform_translate(const float3 t)
Definition transform.h:296
#define CYCLES_VERSION_STRING
Definition version.h:19
void window_opengl_context_disable()
Definition window.cpp:258
bool window_opengl_context_enable()
Definition window.cpp:251
void window_display_info(const char *info)
Definition window.cpp:76
void window_main_loop(const char *title, const int width, const int height, WindowInitFunc initf, WindowExitFunc exitf, WindowResizeFunc resize, WindowDisplayFunc display, WindowKeyboardFunc keyboard, WindowMotionFunc motion)
Definition window.cpp:264
void window_redraw()
Definition window.cpp:345
void window_display_help()
Definition window.cpp:97