ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/ambient.c
Revision: 2.107
Committed: Sat Feb 16 00:09:53 2019 UTC (5 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.106: +2 -2 lines
Log Message:
Fixed infinite loop when setting aa to 0 within rvu

File Contents

# User Rev Content
1 greg 2.107 static const char RCSid[] = "$Id: ambient.c,v 2.106 2018/11/08 00:54:07 greg Exp $";
2 greg 1.1 /*
3     * ambient.c - routines dealing with ambient (inter-reflected) component.
4 greg 2.47 *
5     * Declarations of external symbols in ambient.h
6     */
7    
8 greg 2.48 #include "copyright.h"
9 greg 1.1
10 schorsch 2.52 #include <string.h>
11    
12 schorsch 2.50 #include "platform.h"
13 greg 1.1 #include "ray.h"
14 greg 1.8 #include "otypes.h"
15 greg 2.105 #include "otspecial.h"
16 schorsch 2.56 #include "resolu.h"
17 greg 1.14 #include "ambient.h"
18 greg 1.1 #include "random.h"
19 greg 2.94 #include "pmapamb.h"
20 greg 1.1
21 greg 2.32 #ifndef OCTSCALE
22 greg 2.29 #define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */
23 greg 2.32 #endif
24 greg 1.1
25 greg 2.27 extern char *shm_boundary; /* memory sharing boundary */
26 greg 2.26
27 greg 2.57 #ifndef MAXASET
28 greg 2.67 #define MAXASET 4095 /* maximum number of elements in ambient set */
29 greg 2.57 #endif
30 greg 2.12 OBJECT ambset[MAXASET+1]={0}; /* ambient include/exclude set */
31 greg 1.1
32 greg 2.12 double maxarad; /* maximum ambient radius */
33     double minarad; /* minimum ambient radius */
34 greg 1.1
35 greg 2.12 static AMBTREE atrunk; /* our ambient trunk node */
36 greg 1.1
37     static FILE *ambfp = NULL; /* ambient file pointer */
38 greg 2.16 static int nunflshed = 0; /* number of unflushed ambient values */
39 greg 1.1
40 greg 2.26 #ifndef SORT_THRESH
41 greg 2.49 #ifdef SMLMEM
42 greg 2.71 #define SORT_THRESH ((16L<<20)/sizeof(AMBVAL))
43 greg 2.49 #else
44 greg 2.71 #define SORT_THRESH ((64L<<20)/sizeof(AMBVAL))
45 greg 2.26 #endif
46     #endif
47     #ifndef SORT_INTVL
48 greg 2.41 #define SORT_INTVL (SORT_THRESH<<1)
49 greg 2.26 #endif
50 greg 2.28 #ifndef MAX_SORT_INTVL
51 greg 2.41 #define MAX_SORT_INTVL (SORT_INTVL<<6)
52 greg 2.28 #endif
53 greg 2.26
54 greg 2.74
55 gregl 2.43 static double avsum = 0.; /* computed ambient value sum (log) */
56     static unsigned int navsum = 0; /* number of values in avsum */
57 greg 2.35 static unsigned int nambvals = 0; /* total number of indirect values */
58     static unsigned int nambshare = 0; /* number of values from file */
59 greg 2.27 static unsigned long ambclock = 0; /* ambient access clock */
60     static unsigned long lastsort = 0; /* time of last value sort */
61 greg 2.26 static long sortintvl = SORT_INTVL; /* time until next sort */
62 greg 2.47 static FILE *ambinp = NULL; /* auxiliary file for input */
63     static long lastpos = -1; /* last flush position */
64 greg 2.26
65     #define MAXACLOCK (1L<<30) /* clock turnover value */
66 greg 2.27 /*
67     * Track access times unless we are sharing ambient values
68     * through memory on a multiprocessor, when we want to avoid
69 greg 2.35 * claiming our own memory (copy on write). Go ahead anyway
70     * if more than two thirds of our values are unshared.
71 gregl 2.43 * Compile with -Dtracktime=0 to turn this code off.
72 greg 2.27 */
73 gregl 2.43 #ifndef tracktime
74 greg 2.35 #define tracktime (shm_boundary == NULL || nambvals > 3*nambshare)
75 gregl 2.43 #endif
76 greg 2.26
77 greg 2.12 #define AMBFLUSH (BUFSIZ/AMBVALSIZ)
78 greg 2.6
79 greg 2.47 #define newambval() (AMBVAL *)malloc(sizeof(AMBVAL))
80 greg 1.1
81 schorsch 2.56 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 greg 2.71 typedef void unloadtf_t(AMBVAL *);
88 schorsch 2.56 static unloadtf_t avinsert;
89     static unloadtf_t av2list;
90 greg 2.71 static unloadtf_t avfree;
91 schorsch 2.56 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 greg 2.19 #ifdef F_SETLKW
98 schorsch 2.56 static void aflock(int typ);
99 greg 2.19 #endif
100 greg 1.1
101 greg 2.7
102 greg 2.71 void
103 schorsch 2.56 setambres( /* set ambient resolution */
104     int ar
105     )
106 greg 2.3 {
107 greg 2.21 ambres = ar < 0 ? 0 : ar; /* may be done already */
108 greg 2.3 /* set min & max radii */
109     if (ar <= 0) {
110 greg 2.25 minarad = 0;
111 greg 2.89 maxarad = thescene.cusize*0.2;
112 greg 2.3 } else {
113     minarad = thescene.cusize / ar;
114 greg 2.83 maxarad = 64.0 * minarad; /* heuristic */
115 greg 2.89 if (maxarad > thescene.cusize*0.2)
116     maxarad = thescene.cusize*0.2;
117 greg 2.3 }
118 greg 2.25 if (minarad <= FTINY)
119 greg 2.83 minarad = 10.0*FTINY;
120 greg 2.25 if (maxarad <= minarad)
121 greg 2.83 maxarad = 64.0 * minarad;
122 greg 2.3 }
123    
124    
125 greg 2.71 void
126 schorsch 2.56 setambacc( /* set ambient accuracy */
127     double newa
128     )
129 greg 2.20 {
130 greg 2.84 static double olda; /* remember previous setting here */
131 greg 2.82
132     newa *= (newa > 0);
133     if (fabs(newa - olda) >= .05*(newa + olda)) {
134 greg 2.84 ambacc = newa;
135 greg 2.107 if (ambacc > FTINY && nambvals > 0)
136 greg 2.74 sortambvals(1); /* rebuild tree */
137     }
138 greg 2.20 }
139    
140    
141 greg 2.71 void
142 schorsch 2.56 setambient(void) /* initialize calculation */
143 greg 1.1 {
144 gwlarson 2.46 int readonly = 0;
145 greg 2.66 long flen;
146 greg 2.12 AMBVAL amb;
147 greg 2.47 /* make sure we're fresh */
148     ambdone();
149 greg 2.3 /* init ambient limits */
150     setambres(ambres);
151 greg 2.82 setambacc(ambacc);
152 greg 2.47 if (ambfile == NULL || !ambfile[0])
153 greg 2.19 return;
154 greg 2.20 if (ambacc <= FTINY) {
155 greg 2.21 sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened",
156 greg 2.47 ambfile);
157 greg 2.20 error(WARNING, errmsg);
158     return;
159     }
160 greg 2.3 /* open ambient file */
161 greg 2.47 if ((ambfp = fopen(ambfile, "r+")) == NULL)
162     readonly = (ambfp = fopen(ambfile, "r")) != NULL;
163 gwlarson 2.46 if (ambfp != NULL) {
164     initambfile(0); /* file exists */
165 greg 2.66 lastpos = ftell(ambfp);
166 greg 2.19 while (readambval(&amb, ambfp))
167 greg 2.88 avstore(&amb);
168 gwlarson 2.46 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 greg 2.66 lastpos += (long)nambvals*AMBVALSIZ;
180 greg 2.55 flen = lseek(fileno(ambfp), (off_t)0, SEEK_END);
181 greg 2.66 if (flen != lastpos) {
182 greg 2.42 sprintf(errmsg,
183 greg 2.37 "ignoring last %ld values in ambient file (corrupted)",
184 greg 2.66 (flen - lastpos)/AMBVALSIZ);
185 greg 2.42 error(WARNING, errmsg);
186 greg 2.66 fseek(ambfp, lastpos, SEEK_SET);
187     ftruncate(fileno(ambfp), (off_t)lastpos);
188 greg 2.37 }
189 greg 2.47 } else if ((ambfp = fopen(ambfile, "w+")) != NULL) {
190 gwlarson 2.46 initambfile(1); /* else create new file */
191 greg 2.68 fflush(ambfp);
192 greg 2.66 lastpos = ftell(ambfp);
193 gwlarson 2.46 } else {
194 greg 2.47 sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile);
195 greg 2.19 error(SYSTEM, errmsg);
196 greg 2.16 }
197 greg 2.68 #ifdef F_SETLKW
198     aflock(F_UNLCK); /* release file */
199     #endif
200 greg 1.8 }
201    
202    
203 greg 2.71 void
204 schorsch 2.56 ambdone(void) /* close ambient file and free memory */
205 greg 2.47 {
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 greg 2.99 unloadatree(&atrunk, avfree);
218 greg 2.47 /* 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 greg 2.71 void
230 schorsch 2.56 ambnotify( /* record new modifier */
231     OBJECT obj
232     )
233 greg 1.8 {
234 greg 1.11 static int hitlimit = 0;
235 greg 2.70 OBJREC *o;
236     char **amblp;
237 greg 1.8
238 greg 2.47 if (obj == OVOID) { /* starting over */
239     ambset[0] = 0;
240     hitlimit = 0;
241     return;
242     }
243     o = objptr(obj);
244 greg 1.11 if (hitlimit || !ismodifier(o->otype))
245 greg 1.8 return;
246     for (amblp = amblist; *amblp != NULL; amblp++)
247     if (!strcmp(o->oname, *amblp)) {
248 greg 1.11 if (ambset[0] >= MAXASET) {
249     error(WARNING, "too many modifiers in ambient list");
250     hitlimit++;
251     return; /* should this be fatal? */
252     }
253 greg 1.8 insertelem(ambset, obj);
254     return;
255 greg 1.1 }
256     }
257    
258 greg 2.71 /************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/
259    
260 greg 2.91 #ifndef OLDAMB
261 greg 2.71
262 greg 2.72 #define tfunc(lwr, x, upr) (((x)-(lwr))/((upr)-(lwr)))
263 greg 1.1
264 greg 2.86 static int plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang);
265 greg 2.72 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 greg 2.92 static int extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
269 greg 2.72 FVECT uvw[3]);
270 greg 2.71
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 greg 2.94 COLOR acol, caustic;
280 greg 2.103 int i, ok;
281 greg 2.71 double d, l;
282    
283 greg 2.94 /* 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 greg 2.71 if (ambdiv <= 0) /* no ambient calculation */
296     goto dumbamb;
297     /* check number of bounces */
298     if (rdepth >= ambounce)
299     goto dumbamb;
300     /* check ambient list */
301     if (ambincl != -1 && r->ro != NULL &&
302     ambincl != inset(ambset, r->ro->omod))
303     goto dumbamb;
304    
305     if (ambacc <= FTINY) { /* no ambient storage */
306 greg 2.103 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 greg 2.71 copycolor(acol, aval);
312     rdepth++;
313 greg 2.86 ok = doambient(acol, r, r->rweight,
314 greg 2.103 uvd, NULL, NULL, dgp, NULL);
315 greg 2.71 rdepth--;
316     if (!ok)
317     goto dumbamb;
318 greg 2.103 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 greg 2.71 copycolor(aval, acol);
328 greg 2.94
329     /* PMAP: add in caustic */
330     addcolor(aval, caustic);
331 greg 2.71 return;
332     }
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, nrm, rdepth,
339     &atrunk, thescene.cuorg, thescene.cusize);
340 greg 2.94
341 greg 2.71 if (d > FTINY) {
342     d = 1.0/d;
343     scalecolor(acol, d);
344     multcolor(aval, acol);
345 greg 2.94
346     /* PMAP: add in caustic */
347     addcolor(aval, caustic);
348 greg 2.71 return;
349     }
350 greg 2.94
351 greg 2.71 rdepth++; /* need to cache new value */
352     ok = makeambient(acol, r, nrm, rdepth-1);
353     rdepth--;
354 greg 2.94
355 greg 2.71 if (ok) {
356 greg 2.73 multcolor(aval, acol); /* computed new value */
357 greg 2.94
358     /* PMAP: add in caustic */
359     addcolor(aval, caustic);
360 greg 2.71 return;
361     }
362 greg 2.94
363 greg 2.71 dumbamb: /* return global value */
364     if ((ambvwt <= 0) | (navsum == 0)) {
365     multcolor(aval, ambval);
366 greg 2.94
367     /* PMAP: add in caustic */
368     addcolor(aval, caustic);
369 greg 2.71 return;
370     }
371 greg 2.94
372     l = bright(ambval); /* average in computations */
373 greg 2.71 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 greg 2.86 /* 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 greg 2.105 OBJREC *m = findmaterial(rtst.ro);
420     if (m != NULL && !istransp(m->otype) && !isBSDFproxy(m) &&
421 greg 2.86 (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 greg 2.72 sumambient( /* get interpolated ambient value */
434 greg 2.71 COLOR acol,
435     RAY *r,
436     FVECT rn,
437     int al,
438     AMBTREE *at,
439     FVECT c0,
440     double s
441     )
442 greg 2.80 { /* initial limit is 10 degrees plus ambacc radians */
443     const double minangle = 10.0 * PI/180.;
444 greg 2.84 double maxangle = minangle + ambacc;
445 greg 2.71 double wsum = 0.0;
446     FVECT ck0;
447     int i, j;
448     AMBVAL *av;
449 greg 2.83
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 greg 2.84 if (wsum >= 0.05 && s > minarad*10.0)
468 greg 2.83 return(wsum);
469     }
470 greg 2.84 /* 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 greg 2.71 /* sum this node */
474     for (av = at->alist; av != NULL; av = av->next) {
475 greg 2.86 double u, v, d, delta_r2, delta_t2;
476 greg 2.71 COLOR ct;
477     FVECT uvw[3];
478     /* record access */
479     if (tracktime)
480     av->latick = ambclock;
481     /*
482 greg 2.72 * Ambient level test
483 greg 2.71 */
484 greg 2.87 if (av->lvl > al || /* list sorted, so this works */
485     (av->lvl == al) & (av->weight < 0.9*r->rweight))
486 greg 2.71 break;
487     /*
488 greg 2.72 * Direction test using unperturbed normal
489 greg 2.71 */
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 greg 2.81 * Modified ray behind test
499     */
500 greg 2.85 VSUB(ck0, r->rop, av->pos);
501 greg 2.81 d = DOT(ck0, uvw[2]);
502 greg 2.84 if (d < -minarad*ambacc-.001)
503 greg 2.81 continue;
504     d /= av->rad[0];
505     delta_t2 = d*d;
506 greg 2.84 if (delta_t2 >= ambacc*ambacc)
507 greg 2.81 continue;
508     /*
509 greg 2.72 * Elliptical radii test based on Hessian
510 greg 2.71 */
511     decodedir(uvw[0], av->udir);
512     VCROSS(uvw[1], uvw[2], uvw[0]);
513 greg 2.86 d = (u = DOT(ck0, uvw[0])) / av->rad[0];
514 greg 2.81 delta_t2 += d*d;
515 greg 2.86 d = (v = DOT(ck0, uvw[1])) / av->rad[1];
516 greg 2.71 delta_t2 += d*d;
517 greg 2.84 if (delta_t2 >= ambacc*ambacc)
518 greg 2.71 continue;
519     /*
520 greg 2.86 * Test for potential light leak
521     */
522     if (av->corral && plugaleak(r, av, uvw[2], atan2a(v,u)))
523     continue;
524     /*
525 greg 2.72 * Extrapolate value and compute final weight (hat function)
526 greg 2.71 */
527 greg 2.92 if (!extambient(ct, av, r->rop, rn, uvw))
528     continue;
529 greg 2.71 d = tfunc(maxangle, sqrt(delta_r2), 0.0) *
530 greg 2.84 tfunc(ambacc, sqrt(delta_t2), 0.0);
531 greg 2.71 scalecolor(ct, d);
532     addcolor(acol, ct);
533 greg 2.72 wsum += d;
534 greg 2.71 }
535     return(wsum);
536     }
537    
538    
539 greg 2.86 static int
540 greg 2.71 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 greg 2.72 FVECT uvw[3];
549 greg 2.71 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 greg 2.86 i = doambient(acol, r, amb.weight,
559     uvw, amb.rad, amb.gpos, amb.gdir, &amb.corral);
560 greg 2.71 scalecolor(acol, 1./AVGREFL); /* undo assumed reflectance */
561 greg 2.76 if (i <= 0 || amb.rad[0] <= FTINY) /* no Hessian or zero radius */
562 greg 2.73 return(i);
563 greg 2.71 /* store value */
564     VCOPY(amb.pos, r->rop);
565     amb.ndir = encodedir(r->ron);
566 greg 2.72 amb.udir = encodedir(uvw[0]);
567 greg 2.71 amb.lvl = al;
568     copycolor(amb.val, acol);
569     /* insert into tree */
570     avsave(&amb); /* and save to file */
571 greg 2.72 if (rn != r->ron) { /* texture */
572     VCOPY(uvw[2], r->ron);
573     extambient(acol, &amb, r->rop, rn, uvw);
574     }
575 greg 2.71 return(1);
576     }
577    
578    
579 greg 2.92 static int
580 greg 2.71 extambient( /* extrapolate value at pv, nv */
581     COLOR cr,
582     AMBVAL *ap,
583     FVECT pv,
584 greg 2.72 FVECT nv,
585     FVECT uvw[3]
586 greg 2.71 )
587     {
588 greg 2.93 const double min_d = 0.05;
589 greg 2.72 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 greg 2.71 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 greg 2.93 if (d < min_d) /* should not use if we can avoid it */
609     d = min_d;
610 greg 2.71 copycolor(cr, ap->val);
611     scalecolor(cr, d);
612 greg 2.93 return(d > min_d);
613 greg 2.71 }
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 greg 2.84 while (s*(OCTSCALE/2) > av->rad[1]*ambacc) {
635 greg 2.71 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 greg 2.87 if ( ap->next->lvl > av->lvl ||
650     (ap->next->lvl == av->lvl) &
651     (ap->next->weight <= av->weight) )
652 greg 2.71 break;
653     av->next = ap->next;
654     ap->next = (AMBVAL*)av;
655     at->alist = avh.next;
656     }
657    
658    
659     #else /* ! NEWAMB */
660    
661 greg 2.72 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 greg 2.71
667     void
668 greg 2.58 multambient( /* compute ambient component & multiply by coef. */
669     COLOR aval,
670 greg 2.70 RAY *r,
671 schorsch 2.56 FVECT nrm
672     )
673 greg 1.1 {
674     static int rdepth = 0; /* ambient recursion */
675 greg 2.94 COLOR acol, caustic;
676 gregl 2.43 double d, l;
677 greg 1.1
678 greg 2.94 /* PMAP: Factor in ambient from global photon map (if enabled) and return
679     * as all ambient components accounted for */
680 rschregle 2.95 if (ambPmap(aval, r, rdepth))
681 greg 2.94 return;
682    
683     /* PMAP: Otherwise factor in ambient from caustic photon map
684 rschregle 2.96 * (ambPmapCaustic() returns zero if caustic photons disabled) and
685 greg 2.94 * continue with RADIANCE ambient calculation */
686     copycolor(caustic, aval);
687 rschregle 2.95 ambPmapCaustic(caustic, r, rdepth);
688 greg 2.94
689 greg 1.1 if (ambdiv <= 0) /* no ambient calculation */
690     goto dumbamb;
691     /* check number of bounces */
692 greg 1.16 if (rdepth >= ambounce)
693 greg 1.1 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 greg 2.61 copycolor(acol, aval);
701 greg 1.16 rdepth++;
702 greg 2.61 d = doambient(acol, r, r->rweight, NULL, NULL);
703 greg 1.16 rdepth--;
704 greg 2.32 if (d <= FTINY)
705 greg 1.1 goto dumbamb;
706 greg 2.94 copycolor(aval, acol);
707    
708     /* PMAP: add in caustic */
709     addcolor(aval, caustic);
710 greg 1.16 return;
711 greg 1.1 }
712 greg 2.40
713     if (tracktime) /* sort to minimize thrashing */
714     sortambvals(0);
715 greg 2.61 /* interpolate ambient value */
716 greg 1.1 setcolor(acol, 0.0, 0.0, 0.0);
717 greg 2.61 d = sumambient(acol, r, nrm, rdepth,
718 greg 1.16 &atrunk, thescene.cuorg, thescene.cusize);
719 greg 2.94
720 greg 2.32 if (d > FTINY) {
721 greg 2.61 d = 1.0/d;
722     scalecolor(acol, d);
723 greg 2.58 multcolor(aval, acol);
724 greg 2.94
725     /* PMAP: add in caustic */
726     addcolor(aval, caustic);
727 greg 2.32 return;
728 greg 1.16 }
729 greg 2.94
730 greg 2.33 rdepth++; /* need to cache new value */
731 greg 2.61 d = makeambient(acol, r, nrm, rdepth-1);
732 greg 2.32 rdepth--;
733 greg 2.94
734 greg 2.58 if (d > FTINY) {
735     multcolor(aval, acol); /* got new value */
736 greg 2.94
737     /* PMAP: add in caustic */
738     addcolor(aval, caustic);
739 greg 1.16 return;
740 greg 2.58 }
741 greg 2.94
742 greg 1.1 dumbamb: /* return global value */
743 greg 2.58 if ((ambvwt <= 0) | (navsum == 0)) {
744     multcolor(aval, ambval);
745 greg 2.94
746     /* PMAP: add in caustic */
747     addcolor(aval, caustic);
748 greg 2.32 return;
749 greg 2.58 }
750 greg 2.94
751 gregl 2.43 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 greg 2.58 scalecolor(aval, d);
757     multcolor(aval, ambval); /* apply color of ambval */
758 gregl 2.43 } else {
759     d = exp( avsum / (double)navsum );
760 greg 2.58 scalecolor(aval, d); /* neutral color */
761 gregl 2.43 }
762 greg 1.1 }
763    
764    
765 greg 2.72 static double
766 schorsch 2.56 sumambient( /* get interpolated ambient value */
767     COLOR acol,
768 greg 2.70 RAY *r,
769 schorsch 2.56 FVECT rn,
770     int al,
771     AMBTREE *at,
772     FVECT c0,
773     double s
774     )
775 greg 1.1 {
776 greg 2.12 double d, e1, e2, wt, wsum;
777 greg 1.1 COLOR ct;
778     FVECT ck0;
779     int i;
780 greg 2.70 int j;
781     AMBVAL *av;
782 greg 2.29
783     wsum = 0.0;
784 greg 1.7 /* do this node */
785 greg 1.1 for (av = at->alist; av != NULL; av = av->next) {
786 greg 2.47 double rn_dot = -2.0;
787 greg 2.26 if (tracktime)
788 greg 2.40 av->latick = ambclock;
789 greg 1.1 /*
790 greg 1.16 * Ambient level test.
791 greg 1.1 */
792 greg 2.87 if (av->lvl > al || /* list sorted, so this works */
793     (av->lvl == al) & (av->weight < 0.9*r->rweight))
794 greg 2.23 break;
795 greg 1.1 /*
796     * Ambient radius test.
797     */
798 greg 2.70 VSUB(ck0, av->pos, r->rop);
799     e1 = DOT(ck0, ck0) / (av->rad * av->rad);
800 greg 1.1 if (e1 > ambacc*ambacc*1.21)
801     continue;
802     /*
803 greg 2.47 * Direction test using closest normal.
804 greg 1.1 */
805 greg 2.47 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 greg 2.70 if (e2 < 0.0)
817     e2 = 0.0;
818     else if (e1 + e2 > ambacc*ambacc*1.21)
819 greg 1.1 continue;
820     /*
821     * Ray behind test.
822     */
823     d = 0.0;
824     for (j = 0; j < 3; j++)
825     d += (r->rop[j] - av->pos[j]) *
826     (av->dir[j] + r->ron[j]);
827 greg 1.18 if (d*0.5 < -minarad*ambacc-.001)
828 greg 1.1 continue;
829     /*
830     * Jittering final test reduces image artifacts.
831     */
832 greg 2.47 e1 = sqrt(e1);
833     e2 = sqrt(e2);
834     wt = e1 + e2;
835 greg 2.31 if (wt > ambacc*(.9+.2*urand(9015+samplendx)))
836 greg 1.1 continue;
837 greg 2.47 /*
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 greg 1.1 if (wt <= 1e-3)
845     wt = 1e3;
846     else
847     wt = 1.0 / wt;
848     wsum += wt;
849 greg 2.34 extambient(ct, av, r->rop, rn);
850 greg 1.1 scalecolor(ct, wt);
851     addcolor(acol, ct);
852 greg 1.7 }
853     if (at->kid == NULL)
854     return(wsum);
855     /* do children */
856     s *= 0.5;
857     for (i = 0; i < 8; i++) {
858     for (j = 0; j < 3; j++) {
859     ck0[j] = c0[j];
860     if (1<<j & i)
861     ck0[j] += s;
862     if (r->rop[j] < ck0[j] - OCTSCALE*s)
863     break;
864     if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
865     break;
866     }
867     if (j == 3)
868 greg 2.61 wsum += sumambient(acol, r, rn, al,
869 greg 2.59 at->kid+i, ck0, s);
870 greg 1.1 }
871     return(wsum);
872     }
873    
874    
875 greg 2.72 static double
876 greg 2.61 makeambient( /* make a new ambient value for storage */
877 schorsch 2.56 COLOR acol,
878 greg 2.58 RAY *r,
879 schorsch 2.56 FVECT rn,
880     int al
881     )
882 greg 1.1 {
883 greg 2.12 AMBVAL amb;
884 greg 1.14 FVECT gp, gd;
885 greg 2.60 int i;
886    
887 greg 2.61 amb.weight = 1.0; /* compute weight */
888     for (i = al; i-- > 0; )
889 greg 2.60 amb.weight *= AVGREFL;
890 greg 2.61 if (r->rweight < 0.1*amb.weight) /* heuristic override */
891 greg 2.62 amb.weight = 1.25*r->rweight;
892 greg 2.61 setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
893 greg 1.16 /* compute ambient */
894 greg 2.61 amb.rad = doambient(acol, r, amb.weight, gp, gd);
895     if (amb.rad <= FTINY) {
896     setcolor(acol, 0.0, 0.0, 0.0);
897 greg 1.1 return(0.0);
898 greg 2.61 }
899     scalecolor(acol, 1./AVGREFL); /* undo assumed reflectance */
900     /* store value */
901 greg 1.1 VCOPY(amb.pos, r->rop);
902     VCOPY(amb.dir, r->ron);
903 greg 1.16 amb.lvl = al;
904 greg 1.1 copycolor(amb.val, acol);
905 greg 1.14 VCOPY(amb.gpos, gp);
906     VCOPY(amb.gdir, gd);
907 greg 1.1 /* insert into tree */
908 greg 2.7 avsave(&amb); /* and save to file */
909 greg 2.34 if (rn != r->ron)
910     extambient(acol, &amb, r->rop, rn); /* texture */
911 greg 1.1 return(amb.rad);
912 greg 1.15 }
913    
914    
915 greg 2.72 static void
916 schorsch 2.56 extambient( /* extrapolate value at pv, nv */
917     COLOR cr,
918 greg 2.70 AMBVAL *ap,
919 schorsch 2.56 FVECT pv,
920     FVECT nv
921     )
922 greg 1.15 {
923 gwlarson 2.45 FVECT v1;
924 greg 2.70 int i;
925 greg 2.12 double d;
926 greg 1.15
927     d = 1.0; /* zeroeth order */
928     /* gradient due to translation */
929     for (i = 0; i < 3; i++)
930     d += ap->gpos[i]*(pv[i]-ap->pos[i]);
931     /* gradient due to rotation */
932 gwlarson 2.45 VCROSS(v1, ap->dir, nv);
933     d += DOT(ap->gdir, v1);
934 greg 1.15 if (d <= 0.0) {
935     setcolor(cr, 0.0, 0.0, 0.0);
936     return;
937     }
938     copycolor(cr, ap->val);
939     scalecolor(cr, d);
940 greg 1.1 }
941    
942    
943 greg 2.47 static void
944 greg 2.71 avinsert( /* insert ambient value in our tree */
945     AMBVAL *av
946     )
947     {
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 greg 2.87 if ( ap->next->lvl > av->lvl ||
977     (ap->next->lvl == av->lvl) &
978     (ap->next->weight <= av->weight) )
979 greg 2.71 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 schorsch 2.56 initambfile( /* initialize ambient file */
991 greg 2.68 int cre8
992 schorsch 2.56 )
993 greg 2.9 {
994 greg 2.47 extern char *progname, *octname;
995     static char *mybuf = NULL;
996 greg 2.9
997 greg 2.19 #ifdef F_SETLKW
998 greg 2.69 aflock(cre8 ? F_WRLCK : F_RDLCK);
999 greg 2.19 #endif
1000 schorsch 2.50 SET_FILE_BINARY(ambfp);
1001 greg 2.47 if (mybuf == NULL)
1002     mybuf = (char *)bmalloc(BUFSIZ+8);
1003     setbuf(ambfp, mybuf);
1004 greg 2.68 if (cre8) { /* new file */
1005 greg 2.24 newheader("RADIANCE", ambfp);
1006 greg 2.41 fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ",
1007 greg 2.9 progname, colval(ambval,RED),
1008     colval(ambval,GRN), colval(ambval,BLU),
1009 greg 2.41 ambvwt, ambounce, ambacc);
1010 greg 2.47 fprintf(ambfp, "-ad %d -as %d -ar %d ",
1011     ambdiv, ambssamp, ambres);
1012     if (octname != NULL)
1013 greg 2.68 fputs(octname, ambfp);
1014     fputc('\n', ambfp);
1015 greg 2.9 fprintf(ambfp, "SOFTWARE= %s\n", VersionID);
1016 greg 2.47 fputnow(ambfp);
1017 greg 2.9 fputformat(AMBFMT, ambfp);
1018 greg 2.68 fputc('\n', ambfp);
1019 greg 2.9 putambmagic(ambfp);
1020 greg 2.17 } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp))
1021     error(USER, "bad ambient file");
1022 greg 2.9 }
1023    
1024    
1025 greg 2.47 static void
1026 schorsch 2.56 avsave( /* insert and save an ambient value */
1027     AMBVAL *av
1028     )
1029 greg 1.1 {
1030 greg 2.88 avstore(av);
1031 greg 1.1 if (ambfp == NULL)
1032     return;
1033 greg 2.5 if (writambval(av, ambfp) < 0)
1034 greg 1.1 goto writerr;
1035 greg 2.16 if (++nunflshed >= AMBFLUSH)
1036 greg 2.7 if (ambsync() == EOF)
1037 greg 1.1 goto writerr;
1038     return;
1039     writerr:
1040 greg 2.47 error(SYSTEM, "error writing to ambient file");
1041 greg 1.1 }
1042    
1043    
1044 greg 2.20 static AMBVAL *
1045 greg 2.88 avstore( /* allocate memory and save aval */
1046 greg 2.70 AMBVAL *aval
1047 schorsch 2.56 )
1048 greg 2.20 {
1049 greg 2.70 AMBVAL *av;
1050 gregl 2.43 double d;
1051 greg 2.20
1052     if ((av = newambval()) == NULL)
1053     error(SYSTEM, "out of memory in avstore");
1054 schorsch 2.53 *av = *aval;
1055 greg 2.26 av->latick = ambclock;
1056     av->next = NULL;
1057     nambvals++;
1058 gregl 2.43 d = bright(av->val);
1059     if (d > FTINY) { /* add to log sum for averaging */
1060     avsum += log(d);
1061     navsum++;
1062     }
1063 greg 2.88 avinsert(av); /* insert in our cache tree */
1064 greg 2.20 return(av);
1065     }
1066    
1067    
1068 greg 2.26 #define ATALLOCSZ 512 /* #/8 trees to allocate at once */
1069    
1070     static AMBTREE *atfreelist = NULL; /* free ambient tree structures */
1071    
1072    
1073 greg 2.47 static AMBTREE *
1074 schorsch 2.56 newambtree(void) /* allocate 8 ambient tree structs */
1075 greg 2.26 {
1076 greg 2.70 AMBTREE *atp, *upperlim;
1077 greg 2.26
1078     if (atfreelist == NULL) { /* get more nodes */
1079 greg 2.47 atfreelist = (AMBTREE *)malloc(ATALLOCSZ*8*sizeof(AMBTREE));
1080 greg 2.26 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     atp = atfreelist;
1089     atfreelist = atp->kid;
1090 greg 2.102 memset(atp, 0, 8*sizeof(AMBTREE));
1091 greg 2.26 return(atp);
1092     }
1093    
1094    
1095 greg 2.47 static void
1096 schorsch 2.56 freeambtree( /* free 8 ambient tree structs */
1097     AMBTREE *atp
1098     )
1099 greg 2.26 {
1100 greg 2.100 atp->kid = atfreelist;
1101 greg 2.26 atfreelist = atp;
1102     }
1103    
1104    
1105 greg 2.47 static void
1106 schorsch 2.56 unloadatree( /* unload an ambient value tree */
1107 greg 2.70 AMBTREE *at,
1108 schorsch 2.56 unloadtf_t *f
1109     )
1110 greg 2.20 {
1111 greg 2.70 AMBVAL *av;
1112     int i;
1113 greg 2.20 /* transfer values at this node */
1114     for (av = at->alist; av != NULL; av = at->alist) {
1115     at->alist = av->next;
1116 greg 2.102 av->next = NULL;
1117 greg 2.26 (*f)(av);
1118 greg 2.20 }
1119 greg 2.21 if (at->kid == NULL)
1120     return;
1121 greg 2.20 for (i = 0; i < 8; i++) /* transfer and free children */
1122 greg 2.26 unloadatree(at->kid+i, f);
1123 greg 2.20 freeambtree(at->kid);
1124 greg 2.26 at->kid = NULL;
1125     }
1126    
1127    
1128 greg 2.39 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 greg 2.26 static int i_avlist; /* index for lists */
1134    
1135 schorsch 2.56 static int alatcmp(const void *av1, const void *av2);
1136 greg 2.26
1137 schorsch 2.56 static void
1138 greg 2.71 avfree(AMBVAL *av)
1139     {
1140     free(av);
1141     }
1142    
1143     static void
1144 schorsch 2.56 av2list(
1145 greg 2.71 AMBVAL *av
1146 schorsch 2.56 )
1147 greg 2.26 {
1148 greg 2.27 #ifdef DEBUG
1149 greg 2.26 if (i_avlist >= nambvals)
1150     error(CONSISTENCY, "too many ambient values in av2list1");
1151 greg 2.27 #endif
1152 schorsch 2.56 avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av;
1153 greg 2.71 avlist1[i_avlist++].t = av->latick;
1154 greg 2.26 }
1155    
1156    
1157     static int
1158 schorsch 2.56 alatcmp( /* compare ambient values for MRA */
1159     const void *av1,
1160     const void *av2
1161     )
1162 greg 2.26 {
1163 greg 2.70 long lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t;
1164 greg 2.38 return(lc<0 ? -1 : lc>0 ? 1 : 0);
1165 greg 2.26 }
1166    
1167    
1168 greg 2.47 /* 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 greg 2.26 static int
1175 schorsch 2.56 aposcmp( /* compare ambient value positions */
1176     const void *avp1,
1177     const void *avp2
1178     )
1179 greg 2.26 {
1180 greg 2.70 long diff = *(char * const *)avp1 - *(char * const *)avp2;
1181 greg 2.47 if (diff < 0)
1182     return(-1);
1183     return(diff > 0);
1184 greg 2.26 }
1185    
1186 greg 2.71
1187 greg 2.27 static int
1188 schorsch 2.56 avlmemi( /* find list position from address */
1189     AMBVAL *avaddr
1190     )
1191 greg 2.27 {
1192 greg 2.70 AMBVAL **avlpp;
1193 greg 2.27
1194 greg 2.102 avlpp = (AMBVAL **)bsearch(&avaddr, avlist2,
1195     nambvals, sizeof(AMBVAL *), aposcmp);
1196 greg 2.27 if (avlpp == NULL)
1197     error(CONSISTENCY, "address not found in avlmemi");
1198     return(avlpp - avlist2);
1199     }
1200    
1201    
1202 greg 2.47 static void
1203 schorsch 2.56 sortambvals( /* resort ambient values */
1204     int always
1205     )
1206 greg 2.26 {
1207     AMBTREE oldatrunk;
1208     AMBVAL tav, *tap, *pnext;
1209 greg 2.70 int i, j;
1210 greg 2.28 /* see if it's time yet */
1211 greg 2.40 if (!always && (ambclock++ < lastsort+sortintvl ||
1212 greg 2.28 nambvals < SORT_THRESH))
1213     return;
1214 greg 2.26 /*
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 greg 2.27 if (tracktime) { /* allocate pointer arrays to sort */
1230 greg 2.26 avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
1231 greg 2.39 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 greg 2.102 free(avlist2);
1239 greg 2.27 if (always) { /* rebuild without sorting */
1240 schorsch 2.53 oldatrunk = atrunk;
1241 greg 2.27 atrunk.alist = NULL;
1242     atrunk.kid = NULL;
1243 greg 2.99 unloadatree(&oldatrunk, avinsert);
1244 greg 2.27 }
1245 greg 2.26 } 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 greg 2.27 * everyone at least twice, and this is an expensive process
1252 greg 2.26 * when we're thrashing, which is when we need to do it.
1253     */
1254 greg 2.27 #ifdef DEBUG
1255 greg 2.29 sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...",
1256     nambvals, ambclock);
1257 greg 2.27 eputs(errmsg);
1258     #endif
1259     i_avlist = 0;
1260 greg 2.99 unloadatree(&atrunk, av2list); /* empty current tree */
1261 greg 2.27 #ifdef DEBUG
1262     if (i_avlist < nambvals)
1263     error(CONSISTENCY, "missing ambient values in sortambvals");
1264     #endif
1265 greg 2.102 qsort(avlist1, nambvals, sizeof(struct avl), alatcmp);
1266     qsort(avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
1267 greg 2.26 for (i = 0; i < nambvals; i++) {
1268 greg 2.39 if (avlist1[i].p == NULL)
1269 greg 2.26 continue;
1270     tap = avlist2[i];
1271 schorsch 2.53 tav = *tap;
1272 greg 2.39 for (j = i; (pnext = avlist1[j].p) != tap;
1273 greg 2.27 j = avlmemi(pnext)) {
1274 schorsch 2.53 *(avlist2[j]) = *pnext;
1275 greg 2.26 avinsert(avlist2[j]);
1276 greg 2.39 avlist1[j].p = NULL;
1277 greg 2.26 }
1278 schorsch 2.53 *(avlist2[j]) = tav;
1279 greg 2.26 avinsert(avlist2[j]);
1280 greg 2.39 avlist1[j].p = NULL;
1281 greg 2.26 }
1282 greg 2.102 free(avlist1);
1283     free(avlist2);
1284 greg 2.28 /* compute new sort interval */
1285     sortintvl = ambclock - lastsort;
1286 greg 2.32 if (sortintvl >= MAX_SORT_INTVL/2)
1287 greg 2.28 sortintvl = MAX_SORT_INTVL;
1288     else
1289 greg 2.26 sortintvl <<= 1; /* wait twice as long next */
1290 greg 2.27 #ifdef DEBUG
1291     eputs("done\n");
1292     #endif
1293 greg 2.26 }
1294     if (ambclock >= MAXACLOCK)
1295     ambclock = MAXACLOCK/2;
1296     lastsort = ambclock;
1297 greg 2.20 }
1298    
1299    
1300 greg 2.18 #ifdef F_SETLKW
1301 greg 2.10
1302 greg 2.47 static void
1303 schorsch 2.56 aflock( /* lock/unlock ambient file */
1304     int typ
1305     )
1306 greg 2.19 {
1307     static struct flock fls; /* static so initialized to zeroes */
1308    
1309 greg 2.66 if (typ == fls.l_type) /* already called? */
1310     return;
1311 greg 2.19 fls.l_type = typ;
1312     if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
1313     error(SYSTEM, "cannot (un)lock ambient file");
1314     }
1315    
1316    
1317 greg 2.71 int
1318 schorsch 2.56 ambsync(void) /* synchronize ambient file */
1319 greg 2.7 {
1320 greg 2.15 long flen;
1321 greg 2.12 AMBVAL avs;
1322 greg 2.70 int n;
1323 greg 2.16
1324 greg 2.65 if (ambfp == NULL) /* no ambient file? */
1325     return(0);
1326 greg 2.63 /* gain appropriate access */
1327     aflock(nunflshed ? F_WRLCK : F_RDLCK);
1328 greg 2.7 /* see if file has grown */
1329 greg 2.55 if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0)
1330 greg 2.21 goto seekerr;
1331 greg 2.68 if ((n = flen - lastpos) > 0) { /* file has grown */
1332 greg 2.104 if (ambinp == NULL) { /* get new file pointer */
1333     ambinp = fopen(ambfile, "rb");
1334 greg 2.14 if (ambinp == NULL)
1335 greg 2.104 error(SYSTEM, "fopen failed in ambsync");
1336 greg 2.14 }
1337 greg 2.66 if (fseek(ambinp, lastpos, SEEK_SET) < 0)
1338 greg 2.21 goto seekerr;
1339 greg 2.15 while (n >= AMBVALSIZ) { /* load contributed values */
1340 greg 2.37 if (!readambval(&avs, ambinp)) {
1341     sprintf(errmsg,
1342 greg 2.47 "ambient file \"%s\" corrupted near character %ld",
1343     ambfile, flen - n);
1344 greg 2.37 error(WARNING, errmsg);
1345     break;
1346     }
1347 greg 2.88 avstore(&avs);
1348 greg 2.15 n -= AMBVALSIZ;
1349     }
1350 greg 2.104 lastpos = flen - n; /* check alignment */
1351     if (n && lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
1352     goto seekerr;
1353 greg 2.7 }
1354     n = fflush(ambfp); /* calls write() at last */
1355 greg 2.104 lastpos += (long)nunflshed*AMBVALSIZ;
1356 greg 2.19 aflock(F_UNLCK); /* release file */
1357 greg 2.16 nunflshed = 0;
1358 greg 2.7 return(n);
1359 greg 2.21 seekerr:
1360     error(SYSTEM, "seek failed in ambsync");
1361 greg 2.104 return(EOF); /* pro forma return */
1362 greg 2.18 }
1363    
1364 greg 2.71 #else /* ! F_SETLKW */
1365 greg 2.18
1366 greg 2.71 int
1367 schorsch 2.56 ambsync(void) /* flush ambient file */
1368 greg 2.18 {
1369 greg 2.65 if (ambfp == NULL)
1370     return(0);
1371 greg 2.18 nunflshed = 0;
1372     return(fflush(ambfp));
1373 greg 1.1 }
1374 greg 2.10
1375 greg 2.71 #endif /* ! F_SETLKW */