--- ray/src/rt/ambient.c 1995/04/27 14:12:05 2.26 +++ ray/src/rt/ambient.c 1995/10/17 18:22:47 2.32 @@ -1,4 +1,4 @@ -/* Copyright (c) 1993 Regents of the University of California */ +/* Copyright (c) 1995 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -18,7 +18,12 @@ static char SCCSid[] = "$SunId$ LBL"; #include "random.h" -#define OCTSCALE 0.5 /* ceil((valid rad.)/(cube size)) */ +#ifndef OCTSCALE +#define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */ +#endif +#ifndef AMBVWT +#define AMBVWT 250 /* relative ambient value weight (# calcs) */ +#endif typedef struct ambtree { AMBVAL *alist; /* ambient value list */ @@ -27,7 +32,7 @@ typedef struct ambtree { extern CUBE thescene; /* contains space boundaries */ -extern char *shm_boundary; /* shared memory boundary */ +extern char *shm_boundary; /* memory sharing boundary */ #define MAXASET 511 /* maximum number of elements in ambient set */ OBJECT ambset[MAXASET+1]={0}; /* ambient include/exclude set */ @@ -42,24 +47,31 @@ static int nunflshed = 0; /* number of unflushed ambi #ifndef SORT_THRESH #ifdef BIGMEM -#define SORT_THRESH (6*(1L<<20)/sizeof(AMBVAL)) +#define SORT_THRESH ((9L<<20)/sizeof(AMBVAL)) #else -#define SORT_THRESH (2*(1L<<20)/sizeof(AMBVAL)) +#define SORT_THRESH ((3L<<20)/sizeof(AMBVAL)) #endif #endif #ifndef SORT_INTVL -#define SORT_INTVL (SORT_THRESH*2) +#define SORT_INTVL (SORT_THRESH*256) #endif +#ifndef MAX_SORT_INTVL +#define MAX_SORT_INTVL (SORT_INTVL<<4) +#endif -static long ambclock = 0; /* ambient access clock */ -static int nambvals = 0; /* number of stored ambient values */ -static long lastsort = 0; /* time of last value sort */ +static COLOR avsum = BLKCOLOR; /* computed ambient value sum */ +static unsigned int nambvals = 0; /* number of computed ambient values */ +static unsigned long ambclock = 0; /* ambient access clock */ +static unsigned long lastsort = 0; /* time of last value sort */ static long sortintvl = SORT_INTVL; /* time until next sort */ #define MAXACLOCK (1L<<30) /* clock turnover value */ + /* + * Track access times unless we are sharing ambient values + * through memory on a multiprocessor, when we want to avoid + * claiming our own memory (copy on write). + */ #define tracktime (shm_boundary == NULL || ambfp == NULL) -#define need2sort (ambclock > lastsort+sortintvl && \ - nambvals > SORT_THRESH) #define AMBFLUSH (BUFSIZ/AMBVALSIZ) @@ -83,7 +95,7 @@ int ar; maxarad = thescene.cusize / 2.0; } else { minarad = thescene.cusize / ar; - maxarad = 16 * minarad; /* heuristic */ + maxarad = 64 * minarad; /* heuristic */ if (maxarad > thescene.cusize / 2.0) maxarad = thescene.cusize / 2.0; } @@ -190,26 +202,35 @@ register RAY *r; rdepth++; d = doambient(acol, r, r->rweight, NULL, NULL); rdepth--; - if (d == 0.0) + if (d <= FTINY) goto dumbamb; return; } + /* resort memory? */ + sortambvals(0); /* get ambient value */ - if (need2sort) - sortambvals(0); setcolor(acol, 0.0, 0.0, 0.0); d = sumambient(acol, r, rdepth, &atrunk, thescene.cuorg, thescene.cusize); - if (d > FTINY) + if (d > FTINY) { scalecolor(acol, 1.0/d); - else { - d = makeambient(acol, r, rdepth++); - rdepth--; + return; } + rdepth++; + d = makeambient(acol, r, rdepth-1); + rdepth--; if (d > FTINY) return; dumbamb: /* return global value */ copycolor(acol, ambval); +#if AMBVWT + if (nambvals == 0) + return; + scalecolor(acol, (double)AMBVWT); + addcolor(acol, avsum); /* average in computations */ + d = 1.0/(AMBVWT+nambvals); + scalecolor(acol, d); +#endif } @@ -228,8 +249,9 @@ double s; int i; register int j; register AMBVAL *av; - /* do this node */ + wsum = 0.0; + /* do this node */ for (av = at->alist; av != NULL; av = av->next) { if (tracktime) av->latick = ambclock++; @@ -243,11 +265,12 @@ double s; /* * Ambient radius test. */ - e1 = 0.0; - for (j = 0; j < 3; j++) { - d = av->pos[j] - r->rop[j]; - e1 += d * d; - } + d = av->pos[0] - r->rop[0]; + e1 = d * d; + d = av->pos[1] - r->rop[1]; + e1 += d * d; + d = av->pos[2] - r->rop[2]; + e1 += d * d; e1 /= av->rad * av->rad; if (e1 > ambacc*ambacc*1.21) continue; @@ -271,8 +294,7 @@ double s; * Jittering final test reduces image artifacts. */ wt = sqrt(e1) + sqrt(e2); - wt *= .9 + .2*urand(9015+samplendx); - if (wt > ambacc) + if (wt > ambacc*(.9+.2*urand(9015+samplendx))) continue; if (wt <= 1e-3) wt = 1e3; @@ -419,6 +441,7 @@ register AMBVAL *aval; copystruct(av, aval); av->latick = ambclock; av->next = NULL; + addcolor(avsum, av->val); /* add to sum for averaging */ nambvals++; return(av); } @@ -530,8 +553,10 @@ static av2list(av) 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++; } @@ -553,6 +578,25 @@ AMBVAL **avp1, **avp2; } +#ifdef DEBUG +static int +avlmemi(avaddr) /* find list position from address */ +AMBVAL *avaddr; +{ + register AMBVAL **avlpp; + + avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2, + nambvals, sizeof(AMBVAL *), aposcmp); + if (avlpp == NULL) + error(CONSISTENCY, "address not found in avlmemi"); + return(avlpp - avlist2); +} +#else +#define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \ + nambvals,sizeof(AMBVAL *),aposcmp) - avlist2) +#endif + + static sortambvals(always) /* resort ambient values */ int always; @@ -560,6 +604,10 @@ int always; AMBTREE oldatrunk; AMBVAL tav, *tap, *pnext; register int i, j; + /* see if it's time yet */ + if (!always && (ambclock < lastsort+sortintvl || + nambvals < SORT_THRESH)) + return; /* * The idea here is to minimize memory thrashing * in VM systems by improving reference locality. @@ -575,43 +623,49 @@ int always; * the "always" parameter set to 1 so that the ambient * tree will be rebuilt with the new accuracy parameter. */ - if (tracktime) { + 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) { /* rebuild tree? */ + if (avlist2 == NULL) { /* no time tracking -- rebuild tree? */ if (avlist1 != NULL) free((char *)avlist1); - if (!always) - return; - copystruct(&oldatrunk, &atrunk); - atrunk.alist = NULL; - atrunk.kid = NULL; - unloadatree(&oldatrunk, avinsert); + if (always) { /* rebuild without sorting */ + copystruct(&oldatrunk, &atrunk); + atrunk.alist = NULL; + atrunk.kid = NULL; + unloadatree(&oldatrunk, avinsert); + } } else { /* sort memory by last access time */ - i_avlist = 0; - unloadatree(&atrunk, av2list); /* empty current tree */ /* * Sorting memory is tricky because it isn't contiguous. * We have to sort an array of pointers by MRA and also * by memory position. We then copy values in "loops" * to minimize memory hits. Nevertheless, we will visit - * everyone at least once, and this is an expensive process + * everyone at least twice, and this is an expensive process * when we're thrashing, which is when we need to do it. */ +#ifdef DEBUG + sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...", + nambvals, ambclock); + eputs(errmsg); +#endif + i_avlist = 0; + unloadatree(&atrunk, av2list); /* empty current tree */ +#ifdef DEBUG + if (i_avlist < nambvals) + error(CONSISTENCY, "missing ambient values in sortambvals"); +#endif qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp); qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); for (i = 0; i < nambvals; i++) { - if (avlist1[i] == NULL || avlist1[i] == avlist2[i]) + if (avlist1[i] == NULL) continue; tap = avlist2[i]; copystruct(&tav, tap); for (j = i; (pnext = avlist1[j]) != tap; - j = (AMBVAL **)bsearch((char *)&pnext, - (char *)(avlist2+i),nambvals-i, - sizeof(AMBVAL *),aposcmp) - - avlist2) { + j = avlmemi(pnext)) { copystruct(avlist2[j], pnext); avinsert(avlist2[j]); avlist1[j] = NULL; @@ -622,8 +676,15 @@ int always; } free((char *)avlist1); free((char *)avlist2); - if (sortintvl < MAXACLOCK/4) + /* compute new sort interval */ + sortintvl = ambclock - lastsort; + if (sortintvl >= MAX_SORT_INTVL/2) + sortintvl = MAX_SORT_INTVL; + else sortintvl <<= 1; /* wait twice as long next */ +#ifdef DEBUG + eputs("done\n"); +#endif } if (ambclock >= MAXACLOCK) ambclock = MAXACLOCK/2;