ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtmain.c
Revision: 2.29
Committed: Thu Apr 18 23:58:22 2019 UTC (5 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.28: +1 -5 lines
Log Message:
Made settings consistent with claims of matching rtrace and added missing rand_samp

File Contents

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