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"; |
27 |
|
|
28 |
|
extern CUBE thescene; /* contains space boundaries */ |
29 |
|
|
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 */ |
34 |
|
|
40 |
|
static FILE *ambfp = NULL; /* ambient file pointer */ |
41 |
|
static int nunflshed = 0; /* number of unflushed ambient values */ |
42 |
|
|
43 |
+ |
#ifndef SORT_THRESH |
44 |
+ |
#ifdef BIGMEM |
45 |
+ |
#define SORT_THRESH ((9L<<20)/sizeof(AMBVAL)) |
46 |
+ |
#else |
47 |
+ |
#define SORT_THRESH ((3L<<20)/sizeof(AMBVAL)) |
48 |
+ |
#endif |
49 |
+ |
#endif |
50 |
+ |
#ifndef SORT_INTVL |
51 |
+ |
#define SORT_INTVL (SORT_THRESH*8) |
52 |
+ |
#endif |
53 |
+ |
|
54 |
+ |
static unsigned long ambclock = 0; /* ambient access clock */ |
55 |
+ |
static unsigned int nambvals = 0; /* number of stored ambient values */ |
56 |
+ |
static unsigned long lastsort = 0; /* time of last value sort */ |
57 |
+ |
static long sortintvl = SORT_INTVL; /* time until next sort */ |
58 |
+ |
|
59 |
+ |
#define MAXACLOCK (1L<<30) /* clock turnover value */ |
60 |
+ |
/* |
61 |
+ |
* Track access times unless we are sharing ambient values |
62 |
+ |
* through memory on a multiprocessor, when we want to avoid |
63 |
+ |
* claiming our own memory (copy on write). |
64 |
+ |
*/ |
65 |
+ |
#define tracktime (shm_boundary == NULL || ambfp == NULL) |
66 |
+ |
#define checksort() if (ambclock > lastsort+sortintvl && \ |
67 |
+ |
nambvals > SORT_THRESH) sortambvals(0) |
68 |
+ |
|
69 |
|
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
70 |
|
|
71 |
|
#define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL)) |
72 |
|
|
45 |
– |
#define newambtree() (AMBTREE *)calloc(8, sizeof(AMBTREE)) |
46 |
– |
#define freeambtree(t) free((char *)(t)) |
47 |
– |
|
73 |
|
extern long ftell(), lseek(); |
74 |
< |
static int initambfile(), avsave(), avinsert(), loadatree(); |
74 |
> |
static int initambfile(), avsave(), avinsert(), sortambvals(); |
75 |
|
static AMBVAL *avstore(); |
76 |
|
#ifdef F_SETLKW |
77 |
|
static aflock(); |
84 |
|
ambres = ar < 0 ? 0 : ar; /* may be done already */ |
85 |
|
/* set min & max radii */ |
86 |
|
if (ar <= 0) { |
87 |
< |
minarad = 0.0; |
87 |
> |
minarad = 0; |
88 |
|
maxarad = thescene.cusize / 2.0; |
89 |
|
} else { |
90 |
|
minarad = thescene.cusize / ar; |
91 |
< |
maxarad = 16.0 * minarad; /* heuristic */ |
91 |
> |
maxarad = 16 * minarad; /* heuristic */ |
92 |
|
if (maxarad > thescene.cusize / 2.0) |
93 |
|
maxarad = thescene.cusize / 2.0; |
94 |
|
} |
95 |
< |
if (maxarad <= FTINY) |
96 |
< |
maxarad = .001; |
95 |
> |
if (minarad <= FTINY) |
96 |
> |
minarad = 10*FTINY; |
97 |
> |
if (maxarad <= minarad) |
98 |
> |
maxarad = 64 * minarad; |
99 |
|
} |
100 |
|
|
101 |
|
|
103 |
|
double newa; |
104 |
|
{ |
105 |
|
static double oldambacc = -1.0; |
79 |
– |
AMBTREE oldatrunk; |
106 |
|
|
107 |
|
ambacc = newa < 0.0 ? 0.0 : newa; /* may be done already */ |
108 |
|
if (oldambacc < -FTINY) |
112 |
|
if (ambacc <= FTINY) |
113 |
|
return; /* cannot build new tree */ |
114 |
|
/* else need to rebuild tree */ |
115 |
< |
copystruct(&oldatrunk, &atrunk); |
90 |
< |
atrunk.alist = NULL; |
91 |
< |
atrunk.kid = NULL; |
92 |
< |
loadatree(&oldatrunk); |
115 |
> |
sortambvals(1); |
116 |
|
oldambacc = ambacc; /* remeber setting for next call */ |
117 |
|
} |
118 |
|
|
199 |
|
goto dumbamb; |
200 |
|
return; |
201 |
|
} |
202 |
+ |
/* resort memory? */ |
203 |
+ |
checksort(); |
204 |
|
/* get ambient value */ |
205 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
206 |
|
d = sumambient(acol, r, rdepth, |
236 |
|
/* do this node */ |
237 |
|
wsum = 0.0; |
238 |
|
for (av = at->alist; av != NULL; av = av->next) { |
239 |
+ |
if (tracktime) |
240 |
+ |
av->latick = ambclock++; |
241 |
|
/* |
242 |
|
* Ambient level test. |
243 |
|
*/ |
244 |
< |
if (av->lvl > al || av->weight < r->rweight-FTINY) |
244 |
> |
if (av->lvl > al) /* list sorted, so this works */ |
245 |
> |
break; |
246 |
> |
if (av->weight < r->rweight-FTINY) |
247 |
|
continue; |
248 |
|
/* |
249 |
|
* Ambient radius test. |
378 |
|
#endif |
379 |
|
setbuf(ambfp, bmalloc(BUFSIZ+8)); |
380 |
|
if (creat) { /* new file */ |
381 |
+ |
newheader("RADIANCE", ambfp); |
382 |
|
fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ", |
383 |
|
progname, colval(ambval,RED), |
384 |
|
colval(ambval,GRN), colval(ambval,BLU), |
422 |
|
if ((av = newambval()) == NULL) |
423 |
|
error(SYSTEM, "out of memory in avstore"); |
424 |
|
copystruct(av, aval); |
425 |
+ |
av->latick = ambclock; |
426 |
+ |
av->next = NULL; |
427 |
+ |
nambvals++; |
428 |
|
return(av); |
429 |
|
} |
430 |
|
|
431 |
|
|
432 |
+ |
#define ATALLOCSZ 512 /* #/8 trees to allocate at once */ |
433 |
+ |
|
434 |
+ |
static AMBTREE *atfreelist = NULL; /* free ambient tree structures */ |
435 |
+ |
|
436 |
+ |
|
437 |
|
static |
438 |
+ |
AMBTREE * |
439 |
+ |
newambtree() /* allocate 8 ambient tree structs */ |
440 |
+ |
{ |
441 |
+ |
register AMBTREE *atp, *upperlim; |
442 |
+ |
|
443 |
+ |
if (atfreelist == NULL) { /* get more nodes */ |
444 |
+ |
atfreelist = (AMBTREE *)bmalloc(ATALLOCSZ*8*sizeof(AMBTREE)); |
445 |
+ |
if (atfreelist == NULL) |
446 |
+ |
return(NULL); |
447 |
+ |
/* link new free list */ |
448 |
+ |
upperlim = atfreelist + 8*(ATALLOCSZ-1); |
449 |
+ |
for (atp = atfreelist; atp < upperlim; atp += 8) |
450 |
+ |
atp->kid = atp + 8; |
451 |
+ |
atp->kid = NULL; |
452 |
+ |
} |
453 |
+ |
atp = atfreelist; |
454 |
+ |
atfreelist = atp->kid; |
455 |
+ |
bzero((char *)atp, 8*sizeof(AMBTREE)); |
456 |
+ |
return(atp); |
457 |
+ |
} |
458 |
+ |
|
459 |
+ |
|
460 |
+ |
static |
461 |
+ |
freeambtree(atp) /* free 8 ambient tree structs */ |
462 |
+ |
AMBTREE *atp; |
463 |
+ |
{ |
464 |
+ |
atp->kid = atfreelist; |
465 |
+ |
atfreelist = atp; |
466 |
+ |
} |
467 |
+ |
|
468 |
+ |
|
469 |
+ |
static |
470 |
|
avinsert(av) /* insert ambient value in our tree */ |
471 |
|
register AMBVAL *av; |
472 |
|
{ |
473 |
|
register AMBTREE *at; |
474 |
+ |
register AMBVAL *ap; |
475 |
+ |
AMBVAL avh; |
476 |
|
FVECT ck0; |
477 |
|
double s; |
478 |
|
int branch; |
496 |
|
} |
497 |
|
at = at->kid + branch; |
498 |
|
} |
499 |
< |
av->next = at->alist; |
500 |
< |
at->alist = av; |
499 |
> |
avh.next = at->alist; /* order by increasing level */ |
500 |
> |
for (ap = &avh; ap->next != NULL; ap = ap->next) |
501 |
> |
if (ap->next->lvl >= av->lvl) |
502 |
> |
break; |
503 |
> |
av->next = ap->next; |
504 |
> |
ap->next = av; |
505 |
> |
at->alist = avh.next; |
506 |
|
} |
507 |
|
|
508 |
|
|
509 |
|
static |
510 |
< |
loadatree(at) /* move tree to main store */ |
510 |
> |
unloadatree(at, f) /* unload an ambient value tree */ |
511 |
|
register AMBTREE *at; |
512 |
+ |
int (*f)(); |
513 |
|
{ |
514 |
|
register AMBVAL *av; |
515 |
|
register int i; |
516 |
|
/* transfer values at this node */ |
517 |
|
for (av = at->alist; av != NULL; av = at->alist) { |
518 |
|
at->alist = av->next; |
519 |
< |
avinsert(av); |
519 |
> |
(*f)(av); |
520 |
|
} |
521 |
|
if (at->kid == NULL) |
522 |
|
return; |
523 |
|
for (i = 0; i < 8; i++) /* transfer and free children */ |
524 |
< |
loadatree(at->kid+i); |
524 |
> |
unloadatree(at->kid+i, f); |
525 |
|
freeambtree(at->kid); |
526 |
+ |
at->kid = NULL; |
527 |
+ |
} |
528 |
+ |
|
529 |
+ |
|
530 |
+ |
static AMBVAL **avlist1, **avlist2; /* ambient value lists for sorting */ |
531 |
+ |
static int i_avlist; /* index for lists */ |
532 |
+ |
|
533 |
+ |
|
534 |
+ |
static |
535 |
+ |
av2list(av) |
536 |
+ |
AMBVAL *av; |
537 |
+ |
{ |
538 |
+ |
#ifdef DEBUG |
539 |
+ |
if (i_avlist >= nambvals) |
540 |
+ |
error(CONSISTENCY, "too many ambient values in av2list1"); |
541 |
+ |
#endif |
542 |
+ |
avlist1[i_avlist] = avlist2[i_avlist] = av; |
543 |
+ |
i_avlist++; |
544 |
+ |
} |
545 |
+ |
|
546 |
+ |
|
547 |
+ |
static int |
548 |
+ |
alatcmp(avp1, avp2) /* compare ambient values for MRA */ |
549 |
+ |
AMBVAL **avp1, **avp2; |
550 |
+ |
{ |
551 |
+ |
return((**avp2).latick - (**avp1).latick); |
552 |
+ |
} |
553 |
+ |
|
554 |
+ |
|
555 |
+ |
static int |
556 |
+ |
aposcmp(avp1, avp2) /* compare ambient value positions */ |
557 |
+ |
AMBVAL **avp1, **avp2; |
558 |
+ |
{ |
559 |
+ |
return(*avp1 - *avp2); |
560 |
+ |
} |
561 |
+ |
|
562 |
+ |
|
563 |
+ |
#ifdef DEBUG |
564 |
+ |
static int |
565 |
+ |
avlmemi(avaddr) /* find list position from address */ |
566 |
+ |
AMBVAL *avaddr; |
567 |
+ |
{ |
568 |
+ |
register AMBVAL **avlpp; |
569 |
+ |
|
570 |
+ |
avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2, |
571 |
+ |
nambvals, sizeof(AMBVAL *), aposcmp); |
572 |
+ |
if (avlpp == NULL) |
573 |
+ |
error(CONSISTENCY, "address not found in avlmemi"); |
574 |
+ |
return(avlpp - avlist2); |
575 |
+ |
} |
576 |
+ |
#else |
577 |
+ |
#define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \ |
578 |
+ |
nambvals,sizeof(AMBVAL *),aposcmp) - avlist2) |
579 |
+ |
#endif |
580 |
+ |
|
581 |
+ |
|
582 |
+ |
static |
583 |
+ |
sortambvals(always) /* resort ambient values */ |
584 |
+ |
int always; |
585 |
+ |
{ |
586 |
+ |
AMBTREE oldatrunk; |
587 |
+ |
AMBVAL tav, *tap, *pnext; |
588 |
+ |
register int i, j; |
589 |
+ |
/* |
590 |
+ |
* The idea here is to minimize memory thrashing |
591 |
+ |
* in VM systems by improving reference locality. |
592 |
+ |
* We do this by periodically sorting our stored ambient |
593 |
+ |
* values in memory in order of most recently to least |
594 |
+ |
* recently accessed. This ordering was chosen so that new |
595 |
+ |
* ambient values (which tend to be less important) go into |
596 |
+ |
* higher memory with the infrequently accessed values. |
597 |
+ |
* Since we expect our values to need sorting less |
598 |
+ |
* frequently as the process continues, we double our |
599 |
+ |
* waiting interval after each call. |
600 |
+ |
* This routine is also called by setambacc() with |
601 |
+ |
* the "always" parameter set to 1 so that the ambient |
602 |
+ |
* tree will be rebuilt with the new accuracy parameter. |
603 |
+ |
*/ |
604 |
+ |
if (tracktime) { /* allocate pointer arrays to sort */ |
605 |
+ |
avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); |
606 |
+ |
avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); |
607 |
+ |
} else |
608 |
+ |
avlist1 = avlist2 = NULL; |
609 |
+ |
if (avlist2 == NULL) { /* no time tracking -- rebuild tree? */ |
610 |
+ |
if (avlist1 != NULL) |
611 |
+ |
free((char *)avlist1); |
612 |
+ |
if (always) { /* rebuild without sorting */ |
613 |
+ |
copystruct(&oldatrunk, &atrunk); |
614 |
+ |
atrunk.alist = NULL; |
615 |
+ |
atrunk.kid = NULL; |
616 |
+ |
unloadatree(&oldatrunk, avinsert); |
617 |
+ |
} |
618 |
+ |
} else { /* sort memory by last access time */ |
619 |
+ |
/* |
620 |
+ |
* Sorting memory is tricky because it isn't contiguous. |
621 |
+ |
* We have to sort an array of pointers by MRA and also |
622 |
+ |
* by memory position. We then copy values in "loops" |
623 |
+ |
* to minimize memory hits. Nevertheless, we will visit |
624 |
+ |
* everyone at least twice, and this is an expensive process |
625 |
+ |
* when we're thrashing, which is when we need to do it. |
626 |
+ |
*/ |
627 |
+ |
#ifdef DEBUG |
628 |
+ |
sprintf(errmsg, "sorting %u ambient values...", nambvals); |
629 |
+ |
eputs(errmsg); |
630 |
+ |
#endif |
631 |
+ |
i_avlist = 0; |
632 |
+ |
unloadatree(&atrunk, av2list); /* empty current tree */ |
633 |
+ |
#ifdef DEBUG |
634 |
+ |
if (i_avlist < nambvals) |
635 |
+ |
error(CONSISTENCY, "missing ambient values in sortambvals"); |
636 |
+ |
#endif |
637 |
+ |
qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp); |
638 |
+ |
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
639 |
+ |
for (i = 0; i < nambvals; i++) { |
640 |
+ |
if (avlist1[i] == NULL) |
641 |
+ |
continue; |
642 |
+ |
tap = avlist2[i]; |
643 |
+ |
copystruct(&tav, tap); |
644 |
+ |
for (j = i; (pnext = avlist1[j]) != tap; |
645 |
+ |
j = avlmemi(pnext)) { |
646 |
+ |
copystruct(avlist2[j], pnext); |
647 |
+ |
avinsert(avlist2[j]); |
648 |
+ |
avlist1[j] = NULL; |
649 |
+ |
} |
650 |
+ |
copystruct(avlist2[j], &tav); |
651 |
+ |
avinsert(avlist2[j]); |
652 |
+ |
avlist1[j] = NULL; |
653 |
+ |
} |
654 |
+ |
free((char *)avlist1); |
655 |
+ |
free((char *)avlist2); |
656 |
+ |
if (sortintvl < SORT_INTVL<<6) |
657 |
+ |
sortintvl <<= 1; /* wait twice as long next */ |
658 |
+ |
#ifdef DEBUG |
659 |
+ |
eputs("done\n"); |
660 |
+ |
#endif |
661 |
+ |
} |
662 |
+ |
if (ambclock >= MAXACLOCK) |
663 |
+ |
ambclock = MAXACLOCK/2; |
664 |
+ |
lastsort = ambclock; |
665 |
|
} |
666 |
|
|
667 |
|
|