~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to source/blender/render/intern/source/strand.c

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-05-12 20:02:22 UTC
  • mfrom: (14.2.16 sid)
  • Revision ID: package-import@ubuntu.com-20120512200222-lznjs2cxzaq96wua
Tags: 2.63a-1
* New upstream bugfix release
  + debian/patches/: re-worked since source code changed

Show diffs side-by-side

added added

removed removed

Lines of Context:
323
323
        MemArena *memarena;
324
324
};
325
325
 
 
326
typedef struct StrandCacheEntry {
 
327
        GHashPair pair;
 
328
        ShadeResult shr;
 
329
} StrandCacheEntry;
 
330
 
326
331
StrandShadeCache *strand_shade_cache_create(void)
327
332
{
328
333
        StrandShadeCache *cache;
329
334
 
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");
334
339
        
335
340
        return cache;
338
343
void strand_shade_cache_free(StrandShadeCache *cache)
339
344
{
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);
344
349
}
345
350
 
 
351
static GHashPair strand_shade_hash_pair(ObjectInstanceRen *obi, StrandVert *svert)
 
352
{
 
353
        GHashPair pair = {obi, svert};
 
354
        return pair;
 
355
}
 
356
 
346
357
static void strand_shade_get(Render *re, StrandShadeCache *cache, ShadeSample *ssamp, StrandSegment *sseg, StrandVert *svert)
347
358
{
348
 
        ShadeResult *hashshr;
 
359
        StrandCacheEntry *entry;
349
360
        StrandPoint p;
350
361
        int *refcount;
351
 
 
352
 
        hashshr= BLI_ghash_lookup(cache->resulthash, svert);
353
 
        refcount= BLI_ghash_lookup(cache->refcounthash, svert);
354
 
 
355
 
        if (!hashshr) {
 
362
        GHashPair pair = strand_shade_hash_pair(sseg->obi, svert);
 
363
 
 
364
        entry= BLI_ghash_lookup(cache->resulthash, &pair);
 
365
        refcount= BLI_ghash_lookup(cache->refcounthash, &pair);
 
366
 
 
367
        if (!entry) {
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);
360
372
 
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");
 
374
                entry->pair = pair;
 
375
                entry->shr = ssamp->shr[0];
 
376
                BLI_ghash_insert(cache->resulthash, entry, entry);
364
377
        }
365
378
        else
366
379
                /* already shaded, just copy previous result from hash */
367
 
                ssamp->shr[0]= *hashshr;
 
380
                ssamp->shr[0]= entry->shr;
368
381
        
369
382
        /* lower reference count and remove if not needed anymore by any samples */
370
383
        (*refcount)--;
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);
374
387
        }
375
388
}
376
389
 
388
401
 
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);
392
405
 
393
406
                strand_apply_shaderesult_alpha(ssamp->shr, s);
394
407
        }
395
408
}
396
409
 
397
 
void strand_shade_unref(StrandShadeCache *cache, StrandVert *svert)
 
410
void strand_shade_unref(StrandShadeCache *cache, ObjectInstanceRen *obi, StrandVert *svert)
398
411
{
 
412
        GHashPair pair = strand_shade_hash_pair(obi, svert);
399
413
        int *refcount;
400
414
 
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);
403
417
 
404
418
        (*refcount)--;
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);
408
422
        }
409
423
}
410
424
 
411
 
static void strand_shade_refcount(StrandShadeCache *cache, StrandVert *svert)
 
425
static void strand_shade_refcount(StrandShadeCache *cache, StrandSegment *sseg, StrandVert *svert)
412
426
{
413
 
        int *refcount= BLI_ghash_lookup(cache->refcounthash, svert);
 
427
        GHashPair pair = strand_shade_hash_pair(sseg->obi, svert);
 
428
        GHashPair *key;
 
429
        int *refcount= BLI_ghash_lookup(cache->refcounthash, &pair);
414
430
 
415
431
        if (!refcount) {
 
432
                key= BLI_memarena_alloc(cache->memarena, sizeof(GHashPair));
 
433
                *key = pair;
416
434
                refcount= BLI_memarena_alloc(cache->memarena, sizeof(int));
417
435
                *refcount= 1;
418
 
                BLI_ghash_insert(cache->refcounthash, svert, refcount);
 
436
                BLI_ghash_insert(cache->refcounthash, key, refcount);
419
437
        }
420
438
        else
421
439
                (*refcount)++;
580
598
                        }
581
599
 
582
600
                        if (cache) {
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]);
585
603
                        }
586
604
                        spart->totapixbuf[offset]++;
587
605
                }