forked from KhronosGroup/ANARI-SDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anariInfo.cpp
389 lines (346 loc) · 11.2 KB
/
anariInfo.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// Copyright 2022-2024 The Khronos Group
// SPDX-License-Identifier: Apache-2.0
//
// anariInfo.cpp
//
// anariInfo opens a library and displays queryable information without
// creating any devices.
#ifdef _WIN32
#include <malloc.h>
#else
#include <alloca.h>
#endif
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <type_traits>
// anari
#include "anari/anari_cpp.hpp"
const char *helptext =
"anariInfo -l <library> [-p -t <type> -s <subtype>]\n"
" -p: skip parameter listing\n"
" -t <type>: only show parameters for objects of a type.\n"
" example: -t ANARI_RENDERER\n"
" -s <subtype>: only show parameters for objects of a subtype.\n"
" example: -s triangle\n";
/******************************************************************/
/* errorFunc(): the callback to use when an error is encountered */
void statusFunc(const void *userData,
ANARIDevice device,
ANARIObject source,
ANARIDataType sourceType,
ANARIStatusSeverity severity,
ANARIStatusCode code,
const char *message)
{
(void)userData;
(void)device;
(void)source;
(void)sourceType;
(void)code;
if (severity == ANARI_SEVERITY_FATAL_ERROR) {
fprintf(stderr, "[FATAL] %s\n", message);
} else if (severity == ANARI_SEVERITY_ERROR) {
fprintf(stderr, "[ERROR] %s\n", message);
} else if (severity == ANARI_SEVERITY_WARNING) {
fprintf(stderr, "[WARN ] %s\n", message);
} else if (severity == ANARI_SEVERITY_PERFORMANCE_WARNING) {
fprintf(stderr, "[PERF ] %s\n", message);
} else if (severity == ANARI_SEVERITY_INFO) {
fprintf(stderr, "[INFO ] %s\n", message);
} else if (severity == ANARI_SEVERITY_DEBUG) {
fprintf(stderr, "[DEBUG] %s\n", message);
}
}
const ANARIDataType anonymousTypes[] = {
ANARI_DEVICE,
ANARI_ARRAY1D,
ANARI_ARRAY2D,
ANARI_ARRAY3D,
ANARI_SURFACE,
ANARI_GROUP,
ANARI_WORLD,
ANARI_FRAME,
};
const ANARIDataType namedTypes[] = {
ANARI_INSTANCE,
ANARI_CAMERA,
ANARI_GEOMETRY,
ANARI_LIGHT,
ANARI_MATERIAL,
ANARI_RENDERER,
ANARI_SAMPLER,
ANARI_SPATIAL_FIELD,
ANARI_VOLUME
};
template<int T, class Enable = void>
struct param_printer {
void operator()(const void*) {
}
};
template<>
struct param_printer<ANARI_STRING, void> {
void operator()(const void *mem) {
const char *str = (const char*)mem;
printf("\"%s\"", str);
}
};
template<int T>
struct param_printer<T,
typename std::enable_if<
std::is_floating_point<
typename anari::ANARITypeProperties<T>::base_type
>::value
>::type> {
using base_type = typename anari::ANARITypeProperties<T>::base_type;
static const int components = anari::ANARITypeProperties<T>::components;
void operator()(const void *mem) {
const base_type *data = (base_type*)mem;
printf("%.1f", data[0]);
for(int i = 1;i<components;++i) {
printf(", %.1f", data[i]);
}
}
};
template<int T>
struct param_printer<T,
typename std::enable_if<
std::is_integral<
typename anari::ANARITypeProperties<T>::base_type
>::value
>::type> {
using base_type = typename anari::ANARITypeProperties<T>::base_type;
static const int components = anari::ANARITypeProperties<T>::components;
void operator()(const void *mem) {
const base_type *data = (base_type*)mem;
printf("%lld", (long long)data[0]);
for(int i = 1;i<components;++i) {
printf(", %lld", (long long)data[i]);
}
}
};
template<int T>
struct param_printer<T,
typename std::enable_if<
anari::isObject(T)
>::type> {
using base_type = typename anari::ANARITypeProperties<T>::base_type;
static const int components = anari::ANARITypeProperties<T>::components;
void operator()(const void*) {
printf("%s", anari::toString(T));
}
};
template<int T>
struct param_printer_wrapper : public param_printer<T> { };
static void printAnariFromMemory(ANARIDataType t, const void *mem) {
anari::anariTypeInvoke<void, param_printer_wrapper>(t, mem);
}
void print_info(ANARIDevice device, ANARIDataType objtype, const char *objname, const char *paramname, ANARIDataType paramtype, const char *indent) {
int32_t *required = (int32_t*)anariGetParameterInfo(device, objtype, objname, paramname, paramtype, "required", ANARI_BOOL);
if(required && *required) {
printf("%srequired\n", indent);
}
const void *mem = anariGetParameterInfo(device, objtype, objname, paramname, paramtype, "default", paramtype);
if(mem) {
printf("%sdefault = ", indent);
printAnariFromMemory(paramtype, mem);
printf("\n");
}
mem = anariGetParameterInfo(device, objtype, objname, paramname, paramtype, "minimum", paramtype);
if(mem) {
printf("%sminimum = ", indent);
printAnariFromMemory(paramtype, mem);
printf("\n");
}
mem = anariGetParameterInfo(device, objtype, objname, paramname, paramtype, "maximum", paramtype);
if(mem) {
printf("%smaximum = ", indent);
printAnariFromMemory(paramtype, mem);
printf("\n");
}
mem = anariGetParameterInfo(device, objtype, objname, paramname, paramtype, "use", ANARI_STRING);
if(mem) {
printf("%suse = " , indent);
printAnariFromMemory(ANARI_STRING, mem);
printf("\n");
}
mem = anariGetParameterInfo(device, objtype, objname, paramname, paramtype, "value", ANARI_STRING_LIST);
if(mem) {
const char **list = (const char **)mem;
printf("%svalue =\n", indent);
for(;*list != nullptr;++list) {
printf("%s \"%s\"\n", indent, *list);
}
}
mem = anariGetParameterInfo(device, objtype, objname, paramname, paramtype, "value", ANARI_DATA_TYPE_LIST);
if(mem) {
const ANARIDataType *list = (const ANARIDataType*)mem;
printf("%svalue =\n", indent);
for(;*list != ANARI_UNKNOWN;++list) {
printf("%s %s\n", indent, anari::toString(*list));
}
}
mem = anariGetParameterInfo(device, objtype, objname, paramname, paramtype, "elementType", ANARI_DATA_TYPE_LIST);
if(mem) {
const ANARIDataType *list = (const ANARIDataType*)mem;
printf("%selementType =\n", indent);
for(;*list != ANARI_UNKNOWN;++list) {
printf("%s %s\n", indent, anari::toString(*list));
}
}
mem = anariGetParameterInfo(device, objtype, objname, paramname, paramtype, "description", ANARI_STRING);
if(mem) {
printf("%sdescription = \"%s\"\n", indent, (const char*)mem);
}
mem = anariGetParameterInfo(device, objtype, objname, paramname, paramtype, "sourceExtension", ANARI_STRING);
if(mem) {
printf("%ssourceExtension = %s\n", indent, (const char*)mem);
}
}
/******************************************************************/
int main(int argc, const char **argv)
{
const char *libraryName = NULL;
const char *typeFilter = NULL;
const char *subtypeFilter = NULL;
bool skipParameters = false;
bool info = false;
bool extensions_only = false;
for(int i = 1;i<argc;++i) {
if(strncmp(argv[i], "-l", 2) == 0) {
if(i+1<argc) {
libraryName = argv[++i];
} else {
fprintf(stderr, "missing argument for -l\n");
return 0;
}
}
if(strncmp(argv[i], "-t", 2) == 0) {
if(i+1<argc) {
typeFilter = argv[++i];
} else {
fprintf(stderr, "missing argument for -t\n");
return 0;
}
}
if(strncmp(argv[i], "-s", 2) == 0) {
if(i+1<argc) {
subtypeFilter = argv[++i];
} else {
fprintf(stderr, "missing argument for -s\n");
return 0;
}
}
if(strncmp(argv[i], "-p", 2) == 0) {
skipParameters = true;
}
if(strncmp(argv[i], "-i", 2) == 0) {
info = true;
}
if(strncmp(argv[i], "-f", 2) == 0) {
extensions_only = true;
}
if(strncmp(argv[i], "-h", 2) == 0) {
puts(helptext);
return 0;
}
}
if(libraryName == NULL) {
fprintf(stderr, "specify a library name with -l\n");
return 0;
}
printf("SDK version: %i.%i.%i\n",
ANARI_SDK_VERSION_MAJOR,
ANARI_SDK_VERSION_MINOR,
ANARI_SDK_VERSION_PATCH
);
ANARILibrary lib = anariLoadLibrary(libraryName, statusFunc, NULL);
const char **devices = anariGetDeviceSubtypes(lib);
if(devices) {
printf("Devices:\n");
for(int i = 0;devices[i];++i) {
printf(" %s\n", devices[i]);
}
}
for(int i = 0;devices[i];++i) {
printf("Device \"%s\":\n", devices[i]);
const char **extensions = (const char**)anariGetDeviceExtensions(lib, devices[i]);
if(extensions) {
printf(" Extensions:\n");
for(int k = 0;extensions[k];++k){
printf(" %s\n", extensions[k]);
}
}
if(extensions_only) {
continue;
}
ANARIDevice device = anariNewDevice(lib, devices[i]);
printf(" Subtypes:\n");
for(size_t j = 0;j<sizeof(namedTypes)/sizeof(ANARIDataType);++j) {
const char **types = anariGetObjectSubtypes(device, namedTypes[j]);
// print subtypes of named types
printf(" %s: ", anari::toString(namedTypes[j]));
if(types) {
for(int k = 0;types[k];++k){
printf("%s ", types[k]);
}
}
printf("\n");
}
const char **channels = (const char**)anariGetObjectInfo(device, ANARI_FRAME, NULL, "channel", ANARI_STRING_LIST);
if(channels) {
printf(" Frame Channels:\n");
for(int k = 0;channels[k];++k){
printf(" %s\n", channels[k]);
}
}
if(!skipParameters) {
printf(" Parameters:\n");
for(size_t j = 0;j<sizeof(namedTypes)/sizeof(ANARIDataType);++j) {
if(typeFilter && strstr(anari::toString(namedTypes[j]), typeFilter) == nullptr) {
continue;
}
const char **types = anariGetObjectSubtypes(device, namedTypes[j]);
// print subtypes of named types
if(types) {
for(int k = 0;types[k];++k){
if(subtypeFilter && strstr(types[k], subtypeFilter) == nullptr) {
continue;
}
printf(" %s %s:\n", anari::toString(namedTypes[j]), types[k]);
const ANARIParameter *params = (const ANARIParameter*)anariGetObjectInfo(device, namedTypes[j], types[k], "parameter", ANARI_PARAMETER_LIST);
if(params) {
for(int l = 0;params[l].name;++l){
printf(" * %-25s %-25s\n", params[l].name, anari::toString(params[l].type));
if(info) {
print_info(device, namedTypes[j], types[k], params[l].name, params[l].type, " ");
}
}
}
}
}
}
if(subtypeFilter == nullptr) {
for(size_t j = 0;j<sizeof(anonymousTypes)/sizeof(ANARIDataType);++j) {
if(typeFilter && strstr(anari::toString(anonymousTypes[j]), typeFilter) == nullptr) {
continue;
}
printf(" %s:\n", anari::toString(anonymousTypes[j]));
const ANARIParameter *params = (const ANARIParameter*)anariGetObjectInfo(device, anonymousTypes[j], 0, "parameter", ANARI_PARAMETER_LIST);
if(params) {
for(int l = 0;params[l].name;++l){
printf(" * %-25s %-25s\n", params[l].name, anari::toString(params[l].type));
if(info) {
print_info(device, anonymousTypes[j], nullptr, params[l].name, params[l].type, " ");
}
}
}
}
}
}
anariRelease(device, device);
}
anariUnloadLibrary(lib);
return 0;
}