ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtmain.c
Revision: 2.41
Committed: Thu Feb 2 18:55:09 2023 UTC (15 months, 1 week ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.40: +3 -2 lines
Log Message:
fix: Paired savqstr() and freeqstr() calls for octname

File Contents

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