Render Baking
[blender.git] / source / blender / render / intern / source / rendercore.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributors: Hos, Robert Wenzlaff.
24  * Contributors: 2004/2005/2006 Blender Foundation, full recode
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /* system includes */
30 #include <stdio.h>
31 #include <math.h>
32 #include <string.h>
33
34 /* External modules: */
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_arithb.h"
38 #include "BLI_blenlib.h"
39 #include "BLI_jitter.h"
40 #include "BLI_rand.h"
41 #include "BLI_threads.h"
42
43 #include "BKE_utildefines.h"
44
45 #include "DNA_image_types.h"
46 #include "DNA_lamp_types.h"
47 #include "DNA_material_types.h"
48 #include "DNA_meshdata_types.h"
49
50 #include "BKE_global.h"
51 #include "BKE_image.h"
52 #include "BKE_main.h"
53 #include "BKE_node.h"
54 #include "BKE_texture.h"
55
56 #include "IMB_imbuf_types.h"
57 #include "IMB_imbuf.h"
58
59 /* local include */
60 #include "renderpipeline.h"
61 #include "render_types.h"
62 #include "renderdatabase.h"
63 #include "pixelblending.h"
64 #include "pixelshading.h"
65 #include "shadbuf.h"
66 #include "shading.h"
67 #include "sss.h"
68 #include "zbuf.h"
69 #include "RE_raytrace.h"
70
71 #include "PIL_time.h"
72
73 /* own include */
74 #include "rendercore.h"
75
76
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 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
82
83 /* x and y are current pixels in rect to be rendered */
84 /* do not normalize! */
85 void calc_view_vector(float *view, float x, float y)
86 {
87
88         view[2]= -ABS(R.clipsta);
89         
90         if(R.r.mode & R_ORTHO) {
91                 view[0]= view[1]= 0.0f;
92         }
93         else {
94                 
95                 if(R.r.mode & R_PANORAMA)
96                         x-= R.panodxp;
97                 
98                 /* move x and y to real viewplane coords */
99                 x= (x/(float)R.winx);
100                 view[0]= R.viewplane.xmin + x*(R.viewplane.xmax - R.viewplane.xmin);
101                 
102                 y= (y/(float)R.winy);
103                 view[1]= R.viewplane.ymin + y*(R.viewplane.ymax - R.viewplane.ymin);
104                 
105 //              if(R.flag & R_SEC_FIELD) {
106 //                      if(R.r.mode & R_ODDFIELD) view[1]= (y+R.ystart)*R.ycor;
107 //                      else view[1]= (y+R.ystart+1.0)*R.ycor;
108 //              }
109 //              else view[1]= (y+R.ystart+R.bluroffsy+0.5)*R.ycor;
110         
111                 if(R.r.mode & R_PANORAMA) {
112                         float u= view[0] + R.panodxv; float v= view[2];
113                         view[0]= R.panoco*u + R.panosi*v;
114                         view[2]= -R.panosi*u + R.panoco*v;
115                 }
116         }
117 }
118
119 void calc_renderco_ortho(float *co, float x, float y, int z)
120 {
121         /* x and y 3d coordinate can be derived from pixel coord and winmat */
122         float fx= 2.0f/(R.winx*R.winmat[0][0]);
123         float fy= 2.0f/(R.winy*R.winmat[1][1]);
124         float zco;
125         
126         co[0]= (x - 0.5f*R.winx)*fx - R.winmat[3][0]/R.winmat[0][0];
127         co[1]= (y - 0.5f*R.winy)*fy - R.winmat[3][1]/R.winmat[1][1];
128         
129         zco= ((float)z)/2147483647.0f;
130         co[2]= R.winmat[3][2]/( R.winmat[2][3]*zco - R.winmat[2][2] );
131 }
132
133 void calc_renderco_zbuf(float *co, float *view, int z)
134 {
135         float fac, zco;
136         
137         /* inverse of zbuf calc: zbuf = MAXZ*hoco_z/hoco_w */
138         zco= ((float)z)/2147483647.0f;
139         co[2]= R.winmat[3][2]/( R.winmat[2][3]*zco - R.winmat[2][2] );
140
141         fac= co[2]/view[2];
142         co[0]= fac*view[0];
143         co[1]= fac*view[1];
144 }
145
146 /* also used in zbuf.c and shadbuf.c */
147 int count_mask(unsigned short mask)
148 {
149         if(R.samples)
150                 return (R.samples->cmask[mask & 255]+R.samples->cmask[mask>>8]);
151         return 0;
152 }
153
154 static int calchalo_z(HaloRen *har, int zz)
155 {
156         
157         if(har->type & HA_ONLYSKY) {
158                 if(zz!=0x7FFFFFFF) zz= - 0x7FFFFF;
159         }
160         else {
161                 zz= (zz>>8);
162         }
163         return zz;
164 }
165
166 static void halo_pixelstruct(HaloRen *har, float *rb, float dist, float xn, float yn, PixStr *ps)
167 {
168         float col[4], accol[4];
169         int amount, amountm, zz, flarec;
170         
171         amount= 0;
172         accol[0]=accol[1]=accol[2]=accol[3]= 0.0f;
173         flarec= har->flarec;
174         
175         while(ps) {
176                 amountm= count_mask(ps->mask);
177                 amount+= amountm;
178                 
179                 zz= calchalo_z(har, ps->z);
180                 if(zz> har->zs) {
181                         float fac;
182                         
183                         shadeHaloFloat(har, col, zz, dist, xn, yn, flarec);
184                         fac= ((float)amountm)/(float)R.osa;
185                         accol[0]+= fac*col[0];
186                         accol[1]+= fac*col[1];
187                         accol[2]+= fac*col[2];
188                         accol[3]+= fac*col[3];
189                         flarec= 0;
190                 }
191                 
192                 ps= ps->next;
193         }
194         /* now do the sky sub-pixels */
195         amount= R.osa-amount;
196         if(amount) {
197                 float fac;
198
199                 shadeHaloFloat(har, col, 0x7FFFFF, dist, xn, yn, flarec);
200                 fac= ((float)amount)/(float)R.osa;
201                 accol[0]+= fac*col[0];
202                 accol[1]+= fac*col[1];
203                 accol[2]+= fac*col[2];
204                 accol[3]+= fac*col[3];
205         }
206         col[0]= accol[0];
207         col[1]= accol[1];
208         col[2]= accol[2];
209         col[3]= accol[3];
210         
211         addalphaAddfacFloat(rb, col, har->add);
212         
213 }
214
215 static void halo_tile(RenderPart *pa, float *pass, unsigned int lay)
216 {
217         HaloRen *har = NULL;
218         rcti disprect= pa->disprect, testrect= pa->disprect;
219         float dist, xsq, ysq, xn, yn, *rb;
220         float col[4];
221         long *rd= NULL;
222         int a, *rz, zz, y;
223         short minx, maxx, miny, maxy, x;
224
225         /* we don't render halos in the cropped area, gives errors in flare counter */
226         if(pa->crop) {
227                 testrect.xmin+= pa->crop;
228                 testrect.xmax-= pa->crop;
229                 testrect.ymin+= pa->crop;
230                 testrect.ymax-= pa->crop;
231         }
232         
233         for(a=0; a<R.tothalo; a++) {
234                 if((a & 255)==0)
235                         har= R.bloha[a>>8];
236                 else har++;
237
238                 /* layer test, clip halo with y */
239                 if((har->lay & lay)==0);
240                 else if(testrect.ymin > har->maxy);
241                 else if(testrect.ymax < har->miny);
242                 else {
243                         
244                         minx= floor(har->xs-har->rad);
245                         maxx= ceil(har->xs+har->rad);
246                         
247                         if(testrect.xmin > maxx);
248                         else if(testrect.xmax < minx);
249                         else {
250                                 
251                                 minx= MAX2(minx, testrect.xmin);
252                                 maxx= MIN2(maxx, testrect.xmax);
253                         
254                                 miny= MAX2(har->miny, testrect.ymin);
255                                 maxy= MIN2(har->maxy, testrect.ymax);
256                         
257                                 for(y=miny; y<maxy; y++) {
258                                         int rectofs= (y-disprect.ymin)*pa->rectx + (minx - disprect.xmin);
259                                         rb= pass + 4*rectofs;
260                                         rz= pa->rectz + rectofs;
261                                         
262                                         if(pa->rectdaps)
263                                                 rd= pa->rectdaps + rectofs;
264                                         
265                                         yn= (y-har->ys)*R.ycor;
266                                         ysq= yn*yn;
267                                         
268                                         for(x=minx; x<maxx; x++, rb+=4, rz++) {
269                                                 xn= x- har->xs;
270                                                 xsq= xn*xn;
271                                                 dist= xsq+ysq;
272                                                 if(dist<har->radsq) {
273                                                         if(rd && *rd) {
274                                                                 halo_pixelstruct(har, rb, dist, xn, yn, (PixStr *)*rd);
275                                                         }
276                                                         else {
277                                                                 zz= calchalo_z(har, *rz);
278                                                                 if(zz> har->zs) {
279                                                                         shadeHaloFloat(har, col, zz, dist, xn, yn, har->flarec);
280                                                                         addalphaAddfacFloat(rb, col, har->add);
281                                                                 }
282                                                         }
283                                                 }
284                                                 if(rd) rd++;
285                                         }
286                                 }
287                         }
288                 }
289                 if(R.test_break() ) break; 
290         }
291 }
292
293 static void lamphalo_tile(RenderPart *pa, RenderLayer *rl)
294 {
295         ShadeInput shi;
296         float *pass= rl->rectf;
297         float fac;
298         long *rd= pa->rectdaps;
299         int x, y, *rz= pa->rectz;
300         
301         shade_input_initialize(&shi, pa, rl, 0);
302         
303         for(y=pa->disprect.ymin; y<pa->disprect.ymax; y++) {
304                 for(x=pa->disprect.xmin; x<pa->disprect.xmax; x++, rz++, pass+=4) {
305                         
306                         calc_view_vector(shi.view, x, y);
307                         
308                         if(rd && *rd) {
309                                 PixStr *ps= (PixStr *)*rd;
310                                 int samp, totsamp= 0;
311                                 
312                                 while(ps) {
313                                         if(R.r.mode & R_ORTHO)
314                                                 calc_renderco_ortho(shi.co, (float)x, (float)y, ps->z);
315                                         else
316                                                 calc_renderco_zbuf(shi.co, shi.view, ps->z);
317                                         
318                                         totsamp+= samp= count_mask(ps->mask);
319                                         fac= ((float)samp)/(float)R.osa;
320                                         renderspothalo(&shi, pass, fac);
321                                         ps= ps->next;
322                                 }
323                                 if(totsamp<R.osa) {
324                                         fac= ((float)R.osa-totsamp)/(float)R.osa;
325                                         shi.co[2]= 0.0f;
326                                         renderspothalo(&shi, pass, fac);
327                                 }
328                         }
329                         else {
330                                 if(R.r.mode & R_ORTHO)
331                                         calc_renderco_ortho(shi.co, (float)x, (float)y, *rz);
332                                 else
333                                         calc_renderco_zbuf(shi.co, shi.view, *rz);
334                                 
335                                 renderspothalo(&shi, pass, 1.0f);
336                         }
337                         
338                         if(rd) rd++;
339                 }
340                 if(y&1)
341                         if(R.test_break()) break; 
342         }
343 }                               
344
345
346 /* ********************* MAINLOOPS ******************** */
347
348 /* osa version */
349 static void add_filt_passes(RenderLayer *rl, int curmask, int rectx, int offset, ShadeInput *shi, ShadeResult *shr)
350 {
351         RenderPass *rpass;
352         
353         for(rpass= rl->passes.first; rpass; rpass= rpass->next) {
354                 float *fp, *col= NULL;
355                 int pixsize= 3;
356                 
357                 switch(rpass->passtype) {
358                         case SCE_PASS_RGBA:
359                                 col= shr->col;
360                                 pixsize= 4;
361                                 break;
362                         case SCE_PASS_DIFFUSE:
363                                 col= shr->diff;
364                                 break;
365                         case SCE_PASS_SPEC:
366                                 col= shr->spec;
367                                 break;
368                         case SCE_PASS_SHADOW:
369                                 col= shr->shad;
370                                 break;
371                         case SCE_PASS_AO:
372                                 col= shr->ao;
373                                 break;
374                         case SCE_PASS_REFLECT:
375                                 col= shr->refl;
376                                 break;
377                         case SCE_PASS_REFRACT:
378                                 col= shr->refr;
379                                 break;
380                         case SCE_PASS_RADIO:
381                                 col= shr->rad;
382                                 break;
383                         case SCE_PASS_NORMAL:
384                                 col= shr->nor;
385                                 break;
386                         case SCE_PASS_UV:
387                                 /* box filter only, gauss will screwup UV too much */
388                                 if(shi->totuv) {
389                                         float mult= (float)count_mask(curmask)/(float)R.osa;
390                                         fp= rpass->rect + 3*offset;
391                                         fp[0]+= mult*(0.5f + 0.5f*shi->uv[0].uv[0]);
392                                         fp[1]+= mult*(0.5f + 0.5f*shi->uv[0].uv[1]);
393                                         fp[2]+= mult;
394                                 }
395                                 break;
396                         case SCE_PASS_INDEXOB:
397                                 /* no filter */
398                                 if(shi->vlr) {
399                                         fp= rpass->rect + offset;
400                                         if(*fp==0.0f)
401                                                 *fp= (float)shi->vlr->ob->index;
402                                 }
403                                 break;
404                         case SCE_PASS_VECTOR:
405                         {
406                                 /* add minimum speed in pixel, no filter */
407                                 fp= rpass->rect + 4*offset;
408                                 if( (ABS(shr->winspeed[0]) + ABS(shr->winspeed[1]))< (ABS(fp[0]) + ABS(fp[1])) ) {
409                                         fp[0]= shr->winspeed[0];
410                                         fp[1]= shr->winspeed[1];
411                                 }
412                                 if( (ABS(shr->winspeed[2]) + ABS(shr->winspeed[3]))< (ABS(fp[2]) + ABS(fp[3])) ) {
413                                         fp[2]= shr->winspeed[2];
414                                         fp[3]= shr->winspeed[3];
415                                 }
416                         }
417                                 break;
418                 }
419                 if(col) {
420                         fp= rpass->rect + pixsize*offset;
421                         add_filt_fmask_pixsize(curmask, col, fp, rectx, pixsize);
422                 }
423         }
424 }
425
426 /* non-osa version */
427 static void add_passes(RenderLayer *rl, int offset, ShadeInput *shi, ShadeResult *shr)
428 {
429         RenderPass *rpass;
430         
431         for(rpass= rl->passes.first; rpass; rpass= rpass->next) {
432                 float *fp, *col= NULL, uvcol[3];
433                 int a, pixsize= 3;
434                 
435                 switch(rpass->passtype) {
436                         case SCE_PASS_RGBA:
437                                 col= shr->col;
438                                 pixsize= 4;
439                                 break;
440                         case SCE_PASS_DIFFUSE:
441                                 col= shr->diff;
442                                 break;
443                         case SCE_PASS_SPEC:
444                                 col= shr->spec;
445                                 break;
446                         case SCE_PASS_SHADOW:
447                                 col= shr->shad;
448                                 break;
449                         case SCE_PASS_AO:
450                                 col= shr->ao;
451                                 break;
452                         case SCE_PASS_REFLECT:
453                                 col= shr->refl;
454                                 break;
455                         case SCE_PASS_REFRACT:
456                                 col= shr->refr;
457                                 break;
458                         case SCE_PASS_RADIO:
459                                 col= shr->rad;
460                                 break;
461                         case SCE_PASS_NORMAL:
462                                 col= shr->nor;
463                                 break;
464                         case SCE_PASS_UV:
465                                 if(shi->totuv) {
466                                         uvcol[0]= 0.5f + 0.5f*shi->uv[0].uv[0];
467                                         uvcol[1]= 0.5f + 0.5f*shi->uv[0].uv[1];
468                                         uvcol[2]= 1.0f;
469                                         col= uvcol;
470                                 }
471                                 break;
472                         case SCE_PASS_VECTOR:
473                                 col= shr->winspeed;
474                                 pixsize= 4;
475                                 break;
476                         case SCE_PASS_INDEXOB:
477                                 if(shi->vlr) {
478                                         fp= rpass->rect + offset;
479                                         *fp= (float)shi->vlr->ob->index;
480                                 }
481                                 break;
482                 }
483                 if(col) {
484                         fp= rpass->rect + pixsize*offset;
485                         for(a=0; a<pixsize; a++)
486                                 fp[a]= col[a];
487                 }
488         }
489 }
490
491 /* only do sky, is default in the solid layer (shade_tile) btw */
492 static void sky_tile(RenderPart *pa, float *pass)
493 {
494         float col[4];
495         int x, y;
496         
497         if(R.r.alphamode!=R_ADDSKY)
498                 return;
499         
500         for(y=pa->disprect.ymin; y<pa->disprect.ymax; y++) {
501                 for(x=pa->disprect.xmin; x<pa->disprect.xmax; x++, pass+=4) {
502                         if(pass[3]<1.0f) {
503                                 if(pass[3]==0.0f)
504                                         shadeSkyPixel(pass, x, y);
505                                 else {
506                                         shadeSkyPixel(col, x, y);
507                                         addAlphaOverFloat(col, pass);
508                                         QUATCOPY(pass, col);
509                                 }
510                         }
511                 }
512                 
513                 if(y&1)
514                         if(R.test_break()) break; 
515         }
516 }
517
518 static void shadeDA_tile(RenderPart *pa, RenderLayer *rl)
519 {
520         RenderResult *rr= pa->result;
521         ShadeSample ssamp;
522         float *fcol, *rf, *rectf= rl->rectf;
523         long *rd, *rectdaps= pa->rectdaps;
524         int samp;
525         int x, y, seed, crop=0, offs=0, od, addpassflag;
526         
527         if(R.test_break()) return; 
528         
529         /* irregular shadowb buffer creation */
530         if(R.r.mode & R_SHADOW)
531                 ISB_create(pa, NULL);
532         
533         /* we set per pixel a fixed seed, for random AO and shadow samples */
534         seed= pa->rectx*pa->disprect.ymin;
535         
536         /* general shader info, passes */
537         shade_sample_initialize(&ssamp, pa, rl);
538         addpassflag= rl->passflag & ~(SCE_PASS_Z|SCE_PASS_COMBINED);
539                                 
540         /* filtered render, for now we assume only 1 filter size */
541         if(pa->crop) {
542                 crop= 1;
543                 rectf+= 4*(pa->rectx + 1);
544                 rectdaps+= pa->rectx + 1;
545                 offs= pa->rectx + 1;
546         }
547         
548         /* scanline updates have to be 2 lines behind */
549         rr->renrect.ymin= 0;
550         rr->renrect.ymax= -2*crop;
551         rr->renlay= rl;
552                                 
553         for(y=pa->disprect.ymin+crop; y<pa->disprect.ymax-crop; y++, rr->renrect.ymax++) {
554                 rf= rectf;
555                 rd= rectdaps;
556                 od= offs;
557                 
558                 for(x=pa->disprect.xmin+crop; x<pa->disprect.xmax-crop; x++, rd++, rf+=4, od++) {
559                         BLI_thread_srandom(pa->thread, seed++);
560                         
561                         if(*rd) {
562                                 if(shade_samples(&ssamp, (PixStr *)(*rd), x, y)) {
563                                         for(samp=0; samp<ssamp.tot; samp++) {
564                                                 
565                                                 fcol= ssamp.shr[samp].combined;
566                                                 add_filt_fmask(ssamp.shi[samp].mask, fcol, rf, pa->rectx);
567                                                 
568                                                 if(addpassflag)
569                                                         add_filt_passes(rl, ssamp.shi[samp].mask, pa->rectx, od, &ssamp.shi[samp], &ssamp.shr[samp]);
570                                         }
571                                 }
572                         }
573                 }
574                 
575                 rectf+= 4*pa->rectx;
576                 rectdaps+= pa->rectx;
577                 offs+= pa->rectx;
578                 
579                 if(y&1) if(R.test_break()) break; 
580         }
581         
582         /* disable scanline updating */
583         rr->renlay= NULL;
584         
585         if(R.r.mode & R_SHADOW)
586                 ISB_free(pa);
587 }
588
589 /* ************* pixel struct ******** */
590
591
592 static PixStrMain *addpsmain(ListBase *lb)
593 {
594         PixStrMain *psm;
595         
596         psm= (PixStrMain *)MEM_mallocN(sizeof(PixStrMain),"pixstrMain");
597         BLI_addtail(lb, psm);
598         
599         psm->ps= (PixStr *)MEM_mallocN(4096*sizeof(PixStr),"pixstr");
600         psm->counter= 0;
601         
602         return psm;
603 }
604
605 static void freeps(ListBase *lb)
606 {
607         PixStrMain *psm, *psmnext;
608         
609         for(psm= lb->first; psm; psm= psmnext) {
610                 psmnext= psm->next;
611                 if(psm->ps)
612                         MEM_freeN(psm->ps);
613                 MEM_freeN(psm);
614         }
615         lb->first= lb->last= NULL;
616 }
617
618 static void addps(ListBase *lb, long *rd, int facenr, int z, unsigned short mask)
619 {
620         PixStrMain *psm;
621         PixStr *ps, *last= NULL;
622         
623         if(*rd) {       
624                 ps= (PixStr *)(*rd);
625                 
626                 while(ps) {
627                         if( ps->facenr == facenr ) {
628                                 ps->mask |= mask;
629                                 return;
630                         }
631                         last= ps;
632                         ps= ps->next;
633                 }
634         }
635         
636         /* make new PS (pixel struct) */
637         psm= lb->last;
638         
639         if(psm->counter==4095)
640                 psm= addpsmain(lb);
641         
642         ps= psm->ps + psm->counter++;
643         
644         if(last) last->next= ps;
645         else *rd= (long)ps;
646         
647         ps->next= NULL;
648         ps->facenr= facenr;
649         ps->z= z;
650         ps->mask = mask;
651         ps->shadfac= 0;
652 }
653
654 static void make_pixelstructs(RenderPart *pa, ListBase *lb)
655 {
656         long *rd= pa->rectdaps;
657         int *rp= pa->rectp;
658         int *rz= pa->rectz;
659         int x, y;
660         int mask= 1<<pa->sample;
661         
662         for(y=0; y<pa->recty; y++) {
663                 for(x=0; x<pa->rectx; x++, rd++, rp++) {
664                         if(*rp) {
665                                 addps(lb, rd, *rp, *(rz+x), mask);
666                         }
667                 }
668                 rz+= pa->rectx;
669         }
670 }
671
672 static void edge_enhance_add(RenderPart *pa, float *rectf, float *arect)
673 {
674         float addcol[4];
675         int pix;
676         
677         if(arect==NULL)
678                 return;
679         
680         for(pix= pa->rectx*pa->recty; pix>0; pix--, arect++, rectf+=4) {
681                 if(*arect != 0.0f) {
682                         addcol[0]= *arect * R.r.edgeR;
683                         addcol[1]= *arect * R.r.edgeG;
684                         addcol[2]= *arect * R.r.edgeB;
685                         addcol[3]= *arect;
686                         addAlphaOverFloat(rectf, addcol);
687                 }
688         }
689 }
690
691
692 static void convert_to_key_alpha(RenderPart *pa, float *rectf)
693 {
694         int y;
695         
696         for(y= pa->rectx*pa->recty; y>0; y--, rectf+=4) {
697                 if(rectf[3] >= 1.0f);
698                 else if(rectf[3] > 0.0f) {
699                         rectf[0] /= rectf[3];
700                         rectf[1] /= rectf[3];
701                         rectf[2] /= rectf[3];
702                 }
703         }
704 }
705
706 /* adds only alpha values */
707 static void edge_enhance_tile(RenderPart *pa, float *rectf)     
708 {
709         /* use zbuffer to define edges, add it to the image */
710         int y, x, col, *rz, *rz1, *rz2, *rz3;
711         int zval1, zval2, zval3;
712         float *rf;
713         
714         /* shift values in zbuffer 4 to the right (anti overflows), for filter we need multiplying with 12 max */
715         rz= pa->rectz;
716         if(rz==NULL) return;
717         
718         for(y=0; y<pa->recty; y++)
719                 for(x=0; x<pa->rectx; x++, rz++) (*rz)>>= 4;
720         
721         rz1= pa->rectz;
722         rz2= rz1+pa->rectx;
723         rz3= rz2+pa->rectx;
724         
725         rf= rectf+pa->rectx+1;
726         
727         for(y=0; y<pa->recty-2; y++) {
728                 for(x=0; x<pa->rectx-2; x++, rz1++, rz2++, rz3++, rf++) {
729                         
730                         /* prevent overflow with sky z values */
731                         zval1=   rz1[0] + 2*rz1[1] +   rz1[2];
732                         zval2=  2*rz2[0]           + 2*rz2[2];
733                         zval3=   rz3[0] + 2*rz3[1] +   rz3[2];
734                         
735                         col= ( 4*rz2[1] - (zval1 + zval2 + zval3)/3 );
736                         if(col<0) col= -col;
737                         
738                         col >>= 5;
739                         if(col > (1<<16)) col= (1<<16);
740                         else col= (R.r.edgeint*col)>>8;
741                         
742                         if(col>0) {
743                                 float fcol;
744                                 
745                                 if(col>255) fcol= 1.0f;
746                                 else fcol= (float)col/255.0f;
747                                 
748                                 if(R.osa)
749                                         *rf+= fcol/(float)R.osa;
750                                 else
751                                         *rf= fcol;
752                         }
753                 }
754                 rz1+= 2;
755                 rz2+= 2;
756                 rz3+= 2;
757                 rf+= 2;
758         }
759         
760         /* shift back zbuf values, we might need it still */
761         rz= pa->rectz;
762         for(y=0; y<pa->recty; y++)
763                 for(x=0; x<pa->rectx; x++, rz++) (*rz)<<= 4;
764         
765 }
766
767 static void reset_sky_speed(RenderPart *pa, RenderLayer *rl)
768 {
769         /* for all pixels with max speed, set to zero */
770         float *fp;
771         int a;
772         
773         fp= RE_RenderLayerGetPass(rl, SCE_PASS_VECTOR);
774         if(fp==NULL) return;
775         
776         for(a= 4*pa->rectx*pa->recty - 1; a>=0; a--)
777                 if(fp[a] == PASS_VECTOR_MAX) fp[a]= 0.0f;
778 }
779
780
781 static unsigned short *make_solid_mask(RenderPart *pa)
782
783         long *rd= pa->rectdaps;
784         unsigned short *solidmask, *sp;
785         int x;
786         
787         if(rd==NULL) return NULL;
788         
789         sp=solidmask= MEM_mallocN(sizeof(short)*pa->rectx*pa->recty, "solidmask");
790         
791         for(x=pa->rectx*pa->recty; x>0; x--, rd++, sp++) {
792                 if(*rd) {
793                         PixStr *ps= (PixStr *)*rd;
794                         
795                         *sp= ps->mask;
796                         for(ps= ps->next; ps; ps= ps->next)
797                                 *sp |= ps->mask;
798                 }
799                 else
800                         *sp= 0;
801         }
802                         
803         return solidmask;
804 }
805
806 static void addAlphaOverFloatMask(float *dest, float *source, unsigned short dmask, unsigned short smask)
807 {
808         unsigned short shared= dmask & smask;
809         float mul= 1.0 - source[3];
810         
811         if(shared) {    /* overlapping masks */
812                 
813                 /* masks differ, we make a mixture of 'add' and 'over' */
814                 if(shared!=dmask) {
815                         float shared_bits= (float)count_mask(shared);           /* alpha over */
816                         float tot_bits= (float)count_mask(smask|dmask);         /* alpha add */
817                         
818                         float add= (tot_bits - shared_bits)/tot_bits;           /* add level */
819                         mul= add + (1.0f-add)*mul;
820                 }
821         }
822         else if(dmask && smask) {
823                 /* works for premul only, of course */
824                 dest[0]+= source[0];
825                 dest[1]+= source[1];
826                 dest[2]+= source[2];
827                 dest[3]+= source[3];
828                 
829                 return;
830         }
831
832         dest[0]= (mul*dest[0]) + source[0];
833         dest[1]= (mul*dest[1]) + source[1];
834         dest[2]= (mul*dest[2]) + source[2];
835         dest[3]= (mul*dest[3]) + source[3];
836 }
837
838
839 /* main call for shading Delta Accum, for OSA */
840 /* supposed to be fully threadable! */
841 void zbufshadeDA_tile(RenderPart *pa)
842 {
843         RenderResult *rr= pa->result;
844         RenderLayer *rl;
845         ListBase psmlist= {NULL, NULL};
846         float *edgerect= NULL;
847         
848         set_part_zbuf_clipflag(pa);
849         
850         /* allocate the necessary buffers */
851                                 /* zbuffer inits these rects */
852         pa->rectp= MEM_mallocN(sizeof(int)*pa->rectx*pa->recty, "rectp");
853         pa->rectz= MEM_mallocN(sizeof(int)*pa->rectx*pa->recty, "rectz");
854         
855         for(rl= rr->layers.first; rl; rl= rl->next) {
856
857                 /* initialize pixelstructs and edge buffer */
858                 addpsmain(&psmlist);
859                 pa->rectdaps= MEM_callocN(sizeof(long)*pa->rectx*pa->recty+4, "zbufDArectd");
860                 
861                 if(rl->layflag & SCE_LAY_EDGE) 
862                         if(R.r.mode & R_EDGE) 
863                                 edgerect= MEM_callocN(sizeof(float)*pa->rectx*pa->recty, "rectedge");
864                 
865                 /* always fill visibility */
866                 for(pa->sample=0; pa->sample<R.osa; pa->sample++) {
867                         zbuffer_solid(pa, rl->lay, rl->layflag);
868                         make_pixelstructs(pa, &psmlist);
869                         
870                         if(rl->layflag & SCE_LAY_EDGE) 
871                                 if(R.r.mode & R_EDGE) 
872                                         edge_enhance_tile(pa, edgerect);
873                         
874                         if(R.test_break()) break; 
875                 }
876                 
877                 /* shades solid */
878                 if(rl->layflag & SCE_LAY_SOLID) 
879                         shadeDA_tile(pa, rl);
880                 
881                 /* lamphalo after solid, before ztra, looks nicest because ztra does own halo */
882                 if(R.flag & R_LAMPHALO)
883                         if(rl->layflag & SCE_LAY_HALO)
884                                 lamphalo_tile(pa, rl);
885                 
886                 /* halo before ztra, because ztra fills in zbuffer now */
887                 if(R.flag & R_HALO)
888                         if(rl->layflag & SCE_LAY_HALO)
889                                 halo_tile(pa, rl->rectf, rl->lay);
890                 
891                 /* transp layer */
892                 if(R.flag & R_ZTRA) {
893                         if(rl->layflag & SCE_LAY_ZTRA) {
894                                 unsigned short *ztramask, *solidmask= NULL; /* 16 bits, MAX_OSA */
895                                 
896                                 /* allocate, but not free here, for asynchronous display of this rect in main thread */
897                                 rl->acolrect= MEM_callocN(4*sizeof(float)*pa->rectx*pa->recty, "alpha layer");
898                                 
899                                 /* swap for live updates, and it is used in zbuf.c!!! */
900                                 SWAP(float *, rl->acolrect, rl->rectf);
901                                 ztramask= zbuffer_transp_shade(pa, rl, rl->rectf);
902                                 SWAP(float *, rl->acolrect, rl->rectf);
903                                 
904                                 /* zbuffer transp only returns ztramask if there's solid rendered */
905                                 if(ztramask)
906                                         solidmask= make_solid_mask(pa);
907                                 
908                                 if(ztramask && solidmask) {
909                                         unsigned short *sps= solidmask, *spz= ztramask;
910                                         unsigned short fullmask= (1<<R.osa)-1;
911                                         float *fcol= rl->rectf; float *acol= rl->acolrect;
912                                         int x;
913                                         
914                                         for(x=pa->rectx*pa->recty; x>0; x--, acol+=4, fcol+=4, sps++, spz++) {
915                                                 if(*sps == fullmask)
916                                                         addAlphaOverFloat(fcol, acol);
917                                                 else
918                                                         addAlphaOverFloatMask(fcol, acol, *sps, *spz);
919                                         }
920                                 }
921                                 else {
922                                         float *fcol= rl->rectf; float *acol= rl->acolrect;
923                                         int x;
924                                         for(x=pa->rectx*pa->recty; x>0; x--, acol+=4, fcol+=4) {
925                                                 addAlphaOverFloat(fcol, acol);
926                                         }
927                                 }
928                                 if(solidmask) MEM_freeN(solidmask);
929                                 if(ztramask) MEM_freeN(ztramask);
930                         }
931                 }
932                 /* sky before edge */
933                 if(rl->layflag & SCE_LAY_SKY)
934                         sky_tile(pa, rl->rectf);
935
936                 /* extra layers */
937                 if(rl->layflag & SCE_LAY_EDGE) 
938                         if(R.r.mode & R_EDGE) 
939                                 edge_enhance_add(pa, rl->rectf, edgerect);
940                 
941                 if(rl->passflag & SCE_PASS_Z)
942                         convert_zbuf_to_distbuf(pa, rl);
943                 
944                 if(rl->passflag & SCE_PASS_VECTOR)
945                         reset_sky_speed(pa, rl);
946                 
947                 /* de-premul alpha */
948                 if(R.r.alphamode & R_ALPHAKEY)
949                         convert_to_key_alpha(pa, rl->rectf);
950                 
951                 /* free stuff within loop! */
952                 MEM_freeN(pa->rectdaps); pa->rectdaps= NULL;
953                 freeps(&psmlist);
954                 
955                 if(edgerect) MEM_freeN(edgerect);
956                 edgerect= NULL;
957         }
958         
959         /* free all */
960         MEM_freeN(pa->rectp); pa->rectp= NULL;
961         MEM_freeN(pa->rectz); pa->rectz= NULL;
962         MEM_freeN(pa->clipflag); pa->clipflag= NULL;
963         
964         /* display active layer */
965         rr->renrect.ymin=rr->renrect.ymax= 0;
966         rr->renlay= render_get_active_layer(&R, rr);
967 }
968
969
970 /* ------------------------------------------------------------------------ */
971
972 /* non OSA case, full tile render */
973 /* supposed to be fully threadable! */
974 void zbufshade_tile(RenderPart *pa)
975 {
976         ShadeSample ssamp;
977         RenderResult *rr= pa->result;
978         RenderLayer *rl;
979         PixStr ps;
980         float *edgerect= NULL;
981         int addpassflag;
982         
983         /* fake pixel struct, to comply to osa render */
984         ps.next= NULL;
985         ps.mask= 0xFFFF;
986         
987         set_part_zbuf_clipflag(pa);
988         
989         /* zbuffer code clears/inits rects */
990         pa->rectp= MEM_mallocN(sizeof(int)*pa->rectx*pa->recty, "rectp");
991         pa->rectz= MEM_mallocN(sizeof(int)*pa->rectx*pa->recty, "rectz");
992
993         for(rl= rr->layers.first; rl; rl= rl->next) {
994                 
995                 /* general shader info, passes */
996                 shade_sample_initialize(&ssamp, pa, rl);
997                 addpassflag= rl->passflag & ~(SCE_PASS_Z|SCE_PASS_COMBINED);
998                 
999                 zbuffer_solid(pa, rl->lay, rl->layflag);
1000                 
1001                 if(!R.test_break()) {   /* NOTE: this if() is not consistant */
1002                         
1003                         /* edges only for solid part, ztransp doesn't support it yet anti-aliased */
1004                         if(rl->layflag & SCE_LAY_EDGE) {
1005                                 if(R.r.mode & R_EDGE) {
1006                                         edgerect= MEM_callocN(sizeof(float)*pa->rectx*pa->recty, "rectedge");
1007                                         edge_enhance_tile(pa, edgerect);
1008                                 }
1009                         }
1010                         
1011                         /* initialize scanline updates for main thread */
1012                         rr->renrect.ymin= 0;
1013                         rr->renlay= rl;
1014                         
1015                         if(rl->layflag & SCE_LAY_SOLID) {
1016                                 float *fcol= rl->rectf;
1017                                 int x, y, *rp= pa->rectp, *rz= pa->rectz, offs=0, seed;
1018                                 
1019                                 /* we set per pixel a fixed seed, for random AO and shadow samples */
1020                                 seed= pa->rectx*pa->disprect.ymin;
1021                                 
1022                                 /* irregular shadowb buffer creation */
1023                                 if(R.r.mode & R_SHADOW)
1024                                         ISB_create(pa, NULL);
1025                                 
1026                                 for(y=pa->disprect.ymin; y<pa->disprect.ymax; y++, rr->renrect.ymax++) {
1027                                         for(x=pa->disprect.xmin; x<pa->disprect.xmax; x++, rz++, rp++, fcol+=4, offs++) {
1028                                                 /* per pixel fixed seed */
1029                                                 BLI_thread_srandom(pa->thread, seed++);
1030                                                 
1031                                                 if(*rp) {
1032                                                         ps.facenr= *rp;
1033                                                         ps.z= *rz;
1034                                                         if(shade_samples(&ssamp, &ps, x, y)) {
1035                                                                 QUATCOPY(fcol, ssamp.shr[0].combined);
1036                                                                 
1037                                                                 /* passes */
1038                                                                 if(addpassflag)
1039                                                                         add_passes(rl, offs, ssamp.shi, ssamp.shr);
1040                                                         }
1041                                                 }
1042                                         }
1043                                         if(y&1)
1044                                                 if(R.test_break()) break; 
1045                                 }
1046                                 
1047                                 if(R.r.mode & R_SHADOW)
1048                                         ISB_free(pa);
1049                         }
1050                         
1051                         /* disable scanline updating */
1052                         rr->renlay= NULL;
1053                 }
1054                 
1055                 /* lamphalo after solid, before ztra, looks nicest because ztra does own halo */
1056                 if(R.flag & R_LAMPHALO)
1057                         if(rl->layflag & SCE_LAY_HALO)
1058                                 lamphalo_tile(pa, rl);
1059                 
1060                 /* halo before ztra, because ztra fills in zbuffer now */
1061                 if(R.flag & R_HALO)
1062                         if(rl->layflag & SCE_LAY_HALO)
1063                                 halo_tile(pa, rl->rectf, rl->lay);
1064                 
1065                 if(R.flag & R_ZTRA) {
1066                         if(rl->layflag & SCE_LAY_ZTRA) {
1067                                 float *fcol, *acol;
1068                                 int x;
1069                                 
1070                                 /* allocate, but not free here, for asynchronous display of this rect in main thread */
1071                                 rl->acolrect= MEM_callocN(4*sizeof(float)*pa->rectx*pa->recty, "alpha layer");
1072                                 
1073                                 /* swap for live updates */
1074                                 SWAP(float *, rl->acolrect, rl->rectf);
1075                                 zbuffer_transp_shade(pa, rl, rl->rectf);
1076                                 SWAP(float *, rl->acolrect, rl->rectf);
1077                                 
1078                                 fcol= rl->rectf; acol= rl->acolrect;
1079                                 for(x=pa->rectx*pa->recty; x>0; x--, acol+=4, fcol+=4) {
1080                                         addAlphaOverFloat(fcol, acol);
1081                                 }
1082                         }
1083                 }
1084                 
1085                 /* sky before edge */
1086                 if(rl->layflag & SCE_LAY_SKY)
1087                         sky_tile(pa, rl->rectf);
1088                 
1089                 if(!R.test_break()) {
1090                         if(rl->layflag & SCE_LAY_EDGE) 
1091                                 if(R.r.mode & R_EDGE)
1092                                         edge_enhance_add(pa, rl->rectf, edgerect);
1093                 }
1094                 
1095                 if(rl->passflag & SCE_PASS_Z)
1096                         convert_zbuf_to_distbuf(pa, rl);
1097                 
1098                 if(rl->passflag & SCE_PASS_VECTOR)
1099                         reset_sky_speed(pa, rl);
1100                 
1101                 /* de-premul alpha */
1102                 if(R.r.alphamode & R_ALPHAKEY)
1103                         convert_to_key_alpha(pa, rl->rectf);
1104                 
1105                 if(edgerect) MEM_freeN(edgerect);
1106                 edgerect= NULL;
1107         }
1108
1109         /* display active layer */
1110         rr->renrect.ymin=rr->renrect.ymax= 0;
1111         rr->renlay= render_get_active_layer(&R, rr);
1112         
1113         MEM_freeN(pa->rectp); pa->rectp= NULL;
1114         MEM_freeN(pa->rectz); pa->rectz= NULL;
1115         MEM_freeN(pa->clipflag); pa->clipflag= NULL;
1116 }
1117
1118 /* SSS preprocess tile render, fully threadable */
1119 typedef struct ZBufSSSHandle {
1120         RenderPart *pa;
1121         ListBase psmlist;
1122         int totps;
1123 } ZBufSSSHandle;
1124
1125 static void addps_sss(void *cb_handle, int facenr, int x, int y, int z)
1126 {
1127         ZBufSSSHandle *handle = cb_handle;
1128         RenderPart *pa= handle->pa;
1129
1130         /* extra border for filter gives double samples on part edges,
1131            don't use those */
1132         if(x<pa->crop || x>=pa->rectx-pa->crop)
1133                 return;
1134         if(y<pa->crop || y>=pa->recty-pa->crop)
1135                 return;
1136         
1137         if(pa->rectall) {
1138                 long *rs= pa->rectall + pa->rectx*y + x;
1139
1140                 addps(&handle->psmlist, rs, facenr, z, 0);
1141                 handle->totps++;
1142         }
1143         if(pa->rectz) {
1144                 int *rz= pa->rectz + pa->rectx*y + x;
1145                 int *rp= pa->rectp + pa->rectx*y + x;
1146
1147                 if(z < *rz) {
1148                         if(*rp == 0)
1149                                 handle->totps++;
1150                         *rz= z;
1151                         *rp= facenr;
1152                 }
1153         }
1154         if(pa->rectbackz) {
1155                 int *rz= pa->rectbackz + pa->rectx*y + x;
1156                 int *rp= pa->rectbackp + pa->rectx*y + x;
1157
1158                 if(z >= *rz) {
1159                         if(*rp == 0)
1160                                 handle->totps++;
1161                         *rz= z;
1162                         *rp= facenr;
1163                 }
1164         }
1165 }
1166
1167 static void shade_sample_sss(ShadeSample *ssamp, Material *mat, VlakRen *vlr, int quad, float x, float y, float z, float *co, float *color, float *area)
1168 {
1169         ShadeInput *shi= ssamp->shi;
1170         ShadeResult shr;
1171         float texfac, orthoarea, nor[3];
1172
1173         /* normal flipping must be disabled to make back scattering work, so that
1174            backside faces actually face any lighting from the back */
1175         shi->puno= 0;
1176         
1177         /* cache for shadow */
1178         shi->samplenr++;
1179         
1180         if(quad) 
1181                 shade_input_set_triangle_i(shi, vlr, 0, 2, 3);
1182         else
1183                 shade_input_set_triangle_i(shi, vlr, 0, 1, 2);
1184
1185         /* we don't want flipped normals, they screw up back scattering */
1186         if(vlr->noflag & R_FLIPPED_NO)
1187                 VecMulf(shi->facenor, -1.0f);
1188
1189         /* center pixel */
1190         x += 0.5f;
1191         y += 0.5f;
1192
1193         /* we estimate the area here using shi->dxco and shi->dyco. we need to
1194            enabled shi->osatex these are filled. we compute two areas, one with
1195            the normal pointed at the camera and one with the original normal, and
1196            then clamp to avoid a too large contribution from a single pixel */
1197         shi->osatex= 1;
1198
1199         VECCOPY(nor, shi->facenor);
1200         calc_view_vector(shi->facenor, x, y);
1201         Normalize(shi->facenor);
1202         shade_input_set_viewco(shi, x, y, z);
1203         orthoarea= VecLength(shi->dxco)*VecLength(shi->dyco);
1204
1205         VECCOPY(shi->facenor, nor);
1206         shade_input_set_viewco(shi, x, y, z);
1207         *area= VecLength(shi->dxco)*VecLength(shi->dyco);
1208         *area= MIN2(*area, 2.0f*orthoarea);
1209
1210         shi->osatex= 0;
1211
1212         shade_input_set_uv(shi);
1213         shade_input_set_normals(shi);
1214
1215         /* not a pretty solution, but fixes common cases */
1216         if(vlr->ob && vlr->ob->transflag & OB_NEG_SCALE) {
1217                 VecMulf(shi->vn, -1.0f);
1218                 VecMulf(shi->vno, -1.0f);
1219         }
1220
1221         /* if nodetree, use the material that we are currently preprocessing
1222            instead of the node material */
1223         if(shi->mat->nodetree && shi->mat->use_nodes)
1224                 shi->mat= mat;
1225
1226         /* init material vars */
1227         // note, keep this synced with render_types.h
1228         memcpy(&shi->r, &shi->mat->r, 23*sizeof(float));
1229         shi->har= shi->mat->har;
1230         
1231         /* render */
1232         shade_input_set_shade_texco(shi);
1233         
1234         shade_samples_do_AO(ssamp);
1235         shade_material_loop(shi, &shr);
1236         
1237         VECCOPY(co, shi->co);
1238         VECCOPY(color, shr.combined);
1239
1240         /* texture blending */
1241         texfac= shi->mat->sss_texfac;
1242
1243         if(texfac == 0.0f) {
1244                 if(shr.col[0]!=0.0f) color[0] /= shr.col[0];
1245                 if(shr.col[1]!=0.0f) color[1] /= shr.col[1];
1246                 if(shr.col[2]!=0.0f) color[2] /= shr.col[2];
1247         }
1248         else if(texfac != 1.0f) {
1249                 if(shr.col[0]!=0.0f) color[0] *= pow(shr.col[0], texfac)/shr.col[0];
1250                 if(shr.col[1]!=0.0f) color[1] *= pow(shr.col[1], texfac)/shr.col[1];
1251                 if(shr.col[2]!=0.0f) color[2] *= pow(shr.col[2], texfac)/shr.col[2];
1252         }
1253 }
1254
1255 static void zbufshade_sss_free(RenderPart *pa)
1256 {
1257         MEM_freeN(pa->clipflag); pa->clipflag= NULL;
1258 #if 0
1259         MEM_freeN(pa->rectall); pa->rectall= NULL;
1260         freeps(&handle.psmlist);
1261 #else
1262         MEM_freeN(pa->rectz); pa->rectz= NULL;
1263         MEM_freeN(pa->rectp); pa->rectp= NULL;
1264         MEM_freeN(pa->rectbackz); pa->rectbackz= NULL;
1265         MEM_freeN(pa->rectbackp); pa->rectbackp= NULL;
1266 #endif
1267 }
1268
1269 void zbufshade_sss_tile(RenderPart *pa)
1270 {
1271         Render *re= &R;
1272         ShadeSample ssamp;
1273         ZBufSSSHandle handle;
1274         RenderResult *rr= pa->result;
1275         RenderLayer *rl= rr->layers.first;
1276         VlakRen *vlr;
1277         Material *mat= re->sss_mat;
1278         float (*co)[3], (*color)[3], *area, *fcol= rl->rectf;
1279         int x, y, seed, quad, totpoint, display = !(re->r.scemode & R_PREVIEWBUTS);
1280         int *rz, *rp, *rbz, *rbp;
1281 #if 0
1282         PixStr *ps;
1283         long *rs;
1284         int z;
1285 #endif
1286
1287         set_part_zbuf_clipflag(pa);
1288
1289         /* setup pixelstr list and buffer for zbuffering */
1290         handle.pa= pa;
1291         handle.totps= 0;
1292
1293 #if 0
1294         handle.psmlist.first= handle.psmlist.last= NULL;
1295         addpsmain(&handle.psmlist);
1296
1297         pa->rectall= MEM_callocN(sizeof(long)*pa->rectx*pa->recty+4, "rectall");
1298 #else
1299         pa->rectp= MEM_mallocN(sizeof(int)*pa->rectx*pa->recty, "rectp");
1300         pa->rectz= MEM_mallocN(sizeof(int)*pa->rectx*pa->recty, "rectz");
1301         pa->rectbackp= MEM_mallocN(sizeof(int)*pa->rectx*pa->recty, "rectbackp");
1302         pa->rectbackz= MEM_mallocN(sizeof(int)*pa->rectx*pa->recty, "rectbackz");
1303 #endif
1304
1305         /* create the pixelstrs to be used later */
1306         zbuffer_sss(pa, rl->lay, &handle, addps_sss);
1307
1308         if(handle.totps==0) {
1309                 zbufshade_sss_free(pa);
1310                 return;
1311         }
1312
1313         co= MEM_mallocN(sizeof(float)*3*handle.totps, "SSSCo");
1314         color= MEM_mallocN(sizeof(float)*3*handle.totps, "SSSColor");
1315         area= MEM_mallocN(sizeof(float)*handle.totps, "SSSArea");
1316
1317 #if 0
1318         /* create ISB (does not work currently!) */
1319         if(re->r.mode & R_SHADOW)
1320                 ISB_create(pa, NULL);
1321 #endif
1322
1323         /* setup shade sample with correct passes */
1324         memset(&ssamp, 0, sizeof(ssamp));
1325         shade_sample_initialize(&ssamp, pa, rl);
1326         ssamp.shi[0].passflag= SCE_PASS_DIFFUSE|SCE_PASS_AO|SCE_PASS_RADIO;
1327         ssamp.shi[0].passflag |= SCE_PASS_RGBA;
1328         ssamp.shi[0].combinedflag= ~(SCE_PASS_SPEC);
1329         ssamp.tot= 1;
1330
1331         if(display) {
1332                 /* initialize scanline updates for main thread */
1333                 rr->renrect.ymin= 0;
1334                 rr->renlay= rl;
1335         }
1336         
1337         seed= pa->rectx*pa->disprect.ymin;
1338 #if 0
1339         rs= pa->rectall;
1340 #else
1341         rz= pa->rectz;
1342         rp= pa->rectp;
1343         rbz= pa->rectbackz;
1344         rbp= pa->rectbackp;
1345 #endif
1346         totpoint= 0;
1347
1348         for(y=pa->disprect.ymin; y<pa->disprect.ymax; y++, rr->renrect.ymax++) {
1349                 for(x=pa->disprect.xmin; x<pa->disprect.xmax; x++, fcol+=4) {
1350                         /* per pixel fixed seed */
1351                         BLI_thread_srandom(pa->thread, seed++);
1352                         
1353 #if 0
1354                         if(rs) {
1355                                 /* for each sample in this pixel, shade it */
1356                                 for(ps=(PixStr*)*rs; ps; ps=ps->next) {
1357                                         vlr= RE_findOrAddVlak(re, (ps->facenr-1) & RE_QUAD_MASK);
1358                                         quad= (ps->facenr & RE_QUAD_OFFS);
1359                                         z= ps->z;
1360
1361                                         shade_sample_sss(&ssamp, mat, vlr, quad, x, y, z,
1362                                                 co[totpoint], color[totpoint], &area[totpoint]);
1363
1364                                         totpoint++;
1365
1366                                         VECADD(fcol, fcol, color);
1367                                         fcol[3]= 1.0f;
1368                                 }
1369
1370                                 rs++;
1371                         }
1372 #else
1373                         if(rp) {
1374                                 if(*rp != 0) {
1375                                         /* shade front */
1376                                         vlr= RE_findOrAddVlak(re, (*rp-1) & RE_QUAD_MASK);
1377                                         quad= ((*rp) & RE_QUAD_OFFS);
1378
1379                                         shade_sample_sss(&ssamp, mat, vlr, quad, x, y, *rz,
1380                                                 co[totpoint], color[totpoint], &area[totpoint]);
1381                                         
1382                                         VECADD(fcol, fcol, color[totpoint]);
1383                                         fcol[3]= 1.0f;
1384                                         totpoint++;
1385                                 }
1386
1387                                 rp++; rz++;
1388                         }
1389
1390                         if(rbp) {
1391                                 if(*rbp != 0 && *rbp != *(rp-1)) {
1392                                         /* shade back */
1393                                         vlr= RE_findOrAddVlak(re, (*rbp-1) & RE_QUAD_MASK);
1394                                         quad= ((*rbp) & RE_QUAD_OFFS);
1395
1396                                         shade_sample_sss(&ssamp, mat, vlr, quad, x, y, *rbz,
1397                                                 co[totpoint], color[totpoint], &area[totpoint]);
1398                                         
1399                                         /* to indicate this is a back sample */
1400                                         area[totpoint]= -area[totpoint];
1401
1402                                         VECADD(fcol, fcol, color[totpoint]);
1403                                         fcol[3]= 1.0f;
1404                                         totpoint++;
1405                                 }
1406
1407                                 rbz++; rbp++;
1408                         }
1409 #endif
1410                 }
1411
1412                 if(y&1)
1413                         if(re->test_break()) break; 
1414         }
1415
1416         /* note: after adding we do not free these arrays, sss keeps them */
1417         if(totpoint > 0) {
1418                 sss_add_points(re, co, color, area, totpoint);
1419         }
1420         else {
1421                 MEM_freeN(co);
1422                 MEM_freeN(color);
1423                 MEM_freeN(area);
1424         }
1425         
1426 #if 0
1427         if(re->r.mode & R_SHADOW)
1428                 ISB_free(pa);
1429 #endif
1430                 
1431         if(display) {
1432                 /* display active layer */
1433                 rr->renrect.ymin=rr->renrect.ymax= 0;
1434                 rr->renlay= render_get_active_layer(&R, rr);
1435         }
1436         
1437         zbufshade_sss_free(pa);
1438 }
1439
1440 /* ------------------------------------------------------------------------ */
1441
1442 static void renderhalo_post(RenderResult *rr, float *rectf, HaloRen *har)       /* postprocess version */
1443 {
1444         float dist, xsq, ysq, xn, yn, colf[4], *rectft, *rtf;
1445         float haloxs, haloys;
1446         int minx, maxx, miny, maxy, x, y;
1447
1448         /* calculate the disprect mapped coordinate for halo. note: rectx is disprect corrected */
1449         haloxs= har->xs - R.disprect.xmin;
1450         haloys= har->ys - R.disprect.ymin;
1451         
1452         har->miny= miny= haloys - har->rad/R.ycor;
1453         har->maxy= maxy= haloys + har->rad/R.ycor;
1454         
1455         if(maxy<0);
1456         else if(rr->recty<miny);
1457         else {
1458                 minx= floor(haloxs-har->rad);
1459                 maxx= ceil(haloxs+har->rad);
1460                         
1461                 if(maxx<0);
1462                 else if(rr->rectx<minx);
1463                 else {
1464                 
1465                         if(minx<0) minx= 0;
1466                         if(maxx>=rr->rectx) maxx= rr->rectx-1;
1467                         if(miny<0) miny= 0;
1468                         if(maxy>rr->recty) maxy= rr->recty;
1469         
1470                         rectft= rectf+ 4*rr->rectx*miny;
1471
1472                         for(y=miny; y<maxy; y++) {
1473         
1474                                 rtf= rectft+4*minx;
1475                                 
1476                                 yn= (y - haloys)*R.ycor;
1477                                 ysq= yn*yn;
1478                                 
1479                                 for(x=minx; x<=maxx; x++) {
1480                                         xn= x - haloxs;
1481                                         xsq= xn*xn;
1482                                         dist= xsq+ysq;
1483                                         if(dist<har->radsq) {
1484                                                 
1485                                                 shadeHaloFloat(har, colf, 0x7FFFFF, dist, xn, yn, har->flarec);
1486                                                 addalphaAddfacFloat(rtf, colf, har->add);
1487                                         }
1488                                         rtf+=4;
1489                                 }
1490         
1491                                 rectft+= 4*rr->rectx;
1492                                 
1493                                 if(R.test_break()) break; 
1494                         }
1495                 }
1496         }
1497
1498 /* ------------------------------------------------------------------------ */
1499
1500 static void renderflare(RenderResult *rr, float *rectf, HaloRen *har)
1501 {
1502         extern float hashvectf[];
1503         HaloRen fla;
1504         Material *ma;
1505         float *rc, rad, alfa, visifac, vec[3];
1506         int b, type;
1507         
1508         fla= *har;
1509         fla.linec= fla.ringc= fla.flarec= 0;
1510         
1511         rad= har->rad;
1512         alfa= har->alfa;
1513         
1514         visifac= R.ycor*(har->pixels);
1515         /* all radials added / r^3  == 1.0f! */
1516         visifac /= (har->rad*har->rad*har->rad);
1517         visifac*= visifac;
1518
1519         ma= har->mat;
1520         
1521         /* first halo: just do */
1522         
1523         har->rad= rad*ma->flaresize*visifac;
1524         har->radsq= har->rad*har->rad;
1525         har->zs= fla.zs= 0;
1526         
1527         har->alfa= alfa*visifac;
1528
1529         renderhalo_post(rr, rectf, har);
1530         
1531         /* next halo's: the flares */
1532         rc= hashvectf + ma->seed2;
1533         
1534         for(b=1; b<har->flarec; b++) {
1535                 
1536                 fla.r= fabs(rc[0]);
1537                 fla.g= fabs(rc[1]);
1538                 fla.b= fabs(rc[2]);
1539                 fla.alfa= ma->flareboost*fabs(alfa*visifac*rc[3]);
1540                 fla.hard= 20.0f + fabs(70*rc[7]);
1541                 fla.tex= 0;
1542                 
1543                 type= (int)(fabs(3.9*rc[6]));
1544
1545                 fla.rad= ma->subsize*sqrt(fabs(2.0f*har->rad*rc[4]));
1546                 
1547                 if(type==3) {
1548                         fla.rad*= 3.0f;
1549                         fla.rad+= R.rectx/10;
1550                 }
1551                 
1552                 fla.radsq= fla.rad*fla.rad;
1553                 
1554                 vec[0]= 1.4*rc[5]*(har->xs-R.winx/2);
1555                 vec[1]= 1.4*rc[5]*(har->ys-R.winy/2);
1556                 vec[2]= 32.0f*sqrt(vec[0]*vec[0] + vec[1]*vec[1] + 1.0f);
1557                 
1558                 fla.xs= R.winx/2 + vec[0] + (1.2+rc[8])*R.rectx*vec[0]/vec[2];
1559                 fla.ys= R.winy/2 + vec[1] + (1.2+rc[8])*R.rectx*vec[1]/vec[2];
1560
1561                 if(R.flag & R_SEC_FIELD) {
1562                         if(R.r.mode & R_ODDFIELD) fla.ys += 0.5;
1563                         else fla.ys -= 0.5;
1564                 }
1565                 if(type & 1) fla.type= HA_FLARECIRC;
1566                 else fla.type= 0;
1567                 renderhalo_post(rr, rectf, &fla);
1568
1569                 fla.alfa*= 0.5;
1570                 if(type & 2) fla.type= HA_FLARECIRC;
1571                 else fla.type= 0;
1572                 renderhalo_post(rr, rectf, &fla);
1573                 
1574                 rc+= 7;
1575         }
1576 }
1577
1578 /* needs recode... integrate this better! */
1579 void add_halo_flare(Render *re)
1580 {
1581         RenderResult *rr= re->result;
1582         RenderLayer *rl;
1583         HaloRen *har = NULL;
1584         int a, mode, do_draw=0;
1585         
1586         /* for now, we get the first renderlayer in list with halos set */
1587         for(rl= rr->layers.first; rl; rl= rl->next)
1588                 if(rl->layflag & SCE_LAY_HALO)
1589                         break;
1590
1591         if(rl==NULL || rl->rectf==NULL)
1592                 return;
1593         
1594         mode= R.r.mode;
1595         R.r.mode &= ~R_PANORAMA;
1596         
1597         project_renderdata(&R, projectverto, 0, 0);
1598         
1599         for(a=0; a<R.tothalo; a++) {
1600                 if((a & 255)==0) har= R.bloha[a>>8];
1601                 else har++;
1602                 
1603                 if(har->flarec) {
1604                         do_draw= 1;
1605                         renderflare(rr, rl->rectf, har);
1606                 }
1607         }
1608
1609         if(do_draw) {
1610                 /* weak... the display callback wants an active renderlayer pointer... */
1611                 rr->renlay= rl;
1612                 re->display_draw(rr, NULL);
1613         }
1614         
1615         R.r.mode= mode; 
1616 }
1617
1618 /* ************************* used for shaded view ************************ */
1619
1620 /* if *re, then initialize, otherwise execute */
1621 void RE_shade_external(Render *re, ShadeInput *shi, ShadeResult *shr)
1622 {
1623         static VlakRen vlr;
1624         
1625         /* init */
1626         if(re) {
1627                 R= *re;
1628                 
1629                 /* fake render face */
1630                 memset(&vlr, 0, sizeof(VlakRen));
1631                 vlr.lay= -1;
1632                 
1633                 return;
1634         }
1635         shi->vlr= &vlr;
1636         
1637         if(shi->mat->nodetree && shi->mat->use_nodes)
1638                 ntreeShaderExecTree(shi->mat->nodetree, shi, shr);
1639         else {
1640                 /* copy all relevant material vars, note, keep this synced with render_types.h */
1641                 memcpy(&shi->r, &shi->mat->r, 23*sizeof(float));
1642                 shi->har= shi->mat->har;
1643                 
1644                 shade_material_loop(shi, shr);
1645         }
1646 }
1647
1648 /* ************************* bake ************************ */
1649
1650 #define FTOCHAR(val) val<=0.0f?0: (val>=1.0f?255: (char)(255.0f*val))
1651
1652 typedef struct BakeShade {
1653         ShadeSample ssamp;
1654         VlakRen *vlr;
1655         
1656         ZSpan *zspan;
1657         Image *ima;
1658         ImBuf *ibuf;
1659         
1660         int rectx, recty, quad, type, vdone, ready;
1661
1662         float dir[3];
1663         Object *actob;
1664         
1665         unsigned int *rect;
1666         float *rect_float;
1667 } BakeShade;
1668
1669 static void bake_set_shade_input(VlakRen *vlr, ShadeInput *shi, int quad, int isect, int x, int y, float u, float v)
1670 {
1671         if(isect) {
1672                 /* raytrace intersection with different u,v than scanconvert */
1673                 if(vlr->v4) {
1674                         if(quad)
1675                                 shade_input_set_triangle_i(shi, vlr, 2, 1, 3);
1676                         else
1677                                 shade_input_set_triangle_i(shi, vlr, 0, 1, 3);
1678                 }
1679                 else
1680                         shade_input_set_triangle_i(shi, vlr, 0, 1, 2);
1681         }
1682         else {
1683                 /* regular scanconvert */
1684                 if(quad) 
1685                         shade_input_set_triangle_i(shi, vlr, 0, 2, 3);
1686                 else
1687                         shade_input_set_triangle_i(shi, vlr, 0, 1, 2);
1688         }
1689                 
1690         /* set up view vector */
1691         VECCOPY(shi->view, shi->co);
1692         Normalize(shi->view);
1693         
1694         /* no face normal flip */
1695         shi->puno= 0;
1696         
1697         /* cache for shadow */
1698         shi->samplenr++;
1699         
1700         shi->u= -u;
1701         shi->v= -v;
1702         shi->xs= x;
1703         shi->ys= y;
1704         
1705         shade_input_set_normals(shi);
1706 }
1707
1708 static void bake_shade(void *handle, Object *ob, VlakRen *vlr, ShadeInput *shi, int quad, int x, int y, float u, float v, float *tvn, float *ttang)
1709 {
1710         BakeShade *bs= handle;
1711         ShadeSample *ssamp= &bs->ssamp;
1712         ShadeResult shr;
1713         
1714         /* init material vars */
1715         memcpy(&shi->r, &shi->mat->r, 23*sizeof(float));        // note, keep this synced with render_types.h
1716         shi->har= shi->mat->har;
1717         
1718         if(bs->type==RE_BAKE_AO) {
1719                 ambient_occlusion(shi);
1720                 ambient_occlusion_to_diffuse(shi, shr.combined);
1721         }
1722         else {
1723                 shade_input_set_shade_texco(shi);
1724                 
1725                 shade_samples_do_AO(ssamp);
1726                 
1727                 if(shi->mat->nodetree && shi->mat->use_nodes) {
1728                         ntreeShaderExecTree(shi->mat->nodetree, shi, &shr);
1729                         shi->mat= vlr->mat;             /* shi->mat is being set in nodetree */
1730                 }
1731                 else
1732                         shade_material_loop(shi, &shr);
1733                 
1734                 if(bs->type==RE_BAKE_NORMALS) {
1735                         float nor[3];
1736
1737                         VECCOPY(nor, shi->vn);
1738
1739                         if(R.r.bake_normal_space == R_BAKE_SPACE_CAMERA);
1740                         else if(R.r.bake_normal_space == R_BAKE_SPACE_TANGENT) {
1741                                 float mat[3][3], imat[3][3];
1742
1743                                 /* bitangent */
1744                                 if(tvn && ttang) {
1745                                         VECCOPY(mat[0], ttang);
1746                                         Crossf(mat[1], tvn, ttang);
1747                                         VECCOPY(mat[2], tvn);
1748                                 }
1749                                 else {
1750                                         VECCOPY(mat[0], shi->tang);
1751                                         Crossf(mat[1], shi->vn, shi->tang);
1752                                         VECCOPY(mat[2], shi->vn);
1753                                 }
1754
1755                                 Mat3Inv(imat, mat);
1756                                 Mat3MulVecfl(imat, nor);
1757                         }
1758                         else if(R.r.bake_normal_space == R_BAKE_SPACE_OBJECT)
1759                                 Mat4Mul3Vecfl(ob->imat, nor); /* ob->imat includes viewinv! */
1760                         else if(R.r.bake_normal_space == R_BAKE_SPACE_WORLD)
1761                                 Mat4Mul3Vecfl(R.viewinv, nor);
1762
1763                         Normalize(nor); /* in case object has scaling */
1764
1765                         shr.combined[0]= nor[0]/2.0f + 0.5f;
1766                         shr.combined[1]= 0.5f - nor[1]/2.0f;
1767                         shr.combined[2]= nor[2]/2.0f + 0.5f;
1768                 }
1769                 else if(bs->type==RE_BAKE_TEXTURE) {
1770                         shr.combined[0]= shi->r;
1771                         shr.combined[1]= shi->g;
1772                         shr.combined[2]= shi->b;
1773                 }
1774         }
1775         
1776         if(bs->rect) {
1777                 char *col= (char *)(bs->rect + bs->rectx*y + x);
1778                 col[0]= FTOCHAR(shr.combined[0]);
1779                 col[1]= FTOCHAR(shr.combined[1]);
1780                 col[2]= FTOCHAR(shr.combined[2]);
1781                 col[3]= 255;
1782         }
1783         else {
1784                 float *col= bs->rect_float + 4*(bs->rectx*y + x);
1785                 VECCOPY(col, shr.combined);
1786                 col[3]= 1.0f;
1787         }
1788 }
1789
1790 static int bake_check_intersect(Isect *is, RayFace *face)
1791 {
1792         VlakRen *vlr = (VlakRen*)face;
1793         BakeShade *bs = (BakeShade*)is->userdata;
1794         
1795         /* no direction checking for now, doesn't always improve the result
1796          * (INPR(vlr->n, bs->dir) > 0.0f); */
1797
1798         return (vlr->ob != bs->actob);
1799 }
1800
1801 static int bake_intersect_tree(RayTree* raytree, Isect* isect, float *dir, float sign, float *hitco)
1802 {
1803         float maxdist;
1804         int hit;
1805
1806         /* might be useful to make a user setting for maxsize*/
1807         if(R.r.bake_maxdist > 0.0f)
1808                 maxdist= R.r.bake_maxdist;
1809         else
1810                 maxdist= RE_ray_tree_max_size(R.raytree);
1811
1812         isect->end[0] = isect->start[0] + dir[0]*maxdist*sign;
1813         isect->end[1] = isect->start[1] + dir[1]*maxdist*sign;
1814         isect->end[2] = isect->start[2] + dir[2]*maxdist*sign;
1815
1816         hit = RE_ray_tree_intersect_check(R.raytree, isect, bake_check_intersect);
1817         if(hit) {
1818                 hitco[0] = isect->start[0] + isect->labda*isect->vec[0];
1819                 hitco[1] = isect->start[1] + isect->labda*isect->vec[1];
1820                 hitco[2] = isect->start[2] + isect->labda*isect->vec[2];
1821         }
1822
1823         return hit;
1824 }
1825
1826 static void do_bake_shade(void *handle, int x, int y, float u, float v)
1827 {
1828         BakeShade *bs= handle;
1829         VlakRen *vlr= bs->vlr;
1830         Object *ob= vlr->ob;
1831         float l, *v1, *v2, *v3, tvn[3], ttang[3];
1832         int quad;
1833         ShadeSample *ssamp= &bs->ssamp;
1834         ShadeInput *shi= ssamp->shi;
1835         
1836         /* fast threadsafe break test */
1837         if(R.test_break())
1838                 return;
1839         
1840         /* setup render coordinates */
1841         if(bs->quad) {
1842                 v1= vlr->v1->co;
1843                 v2= vlr->v3->co;
1844                 v3= vlr->v4->co;
1845         }
1846         else {
1847                 v1= vlr->v1->co;
1848                 v2= vlr->v2->co;
1849                 v3= vlr->v3->co;
1850         }
1851         
1852         /* renderco */
1853         l= 1.0f-u-v;
1854         
1855         shi->co[0]= l*v3[0]+u*v1[0]+v*v2[0];
1856         shi->co[1]= l*v3[1]+u*v1[1]+v*v2[1];
1857         shi->co[2]= l*v3[2]+u*v1[2]+v*v2[2];
1858         
1859         quad= bs->quad;
1860         bake_set_shade_input(vlr, shi, quad, 0, x, y, u, v);
1861
1862         if(bs->type==RE_BAKE_NORMALS && R.r.bake_normal_space==R_BAKE_SPACE_TANGENT) {
1863                 shade_input_set_shade_texco(shi);
1864                 VECCOPY(tvn, shi->vn);
1865                 VECCOPY(ttang, shi->tang);
1866         }
1867
1868         /* if we are doing selected to active baking, find point on other face */
1869         if(bs->actob) {
1870                 Isect isec, minisec;
1871                 float co[3], minco[3];
1872                 int hit, sign;
1873                 
1874                 /* intersect with ray going forward and backward*/
1875                 hit= 0;
1876                 memset(&minisec, 0, sizeof(minisec));
1877                 minco[0]= minco[1]= minco[2]= 0.0f;
1878
1879                 VECCOPY(bs->dir, shi->vn);
1880
1881                 for(sign=-1; sign<=1; sign+=2) {
1882                         memset(&isec, 0, sizeof(isec));
1883                         VECCOPY(isec.start, shi->co);
1884                         isec.mode= RE_RAY_MIRROR;
1885                         isec.faceorig= (RayFace*)vlr;
1886                         isec.userdata= bs;
1887
1888                         if(bake_intersect_tree(R.raytree, &isec, shi->vn, sign, co)) {
1889                                 if(!hit || VecLenf(shi->co, co) < VecLenf(shi->co, minco)) {
1890                                         minisec= isec;
1891                                         VECCOPY(minco, co);
1892                                         hit= 1;
1893                                 }
1894                         }
1895                 }
1896
1897                 /* if hit, we shade from the new point, otherwise from point one starting face */
1898                 if(hit) {
1899                         vlr= (VlakRen*)minisec.face;
1900                         quad= (minisec.isect == 2);
1901                         VECCOPY(shi->co, minco);
1902
1903                         u= -minisec.u;
1904                         v= -minisec.v;
1905                         bake_set_shade_input(vlr, shi, quad, 1, x, y, u, v);
1906                 }
1907         }
1908
1909         if(bs->type==RE_BAKE_NORMALS && R.r.bake_normal_space==R_BAKE_SPACE_TANGENT)
1910                 bake_shade(handle, ob, vlr, shi, quad, x, y, u, v, tvn, ttang);
1911         else
1912                 bake_shade(handle, ob, vlr, shi, quad, x, y, u, v, 0, 0);
1913 }
1914
1915 static int get_next_bake_face(BakeShade *bs)
1916 {
1917         VlakRen *vlr;
1918         MTFace *tface;
1919         static int v= 0, vdone= 0;
1920         
1921         if(bs==NULL) {
1922                 vlr= NULL;
1923                 v= vdone= 0;
1924                 return 0;
1925         }
1926         
1927         BLI_lock_thread(LOCK_CUSTOM1);  
1928         
1929         for(; v<R.totvlak; v++) {
1930                 vlr= RE_findOrAddVlak(&R, v);
1931                 
1932                 if((bs->actob && bs->actob == vlr->ob) || (!bs->actob && (vlr->ob->flag & SELECT))) {
1933                         tface= RE_vlakren_get_tface(&R, vlr, 0, NULL, 0);
1934
1935                         if(tface && tface->tpage) {
1936                                 Image *ima= tface->tpage;
1937                                 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
1938                                 float vec[4]= {0.0f, 0.0f, 0.0f, 0.0f};
1939                                 
1940                                 if(ibuf==NULL)
1941                                         continue;
1942                                 
1943                                 if(ibuf->rect==NULL && ibuf->rect_float==NULL)
1944                                         continue;
1945                                 
1946                                 if(ibuf->rect_float && !(ibuf->channels==0 || ibuf->channels==4))
1947                                         continue;
1948                                 
1949                                 /* find the image for the first time? */
1950                                 if(ima->id.flag & LIB_DOIT) {
1951                                         ima->id.flag &= ~LIB_DOIT;
1952                                         
1953                                         /* we either fill in float or char, this ensures things go fine */
1954                                         if(ibuf->rect_float)
1955                                                 imb_freerectImBuf(ibuf);
1956                                         /* clear image */
1957                                         if(R.r.bake_flag & R_BAKE_CLEAR)
1958                                                 IMB_rectfill(ibuf, vec);
1959                                 
1960                                         /* might be read by UI to set active image for display */
1961                                         R.bakebuf= ima;
1962                                 }                               
1963                                 
1964                                 bs->vlr= vlr;
1965                                 
1966                                 bs->vdone++;    /* only for error message if nothing was rendered */
1967                                 v++;
1968                                 
1969                                 BLI_unlock_thread(LOCK_CUSTOM1);
1970                                 return 1;
1971                         }
1972                 }
1973         }
1974         
1975         BLI_unlock_thread(LOCK_CUSTOM1);
1976         return 0;
1977 }
1978
1979 /* already have tested for tface and ima and zspan */
1980 static void shade_tface(BakeShade *bs)
1981 {
1982         VlakRen *vlr= bs->vlr;
1983         MTFace *tface= RE_vlakren_get_tface(&R, vlr, 0, NULL, 0);
1984         Image *ima= tface->tpage;
1985         float vec[4][2];
1986         int a, i1, i2, i3;
1987         
1988         /* check valid zspan */
1989         if(ima!=bs->ima) {
1990                 bs->ima= ima;
1991                 bs->ibuf= BKE_image_get_ibuf(ima, NULL);
1992                 /* note, these calls only free/fill contents of zspan struct, not zspan itself */
1993                 zbuf_free_span(bs->zspan);
1994                 zbuf_alloc_span(bs->zspan, bs->ibuf->x, bs->ibuf->y);
1995         }                               
1996         
1997         bs->rectx= bs->ibuf->x;
1998         bs->recty= bs->ibuf->y;
1999         bs->rect= bs->ibuf->rect;
2000         bs->rect_float= bs->ibuf->rect_float;
2001         bs->quad= 0;
2002         
2003         /* get pixel level vertex coordinates */
2004         for(a=0; a<4; a++) {
2005                 vec[a][0]= tface->uv[a][0]*(float)bs->rectx - 0.5f;
2006                 vec[a][1]= tface->uv[a][1]*(float)bs->recty - 0.5f;
2007         }
2008         
2009         /* UV indices have to be corrected for possible quad->tria splits */
2010         i1= 0; i2= 1; i3= 2;
2011         vlr_set_uv_indices(vlr, &i1, &i2, &i3);
2012         zspan_scanconvert(bs->zspan, bs, vec[i1], vec[i2], vec[i3], do_bake_shade);
2013         
2014         if(vlr->v4) {
2015                 bs->quad= 1;
2016                 zspan_scanconvert(bs->zspan, bs, vec[0], vec[2], vec[3], do_bake_shade);
2017         }
2018 }
2019
2020 static void *do_bake_thread(void *bs_v)
2021 {
2022         BakeShade *bs= bs_v;
2023         
2024         while(get_next_bake_face(bs)) {
2025                 shade_tface(bs);
2026                 
2027                 /* fast threadsafe break test */
2028                 if(R.test_break())
2029                         break;
2030         }
2031         bs->ready= 1;
2032         
2033         return NULL;
2034 }
2035
2036 /* using object selection tags, the faces with UV maps get baked */
2037 /* render should have been setup */
2038 /* returns 0 if nothing was handled */
2039 int RE_bake_shade_all_selected(Render *re, int type, Object *actob)
2040 {
2041         BakeShade handles[BLENDER_MAX_THREADS];
2042         ListBase threads;
2043         Image *ima;
2044         int a, vdone=0;
2045         
2046         /* initialize static vars */
2047         get_next_bake_face(NULL);
2048         
2049         /* baker uses this flag to detect if image was initialized */
2050         for(ima= G.main->image.first; ima; ima= ima->id.next)
2051                 ima->id.flag |= LIB_DOIT;
2052         
2053         /* initialize render global */
2054         R= *re;
2055         R.bakebuf= NULL;
2056         
2057         BLI_init_threads(&threads, do_bake_thread, re->r.threads);
2058
2059         /* get the threads running */
2060         for(a=0; a<re->r.threads; a++) {
2061                 /* set defaults in handles */
2062                 memset(&handles[a], 0, sizeof(BakeShade));
2063                 
2064                 handles[a].ssamp.shi[0].lay= re->scene->lay;
2065                 handles[a].ssamp.shi[0].passflag= SCE_PASS_COMBINED;
2066                 handles[a].ssamp.shi[0].combinedflag= ~(SCE_PASS_SPEC);
2067                 handles[a].ssamp.shi[0].thread= a;
2068                 handles[a].ssamp.tot= 1;
2069                 
2070                 handles[a].type= type;
2071                 handles[a].actob= actob;
2072                 handles[a].zspan= MEM_callocN(sizeof(ZSpan), "zspan for bake");
2073                 
2074                 BLI_insert_thread(&threads, &handles[a]);
2075         }
2076         
2077         /* wait for everything to be done */
2078         a= 0;
2079         while(a!=re->r.threads) {
2080                 
2081                 PIL_sleep_ms(50);
2082
2083                 for(a=0; a<re->r.threads; a++)
2084                         if(handles[a].ready==0)
2085                                 break;
2086         }
2087         
2088         /* filter images */
2089         for(ima= G.main->image.first; ima; ima= ima->id.next) {
2090                 if((ima->id.flag & LIB_DOIT)==0) {
2091                         ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
2092                         for(a=0; a<re->r.bake_filter; a++)
2093                                 IMB_filter_extend(ibuf);
2094                         ibuf->userflags |= IB_BITMAPDIRTY;
2095                 }
2096         }
2097         
2098         /* calculate return value */
2099         for(a=0; a<re->r.threads; a++) {
2100                 vdone+= handles[a].vdone;
2101                 
2102                 zbuf_free_span(handles[a].zspan);
2103                 MEM_freeN(handles[a].zspan);
2104         }
2105         
2106         BLI_end_threads(&threads);
2107         return vdone;
2108 }
2109
2110 struct Image *RE_bake_shade_get_image(void)
2111 {
2112         return R.bakebuf;
2113 }
2114