--- ray/src/rt/ambient.c 1995/04/27 14:12:05 2.26 +++ ray/src/rt/ambient.c 1998/06/17 13:29:55 2.44 @@ -1,4 +1,4 @@ -/* Copyright (c) 1993 Regents of the University of California */ +/* Copyright (c) 1996 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -18,7 +18,9 @@ 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 typedef struct ambtree { AMBVAL *alist; /* ambient value list */ @@ -27,7 +29,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,31 +44,43 @@ 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<<1) #endif +#ifndef MAX_SORT_INTVL +#define MAX_SORT_INTVL (SORT_INTVL<<6) +#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 double avsum = 0.; /* computed ambient value sum (log) */ +static unsigned int navsum = 0; /* number of values in avsum */ +static unsigned int nambvals = 0; /* total number of indirect values */ +static unsigned int nambshare = 0; /* number of values from file */ +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 */ -#define tracktime (shm_boundary == NULL || ambfp == NULL) -#define need2sort (ambclock > lastsort+sortintvl && \ - nambvals > SORT_THRESH) + /* + * 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). Go ahead anyway + * if more than two thirds of our values are unshared. + * Compile with -Dtracktime=0 to turn this code off. + */ +#ifndef tracktime +#define tracktime (shm_boundary == NULL || nambvals > 3*nambshare) +#endif #define AMBFLUSH (BUFSIZ/AMBVALSIZ) #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(); @@ -83,7 +97,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; } @@ -97,25 +111,20 @@ int ar; setambacc(newa) /* set ambient accuracy */ double newa; { - static double oldambacc = -1.0; + double ambdiff; - ambacc = newa < 0.0 ? 0.0 : newa; /* may be done already */ - if (oldambacc < -FTINY) - oldambacc = ambacc; /* do nothing first call */ - if (fabs(newa - oldambacc) < 0.01) - return; /* insignificant -- don't bother */ - if (ambacc <= FTINY) - return; /* cannot build new tree */ - /* else need to rebuild tree */ - sortambvals(1); - oldambacc = ambacc; /* remeber setting for next call */ + if (newa < 0.0) + newa = 0.0; + ambdiff = fabs(newa - ambacc); + if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0) + sortambvals(1); /* rebuild tree */ } setambient(afile) /* initialize calculation */ char *afile; { - long headlen; + long pos, flen; AMBVAL amb; /* init ambient limits */ setambres(ambres); @@ -131,11 +140,21 @@ char *afile; /* open ambient file */ if ((ambfp = fopen(afile, "r+")) != NULL) { initambfile(0); - headlen = ftell(ambfp); + pos = ftell(ambfp); while (readambval(&amb, ambfp)) avinsert(avstore(&amb)); /* align */ - fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1); + pos += (long)nambvals*AMBVALSIZ; + flen = lseek(fileno(ambfp), 0L, 2); + if (flen != pos) { + sprintf(errmsg, + "ignoring last %ld values in ambient file (corrupted)", + (flen - pos)/AMBVALSIZ); + error(WARNING, errmsg); + fseek(ambfp, pos, 0); + ftruncate(fileno(ambfp), pos); + } + nambshare = nambvals; } else if ((ambfp = fopen(afile, "w+")) != NULL) initambfile(1); else { @@ -169,12 +188,13 @@ OBJECT obj; } -ambient(acol, r) /* compute ambient component for ray */ +ambient(acol, r, nrm) /* compute ambient component for ray */ COLOR acol; register RAY *r; +FVECT nrm; { static int rdepth = 0; /* ambient recursion */ - double d; + double d, l; if (ambdiv <= 0) /* no ambient calculation */ goto dumbamb; @@ -190,33 +210,48 @@ register RAY *r; rdepth++; d = doambient(acol, r, r->rweight, NULL, NULL); rdepth--; - if (d == 0.0) + if (d <= FTINY) goto dumbamb; return; } - /* get ambient value */ - if (need2sort) + + if (tracktime) /* sort to minimize thrashing */ sortambvals(0); + /* get ambient value */ setcolor(acol, 0.0, 0.0, 0.0); - d = sumambient(acol, r, rdepth, + d = sumambient(acol, r, nrm, 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++; /* need to cache new value */ + d = makeambient(acol, r, nrm, rdepth-1); + rdepth--; if (d > FTINY) return; dumbamb: /* return global value */ copycolor(acol, ambval); + if (ambvwt <= 0 | navsum == 0) + return; + l = bright(ambval); /* average in computations */ + if (l > FTINY) { + d = (log(l)*(double)ambvwt + avsum) / + (double)(ambvwt + navsum); + d = exp(d) / l; + scalecolor(acol, d); /* apply color of ambval */ + } else { + d = exp( avsum / (double)navsum ); + setcolor(acol, d, d, d); /* neutral color */ + } } double -sumambient(acol, r, al, at, c0, s) /* get interpolated ambient value */ +sumambient(acol, r, rn, al, at, c0, s) /* get interpolated ambient value */ COLOR acol; register RAY *r; +FVECT rn; int al; AMBTREE *at; FVECT c0; @@ -228,11 +263,12 @@ 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++; + av->latick = ambclock; /* * Ambient level test. */ @@ -243,11 +279,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,15 +308,14 @@ 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; else wt = 1.0 / wt; wsum += wt; - extambient(ct, av, r->rop, r->ron); + extambient(ct, av, r->rop, rn); scalecolor(ct, wt); addcolor(acol, ct); } @@ -298,27 +334,28 @@ double s; break; } if (j == 3) - wsum += sumambient(acol, r, al, at->kid+i, ck0, s); + wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s); } return(wsum); } double -makeambient(acol, r, al) /* make a new ambient value */ +makeambient(acol, r, rn, al) /* make a new ambient value */ COLOR acol; register RAY *r; +FVECT rn; int al; { AMBVAL amb; FVECT gp, gd; /* compute weight */ amb.weight = pow(AVGREFL, (double)al); - if (r->rweight < 0.2*amb.weight) /* heuristic */ + if (r->rweight < 0.1*amb.weight) /* heuristic */ amb.weight = r->rweight; /* compute ambient */ amb.rad = doambient(acol, r, amb.weight, gp, gd); - if (amb.rad == 0.0) + if (amb.rad <= FTINY) return(0.0); /* store it */ VCOPY(amb.pos, r->rop); @@ -329,6 +366,8 @@ int al; VCOPY(amb.gdir, gd); /* insert into tree */ avsave(&amb); /* and save to file */ + if (rn != r->ron) + extambient(acol, &amb, r->rop, rn); /* texture */ return(amb.rad); } @@ -374,10 +413,10 @@ int creat; setbuf(ambfp, bmalloc(BUFSIZ+8)); if (creat) { /* new file */ newheader("RADIANCE", ambfp); - fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ", + fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ", progname, colval(ambval,RED), colval(ambval,GRN), colval(ambval,BLU), - ambounce, ambacc); + ambvwt, ambounce, ambacc); fprintf(ambfp, "-ad %d -as %d -ar %d %s\n", ambdiv, ambssamp, ambres, octname==NULL ? "" : octname); @@ -413,6 +452,7 @@ avstore(aval) /* allocate memory and store aval */ register AMBVAL *aval; { register AMBVAL *av; + double d; if ((av = newambval()) == NULL) error(SYSTEM, "out of memory in avstore"); @@ -420,6 +460,11 @@ register AMBVAL *aval; av->latick = ambclock; av->next = NULL; nambvals++; + d = bright(av->val); + if (d > FTINY) { /* add to log sum for averaging */ + avsum += log(d); + navsum++; + } return(av); } @@ -522,26 +567,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"); - avlist1[i_avlist] = avlist2[i_avlist] = av; - i_avlist++; +#endif + 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); } @@ -553,6 +605,25 @@ AMBVAL **avp1, **avp2; } +#if 1 +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 +631,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,55 +650,70 @@ int always; * the "always" parameter set to 1 so that the ambient * tree will be rebuilt with the new accuracy parameter. */ - if (tracktime) { - avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); + if (tracktime) { /* allocate pointer arrays to sort */ avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); - } else - avlist1 = avlist2 = NULL; - if (avlist2 == NULL) { /* rebuild tree? */ - if (avlist1 != NULL) - free((char *)avlist1); - if (!always) - return; - copystruct(&oldatrunk, &atrunk); - atrunk.alist = NULL; - atrunk.kid = NULL; - unloadatree(&oldatrunk, avinsert); + 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; + 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. */ - qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp); +#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(struct avl), 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].p == 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) { + 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); - 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; @@ -672,7 +762,13 @@ ambsync() /* synchronize ambient file */ if (fseek(ambinp, lastpos, 0) < 0) goto seekerr; while (n >= AMBVALSIZ) { /* load contributed values */ - readambval(&avs, ambinp); + if (!readambval(&avs, ambinp)) { + sprintf(errmsg, + "ambient file corrupted near character %ld", + flen - n); + error(WARNING, errmsg); + break; + } avinsert(avstore(&avs)); n -= AMBVALSIZ; }