| 1 |
< |
/* Copyright (c) 1993 Regents of the University of California */ |
| 1 |
> |
/* Copyright (c) 1995 Regents of the University of California */ |
| 2 |
|
|
| 3 |
|
#ifndef lint |
| 4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
| 18 |
|
|
| 19 |
|
#include "random.h" |
| 20 |
|
|
| 21 |
< |
#define OCTSCALE 0.5 /* ceil((valid rad.)/(cube size)) */ |
| 21 |
> |
#ifndef OCTSCALE |
| 22 |
> |
#define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */ |
| 23 |
> |
#endif |
| 24 |
> |
#ifndef AMBVWT |
| 25 |
> |
#define AMBVWT 250 /* relative ambient value weight (# calcs) */ |
| 26 |
> |
#endif |
| 27 |
|
|
| 28 |
|
typedef struct ambtree { |
| 29 |
|
AMBVAL *alist; /* ambient value list */ |
| 32 |
|
|
| 33 |
|
extern CUBE thescene; /* contains space boundaries */ |
| 34 |
|
|
| 35 |
< |
extern char *shm_boundary; /* shared memory boundary */ |
| 35 |
> |
extern char *shm_boundary; /* memory sharing boundary */ |
| 36 |
|
|
| 37 |
|
#define MAXASET 511 /* maximum number of elements in ambient set */ |
| 38 |
|
OBJECT ambset[MAXASET+1]={0}; /* ambient include/exclude set */ |
| 47 |
|
|
| 48 |
|
#ifndef SORT_THRESH |
| 49 |
|
#ifdef BIGMEM |
| 50 |
< |
#define SORT_THRESH (6*(1L<<20)/sizeof(AMBVAL)) |
| 50 |
> |
#define SORT_THRESH ((9L<<20)/sizeof(AMBVAL)) |
| 51 |
|
#else |
| 52 |
< |
#define SORT_THRESH (2*(1L<<20)/sizeof(AMBVAL)) |
| 52 |
> |
#define SORT_THRESH ((3L<<20)/sizeof(AMBVAL)) |
| 53 |
|
#endif |
| 54 |
|
#endif |
| 55 |
|
#ifndef SORT_INTVL |
| 56 |
< |
#define SORT_INTVL (SORT_THRESH*2) |
| 56 |
> |
#define SORT_INTVL (SORT_THRESH*256) |
| 57 |
|
#endif |
| 58 |
+ |
#ifndef MAX_SORT_INTVL |
| 59 |
+ |
#define MAX_SORT_INTVL (SORT_INTVL<<4) |
| 60 |
+ |
#endif |
| 61 |
|
|
| 62 |
< |
static long ambclock = 0; /* ambient access clock */ |
| 63 |
< |
static int nambvals = 0; /* number of stored ambient values */ |
| 64 |
< |
static long lastsort = 0; /* time of last value sort */ |
| 62 |
> |
static COLOR avsum = BLKCOLOR; /* computed ambient value sum */ |
| 63 |
> |
static unsigned int nambvals = 0; /* number of computed ambient values */ |
| 64 |
> |
static unsigned long ambclock = 0; /* ambient access clock */ |
| 65 |
> |
static unsigned long lastsort = 0; /* time of last value sort */ |
| 66 |
|
static long sortintvl = SORT_INTVL; /* time until next sort */ |
| 67 |
|
|
| 68 |
|
#define MAXACLOCK (1L<<30) /* clock turnover value */ |
| 69 |
+ |
/* |
| 70 |
+ |
* Track access times unless we are sharing ambient values |
| 71 |
+ |
* through memory on a multiprocessor, when we want to avoid |
| 72 |
+ |
* claiming our own memory (copy on write). |
| 73 |
+ |
*/ |
| 74 |
|
#define tracktime (shm_boundary == NULL || ambfp == NULL) |
| 61 |
– |
#define need2sort (ambclock > lastsort+sortintvl && \ |
| 62 |
– |
nambvals > SORT_THRESH) |
| 75 |
|
|
| 76 |
|
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
| 77 |
|
|
| 95 |
|
maxarad = thescene.cusize / 2.0; |
| 96 |
|
} else { |
| 97 |
|
minarad = thescene.cusize / ar; |
| 98 |
< |
maxarad = 16 * minarad; /* heuristic */ |
| 98 |
> |
maxarad = 64 * minarad; /* heuristic */ |
| 99 |
|
if (maxarad > thescene.cusize / 2.0) |
| 100 |
|
maxarad = thescene.cusize / 2.0; |
| 101 |
|
} |
| 109 |
|
setambacc(newa) /* set ambient accuracy */ |
| 110 |
|
double newa; |
| 111 |
|
{ |
| 112 |
< |
static double oldambacc = -1.0; |
| 112 |
> |
double ambdiff; |
| 113 |
|
|
| 114 |
< |
ambacc = newa < 0.0 ? 0.0 : newa; /* may be done already */ |
| 115 |
< |
if (oldambacc < -FTINY) |
| 116 |
< |
oldambacc = ambacc; /* do nothing first call */ |
| 117 |
< |
if (fabs(newa - oldambacc) < 0.01) |
| 118 |
< |
return; /* insignificant -- don't bother */ |
| 107 |
< |
if (ambacc <= FTINY) |
| 108 |
< |
return; /* cannot build new tree */ |
| 109 |
< |
/* else need to rebuild tree */ |
| 110 |
< |
sortambvals(1); |
| 111 |
< |
oldambacc = ambacc; /* remeber setting for next call */ |
| 114 |
> |
if (newa < 0.0) |
| 115 |
> |
newa = 0.0; |
| 116 |
> |
ambdiff = fabs(newa - ambacc); |
| 117 |
> |
if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0) |
| 118 |
> |
sortambvals(1); /* rebuild tree */ |
| 119 |
|
} |
| 120 |
|
|
| 121 |
|
|
| 197 |
|
rdepth++; |
| 198 |
|
d = doambient(acol, r, r->rweight, NULL, NULL); |
| 199 |
|
rdepth--; |
| 200 |
< |
if (d == 0.0) |
| 200 |
> |
if (d <= FTINY) |
| 201 |
|
goto dumbamb; |
| 202 |
|
return; |
| 203 |
|
} |
| 204 |
+ |
/* resort memory? */ |
| 205 |
+ |
sortambvals(0); |
| 206 |
|
/* get ambient value */ |
| 198 |
– |
if (need2sort) |
| 199 |
– |
sortambvals(0); |
| 207 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
| 208 |
|
d = sumambient(acol, r, rdepth, |
| 209 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
| 210 |
< |
if (d > FTINY) |
| 210 |
> |
if (d > FTINY) { |
| 211 |
|
scalecolor(acol, 1.0/d); |
| 212 |
< |
else { |
| 206 |
< |
d = makeambient(acol, r, rdepth++); |
| 207 |
< |
rdepth--; |
| 212 |
> |
return; |
| 213 |
|
} |
| 214 |
+ |
rdepth++; /* need to cache new value */ |
| 215 |
+ |
d = makeambient(acol, r, rdepth-1); |
| 216 |
+ |
rdepth--; |
| 217 |
|
if (d > FTINY) |
| 218 |
|
return; |
| 219 |
|
dumbamb: /* return global value */ |
| 220 |
|
copycolor(acol, ambval); |
| 221 |
+ |
#if AMBVWT |
| 222 |
+ |
if (nambvals == 0) |
| 223 |
+ |
return; |
| 224 |
+ |
scalecolor(acol, (double)AMBVWT); |
| 225 |
+ |
addcolor(acol, avsum); /* average in computations */ |
| 226 |
+ |
d = 1.0/(AMBVWT+nambvals); |
| 227 |
+ |
scalecolor(acol, d); |
| 228 |
+ |
#endif |
| 229 |
|
} |
| 230 |
|
|
| 231 |
|
|
| 244 |
|
int i; |
| 245 |
|
register int j; |
| 246 |
|
register AMBVAL *av; |
| 247 |
< |
/* do this node */ |
| 247 |
> |
|
| 248 |
|
wsum = 0.0; |
| 249 |
+ |
/* do this node */ |
| 250 |
|
for (av = at->alist; av != NULL; av = av->next) { |
| 251 |
|
if (tracktime) |
| 252 |
|
av->latick = ambclock++; |
| 260 |
|
/* |
| 261 |
|
* Ambient radius test. |
| 262 |
|
*/ |
| 263 |
< |
e1 = 0.0; |
| 264 |
< |
for (j = 0; j < 3; j++) { |
| 265 |
< |
d = av->pos[j] - r->rop[j]; |
| 266 |
< |
e1 += d * d; |
| 267 |
< |
} |
| 263 |
> |
d = av->pos[0] - r->rop[0]; |
| 264 |
> |
e1 = d * d; |
| 265 |
> |
d = av->pos[1] - r->rop[1]; |
| 266 |
> |
e1 += d * d; |
| 267 |
> |
d = av->pos[2] - r->rop[2]; |
| 268 |
> |
e1 += d * d; |
| 269 |
|
e1 /= av->rad * av->rad; |
| 270 |
|
if (e1 > ambacc*ambacc*1.21) |
| 271 |
|
continue; |
| 289 |
|
* Jittering final test reduces image artifacts. |
| 290 |
|
*/ |
| 291 |
|
wt = sqrt(e1) + sqrt(e2); |
| 292 |
< |
wt *= .9 + .2*urand(9015+samplendx); |
| 275 |
< |
if (wt > ambacc) |
| 292 |
> |
if (wt > ambacc*(.9+.2*urand(9015+samplendx))) |
| 293 |
|
continue; |
| 294 |
|
if (wt <= 1e-3) |
| 295 |
|
wt = 1e3; |
| 331 |
|
FVECT gp, gd; |
| 332 |
|
/* compute weight */ |
| 333 |
|
amb.weight = pow(AVGREFL, (double)al); |
| 334 |
< |
if (r->rweight < 0.2*amb.weight) /* heuristic */ |
| 334 |
> |
if (r->rweight < 0.1*amb.weight) /* heuristic */ |
| 335 |
|
amb.weight = r->rweight; |
| 336 |
|
/* compute ambient */ |
| 337 |
|
amb.rad = doambient(acol, r, amb.weight, gp, gd); |
| 338 |
< |
if (amb.rad == 0.0) |
| 338 |
> |
if (amb.rad <= FTINY) |
| 339 |
|
return(0.0); |
| 340 |
|
/* store it */ |
| 341 |
|
VCOPY(amb.pos, r->rop); |
| 436 |
|
copystruct(av, aval); |
| 437 |
|
av->latick = ambclock; |
| 438 |
|
av->next = NULL; |
| 439 |
+ |
addcolor(avsum, av->val); /* add to sum for averaging */ |
| 440 |
|
nambvals++; |
| 441 |
|
return(av); |
| 442 |
|
} |
| 548 |
|
av2list(av) |
| 549 |
|
AMBVAL *av; |
| 550 |
|
{ |
| 551 |
+ |
#ifdef DEBUG |
| 552 |
|
if (i_avlist >= nambvals) |
| 553 |
|
error(CONSISTENCY, "too many ambient values in av2list1"); |
| 554 |
+ |
#endif |
| 555 |
|
avlist1[i_avlist] = avlist2[i_avlist] = av; |
| 556 |
|
i_avlist++; |
| 557 |
|
} |
| 573 |
|
} |
| 574 |
|
|
| 575 |
|
|
| 576 |
+ |
#ifdef DEBUG |
| 577 |
+ |
static int |
| 578 |
+ |
avlmemi(avaddr) /* find list position from address */ |
| 579 |
+ |
AMBVAL *avaddr; |
| 580 |
+ |
{ |
| 581 |
+ |
register AMBVAL **avlpp; |
| 582 |
+ |
|
| 583 |
+ |
avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2, |
| 584 |
+ |
nambvals, sizeof(AMBVAL *), aposcmp); |
| 585 |
+ |
if (avlpp == NULL) |
| 586 |
+ |
error(CONSISTENCY, "address not found in avlmemi"); |
| 587 |
+ |
return(avlpp - avlist2); |
| 588 |
+ |
} |
| 589 |
+ |
#else |
| 590 |
+ |
#define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \ |
| 591 |
+ |
nambvals,sizeof(AMBVAL *),aposcmp) - avlist2) |
| 592 |
+ |
#endif |
| 593 |
+ |
|
| 594 |
+ |
|
| 595 |
|
static |
| 596 |
|
sortambvals(always) /* resort ambient values */ |
| 597 |
|
int always; |
| 599 |
|
AMBTREE oldatrunk; |
| 600 |
|
AMBVAL tav, *tap, *pnext; |
| 601 |
|
register int i, j; |
| 602 |
+ |
/* see if it's time yet */ |
| 603 |
+ |
if (!always && (ambclock < lastsort+sortintvl || |
| 604 |
+ |
nambvals < SORT_THRESH)) |
| 605 |
+ |
return; |
| 606 |
|
/* |
| 607 |
|
* The idea here is to minimize memory thrashing |
| 608 |
|
* in VM systems by improving reference locality. |
| 618 |
|
* the "always" parameter set to 1 so that the ambient |
| 619 |
|
* tree will be rebuilt with the new accuracy parameter. |
| 620 |
|
*/ |
| 621 |
< |
if (tracktime) { |
| 621 |
> |
if (tracktime) { /* allocate pointer arrays to sort */ |
| 622 |
|
avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); |
| 623 |
|
avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); |
| 624 |
|
} else |
| 625 |
|
avlist1 = avlist2 = NULL; |
| 626 |
< |
if (avlist2 == NULL) { /* rebuild tree? */ |
| 626 |
> |
if (avlist2 == NULL) { /* no time tracking -- rebuild tree? */ |
| 627 |
|
if (avlist1 != NULL) |
| 628 |
|
free((char *)avlist1); |
| 629 |
< |
if (!always) |
| 630 |
< |
return; |
| 631 |
< |
copystruct(&oldatrunk, &atrunk); |
| 632 |
< |
atrunk.alist = NULL; |
| 633 |
< |
atrunk.kid = NULL; |
| 634 |
< |
unloadatree(&oldatrunk, avinsert); |
| 629 |
> |
if (always) { /* rebuild without sorting */ |
| 630 |
> |
copystruct(&oldatrunk, &atrunk); |
| 631 |
> |
atrunk.alist = NULL; |
| 632 |
> |
atrunk.kid = NULL; |
| 633 |
> |
unloadatree(&oldatrunk, avinsert); |
| 634 |
> |
} |
| 635 |
|
} else { /* sort memory by last access time */ |
| 593 |
– |
i_avlist = 0; |
| 594 |
– |
unloadatree(&atrunk, av2list); /* empty current tree */ |
| 636 |
|
/* |
| 637 |
|
* Sorting memory is tricky because it isn't contiguous. |
| 638 |
|
* We have to sort an array of pointers by MRA and also |
| 639 |
|
* by memory position. We then copy values in "loops" |
| 640 |
|
* to minimize memory hits. Nevertheless, we will visit |
| 641 |
< |
* everyone at least once, and this is an expensive process |
| 641 |
> |
* everyone at least twice, and this is an expensive process |
| 642 |
|
* when we're thrashing, which is when we need to do it. |
| 643 |
|
*/ |
| 644 |
+ |
#ifdef DEBUG |
| 645 |
+ |
sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...", |
| 646 |
+ |
nambvals, ambclock); |
| 647 |
+ |
eputs(errmsg); |
| 648 |
+ |
#endif |
| 649 |
+ |
i_avlist = 0; |
| 650 |
+ |
unloadatree(&atrunk, av2list); /* empty current tree */ |
| 651 |
+ |
#ifdef DEBUG |
| 652 |
+ |
if (i_avlist < nambvals) |
| 653 |
+ |
error(CONSISTENCY, "missing ambient values in sortambvals"); |
| 654 |
+ |
#endif |
| 655 |
|
qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp); |
| 656 |
|
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
| 657 |
|
for (i = 0; i < nambvals; i++) { |
| 658 |
< |
if (avlist1[i] == NULL || avlist1[i] == avlist2[i]) |
| 658 |
> |
if (avlist1[i] == NULL) |
| 659 |
|
continue; |
| 660 |
|
tap = avlist2[i]; |
| 661 |
|
copystruct(&tav, tap); |
| 662 |
|
for (j = i; (pnext = avlist1[j]) != tap; |
| 663 |
< |
j = (AMBVAL **)bsearch((char *)&pnext, |
| 612 |
< |
(char *)(avlist2+i),nambvals-i, |
| 613 |
< |
sizeof(AMBVAL *),aposcmp) - |
| 614 |
< |
avlist2) { |
| 663 |
> |
j = avlmemi(pnext)) { |
| 664 |
|
copystruct(avlist2[j], pnext); |
| 665 |
|
avinsert(avlist2[j]); |
| 666 |
|
avlist1[j] = NULL; |
| 671 |
|
} |
| 672 |
|
free((char *)avlist1); |
| 673 |
|
free((char *)avlist2); |
| 674 |
< |
if (sortintvl < MAXACLOCK/4) |
| 674 |
> |
/* compute new sort interval */ |
| 675 |
> |
sortintvl = ambclock - lastsort; |
| 676 |
> |
if (sortintvl >= MAX_SORT_INTVL/2) |
| 677 |
> |
sortintvl = MAX_SORT_INTVL; |
| 678 |
> |
else |
| 679 |
|
sortintvl <<= 1; /* wait twice as long next */ |
| 680 |
+ |
#ifdef DEBUG |
| 681 |
+ |
eputs("done\n"); |
| 682 |
+ |
#endif |
| 683 |
|
} |
| 684 |
|
if (ambclock >= MAXACLOCK) |
| 685 |
|
ambclock = MAXACLOCK/2; |