ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/ambient.c
Revision: 2.102
Committed: Sun Apr 24 16:21:32 2016 UTC (8 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.101: +10 -10 lines
Log Message:
Minor changes

File Contents

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