1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: rtmain.c,v 2.38 2022/10/19 21:25:20 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* rtmain.c - main for rtrace per-ray calculation program |
6 |
*/ |
7 |
|
8 |
#include "copyright.h" |
9 |
|
10 |
#include <signal.h> |
11 |
|
12 |
#include "platform.h" |
13 |
#include "rtprocess.h" /* getpid() */ |
14 |
#include "resolu.h" |
15 |
#include "ray.h" |
16 |
#include "source.h" |
17 |
#include "ambient.h" |
18 |
#include "random.h" |
19 |
#include "paths.h" |
20 |
#include "pmapray.h" |
21 |
|
22 |
extern char *progname; /* global argv[0] */ |
23 |
|
24 |
extern char *shm_boundary; /* boundary of shared memory */ |
25 |
|
26 |
/* persistent processes define */ |
27 |
#ifdef F_SETLKW |
28 |
#define PERSIST 1 /* normal persist */ |
29 |
#define PARALLEL 2 /* parallel persist */ |
30 |
#define PCHILD 3 /* child of normal persist */ |
31 |
#endif |
32 |
|
33 |
char *sigerr[NSIG]; /* signal error messages */ |
34 |
char *errfile = NULL; /* error output file */ |
35 |
|
36 |
int nproc = 1; /* number of processes */ |
37 |
|
38 |
extern char *formstr(int f); /* string from format */ |
39 |
extern int setrtoutput(void); /* set output values */ |
40 |
|
41 |
int inform = 'a'; /* input format */ |
42 |
int outform = 'a'; /* output format */ |
43 |
char *outvals = "v"; /* output specification */ |
44 |
|
45 |
int hresolu = 0; /* horizontal (scan) size */ |
46 |
int vresolu = 0; /* vertical resolution */ |
47 |
|
48 |
extern int castonly; /* only doing ray-casting? */ |
49 |
|
50 |
int imm_irrad = 0; /* compute immediate irradiance? */ |
51 |
int lim_dist = 0; /* limit distance? */ |
52 |
|
53 |
#ifndef MAXMODLIST |
54 |
#define MAXMODLIST 1024 /* maximum modifiers we'll track */ |
55 |
#endif |
56 |
|
57 |
extern void (*addobjnotify[])(); /* object notification calls */ |
58 |
extern void tranotify(OBJECT obj); |
59 |
|
60 |
char *tralist[MAXMODLIST]; /* list of modifers to trace (or no) */ |
61 |
int traincl = -1; /* include == 1, exclude == 0 */ |
62 |
|
63 |
static int loadflags = ~IO_FILES; /* what to load from octree */ |
64 |
|
65 |
static void onsig(int signo); |
66 |
static void sigdie(int signo, char *msg); |
67 |
static void printdefaults(void); |
68 |
|
69 |
#ifdef PERSIST |
70 |
#define RTRACE_FEATURES "Persist\nParallelPersist\nMultiProcessing\n" \ |
71 |
"IrradianceCalc\nImmediateIrradiance\nDistanceLimiting\n" \ |
72 |
"HessianAmbientCache\nAmbientAveraging\n" \ |
73 |
"AmbientValueSharing\nAdaptiveShadowTesting\n" |
74 |
#else |
75 |
#define RTRACE_FEATURES "IrradianceCalc\nIrradianceCalc\nDistanceLimiting\n" \ |
76 |
"HessianAmbientCache\nAmbientAveraging\n" \ |
77 |
"AmbientValueSharing\nAdaptiveShadowTesting\n" |
78 |
#endif |
79 |
|
80 |
|
81 |
int |
82 |
main(int argc, char *argv[]) |
83 |
{ |
84 |
#define check(ol,al) if (argv[i][ol] || \ |
85 |
badarg(argc-i-1,argv+i+1,al)) \ |
86 |
goto badopt |
87 |
#define check_bool(olen,var) switch (argv[i][olen]) { \ |
88 |
case '\0': var = !var; break; \ |
89 |
case 'y': case 'Y': case 't': case 'T': \ |
90 |
case '+': case '1': var = 1; break; \ |
91 |
case 'n': case 'N': case 'f': case 'F': \ |
92 |
case '-': case '0': var = 0; break; \ |
93 |
default: goto badopt; } |
94 |
extern char *octname; |
95 |
int persist = 0; |
96 |
char *octnm = NULL; |
97 |
char **tralp = NULL; |
98 |
int duped1 = -1; |
99 |
int rval; |
100 |
int i; |
101 |
/* global program name */ |
102 |
progname = argv[0] = fixargv0(argv[0]); |
103 |
/* feature check only? */ |
104 |
strcat(RFeatureList, RTRACE_FEATURES); |
105 |
if (!strcmp(argv[1], "-features")) |
106 |
return feature_status(argc-2, argv+2); |
107 |
/* add trace notify function */ |
108 |
for (i = 0; addobjnotify[i] != NULL; i++) |
109 |
; |
110 |
addobjnotify[i] = tranotify; |
111 |
/* option city */ |
112 |
for (i = 1; i < argc; i++) { |
113 |
/* expand arguments */ |
114 |
while ((rval = expandarg(&argc, &argv, i)) > 0) |
115 |
; |
116 |
if (rval < 0) { |
117 |
sprintf(errmsg, "cannot expand '%s'", argv[i]); |
118 |
error(SYSTEM, errmsg); |
119 |
} |
120 |
if (argv[i] == NULL || argv[i][0] != '-') |
121 |
break; /* break from options */ |
122 |
if (!strcmp(argv[i], "-version")) { |
123 |
puts(VersionID); |
124 |
quit(0); |
125 |
} |
126 |
if (!strcmp(argv[i], "-defaults") || |
127 |
!strcmp(argv[i], "-help")) { |
128 |
printdefaults(); |
129 |
quit(0); |
130 |
} |
131 |
rval = getrenderopt(argc-i, argv+i); |
132 |
if (rval >= 0) { |
133 |
i += rval; |
134 |
continue; |
135 |
} |
136 |
switch (argv[i][1]) { |
137 |
case 'n': /* number of cores */ |
138 |
check(2,"i"); |
139 |
nproc = atoi(argv[++i]); |
140 |
if (nproc <= 0) |
141 |
error(USER, "bad number of processes"); |
142 |
break; |
143 |
case 'x': /* x resolution */ |
144 |
check(2,"i"); |
145 |
hresolu = atoi(argv[++i]); |
146 |
break; |
147 |
case 'y': /* y resolution */ |
148 |
check(2,"i"); |
149 |
vresolu = atoi(argv[++i]); |
150 |
break; |
151 |
case 'w': /* warnings */ |
152 |
rval = erract[WARNING].pf != NULL; |
153 |
check_bool(2,rval); |
154 |
if (rval) erract[WARNING].pf = wputs; |
155 |
else erract[WARNING].pf = NULL; |
156 |
break; |
157 |
case 'e': /* error file */ |
158 |
check(2,"s"); |
159 |
errfile = argv[++i]; |
160 |
break; |
161 |
case 'l': /* limit distance */ |
162 |
if (argv[i][2] != 'd') |
163 |
goto badopt; |
164 |
check_bool(3,lim_dist); |
165 |
break; |
166 |
case 'I': /* immed. irradiance */ |
167 |
check_bool(2,imm_irrad); |
168 |
break; |
169 |
case 'f': /* format i/o */ |
170 |
switch (argv[i][2]) { |
171 |
case 'a': /* ascii */ |
172 |
case 'f': /* float */ |
173 |
case 'd': /* double */ |
174 |
inform = argv[i][2]; |
175 |
break; |
176 |
default: |
177 |
goto badopt; |
178 |
} |
179 |
switch (argv[i][3]) { |
180 |
case '\0': |
181 |
outform = inform; |
182 |
break; |
183 |
case 'a': /* ascii */ |
184 |
case 'f': /* float */ |
185 |
case 'd': /* double */ |
186 |
case 'c': /* color */ |
187 |
check(4,""); |
188 |
outform = argv[i][3]; |
189 |
break; |
190 |
default: |
191 |
goto badopt; |
192 |
} |
193 |
break; |
194 |
case 'o': /* output */ |
195 |
outvals = argv[i]+2; |
196 |
break; |
197 |
case 'h': /* header output */ |
198 |
rval = loadflags & IO_INFO; |
199 |
check_bool(2,rval); |
200 |
loadflags = rval ? loadflags | IO_INFO : |
201 |
loadflags & ~IO_INFO; |
202 |
break; |
203 |
case 't': /* trace */ |
204 |
switch (argv[i][2]) { |
205 |
case 'i': /* include */ |
206 |
case 'I': |
207 |
check(3,"s"); |
208 |
if (traincl != 1) { |
209 |
traincl = 1; |
210 |
tralp = tralist; |
211 |
} |
212 |
if (argv[i][2] == 'I') { /* file */ |
213 |
rval = wordfile(tralp, MAXMODLIST-(tralp-tralist), |
214 |
getpath(argv[++i],getrlibpath(),R_OK)); |
215 |
if (rval < 0) { |
216 |
sprintf(errmsg, |
217 |
"cannot open trace include file \"%s\"", |
218 |
argv[i]); |
219 |
error(SYSTEM, errmsg); |
220 |
} |
221 |
tralp += rval; |
222 |
} else { |
223 |
*tralp++ = argv[++i]; |
224 |
*tralp = NULL; |
225 |
} |
226 |
break; |
227 |
case 'e': /* exclude */ |
228 |
case 'E': |
229 |
check(3,"s"); |
230 |
if (traincl != 0) { |
231 |
traincl = 0; |
232 |
tralp = tralist; |
233 |
} |
234 |
if (argv[i][2] == 'E') { /* file */ |
235 |
rval = wordfile(tralp, MAXMODLIST-(tralp-tralist), |
236 |
getpath(argv[++i],getrlibpath(),R_OK)); |
237 |
if (rval < 0) { |
238 |
sprintf(errmsg, |
239 |
"cannot open trace exclude file \"%s\"", |
240 |
argv[i]); |
241 |
error(SYSTEM, errmsg); |
242 |
} |
243 |
tralp += rval; |
244 |
} else { |
245 |
*tralp++ = argv[++i]; |
246 |
*tralp = NULL; |
247 |
} |
248 |
break; |
249 |
default: |
250 |
goto badopt; |
251 |
} |
252 |
break; |
253 |
#ifdef PERSIST |
254 |
case 'P': /* persist file */ |
255 |
if (argv[i][2] == 'P') { |
256 |
check(3,"s"); |
257 |
persist = PARALLEL; |
258 |
} else { |
259 |
check(2,"s"); |
260 |
persist = PERSIST; |
261 |
} |
262 |
persistfile(argv[++i]); |
263 |
break; |
264 |
#endif |
265 |
default: |
266 |
goto badopt; |
267 |
} |
268 |
} |
269 |
if (nproc > 1 && persist) |
270 |
error(USER, "multiprocessing incompatible with persist file"); |
271 |
/* initialize object types */ |
272 |
initotypes(); |
273 |
/* initialize urand */ |
274 |
if (rand_samp) { |
275 |
srandom((long)time(0)); |
276 |
initurand(0); |
277 |
} else { |
278 |
srandom(0L); |
279 |
initurand(2048); |
280 |
} |
281 |
/* set up signal handling */ |
282 |
sigdie(SIGINT, "Interrupt"); |
283 |
#ifdef SIGHUP |
284 |
sigdie(SIGHUP, "Hangup"); |
285 |
#endif |
286 |
sigdie(SIGTERM, "Terminate"); |
287 |
#ifdef SIGPIPE |
288 |
sigdie(SIGPIPE, "Broken pipe"); |
289 |
#endif |
290 |
#ifdef SIGALRM |
291 |
sigdie(SIGALRM, "Alarm clock"); |
292 |
#endif |
293 |
#ifdef SIGXCPU |
294 |
sigdie(SIGXCPU, "CPU limit exceeded"); |
295 |
sigdie(SIGXFSZ, "File size exceeded"); |
296 |
#endif |
297 |
/* open error file */ |
298 |
if (errfile != NULL) { |
299 |
if (freopen(errfile, "a", stderr) == NULL) |
300 |
quit(2); |
301 |
fprintf(stderr, "**************\n*** PID %5d: ", |
302 |
getpid()); |
303 |
printargs(argc, argv, stderr); |
304 |
putc('\n', stderr); |
305 |
fflush(stderr); |
306 |
} |
307 |
#ifdef NICE |
308 |
nice(NICE); /* lower priority */ |
309 |
#endif |
310 |
/* get octree */ |
311 |
if (i == argc) |
312 |
octnm = NULL; |
313 |
else if (i == argc-1) |
314 |
octnm = argv[i]; |
315 |
else |
316 |
goto badopt; |
317 |
if (octnm == NULL) |
318 |
error(USER, "missing octree argument"); |
319 |
/* set up output */ |
320 |
#ifdef PERSIST |
321 |
if (persist) { |
322 |
duped1 = dup(fileno(stdout)); /* don't lose our output */ |
323 |
openheader(); |
324 |
} |
325 |
#endif |
326 |
if (outform != 'a') |
327 |
SET_FILE_BINARY(stdout); |
328 |
rval = setrtoutput(); |
329 |
readoct(octname = octnm, loadflags, &thescene, NULL); |
330 |
nsceneobjs = nobjects; |
331 |
|
332 |
if (loadflags & IO_INFO) { /* print header */ |
333 |
printargs(i, argv, stdout); |
334 |
printf("SOFTWARE= %s\n", VersionID); |
335 |
fputnow(stdout); |
336 |
if (rval > 0) /* saved from setrtoutput() call */ |
337 |
printf("NCOMP=%d\n", rval); |
338 |
if ((outform == 'f') | (outform == 'd')) |
339 |
fputendian(stdout); |
340 |
fputformat(formstr(outform), stdout); |
341 |
putchar('\n'); |
342 |
} |
343 |
|
344 |
if (!castonly) { /* any actual ray traversal to do? */ |
345 |
|
346 |
ray_init_pmap(); /* PMAP: set up & load photon maps */ |
347 |
|
348 |
marksources(); /* find and mark sources */ |
349 |
|
350 |
setambient(); /* initialize ambient calculation */ |
351 |
} else |
352 |
distantsources(); /* else mark only distant sources */ |
353 |
|
354 |
fflush(stdout); /* in case we're duplicating header */ |
355 |
|
356 |
#ifdef PERSIST |
357 |
if (persist) { |
358 |
/* reconnect stdout */ |
359 |
dup2(duped1, fileno(stdout)); |
360 |
close(duped1); |
361 |
if (persist == PARALLEL) { /* multiprocessing */ |
362 |
preload_objs(); /* preload scene */ |
363 |
shm_boundary = (char *)malloc(16); |
364 |
strcpy(shm_boundary, "SHM_BOUNDARY"); |
365 |
while ((rval=fork()) == 0) { /* keep on forkin' */ |
366 |
pflock(1); |
367 |
pfhold(); |
368 |
ambsync(); /* load new values */ |
369 |
} |
370 |
if (rval < 0) |
371 |
error(SYSTEM, "cannot fork child for persist function"); |
372 |
pfdetach(); /* parent will run then exit */ |
373 |
} |
374 |
} |
375 |
runagain: |
376 |
if (persist) |
377 |
dupheader(); /* send header to stdout */ |
378 |
#endif |
379 |
/* trace rays */ |
380 |
rtrace(NULL, nproc); |
381 |
/* flush ambient file */ |
382 |
ambsync(); |
383 |
#ifdef PERSIST |
384 |
if (persist == PERSIST) { /* first run-through */ |
385 |
if ((rval=fork()) == 0) { /* child loops until killed */ |
386 |
pflock(1); |
387 |
persist = PCHILD; |
388 |
} else { /* original process exits */ |
389 |
if (rval < 0) |
390 |
error(SYSTEM, "cannot fork child for persist function"); |
391 |
pfdetach(); /* parent exits */ |
392 |
} |
393 |
} |
394 |
if (persist == PCHILD) { /* wait for a signal then go again */ |
395 |
pfhold(); |
396 |
raynum = nrays = 0; /* reinitialize */ |
397 |
goto runagain; |
398 |
} |
399 |
#endif |
400 |
|
401 |
ray_done_pmap(); /* PMAP: free photon maps */ |
402 |
|
403 |
quit(0); |
404 |
|
405 |
badopt: |
406 |
sprintf(errmsg, "command line error at '%s'", argv[i]); |
407 |
error(USER, errmsg); |
408 |
return 1; /* pro forma return */ |
409 |
|
410 |
#undef check |
411 |
#undef check_bool |
412 |
} |
413 |
|
414 |
|
415 |
void |
416 |
wputs( /* warning output function */ |
417 |
char *s |
418 |
) |
419 |
{ |
420 |
int lasterrno = errno; |
421 |
eputs(s); |
422 |
errno = lasterrno; |
423 |
} |
424 |
|
425 |
|
426 |
void |
427 |
eputs( /* put string to stderr */ |
428 |
char *s |
429 |
) |
430 |
{ |
431 |
static int midline = 0; |
432 |
|
433 |
if (!*s) |
434 |
return; |
435 |
if (!midline++) { |
436 |
fputs(progname, stderr); |
437 |
fputs(": ", stderr); |
438 |
} |
439 |
fputs(s, stderr); |
440 |
if (s[strlen(s)-1] == '\n') { |
441 |
fflush(stderr); |
442 |
midline = 0; |
443 |
} |
444 |
} |
445 |
|
446 |
|
447 |
static void |
448 |
onsig( /* fatal signal */ |
449 |
int signo |
450 |
) |
451 |
{ |
452 |
static int gotsig = 0; |
453 |
|
454 |
if (gotsig++) /* two signals and we're gone! */ |
455 |
_exit(signo); |
456 |
|
457 |
#ifdef SIGALRM |
458 |
alarm(15); /* allow 15 seconds to clean up */ |
459 |
signal(SIGALRM, SIG_DFL); /* make certain we do die */ |
460 |
#endif |
461 |
eputs("signal - "); |
462 |
eputs(sigerr[signo]); |
463 |
eputs("\n"); |
464 |
quit(3); |
465 |
} |
466 |
|
467 |
|
468 |
static void |
469 |
sigdie( /* set fatal signal */ |
470 |
int signo, |
471 |
char *msg |
472 |
) |
473 |
{ |
474 |
if (signal(signo, onsig) == SIG_IGN) |
475 |
signal(signo, SIG_IGN); |
476 |
sigerr[signo] = msg; |
477 |
} |
478 |
|
479 |
|
480 |
static void |
481 |
printdefaults(void) /* print default values to stdout */ |
482 |
{ |
483 |
char *cp; |
484 |
|
485 |
if (imm_irrad) |
486 |
printf("-I+\t\t\t\t# immediate irradiance on\n"); |
487 |
printf("-n %-2d\t\t\t\t# number of rendering processes\n", nproc); |
488 |
printf("-x %-9d\t\t\t# %s\n", hresolu, |
489 |
vresolu && hresolu ? "x resolution" : "flush interval"); |
490 |
printf("-y %-9d\t\t\t# y resolution\n", vresolu); |
491 |
printf(lim_dist ? "-ld+\t\t\t\t# limit distance on\n" : |
492 |
"-ld-\t\t\t\t# limit distance off\n"); |
493 |
printf("-h%c\t\t\t\t# %s header\n", loadflags & IO_INFO ? '+' : '-', |
494 |
loadflags & IO_INFO ? "output" : "no"); |
495 |
printf("-f%c%c\t\t\t\t# format input/output = %s/%s\n", |
496 |
inform, outform, formstr(inform), formstr(outform)); |
497 |
printf("-o%-9s\t\t\t# output", outvals); |
498 |
for (cp = outvals; *cp; cp++) |
499 |
switch (*cp) { |
500 |
case 't': case 'T': printf(" trace"); break; |
501 |
case 'o': printf(" origin"); break; |
502 |
case 'd': printf(" direction"); break; |
503 |
case 'r': printf(" reflect_contrib"); break; |
504 |
case 'R': printf(" reflect_length"); break; |
505 |
case 'x': printf(" unreflect_contrib"); break; |
506 |
case 'X': printf(" unreflect_length"); break; |
507 |
case 'v': printf(" value"); break; |
508 |
case 'V': printf(" contribution"); break; |
509 |
case 'l': printf(" length"); break; |
510 |
case 'L': printf(" first_length"); break; |
511 |
case 'p': printf(" point"); break; |
512 |
case 'n': printf(" normal"); break; |
513 |
case 'N': printf(" unperturbed_normal"); break; |
514 |
case 's': printf(" surface"); break; |
515 |
case 'w': printf(" weight"); break; |
516 |
case 'W': printf(" coefficient"); break; |
517 |
case 'm': printf(" modifier"); break; |
518 |
case 'M': printf(" material"); break; |
519 |
case '~': printf(" tilde"); break; |
520 |
} |
521 |
putchar('\n'); |
522 |
printf(erract[WARNING].pf != NULL ? |
523 |
"-w+\t\t\t\t# warning messages on\n" : |
524 |
"-w-\t\t\t\t# warning messages off\n"); |
525 |
print_rdefaults(); |
526 |
} |