10201087f2d27a0dc72021e29d5b1c6a2fbbfcec
[blender.git] / source / blender / render / intern / source / rendercore.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributors: Hos, Robert Wenzlaff.
24  * Contributors: 2004/2005/2006 Blender Foundation, full recode
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /* system includes */
30 #include <stdio.h>
31 #include <math.h>
32 #include <string.h>
33
34 /* External modules: */
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_arithb.h"
38 #include "BLI_blenlib.h"
39 #include "BLI_jitter.h"
40 #include "BLI_rand.h"
41 #include "BLI_threads.h"
42
43 #include "BKE_utildefines.h"
44
45 #include "DNA_image_types.h"
46 #include "DNA_lamp_types.h"
47 #include "DNA_material_types.h"
48 #include "DNA_meshdata_types.h"
49
50 #include "BKE_global.h"
51 #include "BKE_image.h"
52 #include "BKE_main.h"
53 #include "BKE_node.h"
54 #include "BKE_texture.h"
55
56 #include "IMB_imbuf_types.h"
57 #include "IMB_imbuf.h"
58
59 /* local include */
60 #include "renderpipeline.h"
61 #include "render_types.h"
62 #include "renderdatabase.h"
63 #include "pixelblending.h"
64 #include "pixelshading.h"
65 #include "shadbuf.h"
66 #include "shading.h"
67 #include "zbuf.h"
68
69 #include "PIL_time.h"
70
71 /* own include */
72 #include "rendercore.h"
73
74
75 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
76 /* defined in pipeline.c, is hardcopy of active dynamic allocated Render */
77 /* only to be used here in this file, it's for speed */
78 extern struct Render R;
79 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
80
81 /* x and y are current pixels in rect to be rendered */
82 /* do not normalize! */
83 void calc_view_vector(float *view, float x, float y)
84 {
85
86         view[2]= -ABS(R.clipsta);
87         
88         if(R.r.mode & R_ORTHO) {
89                 view[0]= view[1]= 0.0f;
90         }
91         else {
92                 
93                 if(R.r.mode & R_PANORAMA)
94                         x-= R.panodxp;
95                 
96                 /* move x and y to real viewplane coords */
97                 x= (x/(float)R.winx);
98                 view[0]= R.viewplane.xmin + x*(R.viewplane.xmax - R.viewplane.xmin);
99                 
100                 y= (y/(float)R.winy);
101                 view[1]= R.viewplane.ymin + y*(R.viewplane.ymax - R.viewplane.ymin);
102                 
103 //              if(R.flag & R_SEC_FIELD) {
104 //                      if(R.r.mode & R_ODDFIELD) view[1]= (y+R.ystart)*R.ycor;
105 //                      else view[1]= (y+R.ystart+1.0)*R.ycor;
106 //              }
107 //              else view[1]= (y+R.ystart+R.bluroffsy+0.5)*R.ycor;
108         
109                 if(R.r.mode & R_PANORAMA) {
110                         float u= view[0] + R.panodxv; float v= view[2];
111                         view[0]= R.panoco*u + R.panosi*v;
112                         view[2]= -R.panosi*u + R.panoco*v;
113                 }
114         }
115 }
116
117 void calc_renderco_ortho(float *co, float x, float y, int z)
118 {
119         /* x and y 3d coordinate can be derived from pixel coord and winmat */
120         float fx= 2.0f/(R.winx*R.winmat[0][0]);
121         float fy= 2.0f/(R.winy*R.winmat[1][1]);
122         float zco;
123         
124         co[0]= (x - 0.5f*R.winx)*fx - R.winmat[3][0]/R.winmat[0][0];
125         co[1]= (y - 0.5f*R.winy)*fy - R.winmat[3][1]/R.winmat[1][1];
126         
127         zco= ((float)z)/2147483647.0f;
128         co[2]= R.winmat[3][2]/( R.winmat[2][3]*zco - R.winmat[2][2] );
129 }
130
131 void calc_renderco_zbuf(float *co, float *view, int z)
132 {
133         float fac, zco;
134         
135         /* inverse of zbuf calc: zbuf = MAXZ*hoco_z/hoco_w */
136         zco= ((float)z)/2147483647.0f;
137         co[2]= R.winmat[3][2]/( R.winmat[2][3]*zco - R.winmat[2][2] );
138
139         fac= co[2]/view[2];
140         co[0]= fac*view[0];
141         co[1]= fac*view[1];
142 }
143
144 /* also used in zbuf.c and shadbuf.c */
145 int count_mask(unsigned short mask)
146 {
147         if(R.samples)
148                 return (R.samples->cmask[mask & 255]+R.samples->cmask[mask>>8]);
149         return 0;
150 }
151
152 static int calchalo_z(HaloRen *har, int zz)
153 {
154         
155         if(har->type & HA_ONLYSKY) {
156                 if(zz!=0x7FFFFFFF) zz= - 0x7FFFFF;
157         }
158         else {
159                 zz= (zz>>8);
160         }
161         return zz;
162 }
163
164 static void halo_pixelstruct(HaloRen *har, float *rb, float dist, float xn, float yn, PixStr *ps)
165 {
166         float col[4], accol[4];
167         int amount, amountm, zz, flarec;
168         
169         amount= 0;
170         accol[0]=accol[1]=accol[2]=accol[3]= 0.0f;
171         flarec= har->flarec;
172         
173         while(ps) {
174                 amountm= count_mask(ps->mask);
175                 amount+= amountm;
176                 
177                 zz= calchalo_z(har, ps->z);
178                 if(zz> har->zs) {
179                         float fac;
180                         
181                         shadeHaloFloat(har, col, zz, dist, xn, yn, flarec);
182                         fac= ((float)amountm)/(float)R.osa;
183                         accol[0]+= fac*col[0];
184                         accol[1]+= fac*col[1];
185                         accol[2]+= fac*col[2];
186                         accol[3]+= fac*col[3];
187                         flarec= 0;
188                 }
189                 
190                 ps= ps->next;
191         }
192         /* now do the sky sub-pixels */
193         amount= R.osa-amount;
194         if(amount) {
195                 float fac;
196
197                 shadeHaloFloat(har, col, 0x7FFFFF, dist, xn, yn, flarec);
198                 fac= ((float)amount)/(float)R.osa;
199                 accol[0]+= fac*col[0];
200                 accol[1]+= fac*col[1];
201                 accol[2]+= fac*col[2];
202                 accol[3]+= fac*col[3];
203         }
204         col[0]= accol[0];
205         col[1]= accol[1];
206         col[2]= accol[2];
207         col[3]= accol[3];
208         
209         addalphaAddfacFloat(rb, col, har->add);
210         
211 }
212
213 static void halo_tile(RenderPart *pa, float *pass, unsigned int lay)
214 {
215         HaloRen *har = NULL;
216         rcti disprect= pa->disprect, testrect= pa->disprect;
217         float dist, xsq, ysq, xn, yn, *rb;
218         float col[4];
219         long *rd= NULL;
220         int a, *rz, zz, y;
221         short minx, maxx, miny, maxy, x;
222
223         /* we don't render halos in the cropped area, gives errors in flare counter */
224         if(pa->crop) {
225                 testrect.xmin+= pa->crop;
226                 testrect.xmax-= pa->crop;
227                 testrect.ymin+= pa->crop;
228                 testrect.ymax-= pa->crop;
229         }
230         
231         for(a=0; a<R.tothalo; a++) {
232                 if((a & 255)==0)
233                         har= R.bloha[a>>8];
234                 else har++;
235
236                 /* layer test, clip halo with y */
237                 if((har->lay & lay)==0);
238                 else if(testrect.ymin > har->maxy);
239                 else if(testrect.ymax < har->miny);
240                 else {
241                         
242                         minx= floor(har->xs-har->rad);
243                         maxx= ceil(har->xs+har->rad);
244                         
245                         if(testrect.xmin > maxx);
246                         else if(testrect.xmax < minx);
247                         else {
248                                 
249                                 minx= MAX2(minx, testrect.xmin);
250                                 maxx= MIN2(maxx, testrect.xmax);
251                         
252                                 miny= MAX2(har->miny, testrect.ymin);
253                                 maxy= MIN2(har->maxy, testrect.ymax);
254                         
255                                 for(y=miny; y<maxy; y++) {
256                                         int rectofs= (y-disprect.ymin)*pa->rectx + (minx - disprect.xmin);
257                                         rb= pass + 4*rectofs;
258                                         rz= pa->rectz + rectofs;
259                                         
260                                         if(pa->rectdaps)
261                                                 rd= pa->rectdaps + rectofs;
262                                         
263                                         yn= (y-har->ys)*R.ycor;
264                                         ysq= yn*yn;
265                                         
266                                         for(x=minx; x<maxx; x++, rb+=4, rz++) {
267                                                 xn= x- har->xs;
268                                                 xsq= xn*xn;
269                                                 dist= xsq+ysq;
270                                                 if(dist<har->radsq) {
271                                                         if(rd && *rd) {
272                                                                 halo_pixelstruct(har, rb, dist, xn, yn, (PixStr *)*rd);
273                                                         }
274                                                         else {
275                                                                 zz= calchalo_z(har, *rz);
276                                                                 if(zz> har->zs) {
277                                                                         shadeHaloFloat(har, col, zz, dist, xn, yn, har->flarec);
278                                                                         addalphaAddfacFloat(rb, col, har->add);
279                                                                 }
280                                                         }
281                                                 }
282                                                 if(rd) rd++;
283                                         }
284                                 }
285                         }
286                 }
287                 if(R.test_break() ) break; 
288         }
289 }
290
291 static void lamphalo_tile(RenderPart *pa, RenderLayer *rl)
292 {
293         ShadeInput shi;
294         float *pass= rl->rectf;
295         float fac;
296         long *rd= pa->rectdaps;
297         int x, y, *rz= pa->rectz;
298         
299         shade_input_initialize(&shi, pa, rl, 0);
300         
301         for(y=pa->disprect.ymin; y<pa->disprect.ymax; y++) {
302                 for(x=pa->disprect.xmin; x<pa->disprect.xmax; x++, rz++, pass+=4) {
303                         
304                         calc_view_vector(shi.view, x, y);
305                         
306                         if(rd && *rd) {
307                                 PixStr *ps= (PixStr *)*rd;
308                                 int samp, totsamp= 0;
309                                 
310                                 while(ps) {
311                                         if(R.r.mode & R_ORTHO)
312                                                 calc_renderco_ortho(shi.co, (float)x, (float)y, ps->z);
313                                         else
314                                                 calc_renderco_zbuf(shi.co, shi.view, ps->z);
315                                         
316                                         totsamp+= samp= count_mask(ps->mask);
317                                         fac= ((float)samp)/(float)R.osa;
318                                         renderspothalo(&shi, pass, fac);
319                                         ps= ps->next;
320                                 }
321                                 if(totsamp<R.osa) {
322                                         fac= ((float)R.osa-totsamp)/(float)R.osa;
323                                         shi.co[2]= 0.0f;
324                                         renderspothalo(&shi, pass, fac);
325                                 }
326                         }
327                         else {
328                                 if(R.r.mode & R_ORTHO)
329                                         calc_renderco_ortho(shi.co, (float)x, (float)y, *rz);
330                                 else
331                                         calc_renderco_zbuf(shi.co, shi.view, *rz);
332                                 
333                                 renderspothalo(&shi, pass, 1.0f);
334                         }
335                         
336                         if(rd) rd++;
337                 }
338                 if(y&1)
339                         if(R.test_break()) break; 
340         }
341 }                               
342
343
344 /* ********************* MAINLOOPS ******************** */
345
346 /* osa version */
347 static void add_filt_passes(RenderLayer *rl, int curmask, int rectx, int offset, ShadeInput *shi, ShadeResult *shr)
348 {
349         RenderPass *rpass;
350         
351         for(rpass= rl->passes.first; rpass; rpass= rpass->next) {
352                 float *fp, *col= NULL;
353                 int pixsize= 3;
354                 
355                 switch(rpass->passtype) {
356                         case SCE_PASS_RGBA:
357                                 col= shr->col;
358                                 pixsize= 4;
359                                 break;
360                         case SCE_PASS_DIFFUSE:
361                                 col= shr->diff;
362                                 break;
363                         case SCE_PASS_SPEC:
364                                 col= shr->spec;
365                                 break;
366                         case SCE_PASS_SHADOW:
367                                 col= shr->shad;
368                                 break;
369                         case SCE_PASS_AO:
370                                 col= shr->ao;
371                                 break;
372                         case SCE_PASS_REFLECT:
373                                 col= shr->refl;
374                                 break;
375                         case SCE_PASS_REFRACT:
376                                 col= shr->refr;
377                                 break;
378                         case SCE_PASS_RADIO:
379                                 col= shr->rad;
380                                 break;
381                         case SCE_PASS_NORMAL:
382                                 col= shr->nor;
383                                 break;
384                         case SCE_PASS_UV:
385                                 /* box filter only, gauss will screwup UV too much */
386                                 if(shi->totuv) {
387                                         float mult= (float)count_mask(curmask)/(float)R.osa;
388                                         fp= rpass->rect + 3*offset;
389                                         fp[0]+= mult*(0.5f + 0.5f*shi->uv[0].uv[0]);
390                                         fp[1]+= mult*(0.5f + 0.5f*shi->uv[0].uv[1]);
391                                         fp[2]+= mult;
392                                 }
393                                 break;
394                         case SCE_PASS_INDEXOB:
395                                 /* no filter */
396                                 if(shi->vlr) {
397                                         fp= rpass->rect + offset;
398                                         if(*fp==0.0f)
399                                                 *fp= (float)shi->vlr->ob->index;
400                                 }
401                                 break;
402                         case SCE_PASS_VECTOR:
403                         {
404                                 /* add minimum speed in pixel, no filter */
405                                 fp= rpass->rect + 4*offset;
406                                 if( (ABS(shr->winspeed[0]) + ABS(shr->winspeed[1]))< (ABS(fp[0]) + ABS(fp[1])) ) {
407                                         fp[0]= shr->winspeed[0];
408                                         fp[1]= shr->winspeed[1];
409                                 }
410                                 if( (ABS(shr->winspeed[2]) + ABS(shr->winspeed[3]))< (ABS(fp[2]) + ABS(fp[3])) ) {
411                                         fp[2]= shr->winspeed[2];
412                                         fp[3]= shr->winspeed[3];
413                                 }
414                         }
415                                 break;
416                 }
417                 if(col) {
418                         fp= rpass->rect + pixsize*offset;
419                         add_filt_fmask_pixsize(curmask, col, fp, rectx, pixsize);
420                 }
421         }
422 }
423
424 /* non-osa version */
425 static void add_passes(RenderLayer *rl, int offset, ShadeInput *shi, ShadeResult *shr)
426 {
427         RenderPass *rpass;
428         
429         for(rpass= rl->passes.first; rpass; rpass= rpass->next) {
430                 float *fp, *col= NULL, uvcol[3];
431                 int a, pixsize= 3;
432                 
433                 switch(rpass->passtype) {
434                         case SCE_PASS_RGBA:
435                                 col= shr->col;
436                                 pixsize= 4;
437                                 break;
438                         case SCE_PASS_DIFFUSE:
439                                 col= shr->diff;
440                                 break;
441                         case SCE_PASS_SPEC:
442                                 col= shr->spec;
443                                 break;
444                         case SCE_PASS_SHADOW:
445                                 col= shr->shad;
446                                 break;
447                         case SCE_PASS_AO:
448                                 col= shr->ao;
449                                 break;
450                         case SCE_PASS_REFLECT:
451                                 col= shr->refl;
452                                 break;
453                         case SCE_PASS_REFRACT:
454                                 col= shr->refr;
455                                 break;
456                         case SCE_PASS_RADIO:
457                                 col= shr->rad;
458                                 break;
459                         case SCE_PASS_NORMAL:
460                                 col= shr->nor;
461                                 break;
462                         case SCE_PASS_UV:
463                                 if(shi->totuv) {
464                                         uvcol[0]= 0.5f + 0.5f*shi->uv[0].uv[0];
465                                         uvcol[1]= 0.5f + 0.5f*shi->uv[0].uv[1];
466                                         uvcol[2]= 1.0f;
467                                         col= uvcol;
468                                 }
469                                 break;
470                         case SCE_PASS_VECTOR:
471                                 col= shr->winspeed;
472                                 pixsize= 4;
473                                 break;
474                         case SCE_PASS_INDEXOB:
475                                 if(shi->vlr) {
476                                         fp= rpass->rect + offset;
477                                         *fp= (float)shi->vlr->ob->index;
478                                 }
479                                 break;
480                 }
481                 if(col) {
482                         fp= rpass->rect + pixsize*offset;
483                         for(a=0; a<pixsize; a++)
484                                 fp[a]= col[a];
485                 }
486         }
487 }
488
489 /* only do sky, is default in the solid layer (shade_tile) btw */
490 static void sky_tile(RenderPart *pa, float *pass)
491 {
492         float col[4];
493         int x, y;
494         
495         if(R.r.alphamode!=R_ADDSKY)
496                 return;
497         
498         for(y=pa->disprect.ymin; y<pa->disprect.ymax; y++) {
499                 for(x=pa->disprect.xmin; x<pa->disprect.xmax; x++, pass+=4) {
500                         if(pass[3]<1.0f) {
501                                 if(pass[3]==0.0f)
502                                         shadeSkyPixel(pass, x, y);
503                                 else {
504                                         shadeSkyPixel(col, x, y);
505                                         addAlphaOverFloat(col, pass);
506                                         QUATCOPY(pass, col);
507                                 }
508                         }
509                 }
510                 
511                 if(y&1)
512                         if(R.test_break()) break; 
513         }
514 }
515
516 static void shadeDA_tile(RenderPart *pa, RenderLayer *rl)
517 {
518         RenderResult *rr= pa->result;
519         ShadeSample ssamp;
520         float *fcol, *rf, *rectf= rl->rectf;
521         long *rd, *rectdaps= pa->rectdaps;
522         int samp;
523         int x, y, seed, crop=0, offs=0, od, addpassflag;
524         
525         if(R.test_break()) return; 
526         
527         /* irregular shadowb buffer creation */
528         if(R.r.mode & R_SHADOW)
529                 ISB_create(pa, NULL);
530         
531         /* we set per pixel a fixed seed, for random AO and shadow samples */
532         seed= pa->rectx*pa->disprect.ymin;
533         
534         /* general shader info, passes */
535         shade_sample_initialize(&ssamp, pa, rl);
536         addpassflag= rl->passflag & ~(SCE_PASS_Z|SCE_PASS_COMBINED);
537                                 
538         /* filtered render, for now we assume only 1 filter size */
539         if(pa->crop) {
540                 crop= 1;
541                 rectf+= 4*(pa->rectx + 1);
542                 rectdaps+= pa->rectx + 1;
543                 offs= pa->rectx + 1;
544         }
545         
546         /* scanline updates have to be 2 lines behind */
547         rr->renrect.ymin= 0;
548         rr->renrect.ymax= -2*crop;
549         rr->renlay= rl;
550                                 
551         for(y=pa->disprect.ymin+crop; y<pa->disprect.ymax-crop; y++, rr->renrect.ymax++) {
552                 rf= rectf;
553                 rd= rectdaps;
554                 od= offs;
555                 
556                 for(x=pa->disprect.xmin+crop; x<pa->disprect.xmax-crop; x++, rd++, rf+=4, od++) {
557                         BLI_thread_srandom(pa->thread, seed++);
558                         
559                         if(*rd) {
560                                 if(shade_samples(&ssamp, (PixStr *)(*rd), x, y)) {
561                                         for(samp=0; samp<ssamp.tot; samp++) {
562                                                 
563                                                 fcol= ssamp.shr[samp].combined;
564                                                 add_filt_fmask(ssamp.shi[samp].mask, fcol, rf, pa->rectx);
565                                                 
566                                                 if(addpassflag)
567                                                         add_filt_passes(rl, ssamp.shi[samp].mask, pa->rectx, od, &ssamp.shi[samp], &ssamp.shr[samp]);
568                                         }
569                                 }
570                         }
571                 }
572                 
573                 rectf+= 4*pa->rectx;
574                 rectdaps+= pa->rectx;
575                 offs+= pa->rectx;
576                 
577                 if(y&1) if(R.test_break()) break; 
578         }
579         
580         /* disable scanline updating */
581         rr->renlay= NULL;
582         
583         if(R.r.mode & R_SHADOW)
584                 ISB_free(pa);
585 }
586
587 /* ************* pixel struct ******** */
588
589
590 static PixStrMain *addpsmain(ListBase *lb)
591 {
592         PixStrMain *psm;
593         
594         psm= (PixStrMain *)MEM_mallocN(sizeof(PixStrMain),"pixstrMain");
595         BLI_addtail(lb, psm);
596         
597         psm->ps= (PixStr *)MEM_mallocN(4096*sizeof(PixStr),"pixstr");
598         psm->counter= 0;
599         
600         return psm;
601 }
602
603 static void freeps(ListBase *lb)
604 {
605         PixStrMain *psm, *psmnext;
606         
607         for(psm= lb->first; psm; psm= psmnext) {
608                 psmnext= psm->next;
609                 if(psm->ps)
610                         MEM_freeN(psm->ps);
611                 MEM_freeN(psm);
612         }
613         lb->first= lb->last= NULL;
614 }
615
616 static void addps(ListBase *lb, long *rd, int facenr, int z, unsigned short mask)
617 {
618         PixStrMain *psm;
619         PixStr *ps, *last= NULL;
620         
621         if(*rd) {       
622                 ps= (PixStr *)(*rd);
623                 
624                 while(ps) {
625                         if( ps->facenr == facenr ) {
626                                 ps->mask |= mask;
627                                 return;
628                         }
629                         last= ps;
630                         ps= ps->next;
631                 }
632         }
633         
634         /* make new PS (pixel struct) */
635         psm= lb->last;
636         
637         if(psm->counter==4095)
638                 psm= addpsmain(lb);
639         
640         ps= psm->ps + psm->counter++;
641         
642         if(last) last->next= ps;
643         else *rd= (long)ps;
644         
645         ps->next= NULL;
646         ps->facenr= facenr;
647         ps->z= z;
648         ps->mask = mask;
649         ps->shadfac= 0;
650 }
651
652 static void make_pixelstructs(RenderPart *pa, ListBase *lb)
653 {
654         long *rd= pa->rectdaps;
655         int *rp= pa->rectp;
656         int *rz= pa->rectz;
657         int x, y;
658         int mask= 1<<pa->sample;
659         
660         for(y=0; y<pa->recty; y++) {
661                 for(x=0; x<pa->rectx; x++, rd++, rp++) {
662                         if(*rp) {
663                                 addps(lb, rd, *rp, *(rz+x), mask);
664                         }
665                 }
666                 rz+= pa->rectx;
667         }
668 }
669
670 static void edge_enhance_add(RenderPart *pa, float *rectf, float *arect)
671 {
672         float addcol[4];
673         int pix;
674         
675         if(arect==NULL)
676                 return;
677         
678         for(pix= pa->rectx*pa->recty; pix>0; pix--, arect++, rectf+=4) {
679                 if(*arect != 0.0f) {
680                         addcol[0]= *arect * R.r.edgeR;
681                         addcol[1]= *arect * R.r.edgeG;
682                         addcol[2]= *arect * R.r.edgeB;
683                         addcol[3]= *arect;
684                         addAlphaOverFloat(rectf, addcol);
685                 }
686         }
687 }
688
689
690 static void convert_to_key_alpha(RenderPart *pa, float *rectf)
691 {
692         int y;
693         
694         for(y= pa->rectx*pa->recty; y>0; y--, rectf+=4) {
695                 if(rectf[3] >= 1.0f);
696                 else if(rectf[3] > 0.0f) {
697                         rectf[0] /= rectf[3];
698                         rectf[1] /= rectf[3];
699                         rectf[2] /= rectf[3];
700                 }
701         }
702 }
703
704 /* adds only alpha values */
705 static void edge_enhance_tile(RenderPart *pa, float *rectf)     
706 {
707         /* use zbuffer to define edges, add it to the image */
708         int y, x, col, *rz, *rz1, *rz2, *rz3;
709         int zval1, zval2, zval3;
710         float *rf;
711         
712         /* shift values in zbuffer 4 to the right (anti overflows), for filter we need multiplying with 12 max */
713         rz= pa->rectz;
714         if(rz==NULL) return;
715         
716         for(y=0; y<pa->recty; y++)
717                 for(x=0; x<pa->rectx; x++, rz++) (*rz)>>= 4;
718         
719         rz1= pa->rectz;
720         rz2= rz1+pa->rectx;
721         rz3= rz2+pa->rectx;
722         
723         rf= rectf+pa->rectx+1;
724         
725         for(y=0; y<pa->recty-2; y++) {
726                 for(x=0; x<pa->rectx-2; x++, rz1++, rz2++, rz3++, rf++) {
727                         
728                         /* prevent overflow with sky z values */
729                         zval1=   rz1[0] + 2*rz1[1] +   rz1[2];
730                         zval2=  2*rz2[0]           + 2*rz2[2];
731                         zval3=   rz3[0] + 2*rz3[1] +   rz3[2];
732                         
733                         col= ( 4*rz2[1] - (zval1 + zval2 + zval3)/3 );
734                         if(col<0) col= -col;
735                         
736                         col >>= 5;
737                         if(col > (1<<16)) col= (1<<16);
738                         else col= (R.r.edgeint*col)>>8;
739                         
740                         if(col>0) {
741                                 float fcol;
742                                 
743                                 if(col>255) fcol= 1.0f;
744                                 else fcol= (float)col/255.0f;
745                                 
746                                 if(R.osa)
747                                         *rf+= fcol/(float)R.osa;
748                                 else
749                                         *rf= fcol;
750                         }
751                 }
752                 rz1+= 2;
753                 rz2+= 2;
754                 rz3+= 2;
755                 rf+= 2;
756         }
757         
758         /* shift back zbuf values, we might need it still */
759         rz= pa->rectz;
760         for(y=0; y<pa->recty; y++)
761                 for(x=0; x<pa->rectx; x++, rz++) (*rz)<<= 4;
762         
763 }
764
765 static void reset_sky_speed(RenderPart *pa, RenderLayer *rl)
766 {
767         /* for all pixels with max speed, set to zero */
768         float *fp;
769         int a;
770         
771         fp= RE_RenderLayerGetPass(rl, SCE_PASS_VECTOR);
772         if(fp==NULL) return;
773         
774         for(a= 4*pa->rectx*pa->recty; a>0; a--)
775                 if(fp[a] == PASS_VECTOR_MAX) fp[a]= 0.0f;
776 }
777
778
779 static unsigned short *make_solid_mask(RenderPart *pa)
780
781         long *rd= pa->rectdaps;
782         unsigned short *solidmask, *sp;
783         int x;
784         
785         if(rd==NULL) return NULL;
786         
787         sp=solidmask= MEM_mallocN(sizeof(short)*pa->rectx*pa->recty, "solidmask");
788         
789         for(x=pa->rectx*pa->recty; x>0; x--, rd++, sp++) {
790                 if(*rd) {
791                         PixStr *ps= (PixStr *)*rd;
792                         
793                         *sp= ps->mask;
794                         for(ps= ps->next; ps; ps= ps->next)
795                                 *sp |= ps->mask;
796                 }
797                 else
798                         *sp= 0;
799         }
800                         
801         return solidmask;
802 }
803
804 static void addAlphaOverFloatMask(float *dest, float *source, unsigned short dmask, unsigned short smask)
805 {
806         unsigned short shared= dmask & smask;
807         float mul= 1.0 - source[3];
808         
809         if(shared) {    /* overlapping masks */
810                 
811                 /* masks differ, we make a mixture of 'add' and 'over' */
812                 if(shared!=dmask) {
813                         float shared_bits= (float)count_mask(shared);           /* alpha over */
814                         float tot_bits= (float)count_mask(smask|dmask);         /* alpha add */
815                         
816                         float add= (tot_bits - shared_bits)/tot_bits;           /* add level */
817                         mul= add + (1.0f-add)*mul;
818                 }
819         }
820         else if(dmask && smask) {
821                 /* works for premul only, of course */
822                 dest[0]+= source[0];
823                 dest[1]+= source[1];
824                 dest[2]+= source[2];
825                 dest[3]+= source[3];
826                 
827                 return;
828         }
829
830         dest[0]= (mul*dest[0]) + source[0];
831         dest[1]= (mul*dest[1]) + source[1];
832         dest[2]= (mul*dest[2]) + source[2];
833         dest[3]= (mul*dest[3]) + source[3];
834 }
835
836
837 /* main call for shading Delta Accum, for OSA */
838 /* supposed to be fully threadable! */
839 void zbufshadeDA_tile(RenderPart *pa)
840 {
841         RenderResult *rr= pa->result;
842         RenderLayer *rl;
843         ListBase psmlist= {NULL, NULL};
844         float *edgerect= NULL;
845         
846         set_part_zbuf_clipflag(pa);
847         
848         /* allocate the necessary buffers */
849                                 /* zbuffer inits these rects */
850         pa->rectp= MEM_mallocN(sizeof(int)*pa->rectx*pa->recty, "rectp");
851         pa->rectz= MEM_mallocN(sizeof(int)*pa->rectx*pa->recty, "rectz");
852         
853         for(rl= rr->layers.first; rl; rl= rl->next) {
854
855                 /* initialize pixelstructs and edge buffer */
856                 addpsmain(&psmlist);
857                 pa->rectdaps= MEM_callocN(sizeof(long)*pa->rectx*pa->recty+4, "zbufDArectd");
858                 
859                 if(rl->layflag & SCE_LAY_EDGE) 
860                         if(R.r.mode & R_EDGE) 
861                                 edgerect= MEM_callocN(sizeof(float)*pa->rectx*pa->recty, "rectedge");
862                 
863                 /* always fill visibility */
864                 for(pa->sample=0; pa->sample<R.osa; pa->sample++) {
865                         zbuffer_solid(pa, rl->lay, rl->layflag);
866                         make_pixelstructs(pa, &psmlist);
867                         
868                         if(rl->layflag & SCE_LAY_EDGE) 
869                                 if(R.r.mode & R_EDGE) 
870                                         edge_enhance_tile(pa, edgerect);
871                         
872                         if(R.test_break()) break; 
873                 }
874                 
875                 /* shades solid */
876                 if(rl->layflag & SCE_LAY_SOLID) 
877                         shadeDA_tile(pa, rl);
878                 
879                 /* lamphalo after solid, before ztra, looks nicest because ztra does own halo */
880                 if(R.flag & R_LAMPHALO)
881                         if(rl->layflag & SCE_LAY_HALO)
882                                 lamphalo_tile(pa, rl);
883                 
884                 /* halo before ztra, because ztra fills in zbuffer now */
885                 if(R.flag & R_HALO)
886                         if(rl->layflag & SCE_LAY_HALO)
887                                 halo_tile(pa, rl->rectf, rl->lay);
888                 
889                 /* transp layer */
890                 if(R.flag & R_ZTRA) {
891                         if(rl->layflag & SCE_LAY_ZTRA) {
892                                 unsigned short *ztramask, *solidmask= NULL; /* 16 bits, MAX_OSA */
893                                 
894                                 /* allocate, but not free here, for asynchronous display of this rect in main thread */
895                                 rl->acolrect= MEM_callocN(4*sizeof(float)*pa->rectx*pa->recty, "alpha layer");
896                                 
897                                 /* swap for live updates, and it is used in zbuf.c!!! */
898                                 SWAP(float *, rl->acolrect, rl->rectf);
899                                 ztramask= zbuffer_transp_shade(pa, rl, rl->rectf);
900                                 SWAP(float *, rl->acolrect, rl->rectf);
901                                 
902                                 /* zbuffer transp only returns ztramask if there's solid rendered */
903                                 if(ztramask)
904                                         solidmask= make_solid_mask(pa);
905                                 
906                                 if(ztramask && solidmask) {
907                                         unsigned short *sps= solidmask, *spz= ztramask;
908                                         unsigned short fullmask= (1<<R.osa)-1;
909                                         float *fcol= rl->rectf; float *acol= rl->acolrect;
910                                         int x;
911                                         
912                                         for(x=pa->rectx*pa->recty; x>0; x--, acol+=4, fcol+=4, sps++, spz++) {
913                                                 if(*sps == fullmask)
914                                                         addAlphaOverFloat(fcol, acol);
915                                                 else
916                                                         addAlphaOverFloatMask(fcol, acol, *sps, *spz);
917                                         }
918                                 }
919                                 else {
920                                         float *fcol= rl->rectf; float *acol= rl->acolrect;
921                                         int x;
922                                         for(x=pa->rectx*pa->recty; x>0; x--, acol+=4, fcol+=4) {
923                                                 addAlphaOverFloat(fcol, acol);
924                                         }
925                                 }
926                                 if(solidmask) MEM_freeN(solidmask);
927                                 if(ztramask) MEM_freeN(ztramask);
928                         }
929                 }
930                 /* sky before edge */
931                 if(rl->layflag & SCE_LAY_SKY)
932                         sky_tile(pa, rl->rectf);
933
934                 /* extra layers */
935                 if(rl->layflag & SCE_LAY_EDGE) 
936                         if(R.r.mode & R_EDGE) 
937                                 edge_enhance_add(pa, rl->rectf, edgerect);
938                 
939                 if(rl->passflag & SCE_PASS_Z)
940                         convert_zbuf_to_distbuf(pa, rl);
941                 
942                 if(rl->passflag & SCE_PASS_VECTOR)
943                         reset_sky_speed(pa, rl);
944                 
945                 /* de-premul alpha */
946                 if(R.r.alphamode & R_ALPHAKEY)
947                         convert_to_key_alpha(pa, rl->rectf);
948                 
949                 /* free stuff within loop! */
950                 MEM_freeN(pa->rectdaps); pa->rectdaps= NULL;
951                 freeps(&psmlist);
952                 
953                 if(edgerect) MEM_freeN(edgerect);
954                 edgerect= NULL;
955         }
956         
957         /* free all */
958         MEM_freeN(pa->rectp); pa->rectp= NULL;
959         MEM_freeN(pa->rectz); pa->rectz= NULL;
960         MEM_freeN(pa->clipflag); pa->clipflag= NULL;
961         
962         /* display active layer */
963         rr->renrect.ymin=rr->renrect.ymax= 0;
964         rr->renlay= render_get_active_layer(&R, rr);
965 }
966
967
968 /* ------------------------------------------------------------------------ */
969
970 /* non OSA case, full tile render */
971 /* supposed to be fully threadable! */
972 void zbufshade_tile(RenderPart *pa)
973 {
974         ShadeSample ssamp;
975         RenderResult *rr= pa->result;
976         RenderLayer *rl;
977         PixStr ps;
978         float *edgerect= NULL;
979         int addpassflag;
980         
981         /* fake pixel struct, to comply to osa render */
982         ps.next= NULL;
983         ps.mask= 0xFFFF;
984         
985         set_part_zbuf_clipflag(pa);
986         
987         /* zbuffer code clears/inits rects */
988         pa->rectp= MEM_mallocN(sizeof(int)*pa->rectx*pa->recty, "rectp");
989         pa->rectz= MEM_mallocN(sizeof(int)*pa->rectx*pa->recty, "rectz");
990
991         for(rl= rr->layers.first; rl; rl= rl->next) {
992                 
993                 /* general shader info, passes */
994                 shade_sample_initialize(&ssamp, pa, rl);
995                 addpassflag= rl->passflag & ~(SCE_PASS_Z|SCE_PASS_COMBINED);
996                 
997                 zbuffer_solid(pa, rl->lay, rl->layflag);
998                 
999                 if(!R.test_break()) {   /* NOTE: this if() is not consistant */
1000                         
1001                         /* edges only for solid part, ztransp doesn't support it yet anti-aliased */
1002                         if(rl->layflag & SCE_LAY_EDGE) {
1003                                 if(R.r.mode & R_EDGE) {
1004                                         edgerect= MEM_callocN(sizeof(float)*pa->rectx*pa->recty, "rectedge");
1005                                         edge_enhance_tile(pa, edgerect);
1006                                 }
1007                         }
1008                         
1009                         /* initialize scanline updates for main thread */
1010                         rr->renrect.ymin= 0;
1011                         rr->renlay= rl;
1012                         
1013                         if(rl->layflag & SCE_LAY_SOLID) {
1014                                 float *fcol= rl->rectf;
1015                                 int x, y, *rp= pa->rectp, *rz= pa->rectz, offs=0, seed;
1016                                 
1017                                 /* we set per pixel a fixed seed, for random AO and shadow samples */
1018                                 seed= pa->rectx*pa->disprect.ymin;
1019                                 
1020                                 /* irregular shadowb buffer creation */
1021                                 if(R.r.mode & R_SHADOW)
1022                                         ISB_create(pa, NULL);
1023                                 
1024                                 for(y=pa->disprect.ymin; y<pa->disprect.ymax; y++, rr->renrect.ymax++) {
1025                                         for(x=pa->disprect.xmin; x<pa->disprect.xmax; x++, rz++, rp++, fcol+=4, offs++) {
1026                                                 /* per pixel fixed seed */
1027                                                 BLI_thread_srandom(pa->thread, seed++);
1028                                                 
1029                                                 if(*rp) {
1030                                                         ps.facenr= *rp;
1031                                                         ps.z= *rz;
1032                                                         if(shade_samples(&ssamp, &ps, x, y)) {
1033                                                                 QUATCOPY(fcol, ssamp.shr[0].combined);
1034                                                                 
1035                                                                 /* passes */
1036                                                                 if(addpassflag)
1037                                                                         add_passes(rl, offs, ssamp.shi, ssamp.shr);
1038                                                         }
1039                                                 }
1040                                         }
1041                                         if(y&1)
1042                                                 if(R.test_break()) break; 
1043                                 }
1044                                 
1045                                 if(R.r.mode & R_SHADOW)
1046                                         ISB_free(pa);
1047                         }
1048                         
1049                         /* disable scanline updating */
1050                         rr->renlay= NULL;
1051                 }
1052                 
1053                 /* lamphalo after solid, before ztra, looks nicest because ztra does own halo */
1054                 if(R.flag & R_LAMPHALO)
1055                         if(rl->layflag & SCE_LAY_HALO)
1056                                 lamphalo_tile(pa, rl);
1057                 
1058                 /* halo before ztra, because ztra fills in zbuffer now */
1059                 if(R.flag & R_HALO)
1060                         if(rl->layflag & SCE_LAY_HALO)
1061                                 halo_tile(pa, rl->rectf, rl->lay);
1062                 
1063                 if(R.flag & R_ZTRA) {
1064                         if(rl->layflag & SCE_LAY_ZTRA) {
1065                                 float *fcol, *acol;
1066                                 int x;
1067                                 
1068                                 /* allocate, but not free here, for asynchronous display of this rect in main thread */
1069                                 rl->acolrect= MEM_callocN(4*sizeof(float)*pa->rectx*pa->recty, "alpha layer");
1070                                 
1071                                 /* swap for live updates */
1072                                 SWAP(float *, rl->acolrect, rl->rectf);
1073                                 zbuffer_transp_shade(pa, rl, rl->rectf);
1074                                 SWAP(float *, rl->acolrect, rl->rectf);
1075                                 
1076                                 fcol= rl->rectf; acol= rl->acolrect;
1077                                 for(x=pa->rectx*pa->recty; x>0; x--, acol+=4, fcol+=4) {
1078                                         addAlphaOverFloat(fcol, acol);
1079                                 }
1080                         }
1081                 }
1082                 
1083                 /* sky before edge */
1084                 if(rl->layflag & SCE_LAY_SKY)
1085                         sky_tile(pa, rl->rectf);
1086                 
1087                 if(!R.test_break()) {
1088                         if(rl->layflag & SCE_LAY_EDGE) 
1089                                 if(R.r.mode & R_EDGE)
1090                                         edge_enhance_add(pa, rl->rectf, edgerect);
1091                 }
1092                 
1093                 if(rl->passflag & SCE_PASS_Z)
1094                         convert_zbuf_to_distbuf(pa, rl);
1095                 
1096                 if(rl->passflag & SCE_PASS_VECTOR)
1097                         reset_sky_speed(pa, rl);
1098                 
1099                 /* de-premul alpha */
1100                 if(R.r.alphamode & R_ALPHAKEY)
1101                         convert_to_key_alpha(pa, rl->rectf);
1102                 
1103                 if(edgerect) MEM_freeN(edgerect);
1104                 edgerect= NULL;
1105         }
1106
1107         /* display active layer */
1108         rr->renrect.ymin=rr->renrect.ymax= 0;
1109         rr->renlay= render_get_active_layer(&R, rr);
1110         
1111         MEM_freeN(pa->rectp); pa->rectp= NULL;
1112         MEM_freeN(pa->rectz); pa->rectz= NULL;
1113         MEM_freeN(pa->clipflag); pa->clipflag= NULL;
1114 }
1115
1116 /* ------------------------------------------------------------------------ */
1117
1118 static void renderhalo_post(RenderResult *rr, float *rectf, HaloRen *har)       /* postprocess version */
1119 {
1120         float dist, xsq, ysq, xn, yn, colf[4], *rectft, *rtf;
1121         float haloxs, haloys;
1122         int minx, maxx, miny, maxy, x, y;
1123
1124         /* calculate the disprect mapped coordinate for halo. note: rectx is disprect corrected */
1125         haloxs= har->xs - R.disprect.xmin;
1126         haloys= har->ys - R.disprect.ymin;
1127         
1128         har->miny= miny= haloys - har->rad/R.ycor;
1129         har->maxy= maxy= haloys + har->rad/R.ycor;
1130         
1131         if(maxy<0);
1132         else if(rr->recty<miny);
1133         else {
1134                 minx= floor(haloxs-har->rad);
1135                 maxx= ceil(haloxs+har->rad);
1136                         
1137                 if(maxx<0);
1138                 else if(rr->rectx<minx);
1139                 else {
1140                 
1141                         if(minx<0) minx= 0;
1142                         if(maxx>=rr->rectx) maxx= rr->rectx-1;
1143                         if(miny<0) miny= 0;
1144                         if(maxy>rr->recty) maxy= rr->recty;
1145         
1146                         rectft= rectf+ 4*rr->rectx*miny;
1147
1148                         for(y=miny; y<maxy; y++) {
1149         
1150                                 rtf= rectft+4*minx;
1151                                 
1152                                 yn= (y - haloys)*R.ycor;
1153                                 ysq= yn*yn;
1154                                 
1155                                 for(x=minx; x<=maxx; x++) {
1156                                         xn= x - haloxs;
1157                                         xsq= xn*xn;
1158                                         dist= xsq+ysq;
1159                                         if(dist<har->radsq) {
1160                                                 
1161                                                 shadeHaloFloat(har, colf, 0x7FFFFF, dist, xn, yn, har->flarec);
1162                                                 addalphaAddfacFloat(rtf, colf, har->add);
1163                                         }
1164                                         rtf+=4;
1165                                 }
1166         
1167                                 rectft+= 4*rr->rectx;
1168                                 
1169                                 if(R.test_break()) break; 
1170                         }
1171                 }
1172         }
1173
1174 /* ------------------------------------------------------------------------ */
1175
1176 static void renderflare(RenderResult *rr, float *rectf, HaloRen *har)
1177 {
1178         extern float hashvectf[];
1179         HaloRen fla;
1180         Material *ma;
1181         float *rc, rad, alfa, visifac, vec[3];
1182         int b, type;
1183         
1184         fla= *har;
1185         fla.linec= fla.ringc= fla.flarec= 0;
1186         
1187         rad= har->rad;
1188         alfa= har->alfa;
1189         
1190         visifac= R.ycor*(har->pixels);
1191         /* all radials added / r^3  == 1.0f! */
1192         visifac /= (har->rad*har->rad*har->rad);
1193         visifac*= visifac;
1194
1195         ma= har->mat;
1196         
1197         /* first halo: just do */
1198         
1199         har->rad= rad*ma->flaresize*visifac;
1200         har->radsq= har->rad*har->rad;
1201         har->zs= fla.zs= 0;
1202         
1203         har->alfa= alfa*visifac;
1204
1205         renderhalo_post(rr, rectf, har);
1206         
1207         /* next halo's: the flares */
1208         rc= hashvectf + ma->seed2;
1209         
1210         for(b=1; b<har->flarec; b++) {
1211                 
1212                 fla.r= fabs(rc[0]);
1213                 fla.g= fabs(rc[1]);
1214                 fla.b= fabs(rc[2]);
1215                 fla.alfa= ma->flareboost*fabs(alfa*visifac*rc[3]);
1216                 fla.hard= 20.0f + fabs(70*rc[7]);
1217                 fla.tex= 0;
1218                 
1219                 type= (int)(fabs(3.9*rc[6]));
1220
1221                 fla.rad= ma->subsize*sqrt(fabs(2.0f*har->rad*rc[4]));
1222                 
1223                 if(type==3) {
1224                         fla.rad*= 3.0f;
1225                         fla.rad+= R.rectx/10;
1226                 }
1227                 
1228                 fla.radsq= fla.rad*fla.rad;
1229                 
1230                 vec[0]= 1.4*rc[5]*(har->xs-R.winx/2);
1231                 vec[1]= 1.4*rc[5]*(har->ys-R.winy/2);
1232                 vec[2]= 32.0f*sqrt(vec[0]*vec[0] + vec[1]*vec[1] + 1.0f);
1233                 
1234                 fla.xs= R.winx/2 + vec[0] + (1.2+rc[8])*R.rectx*vec[0]/vec[2];
1235                 fla.ys= R.winy/2 + vec[1] + (1.2+rc[8])*R.rectx*vec[1]/vec[2];
1236
1237                 if(R.flag & R_SEC_FIELD) {
1238                         if(R.r.mode & R_ODDFIELD) fla.ys += 0.5;
1239                         else fla.ys -= 0.5;
1240                 }
1241                 if(type & 1) fla.type= HA_FLARECIRC;
1242                 else fla.type= 0;
1243                 renderhalo_post(rr, rectf, &fla);
1244
1245                 fla.alfa*= 0.5;
1246                 if(type & 2) fla.type= HA_FLARECIRC;
1247                 else fla.type= 0;
1248                 renderhalo_post(rr, rectf, &fla);
1249                 
1250                 rc+= 7;
1251         }
1252 }
1253
1254 /* needs recode... integrate this better! */
1255 void add_halo_flare(Render *re)
1256 {
1257         RenderResult *rr= re->result;
1258         RenderLayer *rl;
1259         HaloRen *har = NULL;
1260         int a, mode, do_draw=0;
1261         
1262         /* for now, we get the first renderlayer in list with halos set */
1263         for(rl= rr->layers.first; rl; rl= rl->next)
1264                 if(rl->layflag & SCE_LAY_HALO)
1265                         break;
1266
1267         if(rl==NULL || rl->rectf==NULL)
1268                 return;
1269         
1270         mode= R.r.mode;
1271         R.r.mode &= ~R_PANORAMA;
1272         
1273         project_renderdata(&R, projectverto, 0, 0);
1274         
1275         for(a=0; a<R.tothalo; a++) {
1276                 if((a & 255)==0) har= R.bloha[a>>8];
1277                 else har++;
1278                 
1279                 if(har->flarec) {
1280                         do_draw= 1;
1281                         renderflare(rr, rl->rectf, har);
1282                 }
1283         }
1284
1285         if(do_draw) {
1286                 /* weak... the display callback wants an active renderlayer pointer... */
1287                 rr->renlay= rl;
1288                 re->display_draw(rr, NULL);
1289         }
1290         
1291         R.r.mode= mode; 
1292 }
1293
1294 /* ************************* used for shaded view ************************ */
1295
1296 /* if *re, then initialize, otherwise execute */
1297 void RE_shade_external(Render *re, ShadeInput *shi, ShadeResult *shr)
1298 {
1299         static VlakRen vlr;
1300         
1301         /* init */
1302         if(re) {
1303                 R= *re;
1304                 
1305                 /* fake render face */
1306                 memset(&vlr, 0, sizeof(VlakRen));
1307                 vlr.lay= -1;
1308                 
1309                 return;
1310         }
1311         shi->vlr= &vlr;
1312         
1313         if(shi->mat->nodetree && shi->mat->use_nodes)
1314                 ntreeShaderExecTree(shi->mat->nodetree, shi, shr);
1315         else {
1316                 /* copy all relevant material vars, note, keep this synced with render_types.h */
1317                 memcpy(&shi->r, &shi->mat->r, 23*sizeof(float));
1318                 shi->har= shi->mat->har;
1319                 
1320                 shade_material_loop(shi, shr);
1321         }
1322 }
1323
1324 /* ************************* bake ************************ */
1325
1326 #define FTOCHAR(val) val<=0.0f?0: (val>=1.0f?255: (char)(255.0f*val))
1327
1328 typedef struct BakeShade {
1329         ShadeSample ssamp;
1330         VlakRen *vlr;
1331         
1332         ZSpan *zspan;
1333         Image *ima;
1334         ImBuf *ibuf;
1335         
1336         int rectx, recty, quad, type, vdone, ready;
1337         
1338         unsigned int *rect;
1339         float *rect_float;
1340 } BakeShade;
1341
1342 static void do_bake_shade(void *handle, int x, int y, float u, float v)
1343 {
1344         BakeShade *bs= handle;
1345         ShadeSample *ssamp= &bs->ssamp;
1346         ShadeInput *shi= ssamp->shi;
1347         ShadeResult shr;
1348         VlakRen *vlr= bs->vlr;
1349         float l, *v1, *v2, *v3;
1350         
1351         /* fast threadsafe break test */
1352         if(R.test_break())
1353                 return;
1354         
1355         /* setup render coordinates */
1356         if(bs->quad) {
1357                 v1= vlr->v1->co;
1358                 v2= vlr->v3->co;
1359                 v3= vlr->v4->co;
1360         }
1361         else {
1362                 v1= vlr->v1->co;
1363                 v2= vlr->v2->co;
1364                 v3= vlr->v3->co;
1365         }
1366         
1367         /* renderco */
1368         l= 1.0f-u-v;
1369         
1370         shi->co[0]= l*v3[0]+u*v1[0]+v*v2[0];
1371         shi->co[1]= l*v3[1]+u*v1[1]+v*v2[1];
1372         shi->co[2]= l*v3[2]+u*v1[2]+v*v2[2];
1373         
1374         /* set up view vector */
1375         VECCOPY(shi->view, shi->co);
1376         Normalize(shi->view);
1377         
1378         /* no face normal flip */
1379         shi->puno= 0;
1380         
1381         /* cache for shadow */
1382         shi->samplenr++;
1383         
1384         if(bs->quad) 
1385                 shade_input_set_triangle_i(shi, vlr, 0, 2, 3);
1386         else
1387                 shade_input_set_triangle_i(shi, vlr, 0, 1, 2);
1388         
1389         shi->u= -u;
1390         shi->v= -v;
1391         shi->xs= x;
1392         shi->ys= y;
1393         
1394         shade_input_set_normals(shi);
1395
1396         /* init material vars */
1397         memcpy(&shi->r, &shi->mat->r, 23*sizeof(float));        // note, keep this synced with render_types.h
1398         shi->har= shi->mat->har;
1399         
1400         if(bs->type==RE_BAKE_AO) {
1401                 ambient_occlusion(shi);
1402                 ambient_occlusion_to_diffuse(shi, shr.combined);
1403         }
1404         else {
1405                 
1406                 shade_input_set_shade_texco(shi);
1407                 
1408                 shade_samples_do_AO(ssamp);
1409                 
1410                 if(shi->mat->nodetree && shi->mat->use_nodes) {
1411                         ntreeShaderExecTree(shi->mat->nodetree, shi, &shr);
1412                         shi->mat= vlr->mat;             /* shi->mat is being set in nodetree */
1413                 }
1414                 else
1415                         shade_material_loop(shi, &shr);
1416                 
1417                 if(bs->type==RE_BAKE_NORMALS) {
1418                         shr.combined[0]= shi->vn[0]/2.0f + 0.5f;
1419                         shr.combined[1]= 0.5f - shi->vn[1]/2.0f;
1420                         shr.combined[2]= shi->vn[2]/2.0f + 0.5f;
1421                 }
1422                 else if(bs->type==RE_BAKE_TEXTURE) {
1423                         shr.combined[0]= shi->r;
1424                         shr.combined[1]= shi->g;
1425                         shr.combined[2]= shi->b;
1426                 }
1427         }
1428         
1429         if(bs->rect) {
1430                 char *col= (char *)(bs->rect + bs->rectx*y + x);
1431                 col[0]= FTOCHAR(shr.combined[0]);
1432                 col[1]= FTOCHAR(shr.combined[1]);
1433                 col[2]= FTOCHAR(shr.combined[2]);
1434                 col[3]= 255;
1435         }
1436         else {
1437                 float *col= bs->rect_float + 4*(bs->rectx*y + x);
1438                 VECCOPY(col, shr.combined);
1439                 col[3]= 1.0f;
1440         }
1441 }
1442
1443 static int get_next_bake_face(BakeShade *bs)
1444 {
1445         VlakRen *vlr;
1446         MTFace *tface;
1447         static int v= 0, vdone= 0;
1448         
1449         if(bs==NULL) {
1450                 vlr= NULL;
1451                 v= vdone= 0;
1452                 return 0;
1453         }
1454         
1455         BLI_lock_thread(LOCK_CUSTOM1);  
1456         
1457         for(; v<R.totvlak; v++) {
1458                 vlr= RE_findOrAddVlak(&R, v);
1459                 
1460                 if(vlr->ob->flag & SELECT) {
1461                         tface= RE_vlakren_get_tface(&R, vlr, 0, NULL, 0);
1462
1463                         if(tface && tface->tpage) {
1464                                 Image *ima= tface->tpage;
1465                                 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
1466                                 float vec[4]= {0.0f, 0.0f, 0.0f, 0.0f};
1467                                 
1468                                 if(ibuf==NULL)
1469                                         continue;
1470                                 
1471                                 if(ibuf->rect==NULL && ibuf->rect_float==NULL)
1472                                         continue;
1473                                 
1474                                 if(ibuf->rect_float && !(ibuf->channels==0 || ibuf->channels==4))
1475                                         continue;
1476                                 
1477                                 /* find the image for the first time? */
1478                                 if(ima->id.flag & LIB_DOIT) {
1479                                         ima->id.flag &= ~LIB_DOIT;
1480                                         
1481                                         /* we either fill in float or char, this ensures things go fine */
1482                                         if(ibuf->rect_float)
1483                                                 imb_freerectImBuf(ibuf);
1484                                         /* clear image */
1485                                         if(R.r.bake_flag & R_BAKE_CLEAR)
1486                                                 IMB_rectfill(ibuf, vec);
1487                                 
1488                                         /* might be read by UI to set active image for display */
1489                                         R.bakebuf= ima;
1490                                 }                               
1491                                 
1492                                 bs->vlr= vlr;
1493                                 
1494                                 bs->vdone++;    /* only for error message if nothing was rendered */
1495                                 v++;
1496                                 
1497                                 BLI_unlock_thread(LOCK_CUSTOM1);
1498                                 return 1;
1499                         }
1500                 }
1501         }
1502         
1503         BLI_unlock_thread(LOCK_CUSTOM1);
1504         return 0;
1505 }
1506
1507 /* already have tested for tface and ima and zspan */
1508 static void shade_tface(BakeShade *bs)
1509 {
1510         VlakRen *vlr= bs->vlr;
1511         MTFace *tface= RE_vlakren_get_tface(&R, vlr, 0, NULL, 0);
1512         Image *ima= tface->tpage;
1513         float vec[4][2];
1514         int a, i1, i2, i3;
1515         
1516         /* check valid zspan */
1517         if(ima!=bs->ima) {
1518                 bs->ima= ima;
1519                 bs->ibuf= BKE_image_get_ibuf(ima, NULL);
1520                 /* note, these calls only free/fill contents of zspan struct, not zspan itself */
1521                 zbuf_free_span(bs->zspan);
1522                 zbuf_alloc_span(bs->zspan, bs->ibuf->x, bs->ibuf->y);
1523         }                               
1524         
1525         bs->rectx= bs->ibuf->x;
1526         bs->recty= bs->ibuf->y;
1527         bs->rect= bs->ibuf->rect;
1528         bs->rect_float= bs->ibuf->rect_float;
1529         bs->quad= 0;
1530         
1531         /* get pixel level vertex coordinates */
1532         for(a=0; a<4; a++) {
1533                 vec[a][0]= tface->uv[a][0]*(float)bs->rectx - 0.5f;
1534                 vec[a][1]= tface->uv[a][1]*(float)bs->recty - 0.5f;
1535         }
1536         
1537         /* UV indices have to be corrected for possible quad->tria splits */
1538         i1= 0; i2= 1; i3= 2;
1539         vlr_set_uv_indices(vlr, &i1, &i2, &i3);
1540         zspan_scanconvert(bs->zspan, bs, vec[i1], vec[i2], vec[i3], do_bake_shade);
1541         
1542         if(vlr->v4) {
1543                 bs->quad= 1;
1544                 zspan_scanconvert(bs->zspan, bs, vec[0], vec[2], vec[3], do_bake_shade);
1545         }
1546 }
1547
1548 static void *do_bake_thread(void *bs_v)
1549 {
1550         BakeShade *bs= bs_v;
1551         
1552         while(get_next_bake_face(bs)) {
1553                 shade_tface(bs);
1554                 
1555                 /* fast threadsafe break test */
1556                 if(R.test_break())
1557                         break;
1558         }
1559         bs->ready= 1;
1560         
1561         return NULL;
1562 }
1563
1564 /* using object selection tags, the faces with UV maps get baked */
1565 /* render should have been setup */
1566 /* returns 0 if nothing was handled */
1567 int RE_bake_shade_all_selected(Render *re, int type)
1568 {
1569         BakeShade handles[BLENDER_MAX_THREADS];
1570         ListBase threads;
1571         Image *ima;
1572         int a, vdone=0;
1573         
1574         /* initialize static vars */
1575         get_next_bake_face(NULL);
1576         
1577         /* baker uses this flag to detect if image was initialized */
1578         for(ima= G.main->image.first; ima; ima= ima->id.next)
1579                 ima->id.flag |= LIB_DOIT;
1580         
1581         /* initialize render global */
1582         R= *re;
1583         R.bakebuf= NULL;
1584         
1585         BLI_init_threads(&threads, do_bake_thread, re->r.threads);
1586
1587         /* get the threads running */
1588         for(a=0; a<re->r.threads; a++) {
1589                 /* set defaults in handles */
1590                 memset(&handles[a], 0, sizeof(BakeShade));
1591                 
1592                 handles[a].ssamp.shi[0].lay= re->scene->lay;
1593                 handles[a].ssamp.shi[0].passflag= SCE_PASS_COMBINED;
1594                 handles[a].ssamp.shi[0].combinedflag= ~(SCE_PASS_SPEC);
1595                 handles[a].ssamp.shi[0].thread= a;
1596                 handles[a].ssamp.tot= 1;
1597                 
1598                 handles[a].type= type;
1599                 handles[a].zspan= MEM_callocN(sizeof(ZSpan), "zspan for bake");
1600                 
1601                 BLI_insert_thread(&threads, &handles[a]);
1602         }
1603         
1604         /* wait for everything to be done */
1605         a= 0;
1606         while(a!=re->r.threads) {
1607                 
1608                 PIL_sleep_ms(50);
1609
1610                 for(a=0; a<re->r.threads; a++)
1611                         if(handles[a].ready==0)
1612                                 break;
1613         }
1614         
1615         /* filter images */
1616         for(ima= G.main->image.first; ima; ima= ima->id.next) {
1617                 if((ima->id.flag & LIB_DOIT)==0) {
1618                         ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
1619                         for(a=0; a<re->r.bake_filter; a++)
1620                                 IMB_filter_extend(ibuf);
1621                         ibuf->userflags |= IB_BITMAPDIRTY;
1622                 }
1623         }
1624         
1625         /* calculate return value */
1626         for(a=0; a<re->r.threads; a++) {
1627                 vdone+= handles[a].vdone;
1628                 
1629                 zbuf_free_span(handles[a].zspan);
1630                 MEM_freeN(handles[a].zspan);
1631         }
1632         
1633         BLI_end_threads(&threads);
1634         return vdone;
1635 }
1636
1637 struct Image *RE_bake_shade_get_image(void)
1638 {
1639         return R.bakebuf;
1640 }
1641