forked from AcademySoftwareFoundation/OpenShadingLanguage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestrender.cpp
More file actions
702 lines (633 loc) · 28.8 KB
/
testrender.cpp
File metadata and controls
702 lines (633 loc) · 28.8 KB
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
/*
Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
All Rights Reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Sony Pictures Imageworks nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cmath>
#include <OpenImageIO/imageio.h>
#include <OpenImageIO/imagebuf.h>
#include <OpenImageIO/imagebufalgo.h>
#include <OpenImageIO/argparse.h>
#include <OpenImageIO/strutil.h>
#include <OpenImageIO/timer.h>
#include <OpenImageIO/thread.h>
#ifdef USE_EXTERNAL_PUGIXML
# include <pugixml.hpp>
#else
# include <OpenImageIO/pugixml.hpp>
#endif
#include <OSL/oslexec.h>
#include "simplerend.h"
#include "raytracer.h"
#include "background.h"
#include "shading.h"
#include "sampling.h"
#include "util.h"
using namespace OSL;
namespace { // anonymous namespace
ShadingSystem *shadingsys = NULL;
bool debug1 = false;
bool debug2 = false;
bool verbose = false;
bool runstats = false;
int profile = 0;
bool O0 = false, O1 = false, O2 = false;
bool debugnan = false;
static std::string extraoptions;
int xres = 640, yres = 480, aa = 1, max_bounces = 1000000, rr_depth = 5;
int num_threads = 0;
ErrorHandler errhandler;
SimpleRenderer rend; // RendererServices
Camera camera;
Scene scene;
int backgroundShaderID = -1;
int backgroundResolution = 0;
Background background;
std::vector<ShaderGroupRef> shaders;
std::string scenefile, imagefile;
static std::string shaderpath;
static void
set_profile (int argc, const char *argv[])
{
profile = 1;
shadingsys->attribute ("profile", profile);
}
int get_filenames(int argc, const char *argv[])
{
for (int i = 0; i < argc; i++) {
if (scenefile.empty())
scenefile = argv[i];
else if (imagefile.empty())
imagefile = argv[i];
}
return 0;
}
void getargs(int argc, const char *argv[])
{
bool help = false;
OIIO::ArgParse ap;
ap.options ("Usage: testrender [options] scene.xml output.exr",
"%*", get_filenames, "",
"--help", &help, "Print help message",
"-v", &verbose, "Verbose messages",
"--debug", &debug1, "Lots of debugging info",
"--debug2", &debug2, "Even more debugging info",
"--runstats", &runstats, "Print run statistics",
"--stats", &runstats, "", // DEPRECATED 1.7
"--profile %@", &set_profile, NULL, "Print profile information",
"-r %d %d", &xres, &yres, "Render a WxH image",
"-aa %d", &aa, "Trace NxN rays per pixel",
"-t %d", &num_threads, "Render using N threads (default: auto-detect)",
"-O0", &O0, "Do no runtime shader optimization",
"-O1", &O1, "Do a little runtime shader optimization",
"-O2", &O2, "Do lots of runtime shader optimization",
"--debugnan", &debugnan, "Turn on 'debugnan' mode",
"--path %s", &shaderpath, "Specify oso search path",
"--options %s", &extraoptions, "Set extra OSL options",
NULL);
if (ap.parse(argc, argv) < 0) {
std::cerr << ap.geterror() << std::endl;
ap.usage ();
exit (EXIT_FAILURE);
}
if (help) {
std::cout <<
"testrender -- Test Renderer for Open Shading Language\n"
OSL_COPYRIGHT_STRING "\n";
ap.usage ();
exit (EXIT_SUCCESS);
}
if (scenefile.empty()) {
std::cerr << "testrender: Must specify an xml scene file to open\n";
ap.usage();
exit (EXIT_FAILURE);
}
if (imagefile.empty()) {
std::cerr << "testrender: Must specify a filename for output render\n";
ap.usage();
exit (EXIT_FAILURE);
}
if (debug1 || verbose)
errhandler.verbosity (ErrorHandler::VERBOSE);
}
Vec3 strtovec(string_view str) {
Vec3 v(0, 0, 0);
OIIO::Strutil::parse_float (str, v[0]);
OIIO::Strutil::parse_char (str, ',');
OIIO::Strutil::parse_float (str, v[1]);
OIIO::Strutil::parse_char (str, ',');
OIIO::Strutil::parse_float (str, v[2]);
return v;
}
bool strtobool(const char* str) {
return strcmp(str, "1") == 0 ||
strcmp(str, "on") == 0 ||
strcmp(str, "yes") == 0;
}
template <int N>
struct ParamStorage {
ParamStorage() : fparamindex(0), iparamindex(0), sparamindex(0) {}
void* Int(int i) {
ASSERT(iparamindex < N);
iparamdata[iparamindex] = i;
iparamindex++;
return &iparamdata[iparamindex - 1];
}
void* Float(float f) {
ASSERT(fparamindex < N);
fparamdata[fparamindex] = f;
fparamindex++;
return &fparamdata[fparamindex - 1];
}
void* Vec(float x, float y, float z) {
Float(x);
Float(y);
Float(z);
return &fparamdata[fparamindex - 3];
}
void* Str(const char* str) {
ASSERT(sparamindex < N);
sparamdata[sparamindex] = ustring(str);
sparamindex++;
return &sparamdata[sparamindex - 1];
}
private:
// storage for shader parameters
float fparamdata[N];
int iparamdata[N];
ustring sparamdata[N];
int fparamindex;
int iparamindex;
int sparamindex;
};
void parse_scene() {
// setup default camera (now that resolution is finalized)
camera = Camera(Vec3(0,0,0), Vec3(0,0,-1), Vec3(0,1,0), 90.0f, xres, yres);
// load entire text file into a buffer
std::ifstream file(scenefile.c_str(), std::ios::binary);
std::vector<char> text((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
if (text.empty()) {
std::cerr << "Error reading " << scenefile << "\n"
<< "File is either missing or empty\n";
exit (EXIT_FAILURE);
}
text.push_back(0); // make sure text ends with trailing 0
#ifdef USING_OIIO_PUGI
namespace pugi = OIIO::pugi;
#endif
// build DOM tree
pugi::xml_document doc;
pugi::xml_parse_result parse_result = doc.load_file(scenefile.c_str());
if (!parse_result) {
std::cerr << "XML parsed with errors: " << parse_result.description() << ", at offset " << parse_result.offset << "\n";
exit (EXIT_FAILURE);
}
pugi::xml_node root = doc.child("World");
if (!root) {
std::cerr << "Error reading " << scenefile << "\n"
<< "Root element <World> is missing\n";
exit (EXIT_FAILURE);
}
// loop over all children of world
for (pugi::xml_node node = root.first_child(); node; node = node.next_sibling()) {
if (strcmp(node.name(), "Option") == 0) {
for (pugi::xml_attribute attr = node.first_attribute(); attr; attr = attr.next_attribute()) {
int i = 0;
if (sscanf(attr.value(), " int %d ", &i) == 1) {
if (strcmp(attr.name(), "max_bounces") == 0)
max_bounces = i;
else if (strcmp(attr.name(), "rr_depth") == 0)
rr_depth = i;
}
// TODO: pass any extra options to shading system (or texture system?)
}
} else if (strcmp(node.name(), "Camera") == 0) {
// defaults
Vec3 eye(0,0,0);
Vec3 dir(0,0,-1);
Vec3 up(0,1,0);
float fov = 90.f;
// load camera (only first attribute counts if duplicates)
pugi::xml_attribute eye_attr = node.attribute("eye");
pugi::xml_attribute dir_attr = node.attribute("dir");
pugi::xml_attribute at_attr = node.attribute("look_at");
pugi::xml_attribute up_attr = node.attribute("up");
pugi::xml_attribute fov_attr = node.attribute("fov");
if (eye_attr) eye = strtovec(eye_attr.value());
if (dir_attr) dir = strtovec(dir_attr.value()); else
if ( at_attr) dir = strtovec( at_attr.value()) - eye;
if ( up_attr) up = strtovec( up_attr.value());
if (fov_attr) fov = OIIO::Strutil::from_string<float>(fov_attr.value());
// create actual camera
camera = Camera(eye, dir, up, fov, xres, yres);
} else if (strcmp(node.name(), "Sphere") == 0) {
// load sphere
pugi::xml_attribute center_attr = node.attribute("center");
pugi::xml_attribute radius_attr = node.attribute("radius");
if (center_attr && radius_attr) {
Vec3 center = strtovec(center_attr.value());
float radius = OIIO::Strutil::from_string<float>(radius_attr.value());
if (radius > 0) {
pugi::xml_attribute light_attr = node.attribute("is_light");
bool is_light = light_attr ? strtobool(light_attr.value()) : false;
scene.add_sphere(Sphere(center, radius, int(shaders.size()) - 1, is_light));
}
}
} else if (strcmp(node.name(), "Quad") == 0) {
// load quad
pugi::xml_attribute corner_attr = node.attribute("corner");
pugi::xml_attribute edge_x_attr = node.attribute("edge_x");
pugi::xml_attribute edge_y_attr = node.attribute("edge_y");
if (corner_attr && edge_x_attr && edge_y_attr) {
pugi::xml_attribute light_attr = node.attribute("is_light");
bool is_light = light_attr ? strtobool(light_attr.value()) : false;
Vec3 co = strtovec(corner_attr.value());
Vec3 ex = strtovec(edge_x_attr.value());
Vec3 ey = strtovec(edge_y_attr.value());
scene.add_quad(Quad(co, ex, ey, int(shaders.size()) - 1, is_light));
}
} else if (strcmp(node.name(), "Background") == 0) {
pugi::xml_attribute res_attr = node.attribute("resolution");
if (res_attr)
backgroundResolution = OIIO::Strutil::from_string<int>(res_attr.value());
backgroundShaderID = int(shaders.size()) - 1;
} else if (strcmp(node.name(), "ShaderGroup") == 0) {
ShaderGroupRef group;
pugi::xml_attribute name_attr = node.attribute("name");
std::string name = name_attr? name_attr.value() : "";
pugi::xml_attribute type_attr = node.attribute("type");
std::string shadertype = type_attr ? type_attr.value() : "surface";
pugi::xml_attribute commands_attr = node.attribute("commands");
std::string commands = commands_attr ? commands_attr.value() : node.text().get();
if (commands.size())
group = shadingsys->ShaderGroupBegin (name, shadertype, commands);
else
group = shadingsys->ShaderGroupBegin (name);
ParamStorage<1024> store; // scratch space to hold parameters until they are read by Shader()
for (pugi::xml_node gnode = node.first_child(); gnode; gnode = gnode.next_sibling()) {
if (strcmp(gnode.name(), "Parameter") == 0) {
// handle parameters
for (pugi::xml_attribute attr = gnode.first_attribute(); attr; attr = attr.next_attribute()) {
int i = 0; float x = 0, y = 0, z = 0;
if (sscanf(attr.value(), " int %d ", &i) == 1)
shadingsys->Parameter(attr.name(), TypeDesc::TypeInt, store.Int(i));
else if (sscanf(attr.value(), " float %f ", &x) == 1)
shadingsys->Parameter(attr.name(), TypeDesc::TypeFloat, store.Float(x));
else if (sscanf(attr.value(), " vector %f %f %f", &x, &y, &z) == 3)
shadingsys->Parameter(attr.name(), TypeDesc::TypeVector, store.Vec(x, y, z));
else if (sscanf(attr.value(), " point %f %f %f", &x, &y, &z) == 3)
shadingsys->Parameter(attr.name(), TypeDesc::TypePoint, store.Vec(x, y, z));
else if (sscanf(attr.value(), " color %f %f %f", &x, &y, &z) == 3)
shadingsys->Parameter(attr.name(), TypeDesc::TypeColor, store.Vec(x, y, z));
else
shadingsys->Parameter(attr.name(), TypeDesc::TypeString, store.Str(attr.value()));
}
} else if (strcmp(gnode.name(), "Shader") == 0) {
pugi::xml_attribute type_attr = gnode.attribute("type");
pugi::xml_attribute name_attr = gnode.attribute("name");
pugi::xml_attribute layer_attr = gnode.attribute("layer");
const char* type = type_attr ? type_attr.value() : "surface";
if (name_attr && layer_attr)
shadingsys->Shader(type, name_attr.value(), layer_attr.value());
} else if (strcmp(gnode.name(), "ConnectShaders") == 0) {
// FIXME: find a more elegant way to encode this
pugi::xml_attribute sl = gnode.attribute("srclayer");
pugi::xml_attribute sp = gnode.attribute("srcparam");
pugi::xml_attribute dl = gnode.attribute("dstlayer");
pugi::xml_attribute dp = gnode.attribute("dstparam");
if (sl && sp && dl && dp)
shadingsys->ConnectShaders(sl.value(), sp.value(),
dl.value(), dp.value());
} else {
// unknow element?
}
}
shadingsys->ShaderGroupEnd();
shaders.push_back (group);
} else {
// unknown element?
}
}
if (root.next_sibling()) {
std::cerr << "Error reading " << scenefile << "\n"
<< "Found multiple top-level elements\n";
exit (EXIT_FAILURE);
}
}
void globals_from_hit(ShaderGlobals& sg, const Ray& r, const Dual2<float>& t, int id, bool flip) {
memset(&sg, 0, sizeof(ShaderGlobals));
Dual2<Vec3> P = r.point(t);
sg.P = P.val(); sg.dPdx = P.dx(); sg.dPdy = P.dy();
Dual2<Vec3> N = scene.normal(P, id);
sg.Ng = sg.N = N.val();
Dual2<Vec2> uv = scene.uv(P, N, sg.dPdu, sg.dPdv, id);
sg.u = uv.val().x; sg.dudx = uv.dx().x; sg.dudy = uv.dy().x;
sg.v = uv.val().y; sg.dvdx = uv.dx().y; sg.dvdy = uv.dy().y;
sg.surfacearea = scene.surfacearea(id);
sg.I = r.d.val();
sg.dIdx = r.d.dx();
sg.dIdy = r.d.dy();
sg.backfacing = sg.N.dot(sg.I) > 0;
if (sg.backfacing) {
sg.N = -sg.N;
sg.Ng = -sg.Ng;
}
sg.flipHandedness = flip;
// In our SimpleRenderer, the "renderstate" itself just a pointer to
// the ShaderGlobals.
sg.renderstate = &sg;
}
Vec3 eval_background(const Dual2<Vec3>& dir, ShadingContext* ctx) {
ShaderGlobals sg;
memset(&sg, 0, sizeof(ShaderGlobals));
sg.I = dir.val();
sg.dIdx = dir.dx();
sg.dIdy = dir.dy();
shadingsys->execute(ctx, *shaders[backgroundShaderID], sg);
return process_background_closure(sg.Ci);
}
Color3 subpixel_radiance(float x, float y, Sampler& sampler, ShadingContext* ctx) {
Ray r = camera.get(x, y);
Color3 path_weight(1, 1, 1);
Color3 path_radiance(0, 0, 0);
int prev_id = -1;
float bsdf_pdf = std::numeric_limits<float>::infinity(); // camera ray has only one possible direction
bool flip = false;
for (int b = 0; b <= max_bounces; b++) {
// trace the ray against the scene
Dual2<float> t; int id = prev_id;
if (!scene.intersect(r, t, id)) {
// we hit nothing? check background shader
if (backgroundShaderID >= 0) {
if (backgroundResolution > 0) {
float bg_pdf = 0;
Vec3 bg = background.eval(r.d.val(), bg_pdf);
path_radiance += path_weight * bg * MIS::power_heuristic<MIS::WEIGHT_WEIGHT>(bsdf_pdf, bg_pdf);
} else {
// we aren't importance sampling the background - so just run it directly
path_radiance += path_weight * eval_background(r.d, ctx);
}
}
break;
}
// construct a shader globals for the hit point
ShaderGlobals sg;
globals_from_hit(sg, r, t, id, flip);
int shaderID = scene.shaderid(id);
if (shaderID < 0 || !shaders[shaderID]) break; // no shader attached? done
// execute shader and process the resulting list of closures
shadingsys->execute (ctx, *shaders[shaderID], sg);
ShadingResult result;
bool last_bounce = b == max_bounces;
process_closure(result, sg.Ci, last_bounce);
// add self-emission
float k = 1;
if (scene.islight(id)) {
// figure out the probability of reaching this point
float light_pdf = scene.shapepdf(id, r.o.val(), sg.P);
k = MIS::power_heuristic<MIS::WEIGHT_EVAL>(bsdf_pdf, light_pdf);
}
path_radiance += path_weight * k * result.Le;
// last bounce? nothing left to do
if (last_bounce) break;
// build internal pdf for sampling between bsdf closures
result.bsdf.prepare(sg, path_weight, b >= rr_depth);
// get two random numbers
Vec3 s = sampler.get();
float xi = s.x;
float yi = s.y;
float zi = s.z;
// trace one ray to the background
if (backgroundResolution > 0) {
Dual2<Vec3> bg_dir;
float bg_pdf = 0, bsdf_pdf = 0;
Vec3 bg = background.sample(xi, yi, bg_dir, bg_pdf);
Color3 bsdf_weight = result.bsdf.eval(sg, bg_dir.val(), bsdf_pdf);
Color3 contrib = path_weight * bsdf_weight * bg * MIS::power_heuristic<MIS::WEIGHT_WEIGHT>(bg_pdf, bsdf_pdf);
if ((contrib.x + contrib.y + contrib.z) > 0) {
int shadow_id = id;
Ray shadow_ray = Ray(sg.P, bg_dir);
Dual2<float> shadow_dist;
if (!scene.intersect(shadow_ray, shadow_dist, shadow_id)) // ray reached the background?
path_radiance += contrib;
}
}
// trace one ray to each light
for (int lid = 0; lid < scene.num_prims(); lid++) {
if (lid == id) continue; // skip self
if (!scene.islight(lid)) continue; // doesn't want to be sampled as a light
int shaderID = scene.shaderid(lid);
if (shaderID < 0 || !shaders[shaderID]) continue; // no shader attached to this light
// sample a random direction towards the object
float light_pdf;
Vec3 ldir = scene.sample(lid, sg.P, xi, yi, light_pdf);
float bsdf_pdf = 0;
Color3 contrib = path_weight * result.bsdf.eval(sg, ldir, bsdf_pdf) * MIS::power_heuristic<MIS::EVAL_WEIGHT>(light_pdf, bsdf_pdf);
if ((contrib.x + contrib.y + contrib.z) > 0) {
Ray shadow_ray = Ray(sg.P, ldir);
// trace a shadow ray and see if we actually hit the target
// in this tiny renderer, tracing a ray is probably cheaper than evaluating the light shader
int shadow_id = id; // ignore self hit
Dual2<float> shadow_dist;
if (scene.intersect(shadow_ray, shadow_dist, shadow_id) && shadow_id == lid) {
// setup a shader global for the point on the light
ShaderGlobals light_sg;
globals_from_hit(light_sg, shadow_ray, shadow_dist, lid, false);
// execute the light shader (for emissive closures only)
shadingsys->execute (ctx, *shaders[shaderID], light_sg);
ShadingResult light_result;
process_closure(light_result, light_sg.Ci, true);
// accumulate contribution
path_radiance += contrib * light_result.Le;
}
}
}
// trace indirect ray and continue
path_weight *= result.bsdf.sample(sg, xi, yi, zi, r.d, bsdf_pdf);
if (!(path_weight.x > 0) && !(path_weight.y > 0) && !(path_weight.z > 0))
break; // filter out all 0's or NaNs
prev_id = id;
r.o = Dual2<Vec3>(sg.P, sg.dPdx, sg.dPdy);
flip ^= sg.Ng.dot(r.d.val()) > 0;
}
return path_radiance;
}
Color3 antialias_pixel(int x, int y, ShadingContext* ctx) {
Color3 result(0, 0, 0);
for (int ay = 0, si = 0; ay < aa; ay++) {
for (int ax = 0; ax < aa; ax++, si++) {
Sampler sampler(x, y, si, aa);
// jitter pixel coordinate [0,1)^2
Vec3 j = sampler.get();
// warp distribution to approximate a tent filter [-1,+1)^2
j.x *= 2; j.x = j.x < 1 ? sqrtf(j.x) - 1 : 1 - sqrtf(2 - j.x);
j.y *= 2; j.y = j.y < 1 ? sqrtf(j.y) - 1 : 1 - sqrtf(2 - j.y);
// trace eye ray (apply jitter from center of the pixel)
result += subpixel_radiance(x + 0.5f + j.x, y + 0.5f + j.y, sampler, ctx);
}
}
return result / float(aa * aa);
}
void scanline_worker(Counter& counter, std::vector<Color3>& pixels) {
// Optional: high-performance apps may request this thread-specific
// pointer in order to save a bit of time on each shade. Just like
// the name implies, a multithreaded renderer would need to do this
// separately for each thread, and be careful to always use the same
// thread_info each time for that thread.
//
// There's nothing wrong with a simpler app just passing NULL for
// the thread_info; in such a case, the ShadingSystem will do the
// necessary calls to find the thread-specific pointer itself, but
// this will degrade performance just a bit.
OSL::PerThreadInfo *thread_info = shadingsys->create_thread_info();
// Request a shading context so that we can execute the shader.
// We could get_context/release_constext for each shading point,
// but to save overhead, it's more efficient to reuse a context
// within a thread.
ShadingContext *ctx = shadingsys->get_context (thread_info);
int y;
while (counter.getnext(y)) {
for (int x = 0, i = xres * y; x < xres; ++x, ++i)
pixels[i] = antialias_pixel(x, y, ctx);
}
// We're done shading with this context.
shadingsys->release_context (ctx);
// Now that we're done rendering, release the thread=specific
// pointer we saved. A simple app could skip this; but if the app
// asks for it (as we have in this example), then it should also
// destroy it when done with it.
shadingsys->destroy_thread_info(thread_info);
}
} // anonymous namespace
int main (int argc, const char *argv[]) {
using namespace OIIO;
Timer timer;
// Create a new shading system. We pass it the RendererServices
// object that services callbacks from the shading system, NULL for
// the TextureSystem (which will create a default OIIO one), and
// an error handler.
shadingsys = new ShadingSystem (&rend, NULL, &errhandler);
// Register the layout of all closures known to this renderer
// Any closure used by the shader which is not registered, or
// registered with a different number of arguments will lead
// to a runtime error.
register_closures(shadingsys);
// Remember that each shader parameter may optionally have a
// metadata hint [[int lockgeom=...]], where 0 indicates that the
// parameter may be overridden by the geometry itself, for example
// with data interpolated from the mesh vertices, and a value of 1
// means that it is "locked" with respect to the geometry (i.e. it
// will not be overridden with interpolated or
// per-geometric-primitive data).
//
// In order to most fully optimize shader, we typically want any
// shader parameter not explicitly specified to default to being
// locked (i.e. no per-geometry override):
shadingsys->attribute("lockgeom", 1);
// Read command line arguments
getargs (argc, argv);
// Setup common attributes
shadingsys->attribute ("debug", debug2 ? 2 : (debug1 ? 1 : 0));
shadingsys->attribute ("compile_report", debug1|debug2);
int opt = O2 ? 2 : (O1 ? 1 : 0);
if (const char *opt_env = getenv ("TESTSHADE_OPT")) // overrides opt
opt = atoi(opt_env);
shadingsys->attribute ("optimize", opt);
shadingsys->attribute ("debugnan", debugnan);
if (! shaderpath.empty())
shadingsys->attribute ("searchpath:shader", shaderpath);
if (extraoptions.size())
shadingsys->attribute ("options", extraoptions);
// Loads a scene, creating camera, geometry and assigning shaders
parse_scene();
// validate options
if (aa < 1) aa = 1;
if (num_threads < 1)
num_threads = std::thread::hardware_concurrency();
// prepare background importance table (if requested)
if (backgroundResolution > 0 && backgroundShaderID >= 0) {
// get a context so we can make several background shader calls
OSL::PerThreadInfo *thread_info = shadingsys->create_thread_info();
ShadingContext *ctx = shadingsys->get_context (thread_info);
// build importance table to optimize background sampling
background.prepare(backgroundResolution, eval_background, ctx);
// release context
shadingsys->release_context (ctx);
shadingsys->destroy_thread_info(thread_info);
} else {
// we aren't directly evaluating the background
backgroundResolution = 0;
}
double setuptime = timer.lap ();
// Local memory for the pixels
std::vector<Color3> pixels(xres * yres, Color3(0,0,0));
// Make an ImageBuf that wraps it ('pixels' still owns the memory)
ImageBuf pixelbuf (ImageSpec(xres, yres, 3, TypeDesc::FLOAT), pixels.data());
// Create shared counter to iterate over one scanline at a time
Counter scanline_counter(errhandler, yres, "Rendering");
// launch a scanline worker for each thread
OIIO::thread_group workers;
for (int i = 0; i < num_threads; i++)
workers.add_thread(new std::thread (scanline_worker, std::ref(scanline_counter), std::ref(pixels)));
workers.join_all();
double runtime = timer.lap();
// Write image to disk
if (Strutil::iends_with (imagefile, ".jpg") ||
Strutil::iends_with (imagefile, ".jpeg") ||
Strutil::iends_with (imagefile, ".gif") ||
Strutil::iends_with (imagefile, ".png")) {
// JPEG, GIF, and PNG images should be automatically saved as sRGB
// because they are almost certainly supposed to be displayed on web
// pages.
ImageBufAlgo::colorconvert (pixelbuf, pixelbuf,
"linear", "sRGB", false, "", "");
}
pixelbuf.set_write_format (TypeDesc::HALF);
if (! pixelbuf.write (imagefile))
errhandler.error ("Unable to write output image: %s",
pixelbuf.geterror().c_str());
// Print some debugging info
if (debug1 || runstats || profile) {
double writetime = timer.lap();
std::cout << "\n";
std::cout << "Setup: " << OIIO::Strutil::timeintervalformat (setuptime,2) << "\n";
std::cout << "Run : " << OIIO::Strutil::timeintervalformat (runtime,2) << "\n";
std::cout << "Write: " << OIIO::Strutil::timeintervalformat (writetime,2) << "\n";
std::cout << "\n";
std::cout << shadingsys->getstats (5) << "\n";
OIIO::TextureSystem *texturesys = shadingsys->texturesys();
if (texturesys)
std::cout << texturesys->getstats (5) << "\n";
std::cout << ustring::getstats() << "\n";
}
// We're done with the shading system now, destroy it
shaders.clear (); // Must release the group refs first
delete shadingsys;
return EXIT_SUCCESS;
}