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 *****
26 /** \file blender/render/intern/source/shadbuf.c
35 #include "MEM_guardedalloc.h"
37 #include "DNA_group_types.h"
38 #include "DNA_lamp_types.h"
39 #include "DNA_material_types.h"
42 #include "BLI_blenlib.h"
43 #include "BLI_jitter.h"
44 #include "BLI_memarena.h"
46 #include "BLI_utildefines.h"
48 #include "BKE_global.h"
49 #include "BKE_scene.h"
53 #include "renderpipeline.h"
54 #include "render_types.h"
55 #include "renderdatabase.h"
56 #include "rendercore.h"
61 /* XXX, could be better implemented... this is for endian issues */
74 #define RCT_SIZE_X(rct) ((rct)->xmax - (rct)->xmin)
75 #define RCT_SIZE_Y(rct) ((rct)->ymax - (rct)->ymin)
77 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
78 /* defined in pipeline.c, is hardcopy of active dynamic allocated Render */
79 /* only to be used here in this file, it's for speed */
80 extern struct Render R;
81 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
83 /* ------------------------------------------------------------------------- */
85 /* initshadowbuf() in convertBlenderScene.c */
87 /* ------------------------------------------------------------------------- */
89 static void copy_to_ztile(int *rectz, int size, int x1, int y1, int tile, char *r1)
96 if (x2>=size) x2= size-1;
97 if (y2>=size) y2= size-1;
99 if (x1>=x2 || y1>=y2) return;
102 rz= rectz + size*y1 + x1;
103 for (; y1<y2; y1++) {
104 memcpy(r1, rz, len4);
111 static int sizeoflampbuf(ShadBuf *shb)
117 num= (shb->size*shb->size)/256;
119 while (num--) count+= *(cp++);
125 /* not threadsafe... */
126 static float *give_jitter_tab(int samp)
128 /* these are all possible jitter tables, takes up some
129 * 12k, not really bad!
130 * For soft shadows, it saves memory and render time
132 static int tab[17]={1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256};
133 static float jit[1496][2];
134 static char ctab[17]= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
138 else if (samp>16) samp= 16;
140 for (a=0; a<samp-1; a++) offset+= tab[a];
144 BLI_jitter_init((float (*)[2])jit[offset], samp*samp);
151 static void make_jitter_weight_tab(Render *re, ShadBuf *shb, short filtertype)
153 float *jit, totw= 0.0f;
154 int samp= get_render_shadow_samples(&re->r, shb->samp);
155 int a, tot=samp*samp;
157 shb->weight= MEM_mallocN(sizeof(float)*tot, "weight tab lamp");
159 for (jit= shb->jit, a=0; a<tot; a++, jit+=2) {
160 if (filtertype==LA_SHADBUF_TENT)
161 shb->weight[a] = 0.71f - sqrtf(jit[0] * jit[0] + jit[1] * jit[1]);
162 else if (filtertype==LA_SHADBUF_GAUSS)
163 shb->weight[a] = RE_filter_value(R_FILTER_GAUSS, 1.8f * sqrtf(jit[0] * jit[0] + jit[1] * jit[1]));
165 shb->weight[a]= 1.0f;
167 totw+= shb->weight[a];
171 for (a=0; a<tot; a++) {
172 shb->weight[a]*= totw;
176 static int verg_deepsample(const void *poin1, const void *poin2)
178 const DeepSample *ds1= (const DeepSample*)poin1;
179 const DeepSample *ds2= (const DeepSample*)poin2;
181 if (ds1->z < ds2->z) return -1;
182 else if (ds1->z == ds2->z) return 0;
186 static int compress_deepsamples(DeepSample *dsample, int tot, float epsilon)
188 /* uses doubles to avoid overflows and other numerical issues,
189 * could be improved */
190 DeepSample *ds, *newds;
192 double slope, slopemin, slopemax, min, max, div, newmin, newmax;
193 int a, first, z, newtot= 0;
197 for (a=0, ds=dsample; a<tot; a++, ds++)
198 printf("%lf, %f ", ds->z/(double)0x7FFFFFFF, ds->v);
203 /* read from and write into same array */
208 /* as long as we are not at the end of the array */
209 for (a++, ds++; a<tot; a++, ds++) {
214 for (; a<tot; a++, ds++) {
215 //dz= ds->z - newds->z;
216 if (ds->z == newds->z) {
217 /* still in same z position, simply check
218 * visibility difference against epsilon */
219 if (!(fabsf(newds->v - ds->v) <= epsilon)) {
225 div= (double)0x7FFFFFFF / ((double)ds->z - (double)newds->z);
226 min= (double)((ds->v - epsilon) - newds->v) * div;
227 max= (double)((ds->v + epsilon) - newds->v) * div;
229 /* adapt existing slopes */
236 newmin= MAX2(slopemin, min);
237 newmax= MIN2(slopemax, max);
239 /* verify if there is still space between the slopes */
240 if (newmin > newmax) {
257 /* always previous z */
260 if (first || a==tot-1) {
261 /* if slopes were not initialized, use last visibility */
265 /* compute visibility at center between slopes at z */
266 slope = (slopemin + slopemax) * 0.5;
267 v = (double)newds->v + slope * ((double)(z - newds->z) / (double)0x7FFFFFFF);
277 if (newtot == 0 || (newds->v != (newds-1)->v))
282 for (a=0, ds=dsample; a<newtot; a++, ds++)
283 printf("%lf, %f ", ds->z/(double)0x7FFFFFFF, ds->v);
291 static float deep_alpha(Render *re, int obinr, int facenr, int strand)
293 ObjectInstanceRen *obi= &re->objectinstance[obinr];
297 StrandRen *strand= RE_findOrAddStrand(obi->obr, facenr-1);
298 ma= strand->buffer->ma;
301 VlakRen *vlr= RE_findOrAddVlak(obi->obr, (facenr-1) & RE_QUAD_MASK);
305 return ma->shad_alpha;
308 static void compress_deepshadowbuf(Render *re, ShadBuf *shb, APixstr *apixbuf, APixstrand *apixbufstrand)
310 ShadSampleBuf *shsample;
311 DeepSample *ds[RE_MAX_OSA], *sampleds[RE_MAX_OSA], *dsb, *newbuf;
313 APixstrand *aps, *apns;
316 const int totbuf= shb->totbuf;
317 const float totbuf_f= (float)shb->totbuf;
318 const float totbuf_f_inv= 1.0f/totbuf_f;
319 const int size= shb->size;
321 int a, b, c, tot, minz, found, prevtot, newtot;
322 int sampletot[RE_MAX_OSA], totsample = 0, totsamplec = 0;
324 shsample= MEM_callocN(sizeof(ShadSampleBuf), "shad sample buf");
325 BLI_addtail(&shb->buffers, shsample);
327 shsample->totbuf = MEM_callocN(sizeof(int) * size * size, "deeptotbuf");
328 shsample->deepbuf = MEM_callocN(sizeof(DeepSample *) * size * size, "deepbuf");
332 for (a=0; a<size*size; a++, ap++, aps++) {
333 /* count number of samples */
334 for (c=0; c<totbuf; c++)
338 for (apn=ap; apn; apn=apn->next)
341 for (c=0; c<totbuf; c++)
342 if (apn->mask[b] & (1<<c))
346 for (apns=aps; apns; apns=apns->next)
349 for (c=0; c<totbuf; c++)
350 if (apns->mask[b] & (1<<c))
354 for (c=0; c<totbuf; c++)
358 shsample->deepbuf[a]= NULL;
359 shsample->totbuf[a]= 0;
364 ds[0]= sampleds[0]= MEM_callocN(sizeof(DeepSample)*tot*2, "deepsample");
365 for (c=1; c<totbuf; c++)
366 ds[c]= sampleds[c]= sampleds[c-1] + sampletot[c-1]*2;
368 for (apn=ap; apn; apn=apn->next) {
369 for (b=0; b<4; b++) {
371 for (c=0; c<totbuf; c++) {
372 if (apn->mask[b] & (1<<c)) {
373 /* two entries to create step profile */
375 ds[c]->v= 1.0f; /* not used */
378 ds[c]->v= deep_alpha(re, apn->obi[b], apn->p[b], 0);
387 for (apns=aps; apns; apns=apns->next) {
388 for (b=0; b<4; b++) {
390 for (c=0; c<totbuf; c++) {
391 if (apns->mask[b] & (1<<c)) {
392 /* two entries to create step profile */
393 ds[c]->z= apns->z[b];
394 ds[c]->v= 1.0f; /* not used */
396 ds[c]->z= apns->z[b];
397 ds[c]->v= deep_alpha(re, apns->obi[b], apns->p[b], 1);
406 for (c=0; c<totbuf; c++) {
407 /* sort by increasing z */
408 qsort(sampleds[c], sampletot[c], sizeof(DeepSample)*2, verg_deepsample);
410 /* sum visibility, replacing alpha values */
414 for (b=0; b<sampletot[c]; b++) {
415 /* two entries creating step profile */
416 ds[c]->v= visibility;
419 visibility *= 1.0f-ds[c]->v;
420 ds[c]->v= visibility;
424 /* halfway trick, probably won't work well for volumes? */
426 for (b=0; b<sampletot[c]; b++) {
427 if (b+1 < sampletot[c]) {
428 ds[c]->z= (ds[c]->z>>1) + ((ds[c]+2)->z>>1);
430 ds[c]->z= (ds[c]->z>>1) + ((ds[c]+2)->z>>1);
434 ds[c]->z= (ds[c]->z>>1) + (0x7FFFFFFF>>1);
436 ds[c]->z= (ds[c]->z>>1) + (0x7FFFFFFF>>1);
441 /* init for merge loop */
446 shsample->deepbuf[a]= MEM_callocN(sizeof(DeepSample)*tot*2, "deepsample");
447 shsample->totbuf[a]= 0;
450 dsb= shsample->deepbuf[a];
455 for (c=0; c<totbuf; c++) {
456 if (sampletot[c] && (!found || ds[c]->z < minz)) {
469 for (c=0; c<totbuf; c++) {
470 if (sampletot[c] && ds[c]->z == minz) {
475 if (sampleds[c] == ds[c])
476 visibility += totbuf_f_inv;
478 visibility += (ds[c]-1)->v / totbuf_f;
483 shsample->totbuf[a]++;
486 prevtot= shsample->totbuf[a];
487 totsample += prevtot;
489 newtot= compress_deepsamples(shsample->deepbuf[a], prevtot, shb->compressthresh);
490 shsample->totbuf[a]= newtot;
491 totsamplec += newtot;
493 if (newtot < prevtot) {
494 newbuf= MEM_mallocN(sizeof(DeepSample)*newtot, "cdeepsample");
495 memcpy(newbuf, shsample->deepbuf[a], sizeof(DeepSample)*newtot);
496 MEM_freeN(shsample->deepbuf[a]);
497 shsample->deepbuf[a]= newbuf;
500 MEM_freeN(sampleds[0]);
503 //printf("%d -> %d, ratio %f\n", totsample, totsamplec, (float)totsamplec/(float)totsample);
506 /* create Z tiles (for compression): this system is 24 bits!!! */
507 static void compress_shadowbuf(ShadBuf *shb, int *rectz, int square)
509 ShadSampleBuf *shsample;
512 int *rz, *rz1, verg, verg1, size= shb->size;
513 int a, x, y, minx, miny, byt1, byt2;
514 char *rc, *rcline, *ctile, *zt;
516 shsample= MEM_callocN(sizeof(ShadSampleBuf), "shad sample buf");
517 BLI_addtail(&shb->buffers, shsample);
519 shsample->zbuf= MEM_mallocN(sizeof(uintptr_t)*(size*size)/256, "initshadbuf2");
520 shsample->cbuf= MEM_callocN((size*size)/256, "initshadbuf3");
522 ztile= (uintptr_t *)shsample->zbuf;
523 ctile= shsample->cbuf;
526 rcline= MEM_mallocN(256*4+sizeof(int), "makeshadbuf2");
528 for (y=0; y<size; y+=16) {
529 if (y< size/2) miny= y+15-size/2;
532 for (x=0; x<size; x+=16) {
534 /* is tile within spotbundle? */
536 if (x< a) minx= x+15-a;
539 dist= sqrt( (float)(minx*minx+miny*miny) );
541 if (square==0 && dist>(float)(a+12)) { /* 12, tested with a onlyshadow lamp */
542 a= 256; verg= 0; /* 0x80000000; */ /* 0x7FFFFFFF; */
546 copy_to_ztile(rectz, size, x, y, 16, rcline);
549 verg= (*rz1 & 0xFFFFFF00);
551 for (a=0;a<256;a++, rz1++) {
552 if ( (*rz1 & 0xFFFFFF00) !=verg) break;
555 if (a==256) { /* complete empty tile */
561 /* ACOMP etc. are defined to work L/B endian */
569 for (a=1;a<256;a++, rc+=4) {
570 byt1 &= (verg==rc[ACOMP]);
571 byt2 &= (verg1==rc[BCOMP]);
575 if (byt1 && byt2) { /* only store byte */
577 *ztile= (uintptr_t)MEM_mallocN(256+4, "tile1");
583 for (a=0; a<256; a++, zt++, rc+=4) *zt= rc[GCOMP];
585 else if (byt1) { /* only store short */
587 *ztile= (uintptr_t)MEM_mallocN(2*256+4, "Tile2");
593 for (a=0; a<256; a++, zt+=2, rc+=4) {
598 else { /* store triple */
600 *ztile= (uintptr_t)MEM_mallocN(3*256, "Tile3");
604 for (a=0; a<256; a++, zt+=3, rc+=4) {
619 /* sets start/end clipping. lar->shb should be initialized */
620 static void shadowbuf_autoclip(Render *re, LampRen *lar)
622 ObjectInstanceRen *obi;
627 float minz, maxz, vec[3], viewmat[4][4], obviewmat[4][4];
628 unsigned int lay = -1;
629 int i, a, maxtotvert, ok= 1;
632 minz= 1.0e30f; maxz= -1.0e30f;
633 copy_m4_m4(viewmat, lar->shb->viewmat);
635 if (lar->mode & (LA_LAYER|LA_LAYER_SHADOW)) lay= lar->lay;
638 for (obr=re->objecttable.first; obr; obr=obr->next)
639 maxtotvert = max_ii(obr->totvert, maxtotvert);
641 clipflag= MEM_callocN(sizeof(char)*maxtotvert, "autoclipflag");
643 /* set clip in vertices when face visible */
644 for (i=0, obi=re->instancetable.first; obi; i++, obi=obi->next) {
647 if (obi->flag & R_TRANSFORMED)
648 mul_m4_m4m4(obviewmat, viewmat, obi->mat);
650 copy_m4_m4(obviewmat, viewmat);
652 memset(clipflag, 0, sizeof(char)*obr->totvert);
654 /* clear clip, is being set if face is visible (clip is calculated for real later) */
655 for (a=0; a<obr->totvlak; a++) {
656 if ((a & 255)==0) vlr= obr->vlaknodes[a>>8].vlak;
659 /* note; these conditions are copied from zbuffer_shadow() */
663 if ((ma->mode2 & MA_CASTSHADOW)==0 || (ma->mode & MA_SHADBUF)==0) ok= 0;
666 if (ok && (obi->lay & lay)) {
667 clipflag[vlr->v1->index]= 1;
668 clipflag[vlr->v2->index]= 1;
669 clipflag[vlr->v3->index]= 1;
670 if (vlr->v4) clipflag[vlr->v4->index]= 1;
674 /* calculate min and max */
675 for (a=0; a< obr->totvert;a++) {
676 if ((a & 255)==0) ver= RE_findOrAddVert(obr, a);
680 copy_v3_v3(vec, ver->co);
681 mul_m4_v3(obviewmat, vec);
682 /* Z on visible side of lamp space */
684 float inpr, z= -vec[2];
686 /* since vec is rotated in lampspace, this is how to get the cosine of angle */
687 /* precision is set 20% larger */
692 if (inpr>=lar->spotsi) {
703 /* set clipping min and max */
705 float delta= (maxz - minz); /* threshold to prevent precision issues */
707 //printf("minz %f maxz %f delta %f\n", minz, maxz, delta);
708 if (lar->bufflag & LA_SHADBUF_AUTO_START)
709 lar->shb->d= minz - delta*0.02f; /* 0.02 is arbitrary... needs more thinking! */
710 if (lar->bufflag & LA_SHADBUF_AUTO_END)
711 lar->shb->clipend= maxz + delta*0.1f;
713 /* bias was calculated as percentage, we scale it to prevent animation issues */
714 delta= (lar->clipend-lar->clipsta)/(lar->shb->clipend-lar->shb->d);
715 //printf("bias delta %f\n", delta);
716 lar->shb->bias= (int) (delta*(float)lar->shb->bias);
720 static void makeflatshadowbuf(Render *re, LampRen *lar, float *jitbuf)
722 ShadBuf *shb= lar->shb;
726 rectz= MEM_mapallocN(sizeof(int)*shb->size*shb->size, "makeshadbuf");
728 for (samples=0; samples<shb->totbuf; samples++) {
729 zbuffer_shadow(re, shb->persmat, lar, rectz, shb->size, jitbuf[2*samples], jitbuf[2*samples+1]);
730 /* create Z tiles (for compression): this system is 24 bits!!! */
731 compress_shadowbuf(shb, rectz, lar->mode & LA_SQUARE);
733 if (re->test_break(re->tbh))
740 static void makedeepshadowbuf(Render *re, LampRen *lar, float *jitbuf)
742 ShadBuf *shb= lar->shb;
744 APixstrand *apixbufstrand= NULL;
745 ListBase apsmbase= {NULL, NULL};
748 apixbuf= MEM_callocN(sizeof(APixstr)*shb->size*shb->size, "APixbuf");
750 apixbufstrand= MEM_callocN(sizeof(APixstrand)*shb->size*shb->size, "APixbufstrand");
752 zbuffer_abuf_shadow(re, lar, shb->persmat, apixbuf, apixbufstrand, &apsmbase, shb->size,
753 shb->totbuf, (float(*)[2])jitbuf);
755 /* create Z tiles (for compression): this system is 24 bits!!! */
756 compress_deepshadowbuf(re, shb, apixbuf, apixbufstrand);
760 MEM_freeN(apixbufstrand);
764 void makeshadowbuf(Render *re, LampRen *lar)
766 ShadBuf *shb= lar->shb;
767 float wsize, *jitbuf, twozero[2]= {0.0f, 0.0f}, angle, temp;
769 if (lar->bufflag & (LA_SHADBUF_AUTO_START|LA_SHADBUF_AUTO_END))
770 shadowbuf_autoclip(re, lar);
772 /* just to enforce identical behavior of all irregular buffers */
773 if (lar->buftype==LA_SHADBUF_IRREGULAR)
776 /* matrices and window: in winmat the transformation is being put,
777 * transforming from observer view to lamp view, including lamp window matrix */
779 angle= saacos(lar->spotsi);
780 temp = 0.5f * shb->size * cosf(angle) / sinf(angle);
781 shb->pixsize= (shb->d)/temp;
782 wsize= shb->pixsize*(shb->size/2.0f);
784 perspective_m4(shb->winmat, -wsize, wsize, -wsize, wsize, shb->d, shb->clipend);
785 mul_m4_m4m4(shb->persmat, shb->winmat, shb->viewmat);
787 if (ELEM3(lar->buftype, LA_SHADBUF_REGULAR, LA_SHADBUF_HALFWAY, LA_SHADBUF_DEEP)) {
788 shb->totbuf= lar->buffers;
790 /* jitter, weights - not threadsafe! */
791 BLI_lock_thread(LOCK_CUSTOM1);
792 shb->jit= give_jitter_tab(get_render_shadow_samples(&re->r, shb->samp));
793 make_jitter_weight_tab(re, shb, lar->filtertype);
794 BLI_unlock_thread(LOCK_CUSTOM1);
796 if (shb->totbuf==4) jitbuf= give_jitter_tab(2);
797 else if (shb->totbuf==9) jitbuf= give_jitter_tab(3);
798 else jitbuf= twozero;
801 if (lar->buftype == LA_SHADBUF_DEEP) {
802 makedeepshadowbuf(re, lar, jitbuf);
806 makeflatshadowbuf(re, lar, jitbuf);
808 /* printf("lampbuf %d\n", sizeoflampbuf(shb)); */
812 static void *do_shadow_thread(void *re_v)
814 Render *re = (Render *)re_v;
818 BLI_lock_thread(LOCK_CUSTOM1);
819 for (lar=re->lampren.first; lar; lar=lar->next) {
820 if (lar->shb && !lar->thread_assigned) {
821 lar->thread_assigned= 1;
825 BLI_unlock_thread(LOCK_CUSTOM1);
827 /* if type is irregular, this only sets the perspective matrix and autoclips */
829 makeshadowbuf(re, lar);
830 BLI_lock_thread(LOCK_CUSTOM1);
831 lar->thread_ready= 1;
832 BLI_unlock_thread(LOCK_CUSTOM1);
834 } while (lar && !re->test_break(re->tbh));
839 static volatile int g_break= 0;
840 static int thread_break(void *UNUSED(arg))
845 void threaded_makeshadowbufs(Render *re)
850 int (*test_break)(void *);
852 /* count number of threads to use */
853 if (G.is_rendering) {
854 for (lar=re->lampren.first; lar; lar= lar->next)
858 totthread = min_ii(totthread, re->r.threads);
861 totthread = 1; /* preview render */
863 if (totthread <= 1) {
864 for (lar=re->lampren.first; lar; lar= lar->next) {
865 if (re->test_break(re->tbh)) break;
867 /* if type is irregular, this only sets the perspective matrix and autoclips */
868 makeshadowbuf(re, lar);
873 /* swap test break function */
874 test_break= re->test_break;
875 re->test_break= thread_break;
877 for (lar=re->lampren.first; lar; lar= lar->next) {
878 lar->thread_assigned= 0;
879 lar->thread_ready= 0;
882 BLI_init_threads(&threads, do_shadow_thread, totthread);
884 for (a=0; a<totthread; a++)
885 BLI_insert_thread(&threads, re);
887 /* keep rendering as long as there are shadow buffers not ready */
889 if ((g_break=test_break(re->tbh)))
894 BLI_lock_thread(LOCK_CUSTOM1);
895 for (lar=re->lampren.first; lar; lar= lar->next)
896 if (lar->shb && !lar->thread_ready)
898 BLI_unlock_thread(LOCK_CUSTOM1);
901 BLI_end_threads(&threads);
903 /* unset threadsafety */
904 re->test_break= test_break;
909 void freeshadowbuf(LampRen *lar)
912 ShadBuf *shb= lar->shb;
913 ShadSampleBuf *shsample;
916 for (shsample= shb->buffers.first; shsample; shsample= shsample->next) {
917 if (shsample->deepbuf) {
918 v= shb->size*shb->size;
920 if (shsample->deepbuf[b])
921 MEM_freeN(shsample->deepbuf[b]);
923 MEM_freeN(shsample->deepbuf);
924 MEM_freeN(shsample->totbuf);
927 intptr_t *ztile= shsample->zbuf;
928 char *ctile= shsample->cbuf;
930 v= (shb->size*shb->size)/256;
931 for (b=0; b<v; b++, ztile++, ctile++)
932 if (*ctile) MEM_freeN((void *) *ztile);
934 MEM_freeN(shsample->zbuf);
935 MEM_freeN(shsample->cbuf);
938 BLI_freelistN(&shb->buffers);
940 if (shb->weight) MEM_freeN(shb->weight);
948 static int firstreadshadbuf(ShadBuf *shb, ShadSampleBuf *shsample, int **rz, int xs, int ys, int nr)
950 /* return a 1 if fully compressed shadbuf-tile && z==const */
954 if (shsample->deepbuf)
957 /* always test borders of shadowbuffer */
958 if (xs<0) xs= 0; else if (xs>=shb->size) xs= shb->size-1;
959 if (ys<0) ys= 0; else if (ys>=shb->size) ys= shb->size-1;
962 ofs= (ys>>4)*(shb->size>>4) + (xs>>4);
963 ct= shsample->cbuf+ofs;
966 *rz= *( (int **)(shsample->zbuf+ofs) );
969 else if (*rz!= *( (int **)(shsample->zbuf+ofs) )) return 0;
977 static float readdeepvisibility(DeepSample *dsample, int tot, int z, int bias, float *biast)
979 DeepSample *ds, *prevds;
983 /* tricky stuff here; we use ints which can overflow easily with bias values */
986 for (a=0; a<tot && (z-bias > ds->z); a++, ds++) {}
991 return (ds-1)->v; /* completely behind all samples */
994 /* check if this read needs bias blending */
997 *biast= (float)(z - ds->z)/(float)bias;
1003 return 1.0f; /* completely in front of all samples */
1005 /* converting to float early here because ds->z - prevds->z can overflow */
1007 t= ((float)(z-bias) - (float)prevds->z)/((float)ds->z - (float)prevds->z);
1008 return t*ds->v + (1.0f-t)*prevds->v;
1011 static float readdeepshadowbuf(ShadBuf *shb, ShadSampleBuf *shsample, int bias, int xs, int ys, int zs)
1013 float v, biasv, biast;
1016 if (zs < - 0x7FFFFE00 + bias)
1017 return 1.0; /* extreme close to clipstart */
1020 ofs= ys*shb->size + xs;
1021 tot= shsample->totbuf[ofs];
1025 v= readdeepvisibility(shsample->deepbuf[ofs], tot, zs, bias, &biast);
1027 if (biast != 0.0f) {
1028 /* in soft bias area */
1029 biasv = readdeepvisibility(shsample->deepbuf[ofs], tot, zs, 0, NULL);
1032 return (1.0f-biast)*v + biast*biasv;
1038 /* return 1.0 : fully in light */
1039 static float readshadowbuf(ShadBuf *shb, ShadSampleBuf *shsample, int bias, int xs, int ys, int zs)
1047 /* if (xs<0 || ys<0) return 1.0; */
1048 /* if (xs>=shb->size || ys>=shb->size) return 1.0; */
1050 /* always test borders of shadowbuffer */
1051 if (xs<0) xs= 0; else if (xs>=shb->size) xs= shb->size-1;
1052 if (ys<0) ys= 0; else if (ys>=shb->size) ys= shb->size-1;
1054 if (shsample->deepbuf)
1055 return readdeepshadowbuf(shb, shsample, bias, xs, ys, zs);
1058 ofs= (ys>>4)*(shb->size>>4) + (xs>>4);
1059 ct= shsample->cbuf+ofs;
1060 rz= *( (int **)(shsample->zbuf+ofs) );
1063 ct= ((char *)rz)+3*16*(ys & 15)+3*(xs & 15);
1071 ct+= 4+2*16*(ys & 15)+2*(xs & 15);
1080 ct+= 4+16*(ys & 15)+(xs & 15);
1088 /* got warning on this for 64 bits.... */
1089 /* but it's working code! in this case rz is not a pointer but zvalue (ton) */
1090 zsamp= GET_INT_FROM_POINTER(rz);
1093 /* tricky stuff here; we use ints which can overflow easily with bias values */
1095 if (zsamp > zs) return 1.0; /* absolute no shadow */
1096 else if (zs < - 0x7FFFFE00 + bias) return 1.0; /* extreme close to clipstart */
1097 else if (zsamp < zs-bias) return 0.0; /* absolute in shadow */
1098 else { /* soft area */
1100 temp= ( (float)(zs- zsamp) )/(float)bias;
1101 return 1.0f - temp*temp;
1106 static void shadowbuf_project_co(float *x, float *y, float *z, ShadBuf *shb, const float co[3])
1108 float hco[4], size= 0.5f*(float)shb->size;
1110 copy_v3_v3(hco, co);
1113 mul_m4_v4(shb->persmat, hco);
1115 *x= size*(1.0f+hco[0]/hco[3]);
1116 *y= size*(1.0f+hco[1]/hco[3]);
1117 if (z) *z= (hco[2]/hco[3]);
1120 /* the externally called shadow testing (reading) function */
1121 /* return 1.0: no shadow at all */
1122 float testshadowbuf(Render *re, ShadBuf *shb, const float co[3], const float dxco[3], const float dyco[3], float inp, float mat_bias)
1124 ShadSampleBuf *shsample;
1125 float fac, dco[3], dx[3], dy[3], shadfac=0.0f;
1126 float xs1, ys1, zs1, *jit, *weight, xres, yres, biasf;
1127 int xs, ys, zs, bias, *rz;
1130 /* crash preventer */
1131 if (shb->buffers.first==NULL)
1134 /* when facing away, assume fully in shadow */
1138 /* project coordinate to pixel space */
1139 shadowbuf_project_co(&xs1, &ys1, &zs1, shb, co);
1141 /* clip z coordinate, z is projected so that (-1.0, 1.0) matches
1142 * (clipstart, clipend), so we can do this simple test */
1145 else if (zs1<= -1.0f)
1148 zs= ((float)0x7FFFFFFF)*zs1;
1150 /* take num*num samples, increase area with fac */
1151 num= get_render_shadow_samples(&re->r, shb->samp);
1155 /* compute z bias */
1156 if (mat_bias!=0.0f) biasf= shb->bias*mat_bias;
1157 else biasf= shb->bias;
1158 /* with inp==1.0, bias is half the size. correction value was 1.1, giving errors
1159 * on cube edges, with one side being almost frontal lighted (ton) */
1160 bias= (1.5f-inp*inp)*biasf;
1162 /* in case of no filtering we can do things simpler */
1164 for (shsample= shb->buffers.first; shsample; shsample= shsample->next)
1165 shadfac += readshadowbuf(shb, shsample, bias, (int)xs1, (int)ys1, zs);
1167 return shadfac/(float)shb->totbuf;
1170 /* calculate filter size */
1171 add_v3_v3v3(dco, co, dxco);
1172 shadowbuf_project_co(&dx[0], &dx[1], NULL, shb, dco);
1176 add_v3_v3v3(dco, co, dyco);
1177 shadowbuf_project_co(&dy[0], &dy[1], NULL, shb, dco);
1181 xres = fac * (fabsf(dx[0]) + fabsf(dy[0]));
1182 yres = fac * (fabsf(dx[1]) + fabsf(dy[1]));
1183 if (xres<1.0f) xres= 1.0f;
1184 if (yres<1.0f) yres= 1.0f;
1186 /* make xs1/xs1 corner of sample area */
1190 /* in case we have a constant value in a tile, we can do quicker lookup */
1191 if (xres<16.0f && yres<16.0f) {
1192 shsample= shb->buffers.first;
1193 if (firstreadshadbuf(shb, shsample, &rz, (int)xs1, (int)ys1, 0)) {
1194 if (firstreadshadbuf(shb, shsample, &rz, (int)(xs1+xres), (int)ys1, 1)) {
1195 if (firstreadshadbuf(shb, shsample, &rz, (int)xs1, (int)(ys1+yres), 1)) {
1196 if (firstreadshadbuf(shb, shsample, &rz, (int)(xs1+xres), (int)(ys1+yres), 1)) {
1197 return readshadowbuf(shb, shsample, bias, (int)xs1, (int)ys1, zs);
1204 /* full jittered shadow buffer lookup */
1205 for (shsample= shb->buffers.first; shsample; shsample= shsample->next) {
1207 weight= shb->weight;
1209 for (a=num; a>0; a--, jit+=2, weight++) {
1210 /* instead of jit i tried random: ugly! */
1211 /* note: the plus 0.5 gives best sampling results, jit goes from -0.5 to 0.5 */
1212 /* xs1 and ys1 are already corrected to be corner of sample area */
1213 xs= xs1 + xres*(jit[0] + 0.5f);
1214 ys= ys1 + yres*(jit[1] + 0.5f);
1216 shadfac+= *weight * readshadowbuf(shb, shsample, bias, xs, ys, zs);
1220 /* Renormalizes for the sample number: */
1221 return shadfac/(float)shb->totbuf;
1224 /* different function... sampling behind clipend can be LIGHT, bias is negative! */
1226 static float readshadowbuf_halo(ShadBuf *shb, ShadSampleBuf *shsample, int xs, int ys, int zs)
1230 int bias, zbias, zsamp;
1233 /* negative! The other side is more important */
1237 if (xs<0 || ys<0) return 0.0;
1238 if (xs>=shb->size || ys>=shb->size) return 0.0;
1241 ofs= (ys>>4)*(shb->size>>4) + (xs>>4);
1242 ct= shsample->cbuf+ofs;
1243 rz= *( (int **)(shsample->zbuf+ofs) );
1246 ct= ((char *)rz)+3*16*(ys & 15)+3*(xs & 15);
1255 ct+= 4+2*16*(ys & 15)+2*(xs & 15);
1264 ct+= 4+16*(ys & 15)+(xs & 15);
1272 /* same as before */
1273 /* still working code! (ton) */
1274 zsamp= GET_INT_FROM_POINTER(rz);
1277 /* NO schadow when sampled at 'eternal' distance */
1279 if (zsamp >= 0x7FFFFE00) return 1.0;
1281 if (zsamp > zs) return 1.0; /* absolute no shadww */
1283 /* bias is negative, so the (zs-bias) can be beyond 0x7fffffff */
1284 zbias= 0x7fffffff - zs;
1285 if (zbias > -bias) {
1286 if ( zsamp < zs-bias) return 0.0; /* absolute in shadow */
1288 else return 0.0; /* absolute shadow */
1293 temp= ( (float)(zs- zsamp) )/(float)bias;
1294 return 1.0f - temp*temp;
1298 float shadow_halo(LampRen *lar, const float p1[3], const float p2[3])
1300 /* p1 p2 already are rotated in spot-space */
1301 ShadBuf *shb= lar->shb;
1302 ShadSampleBuf *shsample;
1304 float lambda, lambda_o, lambda_x, lambda_y, ldx, ldy;
1305 float zf, xf1, yf1, zf1, xf2, yf2, zf2;
1306 float count, lightcount;
1307 int x, y, z, xs1, ys1;
1310 siz= 0.5f*(float)shb->size;
1314 co[2]= p1[2]/lar->sh_zfac;
1316 mul_m4_v4(shb->winmat, co); /* rational hom co */
1317 xf1= siz*(1.0f+co[0]/co[3]);
1318 yf1= siz*(1.0f+co[1]/co[3]);
1324 co[2]= p2[2]/lar->sh_zfac;
1326 mul_m4_v4(shb->winmat, co); /* rational hom co */
1327 xf2= siz*(1.0f+co[0]/co[3]);
1328 yf2= siz*(1.0f+co[1]/co[3]);
1331 /* the 2dda (a pixel line formula) */
1337 if (xf2-xf1 > 0.0f) {
1338 lambda_x= (xf1-xs1-1.0f)/(xf1-xf2);
1339 ldx= -shb->shadhalostep/(xf1-xf2);
1340 dx= shb->shadhalostep;
1343 lambda_x= (xf1-xs1)/(xf1-xf2);
1344 ldx= shb->shadhalostep/(xf1-xf2);
1345 dx= -shb->shadhalostep;
1354 if (yf2-yf1 > 0.0f) {
1355 lambda_y= (yf1-ys1-1.0f)/(yf1-yf2);
1356 ldy= -shb->shadhalostep/(yf1-yf2);
1357 dy= shb->shadhalostep;
1360 lambda_y= (yf1-ys1)/(yf1-yf2);
1361 ldy= shb->shadhalostep/(yf1-yf2);
1362 dy= -shb->shadhalostep;
1372 lambda= count= lightcount= 0.0;
1374 /* printf("start %x %x \n", (int)(0x7FFFFFFF*zf1), (int)(0x7FFFFFFF*zf2)); */
1379 if (lambda_x==lambda_y) {
1386 if (lambda_x<lambda_y) {
1396 lambda = min_ff(lambda_x, lambda_y);
1398 /* not making any progress? */
1399 if (lambda==lambda_o) break;
1401 /* clip to end of volume */
1402 lambda = min_ff(lambda, 1.0f);
1404 zf= zf1 + lambda*(zf2-zf1);
1405 count+= (float)shb->totbuf;
1407 if (zf<= -1.0f) lightcount += 1.0f; /* close to the spot */
1410 /* make sure, behind the clipend we extend halolines. */
1411 if (zf>=1.0f) z= 0x7FFFF000;
1412 else z= (int)(0x7FFFF000*zf);
1414 for (shsample= shb->buffers.first; shsample; shsample= shsample->next)
1415 lightcount+= readshadowbuf_halo(shb, shsample, x, y, z);
1419 while (lambda < 1.0f);
1421 if (count!=0.0f) return (lightcount/count);
1427 /* ********************* Irregular Shadow Buffer (ISB) ************* */
1428 /* ********** storage of all view samples in a raster of lists ***** */
1430 /* based on several articles describing this method, like:
1431 * The Irregular Z-Buffer and its Application to Shadow Mapping
1432 * Gregory S. Johnson - William R. Mark - Christopher A. Burns
1434 * Alias-Free Shadow Maps
1435 * Timo Aila and Samuli Laine
1438 /* bsp structure (actually kd tree) */
1440 #define BSPMAX_SAMPLE 128
1441 #define BSPMAX_DEPTH 32
1443 /* aligned with struct rctf */
1444 typedef struct Boxf {
1450 typedef struct ISBBranch {
1451 struct ISBBranch *left, *right;
1454 short totsamp, index, full, unused;
1455 ISBSample **samples;
1458 typedef struct BSPFace {
1460 const float *v1, *v2, *v3, *v4;
1461 int obi; /* object for face lookup */
1462 int facenr; /* index to retrieve VlakRen */
1463 int type; /* only for strand now */
1464 short shad_alpha, is_full;
1466 /* strand caching data, optimize for point_behind_strand() */
1467 float radline, radline_end, len;
1468 float vec1[3], vec2[3], rc[3];
1471 /* boxes are in lamp projection */
1472 static void init_box(Boxf *box)
1474 box->xmin = 1000000.0f;
1476 box->ymin = 1000000.0f;
1478 box->zmin= 0x7FFFFFFF;
1479 box->zmax= - 0x7FFFFFFF;
1482 /* use v1 to calculate boundbox */
1483 static void bound_boxf(Boxf *box, const float v1[3])
1485 if (v1[0] < box->xmin) box->xmin = v1[0];
1486 if (v1[0] > box->xmax) box->xmax = v1[0];
1487 if (v1[1] < box->ymin) box->ymin = v1[1];
1488 if (v1[1] > box->ymax) box->ymax = v1[1];
1489 if (v1[2] < box->zmin) box->zmin= v1[2];
1490 if (v1[2] > box->zmax) box->zmax= v1[2];
1493 /* use v1 to calculate boundbox */
1494 static void bound_rectf(rctf *box, const float v1[2])
1496 if (v1[0] < box->xmin) box->xmin = v1[0];
1497 if (v1[0] > box->xmax) box->xmax = v1[0];
1498 if (v1[1] < box->ymin) box->ymin = v1[1];
1499 if (v1[1] > box->ymax) box->ymax = v1[1];
1503 /* halfway splitting, for initializing a more regular tree */
1504 static void isb_bsp_split_init(ISBBranch *root, MemArena *mem, int level)
1507 /* if level > 0 we create new branches and go deeper */
1509 ISBBranch *left, *right;
1513 root->divider[0]= 0.5f*(root->box.xmin+root->box.xmax);
1514 root->divider[1]= 0.5f*(root->box.ymin+root->box.ymax);
1516 /* find best splitpoint */
1517 if (RCT_SIZE_X(&root->box) > RCT_SIZE_Y(&root->box))
1518 i = root->index = 0;
1520 i = root->index = 1;
1522 left= root->left= BLI_memarena_alloc(mem, sizeof(ISBBranch));
1523 right= root->right= BLI_memarena_alloc(mem, sizeof(ISBBranch));
1526 left->box= root->box;
1527 right->box= root->box;
1529 left->box.xmax = root->divider[0];
1530 right->box.xmin = root->divider[0];
1533 left->box.ymax = root->divider[1];
1534 right->box.ymin = root->divider[1];
1536 isb_bsp_split_init(left, mem, level-1);
1537 isb_bsp_split_init(right, mem, level-1);
1540 /* we add sample array */
1541 root->samples= BLI_memarena_alloc(mem, BSPMAX_SAMPLE*sizeof(void *));
1545 /* note; if all samples on same location we just spread them over 2 new branches */
1546 static void isb_bsp_split(ISBBranch *root, MemArena *mem)
1548 ISBBranch *left, *right;
1549 ISBSample *samples[BSPMAX_SAMPLE];
1553 root->divider[0]= root->divider[1]= 0.0f;
1554 for (a=BSPMAX_SAMPLE-1; a>=0; a--) {
1555 root->divider[0]+= root->samples[a]->zco[0];
1556 root->divider[1]+= root->samples[a]->zco[1];
1558 root->divider[0]/= BSPMAX_SAMPLE;
1559 root->divider[1]/= BSPMAX_SAMPLE;
1561 /* find best splitpoint */
1562 if (RCT_SIZE_X(&root->box) > RCT_SIZE_Y(&root->box))
1563 i = root->index = 0;
1565 i = root->index = 1;
1568 left= root->left= BLI_memarena_alloc(mem, sizeof(ISBBranch));
1569 right= root->right= BLI_memarena_alloc(mem, sizeof(ISBBranch));
1571 /* new sample array */
1572 left->samples = BLI_memarena_alloc(mem, BSPMAX_SAMPLE*sizeof(void *));
1573 right->samples = samples; /* tmp */
1576 for (a=BSPMAX_SAMPLE-1; a>=0; a--) {
1578 /* this prevents adding samples all to 1 branch when divider is equal to samples */
1579 if (root->samples[a]->zco[i] == root->divider[i])
1581 else if (root->samples[a]->zco[i] < root->divider[i])
1585 left->samples[left->totsamp]= root->samples[a];
1589 right->samples[right->totsamp]= root->samples[a];
1594 /* copy samples from tmp */
1595 memcpy(root->samples, samples, right->totsamp*(sizeof(void *)));
1596 right->samples= root->samples;
1597 root->samples= NULL;
1600 left->box= root->box;
1601 right->box= root->box;
1603 left->box.xmax = root->divider[0];
1604 right->box.xmin = root->divider[0];
1607 left->box.ymax = root->divider[1];
1608 right->box.ymin = root->divider[1];
1612 /* inserts sample in main tree, also splits on threshold */
1613 /* returns 1 if error */
1614 static int isb_bsp_insert(ISBBranch *root, MemArena *memarena, ISBSample *sample)
1616 ISBBranch *bspn= root;
1617 float *zco= sample->zco;
1620 /* debug counter, also used to check if something was filled in ever */
1623 /* going over branches until last one found */
1624 while (bspn->left) {
1625 if (zco[bspn->index] <= bspn->divider[bspn->index])
1631 /* bspn now is the last branch */
1633 if (bspn->totsamp==BSPMAX_SAMPLE) {
1634 printf("error in bsp branch\n"); /* only for debug, cannot happen */
1639 bspn->samples[bspn->totsamp]= sample;
1642 /* split if allowed and needed */
1643 if (bspn->totsamp==BSPMAX_SAMPLE) {
1644 if (i==BSPMAX_DEPTH) {
1645 bspn->totsamp--; /* stop filling in... will give errors */
1648 isb_bsp_split(bspn, memarena);
1653 /* initialize vars in face, for optimal point-in-face test */
1654 static void bspface_init_strand(BSPFace *face)
1657 face->radline= 0.5f* len_v2v2(face->v1, face->v2);
1659 mid_v3_v3v3(face->vec1, face->v1, face->v2);
1661 mid_v3_v3v3(face->vec2, face->v3, face->v4);
1663 copy_v3_v3(face->vec2, face->v3);
1665 face->rc[0]= face->vec2[0]-face->vec1[0];
1666 face->rc[1]= face->vec2[1]-face->vec1[1];
1667 face->rc[2]= face->vec2[2]-face->vec1[2];
1669 face->len= face->rc[0]*face->rc[0]+ face->rc[1]*face->rc[1];
1671 if (face->len != 0.0f) {
1672 face->radline_end = face->radline / sqrtf(face->len);
1673 face->len = 1.0f / face->len;
1677 /* brought back to a simple 2d case */
1678 static int point_behind_strand(const float p[3], BSPFace *face)
1680 /* v1 - v2 is radius, v1 - v3 length */
1681 float dist, rc[2], pt[2];
1683 /* using code from dist_to_line_segment_v2(), distance vec to line-piece */
1685 if (face->len==0.0f) {
1686 rc[0]= p[0]-face->vec1[0];
1687 rc[1]= p[1]-face->vec1[1];
1688 dist= (float)(sqrt(rc[0]*rc[0]+ rc[1]*rc[1]));
1690 if (dist < face->radline)
1694 float lambda= ( face->rc[0]*(p[0]-face->vec1[0]) + face->rc[1]*(p[1]-face->vec1[1]) )*face->len;
1696 if (lambda > -face->radline_end && lambda < 1.0f+face->radline_end) {
1697 /* hesse for dist: */
1698 //dist= (float)(fabs( (p[0]-vec2[0])*rc[1] + (p[1]-vec2[1])*rc[0])/len);
1700 pt[0]= lambda*face->rc[0]+face->vec1[0];
1701 pt[1]= lambda*face->rc[1]+face->vec1[1];
1705 dist= sqrtf(rc[0]*rc[0]+ rc[1]*rc[1]);
1707 if (dist < face->radline) {
1708 float zval= face->vec1[2] + lambda*face->rc[2];
1718 /* return 1 if inside. code derived from src/parametrizer.c */
1719 static int point_behind_tria2d(const float p[3], const float v1[3], const float v2[3], const float v3[3])
1721 float a[2], c[2], h[2], div;
1724 a[0] = v2[0] - v1[0];
1725 a[1] = v2[1] - v1[1];
1726 c[0] = v3[0] - v1[0];
1727 c[1] = v3[1] - v1[1];
1729 div = a[0]*c[1] - a[1]*c[0];
1733 h[0] = p[0] - v1[0];
1734 h[1] = p[1] - v1[1];
1738 u = (h[0]*c[1] - h[1]*c[0])*div;
1740 v = (a[0]*h[1] - a[1]*h[0])*div;
1742 if ( u + v <= 1.0f) {
1743 /* inside, now check if point p is behind */
1744 float z= (1.0f-u-v)*v1[2] + u*v2[2] + v*v3[2];
1755 /* tested these calls, but it gives inaccuracy, 'side' cannot be found reliably using v3 */
1757 /* check if line v1-v2 has all rect points on other side of point v3 */
1758 static int rect_outside_line(rctf *rect, const float v1[3], const float v2[3], const float v3[3])
1763 /* line formula for v1-v2 */
1766 c= -a*v1[0] - b*v1[1];
1767 side= a*v3[0] + b*v3[1] + c < 0.0f;
1769 /* the four quad points */
1770 if ( side==(rect->xmin*a + rect->ymin*b + c >= 0.0f) )
1771 if ( side==(rect->xmax*a + rect->ymin*b + c >= 0.0f) )
1772 if ( side==(rect->xmax*a + rect->ymax*b + c >= 0.0f) )
1773 if ( side==(rect->xmin*a + rect->ymax*b + c >= 0.0f) )
1778 /* check if one of the triangle edges separates all rect points on 1 side */
1779 static int rect_isect_tria(rctf *rect, const float v1[3], const float v2[3], const float v3[3])
1781 if (rect_outside_line(rect, v1, v2, v3))
1783 if (rect_outside_line(rect, v2, v3, v1))
1785 if (rect_outside_line(rect, v3, v1, v2))
1791 /* if face overlaps a branch, it executes func. recursive */
1792 static void isb_bsp_face_inside(ISBBranch *bspn, BSPFace *face)
1795 /* are we descending? */
1797 /* hrmf, the box struct cannot be addressed with index */
1798 if (bspn->index==0) {
1799 if (face->box.xmin <= bspn->divider[0])
1800 isb_bsp_face_inside(bspn->left, face);
1801 if (face->box.xmax > bspn->divider[0])
1802 isb_bsp_face_inside(bspn->right, face);
1805 if (face->box.ymin <= bspn->divider[1])
1806 isb_bsp_face_inside(bspn->left, face);
1807 if (face->box.ymax > bspn->divider[1])
1808 isb_bsp_face_inside(bspn->right, face);
1812 /* else: end branch reached */
1815 if (bspn->totsamp==0) return;
1817 /* check for nodes entirely in shadow, can be skipped */
1818 if (bspn->totsamp==bspn->full)
1821 /* if bsp node is entirely in front of face, give up */
1822 if (bspn->box.zmax < face->box.zmin)
1825 /* if face boundbox is outside of branch rect, give up */
1826 if (0==BLI_rctf_isect((rctf *)&face->box, (rctf *)&bspn->box, NULL))
1829 /* test all points inside branch */
1830 for (a=bspn->totsamp-1; a>=0; a--) {
1831 ISBSample *samp= bspn->samples[a];
1833 if ((samp->facenr!=face->facenr || samp->obi!=face->obi) && samp->shadfac) {
1834 if (face->box.zmin < samp->zco[2]) {
1835 if (BLI_rctf_isect_pt_v((rctf *)&face->box, samp->zco)) {
1839 if (point_behind_strand(samp->zco, face))
1842 else if ( point_behind_tria2d(samp->zco, face->v1, face->v2, face->v3))
1844 else if (face->v4 && point_behind_tria2d(samp->zco, face->v1, face->v3, face->v4))
1848 *(samp->shadfac) += face->shad_alpha;
1849 /* optimize; is_full means shad_alpha==4096 */
1850 if (*(samp->shadfac) >= 4096 || face->is_full) {
1852 samp->shadfac= NULL;
1862 /* based on available samples, recalculate the bounding box for bsp nodes, recursive */
1863 static void isb_bsp_recalc_box(ISBBranch *root)
1866 isb_bsp_recalc_box(root->left);
1867 isb_bsp_recalc_box(root->right);
1869 else if (root->totsamp) {
1872 init_box(&root->box);
1873 for (a=root->totsamp-1; a>=0; a--)
1874 bound_boxf(&root->box, root->samples[a]->zco);
1878 /* callback function for zbuf clip */
1879 static void isb_bsp_test_strand(ZSpan *zspan, int obi, int zvlnr,
1880 const float *v1, const float *v2, const float *v3, const float *v4)
1889 face.facenr= zvlnr & ~RE_QUAD_OFFS;
1890 face.type= R_STRAND;
1892 face.shad_alpha= (short)ceil(4096.0f*zspan->shad_alpha/(float)R.osa);
1894 face.shad_alpha= (short)ceil(4096.0f*zspan->shad_alpha);
1896 face.is_full= (zspan->shad_alpha==1.0f);
1898 /* setup boundbox */
1899 init_box(&face.box);
1900 bound_boxf(&face.box, v1);
1901 bound_boxf(&face.box, v2);
1902 bound_boxf(&face.box, v3);
1904 bound_boxf(&face.box, v4);
1906 /* optimize values */
1907 bspface_init_strand(&face);
1909 isb_bsp_face_inside((ISBBranch *)zspan->rectz, &face);
1913 /* callback function for zbuf clip */
1914 static void isb_bsp_test_face(ZSpan *zspan, int obi, int zvlnr,
1915 const float *v1, const float *v2, const float *v3, const float *v4)
1924 face.facenr= zvlnr & ~RE_QUAD_OFFS;
1927 face.shad_alpha= (short)ceil(4096.0f*zspan->shad_alpha/(float)R.osa);
1929 face.shad_alpha= (short)ceil(4096.0f*zspan->shad_alpha);
1931 face.is_full= (zspan->shad_alpha==1.0f);
1933 /* setup boundbox */
1934 init_box(&face.box);
1935 bound_boxf(&face.box, v1);
1936 bound_boxf(&face.box, v2);
1937 bound_boxf(&face.box, v3);
1939 bound_boxf(&face.box, v4);
1941 isb_bsp_face_inside((ISBBranch *)zspan->rectz, &face);
1944 static int testclip_minmax(const float ho[4], const float minmax[4])
1949 if ( ho[0] > minmax[1]*wco) flag = 1;
1950 else if ( ho[0]< minmax[0]*wco) flag = 2;
1952 if ( ho[1] > minmax[3]*wco) flag |= 4;
1953 else if ( ho[1]< minmax[2]*wco) flag |= 8;
1958 /* main loop going over all faces and check in bsp overlaps, fill in shadfac values */
1959 static void isb_bsp_fillfaces(Render *re, LampRen *lar, ISBBranch *root)
1961 ObjectInstanceRen *obi;
1963 ShadBuf *shb= lar->shb;
1964 ZSpan zspan, zspanstrand;
1967 float minmaxf[4], winmat[4][4];
1968 int size= shb->size;
1969 int i, a, ok=1, lay= -1;
1971 /* further optimize, also sets minz maxz */
1972 isb_bsp_recalc_box(root);
1974 /* extra clipping for minmax */
1975 minmaxf[0]= (2.0f*root->box.xmin - size-2.0f)/size;
1976 minmaxf[1]= (2.0f*root->box.xmax - size+2.0f)/size;
1977 minmaxf[2]= (2.0f*root->box.ymin - size-2.0f)/size;
1978 minmaxf[3]= (2.0f*root->box.ymax - size+2.0f)/size;
1980 if (lar->mode & (LA_LAYER|LA_LAYER_SHADOW)) lay= lar->lay;
1982 /* (ab)use zspan, since we use zbuffer clipping code */
1983 zbuf_alloc_span(&zspan, size, size, re->clipcrop);
1985 zspan.zmulx= ((float)size)/2.0f;
1986 zspan.zmuly= ((float)size)/2.0f;
1990 /* pass on bsp root to zspan */
1991 zspan.rectz= (int *)root;
1993 /* filling methods */
1995 // zspan.zbuflinefunc= zbufline_onlyZ;
1996 zspan.zbuffunc= isb_bsp_test_face;
1997 zspanstrand.zbuffunc= isb_bsp_test_strand;
1999 for (i=0, obi=re->instancetable.first; obi; i++, obi=obi->next) {
2002 if (obi->flag & R_TRANSFORMED)
2003 mul_m4_m4m4(winmat, shb->persmat, obi->mat);
2005 copy_m4_m4(winmat, shb->persmat);
2007 for (a=0; a<obr->totvlak; a++) {
2009 if ((a & 255)==0) vlr= obr->vlaknodes[a>>8].vlak;
2012 /* note, these conditions are copied in shadowbuf_autoclip() */
2013 if (vlr->mat!= ma) {
2016 if ((ma->mode2 & MA_CASTSHADOW)==0 || (ma->mode & MA_SHADBUF)==0) ok= 0;
2017 if (ma->material_type == MA_TYPE_WIRE) ok= 0;
2018 zspanstrand.shad_alpha= zspan.shad_alpha= ma->shad_alpha;
2021 if (ok && (obi->lay & lay)) {
2023 int c1, c2, c3, c4=0;
2024 int d1, d2, d3, d4=0;
2027 /* create hocos per face, it is while render */
2028 projectvert(vlr->v1->co, winmat, hoco[0]); d1= testclip_minmax(hoco[0], minmaxf);
2029 projectvert(vlr->v2->co, winmat, hoco[1]); d2= testclip_minmax(hoco[1], minmaxf);
2030 projectvert(vlr->v3->co, winmat, hoco[2]); d3= testclip_minmax(hoco[2], minmaxf);
2032 projectvert(vlr->v4->co, winmat, hoco[3]); d4= testclip_minmax(hoco[3], minmaxf);
2035 /* minmax clipping */
2036 if (vlr->v4) partclip= d1 & d2 & d3 & d4;
2037 else partclip= d1 & d2 & d3;
2041 /* window clipping */
2042 c1= testclip(hoco[0]);
2043 c2= testclip(hoco[1]);
2044 c3= testclip(hoco[2]);
2046 c4= testclip(hoco[3]);
2048 /* ***** NO WIRE YET */
2049 if (ma->material_type == MA_TYPE_WIRE) {
2051 zbufclipwire(&zspan, i, a+1, vlr->ec, hoco[0], hoco[1], hoco[2], hoco[3], c1, c2, c3, c4);
2053 zbufclipwire(&zspan, i, a+1, vlr->ec, hoco[0], hoco[1], hoco[2], NULL, c1, c2, c3, 0);
2056 if (vlr->flag & R_STRAND)
2057 zbufclip4(&zspanstrand, i, a+1, hoco[0], hoco[1], hoco[2], hoco[3], c1, c2, c3, c4);
2059 zbufclip4(&zspan, i, a+1, hoco[0], hoco[1], hoco[2], hoco[3], c1, c2, c3, c4);
2062 zbufclip(&zspan, i, a+1, hoco[0], hoco[1], hoco[2], c1, c2, c3);
2069 zbuf_free_span(&zspan);
2072 /* returns 1 when the viewpixel is visible in lampbuffer */
2073 static int viewpixel_to_lampbuf(ShadBuf *shb, ObjectInstanceRen *obi, VlakRen *vlr, float x, float y, float co_r[3])
2075 float hoco[4], v1[3], nor[3];
2076 float dface, fac, siz;
2078 RE_vlakren_get_normal(&R, obi, vlr, nor);
2079 copy_v3_v3(v1, vlr->v1->co);
2080 if (obi->flag & R_TRANSFORMED)
2081 mul_m4_v3(obi->mat, v1);
2083 /* from shadepixel() */
2084 dface = dot_v3v3(v1, nor);
2087 /* ortho viewplane cannot intersect using view vector originating in (0, 0, 0) */
2088 if (R.r.mode & R_ORTHO) {
2089 /* x and y 3d coordinate can be derived from pixel coord and winmat */
2090 float fx= 2.0f/(R.winx*R.winmat[0][0]);
2091 float fy= 2.0f/(R.winy*R.winmat[1][1]);
2093 hoco[0]= (x - 0.5f*R.winx)*fx - R.winmat[3][0]/R.winmat[0][0];
2094 hoco[1]= (y - 0.5f*R.winy)*fy - R.winmat[3][1]/R.winmat[1][1];
2096 /* using a*x + b*y + c*z = d equation, (a b c) is normal */
2098 hoco[2]= (dface - nor[0]*hoco[0] - nor[1]*hoco[1])/nor[2];
2105 calc_view_vector(view, x, y);
2107 div = dot_v3v3(nor, view);
2113 hoco[0]= fac*view[0];
2114 hoco[1]= fac*view[1];
2115 hoco[2]= fac*view[2];
2118 /* move 3d vector to lampbuf */
2119 mul_m4_v4(shb->persmat, hoco); /* rational hom co */
2121 /* clip We can test for -1.0/1.0 because of the properties of the
2122 * coordinate transformations. */
2123 fac = fabsf(hoco[3]);
2124 if (hoco[0]<-fac || hoco[0]>fac)
2126 if (hoco[1]<-fac || hoco[1]>fac)
2128 if (hoco[2]<-fac || hoco[2]>fac)
2131 siz= 0.5f*(float)shb->size;
2132 co_r[0]= siz*(1.0f+hoco[0]/hoco[3]) -0.5f;
2133 co_r[1]= siz*(1.0f+hoco[1]/hoco[3]) -0.5f;
2134 co_r[2]= ((float)0x7FFFFFFF)*(hoco[2]/hoco[3]);
2136 /* XXXX bias, much less than normal shadbuf, or do we need a constant? */
2137 co_r[2] -= 0.05f*shb->bias;
2142 /* storage of shadow results, solid osa and transp case */
2143 static void isb_add_shadfac(ISBShadfacA **isbsapp, MemArena *mem, int obi, int facenr, short shadfac, short samples)
2148 /* in osa case, the samples were filled in with factor 1.0/R.osa. if fewer samples we have to correct */
2150 shadfacf= ((float)shadfac*R.osa)/(4096.0f*samples);
2152 shadfacf= ((float)shadfac)/(4096.0f);
2154 new= BLI_memarena_alloc(mem, sizeof(ISBShadfacA));
2156 new->facenr= facenr & ~RE_QUAD_OFFS;
2157 new->shadfac= shadfacf;
2159 new->next= (*isbsapp);
2166 /* adding samples, solid case */
2167 static int isb_add_samples(RenderPart *pa, ISBBranch *root, MemArena *memarena, ISBSample **samplebuf)
2169 int xi, yi, *xcos, *ycos;
2170 int sample, bsp_err= 0;
2172 /* bsp split doesn't like to handle regular sequences */
2173 xcos= MEM_mallocN(pa->rectx*sizeof(int), "xcos");
2174 ycos= MEM_mallocN(pa->recty*sizeof(int), "ycos");
2175 for (xi=0; xi<pa->rectx; xi++)
2177 for (yi=0; yi<pa->recty; yi++)
2179 BLI_array_randomize(xcos, sizeof(int), pa->rectx, 12345);
2180 BLI_array_randomize(ycos, sizeof(int), pa->recty, 54321);
2182 for (sample=0; sample<(R.osa?R.osa:1); sample++) {
2183 ISBSample *samp= samplebuf[sample], *samp1;
2185 for (yi=0; yi<pa->recty; yi++) {
2187 for (xi=0; xi<pa->rectx; xi++) {
2189 samp1= samp + y*pa->rectx + x;
2191 bsp_err |= isb_bsp_insert(root, memarena, samp1);
2204 /* lar->shb, pa->rectz and pa->rectp should exist */
2205 static void isb_make_buffer(RenderPart *pa, LampRen *lar)
2207 ShadBuf *shb= lar->shb;
2209 ISBSample *samp, *samplebuf[16]; /* should be RE_MAX_OSA */
2213 int *recto, *rectp, x, y, sindex, sample, bsp_err=0;
2215 /* storage for shadow, per thread */
2216 isbdata= shb->isb_result[pa->thread];
2218 /* to map the shi->xs and ys coordinate */
2219 isbdata->minx= pa->disprect.xmin;
2220 isbdata->miny= pa->disprect.ymin;
2221 isbdata->rectx= pa->rectx;
2222 isbdata->recty= pa->recty;
2224 /* branches are added using memarena (32k branches) */
2225 memarena = BLI_memarena_new(0x8000 * sizeof(ISBBranch), "isb arena");
2226 BLI_memarena_use_calloc(memarena);
2228 /* samplebuf is in camera view space (pixels) */
2229 for (sample=0; sample<(R.osa?R.osa:1); sample++)
2230 samplebuf[sample]= MEM_callocN(sizeof(ISBSample)*pa->rectx*pa->recty, "isb samplebuf");
2232 /* for end result, ISBSamples point to this in non OSA case, otherwise to pixstruct->shadfac */
2234 isbdata->shadfacs= MEM_callocN(pa->rectx*pa->recty*sizeof(short), "isb shadfacs");
2236 /* setup bsp root */
2237 memset(&root, 0, sizeof(ISBBranch));
2238 root.box.xmin = (float)shb->size;
2239 root.box.ymin = (float)shb->size;
2241 /* create the sample buffers */
2242 for (sindex=0, y=0; y<pa->recty; y++) {
2243 for (x=0; x<pa->rectx; x++, sindex++) {
2245 /* this makes it a long function, but splitting it out would mean 10+ arguments */
2246 /* first check OSA case */
2248 rd= pa->rectdaps + sindex;
2250 float xs= (float)(x + pa->disprect.xmin);
2251 float ys= (float)(y + pa->disprect.ymin);
2253 for (sample=0; sample<R.osa; sample++) {
2254 PixStr *ps= (PixStr *)(*rd);
2255 int mask= (1<<sample);
2258 if (ps->mask & mask)
2262 if (ps && ps->facenr>0) {
2263 ObjectInstanceRen *obi= &R.objectinstance[ps->obi];
2264 ObjectRen *obr= obi->obr;
2265 VlakRen *vlr= RE_findOrAddVlak(obr, (ps->facenr-1) & RE_QUAD_MASK);
2267 samp= samplebuf[sample] + sindex;
2268 /* convert image plane pixel location to lamp buffer space */
2269 if (viewpixel_to_lampbuf(shb, obi, vlr, xs + R.jit[sample][0], ys + R.jit[sample][1], samp->zco)) {
2271 samp->facenr= ps->facenr & ~RE_QUAD_OFFS;
2273 samp->shadfac= &ps->shadfac;
2274 bound_rectf((rctf *)&root.box, samp->zco);
2281 rectp= pa->rectp + sindex;
2282 recto= pa->recto + sindex;
2284 ObjectInstanceRen *obi= &R.objectinstance[*recto];
2285 ObjectRen *obr= obi->obr;
2286 VlakRen *vlr= RE_findOrAddVlak(obr, (*rectp-1) & RE_QUAD_MASK);
2287 float xs= (float)(x + pa->disprect.xmin);
2288 float ys= (float)(y + pa->disprect.ymin);
2290 samp= samplebuf[0] + sindex;
2291 /* convert image plane pixel location to lamp buffer space */
2292 if (viewpixel_to_lampbuf(shb, obi, vlr, xs, ys, samp->zco)) {
2294 samp->facenr= *rectp & ~RE_QUAD_OFFS;
2295 samp->shadfac= isbdata->shadfacs + sindex;
2296 bound_rectf((rctf *)&root.box, samp->zco);
2303 /* simple method to see if we have samples */
2304 if (root.box.xmin != (float)shb->size) {
2305 /* now create a regular split, root.box has the initial bounding box of all pixels */
2306 /* split bsp 8 levels deep, in regular grid (16 x 16) */
2307 isb_bsp_split_init(&root, memarena, 8);
2309 /* insert all samples in BSP now */
2310 bsp_err= isb_add_samples(pa, &root, memarena, samplebuf);
2313 /* go over all faces and fill in shadow values */
2315 isb_bsp_fillfaces(&R, lar, &root); /* shb->persmat should have been calculated */
2317 /* copy shadow samples to persistent buffer, reduce memory overhead */
2319 ISBShadfacA **isbsa= isbdata->shadfaca= MEM_callocN(pa->rectx*pa->recty*sizeof(void *), "isb shadfacs");
2321 isbdata->memarena = BLI_memarena_new(0x8000 * sizeof(ISBSampleA), "isb arena");
2322 BLI_memarena_use_calloc(isbdata->memarena);
2324 for (rd= pa->rectdaps, x=pa->rectx*pa->recty; x>0; x--, rd++, isbsa++) {
2327 PixStr *ps= (PixStr *)(*rd);
2330 isb_add_shadfac(isbsa, isbdata->memarena, ps->obi, ps->facenr, ps->shadfac, count_mask(ps->mask));
2339 if (isbdata->shadfacs) {
2340 MEM_freeN(isbdata->shadfacs);
2341 isbdata->shadfacs= NULL;
2346 BLI_memarena_free(memarena);
2349 for (x=0; x<(R.osa?R.osa:1); x++)
2350 MEM_freeN(samplebuf[x]);
2352 if (bsp_err) printf("error in filling bsp\n");
2355 /* add sample to buffer, isbsa is the root sample in a buffer */
2356 static ISBSampleA *isb_alloc_sample_transp(ISBSampleA **isbsa, MemArena *mem)
2360 new= BLI_memarena_alloc(mem, sizeof(ISBSampleA));
2362 new->next= (*isbsa);
2370 /* adding samples in BSP, transparent case */
2371 static int isb_add_samples_transp(RenderPart *pa, ISBBranch *root, MemArena *memarena, ISBSampleA ***samplebuf)
2373 int xi, yi, *xcos, *ycos;
2374 int sample, bsp_err= 0;
2376 /* bsp split doesn't like to handle regular sequences */
2377 xcos= MEM_mallocN(pa->rectx*sizeof(int), "xcos");
2378 ycos= MEM_mallocN(pa->recty*sizeof(int), "ycos");
2379 for (xi=0; xi<pa->rectx; xi++)
2381 for (yi=0; yi<pa->recty; yi++)
2383 BLI_array_randomize(xcos, sizeof(int), pa->rectx, 12345);
2384 BLI_array_randomize(ycos, sizeof(int), pa->recty, 54321);
2386 for (sample=0; sample<(R.osa?R.osa:1); sample++) {
2387 ISBSampleA **samp= samplebuf[sample], *samp1;
2389 for (yi=0; yi<pa->recty; yi++) {
2391 for (xi=0; xi<pa->rectx; xi++) {
2394 samp1= *(samp + y*pa->rectx + x);
2396 bsp_err |= isb_bsp_insert(root, memarena, (ISBSample *)samp1);
2411 /* Ztransp version */
2412 /* lar->shb, pa->rectz and pa->rectp should exist */
2413 static void isb_make_buffer_transp(RenderPart *pa, APixstr *apixbuf, LampRen *lar)
2415 ShadBuf *shb= lar->shb;
2417 ISBSampleA *samp, **samplebuf[16]; /* MAX_OSA */
2421 int x, y, sindex, sample, bsp_err=0;
2423 /* storage for shadow, per thread */
2424 isbdata= shb->isb_result[pa->thread];
2426 /* to map the shi->xs and ys coordinate */
2427 isbdata->minx= pa->disprect.xmin;
2428 isbdata->miny= pa->disprect.ymin;
2429 isbdata->rectx= pa->rectx;
2430 isbdata->recty= pa->recty;
2432 /* branches are added using memarena (32k branches) */
2433 memarena = BLI_memarena_new(0x8000 * sizeof(ISBBranch), "isb arena");
2434 BLI_memarena_use_calloc(memarena);
2436 /* samplebuf is in camera view space (pixels) */
2437 for (sample=0; sample<(R.osa?R.osa:1); sample++)
2438 samplebuf[sample]= MEM_callocN(sizeof(void *)*pa->rectx*pa->recty, "isb alpha samplebuf");
2440 /* setup bsp root */
2441 memset(&root, 0, sizeof(ISBBranch));
2442 root.box.xmin = (float)shb->size;
2443 root.box.ymin = (float)shb->size;
2445 /* create the sample buffers */
2446 for (ap= apixbuf, sindex=0, y=0; y<pa->recty; y++) {
2447 for (x=0; x<pa->rectx; x++, sindex++, ap++) {
2451 float xs= (float)(x + pa->disprect.xmin);
2452 float ys= (float)(y + pa->disprect.ymin);
2454 for (apn=ap; apn; apn= apn->next) {
2456 for (a=0; a<4; a++) {
2458 ObjectInstanceRen *obi= &R.objectinstance[apn->obi[a]];
2459 ObjectRen *obr= obi->obr;
2460 VlakRen *vlr= RE_findOrAddVlak(obr, (apn->p[a]-1) & RE_QUAD_MASK);
2463 /* here we store shadfac, easier to create the end storage buffer. needs zero'ed, multiple shadowbufs use it */
2467 for (sample=0; sample<R.osa; sample++) {
2468 int mask= (1<<sample);
2470 if (apn->mask[a] & mask) {
2472 /* convert image plane pixel location to lamp buffer space */
2473 if (viewpixel_to_lampbuf(shb, obi, vlr, xs + R.jit[sample][0], ys + R.jit[sample][1], zco)) {
2474 samp= isb_alloc_sample_transp(samplebuf[sample] + sindex, memarena);
2475 samp->obi= apn->obi[a];
2476 samp->facenr= apn->p[a] & ~RE_QUAD_OFFS;
2477 samp->shadfac= &apn->shadfac[a];
2479 copy_v3_v3(samp->zco, zco);
2480 bound_rectf((rctf *)&root.box, samp->zco);
2487 /* convert image plane pixel location to lamp buffer space */
2488 if (viewpixel_to_lampbuf(shb, obi, vlr, xs, ys, zco)) {
2490 samp= isb_alloc_sample_transp(samplebuf[0] + sindex, memarena);
2491 samp->obi= apn->obi[a];
2492 samp->facenr= apn->p[a] & ~RE_QUAD_OFFS;
2493 samp->shadfac= &apn->shadfac[a];
2495 copy_v3_v3(samp->zco, zco);
2496 bound_rectf((rctf *)&root.box, samp->zco);
2506 /* simple method to see if we have samples */
2507 if (root.box.xmin != (float)shb->size) {
2508 /* now create a regular split, root.box has the initial bounding box of all pixels */
2509 /* split bsp 8 levels deep, in regular grid (16 x 16) */
2510 isb_bsp_split_init(&root, memarena, 8);
2512 /* insert all samples in BSP now */
2513 bsp_err= isb_add_samples_transp(pa, &root, memarena, samplebuf);
2516 ISBShadfacA **isbsa;
2518 /* go over all faces and fill in shadow values */
2519 isb_bsp_fillfaces(&R, lar, &root); /* shb->persmat should have been calculated */
2521 /* copy shadow samples to persistent buffer, reduce memory overhead */
2522 isbsa= isbdata->shadfaca= MEM_callocN(pa->rectx*pa->recty*sizeof(void *), "isb shadfacs");
2524 isbdata->memarena = BLI_memarena_new(0x8000 * sizeof(ISBSampleA), "isb arena");
2526 for (ap= apixbuf, x=pa->rectx*pa->recty; x>0; x--, ap++, isbsa++) {
2530 for (apn=ap; apn; apn= apn->next) {
2532 for (a=0; a<4; a++) {
2533 if (apn->p[a] && apn->shadfac[a]) {
2535 isb_add_shadfac(isbsa, isbdata->memarena, apn->obi[a], apn->p[a], apn->shadfac[a], count_mask(apn->mask[a]));
2537 isb_add_shadfac(isbsa, isbdata->memarena, apn->obi[a], apn->p[a], apn->shadfac[a], 0);
2547 BLI_memarena_free(memarena);
2550 for (x=0; x<(R.osa?R.osa:1); x++)
2551 MEM_freeN(samplebuf[x]);
2553 if (bsp_err) printf("error in filling bsp\n");
2560 /* returns amount of light (1.0 = no shadow) */
2561 /* note, shadepixel() rounds the coordinate, not the real sample info */
2562 float ISB_getshadow(ShadeInput *shi, ShadBuf *shb)
2564 /* if raytracing, we can't accept irregular shadow */
2565 if (shi->depth==0) {
2566 ISBData *isbdata= shb->isb_result[shi->thread];
2569 if (isbdata->shadfacs || isbdata->shadfaca) {
2570 int x= shi->xs - isbdata->minx;
2572 if (x >= 0 && x < isbdata->rectx) {
2573 int y= shi->ys - isbdata->miny;
2575 if (y >= 0 && y < isbdata->recty) {
2576 if (isbdata->shadfacs) {
2577 short *sp= isbdata->shadfacs + y*isbdata->rectx + x;
2578 return *sp>=4096?0.0f:1.0f - ((float)*sp)/4096.0f;
2581 int sindex= y*isbdata->rectx + x;
2582 int obi= shi->obi - R.objectinstance;
2583 ISBShadfacA *isbsa= *(isbdata->shadfaca + sindex);
2586 if (isbsa->facenr==shi->facenr+1 && isbsa->obi==obi)
2587 return isbsa->shadfac>=1.0f?0.0f:1.0f - isbsa->shadfac;
2599 /* part is supposed to be solid zbuffered (apixbuf==NULL) or transparent zbuffered */
2600 void ISB_create(RenderPart *pa, APixstr *apixbuf)
2604 /* go over all lamps, and make the irregular buffers */
2605 for (go=R.lights.first; go; go= go->next) {
2606 LampRen *lar= go->lampren;
2608 if (lar->type==LA_SPOT && lar->shb && lar->buftype==LA_SHADBUF_IRREGULAR) {
2610 /* create storage for shadow, per thread */
2611 lar->shb->isb_result[pa->thread]= MEM_callocN(sizeof(ISBData), "isb data");
2614 isb_make_buffer_transp(pa, apixbuf, lar);
2616 isb_make_buffer(pa, lar);
2622 /* end of part rendering, free stored shadow data for this thread from all lamps */
2623 void ISB_free(RenderPart *pa)
2627 /* go over all lamps, and free the irregular buffers */
2628 for (go=R.lights.first; go; go= go->next) {
2629 LampRen *lar= go->lampren;
2631 if (lar->type==LA_SPOT && lar->shb && lar->buftype==LA_SHADBUF_IRREGULAR) {
2632 ISBData *isbdata= lar->shb->isb_result[pa->thread];
2635 if (isbdata->shadfacs)
2636 MEM_freeN(isbdata->shadfacs);
2637 if (isbdata->shadfaca)
2638 MEM_freeN(isbdata->shadfaca);
2640 if (isbdata->memarena)
2641 BLI_memarena_free(isbdata->memarena);
2644 lar->shb->isb_result[pa->thread]= NULL;