2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * Contributor(s): 2004-2006, Blender Foundation
23 * ***** END GPL LICENSE BLOCK *****
30 #include "MEM_guardedalloc.h"
32 #include "DNA_group_types.h"
33 #include "DNA_lamp_types.h"
34 #include "DNA_material_types.h"
36 #include "BKE_global.h"
37 #include "BKE_scene.h"
38 #include "BKE_utildefines.h"
41 #include "BLI_blenlib.h"
42 #include "BLI_jitter.h"
43 #include "BLI_memarena.h"
48 #include "renderpipeline.h"
49 #include "render_types.h"
50 #include "renderdatabase.h"
51 #include "rendercore.h"
56 /* XXX, could be better implemented... this is for endian issues
58 #if defined(__sgi) || defined(__sparc) || defined(__sparc__) || defined (__PPC__) || defined (__ppc__) || defined (__hppa__) || defined (__BIG_ENDIAN__)
70 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
71 /* defined in pipeline.c, is hardcopy of active dynamic allocated Render */
72 /* only to be used here in this file, it's for speed */
73 extern struct Render R;
74 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
76 /* ------------------------------------------------------------------------- */
78 /* initshadowbuf() in convertBlenderScene.c */
80 /* ------------------------------------------------------------------------- */
82 static void copy_to_ztile(int *rectz, int size, int x1, int y1, int tile, char *r1)
89 if(x2>=size) x2= size-1;
90 if(y2>=size) y2= size-1;
92 if(x1>=x2 || y1>=y2) return;
95 rz= rectz + size*y1 + x1;
104 static int sizeoflampbuf(ShadBuf *shb)
110 num= (shb->size*shb->size)/256;
112 while(num--) count+= *(cp++);
118 /* not threadsafe... */
119 static float *give_jitter_tab(int samp)
121 /* these are all possible jitter tables, takes up some
122 * 12k, not really bad!
123 * For soft shadows, it saves memory and render time
125 static int tab[17]={1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256};
126 static float jit[1496][2];
127 static char ctab[17]= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
131 else if(samp>16) samp= 16;
133 for(a=0; a<samp-1; a++) offset+= tab[a];
137 BLI_initjit(jit[offset], samp*samp);
144 static void make_jitter_weight_tab(Render *re, ShadBuf *shb, short filtertype)
146 float *jit, totw= 0.0f;
147 int samp= get_render_shadow_samples(&re->r, shb->samp);
148 int a, tot=samp*samp;
150 shb->weight= MEM_mallocN(sizeof(float)*tot, "weight tab lamp");
152 for(jit= shb->jit, a=0; a<tot; a++, jit+=2) {
153 if(filtertype==LA_SHADBUF_TENT)
154 shb->weight[a]= 0.71f - sqrt(jit[0]*jit[0] + jit[1]*jit[1]);
155 else if(filtertype==LA_SHADBUF_GAUSS)
156 shb->weight[a]= RE_filter_value(R_FILTER_GAUSS, 1.8f*sqrt(jit[0]*jit[0] + jit[1]*jit[1]));
158 shb->weight[a]= 1.0f;
160 totw+= shb->weight[a];
164 for(a=0; a<tot; a++) {
165 shb->weight[a]*= totw;
169 static int verg_deepsample(const void *poin1, const void *poin2)
171 const DeepSample *ds1= (const DeepSample*)poin1;
172 const DeepSample *ds2= (const DeepSample*)poin2;
174 if(ds1->z < ds2->z) return -1;
175 else if(ds1->z == ds2->z) return 0;
179 static int compress_deepsamples(DeepSample *dsample, int tot, float epsilon)
181 /* uses doubles to avoid overflows and other numerical issues,
183 DeepSample *ds, *newds;
185 double slope, slopemin, slopemax, min, max, div, newmin, newmax;
186 int a, first, z, newtot= 0;
189 for(a=0, ds=dsample; a<tot; a++, ds++)
190 printf("%lf,%f ", ds->z/(double)0x7FFFFFFF, ds->v);
194 /* read from and write into same array */
199 /* as long as we are not at the end of the array */
200 for(a++, ds++; a<tot; a++, ds++) {
205 for(; a<tot; a++, ds++) {
206 //dz= ds->z - newds->z;
207 if(ds->z == newds->z) {
208 /* still in same z position, simply check
209 visibility difference against epsilon */
210 if(!(fabs(newds->v - ds->v) <= epsilon)) {
216 div= (double)0x7FFFFFFF/((double)ds->z - (double)newds->z);
217 min= ((ds->v - epsilon) - newds->v)*div;
218 max= ((ds->v + epsilon) - newds->v)*div;
220 /* adapt existing slopes */
227 newmin= MAX2(slopemin, min);
228 newmax= MIN2(slopemax, max);
230 /* verify if there is still space between the slopes */
231 if(newmin > newmax) {
248 /* always previous z */
251 if(first || a==tot-1) {
252 /* if slopes were not initialized, use last visibility */
256 /* compute visibility at center between slopes at z */
257 slope= (slopemin+slopemax)*0.5;
258 v= newds->v + slope*((z - newds->z)/(double)0x7FFFFFFF);
268 if(newtot == 0 || (newds->v != (newds-1)->v))
272 for(a=0, ds=dsample; a<newtot; a++, ds++)
273 printf("%lf,%f ", ds->z/(double)0x7FFFFFFF, ds->v);
280 static float deep_alpha(Render *re, int obinr, int facenr, int strand)
282 ObjectInstanceRen *obi= &re->objectinstance[obinr];
286 StrandRen *strand= RE_findOrAddStrand(obi->obr, facenr-1);
287 ma= strand->buffer->ma;
290 VlakRen *vlr= RE_findOrAddVlak(obi->obr, (facenr-1) & RE_QUAD_MASK);
294 return ma->shad_alpha;
297 static void compress_deepshadowbuf(Render *re, ShadBuf *shb, APixstr *apixbuf, APixstrand *apixbufstrand)
299 ShadSampleBuf *shsample;
300 DeepSample *ds[RE_MAX_OSA], *sampleds[RE_MAX_OSA], *dsb, *newbuf;
302 APixstrand *aps, *apns;
303 float visibility, totbuf= shb->totbuf;
304 int a, b, c, tot, minz, found, size= shb->size, prevtot, newtot;
305 int sampletot[RE_MAX_OSA], totsample = 0, totsamplec = 0;
307 shsample= MEM_callocN( sizeof(ShadSampleBuf), "shad sample buf");
308 BLI_addtail(&shb->buffers, shsample);
310 shsample->totbuf= MEM_callocN(sizeof(int)*size*size, "deeptotbuf");
311 shsample->deepbuf= MEM_callocN(sizeof(DeepSample*)*size*size, "deepbuf");
315 for(a=0; a<size*size; a++, ap++, aps++) {
316 /* count number of samples */
317 for(c=0; c<totbuf; c++)
321 for(apn=ap; apn; apn=apn->next)
324 for(c=0; c<totbuf; c++)
325 if(apn->mask[b] & (1<<c))
329 for(apns=aps; apns; apns=apns->next)
332 for(c=0; c<totbuf; c++)
333 if(apns->mask[b] & (1<<c))
337 for(c=0; c<totbuf; c++)
341 shsample->deepbuf[a]= NULL;
342 shsample->totbuf[a]= 0;
347 ds[0]= sampleds[0]= MEM_callocN(sizeof(DeepSample)*tot*2, "deepsample");
348 for(c=1; c<totbuf; c++)
349 ds[c]= sampleds[c]= sampleds[c-1] + sampletot[c-1]*2;
351 for(apn=ap; apn; apn=apn->next) {
354 for(c=0; c<totbuf; c++) {
355 if(apn->mask[b] & (1<<c)) {
356 /* two entries to create step profile */
358 ds[c]->v= 1.0f; /* not used */
361 ds[c]->v= deep_alpha(re, apn->obi[b], apn->p[b], 0);
370 for(apns=aps; apns; apns=apns->next) {
373 for(c=0; c<totbuf; c++) {
374 if(apns->mask[b] & (1<<c)) {
375 /* two entries to create step profile */
376 ds[c]->z= apns->z[b];
377 ds[c]->v= 1.0f; /* not used */
379 ds[c]->z= apns->z[b];
380 ds[c]->v= deep_alpha(re, apns->obi[b], apns->p[b], 1);
389 for(c=0; c<totbuf; c++) {
390 /* sort by increasing z */
391 qsort(sampleds[c], sampletot[c], sizeof(DeepSample)*2, verg_deepsample);
393 /* sum visibility, replacing alpha values */
397 for(b=0; b<sampletot[c]; b++) {
398 /* two entries creating step profile */
399 ds[c]->v= visibility;
402 visibility *= 1.0f-ds[c]->v;
403 ds[c]->v= visibility;
407 /* halfway trick, probably won't work well for volumes? */
409 for(b=0; b<sampletot[c]; b++) {
410 if(b+1 < sampletot[c]) {
411 ds[c]->z= (ds[c]->z>>1) + ((ds[c]+2)->z>>1);
413 ds[c]->z= (ds[c]->z>>1) + ((ds[c]+2)->z>>1);
417 ds[c]->z= (ds[c]->z>>1) + (0x7FFFFFFF>>1);
419 ds[c]->z= (ds[c]->z>>1) + (0x7FFFFFFF>>1);
424 /* init for merge loop */
429 shsample->deepbuf[a]= MEM_callocN(sizeof(DeepSample)*tot*2, "deepsample");
430 shsample->totbuf[a]= 0;
433 dsb= shsample->deepbuf[a];
438 for(c=0; c<totbuf; c++) {
439 if(sampletot[c] && (!found || ds[c]->z < minz)) {
452 for(c=0; c<totbuf; c++) {
453 if(sampletot[c] && ds[c]->z == minz) {
458 if(sampleds[c] == ds[c])
459 visibility += 1.0f/totbuf;
461 visibility += (ds[c]-1)->v/totbuf;
466 shsample->totbuf[a]++;
469 prevtot= shsample->totbuf[a];
470 totsample += prevtot;
472 newtot= compress_deepsamples(shsample->deepbuf[a], prevtot, shb->compressthresh);
473 shsample->totbuf[a]= newtot;
474 totsamplec += newtot;
476 if(newtot < prevtot) {
477 newbuf= MEM_mallocN(sizeof(DeepSample)*newtot, "cdeepsample");
478 memcpy(newbuf, shsample->deepbuf[a], sizeof(DeepSample)*newtot);
479 MEM_freeN(shsample->deepbuf[a]);
480 shsample->deepbuf[a]= newbuf;
483 MEM_freeN(sampleds[0]);
486 //printf("%d -> %d, ratio %f\n", totsample, totsamplec, (float)totsamplec/(float)totsample);
489 /* create Z tiles (for compression): this system is 24 bits!!! */
490 static void compress_shadowbuf(ShadBuf *shb, int *rectz, int square)
492 ShadSampleBuf *shsample;
495 int *rz, *rz1, verg, verg1, size= shb->size;
496 int a, x, y, minx, miny, byt1, byt2;
497 char *rc, *rcline, *ctile, *zt;
499 shsample= MEM_callocN( sizeof(ShadSampleBuf), "shad sample buf");
500 BLI_addtail(&shb->buffers, shsample);
502 shsample->zbuf= MEM_mallocN( sizeof(uintptr_t)*(size*size)/256, "initshadbuf2");
503 shsample->cbuf= MEM_callocN( (size*size)/256, "initshadbuf3");
505 ztile= (uintptr_t *)shsample->zbuf;
506 ctile= shsample->cbuf;
509 rcline= MEM_mallocN(256*4+sizeof(int), "makeshadbuf2");
511 for(y=0; y<size; y+=16) {
512 if(y< size/2) miny= y+15-size/2;
515 for(x=0; x<size; x+=16) {
517 /* is tile within spotbundle? */
519 if(x< a) minx= x+15-a;
522 dist= sqrt( (float)(minx*minx+miny*miny) );
524 if(square==0 && dist>(float)(a+12)) { /* 12, tested with a onlyshadow lamp */
525 a= 256; verg= 0; /* 0x80000000; */ /* 0x7FFFFFFF; */
529 copy_to_ztile(rectz, size, x, y, 16, rcline);
532 verg= (*rz1 & 0xFFFFFF00);
534 for(a=0;a<256;a++,rz1++) {
535 if( (*rz1 & 0xFFFFFF00) !=verg) break;
538 if(a==256) { /* complete empty tile */
544 /* ACOMP etc. are defined to work L/B endian */
552 for(a=1;a<256;a++,rc+=4) {
553 byt1 &= (verg==rc[ACOMP]);
554 byt2 &= (verg1==rc[BCOMP]);
558 if(byt1 && byt2) { /* only store byte */
560 *ztile= (uintptr_t)MEM_mallocN(256+4, "tile1");
566 for(a=0; a<256; a++, zt++, rc+=4) *zt= rc[GCOMP];
568 else if(byt1) { /* only store short */
570 *ztile= (uintptr_t)MEM_mallocN(2*256+4,"Tile2");
576 for(a=0; a<256; a++, zt+=2, rc+=4) {
581 else { /* store triple */
583 *ztile= (uintptr_t)MEM_mallocN(3*256,"Tile3");
587 for(a=0; a<256; a++, zt+=3, rc+=4) {
602 /* sets start/end clipping. lar->shb should be initialized */
603 static void shadowbuf_autoclip(Render *re, LampRen *lar)
605 ObjectInstanceRen *obi;
610 float minz, maxz, vec[3], viewmat[4][4], obviewmat[4][4];
611 unsigned int lay = -1;
612 int i, a, maxtotvert, ok= 1;
615 minz= 1.0e30f; maxz= -1.0e30f;
616 copy_m4_m4(viewmat, lar->shb->viewmat);
618 if(lar->mode & (LA_LAYER|LA_LAYER_SHADOW)) lay= lar->lay;
621 for(obr=re->objecttable.first; obr; obr=obr->next)
622 maxtotvert= MAX2(obr->totvert, maxtotvert);
624 clipflag= MEM_callocN(sizeof(char)*maxtotvert, "autoclipflag");
626 /* set clip in vertices when face visible */
627 for(i=0, obi=re->instancetable.first; obi; i++, obi=obi->next) {
630 if(obi->flag & R_TRANSFORMED)
631 mul_m4_m4m4(obviewmat, obi->mat, viewmat);
633 copy_m4_m4(obviewmat, viewmat);
635 memset(clipflag, 0, sizeof(char)*obr->totvert);
637 /* clear clip, is being set if face is visible (clip is calculated for real later) */
638 for(a=0; a<obr->totvlak; a++) {
639 if((a & 255)==0) vlr= obr->vlaknodes[a>>8].vlak;
642 /* note; these conditions are copied from zbuffer_shadow() */
646 if((ma->mode & MA_SHADBUF)==0) ok= 0;
649 if(ok && (obi->lay & lay)) {
650 clipflag[vlr->v1->index]= 1;
651 clipflag[vlr->v2->index]= 1;
652 clipflag[vlr->v3->index]= 1;
653 if(vlr->v4) clipflag[vlr->v4->index]= 1;
657 /* calculate min and max */
658 for(a=0; a< obr->totvert;a++) {
659 if((a & 255)==0) ver= RE_findOrAddVert(obr, a);
663 VECCOPY(vec, ver->co);
664 mul_m4_v3(obviewmat, vec);
665 /* Z on visible side of lamp space */
667 float inpr, z= -vec[2];
669 /* since vec is rotated in lampspace, this is how to get the cosine of angle */
670 /* precision is set 20% larger */
675 if(inpr>=lar->spotsi) {
686 /* set clipping min and max */
688 float delta= (maxz - minz); /* threshold to prevent precision issues */
690 //printf("minz %f maxz %f delta %f\n", minz, maxz, delta);
691 if(lar->bufflag & LA_SHADBUF_AUTO_START)
692 lar->shb->d= minz - delta*0.02f; /* 0.02 is arbitrary... needs more thinking! */
693 if(lar->bufflag & LA_SHADBUF_AUTO_END)
694 lar->shb->clipend= maxz + delta*0.1f;
696 /* bias was calculated as percentage, we scale it to prevent animation issues */
697 delta= (lar->clipend-lar->clipsta)/(lar->shb->clipend-lar->shb->d);
698 //printf("bias delta %f\n", delta);
699 lar->shb->bias= (int) (delta*(float)lar->shb->bias);
703 static void makeflatshadowbuf(Render *re, LampRen *lar, float *jitbuf)
705 ShadBuf *shb= lar->shb;
709 rectz= MEM_mapallocN(sizeof(int)*shb->size*shb->size, "makeshadbuf");
711 for(samples=0; samples<shb->totbuf; samples++) {
712 zbuffer_shadow(re, shb->persmat, lar, rectz, shb->size, jitbuf[2*samples], jitbuf[2*samples+1]);
713 /* create Z tiles (for compression): this system is 24 bits!!! */
714 compress_shadowbuf(shb, rectz, lar->mode & LA_SQUARE);
716 if(re->test_break(re->tbh))
723 static void makedeepshadowbuf(Render *re, LampRen *lar, float *jitbuf)
725 ShadBuf *shb= lar->shb;
727 APixstrand *apixbufstrand= NULL;
728 ListBase apsmbase= {NULL, NULL};
731 apixbuf= MEM_callocN(sizeof(APixstr)*shb->size*shb->size, "APixbuf");
733 apixbufstrand= MEM_callocN(sizeof(APixstrand)*shb->size*shb->size, "APixbufstrand");
735 zbuffer_abuf_shadow(re, lar, shb->persmat, apixbuf, apixbufstrand, &apsmbase, shb->size,
736 shb->totbuf, (float(*)[2])jitbuf);
738 /* create Z tiles (for compression): this system is 24 bits!!! */
739 compress_deepshadowbuf(re, shb, apixbuf, apixbufstrand);
743 MEM_freeN(apixbufstrand);
747 void makeshadowbuf(Render *re, LampRen *lar)
749 ShadBuf *shb= lar->shb;
750 float wsize, *jitbuf, twozero[2]= {0.0f, 0.0f}, angle, temp;
752 if(lar->bufflag & (LA_SHADBUF_AUTO_START|LA_SHADBUF_AUTO_END))
753 shadowbuf_autoclip(re, lar);
755 /* just to enforce identical behaviour of all irregular buffers */
756 if(lar->buftype==LA_SHADBUF_IRREGULAR)
759 /* matrices and window: in winmat the transformation is being put,
760 transforming from observer view to lamp view, including lamp window matrix */
762 angle= saacos(lar->spotsi);
763 temp= 0.5f*shb->size*cos(angle)/sin(angle);
764 shb->pixsize= (shb->d)/temp;
765 wsize= shb->pixsize*(shb->size/2.0);
767 perspective_m4( shb->winmat,-wsize, wsize, -wsize, wsize, shb->d, shb->clipend);
768 mul_m4_m4m4(shb->persmat, shb->viewmat, shb->winmat);
770 if(ELEM3(lar->buftype, LA_SHADBUF_REGULAR, LA_SHADBUF_HALFWAY, LA_SHADBUF_DEEP)) {
771 shb->totbuf= lar->buffers;
773 /* jitter, weights - not threadsafe! */
774 BLI_lock_thread(LOCK_CUSTOM1);
775 shb->jit= give_jitter_tab(get_render_shadow_samples(&re->r, shb->samp));
776 make_jitter_weight_tab(re, shb, lar->filtertype);
777 BLI_unlock_thread(LOCK_CUSTOM1);
779 if(shb->totbuf==4) jitbuf= give_jitter_tab(2);
780 else if(shb->totbuf==9) jitbuf= give_jitter_tab(3);
781 else jitbuf= twozero;
784 if(lar->buftype == LA_SHADBUF_DEEP) {
785 makedeepshadowbuf(re, lar, jitbuf);
789 makeflatshadowbuf(re, lar, jitbuf);
791 /* printf("lampbuf %d\n", sizeoflampbuf(shb)); */
795 static void *do_shadow_thread(void *re_v)
797 Render *re= (Render*)re_v;
801 BLI_lock_thread(LOCK_CUSTOM1);
802 for(lar=re->lampren.first; lar; lar=lar->next) {
803 if(lar->shb && !lar->thread_assigned) {
804 lar->thread_assigned= 1;
808 BLI_unlock_thread(LOCK_CUSTOM1);
810 /* if type is irregular, this only sets the perspective matrix and autoclips */
812 makeshadowbuf(re, lar);
813 BLI_lock_thread(LOCK_CUSTOM1);
814 lar->thread_ready= 1;
815 BLI_unlock_thread(LOCK_CUSTOM1);
817 } while(lar && !re->test_break(re->tbh));
822 static volatile int g_break= 0;
823 static int thread_break(void *unused)
828 void threaded_makeshadowbufs(Render *re)
833 int (*test_break)(void *);
835 /* count number of threads to use */
837 for(lar=re->lampren.first; lar; lar= lar->next)
841 totthread= MIN2(totthread, re->r.threads);
844 totthread= 1; /* preview render */
847 for(lar=re->lampren.first; lar; lar= lar->next) {
848 if(re->test_break(re->tbh)) break;
850 /* if type is irregular, this only sets the perspective matrix and autoclips */
851 makeshadowbuf(re, lar);
856 /* swap test break function */
857 test_break= re->test_break;
858 re->test_break= thread_break;
860 for(lar=re->lampren.first; lar; lar= lar->next) {
861 lar->thread_assigned= 0;
862 lar->thread_ready= 0;
865 BLI_init_threads(&threads, do_shadow_thread, totthread);
867 for(a=0; a<totthread; a++)
868 BLI_insert_thread(&threads, re);
870 /* keep rendering as long as there are shadow buffers not ready */
872 if((g_break=test_break(re->tbh)))
877 BLI_lock_thread(LOCK_CUSTOM1);
878 for(lar=re->lampren.first; lar; lar= lar->next)
879 if(lar->shb && !lar->thread_ready)
881 BLI_unlock_thread(LOCK_CUSTOM1);
884 BLI_end_threads(&threads);
886 /* unset threadsafety */
887 re->test_break= test_break;
892 void freeshadowbuf(LampRen *lar)
895 ShadBuf *shb= lar->shb;
896 ShadSampleBuf *shsample;
899 for(shsample= shb->buffers.first; shsample; shsample= shsample->next) {
900 if(shsample->deepbuf) {
901 v= shb->size*shb->size;
903 if(shsample->deepbuf[b])
904 MEM_freeN(shsample->deepbuf[b]);
906 MEM_freeN(shsample->deepbuf);
907 MEM_freeN(shsample->totbuf);
910 intptr_t *ztile= shsample->zbuf;
911 char *ctile= shsample->cbuf;
913 v= (shb->size*shb->size)/256;
914 for(b=0; b<v; b++, ztile++, ctile++)
915 if(*ctile) MEM_freeN((void *) *ztile);
917 MEM_freeN(shsample->zbuf);
918 MEM_freeN(shsample->cbuf);
921 BLI_freelistN(&shb->buffers);
923 if(shb->weight) MEM_freeN(shb->weight);
931 static int firstreadshadbuf(ShadBuf *shb, ShadSampleBuf *shsample, int **rz, int xs, int ys, int nr)
933 /* return a 1 if fully compressed shadbuf-tile && z==const */
937 if(shsample->deepbuf)
940 /* always test borders of shadowbuffer */
941 if(xs<0) xs= 0; else if(xs>=shb->size) xs= shb->size-1;
942 if(ys<0) ys= 0; else if(ys>=shb->size) ys= shb->size-1;
945 ofs= (ys>>4)*(shb->size>>4) + (xs>>4);
946 ct= shsample->cbuf+ofs;
949 *rz= *( (int **)(shsample->zbuf+ofs) );
952 else if(*rz!= *( (int **)(shsample->zbuf+ofs) )) return 0;
960 static float readdeepvisibility(DeepSample *dsample, int tot, int z, int bias, float *biast)
962 DeepSample *ds, *prevds;
966 /* tricky stuff here; we use ints which can overflow easily with bias values */
969 for(a=0; a<tot && (z-bias > ds->z); a++, ds++)
975 return (ds-1)->v; /* completely behind all samples */
978 /* check if this read needs bias blending */
981 *biast= (float)(z - ds->z)/(float)bias;
987 return 1.0f; /* completely in front of all samples */
989 /* converting to float early here because ds->z - prevds->z can overflow */
991 t= ((float)(z-bias) - (float)prevds->z)/((float)ds->z - (float)prevds->z);
992 return t*ds->v + (1.0f-t)*prevds->v;
995 static float readdeepshadowbuf(ShadBuf *shb, ShadSampleBuf *shsample, int bias, int xs, int ys, int zs)
997 float v, biasv, biast;
1000 if(zs < - 0x7FFFFE00 + bias)
1001 return 1.0; /* extreme close to clipstart */
1004 ofs= ys*shb->size + xs;
1005 tot= shsample->totbuf[ofs];
1009 v= readdeepvisibility(shsample->deepbuf[ofs], tot, zs, bias, &biast);
1012 /* in soft bias area */
1013 biasv= readdeepvisibility(shsample->deepbuf[ofs], tot, zs, 0, 0);
1016 return (1.0f-biast)*v + biast*biasv;
1022 /* return 1.0 : fully in light */
1023 static float readshadowbuf(ShadBuf *shb, ShadSampleBuf *shsample, int bias, int xs, int ys, int zs)
1031 /* if(xs<0 || ys<0) return 1.0; */
1032 /* if(xs>=shb->size || ys>=shb->size) return 1.0; */
1034 /* always test borders of shadowbuffer */
1035 if(xs<0) xs= 0; else if(xs>=shb->size) xs= shb->size-1;
1036 if(ys<0) ys= 0; else if(ys>=shb->size) ys= shb->size-1;
1038 if(shsample->deepbuf)
1039 return readdeepshadowbuf(shb, shsample, bias, xs, ys, zs);
1042 ofs= (ys>>4)*(shb->size>>4) + (xs>>4);
1043 ct= shsample->cbuf+ofs;
1044 rz= *( (int **)(shsample->zbuf+ofs) );
1047 ct= ((char *)rz)+3*16*(ys & 15)+3*(xs & 15);
1055 ct+= 4+2*16*(ys & 15)+2*(xs & 15);
1064 ct+= 4+16*(ys & 15)+(xs & 15);
1072 /* got warning on this for 64 bits.... */
1073 /* but it's working code! in this case rz is not a pointer but zvalue (ton) */
1074 zsamp= GET_INT_FROM_POINTER(rz);
1077 /* tricky stuff here; we use ints which can overflow easily with bias values */
1079 if(zsamp > zs) return 1.0; /* absolute no shadow */
1080 else if(zs < - 0x7FFFFE00 + bias) return 1.0; /* extreme close to clipstart */
1081 else if(zsamp < zs-bias) return 0.0 ; /* absolute in shadow */
1082 else { /* soft area */
1084 temp= ( (float)(zs- zsamp) )/(float)bias;
1085 return 1.0 - temp*temp;
1090 static void shadowbuf_project_co(float *x, float *y, float *z, ShadBuf *shb, float co[3])
1092 float hco[4], size= 0.5f*(float)shb->size;
1094 copy_v3_v3(hco, co);
1097 mul_m4_v4(shb->persmat, hco);
1099 *x= size*(1.0f+hco[0]/hco[3]);
1100 *y= size*(1.0f+hco[1]/hco[3]);
1101 if(z) *z= (hco[2]/hco[3]);
1104 /* the externally called shadow testing (reading) function */
1105 /* return 1.0: no shadow at all */
1106 float testshadowbuf(Render *re, ShadBuf *shb, float *co, float *dxco, float *dyco, float inp, float mat_bias)
1108 ShadSampleBuf *shsample;
1109 float fac, dco[3], dx[3], dy[3], shadfac=0.0f;
1110 float xs1, ys1, zs1, *jit, *weight, xres, yres, biasf;
1111 int xs, ys, zs, bias, *rz;
1114 /* crash preventer */
1115 if(shb->buffers.first==NULL)
1118 /* when facing away, assume fully in shadow */
1122 /* project coordinate to pixel space */
1123 shadowbuf_project_co(&xs1, &ys1, &zs1, shb, co);
1125 /* clip z coordinate, z is projected so that (-1.0, 1.0) matches
1126 (clipstart, clipend), so we can do this simple test */
1129 else if(zs1<= -1.0f)
1132 zs= ((float)0x7FFFFFFF)*zs1;
1134 /* take num*num samples, increase area with fac */
1135 num= get_render_shadow_samples(&re->r, shb->samp);
1139 /* compute z bias */
1140 if(mat_bias!=0.0f) biasf= shb->bias*mat_bias;
1141 else biasf= shb->bias;
1142 /* with inp==1.0, bias is half the size. correction value was 1.1, giving errors
1143 on cube edges, with one side being almost frontal lighted (ton) */
1144 bias= (1.5f-inp*inp)*biasf;
1146 /* in case of no filtering we can do things simpler */
1148 for(shsample= shb->buffers.first; shsample; shsample= shsample->next)
1149 shadfac += readshadowbuf(shb, shsample, bias, (int)xs1, (int)ys1, zs);
1151 return shadfac/(float)shb->totbuf;
1154 /* calculate filter size */
1155 add_v3_v3v3(dco, co, dxco);
1156 shadowbuf_project_co(&dx[0], &dx[1], NULL, shb, dco);
1160 add_v3_v3v3(dco, co, dyco);
1161 shadowbuf_project_co(&dy[0], &dy[1], NULL, shb, dco);
1165 xres= fac*(fabs(dx[0]) + fabs(dy[0]));
1166 yres= fac*(fabs(dx[1]) + fabs(dy[1]));
1167 if(xres<1.0f) xres= 1.0f;
1168 if(yres<1.0f) yres= 1.0f;
1170 /* make xs1/xs1 corner of sample area */
1174 /* in case we have a constant value in a tile, we can do quicker lookup */
1175 if(xres<16.0f && yres<16.0f) {
1176 shsample= shb->buffers.first;
1177 if(firstreadshadbuf(shb, shsample, &rz, (int)xs1, (int)ys1, 0)) {
1178 if(firstreadshadbuf(shb, shsample, &rz, (int)(xs1+xres), (int)ys1, 1)) {
1179 if(firstreadshadbuf(shb, shsample, &rz, (int)xs1, (int)(ys1+yres), 1)) {
1180 if(firstreadshadbuf(shb, shsample, &rz, (int)(xs1+xres), (int)(ys1+yres), 1)) {
1181 return readshadowbuf(shb, shsample, bias,(int)xs1, (int)ys1, zs);
1188 /* full jittered shadow buffer lookup */
1189 for(shsample= shb->buffers.first; shsample; shsample= shsample->next) {
1191 weight= shb->weight;
1193 for(a=num; a>0; a--, jit+=2, weight++) {
1194 /* instead of jit i tried random: ugly! */
1195 /* note: the plus 0.5 gives best sampling results, jit goes from -0.5 to 0.5 */
1196 /* xs1 and ys1 are already corrected to be corner of sample area */
1197 xs= xs1 + xres*(jit[0] + 0.5f);
1198 ys= ys1 + yres*(jit[1] + 0.5f);
1200 shadfac+= *weight * readshadowbuf(shb, shsample, bias, xs, ys, zs);
1204 /* Renormalizes for the sample number: */
1205 return shadfac/(float)shb->totbuf;
1208 /* different function... sampling behind clipend can be LIGHT, bias is negative! */
1210 static float readshadowbuf_halo(ShadBuf *shb, ShadSampleBuf *shsample, int xs, int ys, int zs)
1214 int bias, zbias, zsamp;
1217 /* negative! The other side is more important */
1221 if(xs<0 || ys<0) return 0.0;
1222 if(xs>=shb->size || ys>=shb->size) return 0.0;
1225 ofs= (ys>>4)*(shb->size>>4) + (xs>>4);
1226 ct= shsample->cbuf+ofs;
1227 rz= *( (int **)(shsample->zbuf+ofs) );
1230 ct= ((char *)rz)+3*16*(ys & 15)+3*(xs & 15);
1239 ct+= 4+2*16*(ys & 15)+2*(xs & 15);
1248 ct+= 4+16*(ys & 15)+(xs & 15);
1256 /* same as before */
1257 /* still working code! (ton) */
1258 zsamp= GET_INT_FROM_POINTER(rz);
1261 /* NO schadow when sampled at 'eternal' distance */
1263 if(zsamp >= 0x7FFFFE00) return 1.0;
1265 if(zsamp > zs) return 1.0; /* absolute no shadww */
1267 /* bias is negative, so the (zs-bias) can be beyond 0x7fffffff */
1268 zbias= 0x7fffffff - zs;
1270 if( zsamp < zs-bias) return 0.0 ; /* absolute in shadow */
1272 else return 0.0 ; /* absolute shadow */
1277 temp= ( (float)(zs- zsamp) )/(float)bias;
1278 return 1.0 - temp*temp;
1282 float shadow_halo(LampRen *lar, float *p1, float *p2)
1284 /* p1 p2 already are rotated in spot-space */
1285 ShadBuf *shb= lar->shb;
1286 ShadSampleBuf *shsample;
1288 float labda, labdao, labdax, labday, ldx, ldy;
1289 float zf, xf1, yf1, zf1, xf2, yf2, zf2;
1290 float count, lightcount;
1291 int x, y, z, xs1, ys1;
1294 siz= 0.5*(float)shb->size;
1298 co[2]= p1[2]/lar->sh_zfac;
1300 mul_m4_v4(shb->winmat, co); /* rational hom co */
1301 xf1= siz*(1.0+co[0]/co[3]);
1302 yf1= siz*(1.0+co[1]/co[3]);
1308 co[2]= p2[2]/lar->sh_zfac;
1310 mul_m4_v4(shb->winmat, co); /* rational hom co */
1311 xf2= siz*(1.0+co[0]/co[3]);
1312 yf2= siz*(1.0+co[1]/co[3]);
1315 /* the 2dda (a pixel line formula) */
1322 labdax= (xf1-xs1-1.0)/(xf1-xf2);
1323 ldx= -shb->shadhalostep/(xf1-xf2);
1324 dx= shb->shadhalostep;
1327 labdax= (xf1-xs1)/(xf1-xf2);
1328 ldx= shb->shadhalostep/(xf1-xf2);
1329 dx= -shb->shadhalostep;
1339 labday= (yf1-ys1-1.0)/(yf1-yf2);
1340 ldy= -shb->shadhalostep/(yf1-yf2);
1341 dy= shb->shadhalostep;
1344 labday= (yf1-ys1)/(yf1-yf2);
1345 ldy= shb->shadhalostep/(yf1-yf2);
1346 dy= -shb->shadhalostep;
1356 labda= count= lightcount= 0.0;
1358 /* printf("start %x %x \n", (int)(0x7FFFFFFF*zf1), (int)(0x7FFFFFFF*zf2)); */
1363 if(labdax==labday) {
1379 labda= MIN2(labdax, labday);
1380 if(labda==labdao || labda>=1.0) break;
1382 zf= zf1 + labda*(zf2-zf1);
1383 count+= (float)shb->totbuf;
1385 if(zf<= -1.0) lightcount += 1.0; /* close to the spot */
1388 /* make sure, behind the clipend we extend halolines. */
1389 if(zf>=1.0) z= 0x7FFFF000;
1390 else z= (int)(0x7FFFF000*zf);
1392 for(shsample= shb->buffers.first; shsample; shsample= shsample->next)
1393 lightcount+= readshadowbuf_halo(shb, shsample, x, y, z);
1398 if(count!=0.0) return (lightcount/count);
1404 /* ********************* Irregular Shadow Buffer (ISB) ************* */
1405 /* ********** storage of all view samples in a raster of lists ***** */
1407 /* based on several articles describing this method, like:
1408 The Irregular Z-Buffer and its Application to Shadow Mapping
1409 Gregory S. Johnson - William R. Mark - Christopher A. Burns
1411 Alias-Free Shadow Maps
1412 Timo Aila and Samuli Laine
1415 /* bsp structure (actually kd tree) */
1417 #define BSPMAX_SAMPLE 128
1418 #define BSPMAX_DEPTH 32
1420 /* aligned with struct rctf */
1421 typedef struct Boxf {
1427 typedef struct ISBBranch {
1428 struct ISBBranch *left, *right;
1431 short totsamp, index, full, unused;
1432 ISBSample **samples;
1435 typedef struct BSPFace {
1437 float *v1, *v2, *v3, *v4;
1438 int obi; /* object for face lookup */
1439 int facenr; /* index to retrieve VlakRen */
1440 int type; /* only for strand now */
1441 short shad_alpha, is_full;
1443 /* strand caching data, optimize for point_behind_strand() */
1444 float radline, radline_end, len;
1445 float vec1[3], vec2[3], rc[3];
1448 /* boxes are in lamp projection */
1449 static void init_box(Boxf *box)
1451 box->xmin= 1000000.0f;
1453 box->ymin= 1000000.0f;
1455 box->zmin= 0x7FFFFFFF;
1456 box->zmax= - 0x7FFFFFFF;
1459 /* use v1 to calculate boundbox */
1460 static void bound_boxf(Boxf *box, float *v1)
1462 if(v1[0] < box->xmin) box->xmin= v1[0];
1463 if(v1[0] > box->xmax) box->xmax= v1[0];
1464 if(v1[1] < box->ymin) box->ymin= v1[1];
1465 if(v1[1] > box->ymax) box->ymax= v1[1];
1466 if(v1[2] < box->zmin) box->zmin= v1[2];
1467 if(v1[2] > box->zmax) box->zmax= v1[2];
1470 /* use v1 to calculate boundbox */
1471 static void bound_rectf(rctf *box, float *v1)
1473 if(v1[0] < box->xmin) box->xmin= v1[0];
1474 if(v1[0] > box->xmax) box->xmax= v1[0];
1475 if(v1[1] < box->ymin) box->ymin= v1[1];
1476 if(v1[1] > box->ymax) box->ymax= v1[1];
1480 /* halfway splitting, for initializing a more regular tree */
1481 static void isb_bsp_split_init(ISBBranch *root, MemArena *mem, int level)
1484 /* if level > 0 we create new branches and go deeper*/
1486 ISBBranch *left, *right;
1490 root->divider[0]= 0.5f*(root->box.xmin+root->box.xmax);
1491 root->divider[1]= 0.5f*(root->box.ymin+root->box.ymax);
1493 /* find best splitpoint */
1494 if(root->box.xmax-root->box.xmin > root->box.ymax-root->box.ymin)
1499 left= root->left= BLI_memarena_alloc(mem, sizeof(ISBBranch));
1500 right= root->right= BLI_memarena_alloc(mem, sizeof(ISBBranch));
1503 left->box= root->box;
1504 right->box= root->box;
1506 left->box.xmax= root->divider[0];
1507 right->box.xmin= root->divider[0];
1510 left->box.ymax= root->divider[1];
1511 right->box.ymin= root->divider[1];
1513 isb_bsp_split_init(left, mem, level-1);
1514 isb_bsp_split_init(right, mem, level-1);
1517 /* we add sample array */
1518 root->samples= BLI_memarena_alloc(mem, BSPMAX_SAMPLE*sizeof(void *));
1522 /* note; if all samples on same location we just spread them over 2 new branches */
1523 static void isb_bsp_split(ISBBranch *root, MemArena *mem)
1525 ISBBranch *left, *right;
1526 ISBSample *samples[BSPMAX_SAMPLE];
1530 root->divider[0]= root->divider[1]= 0.0f;
1531 for(a=BSPMAX_SAMPLE-1; a>=0; a--) {
1532 root->divider[0]+= root->samples[a]->zco[0];
1533 root->divider[1]+= root->samples[a]->zco[1];
1535 root->divider[0]/= BSPMAX_SAMPLE;
1536 root->divider[1]/= BSPMAX_SAMPLE;
1538 /* find best splitpoint */
1539 if(root->box.xmax-root->box.xmin > root->box.ymax-root->box.ymin)
1545 left= root->left= BLI_memarena_alloc(mem, sizeof(ISBBranch));
1546 right= root->right= BLI_memarena_alloc(mem, sizeof(ISBBranch));
1548 /* new sample array */
1549 left->samples= BLI_memarena_alloc(mem, BSPMAX_SAMPLE*sizeof(void *));
1550 right->samples= samples; // tmp
1553 for(a=BSPMAX_SAMPLE-1; a>=0; a--) {
1555 /* this prevents adding samples all to 1 branch when divider is equal to samples */
1556 if(root->samples[a]->zco[i] == root->divider[i])
1558 else if(root->samples[a]->zco[i] < root->divider[i])
1562 left->samples[left->totsamp]= root->samples[a];
1566 right->samples[right->totsamp]= root->samples[a];
1571 /* copy samples from tmp */
1572 memcpy(root->samples, samples, right->totsamp*(sizeof(void *)));
1573 right->samples= root->samples;
1574 root->samples= NULL;
1577 left->box= root->box;
1578 right->box= root->box;
1580 left->box.xmax= root->divider[0];
1581 right->box.xmin= root->divider[0];
1584 left->box.ymax= root->divider[1];
1585 right->box.ymin= root->divider[1];
1589 /* inserts sample in main tree, also splits on threshold */
1590 /* returns 1 if error */
1591 static int isb_bsp_insert(ISBBranch *root, MemArena *memarena, ISBSample *sample)
1593 ISBBranch *bspn= root;
1594 float *zco= sample->zco;
1597 /* debug counter, also used to check if something was filled in ever */
1600 /* going over branches until last one found */
1602 if(zco[bspn->index] <= bspn->divider[bspn->index])
1608 /* bspn now is the last branch */
1610 if(bspn->totsamp==BSPMAX_SAMPLE) {
1611 printf("error in bsp branch\n"); /* only for debug, cannot happen */
1616 bspn->samples[bspn->totsamp]= sample;
1619 /* split if allowed and needed */
1620 if(bspn->totsamp==BSPMAX_SAMPLE) {
1621 if(i==BSPMAX_DEPTH) {
1622 bspn->totsamp--; /* stop filling in... will give errors */
1625 isb_bsp_split(bspn, memarena);
1630 static float VecLen2f( float *v1, float *v2)
1632 float x= v1[0]-v2[0];
1633 float y= v1[1]-v2[1];
1634 return (float)sqrt(x*x+y*y);
1637 /* initialize vars in face, for optimal point-in-face test */
1638 static void bspface_init_strand(BSPFace *face)
1641 face->radline= 0.5f*VecLen2f(face->v1, face->v2);
1643 mid_v3_v3v3(face->vec1, face->v1, face->v2);
1645 mid_v3_v3v3(face->vec2, face->v3, face->v4);
1647 VECCOPY(face->vec2, face->v3);
1649 face->rc[0]= face->vec2[0]-face->vec1[0];
1650 face->rc[1]= face->vec2[1]-face->vec1[1];
1651 face->rc[2]= face->vec2[2]-face->vec1[2];
1653 face->len= face->rc[0]*face->rc[0]+ face->rc[1]*face->rc[1];
1655 if(face->len!=0.0f) {
1656 face->radline_end= face->radline/sqrt(face->len);
1657 face->len= 1.0f/face->len;
1661 /* brought back to a simple 2d case */
1662 static int point_behind_strand(float *p, BSPFace *face)
1664 /* v1 - v2 is radius, v1 - v3 length */
1665 float dist, rc[2], pt[2];
1667 /* using code from dist_to_line_segment_v2(), distance vec to line-piece */
1669 if(face->len==0.0f) {
1670 rc[0]= p[0]-face->vec1[0];
1671 rc[1]= p[1]-face->vec1[1];
1672 dist= (float)(sqrt(rc[0]*rc[0]+ rc[1]*rc[1]));
1674 if(dist < face->radline)
1678 float labda= ( face->rc[0]*(p[0]-face->vec1[0]) + face->rc[1]*(p[1]-face->vec1[1]) )*face->len;
1680 if(labda > -face->radline_end && labda < 1.0f+face->radline_end) {
1681 /* hesse for dist: */
1682 //dist= (float)(fabs( (p[0]-vec2[0])*rc[1] + (p[1]-vec2[1])*rc[0])/len);
1684 pt[0]= labda*face->rc[0]+face->vec1[0];
1685 pt[1]= labda*face->rc[1]+face->vec1[1];
1689 dist= (float)sqrt(rc[0]*rc[0]+ rc[1]*rc[1]);
1691 if(dist < face->radline) {
1692 float zval= face->vec1[2] + labda*face->rc[2];
1702 /* return 1 if inside. code derived from src/parametrizer.c */
1703 static int point_behind_tria2d(float *p, float *v1, float *v2, float *v3)
1705 float a[2], c[2], h[2], div;
1708 a[0] = v2[0] - v1[0];
1709 a[1] = v2[1] - v1[1];
1710 c[0] = v3[0] - v1[0];
1711 c[1] = v3[1] - v1[1];
1713 div = a[0]*c[1] - a[1]*c[0];
1717 h[0] = p[0] - v1[0];
1718 h[1] = p[1] - v1[1];
1722 u = (h[0]*c[1] - h[1]*c[0])*div;
1724 v = (a[0]*h[1] - a[1]*h[0])*div;
1726 if( u + v <= 1.0f) {
1727 /* inside, now check if point p is behind */
1728 float z= (1.0f-u-v)*v1[2] + u*v2[2] + v*v3[2];
1739 /* tested these calls, but it gives inaccuracy, 'side' cannot be found reliably using v3 */
1741 /* check if line v1-v2 has all rect points on other side of point v3 */
1742 static int rect_outside_line(rctf *rect, float *v1, float *v2, float *v3)
1747 /* line formula for v1-v2 */
1750 c= -a*v1[0] - b*v1[1];
1751 side= a*v3[0] + b*v3[1] + c < 0.0f;
1753 /* the four quad points */
1754 if( side==(rect->xmin*a + rect->ymin*b + c >= 0.0f) )
1755 if( side==(rect->xmax*a + rect->ymin*b + c >= 0.0f) )
1756 if( side==(rect->xmax*a + rect->ymax*b + c >= 0.0f) )
1757 if( side==(rect->xmin*a + rect->ymax*b + c >= 0.0f) )
1762 /* check if one of the triangle edges separates all rect points on 1 side */
1763 static int rect_isect_tria(rctf *rect, float *v1, float *v2, float *v3)
1765 if(rect_outside_line(rect, v1, v2, v3))
1767 if(rect_outside_line(rect, v2, v3, v1))
1769 if(rect_outside_line(rect, v3, v1, v2))
1775 /* if face overlaps a branch, it executes func. recursive */
1776 static void isb_bsp_face_inside(ISBBranch *bspn, BSPFace *face)
1779 /* are we descending? */
1781 /* hrmf, the box struct cannot be addressed with index */
1782 if(bspn->index==0) {
1783 if(face->box.xmin <= bspn->divider[0])
1784 isb_bsp_face_inside(bspn->left, face);
1785 if(face->box.xmax > bspn->divider[0])
1786 isb_bsp_face_inside(bspn->right, face);
1789 if(face->box.ymin <= bspn->divider[1])
1790 isb_bsp_face_inside(bspn->left, face);
1791 if(face->box.ymax > bspn->divider[1])
1792 isb_bsp_face_inside(bspn->right, face);
1796 /* else: end branch reached */
1799 if(bspn->totsamp==0) return;
1801 /* check for nodes entirely in shadow, can be skipped */
1802 if(bspn->totsamp==bspn->full)
1805 /* if bsp node is entirely in front of face, give up */
1806 if(bspn->box.zmax < face->box.zmin)
1809 /* if face boundbox is outside of branch rect, give up */
1810 if(0==BLI_isect_rctf((rctf *)&face->box, (rctf *)&bspn->box, NULL))
1813 /* test all points inside branch */
1814 for(a=bspn->totsamp-1; a>=0; a--) {
1815 ISBSample *samp= bspn->samples[a];
1817 if((samp->facenr!=face->facenr || samp->obi!=face->obi) && samp->shadfac) {
1818 if(face->box.zmin < samp->zco[2]) {
1819 if(BLI_in_rctf((rctf *)&face->box, samp->zco[0], samp->zco[1])) {
1823 if(point_behind_strand(samp->zco, face))
1826 else if( point_behind_tria2d(samp->zco, face->v1, face->v2, face->v3))
1828 else if(face->v4 && point_behind_tria2d(samp->zco, face->v1, face->v3, face->v4))
1832 *(samp->shadfac) += face->shad_alpha;
1833 /* optimize; is_full means shad_alpha==4096 */
1834 if(*(samp->shadfac) >= 4096 || face->is_full) {
1836 samp->shadfac= NULL;
1846 /* based on available samples, recalculate the bounding box for bsp nodes, recursive */
1847 static void isb_bsp_recalc_box(ISBBranch *root)
1850 isb_bsp_recalc_box(root->left);
1851 isb_bsp_recalc_box(root->right);
1853 else if(root->totsamp) {
1856 init_box(&root->box);
1857 for(a=root->totsamp-1; a>=0; a--)
1858 bound_boxf(&root->box, root->samples[a]->zco);
1862 /* callback function for zbuf clip */
1863 static void isb_bsp_test_strand(ZSpan *zspan, int obi, int zvlnr, float *v1, float *v2, float *v3, float *v4)
1872 face.facenr= zvlnr & ~RE_QUAD_OFFS;
1873 face.type= R_STRAND;
1875 face.shad_alpha= (short)ceil(4096.0f*zspan->shad_alpha/(float)R.osa);
1877 face.shad_alpha= (short)ceil(4096.0f*zspan->shad_alpha);
1879 face.is_full= (zspan->shad_alpha==1.0f);
1881 /* setup boundbox */
1882 init_box(&face.box);
1883 bound_boxf(&face.box, v1);
1884 bound_boxf(&face.box, v2);
1885 bound_boxf(&face.box, v3);
1887 bound_boxf(&face.box, v4);
1889 /* optimize values */
1890 bspface_init_strand(&face);
1892 isb_bsp_face_inside((ISBBranch *)zspan->rectz, &face);
1896 /* callback function for zbuf clip */
1897 static void isb_bsp_test_face(ZSpan *zspan, int obi, int zvlnr, float *v1, float *v2, float *v3, float *v4)
1906 face.facenr= zvlnr & ~RE_QUAD_OFFS;
1909 face.shad_alpha= (short)ceil(4096.0f*zspan->shad_alpha/(float)R.osa);
1911 face.shad_alpha= (short)ceil(4096.0f*zspan->shad_alpha);
1913 face.is_full= (zspan->shad_alpha==1.0f);
1915 /* setup boundbox */
1916 init_box(&face.box);
1917 bound_boxf(&face.box, v1);
1918 bound_boxf(&face.box, v2);
1919 bound_boxf(&face.box, v3);
1921 bound_boxf(&face.box, v4);
1923 isb_bsp_face_inside((ISBBranch *)zspan->rectz, &face);
1926 static int testclip_minmax(float *ho, float *minmax)
1931 if( ho[0] > minmax[1]*wco) flag = 1;
1932 else if( ho[0]< minmax[0]*wco) flag = 2;
1934 if( ho[1] > minmax[3]*wco) flag |= 4;
1935 else if( ho[1]< minmax[2]*wco) flag |= 8;
1940 /* main loop going over all faces and check in bsp overlaps, fill in shadfac values */
1941 static void isb_bsp_fillfaces(Render *re, LampRen *lar, ISBBranch *root)
1943 ObjectInstanceRen *obi;
1945 ShadBuf *shb= lar->shb;
1946 ZSpan zspan, zspanstrand;
1949 float minmaxf[4], winmat[4][4];
1950 int size= shb->size;
1951 int i, a, ok=1, lay= -1;
1953 /* further optimize, also sets minz maxz */
1954 isb_bsp_recalc_box(root);
1956 /* extra clipping for minmax */
1957 minmaxf[0]= (2.0f*root->box.xmin - size-2.0f)/size;
1958 minmaxf[1]= (2.0f*root->box.xmax - size+2.0f)/size;
1959 minmaxf[2]= (2.0f*root->box.ymin - size-2.0f)/size;
1960 minmaxf[3]= (2.0f*root->box.ymax - size+2.0f)/size;
1962 if(lar->mode & (LA_LAYER|LA_LAYER_SHADOW)) lay= lar->lay;
1964 /* (ab)use zspan, since we use zbuffer clipping code */
1965 zbuf_alloc_span(&zspan, size, size, re->clipcrop);
1967 zspan.zmulx= ((float)size)/2.0f;
1968 zspan.zmuly= ((float)size)/2.0f;
1972 /* pass on bsp root to zspan */
1973 zspan.rectz= (int *)root;
1975 /* filling methods */
1977 // zspan.zbuflinefunc= zbufline_onlyZ;
1978 zspan.zbuffunc= isb_bsp_test_face;
1979 zspanstrand.zbuffunc= isb_bsp_test_strand;
1981 for(i=0, obi=re->instancetable.first; obi; i++, obi=obi->next) {
1984 if(obi->flag & R_TRANSFORMED)
1985 mul_m4_m4m4(winmat, obi->mat, shb->persmat);
1987 copy_m4_m4(winmat, shb->persmat);
1989 for(a=0; a<obr->totvlak; a++) {
1991 if((a & 255)==0) vlr= obr->vlaknodes[a>>8].vlak;
1994 /* note, these conditions are copied in shadowbuf_autoclip() */
1998 if((ma->mode & MA_SHADBUF)==0) ok= 0;
1999 if(ma->material_type == MA_TYPE_WIRE) ok= 0;
2000 zspanstrand.shad_alpha= zspan.shad_alpha= ma->shad_alpha;
2003 if(ok && (obi->lay & lay)) {
2005 int c1, c2, c3, c4=0;
2006 int d1, d2, d3, d4=0;
2009 /* create hocos per face, it is while render */
2010 projectvert(vlr->v1->co, winmat, hoco[0]); d1= testclip_minmax(hoco[0], minmaxf);
2011 projectvert(vlr->v2->co, winmat, hoco[1]); d2= testclip_minmax(hoco[1], minmaxf);
2012 projectvert(vlr->v3->co, winmat, hoco[2]); d3= testclip_minmax(hoco[2], minmaxf);
2014 projectvert(vlr->v4->co, winmat, hoco[3]); d4= testclip_minmax(hoco[3], minmaxf);
2017 /* minmax clipping */
2018 if(vlr->v4) partclip= d1 & d2 & d3 & d4;
2019 else partclip= d1 & d2 & d3;
2023 /* window clipping */
2024 c1= testclip(hoco[0]);
2025 c2= testclip(hoco[1]);
2026 c3= testclip(hoco[2]);
2028 c4= testclip(hoco[3]);
2030 /* ***** NO WIRE YET */
2031 if(ma->material_type == MA_TYPE_WIRE) {
2033 zbufclipwire(&zspan, i, a+1, vlr->ec, hoco[0], hoco[1], hoco[2], hoco[3], c1, c2, c3, c4);
2035 zbufclipwire(&zspan, i, a+1, vlr->ec, hoco[0], hoco[1], hoco[2], 0, c1, c2, c3, 0);
2038 if(vlr->flag & R_STRAND)
2039 zbufclip4(&zspanstrand, i, a+1, hoco[0], hoco[1], hoco[2], hoco[3], c1, c2, c3, c4);
2041 zbufclip4(&zspan, i, a+1, hoco[0], hoco[1], hoco[2], hoco[3], c1, c2, c3, c4);
2044 zbufclip(&zspan, i, a+1, hoco[0], hoco[1], hoco[2], c1, c2, c3);
2051 zbuf_free_span(&zspan);
2054 /* returns 1 when the viewpixel is visible in lampbuffer */
2055 static int viewpixel_to_lampbuf(ShadBuf *shb, ObjectInstanceRen *obi, VlakRen *vlr, float x, float y, float *co)
2057 float hoco[4], v1[3], nor[3];
2058 float dface, fac, siz;
2060 RE_vlakren_get_normal(&R, obi, vlr, nor);
2061 VECCOPY(v1, vlr->v1->co);
2062 if(obi->flag & R_TRANSFORMED)
2063 mul_m4_v3(obi->mat, v1);
2065 /* from shadepixel() */
2066 dface= v1[0]*nor[0] + v1[1]*nor[1] + v1[2]*nor[2];
2069 /* ortho viewplane cannot intersect using view vector originating in (0,0,0) */
2070 if(R.r.mode & R_ORTHO) {
2071 /* x and y 3d coordinate can be derived from pixel coord and winmat */
2072 float fx= 2.0/(R.winx*R.winmat[0][0]);
2073 float fy= 2.0/(R.winy*R.winmat[1][1]);
2075 hoco[0]= (x - 0.5*R.winx)*fx - R.winmat[3][0]/R.winmat[0][0];
2076 hoco[1]= (y - 0.5*R.winy)*fy - R.winmat[3][1]/R.winmat[1][1];
2078 /* using a*x + b*y + c*z = d equation, (a b c) is normal */
2080 hoco[2]= (dface - nor[0]*hoco[0] - nor[1]*hoco[1])/nor[2];
2087 calc_view_vector(view, x, y);
2089 div= nor[0]*view[0] + nor[1]*view[1] + nor[2]*view[2];
2095 hoco[0]= fac*view[0];
2096 hoco[1]= fac*view[1];
2097 hoco[2]= fac*view[2];
2100 /* move 3d vector to lampbuf */
2101 mul_m4_v4(shb->persmat, hoco); /* rational hom co */
2103 /* clip We can test for -1.0/1.0 because of the properties of the
2104 * coordinate transformations. */
2106 if(hoco[0]<-fac || hoco[0]>fac)
2108 if(hoco[1]<-fac || hoco[1]>fac)
2110 if(hoco[2]<-fac || hoco[2]>fac)
2113 siz= 0.5f*(float)shb->size;
2114 co[0]= siz*(1.0f+hoco[0]/hoco[3]) -0.5f;
2115 co[1]= siz*(1.0f+hoco[1]/hoco[3]) -0.5f;
2116 co[2]= ((float)0x7FFFFFFF)*(hoco[2]/hoco[3]);
2118 /* XXXX bias, much less than normal shadbuf, or do we need a constant? */
2119 co[2] -= 0.05f*shb->bias;
2124 /* storage of shadow results, solid osa and transp case */
2125 static void isb_add_shadfac(ISBShadfacA **isbsapp, MemArena *mem, int obi, int facenr, short shadfac, short samples)
2130 /* in osa case, the samples were filled in with factor 1.0/R.osa. if fewer samples we have to correct */
2132 shadfacf= ((float)shadfac*R.osa)/(4096.0*samples);
2134 shadfacf= ((float)shadfac)/(4096.0);
2136 new= BLI_memarena_alloc(mem, sizeof(ISBShadfacA));
2138 new->facenr= facenr & ~RE_QUAD_OFFS;
2139 new->shadfac= shadfacf;
2141 new->next= (*isbsapp);
2148 /* adding samples, solid case */
2149 static int isb_add_samples(RenderPart *pa, ISBBranch *root, MemArena *memarena, ISBSample **samplebuf)
2151 int xi, yi, *xcos, *ycos;
2152 int sample, bsp_err= 0;
2154 /* bsp split doesn't like to handle regular sequenes */
2155 xcos= MEM_mallocN( pa->rectx*sizeof(int), "xcos");
2156 ycos= MEM_mallocN( pa->recty*sizeof(int), "ycos");
2157 for(xi=0; xi<pa->rectx; xi++)
2159 for(yi=0; yi<pa->recty; yi++)
2161 BLI_array_randomize(xcos, sizeof(int), pa->rectx, 12345);
2162 BLI_array_randomize(ycos, sizeof(int), pa->recty, 54321);
2164 for(sample=0; sample<(R.osa?R.osa:1); sample++) {
2165 ISBSample *samp= samplebuf[sample], *samp1;
2167 for(yi=0; yi<pa->recty; yi++) {
2169 for(xi=0; xi<pa->rectx; xi++) {
2171 samp1= samp + y*pa->rectx + x;
2173 bsp_err |= isb_bsp_insert(root, memarena, samp1);
2186 /* lar->shb, pa->rectz and pa->rectp should exist */
2187 static void isb_make_buffer(RenderPart *pa, LampRen *lar)
2189 ShadBuf *shb= lar->shb;
2191 ISBSample *samp, *samplebuf[16]; /* should be RE_MAX_OSA */
2195 int *recto, *rectp, x, y, sindex, sample, bsp_err=0;
2197 /* storage for shadow, per thread */
2198 isbdata= shb->isb_result[pa->thread];
2200 /* to map the shi->xs and ys coordinate */
2201 isbdata->minx= pa->disprect.xmin;
2202 isbdata->miny= pa->disprect.ymin;
2203 isbdata->rectx= pa->rectx;
2204 isbdata->recty= pa->recty;
2206 /* branches are added using memarena (32k branches) */
2207 memarena = BLI_memarena_new(0x8000 * sizeof(ISBBranch), "isb arena");
2208 BLI_memarena_use_calloc(memarena);
2210 /* samplebuf is in camera view space (pixels) */
2211 for(sample=0; sample<(R.osa?R.osa:1); sample++)
2212 samplebuf[sample]= MEM_callocN(sizeof(ISBSample)*pa->rectx*pa->recty, "isb samplebuf");
2214 /* for end result, ISBSamples point to this in non OSA case, otherwise to pixstruct->shadfac */
2216 isbdata->shadfacs= MEM_callocN(pa->rectx*pa->recty*sizeof(short), "isb shadfacs");
2218 /* setup bsp root */
2219 memset(&root, 0, sizeof(ISBBranch));
2220 root.box.xmin= (float)shb->size;
2221 root.box.ymin= (float)shb->size;
2223 /* create the sample buffers */
2224 for(sindex=0, y=0; y<pa->recty; y++) {
2225 for(x=0; x<pa->rectx; x++, sindex++) {
2227 /* this makes it a long function, but splitting it out would mean 10+ arguments */
2228 /* first check OSA case */
2230 rd= pa->rectdaps + sindex;
2232 float xs= (float)(x + pa->disprect.xmin);
2233 float ys= (float)(y + pa->disprect.ymin);
2235 for(sample=0; sample<R.osa; sample++) {
2236 PixStr *ps= (PixStr *)(*rd);
2237 int mask= (1<<sample);
2244 if(ps && ps->facenr>0) {
2245 ObjectInstanceRen *obi= &R.objectinstance[ps->obi];
2246 ObjectRen *obr= obi->obr;
2247 VlakRen *vlr= RE_findOrAddVlak(obr, (ps->facenr-1) & RE_QUAD_MASK);
2249 samp= samplebuf[sample] + sindex;
2250 /* convert image plane pixel location to lamp buffer space */
2251 if(viewpixel_to_lampbuf(shb, obi, vlr, xs + R.jit[sample][0], ys + R.jit[sample][1], samp->zco)) {
2253 samp->facenr= ps->facenr & ~RE_QUAD_OFFS;
2255 samp->shadfac= &ps->shadfac;
2256 bound_rectf((rctf *)&root.box, samp->zco);
2263 rectp= pa->rectp + sindex;
2264 recto= pa->recto + sindex;
2266 ObjectInstanceRen *obi= &R.objectinstance[*recto];
2267 ObjectRen *obr= obi->obr;
2268 VlakRen *vlr= RE_findOrAddVlak(obr, (*rectp-1) & RE_QUAD_MASK);
2269 float xs= (float)(x + pa->disprect.xmin);
2270 float ys= (float)(y + pa->disprect.ymin);
2272 samp= samplebuf[0] + sindex;
2273 /* convert image plane pixel location to lamp buffer space */
2274 if(viewpixel_to_lampbuf(shb, obi, vlr, xs, ys, samp->zco)) {
2276 samp->facenr= *rectp & ~RE_QUAD_OFFS;
2277 samp->shadfac= isbdata->shadfacs + sindex;
2278 bound_rectf((rctf *)&root.box, samp->zco);
2285 /* simple method to see if we have samples */
2286 if(root.box.xmin != (float)shb->size) {
2287 /* now create a regular split, root.box has the initial bounding box of all pixels */
2288 /* split bsp 8 levels deep, in regular grid (16 x 16) */
2289 isb_bsp_split_init(&root, memarena, 8);
2291 /* insert all samples in BSP now */
2292 bsp_err= isb_add_samples(pa, &root, memarena, samplebuf);
2295 /* go over all faces and fill in shadow values */
2297 isb_bsp_fillfaces(&R, lar, &root); /* shb->persmat should have been calculated */
2299 /* copy shadow samples to persistant buffer, reduce memory overhead */
2301 ISBShadfacA **isbsa= isbdata->shadfaca= MEM_callocN(pa->rectx*pa->recty*sizeof(void *), "isb shadfacs");
2303 isbdata->memarena = BLI_memarena_new(0x8000 * sizeof(ISBSampleA), "isb arena");
2304 BLI_memarena_use_calloc(isbdata->memarena);
2306 for(rd= pa->rectdaps, x=pa->rectx*pa->recty; x>0; x--, rd++, isbsa++) {
2309 PixStr *ps= (PixStr *)(*rd);
2312 isb_add_shadfac(isbsa, isbdata->memarena, ps->obi, ps->facenr, ps->shadfac, count_mask(ps->mask));
2321 if(isbdata->shadfacs) {
2322 MEM_freeN(isbdata->shadfacs);
2323 isbdata->shadfacs= NULL;
2328 BLI_memarena_free(memarena);
2331 for(x=0; x<(R.osa?R.osa:1); x++)
2332 MEM_freeN(samplebuf[x]);
2334 if(bsp_err) printf("error in filling bsp\n");
2337 /* add sample to buffer, isbsa is the root sample in a buffer */
2338 static ISBSampleA *isb_alloc_sample_transp(ISBSampleA **isbsa, MemArena *mem)
2342 new= BLI_memarena_alloc(mem, sizeof(ISBSampleA));
2344 new->next= (*isbsa);
2352 /* adding samples in BSP, transparent case */
2353 static int isb_add_samples_transp(RenderPart *pa, ISBBranch *root, MemArena *memarena, ISBSampleA ***samplebuf)
2355 int xi, yi, *xcos, *ycos;
2356 int sample, bsp_err= 0;
2358 /* bsp split doesn't like to handle regular sequenes */
2359 xcos= MEM_mallocN( pa->rectx*sizeof(int), "xcos");
2360 ycos= MEM_mallocN( pa->recty*sizeof(int), "ycos");
2361 for(xi=0; xi<pa->rectx; xi++)
2363 for(yi=0; yi<pa->recty; yi++)
2365 BLI_array_randomize(xcos, sizeof(int), pa->rectx, 12345);
2366 BLI_array_randomize(ycos, sizeof(int), pa->recty, 54321);
2368 for(sample=0; sample<(R.osa?R.osa:1); sample++) {
2369 ISBSampleA **samp= samplebuf[sample], *samp1;
2371 for(yi=0; yi<pa->recty; yi++) {
2373 for(xi=0; xi<pa->rectx; xi++) {
2376 samp1= *(samp + y*pa->rectx + x);
2378 bsp_err |= isb_bsp_insert(root, memarena, (ISBSample *)samp1);
2393 /* Ztransp version */
2394 /* lar->shb, pa->rectz and pa->rectp should exist */
2395 static void isb_make_buffer_transp(RenderPart *pa, APixstr *apixbuf, LampRen *lar)
2397 ShadBuf *shb= lar->shb;
2399 ISBSampleA *samp, **samplebuf[16]; /* MAX_OSA */
2403 int x, y, sindex, sample, bsp_err=0;
2405 /* storage for shadow, per thread */
2406 isbdata= shb->isb_result[pa->thread];
2408 /* to map the shi->xs and ys coordinate */
2409 isbdata->minx= pa->disprect.xmin;
2410 isbdata->miny= pa->disprect.ymin;
2411 isbdata->rectx= pa->rectx;
2412 isbdata->recty= pa->recty;
2414 /* branches are added using memarena (32k branches) */
2415 memarena = BLI_memarena_new(0x8000 * sizeof(ISBBranch), "isb arena");
2416 BLI_memarena_use_calloc(memarena);
2418 /* samplebuf is in camera view space (pixels) */
2419 for(sample=0; sample<(R.osa?R.osa:1); sample++)
2420 samplebuf[sample]= MEM_callocN(sizeof(void *)*pa->rectx*pa->recty, "isb alpha samplebuf");
2422 /* setup bsp root */
2423 memset(&root, 0, sizeof(ISBBranch));
2424 root.box.xmin= (float)shb->size;
2425 root.box.ymin= (float)shb->size;
2427 /* create the sample buffers */
2428 for(ap= apixbuf, sindex=0, y=0; y<pa->recty; y++) {
2429 for(x=0; x<pa->rectx; x++, sindex++, ap++) {
2433 float xs= (float)(x + pa->disprect.xmin);
2434 float ys= (float)(y + pa->disprect.ymin);
2436 for(apn=ap; apn; apn= apn->next) {
2438 for(a=0; a<4; a++) {
2440 ObjectInstanceRen *obi= &R.objectinstance[apn->obi[a]];
2441 ObjectRen *obr= obi->obr;
2442 VlakRen *vlr= RE_findOrAddVlak(obr, (apn->p[a]-1) & RE_QUAD_MASK);
2445 /* here we store shadfac, easier to create the end storage buffer. needs zero'ed, multiple shadowbufs use it */
2449 for(sample=0; sample<R.osa; sample++) {
2450 int mask= (1<<sample);
2452 if(apn->mask[a] & mask) {
2454 /* convert image plane pixel location to lamp buffer space */
2455 if(viewpixel_to_lampbuf(shb, obi, vlr, xs + R.jit[sample][0], ys + R.jit[sample][1], zco)) {
2456 samp= isb_alloc_sample_transp(samplebuf[sample] + sindex, memarena);
2457 samp->obi= apn->obi[a];
2458 samp->facenr= apn->p[a] & ~RE_QUAD_OFFS;
2459 samp->shadfac= &apn->shadfac[a];
2461 VECCOPY(samp->zco, zco);
2462 bound_rectf((rctf *)&root.box, samp->zco);
2469 /* convert image plane pixel location to lamp buffer space */
2470 if(viewpixel_to_lampbuf(shb, obi, vlr, xs, ys, zco)) {
2472 samp= isb_alloc_sample_transp(samplebuf[0] + sindex, memarena);
2473 samp->obi= apn->obi[a];
2474 samp->facenr= apn->p[a] & ~RE_QUAD_OFFS;
2475 samp->shadfac= &apn->shadfac[a];
2477 VECCOPY(samp->zco, zco);
2478 bound_rectf((rctf *)&root.box, samp->zco);
2488 /* simple method to see if we have samples */
2489 if(root.box.xmin != (float)shb->size) {
2490 /* now create a regular split, root.box has the initial bounding box of all pixels */
2491 /* split bsp 8 levels deep, in regular grid (16 x 16) */
2492 isb_bsp_split_init(&root, memarena, 8);
2494 /* insert all samples in BSP now */
2495 bsp_err= isb_add_samples_transp(pa, &root, memarena, samplebuf);
2498 ISBShadfacA **isbsa;
2500 /* go over all faces and fill in shadow values */
2501 isb_bsp_fillfaces(&R, lar, &root); /* shb->persmat should have been calculated */
2503 /* copy shadow samples to persistant buffer, reduce memory overhead */
2504 isbsa= isbdata->shadfaca= MEM_callocN(pa->rectx*pa->recty*sizeof(void *), "isb shadfacs");
2506 isbdata->memarena = BLI_memarena_new(0x8000 * sizeof(ISBSampleA), "isb arena");
2508 for(ap= apixbuf, x=pa->rectx*pa->recty; x>0; x--, ap++, isbsa++) {
2512 for(apn=ap; apn; apn= apn->next) {
2514 for(a=0; a<4; a++) {
2515 if(apn->p[a] && apn->shadfac[a]) {
2517 isb_add_shadfac(isbsa, isbdata->memarena, apn->obi[a], apn->p[a], apn->shadfac[a], count_mask(apn->mask[a]));
2519 isb_add_shadfac(isbsa, isbdata->memarena, apn->obi[a], apn->p[a], apn->shadfac[a], 0);
2529 BLI_memarena_free(memarena);
2532 for(x=0; x<(R.osa?R.osa:1); x++)
2533 MEM_freeN(samplebuf[x]);
2535 if(bsp_err) printf("error in filling bsp\n");
2542 /* returns amount of light (1.0 = no shadow) */
2543 /* note, shadepixel() rounds the coordinate, not the real sample info */
2544 float ISB_getshadow(ShadeInput *shi, ShadBuf *shb)
2546 /* if raytracing, we can't accept irregular shadow */
2548 ISBData *isbdata= shb->isb_result[shi->thread];
2551 if(isbdata->shadfacs || isbdata->shadfaca) {
2552 int x= shi->xs - isbdata->minx;
2554 if(x >= 0 && x < isbdata->rectx) {
2555 int y= shi->ys - isbdata->miny;
2557 if(y >= 0 && y < isbdata->recty) {
2558 if(isbdata->shadfacs) {
2559 short *sp= isbdata->shadfacs + y*isbdata->rectx + x;
2560 return *sp>=4096?0.0f:1.0f - ((float)*sp)/4096.0f;
2563 int sindex= y*isbdata->rectx + x;
2564 int obi= shi->obi - R.objectinstance;
2565 ISBShadfacA *isbsa= *(isbdata->shadfaca + sindex);
2568 if(isbsa->facenr==shi->facenr+1 && isbsa->obi==obi)
2569 return isbsa->shadfac>=1.0f?0.0f:1.0f - isbsa->shadfac;
2581 /* part is supposed to be solid zbuffered (apixbuf==NULL) or transparent zbuffered */
2582 void ISB_create(RenderPart *pa, APixstr *apixbuf)
2586 /* go over all lamps, and make the irregular buffers */
2587 for(go=R.lights.first; go; go= go->next) {
2588 LampRen *lar= go->lampren;
2590 if(lar->type==LA_SPOT && lar->shb && lar->buftype==LA_SHADBUF_IRREGULAR) {
2592 /* create storage for shadow, per thread */
2593 lar->shb->isb_result[pa->thread]= MEM_callocN(sizeof(ISBData), "isb data");
2596 isb_make_buffer_transp(pa, apixbuf, lar);
2598 isb_make_buffer(pa, lar);
2604 /* end of part rendering, free stored shadow data for this thread from all lamps */
2605 void ISB_free(RenderPart *pa)
2609 /* go over all lamps, and free the irregular buffers */
2610 for(go=R.lights.first; go; go= go->next) {
2611 LampRen *lar= go->lampren;
2613 if(lar->type==LA_SPOT && lar->shb && lar->buftype==LA_SHADBUF_IRREGULAR) {
2614 ISBData *isbdata= lar->shb->isb_result[pa->thread];
2617 if(isbdata->shadfacs)
2618 MEM_freeN(isbdata->shadfacs);
2619 if(isbdata->shadfaca)
2620 MEM_freeN(isbdata->shadfaca);
2622 if(isbdata->memarena)
2623 BLI_memarena_free(isbdata->memarena);
2626 lar->shb->isb_result[pa->thread]= NULL;