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.22 by greg, Fri Aug 6 17:24:24 1993 UTC vs.
Revision 2.36 by greg, Wed Feb 14 15:43:22 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;     /* 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 */
36  
# Line 38 | Line 42 | static AMBTREE atrunk;         /* our ambient trunk node */
42   static FILE  *ambfp = NULL;     /* ambient file pointer */
43   static int  nunflshed = 0;      /* number of unflushed ambient values */
44  
45 + #ifndef SORT_THRESH
46 + #ifdef BIGMEM
47 + #define SORT_THRESH     ((9L<<20)/sizeof(AMBVAL))
48 + #else
49 + #define SORT_THRESH     ((3L<<20)/sizeof(AMBVAL))
50 + #endif
51 + #endif
52 + #ifndef SORT_INTVL
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 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 +        /*
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  
77   #define  newambval()    (AMBVAL *)bmalloc(sizeof(AMBVAL))
78  
45 #define  newambtree()   (AMBTREE *)calloc(8, sizeof(AMBTREE))
46 #define  freeambtree(t) free((char *)(t))
47
79   extern long  ftell(), lseek();
80 < static int  initambfile(), avsave(), avinsert(), loadatree();
80 > static int  initambfile(), avsave(), avinsert(), sortambvals();
81   static AMBVAL  *avstore();
82   #ifdef  F_SETLKW
83   static  aflock();
# Line 59 | Line 90 | int  ar;
90          ambres = ar < 0 ? 0 : ar;               /* may be done already */
91                                                  /* set min & max radii */
92          if (ar <= 0) {
93 <                minarad = 0.0;
93 >                minarad = 0;
94                  maxarad = thescene.cusize / 2.0;
95          } else {
96                  minarad = thescene.cusize / ar;
97 <                maxarad = 16.0 * minarad;               /* heuristic */
97 >                maxarad = 64 * minarad;                 /* heuristic */
98                  if (maxarad > thescene.cusize / 2.0)
99                          maxarad = thescene.cusize / 2.0;
100          }
101 <        if (maxarad <= FTINY)
102 <                maxarad = .001;
101 >        if (minarad <= FTINY)
102 >                minarad = 10*FTINY;
103 >        if (maxarad <= minarad)
104 >                maxarad = 64 * minarad;
105   }
106  
107  
108   setambacc(newa)                         /* set ambient accuracy */
109   double  newa;
110   {
111 <        static double  oldambacc = -1.0;
79 <        AMBTREE  oldatrunk;
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 */
86 <        if (ambacc <= FTINY)
87 <                return;                 /* cannot build new tree */
88 <                                        /* else need to rebuild tree */
89 <        copystruct(&oldatrunk, &atrunk);
90 <        atrunk.alist = NULL;
91 <        atrunk.kid = NULL;
92 <        loadatree(&oldatrunk);
93 <        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  
# Line 118 | Line 142 | char  *afile;
142                          avinsert(avstore(&amb));
143                                                  /* align */
144                  fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
145 +                nambshare = nambvals;
146          } else if ((ambfp = fopen(afile, "w+")) != NULL)
147                  initambfile(1);
148          else {
# Line 151 | 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 172 | 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 */
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 {
186 <                d = makeambient(acol, r, rdepth++);
187 <                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 <= 0 | nambvals == 0)
223 +                return;
224 +        scalecolor(acol, (double)ambvwt);
225 +        addcolor(acol, avsum);                  /* average in computations */
226 +        d = 1.0/(ambvwt+nambvals);
227 +        scalecolor(acol, d);
228   }
229  
230  
231   double
232 < sumambient(acol, r, al, at, c0, s)      /* get interpolated ambient value */
232 > sumambient(acol, r, rn, al, at, c0, s)  /* get interpolated ambient value */
233   COLOR  acol;
234   register RAY  *r;
235 + FVECT  rn;
236   int  al;
237   AMBTREE  *at;
238   FVECT  c0;
# Line 208 | Line 244 | double s;
244          int  i;
245          register int  j;
246          register AMBVAL  *av;
247 <                                        /* do this node */
247 >
248          wsum = 0.0;
249 +                                        /* do this node */
250          for (av = at->alist; av != NULL; av = av->next) {
251 +                if (tracktime)
252 +                        av->latick = ambclock++;
253                  /*
254                   *  Ambient level test.
255                   */
256 <                if (av->lvl > al || av->weight < r->rweight-FTINY)
256 >                if (av->lvl > al)       /* list sorted, so this works */
257 >                        break;
258 >                if (av->weight < r->rweight-FTINY)
259                          continue;
260                  /*
261                   *  Ambient radius test.
262                   */
263 <                e1 = 0.0;
264 <                for (j = 0; j < 3; j++) {
265 <                        d = av->pos[j] - r->rop[j];
266 <                        e1 += d * d;
267 <                }
263 >                d = av->pos[0] - r->rop[0];
264 >                e1 = d * d;
265 >                d = av->pos[1] - r->rop[1];
266 >                e1 += d * d;
267 >                d = av->pos[2] - r->rop[2];
268 >                e1 += d * d;
269                  e1 /= av->rad * av->rad;
270                  if (e1 > ambacc*ambacc*1.21)
271                          continue;
# Line 247 | Line 289 | double s;
289                   *  Jittering final test reduces image artifacts.
290                   */
291                  wt = sqrt(e1) + sqrt(e2);
292 <                wt *= .9 + .2*urand(9015+samplendx);
251 <                if (wt > ambacc)
292 >                if (wt > ambacc*(.9+.2*urand(9015+samplendx)))
293                          continue;
294                  if (wt <= 1e-3)
295                          wt = 1e3;
296                  else
297                          wt = 1.0 / wt;
298                  wsum += wt;
299 <                extambient(ct, av, r->rop, r->ron);
299 >                extambient(ct, av, r->rop, rn);
300                  scalecolor(ct, wt);
301                  addcolor(acol, ct);
302          }
# Line 274 | Line 315 | double s;
315                                  break;
316                  }
317                  if (j == 3)
318 <                        wsum += sumambient(acol, r, al, at->kid+i, ck0, s);
318 >                        wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s);
319          }
320          return(wsum);
321   }
322  
323  
324   double
325 < makeambient(acol, r, al)        /* make a new ambient value */
325 > makeambient(acol, r, rn, al)    /* make a new ambient value */
326   COLOR  acol;
327   register RAY  *r;
328 + FVECT  rn;
329   int  al;
330   {
331          AMBVAL  amb;
332          FVECT   gp, gd;
333                                                  /* compute weight */
334          amb.weight = pow(AVGREFL, (double)al);
335 <        if (r->rweight < 0.2*amb.weight)        /* heuristic */
335 >        if (r->rweight < 0.1*amb.weight)        /* heuristic */
336                  amb.weight = r->rweight;
337                                                  /* compute ambient */
338          amb.rad = doambient(acol, r, amb.weight, gp, gd);
339 <        if (amb.rad == 0.0)
339 >        if (amb.rad <= FTINY)
340                  return(0.0);
341                                                  /* store it */
342          VCOPY(amb.pos, r->rop);
# Line 305 | Line 347 | int  al;
347          VCOPY(amb.gdir, gd);
348                                                  /* insert into tree */
349          avsave(&amb);                           /* and save to file */
350 +        if (rn != r->ron)
351 +                extambient(acol, &amb, r->rop, rn);     /* texture */
352          return(amb.rad);
353   }
354  
# Line 349 | Line 393 | int  creat;
393   #endif
394          setbuf(ambfp, bmalloc(BUFSIZ+8));
395          if (creat) {                    /* new file */
396 +                newheader("RADIANCE", ambfp);
397                  fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ",
398                                  progname, colval(ambval,RED),
399                                  colval(ambval,GRN), colval(ambval,BLU),
# Line 392 | Line 437 | register AMBVAL  *aval;
437          if ((av = newambval()) == NULL)
438                  error(SYSTEM, "out of memory in avstore");
439          copystruct(av, aval);
440 +        av->latick = ambclock;
441 +        av->next = NULL;
442 +        addcolor(avsum, av->val);       /* add to sum for averaging */
443 +        nambvals++;
444          return(av);
445   }
446  
447  
448 + #define ATALLOCSZ       512             /* #/8 trees to allocate at once */
449 +
450 + static AMBTREE  *atfreelist = NULL;     /* free ambient tree structures */
451 +
452 +
453   static
454 + AMBTREE *
455 + newambtree()                            /* allocate 8 ambient tree structs */
456 + {
457 +        register AMBTREE  *atp, *upperlim;
458 +
459 +        if (atfreelist == NULL) {       /* get more nodes */
460 +                atfreelist = (AMBTREE *)bmalloc(ATALLOCSZ*8*sizeof(AMBTREE));
461 +                if (atfreelist == NULL)
462 +                        return(NULL);
463 +                                        /* link new free list */
464 +                upperlim = atfreelist + 8*(ATALLOCSZ-1);
465 +                for (atp = atfreelist; atp < upperlim; atp += 8)
466 +                        atp->kid = atp + 8;
467 +                atp->kid = NULL;
468 +        }
469 +        atp = atfreelist;
470 +        atfreelist = atp->kid;
471 +        bzero((char *)atp, 8*sizeof(AMBTREE));
472 +        return(atp);
473 + }
474 +
475 +
476 + static
477 + freeambtree(atp)                        /* free 8 ambient tree structs */
478 + AMBTREE  *atp;
479 + {
480 +        atp->kid = atfreelist;
481 +        atfreelist = atp;
482 + }
483 +
484 +
485 + static
486   avinsert(av)                            /* insert ambient value in our tree */
487   register AMBVAL  *av;
488   {
489          register AMBTREE  *at;
490 +        register AMBVAL  *ap;
491 +        AMBVAL  avh;
492          FVECT  ck0;
493          double  s;
494          int  branch;
# Line 424 | Line 512 | register AMBVAL         *av;
512                          }
513                  at = at->kid + branch;
514          }
515 <        av->next = at->alist;
516 <        at->alist = av;
515 >        avh.next = at->alist;           /* order by increasing level */
516 >        for (ap = &avh; ap->next != NULL; ap = ap->next)
517 >                if (ap->next->lvl >= av->lvl)
518 >                        break;
519 >        av->next = ap->next;
520 >        ap->next = av;
521 >        at->alist = avh.next;
522   }
523  
524  
525   static
526 < loadatree(at)                           /* move tree to main store */
526 > unloadatree(at, f)                      /* unload an ambient value tree */
527   register AMBTREE  *at;
528 + int     (*f)();
529   {
530          register AMBVAL  *av;
531          register int  i;
532                                          /* transfer values at this node */
533          for (av = at->alist; av != NULL; av = at->alist) {
534                  at->alist = av->next;
535 <                avinsert(av);
535 >                (*f)(av);
536          }
537          if (at->kid == NULL)
538                  return;
539          for (i = 0; i < 8; i++)         /* transfer and free children */
540 <                loadatree(at->kid+i);
540 >                unloadatree(at->kid+i, f);
541          freeambtree(at->kid);
542 +        at->kid = NULL;
543 + }
544 +
545 +
546 + static AMBVAL   **avlist1, **avlist2;   /* ambient value lists for sorting */
547 + static int      i_avlist;               /* index for lists */
548 +
549 +
550 + static
551 + av2list(av)
552 + AMBVAL  *av;
553 + {
554 + #ifdef DEBUG
555 +        if (i_avlist >= nambvals)
556 +                error(CONSISTENCY, "too many ambient values in av2list1");
557 + #endif
558 +        avlist1[i_avlist] = avlist2[i_avlist] = av;
559 +        i_avlist++;
560 + }
561 +
562 +
563 + static int
564 + alatcmp(avp1, avp2)                     /* compare ambient values for MRA */
565 + AMBVAL  **avp1, **avp2;
566 + {
567 +        return((**avp2).latick - (**avp1).latick);
568 + }
569 +
570 +
571 + static int
572 + aposcmp(avp1, avp2)                     /* compare ambient value positions */
573 + AMBVAL  **avp1, **avp2;
574 + {
575 +        return(*avp1 - *avp2);
576 + }
577 +
578 +
579 + #ifdef DEBUG
580 + static int
581 + avlmemi(avaddr)                         /* find list position from address */
582 + AMBVAL  *avaddr;
583 + {
584 +        register AMBVAL  **avlpp;
585 +
586 +        avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
587 +                        nambvals, sizeof(AMBVAL *), aposcmp);
588 +        if (avlpp == NULL)
589 +                error(CONSISTENCY, "address not found in avlmemi");
590 +        return(avlpp - avlist2);
591 + }
592 + #else
593 + #define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \
594 +                                nambvals,sizeof(AMBVAL *),aposcmp) - avlist2)
595 + #endif
596 +
597 +
598 + static
599 + sortambvals(always)                     /* resort ambient values */
600 + int     always;
601 + {
602 +        AMBTREE  oldatrunk;
603 +        AMBVAL  tav, *tap, *pnext;
604 +        register int    i, j;
605 +                                        /* see if it's time yet */
606 +        if (!always && (ambclock < lastsort+sortintvl ||
607 +                        nambvals < SORT_THRESH))
608 +                return;
609 +        /*
610 +         * The idea here is to minimize memory thrashing
611 +         * in VM systems by improving reference locality.
612 +         * We do this by periodically sorting our stored ambient
613 +         * values in memory in order of most recently to least
614 +         * recently accessed.  This ordering was chosen so that new
615 +         * ambient values (which tend to be less important) go into
616 +         * higher memory with the infrequently accessed values.
617 +         *      Since we expect our values to need sorting less
618 +         * frequently as the process continues, we double our
619 +         * waiting interval after each call.
620 +         *      This routine is also called by setambacc() with
621 +         * the "always" parameter set to 1 so that the ambient
622 +         * tree will be rebuilt with the new accuracy parameter.
623 +         */
624 +        if (tracktime) {                /* allocate pointer arrays to sort */
625 +                avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
626 +                avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
627 +        } else
628 +                avlist1 = avlist2 = NULL;
629 +        if (avlist2 == NULL) {          /* no time tracking -- rebuild tree? */
630 +                if (avlist1 != NULL)
631 +                        free((char *)avlist1);
632 +                if (always) {           /* rebuild without sorting */
633 +                        copystruct(&oldatrunk, &atrunk);
634 +                        atrunk.alist = NULL;
635 +                        atrunk.kid = NULL;
636 +                        unloadatree(&oldatrunk, avinsert);
637 +                }
638 +        } else {                        /* sort memory by last access time */
639 +                /*
640 +                 * Sorting memory is tricky because it isn't contiguous.
641 +                 * We have to sort an array of pointers by MRA and also
642 +                 * by memory position.  We then copy values in "loops"
643 +                 * to minimize memory hits.  Nevertheless, we will visit
644 +                 * everyone at least twice, and this is an expensive process
645 +                 * when we're thrashing, which is when we need to do it.
646 +                 */
647 + #ifdef DEBUG
648 +                sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...",
649 +                                nambvals, ambclock);
650 +                eputs(errmsg);
651 + #endif
652 +                i_avlist = 0;
653 +                unloadatree(&atrunk, av2list);  /* empty current tree */
654 + #ifdef DEBUG
655 +                if (i_avlist < nambvals)
656 +                        error(CONSISTENCY, "missing ambient values in sortambvals");
657 + #endif
658 +                qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp);
659 +                qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
660 +                for (i = 0; i < nambvals; i++) {
661 +                        if (avlist1[i] == NULL)
662 +                                continue;
663 +                        tap = avlist2[i];
664 +                        copystruct(&tav, tap);
665 +                        for (j = i; (pnext = avlist1[j]) != tap;
666 +                                        j = avlmemi(pnext)) {
667 +                                copystruct(avlist2[j], pnext);
668 +                                avinsert(avlist2[j]);
669 +                                avlist1[j] = NULL;
670 +                        }
671 +                        copystruct(avlist2[j], &tav);
672 +                        avinsert(avlist2[j]);
673 +                        avlist1[j] = NULL;
674 +                }
675 +                free((char *)avlist1);
676 +                free((char *)avlist2);
677 +                                                /* compute new sort interval */
678 +                sortintvl = ambclock - lastsort;
679 +                if (sortintvl >= MAX_SORT_INTVL/2)
680 +                        sortintvl = MAX_SORT_INTVL;
681 +                else
682 +                        sortintvl <<= 1;        /* wait twice as long next */
683 + #ifdef DEBUG
684 +                eputs("done\n");
685 + #endif
686 +        }
687 +        if (ambclock >= MAXACLOCK)
688 +                ambclock = MAXACLOCK/2;
689 +        lastsort = ambclock;
690   }
691  
692  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines