forked from KhronosGroup/ANARI-SDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
288 lines (221 loc) · 7.99 KB
/
main.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Copyright 2021-2024 The Khronos Group
// SPDX-License-Identifier: Apache-2.0
// anari_test_scenes
#include "anari_test_scenes.h"
// stb_image
#include "stb_image_write.h"
// std
#include <string>
#include <vector>
// Globals ////////////////////////////////////////////////////////////////////
static const std::vector<std::string> g_categories = []() {
auto v = anari::scenes::getAvailableSceneCategories();
auto fileBased = [](const std::string &c) { return c == "file"; };
v.erase(std::remove_if(v.begin(), v.end(), fileBased), v.end());
std::sort(v.begin(), v.end());
return v;
}();
static const std::vector<std::vector<std::string>> g_scenes = []() {
std::vector<std::vector<std::string>> v;
for (auto &c : g_categories) {
auto names = anari::scenes::getAvailableSceneNames(c.c_str());
std::sort(names.begin(), names.end());
v.push_back(names);
}
return v;
}();
std::string g_category;
std::string g_scene;
anari::math::uint2 g_frameSize(1024, 768);
int g_numPixelSamples = 1;
std::string g_libraryType = "environment";
std::string g_deviceType = "default";
std::string g_rendererType = "default";
bool g_enableDebug = false;
bool g_verbose = false;
anari::Library g_library = nullptr;
anari::Device g_device = nullptr;
anari::Library g_debugLibrary = nullptr;
// Helper functions ///////////////////////////////////////////////////////////
static void statusFunc(const void *userData,
anari::Device device,
anari::Object source,
anari::DataType sourceType,
anari::StatusSeverity severity,
anari::StatusCode code,
const char *message)
{
const bool verbose = userData ? *(const bool *)userData : false;
if (severity == ANARI_SEVERITY_FATAL_ERROR) {
fprintf(stderr, "[FATAL][%p] %s\n", source, message);
} else if (severity == ANARI_SEVERITY_ERROR) {
fprintf(stderr, "[ERROR][%p] %s\n", source, message);
} else if (verbose && severity == ANARI_SEVERITY_WARNING) {
fprintf(stderr, "[WARN ][%p] %s\n", source, message);
} else if (verbose && severity == ANARI_SEVERITY_PERFORMANCE_WARNING) {
fprintf(stderr, "[PERF ][%p] %s\n", source, message);
} else if (verbose && severity == ANARI_SEVERITY_INFO) {
fprintf(stderr, "[INFO ][%p] %s\n", source, message);
} else if (verbose && severity == ANARI_SEVERITY_DEBUG) {
fprintf(stderr, "[DEBUG][%p] %s\n", source, message);
}
}
static void initializeANARI()
{
g_library = anari::loadLibrary(g_libraryType.c_str(), statusFunc, &g_verbose);
if (g_enableDebug)
g_debugLibrary = anari::loadLibrary("debug", statusFunc, &g_verbose);
if (!g_library) {
printf("############ WARNING ############\n");
printf("'%s' library failed to load, falling back to 'helide'\n",
g_libraryType.c_str());
printf("#################################\n");
g_library = anari::loadLibrary("helide", statusFunc, &g_verbose);
}
if (!g_library)
throw std::runtime_error("Failed to load ANARI library");
ANARIDevice d = anari::newDevice(g_library, g_deviceType.c_str());
if (!d)
std::exit(1);
anari::commitParameters(d, d);
int version = -1;
anari::getProperty(d, d, "version", version);
printf("\ndevice version: %i\n\n", version);
g_device = d;
}
static void renderScene(
ANARIDevice d, const std::string &category, const std::string &scene)
{
auto s = anari::scenes::createScene(d, category.c_str(), scene.c_str());
anari::scenes::commit(s);
auto camera = anari::newObject<anari::Camera>(d, "perspective");
anari::setParameter(
d, camera, "aspect", (float)g_frameSize.x / (float)g_frameSize.y);
auto renderer = anari::newObject<anari::Renderer>(d, g_rendererType.c_str());
anari::setParameter(d, renderer, "pixelSamples", g_numPixelSamples);
anari::setParameter(
d, renderer, "background", anari::math::float4(anari::math::float3(0.1f), 1));
anari::commitParameters(d, renderer);
auto frame = anari::newObject<anari::Frame>(d);
anari::setParameter(d, frame, "size", g_frameSize);
anari::setParameter(d, frame, "channel.color", ANARI_UFIXED8_RGBA_SRGB);
anari::setParameter(d, frame, "renderer", renderer);
anari::setParameter(d, frame, "camera", camera);
anari::setParameter(d, frame, "world", anari::scenes::getWorld(s));
anari::commitParameters(d, frame);
int imgNum = 0;
auto cameras = anari::scenes::getCameras(s);
for (auto &cam : cameras) {
anari::setParameter(d, camera, "position", cam.position);
anari::setParameter(d, camera, "direction", cam.direction);
anari::setParameter(d, camera, "up", cam.up);
anari::commitParameters(d, camera);
std::string fileName =
category + '_' + scene + '_' + std::to_string(imgNum++) + ".png";
printf("rendering '%s/%s'...", category.c_str(), fileName.c_str());
anari::render(d, frame);
anari::wait(d, frame);
printf("done!\n");
auto fb = anari::map<uint32_t>(d, frame, "channel.color");
stbi_write_png(fileName.c_str(),
int(fb.width),
int(fb.height),
4,
fb.data,
4 * int(fb.width));
anari::unmap(d, frame, "channel.color");
}
anari::release(d, camera);
anari::release(d, frame);
anari::release(d, renderer);
anari::scenes::release(s);
}
void printHelp()
{
printf("%s",
R"help(
usage: anariRenderTests [options]
options:
--help | -h
Print this help text
--library [name] | -l [name]
Which library to load, which will use the "default" device
default --> "environment"
--verbose | -v
Enable verbose output from the device, otherwise just errors
--num_samples [N]
Number of samples to be take for each pixel per-frame,
relevant to ray tracers
default --> 1
--renderer [name]
Which renderer to use for each frame
default --> "default"
--scene [category] [name] | -s [category] [name]
Which scene to render
default --> all scenes in all categories will be rendered
--image_size [width] [height]
Set the size of the images to be rendered
default --> 1024 768
--debug | -d
Enable the debug layer
)help");
}
void parseCommandLine(int argc, const char *argv[])
{
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "--help" || arg == "-h") {
printHelp();
std::exit(0);
} else if (arg == "--library" || arg == "-l") {
g_libraryType = argv[++i];
} else if (arg == "--num_samples") {
g_numPixelSamples = std::atoi(argv[++i]);
} else if (arg == "--renderer") {
g_rendererType = argv[++i];
} else if (arg == "--scene" || arg == "-s") {
g_category = argv[++i];
g_scene = argv[++i];
} else if (arg == "--image_size") {
g_frameSize.x = (unsigned)std::strtoul(argv[++i], nullptr, 10);
g_frameSize.y = (unsigned)std::strtoul(argv[++i], nullptr, 10);
} else if (arg == "--debug" || arg == "-d") {
g_enableDebug = true;
} else if (arg == "--verbose" || arg == "-v") {
g_verbose = true;
}
}
}
void printConfig()
{
printf("CONFIGURATION:\n");
printf(" image size = {%i, %i}\n", g_frameSize.x, g_frameSize.y);
printf(" device = %s\n", g_deviceType.c_str());
printf(" renderer = %s\n", g_rendererType.c_str());
printf(" pixel samples = %i\n", g_numPixelSamples);
printf("\n");
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
int main(int argc, const char *argv[])
{
stbi_flip_vertically_on_write(1);
parseCommandLine(argc, argv);
printConfig();
initializeANARI();
if (g_scene.empty()) {
int i = 0;
for (auto &category : g_categories) {
for (auto &scene : g_scenes[i++])
renderScene(g_device, category, scene);
}
} else {
renderScene(g_device, g_category, g_scene);
}
anari::release(g_device, g_device);
if (g_enableDebug)
anari::unloadLibrary(g_debugLibrary);
anari::unloadLibrary(g_library);
return 0;
}