--- ray/src/rt/ambient.c 1995/04/29 10:51:28 2.27 +++ ray/src/rt/ambient.c 1995/05/02 15:28:36 2.29 @@ -18,7 +18,7 @@ static char SCCSid[] = "$SunId$ LBL"; #include "random.h" -#define OCTSCALE 0.5 /* ceil((valid rad.)/(cube size)) */ +#define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */ typedef struct ambtree { AMBVAL *alist; /* ambient value list */ @@ -48,8 +48,11 @@ static int nunflshed = 0; /* number of unflushed ambi #endif #endif #ifndef SORT_INTVL -#define SORT_INTVL (SORT_THRESH*8) +#define SORT_INTVL (SORT_THRESH*256) #endif +#ifndef MAX_SORT_INTVL +#define MAX_SORT_INTVL (SORT_INTVL<<4) +#endif static unsigned long ambclock = 0; /* ambient access clock */ static unsigned int nambvals = 0; /* number of stored ambient values */ @@ -63,8 +66,6 @@ static long sortintvl = SORT_INTVL; /* time until nex * claiming our own memory (copy on write). */ #define tracktime (shm_boundary == NULL || ambfp == NULL) -#define checksort() if (ambclock > lastsort+sortintvl && \ - nambvals > SORT_THRESH) sortambvals(0) #define AMBFLUSH (BUFSIZ/AMBVALSIZ) @@ -200,7 +201,7 @@ register RAY *r; return; } /* resort memory? */ - checksort(); + sortambvals(0); /* get ambient value */ setcolor(acol, 0.0, 0.0, 0.0); d = sumambient(acol, r, rdepth, @@ -233,8 +234,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++; @@ -248,11 +250,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; @@ -586,6 +589,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. @@ -625,7 +632,8 @@ int always; * when we're thrashing, which is when we need to do it. */ #ifdef DEBUG - sprintf(errmsg, "sorting %u ambient values...", nambvals); + sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...", + nambvals, ambclock); eputs(errmsg); #endif i_avlist = 0; @@ -653,7 +661,11 @@ int always; } free((char *)avlist1); free((char *)avlist2); - if (sortintvl < SORT_INTVL<<6) + /* compute new sort interval */ + sortintvl = ambclock - lastsort; + if (sortintvl > MAX_SORT_INTVL) + sortintvl = MAX_SORT_INTVL; + else sortintvl <<= 1; /* wait twice as long next */ #ifdef DEBUG eputs("done\n");