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.19 by greg, Wed Aug 4 14:22:11 1993 UTC vs.
Revision 2.35 by greg, Tue Nov 21 14:28:22 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 18 | Line 18 | static char SCCSid[] = "$SunId$ LBL";
18  
19   #include  "random.h"
20  
21 < #define  OCTSCALE       0.5     /* ceil((valid rad.)/(cube size)) */
21 > #ifndef  OCTSCALE
22 > #define  OCTSCALE       1.0     /* ceil((valid rad.)/(cube size)) */
23 > #endif
24 > #ifndef  AMBVWT
25 > #define  AMBVWT         250     /* relative ambient value weight (# calcs) */
26 > #endif
27  
28   typedef struct ambtree {
29          AMBVAL  *alist;         /* ambient value list */
# Line 27 | Line 32 | typedef struct ambtree {
32  
33   extern CUBE  thescene;          /* contains space boundaries */
34  
35 + extern char  *shm_boundary;     /* memory sharing boundary */
36 +
37   #define  MAXASET        511     /* maximum number of elements in ambient set */
38   OBJECT  ambset[MAXASET+1]={0};  /* ambient include/exclude set */
39  
# Line 38 | Line 45 | static AMBTREE atrunk;         /* our ambient trunk node */
45   static FILE  *ambfp = NULL;     /* ambient file pointer */
46   static int  nunflshed = 0;      /* number of unflushed ambient values */
47  
48 + #ifndef SORT_THRESH
49 + #ifdef BIGMEM
50 + #define SORT_THRESH     ((9L<<20)/sizeof(AMBVAL))
51 + #else
52 + #define SORT_THRESH     ((3L<<20)/sizeof(AMBVAL))
53 + #endif
54 + #endif
55 + #ifndef SORT_INTVL
56 + #define SORT_INTVL      (SORT_THRESH*256)
57 + #endif
58 + #ifndef MAX_SORT_INTVL
59 + #define MAX_SORT_INTVL  (SORT_INTVL<<4)
60 + #endif
61 +
62 + static COLOR  avsum = BLKCOLOR;         /* computed ambient value sum */
63 + static unsigned int  nambvals = 0;      /* total number of indirect values */
64 + static unsigned int  nambshare = 0;     /* number of values from file */
65 + static unsigned long  ambclock = 0;     /* ambient access clock */
66 + static unsigned long  lastsort = 0;     /* time of last value sort */
67 + static long  sortintvl = SORT_INTVL;    /* time until next sort */
68 +
69 + #define MAXACLOCK       (1L<<30)        /* clock turnover value */
70 +        /*
71 +         * Track access times unless we are sharing ambient values
72 +         * through memory on a multiprocessor, when we want to avoid
73 +         * claiming our own memory (copy on write).  Go ahead anyway
74 +         * if more than two thirds of our values are unshared.
75 +         */
76 + #define tracktime       (shm_boundary == NULL || nambvals > 3*nambshare)
77 +
78   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
79  
80   #define  newambval()    (AMBVAL *)bmalloc(sizeof(AMBVAL))
81  
45 #define  newambtree()   (AMBTREE *)calloc(8, sizeof(AMBTREE))
46
82   extern long  ftell(), lseek();
83 < static int  initambfile(), avsave(), avinsert();
83 > static int  initambfile(), avsave(), avinsert(), sortambvals();
84 > static AMBVAL  *avstore();
85   #ifdef  F_SETLKW
86   static  aflock();
87   #endif
# Line 54 | Line 90 | static  aflock();
90   setambres(ar)                           /* set ambient resolution */
91   int  ar;
92   {
93 +        ambres = ar < 0 ? 0 : ar;               /* may be done already */
94                                                  /* set min & max radii */
95          if (ar <= 0) {
96 <                minarad = 0.0;
96 >                minarad = 0;
97                  maxarad = thescene.cusize / 2.0;
98          } else {
99                  minarad = thescene.cusize / ar;
100 <                maxarad = 16.0 * minarad;               /* heuristic */
100 >                maxarad = 64 * minarad;                 /* heuristic */
101                  if (maxarad > thescene.cusize / 2.0)
102                          maxarad = thescene.cusize / 2.0;
103          }
104 <        if (maxarad <= FTINY)
105 <                maxarad = .001;
104 >        if (minarad <= FTINY)
105 >                minarad = 10*FTINY;
106 >        if (maxarad <= minarad)
107 >                maxarad = 64 * minarad;
108   }
109  
110  
111 + setambacc(newa)                         /* set ambient accuracy */
112 + double  newa;
113 + {
114 +        double  ambdiff;
115 +
116 +        if (newa < 0.0)
117 +                newa = 0.0;
118 +        ambdiff = fabs(newa - ambacc);
119 +        if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0)
120 +                sortambvals(1);                 /* rebuild tree */
121 + }
122 +
123 +
124   setambient(afile)                       /* initialize calculation */
125   char  *afile;
126   {
# Line 76 | Line 128 | char  *afile;
128          AMBVAL  amb;
129                                                  /* init ambient limits */
130          setambres(ambres);
131 +        setambacc(ambacc);
132          if (afile == NULL)
133                  return;
134 +        if (ambacc <= FTINY) {
135 +                sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened",
136 +                                afile);
137 +                error(WARNING, errmsg);
138 +                return;
139 +        }
140                                                  /* open ambient file */
141          if ((ambfp = fopen(afile, "r+")) != NULL) {
142                  initambfile(0);
143                  headlen = ftell(ambfp);
144                  while (readambval(&amb, ambfp))
145 <                        avinsert(&amb, &atrunk, thescene.cuorg,
87 <                                        thescene.cusize);
145 >                        avinsert(avstore(&amb));
146                                                  /* align */
147                  fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
148 +                nambshare = nambvals;
149          } else if ((ambfp = fopen(afile, "w+")) != NULL)
150                  initambfile(1);
151          else {
# Line 120 | Line 179 | OBJECT obj;
179   }
180  
181  
182 < ambient(acol, r)                /* compute ambient component for ray */
182 > ambient(acol, r, nrm)           /* compute ambient component for ray */
183   COLOR  acol;
184   register RAY  *r;
185 + FVECT  nrm;
186   {
187          static int  rdepth = 0;                 /* ambient recursion */
188          double  d;
# Line 141 | Line 201 | register RAY  *r;
201                  rdepth++;
202                  d = doambient(acol, r, r->rweight, NULL, NULL);
203                  rdepth--;
204 <                if (d == 0.0)
204 >                if (d <= FTINY)
205                          goto dumbamb;
206                  return;
207          }
208 +                                                /* resort memory? */
209 +        sortambvals(0);
210                                                  /* get ambient value */
211          setcolor(acol, 0.0, 0.0, 0.0);
212 <        d = sumambient(acol, r, rdepth,
212 >        d = sumambient(acol, r, nrm, rdepth,
213                          &atrunk, thescene.cuorg, thescene.cusize);
214 <        if (d > FTINY)
214 >        if (d > FTINY) {
215                  scalecolor(acol, 1.0/d);
216 <        else {
155 <                d = makeambient(acol, r, rdepth++);
156 <                rdepth--;
216 >                return;
217          }
218 +        rdepth++;                               /* need to cache new value */
219 +        d = makeambient(acol, r, nrm, rdepth-1);
220 +        rdepth--;
221          if (d > FTINY)
222                  return;
223   dumbamb:                                        /* return global value */
224          copycolor(acol, ambval);
225 + #if  AMBVWT
226 +        if (nambvals == 0)
227 +                return;
228 +        scalecolor(acol, (double)AMBVWT);
229 +        addcolor(acol, avsum);                  /* average in computations */
230 +        d = 1.0/(AMBVWT+nambvals);
231 +        scalecolor(acol, d);
232 + #endif
233   }
234  
235  
236   double
237 < sumambient(acol, r, al, at, c0, s)      /* get interpolated ambient value */
237 > sumambient(acol, r, rn, al, at, c0, s)  /* get interpolated ambient value */
238   COLOR  acol;
239   register RAY  *r;
240 + FVECT  rn;
241   int  al;
242   AMBTREE  *at;
243   FVECT  c0;
# Line 177 | Line 249 | double s;
249          int  i;
250          register int  j;
251          register AMBVAL  *av;
252 <                                        /* do this node */
252 >
253          wsum = 0.0;
254 +                                        /* do this node */
255          for (av = at->alist; av != NULL; av = av->next) {
256 +                if (tracktime)
257 +                        av->latick = ambclock++;
258                  /*
259                   *  Ambient level test.
260                   */
261 <                if (av->lvl > al || av->weight < r->rweight-FTINY)
261 >                if (av->lvl > al)       /* list sorted, so this works */
262 >                        break;
263 >                if (av->weight < r->rweight-FTINY)
264                          continue;
265                  /*
266                   *  Ambient radius test.
267                   */
268 <                e1 = 0.0;
269 <                for (j = 0; j < 3; j++) {
270 <                        d = av->pos[j] - r->rop[j];
271 <                        e1 += d * d;
272 <                }
268 >                d = av->pos[0] - r->rop[0];
269 >                e1 = d * d;
270 >                d = av->pos[1] - r->rop[1];
271 >                e1 += d * d;
272 >                d = av->pos[2] - r->rop[2];
273 >                e1 += d * d;
274                  e1 /= av->rad * av->rad;
275                  if (e1 > ambacc*ambacc*1.21)
276                          continue;
# Line 216 | Line 294 | double s;
294                   *  Jittering final test reduces image artifacts.
295                   */
296                  wt = sqrt(e1) + sqrt(e2);
297 <                wt *= .9 + .2*urand(9015+samplendx);
220 <                if (wt > ambacc)
297 >                if (wt > ambacc*(.9+.2*urand(9015+samplendx)))
298                          continue;
299                  if (wt <= 1e-3)
300                          wt = 1e3;
301                  else
302                          wt = 1.0 / wt;
303                  wsum += wt;
304 <                extambient(ct, av, r->rop, r->ron);
304 >                extambient(ct, av, r->rop, rn);
305                  scalecolor(ct, wt);
306                  addcolor(acol, ct);
307          }
# Line 243 | Line 320 | double s;
320                                  break;
321                  }
322                  if (j == 3)
323 <                        wsum += sumambient(acol, r, al, at->kid+i, ck0, s);
323 >                        wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s);
324          }
325          return(wsum);
326   }
327  
328  
329   double
330 < makeambient(acol, r, al)        /* make a new ambient value */
330 > makeambient(acol, r, rn, al)    /* make a new ambient value */
331   COLOR  acol;
332   register RAY  *r;
333 + FVECT  rn;
334   int  al;
335   {
336          AMBVAL  amb;
337          FVECT   gp, gd;
338                                                  /* compute weight */
339          amb.weight = pow(AVGREFL, (double)al);
340 <        if (r->rweight < 0.2*amb.weight)        /* heuristic */
340 >        if (r->rweight < 0.1*amb.weight)        /* heuristic */
341                  amb.weight = r->rweight;
342                                                  /* compute ambient */
343          amb.rad = doambient(acol, r, amb.weight, gp, gd);
344 <        if (amb.rad == 0.0)
344 >        if (amb.rad <= FTINY)
345                  return(0.0);
346                                                  /* store it */
347          VCOPY(amb.pos, r->rop);
# Line 274 | Line 352 | int  al;
352          VCOPY(amb.gdir, gd);
353                                                  /* insert into tree */
354          avsave(&amb);                           /* and save to file */
355 +        if (rn != r->ron)
356 +                extambient(acol, &amb, r->rop, rn);     /* texture */
357          return(amb.rad);
358   }
359  
# Line 316 | Line 396 | int  creat;
396   #ifdef MSDOS
397          setmode(fileno(ambfp), O_BINARY);
398   #endif
399 <        setbuf(ambfp, bmalloc(BUFSIZ));
399 >        setbuf(ambfp, bmalloc(BUFSIZ+8));
400          if (creat) {                    /* new file */
401 +                newheader("RADIANCE", ambfp);
402                  fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ",
403                                  progname, colval(ambval,RED),
404                                  colval(ambval,GRN), colval(ambval,BLU),
# Line 338 | Line 419 | static
419   avsave(av)                              /* insert and save an ambient value */
420   AMBVAL  *av;
421   {
422 <        avinsert(av, &atrunk, thescene.cuorg, thescene.cusize);
422 >        avinsert(avstore(av));
423          if (ambfp == NULL)
424                  return;
425          if (writambval(av, ambfp) < 0)
# Line 352 | Line 433 | writerr:
433   }
434  
435  
436 + static AMBVAL *
437 + avstore(aval)                           /* allocate memory and store aval */
438 + register AMBVAL  *aval;
439 + {
440 +        register AMBVAL  *av;
441 +
442 +        if ((av = newambval()) == NULL)
443 +                error(SYSTEM, "out of memory in avstore");
444 +        copystruct(av, aval);
445 +        av->latick = ambclock;
446 +        av->next = NULL;
447 +        addcolor(avsum, av->val);       /* add to sum for averaging */
448 +        nambvals++;
449 +        return(av);
450 + }
451 +
452 +
453 + #define ATALLOCSZ       512             /* #/8 trees to allocate at once */
454 +
455 + static AMBTREE  *atfreelist = NULL;     /* free ambient tree structures */
456 +
457 +
458   static
459 < avinsert(aval, at, c0, s)               /* insert ambient value in a tree */
460 < AMBVAL  *aval;
358 < register AMBTREE  *at;
359 < FVECT  c0;
360 < double  s;
459 > AMBTREE *
460 > newambtree()                            /* allocate 8 ambient tree structs */
461   {
462 +        register AMBTREE  *atp, *upperlim;
463 +
464 +        if (atfreelist == NULL) {       /* get more nodes */
465 +                atfreelist = (AMBTREE *)bmalloc(ATALLOCSZ*8*sizeof(AMBTREE));
466 +                if (atfreelist == NULL)
467 +                        return(NULL);
468 +                                        /* link new free list */
469 +                upperlim = atfreelist + 8*(ATALLOCSZ-1);
470 +                for (atp = atfreelist; atp < upperlim; atp += 8)
471 +                        atp->kid = atp + 8;
472 +                atp->kid = NULL;
473 +        }
474 +        atp = atfreelist;
475 +        atfreelist = atp->kid;
476 +        bzero((char *)atp, 8*sizeof(AMBTREE));
477 +        return(atp);
478 + }
479 +
480 +
481 + static
482 + freeambtree(atp)                        /* free 8 ambient tree structs */
483 + AMBTREE  *atp;
484 + {
485 +        atp->kid = atfreelist;
486 +        atfreelist = atp;
487 + }
488 +
489 +
490 + static
491 + avinsert(av)                            /* insert ambient value in our tree */
492 + register AMBVAL  *av;
493 + {
494 +        register AMBTREE  *at;
495 +        register AMBVAL  *ap;
496 +        AMBVAL  avh;
497          FVECT  ck0;
498 +        double  s;
499          int  branch;
364        register AMBVAL  *av;
500          register int  i;
501  
502 <        if ((av = newambval()) == NULL)
503 <                goto memerr;
504 <        copystruct(av, aval);
505 <        VCOPY(ck0, c0);
502 >        if (av->rad <= FTINY)
503 >                error(CONSISTENCY, "zero ambient radius in avinsert");
504 >        at = &atrunk;
505 >        VCOPY(ck0, thescene.cuorg);
506 >        s = thescene.cusize;
507          while (s*(OCTSCALE/2) > av->rad*ambacc) {
508                  if (at->kid == NULL)
509                          if ((at->kid = newambtree()) == NULL)
510 <                                goto memerr;
510 >                                error(SYSTEM, "out of memory in avinsert");
511                  s *= 0.5;
512                  branch = 0;
513                  for (i = 0; i < 3; i++)
# Line 381 | Line 517 | double s;
517                          }
518                  at = at->kid + branch;
519          }
520 <        av->next = at->alist;
521 <        at->alist = av;
522 <        return;
523 < memerr:
524 <        error(SYSTEM, "out of memory in avinsert");
520 >        avh.next = at->alist;           /* order by increasing level */
521 >        for (ap = &avh; ap->next != NULL; ap = ap->next)
522 >                if (ap->next->lvl >= av->lvl)
523 >                        break;
524 >        av->next = ap->next;
525 >        ap->next = av;
526 >        at->alist = avh.next;
527   }
528  
529  
530 + static
531 + unloadatree(at, f)                      /* unload an ambient value tree */
532 + register AMBTREE  *at;
533 + int     (*f)();
534 + {
535 +        register AMBVAL  *av;
536 +        register int  i;
537 +                                        /* transfer values at this node */
538 +        for (av = at->alist; av != NULL; av = at->alist) {
539 +                at->alist = av->next;
540 +                (*f)(av);
541 +        }
542 +        if (at->kid == NULL)
543 +                return;
544 +        for (i = 0; i < 8; i++)         /* transfer and free children */
545 +                unloadatree(at->kid+i, f);
546 +        freeambtree(at->kid);
547 +        at->kid = NULL;
548 + }
549 +
550 +
551 + static AMBVAL   **avlist1, **avlist2;   /* ambient value lists for sorting */
552 + static int      i_avlist;               /* index for lists */
553 +
554 +
555 + static
556 + av2list(av)
557 + AMBVAL  *av;
558 + {
559 + #ifdef DEBUG
560 +        if (i_avlist >= nambvals)
561 +                error(CONSISTENCY, "too many ambient values in av2list1");
562 + #endif
563 +        avlist1[i_avlist] = avlist2[i_avlist] = av;
564 +        i_avlist++;
565 + }
566 +
567 +
568 + static int
569 + alatcmp(avp1, avp2)                     /* compare ambient values for MRA */
570 + AMBVAL  **avp1, **avp2;
571 + {
572 +        return((**avp2).latick - (**avp1).latick);
573 + }
574 +
575 +
576 + static int
577 + aposcmp(avp1, avp2)                     /* compare ambient value positions */
578 + AMBVAL  **avp1, **avp2;
579 + {
580 +        return(*avp1 - *avp2);
581 + }
582 +
583 +
584 + #ifdef DEBUG
585 + static int
586 + avlmemi(avaddr)                         /* find list position from address */
587 + AMBVAL  *avaddr;
588 + {
589 +        register AMBVAL  **avlpp;
590 +
591 +        avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
592 +                        nambvals, sizeof(AMBVAL *), aposcmp);
593 +        if (avlpp == NULL)
594 +                error(CONSISTENCY, "address not found in avlmemi");
595 +        return(avlpp - avlist2);
596 + }
597 + #else
598 + #define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \
599 +                                nambvals,sizeof(AMBVAL *),aposcmp) - avlist2)
600 + #endif
601 +
602 +
603 + static
604 + sortambvals(always)                     /* resort ambient values */
605 + int     always;
606 + {
607 +        AMBTREE  oldatrunk;
608 +        AMBVAL  tav, *tap, *pnext;
609 +        register int    i, j;
610 +                                        /* see if it's time yet */
611 +        if (!always && (ambclock < lastsort+sortintvl ||
612 +                        nambvals < SORT_THRESH))
613 +                return;
614 +        /*
615 +         * The idea here is to minimize memory thrashing
616 +         * in VM systems by improving reference locality.
617 +         * We do this by periodically sorting our stored ambient
618 +         * values in memory in order of most recently to least
619 +         * recently accessed.  This ordering was chosen so that new
620 +         * ambient values (which tend to be less important) go into
621 +         * higher memory with the infrequently accessed values.
622 +         *      Since we expect our values to need sorting less
623 +         * frequently as the process continues, we double our
624 +         * waiting interval after each call.
625 +         *      This routine is also called by setambacc() with
626 +         * the "always" parameter set to 1 so that the ambient
627 +         * tree will be rebuilt with the new accuracy parameter.
628 +         */
629 +        if (tracktime) {                /* allocate pointer arrays to sort */
630 +                avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
631 +                avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
632 +        } else
633 +                avlist1 = avlist2 = NULL;
634 +        if (avlist2 == NULL) {          /* no time tracking -- rebuild tree? */
635 +                if (avlist1 != NULL)
636 +                        free((char *)avlist1);
637 +                if (always) {           /* rebuild without sorting */
638 +                        copystruct(&oldatrunk, &atrunk);
639 +                        atrunk.alist = NULL;
640 +                        atrunk.kid = NULL;
641 +                        unloadatree(&oldatrunk, avinsert);
642 +                }
643 +        } else {                        /* sort memory by last access time */
644 +                /*
645 +                 * Sorting memory is tricky because it isn't contiguous.
646 +                 * We have to sort an array of pointers by MRA and also
647 +                 * by memory position.  We then copy values in "loops"
648 +                 * to minimize memory hits.  Nevertheless, we will visit
649 +                 * everyone at least twice, and this is an expensive process
650 +                 * when we're thrashing, which is when we need to do it.
651 +                 */
652 + #ifdef DEBUG
653 +                sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...",
654 +                                nambvals, ambclock);
655 +                eputs(errmsg);
656 + #endif
657 +                i_avlist = 0;
658 +                unloadatree(&atrunk, av2list);  /* empty current tree */
659 + #ifdef DEBUG
660 +                if (i_avlist < nambvals)
661 +                        error(CONSISTENCY, "missing ambient values in sortambvals");
662 + #endif
663 +                qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp);
664 +                qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
665 +                for (i = 0; i < nambvals; i++) {
666 +                        if (avlist1[i] == NULL)
667 +                                continue;
668 +                        tap = avlist2[i];
669 +                        copystruct(&tav, tap);
670 +                        for (j = i; (pnext = avlist1[j]) != tap;
671 +                                        j = avlmemi(pnext)) {
672 +                                copystruct(avlist2[j], pnext);
673 +                                avinsert(avlist2[j]);
674 +                                avlist1[j] = NULL;
675 +                        }
676 +                        copystruct(avlist2[j], &tav);
677 +                        avinsert(avlist2[j]);
678 +                        avlist1[j] = NULL;
679 +                }
680 +                free((char *)avlist1);
681 +                free((char *)avlist2);
682 +                                                /* compute new sort interval */
683 +                sortintvl = ambclock - lastsort;
684 +                if (sortintvl >= MAX_SORT_INTVL/2)
685 +                        sortintvl = MAX_SORT_INTVL;
686 +                else
687 +                        sortintvl <<= 1;        /* wait twice as long next */
688 + #ifdef DEBUG
689 +                eputs("done\n");
690 + #endif
691 +        }
692 +        if (ambclock >= MAXACLOCK)
693 +                ambclock = MAXACLOCK/2;
694 +        lastsort = ambclock;
695 + }
696 +
697 +
698   #ifdef  F_SETLKW
699  
700   static
# Line 420 | Line 726 | ambsync()                      /* synchronize ambient file */
726          aflock(F_WRLCK);
727                                  /* see if file has grown */
728          if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0)
729 <                error(SYSTEM, "cannot seek on ambient file");
729 >                goto seekerr;
730          if (n = flen - lastpos) {               /* file has grown */
731                  if (ambinp == NULL) {           /* use duplicate filedes */
732                          ambinp = fdopen(dup(fileno(ambfp)), "r");
# Line 428 | Line 734 | ambsync()                      /* synchronize ambient file */
734                                  error(SYSTEM, "fdopen failed in ambsync");
735                  }
736                  if (fseek(ambinp, lastpos, 0) < 0)
737 <                        error(SYSTEM, "fseek failed in ambsync");
737 >                        goto seekerr;
738                  while (n >= AMBVALSIZ) {        /* load contributed values */
739                          readambval(&avs, ambinp);
740 <                        avinsert(&avs,&atrunk,thescene.cuorg,thescene.cusize);
740 >                        avinsert(avstore(&avs));
741                          n -= AMBVALSIZ;
742                  }
743 <                if (n)                          /* alignment */
744 <                        lseek(fileno(ambfp), flen-n, 0);
743 >                /*** seek always as safety measure
744 >                if (n) ***/                     /* alignment */
745 >                        if (lseek(fileno(ambfp), flen-n, 0) < 0)
746 >                                goto seekerr;
747          }
748 + #ifdef  DEBUG
749 +        if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) {
750 +                sprintf(errmsg, "ambient file buffer at %d rather than %d",
751 +                                ambfp->_ptr - ambfp->_base,
752 +                                nunflshed*AMBVALSIZ);
753 +                error(CONSISTENCY, errmsg);
754 +        }
755 + #endif
756   syncend:
757          n = fflush(ambfp);                      /* calls write() at last */
758 <        lastpos = lseek(fileno(ambfp), 0L, 1);
758 >        if ((lastpos = lseek(fileno(ambfp), 0L, 1)) < 0)
759 >                goto seekerr;
760          aflock(F_UNLCK);                        /* release file */
761          nunflshed = 0;
762          return(n);
763 + seekerr:
764 +        error(SYSTEM, "seek failed in ambsync");
765   }
766  
767   #else

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines