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 |
> |
#define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */ |
22 |
|
|
23 |
|
typedef struct ambtree { |
24 |
|
AMBVAL *alist; /* ambient value list */ |
27 |
|
|
28 |
|
extern CUBE thescene; /* contains space boundaries */ |
29 |
|
|
30 |
< |
extern char *shm_boundary; /* shared memory boundary */ |
30 |
> |
extern char *shm_boundary; /* memory sharing boundary */ |
31 |
|
|
32 |
|
#define MAXASET 511 /* maximum number of elements in ambient set */ |
33 |
|
OBJECT ambset[MAXASET+1]={0}; /* ambient include/exclude set */ |
42 |
|
|
43 |
|
#ifndef SORT_THRESH |
44 |
|
#ifdef BIGMEM |
45 |
< |
#define SORT_THRESH (6*(1L<<20)/sizeof(AMBVAL)) |
45 |
> |
#define SORT_THRESH ((9L<<20)/sizeof(AMBVAL)) |
46 |
|
#else |
47 |
< |
#define SORT_THRESH (2*(1L<<20)/sizeof(AMBVAL)) |
47 |
> |
#define SORT_THRESH ((3L<<20)/sizeof(AMBVAL)) |
48 |
|
#endif |
49 |
|
#endif |
50 |
|
#ifndef SORT_INTVL |
51 |
< |
#define SORT_INTVL (SORT_THRESH*2) |
51 |
> |
#define SORT_INTVL (SORT_THRESH*256) |
52 |
|
#endif |
53 |
+ |
#ifndef MAX_SORT_INTVL |
54 |
+ |
#define MAX_SORT_INTVL (SORT_INTVL<<4) |
55 |
+ |
#endif |
56 |
|
|
57 |
< |
static long ambclock = 0; /* ambient access clock */ |
58 |
< |
static int nambvals = 0; /* number of stored ambient values */ |
59 |
< |
static long lastsort = 0; /* time of last value sort */ |
57 |
> |
static unsigned long ambclock = 0; /* ambient access clock */ |
58 |
> |
static unsigned int nambvals = 0; /* number of stored ambient values */ |
59 |
> |
static unsigned long lastsort = 0; /* time of last value sort */ |
60 |
|
static long sortintvl = SORT_INTVL; /* time until next sort */ |
61 |
|
|
62 |
|
#define MAXACLOCK (1L<<30) /* clock turnover value */ |
63 |
+ |
/* |
64 |
+ |
* Track access times unless we are sharing ambient values |
65 |
+ |
* through memory on a multiprocessor, when we want to avoid |
66 |
+ |
* claiming our own memory (copy on write). |
67 |
+ |
*/ |
68 |
|
#define tracktime (shm_boundary == NULL || ambfp == NULL) |
61 |
– |
#define need2sort (ambclock > lastsort+sortintvl && \ |
62 |
– |
nambvals > SORT_THRESH) |
69 |
|
|
70 |
|
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
71 |
|
|
89 |
|
maxarad = thescene.cusize / 2.0; |
90 |
|
} else { |
91 |
|
minarad = thescene.cusize / ar; |
92 |
< |
maxarad = 16 * minarad; /* heuristic */ |
92 |
> |
maxarad = 64 * minarad; /* heuristic */ |
93 |
|
if (maxarad > thescene.cusize / 2.0) |
94 |
|
maxarad = thescene.cusize / 2.0; |
95 |
|
} |
200 |
|
goto dumbamb; |
201 |
|
return; |
202 |
|
} |
203 |
+ |
/* resort memory? */ |
204 |
+ |
sortambvals(0); |
205 |
|
/* get ambient value */ |
198 |
– |
if (need2sort) |
199 |
– |
sortambvals(0); |
206 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
207 |
|
d = sumambient(acol, r, rdepth, |
208 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
234 |
|
int i; |
235 |
|
register int j; |
236 |
|
register AMBVAL *av; |
237 |
< |
/* do this node */ |
237 |
> |
|
238 |
|
wsum = 0.0; |
239 |
+ |
/* do this node */ |
240 |
|
for (av = at->alist; av != NULL; av = av->next) { |
241 |
|
if (tracktime) |
242 |
|
av->latick = ambclock++; |
250 |
|
/* |
251 |
|
* Ambient radius test. |
252 |
|
*/ |
253 |
< |
e1 = 0.0; |
254 |
< |
for (j = 0; j < 3; j++) { |
255 |
< |
d = av->pos[j] - r->rop[j]; |
256 |
< |
e1 += d * d; |
257 |
< |
} |
253 |
> |
d = av->pos[0] - r->rop[0]; |
254 |
> |
e1 = d * d; |
255 |
> |
d = av->pos[1] - r->rop[1]; |
256 |
> |
e1 += d * d; |
257 |
> |
d = av->pos[2] - r->rop[2]; |
258 |
> |
e1 += d * d; |
259 |
|
e1 /= av->rad * av->rad; |
260 |
|
if (e1 > ambacc*ambacc*1.21) |
261 |
|
continue; |
279 |
|
* Jittering final test reduces image artifacts. |
280 |
|
*/ |
281 |
|
wt = sqrt(e1) + sqrt(e2); |
282 |
< |
wt *= .9 + .2*urand(9015+samplendx); |
275 |
< |
if (wt > ambacc) |
282 |
> |
if (wt > ambacc*(.9+.2*urand(9015+samplendx))) |
283 |
|
continue; |
284 |
|
if (wt <= 1e-3) |
285 |
|
wt = 1e3; |
537 |
|
av2list(av) |
538 |
|
AMBVAL *av; |
539 |
|
{ |
540 |
+ |
#ifdef DEBUG |
541 |
|
if (i_avlist >= nambvals) |
542 |
|
error(CONSISTENCY, "too many ambient values in av2list1"); |
543 |
+ |
#endif |
544 |
|
avlist1[i_avlist] = avlist2[i_avlist] = av; |
545 |
|
i_avlist++; |
546 |
|
} |
562 |
|
} |
563 |
|
|
564 |
|
|
565 |
+ |
#ifdef DEBUG |
566 |
+ |
static int |
567 |
+ |
avlmemi(avaddr) /* find list position from address */ |
568 |
+ |
AMBVAL *avaddr; |
569 |
+ |
{ |
570 |
+ |
register AMBVAL **avlpp; |
571 |
+ |
|
572 |
+ |
avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2, |
573 |
+ |
nambvals, sizeof(AMBVAL *), aposcmp); |
574 |
+ |
if (avlpp == NULL) |
575 |
+ |
error(CONSISTENCY, "address not found in avlmemi"); |
576 |
+ |
return(avlpp - avlist2); |
577 |
+ |
} |
578 |
+ |
#else |
579 |
+ |
#define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \ |
580 |
+ |
nambvals,sizeof(AMBVAL *),aposcmp) - avlist2) |
581 |
+ |
#endif |
582 |
+ |
|
583 |
+ |
|
584 |
|
static |
585 |
|
sortambvals(always) /* resort ambient values */ |
586 |
|
int always; |
588 |
|
AMBTREE oldatrunk; |
589 |
|
AMBVAL tav, *tap, *pnext; |
590 |
|
register int i, j; |
591 |
+ |
/* see if it's time yet */ |
592 |
+ |
if (!always && (ambclock < lastsort+sortintvl || |
593 |
+ |
nambvals < SORT_THRESH)) |
594 |
+ |
return; |
595 |
|
/* |
596 |
|
* The idea here is to minimize memory thrashing |
597 |
|
* in VM systems by improving reference locality. |
607 |
|
* the "always" parameter set to 1 so that the ambient |
608 |
|
* tree will be rebuilt with the new accuracy parameter. |
609 |
|
*/ |
610 |
< |
if (tracktime) { |
610 |
> |
if (tracktime) { /* allocate pointer arrays to sort */ |
611 |
|
avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); |
612 |
|
avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); |
613 |
|
} else |
614 |
|
avlist1 = avlist2 = NULL; |
615 |
< |
if (avlist2 == NULL) { /* rebuild tree? */ |
615 |
> |
if (avlist2 == NULL) { /* no time tracking -- rebuild tree? */ |
616 |
|
if (avlist1 != NULL) |
617 |
|
free((char *)avlist1); |
618 |
< |
if (!always) |
619 |
< |
return; |
620 |
< |
copystruct(&oldatrunk, &atrunk); |
621 |
< |
atrunk.alist = NULL; |
622 |
< |
atrunk.kid = NULL; |
623 |
< |
unloadatree(&oldatrunk, avinsert); |
618 |
> |
if (always) { /* rebuild without sorting */ |
619 |
> |
copystruct(&oldatrunk, &atrunk); |
620 |
> |
atrunk.alist = NULL; |
621 |
> |
atrunk.kid = NULL; |
622 |
> |
unloadatree(&oldatrunk, avinsert); |
623 |
> |
} |
624 |
|
} else { /* sort memory by last access time */ |
593 |
– |
i_avlist = 0; |
594 |
– |
unloadatree(&atrunk, av2list); /* empty current tree */ |
625 |
|
/* |
626 |
|
* Sorting memory is tricky because it isn't contiguous. |
627 |
|
* We have to sort an array of pointers by MRA and also |
628 |
|
* by memory position. We then copy values in "loops" |
629 |
|
* to minimize memory hits. Nevertheless, we will visit |
630 |
< |
* everyone at least once, and this is an expensive process |
630 |
> |
* everyone at least twice, and this is an expensive process |
631 |
|
* when we're thrashing, which is when we need to do it. |
632 |
|
*/ |
633 |
+ |
#ifdef DEBUG |
634 |
+ |
sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...", |
635 |
+ |
nambvals, ambclock); |
636 |
+ |
eputs(errmsg); |
637 |
+ |
#endif |
638 |
+ |
i_avlist = 0; |
639 |
+ |
unloadatree(&atrunk, av2list); /* empty current tree */ |
640 |
+ |
#ifdef DEBUG |
641 |
+ |
if (i_avlist < nambvals) |
642 |
+ |
error(CONSISTENCY, "missing ambient values in sortambvals"); |
643 |
+ |
#endif |
644 |
|
qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp); |
645 |
|
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
646 |
|
for (i = 0; i < nambvals; i++) { |
647 |
< |
if (avlist1[i] == NULL || avlist1[i] == avlist2[i]) |
647 |
> |
if (avlist1[i] == NULL) |
648 |
|
continue; |
649 |
|
tap = avlist2[i]; |
650 |
|
copystruct(&tav, tap); |
651 |
|
for (j = i; (pnext = avlist1[j]) != tap; |
652 |
< |
j = (AMBVAL **)bsearch((char *)&pnext, |
612 |
< |
(char *)(avlist2+i),nambvals-i, |
613 |
< |
sizeof(AMBVAL *),aposcmp) - |
614 |
< |
avlist2) { |
652 |
> |
j = avlmemi(pnext)) { |
653 |
|
copystruct(avlist2[j], pnext); |
654 |
|
avinsert(avlist2[j]); |
655 |
|
avlist1[j] = NULL; |
660 |
|
} |
661 |
|
free((char *)avlist1); |
662 |
|
free((char *)avlist2); |
663 |
< |
if (sortintvl < MAXACLOCK/4) |
663 |
> |
/* compute new sort interval */ |
664 |
> |
sortintvl = ambclock - lastsort; |
665 |
> |
if (sortintvl > MAX_SORT_INTVL) |
666 |
> |
sortintvl = MAX_SORT_INTVL; |
667 |
> |
else |
668 |
|
sortintvl <<= 1; /* wait twice as long next */ |
669 |
+ |
#ifdef DEBUG |
670 |
+ |
eputs("done\n"); |
671 |
+ |
#endif |
672 |
|
} |
673 |
|
if (ambclock >= MAXACLOCK) |
674 |
|
ambclock = MAXACLOCK/2; |