--- ray/src/util/glareval.c 1991/03/18 12:15:41 1.1 +++ ray/src/util/glareval.c 1991/04/02 14:29:17 1.7 @@ -20,7 +20,7 @@ static char SCCSid[] = "$SunId$ LBL"; #define vfork fork #endif -#define NSCANS 16 /* number of scanlines to buffer */ +#define NSCANS 64 /* number of scanlines to buffer */ int rt_pid = -1; /* process id for rtrace */ int fd_tort, fd_fromrt; /* pipe descriptors */ @@ -36,13 +36,16 @@ struct { COLR *sl; /* scanline contents */ } scan[NSCANS]; /* buffered scanlines */ +static long ncall = 0L; /* number of calls to getpictscan */ +static long nread = 0L; /* number of scanlines read */ +extern long ftell(); + + COLR * getpictscan(y) /* get picture scanline */ int y; { - extern long ftell(); - static long ncall = 0; int minused; register int i; /* first check our buffers */ @@ -57,34 +60,57 @@ int y; minused = i; } /* not there, read it in */ - if (scanpos[y] == -1) { /* need to search */ - if (verbose) - fprintf(stderr, "%s: reading picture...\n", - progname); - while (curpos > y) { + if (scanpos[y] < 0) { /* need to search */ + for (i = y+1; i < curpos; i++) + if (scanpos[i] >= 0) { + if (fseek(pictfp, scanpos[i], 0) < 0) + goto seekerr; + curpos = i; + break; + } + while (curpos >= y) { scanpos[curpos] = ftell(pictfp); if (freadcolrs(scan[minused].sl, pxsiz, pictfp) < 0) goto readerr; + nread++; curpos--; } - } else if (fseek(pictfp, scanpos[y], 0) < 0) { - fprintf(stderr, "%s: picture seek error\n", progname); - exit(1); + } else { + if (curpos != y && fseek(pictfp, scanpos[y], 0) < 0) + goto seekerr; + if (freadcolrs(scan[minused].sl, pxsiz, pictfp) < 0) + goto readerr; + nread++; + curpos = y-1; } - if (verbose) - fprintf(stderr, "%s: reading scanline %d...\n", progname, y); - if (freadcolrs(scan[minused].sl, pxsiz, pictfp) < 0) - goto readerr; - curpos = y-1; scan[minused].lused = ncall; scan[minused].y = y; return(scan[minused].sl); readerr: fprintf(stderr, "%s: picture read error\n", progname); exit(1); +seekerr: + fprintf(stderr, "%s: picture seek error\n", progname); + exit(1); } +#ifdef DEBUG +pict_stats() /* print out picture read statistics */ +{ + static long lastcall = 0L; /* ncall at last report */ + static long lastread = 0L; /* nread at last report */ + + if (ncall == lastcall) + return; + fprintf(stderr, "%s: %ld scanlines read in %ld calls\n", + progname, nread-lastread, ncall-lastcall); + lastcall = ncall; + lastread = nread; +} +#endif + + double pict_val(vd) /* find picture value for view direction */ FVECT vd; @@ -116,10 +142,13 @@ int vh, vv; if (compdir(dir, vh, vv) < 0) return(-1.0); + npixinvw++; if ((res = pict_val(dir)) >= 0.0) return(res); - if (rt_pid == -1) + if (rt_pid == -1) { + npixmiss++; return(-1.0); + } rt_buf[0] = ourview.vp[0]; rt_buf[1] = ourview.vp[1]; rt_buf[2] = ourview.vp[2]; @@ -136,44 +165,51 @@ int vv; float *vb; { float rt_buf[6*MAXPIX]; /* rtrace send/receive buffer */ - int npix_tort; /* number of pixels in buffer */ + register int n; /* number of pixels in buffer */ short buf_vh[MAXPIX]; /* pixel positions */ FVECT dir; register int vh; - register int i; +#ifdef DEBUG if (verbose) fprintf(stderr, "%s: computing view span at %d...\n", progname, vv); - npix_tort = 0; +#endif + n = 0; for (vh = -hsize; vh <= hsize; vh++) { if (compdir(dir, vh, vv) < 0) { /* off viewable region */ vb[vh+hsize] = -1.0; continue; } + npixinvw++; if ((vb[vh+hsize] = pict_val(dir)) >= 0.0) continue; - if (rt_pid == -1) /* missing information */ + if (rt_pid == -1) { /* missing information */ + npixmiss++; continue; + } /* send to rtrace */ - if (npix_tort >= MAXPIX) { /* flush */ - rt_compute(rt_buf, npix_tort); - for (i = 0; i < npix_tort; i++) - vb[buf_vh[i]+hsize] = luminance(rt_buf+3*i); - npix_tort = 0; + if (n >= MAXPIX) { /* flush */ + rt_compute(rt_buf, n); + while (n-- > 0) + vb[buf_vh[n]+hsize] = luminance(rt_buf+3*n); } - rt_buf[npix_tort] = ourview.vp[0]; - rt_buf[npix_tort+1] = ourview.vp[1]; - rt_buf[npix_tort+2] = ourview.vp[2]; - rt_buf[npix_tort+3] = dir[0]; - rt_buf[npix_tort+4] = dir[1]; - rt_buf[npix_tort+5] = dir[2]; - buf_vh[npix_tort++] = vh; + rt_buf[6*n] = ourview.vp[0]; + rt_buf[6*n+1] = ourview.vp[1]; + rt_buf[6*n+2] = ourview.vp[2]; + rt_buf[6*n+3] = dir[0]; + rt_buf[6*n+4] = dir[1]; + rt_buf[6*n+5] = dir[2]; + buf_vh[n++] = vh; } - if (npix_tort > 0) { /* process pending buffer */ - rt_compute(rt_buf, npix_tort); - for (i = 0; i < npix_tort; i++) - vb[buf_vh[i]+hsize] = luminance(rt_buf+3*i); +#ifdef DEBUG + if (verbose) + pict_stats(); +#endif + if (n > 0) { /* process pending buffer */ + rt_compute(rt_buf, n); + while (n-- > 0) + vb[buf_vh[n]+hsize] = luminance(rt_buf+3*n); } } @@ -184,9 +220,11 @@ int np; { static float nbuf[6] = {0.,0.,0.,0.,0.,0.}; +#ifdef DEBUG if (verbose && np > 1) fprintf(stderr, "%s: sending %d samples to rtrace...\n", progname, np); +#endif if (writebuf(fd_tort,(char *)pb,6*sizeof(float)*np) < 6*sizeof(float)*np || writebuf(fd_tort,(char *)nbuf,sizeof(nbuf)) < sizeof(nbuf)) { fprintf(stderr, "%s: error writing to rtrace process\n", @@ -228,8 +266,9 @@ char *fn; scanpos = (long *)malloc(pysiz*sizeof(long)); if (scanpos == NULL) memerr("scanline positions"); - for (i = 0; i < pysiz; i++) + for (i = pysiz-1; i >= 0; i--) scanpos[i] = -1L; + curpos = pysiz-1; for (i = 0; i < NSCANS; i++) { scan[i].lused = -1; scan[i].y = -1; @@ -237,7 +276,6 @@ char *fn; if (scan[i].sl == NULL) memerr("scanline buffers"); } - curpos = pysiz-1; } @@ -283,6 +321,8 @@ char *av[]; perror(progname); exit(1); } + close(p0[0]); + close(p1[1]); fd_tort = p0[1]; fd_fromrt = p1[0]; }