323
323
MemArena *memarena;
326
typedef struct StrandCacheEntry {
326
331
StrandShadeCache *strand_shade_cache_create(void)
328
333
StrandShadeCache *cache;
330
335
cache= MEM_callocN(sizeof(StrandShadeCache), "StrandShadeCache");
331
cache->resulthash= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "strand_shade_cache_create1 gh");
332
cache->refcounthash= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "strand_shade_cache_create2 gh");
336
cache->resulthash= BLI_ghash_new(BLI_ghashutil_pairhash, BLI_ghashutil_paircmp, "strand_shade_cache_create1 gh");
337
cache->refcounthash= BLI_ghash_new(BLI_ghashutil_pairhash, BLI_ghashutil_paircmp, "strand_shade_cache_create2 gh");
333
338
cache->memarena= BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, "strand shade cache arena");
338
343
void strand_shade_cache_free(StrandShadeCache *cache)
340
345
BLI_ghash_free(cache->refcounthash, NULL, NULL);
341
BLI_ghash_free(cache->resulthash, NULL, (GHashValFreeFP)MEM_freeN);
346
BLI_ghash_free(cache->resulthash, (GHashKeyFreeFP)MEM_freeN, NULL);
342
347
BLI_memarena_free(cache->memarena);
343
348
MEM_freeN(cache);
351
static GHashPair strand_shade_hash_pair(ObjectInstanceRen *obi, StrandVert *svert)
353
GHashPair pair = {obi, svert};
346
357
static void strand_shade_get(Render *re, StrandShadeCache *cache, ShadeSample *ssamp, StrandSegment *sseg, StrandVert *svert)
348
ShadeResult *hashshr;
359
StrandCacheEntry *entry;
352
hashshr= BLI_ghash_lookup(cache->resulthash, svert);
353
refcount= BLI_ghash_lookup(cache->refcounthash, svert);
362
GHashPair pair = strand_shade_hash_pair(sseg->obi, svert);
364
entry= BLI_ghash_lookup(cache->resulthash, &pair);
365
refcount= BLI_ghash_lookup(cache->refcounthash, &pair);
356
368
/* not shaded yet, shade and insert into hash */
357
369
p.t= (sseg->v[1] == svert)? 0.0f: 1.0f;
358
370
strand_eval_point(sseg, &p);
359
371
strand_shade_point(re, ssamp, sseg, svert, &p);
361
hashshr= MEM_callocN(sizeof(ShadeResult), "HashShadeResult");
362
*hashshr= ssamp->shr[0];
363
BLI_ghash_insert(cache->resulthash, svert, hashshr);
373
entry= MEM_callocN(sizeof(StrandCacheEntry), "StrandCacheEntry");
375
entry->shr = ssamp->shr[0];
376
BLI_ghash_insert(cache->resulthash, entry, entry);
366
379
/* already shaded, just copy previous result from hash */
367
ssamp->shr[0]= *hashshr;
380
ssamp->shr[0]= entry->shr;
369
382
/* lower reference count and remove if not needed anymore by any samples */
371
384
if (*refcount == 0) {
372
BLI_ghash_remove(cache->resulthash, svert, NULL, (GHashValFreeFP)MEM_freeN);
373
BLI_ghash_remove(cache->refcounthash, svert, NULL, NULL);
385
BLI_ghash_remove(cache->resulthash, &pair, (GHashKeyFreeFP)MEM_freeN, NULL);
386
BLI_ghash_remove(cache->refcounthash, &pair, NULL, NULL);
389
402
/* apply alpha along width */
390
403
if (sseg->buffer->widthfade != 0.0f) {
391
s = 1.0f - pow(fabs(s), sseg->buffer->widthfade);
404
s = 1.0f - powf(fabsf(s), sseg->buffer->widthfade);
393
406
strand_apply_shaderesult_alpha(ssamp->shr, s);
397
void strand_shade_unref(StrandShadeCache *cache, StrandVert *svert)
410
void strand_shade_unref(StrandShadeCache *cache, ObjectInstanceRen *obi, StrandVert *svert)
412
GHashPair pair = strand_shade_hash_pair(obi, svert);
401
415
/* lower reference count and remove if not needed anymore by any samples */
402
refcount= BLI_ghash_lookup(cache->refcounthash, svert);
416
refcount= BLI_ghash_lookup(cache->refcounthash, &pair);
405
419
if (*refcount == 0) {
406
BLI_ghash_remove(cache->resulthash, svert, NULL, (GHashValFreeFP)MEM_freeN);
407
BLI_ghash_remove(cache->refcounthash, svert, NULL, NULL);
420
BLI_ghash_remove(cache->resulthash, &pair, (GHashKeyFreeFP)MEM_freeN, NULL);
421
BLI_ghash_remove(cache->refcounthash, &pair, NULL, NULL);
411
static void strand_shade_refcount(StrandShadeCache *cache, StrandVert *svert)
425
static void strand_shade_refcount(StrandShadeCache *cache, StrandSegment *sseg, StrandVert *svert)
413
int *refcount= BLI_ghash_lookup(cache->refcounthash, svert);
427
GHashPair pair = strand_shade_hash_pair(sseg->obi, svert);
429
int *refcount= BLI_ghash_lookup(cache->refcounthash, &pair);
432
key= BLI_memarena_alloc(cache->memarena, sizeof(GHashPair));
416
434
refcount= BLI_memarena_alloc(cache->memarena, sizeof(int));
418
BLI_ghash_insert(cache->refcounthash, svert, refcount);
436
BLI_ghash_insert(cache->refcounthash, key, refcount);
583
strand_shade_refcount(cache, sseg->v[1]);
584
strand_shade_refcount(cache, sseg->v[2]);
601
strand_shade_refcount(cache, sseg, sseg->v[1]);
602
strand_shade_refcount(cache, sseg, sseg->v[2]);
586
604
spart->totapixbuf[offset]++;