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

Comparing ray/src/rt/ambient.c (file contents):
Revision 2.26 by greg, Thu Apr 27 14:12:05 1995 UTC vs.
Revision 2.37 by greg, Tue May 28 14:24:44 1996 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1993 Regents of the University of California */
1 > /* Copyright (c) 1995 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 18 | Line 18 | static char SCCSid[] = "$SunId$ LBL";
18  
19   #include  "random.h"
20  
21 < #define  OCTSCALE       0.5     /* ceil((valid rad.)/(cube size)) */
21 > #ifndef  OCTSCALE
22 > #define  OCTSCALE       1.0     /* ceil((valid rad.)/(cube size)) */
23 > #endif
24  
25   typedef struct ambtree {
26          AMBVAL  *alist;         /* ambient value list */
# Line 27 | Line 29 | typedef struct ambtree {
29  
30   extern CUBE  thescene;          /* contains space boundaries */
31  
32 < extern char  *shm_boundary;     /* shared memory boundary */
32 > extern char  *shm_boundary;     /* memory sharing boundary */
33  
34   #define  MAXASET        511     /* maximum number of elements in ambient set */
35   OBJECT  ambset[MAXASET+1]={0};  /* ambient include/exclude set */
# Line 42 | Line 44 | static int  nunflshed = 0;     /* number of unflushed ambi
44  
45   #ifndef SORT_THRESH
46   #ifdef BIGMEM
47 < #define SORT_THRESH     (6*(1L<<20)/sizeof(AMBVAL))
47 > #define SORT_THRESH     ((9L<<20)/sizeof(AMBVAL))
48   #else
49 < #define SORT_THRESH     (2*(1L<<20)/sizeof(AMBVAL))
49 > #define SORT_THRESH     ((3L<<20)/sizeof(AMBVAL))
50   #endif
51   #endif
52   #ifndef SORT_INTVL
53 < #define SORT_INTVL      (SORT_THRESH*2)
53 > #define SORT_INTVL      (SORT_THRESH*256)
54   #endif
55 + #ifndef MAX_SORT_INTVL
56 + #define MAX_SORT_INTVL  (SORT_INTVL<<4)
57 + #endif
58  
59 < static long  ambclock = 0;      /* ambient access clock */
60 < static int  nambvals = 0;       /* number of stored ambient values */
61 < static long  lastsort = 0;      /* time of last value sort */
59 > static COLOR  avsum = BLKCOLOR;         /* computed ambient value sum */
60 > static unsigned int  nambvals = 0;      /* total number of indirect values */
61 > static unsigned int  nambshare = 0;     /* number of values from file */
62 > static unsigned long  ambclock = 0;     /* ambient access clock */
63 > static unsigned long  lastsort = 0;     /* time of last value sort */
64   static long  sortintvl = SORT_INTVL;    /* time until next sort */
65  
66   #define MAXACLOCK       (1L<<30)        /* clock turnover value */
67 < #define tracktime       (shm_boundary == NULL || ambfp == NULL)
68 < #define need2sort       (ambclock > lastsort+sortintvl && \
69 <                                nambvals > SORT_THRESH)
67 >        /*
68 >         * Track access times unless we are sharing ambient values
69 >         * through memory on a multiprocessor, when we want to avoid
70 >         * claiming our own memory (copy on write).  Go ahead anyway
71 >         * if more than two thirds of our values are unshared.
72 >         */
73 > #define tracktime       (shm_boundary == NULL || nambvals > 3*nambshare)
74  
75   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
76  
# Line 83 | Line 94 | int  ar;
94                  maxarad = thescene.cusize / 2.0;
95          } else {
96                  minarad = thescene.cusize / ar;
97 <                maxarad = 16 * minarad;                 /* heuristic */
97 >                maxarad = 64 * minarad;                 /* heuristic */
98                  if (maxarad > thescene.cusize / 2.0)
99                          maxarad = thescene.cusize / 2.0;
100          }
# Line 97 | Line 108 | int  ar;
108   setambacc(newa)                         /* set ambient accuracy */
109   double  newa;
110   {
111 <        static double  oldambacc = -1.0;
111 >        double  ambdiff;
112  
113 <        ambacc = newa < 0.0 ? 0.0 : newa;       /* may be done already */
114 <        if (oldambacc < -FTINY)
115 <                oldambacc = ambacc;     /* do nothing first call */
116 <        if (fabs(newa - oldambacc) < 0.01)
117 <                return;                 /* insignificant -- don't bother */
107 <        if (ambacc <= FTINY)
108 <                return;                 /* cannot build new tree */
109 <                                        /* else need to rebuild tree */
110 <        sortambvals(1);
111 <        oldambacc = ambacc;             /* remeber setting for next call */
113 >        if (newa < 0.0)
114 >                newa = 0.0;
115 >        ambdiff = fabs(newa - ambacc);
116 >        if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0)
117 >                sortambvals(1);                 /* rebuild tree */
118   }
119  
120  
121   setambient(afile)                       /* initialize calculation */
122   char  *afile;
123   {
124 <        long  headlen;
124 >        long  pos, flen;
125          AMBVAL  amb;
126                                                  /* init ambient limits */
127          setambres(ambres);
# Line 131 | Line 137 | char  *afile;
137                                                  /* open ambient file */
138          if ((ambfp = fopen(afile, "r+")) != NULL) {
139                  initambfile(0);
140 <                headlen = ftell(ambfp);
140 >                pos = ftell(ambfp);
141                  while (readambval(&amb, ambfp))
142                          avinsert(avstore(&amb));
143                                                  /* align */
144 <                fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
144 >                pos += (long)nambvals*AMBVALSIZ;
145 >                flen = lseek(fileno(ambfp), 0L, 2);
146 >                if (flen != pos) {
147 >                        error(WARNING,
148 >                        "ignoring last %ld values in ambient file (corrupted)",
149 >                                        (flen - pos)/AMBVALSIZ);
150 >                        fseek(ambfp, pos, 0);
151 >                        ftruncate(fileno(ambfp), pos);
152 >                }
153 >                nambshare = nambvals;
154          } else if ((ambfp = fopen(afile, "w+")) != NULL)
155                  initambfile(1);
156          else {
# Line 169 | Line 184 | OBJECT obj;
184   }
185  
186  
187 < ambient(acol, r)                /* compute ambient component for ray */
187 > ambient(acol, r, nrm)           /* compute ambient component for ray */
188   COLOR  acol;
189   register RAY  *r;
190 + FVECT  nrm;
191   {
192          static int  rdepth = 0;                 /* ambient recursion */
193          double  d;
# Line 190 | Line 206 | register RAY  *r;
206                  rdepth++;
207                  d = doambient(acol, r, r->rweight, NULL, NULL);
208                  rdepth--;
209 <                if (d == 0.0)
209 >                if (d <= FTINY)
210                          goto dumbamb;
211                  return;
212          }
213 +                                                /* resort memory? */
214 +        sortambvals(0);
215                                                  /* get ambient value */
198        if (need2sort)
199                sortambvals(0);
216          setcolor(acol, 0.0, 0.0, 0.0);
217 <        d = sumambient(acol, r, rdepth,
217 >        d = sumambient(acol, r, nrm, rdepth,
218                          &atrunk, thescene.cuorg, thescene.cusize);
219 <        if (d > FTINY)
219 >        if (d > FTINY) {
220                  scalecolor(acol, 1.0/d);
221 <        else {
206 <                d = makeambient(acol, r, rdepth++);
207 <                rdepth--;
221 >                return;
222          }
223 +        rdepth++;                               /* need to cache new value */
224 +        d = makeambient(acol, r, nrm, rdepth-1);
225 +        rdepth--;
226          if (d > FTINY)
227                  return;
228   dumbamb:                                        /* return global value */
229          copycolor(acol, ambval);
230 +        if (ambvwt <= 0 | nambvals == 0)
231 +                return;
232 +        scalecolor(acol, (double)ambvwt);
233 +        addcolor(acol, avsum);                  /* average in computations */
234 +        d = 1.0/(ambvwt+nambvals);
235 +        scalecolor(acol, d);
236   }
237  
238  
239   double
240 < sumambient(acol, r, al, at, c0, s)      /* get interpolated ambient value */
240 > sumambient(acol, r, rn, al, at, c0, s)  /* get interpolated ambient value */
241   COLOR  acol;
242   register RAY  *r;
243 + FVECT  rn;
244   int  al;
245   AMBTREE  *at;
246   FVECT  c0;
# Line 228 | Line 252 | double s;
252          int  i;
253          register int  j;
254          register AMBVAL  *av;
255 <                                        /* do this node */
255 >
256          wsum = 0.0;
257 +                                        /* do this node */
258          for (av = at->alist; av != NULL; av = av->next) {
259                  if (tracktime)
260                          av->latick = ambclock++;
# Line 243 | Line 268 | double s;
268                  /*
269                   *  Ambient radius test.
270                   */
271 <                e1 = 0.0;
272 <                for (j = 0; j < 3; j++) {
273 <                        d = av->pos[j] - r->rop[j];
274 <                        e1 += d * d;
275 <                }
271 >                d = av->pos[0] - r->rop[0];
272 >                e1 = d * d;
273 >                d = av->pos[1] - r->rop[1];
274 >                e1 += d * d;
275 >                d = av->pos[2] - r->rop[2];
276 >                e1 += d * d;
277                  e1 /= av->rad * av->rad;
278                  if (e1 > ambacc*ambacc*1.21)
279                          continue;
# Line 271 | Line 297 | double s;
297                   *  Jittering final test reduces image artifacts.
298                   */
299                  wt = sqrt(e1) + sqrt(e2);
300 <                wt *= .9 + .2*urand(9015+samplendx);
275 <                if (wt > ambacc)
300 >                if (wt > ambacc*(.9+.2*urand(9015+samplendx)))
301                          continue;
302                  if (wt <= 1e-3)
303                          wt = 1e3;
304                  else
305                          wt = 1.0 / wt;
306                  wsum += wt;
307 <                extambient(ct, av, r->rop, r->ron);
307 >                extambient(ct, av, r->rop, rn);
308                  scalecolor(ct, wt);
309                  addcolor(acol, ct);
310          }
# Line 298 | Line 323 | double s;
323                                  break;
324                  }
325                  if (j == 3)
326 <                        wsum += sumambient(acol, r, al, at->kid+i, ck0, s);
326 >                        wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s);
327          }
328          return(wsum);
329   }
330  
331  
332   double
333 < makeambient(acol, r, al)        /* make a new ambient value */
333 > makeambient(acol, r, rn, al)    /* make a new ambient value */
334   COLOR  acol;
335   register RAY  *r;
336 + FVECT  rn;
337   int  al;
338   {
339          AMBVAL  amb;
340          FVECT   gp, gd;
341                                                  /* compute weight */
342          amb.weight = pow(AVGREFL, (double)al);
343 <        if (r->rweight < 0.2*amb.weight)        /* heuristic */
343 >        if (r->rweight < 0.1*amb.weight)        /* heuristic */
344                  amb.weight = r->rweight;
345                                                  /* compute ambient */
346          amb.rad = doambient(acol, r, amb.weight, gp, gd);
347 <        if (amb.rad == 0.0)
347 >        if (amb.rad <= FTINY)
348                  return(0.0);
349                                                  /* store it */
350          VCOPY(amb.pos, r->rop);
# Line 329 | Line 355 | int  al;
355          VCOPY(amb.gdir, gd);
356                                                  /* insert into tree */
357          avsave(&amb);                           /* and save to file */
358 +        if (rn != r->ron)
359 +                extambient(acol, &amb, r->rop, rn);     /* texture */
360          return(amb.rad);
361   }
362  
# Line 419 | Line 447 | register AMBVAL  *aval;
447          copystruct(av, aval);
448          av->latick = ambclock;
449          av->next = NULL;
450 +        addcolor(avsum, av->val);       /* add to sum for averaging */
451          nambvals++;
452          return(av);
453   }
# Line 530 | Line 559 | static
559   av2list(av)
560   AMBVAL  *av;
561   {
562 + #ifdef DEBUG
563          if (i_avlist >= nambvals)
564                  error(CONSISTENCY, "too many ambient values in av2list1");
565 + #endif
566          avlist1[i_avlist] = avlist2[i_avlist] = av;
567          i_avlist++;
568   }
# Line 553 | Line 584 | AMBVAL **avp1, **avp2;
584   }
585  
586  
587 + #ifdef DEBUG
588 + static int
589 + avlmemi(avaddr)                         /* find list position from address */
590 + AMBVAL  *avaddr;
591 + {
592 +        register AMBVAL  **avlpp;
593 +
594 +        avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
595 +                        nambvals, sizeof(AMBVAL *), aposcmp);
596 +        if (avlpp == NULL)
597 +                error(CONSISTENCY, "address not found in avlmemi");
598 +        return(avlpp - avlist2);
599 + }
600 + #else
601 + #define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \
602 +                                nambvals,sizeof(AMBVAL *),aposcmp) - avlist2)
603 + #endif
604 +
605 +
606   static
607   sortambvals(always)                     /* resort ambient values */
608   int     always;
# Line 560 | Line 610 | int    always;
610          AMBTREE  oldatrunk;
611          AMBVAL  tav, *tap, *pnext;
612          register int    i, j;
613 +                                        /* see if it's time yet */
614 +        if (!always && (ambclock < lastsort+sortintvl ||
615 +                        nambvals < SORT_THRESH))
616 +                return;
617          /*
618           * The idea here is to minimize memory thrashing
619           * in VM systems by improving reference locality.
# Line 575 | Line 629 | int    always;
629           * the "always" parameter set to 1 so that the ambient
630           * tree will be rebuilt with the new accuracy parameter.
631           */
632 <        if (tracktime) {
632 >        if (tracktime) {                /* allocate pointer arrays to sort */
633                  avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
634                  avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
635          } else
636                  avlist1 = avlist2 = NULL;
637 <        if (avlist2 == NULL) {          /* rebuild tree? */
637 >        if (avlist2 == NULL) {          /* no time tracking -- rebuild tree? */
638                  if (avlist1 != NULL)
639                          free((char *)avlist1);
640 <                if (!always)
641 <                        return;
642 <                copystruct(&oldatrunk, &atrunk);
643 <                atrunk.alist = NULL;
644 <                atrunk.kid = NULL;
645 <                unloadatree(&oldatrunk, avinsert);
640 >                if (always) {           /* rebuild without sorting */
641 >                        copystruct(&oldatrunk, &atrunk);
642 >                        atrunk.alist = NULL;
643 >                        atrunk.kid = NULL;
644 >                        unloadatree(&oldatrunk, avinsert);
645 >                }
646          } else {                        /* sort memory by last access time */
593                i_avlist = 0;
594                unloadatree(&atrunk, av2list);  /* empty current tree */
647                  /*
648                   * Sorting memory is tricky because it isn't contiguous.
649                   * We have to sort an array of pointers by MRA and also
650                   * by memory position.  We then copy values in "loops"
651                   * to minimize memory hits.  Nevertheless, we will visit
652 <                 * everyone at least once, and this is an expensive process
652 >                 * everyone at least twice, and this is an expensive process
653                   * when we're thrashing, which is when we need to do it.
654                   */
655 + #ifdef DEBUG
656 +                sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...",
657 +                                nambvals, ambclock);
658 +                eputs(errmsg);
659 + #endif
660 +                i_avlist = 0;
661 +                unloadatree(&atrunk, av2list);  /* empty current tree */
662 + #ifdef DEBUG
663 +                if (i_avlist < nambvals)
664 +                        error(CONSISTENCY, "missing ambient values in sortambvals");
665 + #endif
666                  qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp);
667                  qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
668                  for (i = 0; i < nambvals; i++) {
669 <                        if (avlist1[i] == NULL || avlist1[i] == avlist2[i])
669 >                        if (avlist1[i] == NULL)
670                                  continue;
671                          tap = avlist2[i];
672                          copystruct(&tav, tap);
673                          for (j = i; (pnext = avlist1[j]) != tap;
674 <                                        j = (AMBVAL **)bsearch((char *)&pnext,
612 <                                                (char *)(avlist2+i),nambvals-i,
613 <                                                sizeof(AMBVAL *),aposcmp) -
614 <                                                avlist2) {
674 >                                        j = avlmemi(pnext)) {
675                                  copystruct(avlist2[j], pnext);
676                                  avinsert(avlist2[j]);
677                                  avlist1[j] = NULL;
# Line 622 | Line 682 | int    always;
682                  }
683                  free((char *)avlist1);
684                  free((char *)avlist2);
685 <                if (sortintvl < MAXACLOCK/4)
685 >                                                /* compute new sort interval */
686 >                sortintvl = ambclock - lastsort;
687 >                if (sortintvl >= MAX_SORT_INTVL/2)
688 >                        sortintvl = MAX_SORT_INTVL;
689 >                else
690                          sortintvl <<= 1;        /* wait twice as long next */
691 + #ifdef DEBUG
692 +                eputs("done\n");
693 + #endif
694          }
695          if (ambclock >= MAXACLOCK)
696                  ambclock = MAXACLOCK/2;
# Line 672 | Line 739 | ambsync()                      /* synchronize ambient file */
739                  if (fseek(ambinp, lastpos, 0) < 0)
740                          goto seekerr;
741                  while (n >= AMBVALSIZ) {        /* load contributed values */
742 <                        readambval(&avs, ambinp);
742 >                        if (!readambval(&avs, ambinp)) {
743 >                                sprintf(errmsg,
744 >                                "ambient file corrupted near character %ld",
745 >                                                flen - n);
746 >                                error(WARNING, errmsg);
747 >                                break;
748 >                        }
749                          avinsert(avstore(&avs));
750                          n -= AMBVALSIZ;
751                  }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines