--- ray/src/util/rad.c 2003/07/03 15:00:19 2.66 +++ ray/src/util/rad.c 2003/07/14 20:02:30 2.69 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rad.c,v 2.66 2003/07/03 15:00:19 greg Exp $"; +static const char RCSid[] = "$Id: rad.c,v 2.69 2003/07/14 20:02:30 schorsch Exp $"; #endif /* * Executive program for oconv, rpict and pfilt @@ -8,12 +8,21 @@ static const char RCSid[] = "$Id: rad.c,v 2.66 2003/07 #include "standard.h" #include +#include #include "platform.h" #include "view.h" #include "paths.h" #include "vars.h" +#ifdef _WIN32 + #define DELCMD "del" + #define RENAMECMD "rename" +#else + #define DELCMD "rm -f" + #define RENAMECMD "mv" +#endif + /* variables (alphabetical by name) */ #define AMBFILE 0 /* ambient file name */ #define DETAIL 1 /* level of scene detail */ @@ -80,7 +89,6 @@ char overfile[] = NULL_DEVICE; char overfile[] = "overture.unf"; #endif -extern time_t time(); time_t scenedate; /* date of latest scene or object file */ time_t octreedate; /* date of octree */ @@ -110,7 +118,9 @@ char *rifname; /* global rad input file name */ char radname[PATH_MAX]; /* root Radiance file name */ +#define inchild() (children_running < 0) + main(argc, argv) int argc; char *argv[]; @@ -158,6 +168,9 @@ char *argv[]; 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 */ @@ -1037,8 +1050,10 @@ char *vn; /* returned view name */ } -printview(vopts) /* print out selected view */ +int +myprintview(vopts, fp) /* print out selected view */ register char *vopts; +FILE *fp; { VIEW vwr; char buf[128]; @@ -1059,9 +1074,9 @@ again: viewfile(buf, &vwr, NULL); /* load -vf file */ sscanview(&vwr, cp); /* reset tail */ } - fputs(VIEWSTR, stdout); - fprintview(&vwr, stdout); /* print full spec. */ - fputc('\n', stdout); + fputs(VIEWSTR, fp); + fprintview(&vwr, fp); /* print full spec. */ + fputc('\n', fp); return(0); } @@ -1075,7 +1090,7 @@ char *opts, *po; if (touchonly || (vw = getview(0, NULL)) == NULL) return; if (sayview) - printview(vw); + myprintview(vw, stdout); sprintf(combuf, "rview %s%s%s -R %s ", vw, po, opts, rifname); if (rvdevice != NULL) sprintf(combuf+strlen(combuf), "-o %s ", rvdevice); @@ -1095,9 +1110,11 @@ char *opts, *po; char combuf[PATH_MAX]; char rawfile[PATH_MAX], picfile[PATH_MAX]; char zopt[PATH_MAX+4], rep[PATH_MAX+16], res[32]; + char rppopt[128], *pfile = NULL; char pfopts[128]; char vs[32], *vw; int vn, mult; + FILE *fp; time_t rfdt, pfdt; /* get pfilt options */ pfiltopts(pfopts); @@ -1139,10 +1156,17 @@ char *opts, *po; else badvalue(REPORT); } + /* set up parallel rendering */ + if (nprocs > 1 & !vdef(ZFILE)) { + strcpy(rppopt, "-S 1 -PP pfXXXXXX"); + pfile = rppopt+9; + if (mktemp(pfile) == NULL) + pfile = NULL; + } vn = 0; /* do each view */ while ((vw = getview(vn++, vs)) != NULL) { if (sayview) - printview(vw); + myprintview(vw, stdout); if (!vs[0]) sprintf(vs, "%d", vn); sprintf(picfile, "%s_%s.pic", vval(PICTURE), vs); @@ -1166,14 +1190,19 @@ char *opts, *po; touch(picfile); continue; } - if (next_process()) /* parallel running? */ + if (next_process()) { /* parallel running? */ + if (pfile != NULL) + sleep(20); continue; + } /* XXX Remember to call finish_process() */ /* build rpict command */ - if (rfdt >= oct1date) /* recover */ + if (rfdt >= oct1date) { /* recover */ sprintf(combuf, "rpict%s%s%s%s -ro %s %s", rep, po, opts, zopt, rawfile, oct1name); - else { + if (runcom(combuf)) /* run rpict */ + goto rperror; + } else { if (overture) { /* run overture calculation */ sprintf(combuf, "rpict%s %s%s -x 64 -y 64 -ps 1 %s > %s", @@ -1190,14 +1219,27 @@ char *opts, *po; #endif } sprintf(combuf, "rpict%s %s %s%s%s%s %s > %s", - rep, vw, res, po, opts, zopt, - oct1name, rawfile); + rep, vw, res, po, opts, + zopt, oct1name, rawfile); + if (pfile != NULL && inchild()) { + /* rpict persistent mode */ + if (!silent) + printf("\t%s\n", combuf); + sprintf(combuf, "rpict%s %s %s%s%s %s > %s", + rep, rppopt, res, po, opts, + oct1name, rawfile); + fflush(stdout); + 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; + } } - if (runcom(combuf)) { /* run rpict */ - fprintf(stderr, "%s: error rendering view %s\n", - progname, vs); - quit(1); - } if (!vdef(RAWFILE) || strcmp(vval(RAWFILE),vval(PICTURE))) { /* build pfilt command */ if (mult > 1) @@ -1206,7 +1248,7 @@ char *opts, *po; else sprintf(combuf, "pfilt%s %s > %s", pfopts, rawfile, picfile); - if (runcom(combuf)) { /* run pfilt */ + if (runcom(combuf)) { /* run pfilt */ fprintf(stderr, "%s: error filtering view %s\n\t%s removed\n", progname, vs, picfile); @@ -1223,6 +1265,20 @@ char *opts, *po; finish_process(); /* leave if child */ } wait_process(1); /* wait for children to finish */ + if (pfile != NULL) { /* clean up rpict persistent mode */ + int pid; + fp = fopen(pfile, "r"); + if (fp != NULL) { + if (fscanf(fp, "%*s %d", &pid) != 1 || + kill(pid, 1) == -1) + unlink(pfile); + fclose(fp); + } + } + return; +rperror: + fprintf(stderr, "%s: error rendering view %s\n", progname, vs); + quit(1); } @@ -1258,11 +1314,7 @@ rmfile(fn) /* remove a file */ char *fn; { if (!silent) -#ifdef _WIN32 - printf("\tdel %s\n", fn); -#else - printf("\trm -f %s\n", fn); -#endif + printf("\t%s %s\n", DELCMD, fn); if (!nprocs) return(0); return(unlink(fn)); @@ -1273,11 +1325,7 @@ mvfile(fold, fnew) /* move a file */ char *fold, *fnew; { if (!silent) -#ifdef _WIN32 - printf("\trename %s %s\n", fold, fnew); -#else - printf("\tmv %s %s\n", fold, fnew); -#endif + printf("\t%s %s %s\n", RENAMECMD, fold, fnew); if (!nprocs) return(0); return(rename(fold, fnew)); @@ -1292,7 +1340,7 @@ next_process() /* fork the next process (max. nprocs if (nprocs <= 1) return(0); /* it's us or no one */ - if (children_running < 0) { + if (inchild()) { fprintf(stderr, "%s: internal error 1 in spawn_process()\n", progname); quit(1); @@ -1351,11 +1399,17 @@ int all; { (void)all; /* no one to wait for */ } +int +kill(pid, sig) /* win|unix_process.c should also wait and kill */ +int pid, sig; +{ + return 0; +} #endif /* ! RHAS_FORK_EXEC */ finish_process() /* exit a child process */ { - if (children_running >= 0) + if (!inchild()) return; /* in parent -- noop */ exit(0); }