ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/glareval.c
(Generate patch)

Comparing ray/src/util/glareval.c (file contents):
Revision 1.1 by greg, Mon Mar 18 12:15:41 1991 UTC vs.
Revision 1.11 by greg, Fri Apr 5 14:57:28 1991 UTC

# Line 20 | Line 20 | static char SCCSid[] = "$SunId$ LBL";
20   #define vfork           fork
21   #endif
22  
23 < #define NSCANS          16              /* number of scanlines to buffer */
23 > #define MAXSBUF         524268  /* maximum total size of scanline buffer */
24 > #define HSIZE           317     /* size of scanline hash table */
25 > #define NRETIRE         16      /* number of scanlines to retire at once */
26  
27   int     rt_pid = -1;            /* process id for rtrace */
28   int     fd_tort, fd_fromrt;     /* pipe descriptors */
# Line 28 | Line 30 | int    fd_tort, fd_fromrt;     /* pipe descriptors */
30   FILE    *pictfp = NULL;         /* picture file pointer */
31   double  exposure;               /* picture exposure */
32   int     pxsiz, pysiz;           /* picture dimensions */
33 < int     curpos;                 /* current scanline */
34 < long    *scanpos;               /* scanline positions */
35 < struct {
36 <        long    lused;          /* for LRU replacement */
33 >
34 > static int      curpos;         /* current scanline */
35 > static long     *scanpos;       /* scanline positions */
36 >
37 > typedef struct scan {
38          int     y;              /* scanline position */
39 <        COLR    *sl;            /* scanline contents */
40 < } scan[NSCANS];         /* buffered scanlines */
39 >        long    lused;          /* for LRU replacement */
40 >        struct scan     *next;  /* next in this hash or free list */
41 >        /* followed by the scanline data */
42 > } SCAN;                 /* buffered scanline */
43  
44 + #define scandata(sl)    ((COLR *)((sl)+1))
45 + #define shash(y)        ((y)%HSIZE)
46  
47 + static SCAN     *freelist;              /* scanline free list */
48 + static SCAN     *hashtab[HSIZE];        /* scanline hash table */
49 +
50 + static long     ncall = 0L;     /* number of calls to getpictscan */
51 + static long     nread = 0L;     /* number of scanlines read */
52 +
53 + SCAN    *scanretire();
54 +
55 + extern long     ftell();
56 +
57 +
58 + SCAN *
59 + claimscan(y)                    /* claim scanline from buffers */
60 + int     y;
61 + {
62 +        int     hi = shash(y);
63 +        SCAN    *slast;
64 +        register SCAN   *sl;
65 +
66 +        for (sl = hashtab[hi]; sl != NULL; sl = sl->next)
67 +                if (sl->y == y)                         /* active scanline */
68 +                        return(sl);
69 +        for (slast = NULL, sl = freelist; sl != NULL; slast = sl, sl = sl->next)
70 +                if (sl->y == -1 || sl->y == y || sl->next == NULL) {
71 +                        if (slast == NULL)              /* remove from free */
72 +                                freelist = sl->next;
73 +                        else
74 +                                slast->next = sl->next;
75 +                        if (sl->y == y) {               /* reclaim */
76 +                                sl->next = hashtab[hi];
77 +                                hashtab[hi] = sl;
78 + #ifdef DEBUG
79 +                                if (verbose)
80 +                                        fprintf(stderr,
81 +                                                "%s: scanline %d reclaimed\n",
82 +                                                        progname, y);
83 + #endif
84 +                        }
85 +                        return(sl);
86 +                }
87 +        return(scanretire());           /* need more free scanlines */
88 + }
89 +
90 +
91   COLR *
92   getpictscan(y)                  /* get picture scanline */
93   int     y;
94   {
95 <        extern long     ftell();
45 <        static long     ncall = 0;
46 <        int     minused;
95 >        register SCAN   *sl;
96          register int    i;
97                                          /* first check our buffers */
98 <        ncall++;
99 <        minused = 0;
100 <        for (i = 0; i < NSCANS; i++) {
101 <                if (scan[i].y == y) {
102 <                        scan[i].lused = ncall;
103 <                        return(scan[i].sl);
104 <                }
105 <                if (scan[i].lused < scan[minused].lused)
106 <                        minused = i;
107 <        }
108 <                                        /* not there, read it in */
109 <        if (scanpos[y] == -1) {                 /* need to search */
110 <                if (verbose)
111 <                        fprintf(stderr, "%s: reading picture...\n",
112 <                                        progname);
113 <                while (curpos > y) {
98 >        sl = claimscan(y);
99 >        if (sl == NULL)
100 >                memerr("claimscan()");
101 >        sl->lused = ncall++;
102 >        if (sl->y == y)                 /* scan hit */
103 >                return(scandata(sl));
104 >                                        /* else read in replacement */
105 >        if (scanpos[y] < 0) {                   /* need to search */
106 >                for (i = y+1; i < curpos; i++)
107 >                        if (scanpos[i] >= 0) {
108 >                                if (fseek(pictfp, scanpos[i], 0) < 0)
109 >                                        goto seekerr;
110 >                                curpos = i;
111 >                                break;
112 >                        }
113 >                while (curpos >= y) {
114                          scanpos[curpos] = ftell(pictfp);
115 <                        if (freadcolrs(scan[minused].sl, pxsiz, pictfp) < 0)
115 >                        if (freadcolrs(scandata(sl), pxsiz, pictfp) < 0)
116                                  goto readerr;
117 +                        nread++;
118                          curpos--;
119                  }
120 <        } else if (fseek(pictfp, scanpos[y], 0) < 0) {
121 <                fprintf(stderr, "%s: picture seek error\n", progname);
122 <                exit(1);
120 >        } else {
121 >                if (curpos != y && fseek(pictfp, scanpos[y], 0) < 0)
122 >                        goto seekerr;
123 >                if (freadcolrs(scandata(sl), pxsiz, pictfp) < 0)
124 >                        goto readerr;
125 >                nread++;
126 >                curpos = y-1;
127          }
128 <        if (verbose)
129 <                fprintf(stderr, "%s: reading scanline %d...\n", progname, y);
130 <        if (freadcolrs(scan[minused].sl, pxsiz, pictfp) < 0)
131 <                goto readerr;
132 <        curpos = y-1;
79 <        scan[minused].lused = ncall;
80 <        scan[minused].y = y;
81 <        return(scan[minused].sl);
128 >        sl->y = y;
129 >        i = shash(y);                   /* add to hash list */
130 >        sl->next = hashtab[i];
131 >        hashtab[i] = sl;
132 >        return(scandata(sl));
133   readerr:
134          fprintf(stderr, "%s: picture read error\n", progname);
135          exit(1);
136 + seekerr:
137 +        fprintf(stderr, "%s: picture seek error\n", progname);
138 +        exit(1);
139   }
140  
141  
142 + #ifdef DEBUG
143 + pict_stats()                    /* print out picture read statistics */
144 + {
145 +        static long     lastcall = 0L;  /* ncall at last report */
146 +        static long     lastread = 0L;  /* nread at last report */
147 +
148 +        if (ncall == lastcall)
149 +                return;
150 +        fprintf(stderr, "%s: %ld scanlines read in %ld calls\n",
151 +                        progname, nread-lastread, ncall-lastcall);
152 +        lastcall = ncall;
153 +        lastread = nread;
154 + }
155 + #endif
156 +
157 +
158   double
159   pict_val(vd)                    /* find picture value for view direction */
160   FVECT   vd;
# Line 116 | Line 186 | int    vh, vv;
186  
187          if (compdir(dir, vh, vv) < 0)
188                  return(-1.0);
189 +        npixinvw++;
190          if ((res = pict_val(dir)) >= 0.0)
191                  return(res);
192 <        if (rt_pid == -1)
192 >        if (rt_pid == -1) {
193 >                npixmiss++;
194                  return(-1.0);
195 +        }
196          rt_buf[0] = ourview.vp[0];
197          rt_buf[1] = ourview.vp[1];
198          rt_buf[2] = ourview.vp[2];
# Line 136 | Line 209 | int    vv;
209   float   *vb;
210   {
211          float   rt_buf[6*MAXPIX];       /* rtrace send/receive buffer */
212 <        int     npix_tort;              /* number of pixels in buffer */
212 >        register int    n;              /* number of pixels in buffer */
213          short   buf_vh[MAXPIX];         /* pixel positions */
214          FVECT   dir;
215          register int    vh;
143        register int    i;
216  
217 + #ifdef DEBUG
218          if (verbose)
219                  fprintf(stderr, "%s: computing view span at %d...\n",
220                                  progname, vv);
221 <        npix_tort = 0;
221 > #endif
222 >        n = 0;
223          for (vh = -hsize; vh <= hsize; vh++) {
224                  if (compdir(dir, vh, vv) < 0) { /* off viewable region */
225                          vb[vh+hsize] = -1.0;
226                          continue;
227                  }
228 +                npixinvw++;
229                  if ((vb[vh+hsize] = pict_val(dir)) >= 0.0)
230                          continue;
231 <                if (rt_pid == -1)               /* missing information */
231 >                if (rt_pid == -1) {             /* missing information */
232 >                        npixmiss++;
233                          continue;
234 +                }
235                                                  /* send to rtrace */
236 <                if (npix_tort >= MAXPIX) {              /* flush */
237 <                        rt_compute(rt_buf, npix_tort);
238 <                        for (i = 0; i < npix_tort; i++)
239 <                                vb[buf_vh[i]+hsize] = luminance(rt_buf+3*i);
163 <                        npix_tort = 0;
236 >                if (n >= MAXPIX) {                      /* flush */
237 >                        rt_compute(rt_buf, n);
238 >                        while (n-- > 0)
239 >                                vb[buf_vh[n]+hsize] = luminance(rt_buf+3*n);
240                  }
241 <                rt_buf[npix_tort] = ourview.vp[0];
242 <                rt_buf[npix_tort+1] = ourview.vp[1];
243 <                rt_buf[npix_tort+2] = ourview.vp[2];
244 <                rt_buf[npix_tort+3] = dir[0];
245 <                rt_buf[npix_tort+4] = dir[1];
246 <                rt_buf[npix_tort+5] = dir[2];
247 <                buf_vh[npix_tort++] = vh;
241 >                rt_buf[6*n] = ourview.vp[0];
242 >                rt_buf[6*n+1] = ourview.vp[1];
243 >                rt_buf[6*n+2] = ourview.vp[2];
244 >                rt_buf[6*n+3] = dir[0];
245 >                rt_buf[6*n+4] = dir[1];
246 >                rt_buf[6*n+5] = dir[2];
247 >                buf_vh[n++] = vh;
248          }
249 <        if (npix_tort > 0) {                    /* process pending buffer */
250 <                rt_compute(rt_buf, npix_tort);
251 <                for (i = 0; i < npix_tort; i++)
252 <                        vb[buf_vh[i]+hsize] = luminance(rt_buf+3*i);
249 > #ifdef DEBUG
250 >        if (verbose)
251 >                pict_stats();
252 > #endif
253 >        if (n > 0) {                            /* process pending buffer */
254 >                rt_compute(rt_buf, n);
255 >                while (n-- > 0)
256 >                        vb[buf_vh[n]+hsize] = luminance(rt_buf+3*n);
257          }
258   }
259  
# Line 184 | Line 264 | int    np;
264   {
265          static float    nbuf[6] = {0.,0.,0.,0.,0.,0.};
266  
267 + #ifdef DEBUG
268          if (verbose && np > 1)
269                  fprintf(stderr, "%s: sending %d samples to rtrace...\n",
270                                  progname, np);
271 + #endif
272          if (writebuf(fd_tort,(char *)pb,6*sizeof(float)*np) < 6*sizeof(float)*np
273                  || writebuf(fd_tort,(char *)nbuf,sizeof(nbuf)) < sizeof(nbuf)) {
274                  fprintf(stderr, "%s: error writing to rtrace process\n",
# Line 225 | Line 307 | char   *fn;
307                  fprintf("%s: bad picture resolution\n", fn);
308                  exit(1);
309          }
310 <        scanpos = (long *)malloc(pysiz*sizeof(long));
229 <        if (scanpos == NULL)
230 <                memerr("scanline positions");
231 <        for (i = 0; i < pysiz; i++)
232 <                scanpos[i] = -1L;
233 <        for (i = 0; i < NSCANS; i++) {
234 <                scan[i].lused = -1;
235 <                scan[i].y = -1;
236 <                scan[i].sl = (COLR *)malloc(pxsiz*sizeof(COLR));
237 <                if (scan[i].sl == NULL)
238 <                        memerr("scanline buffers");
239 <        }
240 <        curpos = pysiz-1;
310 >        initscans();
311   }
312  
313  
# Line 248 | Line 318 | close_pict()                   /* done with picture */
318          if (pictfp == NULL)
319                  return;
320          fclose(pictfp);
321 <        free((char *)scanpos);
252 <        for (i = 0; i < NSCANS; i++)
253 <                free((char *)scan[i].sl);
321 >        donescans();
322          pictfp = NULL;
323   }
324  
# Line 283 | Line 351 | char   *av[];
351                  perror(progname);
352                  exit(1);
353          }
354 +        close(p0[0]);
355 +        close(p1[1]);
356          fd_tort = p0[1];
357          fd_fromrt = p1[0];
358   }
# Line 339 | Line 409 | int    siz;
409          if (cc < 0)
410                  return(cc);
411          return(siz-nrem);
412 + }
413 +
414 +
415 + SCAN *
416 + scanretire()                    /* retire old scanlines to free list */
417 + {
418 +        SCAN    *sold[NRETIRE];
419 +        int     n;
420 +        int     h;
421 +        register SCAN   *sl;
422 +        register int    i;
423 +                                        /* grab the NRETIRE oldest scanlines */
424 +        sold[n = 0] = NULL;
425 +        for (h = 0; h < HSIZE; h++)
426 +                for (sl = hashtab[h]; sl != NULL; sl = sl->next) {
427 +                        for (i = n; i && sold[i-1]->lused > sl->lused; i--)
428 +                                if (i < NRETIRE)
429 +                                        sold[i] = sold[i-1];
430 +                        if (i < NRETIRE) {
431 +                                sold[i] = sl;
432 +                                if (n < NRETIRE)        /* grow list */
433 +                                        n++;
434 +                        }
435 +                }
436 +                                        /* put scanlines into free list */
437 +        for (i = 0; i < n; i++) {
438 +                h = shash(sold[i]->y);
439 +                sl = hashtab[h];
440 +                if (sl == sold[i])
441 +                        hashtab[h] = sl->next;
442 +                else {
443 +                        while (sl->next != sold[i])     /* IS in list */
444 +                                sl = sl->next;
445 +                        sl->next = sold[i]->next;
446 +                }
447 +                if (i > 0) {            /* save oldest as return value */
448 +                        sold[i]->next = freelist;
449 +                        freelist = sold[i];
450 +                }
451 +        }
452 +        return(sold[0]);
453 + }
454 +
455 +
456 + static char     *scan_buf;
457 +
458 +
459 + initscans()                             /* initialize scanline buffers */
460 + {
461 +        int     scansize;
462 +        register SCAN   *ptr;
463 +        register int    i;
464 +                                        /* initialize positions */
465 +        scanpos = (long *)malloc(pysiz*sizeof(long));
466 +        if (scanpos == NULL)
467 +                memerr("scanline positions");
468 +        for (i = pysiz-1; i >= 0; i--)
469 +                scanpos[i] = -1L;
470 +        curpos = pysiz-1;
471 +                                        /* clear hash table */
472 +        for (i = 0; i < HSIZE; i++)
473 +                hashtab[i] = NULL;
474 +                                        /* allocate scanline buffers */
475 +        scansize = sizeof(SCAN) + pxsiz*sizeof(COLR);
476 + #ifdef ALIGN
477 +        scansize = scansize+(sizeof(ALIGN)-1)) & ~(sizeof(ALIGN)-1);
478 + #endif
479 +        i = MAXSBUF / scansize;         /* compute number to allocate */
480 +        if (i > HSIZE)
481 +                i = HSIZE;
482 +        scan_buf = malloc(i*scansize);  /* get in one big chunk */
483 +        if (scan_buf == NULL)
484 +                memerr("scanline buffers");
485 +        ptr = (SCAN *)scan_buf;
486 +        freelist = NULL;                /* build our free list */
487 +        while (i-- > 0) {
488 +                ptr->y = -1;
489 +                ptr->lused = -1;
490 +                ptr->next = freelist;
491 +                freelist = ptr;
492 +                ptr = (SCAN *)((char *)ptr + scansize); /* beware of C bugs */
493 +        }
494 + }
495 +
496 +
497 + donescans()                             /* free up scanlines */
498 + {
499 +        free(scan_buf);
500 +        free((char *)scanpos);
501   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines