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.35 by greg, Tue Nov 21 14:28:22 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;      /* total number of indirect values */
64 > static unsigned int  nambshare = 0;     /* number of values from file */
65 > static unsigned long  ambclock = 0;     /* ambient access clock */
66 > static unsigned long  lastsort = 0;     /* time of last value sort */
67   static long  sortintvl = SORT_INTVL;    /* time until next sort */
68  
69   #define MAXACLOCK       (1L<<30)        /* clock turnover value */
70 < #define tracktime       (shm_boundary == NULL || ambfp == NULL)
71 < #define need2sort       (ambclock > lastsort+sortintvl && \
72 <                                nambvals > SORT_THRESH)
70 >        /*
71 >         * Track access times unless we are sharing ambient values
72 >         * through memory on a multiprocessor, when we want to avoid
73 >         * claiming our own memory (copy on write).  Go ahead anyway
74 >         * if more than two thirds of our values are unshared.
75 >         */
76 > #define tracktime       (shm_boundary == NULL || nambvals > 3*nambshare)
77  
78   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
79  
# Line 83 | Line 97 | int  ar;
97                  maxarad = thescene.cusize / 2.0;
98          } else {
99                  minarad = thescene.cusize / ar;
100 <                maxarad = 16 * minarad;                 /* heuristic */
100 >                maxarad = 64 * minarad;                 /* heuristic */
101                  if (maxarad > thescene.cusize / 2.0)
102                          maxarad = thescene.cusize / 2.0;
103          }
# Line 97 | Line 111 | int  ar;
111   setambacc(newa)                         /* set ambient accuracy */
112   double  newa;
113   {
114 <        static double  oldambacc = -1.0;
114 >        double  ambdiff;
115  
116 <        ambacc = newa < 0.0 ? 0.0 : newa;       /* may be done already */
117 <        if (oldambacc < -FTINY)
118 <                oldambacc = ambacc;     /* do nothing first call */
119 <        if (fabs(newa - oldambacc) < 0.01)
120 <                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 */
116 >        if (newa < 0.0)
117 >                newa = 0.0;
118 >        ambdiff = fabs(newa - ambacc);
119 >        if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0)
120 >                sortambvals(1);                 /* rebuild tree */
121   }
122  
123  
# Line 136 | Line 145 | char  *afile;
145                          avinsert(avstore(&amb));
146                                                  /* align */
147                  fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
148 +                nambshare = nambvals;
149          } else if ((ambfp = fopen(afile, "w+")) != NULL)
150                  initambfile(1);
151          else {
# Line 169 | Line 179 | OBJECT obj;
179   }
180  
181  
182 < ambient(acol, r)                /* compute ambient component for ray */
182 > ambient(acol, r, nrm)           /* compute ambient component for ray */
183   COLOR  acol;
184   register RAY  *r;
185 + FVECT  nrm;
186   {
187          static int  rdepth = 0;                 /* ambient recursion */
188          double  d;
# Line 190 | Line 201 | register RAY  *r;
201                  rdepth++;
202                  d = doambient(acol, r, r->rweight, NULL, NULL);
203                  rdepth--;
204 <                if (d == 0.0)
204 >                if (d <= FTINY)
205                          goto dumbamb;
206                  return;
207          }
208 +                                                /* resort memory? */
209 +        sortambvals(0);
210                                                  /* get ambient value */
198        if (need2sort)
199                sortambvals(0);
211          setcolor(acol, 0.0, 0.0, 0.0);
212 <        d = sumambient(acol, r, rdepth,
212 >        d = sumambient(acol, r, nrm, rdepth,
213                          &atrunk, thescene.cuorg, thescene.cusize);
214 <        if (d > FTINY)
214 >        if (d > FTINY) {
215                  scalecolor(acol, 1.0/d);
216 <        else {
206 <                d = makeambient(acol, r, rdepth++);
207 <                rdepth--;
216 >                return;
217          }
218 +        rdepth++;                               /* need to cache new value */
219 +        d = makeambient(acol, r, nrm, rdepth-1);
220 +        rdepth--;
221          if (d > FTINY)
222                  return;
223   dumbamb:                                        /* return global value */
224          copycolor(acol, ambval);
225 + #if  AMBVWT
226 +        if (nambvals == 0)
227 +                return;
228 +        scalecolor(acol, (double)AMBVWT);
229 +        addcolor(acol, avsum);                  /* average in computations */
230 +        d = 1.0/(AMBVWT+nambvals);
231 +        scalecolor(acol, d);
232 + #endif
233   }
234  
235  
236   double
237 < sumambient(acol, r, al, at, c0, s)      /* get interpolated ambient value */
237 > sumambient(acol, r, rn, al, at, c0, s)  /* get interpolated ambient value */
238   COLOR  acol;
239   register RAY  *r;
240 + FVECT  rn;
241   int  al;
242   AMBTREE  *at;
243   FVECT  c0;
# Line 228 | Line 249 | double s;
249          int  i;
250          register int  j;
251          register AMBVAL  *av;
252 <                                        /* do this node */
252 >
253          wsum = 0.0;
254 +                                        /* do this node */
255          for (av = at->alist; av != NULL; av = av->next) {
256                  if (tracktime)
257                          av->latick = ambclock++;
# Line 243 | Line 265 | double s;
265                  /*
266                   *  Ambient radius test.
267                   */
268 <                e1 = 0.0;
269 <                for (j = 0; j < 3; j++) {
270 <                        d = av->pos[j] - r->rop[j];
271 <                        e1 += d * d;
272 <                }
268 >                d = av->pos[0] - r->rop[0];
269 >                e1 = d * d;
270 >                d = av->pos[1] - r->rop[1];
271 >                e1 += d * d;
272 >                d = av->pos[2] - r->rop[2];
273 >                e1 += d * d;
274                  e1 /= av->rad * av->rad;
275                  if (e1 > ambacc*ambacc*1.21)
276                          continue;
# Line 271 | Line 294 | double s;
294                   *  Jittering final test reduces image artifacts.
295                   */
296                  wt = sqrt(e1) + sqrt(e2);
297 <                wt *= .9 + .2*urand(9015+samplendx);
275 <                if (wt > ambacc)
297 >                if (wt > ambacc*(.9+.2*urand(9015+samplendx)))
298                          continue;
299                  if (wt <= 1e-3)
300                          wt = 1e3;
301                  else
302                          wt = 1.0 / wt;
303                  wsum += wt;
304 <                extambient(ct, av, r->rop, r->ron);
304 >                extambient(ct, av, r->rop, rn);
305                  scalecolor(ct, wt);
306                  addcolor(acol, ct);
307          }
# Line 298 | Line 320 | double s;
320                                  break;
321                  }
322                  if (j == 3)
323 <                        wsum += sumambient(acol, r, al, at->kid+i, ck0, s);
323 >                        wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s);
324          }
325          return(wsum);
326   }
327  
328  
329   double
330 < makeambient(acol, r, al)        /* make a new ambient value */
330 > makeambient(acol, r, rn, al)    /* make a new ambient value */
331   COLOR  acol;
332   register RAY  *r;
333 + FVECT  rn;
334   int  al;
335   {
336          AMBVAL  amb;
337          FVECT   gp, gd;
338                                                  /* compute weight */
339          amb.weight = pow(AVGREFL, (double)al);
340 <        if (r->rweight < 0.2*amb.weight)        /* heuristic */
340 >        if (r->rweight < 0.1*amb.weight)        /* heuristic */
341                  amb.weight = r->rweight;
342                                                  /* compute ambient */
343          amb.rad = doambient(acol, r, amb.weight, gp, gd);
344 <        if (amb.rad == 0.0)
344 >        if (amb.rad <= FTINY)
345                  return(0.0);
346                                                  /* store it */
347          VCOPY(amb.pos, r->rop);
# Line 329 | Line 352 | int  al;
352          VCOPY(amb.gdir, gd);
353                                                  /* insert into tree */
354          avsave(&amb);                           /* and save to file */
355 +        if (rn != r->ron)
356 +                extambient(acol, &amb, r->rop, rn);     /* texture */
357          return(amb.rad);
358   }
359  
# Line 419 | Line 444 | register AMBVAL  *aval;
444          copystruct(av, aval);
445          av->latick = ambclock;
446          av->next = NULL;
447 +        addcolor(avsum, av->val);       /* add to sum for averaging */
448          nambvals++;
449          return(av);
450   }
# Line 530 | Line 556 | static
556   av2list(av)
557   AMBVAL  *av;
558   {
559 + #ifdef DEBUG
560          if (i_avlist >= nambvals)
561                  error(CONSISTENCY, "too many ambient values in av2list1");
562 + #endif
563          avlist1[i_avlist] = avlist2[i_avlist] = av;
564          i_avlist++;
565   }
# Line 553 | Line 581 | AMBVAL **avp1, **avp2;
581   }
582  
583  
584 + #ifdef DEBUG
585 + static int
586 + avlmemi(avaddr)                         /* find list position from address */
587 + AMBVAL  *avaddr;
588 + {
589 +        register AMBVAL  **avlpp;
590 +
591 +        avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
592 +                        nambvals, sizeof(AMBVAL *), aposcmp);
593 +        if (avlpp == NULL)
594 +                error(CONSISTENCY, "address not found in avlmemi");
595 +        return(avlpp - avlist2);
596 + }
597 + #else
598 + #define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \
599 +                                nambvals,sizeof(AMBVAL *),aposcmp) - avlist2)
600 + #endif
601 +
602 +
603   static
604   sortambvals(always)                     /* resort ambient values */
605   int     always;
# Line 560 | Line 607 | int    always;
607          AMBTREE  oldatrunk;
608          AMBVAL  tav, *tap, *pnext;
609          register int    i, j;
610 +                                        /* see if it's time yet */
611 +        if (!always && (ambclock < lastsort+sortintvl ||
612 +                        nambvals < SORT_THRESH))
613 +                return;
614          /*
615           * The idea here is to minimize memory thrashing
616           * in VM systems by improving reference locality.
# Line 575 | Line 626 | int    always;
626           * the "always" parameter set to 1 so that the ambient
627           * tree will be rebuilt with the new accuracy parameter.
628           */
629 <        if (tracktime) {
629 >        if (tracktime) {                /* allocate pointer arrays to sort */
630                  avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
631                  avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
632          } else
633                  avlist1 = avlist2 = NULL;
634 <        if (avlist2 == NULL) {          /* rebuild tree? */
634 >        if (avlist2 == NULL) {          /* no time tracking -- rebuild tree? */
635                  if (avlist1 != NULL)
636                          free((char *)avlist1);
637 <                if (!always)
638 <                        return;
639 <                copystruct(&oldatrunk, &atrunk);
640 <                atrunk.alist = NULL;
641 <                atrunk.kid = NULL;
642 <                unloadatree(&oldatrunk, avinsert);
637 >                if (always) {           /* rebuild without sorting */
638 >                        copystruct(&oldatrunk, &atrunk);
639 >                        atrunk.alist = NULL;
640 >                        atrunk.kid = NULL;
641 >                        unloadatree(&oldatrunk, avinsert);
642 >                }
643          } else {                        /* sort memory by last access time */
593                i_avlist = 0;
594                unloadatree(&atrunk, av2list);  /* empty current tree */
644                  /*
645                   * Sorting memory is tricky because it isn't contiguous.
646                   * We have to sort an array of pointers by MRA and also
647                   * by memory position.  We then copy values in "loops"
648                   * to minimize memory hits.  Nevertheless, we will visit
649 <                 * everyone at least once, and this is an expensive process
649 >                 * everyone at least twice, and this is an expensive process
650                   * when we're thrashing, which is when we need to do it.
651                   */
652 + #ifdef DEBUG
653 +                sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...",
654 +                                nambvals, ambclock);
655 +                eputs(errmsg);
656 + #endif
657 +                i_avlist = 0;
658 +                unloadatree(&atrunk, av2list);  /* empty current tree */
659 + #ifdef DEBUG
660 +                if (i_avlist < nambvals)
661 +                        error(CONSISTENCY, "missing ambient values in sortambvals");
662 + #endif
663                  qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp);
664                  qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
665                  for (i = 0; i < nambvals; i++) {
666 <                        if (avlist1[i] == NULL || avlist1[i] == avlist2[i])
666 >                        if (avlist1[i] == NULL)
667                                  continue;
668                          tap = avlist2[i];
669                          copystruct(&tav, tap);
670                          for (j = i; (pnext = avlist1[j]) != tap;
671 <                                        j = (AMBVAL **)bsearch((char *)&pnext,
612 <                                                (char *)(avlist2+i),nambvals-i,
613 <                                                sizeof(AMBVAL *),aposcmp) -
614 <                                                avlist2) {
671 >                                        j = avlmemi(pnext)) {
672                                  copystruct(avlist2[j], pnext);
673                                  avinsert(avlist2[j]);
674                                  avlist1[j] = NULL;
# Line 622 | Line 679 | int    always;
679                  }
680                  free((char *)avlist1);
681                  free((char *)avlist2);
682 <                if (sortintvl < MAXACLOCK/4)
682 >                                                /* compute new sort interval */
683 >                sortintvl = ambclock - lastsort;
684 >                if (sortintvl >= MAX_SORT_INTVL/2)
685 >                        sortintvl = MAX_SORT_INTVL;
686 >                else
687                          sortintvl <<= 1;        /* wait twice as long next */
688 + #ifdef DEBUG
689 +                eputs("done\n");
690 + #endif
691          }
692          if (ambclock >= MAXACLOCK)
693                  ambclock = MAXACLOCK/2;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines