forked from LuxCoreRender/LuxCore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
luxcoreconsole.cpp
264 lines (219 loc) · 9.96 KB
/
luxcoreconsole.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/***************************************************************************
* Copyright 1998-2018 by authors (see AUTHORS.txt) *
* *
* This file is part of LuxCoreRender. *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.*
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <stdexcept>
#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/format.hpp>
#include <boost/thread.hpp>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string/case_conv.hpp>
#include "luxrays/utils/oclerror.h"
#include "luxcore/luxcore.h"
using namespace std;
using namespace luxrays;
using namespace luxcore;
static string GetFileNameExt(const string &fileName) {
return boost::algorithm::to_lower_copy(boost::filesystem::path(fileName).extension().string());
}
static void BatchRendering(RenderConfig *config, RenderState *startState, Film *startFilm,
const bool showDevicesStats) {
RenderSession *session = RenderSession::Create(config, startState, startFilm);
const unsigned int haltTime = config->GetProperty("batch.halttime").Get<unsigned int>();
const unsigned int haltSpp = config->GetProperty("batch.haltspp").Get<unsigned int>(0);
// Start the rendering
session->Start();
const Properties &stats = session->GetStats();
while (!session->HasDone()) {
boost::this_thread::sleep(boost::posix_time::millisec(1000));
session->UpdateStats();
const double elapsedTime = stats.Get("stats.renderengine.time").Get<double>();
const unsigned int pass = stats.Get("stats.renderengine.pass").Get<unsigned int>();
// Convergence test is update inside UpdateFilm()
const float convergence = stats.Get("stats.renderengine.convergence").Get<float>();
// Print some information about the rendering progress
LC_LOG(boost::str(boost::format("[Elapsed time: %3d/%dsec][Samples %4d/%d][Convergence %f%%][Avg. samples/sec % 3.2fM on %.1fK tris]") %
int(elapsedTime) % int(haltTime) % pass % haltSpp % (100.f * convergence) %
(stats.Get("stats.renderengine.total.samplesec").Get<double>() / 1000000.0) %
(stats.Get("stats.dataset.trianglecount").Get<double>() / 1000.0)));
if (showDevicesStats) {
// Intersection devices
const Property &deviceNames = stats.Get("stats.renderengine.devices");
double minPerf = numeric_limits<double>::infinity();
double totalPerf = 0.0;
for (unsigned int i = 0; i < deviceNames.GetSize(); ++i) {
const string deviceName = deviceNames.Get<string>(i);
const double perf = stats.Get("stats.renderengine.devices." + deviceName + ".performance.total").Get<double>();
minPerf = Min(minPerf, perf);
totalPerf += perf;
}
for (unsigned int i = 0; i < deviceNames.GetSize(); ++i) {
const string deviceName = deviceNames.Get<string>(i);
LC_LOG(boost::str(boost::format(" %s: [Rays/sec %dK (%dK + %dK)][Prf Idx %.2f][Wrkld %.1f%%][Mem %dM/%dM]") %
deviceName %
int(stats.Get("stats.renderengine.devices." + deviceName + ".performance.total").Get<double>() / 1000.0) %
int(stats.Get("stats.renderengine.devices." + deviceName + ".performance.serial").Get<double>() / 1000.0) %
int(stats.Get("stats.renderengine.devices." + deviceName + ".performance.dataparallel").Get<double>() / 1000.0) %
(stats.Get("stats.renderengine.devices." + deviceName + ".performance.total").Get<double>() / minPerf) %
(100.0 * stats.Get("stats.renderengine.devices." + deviceName + ".performance.total").Get<double>() / totalPerf) %
int(stats.Get("stats.renderengine.devices." + deviceName + ".memory.used").Get<double>() / (1024 * 1024)) %
int(stats.Get("stats.renderengine.devices." + deviceName + ".memory.total").Get<double>() / (1024 * 1024))));
}
}
}
// Stop the rendering
session->Stop();
const string renderEngine = config->GetProperty("renderengine.type").Get<string>();
if (renderEngine != "FILESAVER") {
// Save the rendered image
session->GetFilm().SaveOutputs();
}
delete session;
}
int main(int argc, char *argv[]) {
// This is required to run AMD GPU profiler
//XInitThreads();
try {
// Initialize LuxCore
luxcore::Init();
bool removeUnused = false;
bool showDevicesStats = false;
Properties cmdLineProp;
string configFileName;
for (int i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
// I should check for out of range array index...
if (argv[i][1] == 'h') {
LC_LOG("Usage: " << argv[0] << " [options] [configuration file]" << endl <<
" -o [configuration file]" << endl <<
" -f [scene file]" << endl <<
" -w [film width]" << endl <<
" -e [film height]" << endl <<
" -D [property name] [property value]" << endl <<
" -d [current directory path]" << endl <<
" -c <remove all unused meshes, materials, textures and image maps>" << endl <<
" -s" << endl <<
" -h <display this help and exit>");
exit(EXIT_SUCCESS);
}
else if (argv[i][1] == 'o') {
if (configFileName.compare("") != 0)
throw runtime_error("Used multiple configuration files");
configFileName = string(argv[++i]);
}
else if (argv[i][1] == 'e') cmdLineProp.Set(Property("film.height")(argv[++i]));
else if (argv[i][1] == 'w') cmdLineProp.Set(Property("film.width")(argv[++i]));
else if (argv[i][1] == 'f') cmdLineProp.Set(Property("scene.file")(argv[++i]));
else if (argv[i][1] == 'D') {
cmdLineProp.Set(Property(argv[i + 1]).Add(argv[i + 2]));
i += 2;
}
else if (argv[i][1] == 'd') boost::filesystem::current_path(boost::filesystem::path(argv[++i]));
else if (argv[i][1] == 'c') removeUnused = true;
else if (argv[i][1] == 's') showDevicesStats = true;
else {
LC_LOG("Invalid option: " << argv[i]);
exit(EXIT_FAILURE);
}
} else {
const string fileName = argv[i];
const string ext = GetFileNameExt(argv[i]);
if ((ext == ".cfg") ||
(ext == ".lxs") ||
(ext == ".bcf") ||
(ext == ".rsm")) {
if (configFileName.compare("") != 0)
throw runtime_error("Used multiple configuration files");
configFileName = fileName;
} else
throw runtime_error("Unknown file extension: " + fileName);
}
}
// Load the Scene
if (configFileName.compare("") == 0)
throw runtime_error("You must specify a file to render");
// Check if we have to parse a LuxCore SDL file or a LuxRender SDL file
Scene *scene = NULL;
RenderConfig *config;
RenderState *startRenderState = NULL;
Film *startFilm = NULL;
if (configFileName.compare("") != 0) {
// Clear the file name resolver list
luxcore::ClearFileNameResolverPaths();
// Add the current directory to the list of place where to look for files
luxcore::AddFileNameResolverPath(".");
// Add the .cfg directory to the list of place where to look for files
boost::filesystem::path path(configFileName);
luxcore::AddFileNameResolverPath(path.parent_path().generic_string());
}
const string configFileNameExt = GetFileNameExt(configFileName);
if (configFileNameExt == ".lxs") {
// It is a LuxRender SDL file
LC_LOG("Parsing LuxRender SDL file...");
Properties renderConfigProps, sceneProps;
luxcore::ParseLXS(configFileName, renderConfigProps, sceneProps);
// For debugging
//LC_LOG("RenderConfig: \n" << renderConfigProps);
//LC_LOG("Scene: \n" << sceneProps);
renderConfigProps.Set(cmdLineProp);
scene = Scene::Create();
scene->Parse(sceneProps);
config = RenderConfig::Create(renderConfigProps.Set(cmdLineProp), scene);
} else if (configFileNameExt == ".cfg") {
// It is a LuxCore SDL file
config = RenderConfig::Create(Properties(configFileName).Set(cmdLineProp));
} else if (configFileNameExt == ".bcf") {
// It is a LuxCore RenderConfig binary archive
config = RenderConfig::Create(configFileName);
config->Parse(cmdLineProp);
} else if (configFileNameExt == ".rsm") {
// It is a rendering resume file
config = RenderConfig::Create(configFileName, &startRenderState, &startFilm);
config->Parse(cmdLineProp);
} else
throw runtime_error("Unknown file extension: " + configFileName);
if (removeUnused) {
// Remove unused Meshes, Image maps, materials and textures
config->GetScene().RemoveUnusedMeshes();
config->GetScene().RemoveUnusedImageMaps();
config->GetScene().RemoveUnusedMaterials();
config->GetScene().RemoveUnusedTextures();
}
const bool fileSaverRenderEngine = (config->GetProperty("renderengine.type").Get<string>() == "FILESAVER");
if (!fileSaverRenderEngine) {
// Force the film update at 2.5secs (mostly used by PathOCL)
config->Parse(Properties().Set(Property("screen.refresh.interval")(2500)));
}
BatchRendering(config, startRenderState, startFilm, showDevicesStats);
delete config;
delete scene;
LC_LOG("Done.");
} catch (runtime_error &err) {
LC_LOG("RUNTIME ERROR: " << err.what());
return EXIT_FAILURE;
} catch (exception &err) {
LC_LOG("ERROR: " << err.what());
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}