5 |
|
* Gather rtrace output to compute contributions from particular sources |
6 |
|
*/ |
7 |
|
|
8 |
+ |
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
9 |
+ |
Need to refactor code by forking a subprocess for each |
10 |
+ |
rtrace call to take output and accumulate it into bins |
11 |
+ |
for the parent process. This will avoid our current |
12 |
+ |
bottleneck around processing output queues, computing |
13 |
+ |
bins and the associated buffer growth, which can be crazy |
14 |
+ |
(gigabytes/subprocess). Each child process will return |
15 |
+ |
a ray number and a fully computed and ready-to-output |
16 |
+ |
record of modifiers and their summed bins. These will |
17 |
+ |
be queued and sorted by the parent for ordered output. |
18 |
+ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/ |
19 |
+ |
|
20 |
|
#include "standard.h" |
21 |
|
#include <ctype.h> |
22 |
|
#include <signal.h> |
28 |
|
#include "lookup.h" |
29 |
|
#include "calcomp.h" |
30 |
|
|
31 |
+ |
#ifdef _WIN32 |
32 |
+ |
typedef int ssize_t; |
33 |
+ |
#endif |
34 |
+ |
|
35 |
|
#ifndef MAXMODLIST |
36 |
|
#define MAXMODLIST 1024 /* maximum modifiers we'll track */ |
37 |
|
#endif |
38 |
|
|
39 |
< |
int treebufsiz = BUFSIZ; /* current tree buffer size */ |
39 |
> |
ssize_t treebufsiz = BUFSIZ; /* current tree buffer size */ |
40 |
|
|
41 |
|
typedef double DCOLOR[3]; /* double-precision color */ |
42 |
|
|
108 |
|
struct rtproc *next; /* next in list of processes */ |
109 |
|
SUBPROC pd; /* rtrace pipe descriptors */ |
110 |
|
unsigned long raynum; /* ray number for this tree */ |
111 |
< |
int bsiz; /* ray tree buffer length */ |
111 |
> |
size_t bsiz; /* ray tree buffer length */ |
112 |
|
char *buf; /* ray tree buffer */ |
113 |
< |
int nbr; /* number of bytes from rtrace */ |
113 |
> |
size_t nbr; /* number of bytes from rtrace */ |
114 |
|
}; /* rtrace process buffer */ |
115 |
|
|
116 |
|
/* rtrace command and defaults */ |
117 |
|
char *rtargv[256+2*MAXMODLIST] = { "rtrace", |
118 |
< |
"-dj", ".5", "-dr", "3", |
118 |
> |
"-dj", ".9", "-dr", "3", |
119 |
|
"-ab", "1", "-ad", "350", }; |
120 |
|
|
121 |
|
int rtargc = 9; |
123 |
|
char *myrtopts[] = { "-h-", "-x", "1", "-y", "0", |
124 |
|
"-dt", "0", "-as", "0", "-aa", "0", NULL }; |
125 |
|
|
126 |
< |
#define RTCOEFF "-o~~TmWdp" /* compute coefficients only */ |
127 |
< |
#define RTCONTRIB "-o~~TmVdp" /* compute ray contributions */ |
126 |
> |
#define RTCOEFF "-o~~~TmWdp" /* compute coefficients only */ |
127 |
> |
#define RTCONTRIB "-o~~~TmVdp" /* compute ray contributions */ |
128 |
|
|
129 |
|
struct rtproc rt0; /* head of rtrace process list */ |
130 |
|
|
147 |
|
int outfmt = 'a'; /* output format */ |
148 |
|
|
149 |
|
int header = 1; /* output header? */ |
134 |
– |
int accumulate = 0; /* accumulate ray values? */ |
150 |
|
int force_open = 0; /* truncate existing output? */ |
151 |
+ |
int recover = 0; /* recover previous output? */ |
152 |
+ |
int accumulate = 1; /* input rays per output record */ |
153 |
|
int xres = 0; /* horiz. output resolution */ |
154 |
|
int yres = 0; /* vert. output resolution */ |
155 |
|
|
156 |
+ |
int account; /* current accumulation count */ |
157 |
|
unsigned long raysleft; /* number of rays left to trace */ |
158 |
|
long waitflush; /* how long until next flush */ |
159 |
|
|
183 |
|
|
184 |
|
void put_contrib(const DCOLOR cnt, FILE *fout); |
185 |
|
void add_contrib(const char *modn); |
186 |
< |
void done_contrib(void); |
186 |
> |
void done_contrib(int navg); |
187 |
|
|
188 |
+ |
#ifdef getc_unlocked /* avoid nasty overheads */ |
189 |
+ |
#undef getc |
190 |
+ |
#define getc getc_unlocked |
191 |
+ |
#undef putc |
192 |
+ |
#define putc putc_unlocked |
193 |
+ |
#undef ferror |
194 |
+ |
#define ferror ferror_unlocked |
195 |
+ |
static int |
196 |
+ |
fread_unl(void *ptr, int size, int nitems, FILE *fp) |
197 |
+ |
{ |
198 |
+ |
char *p = (char *)ptr; |
199 |
+ |
int len = size*nitems; |
200 |
+ |
while (len-- > 0) { |
201 |
+ |
int c = getc_unlocked(fp); |
202 |
+ |
if (c == EOF) |
203 |
+ |
return((p - (char *)ptr)/size); |
204 |
+ |
*p++ = c; |
205 |
+ |
} |
206 |
+ |
return(nitems); |
207 |
+ |
} |
208 |
+ |
#undef fread |
209 |
+ |
#define fread fread_unl |
210 |
+ |
static int |
211 |
+ |
fwrite_unl(const void *ptr, int size, int nitems, FILE *fp) |
212 |
+ |
{ |
213 |
+ |
const char *p = (const char *)ptr; |
214 |
+ |
int len = size*nitems; |
215 |
+ |
while (len-- > 0) |
216 |
+ |
putc_unlocked(*p++, fp); |
217 |
+ |
if (ferror_unlocked(fp)) |
218 |
+ |
return(0); |
219 |
+ |
return(nitems); |
220 |
+ |
} |
221 |
+ |
#undef fwrite |
222 |
+ |
#define fwrite fwrite_unl |
223 |
+ |
#endif |
224 |
+ |
|
225 |
|
/* return number of open rtrace processes */ |
226 |
|
static int |
227 |
|
nrtprocs(void) |
275 |
|
{ |
276 |
|
int contrib = 0; |
277 |
|
int nprocs = 1; |
223 |
– |
int recover = 0; |
278 |
|
char *curout = NULL; |
279 |
|
char *binval = NULL; |
280 |
|
int bincnt = 0; |
299 |
|
while ((j = expandarg(&argc, &argv, i)) > 0) |
300 |
|
; |
301 |
|
if (j < 0) { |
302 |
< |
fprintf(stderr, "%s: cannot expand '%s'", |
302 |
> |
fprintf(stderr, "%s: cannot expand '%s'\n", |
303 |
|
argv[0], argv[i]); |
304 |
|
exit(1); |
305 |
|
} |
328 |
|
continue; |
329 |
|
} |
330 |
|
break; |
331 |
< |
case 'c': /* accumulate ray values */ |
332 |
< |
switch (argv[i][2]) { |
333 |
< |
case '\0': |
334 |
< |
accumulate = !accumulate; |
281 |
< |
continue; |
282 |
< |
case '+': case '1': |
283 |
< |
case 'T': case 't': |
284 |
< |
case 'Y': case 'y': |
285 |
< |
accumulate = 1; |
286 |
< |
continue; |
287 |
< |
case '-': case '0': |
288 |
< |
case 'F': case 'f': |
289 |
< |
case 'N': case 'n': |
290 |
< |
accumulate = 0; |
291 |
< |
continue; |
292 |
< |
} |
293 |
< |
break; |
331 |
> |
case 'c': /* input rays per output */ |
332 |
> |
if (argv[i][2] || i >= argc-2) break; |
333 |
> |
accumulate = atoi(argv[++i]); |
334 |
> |
continue; |
335 |
|
case 'r': /* recover output */ |
336 |
|
if (argv[i][2]) break; |
337 |
< |
recover++; |
337 |
> |
recover = 1; |
338 |
|
continue; |
339 |
|
case 'h': /* output header? */ |
340 |
|
switch (argv[i][2]) { |
369 |
|
continue; |
370 |
|
} |
371 |
|
if (argv[i][2] == 'o') { |
372 |
< |
force_open++; |
372 |
> |
force_open = 1; |
373 |
|
continue; |
374 |
|
} |
375 |
|
setformat(argv[i]+2); |
393 |
|
case 'b': /* bin expression/count */ |
394 |
|
if (i >= argc-2) break; |
395 |
|
if (argv[i][2] == 'n') { |
396 |
< |
bincnt = atoi(argv[++i]); |
396 |
> |
bincnt = (int)(eval(argv[++i]) + .5); |
397 |
|
continue; |
398 |
|
} |
399 |
|
if (argv[i][2]) break; |
421 |
|
} |
422 |
|
rtargv[rtargc++] = argv[i]; /* assume rtrace option */ |
423 |
|
} |
424 |
< |
if (accumulate) /* no output flushing for single record */ |
424 |
> |
if (accumulate <= 0) /* no output flushing for single record */ |
425 |
|
xres = yres = 0; |
426 |
|
/* set global argument list */ |
427 |
|
gargc = argc; gargv = argv; |
431 |
|
rtargv[rtargc++] = contrib ? RTCONTRIB : RTCOEFF; |
432 |
|
/* just asking for defaults? */ |
433 |
|
if (!strcmp(argv[i], "-defaults")) { |
434 |
< |
char sxres[16], syres[16]; |
434 |
> |
char nps[8], sxres[16], syres[16]; |
435 |
|
char *rtpath; |
436 |
< |
printf("-n %-2d\t\t\t\t# number of processes\n", nprocs); |
436 |
> |
printf("-c %-5d\t\t\t# accumulated rays per record\n", |
437 |
> |
accumulate); |
438 |
|
printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-', |
439 |
|
contrib ? "contributions" : "coefficients"); |
398 |
– |
printf("-c%c\t\t\t\t# %s\n", accumulate ? '+' : '-', |
399 |
– |
accumulate ? "accumulate ray values" : |
400 |
– |
"one output record per ray"); |
440 |
|
fflush(stdout); /* report OUR options */ |
441 |
+ |
rtargv[rtargc++] = "-n"; |
442 |
+ |
sprintf(nps, "%d", nprocs); |
443 |
+ |
rtargv[rtargc++] = nps; |
444 |
|
rtargv[rtargc++] = header ? "-h+" : "-h-"; |
445 |
|
sprintf(fmt, "-f%c%c", inpfmt, outfmt); |
446 |
|
rtargv[rtargc++] = fmt; |
479 |
|
error(USER, "missing octree argument"); |
480 |
|
rtargv[rtargc++] = octree = argv[i]; |
481 |
|
rtargv[rtargc] = NULL; |
482 |
< |
/* start rtrace */ |
482 |
> |
/* start rtrace & recover if requested */ |
483 |
|
init(nprocs); |
484 |
< |
if (recover) /* perform recovery if requested */ |
485 |
< |
if ((force_open = accumulate)) |
486 |
< |
reload_output(); |
445 |
< |
else |
446 |
< |
recover_output(stdin); |
447 |
< |
trace_contribs(stdin); /* compute contributions */ |
484 |
> |
/* compute contributions */ |
485 |
> |
trace_contribs(stdin); |
486 |
> |
/* clean up */ |
487 |
|
quit(0); |
488 |
|
} |
489 |
|
|
596 |
|
raysleft = yres; |
597 |
|
} else |
598 |
|
raysleft = 0; |
599 |
< |
waitflush = xres; |
599 |
> |
if ((account = accumulate) > 0) |
600 |
> |
raysleft *= accumulate; |
601 |
> |
waitflush = (yres > 0) & (xres > 1) ? 0 : xres; |
602 |
> |
if (!recover) |
603 |
> |
return; |
604 |
> |
/* recover previous values */ |
605 |
> |
if (accumulate <= 0) |
606 |
> |
reload_output(); |
607 |
> |
else |
608 |
> |
recover_output(stdin); |
609 |
|
} |
610 |
|
|
611 |
|
/* grow modifier to accommodate more bins */ |
635 |
|
error(USER, errmsg); |
636 |
|
} |
637 |
|
if (nmods >= MAXMODLIST) |
638 |
< |
error(USER, "too many modifiers"); |
638 |
> |
error(INTERNAL, "too many modifiers"); |
639 |
|
modname[nmods++] = modn; /* XXX assumes static string */ |
640 |
|
lep->key = modn; /* XXX assumes static string */ |
641 |
|
mp = (MODCONT *)malloc(sizeof(MODCONT)); |
643 |
|
error(SYSTEM, "out of memory in addmodifier"); |
644 |
|
mp->outspec = outf; /* XXX assumes static string */ |
645 |
|
mp->modname = modn; /* XXX assumes static string */ |
646 |
< |
if (binv != NULL) |
647 |
< |
mp->binv = eparse(binv); |
648 |
< |
else |
649 |
< |
mp->binv = eparse("0"); |
650 |
< |
mp->nbins = 1; |
646 |
> |
if (binv == NULL) |
647 |
> |
binv = "0"; /* use single bin if unspecified */ |
648 |
> |
mp->binv = eparse(binv); |
649 |
> |
if (mp->binv->type == NUM) { /* check value if constant */ |
650 |
> |
bincnt = (int)(evalue(mp->binv) + 1.5); |
651 |
> |
if (bincnt != 1) { |
652 |
> |
sprintf(errmsg, "illegal non-zero constant for bin (%s)", |
653 |
> |
binv); |
654 |
> |
error(USER, errmsg); |
655 |
> |
} |
656 |
> |
} |
657 |
> |
mp->nbins = 1; /* initialize results holder */ |
658 |
|
setcolor(mp->cbin[0], 0., 0., 0.); |
659 |
< |
if (mp->binv->type == NUM) /* assume one bin if constant */ |
605 |
< |
bincnt = 1; |
606 |
< |
else if (bincnt > 1) |
659 |
> |
if (bincnt > 1) |
660 |
|
mp = growmodifier(mp, bincnt); |
661 |
|
lep->data = (char *)mp; |
662 |
|
/* allocate output streams */ |
719 |
|
mnp = cp; |
720 |
|
break; |
721 |
|
case 'd': |
722 |
+ |
case 'i': |
723 |
+ |
case 'o': |
724 |
+ |
case 'x': |
725 |
+ |
case 'X': |
726 |
|
if (bnp != NULL) |
727 |
|
return -1; |
728 |
|
bnp = cp; |
755 |
|
printheader(FILE *fout, const char *info) |
756 |
|
{ |
757 |
|
extern char VersionID[]; |
758 |
< |
FILE *fin = fopen(octree, "r"); |
759 |
< |
|
760 |
< |
if (fin == NULL) |
761 |
< |
quit(1); |
762 |
< |
checkheader(fin, "ignore", fout); /* copy octree header */ |
763 |
< |
fclose(fin); |
758 |
> |
/* copy octree header */ |
759 |
> |
if (octree[0] == '!') { |
760 |
> |
newheader("RADIANCE", fout); |
761 |
> |
fputs(octree+1, fout); |
762 |
> |
if (octree[strlen(octree)-1] != '\n') |
763 |
> |
fputc('\n', fout); |
764 |
> |
} else { |
765 |
> |
FILE *fin = fopen(octree, "r"); |
766 |
> |
if (fin == NULL) |
767 |
> |
quit(1); |
768 |
> |
checkheader(fin, "ignore", fout); |
769 |
> |
fclose(fin); |
770 |
> |
} |
771 |
|
printargs(gargc-1, gargv, fout); /* add our command */ |
772 |
|
fprintf(fout, "SOFTWARE= %s\n", VersionID); |
773 |
|
fputnow(fout); |
796 |
|
{ |
797 |
|
if ((xr > 0) & (yr > 0)) /* resolution string */ |
798 |
|
fprtresolu(xr, yr, fout); |
735 |
– |
if (xres > 0) /* global flush flag */ |
736 |
– |
fflush(fout); |
799 |
|
} |
800 |
|
|
801 |
|
/* Get output stream pointer (open and write header if new and noopen==0) */ |
816 |
|
if (header) |
817 |
|
printheader(stdout, NULL); |
818 |
|
printresolu(stdout, xres, yres); |
819 |
+ |
if (waitflush > 0) |
820 |
+ |
fflush(stdout); |
821 |
|
stdos.xr = xres; stdos.yr = yres; |
822 |
|
using_stdout = 1; |
823 |
|
} |
843 |
|
sop->ofp = NULL; /* open iff noopen==0 */ |
844 |
|
sop->xr = xres; sop->yr = yres; |
845 |
|
lep->data = (char *)sop; |
846 |
< |
if (!force_open && access(oname, F_OK) == 0) { |
846 |
> |
if (!sop->outpipe & !force_open & !recover && |
847 |
> |
access(oname, F_OK) == 0) { |
848 |
|
errno = EEXIST; /* file exists */ |
849 |
|
goto openerr; |
850 |
|
} |
859 |
|
goto openerr; |
860 |
|
if (outfmt != 'a') |
861 |
|
SET_FILE_BINARY(sop->ofp); |
862 |
+ |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
863 |
+ |
flockfile(sop->ofp); |
864 |
+ |
#endif |
865 |
|
if (header) { |
866 |
|
char info[512]; |
867 |
|
char *cp = info; |
876 |
|
*cp = '\0'; |
877 |
|
printheader(sop->ofp, info); |
878 |
|
} |
879 |
+ |
if (accumulate > 0) { /* global resolution */ |
880 |
+ |
sop->xr = xres; sop->yr = yres; |
881 |
+ |
} |
882 |
|
printresolu(sop->ofp, sop->xr, sop->yr); |
883 |
|
/* play catch-up */ |
884 |
< |
for (i = accumulate ? 0 : lastdone; i--; ) { |
884 |
> |
for (i = accumulate > 0 ? lastdone/accumulate : 0; i--; ) { |
885 |
|
int j = sop->reclen; |
886 |
|
if (j <= 0) j = 1; |
887 |
|
while (j--) |
889 |
|
if (outfmt == 'a') |
890 |
|
putc('\n', sop->ofp); |
891 |
|
} |
892 |
< |
if (xres > 0) |
892 |
> |
if (waitflush > 0) |
893 |
|
fflush(sop->ofp); |
894 |
|
} |
895 |
|
sop->reclen += noopen; /* add to length if noopen */ |
927 |
|
return 0; /* dummy ray */ |
928 |
|
return strlen(buf); |
929 |
|
case 'f': |
930 |
< |
if (fread(buf, sizeof(float), 6, fp) < 6) |
930 |
> |
if (fread(buf, sizeof(float), 6, fp) != 6) |
931 |
|
return -1; |
932 |
|
fvp = (float *)buf + 3; |
933 |
|
if (DOT(fvp,fvp) <= FTINY*FTINY) |
934 |
|
return 0; /* dummy ray */ |
935 |
|
return sizeof(float)*6; |
936 |
|
case 'd': |
937 |
< |
if (fread(buf, sizeof(double), 6, fp) < 6) |
937 |
> |
if (fread(buf, sizeof(double), 6, fp) != 6) |
938 |
|
return -1; |
939 |
|
dvp = (double *)buf + 3; |
940 |
|
if (DOT(dvp,dvp) <= FTINY*FTINY) |
1006 |
|
fprintf(fout, "%.6e\t%.6e\t%.6e\t", cnt[0], cnt[1], cnt[2]); |
1007 |
|
break; |
1008 |
|
case 'f': |
1009 |
< |
fv[0] = cnt[0]; |
939 |
< |
fv[1] = cnt[1]; |
940 |
< |
fv[2] = cnt[2]; |
1009 |
> |
copycolor(fv, cnt); |
1010 |
|
fwrite(fv, sizeof(float), 3, fout); |
1011 |
|
break; |
1012 |
|
case 'd': |
1023 |
|
|
1024 |
|
/* output ray tallies and clear for next accumulation */ |
1025 |
|
void |
1026 |
< |
done_contrib(void) |
1026 |
> |
done_contrib(int navg) |
1027 |
|
{ |
1028 |
+ |
double sf = 1.; |
1029 |
|
int i, j; |
1030 |
|
MODCONT *mp; |
1031 |
|
STREAMOUT *sop; |
1032 |
+ |
/* set average scaling */ |
1033 |
+ |
if (navg > 1) |
1034 |
+ |
sf = 1. / (double)navg; |
1035 |
|
/* output modifiers in order */ |
1036 |
|
for (i = 0; i < nmods; i++) { |
1037 |
|
mp = (MODCONT *)lu_find(&modconttab,modname[i])->data; |
1038 |
+ |
if (navg > 1) /* average scaling */ |
1039 |
+ |
for (j = mp->nbins; j--; ) |
1040 |
+ |
scalecolor(mp->cbin[j], sf); |
1041 |
|
sop = getostream(mp->outspec, mp->modname, 0,0); |
1042 |
|
put_contrib(mp->cbin[0], sop->ofp); |
1043 |
|
if (mp->nbins > 3 && /* minor optimization */ |
1056 |
|
if (using_stdout & (outfmt == 'a')) |
1057 |
|
putc('\n', stdout); |
1058 |
|
if (!waitflush) { |
1059 |
< |
waitflush = xres; |
1059 |
> |
waitflush = (yres > 0) & (xres > 1) ? 0 : xres; |
1060 |
|
if (using_stdout) |
1061 |
|
fflush(stdout); |
1062 |
|
} |
1121 |
|
cp += sizeof(float)*9; n -= sizeof(float)*9; |
1122 |
|
add_contrib(modname); |
1123 |
|
} |
1124 |
< |
if (!accumulate) |
1125 |
< |
done_contrib(); /* sum up contributions & output */ |
1124 |
> |
/* time to produce record? */ |
1125 |
> |
if (account > 0 && !--account) |
1126 |
> |
done_contrib(account = accumulate); |
1127 |
|
lastdone = rtp->raynum; |
1128 |
|
if (rtp->buf != NULL) /* free up buffer space */ |
1129 |
|
free(rtp->buf); |
1138 |
|
{ |
1139 |
|
struct rtproc *rtfree = NULL; |
1140 |
|
fd_set readset, errset; |
1141 |
< |
int nr; |
1141 |
> |
ssize_t nr; |
1142 |
|
struct rtproc *rt; |
1143 |
|
int n; |
1144 |
|
|
1169 |
|
continue; |
1170 |
|
if (rt->buf == NULL) { |
1171 |
|
rt->bsiz = treebufsiz; |
1172 |
< |
rt->buf = (char *)malloc(treebufsiz); |
1172 |
> |
rt->buf = (char *)malloc(rt->bsiz); |
1173 |
|
} else if (rt->nbr + BUFSIZ > rt->bsiz) { |
1174 |
|
if (rt->bsiz + BUFSIZ <= treebufsiz) |
1175 |
|
rt->bsiz = treebufsiz; |
1176 |
< |
else |
1177 |
< |
treebufsiz = rt->bsiz += BUFSIZ; |
1176 |
> |
else if ((treebufsiz = rt->bsiz += BUFSIZ) < 0) |
1177 |
> |
error(INTERNAL, |
1178 |
> |
"ray buffer does not fit memory"); |
1179 |
|
rt->buf = (char *)realloc(rt->buf, rt->bsiz); |
1180 |
|
} |
1181 |
|
if (rt->buf == NULL) |
1182 |
|
error(SYSTEM, "out of memory in wait_rproc"); |
1183 |
< |
nr = read(rt->pd.r, rt->buf+rt->nbr, rt->bsiz-rt->nbr); |
1184 |
< |
if (nr <= 0) |
1183 |
> |
nr = rt->bsiz - rt->nbr; |
1184 |
> |
if (nr & ~0x7fffffff) /* avoid 32-bit OS issues */ |
1185 |
> |
nr = 0x7fffffff; |
1186 |
> |
nr = read(rt->pd.r, rt->buf+rt->nbr, nr); |
1187 |
> |
if (nr < 0) |
1188 |
> |
error(SYSTEM, "read error from rtrace"); |
1189 |
> |
if (!nr) |
1190 |
|
error(USER, "rtrace process died"); |
1191 |
|
rt->nbr += nr; /* advance & check */ |
1192 |
< |
if (rt->nbr >= 4 && !memcmp(rt->buf+rt->nbr-4, |
1193 |
< |
"~\t~\t", 4)) { |
1194 |
< |
rt->nbr -= 4; /* elide terminator */ |
1192 |
> |
if (rt->nbr >= 6 && !memcmp(rt->buf+rt->nbr-6, |
1193 |
> |
"~\t~\t~\t", 6)) { |
1194 |
> |
rt->nbr -= 6; /* elide terminator */ |
1195 |
|
queue_raytree(rt); |
1196 |
|
rtfree = rt; /* ready for next ray */ |
1197 |
|
} |
1216 |
|
void |
1217 |
|
trace_contribs(FILE *fin) |
1218 |
|
{ |
1219 |
+ |
static int ignore_warning_given = 0; |
1220 |
|
char inpbuf[128]; |
1221 |
|
int iblen; |
1222 |
|
struct rtproc *rtp; |
1223 |
|
/* loop over input */ |
1224 |
|
while ((iblen = getinp(inpbuf, fin)) >= 0) { |
1225 |
< |
if (!iblen || /* need reset? */ |
1225 |
> |
if (!iblen && accumulate != 1) { |
1226 |
> |
if (!ignore_warning_given++) |
1227 |
> |
error(WARNING, |
1228 |
> |
"dummy ray(s) ignored during accumulation\n"); |
1229 |
> |
continue; |
1230 |
> |
} |
1231 |
> |
if (!iblen || /* need flush/reset? */ |
1232 |
|
queue_length() > 10*nrtprocs() || |
1233 |
|
lastray+1 < lastray) { |
1234 |
|
while (wait_rproc() != NULL) |
1235 |
|
process_queue(); |
1236 |
< |
if (lastray+1 < lastray) |
1147 |
< |
lastdone = lastray = 0; |
1236 |
> |
lastdone = lastray = 0; |
1237 |
|
} |
1238 |
|
rtp = get_rproc(); /* get avail. rtrace process */ |
1239 |
|
rtp->raynum = ++lastray; /* assign ray */ |
1240 |
|
if (iblen) { /* trace ray if valid */ |
1241 |
|
writebuf(rtp->pd.w, inpbuf, iblen); |
1242 |
|
} else { /* else bypass dummy ray */ |
1243 |
< |
queue_raytree(rtp); /* empty tree */ |
1244 |
< |
if ((yres <= 0) | (waitflush > 1)) |
1245 |
< |
waitflush = 1; /* flush after this */ |
1243 |
> |
queue_raytree(rtp); /* queue empty ray/record */ |
1244 |
> |
if ((yres <= 0) | (xres <= 0)) |
1245 |
> |
waitflush = 1; /* flush right after */ |
1246 |
|
} |
1247 |
|
process_queue(); /* catch up with results */ |
1248 |
|
if (raysleft && !--raysleft) |
1250 |
|
} |
1251 |
|
while (wait_rproc() != NULL) /* process outstanding rays */ |
1252 |
|
process_queue(); |
1253 |
< |
if (accumulate) |
1254 |
< |
done_contrib(); /* output tallies */ |
1253 |
> |
if (accumulate <= 0) |
1254 |
> |
done_contrib(0); /* output tallies */ |
1255 |
> |
else if (account < accumulate) { |
1256 |
> |
error(WARNING, "partial accumulation in final record"); |
1257 |
> |
done_contrib(accumulate - account); |
1258 |
> |
} |
1259 |
|
if (raysleft) |
1260 |
|
error(USER, "unexpected EOF on input"); |
1261 |
|
lu_done(&ofiletab); /* close output files */ |
1262 |
|
} |
1263 |
|
|
1264 |
< |
/* seek on the given output file */ |
1264 |
> |
/* get ray contribution from previous file */ |
1265 |
|
static int |
1266 |
< |
myseeko(const LUENT *e, void *p) |
1266 |
> |
get_contrib(DCOLOR cnt, FILE *finp) |
1267 |
|
{ |
1268 |
+ |
COLOR fv; |
1269 |
+ |
COLR cv; |
1270 |
+ |
|
1271 |
+ |
switch (outfmt) { |
1272 |
+ |
case 'a': |
1273 |
+ |
return(fscanf(finp,"%lf %lf %lf",&cnt[0],&cnt[1],&cnt[2]) == 3); |
1274 |
+ |
case 'f': |
1275 |
+ |
if (fread(fv, sizeof(fv[0]), 3, finp) != 3) |
1276 |
+ |
return(0); |
1277 |
+ |
copycolor(cnt, fv); |
1278 |
+ |
return(1); |
1279 |
+ |
case 'd': |
1280 |
+ |
return(fread(cnt, sizeof(cnt[0]), 3, finp) == 3); |
1281 |
+ |
case 'c': |
1282 |
+ |
if (fread(cv, sizeof(cv), 1, finp) != 1) |
1283 |
+ |
return(0); |
1284 |
+ |
colr_color(fv, cv); |
1285 |
+ |
copycolor(cnt, fv); |
1286 |
+ |
return(1); |
1287 |
+ |
default: |
1288 |
+ |
error(INTERNAL, "botched output format"); |
1289 |
+ |
} |
1290 |
+ |
return(0); /* pro forma return */ |
1291 |
+ |
} |
1292 |
+ |
|
1293 |
+ |
/* close output file opened for input */ |
1294 |
+ |
static int |
1295 |
+ |
myclose(const LUENT *e, void *p) |
1296 |
+ |
{ |
1297 |
|
STREAMOUT *sop = (STREAMOUT *)e->data; |
1176 |
– |
off_t nbytes = *(off_t *)p; |
1298 |
|
|
1299 |
< |
if (sop->reclen > 1) |
1300 |
< |
nbytes = nbytes * sop->reclen; |
1301 |
< |
if (fseeko(sop->ofp, nbytes, SEEK_CUR) < 0) { |
1302 |
< |
sprintf(errmsg, "seek error on file '%s'", e->key); |
1303 |
< |
error(SYSTEM, errmsg); |
1183 |
< |
} |
1184 |
< |
return 0; |
1299 |
> |
if (sop->ofp == NULL) |
1300 |
> |
return(0); |
1301 |
> |
fclose(sop->ofp); |
1302 |
> |
sop->ofp = NULL; |
1303 |
> |
return(0); |
1304 |
|
} |
1305 |
|
|
1306 |
|
/* load previously accumulated values */ |
1314 |
|
char *fmode = "rb"; |
1315 |
|
char *outvfmt; |
1316 |
|
LUENT *ment, *oent; |
1317 |
+ |
int xr, yr; |
1318 |
|
STREAMOUT sout; |
1319 |
+ |
DCOLOR rgbv; |
1320 |
|
|
1200 |
– |
error(INTERNAL, "-r option not yet implemented with -c"); |
1201 |
– |
|
1321 |
|
switch (outfmt) { |
1322 |
|
case 'a': |
1323 |
|
outvfmt = "ascii"; |
1336 |
|
error(INTERNAL, "botched output format"); |
1337 |
|
return; |
1338 |
|
} |
1339 |
< |
/* check modifier outputs */ |
1339 |
> |
/* reload modifier values */ |
1340 |
|
for (i = 0; i < nmods; i++) { |
1341 |
|
ment = lu_find(&modconttab,modname[i]); |
1342 |
|
mp = (MODCONT *)ment->data; |
1344 |
|
error(USER, "cannot reload from stdout"); |
1345 |
|
if (mp->outspec[0] == '!') |
1346 |
|
error(USER, "cannot reload from command"); |
1347 |
< |
for (j = 0; ; j++) { /* check each bin's file */ |
1347 |
> |
for (j = 0; ; j++) { /* load each modifier bin */ |
1348 |
|
ofl = ofname(oname, mp->outspec, mp->modname, j); |
1349 |
|
if (ofl < 0) |
1350 |
|
error(USER, "bad output file specification"); |
1353 |
|
sout = *(STREAMOUT *)oent->data; |
1354 |
|
} else { |
1355 |
|
sout.reclen = 0; |
1356 |
+ |
sout.outpipe = 0; |
1357 |
+ |
sout.xr = xres; sout.yr = yres; |
1358 |
|
sout.ofp = NULL; |
1359 |
|
} |
1360 |
< |
if (sout.ofp != NULL) { /* already open? */ |
1361 |
< |
if (ofl & OF_BIN) |
1362 |
< |
continue; |
1363 |
< |
break; |
1360 |
> |
if (sout.ofp == NULL) { /* open output as input */ |
1361 |
> |
sout.ofp = fopen(oname, fmode); |
1362 |
> |
if (sout.ofp == NULL) { |
1363 |
> |
if (j) |
1364 |
> |
break; /* assume end of modifier */ |
1365 |
> |
sprintf(errmsg, "missing reload file '%s'", |
1366 |
> |
oname); |
1367 |
> |
error(WARNING, errmsg); |
1368 |
> |
break; |
1369 |
> |
} |
1370 |
> |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
1371 |
> |
flockfile(sout.ofp); |
1372 |
> |
#endif |
1373 |
> |
if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) { |
1374 |
> |
sprintf(errmsg, "format mismatch for '%s'", |
1375 |
> |
oname); |
1376 |
> |
error(USER, errmsg); |
1377 |
> |
} |
1378 |
> |
if ((sout.xr > 0) & (sout.yr > 0) && |
1379 |
> |
(!fscnresolu(&xr, &yr, sout.ofp) || |
1380 |
> |
(xr != sout.xr) | |
1381 |
> |
(yr != sout.yr))) { |
1382 |
> |
sprintf(errmsg, "resolution mismatch for '%s'", |
1383 |
> |
oname); |
1384 |
> |
error(USER, errmsg); |
1385 |
> |
} |
1386 |
|
} |
1387 |
< |
/* open output */ |
1388 |
< |
sout.ofp = fopen(oname, fmode); |
1389 |
< |
if (sout.ofp == NULL) { |
1390 |
< |
if (j) |
1391 |
< |
break; /* assume end of modifier */ |
1392 |
< |
sprintf(errmsg, "missing reload file '%s'", |
1393 |
< |
oname); |
1394 |
< |
error(WARNING, errmsg); |
1387 |
> |
/* read in RGB value */ |
1388 |
> |
if (!get_contrib(rgbv, sout.ofp)) { |
1389 |
> |
if (!j) { |
1390 |
> |
fclose(sout.ofp); |
1391 |
> |
break; /* ignore empty file */ |
1392 |
> |
} |
1393 |
> |
if (j < mp->nbins) { |
1394 |
> |
sprintf(errmsg, "missing data in '%s'", |
1395 |
> |
oname); |
1396 |
> |
error(USER, errmsg); |
1397 |
> |
} |
1398 |
|
break; |
1399 |
|
} |
1400 |
< |
if (oent->key == NULL) /* new entry */ |
1400 |
> |
if (j >= mp->nbins) /* grow modifier size */ |
1401 |
> |
ment->data = (char *)(mp = growmodifier(mp, j+1)); |
1402 |
> |
copycolor(mp->cbin[j], rgbv); |
1403 |
> |
if (oent->key == NULL) /* new file entry */ |
1404 |
|
oent->key = strcpy((char *) |
1405 |
|
malloc(strlen(oname)+1), oname); |
1406 |
|
if (oent->data == NULL) |
1407 |
|
oent->data = (char *)malloc(sizeof(STREAMOUT)); |
1408 |
|
*(STREAMOUT *)oent->data = sout; |
1260 |
– |
if (!(ofl & OF_BIN)) |
1261 |
– |
break; /* no bin separation */ |
1409 |
|
} |
1263 |
– |
if (j > mp->nbins) /* reallocate modifier bins */ |
1264 |
– |
ment->data = (char *)(mp = growmodifier(mp, j)); |
1410 |
|
} |
1411 |
< |
lu_done(&ofiletab); /* close all files */ |
1411 |
> |
lu_doall(&ofiletab, myclose, NULL); /* close all files */ |
1412 |
|
} |
1413 |
|
|
1414 |
+ |
/* seek on the given output file */ |
1415 |
+ |
static int |
1416 |
+ |
myseeko(const LUENT *e, void *p) |
1417 |
+ |
{ |
1418 |
+ |
STREAMOUT *sop = (STREAMOUT *)e->data; |
1419 |
+ |
off_t nbytes = *(off_t *)p; |
1420 |
+ |
|
1421 |
+ |
if (sop->reclen > 1) |
1422 |
+ |
nbytes = nbytes * sop->reclen; |
1423 |
+ |
if (fseeko(sop->ofp, nbytes, SEEK_CUR) < 0) { |
1424 |
+ |
sprintf(errmsg, "seek error on file '%s'", e->key); |
1425 |
+ |
error(SYSTEM, errmsg); |
1426 |
+ |
} |
1427 |
+ |
return 0; |
1428 |
+ |
} |
1429 |
+ |
|
1430 |
|
/* recover output if possible */ |
1431 |
|
void |
1432 |
|
recover_output(FILE *fin) |
1480 |
|
sout = *(STREAMOUT *)oent->data; |
1481 |
|
} else { |
1482 |
|
sout.reclen = 0; |
1483 |
+ |
sout.outpipe = 0; |
1484 |
|
sout.ofp = NULL; |
1485 |
|
} |
1486 |
|
if (sout.ofp != NULL) { /* already open? */ |
1521 |
|
oname); |
1522 |
|
error(USER, errmsg); |
1523 |
|
} |
1524 |
< |
if ((xres > 0) & (yres > 0) && |
1524 |
> |
sout.xr = xres; sout.yr = yres; |
1525 |
> |
if ((sout.xr > 0) & (sout.yr > 0) && |
1526 |
|
(!fscnresolu(&xr, &yr, sout.ofp) || |
1527 |
< |
xr != xres || |
1528 |
< |
yr != yres)) { |
1527 |
> |
(xr != sout.xr) | |
1528 |
> |
(yr != sout.yr))) { |
1529 |
|
sprintf(errmsg, "resolution mismatch for '%s'", |
1530 |
|
oname); |
1531 |
|
error(USER, errmsg); |
1554 |
|
error(WARNING, "no output files to recover"); |
1555 |
|
return; |
1556 |
|
} |
1557 |
< |
if (raysleft && lastout >= raysleft) { |
1557 |
> |
if (raysleft && lastout >= raysleft/accumulate) { |
1558 |
|
error(WARNING, "output appears to be complete"); |
1559 |
|
/* XXX should read & discard input? */ |
1560 |
|
quit(0); |
1566 |
|
for (nvals = 0; nvals < lastout; nvals++) |
1567 |
|
if (getinp(oname, fin) < 0) |
1568 |
|
error(USER, "unexpected EOF on input"); |
1569 |
< |
lastray = lastdone = (unsigned long)lastout; |
1569 |
> |
lastray = lastdone = (unsigned long)lastout * accumulate; |
1570 |
|
if (raysleft) |
1571 |
|
raysleft -= lastray; |
1572 |
|
} |