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; /* 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 */ |
39 |
|
|
45 |
|
static FILE *ambfp = NULL; /* ambient file pointer */ |
46 |
|
static int nunflshed = 0; /* number of unflushed ambient values */ |
47 |
|
|
48 |
+ |
#ifndef SORT_THRESH |
49 |
+ |
#ifdef BIGMEM |
50 |
+ |
#define SORT_THRESH ((9L<<20)/sizeof(AMBVAL)) |
51 |
+ |
#else |
52 |
+ |
#define SORT_THRESH ((3L<<20)/sizeof(AMBVAL)) |
53 |
+ |
#endif |
54 |
+ |
#endif |
55 |
+ |
#ifndef SORT_INTVL |
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 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) |
75 |
+ |
|
76 |
|
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
77 |
|
|
78 |
|
#define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL)) |
79 |
|
|
45 |
– |
#define newambtree() (AMBTREE *)calloc(8, sizeof(AMBTREE)) |
46 |
– |
#define freeambtree(t) free((char *)(t)) |
47 |
– |
|
80 |
|
extern long ftell(), lseek(); |
81 |
< |
static int initambfile(), avsave(), avinsert(), loadatree(); |
81 |
> |
static int initambfile(), avsave(), avinsert(), sortambvals(); |
82 |
|
static AMBVAL *avstore(); |
83 |
|
#ifdef F_SETLKW |
84 |
|
static aflock(); |
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 |
|
} |
110 |
|
double newa; |
111 |
|
{ |
112 |
|
static double oldambacc = -1.0; |
81 |
– |
AMBTREE oldatrunk; |
113 |
|
|
114 |
|
ambacc = newa < 0.0 ? 0.0 : newa; /* may be done already */ |
115 |
|
if (oldambacc < -FTINY) |
119 |
|
if (ambacc <= FTINY) |
120 |
|
return; /* cannot build new tree */ |
121 |
|
/* else need to rebuild tree */ |
122 |
< |
copystruct(&oldatrunk, &atrunk); |
92 |
< |
atrunk.alist = NULL; |
93 |
< |
atrunk.kid = NULL; |
94 |
< |
loadatree(&oldatrunk); |
122 |
> |
sortambvals(1); |
123 |
|
oldambacc = ambacc; /* remeber setting for next call */ |
124 |
|
} |
125 |
|
|
202 |
|
rdepth++; |
203 |
|
d = doambient(acol, r, r->rweight, NULL, NULL); |
204 |
|
rdepth--; |
205 |
< |
if (d == 0.0) |
205 |
> |
if (d <= FTINY) |
206 |
|
goto dumbamb; |
207 |
|
return; |
208 |
|
} |
209 |
+ |
/* resort memory? */ |
210 |
+ |
sortambvals(0); |
211 |
|
/* get ambient value */ |
212 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
213 |
|
d = sumambient(acol, r, rdepth, |
214 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
215 |
< |
if (d > FTINY) |
215 |
> |
if (d > FTINY) { |
216 |
|
scalecolor(acol, 1.0/d); |
217 |
< |
else { |
188 |
< |
d = makeambient(acol, r, rdepth++); |
189 |
< |
rdepth--; |
217 |
> |
return; |
218 |
|
} |
219 |
+ |
rdepth++; |
220 |
+ |
d = makeambient(acol, r, rdepth-1); |
221 |
+ |
rdepth--; |
222 |
|
if (d > FTINY) |
223 |
|
return; |
224 |
|
dumbamb: /* return global value */ |
225 |
|
copycolor(acol, ambval); |
226 |
+ |
#if AMBVWT |
227 |
+ |
if (nambvals == 0) |
228 |
+ |
return; |
229 |
+ |
scalecolor(acol, (double)AMBVWT); |
230 |
+ |
addcolor(acol, avsum); /* average in computations */ |
231 |
+ |
d = 1.0/(AMBVWT+nambvals); |
232 |
+ |
scalecolor(acol, d); |
233 |
+ |
#endif |
234 |
|
} |
235 |
|
|
236 |
|
|
249 |
|
int i; |
250 |
|
register int j; |
251 |
|
register AMBVAL *av; |
252 |
< |
/* do this node */ |
252 |
> |
|
253 |
|
wsum = 0.0; |
254 |
+ |
/* do this node */ |
255 |
|
for (av = at->alist; av != NULL; av = av->next) { |
256 |
+ |
if (tracktime) |
257 |
+ |
av->latick = ambclock++; |
258 |
|
/* |
259 |
|
* Ambient level test. |
260 |
|
*/ |
265 |
|
/* |
266 |
|
* Ambient radius test. |
267 |
|
*/ |
268 |
< |
e1 = 0.0; |
269 |
< |
for (j = 0; j < 3; j++) { |
270 |
< |
d = av->pos[j] - r->rop[j]; |
271 |
< |
e1 += d * d; |
272 |
< |
} |
268 |
> |
d = av->pos[0] - r->rop[0]; |
269 |
> |
e1 = d * d; |
270 |
> |
d = av->pos[1] - r->rop[1]; |
271 |
> |
e1 += d * d; |
272 |
> |
d = av->pos[2] - r->rop[2]; |
273 |
> |
e1 += d * d; |
274 |
|
e1 /= av->rad * av->rad; |
275 |
|
if (e1 > ambacc*ambacc*1.21) |
276 |
|
continue; |
294 |
|
* Jittering final test reduces image artifacts. |
295 |
|
*/ |
296 |
|
wt = sqrt(e1) + sqrt(e2); |
297 |
< |
wt *= .9 + .2*urand(9015+samplendx); |
255 |
< |
if (wt > ambacc) |
297 |
> |
if (wt > ambacc*(.9+.2*urand(9015+samplendx))) |
298 |
|
continue; |
299 |
|
if (wt <= 1e-3) |
300 |
|
wt = 1e3; |
439 |
|
if ((av = newambval()) == NULL) |
440 |
|
error(SYSTEM, "out of memory in avstore"); |
441 |
|
copystruct(av, aval); |
442 |
+ |
av->latick = ambclock; |
443 |
+ |
av->next = NULL; |
444 |
+ |
addcolor(avsum, av->val); /* add to sum for averaging */ |
445 |
+ |
nambvals++; |
446 |
|
return(av); |
447 |
|
} |
448 |
|
|
449 |
|
|
450 |
+ |
#define ATALLOCSZ 512 /* #/8 trees to allocate at once */ |
451 |
+ |
|
452 |
+ |
static AMBTREE *atfreelist = NULL; /* free ambient tree structures */ |
453 |
+ |
|
454 |
+ |
|
455 |
|
static |
456 |
+ |
AMBTREE * |
457 |
+ |
newambtree() /* allocate 8 ambient tree structs */ |
458 |
+ |
{ |
459 |
+ |
register AMBTREE *atp, *upperlim; |
460 |
+ |
|
461 |
+ |
if (atfreelist == NULL) { /* get more nodes */ |
462 |
+ |
atfreelist = (AMBTREE *)bmalloc(ATALLOCSZ*8*sizeof(AMBTREE)); |
463 |
+ |
if (atfreelist == NULL) |
464 |
+ |
return(NULL); |
465 |
+ |
/* link new free list */ |
466 |
+ |
upperlim = atfreelist + 8*(ATALLOCSZ-1); |
467 |
+ |
for (atp = atfreelist; atp < upperlim; atp += 8) |
468 |
+ |
atp->kid = atp + 8; |
469 |
+ |
atp->kid = NULL; |
470 |
+ |
} |
471 |
+ |
atp = atfreelist; |
472 |
+ |
atfreelist = atp->kid; |
473 |
+ |
bzero((char *)atp, 8*sizeof(AMBTREE)); |
474 |
+ |
return(atp); |
475 |
+ |
} |
476 |
+ |
|
477 |
+ |
|
478 |
+ |
static |
479 |
+ |
freeambtree(atp) /* free 8 ambient tree structs */ |
480 |
+ |
AMBTREE *atp; |
481 |
+ |
{ |
482 |
+ |
atp->kid = atfreelist; |
483 |
+ |
atfreelist = atp; |
484 |
+ |
} |
485 |
+ |
|
486 |
+ |
|
487 |
+ |
static |
488 |
|
avinsert(av) /* insert ambient value in our tree */ |
489 |
|
register AMBVAL *av; |
490 |
|
{ |
525 |
|
|
526 |
|
|
527 |
|
static |
528 |
< |
loadatree(at) /* move tree to main store */ |
528 |
> |
unloadatree(at, f) /* unload an ambient value tree */ |
529 |
|
register AMBTREE *at; |
530 |
+ |
int (*f)(); |
531 |
|
{ |
532 |
|
register AMBVAL *av; |
533 |
|
register int i; |
534 |
|
/* transfer values at this node */ |
535 |
|
for (av = at->alist; av != NULL; av = at->alist) { |
536 |
|
at->alist = av->next; |
537 |
< |
avinsert(av); |
537 |
> |
(*f)(av); |
538 |
|
} |
539 |
|
if (at->kid == NULL) |
540 |
|
return; |
541 |
|
for (i = 0; i < 8; i++) /* transfer and free children */ |
542 |
< |
loadatree(at->kid+i); |
542 |
> |
unloadatree(at->kid+i, f); |
543 |
|
freeambtree(at->kid); |
544 |
+ |
at->kid = NULL; |
545 |
+ |
} |
546 |
+ |
|
547 |
+ |
|
548 |
+ |
static AMBVAL **avlist1, **avlist2; /* ambient value lists for sorting */ |
549 |
+ |
static int i_avlist; /* index for lists */ |
550 |
+ |
|
551 |
+ |
|
552 |
+ |
static |
553 |
+ |
av2list(av) |
554 |
+ |
AMBVAL *av; |
555 |
+ |
{ |
556 |
+ |
#ifdef DEBUG |
557 |
+ |
if (i_avlist >= nambvals) |
558 |
+ |
error(CONSISTENCY, "too many ambient values in av2list1"); |
559 |
+ |
#endif |
560 |
+ |
avlist1[i_avlist] = avlist2[i_avlist] = av; |
561 |
+ |
i_avlist++; |
562 |
+ |
} |
563 |
+ |
|
564 |
+ |
|
565 |
+ |
static int |
566 |
+ |
alatcmp(avp1, avp2) /* compare ambient values for MRA */ |
567 |
+ |
AMBVAL **avp1, **avp2; |
568 |
+ |
{ |
569 |
+ |
return((**avp2).latick - (**avp1).latick); |
570 |
+ |
} |
571 |
+ |
|
572 |
+ |
|
573 |
+ |
static int |
574 |
+ |
aposcmp(avp1, avp2) /* compare ambient value positions */ |
575 |
+ |
AMBVAL **avp1, **avp2; |
576 |
+ |
{ |
577 |
+ |
return(*avp1 - *avp2); |
578 |
+ |
} |
579 |
+ |
|
580 |
+ |
|
581 |
+ |
#ifdef DEBUG |
582 |
+ |
static int |
583 |
+ |
avlmemi(avaddr) /* find list position from address */ |
584 |
+ |
AMBVAL *avaddr; |
585 |
+ |
{ |
586 |
+ |
register AMBVAL **avlpp; |
587 |
+ |
|
588 |
+ |
avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2, |
589 |
+ |
nambvals, sizeof(AMBVAL *), aposcmp); |
590 |
+ |
if (avlpp == NULL) |
591 |
+ |
error(CONSISTENCY, "address not found in avlmemi"); |
592 |
+ |
return(avlpp - avlist2); |
593 |
+ |
} |
594 |
+ |
#else |
595 |
+ |
#define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \ |
596 |
+ |
nambvals,sizeof(AMBVAL *),aposcmp) - avlist2) |
597 |
+ |
#endif |
598 |
+ |
|
599 |
+ |
|
600 |
+ |
static |
601 |
+ |
sortambvals(always) /* resort ambient values */ |
602 |
+ |
int always; |
603 |
+ |
{ |
604 |
+ |
AMBTREE oldatrunk; |
605 |
+ |
AMBVAL tav, *tap, *pnext; |
606 |
+ |
register int i, j; |
607 |
+ |
/* see if it's time yet */ |
608 |
+ |
if (!always && (ambclock < lastsort+sortintvl || |
609 |
+ |
nambvals < SORT_THRESH)) |
610 |
+ |
return; |
611 |
+ |
/* |
612 |
+ |
* The idea here is to minimize memory thrashing |
613 |
+ |
* in VM systems by improving reference locality. |
614 |
+ |
* We do this by periodically sorting our stored ambient |
615 |
+ |
* values in memory in order of most recently to least |
616 |
+ |
* recently accessed. This ordering was chosen so that new |
617 |
+ |
* ambient values (which tend to be less important) go into |
618 |
+ |
* higher memory with the infrequently accessed values. |
619 |
+ |
* Since we expect our values to need sorting less |
620 |
+ |
* frequently as the process continues, we double our |
621 |
+ |
* waiting interval after each call. |
622 |
+ |
* This routine is also called by setambacc() with |
623 |
+ |
* the "always" parameter set to 1 so that the ambient |
624 |
+ |
* tree will be rebuilt with the new accuracy parameter. |
625 |
+ |
*/ |
626 |
+ |
if (tracktime) { /* allocate pointer arrays to sort */ |
627 |
+ |
avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); |
628 |
+ |
avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); |
629 |
+ |
} else |
630 |
+ |
avlist1 = avlist2 = NULL; |
631 |
+ |
if (avlist2 == NULL) { /* no time tracking -- rebuild tree? */ |
632 |
+ |
if (avlist1 != NULL) |
633 |
+ |
free((char *)avlist1); |
634 |
+ |
if (always) { /* rebuild without sorting */ |
635 |
+ |
copystruct(&oldatrunk, &atrunk); |
636 |
+ |
atrunk.alist = NULL; |
637 |
+ |
atrunk.kid = NULL; |
638 |
+ |
unloadatree(&oldatrunk, avinsert); |
639 |
+ |
} |
640 |
+ |
} else { /* sort memory by last access time */ |
641 |
+ |
/* |
642 |
+ |
* Sorting memory is tricky because it isn't contiguous. |
643 |
+ |
* We have to sort an array of pointers by MRA and also |
644 |
+ |
* by memory position. We then copy values in "loops" |
645 |
+ |
* to minimize memory hits. Nevertheless, we will visit |
646 |
+ |
* everyone at least twice, and this is an expensive process |
647 |
+ |
* when we're thrashing, which is when we need to do it. |
648 |
+ |
*/ |
649 |
+ |
#ifdef DEBUG |
650 |
+ |
sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...", |
651 |
+ |
nambvals, ambclock); |
652 |
+ |
eputs(errmsg); |
653 |
+ |
#endif |
654 |
+ |
i_avlist = 0; |
655 |
+ |
unloadatree(&atrunk, av2list); /* empty current tree */ |
656 |
+ |
#ifdef DEBUG |
657 |
+ |
if (i_avlist < nambvals) |
658 |
+ |
error(CONSISTENCY, "missing ambient values in sortambvals"); |
659 |
+ |
#endif |
660 |
+ |
qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp); |
661 |
+ |
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
662 |
+ |
for (i = 0; i < nambvals; i++) { |
663 |
+ |
if (avlist1[i] == NULL) |
664 |
+ |
continue; |
665 |
+ |
tap = avlist2[i]; |
666 |
+ |
copystruct(&tav, tap); |
667 |
+ |
for (j = i; (pnext = avlist1[j]) != tap; |
668 |
+ |
j = avlmemi(pnext)) { |
669 |
+ |
copystruct(avlist2[j], pnext); |
670 |
+ |
avinsert(avlist2[j]); |
671 |
+ |
avlist1[j] = NULL; |
672 |
+ |
} |
673 |
+ |
copystruct(avlist2[j], &tav); |
674 |
+ |
avinsert(avlist2[j]); |
675 |
+ |
avlist1[j] = NULL; |
676 |
+ |
} |
677 |
+ |
free((char *)avlist1); |
678 |
+ |
free((char *)avlist2); |
679 |
+ |
/* compute new sort interval */ |
680 |
+ |
sortintvl = ambclock - lastsort; |
681 |
+ |
if (sortintvl >= MAX_SORT_INTVL/2) |
682 |
+ |
sortintvl = MAX_SORT_INTVL; |
683 |
+ |
else |
684 |
+ |
sortintvl <<= 1; /* wait twice as long next */ |
685 |
+ |
#ifdef DEBUG |
686 |
+ |
eputs("done\n"); |
687 |
+ |
#endif |
688 |
+ |
} |
689 |
+ |
if (ambclock >= MAXACLOCK) |
690 |
+ |
ambclock = MAXACLOCK/2; |
691 |
+ |
lastsort = ambclock; |
692 |
|
} |
693 |
|
|
694 |
|
|