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.15 by greg, Wed Jan 20 10:48:23 1993 UTC vs.
Revision 2.68 by greg, Thu Oct 14 05:54:44 2010 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1993 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  ambient.c - routines dealing with ambient (inter-reflected) component.
6 + *
7 + *  Declarations of external symbols in ambient.h
8   */
9  
10 < #include  "ray.h"
10 > #include "copyright.h"
11  
12 < #include  "octree.h"
12 > #include <string.h>
13  
14 + #include  "platform.h"
15 + #include  "ray.h"
16   #include  "otypes.h"
17 <
17 > #include  "resolu.h"
18   #include  "ambient.h"
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 {
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 35 | Line 34 | double minarad;                /* minimum ambient radius */
34  
35   static AMBTREE  atrunk;         /* our ambient trunk node */
36  
38 static char  *ambfname = NULL;  /* ambient file name */
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     ((3L<<20)/sizeof(AMBVAL))
43 + #else
44 + #define SORT_THRESH     ((9L<<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 + static double  avsum = 0.;              /* computed ambient value sum (log) */
55 + static unsigned int  navsum = 0;        /* number of values in avsum */
56 + static unsigned int  nambvals = 0;      /* total number of indirect values */
57 + static unsigned int  nambshare = 0;     /* number of values from file */
58 + static unsigned long  ambclock = 0;     /* ambient access clock */
59 + static unsigned long  lastsort = 0;     /* time of last value sort */
60 + static long  sortintvl = SORT_INTVL;    /* time until next sort */
61 + static FILE  *ambinp = NULL;            /* auxiliary file for input */
62 + static long  lastpos = -1;              /* last flush position */
63 +
64 + #define MAXACLOCK       (1L<<30)        /* clock turnover value */
65 +        /*
66 +         * Track access times unless we are sharing ambient values
67 +         * through memory on a multiprocessor, when we want to avoid
68 +         * claiming our own memory (copy on write).  Go ahead anyway
69 +         * if more than two thirds of our values are unshared.
70 +         * Compile with -Dtracktime=0 to turn this code off.
71 +         */
72 + #ifndef tracktime
73 + #define tracktime       (shm_boundary == NULL || nambvals > 3*nambshare)
74 + #endif
75 +
76   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
77  
78 < #define  newambval()    (AMBVAL *)bmalloc(sizeof(AMBVAL))
78 > #define  newambval()    (AMBVAL *)malloc(sizeof(AMBVAL))
79 > #define  freeav(av)     free((void *)av);
80  
81 < #define  newambtree()   (AMBTREE *)calloc(8, sizeof(AMBTREE))
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(), ambsync();
87 > typedef void unloadtf_t(void *);
88 > static unloadtf_t avinsert;
89 > static unloadtf_t av2list;
90 > static void unloadatree(AMBTREE  *at, unloadtf_t *f);
91  
92 + static int aposcmp(const void *avp1, const void *avp2);
93 + static int avlmemi(AMBVAL *avaddr);
94 + static void sortambvals(int always);
95  
96 < setambres(ar)                           /* set ambient resolution */
97 < int  ar;
96 > #ifdef  F_SETLKW
97 > static void aflock(int  typ);
98 > #endif
99 >
100 >
101 > extern void
102 > setambres(                              /* set ambient resolution */
103 >        int  ar
104 > )
105   {
106 +        ambres = ar < 0 ? 0 : ar;               /* may be done already */
107                                                  /* set min & max radii */
108          if (ar <= 0) {
109 <                minarad = 0.0;
109 >                minarad = 0;
110                  maxarad = thescene.cusize / 2.0;
111          } else {
112                  minarad = thescene.cusize / ar;
113 <                maxarad = 16.0 * minarad;               /* heuristic */
113 >                maxarad = 64 * minarad;                 /* heuristic */
114                  if (maxarad > thescene.cusize / 2.0)
115                          maxarad = thescene.cusize / 2.0;
116          }
117 <        if (maxarad <= FTINY)
118 <                maxarad = .001;
117 >        if (minarad <= FTINY)
118 >                minarad = 10*FTINY;
119 >        if (maxarad <= minarad)
120 >                maxarad = 64 * minarad;
121   }
122  
123  
124 < setambient(afile)                       /* initialize calculation */
125 < char  *afile;
124 > extern void
125 > setambacc(                              /* set ambient accuracy */
126 >        double  newa
127 > )
128   {
129 <        long  headlen;
129 >        double  ambdiff;
130 >
131 >        if (newa < 0.0)
132 >                newa = 0.0;
133 >        ambdiff = fabs(newa - ambacc);
134 >        if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0)
135 >                sortambvals(1);                 /* rebuild tree */
136 > }
137 >
138 >
139 > extern void
140 > setambient(void)                                /* initialize calculation */
141 > {
142 >        int     readonly = 0;
143 >        long    flen;
144          AMBVAL  amb;
145 +                                                /* make sure we're fresh */
146 +        ambdone();
147                                                  /* init ambient limits */
148          setambres(ambres);
149 +        setambacc(ambacc);
150 +        if (ambfile == NULL || !ambfile[0])
151 +                return;
152 +        if (ambacc <= FTINY) {
153 +                sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened",
154 +                                ambfile);
155 +                error(WARNING, errmsg);
156 +                return;
157 +        }
158                                                  /* open ambient file */
159 <        if ((ambfname = afile) != NULL)
160 <                if ((ambfp = fopen(afile, "r+")) != NULL) {
161 <                        initambfile(0);
162 <                        headlen = ftell(ambfp);
163 <                        while (readambval(&amb, ambfp))
164 <                                avinsert(&amb, &atrunk, thescene.cuorg,
165 <                                                thescene.cusize);
166 <                                                        /* align */
167 <                        fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
168 <                } else if ((ambfp = fopen(afile, "w+")) != NULL)
169 <                        initambfile(1);
170 <                else {
171 <                        sprintf(errmsg, "cannot open ambient file \"%s\"",
172 <                                        afile);
173 <                        error(SYSTEM, errmsg);
159 >        if ((ambfp = fopen(ambfile, "r+")) == NULL)
160 >                readonly = (ambfp = fopen(ambfile, "r")) != NULL;
161 >        if (ambfp != NULL) {
162 >                initambfile(0);                 /* file exists */
163 >                lastpos = ftell(ambfp);
164 >                while (readambval(&amb, ambfp))
165 >                        avinsert(avstore(&amb));
166 >                nambshare = nambvals;           /* share loaded values */
167 >                if (readonly) {
168 >                        sprintf(errmsg,
169 >                                "loaded %u values from read-only ambient file",
170 >                                        nambvals);
171 >                        error(WARNING, errmsg);
172 >                        fclose(ambfp);          /* close file so no writes */
173 >                        ambfp = NULL;
174 >                        return;                 /* avoid ambsync() */
175                  }
176 +                                                /* align file pointer */
177 +                lastpos += (long)nambvals*AMBVALSIZ;
178 +                flen = lseek(fileno(ambfp), (off_t)0, SEEK_END);
179 +                if (flen != lastpos) {
180 +                        sprintf(errmsg,
181 +                        "ignoring last %ld values in ambient file (corrupted)",
182 +                                        (flen - lastpos)/AMBVALSIZ);
183 +                        error(WARNING, errmsg);
184 +                        fseek(ambfp, lastpos, SEEK_SET);
185 + #ifndef _WIN32 /* XXX we need a replacement for that one */
186 +                        ftruncate(fileno(ambfp), (off_t)lastpos);
187 + #endif
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 + #ifdef  F_SETLKW
198 +        aflock(F_UNLCK);                        /* release file */
199 + #endif
200   }
201  
202  
203 < ambnotify(obj)                  /* record new modifier */
204 < OBJECT  obj;
203 > extern 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, free);
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 + extern void
230 + ambnotify(                      /* record new modifier */
231 +        OBJECT  obj
232 + )
233 + {
234          static int  hitlimit = 0;
235 <        register OBJREC  *o = objptr(obj);
235 >        register OBJREC  *o;
236          register 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 115 | Line 256 | OBJECT obj;
256   }
257  
258  
259 < ambient(acol, r)                /* compute ambient component for ray */
260 < COLOR  acol;
261 < register RAY  *r;
259 > extern void
260 > multambient(            /* compute ambient component & multiply by coef. */
261 >        COLOR  aval,
262 >        register RAY  *r,
263 >        FVECT  nrm
264 > )
265   {
266          static int  rdepth = 0;                 /* ambient recursion */
267 <        double  d;
267 >        COLOR   acol;
268 >        double  d, l;
269  
270          if (ambdiv <= 0)                        /* no ambient calculation */
271                  goto dumbamb;
# Line 133 | Line 278 | register RAY  *r;
278                  goto dumbamb;
279  
280          if (ambacc <= FTINY) {                  /* no ambient storage */
281 +                copycolor(acol, aval);
282                  rdepth++;
283                  d = doambient(acol, r, r->rweight, NULL, NULL);
284                  rdepth--;
285 <                if (d == 0.0)
285 >                if (d <= FTINY)
286                          goto dumbamb;
287 +                copycolor(aval, acol);
288                  return;
289          }
290 <                                                /* get ambient value */
290 >
291 >        if (tracktime)                          /* sort to minimize thrashing */
292 >                sortambvals(0);
293 >                                                /* interpolate ambient value */
294          setcolor(acol, 0.0, 0.0, 0.0);
295 <        d = sumambient(acol, r, rdepth,
295 >        d = sumambient(acol, r, nrm, rdepth,
296                          &atrunk, thescene.cuorg, thescene.cusize);
297 <        if (d > FTINY)
298 <                scalecolor(acol, 1.0/d);
299 <        else {
300 <                d = makeambient(acol, r, rdepth++);
301 <                rdepth--;
297 >        if (d > FTINY) {
298 >                d = 1.0/d;
299 >                scalecolor(acol, d);
300 >                multcolor(aval, acol);
301 >                return;
302          }
303 <        if (d > FTINY)
303 >        rdepth++;                               /* need to cache new value */
304 >        d = makeambient(acol, r, nrm, rdepth-1);
305 >        rdepth--;
306 >        if (d > FTINY) {
307 >                multcolor(aval, acol);          /* got new value */
308                  return;
309 +        }
310   dumbamb:                                        /* return global value */
311 <        copycolor(acol, ambval);
311 >        if ((ambvwt <= 0) | (navsum == 0)) {
312 >                multcolor(aval, ambval);
313 >                return;
314 >        }
315 >        l = bright(ambval);                     /* average in computations */
316 >        if (l > FTINY) {
317 >                d = (log(l)*(double)ambvwt + avsum) /
318 >                                (double)(ambvwt + navsum);
319 >                d = exp(d) / l;
320 >                scalecolor(aval, d);
321 >                multcolor(aval, ambval);        /* apply color of ambval */
322 >        } else {
323 >                d = exp( avsum / (double)navsum );
324 >                scalecolor(aval, d);            /* neutral color */
325 >        }
326   }
327  
328  
329 < double
330 < sumambient(acol, r, al, at, c0, s)      /* get interpolated ambient value */
331 < COLOR  acol;
332 < register RAY  *r;
333 < int  al;
334 < AMBTREE  *at;
335 < FVECT  c0;
336 < double  s;
329 > extern double
330 > sumambient(     /* get interpolated ambient value */
331 >        COLOR  acol,
332 >        register RAY  *r,
333 >        FVECT  rn,
334 >        int  al,
335 >        AMBTREE  *at,
336 >        FVECT  c0,
337 >        double  s
338 > )
339   {
340          double  d, e1, e2, wt, wsum;
341          COLOR  ct;
# Line 172 | Line 343 | double s;
343          int  i;
344          register int  j;
345          register AMBVAL  *av;
346 <                                        /* do this node */
346 >
347          wsum = 0.0;
348 +                                        /* do this node */
349          for (av = at->alist; av != NULL; av = av->next) {
350 +                double  rn_dot = -2.0;
351 +                if (tracktime)
352 +                        av->latick = ambclock;
353                  /*
354                   *  Ambient level test.
355                   */
356 <                if (av->lvl > al || av->weight < r->rweight-FTINY)
356 >                if (av->lvl > al)       /* list sorted, so this works */
357 >                        break;
358 >                if (av->weight < 0.9*r->rweight)
359                          continue;
360                  /*
361                   *  Ambient radius test.
362                   */
363 <                e1 = 0.0;
364 <                for (j = 0; j < 3; j++) {
365 <                        d = av->pos[j] - r->rop[j];
366 <                        e1 += d * d;
367 <                }
363 >                d = av->pos[0] - r->rop[0];
364 >                e1 = d * d;
365 >                d = av->pos[1] - r->rop[1];
366 >                e1 += d * d;
367 >                d = av->pos[2] - r->rop[2];
368 >                e1 += d * d;
369                  e1 /= av->rad * av->rad;
370                  if (e1 > ambacc*ambacc*1.21)
371                          continue;
372                  /*
373 <                 *  Normal direction test.
373 >                 *  Direction test using closest normal.
374                   */
375 <                e2 = (1.0 - DOT(av->dir, r->ron)) * r->rweight;
375 >                d = DOT(av->dir, r->ron);
376 >                if (rn != r->ron) {
377 >                        rn_dot = DOT(av->dir, rn);
378 >                        if (rn_dot > 1.0-FTINY)
379 >                                rn_dot = 1.0-FTINY;
380 >                        if (rn_dot >= d-FTINY) {
381 >                                d = rn_dot;
382 >                                rn_dot = -2.0;
383 >                        }
384 >                }
385 >                e2 = (1.0 - d) * r->rweight;
386                  if (e2 < 0.0) e2 = 0.0;
387                  if (e1 + e2 > ambacc*ambacc*1.21)
388                          continue;
# Line 210 | Line 398 | double s;
398                  /*
399                   *  Jittering final test reduces image artifacts.
400                   */
401 <                wt = sqrt(e1) + sqrt(e2);
402 <                wt *= .9 + .2*urand(9015+samplendx);
403 <                if (wt > ambacc)
401 >                e1 = sqrt(e1);
402 >                e2 = sqrt(e2);
403 >                wt = e1 + e2;
404 >                if (wt > ambacc*(.9+.2*urand(9015+samplendx)))
405                          continue;
406 +                /*
407 +                 *  Recompute directional error using perturbed normal
408 +                 */
409 +                if (rn_dot > 0.0) {
410 +                        e2 = sqrt((1.0 - rn_dot)*r->rweight);
411 +                        wt = e1 + e2;
412 +                }
413                  if (wt <= 1e-3)
414                          wt = 1e3;
415                  else
416                          wt = 1.0 / wt;
417                  wsum += wt;
418 <                extambient(ct, av, r->rop, r->ron);
418 >                extambient(ct, av, r->rop, rn);
419                  scalecolor(ct, wt);
420                  addcolor(acol, ct);
421          }
# Line 238 | Line 434 | double s;
434                                  break;
435                  }
436                  if (j == 3)
437 <                        wsum += sumambient(acol, r, al, at->kid+i, ck0, s);
437 >                        wsum += sumambient(acol, r, rn, al,
438 >                                                at->kid+i, ck0, s);
439          }
440          return(wsum);
441   }
442  
443  
444 < double
445 < makeambient(acol, r, al)        /* make a new ambient value */
446 < COLOR  acol;
447 < register RAY  *r;
448 < int  al;
444 > extern double
445 > makeambient(            /* make a new ambient value for storage */
446 >        COLOR  acol,
447 >        RAY  *r,
448 >        FVECT  rn,
449 >        int  al
450 > )
451   {
452          AMBVAL  amb;
453          FVECT   gp, gd;
454 <                                                /* compute weight */
455 <        amb.weight = pow(AVGREFL, (double)al);
456 <        if (r->rweight < 0.2*amb.weight)        /* heuristic */
457 <                amb.weight = r->rweight;
454 >        int     i;
455 >
456 >        amb.weight = 1.0;                       /* compute weight */
457 >        for (i = al; i-- > 0; )
458 >                amb.weight *= AVGREFL;
459 >        if (r->rweight < 0.1*amb.weight)        /* heuristic override */
460 >                amb.weight = 1.25*r->rweight;
461 >        setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
462                                                  /* compute ambient */
463          amb.rad = doambient(acol, r, amb.weight, gp, gd);
464 <        if (amb.rad == 0.0)
464 >        if (amb.rad <= FTINY) {
465 >                setcolor(acol, 0.0, 0.0, 0.0);
466                  return(0.0);
467 <                                                /* store it */
467 >        }
468 >        scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
469 >                                                /* store value */
470          VCOPY(amb.pos, r->rop);
471          VCOPY(amb.dir, r->ron);
472          amb.lvl = al;
# Line 269 | Line 475 | int  al;
475          VCOPY(amb.gdir, gd);
476                                                  /* insert into tree */
477          avsave(&amb);                           /* and save to file */
478 +        if (rn != r->ron)
479 +                extambient(acol, &amb, r->rop, rn);     /* texture */
480          return(amb.rad);
481   }
482  
483  
484 < extambient(cr, ap, pv, nv)              /* extrapolate value at pv, nv */
485 < COLOR  cr;
486 < register AMBVAL  *ap;
487 < FVECT  pv, nv;
484 > extern void
485 > extambient(             /* extrapolate value at pv, nv */
486 >        COLOR  cr,
487 >        register AMBVAL  *ap,
488 >        FVECT  pv,
489 >        FVECT  nv
490 > )
491   {
492 <        FVECT  v1, v2;
492 >        FVECT  v1;
493          register int  i;
494          double  d;
495  
# Line 287 | Line 498 | FVECT  pv, nv;
498          for (i = 0; i < 3; i++)
499                  d += ap->gpos[i]*(pv[i]-ap->pos[i]);
500                                          /* gradient due to rotation */
501 <        VCOPY(v1, ap->dir);
502 <        fcross(v2, v1, nv);
292 <        d += DOT(ap->gdir, v2);
501 >        VCROSS(v1, ap->dir, nv);
502 >        d += DOT(ap->gdir, v1);
503          if (d <= 0.0) {
504                  setcolor(cr, 0.0, 0.0, 0.0);
505                  return;
# Line 299 | Line 509 | FVECT  pv, nv;
509   }
510  
511  
512 < static
513 < initambfile(creat)              /* initialize ambient file */
514 < int  creat;
512 > static void
513 > initambfile(            /* initialize ambient file */
514 >        int  cre8
515 > )
516   {
517 <        extern char  *progname, *octname, VersionID[];
517 >        extern char  *progname, *octname;
518 >        static char  *mybuf = NULL;
519  
520 < #ifdef MSDOS
521 <        setmode(fileno(ambfp), O_BINARY);
520 > #ifdef  F_SETLKW
521 >        aflock(creat ? F_WRLCK : F_RDLCK);
522   #endif
523 <        setbuf(ambfp, bmalloc(BUFSIZ));
524 <        if (creat) {                    /* new file */
525 <                fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ",
523 >        SET_FILE_BINARY(ambfp);
524 >        if (mybuf == NULL)
525 >                mybuf = (char *)bmalloc(BUFSIZ+8);
526 >        setbuf(ambfp, mybuf);
527 >        if (cre8) {                     /* new file */
528 >                newheader("RADIANCE", ambfp);
529 >                fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ",
530                                  progname, colval(ambval,RED),
531                                  colval(ambval,GRN), colval(ambval,BLU),
532 <                                ambounce, ambacc);
533 <                fprintf(ambfp, "-ad %d -as %d -ar %d %s\n",
534 <                                ambdiv, ambssamp, ambres,
535 <                                octname==NULL ? "" : octname);
532 >                                ambvwt, ambounce, ambacc);
533 >                fprintf(ambfp, "-ad %d -as %d -ar %d ",
534 >                                ambdiv, ambssamp, ambres);
535 >                if (octname != NULL)
536 >                        fputs(octname, ambfp);
537 >                fputc('\n', ambfp);
538                  fprintf(ambfp, "SOFTWARE= %s\n", VersionID);
539 +                fputnow(ambfp);
540                  fputformat(AMBFMT, ambfp);
541 <                putc('\n', ambfp);
541 >                fputc('\n', ambfp);
542                  putambmagic(ambfp);
543          } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp))
544                  error(USER, "bad ambient file");
326        ambsync();
545   }
546  
547  
548 < static
549 < avsave(av)                              /* insert and save an ambient value */
550 < AMBVAL  *av;
548 > static void
549 > avsave(                         /* insert and save an ambient value */
550 >        AMBVAL  *av
551 > )
552   {
553 <        static int  nunflshed = 0;
335 <
336 <        avinsert(av, &atrunk, thescene.cuorg, thescene.cusize);
553 >        avinsert(avstore(av));
554          if (ambfp == NULL)
555                  return;
556          if (writambval(av, ambfp) < 0)
557                  goto writerr;
558 <        if (++nunflshed >= AMBFLUSH) {
558 >        if (++nunflshed >= AMBFLUSH)
559                  if (ambsync() == EOF)
560                          goto writerr;
344                nunflshed = 0;
345        }
561          return;
562   writerr:
563 <        error(SYSTEM, "error writing ambient file");
563 >        error(SYSTEM, "error writing to ambient file");
564   }
565  
566  
567 < static
568 < avinsert(aval, at, c0, s)               /* insert ambient value in a tree */
569 < AMBVAL  *aval;
570 < register AMBTREE  *at;
356 < FVECT  c0;
357 < double  s;
567 > static AMBVAL *
568 > avstore(                                /* allocate memory and store aval */
569 >        register AMBVAL  *aval
570 > )
571   {
572 +        register AMBVAL  *av;
573 +        double  d;
574 +
575 +        if ((av = newambval()) == NULL)
576 +                error(SYSTEM, "out of memory in avstore");
577 +        *av = *aval;
578 +        av->latick = ambclock;
579 +        av->next = NULL;
580 +        nambvals++;
581 +        d = bright(av->val);
582 +        if (d > FTINY) {                /* add to log sum for averaging */
583 +                avsum += log(d);
584 +                navsum++;
585 +        }
586 +        return(av);
587 + }
588 +
589 +
590 + #define ATALLOCSZ       512             /* #/8 trees to allocate at once */
591 +
592 + static AMBTREE  *atfreelist = NULL;     /* free ambient tree structures */
593 +
594 +
595 + static AMBTREE *
596 + newambtree(void)                                /* allocate 8 ambient tree structs */
597 + {
598 +        register AMBTREE  *atp, *upperlim;
599 +
600 +        if (atfreelist == NULL) {       /* get more nodes */
601 +                atfreelist = (AMBTREE *)malloc(ATALLOCSZ*8*sizeof(AMBTREE));
602 +                if (atfreelist == NULL)
603 +                        return(NULL);
604 +                                        /* link new free list */
605 +                upperlim = atfreelist + 8*(ATALLOCSZ-1);
606 +                for (atp = atfreelist; atp < upperlim; atp += 8)
607 +                        atp->kid = atp + 8;
608 +                atp->kid = NULL;
609 +        }
610 +        atp = atfreelist;
611 +        atfreelist = atp->kid;
612 +        memset((char *)atp, '\0', 8*sizeof(AMBTREE));
613 +        return(atp);
614 + }
615 +
616 +
617 + static void
618 + freeambtree(                    /* free 8 ambient tree structs */
619 +        AMBTREE  *atp
620 + )
621 + {
622 +        atp->kid = atfreelist;
623 +        atfreelist = atp;
624 + }
625 +
626 +
627 + static void
628 + avinsert(                               /* insert ambient value in our tree */
629 +        void *av
630 + )
631 + {
632 +        register AMBTREE  *at;
633 +        register AMBVAL  *ap;
634 +        AMBVAL  avh;
635          FVECT  ck0;
636 +        double  s;
637          int  branch;
361        register AMBVAL  *av;
638          register int  i;
639  
640 <        if ((av = newambval()) == NULL)
641 <                goto memerr;
642 <        copystruct(av, aval);
643 <        VCOPY(ck0, c0);
644 <        while (s*(OCTSCALE/2) > av->rad*ambacc) {
640 >        if (((AMBVAL*)av)->rad <= FTINY)
641 >                error(CONSISTENCY, "zero ambient radius in avinsert");
642 >        at = &atrunk;
643 >        VCOPY(ck0, thescene.cuorg);
644 >        s = thescene.cusize;
645 >        while (s*(OCTSCALE/2) > ((AMBVAL*)av)->rad*ambacc) {
646                  if (at->kid == NULL)
647                          if ((at->kid = newambtree()) == NULL)
648 <                                goto memerr;
648 >                                error(SYSTEM, "out of memory in avinsert");
649                  s *= 0.5;
650                  branch = 0;
651                  for (i = 0; i < 3; i++)
652 <                        if (av->pos[i] > ck0[i] + s) {
652 >                        if (((AMBVAL*)av)->pos[i] > ck0[i] + s) {
653                                  ck0[i] += s;
654                                  branch |= 1 << i;
655                          }
656                  at = at->kid + branch;
657          }
658 <        av->next = at->alist;
659 <        at->alist = av;
660 <        return;
661 < memerr:
662 <        error(SYSTEM, "out of memory in avinsert");
658 >        avh.next = at->alist;           /* order by increasing level */
659 >        for (ap = &avh; ap->next != NULL; ap = ap->next)
660 >                if (ap->next->lvl >= ((AMBVAL*)av)->lvl)
661 >                        break;
662 >        ((AMBVAL*)av)->next = ap->next;
663 >        ap->next = (AMBVAL*)av;
664 >        at->alist = avh.next;
665   }
666  
667  
668 < #ifdef  NIX
668 > static void
669 > unloadatree(                    /* unload an ambient value tree */
670 >        register AMBTREE  *at,
671 >        unloadtf_t *f
672 > )
673 > {
674 >        register AMBVAL  *av;
675 >        register int  i;
676 >                                        /* transfer values at this node */
677 >        for (av = at->alist; av != NULL; av = at->alist) {
678 >                at->alist = av->next;
679 >                (*f)(av);
680 >        }
681 >        if (at->kid == NULL)
682 >                return;
683 >        for (i = 0; i < 8; i++)         /* transfer and free children */
684 >                unloadatree(at->kid+i, f);
685 >        freeambtree(at->kid);
686 >        at->kid = NULL;
687 > }
688  
689 < static
690 < ambsync()                       /* flush ambient file */
689 >
690 > static struct avl {
691 >        AMBVAL  *p;
692 >        unsigned long   t;
693 > }       *avlist1;                       /* ambient value list with ticks */
694 > static AMBVAL   **avlist2;              /* memory positions for sorting */
695 > static int      i_avlist;               /* index for lists */
696 >
697 > static int alatcmp(const void *av1, const void *av2);
698 >
699 > static void
700 > av2list(
701 >        void *av
702 > )
703   {
704 <        return(fflush(ambfp));
704 > #ifdef DEBUG
705 >        if (i_avlist >= nambvals)
706 >                error(CONSISTENCY, "too many ambient values in av2list1");
707 > #endif
708 >        avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av;
709 >        avlist1[i_avlist++].t = ((AMBVAL*)av)->latick;
710   }
711  
712 +
713 + static int
714 + alatcmp(                        /* compare ambient values for MRA */
715 +        const void *av1,
716 +        const void *av2
717 + )
718 + {
719 +        register long  lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t;
720 +        return(lc<0 ? -1 : lc>0 ? 1 : 0);
721 + }
722 +
723 +
724 + /* GW NOTE 2002/10/3:
725 + * I used to compare AMBVAL pointers, but found that this was the
726 + * cause of a serious consistency error with gcc, since the optimizer
727 + * uses some dangerous trick in pointer subtraction that
728 + * assumes pointers differ by exact struct size increments.
729 + */
730 + static int
731 + aposcmp(                        /* compare ambient value positions */
732 +        const void      *avp1,
733 +        const void      *avp2
734 + )
735 + {
736 +        register long   diff = *(char * const *)avp1 - *(char * const *)avp2;
737 +        if (diff < 0)
738 +                return(-1);
739 +        return(diff > 0);
740 + }
741 +
742 + #if 1
743 + static int
744 + avlmemi(                                /* find list position from address */
745 +        AMBVAL  *avaddr
746 + )
747 + {
748 +        register AMBVAL  **avlpp;
749 +
750 +        avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
751 +                        nambvals, sizeof(AMBVAL *), aposcmp);
752 +        if (avlpp == NULL)
753 +                error(CONSISTENCY, "address not found in avlmemi");
754 +        return(avlpp - avlist2);
755 + }
756   #else
757 + #define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \
758 +                                nambvals,sizeof(AMBVAL *),aposcmp) - avlist2)
759 + #endif
760  
761 < static
762 < ambsync()                       /* synchronize ambient file */
761 >
762 > static void
763 > sortambvals(                    /* resort ambient values */
764 >        int     always
765 > )
766   {
767 <        static FILE  *ambinp = NULL;
768 <        static long  lastpos = -1;
769 <        struct flock  fls;
767 >        AMBTREE  oldatrunk;
768 >        AMBVAL  tav, *tap, *pnext;
769 >        register int    i, j;
770 >                                        /* see if it's time yet */
771 >        if (!always && (ambclock++ < lastsort+sortintvl ||
772 >                        nambvals < SORT_THRESH))
773 >                return;
774 >        /*
775 >         * The idea here is to minimize memory thrashing
776 >         * in VM systems by improving reference locality.
777 >         * We do this by periodically sorting our stored ambient
778 >         * values in memory in order of most recently to least
779 >         * recently accessed.  This ordering was chosen so that new
780 >         * ambient values (which tend to be less important) go into
781 >         * higher memory with the infrequently accessed values.
782 >         *      Since we expect our values to need sorting less
783 >         * frequently as the process continues, we double our
784 >         * waiting interval after each call.
785 >         *      This routine is also called by setambacc() with
786 >         * the "always" parameter set to 1 so that the ambient
787 >         * tree will be rebuilt with the new accuracy parameter.
788 >         */
789 >        if (tracktime) {                /* allocate pointer arrays to sort */
790 >                avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
791 >                avlist1 = (struct avl *)malloc(nambvals*sizeof(struct avl));
792 >        } else {
793 >                avlist2 = NULL;
794 >                avlist1 = NULL;
795 >        }
796 >        if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */
797 >                if (avlist2 != NULL)
798 >                        free((void *)avlist2);
799 >                if (always) {           /* rebuild without sorting */
800 >                        oldatrunk = atrunk;
801 >                        atrunk.alist = NULL;
802 >                        atrunk.kid = NULL;
803 >                        unloadatree(&oldatrunk, avinsert);
804 >                }
805 >        } else {                        /* sort memory by last access time */
806 >                /*
807 >                 * Sorting memory is tricky because it isn't contiguous.
808 >                 * We have to sort an array of pointers by MRA and also
809 >                 * by memory position.  We then copy values in "loops"
810 >                 * to minimize memory hits.  Nevertheless, we will visit
811 >                 * everyone at least twice, and this is an expensive process
812 >                 * when we're thrashing, which is when we need to do it.
813 >                 */
814 > #ifdef DEBUG
815 >                sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...",
816 >                                nambvals, ambclock);
817 >                eputs(errmsg);
818 > #endif
819 >                i_avlist = 0;
820 >                unloadatree(&atrunk, av2list);  /* empty current tree */
821 > #ifdef DEBUG
822 >                if (i_avlist < nambvals)
823 >                        error(CONSISTENCY, "missing ambient values in sortambvals");
824 > #endif
825 >                qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp);
826 >                qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
827 >                for (i = 0; i < nambvals; i++) {
828 >                        if (avlist1[i].p == NULL)
829 >                                continue;
830 >                        tap = avlist2[i];
831 >                        tav = *tap;
832 >                        for (j = i; (pnext = avlist1[j].p) != tap;
833 >                                        j = avlmemi(pnext)) {
834 >                                *(avlist2[j]) = *pnext;
835 >                                avinsert(avlist2[j]);
836 >                                avlist1[j].p = NULL;
837 >                        }
838 >                        *(avlist2[j]) = tav;
839 >                        avinsert(avlist2[j]);
840 >                        avlist1[j].p = NULL;
841 >                }
842 >                free((void *)avlist1);
843 >                free((void *)avlist2);
844 >                                                /* compute new sort interval */
845 >                sortintvl = ambclock - lastsort;
846 >                if (sortintvl >= MAX_SORT_INTVL/2)
847 >                        sortintvl = MAX_SORT_INTVL;
848 >                else
849 >                        sortintvl <<= 1;        /* wait twice as long next */
850 > #ifdef DEBUG
851 >                eputs("done\n");
852 > #endif
853 >        }
854 >        if (ambclock >= MAXACLOCK)
855 >                ambclock = MAXACLOCK/2;
856 >        lastsort = ambclock;
857 > }
858 >
859 >
860 > #ifdef  F_SETLKW
861 >
862 > static void
863 > aflock(                 /* lock/unlock ambient file */
864 >        int  typ
865 > )
866 > {
867 >        static struct flock  fls;       /* static so initialized to zeroes */
868 >
869 >        if (typ == fls.l_type)          /* already called? */
870 >                return;
871 >        fls.l_type = typ;
872 >        if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
873 >                error(SYSTEM, "cannot (un)lock ambient file");
874 > }
875 >
876 >
877 > extern int
878 > ambsync(void)                   /* synchronize ambient file */
879 > {
880          long  flen;
881          AMBVAL  avs;
882          register int  n;
883 <                                /* gain exclusive access */
884 <        fls.l_type = F_WRLCK;
885 <        fls.l_whence = 0;
886 <        fls.l_start = 0L;
887 <        fls.l_len = 0L;
413 <        if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
414 <                error(SYSTEM, "cannot lock ambient file");
415 <        if (lastpos < 0)        /* initializing */
416 <                goto syncend;
883 >
884 >        if (ambfp == NULL)      /* no ambient file? */
885 >                return(0);
886 >                                /* gain appropriate access */
887 >        aflock(nunflshed ? F_WRLCK : F_RDLCK);
888                                  /* see if file has grown */
889 <        if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0)
890 <                error(SYSTEM, "cannot seek on ambient file");
891 <        if (n = flen - lastpos) {               /* file has grown */
892 <                if (ambinp == NULL) {
893 <                        ambinp = fopen(ambfname, "r");
889 >        if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0)
890 >                goto seekerr;
891 >        if ((n = flen - lastpos) > 0) {         /* file has grown */
892 >                if (ambinp == NULL) {           /* use duplicate filedes */
893 >                        ambinp = fdopen(dup(fileno(ambfp)), "r");
894                          if (ambinp == NULL)
895 <                                error(SYSTEM, "fopen failed in ambsync");
895 >                                error(SYSTEM, "fdopen failed in ambsync");
896                  }
897 <                if (fseek(ambinp, lastpos, 0) < 0)
898 <                        error(SYSTEM, "fseek failed in ambsync");
897 >                if (fseek(ambinp, lastpos, SEEK_SET) < 0)
898 >                        goto seekerr;
899                  while (n >= AMBVALSIZ) {        /* load contributed values */
900 <                        readambval(&avs, ambinp);
901 <                        avinsert(&avs,&atrunk,thescene.cuorg,thescene.cusize);
900 >                        if (!readambval(&avs, ambinp)) {
901 >                                sprintf(errmsg,
902 >                        "ambient file \"%s\" corrupted near character %ld",
903 >                                                ambfile, flen - n);
904 >                                error(WARNING, errmsg);
905 >                                break;
906 >                        }
907 >                        avinsert(avstore(&avs));
908                          n -= AMBVALSIZ;
909                  }
910 <                if (n)                          /* alignment */
911 <                        lseek(fileno(ambfp), flen-n, 0);
910 >                lastpos = flen - n;
911 >                /*** seek always as safety measure
912 >                if (n) ***/                     /* alignment */
913 >                        if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
914 >                                goto seekerr;
915          }
436 syncend:
916          n = fflush(ambfp);                      /* calls write() at last */
917 <        lastpos = lseek(fileno(ambfp), 0L, 1);
918 <        fls.l_type = F_UNLCK;                   /* release file */
919 <        fcntl(fileno(ambfp), F_SETLKW, &fls);
917 >        if (n != EOF)
918 >                lastpos += (long)nunflshed*AMBVALSIZ;
919 >        else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
920 >                goto seekerr;
921 >                
922 >        aflock(F_UNLCK);                        /* release file */
923 >        nunflshed = 0;
924          return(n);
925 + seekerr:
926 +        error(SYSTEM, "seek failed in ambsync");
927 +        return -1; /* pro forma return */
928 + }
929 +
930 + #else
931 +
932 + extern int
933 + ambsync(void)                   /* flush ambient file */
934 + {
935 +        if (ambfp == NULL)
936 +                return(0);
937 +        nunflshed = 0;
938 +        return(fflush(ambfp));
939   }
940  
941   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines