--- ray/src/rt/ambient.c 1996/05/28 14:24:44 2.37 +++ ray/src/rt/ambient.c 1996/07/09 21:56:44 2.40 @@ -1,4 +1,4 @@ -/* Copyright (c) 1995 Regents of the University of California */ +/* Copyright (c) 1996 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -50,10 +50,10 @@ static int nunflshed = 0; /* number of unflushed ambi #endif #endif #ifndef SORT_INTVL -#define SORT_INTVL (SORT_THRESH*256) +#define SORT_INTVL (SORT_THRESH<<4) #endif #ifndef MAX_SORT_INTVL -#define MAX_SORT_INTVL (SORT_INTVL<<4) +#define MAX_SORT_INTVL (SORT_INTVL<<8) #endif static COLOR avsum = BLKCOLOR; /* computed ambient value sum */ @@ -77,7 +77,7 @@ static long sortintvl = SORT_INTVL; /* time until nex #define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL)) extern long ftell(), lseek(); -static int initambfile(), avsave(), avinsert(), sortambvals(); +static int initambfile(), avsave(), avinsert(), sortambvals(), avlmemi(); static AMBVAL *avstore(); #ifdef F_SETLKW static aflock(); @@ -210,8 +210,9 @@ FVECT nrm; goto dumbamb; return; } - /* resort memory? */ - sortambvals(0); + + if (tracktime) /* sort to minimize thrashing */ + sortambvals(0); /* get ambient value */ setcolor(acol, 0.0, 0.0, 0.0); d = sumambient(acol, r, nrm, rdepth, @@ -257,7 +258,7 @@ double s; /* do this node */ for (av = at->alist; av != NULL; av = av->next) { if (tracktime) - av->latick = ambclock++; + av->latick = ambclock; /* * Ambient level test. */ @@ -551,28 +552,33 @@ int (*f)(); } -static AMBVAL **avlist1, **avlist2; /* ambient value lists for sorting */ +static struct avl { + AMBVAL *p; + unsigned long t; +} *avlist1; /* ambient value list with ticks */ +static AMBVAL **avlist2; /* memory positions for sorting */ static int i_avlist; /* index for lists */ static av2list(av) -AMBVAL *av; +register AMBVAL *av; { #ifdef DEBUG if (i_avlist >= nambvals) error(CONSISTENCY, "too many ambient values in av2list1"); #endif - avlist1[i_avlist] = avlist2[i_avlist] = av; - i_avlist++; + avlist1[i_avlist].p = avlist2[i_avlist] = av; + avlist1[i_avlist++].t = av->latick; } static int -alatcmp(avp1, avp2) /* compare ambient values for MRA */ -AMBVAL **avp1, **avp2; +alatcmp(av1, av2) /* compare ambient values for MRA */ +struct avl *av1, *av2; { - return((**avp2).latick - (**avp1).latick); + register long lc = av2->t - av1->t; + return(lc<0 ? -1 : lc>0 ? 1 : 0); } @@ -584,7 +590,7 @@ AMBVAL **avp1, **avp2; } -#ifdef DEBUG +#if 1 static int avlmemi(avaddr) /* find list position from address */ AMBVAL *avaddr; @@ -611,7 +617,7 @@ int always; AMBVAL tav, *tap, *pnext; register int i, j; /* see if it's time yet */ - if (!always && (ambclock < lastsort+sortintvl || + if (!always && (ambclock++ < lastsort+sortintvl || nambvals < SORT_THRESH)) return; /* @@ -630,13 +636,15 @@ int always; * tree will be rebuilt with the new accuracy parameter. */ if (tracktime) { /* allocate pointer arrays to sort */ - avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); - } else - avlist1 = avlist2 = NULL; - if (avlist2 == NULL) { /* no time tracking -- rebuild tree? */ - if (avlist1 != NULL) - free((char *)avlist1); + avlist1 = (struct avl *)malloc(nambvals*sizeof(struct avl)); + } else { + avlist2 = NULL; + avlist1 = NULL; + } + if (avlist1 == NULL) { /* no time tracking -- rebuild tree? */ + if (avlist2 != NULL) + free((char *)avlist2); if (always) { /* rebuild without sorting */ copystruct(&oldatrunk, &atrunk); atrunk.alist = NULL; @@ -663,22 +671,22 @@ int always; if (i_avlist < nambvals) error(CONSISTENCY, "missing ambient values in sortambvals"); #endif - qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp); + qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp); qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); for (i = 0; i < nambvals; i++) { - if (avlist1[i] == NULL) + if (avlist1[i].p == NULL) continue; tap = avlist2[i]; copystruct(&tav, tap); - for (j = i; (pnext = avlist1[j]) != tap; + for (j = i; (pnext = avlist1[j].p) != tap; j = avlmemi(pnext)) { copystruct(avlist2[j], pnext); avinsert(avlist2[j]); - avlist1[j] = NULL; + avlist1[j].p = NULL; } copystruct(avlist2[j], &tav); avinsert(avlist2[j]); - avlist1[j] = NULL; + avlist1[j].p = NULL; } free((char *)avlist1); free((char *)avlist2);