22 |
|
#define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */ |
23 |
|
#endif |
24 |
|
|
25 |
– |
extern char *shm_boundary; /* memory sharing boundary */ |
26 |
– |
|
25 |
|
#ifndef MAXASET |
26 |
|
#define MAXASET 4095 /* maximum number of elements in ambient set */ |
27 |
|
#endif |
35 |
|
static FILE *ambfp = NULL; /* ambient file pointer */ |
36 |
|
static int nunflshed = 0; /* number of unflushed ambient values */ |
37 |
|
|
40 |
– |
#ifndef SORT_THRESH |
41 |
– |
#ifdef SMLMEM |
42 |
– |
#define SORT_THRESH ((16L<<20)/sizeof(AMBVAL)) |
43 |
– |
#else |
44 |
– |
#define SORT_THRESH ((64L<<20)/sizeof(AMBVAL)) |
45 |
– |
#endif |
46 |
– |
#endif |
47 |
– |
#ifndef SORT_INTVL |
48 |
– |
#define SORT_INTVL (SORT_THRESH<<1) |
49 |
– |
#endif |
50 |
– |
#ifndef MAX_SORT_INTVL |
51 |
– |
#define MAX_SORT_INTVL (SORT_INTVL<<6) |
52 |
– |
#endif |
53 |
– |
|
54 |
– |
|
38 |
|
static double avsum = 0.; /* computed ambient value sum (log) */ |
39 |
|
static unsigned int navsum = 0; /* number of values in avsum */ |
40 |
|
static unsigned int nambvals = 0; /* total number of indirect values */ |
41 |
|
static unsigned int nambshare = 0; /* number of values from file */ |
59 |
– |
static unsigned long ambclock = 0; /* ambient access clock */ |
60 |
– |
static unsigned long lastsort = 0; /* time of last value sort */ |
61 |
– |
static long sortintvl = SORT_INTVL; /* time until next sort */ |
42 |
|
static FILE *ambinp = NULL; /* auxiliary file for input */ |
43 |
|
static long lastpos = -1; /* last flush position */ |
44 |
|
|
65 |
– |
#define MAXACLOCK (1L<<30) /* clock turnover value */ |
66 |
– |
/* |
67 |
– |
* Track access times unless we are sharing ambient values |
68 |
– |
* through memory on a multiprocessor, when we want to avoid |
69 |
– |
* claiming our own memory (copy on write). Go ahead anyway |
70 |
– |
* if more than two thirds of our values are unshared. |
71 |
– |
* Compile with -Dtracktime=0 to turn this code off. |
72 |
– |
*/ |
73 |
– |
#ifndef tracktime |
74 |
– |
#define tracktime (shm_boundary == NULL || nambvals > 3*nambshare) |
75 |
– |
#endif |
76 |
– |
|
45 |
|
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
46 |
|
|
47 |
|
#define newambval() (AMBVAL *)malloc(sizeof(AMBVAL)) |
48 |
|
|
49 |
+ |
#define tfunc(lwr, x, upr) (((x)-(lwr))/((upr)-(lwr))) |
50 |
+ |
|
51 |
|
static void initambfile(int creat); |
52 |
|
static void avsave(AMBVAL *av); |
53 |
|
static AMBVAL *avstore(AMBVAL *aval); |
56 |
|
|
57 |
|
typedef void unloadtf_t(AMBVAL *); |
58 |
|
static unloadtf_t avinsert; |
89 |
– |
static unloadtf_t av2list; |
59 |
|
static unloadtf_t avfree; |
60 |
|
static void unloadatree(AMBTREE *at, unloadtf_t *f); |
61 |
|
|
62 |
< |
static int aposcmp(const void *avp1, const void *avp2); |
94 |
< |
static int avlmemi(AMBVAL *avaddr); |
95 |
< |
static void sortambvals(int always); |
62 |
> |
static void sortambvals(void); |
63 |
|
|
64 |
+ |
static int plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang); |
65 |
+ |
static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, |
66 |
+ |
AMBTREE *at, FVECT c0, double s); |
67 |
+ |
static int makeambient(COLOR acol, RAY *r, FVECT rn, int al); |
68 |
+ |
static int extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv, |
69 |
+ |
FVECT uvw[3]); |
70 |
+ |
|
71 |
|
#ifdef F_SETLKW |
72 |
|
static void aflock(int typ); |
73 |
|
#endif |
107 |
|
if (fabs(newa - olda) >= .05*(newa + olda)) { |
108 |
|
ambacc = newa; |
109 |
|
if (ambacc > FTINY && nambvals > 0) |
110 |
< |
sortambvals(1); /* rebuild tree */ |
110 |
> |
sortambvals(); /* rebuild tree */ |
111 |
|
} |
112 |
|
} |
113 |
|
|
194 |
|
navsum = 0; |
195 |
|
nambvals = 0; |
196 |
|
nambshare = 0; |
223 |
– |
ambclock = 0; |
224 |
– |
lastsort = 0; |
225 |
– |
sortintvl = SORT_INTVL; |
197 |
|
} |
198 |
|
|
199 |
|
|
226 |
|
} |
227 |
|
} |
228 |
|
|
258 |
– |
/************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/ |
229 |
|
|
260 |
– |
#ifndef OLDAMB |
261 |
– |
|
262 |
– |
#define tfunc(lwr, x, upr) (((x)-(lwr))/((upr)-(lwr))) |
263 |
– |
|
264 |
– |
static int plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang); |
265 |
– |
static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, |
266 |
– |
AMBTREE *at, FVECT c0, double s); |
267 |
– |
static int makeambient(COLOR acol, RAY *r, FVECT rn, int al); |
268 |
– |
static int extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv, |
269 |
– |
FVECT uvw[3]); |
270 |
– |
|
230 |
|
void |
231 |
|
multambient( /* compute ambient component & multiply by coef. */ |
232 |
|
COLOR aval, |
234 |
|
FVECT nrm |
235 |
|
) |
236 |
|
{ |
237 |
+ |
static double logAvgAbsorp = 1; |
238 |
|
static int rdepth = 0; /* ambient recursion */ |
239 |
|
COLOR acol, caustic; |
240 |
|
int i, ok; |
246 |
|
if (ambPmap(aval, r, rdepth)) |
247 |
|
return; |
248 |
|
|
249 |
+ |
if (logAvgAbsorp > 0) /* exclude in -aw to avoid growth */ |
250 |
+ |
logAvgAbsorp = log(1.-AVGREFL); |
251 |
+ |
|
252 |
|
/* PMAP: Factor in specular-diffuse ambient (caustics) from photon |
253 |
|
* map, if enabled and ray is primary, else caustic is zero. Continue |
254 |
|
* with RADIANCE ambient calculation */ |
293 |
|
addcolor(aval, caustic); |
294 |
|
return; |
295 |
|
} |
333 |
– |
|
334 |
– |
if (tracktime) /* sort to minimize thrashing */ |
335 |
– |
sortambvals(0); |
296 |
|
/* interpolate ambient value */ |
297 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
298 |
|
d = sumambient(acol, r, nrm, rdepth, |
331 |
|
|
332 |
|
l = bright(ambval); /* average in computations */ |
333 |
|
if (l > FTINY) { |
334 |
< |
d = (log(l)*(double)ambvwt + avsum) / |
334 |
> |
d = (log(l)*(double)ambvwt + avsum + logAvgAbsorp*navsum) / |
335 |
|
(double)(ambvwt + navsum); |
336 |
|
d = exp(d) / l; |
337 |
|
scalecolor(aval, d); |
338 |
|
multcolor(aval, ambval); /* apply color of ambval */ |
339 |
|
} else { |
340 |
< |
d = exp( avsum / (double)navsum ); |
340 |
> |
d = exp( avsum/(double)navsum + logAvgAbsorp ); |
341 |
|
scalecolor(aval, d); /* neutral color */ |
342 |
|
} |
343 |
|
} |
435 |
|
double u, v, d, delta_r2, delta_t2; |
436 |
|
COLOR ct; |
437 |
|
FVECT uvw[3]; |
478 |
– |
/* record access */ |
479 |
– |
if (tracktime) |
480 |
– |
av->latick = ambclock; |
438 |
|
/* |
439 |
|
* Ambient level test |
440 |
|
*/ |
543 |
|
) |
544 |
|
{ |
545 |
|
const double min_d = 0.05; |
546 |
+ |
const double max_d = 20.; |
547 |
|
static FVECT my_uvw[3]; |
548 |
|
FVECT v1; |
549 |
|
int i; |
563 |
|
for (i = 3; i--; ) |
564 |
|
d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]); |
565 |
|
|
566 |
< |
if (d < min_d) /* should not use if we can avoid it */ |
566 |
> |
if (d < min_d) /* clamp min/max scaling */ |
567 |
|
d = min_d; |
568 |
+ |
else if (d > max_d) |
569 |
+ |
d = max_d; |
570 |
|
copycolor(cr, ap->val); |
571 |
|
scalecolor(cr, d); |
572 |
|
return(d > min_d); |
616 |
|
} |
617 |
|
|
618 |
|
|
659 |
– |
#else /* ! NEWAMB */ |
660 |
– |
|
661 |
– |
static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, |
662 |
– |
AMBTREE *at, FVECT c0, double s); |
663 |
– |
static double makeambient(COLOR acol, RAY *r, FVECT rn, int al); |
664 |
– |
static void extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv); |
665 |
– |
|
666 |
– |
|
667 |
– |
void |
668 |
– |
multambient( /* compute ambient component & multiply by coef. */ |
669 |
– |
COLOR aval, |
670 |
– |
RAY *r, |
671 |
– |
FVECT nrm |
672 |
– |
) |
673 |
– |
{ |
674 |
– |
static int rdepth = 0; /* ambient recursion */ |
675 |
– |
COLOR acol, caustic; |
676 |
– |
double d, l; |
677 |
– |
|
678 |
– |
/* PMAP: Factor in ambient from global photon map (if enabled) and return |
679 |
– |
* as all ambient components accounted for */ |
680 |
– |
if (ambPmap(aval, r, rdepth)) |
681 |
– |
return; |
682 |
– |
|
683 |
– |
/* PMAP: Otherwise factor in ambient from caustic photon map |
684 |
– |
* (ambPmapCaustic() returns zero if caustic photons disabled) and |
685 |
– |
* continue with RADIANCE ambient calculation */ |
686 |
– |
copycolor(caustic, aval); |
687 |
– |
ambPmapCaustic(caustic, r, rdepth); |
688 |
– |
|
689 |
– |
if (ambdiv <= 0) /* no ambient calculation */ |
690 |
– |
goto dumbamb; |
691 |
– |
/* check number of bounces */ |
692 |
– |
if (rdepth >= ambounce) |
693 |
– |
goto dumbamb; |
694 |
– |
/* check ambient list */ |
695 |
– |
if (ambincl != -1 && r->ro != NULL && |
696 |
– |
ambincl != inset(ambset, r->ro->omod)) |
697 |
– |
goto dumbamb; |
698 |
– |
|
699 |
– |
if (ambacc <= FTINY) { /* no ambient storage */ |
700 |
– |
copycolor(acol, aval); |
701 |
– |
rdepth++; |
702 |
– |
d = doambient(acol, r, r->rweight, NULL, NULL); |
703 |
– |
rdepth--; |
704 |
– |
if (d <= FTINY) |
705 |
– |
goto dumbamb; |
706 |
– |
copycolor(aval, acol); |
707 |
– |
|
708 |
– |
/* PMAP: add in caustic */ |
709 |
– |
addcolor(aval, caustic); |
710 |
– |
return; |
711 |
– |
} |
712 |
– |
|
713 |
– |
if (tracktime) /* sort to minimize thrashing */ |
714 |
– |
sortambvals(0); |
715 |
– |
/* interpolate ambient value */ |
716 |
– |
setcolor(acol, 0.0, 0.0, 0.0); |
717 |
– |
d = sumambient(acol, r, nrm, rdepth, |
718 |
– |
&atrunk, thescene.cuorg, thescene.cusize); |
719 |
– |
|
720 |
– |
if (d > FTINY) { |
721 |
– |
d = 1.0/d; |
722 |
– |
scalecolor(acol, d); |
723 |
– |
multcolor(aval, acol); |
724 |
– |
|
725 |
– |
/* PMAP: add in caustic */ |
726 |
– |
addcolor(aval, caustic); |
727 |
– |
return; |
728 |
– |
} |
729 |
– |
|
730 |
– |
rdepth++; /* need to cache new value */ |
731 |
– |
d = makeambient(acol, r, nrm, rdepth-1); |
732 |
– |
rdepth--; |
733 |
– |
|
734 |
– |
if (d > FTINY) { |
735 |
– |
multcolor(aval, acol); /* got new value */ |
736 |
– |
|
737 |
– |
/* PMAP: add in caustic */ |
738 |
– |
addcolor(aval, caustic); |
739 |
– |
return; |
740 |
– |
} |
741 |
– |
|
742 |
– |
dumbamb: /* return global value */ |
743 |
– |
if ((ambvwt <= 0) | (navsum == 0)) { |
744 |
– |
multcolor(aval, ambval); |
745 |
– |
|
746 |
– |
/* PMAP: add in caustic */ |
747 |
– |
addcolor(aval, caustic); |
748 |
– |
return; |
749 |
– |
} |
750 |
– |
|
751 |
– |
l = bright(ambval); /* average in computations */ |
752 |
– |
if (l > FTINY) { |
753 |
– |
d = (log(l)*(double)ambvwt + avsum) / |
754 |
– |
(double)(ambvwt + navsum); |
755 |
– |
d = exp(d) / l; |
756 |
– |
scalecolor(aval, d); |
757 |
– |
multcolor(aval, ambval); /* apply color of ambval */ |
758 |
– |
} else { |
759 |
– |
d = exp( avsum / (double)navsum ); |
760 |
– |
scalecolor(aval, d); /* neutral color */ |
761 |
– |
} |
762 |
– |
} |
763 |
– |
|
764 |
– |
|
765 |
– |
static double |
766 |
– |
sumambient( /* get interpolated ambient value */ |
767 |
– |
COLOR acol, |
768 |
– |
RAY *r, |
769 |
– |
FVECT rn, |
770 |
– |
int al, |
771 |
– |
AMBTREE *at, |
772 |
– |
FVECT c0, |
773 |
– |
double s |
774 |
– |
) |
775 |
– |
{ |
776 |
– |
double d, e1, e2, wt, wsum; |
777 |
– |
COLOR ct; |
778 |
– |
FVECT ck0; |
779 |
– |
int i; |
780 |
– |
int j; |
781 |
– |
AMBVAL *av; |
782 |
– |
|
783 |
– |
wsum = 0.0; |
784 |
– |
/* do this node */ |
785 |
– |
for (av = at->alist; av != NULL; av = av->next) { |
786 |
– |
double rn_dot = -2.0; |
787 |
– |
if (tracktime) |
788 |
– |
av->latick = ambclock; |
789 |
– |
/* |
790 |
– |
* Ambient level test. |
791 |
– |
*/ |
792 |
– |
if (av->lvl > al || /* list sorted, so this works */ |
793 |
– |
(av->lvl == al) & (av->weight < 0.9*r->rweight)) |
794 |
– |
break; |
795 |
– |
/* |
796 |
– |
* Ambient radius test. |
797 |
– |
*/ |
798 |
– |
VSUB(ck0, av->pos, r->rop); |
799 |
– |
e1 = DOT(ck0, ck0) / (av->rad * av->rad); |
800 |
– |
if (e1 > ambacc*ambacc*1.21) |
801 |
– |
continue; |
802 |
– |
/* |
803 |
– |
* Direction test using closest normal. |
804 |
– |
*/ |
805 |
– |
d = DOT(av->dir, r->ron); |
806 |
– |
if (rn != r->ron) { |
807 |
– |
rn_dot = DOT(av->dir, rn); |
808 |
– |
if (rn_dot > 1.0-FTINY) |
809 |
– |
rn_dot = 1.0-FTINY; |
810 |
– |
if (rn_dot >= d-FTINY) { |
811 |
– |
d = rn_dot; |
812 |
– |
rn_dot = -2.0; |
813 |
– |
} |
814 |
– |
} |
815 |
– |
e2 = (1.0 - d) * r->rweight; |
816 |
– |
if (e2 < 0.0) |
817 |
– |
e2 = 0.0; |
818 |
– |
else if (e1 + e2 > ambacc*ambacc*1.21) |
819 |
– |
continue; |
820 |
– |
/* |
821 |
– |
* Ray behind test. |
822 |
– |
*/ |
823 |
– |
d = 0.0; |
824 |
– |
for (j = 0; j < 3; j++) |
825 |
– |
d += (r->rop[j] - av->pos[j]) * |
826 |
– |
(av->dir[j] + r->ron[j]); |
827 |
– |
if (d*0.5 < -minarad*ambacc-.001) |
828 |
– |
continue; |
829 |
– |
/* |
830 |
– |
* Jittering final test reduces image artifacts. |
831 |
– |
*/ |
832 |
– |
e1 = sqrt(e1); |
833 |
– |
e2 = sqrt(e2); |
834 |
– |
wt = e1 + e2; |
835 |
– |
if (wt > ambacc*(.9+.2*urand(9015+samplendx))) |
836 |
– |
continue; |
837 |
– |
/* |
838 |
– |
* Recompute directional error using perturbed normal |
839 |
– |
*/ |
840 |
– |
if (rn_dot > 0.0) { |
841 |
– |
e2 = sqrt((1.0 - rn_dot)*r->rweight); |
842 |
– |
wt = e1 + e2; |
843 |
– |
} |
844 |
– |
if (wt <= 1e-3) |
845 |
– |
wt = 1e3; |
846 |
– |
else |
847 |
– |
wt = 1.0 / wt; |
848 |
– |
wsum += wt; |
849 |
– |
extambient(ct, av, r->rop, rn); |
850 |
– |
scalecolor(ct, wt); |
851 |
– |
addcolor(acol, ct); |
852 |
– |
} |
853 |
– |
if (at->kid == NULL) |
854 |
– |
return(wsum); |
855 |
– |
/* do children */ |
856 |
– |
s *= 0.5; |
857 |
– |
for (i = 0; i < 8; i++) { |
858 |
– |
for (j = 0; j < 3; j++) { |
859 |
– |
ck0[j] = c0[j]; |
860 |
– |
if (1<<j & i) |
861 |
– |
ck0[j] += s; |
862 |
– |
if (r->rop[j] < ck0[j] - OCTSCALE*s) |
863 |
– |
break; |
864 |
– |
if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s) |
865 |
– |
break; |
866 |
– |
} |
867 |
– |
if (j == 3) |
868 |
– |
wsum += sumambient(acol, r, rn, al, |
869 |
– |
at->kid+i, ck0, s); |
870 |
– |
} |
871 |
– |
return(wsum); |
872 |
– |
} |
873 |
– |
|
874 |
– |
|
875 |
– |
static double |
876 |
– |
makeambient( /* make a new ambient value for storage */ |
877 |
– |
COLOR acol, |
878 |
– |
RAY *r, |
879 |
– |
FVECT rn, |
880 |
– |
int al |
881 |
– |
) |
882 |
– |
{ |
883 |
– |
AMBVAL amb; |
884 |
– |
FVECT gp, gd; |
885 |
– |
int i; |
886 |
– |
|
887 |
– |
amb.weight = 1.0; /* compute weight */ |
888 |
– |
for (i = al; i-- > 0; ) |
889 |
– |
amb.weight *= AVGREFL; |
890 |
– |
if (r->rweight < 0.1*amb.weight) /* heuristic override */ |
891 |
– |
amb.weight = 1.25*r->rweight; |
892 |
– |
setcolor(acol, AVGREFL, AVGREFL, AVGREFL); |
893 |
– |
/* compute ambient */ |
894 |
– |
amb.rad = doambient(acol, r, amb.weight, gp, gd); |
895 |
– |
if (amb.rad <= FTINY) { |
896 |
– |
setcolor(acol, 0.0, 0.0, 0.0); |
897 |
– |
return(0.0); |
898 |
– |
} |
899 |
– |
scalecolor(acol, 1./AVGREFL); /* undo assumed reflectance */ |
900 |
– |
/* store value */ |
901 |
– |
VCOPY(amb.pos, r->rop); |
902 |
– |
VCOPY(amb.dir, r->ron); |
903 |
– |
amb.lvl = al; |
904 |
– |
copycolor(amb.val, acol); |
905 |
– |
VCOPY(amb.gpos, gp); |
906 |
– |
VCOPY(amb.gdir, gd); |
907 |
– |
/* insert into tree */ |
908 |
– |
avsave(&amb); /* and save to file */ |
909 |
– |
if (rn != r->ron) |
910 |
– |
extambient(acol, &amb, r->rop, rn); /* texture */ |
911 |
– |
return(amb.rad); |
912 |
– |
} |
913 |
– |
|
914 |
– |
|
619 |
|
static void |
916 |
– |
extambient( /* extrapolate value at pv, nv */ |
917 |
– |
COLOR cr, |
918 |
– |
AMBVAL *ap, |
919 |
– |
FVECT pv, |
920 |
– |
FVECT nv |
921 |
– |
) |
922 |
– |
{ |
923 |
– |
FVECT v1; |
924 |
– |
int i; |
925 |
– |
double d; |
926 |
– |
|
927 |
– |
d = 1.0; /* zeroeth order */ |
928 |
– |
/* gradient due to translation */ |
929 |
– |
for (i = 0; i < 3; i++) |
930 |
– |
d += ap->gpos[i]*(pv[i]-ap->pos[i]); |
931 |
– |
/* gradient due to rotation */ |
932 |
– |
VCROSS(v1, ap->dir, nv); |
933 |
– |
d += DOT(ap->gdir, v1); |
934 |
– |
if (d <= 0.0) { |
935 |
– |
setcolor(cr, 0.0, 0.0, 0.0); |
936 |
– |
return; |
937 |
– |
} |
938 |
– |
copycolor(cr, ap->val); |
939 |
– |
scalecolor(cr, d); |
940 |
– |
} |
941 |
– |
|
942 |
– |
|
943 |
– |
static void |
944 |
– |
avinsert( /* insert ambient value in our tree */ |
945 |
– |
AMBVAL *av |
946 |
– |
) |
947 |
– |
{ |
948 |
– |
AMBTREE *at; |
949 |
– |
AMBVAL *ap; |
950 |
– |
AMBVAL avh; |
951 |
– |
FVECT ck0; |
952 |
– |
double s; |
953 |
– |
int branch; |
954 |
– |
int i; |
955 |
– |
|
956 |
– |
if (av->rad <= FTINY) |
957 |
– |
error(CONSISTENCY, "zero ambient radius in avinsert"); |
958 |
– |
at = &atrunk; |
959 |
– |
VCOPY(ck0, thescene.cuorg); |
960 |
– |
s = thescene.cusize; |
961 |
– |
while (s*(OCTSCALE/2) > av->rad*ambacc) { |
962 |
– |
if (at->kid == NULL) |
963 |
– |
if ((at->kid = newambtree()) == NULL) |
964 |
– |
error(SYSTEM, "out of memory in avinsert"); |
965 |
– |
s *= 0.5; |
966 |
– |
branch = 0; |
967 |
– |
for (i = 0; i < 3; i++) |
968 |
– |
if (av->pos[i] > ck0[i] + s) { |
969 |
– |
ck0[i] += s; |
970 |
– |
branch |= 1 << i; |
971 |
– |
} |
972 |
– |
at = at->kid + branch; |
973 |
– |
} |
974 |
– |
avh.next = at->alist; /* order by increasing level */ |
975 |
– |
for (ap = &avh; ap->next != NULL; ap = ap->next) |
976 |
– |
if ( ap->next->lvl > av->lvl || |
977 |
– |
(ap->next->lvl == av->lvl) & |
978 |
– |
(ap->next->weight <= av->weight) ) |
979 |
– |
break; |
980 |
– |
av->next = ap->next; |
981 |
– |
ap->next = (AMBVAL*)av; |
982 |
– |
at->alist = avh.next; |
983 |
– |
} |
984 |
– |
|
985 |
– |
#endif /* ! NEWAMB */ |
986 |
– |
|
987 |
– |
/************* FOLLOWING ROUTINES SAME FOR NEW & OLD METHODS ***************/ |
988 |
– |
|
989 |
– |
static void |
620 |
|
initambfile( /* initialize ambient file */ |
621 |
|
int cre8 |
622 |
|
) |
639 |
|
ambvwt, ambounce, ambacc); |
640 |
|
fprintf(ambfp, "-ad %d -as %d -ar %d ", |
641 |
|
ambdiv, ambssamp, ambres); |
642 |
+ |
fprintf(ambfp, "-dt %g -dc %g -ss %g -lr %d -lw %g ", |
643 |
+ |
shadthresh, shadcert, specjitter, |
644 |
+ |
maxdepth, minweight); |
645 |
|
if (octname != NULL) |
646 |
|
fputs(octname, ambfp); |
647 |
|
fputc('\n', ambfp); |
685 |
|
if ((av = newambval()) == NULL) |
686 |
|
error(SYSTEM, "out of memory in avstore"); |
687 |
|
*av = *aval; |
1055 |
– |
av->latick = ambclock; |
688 |
|
av->next = NULL; |
689 |
|
nambvals++; |
690 |
|
d = bright(av->val); |
757 |
|
} |
758 |
|
|
759 |
|
|
1128 |
– |
static struct avl { |
1129 |
– |
AMBVAL *p; |
1130 |
– |
unsigned long t; |
1131 |
– |
} *avlist1; /* ambient value list with ticks */ |
1132 |
– |
static AMBVAL **avlist2; /* memory positions for sorting */ |
1133 |
– |
static int i_avlist; /* index for lists */ |
1134 |
– |
|
1135 |
– |
static int alatcmp(const void *av1, const void *av2); |
1136 |
– |
|
760 |
|
static void |
761 |
|
avfree(AMBVAL *av) |
762 |
|
{ |
763 |
|
free(av); |
764 |
|
} |
765 |
|
|
766 |
+ |
|
767 |
|
static void |
768 |
< |
av2list( |
1145 |
< |
AMBVAL *av |
1146 |
< |
) |
768 |
> |
sortambvals(void) /* resort ambient values */ |
769 |
|
{ |
770 |
< |
#ifdef DEBUG |
1149 |
< |
if (i_avlist >= nambvals) |
1150 |
< |
error(CONSISTENCY, "too many ambient values in av2list1"); |
1151 |
< |
#endif |
1152 |
< |
avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av; |
1153 |
< |
avlist1[i_avlist++].t = av->latick; |
1154 |
< |
} |
770 |
> |
AMBTREE oldatrunk = atrunk; |
771 |
|
|
772 |
< |
|
773 |
< |
static int |
774 |
< |
alatcmp( /* compare ambient values for MRA */ |
1159 |
< |
const void *av1, |
1160 |
< |
const void *av2 |
1161 |
< |
) |
1162 |
< |
{ |
1163 |
< |
long lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t; |
1164 |
< |
return(lc<0 ? -1 : lc>0 ? 1 : 0); |
772 |
> |
atrunk.alist = NULL; |
773 |
> |
atrunk.kid = NULL; |
774 |
> |
unloadatree(&oldatrunk, avinsert); |
775 |
|
} |
776 |
|
|
777 |
|
|
1168 |
– |
/* GW NOTE 2002/10/3: |
1169 |
– |
* I used to compare AMBVAL pointers, but found that this was the |
1170 |
– |
* cause of a serious consistency error with gcc, since the optimizer |
1171 |
– |
* uses some dangerous trick in pointer subtraction that |
1172 |
– |
* assumes pointers differ by exact struct size increments. |
1173 |
– |
*/ |
1174 |
– |
static int |
1175 |
– |
aposcmp( /* compare ambient value positions */ |
1176 |
– |
const void *avp1, |
1177 |
– |
const void *avp2 |
1178 |
– |
) |
1179 |
– |
{ |
1180 |
– |
long diff = *(char * const *)avp1 - *(char * const *)avp2; |
1181 |
– |
if (diff < 0) |
1182 |
– |
return(-1); |
1183 |
– |
return(diff > 0); |
1184 |
– |
} |
1185 |
– |
|
1186 |
– |
|
1187 |
– |
static int |
1188 |
– |
avlmemi( /* find list position from address */ |
1189 |
– |
AMBVAL *avaddr |
1190 |
– |
) |
1191 |
– |
{ |
1192 |
– |
AMBVAL **avlpp; |
1193 |
– |
|
1194 |
– |
avlpp = (AMBVAL **)bsearch(&avaddr, avlist2, |
1195 |
– |
nambvals, sizeof(AMBVAL *), aposcmp); |
1196 |
– |
if (avlpp == NULL) |
1197 |
– |
error(CONSISTENCY, "address not found in avlmemi"); |
1198 |
– |
return(avlpp - avlist2); |
1199 |
– |
} |
1200 |
– |
|
1201 |
– |
|
1202 |
– |
static void |
1203 |
– |
sortambvals( /* resort ambient values */ |
1204 |
– |
int always |
1205 |
– |
) |
1206 |
– |
{ |
1207 |
– |
AMBTREE oldatrunk; |
1208 |
– |
AMBVAL tav, *tap, *pnext; |
1209 |
– |
int i, j; |
1210 |
– |
/* see if it's time yet */ |
1211 |
– |
if (!always && (ambclock++ < lastsort+sortintvl || |
1212 |
– |
nambvals < SORT_THRESH)) |
1213 |
– |
return; |
1214 |
– |
/* |
1215 |
– |
* The idea here is to minimize memory thrashing |
1216 |
– |
* in VM systems by improving reference locality. |
1217 |
– |
* We do this by periodically sorting our stored ambient |
1218 |
– |
* values in memory in order of most recently to least |
1219 |
– |
* recently accessed. This ordering was chosen so that new |
1220 |
– |
* ambient values (which tend to be less important) go into |
1221 |
– |
* higher memory with the infrequently accessed values. |
1222 |
– |
* Since we expect our values to need sorting less |
1223 |
– |
* frequently as the process continues, we double our |
1224 |
– |
* waiting interval after each call. |
1225 |
– |
* This routine is also called by setambacc() with |
1226 |
– |
* the "always" parameter set to 1 so that the ambient |
1227 |
– |
* tree will be rebuilt with the new accuracy parameter. |
1228 |
– |
*/ |
1229 |
– |
if (tracktime) { /* allocate pointer arrays to sort */ |
1230 |
– |
avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); |
1231 |
– |
avlist1 = (struct avl *)malloc(nambvals*sizeof(struct avl)); |
1232 |
– |
} else { |
1233 |
– |
avlist2 = NULL; |
1234 |
– |
avlist1 = NULL; |
1235 |
– |
} |
1236 |
– |
if (avlist1 == NULL) { /* no time tracking -- rebuild tree? */ |
1237 |
– |
if (avlist2 != NULL) |
1238 |
– |
free(avlist2); |
1239 |
– |
if (always) { /* rebuild without sorting */ |
1240 |
– |
oldatrunk = atrunk; |
1241 |
– |
atrunk.alist = NULL; |
1242 |
– |
atrunk.kid = NULL; |
1243 |
– |
unloadatree(&oldatrunk, avinsert); |
1244 |
– |
} |
1245 |
– |
} else { /* sort memory by last access time */ |
1246 |
– |
/* |
1247 |
– |
* Sorting memory is tricky because it isn't contiguous. |
1248 |
– |
* We have to sort an array of pointers by MRA and also |
1249 |
– |
* by memory position. We then copy values in "loops" |
1250 |
– |
* to minimize memory hits. Nevertheless, we will visit |
1251 |
– |
* everyone at least twice, and this is an expensive process |
1252 |
– |
* when we're thrashing, which is when we need to do it. |
1253 |
– |
*/ |
1254 |
– |
#ifdef DEBUG |
1255 |
– |
sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...", |
1256 |
– |
nambvals, ambclock); |
1257 |
– |
eputs(errmsg); |
1258 |
– |
#endif |
1259 |
– |
i_avlist = 0; |
1260 |
– |
unloadatree(&atrunk, av2list); /* empty current tree */ |
1261 |
– |
#ifdef DEBUG |
1262 |
– |
if (i_avlist < nambvals) |
1263 |
– |
error(CONSISTENCY, "missing ambient values in sortambvals"); |
1264 |
– |
#endif |
1265 |
– |
qsort(avlist1, nambvals, sizeof(struct avl), alatcmp); |
1266 |
– |
qsort(avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
1267 |
– |
for (i = 0; i < nambvals; i++) { |
1268 |
– |
if (avlist1[i].p == NULL) |
1269 |
– |
continue; |
1270 |
– |
tap = avlist2[i]; |
1271 |
– |
tav = *tap; |
1272 |
– |
for (j = i; (pnext = avlist1[j].p) != tap; |
1273 |
– |
j = avlmemi(pnext)) { |
1274 |
– |
*(avlist2[j]) = *pnext; |
1275 |
– |
avinsert(avlist2[j]); |
1276 |
– |
avlist1[j].p = NULL; |
1277 |
– |
} |
1278 |
– |
*(avlist2[j]) = tav; |
1279 |
– |
avinsert(avlist2[j]); |
1280 |
– |
avlist1[j].p = NULL; |
1281 |
– |
} |
1282 |
– |
free(avlist1); |
1283 |
– |
free(avlist2); |
1284 |
– |
/* compute new sort interval */ |
1285 |
– |
sortintvl = ambclock - lastsort; |
1286 |
– |
if (sortintvl >= MAX_SORT_INTVL/2) |
1287 |
– |
sortintvl = MAX_SORT_INTVL; |
1288 |
– |
else |
1289 |
– |
sortintvl <<= 1; /* wait twice as long next */ |
1290 |
– |
#ifdef DEBUG |
1291 |
– |
eputs("done\n"); |
1292 |
– |
#endif |
1293 |
– |
} |
1294 |
– |
if (ambclock >= MAXACLOCK) |
1295 |
– |
ambclock = MAXACLOCK/2; |
1296 |
– |
lastsort = ambclock; |
1297 |
– |
} |
1298 |
– |
|
1299 |
– |
|
778 |
|
#ifdef F_SETLKW |
779 |
|
|
780 |
|
static void |
786 |
|
|
787 |
|
if (typ == fls.l_type) /* already called? */ |
788 |
|
return; |
789 |
+ |
|
790 |
|
fls.l_type = typ; |
791 |
< |
if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0) |
792 |
< |
error(SYSTEM, "cannot (un)lock ambient file"); |
791 |
> |
do |
792 |
> |
if (fcntl(fileno(ambfp), F_SETLKW, &fls) != -1) |
793 |
> |
return; |
794 |
> |
while (errno == EINTR); |
795 |
> |
|
796 |
> |
error(SYSTEM, "cannot (un)lock ambient file"); |
797 |
|
} |
798 |
|
|
799 |
|
|