ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtmain.c
Revision: 2.38
Committed: Wed Oct 19 21:25:20 2022 UTC (18 months, 3 weeks ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.37: +16 -3 lines
Log Message:
feat(rpict,rtrace,rcontrib): Added -features option to check for method support

File Contents

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