-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
386 lines (312 loc) · 12.5 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
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
// Copyright 2023 Stefan Zellmann and Jefferson Amstutz
// SPDX-License-Identifier: Apache-2.0
// anari_cpp
#define ANARI_EXTENSION_UTILITY_IMPL
#include <anari/anari_cpp.hpp>
// std
#include <algorithm>
#include <array>
#include <cstdio>
#include <iostream>
#include <numeric>
#include <random>
// stb_image
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
// ours
#include "math-helpers.h"
#include "Projection.h"
// ========================================================
// generate our test scene
// ========================================================
anari::World generateScene(anari::Device device, const float3 &pos)
{
const uint32_t numSpheres = 10000;
const float radius = .015f;
std::mt19937 rng;
rng.seed(0);
std::normal_distribution<float> vert_dist(0.f, 0.25f);
// Create + fill position and color arrays with randomized values //
auto indicesArray = anari::newArray1D(device, ANARI_UINT32, numSpheres);
auto positionsArray =
anari::newArray1D(device, ANARI_FLOAT32_VEC3, numSpheres);
auto distanceArray = anari::newArray1D(device, ANARI_FLOAT32, numSpheres);
{
auto *positions = anari::map<float3>(device, positionsArray);
auto *distances = anari::map<float>(device, distanceArray);
for (uint32_t i = 0; i < numSpheres; i++) {
const auto a = positions[i][0] = vert_dist(rng);
const auto b = positions[i][1] = vert_dist(rng);
const auto c = positions[i][2] = vert_dist(rng);
distances[i] = std::sqrt(a * a + b * b + c * c); // will be roughly 0-1
// translate
positions[i] += pos;
}
anari::unmap(device, positionsArray);
anari::unmap(device, distanceArray);
auto *indicesBegin = anari::map<uint32_t>(device, indicesArray);
auto *indicesEnd = indicesBegin + numSpheres;
std::iota(indicesBegin, indicesEnd, 0);
std::shuffle(indicesBegin, indicesEnd, rng);
anari::unmap(device, indicesArray);
}
// Create and parameterize geometry //
auto geometry = anari::newObject<anari::Geometry>(device, "sphere");
anari::setAndReleaseParameter(
device, geometry, "primitive.index", indicesArray);
anari::setAndReleaseParameter(
device, geometry, "vertex.position", positionsArray);
anari::setAndReleaseParameter(
device, geometry, "vertex.attribute0", distanceArray);
anari::setParameter(device, geometry, "radius", radius);
anari::commitParameters(device, geometry);
// Create color map texture //
auto texelArray = anari::newArray1D(device, ANARI_FLOAT32_VEC3, 2);
{
auto *texels = anari::map<float3>(device, texelArray);
texels[0][0] = 1.f;
texels[0][1] = 0.f;
texels[0][2] = 0.f;
texels[1][0] = 0.f;
texels[1][1] = 1.f;
texels[1][2] = 0.f;
anari::unmap(device, texelArray);
}
auto texture = anari::newObject<anari::Sampler>(device, "image1D");
anari::setAndReleaseParameter(device, texture, "image", texelArray);
anari::setParameter(device, texture, "filter", "linear");
anari::commitParameters(device, texture);
// Create and parameterize material //
auto material = anari::newObject<anari::Material>(device, "matte");
anari::setAndReleaseParameter(device, material, "color", texture);
anari::commitParameters(device, material);
// Create and parameterize surface //
auto surface = anari::newObject<anari::Surface>(device);
anari::setAndReleaseParameter(device, surface, "geometry", geometry);
anari::setAndReleaseParameter(device, surface, "material", material);
anari::commitParameters(device, surface);
// Create and parameterize world //
auto world = anari::newObject<anari::World>(device);
#if 1
{
auto surfaceArray = anari::newArray1D(device, ANARI_SURFACE, 1);
auto *s = anari::map<anari::Surface>(device, surfaceArray);
s[0] = surface;
anari::unmap(device, surfaceArray);
anari::setAndReleaseParameter(device, world, "surface", surfaceArray);
}
#else
anari::setAndReleaseParameter(
device, world, "surface", anari::newArray1D(device, &surface));
#endif
anari::release(device, surface);
anari::commitParameters(device, world);
return world;
}
// ========================================================
// query anari extensions (ANARI_VSNRAY_CAMERA_MATRIX)
// ========================================================
static bool deviceHasExtension(anari::Library library,
const std::string &deviceSubtype,
const std::string &extName)
{
const char **extensions =
anariGetDeviceExtensions(library, deviceSubtype.c_str());
for (; *extensions; extensions++) {
if (*extensions == extName)
return true;
}
return false;
}
// ========================================================
// Log ANARI errors
// ========================================================
static void statusFunc(const void * /*userData*/,
ANARIDevice /*device*/,
ANARIObject source,
ANARIDataType /*sourceType*/,
ANARIStatusSeverity severity,
ANARIStatusCode /*code*/,
const char *message)
{
if (severity == ANARI_SEVERITY_FATAL_ERROR) {
fprintf(stderr, "[FATAL][%p] %s\n", source, message);
std::exit(1);
} else if (severity == ANARI_SEVERITY_ERROR) {
fprintf(stderr, "[ERROR][%p] %s\n", source, message);
} else if (severity == ANARI_SEVERITY_WARNING) {
fprintf(stderr, "[WARN ][%p] %s\n", source, message);
} else if (severity == ANARI_SEVERITY_PERFORMANCE_WARNING) {
fprintf(stderr, "[PERF ][%p] %s\n", source, message);
}
// Ignore INFO/DEBUG messages
}
// ========================================================
// Function to render a given frame (renderer+world+cam)
// and produce an output image
// ========================================================
static void render(
anari::Device device, anari::Frame frame, const std::string &fileName)
{
// Render frame and print out duration property //
anari::render(device, frame);
anari::wait(device, frame);
float duration = 0.f;
anari::getProperty(device, frame, "duration", duration, ANARI_NO_WAIT);
printf("rendered frame in %fms\n", duration * 1000);
stbi_flip_vertically_on_write(1);
auto fb = anari::map<uint32_t>(device, frame, "channel.color");
stbi_write_png(
fileName.c_str(), fb.width, fb.height, 4, fb.data, 4 * fb.width);
anari::unmap(device, frame, "channel.color");
std::cout << "Output: " << fileName << '\n';
}
// ========================================================
// Strategy 1
// requires an ANARI extension, provided by the
// anari-visionaray device
// ========================================================
static void renderMatricesWithMatrixCamExtension(
anari::Device device, anari::Frame frame, mat4 proj, mat4 view)
{
// Create camera //
auto camera = anari::newObject<anari::Camera>(device, "matrix");
anari::setParameter(device, camera, "proj", proj);
anari::setParameter(device, camera, "view", view);
anari::commitParameters(device, camera);
anari::setParameter(device, frame, "camera", camera);
anari::commitParameters(device, frame);
render(device, frame, "strategy1.png");
anari::release(device, camera);
}
// ========================================================
// Strategy 2
// ========================================================
static void renderFixedFrameWithPerspectiveCam(anari::Device device,
anari::Frame frame,
float3 LL,
float3 LR,
float3 UR,
float3 eye)
{
float3 dir, up;
float fovy, aspect;
float4 imgRegion;
offaxisStereoCamera(LL, LR, UR, eye, dir, up, fovy, aspect, imgRegion);
// Create camera //
auto camera = anari::newObject<anari::Camera>(device, "perspective");
anari::setParameter(device, camera, "position", eye);
anari::setParameter(device, camera, "direction", dir);
anari::setParameter(device, camera, "up", up);
anari::setParameter(device, camera, "fovy", fovy);
anari::setParameter(device, camera, "aspect", aspect);
anari::setParameter(
device, camera, "imageRegion", ANARI_FLOAT32_BOX2, &imgRegion);
anari::commitParameters(device, camera);
anari::setParameter(device, frame, "camera", camera);
anari::commitParameters(device, frame);
render(device, frame, "strategy2.png");
anari::release(device, camera);
}
// ========================================================
// Strategy 3
// ========================================================
static void renderMatricesWithPerspectiveCam(
anari::Device device, anari::Frame frame, mat4 proj, mat4 view)
{
float3 eye, dir, up;
float fovy, aspect;
float4 imgRegion;
offaxisStereoCameraFromTransform(
inverse(proj), inverse(view), eye, dir, up, fovy, aspect, imgRegion);
// Create camera //
auto camera = anari::newObject<anari::Camera>(device, "perspective");
anari::setParameter(device, camera, "position", eye);
anari::setParameter(device, camera, "direction", dir);
anari::setParameter(device, camera, "up", up);
anari::setParameter(device, camera, "fovy", fovy);
anari::setParameter(device, camera, "aspect", aspect);
anari::setParameter(
device, camera, "imageRegion", ANARI_FLOAT32_BOX2, &imgRegion);
anari::commitParameters(device, camera);
anari::setParameter(device, frame, "camera", camera);
anari::commitParameters(device, frame);
render(device, frame, "strategy3.png");
anari::release(device, camera);
}
int main()
{
// Setup ANARI device //
auto library = anari::loadLibrary("environment", statusFunc);
auto device = anari::newDevice(library, "default");
anari::Extensions extensions =
anari::extension::getInstanceExtensionStruct(device, device);
if (!extensions.ANARI_KHR_GEOMETRY_SPHERE)
printf("WARNING: device doesn't support ANARI_KHR_GEOMETRY_SPHERE\n");
if (!extensions.ANARI_KHR_CAMERA_PERSPECTIVE)
printf("WARNING: device doesn't support ANARI_KHR_CAMERA_PERSPECTIVE\n");
if (!extensions.ANARI_KHR_LIGHT_DIRECTIONAL)
printf("WARNING: device doesn't support ANARI_KHR_LIGHT_DIRECTIONAL\n");
if (!extensions.ANARI_KHR_MATERIAL_MATTE)
printf("WARNING: device doesn't support ANARI_KHR_MATERIAL_MATTE\n");
// Create world from a helper function //
auto world = generateScene(device, float3(1.5f, 1.5f, 0.f));
// Add a directional light source //
auto light = anari::newObject<anari::Light>(device, "directional");
anari::setParameterArray1D(device, world, "light", &light, 1);
anari::release(device, light);
// Create renderer //
auto renderer = anari::newObject<anari::Renderer>(device, "default");
const float4 backgroundColor = {0.1f, 0.1f, 0.1f, 1.f};
anari::setParameter(device, renderer, "background", backgroundColor);
anari::setParameter(device, renderer, "pixelSamples", 32);
anari::commitParameters(device, renderer);
// Create frame (top-level object) //
auto frame = anari::newObject<anari::Frame>(device);
uint2 imageSize = {800, 800};
anari::setParameter(device, frame, "size", imageSize);
anari::setParameter(device, frame, "channel.color", ANARI_UFIXED8_RGBA_SRGB);
anari::setParameter(device, frame, "world", world);
anari::setParameter(device, frame, "renderer", renderer);
bool hasMatrixCameraExt =
deviceHasExtension(library, "default", "ANARI_VSNRAY_CAMERA_MATRIX");
// Input configuration: screen of size 3x3, viewer at the center
// but with an offset in Y, so the frustum is tilted a little
// towards the top
float3 LL(0.f, 0.f, 0.f);
float3 LR(3.f, 0.f, 0.f);
float3 UR(3.f, 3.f, 0.f);
float3 eye(1.5f, 1.68f, 1.5f);
// Strategy 1: use matrices coming from the app, plus an extension that
// unprojects rays in NDC back to world space
// (the renderer has to support/implement this)
if (hasMatrixCameraExt) {
std::cout << "Strategy 1 ...\n";
mat4 proj, view;
offaxisStereoTransform(LL, LR, UR, eye, proj, view);
renderMatricesWithMatrixCamExtension(device, frame, proj, view);
} else {
std::cerr
<< "Extension ANARI_VSNRAY_CAMERA_MATRIX not found, skipping Strategy 1\n";
}
// Strategy 2: transform the input frame to a format any ANARI device supports
{
std::cout << "Strategy 2 ...\n";
renderFixedFrameWithPerspectiveCam(device, frame, LL, LR, UR, eye);
}
// Strategy 3: given the input matrices, first reconstruct the frustum,
// then transform input frame as in Strategy 2
{
std::cout << "Strategy 3 ...\n";
mat4 proj, view;
offaxisStereoTransform(LL, LR, UR, eye, proj, view);
renderMatricesWithPerspectiveCam(device, frame, proj, view);
}
// Cleanup remaining ANARI objets //
anari::release(device, renderer);
anari::release(device, world);
anari::release(device, frame);
anari::release(device, device);
anari::unloadLibrary(library);
return 0;
}