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.23 by greg, Sat Dec 18 10:37:01 1993 UTC vs.
Revision 2.107 by greg, Sat Feb 16 00:09:53 2019 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1993 Regents of the University of California */
2 <
3 < #ifndef lint
4 < static char SCCSid[] = "$SunId$ LBL";
5 < #endif
6 <
1 > static const char       RCSid[] = "$Id$";
2   /*
3   *  ambient.c - routines dealing with ambient (inter-reflected) component.
4 + *
5 + *  Declarations of external symbols in ambient.h
6   */
7  
8 < #include  "ray.h"
8 > #include "copyright.h"
9  
10 < #include  "octree.h"
10 > #include <string.h>
11  
12 + #include  "platform.h"
13 + #include  "ray.h"
14   #include  "otypes.h"
15 <
15 > #include  "otspecial.h"
16 > #include  "resolu.h"
17   #include  "ambient.h"
18
18   #include  "random.h"
19 + #include  "pmapamb.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 {
24 <        AMBVAL  *alist;         /* ambient value list */
25 <        struct ambtree  *kid;   /* 8 child nodes */
26 < }  AMBTREE;                     /* ambient octree */
25 > extern char  *shm_boundary;     /* memory sharing boundary */
26  
27 < extern CUBE  thescene;          /* contains space boundaries */
28 <
29 < #define  MAXASET        511     /* maximum number of elements in ambient set */
27 > #ifndef  MAXASET
28 > #define  MAXASET        4095    /* maximum number of elements in ambient set */
29 > #endif
30   OBJECT  ambset[MAXASET+1]={0};  /* ambient include/exclude set */
31  
32   double  maxarad;                /* maximum ambient radius */
# Line 38 | Line 37 | static AMBTREE atrunk;         /* our ambient trunk node */
37   static FILE  *ambfp = NULL;     /* ambient file pointer */
38   static int  nunflshed = 0;      /* number of unflushed ambient values */
39  
40 + #ifndef SORT_THRESH
41 + #ifdef SMLMEM
42 + #define SORT_THRESH     ((16L<<20)/sizeof(AMBVAL))
43 + #else
44 + #define SORT_THRESH     ((64L<<20)/sizeof(AMBVAL))
45 + #endif
46 + #endif
47 + #ifndef SORT_INTVL
48 + #define SORT_INTVL      (SORT_THRESH<<1)
49 + #endif
50 + #ifndef MAX_SORT_INTVL
51 + #define MAX_SORT_INTVL  (SORT_INTVL<<6)
52 + #endif
53 +
54 +
55 + static double  avsum = 0.;              /* computed ambient value sum (log) */
56 + static unsigned int  navsum = 0;        /* number of values in avsum */
57 + static unsigned int  nambvals = 0;      /* total number of indirect values */
58 + static unsigned int  nambshare = 0;     /* number of values from file */
59 + static unsigned long  ambclock = 0;     /* ambient access clock */
60 + static unsigned long  lastsort = 0;     /* time of last value sort */
61 + static long  sortintvl = SORT_INTVL;    /* time until next sort */
62 + static FILE  *ambinp = NULL;            /* auxiliary file for input */
63 + static long  lastpos = -1;              /* last flush position */
64 +
65 + #define MAXACLOCK       (1L<<30)        /* clock turnover value */
66 +        /*
67 +         * Track access times unless we are sharing ambient values
68 +         * through memory on a multiprocessor, when we want to avoid
69 +         * claiming our own memory (copy on write).  Go ahead anyway
70 +         * if more than two thirds of our values are unshared.
71 +         * Compile with -Dtracktime=0 to turn this code off.
72 +         */
73 + #ifndef tracktime
74 + #define tracktime       (shm_boundary == NULL || nambvals > 3*nambshare)
75 + #endif
76 +
77   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
78  
79 < #define  newambval()    (AMBVAL *)bmalloc(sizeof(AMBVAL))
79 > #define  newambval()    (AMBVAL *)malloc(sizeof(AMBVAL))
80  
81 < #define  newambtree()   (AMBTREE *)calloc(8, sizeof(AMBTREE))
82 < #define  freeambtree(t) free((char *)(t))
81 > static void initambfile(int creat);
82 > static void avsave(AMBVAL *av);
83 > static AMBVAL *avstore(AMBVAL  *aval);
84 > static AMBTREE *newambtree(void);
85 > static void freeambtree(AMBTREE  *atp);
86  
87 < extern long  ftell(), lseek();
88 < static int  initambfile(), avsave(), avinsert(), loadatree();
89 < static AMBVAL  *avstore();
87 > typedef void unloadtf_t(AMBVAL *);
88 > static unloadtf_t avinsert;
89 > static unloadtf_t av2list;
90 > static unloadtf_t avfree;
91 > static void unloadatree(AMBTREE  *at, unloadtf_t *f);
92 >
93 > static int aposcmp(const void *avp1, const void *avp2);
94 > static int avlmemi(AMBVAL *avaddr);
95 > static void sortambvals(int always);
96 >
97   #ifdef  F_SETLKW
98 < static  aflock();
98 > static void aflock(int  typ);
99   #endif
100  
101  
102 < setambres(ar)                           /* set ambient resolution */
103 < int  ar;
102 > void
103 > setambres(                              /* set ambient resolution */
104 >        int  ar
105 > )
106   {
107          ambres = ar < 0 ? 0 : ar;               /* may be done already */
108                                                  /* set min & max radii */
109          if (ar <= 0) {
110 <                minarad = 0.0;
111 <                maxarad = thescene.cusize / 2.0;
110 >                minarad = 0;
111 >                maxarad = thescene.cusize*0.2;
112          } else {
113                  minarad = thescene.cusize / ar;
114 <                maxarad = 16.0 * minarad;               /* heuristic */
115 <                if (maxarad > thescene.cusize / 2.0)
116 <                        maxarad = thescene.cusize / 2.0;
114 >                maxarad = 64.0 * minarad;               /* heuristic */
115 >                if (maxarad > thescene.cusize*0.2)
116 >                        maxarad = thescene.cusize*0.2;
117          }
118 <        if (maxarad <= FTINY)
119 <                maxarad = .001;
118 >        if (minarad <= FTINY)
119 >                minarad = 10.0*FTINY;
120 >        if (maxarad <= minarad)
121 >                maxarad = 64.0 * minarad;
122   }
123  
124  
125 < setambacc(newa)                         /* set ambient accuracy */
126 < double  newa;
125 > void
126 > setambacc(                              /* set ambient accuracy */
127 >        double  newa
128 > )
129   {
130 <        static double  oldambacc = -1.0;
131 <        AMBTREE  oldatrunk;
132 <
133 <        ambacc = newa < 0.0 ? 0.0 : newa;       /* may be done already */
134 <        if (oldambacc < -FTINY)
135 <                oldambacc = ambacc;     /* do nothing first call */
136 <        if (fabs(newa - oldambacc) < 0.01)
137 <                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 */
130 >        static double   olda;           /* remember previous setting here */
131 >        
132 >        newa *= (newa > 0);
133 >        if (fabs(newa - olda) >= .05*(newa + olda)) {
134 >                ambacc = newa;
135 >                if (ambacc > FTINY && nambvals > 0)
136 >                        sortambvals(1);         /* rebuild tree */
137 >        }
138   }
139  
140  
141 < setambient(afile)                       /* initialize calculation */
142 < char  *afile;
141 > void
142 > setambient(void)                                /* initialize calculation */
143   {
144 <        long  headlen;
144 >        int     readonly = 0;
145 >        long    flen;
146          AMBVAL  amb;
147 +                                                /* make sure we're fresh */
148 +        ambdone();
149                                                  /* init ambient limits */
150          setambres(ambres);
151          setambacc(ambacc);
152 <        if (afile == NULL)
152 >        if (ambfile == NULL || !ambfile[0])
153                  return;
154          if (ambacc <= FTINY) {
155                  sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened",
156 <                                afile);
156 >                                ambfile);
157                  error(WARNING, errmsg);
158                  return;
159          }
160                                                  /* open ambient file */
161 <        if ((ambfp = fopen(afile, "r+")) != NULL) {
162 <                initambfile(0);
163 <                headlen = ftell(ambfp);
161 >        if ((ambfp = fopen(ambfile, "r+")) == NULL)
162 >                readonly = (ambfp = fopen(ambfile, "r")) != NULL;
163 >        if (ambfp != NULL) {
164 >                initambfile(0);                 /* file exists */
165 >                lastpos = ftell(ambfp);
166                  while (readambval(&amb, ambfp))
167 <                        avinsert(avstore(&amb));
168 <                                                /* align */
169 <                fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
170 <        } else if ((ambfp = fopen(afile, "w+")) != NULL)
171 <                initambfile(1);
172 <        else {
173 <                sprintf(errmsg, "cannot open ambient file \"%s\"", afile);
167 >                        avstore(&amb);
168 >                nambshare = nambvals;           /* share loaded values */
169 >                if (readonly) {
170 >                        sprintf(errmsg,
171 >                                "loaded %u values from read-only ambient file",
172 >                                        nambvals);
173 >                        error(WARNING, errmsg);
174 >                        fclose(ambfp);          /* close file so no writes */
175 >                        ambfp = NULL;
176 >                        return;                 /* avoid ambsync() */
177 >                }
178 >                                                /* align file pointer */
179 >                lastpos += (long)nambvals*AMBVALSIZ;
180 >                flen = lseek(fileno(ambfp), (off_t)0, SEEK_END);
181 >                if (flen != lastpos) {
182 >                        sprintf(errmsg,
183 >                        "ignoring last %ld values in ambient file (corrupted)",
184 >                                        (flen - lastpos)/AMBVALSIZ);
185 >                        error(WARNING, errmsg);
186 >                        fseek(ambfp, lastpos, SEEK_SET);
187 >                        ftruncate(fileno(ambfp), (off_t)lastpos);
188 >                }
189 >        } else if ((ambfp = fopen(ambfile, "w+")) != NULL) {
190 >                initambfile(1);                 /* else create new file */
191 >                fflush(ambfp);
192 >                lastpos = ftell(ambfp);
193 >        } else {
194 >                sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile);
195                  error(SYSTEM, errmsg);
196          }
197 <        nunflshed++;    /* lie */
198 <        ambsync();
197 > #ifdef  F_SETLKW
198 >        aflock(F_UNLCK);                        /* release file */
199 > #endif
200   }
201  
202  
203 < ambnotify(obj)                  /* record new modifier */
204 < OBJECT  obj;
203 > void
204 > ambdone(void)                   /* close ambient file and free memory */
205   {
206 +        if (ambfp != NULL) {            /* close ambient file */
207 +                ambsync();
208 +                fclose(ambfp);
209 +                ambfp = NULL;
210 +                if (ambinp != NULL) {  
211 +                        fclose(ambinp);
212 +                        ambinp = NULL;
213 +                }
214 +                lastpos = -1;
215 +        }
216 +                                        /* free ambient tree */
217 +        unloadatree(&atrunk, avfree);
218 +                                        /* reset state variables */
219 +        avsum = 0.;
220 +        navsum = 0;
221 +        nambvals = 0;
222 +        nambshare = 0;
223 +        ambclock = 0;
224 +        lastsort = 0;
225 +        sortintvl = SORT_INTVL;
226 + }
227 +
228 +
229 + void
230 + ambnotify(                      /* record new modifier */
231 +        OBJECT  obj
232 + )
233 + {
234          static int  hitlimit = 0;
235 <        register OBJREC  *o = objptr(obj);
236 <        register char  **amblp;
235 >        OBJREC   *o;
236 >        char  **amblp;
237  
238 +        if (obj == OVOID) {             /* starting over */
239 +                ambset[0] = 0;
240 +                hitlimit = 0;
241 +                return;
242 +        }
243 +        o = objptr(obj);
244          if (hitlimit || !ismodifier(o->otype))
245                  return;
246          for (amblp = amblist; *amblp != NULL; amblp++)
# Line 150 | Line 255 | OBJECT obj;
255                  }
256   }
257  
258 + /************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/
259  
260 < ambient(acol, r)                /* compute ambient component for ray */
261 < COLOR  acol;
262 < register RAY  *r;
260 > #ifndef OLDAMB
261 >
262 > #define tfunc(lwr, x, upr)      (((x)-(lwr))/((upr)-(lwr)))
263 >
264 > static int      plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang);
265 > static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
266 >                                AMBTREE *at, FVECT c0, double s);
267 > static int      makeambient(COLOR acol, RAY *r, FVECT rn, int al);
268 > static int      extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
269 >                                FVECT uvw[3]);
270 >
271 > void
272 > multambient(            /* compute ambient component & multiply by coef. */
273 >        COLOR  aval,
274 >        RAY  *r,
275 >        FVECT  nrm
276 > )
277   {
278          static int  rdepth = 0;                 /* ambient recursion */
279 <        double  d;
279 >        COLOR   acol, caustic;
280 >        int     i, ok;
281 >        double  d, l;
282  
283 +        /* PMAP: Factor in ambient from photon map, if enabled and ray is
284 +         * ambient. Return as all ambient components accounted for, else
285 +         * continue. */
286 +        if (ambPmap(aval, r, rdepth))
287 +                return;
288 +
289 +        /* PMAP: Factor in specular-diffuse ambient (caustics) from photon
290 +         * map, if enabled and ray is primary, else caustic is zero.  Continue
291 +         * with RADIANCE ambient calculation */
292 +        copycolor(caustic, aval);
293 +        ambPmapCaustic(caustic, r, rdepth);
294 +        
295          if (ambdiv <= 0)                        /* no ambient calculation */
296                  goto dumbamb;
297                                                  /* check number of bounces */
# Line 169 | Line 303 | register RAY  *r;
303                  goto dumbamb;
304  
305          if (ambacc <= FTINY) {                  /* no ambient storage */
306 +                FVECT   uvd[2];
307 +                float   dgrad[2], *dgp = NULL;
308 +
309 +                if (nrm != r->ron && DOT(nrm,r->ron) < 0.9999)
310 +                        dgp = dgrad;            /* compute rotational grad. */
311 +                copycolor(acol, aval);
312                  rdepth++;
313 <                d = doambient(acol, r, r->rweight, NULL, NULL);
313 >                ok = doambient(acol, r, r->rweight,
314 >                                uvd, NULL, NULL, dgp, NULL);
315                  rdepth--;
316 <                if (d == 0.0)
316 >                if (!ok)
317                          goto dumbamb;
318 +                if ((ok > 0) & (dgp != NULL)) { /* apply texture */
319 +                        FVECT   v1;
320 +                        VCROSS(v1, r->ron, nrm);
321 +                        d = 1.0;
322 +                        for (i = 3; i--; )
323 +                                d += v1[i] * (dgp[0]*uvd[0][i] + dgp[1]*uvd[1][i]);
324 +                        if (d >= 0.05)
325 +                                scalecolor(acol, d);
326 +                }
327 +                copycolor(aval, acol);
328 +
329 +                /* PMAP: add in caustic */
330 +                addcolor(aval, caustic);
331                  return;
332          }
333 <                                                /* get ambient value */
333 >
334 >        if (tracktime)                          /* sort to minimize thrashing */
335 >                sortambvals(0);
336 >                                                /* interpolate ambient value */
337          setcolor(acol, 0.0, 0.0, 0.0);
338 <        d = sumambient(acol, r, rdepth,
338 >        d = sumambient(acol, r, nrm, rdepth,
339                          &atrunk, thescene.cuorg, thescene.cusize);
340 <        if (d > FTINY)
341 <                scalecolor(acol, 1.0/d);
342 <        else {
343 <                d = makeambient(acol, r, rdepth++);
340 >                        
341 >        if (d > FTINY) {
342 >                d = 1.0/d;
343 >                scalecolor(acol, d);
344 >                multcolor(aval, acol);
345 >
346 >                /* PMAP: add in caustic */
347 >                addcolor(aval, caustic);
348 >                return;
349 >        }
350 >        
351 >        rdepth++;                               /* need to cache new value */
352 >        ok = makeambient(acol, r, nrm, rdepth-1);
353 >        rdepth--;
354 >        
355 >        if (ok) {
356 >                multcolor(aval, acol);          /* computed new value */
357 >
358 >                /* PMAP: add in caustic */
359 >                addcolor(aval, caustic);
360 >                return;
361 >        }
362 >        
363 > dumbamb:                                        /* return global value */
364 >        if ((ambvwt <= 0) | (navsum == 0)) {
365 >                multcolor(aval, ambval);
366 >                
367 >                /* PMAP: add in caustic */
368 >                addcolor(aval, caustic);
369 >                return;
370 >        }
371 >        
372 >        l = bright(ambval);                     /* average in computations */  
373 >        if (l > FTINY) {
374 >                d = (log(l)*(double)ambvwt + avsum) /
375 >                                (double)(ambvwt + navsum);
376 >                d = exp(d) / l;
377 >                scalecolor(aval, d);
378 >                multcolor(aval, ambval);        /* apply color of ambval */
379 >        } else {
380 >                d = exp( avsum / (double)navsum );
381 >                scalecolor(aval, d);            /* neutral color */
382 >        }
383 > }
384 >
385 >
386 > /* Plug a potential leak where ambient cache value is occluded */
387 > static int
388 > plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang)
389 > {
390 >        const double    cost70sq = 0.1169778;   /* cos(70deg)^2 */
391 >        RAY             rtst;
392 >        FVECT           vdif;
393 >        double          normdot, ndotd, nadotd;
394 >        double          a, b, c, t[2];
395 >
396 >        ang += 2.*PI*(ang < 0);                 /* check direction flags */
397 >        if ( !(ap->corral>>(int)(ang*(16./PI)) & 1) )
398 >                return(0);
399 >        /*
400 >         * Generate test ray, targeting 20 degrees above sample point plane
401 >         * along surface normal from cache position.  This should be high
402 >         * enough to miss local geometry we don't really care about.
403 >         */
404 >        VSUB(vdif, ap->pos, r->rop);
405 >        normdot = DOT(anorm, r->ron);
406 >        ndotd = DOT(vdif, r->ron);
407 >        nadotd = DOT(vdif, anorm);
408 >        a = normdot*normdot - cost70sq;
409 >        b = 2.0*(normdot*ndotd - nadotd*cost70sq);
410 >        c = ndotd*ndotd - DOT(vdif,vdif)*cost70sq;
411 >        if (quadratic(t, a, b, c) != 2)
412 >                return(1);                      /* should rarely happen */
413 >        if (t[1] <= FTINY)
414 >                return(0);                      /* should fail behind test */
415 >        rayorigin(&rtst, SHADOW, r, NULL);
416 >        VSUM(rtst.rdir, vdif, anorm, t[1]);     /* further dist. > plane */
417 >        rtst.rmax = normalize(rtst.rdir);       /* short ray test */
418 >        while (localhit(&rtst, &thescene)) {    /* check for occluder */
419 >                OBJREC  *m = findmaterial(rtst.ro);
420 >                if (m != NULL && !istransp(m->otype) && !isBSDFproxy(m) &&
421 >                                (rtst.clipset == NULL ||
422 >                                        !inset(rtst.clipset, rtst.ro->omod)))
423 >                        return(1);              /* plug light leak */
424 >                VCOPY(rtst.rorg, rtst.rop);     /* skip invisible surface */
425 >                rtst.rmax -= rtst.rot;
426 >                rayclear(&rtst);
427 >        }
428 >        return(0);                              /* seems we're OK */
429 > }
430 >
431 >
432 > static double
433 > sumambient(             /* get interpolated ambient value */
434 >        COLOR  acol,
435 >        RAY  *r,
436 >        FVECT  rn,
437 >        int  al,
438 >        AMBTREE  *at,
439 >        FVECT  c0,
440 >        double  s
441 > )
442 > {                       /* initial limit is 10 degrees plus ambacc radians */
443 >        const double    minangle = 10.0 * PI/180.;
444 >        double          maxangle = minangle + ambacc;
445 >        double          wsum = 0.0;
446 >        FVECT           ck0;
447 >        int             i, j;
448 >        AMBVAL          *av;
449 >
450 >        if (at->kid != NULL) {          /* sum children first */                                
451 >                s *= 0.5;
452 >                for (i = 0; i < 8; i++) {
453 >                        for (j = 0; j < 3; j++) {
454 >                                ck0[j] = c0[j];
455 >                                if (1<<j & i)
456 >                                        ck0[j] += s;
457 >                                if (r->rop[j] < ck0[j] - OCTSCALE*s)
458 >                                        break;
459 >                                if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
460 >                                        break;
461 >                        }
462 >                        if (j == 3)
463 >                                wsum += sumambient(acol, r, rn, al,
464 >                                                        at->kid+i, ck0, s);
465 >                }
466 >                                        /* good enough? */
467 >                if (wsum >= 0.05 && s > minarad*10.0)
468 >                        return(wsum);
469 >        }
470 >                                        /* adjust maximum angle */
471 >        if (at->alist != NULL && (at->alist->lvl <= al) & (r->rweight < 0.6))
472 >                maxangle = (maxangle - PI/2.)*pow(r->rweight,0.13) + PI/2.;
473 >                                        /* sum this node */
474 >        for (av = at->alist; av != NULL; av = av->next) {
475 >                double  u, v, d, delta_r2, delta_t2;
476 >                COLOR   ct;
477 >                FVECT   uvw[3];
478 >                                        /* record access */
479 >                if (tracktime)
480 >                        av->latick = ambclock;
481 >                /*
482 >                 *  Ambient level test
483 >                 */
484 >                if (av->lvl > al ||     /* list sorted, so this works */
485 >                                (av->lvl == al) & (av->weight < 0.9*r->rweight))
486 >                        break;
487 >                /*
488 >                 *  Direction test using unperturbed normal
489 >                 */
490 >                decodedir(uvw[2], av->ndir);
491 >                d = DOT(uvw[2], r->ron);
492 >                if (d <= 0.0)           /* >= 90 degrees */
493 >                        continue;
494 >                delta_r2 = 2.0 - 2.0*d; /* approx. radians^2 */
495 >                if (delta_r2 >= maxangle*maxangle)
496 >                        continue;
497 >                /*
498 >                 *  Modified ray behind test
499 >                 */
500 >                VSUB(ck0, r->rop, av->pos);
501 >                d = DOT(ck0, uvw[2]);
502 >                if (d < -minarad*ambacc-.001)
503 >                        continue;
504 >                d /= av->rad[0];
505 >                delta_t2 = d*d;
506 >                if (delta_t2 >= ambacc*ambacc)
507 >                        continue;
508 >                /*
509 >                 *  Elliptical radii test based on Hessian
510 >                 */
511 >                decodedir(uvw[0], av->udir);
512 >                VCROSS(uvw[1], uvw[2], uvw[0]);
513 >                d = (u = DOT(ck0, uvw[0])) / av->rad[0];
514 >                delta_t2 += d*d;
515 >                d = (v = DOT(ck0, uvw[1])) / av->rad[1];
516 >                delta_t2 += d*d;
517 >                if (delta_t2 >= ambacc*ambacc)
518 >                        continue;
519 >                /*
520 >                 *  Test for potential light leak
521 >                 */
522 >                if (av->corral && plugaleak(r, av, uvw[2], atan2a(v,u)))
523 >                        continue;
524 >                /*
525 >                 *  Extrapolate value and compute final weight (hat function)
526 >                 */
527 >                if (!extambient(ct, av, r->rop, rn, uvw))
528 >                        continue;
529 >                d = tfunc(maxangle, sqrt(delta_r2), 0.0) *
530 >                        tfunc(ambacc, sqrt(delta_t2), 0.0);
531 >                scalecolor(ct, d);
532 >                addcolor(acol, ct);
533 >                wsum += d;
534 >        }
535 >        return(wsum);
536 > }
537 >
538 >
539 > static int
540 > makeambient(            /* make a new ambient value for storage */
541 >        COLOR  acol,
542 >        RAY  *r,
543 >        FVECT  rn,
544 >        int  al
545 > )
546 > {
547 >        AMBVAL  amb;
548 >        FVECT   uvw[3];
549 >        int     i;
550 >
551 >        amb.weight = 1.0;                       /* compute weight */
552 >        for (i = al; i-- > 0; )
553 >                amb.weight *= AVGREFL;
554 >        if (r->rweight < 0.1*amb.weight)        /* heuristic override */
555 >                amb.weight = 1.25*r->rweight;
556 >        setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
557 >                                                /* compute ambient */
558 >        i = doambient(acol, r, amb.weight,
559 >                        uvw, amb.rad, amb.gpos, amb.gdir, &amb.corral);
560 >        scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
561 >        if (i <= 0 || amb.rad[0] <= FTINY)      /* no Hessian or zero radius */
562 >                return(i);
563 >                                                /* store value */
564 >        VCOPY(amb.pos, r->rop);
565 >        amb.ndir = encodedir(r->ron);
566 >        amb.udir = encodedir(uvw[0]);
567 >        amb.lvl = al;
568 >        copycolor(amb.val, acol);
569 >                                                /* insert into tree */
570 >        avsave(&amb);                           /* and save to file */
571 >        if (rn != r->ron) {                     /* texture */
572 >                VCOPY(uvw[2], r->ron);
573 >                extambient(acol, &amb, r->rop, rn, uvw);
574 >        }
575 >        return(1);
576 > }
577 >
578 >
579 > static int
580 > extambient(             /* extrapolate value at pv, nv */
581 >        COLOR  cr,
582 >        AMBVAL   *ap,
583 >        FVECT  pv,
584 >        FVECT  nv,
585 >        FVECT  uvw[3]
586 > )
587 > {
588 >        const double    min_d = 0.05;
589 >        static FVECT    my_uvw[3];
590 >        FVECT           v1;
591 >        int             i;
592 >        double          d = 1.0;        /* zeroeth order */
593 >
594 >        if (uvw == NULL) {              /* need local coordinates? */
595 >                decodedir(my_uvw[2], ap->ndir);
596 >                decodedir(my_uvw[0], ap->udir);
597 >                VCROSS(my_uvw[1], my_uvw[2], my_uvw[0]);
598 >                uvw = my_uvw;
599 >        }
600 >        for (i = 3; i--; )              /* gradient due to translation */
601 >                d += (pv[i] - ap->pos[i]) *
602 >                        (ap->gpos[0]*uvw[0][i] + ap->gpos[1]*uvw[1][i]);
603 >
604 >        VCROSS(v1, uvw[2], nv);         /* gradient due to rotation */
605 >        for (i = 3; i--; )
606 >                d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]);
607 >        
608 >        if (d < min_d)                  /* should not use if we can avoid it */
609 >                d = min_d;
610 >        copycolor(cr, ap->val);
611 >        scalecolor(cr, d);
612 >        return(d > min_d);
613 > }
614 >
615 >
616 > static void
617 > avinsert(                               /* insert ambient value in our tree */
618 >        AMBVAL *av
619 > )
620 > {
621 >        AMBTREE  *at;
622 >        AMBVAL  *ap;
623 >        AMBVAL  avh;
624 >        FVECT  ck0;
625 >        double  s;
626 >        int  branch;
627 >        int  i;
628 >
629 >        if (av->rad[0] <= FTINY)
630 >                error(CONSISTENCY, "zero ambient radius in avinsert");
631 >        at = &atrunk;
632 >        VCOPY(ck0, thescene.cuorg);
633 >        s = thescene.cusize;
634 >        while (s*(OCTSCALE/2) > av->rad[1]*ambacc) {
635 >                if (at->kid == NULL)
636 >                        if ((at->kid = newambtree()) == NULL)
637 >                                error(SYSTEM, "out of memory in avinsert");
638 >                s *= 0.5;
639 >                branch = 0;
640 >                for (i = 0; i < 3; i++)
641 >                        if (av->pos[i] > ck0[i] + s) {
642 >                                ck0[i] += s;
643 >                                branch |= 1 << i;
644 >                        }
645 >                at = at->kid + branch;
646 >        }
647 >        avh.next = at->alist;           /* order by increasing level */
648 >        for (ap = &avh; ap->next != NULL; ap = ap->next)
649 >                if ( ap->next->lvl > av->lvl ||
650 >                                (ap->next->lvl == av->lvl) &
651 >                                (ap->next->weight <= av->weight) )
652 >                        break;
653 >        av->next = ap->next;
654 >        ap->next = (AMBVAL*)av;
655 >        at->alist = avh.next;
656 > }
657 >
658 >
659 > #else /* ! NEWAMB */
660 >
661 > static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
662 >                                AMBTREE *at, FVECT c0, double s);
663 > static double   makeambient(COLOR acol, RAY *r, FVECT rn, int al);
664 > static void     extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv);
665 >
666 >
667 > void
668 > multambient(            /* compute ambient component & multiply by coef. */
669 >        COLOR  aval,
670 >        RAY  *r,
671 >        FVECT  nrm
672 > )
673 > {
674 >        static int  rdepth = 0;                 /* ambient recursion */
675 >        COLOR   acol, caustic;
676 >        double  d, l;
677 >
678 >        /* PMAP: Factor in ambient from global photon map (if enabled) and return
679 >         * as all ambient components accounted for */
680 >        if (ambPmap(aval, r, rdepth))
681 >                return;
682 >
683 >        /* PMAP: Otherwise factor in ambient from caustic photon map
684 >         * (ambPmapCaustic() returns zero if caustic photons disabled) and
685 >         * continue with RADIANCE ambient calculation */
686 >        copycolor(caustic, aval);
687 >        ambPmapCaustic(caustic, r, rdepth);
688 >        
689 >        if (ambdiv <= 0)                        /* no ambient calculation */
690 >                goto dumbamb;
691 >                                                /* check number of bounces */
692 >        if (rdepth >= ambounce)
693 >                goto dumbamb;
694 >                                                /* check ambient list */
695 >        if (ambincl != -1 && r->ro != NULL &&
696 >                        ambincl != inset(ambset, r->ro->omod))
697 >                goto dumbamb;
698 >
699 >        if (ambacc <= FTINY) {                  /* no ambient storage */
700 >                copycolor(acol, aval);
701 >                rdepth++;
702 >                d = doambient(acol, r, r->rweight, NULL, NULL);
703                  rdepth--;
704 +                if (d <= FTINY)
705 +                        goto dumbamb;
706 +                copycolor(aval, acol);          
707 +        
708 +           /* PMAP: add in caustic */
709 +                addcolor(aval, caustic);        
710 +                return;
711          }
712 <        if (d > FTINY)
712 >
713 >        if (tracktime)                          /* sort to minimize thrashing */
714 >                sortambvals(0);
715 >                                                /* interpolate ambient value */
716 >        setcolor(acol, 0.0, 0.0, 0.0);
717 >        d = sumambient(acol, r, nrm, rdepth,
718 >                        &atrunk, thescene.cuorg, thescene.cusize);
719 >                        
720 >        if (d > FTINY) {
721 >                d = 1.0/d;
722 >                scalecolor(acol, d);
723 >                multcolor(aval, acol);
724 >                
725 >                /* PMAP: add in caustic */
726 >                addcolor(aval, caustic);        
727                  return;
728 +        }
729 +        
730 +        rdepth++;                               /* need to cache new value */
731 +        d = makeambient(acol, r, nrm, rdepth-1);
732 +        rdepth--;
733 +        
734 +        if (d > FTINY) {
735 +                multcolor(aval, acol);          /* got new value */
736 +
737 +                /* PMAP: add in caustic */
738 +                addcolor(aval, caustic);                        
739 +                return;
740 +        }
741 +        
742   dumbamb:                                        /* return global value */
743 <        copycolor(acol, ambval);
743 >        if ((ambvwt <= 0) | (navsum == 0)) {
744 >                multcolor(aval, ambval);
745 >
746 >                /* PMAP: add in caustic */
747 >                addcolor(aval, caustic);        
748 >                return;
749 >        }
750 >        
751 >        l = bright(ambval);                     /* average in computations */
752 >        if (l > FTINY) {
753 >                d = (log(l)*(double)ambvwt + avsum) /
754 >                                (double)(ambvwt + navsum);
755 >                d = exp(d) / l;
756 >                scalecolor(aval, d);
757 >                multcolor(aval, ambval);        /* apply color of ambval */
758 >        } else {
759 >                d = exp( avsum / (double)navsum );
760 >                scalecolor(aval, d);            /* neutral color */
761 >        }
762   }
763  
764  
765 < double
766 < sumambient(acol, r, al, at, c0, s)      /* get interpolated ambient value */
767 < COLOR  acol;
768 < register RAY  *r;
769 < int  al;
770 < AMBTREE  *at;
771 < FVECT  c0;
772 < double  s;
765 > static double
766 > sumambient(     /* get interpolated ambient value */
767 >        COLOR  acol,
768 >        RAY  *r,
769 >        FVECT  rn,
770 >        int  al,
771 >        AMBTREE  *at,
772 >        FVECT  c0,
773 >        double  s
774 > )
775   {
776          double  d, e1, e2, wt, wsum;
777          COLOR  ct;
778          FVECT  ck0;
779          int  i;
780 <        register int  j;
781 <        register AMBVAL  *av;
782 <                                        /* do this node */
780 >        int  j;
781 >        AMBVAL   *av;
782 >
783          wsum = 0.0;
784 +                                        /* do this node */
785          for (av = at->alist; av != NULL; av = av->next) {
786 +                double  rn_dot = -2.0;
787 +                if (tracktime)
788 +                        av->latick = ambclock;
789                  /*
790                   *  Ambient level test.
791                   */
792 <                if (av->lvl > al)       /* list sorted, so this works */
792 >                if (av->lvl > al ||     /* list sorted, so this works */
793 >                                (av->lvl == al) & (av->weight < 0.9*r->rweight))
794                          break;
219                if (av->weight < r->rweight-FTINY)
220                        continue;
795                  /*
796                   *  Ambient radius test.
797                   */
798 <                e1 = 0.0;
799 <                for (j = 0; j < 3; j++) {
226 <                        d = av->pos[j] - r->rop[j];
227 <                        e1 += d * d;
228 <                }
229 <                e1 /= av->rad * av->rad;
798 >                VSUB(ck0, av->pos, r->rop);
799 >                e1 = DOT(ck0, ck0) / (av->rad * av->rad);
800                  if (e1 > ambacc*ambacc*1.21)
801                          continue;
802                  /*
803 <                 *  Normal direction test.
803 >                 *  Direction test using closest normal.
804                   */
805 <                e2 = (1.0 - DOT(av->dir, r->ron)) * r->rweight;
806 <                if (e2 < 0.0) e2 = 0.0;
807 <                if (e1 + e2 > ambacc*ambacc*1.21)
805 >                d = DOT(av->dir, r->ron);
806 >                if (rn != r->ron) {
807 >                        rn_dot = DOT(av->dir, rn);
808 >                        if (rn_dot > 1.0-FTINY)
809 >                                rn_dot = 1.0-FTINY;
810 >                        if (rn_dot >= d-FTINY) {
811 >                                d = rn_dot;
812 >                                rn_dot = -2.0;
813 >                        }
814 >                }
815 >                e2 = (1.0 - d) * r->rweight;
816 >                if (e2 < 0.0)
817 >                        e2 = 0.0;
818 >                else if (e1 + e2 > ambacc*ambacc*1.21)
819                          continue;
820                  /*
821                   *  Ray behind test.
# Line 248 | Line 829 | double s;
829                  /*
830                   *  Jittering final test reduces image artifacts.
831                   */
832 <                wt = sqrt(e1) + sqrt(e2);
833 <                wt *= .9 + .2*urand(9015+samplendx);
834 <                if (wt > ambacc)
832 >                e1 = sqrt(e1);
833 >                e2 = sqrt(e2);
834 >                wt = e1 + e2;
835 >                if (wt > ambacc*(.9+.2*urand(9015+samplendx)))
836                          continue;
837 +                /*
838 +                 *  Recompute directional error using perturbed normal
839 +                 */
840 +                if (rn_dot > 0.0) {
841 +                        e2 = sqrt((1.0 - rn_dot)*r->rweight);
842 +                        wt = e1 + e2;
843 +                }
844                  if (wt <= 1e-3)
845                          wt = 1e3;
846                  else
847                          wt = 1.0 / wt;
848                  wsum += wt;
849 <                extambient(ct, av, r->rop, r->ron);
849 >                extambient(ct, av, r->rop, rn);
850                  scalecolor(ct, wt);
851                  addcolor(acol, ct);
852          }
# Line 276 | Line 865 | double s;
865                                  break;
866                  }
867                  if (j == 3)
868 <                        wsum += sumambient(acol, r, al, at->kid+i, ck0, s);
868 >                        wsum += sumambient(acol, r, rn, al,
869 >                                                at->kid+i, ck0, s);
870          }
871          return(wsum);
872   }
873  
874  
875 < double
876 < makeambient(acol, r, al)        /* make a new ambient value */
877 < COLOR  acol;
878 < register RAY  *r;
879 < int  al;
875 > static double
876 > makeambient(            /* make a new ambient value for storage */
877 >        COLOR  acol,
878 >        RAY  *r,
879 >        FVECT  rn,
880 >        int  al
881 > )
882   {
883          AMBVAL  amb;
884          FVECT   gp, gd;
885 <                                                /* compute weight */
886 <        amb.weight = pow(AVGREFL, (double)al);
887 <        if (r->rweight < 0.2*amb.weight)        /* heuristic */
888 <                amb.weight = r->rweight;
885 >        int     i;
886 >
887 >        amb.weight = 1.0;                       /* compute weight */
888 >        for (i = al; i-- > 0; )
889 >                amb.weight *= AVGREFL;
890 >        if (r->rweight < 0.1*amb.weight)        /* heuristic override */
891 >                amb.weight = 1.25*r->rweight;
892 >        setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
893                                                  /* compute ambient */
894          amb.rad = doambient(acol, r, amb.weight, gp, gd);
895 <        if (amb.rad == 0.0)
895 >        if (amb.rad <= FTINY) {
896 >                setcolor(acol, 0.0, 0.0, 0.0);
897                  return(0.0);
898 <                                                /* store it */
898 >        }
899 >        scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
900 >                                                /* store value */
901          VCOPY(amb.pos, r->rop);
902          VCOPY(amb.dir, r->ron);
903          amb.lvl = al;
# Line 307 | Line 906 | int  al;
906          VCOPY(amb.gdir, gd);
907                                                  /* insert into tree */
908          avsave(&amb);                           /* and save to file */
909 +        if (rn != r->ron)
910 +                extambient(acol, &amb, r->rop, rn);     /* texture */
911          return(amb.rad);
912   }
913  
914  
915 < extambient(cr, ap, pv, nv)              /* extrapolate value at pv, nv */
916 < COLOR  cr;
917 < register AMBVAL  *ap;
918 < FVECT  pv, nv;
915 > static void
916 > extambient(             /* extrapolate value at pv, nv */
917 >        COLOR  cr,
918 >        AMBVAL   *ap,
919 >        FVECT  pv,
920 >        FVECT  nv
921 > )
922   {
923 <        FVECT  v1, v2;
924 <        register int  i;
923 >        FVECT  v1;
924 >        int  i;
925          double  d;
926  
927          d = 1.0;                        /* zeroeth order */
# Line 325 | Line 929 | FVECT  pv, nv;
929          for (i = 0; i < 3; i++)
930                  d += ap->gpos[i]*(pv[i]-ap->pos[i]);
931                                          /* gradient due to rotation */
932 <        VCOPY(v1, ap->dir);
933 <        fcross(v2, v1, nv);
330 <        d += DOT(ap->gdir, v2);
932 >        VCROSS(v1, ap->dir, nv);
933 >        d += DOT(ap->gdir, v1);
934          if (d <= 0.0) {
935                  setcolor(cr, 0.0, 0.0, 0.0);
936                  return;
# Line 337 | Line 940 | FVECT  pv, nv;
940   }
941  
942  
943 < static
944 < initambfile(creat)              /* initialize ambient file */
945 < int  creat;
943 > static void
944 > avinsert(                               /* insert ambient value in our tree */
945 >        AMBVAL *av
946 > )
947   {
948 <        extern char  *progname, *octname, VersionID[];
948 >        AMBTREE  *at;
949 >        AMBVAL  *ap;
950 >        AMBVAL  avh;
951 >        FVECT  ck0;
952 >        double  s;
953 >        int  branch;
954 >        int  i;
955  
956 +        if (av->rad <= FTINY)
957 +                error(CONSISTENCY, "zero ambient radius in avinsert");
958 +        at = &atrunk;
959 +        VCOPY(ck0, thescene.cuorg);
960 +        s = thescene.cusize;
961 +        while (s*(OCTSCALE/2) > av->rad*ambacc) {
962 +                if (at->kid == NULL)
963 +                        if ((at->kid = newambtree()) == NULL)
964 +                                error(SYSTEM, "out of memory in avinsert");
965 +                s *= 0.5;
966 +                branch = 0;
967 +                for (i = 0; i < 3; i++)
968 +                        if (av->pos[i] > ck0[i] + s) {
969 +                                ck0[i] += s;
970 +                                branch |= 1 << i;
971 +                        }
972 +                at = at->kid + branch;
973 +        }
974 +        avh.next = at->alist;           /* order by increasing level */
975 +        for (ap = &avh; ap->next != NULL; ap = ap->next)
976 +                if ( ap->next->lvl > av->lvl ||
977 +                                (ap->next->lvl == av->lvl) &
978 +                                (ap->next->weight <= av->weight) )
979 +                        break;
980 +        av->next = ap->next;
981 +        ap->next = (AMBVAL*)av;
982 +        at->alist = avh.next;
983 + }
984 +
985 + #endif  /* ! NEWAMB */
986 +
987 + /************* FOLLOWING ROUTINES SAME FOR NEW & OLD METHODS ***************/
988 +
989 + static void
990 + initambfile(            /* initialize ambient file */
991 +        int  cre8
992 + )
993 + {
994 +        extern char  *progname, *octname;
995 +        static char  *mybuf = NULL;
996 +
997   #ifdef  F_SETLKW
998 <        aflock(creat ? F_WRLCK : F_RDLCK);
998 >        aflock(cre8 ? F_WRLCK : F_RDLCK);
999   #endif
1000 < #ifdef MSDOS
1001 <        setmode(fileno(ambfp), O_BINARY);
1002 < #endif
1003 <        setbuf(ambfp, bmalloc(BUFSIZ+8));
1004 <        if (creat) {                    /* new file */
1005 <                fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ",
1000 >        SET_FILE_BINARY(ambfp);
1001 >        if (mybuf == NULL)
1002 >                mybuf = (char *)bmalloc(BUFSIZ+8);
1003 >        setbuf(ambfp, mybuf);
1004 >        if (cre8) {                     /* new file */
1005 >                newheader("RADIANCE", ambfp);
1006 >                fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ",
1007                                  progname, colval(ambval,RED),
1008                                  colval(ambval,GRN), colval(ambval,BLU),
1009 <                                ambounce, ambacc);
1010 <                fprintf(ambfp, "-ad %d -as %d -ar %d %s\n",
1011 <                                ambdiv, ambssamp, ambres,
1012 <                                octname==NULL ? "" : octname);
1009 >                                ambvwt, ambounce, ambacc);
1010 >                fprintf(ambfp, "-ad %d -as %d -ar %d ",
1011 >                                ambdiv, ambssamp, ambres);
1012 >                if (octname != NULL)
1013 >                        fputs(octname, ambfp);
1014 >                fputc('\n', ambfp);
1015                  fprintf(ambfp, "SOFTWARE= %s\n", VersionID);
1016 +                fputnow(ambfp);
1017                  fputformat(AMBFMT, ambfp);
1018 <                putc('\n', ambfp);
1018 >                fputc('\n', ambfp);
1019                  putambmagic(ambfp);
1020          } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp))
1021                  error(USER, "bad ambient file");
1022   }
1023  
1024  
1025 < static
1026 < avsave(av)                              /* insert and save an ambient value */
1027 < AMBVAL  *av;
1025 > static void
1026 > avsave(                         /* insert and save an ambient value */
1027 >        AMBVAL  *av
1028 > )
1029   {
1030 <        avinsert(avstore(av));
1030 >        avstore(av);
1031          if (ambfp == NULL)
1032                  return;
1033          if (writambval(av, ambfp) < 0)
# Line 381 | Line 1037 | AMBVAL *av;
1037                          goto writerr;
1038          return;
1039   writerr:
1040 <        error(SYSTEM, "error writing ambient file");
1040 >        error(SYSTEM, "error writing to ambient file");
1041   }
1042  
1043  
1044   static AMBVAL *
1045 < avstore(aval)                           /* allocate memory and store aval */
1046 < register AMBVAL  *aval;
1045 > avstore(                                /* allocate memory and save aval */
1046 >        AMBVAL  *aval
1047 > )
1048   {
1049 <        register AMBVAL  *av;
1049 >        AMBVAL  *av;
1050 >        double  d;
1051  
1052          if ((av = newambval()) == NULL)
1053                  error(SYSTEM, "out of memory in avstore");
1054 <        copystruct(av, aval);
1054 >        *av = *aval;
1055 >        av->latick = ambclock;
1056 >        av->next = NULL;
1057 >        nambvals++;
1058 >        d = bright(av->val);
1059 >        if (d > FTINY) {                /* add to log sum for averaging */
1060 >                avsum += log(d);
1061 >                navsum++;
1062 >        }
1063 >        avinsert(av);                   /* insert in our cache tree */
1064          return(av);
1065   }
1066  
1067  
1068 < static
1069 < avinsert(av)                            /* insert ambient value in our tree */
1070 < register AMBVAL  *av;
1068 > #define ATALLOCSZ       512             /* #/8 trees to allocate at once */
1069 >
1070 > static AMBTREE  *atfreelist = NULL;     /* free ambient tree structures */
1071 >
1072 >
1073 > static AMBTREE *
1074 > newambtree(void)                                /* allocate 8 ambient tree structs */
1075   {
1076 <        register AMBTREE  *at;
406 <        register AMBVAL  *ap;
407 <        AMBVAL  avh;
408 <        FVECT  ck0;
409 <        double  s;
410 <        int  branch;
411 <        register int  i;
1076 >        AMBTREE  *atp, *upperlim;
1077  
1078 <        if (av->rad <= FTINY)
1079 <                error(CONSISTENCY, "zero ambient radius in avinsert");
1080 <        at = &atrunk;
1081 <        VCOPY(ck0, thescene.cuorg);
1082 <        s = thescene.cusize;
1083 <        while (s*(OCTSCALE/2) > av->rad*ambacc) {
1084 <                if (at->kid == NULL)
1085 <                        if ((at->kid = newambtree()) == NULL)
1086 <                                error(SYSTEM, "out of memory in avinsert");
422 <                s *= 0.5;
423 <                branch = 0;
424 <                for (i = 0; i < 3; i++)
425 <                        if (av->pos[i] > ck0[i] + s) {
426 <                                ck0[i] += s;
427 <                                branch |= 1 << i;
428 <                        }
429 <                at = at->kid + branch;
1078 >        if (atfreelist == NULL) {       /* get more nodes */
1079 >                atfreelist = (AMBTREE *)malloc(ATALLOCSZ*8*sizeof(AMBTREE));
1080 >                if (atfreelist == NULL)
1081 >                        return(NULL);
1082 >                                        /* link new free list */
1083 >                upperlim = atfreelist + 8*(ATALLOCSZ-1);
1084 >                for (atp = atfreelist; atp < upperlim; atp += 8)
1085 >                        atp->kid = atp + 8;
1086 >                atp->kid = NULL;
1087          }
1088 <        avh.next = at->alist;           /* order by increasing level */
1089 <        for (ap = &avh; ap->next != NULL; ap = ap->next)
1090 <                if (ap->next->lvl >= av->lvl)
1091 <                        break;
435 <        av->next = ap->next;
436 <        ap->next = av;
437 <        at->alist = avh.next;
1088 >        atp = atfreelist;
1089 >        atfreelist = atp->kid;
1090 >        memset(atp, 0, 8*sizeof(AMBTREE));
1091 >        return(atp);
1092   }
1093  
1094  
1095 < static
1096 < loadatree(at)                           /* move tree to main store */
1097 < register AMBTREE  *at;
1095 > static void
1096 > freeambtree(                    /* free 8 ambient tree structs */
1097 >        AMBTREE  *atp
1098 > )
1099   {
1100 <        register AMBVAL  *av;
1101 <        register int  i;
1100 >        atp->kid = atfreelist;
1101 >        atfreelist = atp;
1102 > }
1103 >
1104 >
1105 > static void
1106 > unloadatree(                    /* unload an ambient value tree */
1107 >        AMBTREE  *at,
1108 >        unloadtf_t *f
1109 > )
1110 > {
1111 >        AMBVAL  *av;
1112 >        int  i;
1113                                          /* transfer values at this node */
1114          for (av = at->alist; av != NULL; av = at->alist) {
1115                  at->alist = av->next;
1116 <                avinsert(av);
1116 >                av->next = NULL;
1117 >                (*f)(av);
1118          }
1119          if (at->kid == NULL)
1120                  return;
1121          for (i = 0; i < 8; i++)         /* transfer and free children */
1122 <                loadatree(at->kid+i);
1122 >                unloadatree(at->kid+i, f);
1123          freeambtree(at->kid);
1124 +        at->kid = NULL;
1125   }
1126  
1127  
1128 + static struct avl {
1129 +        AMBVAL  *p;
1130 +        unsigned long   t;
1131 + }       *avlist1;                       /* ambient value list with ticks */
1132 + static AMBVAL   **avlist2;              /* memory positions for sorting */
1133 + static int      i_avlist;               /* index for lists */
1134 +
1135 + static int alatcmp(const void *av1, const void *av2);
1136 +
1137 + static void
1138 + avfree(AMBVAL *av)
1139 + {
1140 +        free(av);
1141 + }
1142 +
1143 + static void
1144 + av2list(
1145 +        AMBVAL *av
1146 + )
1147 + {
1148 + #ifdef DEBUG
1149 +        if (i_avlist >= nambvals)
1150 +                error(CONSISTENCY, "too many ambient values in av2list1");
1151 + #endif
1152 +        avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av;
1153 +        avlist1[i_avlist++].t = av->latick;
1154 + }
1155 +
1156 +
1157 + static int
1158 + alatcmp(                        /* compare ambient values for MRA */
1159 +        const void *av1,
1160 +        const void *av2
1161 + )
1162 + {
1163 +        long  lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t;
1164 +        return(lc<0 ? -1 : lc>0 ? 1 : 0);
1165 + }
1166 +
1167 +
1168 + /* GW NOTE 2002/10/3:
1169 + * I used to compare AMBVAL pointers, but found that this was the
1170 + * cause of a serious consistency error with gcc, since the optimizer
1171 + * uses some dangerous trick in pointer subtraction that
1172 + * assumes pointers differ by exact struct size increments.
1173 + */
1174 + static int
1175 + aposcmp(                        /* compare ambient value positions */
1176 +        const void      *avp1,
1177 +        const void      *avp2
1178 + )
1179 + {
1180 +        long    diff = *(char * const *)avp1 - *(char * const *)avp2;
1181 +        if (diff < 0)
1182 +                return(-1);
1183 +        return(diff > 0);
1184 + }
1185 +
1186 +
1187 + static int
1188 + avlmemi(                                /* find list position from address */
1189 +        AMBVAL  *avaddr
1190 + )
1191 + {
1192 +        AMBVAL  **avlpp;
1193 +
1194 +        avlpp = (AMBVAL **)bsearch(&avaddr, avlist2,
1195 +                        nambvals, sizeof(AMBVAL *), aposcmp);
1196 +        if (avlpp == NULL)
1197 +                error(CONSISTENCY, "address not found in avlmemi");
1198 +        return(avlpp - avlist2);
1199 + }
1200 +
1201 +
1202 + static void
1203 + sortambvals(                    /* resort ambient values */
1204 +        int     always
1205 + )
1206 + {
1207 +        AMBTREE  oldatrunk;
1208 +        AMBVAL  tav, *tap, *pnext;
1209 +        int     i, j;
1210 +                                        /* see if it's time yet */
1211 +        if (!always && (ambclock++ < lastsort+sortintvl ||
1212 +                        nambvals < SORT_THRESH))
1213 +                return;
1214 +        /*
1215 +         * The idea here is to minimize memory thrashing
1216 +         * in VM systems by improving reference locality.
1217 +         * We do this by periodically sorting our stored ambient
1218 +         * values in memory in order of most recently to least
1219 +         * recently accessed.  This ordering was chosen so that new
1220 +         * ambient values (which tend to be less important) go into
1221 +         * higher memory with the infrequently accessed values.
1222 +         *      Since we expect our values to need sorting less
1223 +         * frequently as the process continues, we double our
1224 +         * waiting interval after each call.
1225 +         *      This routine is also called by setambacc() with
1226 +         * the "always" parameter set to 1 so that the ambient
1227 +         * tree will be rebuilt with the new accuracy parameter.
1228 +         */
1229 +        if (tracktime) {                /* allocate pointer arrays to sort */
1230 +                avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
1231 +                avlist1 = (struct avl *)malloc(nambvals*sizeof(struct avl));
1232 +        } else {
1233 +                avlist2 = NULL;
1234 +                avlist1 = NULL;
1235 +        }
1236 +        if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */
1237 +                if (avlist2 != NULL)
1238 +                        free(avlist2);
1239 +                if (always) {           /* rebuild without sorting */
1240 +                        oldatrunk = atrunk;
1241 +                        atrunk.alist = NULL;
1242 +                        atrunk.kid = NULL;
1243 +                        unloadatree(&oldatrunk, avinsert);
1244 +                }
1245 +        } else {                        /* sort memory by last access time */
1246 +                /*
1247 +                 * Sorting memory is tricky because it isn't contiguous.
1248 +                 * We have to sort an array of pointers by MRA and also
1249 +                 * by memory position.  We then copy values in "loops"
1250 +                 * to minimize memory hits.  Nevertheless, we will visit
1251 +                 * everyone at least twice, and this is an expensive process
1252 +                 * when we're thrashing, which is when we need to do it.
1253 +                 */
1254 + #ifdef DEBUG
1255 +                sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...",
1256 +                                nambvals, ambclock);
1257 +                eputs(errmsg);
1258 + #endif
1259 +                i_avlist = 0;
1260 +                unloadatree(&atrunk, av2list);  /* empty current tree */
1261 + #ifdef DEBUG
1262 +                if (i_avlist < nambvals)
1263 +                        error(CONSISTENCY, "missing ambient values in sortambvals");
1264 + #endif
1265 +                qsort(avlist1, nambvals, sizeof(struct avl), alatcmp);
1266 +                qsort(avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
1267 +                for (i = 0; i < nambvals; i++) {
1268 +                        if (avlist1[i].p == NULL)
1269 +                                continue;
1270 +                        tap = avlist2[i];
1271 +                        tav = *tap;
1272 +                        for (j = i; (pnext = avlist1[j].p) != tap;
1273 +                                        j = avlmemi(pnext)) {
1274 +                                *(avlist2[j]) = *pnext;
1275 +                                avinsert(avlist2[j]);
1276 +                                avlist1[j].p = NULL;
1277 +                        }
1278 +                        *(avlist2[j]) = tav;
1279 +                        avinsert(avlist2[j]);
1280 +                        avlist1[j].p = NULL;
1281 +                }
1282 +                free(avlist1);
1283 +                free(avlist2);
1284 +                                                /* compute new sort interval */
1285 +                sortintvl = ambclock - lastsort;
1286 +                if (sortintvl >= MAX_SORT_INTVL/2)
1287 +                        sortintvl = MAX_SORT_INTVL;
1288 +                else
1289 +                        sortintvl <<= 1;        /* wait twice as long next */
1290 + #ifdef DEBUG
1291 +                eputs("done\n");
1292 + #endif
1293 +        }
1294 +        if (ambclock >= MAXACLOCK)
1295 +                ambclock = MAXACLOCK/2;
1296 +        lastsort = ambclock;
1297 + }
1298 +
1299 +
1300   #ifdef  F_SETLKW
1301  
1302 < static
1303 < aflock(typ)                     /* lock/unlock ambient file */
1304 < int  typ;
1302 > static void
1303 > aflock(                 /* lock/unlock ambient file */
1304 >        int  typ
1305 > )
1306   {
1307          static struct flock  fls;       /* static so initialized to zeroes */
1308  
1309 +        if (typ == fls.l_type)          /* already called? */
1310 +                return;
1311          fls.l_type = typ;
1312          if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
1313                  error(SYSTEM, "cannot (un)lock ambient file");
# Line 472 | Line 1315 | int  typ;
1315  
1316  
1317   int
1318 < ambsync()                       /* synchronize ambient file */
1318 > ambsync(void)                   /* synchronize ambient file */
1319   {
477        static FILE  *ambinp = NULL;
478        static long  lastpos = -1;
1320          long  flen;
1321          AMBVAL  avs;
1322 <        register int  n;
1322 >        int  n;
1323  
1324 <        if (nunflshed == 0)
1324 >        if (ambfp == NULL)      /* no ambient file? */
1325                  return(0);
1326 <        if (lastpos < 0)        /* initializing (locked in initambfile) */
1327 <                goto syncend;
487 <                                /* gain exclusive access */
488 <        aflock(F_WRLCK);
1326 >                                /* gain appropriate access */
1327 >        aflock(nunflshed ? F_WRLCK : F_RDLCK);
1328                                  /* see if file has grown */
1329 <        if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0)
1329 >        if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0)
1330                  goto seekerr;
1331 <        if (n = flen - lastpos) {               /* file has grown */
1332 <                if (ambinp == NULL) {           /* use duplicate filedes */
1333 <                        ambinp = fdopen(dup(fileno(ambfp)), "r");
1331 >        if ((n = flen - lastpos) > 0) {         /* file has grown */
1332 >                if (ambinp == NULL) {           /* get new file pointer */
1333 >                        ambinp = fopen(ambfile, "rb");
1334                          if (ambinp == NULL)
1335 <                                error(SYSTEM, "fdopen failed in ambsync");
1335 >                                error(SYSTEM, "fopen failed in ambsync");
1336                  }
1337 <                if (fseek(ambinp, lastpos, 0) < 0)
1337 >                if (fseek(ambinp, lastpos, SEEK_SET) < 0)
1338                          goto seekerr;
1339                  while (n >= AMBVALSIZ) {        /* load contributed values */
1340 <                        readambval(&avs, ambinp);
1341 <                        avinsert(avstore(&avs));
1340 >                        if (!readambval(&avs, ambinp)) {
1341 >                                sprintf(errmsg,
1342 >                        "ambient file \"%s\" corrupted near character %ld",
1343 >                                                ambfile, flen - n);
1344 >                                error(WARNING, errmsg);
1345 >                                break;
1346 >                        }
1347 >                        avstore(&avs);
1348                          n -= AMBVALSIZ;
1349                  }
1350 <                /*** seek always as safety measure
1351 <                if (n) ***/                     /* alignment */
1352 <                        if (lseek(fileno(ambfp), flen-n, 0) < 0)
508 <                                goto seekerr;
1350 >                lastpos = flen - n;             /* check alignment */
1351 >                if (n && lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
1352 >                        goto seekerr;
1353          }
510 #ifdef  DEBUG
511        if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) {
512                sprintf(errmsg, "ambient file buffer at %d rather than %d",
513                                ambfp->_ptr - ambfp->_base,
514                                nunflshed*AMBVALSIZ);
515                error(CONSISTENCY, errmsg);
516        }
517 #endif
518 syncend:
1354          n = fflush(ambfp);                      /* calls write() at last */
1355 <        if ((lastpos = lseek(fileno(ambfp), 0L, 1)) < 0)
521 <                goto seekerr;
1355 >        lastpos += (long)nunflshed*AMBVALSIZ;
1356          aflock(F_UNLCK);                        /* release file */
1357          nunflshed = 0;
1358          return(n);
1359   seekerr:
1360          error(SYSTEM, "seek failed in ambsync");
1361 +        return(EOF);    /* pro forma return */
1362   }
1363  
1364 < #else
1364 > #else   /* ! F_SETLKW */
1365  
1366   int
1367 < ambsync()                       /* flush ambient file */
1367 > ambsync(void)                   /* flush ambient file */
1368   {
1369 <        if (nunflshed == 0)
1369 >        if (ambfp == NULL)
1370                  return(0);
1371          nunflshed = 0;
1372          return(fflush(ambfp));
1373   }
1374  
1375 < #endif
1375 > #endif  /* ! F_SETLKW */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines