5 |
|
* Make illum sources for optimizing rendering process |
6 |
|
*/ |
7 |
|
|
8 |
– |
#include "mkillum.h" |
9 |
– |
|
8 |
|
#include <signal.h> |
11 |
– |
|
9 |
|
#include <ctype.h> |
10 |
|
|
11 |
+ |
#include "platform.h" |
12 |
+ |
#include "mkillum.h" |
13 |
+ |
#include "random.h" |
14 |
+ |
|
15 |
|
/* default parameters */ |
16 |
|
#define SAMPDENS 48 /* points per projected steradian */ |
17 |
|
#define NSAMPS 32 /* samples per point */ |
25 |
|
|
26 |
|
/* rtrace command and defaults */ |
27 |
|
char *rtargv[64] = { "rtrace", "-dj", ".25", "-dr", "3", "-dv-", |
28 |
< |
"-ab", "2", "-ad", "256", "-as", "128", "-aa", ".15", }; |
28 |
> |
"-ab", "2", "-ad", "1024", "-as", "512", "-aa", ".1", }; |
29 |
|
int rtargc = 14; |
30 |
|
/* overriding rtrace options */ |
31 |
|
char *myrtopts[] = { "-I-", "-i-", "-ld-", "-ov", "-h-", |
32 |
|
"-fff", "-y", "0", NULL }; |
33 |
|
|
34 |
< |
struct rtproc rt; /* our rtrace process */ |
34 |
> |
struct rtproc rt0; /* head of rtrace process list */ |
35 |
|
|
36 |
|
struct illum_args thisillum = { /* our illum and default values */ |
37 |
|
0, |
49 |
|
|
50 |
|
FUN ofun[NUMOTYPE] = INIT_OTYPE; /* object types */ |
51 |
|
|
52 |
+ |
char persistfn[] = "pfXXXXXX"; /* persist file name */ |
53 |
+ |
|
54 |
|
int gargc; /* global argc */ |
55 |
|
char **gargv; /* global argv */ |
56 |
|
#define progname gargv[0] |
60 |
|
|
61 |
|
int warnings = 1; /* print warnings? */ |
62 |
|
|
63 |
+ |
int done_rprocs(struct rtproc *rtp); |
64 |
+ |
void init(int np); |
65 |
+ |
void filter(register FILE *infp, char *name); |
66 |
+ |
void xoptions(char *s, char *nm); |
67 |
+ |
void printopts(void); |
68 |
+ |
void printhead(register int ac, register char **av); |
69 |
+ |
void xobject(FILE *fp, char *nm); |
70 |
|
|
71 |
< |
main(argc, argv) /* compute illum distributions using rtrace */ |
72 |
< |
int argc; |
73 |
< |
char *argv[]; |
71 |
> |
|
72 |
> |
int |
73 |
> |
main( /* compute illum distributions using rtrace */ |
74 |
> |
int argc, |
75 |
> |
char *argv[] |
76 |
> |
) |
77 |
|
{ |
78 |
+ |
int nprocs = 1; |
79 |
|
char *rtpath; |
80 |
|
FILE *fp; |
81 |
|
register int i; |
82 |
|
/* set global arguments */ |
83 |
|
gargv = argv; |
84 |
+ |
/* check for -n option */ |
85 |
+ |
if (!strcmp(argv[1], "-n")) { |
86 |
+ |
nprocs = atoi(argv[2]); |
87 |
+ |
if (nprocs <= 0) |
88 |
+ |
error(USER, "illegal number of processes"); |
89 |
+ |
i = 3; |
90 |
+ |
} else |
91 |
+ |
i = 1; |
92 |
|
/* set up rtrace command */ |
93 |
< |
for (i = 1; i < argc; i++) { |
93 |
> |
for ( ; i < argc; i++) { |
94 |
|
if (argv[i][0] == '<' && argv[i][1] == '\0') |
95 |
|
break; |
96 |
|
rtargv[rtargc++] = argv[i]; |
114 |
|
} |
115 |
|
} |
116 |
|
gargc = i; |
117 |
< |
rtargc--; |
117 |
> |
if (!strcmp(rtargv[--rtargc], "-defaults")) |
118 |
> |
nprocs = 0; |
119 |
> |
if (nprocs > 1) { /* add persist file if parallel invocation */ |
120 |
> |
rtargv[rtargc++] = "-PP"; |
121 |
> |
rtargv[rtargc++] = mktemp(persistfn); |
122 |
> |
} |
123 |
> |
/* add "mandatory" rtrace options */ |
124 |
|
for (i = 0; myrtopts[i] != NULL; i++) |
125 |
|
rtargv[rtargc++] = myrtopts[i]; |
126 |
+ |
/* finally, put back final argument */ |
127 |
|
rtargv[rtargc++] = argv[gargc-1]; |
128 |
|
rtargv[rtargc] = NULL; |
129 |
< |
/* just asking for defaults? */ |
101 |
< |
if (!strcmp(argv[gargc-1], "-defaults")) { |
129 |
> |
if (!nprocs) { /* just asking for defaults? */ |
130 |
|
printopts(); fflush(stdout); |
131 |
|
rtpath = getpath(rtargv[0], getenv("PATH"), X_OK); |
132 |
|
if (rtpath == NULL) { |
135 |
|
exit(1); |
136 |
|
} |
137 |
|
execv(rtpath, rtargv); |
138 |
< |
perror(rtpath); |
138 |
> |
perror(rtpath); /* execv() should not return */ |
139 |
|
exit(1); |
140 |
|
} |
141 |
|
if (gargc < 2 || argv[gargc-1][0] == '-') |
142 |
|
error(USER, "missing octree argument"); |
143 |
|
/* else initialize and run our calculation */ |
144 |
< |
init(); |
144 |
> |
init(nprocs); |
145 |
|
if (gargc+1 < argc) |
146 |
|
for (i = gargc+1; i < argc; i++) { |
147 |
|
if ((fp = fopen(argv[i], "r")) == NULL) { |
155 |
|
else |
156 |
|
filter(stdin, "standard input"); |
157 |
|
quit(0); |
158 |
+ |
return 0; /* pro forma return */ |
159 |
|
} |
160 |
|
|
161 |
|
|
162 |
+ |
#ifndef SIGALRM |
163 |
+ |
#define SIGALRM SIGTERM |
164 |
+ |
#endif |
165 |
+ |
static void |
166 |
+ |
killpersist(void) /* kill persistent rtrace process */ |
167 |
+ |
{ |
168 |
+ |
FILE *fp = fopen(persistfn, "r"); |
169 |
+ |
int pid; |
170 |
+ |
|
171 |
+ |
if (fp == NULL) |
172 |
+ |
return; |
173 |
+ |
if (fscanf(fp, "%*s %d", &pid) != 1 || kill(pid, SIGALRM) < 0) |
174 |
+ |
unlink(persistfn); |
175 |
+ |
fclose(fp); |
176 |
+ |
} |
177 |
+ |
|
178 |
+ |
|
179 |
+ |
int |
180 |
+ |
done_rprocs(struct rtproc *rtp) |
181 |
+ |
{ |
182 |
+ |
int st0, st1 = 0; |
183 |
+ |
|
184 |
+ |
if (rtp->next != NULL) { /* close last opened first! */ |
185 |
+ |
st1 = done_rprocs(rtp->next); |
186 |
+ |
free((void *)rtp->next); |
187 |
+ |
rtp->next = NULL; |
188 |
+ |
} |
189 |
+ |
st0 = close_process(&rtp->pd); |
190 |
+ |
if (st0 < 0) |
191 |
+ |
error(WARNING, "unknown return status from rtrace process"); |
192 |
+ |
else if (st0 > 0) |
193 |
+ |
return(st0); |
194 |
+ |
return(st1); |
195 |
+ |
} |
196 |
+ |
|
197 |
|
void |
198 |
< |
quit(status) /* exit with status */ |
135 |
< |
int status; |
198 |
> |
quit(int status) /* exit with status */ |
199 |
|
{ |
200 |
|
int rtstat; |
201 |
|
|
202 |
< |
rtstat = close_process(rt.pd); |
202 |
> |
if (rt0.next != NULL) /* terminate persistent rtrace */ |
203 |
> |
killpersist(); |
204 |
> |
/* clean up rtrace process(es) */ |
205 |
> |
rtstat = done_rprocs(&rt0); |
206 |
|
if (status == 0) |
207 |
< |
if (rtstat < 0) |
142 |
< |
error(WARNING, |
143 |
< |
"unknown return status from rtrace process"); |
144 |
< |
else |
145 |
< |
status = rtstat; |
207 |
> |
status = rtstat; |
208 |
|
exit(status); |
209 |
|
} |
210 |
|
|
211 |
< |
|
212 |
< |
init() /* start rtrace and set up buffers */ |
211 |
> |
void |
212 |
> |
init(int np) /* start rtrace and set up buffers */ |
213 |
|
{ |
214 |
< |
extern int o_face(), o_sphere(), o_ring(); |
214 |
> |
struct rtproc *rtp; |
215 |
> |
int i; |
216 |
|
int maxbytes; |
217 |
|
/* set up object functions */ |
218 |
|
ofun[OBJ_FACE].funp = o_face; |
219 |
|
ofun[OBJ_SPHERE].funp = o_sphere; |
220 |
|
ofun[OBJ_RING].funp = o_ring; |
221 |
|
/* set up signal handling */ |
222 |
+ |
signal(SIGINT, quit); |
223 |
+ |
#ifdef SIGHUP |
224 |
+ |
signal(SIGHUP, quit); |
225 |
+ |
#endif |
226 |
+ |
#ifdef SIGTERM |
227 |
+ |
signal(SIGTERM, quit); |
228 |
+ |
#endif |
229 |
+ |
#ifdef SIGPIPE |
230 |
|
signal(SIGPIPE, quit); |
231 |
< |
/* start rtrace process */ |
232 |
< |
errno = 0; |
233 |
< |
maxbytes = open_process(rt.pd, rtargv); |
234 |
< |
if (maxbytes == 0) { |
235 |
< |
eputs(rtargv[0]); |
236 |
< |
eputs(": command not found\n"); |
237 |
< |
exit(1); |
231 |
> |
#endif |
232 |
> |
rtp = &rt0; /* start rtrace process(es) */ |
233 |
> |
for (i = 0; i++ < np; ) { |
234 |
> |
errno = 0; |
235 |
> |
maxbytes = open_process(&rtp->pd, rtargv); |
236 |
> |
if (maxbytes == 0) { |
237 |
> |
eputs(rtargv[0]); |
238 |
> |
eputs(": command not found\n"); |
239 |
> |
exit(1); |
240 |
> |
} |
241 |
> |
if (maxbytes < 0) |
242 |
> |
error(SYSTEM, "cannot start rtrace process"); |
243 |
> |
rtp->bsiz = maxbytes/(6*sizeof(float)); |
244 |
> |
rtp->buf = (float *)malloc(6*sizeof(float)*rtp->bsiz--); |
245 |
> |
rtp->dest = (float **)calloc(rtp->bsiz, sizeof(float *)); |
246 |
> |
if (rtp->buf == NULL || rtp->dest == NULL) |
247 |
> |
error(SYSTEM, "out of memory in init"); |
248 |
> |
rtp->nrays = 0; |
249 |
> |
if (i == np) /* last process? */ |
250 |
> |
break; |
251 |
> |
if (np > 1) |
252 |
> |
sleep(2); /* wait for persist file */ |
253 |
> |
rtp->next = (struct rtproc *)malloc(sizeof(struct rtproc)); |
254 |
> |
if (rtp->next == NULL) |
255 |
> |
error(SYSTEM, "out of memory in init"); |
256 |
> |
rtp = rtp->next; |
257 |
|
} |
258 |
< |
if (maxbytes < 0) |
169 |
< |
error(SYSTEM, "cannot start rtrace process"); |
170 |
< |
rt.bsiz = maxbytes/(6*sizeof(float)); |
171 |
< |
rt.buf = (float *)malloc(6*sizeof(float)*rt.bsiz--); |
172 |
< |
rt.dest = (float **)malloc(sizeof(float *)*rt.bsiz); |
173 |
< |
if (rt.buf == NULL || rt.dest == NULL) |
174 |
< |
error(SYSTEM, "out of memory in init"); |
175 |
< |
rt.nrays = 0; |
258 |
> |
rtp->next = NULL; |
259 |
|
/* set up urand */ |
260 |
|
initurand(16384); |
261 |
|
} |
262 |
|
|
263 |
|
|
264 |
|
void |
265 |
< |
eputs(s) /* put string to stderr */ |
266 |
< |
register char *s; |
265 |
> |
eputs( /* put string to stderr */ |
266 |
> |
register char *s |
267 |
> |
) |
268 |
|
{ |
269 |
|
static int midline = 0; |
270 |
|
|
287 |
|
} |
288 |
|
|
289 |
|
|
290 |
< |
filter(infp, name) /* process stream */ |
291 |
< |
register FILE *infp; |
292 |
< |
char *name; |
290 |
> |
void |
291 |
> |
filter( /* process stream */ |
292 |
> |
register FILE *infp, |
293 |
> |
char *name |
294 |
> |
) |
295 |
|
{ |
296 |
|
char buf[512]; |
297 |
|
FILE *pfp; |
321 |
|
} |
322 |
|
|
323 |
|
|
324 |
< |
xoptions(s, nm) /* process options in string s */ |
325 |
< |
char *s; |
326 |
< |
char *nm; |
324 |
> |
void |
325 |
> |
xoptions( /* process options in string s */ |
326 |
> |
char *s, |
327 |
> |
char *nm |
328 |
> |
) |
329 |
|
{ |
330 |
|
extern FILE *freopen(); |
331 |
|
char buf[64]; |
484 |
|
printf("# %s", s+2); |
485 |
|
} |
486 |
|
|
487 |
< |
|
488 |
< |
printopts() /* print out option default values */ |
487 |
> |
void |
488 |
> |
printopts(void) /* print out option default values */ |
489 |
|
{ |
490 |
|
printf("m=%-15s\t\t# material name\n", thisillum.matname); |
491 |
|
printf("f=%-15s\t\t# data file name\n", thisillum.datafile); |
506 |
|
} |
507 |
|
|
508 |
|
|
509 |
< |
printhead(ac, av) /* print out header */ |
510 |
< |
register int ac; |
511 |
< |
register char **av; |
509 |
> |
void |
510 |
> |
printhead( /* print out header */ |
511 |
> |
register int ac, |
512 |
> |
register char **av |
513 |
> |
) |
514 |
|
{ |
515 |
|
putchar('#'); |
516 |
|
while (ac-- > 0) { |
521 |
|
} |
522 |
|
|
523 |
|
|
524 |
< |
xobject(fp, nm) /* translate an object from fp */ |
525 |
< |
FILE *fp; |
526 |
< |
char *nm; |
524 |
> |
void |
525 |
> |
xobject( /* translate an object from fp */ |
526 |
> |
FILE *fp, |
527 |
> |
char *nm |
528 |
> |
) |
529 |
|
{ |
530 |
|
OBJREC thisobj; |
531 |
|
char str[MAXSTR]; |
576 |
|
checkhead(); |
577 |
|
/* process object */ |
578 |
|
if (doit) |
579 |
< |
(*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt, nm); |
579 |
> |
(*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt0, nm); |
580 |
|
else |
581 |
|
printobj(thisillum.altmat, &thisobj); |
582 |
|
/* free arguments */ |