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