ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/ambient.c
(Generate patch)

Comparing ray/src/rt/ambient.c (file contents):
Revision 2.20 by greg, Thu Aug 5 10:02:00 1993 UTC vs.
Revision 2.28 by greg, Sun Apr 30 13:24:36 1995 UTC

# Line 1 | Line 1
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";
# Line 27 | Line 27 | typedef struct ambtree {
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  
# Line 38 | Line 40 | static AMBTREE atrunk;         /* our ambient trunk node */
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*32)
52 + #endif
53 + #ifndef MAX_SORT_INTVL
54 + #define MAX_SORT_INTVL  (SORT_INTVL<<8)
55 + #endif
56 +
57 + static unsigned long  ambclock = 0;     /* ambient access clock */
58 + static unsigned int  nambvals = 0;      /* number of stored ambient values */
59 + static unsigned long  lastsort = 0;     /* time of last value sort */
60 + static long  sortintvl = SORT_INTVL;    /* time until next sort */
61 +
62 + #define MAXACLOCK       (1L<<30)        /* clock turnover value */
63 +        /*
64 +         * Track access times unless we are sharing ambient values
65 +         * through memory on a multiprocessor, when we want to avoid
66 +         * claiming our own memory (copy on write).
67 +         */
68 + #define tracktime       (shm_boundary == NULL || ambfp == NULL)
69 +
70   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
71  
72   #define  newambval()    (AMBVAL *)bmalloc(sizeof(AMBVAL))
73  
45 #define  newambtree()   (AMBTREE *)calloc(8, sizeof(AMBTREE))
46 #define  freeambtree(t) free((char *)(t))
47
74   extern long  ftell(), lseek();
75 < static int  initambfile(), avsave(), avinsert(), loadtree();
75 > static int  initambfile(), avsave(), avinsert(), sortambvals();
76   static AMBVAL  *avstore();
77   #ifdef  F_SETLKW
78   static  aflock();
# Line 56 | Line 82 | static  aflock();
82   setambres(ar)                           /* set ambient resolution */
83   int  ar;
84   {
85 <        ambres = ar;                    /* may be done already */
85 >        ambres = ar < 0 ? 0 : ar;               /* may be done already */
86                                                  /* set min & max radii */
87          if (ar <= 0) {
88 <                minarad = 0.0;
88 >                minarad = 0;
89                  maxarad = thescene.cusize / 2.0;
90          } else {
91                  minarad = thescene.cusize / ar;
92 <                maxarad = 16.0 * minarad;               /* heuristic */
92 >                maxarad = 16 * minarad;                 /* heuristic */
93                  if (maxarad > thescene.cusize / 2.0)
94                          maxarad = thescene.cusize / 2.0;
95          }
96 <        if (maxarad <= FTINY)
97 <                maxarad = .001;
96 >        if (minarad <= FTINY)
97 >                minarad = 10*FTINY;
98 >        if (maxarad <= minarad)
99 >                maxarad = 64 * minarad;
100   }
101  
102  
103 < resetambacc(newa)                       /* change ambient accuracy setting */
103 > setambacc(newa)                         /* set ambient accuracy */
104   double  newa;
105   {
106 <        AMBTREE  oldatrunk;
106 >        static double  oldambacc = -1.0;
107  
108 <        if (fabs(newa - ambacc) < 0.01)
108 >        ambacc = newa < 0.0 ? 0.0 : newa;       /* may be done already */
109 >        if (oldambacc < -FTINY)
110 >                oldambacc = ambacc;     /* do nothing first call */
111 >        if (fabs(newa - oldambacc) < 0.01)
112                  return;                 /* insignificant -- don't bother */
82        ambacc = newa;
113          if (ambacc <= FTINY)
114                  return;                 /* cannot build new tree */
115                                          /* else need to rebuild tree */
116 <        copystruct(&oldatrunk, &atrunk);
117 <        atrunk.alist = NULL;
88 <        atrunk.kid = NULL;
89 <        loadtree(&oldatrunk);
116 >        sortambvals(1);
117 >        oldambacc = ambacc;             /* remeber setting for next call */
118   }
119  
120  
# Line 97 | Line 125 | char  *afile;
125          AMBVAL  amb;
126                                                  /* init ambient limits */
127          setambres(ambres);
128 +        setambacc(ambacc);
129          if (afile == NULL)
130                  return;
131          if (ambacc <= FTINY) {
132 <                sprintf(errmsg, "zero ambient accuracy so \"%s\" not loaded",
132 >                sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened",
133                                  afile);
134                  error(WARNING, errmsg);
135                  return;
# Line 110 | Line 139 | char  *afile;
139                  initambfile(0);
140                  headlen = ftell(ambfp);
141                  while (readambval(&amb, ambfp))
142 <                        avinsert(avstore(&amb), &atrunk,
114 <                                        thescene.cuorg, thescene.cusize);
142 >                        avinsert(avstore(&amb));
143                                                  /* align */
144                  fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
145          } else if ((ambfp = fopen(afile, "w+")) != NULL)
# Line 172 | Line 200 | register RAY  *r;
200                          goto dumbamb;
201                  return;
202          }
203 +                                                /* resort memory? */
204 +        sortambvals(0);
205                                                  /* get ambient value */
206          setcolor(acol, 0.0, 0.0, 0.0);
207          d = sumambient(acol, r, rdepth,
# Line 207 | Line 237 | double s;
237                                          /* do this node */
238          wsum = 0.0;
239          for (av = at->alist; av != NULL; av = av->next) {
240 +                if (tracktime)
241 +                        av->latick = ambclock++;
242                  /*
243                   *  Ambient level test.
244                   */
245 <                if (av->lvl > al || av->weight < r->rweight-FTINY)
245 >                if (av->lvl > al)       /* list sorted, so this works */
246 >                        break;
247 >                if (av->weight < r->rweight-FTINY)
248                          continue;
249                  /*
250                   *  Ambient radius test.
# Line 343 | Line 377 | int  creat;
377   #ifdef MSDOS
378          setmode(fileno(ambfp), O_BINARY);
379   #endif
380 <        setbuf(ambfp, bmalloc(BUFSIZ));
380 >        setbuf(ambfp, bmalloc(BUFSIZ+8));
381          if (creat) {                    /* new file */
382 +                newheader("RADIANCE", ambfp);
383                  fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ",
384                                  progname, colval(ambval,RED),
385                                  colval(ambval,GRN), colval(ambval,BLU),
# Line 365 | Line 400 | static
400   avsave(av)                              /* insert and save an ambient value */
401   AMBVAL  *av;
402   {
403 <        avinsert(avstore(av), &atrunk, thescene.cuorg, thescene.cusize);
403 >        avinsert(avstore(av));
404          if (ambfp == NULL)
405                  return;
406          if (writambval(av, ambfp) < 0)
# Line 388 | Line 423 | register AMBVAL  *aval;
423          if ((av = newambval()) == NULL)
424                  error(SYSTEM, "out of memory in avstore");
425          copystruct(av, aval);
426 +        av->latick = ambclock;
427 +        av->next = NULL;
428 +        nambvals++;
429          return(av);
430   }
431  
432  
433 + #define ATALLOCSZ       512             /* #/8 trees to allocate at once */
434 +
435 + static AMBTREE  *atfreelist = NULL;     /* free ambient tree structures */
436 +
437 +
438   static
439 < avinsert(av, at, c0, s)                 /* insert ambient value in a tree */
439 > AMBTREE *
440 > newambtree()                            /* allocate 8 ambient tree structs */
441 > {
442 >        register AMBTREE  *atp, *upperlim;
443 >
444 >        if (atfreelist == NULL) {       /* get more nodes */
445 >                atfreelist = (AMBTREE *)bmalloc(ATALLOCSZ*8*sizeof(AMBTREE));
446 >                if (atfreelist == NULL)
447 >                        return(NULL);
448 >                                        /* link new free list */
449 >                upperlim = atfreelist + 8*(ATALLOCSZ-1);
450 >                for (atp = atfreelist; atp < upperlim; atp += 8)
451 >                        atp->kid = atp + 8;
452 >                atp->kid = NULL;
453 >        }
454 >        atp = atfreelist;
455 >        atfreelist = atp->kid;
456 >        bzero((char *)atp, 8*sizeof(AMBTREE));
457 >        return(atp);
458 > }
459 >
460 >
461 > static
462 > freeambtree(atp)                        /* free 8 ambient tree structs */
463 > AMBTREE  *atp;
464 > {
465 >        atp->kid = atfreelist;
466 >        atfreelist = atp;
467 > }
468 >
469 >
470 > static
471 > avinsert(av)                            /* insert ambient value in our tree */
472   register AMBVAL  *av;
398 register AMBTREE  *at;
399 FVECT  c0;
400 double  s;
473   {
474 +        register AMBTREE  *at;
475 +        register AMBVAL  *ap;
476 +        AMBVAL  avh;
477          FVECT  ck0;
478 +        double  s;
479          int  branch;
480          register int  i;
481  
482          if (av->rad <= FTINY)
483                  error(CONSISTENCY, "zero ambient radius in avinsert");
484 <        VCOPY(ck0, c0);
484 >        at = &atrunk;
485 >        VCOPY(ck0, thescene.cuorg);
486 >        s = thescene.cusize;
487          while (s*(OCTSCALE/2) > av->rad*ambacc) {
488                  if (at->kid == NULL)
489                          if ((at->kid = newambtree()) == NULL)
# Line 419 | Line 497 | double s;
497                          }
498                  at = at->kid + branch;
499          }
500 <        av->next = at->alist;
501 <        at->alist = av;
500 >        avh.next = at->alist;           /* order by increasing level */
501 >        for (ap = &avh; ap->next != NULL; ap = ap->next)
502 >                if (ap->next->lvl >= av->lvl)
503 >                        break;
504 >        av->next = ap->next;
505 >        ap->next = av;
506 >        at->alist = avh.next;
507   }
508  
509  
510   static
511 < loadtree(at)                            /* move tree to main store */
511 > unloadatree(at, f)                      /* unload an ambient value tree */
512   register AMBTREE  *at;
513 + int     (*f)();
514   {
515          register AMBVAL  *av;
516          register int  i;
517                                          /* transfer values at this node */
518          for (av = at->alist; av != NULL; av = at->alist) {
519                  at->alist = av->next;
520 <                avinsert(av, &atrunk, thescene.cuorg, thescene.cusize);
520 >                (*f)(av);
521          }
522 +        if (at->kid == NULL)
523 +                return;
524          for (i = 0; i < 8; i++)         /* transfer and free children */
525 <                loadtree(at->kid+i);
525 >                unloadatree(at->kid+i, f);
526          freeambtree(at->kid);
527 +        at->kid = NULL;
528   }
529  
530  
531 + static AMBVAL   **avlist1, **avlist2;   /* ambient value lists for sorting */
532 + static int      i_avlist;               /* index for lists */
533 +
534 +
535 + static
536 + av2list(av)
537 + AMBVAL  *av;
538 + {
539 + #ifdef DEBUG
540 +        if (i_avlist >= nambvals)
541 +                error(CONSISTENCY, "too many ambient values in av2list1");
542 + #endif
543 +        avlist1[i_avlist] = avlist2[i_avlist] = av;
544 +        i_avlist++;
545 + }
546 +
547 +
548 + static int
549 + alatcmp(avp1, avp2)                     /* compare ambient values for MRA */
550 + AMBVAL  **avp1, **avp2;
551 + {
552 +        return((**avp2).latick - (**avp1).latick);
553 + }
554 +
555 +
556 + static int
557 + aposcmp(avp1, avp2)                     /* compare ambient value positions */
558 + AMBVAL  **avp1, **avp2;
559 + {
560 +        return(*avp1 - *avp2);
561 + }
562 +
563 +
564 + #ifdef DEBUG
565 + static int
566 + avlmemi(avaddr)                         /* find list position from address */
567 + AMBVAL  *avaddr;
568 + {
569 +        register AMBVAL  **avlpp;
570 +
571 +        avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
572 +                        nambvals, sizeof(AMBVAL *), aposcmp);
573 +        if (avlpp == NULL)
574 +                error(CONSISTENCY, "address not found in avlmemi");
575 +        return(avlpp - avlist2);
576 + }
577 + #else
578 + #define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \
579 +                                nambvals,sizeof(AMBVAL *),aposcmp) - avlist2)
580 + #endif
581 +
582 +
583 + static
584 + sortambvals(always)                     /* resort ambient values */
585 + int     always;
586 + {
587 +        AMBTREE  oldatrunk;
588 +        AMBVAL  tav, *tap, *pnext;
589 +        register int    i, j;
590 +                                        /* see if it's time yet */
591 +        if (!always && (ambclock < lastsort+sortintvl ||
592 +                        nambvals < SORT_THRESH))
593 +                return;
594 +        /*
595 +         * The idea here is to minimize memory thrashing
596 +         * in VM systems by improving reference locality.
597 +         * We do this by periodically sorting our stored ambient
598 +         * values in memory in order of most recently to least
599 +         * recently accessed.  This ordering was chosen so that new
600 +         * ambient values (which tend to be less important) go into
601 +         * higher memory with the infrequently accessed values.
602 +         *      Since we expect our values to need sorting less
603 +         * frequently as the process continues, we double our
604 +         * waiting interval after each call.
605 +         *      This routine is also called by setambacc() with
606 +         * the "always" parameter set to 1 so that the ambient
607 +         * tree will be rebuilt with the new accuracy parameter.
608 +         */
609 +        if (tracktime) {                /* allocate pointer arrays to sort */
610 +                avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
611 +                avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
612 +        } else
613 +                avlist1 = avlist2 = NULL;
614 +        if (avlist2 == NULL) {          /* no time tracking -- rebuild tree? */
615 +                if (avlist1 != NULL)
616 +                        free((char *)avlist1);
617 +                if (always) {           /* rebuild without sorting */
618 +                        copystruct(&oldatrunk, &atrunk);
619 +                        atrunk.alist = NULL;
620 +                        atrunk.kid = NULL;
621 +                        unloadatree(&oldatrunk, avinsert);
622 +                }
623 +        } else {                        /* sort memory by last access time */
624 +                /*
625 +                 * Sorting memory is tricky because it isn't contiguous.
626 +                 * We have to sort an array of pointers by MRA and also
627 +                 * by memory position.  We then copy values in "loops"
628 +                 * to minimize memory hits.  Nevertheless, we will visit
629 +                 * everyone at least twice, and this is an expensive process
630 +                 * when we're thrashing, which is when we need to do it.
631 +                 */
632 + #ifdef DEBUG
633 +                sprintf(errmsg, "sorting %u ambient values...", nambvals);
634 +                eputs(errmsg);
635 + #endif
636 +                i_avlist = 0;
637 +                unloadatree(&atrunk, av2list);  /* empty current tree */
638 + #ifdef DEBUG
639 +                if (i_avlist < nambvals)
640 +                        error(CONSISTENCY, "missing ambient values in sortambvals");
641 + #endif
642 +                qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp);
643 +                qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
644 +                for (i = 0; i < nambvals; i++) {
645 +                        if (avlist1[i] == NULL)
646 +                                continue;
647 +                        tap = avlist2[i];
648 +                        copystruct(&tav, tap);
649 +                        for (j = i; (pnext = avlist1[j]) != tap;
650 +                                        j = avlmemi(pnext)) {
651 +                                copystruct(avlist2[j], pnext);
652 +                                avinsert(avlist2[j]);
653 +                                avlist1[j] = NULL;
654 +                        }
655 +                        copystruct(avlist2[j], &tav);
656 +                        avinsert(avlist2[j]);
657 +                        avlist1[j] = NULL;
658 +                }
659 +                free((char *)avlist1);
660 +                free((char *)avlist2);
661 +                                                /* compute new sort interval */
662 +                sortintvl = ambclock - lastsort;
663 +                if (sortintvl > MAX_SORT_INTVL)
664 +                        sortintvl = MAX_SORT_INTVL;
665 +                else
666 +                        sortintvl <<= 1;        /* wait twice as long next */
667 + #ifdef DEBUG
668 +                eputs("done\n");
669 + #endif
670 +        }
671 +        if (ambclock >= MAXACLOCK)
672 +                ambclock = MAXACLOCK/2;
673 +        lastsort = ambclock;
674 + }
675 +
676 +
677   #ifdef  F_SETLKW
678  
679   static
# Line 472 | Line 705 | ambsync()                      /* synchronize ambient file */
705          aflock(F_WRLCK);
706                                  /* see if file has grown */
707          if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0)
708 <                error(SYSTEM, "cannot seek on ambient file");
708 >                goto seekerr;
709          if (n = flen - lastpos) {               /* file has grown */
710                  if (ambinp == NULL) {           /* use duplicate filedes */
711                          ambinp = fdopen(dup(fileno(ambfp)), "r");
# Line 480 | Line 713 | ambsync()                      /* synchronize ambient file */
713                                  error(SYSTEM, "fdopen failed in ambsync");
714                  }
715                  if (fseek(ambinp, lastpos, 0) < 0)
716 <                        error(SYSTEM, "fseek failed in ambsync");
716 >                        goto seekerr;
717                  while (n >= AMBVALSIZ) {        /* load contributed values */
718                          readambval(&avs, ambinp);
719 <                        avinsert(avstore(&avs), &atrunk,
487 <                                        thescene.cuorg, thescene.cusize);
719 >                        avinsert(avstore(&avs));
720                          n -= AMBVALSIZ;
721                  }
722 <                if (n)                          /* alignment */
723 <                        lseek(fileno(ambfp), flen-n, 0);
722 >                /*** seek always as safety measure
723 >                if (n) ***/                     /* alignment */
724 >                        if (lseek(fileno(ambfp), flen-n, 0) < 0)
725 >                                goto seekerr;
726          }
727 + #ifdef  DEBUG
728 +        if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) {
729 +                sprintf(errmsg, "ambient file buffer at %d rather than %d",
730 +                                ambfp->_ptr - ambfp->_base,
731 +                                nunflshed*AMBVALSIZ);
732 +                error(CONSISTENCY, errmsg);
733 +        }
734 + #endif
735   syncend:
736          n = fflush(ambfp);                      /* calls write() at last */
737 <        lastpos = lseek(fileno(ambfp), 0L, 1);
737 >        if ((lastpos = lseek(fileno(ambfp), 0L, 1)) < 0)
738 >                goto seekerr;
739          aflock(F_UNLCK);                        /* release file */
740          nunflshed = 0;
741          return(n);
742 + seekerr:
743 +        error(SYSTEM, "seek failed in ambsync");
744   }
745  
746   #else

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines