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.34 by greg, Mon Nov 6 12:03:13 1995 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 > #ifndef  AMBVWT
25 > #define  AMBVWT         250     /* relative ambient value weight (# calcs) */
26 > #endif
27  
28   typedef struct ambtree {
29          AMBVAL  *alist;         /* ambient value list */
# Line 27 | Line 32 | typedef struct ambtree {
32  
33   extern CUBE  thescene;          /* contains space boundaries */
34  
35 < extern char  *shm_boundary;     /* shared memory boundary */
35 > extern char  *shm_boundary;     /* memory sharing boundary */
36  
37   #define  MAXASET        511     /* maximum number of elements in ambient set */
38   OBJECT  ambset[MAXASET+1]={0};  /* ambient include/exclude set */
# Line 42 | Line 47 | static int  nunflshed = 0;     /* number of unflushed ambi
47  
48   #ifndef SORT_THRESH
49   #ifdef BIGMEM
50 < #define SORT_THRESH     (6*(1L<<20)/sizeof(AMBVAL))
50 > #define SORT_THRESH     ((9L<<20)/sizeof(AMBVAL))
51   #else
52 < #define SORT_THRESH     (2*(1L<<20)/sizeof(AMBVAL))
52 > #define SORT_THRESH     ((3L<<20)/sizeof(AMBVAL))
53   #endif
54   #endif
55   #ifndef SORT_INTVL
56 < #define SORT_INTVL      (SORT_THRESH*2)
56 > #define SORT_INTVL      (SORT_THRESH*256)
57   #endif
58 + #ifndef MAX_SORT_INTVL
59 + #define MAX_SORT_INTVL  (SORT_INTVL<<4)
60 + #endif
61  
62 < static long  ambclock = 0;      /* ambient access clock */
63 < static int  nambvals = 0;       /* number of stored ambient values */
64 < static long  lastsort = 0;      /* time of last value sort */
62 > static COLOR  avsum = BLKCOLOR;         /* computed ambient value sum */
63 > static unsigned int  nambvals = 0;      /* number of computed ambient values */
64 > static unsigned long  ambclock = 0;     /* ambient access clock */
65 > static unsigned long  lastsort = 0;     /* time of last value sort */
66   static long  sortintvl = SORT_INTVL;    /* time until next sort */
67  
68   #define MAXACLOCK       (1L<<30)        /* clock turnover value */
69 +        /*
70 +         * Track access times unless we are sharing ambient values
71 +         * through memory on a multiprocessor, when we want to avoid
72 +         * claiming our own memory (copy on write).
73 +         */
74   #define tracktime       (shm_boundary == NULL || ambfp == NULL)
61 #define need2sort       (ambclock > lastsort+sortintvl && \
62                                nambvals > SORT_THRESH)
75  
76   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
77  
# Line 83 | Line 95 | int  ar;
95                  maxarad = thescene.cusize / 2.0;
96          } else {
97                  minarad = thescene.cusize / ar;
98 <                maxarad = 16 * minarad;                 /* heuristic */
98 >                maxarad = 64 * minarad;                 /* heuristic */
99                  if (maxarad > thescene.cusize / 2.0)
100                          maxarad = thescene.cusize / 2.0;
101          }
# Line 97 | Line 109 | int  ar;
109   setambacc(newa)                         /* set ambient accuracy */
110   double  newa;
111   {
112 <        static double  oldambacc = -1.0;
112 >        double  ambdiff;
113  
114 <        ambacc = newa < 0.0 ? 0.0 : newa;       /* may be done already */
115 <        if (oldambacc < -FTINY)
116 <                oldambacc = ambacc;     /* do nothing first call */
117 <        if (fabs(newa - oldambacc) < 0.01)
118 <                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 */
114 >        if (newa < 0.0)
115 >                newa = 0.0;
116 >        ambdiff = fabs(newa - ambacc);
117 >        if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0)
118 >                sortambvals(1);                 /* rebuild tree */
119   }
120  
121  
# Line 169 | Line 176 | OBJECT obj;
176   }
177  
178  
179 < ambient(acol, r)                /* compute ambient component for ray */
179 > ambient(acol, r, nrm)           /* compute ambient component for ray */
180   COLOR  acol;
181   register RAY  *r;
182 + FVECT  nrm;
183   {
184          static int  rdepth = 0;                 /* ambient recursion */
185          double  d;
# Line 190 | Line 198 | register RAY  *r;
198                  rdepth++;
199                  d = doambient(acol, r, r->rweight, NULL, NULL);
200                  rdepth--;
201 <                if (d == 0.0)
201 >                if (d <= FTINY)
202                          goto dumbamb;
203                  return;
204          }
205 +                                                /* resort memory? */
206 +        sortambvals(0);
207                                                  /* get ambient value */
198        if (need2sort)
199                sortambvals(0);
208          setcolor(acol, 0.0, 0.0, 0.0);
209 <        d = sumambient(acol, r, rdepth,
209 >        d = sumambient(acol, r, nrm, rdepth,
210                          &atrunk, thescene.cuorg, thescene.cusize);
211 <        if (d > FTINY)
211 >        if (d > FTINY) {
212                  scalecolor(acol, 1.0/d);
213 <        else {
206 <                d = makeambient(acol, r, rdepth++);
207 <                rdepth--;
213 >                return;
214          }
215 +        rdepth++;                               /* need to cache new value */
216 +        d = makeambient(acol, r, nrm, rdepth-1);
217 +        rdepth--;
218          if (d > FTINY)
219                  return;
220   dumbamb:                                        /* return global value */
221          copycolor(acol, ambval);
222 + #if  AMBVWT
223 +        if (nambvals == 0)
224 +                return;
225 +        scalecolor(acol, (double)AMBVWT);
226 +        addcolor(acol, avsum);                  /* average in computations */
227 +        d = 1.0/(AMBVWT+nambvals);
228 +        scalecolor(acol, d);
229 + #endif
230   }
231  
232  
233   double
234 < sumambient(acol, r, al, at, c0, s)      /* get interpolated ambient value */
234 > sumambient(acol, r, rn, al, at, c0, s)  /* get interpolated ambient value */
235   COLOR  acol;
236   register RAY  *r;
237 + FVECT  rn;
238   int  al;
239   AMBTREE  *at;
240   FVECT  c0;
# Line 228 | Line 246 | double s;
246          int  i;
247          register int  j;
248          register AMBVAL  *av;
249 <                                        /* do this node */
249 >
250          wsum = 0.0;
251 +                                        /* do this node */
252          for (av = at->alist; av != NULL; av = av->next) {
253                  if (tracktime)
254                          av->latick = ambclock++;
# Line 243 | Line 262 | double s;
262                  /*
263                   *  Ambient radius test.
264                   */
265 <                e1 = 0.0;
266 <                for (j = 0; j < 3; j++) {
267 <                        d = av->pos[j] - r->rop[j];
268 <                        e1 += d * d;
269 <                }
265 >                d = av->pos[0] - r->rop[0];
266 >                e1 = d * d;
267 >                d = av->pos[1] - r->rop[1];
268 >                e1 += d * d;
269 >                d = av->pos[2] - r->rop[2];
270 >                e1 += d * d;
271                  e1 /= av->rad * av->rad;
272                  if (e1 > ambacc*ambacc*1.21)
273                          continue;
# Line 271 | Line 291 | double s;
291                   *  Jittering final test reduces image artifacts.
292                   */
293                  wt = sqrt(e1) + sqrt(e2);
294 <                wt *= .9 + .2*urand(9015+samplendx);
275 <                if (wt > ambacc)
294 >                if (wt > ambacc*(.9+.2*urand(9015+samplendx)))
295                          continue;
296                  if (wt <= 1e-3)
297                          wt = 1e3;
298                  else
299                          wt = 1.0 / wt;
300                  wsum += wt;
301 <                extambient(ct, av, r->rop, r->ron);
301 >                extambient(ct, av, r->rop, rn);
302                  scalecolor(ct, wt);
303                  addcolor(acol, ct);
304          }
# Line 298 | Line 317 | double s;
317                                  break;
318                  }
319                  if (j == 3)
320 <                        wsum += sumambient(acol, r, al, at->kid+i, ck0, s);
320 >                        wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s);
321          }
322          return(wsum);
323   }
324  
325  
326   double
327 < makeambient(acol, r, al)        /* make a new ambient value */
327 > makeambient(acol, r, rn, al)    /* make a new ambient value */
328   COLOR  acol;
329   register RAY  *r;
330 + FVECT  rn;
331   int  al;
332   {
333          AMBVAL  amb;
334          FVECT   gp, gd;
335                                                  /* compute weight */
336          amb.weight = pow(AVGREFL, (double)al);
337 <        if (r->rweight < 0.2*amb.weight)        /* heuristic */
337 >        if (r->rweight < 0.1*amb.weight)        /* heuristic */
338                  amb.weight = r->rweight;
339                                                  /* compute ambient */
340          amb.rad = doambient(acol, r, amb.weight, gp, gd);
341 <        if (amb.rad == 0.0)
341 >        if (amb.rad <= FTINY)
342                  return(0.0);
343                                                  /* store it */
344          VCOPY(amb.pos, r->rop);
# Line 329 | Line 349 | int  al;
349          VCOPY(amb.gdir, gd);
350                                                  /* insert into tree */
351          avsave(&amb);                           /* and save to file */
352 +        if (rn != r->ron)
353 +                extambient(acol, &amb, r->rop, rn);     /* texture */
354          return(amb.rad);
355   }
356  
# Line 419 | Line 441 | register AMBVAL  *aval;
441          copystruct(av, aval);
442          av->latick = ambclock;
443          av->next = NULL;
444 +        addcolor(avsum, av->val);       /* add to sum for averaging */
445          nambvals++;
446          return(av);
447   }
# Line 530 | Line 553 | static
553   av2list(av)
554   AMBVAL  *av;
555   {
556 + #ifdef DEBUG
557          if (i_avlist >= nambvals)
558                  error(CONSISTENCY, "too many ambient values in av2list1");
559 + #endif
560          avlist1[i_avlist] = avlist2[i_avlist] = av;
561          i_avlist++;
562   }
# Line 553 | Line 578 | AMBVAL **avp1, **avp2;
578   }
579  
580  
581 + #ifdef DEBUG
582 + static int
583 + avlmemi(avaddr)                         /* find list position from address */
584 + AMBVAL  *avaddr;
585 + {
586 +        register AMBVAL  **avlpp;
587 +
588 +        avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
589 +                        nambvals, sizeof(AMBVAL *), aposcmp);
590 +        if (avlpp == NULL)
591 +                error(CONSISTENCY, "address not found in avlmemi");
592 +        return(avlpp - avlist2);
593 + }
594 + #else
595 + #define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \
596 +                                nambvals,sizeof(AMBVAL *),aposcmp) - avlist2)
597 + #endif
598 +
599 +
600   static
601   sortambvals(always)                     /* resort ambient values */
602   int     always;
# Line 560 | Line 604 | int    always;
604          AMBTREE  oldatrunk;
605          AMBVAL  tav, *tap, *pnext;
606          register int    i, j;
607 +                                        /* see if it's time yet */
608 +        if (!always && (ambclock < lastsort+sortintvl ||
609 +                        nambvals < SORT_THRESH))
610 +                return;
611          /*
612           * The idea here is to minimize memory thrashing
613           * in VM systems by improving reference locality.
# Line 575 | Line 623 | int    always;
623           * the "always" parameter set to 1 so that the ambient
624           * tree will be rebuilt with the new accuracy parameter.
625           */
626 <        if (tracktime) {
626 >        if (tracktime) {                /* allocate pointer arrays to sort */
627                  avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
628                  avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
629          } else
630                  avlist1 = avlist2 = NULL;
631 <        if (avlist2 == NULL) {          /* rebuild tree? */
631 >        if (avlist2 == NULL) {          /* no time tracking -- rebuild tree? */
632                  if (avlist1 != NULL)
633                          free((char *)avlist1);
634 <                if (!always)
635 <                        return;
636 <                copystruct(&oldatrunk, &atrunk);
637 <                atrunk.alist = NULL;
638 <                atrunk.kid = NULL;
639 <                unloadatree(&oldatrunk, avinsert);
634 >                if (always) {           /* rebuild without sorting */
635 >                        copystruct(&oldatrunk, &atrunk);
636 >                        atrunk.alist = NULL;
637 >                        atrunk.kid = NULL;
638 >                        unloadatree(&oldatrunk, avinsert);
639 >                }
640          } else {                        /* sort memory by last access time */
593                i_avlist = 0;
594                unloadatree(&atrunk, av2list);  /* empty current tree */
641                  /*
642                   * Sorting memory is tricky because it isn't contiguous.
643                   * We have to sort an array of pointers by MRA and also
644                   * by memory position.  We then copy values in "loops"
645                   * to minimize memory hits.  Nevertheless, we will visit
646 <                 * everyone at least once, and this is an expensive process
646 >                 * everyone at least twice, and this is an expensive process
647                   * when we're thrashing, which is when we need to do it.
648                   */
649 + #ifdef DEBUG
650 +                sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...",
651 +                                nambvals, ambclock);
652 +                eputs(errmsg);
653 + #endif
654 +                i_avlist = 0;
655 +                unloadatree(&atrunk, av2list);  /* empty current tree */
656 + #ifdef DEBUG
657 +                if (i_avlist < nambvals)
658 +                        error(CONSISTENCY, "missing ambient values in sortambvals");
659 + #endif
660                  qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp);
661                  qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
662                  for (i = 0; i < nambvals; i++) {
663 <                        if (avlist1[i] == NULL || avlist1[i] == avlist2[i])
663 >                        if (avlist1[i] == NULL)
664                                  continue;
665                          tap = avlist2[i];
666                          copystruct(&tav, tap);
667                          for (j = i; (pnext = avlist1[j]) != tap;
668 <                                        j = (AMBVAL **)bsearch((char *)&pnext,
612 <                                                (char *)(avlist2+i),nambvals-i,
613 <                                                sizeof(AMBVAL *),aposcmp) -
614 <                                                avlist2) {
668 >                                        j = avlmemi(pnext)) {
669                                  copystruct(avlist2[j], pnext);
670                                  avinsert(avlist2[j]);
671                                  avlist1[j] = NULL;
# Line 622 | Line 676 | int    always;
676                  }
677                  free((char *)avlist1);
678                  free((char *)avlist2);
679 <                if (sortintvl < MAXACLOCK/4)
679 >                                                /* compute new sort interval */
680 >                sortintvl = ambclock - lastsort;
681 >                if (sortintvl >= MAX_SORT_INTVL/2)
682 >                        sortintvl = MAX_SORT_INTVL;
683 >                else
684                          sortintvl <<= 1;        /* wait twice as long next */
685 + #ifdef DEBUG
686 +                eputs("done\n");
687 + #endif
688          }
689          if (ambclock >= MAXACLOCK)
690                  ambclock = MAXACLOCK/2;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines