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.26 by greg, Thu Apr 27 14:12:05 1995 UTC

# Line 27 | Line 27 | typedef struct ambtree {
27  
28   extern CUBE  thescene;          /* contains space boundaries */
29  
30 + extern char  *shm_boundary;     /* shared memory 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     (6*(1L<<20)/sizeof(AMBVAL))
46 + #else
47 + #define SORT_THRESH     (2*(1L<<20)/sizeof(AMBVAL))
48 + #endif
49 + #endif
50 + #ifndef SORT_INTVL
51 + #define SORT_INTVL      (SORT_THRESH*2)
52 + #endif
53 +
54 + static long  ambclock = 0;      /* ambient access clock */
55 + static int  nambvals = 0;       /* number of stored ambient values */
56 + static long  lastsort = 0;      /* time of last value sort */
57 + static long  sortintvl = SORT_INTVL;    /* time until next sort */
58 +
59 + #define MAXACLOCK       (1L<<30)        /* clock turnover value */
60 + #define tracktime       (shm_boundary == NULL || ambfp == NULL)
61 + #define need2sort       (ambclock > lastsort+sortintvl && \
62 +                                nambvals > SORT_THRESH)
63 +
64   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
65  
66   #define  newambval()    (AMBVAL *)bmalloc(sizeof(AMBVAL))
67  
45 #define  newambtree()   (AMBTREE *)calloc(8, sizeof(AMBTREE))
46 #define  freeambtree(t) free((char *)(t))
47
68   extern long  ftell(), lseek();
69 < static int  initambfile(), avsave(), avinsert(), loadtree();
69 > static int  initambfile(), avsave(), avinsert(), sortambvals();
70   static AMBVAL  *avstore();
71   #ifdef  F_SETLKW
72   static  aflock();
# Line 56 | Line 76 | static  aflock();
76   setambres(ar)                           /* set ambient resolution */
77   int  ar;
78   {
79 <        ambres = ar;                    /* may be done already */
79 >        ambres = ar < 0 ? 0 : ar;               /* may be done already */
80                                                  /* set min & max radii */
81          if (ar <= 0) {
82 <                minarad = 0.0;
82 >                minarad = 0;
83                  maxarad = thescene.cusize / 2.0;
84          } else {
85                  minarad = thescene.cusize / ar;
86 <                maxarad = 16.0 * minarad;               /* heuristic */
86 >                maxarad = 16 * minarad;                 /* heuristic */
87                  if (maxarad > thescene.cusize / 2.0)
88                          maxarad = thescene.cusize / 2.0;
89          }
90 <        if (maxarad <= FTINY)
91 <                maxarad = .001;
90 >        if (minarad <= FTINY)
91 >                minarad = 10*FTINY;
92 >        if (maxarad <= minarad)
93 >                maxarad = 64 * minarad;
94   }
95  
96  
97 < resetambacc(newa)                       /* change ambient accuracy setting */
97 > setambacc(newa)                         /* set ambient accuracy */
98   double  newa;
99   {
100 <        AMBTREE  oldatrunk;
100 >        static double  oldambacc = -1.0;
101  
102 <        if (fabs(newa - ambacc) < 0.01)
102 >        ambacc = newa < 0.0 ? 0.0 : newa;       /* may be done already */
103 >        if (oldambacc < -FTINY)
104 >                oldambacc = ambacc;     /* do nothing first call */
105 >        if (fabs(newa - oldambacc) < 0.01)
106                  return;                 /* insignificant -- don't bother */
82        ambacc = newa;
107          if (ambacc <= FTINY)
108                  return;                 /* cannot build new tree */
109                                          /* else need to rebuild tree */
110 <        copystruct(&oldatrunk, &atrunk);
111 <        atrunk.alist = NULL;
88 <        atrunk.kid = NULL;
89 <        loadtree(&oldatrunk);
110 >        sortambvals(1);
111 >        oldambacc = ambacc;             /* remeber setting for next call */
112   }
113  
114  
# Line 97 | Line 119 | char  *afile;
119          AMBVAL  amb;
120                                                  /* init ambient limits */
121          setambres(ambres);
122 +        setambacc(ambacc);
123          if (afile == NULL)
124                  return;
125          if (ambacc <= FTINY) {
126 <                sprintf(errmsg, "zero ambient accuracy so \"%s\" not loaded",
126 >                sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened",
127                                  afile);
128                  error(WARNING, errmsg);
129                  return;
# Line 110 | Line 133 | char  *afile;
133                  initambfile(0);
134                  headlen = ftell(ambfp);
135                  while (readambval(&amb, ambfp))
136 <                        avinsert(avstore(&amb), &atrunk,
114 <                                        thescene.cuorg, thescene.cusize);
136 >                        avinsert(avstore(&amb));
137                                                  /* align */
138                  fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
139          } else if ((ambfp = fopen(afile, "w+")) != NULL)
# Line 173 | Line 195 | register RAY  *r;
195                  return;
196          }
197                                                  /* get ambient value */
198 +        if (need2sort)
199 +                sortambvals(0);
200          setcolor(acol, 0.0, 0.0, 0.0);
201          d = sumambient(acol, r, rdepth,
202                          &atrunk, thescene.cuorg, thescene.cusize);
# Line 207 | Line 231 | double s;
231                                          /* do this node */
232          wsum = 0.0;
233          for (av = at->alist; av != NULL; av = av->next) {
234 +                if (tracktime)
235 +                        av->latick = ambclock++;
236                  /*
237                   *  Ambient level test.
238                   */
239 <                if (av->lvl > al || av->weight < r->rweight-FTINY)
239 >                if (av->lvl > al)       /* list sorted, so this works */
240 >                        break;
241 >                if (av->weight < r->rweight-FTINY)
242                          continue;
243                  /*
244                   *  Ambient radius test.
# Line 343 | Line 371 | int  creat;
371   #ifdef MSDOS
372          setmode(fileno(ambfp), O_BINARY);
373   #endif
374 <        setbuf(ambfp, bmalloc(BUFSIZ));
374 >        setbuf(ambfp, bmalloc(BUFSIZ+8));
375          if (creat) {                    /* new file */
376 +                newheader("RADIANCE", ambfp);
377                  fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ",
378                                  progname, colval(ambval,RED),
379                                  colval(ambval,GRN), colval(ambval,BLU),
# Line 365 | Line 394 | static
394   avsave(av)                              /* insert and save an ambient value */
395   AMBVAL  *av;
396   {
397 <        avinsert(avstore(av), &atrunk, thescene.cuorg, thescene.cusize);
397 >        avinsert(avstore(av));
398          if (ambfp == NULL)
399                  return;
400          if (writambval(av, ambfp) < 0)
# Line 388 | Line 417 | register AMBVAL  *aval;
417          if ((av = newambval()) == NULL)
418                  error(SYSTEM, "out of memory in avstore");
419          copystruct(av, aval);
420 +        av->latick = ambclock;
421 +        av->next = NULL;
422 +        nambvals++;
423          return(av);
424   }
425  
426  
427 + #define ATALLOCSZ       512             /* #/8 trees to allocate at once */
428 +
429 + static AMBTREE  *atfreelist = NULL;     /* free ambient tree structures */
430 +
431 +
432   static
433 < avinsert(av, at, c0, s)                 /* insert ambient value in a tree */
433 > AMBTREE *
434 > newambtree()                            /* allocate 8 ambient tree structs */
435 > {
436 >        register AMBTREE  *atp, *upperlim;
437 >
438 >        if (atfreelist == NULL) {       /* get more nodes */
439 >                atfreelist = (AMBTREE *)bmalloc(ATALLOCSZ*8*sizeof(AMBTREE));
440 >                if (atfreelist == NULL)
441 >                        return(NULL);
442 >                                        /* link new free list */
443 >                upperlim = atfreelist + 8*(ATALLOCSZ-1);
444 >                for (atp = atfreelist; atp < upperlim; atp += 8)
445 >                        atp->kid = atp + 8;
446 >                atp->kid = NULL;
447 >        }
448 >        atp = atfreelist;
449 >        atfreelist = atp->kid;
450 >        bzero((char *)atp, 8*sizeof(AMBTREE));
451 >        return(atp);
452 > }
453 >
454 >
455 > static
456 > freeambtree(atp)                        /* free 8 ambient tree structs */
457 > AMBTREE  *atp;
458 > {
459 >        atp->kid = atfreelist;
460 >        atfreelist = atp;
461 > }
462 >
463 >
464 > static
465 > avinsert(av)                            /* insert ambient value in our tree */
466   register AMBVAL  *av;
398 register AMBTREE  *at;
399 FVECT  c0;
400 double  s;
467   {
468 +        register AMBTREE  *at;
469 +        register AMBVAL  *ap;
470 +        AMBVAL  avh;
471          FVECT  ck0;
472 +        double  s;
473          int  branch;
474          register int  i;
475  
476          if (av->rad <= FTINY)
477                  error(CONSISTENCY, "zero ambient radius in avinsert");
478 <        VCOPY(ck0, c0);
478 >        at = &atrunk;
479 >        VCOPY(ck0, thescene.cuorg);
480 >        s = thescene.cusize;
481          while (s*(OCTSCALE/2) > av->rad*ambacc) {
482                  if (at->kid == NULL)
483                          if ((at->kid = newambtree()) == NULL)
# Line 419 | Line 491 | double s;
491                          }
492                  at = at->kid + branch;
493          }
494 <        av->next = at->alist;
495 <        at->alist = av;
494 >        avh.next = at->alist;           /* order by increasing level */
495 >        for (ap = &avh; ap->next != NULL; ap = ap->next)
496 >                if (ap->next->lvl >= av->lvl)
497 >                        break;
498 >        av->next = ap->next;
499 >        ap->next = av;
500 >        at->alist = avh.next;
501   }
502  
503  
504   static
505 < loadtree(at)                            /* move tree to main store */
505 > unloadatree(at, f)                      /* unload an ambient value tree */
506   register AMBTREE  *at;
507 + int     (*f)();
508   {
509          register AMBVAL  *av;
510          register int  i;
511                                          /* transfer values at this node */
512          for (av = at->alist; av != NULL; av = at->alist) {
513                  at->alist = av->next;
514 <                avinsert(av, &atrunk, thescene.cuorg, thescene.cusize);
514 >                (*f)(av);
515          }
516 +        if (at->kid == NULL)
517 +                return;
518          for (i = 0; i < 8; i++)         /* transfer and free children */
519 <                loadtree(at->kid+i);
519 >                unloadatree(at->kid+i, f);
520          freeambtree(at->kid);
521 +        at->kid = NULL;
522   }
523  
524  
525 + static AMBVAL   **avlist1, **avlist2;   /* ambient value lists for sorting */
526 + static int      i_avlist;               /* index for lists */
527 +
528 +
529 + static
530 + av2list(av)
531 + AMBVAL  *av;
532 + {
533 +        if (i_avlist >= nambvals)
534 +                error(CONSISTENCY, "too many ambient values in av2list1");
535 +        avlist1[i_avlist] = avlist2[i_avlist] = av;
536 +        i_avlist++;
537 + }
538 +
539 +
540 + static int
541 + alatcmp(avp1, avp2)                     /* compare ambient values for MRA */
542 + AMBVAL  **avp1, **avp2;
543 + {
544 +        return((**avp2).latick - (**avp1).latick);
545 + }
546 +
547 +
548 + static int
549 + aposcmp(avp1, avp2)                     /* compare ambient value positions */
550 + AMBVAL  **avp1, **avp2;
551 + {
552 +        return(*avp1 - *avp2);
553 + }
554 +
555 +
556 + static
557 + sortambvals(always)                     /* resort ambient values */
558 + int     always;
559 + {
560 +        AMBTREE  oldatrunk;
561 +        AMBVAL  tav, *tap, *pnext;
562 +        register int    i, j;
563 +        /*
564 +         * The idea here is to minimize memory thrashing
565 +         * in VM systems by improving reference locality.
566 +         * We do this by periodically sorting our stored ambient
567 +         * values in memory in order of most recently to least
568 +         * recently accessed.  This ordering was chosen so that new
569 +         * ambient values (which tend to be less important) go into
570 +         * higher memory with the infrequently accessed values.
571 +         *      Since we expect our values to need sorting less
572 +         * frequently as the process continues, we double our
573 +         * waiting interval after each call.
574 +         *      This routine is also called by setambacc() with
575 +         * the "always" parameter set to 1 so that the ambient
576 +         * tree will be rebuilt with the new accuracy parameter.
577 +         */
578 +        if (tracktime) {
579 +                avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
580 +                avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
581 +        } else
582 +                avlist1 = avlist2 = NULL;
583 +        if (avlist2 == NULL) {          /* rebuild tree? */
584 +                if (avlist1 != NULL)
585 +                        free((char *)avlist1);
586 +                if (!always)
587 +                        return;
588 +                copystruct(&oldatrunk, &atrunk);
589 +                atrunk.alist = NULL;
590 +                atrunk.kid = NULL;
591 +                unloadatree(&oldatrunk, avinsert);
592 +        } else {                        /* sort memory by last access time */
593 +                i_avlist = 0;
594 +                unloadatree(&atrunk, av2list);  /* empty current tree */
595 +                /*
596 +                 * Sorting memory is tricky because it isn't contiguous.
597 +                 * We have to sort an array of pointers by MRA and also
598 +                 * by memory position.  We then copy values in "loops"
599 +                 * to minimize memory hits.  Nevertheless, we will visit
600 +                 * everyone at least once, and this is an expensive process
601 +                 * when we're thrashing, which is when we need to do it.
602 +                 */
603 +                qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp);
604 +                qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
605 +                for (i = 0; i < nambvals; i++) {
606 +                        if (avlist1[i] == NULL || avlist1[i] == avlist2[i])
607 +                                continue;
608 +                        tap = avlist2[i];
609 +                        copystruct(&tav, tap);
610 +                        for (j = i; (pnext = avlist1[j]) != tap;
611 +                                        j = (AMBVAL **)bsearch((char *)&pnext,
612 +                                                (char *)(avlist2+i),nambvals-i,
613 +                                                sizeof(AMBVAL *),aposcmp) -
614 +                                                avlist2) {
615 +                                copystruct(avlist2[j], pnext);
616 +                                avinsert(avlist2[j]);
617 +                                avlist1[j] = NULL;
618 +                        }
619 +                        copystruct(avlist2[j], &tav);
620 +                        avinsert(avlist2[j]);
621 +                        avlist1[j] = NULL;
622 +                }
623 +                free((char *)avlist1);
624 +                free((char *)avlist2);
625 +                if (sortintvl < MAXACLOCK/4)
626 +                        sortintvl <<= 1;        /* wait twice as long next */
627 +        }
628 +        if (ambclock >= MAXACLOCK)
629 +                ambclock = MAXACLOCK/2;
630 +        lastsort = ambclock;
631 + }
632 +
633 +
634   #ifdef  F_SETLKW
635  
636   static
# Line 472 | Line 662 | ambsync()                      /* synchronize ambient file */
662          aflock(F_WRLCK);
663                                  /* see if file has grown */
664          if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0)
665 <                error(SYSTEM, "cannot seek on ambient file");
665 >                goto seekerr;
666          if (n = flen - lastpos) {               /* file has grown */
667                  if (ambinp == NULL) {           /* use duplicate filedes */
668                          ambinp = fdopen(dup(fileno(ambfp)), "r");
# Line 480 | Line 670 | ambsync()                      /* synchronize ambient file */
670                                  error(SYSTEM, "fdopen failed in ambsync");
671                  }
672                  if (fseek(ambinp, lastpos, 0) < 0)
673 <                        error(SYSTEM, "fseek failed in ambsync");
673 >                        goto seekerr;
674                  while (n >= AMBVALSIZ) {        /* load contributed values */
675                          readambval(&avs, ambinp);
676 <                        avinsert(avstore(&avs), &atrunk,
487 <                                        thescene.cuorg, thescene.cusize);
676 >                        avinsert(avstore(&avs));
677                          n -= AMBVALSIZ;
678                  }
679 <                if (n)                          /* alignment */
680 <                        lseek(fileno(ambfp), flen-n, 0);
679 >                /*** seek always as safety measure
680 >                if (n) ***/                     /* alignment */
681 >                        if (lseek(fileno(ambfp), flen-n, 0) < 0)
682 >                                goto seekerr;
683          }
684 + #ifdef  DEBUG
685 +        if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) {
686 +                sprintf(errmsg, "ambient file buffer at %d rather than %d",
687 +                                ambfp->_ptr - ambfp->_base,
688 +                                nunflshed*AMBVALSIZ);
689 +                error(CONSISTENCY, errmsg);
690 +        }
691 + #endif
692   syncend:
693          n = fflush(ambfp);                      /* calls write() at last */
694 <        lastpos = lseek(fileno(ambfp), 0L, 1);
694 >        if ((lastpos = lseek(fileno(ambfp), 0L, 1)) < 0)
695 >                goto seekerr;
696          aflock(F_UNLCK);                        /* release file */
697          nunflshed = 0;
698          return(n);
699 + seekerr:
700 +        error(SYSTEM, "seek failed in ambsync");
701   }
702  
703   #else

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines