1 |
– |
/* Copyright (c) 1986 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 |
< |
* The macro AMBFLUSH (if defined) is the number of ambient values |
8 |
< |
* to wait before flushing to the ambient file. |
7 |
> |
* Declarations of external symbols in ambient.h |
8 |
> |
*/ |
9 |
> |
|
10 |
> |
/* ==================================================================== |
11 |
> |
* The Radiance Software License, Version 1.0 |
12 |
|
* |
13 |
< |
* 5/9/86 |
13 |
> |
* Copyright (c) 1990 - 2002 The Regents of the University of California, |
14 |
> |
* through Lawrence Berkeley National Laboratory. All rights reserved. |
15 |
> |
* |
16 |
> |
* Redistribution and use in source and binary forms, with or without |
17 |
> |
* modification, are permitted provided that the following conditions |
18 |
> |
* are met: |
19 |
> |
* |
20 |
> |
* 1. Redistributions of source code must retain the above copyright |
21 |
> |
* notice, this list of conditions and the following disclaimer. |
22 |
> |
* |
23 |
> |
* 2. Redistributions in binary form must reproduce the above copyright |
24 |
> |
* notice, this list of conditions and the following disclaimer in |
25 |
> |
* the documentation and/or other materials provided with the |
26 |
> |
* distribution. |
27 |
> |
* |
28 |
> |
* 3. The end-user documentation included with the redistribution, |
29 |
> |
* if any, must include the following acknowledgment: |
30 |
> |
* "This product includes Radiance software |
31 |
> |
* (http://radsite.lbl.gov/) |
32 |
> |
* developed by the Lawrence Berkeley National Laboratory |
33 |
> |
* (http://www.lbl.gov/)." |
34 |
> |
* Alternately, this acknowledgment may appear in the software itself, |
35 |
> |
* if and wherever such third-party acknowledgments normally appear. |
36 |
> |
* |
37 |
> |
* 4. The names "Radiance," "Lawrence Berkeley National Laboratory" |
38 |
> |
* and "The Regents of the University of California" must |
39 |
> |
* not be used to endorse or promote products derived from this |
40 |
> |
* software without prior written permission. For written |
41 |
> |
* permission, please contact [email protected]. |
42 |
> |
* |
43 |
> |
* 5. Products derived from this software may not be called "Radiance", |
44 |
> |
* nor may "Radiance" appear in their name, without prior written |
45 |
> |
* permission of Lawrence Berkeley National Laboratory. |
46 |
> |
* |
47 |
> |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
48 |
> |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
49 |
> |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
50 |
> |
* DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR |
51 |
> |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
52 |
> |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
53 |
> |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
54 |
> |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
55 |
> |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
56 |
> |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
57 |
> |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
58 |
> |
* SUCH DAMAGE. |
59 |
> |
* ==================================================================== |
60 |
> |
* |
61 |
> |
* This software consists of voluntary contributions made by many |
62 |
> |
* individuals on behalf of Lawrence Berkeley National Laboratory. For more |
63 |
> |
* information on Lawrence Berkeley National Laboratory, please see |
64 |
> |
* <http://www.lbl.gov/>. |
65 |
|
*/ |
66 |
|
|
67 |
|
#include "ray.h" |
68 |
|
|
69 |
< |
#include "octree.h" |
69 |
> |
#include "otypes.h" |
70 |
|
|
71 |
+ |
#include "ambient.h" |
72 |
+ |
|
73 |
|
#include "random.h" |
74 |
|
|
75 |
< |
#define OCTSCALE 0.5 /* ceil((valid rad.)/(cube size)) */ |
75 |
> |
#ifndef OCTSCALE |
76 |
> |
#define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */ |
77 |
> |
#endif |
78 |
|
|
79 |
< |
extern CUBE thescene; /* contains space boundaries */ |
79 |
> |
extern char *shm_boundary; /* memory sharing boundary */ |
80 |
|
|
81 |
< |
extern COLOR ambval; /* global ambient component */ |
82 |
< |
extern double ambacc; /* ambient accuracy */ |
28 |
< |
extern int ambres; /* ambient resolution */ |
29 |
< |
extern int ambdiv; /* number of divisions for calculation */ |
30 |
< |
extern int ambssamp; /* number of super-samples */ |
31 |
< |
extern int ambounce; /* number of ambient bounces */ |
32 |
< |
extern char *amblist[]; /* ambient include/exclude list */ |
33 |
< |
extern int ambincl; /* include == 1, exclude == 0 */ |
81 |
> |
#define MAXASET 511 /* maximum number of elements in ambient set */ |
82 |
> |
OBJECT ambset[MAXASET+1]={0}; /* ambient include/exclude set */ |
83 |
|
|
84 |
< |
OBJECT ambset[128]; /* ambient include/exclude set */ |
84 |
> |
double maxarad; /* maximum ambient radius */ |
85 |
> |
double minarad; /* minimum ambient radius */ |
86 |
|
|
87 |
< |
double maxarad; /* maximum ambient radius */ |
38 |
< |
double minarad; /* minimum ambient radius */ |
87 |
> |
static AMBTREE atrunk; /* our ambient trunk node */ |
88 |
|
|
89 |
< |
typedef struct ambval { |
90 |
< |
FVECT pos; /* position in space */ |
42 |
< |
FVECT dir; /* normal direction */ |
43 |
< |
int lvl; /* recursion level of parent ray */ |
44 |
< |
float weight; /* weight of parent ray */ |
45 |
< |
COLOR val; /* computed ambient value */ |
46 |
< |
float rad; /* validity radius */ |
47 |
< |
struct ambval *next; /* next in list */ |
48 |
< |
} AMBVAL; /* ambient value */ |
89 |
> |
static FILE *ambfp = NULL; /* ambient file pointer */ |
90 |
> |
static int nunflshed = 0; /* number of unflushed ambient values */ |
91 |
|
|
92 |
< |
typedef struct ambtree { |
93 |
< |
AMBVAL *alist; /* ambient value list */ |
94 |
< |
struct ambtree *kid; /* 8 child nodes */ |
95 |
< |
} AMBTREE; /* ambient octree */ |
92 |
> |
#ifndef SORT_THRESH |
93 |
> |
#ifdef BIGMEM |
94 |
> |
#define SORT_THRESH ((9L<<20)/sizeof(AMBVAL)) |
95 |
> |
#else |
96 |
> |
#define SORT_THRESH ((3L<<20)/sizeof(AMBVAL)) |
97 |
> |
#endif |
98 |
> |
#endif |
99 |
> |
#ifndef SORT_INTVL |
100 |
> |
#define SORT_INTVL (SORT_THRESH<<1) |
101 |
> |
#endif |
102 |
> |
#ifndef MAX_SORT_INTVL |
103 |
> |
#define MAX_SORT_INTVL (SORT_INTVL<<6) |
104 |
> |
#endif |
105 |
|
|
106 |
< |
typedef struct { |
107 |
< |
float k; /* error contribution per sample */ |
108 |
< |
COLOR v; /* ray sum */ |
109 |
< |
int n; /* number of samples */ |
110 |
< |
short t, p; /* theta, phi indices */ |
111 |
< |
} AMBSAMP; /* ambient sample */ |
106 |
> |
static double avsum = 0.; /* computed ambient value sum (log) */ |
107 |
> |
static unsigned int navsum = 0; /* number of values in avsum */ |
108 |
> |
static unsigned int nambvals = 0; /* total number of indirect values */ |
109 |
> |
static unsigned int nambshare = 0; /* number of values from file */ |
110 |
> |
static unsigned long ambclock = 0; /* ambient access clock */ |
111 |
> |
static unsigned long lastsort = 0; /* time of last value sort */ |
112 |
> |
static long sortintvl = SORT_INTVL; /* time until next sort */ |
113 |
> |
static FILE *ambinp = NULL; /* auxiliary file for input */ |
114 |
> |
static long lastpos = -1; /* last flush position */ |
115 |
|
|
116 |
< |
static AMBTREE atrunk; /* our ambient trunk node */ |
116 |
> |
#define MAXACLOCK (1L<<30) /* clock turnover value */ |
117 |
> |
/* |
118 |
> |
* Track access times unless we are sharing ambient values |
119 |
> |
* through memory on a multiprocessor, when we want to avoid |
120 |
> |
* claiming our own memory (copy on write). Go ahead anyway |
121 |
> |
* if more than two thirds of our values are unshared. |
122 |
> |
* Compile with -Dtracktime=0 to turn this code off. |
123 |
> |
*/ |
124 |
> |
#ifndef tracktime |
125 |
> |
#define tracktime (shm_boundary == NULL || nambvals > 3*nambshare) |
126 |
> |
#endif |
127 |
|
|
128 |
< |
static FILE *ambfp = NULL; /* ambient file pointer */ |
128 |
> |
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
129 |
|
|
130 |
< |
#define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL)) |
130 |
> |
#define newambval() (AMBVAL *)malloc(sizeof(AMBVAL)) |
131 |
> |
#define freeav(av) free((void *)av); |
132 |
|
|
133 |
< |
#define newambtree() (AMBTREE *)calloc(8, sizeof(AMBTREE)) |
133 |
> |
static void initambfile(), avsave(), avinsert(), sortambvals(), unloadatree(); |
134 |
> |
static int avlmemi(); |
135 |
> |
static AMBVAL *avstore(); |
136 |
> |
#ifdef F_SETLKW |
137 |
> |
static void aflock(); |
138 |
> |
#endif |
139 |
|
|
70 |
– |
double sumambient(), doambient(), makeambient(); |
140 |
|
|
141 |
+ |
void |
142 |
+ |
setambres(ar) /* set ambient resolution */ |
143 |
+ |
int ar; |
144 |
+ |
{ |
145 |
+ |
ambres = ar < 0 ? 0 : ar; /* may be done already */ |
146 |
+ |
/* set min & max radii */ |
147 |
+ |
if (ar <= 0) { |
148 |
+ |
minarad = 0; |
149 |
+ |
maxarad = thescene.cusize / 2.0; |
150 |
+ |
} else { |
151 |
+ |
minarad = thescene.cusize / ar; |
152 |
+ |
maxarad = 64 * minarad; /* heuristic */ |
153 |
+ |
if (maxarad > thescene.cusize / 2.0) |
154 |
+ |
maxarad = thescene.cusize / 2.0; |
155 |
+ |
} |
156 |
+ |
if (minarad <= FTINY) |
157 |
+ |
minarad = 10*FTINY; |
158 |
+ |
if (maxarad <= minarad) |
159 |
+ |
maxarad = 64 * minarad; |
160 |
+ |
} |
161 |
|
|
162 |
< |
setambient(afile) /* initialize calculation */ |
163 |
< |
char *afile; |
162 |
> |
|
163 |
> |
void |
164 |
> |
setambacc(newa) /* set ambient accuracy */ |
165 |
> |
double newa; |
166 |
|
{ |
167 |
< |
long ftell(); |
168 |
< |
char **amblp; |
169 |
< |
OBJECT obj; |
170 |
< |
AMBVAL amb; |
171 |
< |
/* set up ambient set */ |
172 |
< |
ambset[0] = 0; |
173 |
< |
for (amblp = amblist; *amblp != NULL; amblp++) { |
174 |
< |
if ((obj = modifier(*amblp)) == OVOID) { |
175 |
< |
sprintf(errmsg, "unknown %s modifier \"%s\"", |
176 |
< |
ambincl ? "include" : "exclude", *amblp); |
167 |
> |
double ambdiff; |
168 |
> |
|
169 |
> |
if (newa < 0.0) |
170 |
> |
newa = 0.0; |
171 |
> |
ambdiff = fabs(newa - ambacc); |
172 |
> |
if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0) |
173 |
> |
sortambvals(1); /* rebuild tree */ |
174 |
> |
} |
175 |
> |
|
176 |
> |
|
177 |
> |
void |
178 |
> |
setambient() /* initialize calculation */ |
179 |
> |
{ |
180 |
> |
int readonly = 0; |
181 |
> |
long pos, flen; |
182 |
> |
AMBVAL amb; |
183 |
> |
/* make sure we're fresh */ |
184 |
> |
ambdone(); |
185 |
> |
/* init ambient limits */ |
186 |
> |
setambres(ambres); |
187 |
> |
setambacc(ambacc); |
188 |
> |
if (ambfile == NULL || !ambfile[0]) |
189 |
> |
return; |
190 |
> |
if (ambacc <= FTINY) { |
191 |
> |
sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened", |
192 |
> |
ambfile); |
193 |
> |
error(WARNING, errmsg); |
194 |
> |
return; |
195 |
> |
} |
196 |
> |
/* open ambient file */ |
197 |
> |
if ((ambfp = fopen(ambfile, "r+")) == NULL) |
198 |
> |
readonly = (ambfp = fopen(ambfile, "r")) != NULL; |
199 |
> |
if (ambfp != NULL) { |
200 |
> |
initambfile(0); /* file exists */ |
201 |
> |
pos = ftell(ambfp); |
202 |
> |
while (readambval(&amb, ambfp)) |
203 |
> |
avinsert(avstore(&amb)); |
204 |
> |
nambshare = nambvals; /* share loaded values */ |
205 |
> |
if (readonly) { |
206 |
> |
sprintf(errmsg, |
207 |
> |
"loaded %u values from read-only ambient file", |
208 |
> |
nambvals); |
209 |
|
error(WARNING, errmsg); |
210 |
< |
continue; |
210 |
> |
fclose(ambfp); /* close file so no writes */ |
211 |
> |
ambfp = NULL; |
212 |
> |
return; /* avoid ambsync() */ |
213 |
|
} |
214 |
< |
if (!inset(ambset, obj)) |
215 |
< |
insertelem(ambset, obj); |
214 |
> |
/* align file pointer */ |
215 |
> |
pos += (long)nambvals*AMBVALSIZ; |
216 |
> |
flen = lseek(fileno(ambfp), (off_t)0L, 2); |
217 |
> |
if (flen != pos) { |
218 |
> |
sprintf(errmsg, |
219 |
> |
"ignoring last %ld values in ambient file (corrupted)", |
220 |
> |
(flen - pos)/AMBVALSIZ); |
221 |
> |
error(WARNING, errmsg); |
222 |
> |
fseek(ambfp, pos, 0); |
223 |
> |
ftruncate(fileno(ambfp), (off_t)pos); |
224 |
> |
} |
225 |
> |
} else if ((ambfp = fopen(ambfile, "w+")) != NULL) { |
226 |
> |
initambfile(1); /* else create new file */ |
227 |
> |
} else { |
228 |
> |
sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); |
229 |
> |
error(SYSTEM, errmsg); |
230 |
|
} |
231 |
< |
maxarad = thescene.cusize / 2.0; /* maximum radius */ |
232 |
< |
/* minimum radius */ |
233 |
< |
minarad = ambres > 0 ? thescene.cusize/ambres : 0.0; |
231 |
> |
nunflshed++; /* lie */ |
232 |
> |
ambsync(); |
233 |
> |
} |
234 |
|
|
235 |
< |
/* open ambient file */ |
236 |
< |
if (afile != NULL) |
237 |
< |
if ((ambfp = fopen(afile, "r+")) != NULL) { |
238 |
< |
while (fread(&amb, sizeof(AMBVAL), 1, ambfp) == 1) |
239 |
< |
avinsert(&amb, &atrunk, thescene.cuorg, |
240 |
< |
thescene.cusize); |
241 |
< |
/* align */ |
242 |
< |
fseek(ambfp, -(ftell(ambfp)%sizeof(AMBVAL)), 1); |
243 |
< |
} else if ((ambfp = fopen(afile, "w")) == NULL) { |
244 |
< |
sprintf(errmsg, "cannot open ambient file \"%s\"", |
245 |
< |
afile); |
107 |
< |
error(SYSTEM, errmsg); |
235 |
> |
|
236 |
> |
void |
237 |
> |
ambdone() /* close ambient file and free memory */ |
238 |
> |
{ |
239 |
> |
if (ambfp != NULL) { /* close ambient file */ |
240 |
> |
ambsync(); |
241 |
> |
fclose(ambfp); |
242 |
> |
ambfp = NULL; |
243 |
> |
if (ambinp != NULL) { |
244 |
> |
fclose(ambinp); |
245 |
> |
ambinp = NULL; |
246 |
|
} |
247 |
+ |
lastpos = -1; |
248 |
+ |
} |
249 |
+ |
/* free ambient tree */ |
250 |
+ |
unloadatree(&atrunk, free); |
251 |
+ |
/* reset state variables */ |
252 |
+ |
avsum = 0.; |
253 |
+ |
navsum = 0; |
254 |
+ |
nambvals = 0; |
255 |
+ |
nambshare = 0; |
256 |
+ |
ambclock = 0; |
257 |
+ |
lastsort = 0; |
258 |
+ |
sortintvl = SORT_INTVL; |
259 |
|
} |
260 |
|
|
261 |
|
|
262 |
< |
ambient(acol, r) /* compute ambient component for ray */ |
262 |
> |
void |
263 |
> |
ambnotify(obj) /* record new modifier */ |
264 |
> |
OBJECT obj; |
265 |
> |
{ |
266 |
> |
static int hitlimit = 0; |
267 |
> |
register OBJREC *o; |
268 |
> |
register char **amblp; |
269 |
> |
|
270 |
> |
if (obj == OVOID) { /* starting over */ |
271 |
> |
ambset[0] = 0; |
272 |
> |
hitlimit = 0; |
273 |
> |
return; |
274 |
> |
} |
275 |
> |
o = objptr(obj); |
276 |
> |
if (hitlimit || !ismodifier(o->otype)) |
277 |
> |
return; |
278 |
> |
for (amblp = amblist; *amblp != NULL; amblp++) |
279 |
> |
if (!strcmp(o->oname, *amblp)) { |
280 |
> |
if (ambset[0] >= MAXASET) { |
281 |
> |
error(WARNING, "too many modifiers in ambient list"); |
282 |
> |
hitlimit++; |
283 |
> |
return; /* should this be fatal? */ |
284 |
> |
} |
285 |
> |
insertelem(ambset, obj); |
286 |
> |
return; |
287 |
> |
} |
288 |
> |
} |
289 |
> |
|
290 |
> |
|
291 |
> |
void |
292 |
> |
ambient(acol, r, nrm) /* compute ambient component for ray */ |
293 |
|
COLOR acol; |
294 |
|
register RAY *r; |
295 |
+ |
FVECT nrm; |
296 |
|
{ |
297 |
|
static int rdepth = 0; /* ambient recursion */ |
298 |
< |
double wsum; |
298 |
> |
double d, l; |
299 |
|
|
119 |
– |
rdepth++; /* increment level */ |
120 |
– |
|
300 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
301 |
|
goto dumbamb; |
302 |
|
/* check number of bounces */ |
303 |
< |
if (rdepth > ambounce) |
303 |
> |
if (rdepth >= ambounce) |
304 |
|
goto dumbamb; |
305 |
|
/* check ambient list */ |
306 |
|
if (ambincl != -1 && r->ro != NULL && |
308 |
|
goto dumbamb; |
309 |
|
|
310 |
|
if (ambacc <= FTINY) { /* no ambient storage */ |
311 |
< |
if (doambient(acol, r) == 0.0) |
311 |
> |
rdepth++; |
312 |
> |
d = doambient(acol, r, r->rweight, NULL, NULL); |
313 |
> |
rdepth--; |
314 |
> |
if (d <= FTINY) |
315 |
|
goto dumbamb; |
316 |
< |
goto done; |
316 |
> |
return; |
317 |
|
} |
318 |
+ |
|
319 |
+ |
if (tracktime) /* sort to minimize thrashing */ |
320 |
+ |
sortambvals(0); |
321 |
|
/* get ambient value */ |
322 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
323 |
< |
wsum = sumambient(acol, r, &atrunk, thescene.cuorg, thescene.cusize); |
324 |
< |
if (wsum > FTINY) |
325 |
< |
scalecolor(acol, 1.0/wsum); |
326 |
< |
else if (makeambient(acol, r) == 0.0) |
327 |
< |
goto dumbamb; |
328 |
< |
goto done; |
329 |
< |
|
323 |
> |
d = sumambient(acol, r, nrm, rdepth, |
324 |
> |
&atrunk, thescene.cuorg, thescene.cusize); |
325 |
> |
if (d > FTINY) { |
326 |
> |
scalecolor(acol, 1.0/d); |
327 |
> |
return; |
328 |
> |
} |
329 |
> |
rdepth++; /* need to cache new value */ |
330 |
> |
d = makeambient(acol, r, nrm, rdepth-1); |
331 |
> |
rdepth--; |
332 |
> |
if (d > FTINY) |
333 |
> |
return; |
334 |
|
dumbamb: /* return global value */ |
335 |
|
copycolor(acol, ambval); |
336 |
< |
done: /* must finish here! */ |
337 |
< |
rdepth--; |
336 |
> |
if (ambvwt <= 0 | navsum == 0) |
337 |
> |
return; |
338 |
> |
l = bright(ambval); /* average in computations */ |
339 |
> |
if (l > FTINY) { |
340 |
> |
d = (log(l)*(double)ambvwt + avsum) / |
341 |
> |
(double)(ambvwt + navsum); |
342 |
> |
d = exp(d) / l; |
343 |
> |
scalecolor(acol, d); /* apply color of ambval */ |
344 |
> |
} else { |
345 |
> |
d = exp( avsum / (double)navsum ); |
346 |
> |
setcolor(acol, d, d, d); /* neutral color */ |
347 |
> |
} |
348 |
|
} |
349 |
|
|
350 |
|
|
351 |
|
double |
352 |
< |
sumambient(acol, r, at, c0, s) /* get interpolated ambient value */ |
352 |
> |
sumambient(acol, r, rn, al, at, c0, s) /* get interpolated ambient value */ |
353 |
|
COLOR acol; |
354 |
|
register RAY *r; |
355 |
< |
AMBTREE *at; |
355 |
> |
FVECT rn; |
356 |
> |
int al; |
357 |
> |
AMBTREE *at; |
358 |
|
FVECT c0; |
359 |
< |
double s; |
359 |
> |
double s; |
360 |
|
{ |
361 |
< |
extern double sqrt(); |
161 |
< |
double d, e1, e2, wt, wsum; |
361 |
> |
double d, e1, e2, wt, wsum; |
362 |
|
COLOR ct; |
363 |
|
FVECT ck0; |
364 |
|
int i; |
365 |
|
register int j; |
366 |
< |
register AMBVAL *av; |
367 |
< |
/* do this node */ |
366 |
> |
register AMBVAL *av; |
367 |
> |
|
368 |
|
wsum = 0.0; |
369 |
+ |
/* do this node */ |
370 |
|
for (av = at->alist; av != NULL; av = av->next) { |
371 |
+ |
double rn_dot = -2.0; |
372 |
+ |
if (tracktime) |
373 |
+ |
av->latick = ambclock; |
374 |
|
/* |
375 |
< |
* Ray strength test. |
375 |
> |
* Ambient level test. |
376 |
|
*/ |
377 |
< |
if (av->lvl > r->rlvl || av->weight < r->rweight-FTINY) |
377 |
> |
if (av->lvl > al) /* list sorted, so this works */ |
378 |
> |
break; |
379 |
> |
if (av->weight < r->rweight-FTINY) |
380 |
|
continue; |
381 |
|
/* |
382 |
|
* Ambient radius test. |
383 |
|
*/ |
384 |
< |
e1 = 0.0; |
385 |
< |
for (j = 0; j < 3; j++) { |
386 |
< |
d = av->pos[j] - r->rop[j]; |
387 |
< |
e1 += d * d; |
388 |
< |
} |
384 |
> |
d = av->pos[0] - r->rop[0]; |
385 |
> |
e1 = d * d; |
386 |
> |
d = av->pos[1] - r->rop[1]; |
387 |
> |
e1 += d * d; |
388 |
> |
d = av->pos[2] - r->rop[2]; |
389 |
> |
e1 += d * d; |
390 |
|
e1 /= av->rad * av->rad; |
391 |
|
if (e1 > ambacc*ambacc*1.21) |
392 |
|
continue; |
393 |
|
/* |
394 |
< |
* Normal direction test. |
394 |
> |
* Direction test using closest normal. |
395 |
|
*/ |
396 |
< |
e2 = (1.0 - DOT(av->dir, r->ron)) * r->rweight; |
396 |
> |
d = DOT(av->dir, r->ron); |
397 |
> |
if (rn != r->ron) { |
398 |
> |
rn_dot = DOT(av->dir, rn); |
399 |
> |
if (rn_dot > 1.0-FTINY) |
400 |
> |
rn_dot = 1.0-FTINY; |
401 |
> |
if (rn_dot >= d-FTINY) { |
402 |
> |
d = rn_dot; |
403 |
> |
rn_dot = -2.0; |
404 |
> |
} |
405 |
> |
} |
406 |
> |
e2 = (1.0 - d) * r->rweight; |
407 |
|
if (e2 < 0.0) e2 = 0.0; |
408 |
|
if (e1 + e2 > ambacc*ambacc*1.21) |
409 |
|
continue; |
414 |
|
for (j = 0; j < 3; j++) |
415 |
|
d += (r->rop[j] - av->pos[j]) * |
416 |
|
(av->dir[j] + r->ron[j]); |
417 |
< |
if (d < -minarad) |
417 |
> |
if (d*0.5 < -minarad*ambacc-.001) |
418 |
|
continue; |
419 |
|
/* |
420 |
|
* Jittering final test reduces image artifacts. |
421 |
|
*/ |
422 |
< |
wt = sqrt(e1) + sqrt(e2); |
423 |
< |
wt *= .9 + .2*frandom(); |
424 |
< |
if (wt > ambacc) |
422 |
> |
e1 = sqrt(e1); |
423 |
> |
e2 = sqrt(e2); |
424 |
> |
wt = e1 + e2; |
425 |
> |
if (wt > ambacc*(.9+.2*urand(9015+samplendx))) |
426 |
|
continue; |
427 |
+ |
/* |
428 |
+ |
* Recompute directional error using perturbed normal |
429 |
+ |
*/ |
430 |
+ |
if (rn_dot > 0.0) { |
431 |
+ |
e2 = sqrt((1.0 - rn_dot)*r->rweight); |
432 |
+ |
wt = e1 + e2; |
433 |
+ |
} |
434 |
|
if (wt <= 1e-3) |
435 |
|
wt = 1e3; |
436 |
|
else |
437 |
|
wt = 1.0 / wt; |
438 |
|
wsum += wt; |
439 |
< |
copycolor(ct, av->val); |
439 |
> |
extambient(ct, av, r->rop, rn); |
440 |
|
scalecolor(ct, wt); |
441 |
|
addcolor(acol, ct); |
442 |
|
} |
455 |
|
break; |
456 |
|
} |
457 |
|
if (j == 3) |
458 |
< |
wsum += sumambient(acol, r, at->kid+i, ck0, s); |
458 |
> |
wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s); |
459 |
|
} |
460 |
|
return(wsum); |
461 |
|
} |
462 |
|
|
463 |
|
|
464 |
|
double |
465 |
< |
makeambient(acol, r) /* make a new ambient value */ |
465 |
> |
makeambient(acol, r, rn, al) /* make a new ambient value */ |
466 |
|
COLOR acol; |
467 |
|
register RAY *r; |
468 |
+ |
FVECT rn; |
469 |
+ |
int al; |
470 |
|
{ |
471 |
< |
AMBVAL amb; |
472 |
< |
|
473 |
< |
amb.rad = doambient(acol, r); /* compute ambient */ |
474 |
< |
if (amb.rad == 0.0) |
471 |
> |
AMBVAL amb; |
472 |
> |
FVECT gp, gd; |
473 |
> |
/* compute weight */ |
474 |
> |
amb.weight = pow(AVGREFL, (double)al); |
475 |
> |
if (r->rweight < 0.1*amb.weight) /* heuristic */ |
476 |
> |
amb.weight = r->rweight; |
477 |
> |
/* compute ambient */ |
478 |
> |
amb.rad = doambient(acol, r, amb.weight, gp, gd); |
479 |
> |
if (amb.rad <= FTINY) |
480 |
|
return(0.0); |
481 |
|
/* store it */ |
482 |
|
VCOPY(amb.pos, r->rop); |
483 |
|
VCOPY(amb.dir, r->ron); |
484 |
< |
amb.lvl = r->rlvl; |
253 |
< |
amb.weight = r->rweight; |
484 |
> |
amb.lvl = al; |
485 |
|
copycolor(amb.val, acol); |
486 |
+ |
VCOPY(amb.gpos, gp); |
487 |
+ |
VCOPY(amb.gdir, gd); |
488 |
|
/* insert into tree */ |
489 |
< |
avinsert(&amb, &atrunk, thescene.cuorg, thescene.cusize); |
490 |
< |
avsave(&amb); /* write to file */ |
489 |
> |
avsave(&amb); /* and save to file */ |
490 |
> |
if (rn != r->ron) |
491 |
> |
extambient(acol, &amb, r->rop, rn); /* texture */ |
492 |
|
return(amb.rad); |
493 |
|
} |
494 |
|
|
495 |
|
|
496 |
< |
double |
497 |
< |
doambient(acol, r) /* compute ambient component */ |
498 |
< |
COLOR acol; |
499 |
< |
register RAY *r; |
496 |
> |
void |
497 |
> |
extambient(cr, ap, pv, nv) /* extrapolate value at pv, nv */ |
498 |
> |
COLOR cr; |
499 |
> |
register AMBVAL *ap; |
500 |
> |
FVECT pv, nv; |
501 |
|
{ |
502 |
< |
extern int ambcmp(); |
503 |
< |
extern double sin(), cos(), sqrt(); |
504 |
< |
double phi, xd, yd, zd; |
270 |
< |
double b, b2; |
271 |
< |
register AMBSAMP *div; |
272 |
< |
AMBSAMP dnew; |
273 |
< |
RAY ar; |
274 |
< |
FVECT ux, uy; |
275 |
< |
double arad; |
276 |
< |
int ndivs, nt, np, ns, ne, i, j; |
277 |
< |
register int k; |
502 |
> |
FVECT v1; |
503 |
> |
register int i; |
504 |
> |
double d; |
505 |
|
|
506 |
< |
setcolor(acol, 0.0, 0.0, 0.0); |
507 |
< |
/* set number of divisions */ |
508 |
< |
nt = sqrt(ambdiv * r->rweight * 0.5) + 0.5; |
509 |
< |
np = 2 * nt; |
510 |
< |
ndivs = nt * np; |
511 |
< |
/* check first */ |
512 |
< |
if (ndivs == 0 || rayorigin(&ar, r, AMBIENT, 0.5) < 0) |
513 |
< |
return(0.0); |
514 |
< |
/* set number of super-samples */ |
515 |
< |
ns = ambssamp * r->rweight + 0.5; |
289 |
< |
if (ns > 0) { |
290 |
< |
div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP)); |
291 |
< |
if (div == NULL) |
292 |
< |
error(SYSTEM, "out of memory in doambient"); |
506 |
> |
d = 1.0; /* zeroeth order */ |
507 |
> |
/* gradient due to translation */ |
508 |
> |
for (i = 0; i < 3; i++) |
509 |
> |
d += ap->gpos[i]*(pv[i]-ap->pos[i]); |
510 |
> |
/* gradient due to rotation */ |
511 |
> |
VCROSS(v1, ap->dir, nv); |
512 |
> |
d += DOT(ap->gdir, v1); |
513 |
> |
if (d <= 0.0) { |
514 |
> |
setcolor(cr, 0.0, 0.0, 0.0); |
515 |
> |
return; |
516 |
|
} |
517 |
< |
/* make axes */ |
518 |
< |
uy[0] = uy[1] = uy[2] = 0.0; |
296 |
< |
for (k = 0; k < 3; k++) |
297 |
< |
if (r->ron[k] < 0.6 && r->ron[k] > -0.6) |
298 |
< |
break; |
299 |
< |
uy[k] = 1.0; |
300 |
< |
fcross(ux, r->ron, uy); |
301 |
< |
normalize(ux); |
302 |
< |
fcross(uy, ux, r->ron); |
303 |
< |
/* sample divisions */ |
304 |
< |
arad = 0.0; |
305 |
< |
ne = 0; |
306 |
< |
for (i = 0; i < nt; i++) |
307 |
< |
for (j = 0; j < np; j++) { |
308 |
< |
rayorigin(&ar, r, AMBIENT, 0.5); /* pretested */ |
309 |
< |
zd = sqrt((i+frandom())/nt); |
310 |
< |
phi = 2.0*PI * (j+frandom())/np; |
311 |
< |
xd = cos(phi) * zd; |
312 |
< |
yd = sin(phi) * zd; |
313 |
< |
zd = sqrt(1.0 - zd*zd); |
314 |
< |
for (k = 0; k < 3; k++) |
315 |
< |
ar.rdir[k] = xd*ux[k]+yd*uy[k]+zd*r->ron[k]; |
316 |
< |
rayvalue(&ar); |
317 |
< |
if (ar.rot < FHUGE) |
318 |
< |
arad += 1.0 / ar.rot; |
319 |
< |
if (ns > 0) { /* save division */ |
320 |
< |
div[ne].k = 0.0; |
321 |
< |
copycolor(div[ne].v, ar.rcol); |
322 |
< |
div[ne].n = 0; |
323 |
< |
div[ne].t = i; div[ne].p = j; |
324 |
< |
/* sum errors */ |
325 |
< |
b = bright(ar.rcol); |
326 |
< |
if (i > 0) { /* from above */ |
327 |
< |
b2 = bright(div[ne-np].v) - b; |
328 |
< |
b2 *= b2 * 0.25; |
329 |
< |
div[ne].k += b2; |
330 |
< |
div[ne].n++; |
331 |
< |
div[ne-np].k += b2; |
332 |
< |
div[ne-np].n++; |
333 |
< |
} |
334 |
< |
if (j > 0) { /* from behind */ |
335 |
< |
b2 = bright(div[ne-1].v) - b; |
336 |
< |
b2 *= b2 * 0.25; |
337 |
< |
div[ne].k += b2; |
338 |
< |
div[ne].n++; |
339 |
< |
div[ne-1].k += b2; |
340 |
< |
div[ne-1].n++; |
341 |
< |
} |
342 |
< |
if (j == np-1) { /* around */ |
343 |
< |
b2 = bright(div[ne-(np-1)].v) - b; |
344 |
< |
b2 *= b2 * 0.25; |
345 |
< |
div[ne].k += b2; |
346 |
< |
div[ne].n++; |
347 |
< |
div[ne-(np-1)].k += b2; |
348 |
< |
div[ne-(np-1)].n++; |
349 |
< |
} |
350 |
< |
ne++; |
351 |
< |
} else |
352 |
< |
addcolor(acol, ar.rcol); |
353 |
< |
} |
354 |
< |
for (k = 0; k < ne; k++) { /* compute errors */ |
355 |
< |
if (div[k].n > 1) |
356 |
< |
div[k].k /= div[k].n; |
357 |
< |
div[k].n = 1; |
358 |
< |
} |
359 |
< |
/* sort the divisions */ |
360 |
< |
qsort(div, ne, sizeof(AMBSAMP), ambcmp); |
361 |
< |
/* skim excess */ |
362 |
< |
while (ne > ns) { |
363 |
< |
ne--; |
364 |
< |
addcolor(acol, div[ne].v); |
365 |
< |
} |
366 |
< |
/* super-sample */ |
367 |
< |
for (i = ns; i > 0; i--) { |
368 |
< |
rayorigin(&ar, r, AMBIENT, 0.5); /* pretested */ |
369 |
< |
zd = sqrt((div[0].t+frandom())/nt); |
370 |
< |
phi = 2.0*PI * (div[0].p+frandom())/np; |
371 |
< |
xd = cos(phi) * zd; |
372 |
< |
yd = sin(phi) * zd; |
373 |
< |
zd = sqrt(1.0 - zd*zd); |
374 |
< |
for (k = 0; k < 3; k++) |
375 |
< |
ar.rdir[k] = xd*ux[k]+yd*uy[k]+zd*r->ron[k]; |
376 |
< |
rayvalue(&ar); |
377 |
< |
if (ar.rot < FHUGE) |
378 |
< |
arad += 1.0 / ar.rot; |
379 |
< |
/* recompute error */ |
380 |
< |
copycolor(dnew.v, div[0].v); |
381 |
< |
addcolor(dnew.v, ar.rcol); |
382 |
< |
dnew.n = div[0].n + 1; |
383 |
< |
dnew.t = div[0].t; dnew.p = div[0].p; |
384 |
< |
b2 = bright(dnew.v)/dnew.n - bright(ar.rcol); |
385 |
< |
b2 = b2*b2 + div[0].k*(div[0].n*div[0].n); |
386 |
< |
dnew.k = b2/(dnew.n*dnew.n); |
387 |
< |
/* reinsert */ |
388 |
< |
for (k = 0; k < ne-1 && dnew.k < div[k+1].k; k++) |
389 |
< |
bcopy(&div[k+1], &div[k], sizeof(AMBSAMP)); |
390 |
< |
bcopy(&dnew, &div[k], sizeof(AMBSAMP)); |
391 |
< |
|
392 |
< |
if (ne >= i) { /* extract darkest division */ |
393 |
< |
ne--; |
394 |
< |
if (div[ne].n > 1) |
395 |
< |
scalecolor(div[ne].v, 1.0/div[ne].n); |
396 |
< |
addcolor(acol, div[ne].v); |
397 |
< |
} |
398 |
< |
} |
399 |
< |
scalecolor(acol, 1.0/ndivs); |
400 |
< |
if (arad <= FTINY) |
401 |
< |
arad = FHUGE; |
402 |
< |
else |
403 |
< |
arad = (ndivs+ns) / arad / sqrt(r->rweight); |
404 |
< |
if (arad > maxarad) |
405 |
< |
arad = maxarad; |
406 |
< |
else if (arad < minarad) |
407 |
< |
arad = minarad; |
408 |
< |
if (ns > 0) |
409 |
< |
free((char *)div); |
410 |
< |
return(arad); |
517 |
> |
copycolor(cr, ap->val); |
518 |
> |
scalecolor(cr, d); |
519 |
|
} |
520 |
|
|
521 |
|
|
522 |
< |
static int |
523 |
< |
ambcmp(d1, d2) /* decreasing order */ |
524 |
< |
AMBSAMP *d1, *d2; |
522 |
> |
static void |
523 |
> |
initambfile(creat) /* initialize ambient file */ |
524 |
> |
int creat; |
525 |
|
{ |
526 |
< |
if (d1->k < d2->k) |
527 |
< |
return(1); |
528 |
< |
if (d1->k > d2->k) |
529 |
< |
return(-1); |
530 |
< |
return(0); |
526 |
> |
extern char *progname, *octname; |
527 |
> |
static char *mybuf = NULL; |
528 |
> |
|
529 |
> |
#ifdef F_SETLKW |
530 |
> |
aflock(creat ? F_WRLCK : F_RDLCK); |
531 |
> |
#endif |
532 |
> |
#ifdef MSDOS |
533 |
> |
setmode(fileno(ambfp), O_BINARY); |
534 |
> |
#endif |
535 |
> |
if (mybuf == NULL) |
536 |
> |
mybuf = (char *)bmalloc(BUFSIZ+8); |
537 |
> |
setbuf(ambfp, mybuf); |
538 |
> |
if (creat) { /* new file */ |
539 |
> |
newheader("RADIANCE", ambfp); |
540 |
> |
fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ", |
541 |
> |
progname, colval(ambval,RED), |
542 |
> |
colval(ambval,GRN), colval(ambval,BLU), |
543 |
> |
ambvwt, ambounce, ambacc); |
544 |
> |
fprintf(ambfp, "-ad %d -as %d -ar %d ", |
545 |
> |
ambdiv, ambssamp, ambres); |
546 |
> |
if (octname != NULL) |
547 |
> |
printargs(1, &octname, ambfp); |
548 |
> |
else |
549 |
> |
fputc('\n', ambfp); |
550 |
> |
fprintf(ambfp, "SOFTWARE= %s\n", VersionID); |
551 |
> |
fputnow(ambfp); |
552 |
> |
fputformat(AMBFMT, ambfp); |
553 |
> |
putc('\n', ambfp); |
554 |
> |
putambmagic(ambfp); |
555 |
> |
} else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp)) |
556 |
> |
error(USER, "bad ambient file"); |
557 |
|
} |
558 |
|
|
559 |
|
|
560 |
< |
static |
561 |
< |
avsave(av) /* save an ambient value */ |
562 |
< |
AMBVAL *av; |
560 |
> |
static void |
561 |
> |
avsave(av) /* insert and save an ambient value */ |
562 |
> |
AMBVAL *av; |
563 |
|
{ |
564 |
< |
#ifdef AMBFLUSH |
431 |
< |
static int nunflshed = 0; |
432 |
< |
#endif |
564 |
> |
avinsert(avstore(av)); |
565 |
|
if (ambfp == NULL) |
566 |
|
return; |
567 |
< |
if (fwrite(av, sizeof(AMBVAL), 1, ambfp) != 1) |
567 |
> |
if (writambval(av, ambfp) < 0) |
568 |
|
goto writerr; |
569 |
< |
#ifdef AMBFLUSH |
570 |
< |
if (++nunflshed >= AMBFLUSH) { |
439 |
< |
if (fflush(ambfp) == EOF) |
569 |
> |
if (++nunflshed >= AMBFLUSH) |
570 |
> |
if (ambsync() == EOF) |
571 |
|
goto writerr; |
441 |
– |
nunflshed = 0; |
442 |
– |
} |
443 |
– |
#endif |
572 |
|
return; |
573 |
|
writerr: |
574 |
< |
error(SYSTEM, "error writing ambient file"); |
574 |
> |
error(SYSTEM, "error writing to ambient file"); |
575 |
|
} |
576 |
|
|
577 |
|
|
578 |
< |
static |
579 |
< |
avinsert(aval, at, c0, s) /* insert ambient value in a tree */ |
580 |
< |
AMBVAL *aval; |
453 |
< |
register AMBTREE *at; |
454 |
< |
FVECT c0; |
455 |
< |
double s; |
578 |
> |
static AMBVAL * |
579 |
> |
avstore(aval) /* allocate memory and store aval */ |
580 |
> |
register AMBVAL *aval; |
581 |
|
{ |
582 |
+ |
register AMBVAL *av; |
583 |
+ |
double d; |
584 |
+ |
|
585 |
+ |
if ((av = newambval()) == NULL) |
586 |
+ |
error(SYSTEM, "out of memory in avstore"); |
587 |
+ |
copystruct(av, aval); |
588 |
+ |
av->latick = ambclock; |
589 |
+ |
av->next = NULL; |
590 |
+ |
nambvals++; |
591 |
+ |
d = bright(av->val); |
592 |
+ |
if (d > FTINY) { /* add to log sum for averaging */ |
593 |
+ |
avsum += log(d); |
594 |
+ |
navsum++; |
595 |
+ |
} |
596 |
+ |
return(av); |
597 |
+ |
} |
598 |
+ |
|
599 |
+ |
|
600 |
+ |
#define ATALLOCSZ 512 /* #/8 trees to allocate at once */ |
601 |
+ |
|
602 |
+ |
static AMBTREE *atfreelist = NULL; /* free ambient tree structures */ |
603 |
+ |
|
604 |
+ |
|
605 |
+ |
static AMBTREE * |
606 |
+ |
newambtree() /* allocate 8 ambient tree structs */ |
607 |
+ |
{ |
608 |
+ |
register AMBTREE *atp, *upperlim; |
609 |
+ |
|
610 |
+ |
if (atfreelist == NULL) { /* get more nodes */ |
611 |
+ |
atfreelist = (AMBTREE *)malloc(ATALLOCSZ*8*sizeof(AMBTREE)); |
612 |
+ |
if (atfreelist == NULL) |
613 |
+ |
return(NULL); |
614 |
+ |
/* link new free list */ |
615 |
+ |
upperlim = atfreelist + 8*(ATALLOCSZ-1); |
616 |
+ |
for (atp = atfreelist; atp < upperlim; atp += 8) |
617 |
+ |
atp->kid = atp + 8; |
618 |
+ |
atp->kid = NULL; |
619 |
+ |
} |
620 |
+ |
atp = atfreelist; |
621 |
+ |
atfreelist = atp->kid; |
622 |
+ |
bzero((char *)atp, 8*sizeof(AMBTREE)); |
623 |
+ |
return(atp); |
624 |
+ |
} |
625 |
+ |
|
626 |
+ |
|
627 |
+ |
static void |
628 |
+ |
freeambtree(atp) /* free 8 ambient tree structs */ |
629 |
+ |
AMBTREE *atp; |
630 |
+ |
{ |
631 |
+ |
atp->kid = atfreelist; |
632 |
+ |
atfreelist = atp; |
633 |
+ |
} |
634 |
+ |
|
635 |
+ |
|
636 |
+ |
static void |
637 |
+ |
avinsert(av) /* insert ambient value in our tree */ |
638 |
+ |
register AMBVAL *av; |
639 |
+ |
{ |
640 |
+ |
register AMBTREE *at; |
641 |
+ |
register AMBVAL *ap; |
642 |
+ |
AMBVAL avh; |
643 |
|
FVECT ck0; |
644 |
+ |
double s; |
645 |
|
int branch; |
459 |
– |
register AMBVAL *av; |
646 |
|
register int i; |
647 |
|
|
648 |
< |
if ((av = newambval()) == NULL) |
649 |
< |
goto memerr; |
650 |
< |
bcopy(aval, av, sizeof(AMBVAL)); |
651 |
< |
VCOPY(ck0, c0); |
648 |
> |
if (av->rad <= FTINY) |
649 |
> |
error(CONSISTENCY, "zero ambient radius in avinsert"); |
650 |
> |
at = &atrunk; |
651 |
> |
VCOPY(ck0, thescene.cuorg); |
652 |
> |
s = thescene.cusize; |
653 |
|
while (s*(OCTSCALE/2) > av->rad*ambacc) { |
654 |
|
if (at->kid == NULL) |
655 |
|
if ((at->kid = newambtree()) == NULL) |
656 |
< |
goto memerr; |
656 |
> |
error(SYSTEM, "out of memory in avinsert"); |
657 |
|
s *= 0.5; |
658 |
|
branch = 0; |
659 |
|
for (i = 0; i < 3; i++) |
663 |
|
} |
664 |
|
at = at->kid + branch; |
665 |
|
} |
666 |
< |
av->next = at->alist; |
667 |
< |
at->alist = av; |
668 |
< |
return; |
669 |
< |
memerr: |
670 |
< |
error(SYSTEM, "out of memory in avinsert"); |
666 |
> |
avh.next = at->alist; /* order by increasing level */ |
667 |
> |
for (ap = &avh; ap->next != NULL; ap = ap->next) |
668 |
> |
if (ap->next->lvl >= av->lvl) |
669 |
> |
break; |
670 |
> |
av->next = ap->next; |
671 |
> |
ap->next = av; |
672 |
> |
at->alist = avh.next; |
673 |
|
} |
674 |
+ |
|
675 |
+ |
|
676 |
+ |
static void |
677 |
+ |
unloadatree(at, f) /* unload an ambient value tree */ |
678 |
+ |
register AMBTREE *at; |
679 |
+ |
void (*f)(); |
680 |
+ |
{ |
681 |
+ |
register AMBVAL *av; |
682 |
+ |
register int i; |
683 |
+ |
/* transfer values at this node */ |
684 |
+ |
for (av = at->alist; av != NULL; av = at->alist) { |
685 |
+ |
at->alist = av->next; |
686 |
+ |
(*f)(av); |
687 |
+ |
} |
688 |
+ |
if (at->kid == NULL) |
689 |
+ |
return; |
690 |
+ |
for (i = 0; i < 8; i++) /* transfer and free children */ |
691 |
+ |
unloadatree(at->kid+i, f); |
692 |
+ |
freeambtree(at->kid); |
693 |
+ |
at->kid = NULL; |
694 |
+ |
} |
695 |
+ |
|
696 |
+ |
|
697 |
+ |
static struct avl { |
698 |
+ |
AMBVAL *p; |
699 |
+ |
unsigned long t; |
700 |
+ |
} *avlist1; /* ambient value list with ticks */ |
701 |
+ |
static AMBVAL **avlist2; /* memory positions for sorting */ |
702 |
+ |
static int i_avlist; /* index for lists */ |
703 |
+ |
|
704 |
+ |
|
705 |
+ |
static int |
706 |
+ |
av2list(av) |
707 |
+ |
register AMBVAL *av; |
708 |
+ |
{ |
709 |
+ |
#ifdef DEBUG |
710 |
+ |
if (i_avlist >= nambvals) |
711 |
+ |
error(CONSISTENCY, "too many ambient values in av2list1"); |
712 |
+ |
#endif |
713 |
+ |
avlist1[i_avlist].p = avlist2[i_avlist] = av; |
714 |
+ |
avlist1[i_avlist++].t = av->latick; |
715 |
+ |
} |
716 |
+ |
|
717 |
+ |
|
718 |
+ |
static int |
719 |
+ |
alatcmp(av1, av2) /* compare ambient values for MRA */ |
720 |
+ |
struct avl *av1, *av2; |
721 |
+ |
{ |
722 |
+ |
register long lc = av2->t - av1->t; |
723 |
+ |
return(lc<0 ? -1 : lc>0 ? 1 : 0); |
724 |
+ |
} |
725 |
+ |
|
726 |
+ |
|
727 |
+ |
/* GW NOTE 2002/10/3: |
728 |
+ |
* I used to compare AMBVAL pointers, but found that this was the |
729 |
+ |
* cause of a serious consistency error with gcc, since the optimizer |
730 |
+ |
* uses some dangerous trick in pointer subtraction that |
731 |
+ |
* assumes pointers differ by exact struct size increments. |
732 |
+ |
*/ |
733 |
+ |
static int |
734 |
+ |
aposcmp(avp1, avp2) /* compare ambient value positions */ |
735 |
+ |
const void *avp1, *avp2; |
736 |
+ |
{ |
737 |
+ |
register long diff = *(char * const *)avp1 - *(char * const *)avp2; |
738 |
+ |
if (diff < 0) |
739 |
+ |
return(-1); |
740 |
+ |
return(diff > 0); |
741 |
+ |
} |
742 |
+ |
|
743 |
+ |
#if 1 |
744 |
+ |
static int |
745 |
+ |
avlmemi(avaddr) /* find list position from address */ |
746 |
+ |
AMBVAL *avaddr; |
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 |
+ |
|
762 |
+ |
static void |
763 |
+ |
sortambvals(always) /* resort ambient values */ |
764 |
+ |
int always; |
765 |
+ |
{ |
766 |
+ |
AMBTREE oldatrunk; |
767 |
+ |
AMBVAL tav, *tap, *pnext; |
768 |
+ |
register int i, j; |
769 |
+ |
/* see if it's time yet */ |
770 |
+ |
if (!always && (ambclock++ < lastsort+sortintvl || |
771 |
+ |
nambvals < SORT_THRESH)) |
772 |
+ |
return; |
773 |
+ |
/* |
774 |
+ |
* The idea here is to minimize memory thrashing |
775 |
+ |
* in VM systems by improving reference locality. |
776 |
+ |
* We do this by periodically sorting our stored ambient |
777 |
+ |
* values in memory in order of most recently to least |
778 |
+ |
* recently accessed. This ordering was chosen so that new |
779 |
+ |
* ambient values (which tend to be less important) go into |
780 |
+ |
* higher memory with the infrequently accessed values. |
781 |
+ |
* Since we expect our values to need sorting less |
782 |
+ |
* frequently as the process continues, we double our |
783 |
+ |
* waiting interval after each call. |
784 |
+ |
* This routine is also called by setambacc() with |
785 |
+ |
* the "always" parameter set to 1 so that the ambient |
786 |
+ |
* tree will be rebuilt with the new accuracy parameter. |
787 |
+ |
*/ |
788 |
+ |
if (tracktime) { /* allocate pointer arrays to sort */ |
789 |
+ |
avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); |
790 |
+ |
avlist1 = (struct avl *)malloc(nambvals*sizeof(struct avl)); |
791 |
+ |
} else { |
792 |
+ |
avlist2 = NULL; |
793 |
+ |
avlist1 = NULL; |
794 |
+ |
} |
795 |
+ |
if (avlist1 == NULL) { /* no time tracking -- rebuild tree? */ |
796 |
+ |
if (avlist2 != NULL) |
797 |
+ |
free((void *)avlist2); |
798 |
+ |
if (always) { /* rebuild without sorting */ |
799 |
+ |
copystruct(&oldatrunk, &atrunk); |
800 |
+ |
atrunk.alist = NULL; |
801 |
+ |
atrunk.kid = NULL; |
802 |
+ |
unloadatree(&oldatrunk, avinsert); |
803 |
+ |
} |
804 |
+ |
} else { /* sort memory by last access time */ |
805 |
+ |
/* |
806 |
+ |
* Sorting memory is tricky because it isn't contiguous. |
807 |
+ |
* We have to sort an array of pointers by MRA and also |
808 |
+ |
* by memory position. We then copy values in "loops" |
809 |
+ |
* to minimize memory hits. Nevertheless, we will visit |
810 |
+ |
* everyone at least twice, and this is an expensive process |
811 |
+ |
* when we're thrashing, which is when we need to do it. |
812 |
+ |
*/ |
813 |
+ |
#ifdef DEBUG |
814 |
+ |
sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...", |
815 |
+ |
nambvals, ambclock); |
816 |
+ |
eputs(errmsg); |
817 |
+ |
#endif |
818 |
+ |
i_avlist = 0; |
819 |
+ |
unloadatree(&atrunk, av2list); /* empty current tree */ |
820 |
+ |
#ifdef DEBUG |
821 |
+ |
if (i_avlist < nambvals) |
822 |
+ |
error(CONSISTENCY, "missing ambient values in sortambvals"); |
823 |
+ |
#endif |
824 |
+ |
qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp); |
825 |
+ |
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
826 |
+ |
for (i = 0; i < nambvals; i++) { |
827 |
+ |
if (avlist1[i].p == NULL) |
828 |
+ |
continue; |
829 |
+ |
tap = avlist2[i]; |
830 |
+ |
copystruct(&tav, tap); |
831 |
+ |
for (j = i; (pnext = avlist1[j].p) != tap; |
832 |
+ |
j = avlmemi(pnext)) { |
833 |
+ |
copystruct(avlist2[j], pnext); |
834 |
+ |
avinsert(avlist2[j]); |
835 |
+ |
avlist1[j].p = NULL; |
836 |
+ |
} |
837 |
+ |
copystruct(avlist2[j], &tav); |
838 |
+ |
avinsert(avlist2[j]); |
839 |
+ |
avlist1[j].p = NULL; |
840 |
+ |
} |
841 |
+ |
free((void *)avlist1); |
842 |
+ |
free((void *)avlist2); |
843 |
+ |
/* compute new sort interval */ |
844 |
+ |
sortintvl = ambclock - lastsort; |
845 |
+ |
if (sortintvl >= MAX_SORT_INTVL/2) |
846 |
+ |
sortintvl = MAX_SORT_INTVL; |
847 |
+ |
else |
848 |
+ |
sortintvl <<= 1; /* wait twice as long next */ |
849 |
+ |
#ifdef DEBUG |
850 |
+ |
eputs("done\n"); |
851 |
+ |
#endif |
852 |
+ |
} |
853 |
+ |
if (ambclock >= MAXACLOCK) |
854 |
+ |
ambclock = MAXACLOCK/2; |
855 |
+ |
lastsort = ambclock; |
856 |
+ |
} |
857 |
+ |
|
858 |
+ |
|
859 |
+ |
#ifdef F_SETLKW |
860 |
+ |
|
861 |
+ |
static void |
862 |
+ |
aflock(typ) /* lock/unlock ambient file */ |
863 |
+ |
int typ; |
864 |
+ |
{ |
865 |
+ |
static struct flock fls; /* static so initialized to zeroes */ |
866 |
+ |
|
867 |
+ |
fls.l_type = typ; |
868 |
+ |
if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0) |
869 |
+ |
error(SYSTEM, "cannot (un)lock ambient file"); |
870 |
+ |
} |
871 |
+ |
|
872 |
+ |
|
873 |
+ |
int |
874 |
+ |
ambsync() /* synchronize ambient file */ |
875 |
+ |
{ |
876 |
+ |
long flen; |
877 |
+ |
AMBVAL avs; |
878 |
+ |
register int n; |
879 |
+ |
|
880 |
+ |
if (nunflshed == 0) |
881 |
+ |
return(0); |
882 |
+ |
if (lastpos < 0) /* initializing (locked in initambfile) */ |
883 |
+ |
goto syncend; |
884 |
+ |
/* gain exclusive access */ |
885 |
+ |
aflock(F_WRLCK); |
886 |
+ |
/* see if file has grown */ |
887 |
+ |
if ((flen = lseek(fileno(ambfp), (off_t)0L, 2)) < 0) |
888 |
+ |
goto seekerr; |
889 |
+ |
if (n = flen - lastpos) { /* file has grown */ |
890 |
+ |
if (ambinp == NULL) { /* use duplicate filedes */ |
891 |
+ |
ambinp = fdopen(dup(fileno(ambfp)), "r"); |
892 |
+ |
if (ambinp == NULL) |
893 |
+ |
error(SYSTEM, "fdopen failed in ambsync"); |
894 |
+ |
} |
895 |
+ |
if (fseek(ambinp, lastpos, 0) < 0) |
896 |
+ |
goto seekerr; |
897 |
+ |
while (n >= AMBVALSIZ) { /* load contributed values */ |
898 |
+ |
if (!readambval(&avs, ambinp)) { |
899 |
+ |
sprintf(errmsg, |
900 |
+ |
"ambient file \"%s\" corrupted near character %ld", |
901 |
+ |
ambfile, flen - n); |
902 |
+ |
error(WARNING, errmsg); |
903 |
+ |
break; |
904 |
+ |
} |
905 |
+ |
avinsert(avstore(&avs)); |
906 |
+ |
n -= AMBVALSIZ; |
907 |
+ |
} |
908 |
+ |
/*** seek always as safety measure |
909 |
+ |
if (n) ***/ /* alignment */ |
910 |
+ |
if (lseek(fileno(ambfp), (off_t)(flen-n), 0) < 0) |
911 |
+ |
goto seekerr; |
912 |
+ |
} |
913 |
+ |
#ifdef DEBUG |
914 |
+ |
if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) { |
915 |
+ |
sprintf(errmsg, "ambient file buffer at %d rather than %d", |
916 |
+ |
ambfp->_ptr - ambfp->_base, |
917 |
+ |
nunflshed*AMBVALSIZ); |
918 |
+ |
error(CONSISTENCY, errmsg); |
919 |
+ |
} |
920 |
+ |
#endif |
921 |
+ |
syncend: |
922 |
+ |
n = fflush(ambfp); /* calls write() at last */ |
923 |
+ |
if ((lastpos = lseek(fileno(ambfp), (off_t)0L, 1)) < 0) |
924 |
+ |
goto seekerr; |
925 |
+ |
aflock(F_UNLCK); /* release file */ |
926 |
+ |
nunflshed = 0; |
927 |
+ |
return(n); |
928 |
+ |
seekerr: |
929 |
+ |
error(SYSTEM, "seek failed in ambsync"); |
930 |
+ |
} |
931 |
+ |
|
932 |
+ |
#else |
933 |
+ |
|
934 |
+ |
int |
935 |
+ |
ambsync() /* flush ambient file */ |
936 |
+ |
{ |
937 |
+ |
if (nunflshed == 0) |
938 |
+ |
return(0); |
939 |
+ |
nunflshed = 0; |
940 |
+ |
return(fflush(ambfp)); |
941 |
+ |
} |
942 |
+ |
|
943 |
+ |
#endif |