--- ray/src/rt/ambcomp.c 2014/05/01 16:06:11 2.44 +++ ray/src/rt/ambcomp.c 2014/05/09 22:53:11 2.57 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: ambcomp.c,v 2.44 2014/05/01 16:06:11 greg Exp $"; +static const char RCSid[] = "$Id: ambcomp.c,v 2.57 2014/05/09 22:53:11 greg Exp $"; #endif /* * Routines to compute "ambient" values using Monte Carlo @@ -8,6 +8,10 @@ static const char RCSid[] = "$Id: ambcomp.c,v 2.44 201 * for Irradiance Caching" by Schwarzhaupt, Wann Jensen, & Jarosz * from ACM SIGGRAPH Asia 2012 conference proceedings. * + * Added book-keeping optimization to avoid calculations that would + * cancel due to traversal both directions on edges that are adjacent + * to same-valued triangles. This cuts about half of Hessian math. + * * Declarations of external symbols in ambient.h */ @@ -23,6 +27,7 @@ extern void SDsquare2disk(double ds[2], double seedx, typedef struct { COLOR v; /* hemisphere sample value */ + float d; /* reciprocal distance (1/rt) */ FVECT p; /* intersection point */ } AMBSAMP; /* sample value */ @@ -34,7 +39,8 @@ typedef struct { AMBSAMP sa[1]; /* sample array (extends struct) */ } AMBHEMI; /* ambient sample hemisphere */ -#define ambsam(h,i,j) (h)->sa[(i)*(h)->ns + (j)] +#define AI(h,i,j) ((i)*(h)->ns + (j)) +#define ambsam(h,i,j) (h)->sa[AI(h,i,j)] typedef struct { FVECT r_i, r_i1, e_i, rcp, rI2_eJ2; @@ -106,23 +112,23 @@ getambsamp(RAY *arp, AMBHEMI *hp, int i, int j, int n) scalecolor(arp->rcoef, 1./AVGREFL); } hlist[0] = hp->rp->rno; - hlist[1] = i; - hlist[2] = j; + hlist[1] = j; + hlist[2] = i; multisamp(spt, 2, urand(ilhash(hlist,3)+n)); if (!n) { /* avoid border samples for n==0 */ - if ((spt[0] < 0.1) | (spt[0] > 0.9)) + if ((spt[0] < 0.1) | (spt[0] >= 0.9)) spt[0] = 0.1 + 0.8*frandom(); - if ((spt[1] < 0.1) | (spt[1] > 0.9)) + if ((spt[1] < 0.1) | (spt[1] >= 0.9)) spt[1] = 0.1 + 0.8*frandom(); } - SDsquare2disk(spt, (i+spt[0])/hp->ns, (j+spt[1])/hp->ns); + SDsquare2disk(spt, (j+spt[1])/hp->ns, (i+spt[0])/hp->ns); zd = sqrt(1. - spt[0]*spt[0] - spt[1]*spt[1]); for (ii = 3; ii--; ) arp->rdir[ii] = spt[0]*hp->ux[ii] + spt[1]*hp->uy[ii] + zd*hp->rp->ron[ii]; checknorm(arp->rdir); - dimlist[ndims++] = i*hp->ns + j + 90171; + dimlist[ndims++] = AI(hp,i,j) + 90171; rayvalue(arp); /* evaluate ray */ ndims--; /* apply coefficient */ multcolor(arp->rcol, arp->rcoef); @@ -140,20 +146,16 @@ ambsample( /* initial ambient division sample */ AMBSAMP *ap = &ambsam(hp,i,j); RAY ar; /* generate hemispherical sample */ - if (!getambsamp(&ar, hp, i, j, 0)) - goto badsample; - /* limit vertex distance */ + if (!getambsamp(&ar, hp, i, j, 0) || ar.rt <= FTINY) { + memset(ap, 0, sizeof(AMBSAMP)); + return(NULL); + } + ap->d = 1.0/ar.rt; /* limit vertex distance */ if (ar.rt > 10.0*thescene.cusize) ar.rt = 10.0*thescene.cusize; - else if (ar.rt <= FTINY) /* should never happen! */ - goto badsample; VSUM(ap->p, ar.rorg, ar.rdir, ar.rt); copycolor(ap->v, ar.rcol); return(ap); -badsample: - setcolor(ap->v, 0., 0., 0.); - VCOPY(ap->p, hp->rp->rop); - return(NULL); } @@ -161,7 +163,7 @@ badsample: static float * getambdiffs(AMBHEMI *hp) { - float *earr = calloc(hp->ns*hp->ns, sizeof(float)); + float *earr = (float *)calloc(hp->ns*hp->ns, sizeof(float)); float *ep; AMBSAMP *ap; double b, d2; @@ -179,25 +181,31 @@ getambdiffs(AMBHEMI *hp) ep[0] += d2; ep[-hp->ns] += d2; } - if (j) { /* from behind */ - d2 = b - bright(ap[-1].v); - d2 *= d2; - ep[0] += d2; - ep[-1] += d2; - } + if (!j) continue; + /* from behind */ + d2 = b - bright(ap[-1].v); + d2 *= d2; + ep[0] += d2; + ep[-1] += d2; + if (!i) continue; + /* diagonal */ + d2 = b - bright(ap[-hp->ns-1].v); + d2 *= d2; + ep[0] += d2; + ep[-hp->ns-1] += d2; } /* correct for number of neighbors */ - earr[0] *= 2.f; - earr[hp->ns-1] *= 2.f; - earr[(hp->ns-1)*hp->ns] *= 2.f; - earr[(hp->ns-1)*hp->ns + hp->ns-1] *= 2.f; + earr[0] *= 8./3.; + earr[hp->ns-1] *= 8./3.; + earr[(hp->ns-1)*hp->ns] *= 8./3.; + earr[(hp->ns-1)*hp->ns + hp->ns-1] *= 8./3.; for (i = 1; i < hp->ns-1; i++) { - earr[i*hp->ns] *= 4./3.; - earr[i*hp->ns + hp->ns-1] *= 4./3.; + earr[i*hp->ns] *= 8./5.; + earr[i*hp->ns + hp->ns-1] *= 8./5.; } for (j = 1; j < hp->ns-1; j++) { - earr[j] *= 4./3.; - earr[(hp->ns-1)*hp->ns + j] *= 4./3.; + earr[j] *= 8./5.; + earr[(hp->ns-1)*hp->ns + j] *= 8./5.; } return(earr); } @@ -208,23 +216,25 @@ static void ambsupersamp(double acol[3], AMBHEMI *hp, int cnt) { float *earr = getambdiffs(hp); - double e2sum = 0; + double e2rem = 0; AMBSAMP *ap; RAY ar; - COLOR asum; + double asum[3]; float *ep; - int i, j, n; + int i, j, n, nss; if (earr == NULL) /* just skip calc. if no memory */ return; - /* add up estimated variances */ - for (ep = earr + hp->ns*hp->ns; ep-- > earr; ) - e2sum += *ep; + /* accumulate estimated variances */ + for (ep = earr + hp->ns*hp->ns; ep > earr; ) + e2rem += *--ep; ep = earr; /* perform super-sampling */ for (ap = hp->sa, i = 0; i < hp->ns; i++) for (j = 0; j < hp->ns; j++, ap++) { - int nss = *ep/e2sum*cnt + frandom(); - setcolor(asum, 0., 0., 0.); + if (e2rem <= FTINY) + goto done; /* nothing left to do */ + nss = *ep/e2rem*cnt + frandom(); + asum[0] = asum[1] = asum[2] = 0.0; for (n = 1; n <= nss; n++) { if (!getambsamp(&ar, hp, i, j, n)) { nss = n-1; @@ -233,28 +243,44 @@ ambsupersamp(double acol[3], AMBHEMI *hp, int cnt) addcolor(asum, ar.rcol); } if (nss) { /* update returned ambient value */ - const double ssf = 1./(nss + 1); + const double ssf = 1./(nss + 1.); for (n = 3; n--; ) - acol[n] += ssf*colval(asum,n) + + acol[n] += ssf*asum[n] + (ssf - 1.)*colval(ap->v,n); } - e2sum -= *ep++; /* update remainders */ + e2rem -= *ep++; /* update remainders */ cnt -= nss; } +done: free(earr); } +/* Return brightness of farthest ambient sample */ +static double +back_ambval(AMBHEMI *hp, const int n1, const int n2, const int n3) +{ + if (hp->sa[n1].d <= hp->sa[n2].d) { + if (hp->sa[n1].d <= hp->sa[n3].d) + return(colval(hp->sa[n1].v,CIEY)); + return(colval(hp->sa[n3].v,CIEY)); + } + if (hp->sa[n2].d <= hp->sa[n3].d) + return(colval(hp->sa[n2].v,CIEY)); + return(colval(hp->sa[n3].v,CIEY)); +} + + /* Compute vectors and coefficients for Hessian/gradient calcs */ static void -comp_fftri(FFTRI *ftp, FVECT ap0, FVECT ap1, FVECT rop) +comp_fftri(FFTRI *ftp, AMBHEMI *hp, const int n0, const int n1) { double rdot_cp, dot_e, dot_er, rdot_r, rdot_r1, J2; - int i; + int ii; - VSUB(ftp->r_i, ap0, rop); - VSUB(ftp->r_i1, ap1, rop); - VSUB(ftp->e_i, ap1, ap0); + VSUB(ftp->r_i, hp->sa[n0].p, hp->rp->rop); + VSUB(ftp->r_i1, hp->sa[n1].p, hp->rp->rop); + VSUB(ftp->e_i, hp->sa[n1].p, hp->sa[n0].p); VCROSS(ftp->rcp, ftp->r_i, ftp->r_i1); rdot_cp = 1.0/DOT(ftp->rcp,ftp->rcp); dot_e = DOT(ftp->e_i,ftp->e_i); @@ -266,8 +292,8 @@ comp_fftri(FFTRI *ftp, FVECT ap0, FVECT ap1, FVECT rop ftp->I2 = ( DOT(ftp->e_i, ftp->r_i1)*rdot_r1 - dot_er*rdot_r + dot_e*ftp->I1 )*0.5*rdot_cp; J2 = ( 0.5*(rdot_r - rdot_r1) - dot_er*ftp->I2 ) / dot_e; - for (i = 3; i--; ) - ftp->rI2_eJ2[i] = ftp->I2*ftp->r_i[i] + J2*ftp->e_i[i]; + for (ii = 3; ii--; ) + ftp->rI2_eJ2[ii] = ftp->I2*ftp->r_i[ii] + J2*ftp->e_i[ii]; } @@ -316,7 +342,7 @@ comp_hessian(FVECT hess[3], FFTRI *ftp, FVECT nrm) hess[i][j] = m1[i][j] + d1*( I3*m2[i][j] + K3*m3[i][j] + 2.0*J3*m4[i][j] ); hess[i][j] += d2*(i==j); - hess[i][j] *= 1.0/PI; + hess[i][j] *= -1.0/PI; } } @@ -338,7 +364,7 @@ rev_hessian(FVECT hess[3]) /* Add to radiometric Hessian from the given triangle */ static void add2hessian(FVECT hess[3], FVECT ehess1[3], - FVECT ehess2[3], FVECT ehess3[3], COLORV v) + FVECT ehess2[3], FVECT ehess3[3], double v) { int i, j; @@ -359,7 +385,7 @@ comp_gradient(FVECT grad, FFTRI *ftp, FVECT nrm) f1 = 2.0*DOT(nrm, ftp->rcp); VCROSS(ncp, nrm, ftp->e_i); for (i = 3; i--; ) - grad[i] = (-0.5/PI)*( ftp->I1*ncp[i] + f1*ftp->rI2_eJ2[i] ); + grad[i] = (0.5/PI)*( ftp->I1*ncp[i] + f1*ftp->rI2_eJ2[i] ); } @@ -375,7 +401,7 @@ rev_gradient(FVECT grad) /* Add to displacement gradient from the given triangle */ static void -add2gradient(FVECT grad, FVECT egrad1, FVECT egrad2, FVECT egrad3, COLORV v) +add2gradient(FVECT grad, FVECT egrad1, FVECT egrad2, FVECT egrad3, double v) { int i; @@ -384,33 +410,8 @@ add2gradient(FVECT grad, FVECT egrad1, FVECT egrad2, F } -/* Return brightness of furthest ambient sample */ -static COLORV -back_ambval(AMBSAMP *ap1, AMBSAMP *ap2, AMBSAMP *ap3, FVECT orig) -{ - COLORV vback; - FVECT vec; - double d2, d2best; - - VSUB(vec, ap1->p, orig); - d2best = DOT(vec,vec); - vback = colval(ap1->v,CIEY); - VSUB(vec, ap2->p, orig); - d2 = DOT(vec,vec); - if (d2 > d2best) { - d2best = d2; - vback = colval(ap2->v,CIEY); - } - VSUB(vec, ap3->p, orig); - d2 = DOT(vec,vec); - if (d2 > d2best) - return(colval(ap3->v,CIEY)); - return(vback); -} - - /* Compute anisotropic radii and eigenvector directions */ -static int +static void eigenvectors(FVECT uv[2], float ra[2], FVECT hessian[3]) { double hess2[2][2]; @@ -432,9 +433,10 @@ eigenvectors(FVECT uv[2], float ra[2], FVECT hessian[3 if (i == 1) /* double-root (circle) */ evalue[1] = evalue[0]; if (!i || ((evalue[0] = fabs(evalue[0])) <= FTINY*FTINY) | - ((evalue[1] = fabs(evalue[1])) <= FTINY*FTINY) ) - error(INTERNAL, "bad eigenvalue calculation"); - + ((evalue[1] = fabs(evalue[1])) <= FTINY*FTINY) ) { + ra[0] = ra[1] = maxarad; + return; + } if (evalue[0] > evalue[1]) { ra[0] = sqrt(sqrt(4.0/evalue[0])); ra[1] = sqrt(sqrt(4.0/evalue[1])); @@ -492,8 +494,7 @@ ambHessian( /* anisotropic radii & pos. gradient */ } /* compute first row of edges */ for (j = 0; j < hp->ns-1; j++) { - comp_fftri(&fftr, ambsam(hp,0,j).p, - ambsam(hp,0,j+1).p, hp->rp->rop); + comp_fftri(&fftr, hp, AI(hp,0,j), AI(hp,0,j+1)); if (hessrow != NULL) comp_hessian(hessrow[j], &fftr, hp->rp->ron); if (gradrow != NULL) @@ -503,8 +504,7 @@ ambHessian( /* anisotropic radii & pos. gradient */ for (i = 0; i < hp->ns-1; i++) { FVECT hesscol[3]; /* compute first vertical edge */ FVECT gradcol; - comp_fftri(&fftr, ambsam(hp,i,0).p, - ambsam(hp,i+1,0).p, hp->rp->rop); + comp_fftri(&fftr, hp, AI(hp,i,0), AI(hp,i+1,0)); if (hessrow != NULL) comp_hessian(hesscol, &fftr, hp->rp->ron); if (gradrow != NULL) @@ -512,12 +512,11 @@ ambHessian( /* anisotropic radii & pos. gradient */ for (j = 0; j < hp->ns-1; j++) { FVECT hessdia[3]; /* compute triangle contributions */ FVECT graddia; - COLORV backg; - backg = back_ambval(&ambsam(hp,i,j), &ambsam(hp,i,j+1), - &ambsam(hp,i+1,j), hp->rp->rop); + double backg; + backg = back_ambval(hp, AI(hp,i,j), + AI(hp,i,j+1), AI(hp,i+1,j)); /* diagonal (inner) edge */ - comp_fftri(&fftr, ambsam(hp,i,j+1).p, - ambsam(hp,i+1,j).p, hp->rp->rop); + comp_fftri(&fftr, hp, AI(hp,i,j+1), AI(hp,i+1,j)); if (hessrow != NULL) { comp_hessian(hessdia, &fftr, hp->rp->ron); rev_hessian(hesscol); @@ -529,17 +528,15 @@ ambHessian( /* anisotropic radii & pos. gradient */ add2gradient(gradient, gradrow[j], graddia, gradcol, backg); } /* initialize edge in next row */ - comp_fftri(&fftr, ambsam(hp,i+1,j+1).p, - ambsam(hp,i+1,j).p, hp->rp->rop); + comp_fftri(&fftr, hp, AI(hp,i+1,j+1), AI(hp,i+1,j)); if (hessrow != NULL) comp_hessian(hessrow[j], &fftr, hp->rp->ron); if (gradrow != NULL) comp_gradient(gradrow[j], &fftr, hp->rp->ron); /* new column edge & paired triangle */ - backg = back_ambval(&ambsam(hp,i,j+1), &ambsam(hp,i+1,j+1), - &ambsam(hp,i+1,j), hp->rp->rop); - comp_fftri(&fftr, ambsam(hp,i,j+1).p, ambsam(hp,i+1,j+1).p, - hp->rp->rop); + backg = back_ambval(hp, AI(hp,i+1,j+1), + AI(hp,i+1,j), AI(hp,i,j+1)); + comp_fftri(&fftr, hp, AI(hp,i,j+1), AI(hp,i+1,j+1)); if (hessrow != NULL) { comp_hessian(hesscol, &fftr, hp->rp->ron); rev_hessian(hessdia); @@ -594,6 +591,51 @@ ambdirgrad(AMBHEMI *hp, FVECT uv[2], float dg[2]) } +/* Compute potential light leak direction flags for cache value */ +static uint32 +ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, const double r1) +{ + const double max_d = 1.0/(minarad*ambacc + 0.001); + const double ang_res = 0.5*PI/(hp->ns-1); + const double ang_step = ang_res/((int)(16/PI*ang_res) + (1+FTINY)); + double avg_d = 0; + uint32 flgs = 0; + int i, j; + /* don't bother for a few samples */ + if (hp->ns < 12) + return(0); + /* check distances overhead */ + for (i = hp->ns*3/4; i-- > hp->ns>>2; ) + for (j = hp->ns*3/4; j-- > hp->ns>>2; ) + avg_d += ambsam(hp,i,j).d; + avg_d *= 4.0/(hp->ns*hp->ns); + if (avg_d*r0 >= 1.0) /* ceiling too low for corral? */ + return(0); + if (avg_d >= max_d) /* insurance */ + return(0); + /* else circle around perimeter */ + for (i = 0; i < hp->ns; i++) + for (j = 0; j < hp->ns; j += !i|(i==hp->ns-1) ? 1 : hp->ns-1) { + AMBSAMP *ap = &ambsam(hp,i,j); + FVECT vec; + double u, v; + double ang, a1; + int abp; + if ((ap->d <= FTINY) | (ap->d >= max_d)) + continue; /* too far or too near */ + VSUB(vec, ap->p, hp->rp->rop); + u = DOT(vec, uv[0]) * ap->d; + v = DOT(vec, uv[1]) * ap->d; + if ((r0*r0*u*u + r1*r1*v*v) * ap->d*ap->d <= 1.0) + continue; /* occluder outside ellipse */ + ang = atan2a(v, u); /* else set direction flags */ + for (a1 = ang-.5*ang_res; a1 <= ang+.5*ang_res; a1 += ang_step) + flgs |= 1L<<(int)(16/PI*(a1 + 2.*PI*(a1 < 0))); + } + return(flgs); +} + + int doambient( /* compute ambient component */ COLOR rcol, /* input/output color */ @@ -602,11 +644,12 @@ doambient( /* compute ambient component */ FVECT uv[2], /* returned (optional) */ float ra[2], /* returned (optional) */ float pg[2], /* returned (optional) */ - float dg[2] /* returned (optional) */ + float dg[2], /* returned (optional) */ + uint32 *crlp /* returned (optional) */ ) { AMBHEMI *hp = inithemi(rcol, r, wt); - int cnt = 0; + int cnt; FVECT my_uv[2]; double d, K, acol[3]; AMBSAMP *ap; @@ -622,8 +665,11 @@ doambient( /* compute ambient component */ pg[0] = pg[1] = 0.0; if (dg != NULL) dg[0] = dg[1] = 0.0; + if (crlp != NULL) + *crlp = 0; /* sample the hemisphere */ acol[0] = acol[1] = acol[2] = 0.0; + cnt = 0; for (i = hp->ns; i--; ) for (j = hp->ns; j--; ) if ((ap = ambsample(hp, i, j)) != NULL) { @@ -641,21 +687,21 @@ doambient( /* compute ambient component */ return(-1); /* return value w/o Hessian */ } cnt = ambssamp*wt + 0.5; /* perform super-sampling? */ - if (cnt > 0) + if (cnt > 8) ambsupersamp(acol, hp, cnt); copycolor(rcol, acol); /* final indirect irradiance/PI */ if ((ra == NULL) & (pg == NULL) & (dg == NULL)) { free(hp); return(-1); /* no radius or gradient calc. */ } - if (bright(acol) > FTINY) { /* normalize Y values */ - d = 0.99*cnt/bright(acol); + if ((d = bright(acol)) > FTINY) { /* normalize Y values */ + d = 0.99*(hp->ns*hp->ns)/d; K = 0.01; - } else { /* geometric Hessian fall-back */ - d = 0.0; + } else { /* or fall back on geometric Hessian */ K = 1.0; pg = NULL; dg = NULL; + crlp = NULL; } ap = hp->sa; /* relative Y channel from here on... */ for (i = hp->ns*hp->ns; i--; ap++) @@ -691,6 +737,9 @@ doambient( /* compute ambient component */ if (ra[0] > maxarad) ra[0] = maxarad; } + /* flag encroached directions */ + if ((wt >= 0.89*AVGREFL) & (crlp != NULL)) + *crlp = ambcorral(hp, uv, ra[0]*ambacc, ra[1]*ambacc); if (pg != NULL) { /* cap gradient if necessary */ d = pg[0]*pg[0]*ra[0]*ra[0] + pg[1]*pg[1]*ra[1]*ra[1]; if (d > 1.0) {