--- ray/src/util/rad.c 2008/08/21 07:05:59 2.86 +++ ray/src/util/rad.c 2010/10/08 22:11:57 2.99 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rad.c,v 2.86 2008/08/21 07:05:59 greg Exp $"; +static const char RCSid[] = "$Id: rad.c,v 2.99 2010/10/08 22:11:57 greg Exp $"; #endif /* * Executive program for oconv, rpict and pfilt @@ -47,14 +47,16 @@ static const char RCSid[] = "$Id: rad.c,v 2.86 2008/08 #define RENDER 17 /* rendering options */ #define REPORT 18 /* report frequency and errfile */ #define RESOLUTION 19 /* maximum picture resolution */ -#define SCENE 20 /* scene files */ -#define UP 21 /* view up (X, Y or Z) */ -#define VARIABILITY 22 /* level of light variability */ -#define VIEWS 23 /* view(s) for picture(s) */ -#define ZFILE 24 /* distance file root name */ -#define ZONE 25 /* simulation zone */ +#define RPICT 20 /* rpict parameters */ +#define RVU 21 /* rvu parameters */ +#define SCENE 22 /* scene files */ +#define UP 23 /* view up (X, Y or Z) */ +#define VARIABILITY 24 /* level of light variability */ +#define VIEWS 25 /* view(s) for picture(s) */ +#define ZFILE 26 /* distance file root name */ +#define ZONE 27 /* simulation zone */ /* total number of variables */ -int NVARS = 26; +int NVARS = 28; VARIABLE vv[] = { /* variable-value pairs */ {"AMBFILE", 3, 0, NULL, onevalue}, @@ -77,6 +79,8 @@ VARIABLE vv[] = { /* variable-value pairs */ {"render", 3, 0, NULL, catvalues}, {"REPORT", 3, 0, NULL, onevalue}, {"RESOLUTION", 3, 0, NULL, onevalue}, + {"rpict", 3, 0, NULL, catvalues}, + {"rvu", 3, 0, NULL, catvalues}, {"scene", 3, 0, NULL, catvalues}, {"UP", 2, 0, NULL, onevalue}, {"VARIABILITY", 3, 0, NULL, qualvalue}, @@ -112,6 +116,16 @@ int sayview = 0; /* print view out */ char *rvdevice = NULL; /* rvu output device */ char *viewselect = NULL; /* specific view only */ +#define DEF_RPICT_PATH "rpict" /* default rpict path */ + + /* command paths */ +char c_oconv[256] = "oconv"; +char c_mkillum[256] = "mkillum"; +char c_rvu[256] = "rvu"; +char c_rpict[256] = DEF_RPICT_PATH; +char c_rpiece[] = "rpiece"; +char c_pfilt[256] = "pfilt"; + int overture = 0; /* overture calculation needed */ int children_running = 0; /* set negative in children */ @@ -151,7 +165,7 @@ static int touch(char *fn); static int runcom(char *cs); static int rmfile(char *fn); static int mvfile(char *fold, char *fnew); -static int next_process(void); +static int next_process(int reserve); static void wait_process(int all); static void finish_process(void); static void badvalue(int vc); @@ -207,9 +221,6 @@ main( if (i >= argc) goto userr; rifname = argv[i]; - /* check command-line options */ - if ((nprocs > 1) & (viewselect != NULL)) - nprocs = 1; /* assign Radiance root file name */ rootname(radname, rifname); /* load variable values */ @@ -480,11 +491,11 @@ oconv(void) /* run oconv and mkillum if necessary * touch(vval(OCTREE)); else { /* build command */ if (vdef(MATERIAL)) - sprintf(combuf, "oconv%s %s %s > %s", ocopts, - vval(MATERIAL), vval(SCENE), - vval(OCTREE)); + sprintf(combuf, "%s%s %s %s > %s", c_oconv, + ocopts, vval(MATERIAL), + vval(SCENE), vval(OCTREE)); else - sprintf(combuf, "oconv%s %s > %s", ocopts, + sprintf(combuf, "%s%s %s > %s", c_oconv, ocopts, vval(SCENE), vval(OCTREE)); if (runcom(combuf)) { /* run it */ @@ -510,14 +521,16 @@ oconv(void) /* run oconv and mkillum if necessary * touch(oct0name); else { /* build command */ if (octreedate) - sprintf(combuf, "oconv%s -i %s %s > %s", ocopts, - vval(OCTREE), vval(ILLUM), oct0name); + sprintf(combuf, "%s%s -i %s %s > %s", c_oconv, + ocopts, vval(OCTREE), + vval(ILLUM), oct0name); else if (vdef(MATERIAL)) - sprintf(combuf, "oconv%s %s %s > %s", ocopts, - vval(MATERIAL), vval(ILLUM), oct0name); - else - sprintf(combuf, "oconv%s %s > %s", ocopts, + sprintf(combuf, "%s%s %s %s > %s", c_oconv, + ocopts, vval(MATERIAL), vval(ILLUM), oct0name); + else + sprintf(combuf, "%s%s %s > %s", c_oconv, + ocopts, vval(ILLUM), oct0name); if (runcom(combuf)) { /* run it */ fprintf(stderr, "%s: error generating octree\n\t%s removed\n", @@ -537,23 +550,23 @@ oconv(void) /* run oconv and mkillum if necessary * else { mkillumopts(mkopts); /* build mkillum command */ mktemp(illumtmp); - sprintf(combuf, "mkillum%s %s \"<\" %s > %s", mkopts, + sprintf(combuf, "%s%s %s \"<\" %s > %s", c_mkillum, mkopts, oct0name, vval(ILLUM), illumtmp); if (runcom(combuf)) { /* run it */ - fprintf(stderr, "%s: error running mkillum\n", - progname); + fprintf(stderr, "%s: error running %s\n", + progname, c_mkillum); unlink(illumtmp); quit(1); } /* make octree1 (frozen) */ if (octreedate) - sprintf(combuf, "oconv%s -f -i %s %s > %s", ocopts, - vval(OCTREE), illumtmp, oct1name); + sprintf(combuf, "%s%s -f -i %s %s > %s", c_oconv, + ocopts, vval(OCTREE), illumtmp, oct1name); else if (vdef(MATERIAL)) - sprintf(combuf, "oconv%s -f %s %s > %s", ocopts, - vval(MATERIAL), illumtmp, oct1name); + sprintf(combuf, "%s%s -f %s %s > %s", c_oconv, + ocopts, vval(MATERIAL), illumtmp, oct1name); else - sprintf(combuf, "oconv%s -f %s > %s", ocopts, + sprintf(combuf, "%s%s -f %s > %s", c_oconv, ocopts, illumtmp, oct1name); if (runcom(combuf)) { /* run it */ fprintf(stderr, @@ -572,11 +585,13 @@ oconv(void) /* run oconv and mkillum if necessary * static char * -addarg( /* add argument and advance pointer */ +addarg( /* append argument and advance pointer */ register char *op, register char *arg ) { + while (*op) + op++; *op = ' '; while ( (*++op = *arg++) ) ; @@ -593,7 +608,11 @@ oconvopts( /* get oconv options */ *oo = '\0'; if (vdef(OCONV)) - addarg(oo, vval(OCONV)); + if (vval(OCONV)[0] != '-') { + atos(c_oconv, sizeof(c_oconv), vval(OCONV)); + oo = addarg(oo, sskip2(vval(OCONV), 1)); + } else + oo = addarg(oo, vval(OCONV)); } @@ -604,14 +623,16 @@ mkillumopts( /* get mkillum options */ { /* BEWARE: This may be called via setdefaults(), so no assumptions */ - if (nprocs > 1) { + if (nprocs > 1) sprintf(mo, " -n %d", nprocs); - while (*mo) - mo++; - } else + else *mo = '\0'; if (vdef(MKILLUM)) - addarg(mo, vval(MKILLUM)); + if (vval(MKILLUM)[0] != '-') { + atos(c_mkillum, sizeof(c_mkillum), vval(MKILLUM)); + mo = addarg(mo, sskip2(vval(MKILLUM), 1)); + } else + mo = addarg(mo, vval(MKILLUM)); } @@ -667,6 +688,23 @@ renderopts( /* set rendering options */ hiqopts(op, po); break; } + if (vdef(RENDER)) + op = addarg(op, vval(RENDER)); + if (rvdevice != NULL) { + if (vdef(RVU)) + if (vval(RVU)[0] != '-') { + atos(c_rvu, sizeof(c_rvu), vval(RVU)); + po = addarg(po, sskip2(vval(RVU), 1)); + } else + po = addarg(po, vval(RVU)); + } else { + if (vdef(RPICT)) + if (vval(RPICT)[0] != '-') { + atos(c_rpict, sizeof(c_rpict), vval(RPICT)); + po = addarg(po, sskip2(vval(RPICT), 1)); + } else + po = addarg(po, vval(RPICT)); + } } @@ -713,7 +751,7 @@ lowqopts( /* low quality rendering options */ op = addarg(op, "-ds .4"); else op = addarg(op, "-ds 0"); - op = addarg(op, "-dt .2 -dc .25 -dr 0 -sj 0 -st .5"); + op = addarg(op, "-dt .2 -dc .25 -dr 0 -ss 0 -st .5"); if (vdef(AMBFILE)) { sprintf(op, " -af %s", vval(AMBFILE)); op += strlen(op); @@ -735,8 +773,6 @@ lowqopts( /* low quality rendering options */ sprintf(op, " -av %.2g %.2g %.2g", d, d, d); op += strlen(op); op = addarg(op, "-lr 6 -lw .01"); - if (vdef(RENDER)) - op = addarg(op, vval(RENDER)); } @@ -787,10 +823,10 @@ medqopts( /* medium quality rendering options */ } po = addarg(po, "-pt .08"); if (vbool(PENUMBRAS)) - op = addarg(op, "-ds .2 -dj .5"); + op = addarg(op, "-ds .2 -dj .9"); else op = addarg(op, "-ds .3"); - op = addarg(op, "-dt .1 -dc .5 -dr 1 -sj .7 -st .1"); + op = addarg(op, "-dt .1 -dc .5 -dr 1 -ss .7 -st .1"); if ( (overture = vint(INDIRECT)) ) { sprintf(op, " -ab %d", overture); op += strlen(op); @@ -815,8 +851,6 @@ medqopts( /* medium quality rendering options */ sprintf(op, " -av %.2g %.2g %.2g", d, d, d); op += strlen(op); op = addarg(op, "-lr 8 -lw .002"); - if (vdef(RENDER)) - op = addarg(op, vval(RENDER)); } @@ -867,10 +901,10 @@ hiqopts( /* high quality rendering options */ } po = addarg(po, "-pt .04"); if (vbool(PENUMBRAS)) - op = addarg(op, "-ds .1 -dj .65"); + op = addarg(op, "-ds .1 -dj .9"); else op = addarg(op, "-ds .2"); - op = addarg(op, "-dt .05 -dc .75 -dr 3 -sj 1 -st .01"); + op = addarg(op, "-dt .05 -dc .75 -dr 3 -ss 1 -st .01"); sprintf(op, " -ab %d", overture=vint(INDIRECT)+1); op += strlen(op); if (vdef(AMBFILE)) { @@ -893,8 +927,6 @@ hiqopts( /* high quality rendering options */ sprintf(op, " -av %.2g %.2g %.2g", d, d, d); op += strlen(op); op = addarg(op, "-lr 12 -lw .0005"); - if (vdef(RENDER)) - op = addarg(op, vval(RENDER)); } @@ -950,7 +982,11 @@ pfiltopts( /* get pfilt options */ break; } if (vdef(PFILT)) - po = addarg(po, vval(PFILT)); + if (vval(PFILT)[0] != '-') { + atos(c_pfilt, sizeof(c_pfilt), vval(PFILT)); + po = addarg(po, sskip2(vval(PFILT), 1)); + } else + po = addarg(po, vval(PFILT)); } @@ -1087,7 +1123,7 @@ specview( /* get proper view spec from vs */ while (!isspace(*vs)) /* else skip id */ if (!*vs++) return(NULL); - if (upax) { /* specify up vector */ + if (upax) { /* prepend up vector */ strcpy(cp, vup[upax+3]); cp += strlen(cp); } @@ -1116,8 +1152,14 @@ getview( /* get view n, or NULL if none */ if (viewselect != NULL) { /* command-line selected */ if (n) /* only do one */ return(NULL); + + if (isint(viewselect)) { /* view number? */ + n = atoi(viewselect)-1; + goto numview; + } if (viewselect[0] == '-') { /* already specified */ - if (vn != NULL) *vn = '\0'; + if (vn != NULL) + strcpy(vn, "0"); return(viewselect); } if (vn != NULL) { @@ -1126,23 +1168,24 @@ getview( /* get view n, or NULL if none */ ; *vn = '\0'; } - /* view number? */ - if (isint(viewselect)) - return(specview(nvalue(VIEWS, atoi(viewselect)-1))); /* check list */ while ((mv = nvalue(VIEWS, n++)) != NULL) if (matchword(viewselect, mv)) return(specview(mv)); + return(specview(viewselect)); /* standard view? */ } +numview: mv = nvalue(VIEWS, n); /* use view n */ - if ((vn != NULL) & (mv != NULL)) { - register char *mv2 = mv; - if (*mv2 != '-') + if ((vn != NULL) & (mv != NULL)) + if (*mv != '-') { + register char *mv2 = mv; while (*mv2 && !isspace(*mv2)) *vn++ = *mv2++; - *vn = '\0'; - } + *vn = '\0'; + } else + sprintf(vn, "%d", n+1); + return(specview(mv)); } @@ -1196,7 +1239,7 @@ rvu( /* run rvu with first view */ return; if (sayview) myprintview(vw, stdout); - sprintf(combuf, "rvu %s%s%s -R %s ", vw, po, opts, rifname); + sprintf(combuf, "%s %s%s%s -R %s ", c_rvu, vw, opts, po, rifname); if (nprocs > 1) sprintf(combuf+strlen(combuf), "-n %d ", nprocs); if (rvdevice != NULL) @@ -1205,7 +1248,7 @@ rvu( /* run rvu with first view */ sprintf(combuf+strlen(combuf), "-pe %s ", vval(EXPOSURE)); strcat(combuf, oct1name); if (runcom(combuf)) { /* run it */ - fprintf(stderr, "%s: error running rvu\n", progname); + fprintf(stderr, "%s: error running %s\n", progname, c_rvu); quit(1); } } @@ -1217,10 +1260,11 @@ rpict( /* run rpict and pfilt for each view */ char *po ) { - char combuf[PATH_MAX]; +#define do_rpiece (sfile[0]!='\0') + char combuf[4*PATH_MAX+512]; char rawfile[PATH_MAX], picfile[PATH_MAX]; char zopt[PATH_MAX+4], rep[PATH_MAX+16], res[32]; - char rppopt[128], *pfile = NULL; + char rppopt[128], sfile[64], *pfile = NULL; char pfopts[128]; char vs[32], *vw; int vn, mult; @@ -1267,19 +1311,30 @@ rpict( /* run rpict and pfilt for each view */ badvalue(REPORT); } /* set up parallel rendering */ - if ((nprocs > 1) & (!vdef(ZFILE))) { - strcpy(rppopt, "-S 1 -PP pfXXXXXX"); - pfile = rppopt+9; - if (mktemp(pfile) == NULL) + sfile[0] = '\0'; + if ((nprocs > 1) & !touchonly & !vdef(ZFILE) && + getview(0, vs) != NULL) { + if (!strcmp(c_rpict, DEF_RPICT_PATH) && + getview(1, NULL) == NULL) { + sprintf(sfile, "rpsync_%s.txt", vs); + strcpy(rppopt, "-PP pfXXXXXX"); + } else { + strcpy(rppopt, "-S 1 -PP pfXXXXXX"); + } + pfile = rppopt + strlen(rppopt) - 8; + if (mktemp(pfile) == NULL) { + if (do_rpiece) { + fprintf(stderr, "%s: cannot create\n", pfile); + quit(1); + } pfile = NULL; + } } vn = 0; /* do each view */ while ((vw = getview(vn++, vs)) != NULL) { if (sayview) myprintview(vw, stdout); - if (!vs[0]) - sprintf(vs, "%d", vn); - sprintf(picfile, "%s_%s.pic", vval(PICTURE), vs); + sprintf(picfile, "%s_%s.hdr", vval(PICTURE), vs); if (vdef(ZFILE)) sprintf(zopt, " -z %s_%s.zbf", vval(ZFILE), vs); else @@ -1300,64 +1355,108 @@ rpict( /* run rpict and pfilt for each view */ touch(picfile); continue; } - if (next_process()) { /* parallel running? */ + /* parallel running? */ + if (do_rpiece) { + if (rfdt < oct1date || !fdate(sfile)) { + int xdiv = 8+nprocs/3, ydiv = 8+nprocs/3; + rfdt = 0; /* start fresh */ + if (!silent) + printf("\techo %d %d > %s\n", + xdiv, ydiv, sfile); + if ((fp = fopen(sfile, "w")) == NULL) { + fprintf(stderr, "%s: cannot create\n", + sfile); + quit(1); + } + fprintf(fp, "%d %d\n", xdiv, ydiv); + fclose(fp); + } + } else if (next_process(0)) { if (pfile != NULL) - sleep(20); + sleep(10); continue; - } + } else if (!inchild()) + pfile = NULL; /* XXX Remember to call finish_process() */ /* build rpict command */ - if (rfdt >= oct1date) { /* recover */ - sprintf(combuf, "rpict%s%s%s%s -ro %s %s", - rep, po, opts, zopt, rawfile, oct1name); - if (runcom(combuf)) /* run rpict */ + if (rfdt >= oct1date) { /* already in progress */ + if (do_rpiece) { + sprintf(combuf, "%s -R %s %s%s %s %s%s%s -o %s %s", + c_rpiece, sfile, rppopt, rep, vw, + res, opts, po, rawfile, oct1name); + while (next_process(1)) { + sleep(10); + combuf[strlen(c_rpiece)+2] = 'F'; + } + } else + sprintf(combuf, "%s%s%s%s%s -ro %s %s", c_rpict, + rep, opts, po, zopt, rawfile, oct1name); + if (runcom(combuf)) /* run rpict/rpiece */ goto rperror; } else { if (overture) { /* run overture calculation */ sprintf(combuf, - "rpict%s %s%s -x 64 -y 64 -ps 1 %s > %s", - rep, vw, opts, + "%s%s %s%s -x 64 -y 64 -ps 1 %s > %s", + c_rpict, rep, vw, opts, oct1name, overfile); + if (do_rpiece) + while (next_process(1)) + sleep(5); if (runcom(combuf)) { fprintf(stderr, "%s: error in overture for view %s\n", progname, vs); quit(1); } + if (do_rpiece) { + finish_process(); + wait_process(1); + } #ifndef NULL_DEVICE rmfile(overfile); #endif } - sprintf(combuf, "rpict%s %s %s%s%s%s %s > %s", - rep, vw, res, po, opts, - zopt, oct1name, rawfile); - if (pfile != NULL && inchild()) { - /* rpict persistent mode */ + if (do_rpiece) { + sprintf(combuf, "%s -F %s %s%s %s %s%s%s -o %s %s", + c_rpiece, sfile, rppopt, rep, vw, + res, opts, po, rawfile, oct1name); + while (next_process(1)) + sleep(10); + } else { + sprintf(combuf, "%s%s %s %s%s%s%s %s > %s", + c_rpict, rep, vw, res, opts, po, + zopt, oct1name, rawfile); + } + if ((pfile != NULL) & !do_rpiece) { if (!silent) printf("\t%s\n", combuf); fflush(stdout); - sprintf(combuf, "rpict%s %s %s%s%s %s > %s", - rep, rppopt, res, po, opts, - oct1name, rawfile); + sprintf(combuf, "%s%s %s %s%s%s %s > %s", + c_rpict, rep, rppopt, res, + opts, po, oct1name, rawfile); fp = popen(combuf, "w"); if (fp == NULL) goto rperror; myprintview(vw, fp); if (pclose(fp)) goto rperror; - } else { /* rpict normal mode */ - if (runcom(combuf)) - goto rperror; - } + } else if (runcom(combuf)) + goto rperror; } + if (do_rpiece) { /* need to finish raw, first */ + finish_process(); + wait_process(1); + /* XXX should check sync file to see if really done? */ + } if (!vdef(RAWFILE) || strcmp(vval(RAWFILE),vval(PICTURE))) { /* build pfilt command */ if (mult > 1) - sprintf(combuf, "pfilt%s -x /%d -y /%d %s > %s", - pfopts, mult, mult, rawfile, picfile); + sprintf(combuf, "%s%s -x /%d -y /%d %s > %s", + c_pfilt, pfopts, mult, mult, + rawfile, picfile); else - sprintf(combuf, "pfilt%s %s > %s", pfopts, - rawfile, picfile); + sprintf(combuf, "%s%s %s > %s", c_pfilt, + pfopts, rawfile, picfile); if (runcom(combuf)) { /* run pfilt */ fprintf(stderr, "%s: error filtering view %s\n\t%s removed\n", @@ -1368,14 +1467,17 @@ rpict( /* run rpict and pfilt for each view */ } /* remove/rename raw file */ if (vdef(RAWFILE)) { - sprintf(combuf, "%s_%s.pic", vval(RAWFILE), vs); + sprintf(combuf, "%s_%s.hdr", vval(RAWFILE), vs); mvfile(rawfile, combuf); } else rmfile(rawfile); - finish_process(); /* exit if child */ + if (do_rpiece) /* done with sync file */ + rmfile(sfile); + else + finish_process(); /* exit if child */ } wait_process(1); /* wait for children to finish */ - if (pfile != NULL) { /* clean up rpict persistent mode */ + if (pfile != NULL) { /* clean up persistent rpict */ RT_PID pid; fp = fopen(pfile, "r"); if (fp != NULL) { @@ -1389,6 +1491,7 @@ rpict( /* run rpict and pfilt for each view */ rperror: fprintf(stderr, "%s: error rendering view %s\n", progname, vs); quit(1); +#undef do_rpiece } @@ -1401,11 +1504,6 @@ touch( /* update a file */ printf("\ttouch %s\n", fn); if (!nprocs) return(0); -#ifdef notused - if (access(fn, F_OK) == -1) /* create it */ - if (close(open(fn, O_WRONLY|O_CREAT, 0666)) == -1) - return(-1); -#endif return(setfdate(fn, time((time_t *)NULL))); } @@ -1453,7 +1551,7 @@ mvfile( /* move a file */ #ifdef RHAS_FORK_EXEC static int -next_process(void) /* fork the next process (max. nprocs) */ +next_process(int reserve) /* fork the next process */ { RT_PID child_pid; @@ -1464,6 +1562,8 @@ next_process(void) /* fork the next process (max. np progname); quit(1); } + if (reserve > 0 && children_running >= nprocs-reserve) + return(0); /* caller holding back process(es) */ if (children_running >= nprocs) wait_process(0); /* wait for someone to finish */ fflush(NULL); /* flush output */ @@ -1511,7 +1611,7 @@ wait_process( /* wait for process(es) to finish */ } #else /* ! RHAS_FORK_EXEC */ static int -next_process(void) +next_process(int reserve) { return(0); /* cannot start new process */ }