--- ray/src/common/bsdf_t.c 2018/05/10 22:55:35 3.47 +++ ray/src/common/bsdf_t.c 2021/12/07 23:49:50 3.52 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: bsdf_t.c,v 3.47 2018/05/10 22:55:35 greg Exp $"; +static const char RCSid[] = "$Id: bsdf_t.c,v 3.52 2021/12/07 23:49:50 greg Exp $"; #endif /* * bsdf_t.c @@ -518,7 +518,7 @@ SDyuv2rgb(double yval, double uprime, double vprime, f /* Query BSDF value and sample hypercube for the given vectors */ static int SDqueryTre(const SDTre *sdt, float *coef, - const FVECT outVec, const FVECT inVec, double *hc) + const FVECT inVec, const FVECT outVec, double *hc) { const RREAL *vtmp; float yval; @@ -744,11 +744,12 @@ make_cdist(const SDTre *sdt, const double *invec, int const SDCDst * SDgetTreCDist(const FVECT inVec, SDComponent *sdc) { + unsigned long cacheLeft = SDmaxCache; const SDTre *sdt; double inCoord[2]; int i; int mode; - SDTreCDst *cd, *cdlast; + SDTreCDst *cd, *cdlast, *cdlimit; /* check arguments */ if ((inVec == NULL) | (sdc == NULL) || (sdt = (SDTre *)sdc->dist) == NULL) @@ -788,10 +789,20 @@ SDgetTreCDist(const FVECT inVec, SDComponent *sdc) /* quantize to avoid f.p. errors */ for (i = sdt->stc[tt_Y]->ndim - 2; i--; ) inCoord[i] = floor(inCoord[i]/quantum)*quantum + .5*quantum; - cdlast = NULL; /* check for direction in cache list */ + cdlast = cdlimit = NULL; /* check for direction in cache list */ /* PLACE MUTEX LOCK HERE FOR THREAD-SAFE */ for (cd = (SDTreCDst *)sdc->cdList; cd != NULL; cdlast = cd, cd = cd->next) { + if (cacheLeft) { /* check cache size limit */ + long csiz = sizeof(SDTreCDst) + + sizeof(cd->carr[0])*cd->calen; + if (cacheLeft > csiz) + cacheLeft -= csiz; + else { + cdlimit = cdlast; + cacheLeft = 0; + } + } if (cd->sidef != mode) continue; for (i = sdt->stc[tt_Y]->ndim - 2; i--; ) @@ -801,8 +812,14 @@ SDgetTreCDist(const FVECT inVec, SDComponent *sdc) if (i < 0) break; /* means we have a match */ } - if (cd == NULL) /* need to create new entry? */ + if (cd == NULL) { /* need to create new entry? */ + if (cdlimit != NULL) /* exceeded cache size limit? */ + while ((cd = cdlimit->next) != NULL) { + cdlimit->next = cd->next; + free(cd); + } cdlast = cd = make_cdist(sdt, inCoord, mode != sdt->sidef); + } if (cdlast != NULL) { /* move entry to head of cache list */ cdlast->next = cd->next; cd->next = (SDTreCDst *)sdc->cdList; @@ -1413,10 +1430,13 @@ SDloadTre(SDData *sd, ezxml_t wtl) /* separate diffuse components */ extract_diffuse(&sd->rLambFront, sd->rf); extract_diffuse(&sd->rLambBack, sd->rb); - if (sd->tf != NULL) - extract_diffuse(&sd->tLamb, sd->tf); - if (sd->tb != NULL) - extract_diffuse(&sd->tLamb, sd->tb); + extract_diffuse(&sd->tLambFront, sd->tf); + if (sd->tb != NULL) { + extract_diffuse(&sd->tLambBack, sd->tb); + if (sd->tf == NULL) + sd->tLambFront = sd->tLambBack; + } else if (sd->tf != NULL) + sd->tLambBack = sd->tLambFront; /* return success */ return SDEnone; }