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