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