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