1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: rpmain.c,v 2.36 2024/11/15 20:47:42 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* rpmain.c - main for rpict batch rendering program |
6 |
*/ |
7 |
|
8 |
#include "copyright.h" |
9 |
|
10 |
#include <time.h> |
11 |
#include <signal.h> |
12 |
|
13 |
#include "platform.h" |
14 |
#include "rtprocess.h" /* getpid() */ |
15 |
#include "ray.h" |
16 |
#include "source.h" |
17 |
#include "ambient.h" |
18 |
#include "random.h" |
19 |
#include "paths.h" |
20 |
#include "view.h" |
21 |
#include "pmapray.h" |
22 |
|
23 |
/* persistent processes define */ |
24 |
#ifdef F_SETLKW |
25 |
#define PERSIST 1 /* normal persist */ |
26 |
#define PARALLEL 2 /* parallel persist */ |
27 |
#define PCHILD 3 /* child of normal persist */ |
28 |
#endif |
29 |
|
30 |
char *progname; /* argv[0] */ |
31 |
char *octname; /* octree name */ |
32 |
char *sigerr[NSIG]; /* signal error messages */ |
33 |
char *errfile = NULL; /* error output file */ |
34 |
|
35 |
extern time_t time(); |
36 |
extern time_t tstart; /* start time */ |
37 |
|
38 |
extern int ralrm; /* seconds between reports */ |
39 |
|
40 |
extern VIEW ourview; /* viewing parameters */ |
41 |
|
42 |
extern int hresolu; /* horizontal resolution */ |
43 |
extern int vresolu; /* vertical resolution */ |
44 |
extern double pixaspect; /* pixel aspect ratio */ |
45 |
|
46 |
extern int psample; /* pixel sample size */ |
47 |
extern double maxdiff; /* max. sample difference */ |
48 |
extern double dstrpix; /* square pixel distribution */ |
49 |
|
50 |
extern double mblur; /* motion blur parameter */ |
51 |
|
52 |
extern double dblur; /* depth-of-field blur parameter */ |
53 |
|
54 |
RGBPRIMP out_prims = stdprims; /* output color primitives */ |
55 |
static RGBPRIMS our_prims; /* private output color primitives */ |
56 |
|
57 |
static void onsig(int signo); |
58 |
static void sigdie(int signo, char *msg); |
59 |
static void printdefaults(void); |
60 |
/* rpict additional features */ |
61 |
#ifdef PERSIST |
62 |
#define RPICT_FEATURES "Persist\nParallelPersist\n" \ |
63 |
"ParticipatingMedia=Mist\n" \ |
64 |
"Recovery\nIrradianceCalc\nViewTypes=v,l,a,h,s,c\n" \ |
65 |
"HessianAmbientCache\nAmbientAveraging\nAmbientValueSharing\n" \ |
66 |
"PixelJitter\nPixelSampling\nPixelMotion\nPixelDepthOfField\n" \ |
67 |
"SmallSourceDrawing\nViewSequence\nProgressReporting\n" \ |
68 |
"AdaptiveShadowTesting\nOutputs=v,l\n" \ |
69 |
"OutputCS=RGB,XYZ,prims\n" |
70 |
#else |
71 |
#define RPICT_FEATURES "Recovery\nIrradianceCalc\nViewTypes=v,l,a,h,s,c\n" \ |
72 |
"ParticipatingMedia=Mist\n" \ |
73 |
"HessianAmbientCache\nAmbientAveraging\nAmbientValueSharing\n" \ |
74 |
"PixelJitter\nPixelSampling\nPixelMotion\nPixelDepthOfField\n" \ |
75 |
"SmallSourceDrawing\nViewSequence\nProgressReporting\n" \ |
76 |
"AdaptiveShadowTesting\nOutputs=v,l\n" \ |
77 |
"OutputCS=RGB,XYZ,prims\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 |
char *err; |
95 |
char *recover = NULL; |
96 |
char *outfile = NULL; |
97 |
char *zfile = NULL; |
98 |
int loadflags = ~IO_FILES; |
99 |
int seqstart = 0; |
100 |
int persist = 0; |
101 |
int duped1 = -1; |
102 |
int rval; |
103 |
int i; |
104 |
/* record start time */ |
105 |
tstart = time((time_t *)NULL); |
106 |
/* global program name */ |
107 |
progname = argv[0] = fixargv0(argv[0]); |
108 |
/* feature check only? */ |
109 |
strcat(RFeatureList, RPICT_FEATURES); |
110 |
if (argc > 1 && !strcmp(argv[1], "-features")) |
111 |
return feature_status(argc-2, argv+2); |
112 |
/* option city */ |
113 |
for (i = 1; i < argc; i++) { |
114 |
/* expand arguments */ |
115 |
while ((rval = expandarg(&argc, &argv, i)) > 0) |
116 |
; |
117 |
if (rval < 0) { |
118 |
sprintf(errmsg, "cannot expand '%s'", argv[i]); |
119 |
error(SYSTEM, errmsg); |
120 |
} |
121 |
if (argv[i] == NULL || argv[i][0] != '-') |
122 |
break; /* break from options */ |
123 |
if (!strcmp(argv[i], "-version")) { |
124 |
puts(VersionID); |
125 |
quit(0); |
126 |
} |
127 |
if (!strcmp(argv[i], "-defaults") || |
128 |
!strcmp(argv[i], "-help")) { |
129 |
printdefaults(); |
130 |
quit(0); |
131 |
} |
132 |
rval = getrenderopt(argc-i, argv+i); |
133 |
if (rval >= 0) { |
134 |
i += rval; |
135 |
continue; |
136 |
} |
137 |
rval = getviewopt(&ourview, argc-i, argv+i); |
138 |
if (rval >= 0) { |
139 |
i += rval; |
140 |
continue; |
141 |
} |
142 |
/* rpict options */ |
143 |
switch (argv[i][1]) { |
144 |
case 'v': /* view file */ |
145 |
if (argv[i][2] != 'f') |
146 |
goto badopt; |
147 |
check(3,"s"); |
148 |
rval = viewfile(argv[++i], &ourview, NULL); |
149 |
if (rval < 0) { |
150 |
sprintf(errmsg, |
151 |
"cannot open view file \"%s\"", |
152 |
argv[i]); |
153 |
error(SYSTEM, errmsg); |
154 |
} else if (rval == 0) { |
155 |
sprintf(errmsg, |
156 |
"bad view file \"%s\"", |
157 |
argv[i]); |
158 |
error(USER, errmsg); |
159 |
} |
160 |
break; |
161 |
case 'p': /* pixel */ |
162 |
switch (argv[i][2]) { |
163 |
case 's': /* sample */ |
164 |
check(3,"i"); |
165 |
psample = atoi(argv[++i]); |
166 |
break; |
167 |
case 't': /* threshold */ |
168 |
check(3,"f"); |
169 |
maxdiff = atof(argv[++i]); |
170 |
break; |
171 |
case 'j': /* jitter */ |
172 |
check(3,"f"); |
173 |
dstrpix = atof(argv[++i]); |
174 |
break; |
175 |
case 'a': /* aspect */ |
176 |
check(3,"f"); |
177 |
pixaspect = atof(argv[++i]); |
178 |
break; |
179 |
case 'm': /* motion */ |
180 |
check(3,"f"); |
181 |
mblur = atof(argv[++i]); |
182 |
break; |
183 |
case 'd': /* aperture */ |
184 |
check(3,"f"); |
185 |
dblur = atof(argv[++i]); |
186 |
break; |
187 |
case 'R': /* standard RGB output */ |
188 |
if (strcmp(argv[i]+2, "RGB")) |
189 |
goto badopt; |
190 |
out_prims = stdprims; |
191 |
break; |
192 |
case 'X': /* XYZ output */ |
193 |
if (strcmp(argv[i]+2, "XYZ")) |
194 |
goto badopt; |
195 |
out_prims = xyzprims; |
196 |
break; |
197 |
case 'c': { /* chromaticities */ |
198 |
int j; |
199 |
check(3,"ffffffff"); |
200 |
rval = 0; |
201 |
for (j = 0; j < 8; j++) { |
202 |
our_prims[0][j] = atof(argv[++i]); |
203 |
rval |= fabs(our_prims[0][j]-stdprims[0][j]) > .001; |
204 |
} |
205 |
if (rval) { |
206 |
if (!colorprimsOK(our_prims)) |
207 |
error(USER, "illegal primary chromaticities"); |
208 |
out_prims = our_prims; |
209 |
} else |
210 |
out_prims = stdprims; |
211 |
} break; |
212 |
default: |
213 |
goto badopt; |
214 |
} |
215 |
break; |
216 |
case 'x': /* x resolution */ |
217 |
check(2,"i"); |
218 |
hresolu = atoi(argv[++i]); |
219 |
break; |
220 |
case 'y': /* y resolution */ |
221 |
check(2,"i"); |
222 |
vresolu = atoi(argv[++i]); |
223 |
break; |
224 |
case 'S': /* slave index */ |
225 |
check(2,"i"); |
226 |
seqstart = atoi(argv[++i]); |
227 |
break; |
228 |
case 'o': /* output file */ |
229 |
check(2,"s"); |
230 |
outfile = argv[++i]; |
231 |
break; |
232 |
case 'z': /* z file */ |
233 |
check(2,"s"); |
234 |
zfile = argv[++i]; |
235 |
break; |
236 |
case 'r': /* recover file */ |
237 |
if (argv[i][2] == 'o') { /* +output */ |
238 |
check(3,"s"); |
239 |
outfile = argv[i+1]; |
240 |
} else |
241 |
check(2,"s"); |
242 |
recover = argv[++i]; |
243 |
break; |
244 |
#ifdef PERSIST |
245 |
case 'P': /* persist file */ |
246 |
if (argv[i][2] == 'P') { |
247 |
check(3,"s"); |
248 |
persist = PARALLEL; |
249 |
} else { |
250 |
check(2,"s"); |
251 |
persist = PERSIST; |
252 |
} |
253 |
persistfile(argv[++i]); |
254 |
break; |
255 |
#endif |
256 |
case 't': /* timer */ |
257 |
check(2,"i"); |
258 |
ralrm = atoi(argv[++i]); |
259 |
break; |
260 |
case 'w': /* warnings */ |
261 |
rval = erract[WARNING].pf != NULL; |
262 |
check_bool(2,rval); |
263 |
if (rval) erract[WARNING].pf = wputs; |
264 |
else erract[WARNING].pf = NULL; |
265 |
break; |
266 |
case 'e': /* error file */ |
267 |
check(2,"s"); |
268 |
errfile = argv[++i]; |
269 |
break; |
270 |
default: |
271 |
goto badopt; |
272 |
} |
273 |
} |
274 |
/* set/check spectral sampling */ |
275 |
if (setspectrsamp(CNDX, WLPART) <= 0) |
276 |
error(USER, "unsupported spectral sampling"); |
277 |
|
278 |
err = setview(&ourview); /* set viewing parameters */ |
279 |
if (err != NULL) |
280 |
error(USER, err); |
281 |
/* initialize object types */ |
282 |
initotypes(); |
283 |
/* initialize urand */ |
284 |
if (rand_samp) { |
285 |
srandom((long)time(0)); |
286 |
initurand(0); |
287 |
} else { |
288 |
srandom(0L); |
289 |
initurand(2048); |
290 |
} |
291 |
/* set up signal handling */ |
292 |
sigdie(SIGINT, "Interrupt"); |
293 |
#ifdef SIGHUP |
294 |
sigdie(SIGHUP, "Hangup"); |
295 |
#endif |
296 |
sigdie(SIGTERM, "Terminate"); |
297 |
#ifdef SIGPIPE |
298 |
sigdie(SIGPIPE, "Broken pipe"); |
299 |
#endif |
300 |
#ifdef SIGALRM |
301 |
sigdie(SIGALRM, "Alarm clock"); |
302 |
#endif |
303 |
#ifdef SIGXCPU |
304 |
sigdie(SIGXCPU, "CPU limit exceeded"); |
305 |
sigdie(SIGXFSZ, "File size exceeded"); |
306 |
#endif |
307 |
/* open error file */ |
308 |
if (errfile != NULL) { |
309 |
if (freopen(errfile, "a", stderr) == NULL) |
310 |
quit(2); |
311 |
fprintf(stderr, "**************\n*** PID %5d: ", |
312 |
getpid()); |
313 |
printargs(argc, argv, stderr); |
314 |
putc('\n', stderr); |
315 |
fflush(stderr); |
316 |
} |
317 |
#ifdef NICE |
318 |
nice(NICE); /* lower priority */ |
319 |
#endif |
320 |
/* get octree */ |
321 |
if (i == argc) |
322 |
octname = NULL; |
323 |
else if (i == argc-1) |
324 |
octname = argv[i]; |
325 |
else |
326 |
goto badopt; |
327 |
if (seqstart > 0 && octname == NULL) |
328 |
error(USER, "missing octree argument"); |
329 |
/* set up output */ |
330 |
#ifdef PERSIST |
331 |
if (persist) { |
332 |
if (recover != NULL) |
333 |
error(USER, "persist option used with recover file"); |
334 |
if (seqstart <= 0) |
335 |
error(USER, "persist option only for sequences"); |
336 |
if (outfile == NULL) |
337 |
duped1 = dup(fileno(stdout)); /* don't lose our output */ |
338 |
openheader(); |
339 |
} else |
340 |
#endif |
341 |
if (outfile != NULL) |
342 |
openheader(); |
343 |
SET_FILE_BINARY(stdout); |
344 |
if (octname == NULL) |
345 |
SET_FILE_BINARY(stdin); |
346 |
readoct(octname, loadflags, &thescene, NULL); |
347 |
nsceneobjs = nobjects; |
348 |
|
349 |
if (loadflags & IO_INFO) { /* print header */ |
350 |
printargs(i, argv, stdout); |
351 |
printf("SOFTWARE= %s\n", VersionID); |
352 |
} |
353 |
|
354 |
ray_init_pmap(); /* PMAP: set up & load photon maps */ |
355 |
|
356 |
marksources(); /* find and mark sources */ |
357 |
setambient(); /* initialize ambient calculation */ |
358 |
|
359 |
fflush(stdout); /* in case we're duplicating header */ |
360 |
|
361 |
#ifdef PERSIST |
362 |
if (persist) { |
363 |
if (outfile == NULL) { /* reconnect stdout */ |
364 |
dup2(duped1, fileno(stdout)); |
365 |
close(duped1); |
366 |
} |
367 |
if (persist == PARALLEL) { /* multiprocessing */ |
368 |
cow_memshare(); /* preloads scene */ |
369 |
while ((rval=fork()) == 0) { /* keep on forkin' */ |
370 |
pflock(1); |
371 |
pfhold(); |
372 |
tstart = time((time_t *)NULL); |
373 |
ambsync(); /* load new values */ |
374 |
} |
375 |
if (rval < 0) |
376 |
error(SYSTEM, "cannot fork child for persist function"); |
377 |
pfdetach(); /* parent will run then exit */ |
378 |
} |
379 |
} |
380 |
runagain: |
381 |
if (persist) { |
382 |
if (outfile == NULL) /* if out to stdout */ |
383 |
dupheader(); /* send header */ |
384 |
else /* if out to file */ |
385 |
duped1 = dup(fileno(stdout)); /* hang onto pipe */ |
386 |
} |
387 |
#endif |
388 |
/* batch render picture(s) */ |
389 |
rpict(seqstart, outfile, zfile, recover); |
390 |
/* flush ambient file */ |
391 |
ambsync(); |
392 |
#ifdef PERSIST |
393 |
if (persist == PERSIST) { /* first run-through */ |
394 |
if ((rval=fork()) == 0) { /* child loops until killed */ |
395 |
pflock(1); |
396 |
persist = PCHILD; |
397 |
} else { /* original process exits */ |
398 |
if (rval < 0) |
399 |
error(SYSTEM, "cannot fork child for persist function"); |
400 |
pfdetach(); /* parent exits */ |
401 |
} |
402 |
} |
403 |
if (persist == PCHILD) { /* wait for a signal then go again */ |
404 |
if (outfile != NULL) |
405 |
close(duped1); /* release output handle */ |
406 |
pfhold(); |
407 |
tstart = time((time_t *)NULL); /* reinitialize */ |
408 |
raynum = nrays = 0; |
409 |
goto runagain; |
410 |
} |
411 |
#endif |
412 |
|
413 |
|
414 |
ray_done_pmap(); /* PMAP: free photon maps */ |
415 |
|
416 |
quit(0); |
417 |
|
418 |
badopt: |
419 |
sprintf(errmsg, "command line error at '%s'", argv[i]); |
420 |
error(USER, errmsg); |
421 |
return 1; /* pro forma return */ |
422 |
|
423 |
#undef check |
424 |
#undef check_bool |
425 |
} |
426 |
|
427 |
|
428 |
void |
429 |
wputs( /* warning output function */ |
430 |
const char *s |
431 |
) |
432 |
{ |
433 |
int lasterrno = errno; |
434 |
if (erract[WARNING].pf == NULL) |
435 |
return; /* called by calcomp or someone */ |
436 |
eputs(s); |
437 |
errno = lasterrno; |
438 |
} |
439 |
|
440 |
|
441 |
void |
442 |
eputs( /* put string to stderr */ |
443 |
const char *s |
444 |
) |
445 |
{ |
446 |
static int midline = 0; |
447 |
|
448 |
if (!*s) |
449 |
return; |
450 |
if (!midline++) { |
451 |
fputs(progname, stderr); |
452 |
fputs(": ", stderr); |
453 |
} |
454 |
fputs(s, stderr); |
455 |
if (s[strlen(s)-1] == '\n') { |
456 |
fflush(stderr); |
457 |
midline = 0; |
458 |
} |
459 |
} |
460 |
|
461 |
|
462 |
static void |
463 |
onsig( /* fatal signal */ |
464 |
int signo |
465 |
) |
466 |
{ |
467 |
static int gotsig = 0; |
468 |
|
469 |
if (gotsig++) /* two signals and we're gone! */ |
470 |
_exit(signo); |
471 |
|
472 |
#ifdef SIGALRM /* XXX how critical is this? */ |
473 |
alarm(15); /* allow 15 seconds to clean up */ |
474 |
signal(SIGALRM, SIG_DFL); /* make certain we do die */ |
475 |
#endif |
476 |
eputs("signal - "); |
477 |
eputs(sigerr[signo]); |
478 |
eputs("\n"); |
479 |
quit(3); |
480 |
} |
481 |
|
482 |
|
483 |
static void |
484 |
sigdie( /* set fatal signal */ |
485 |
int signo, |
486 |
char *msg |
487 |
) |
488 |
{ |
489 |
if (signal(signo, onsig) == SIG_IGN) |
490 |
signal(signo, SIG_IGN); |
491 |
sigerr[signo] = msg; |
492 |
} |
493 |
|
494 |
|
495 |
static void |
496 |
printdefaults(void) /* print default values to stdout */ |
497 |
{ |
498 |
printf("-vt%c\t\t\t\t# view type %s\n", ourview.type, |
499 |
ourview.type==VT_PER ? "perspective" : |
500 |
ourview.type==VT_PAR ? "parallel" : |
501 |
ourview.type==VT_HEM ? "hemispherical" : |
502 |
ourview.type==VT_ANG ? "angular" : |
503 |
ourview.type==VT_CYL ? "cylindrical" : |
504 |
ourview.type==VT_PLS ? "planisphere" : |
505 |
"unknown"); |
506 |
printf("-vp %f %f %f\t# view point\n", |
507 |
ourview.vp[0], ourview.vp[1], ourview.vp[2]); |
508 |
printf("-vd %f %f %f\t# view direction\n", |
509 |
ourview.vdir[0], ourview.vdir[1], ourview.vdir[2]); |
510 |
printf("-vu %f %f %f\t# view up\n", |
511 |
ourview.vup[0], ourview.vup[1], ourview.vup[2]); |
512 |
printf("-vh %f\t\t\t# view horizontal size\n", ourview.horiz); |
513 |
printf("-vv %f\t\t\t# view vertical size\n", ourview.vert); |
514 |
printf("-vo %f\t\t\t# view fore clipping plane\n", ourview.vfore); |
515 |
printf("-va %f\t\t\t# view aft clipping plane\n", ourview.vaft); |
516 |
printf("-vs %f\t\t\t# view shift\n", ourview.hoff); |
517 |
printf("-vl %f\t\t\t# view lift\n", ourview.voff); |
518 |
printf("-x %-9d\t\t\t# x resolution\n", hresolu); |
519 |
printf("-y %-9d\t\t\t# y resolution\n", vresolu); |
520 |
if (out_prims == stdprims) |
521 |
printf("-pRGB\t\t\t\t# standard RGB color output\n"); |
522 |
else if (out_prims == xyzprims) |
523 |
printf("-pXYZ\t\t\t\t# CIE XYZ color output\n"); |
524 |
else if (out_prims != NULL) |
525 |
printf("-pc %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f\t# output color primaries and white point\n", |
526 |
out_prims[RED][0], out_prims[RED][1], |
527 |
out_prims[GRN][0], out_prims[GRN][1], |
528 |
out_prims[BLU][0], out_prims[BLU][1], |
529 |
out_prims[WHT][0], out_prims[WHT][1]); |
530 |
printf("-pa %f\t\t\t# pixel aspect ratio\n", pixaspect); |
531 |
printf("-pj %f\t\t\t# pixel jitter\n", dstrpix); |
532 |
printf("-pm %f\t\t\t# pixel motion\n", mblur); |
533 |
printf("-pd %f\t\t\t# pixel depth-of-field\n", dblur); |
534 |
printf("-ps %-9d\t\t\t# pixel sample\n", psample); |
535 |
printf("-pt %f\t\t\t# pixel threshold\n", maxdiff); |
536 |
printf("-t %-9d\t\t\t# time between reports\n", ralrm); |
537 |
printf(erract[WARNING].pf != NULL ? |
538 |
"-w+\t\t\t\t# warning messages on\n" : |
539 |
"-w-\t\t\t\t# warning messages off\n"); |
540 |
print_rdefaults(); |
541 |
} |