Undo revision 23130 which was a merge with 2.5, a messy one because I did something...
[blender.git] / source / blender / render / intern / source / initrender.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: 2004/2005/2006 Blender Foundation, full recode
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28
29 /* Global includes */
30
31 #include <math.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <stdio.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "PIL_time.h"
39
40 #include "BLI_arithb.h"
41 #include "BLI_blenlib.h"
42 #include "BLI_jitter.h"
43
44 #include "MTC_matrixops.h"
45
46 #include "DNA_camera_types.h"
47 #include "DNA_group_types.h"
48 #include "DNA_image_types.h"
49 #include "DNA_lamp_types.h"
50 #include "DNA_object_types.h"
51 #include "DNA_scene_types.h"
52
53 #include "BKE_utildefines.h"
54 #include "BKE_global.h"
55 #include "BKE_material.h"
56 #include "BKE_object.h"
57 #include "BKE_image.h"
58 #include "BKE_ipo.h"
59 #include "BKE_key.h"
60 #include "BKE_action.h"
61 #include "BKE_writeavi.h"
62 #include "BKE_scene.h"
63
64 #include "IMB_imbuf_types.h"
65 #include "IMB_imbuf.h"
66
67 #ifdef WITH_QUICKTIME
68 #include "quicktime_export.h"
69 #endif
70
71 /* this module */
72 #include "renderpipeline.h"
73 #include "render_types.h"
74
75 #include "rendercore.h"
76 #include "pixelshading.h"
77 #include "zbuf.h"
78
79 /* Own includes */
80 #include "initrender.h"
81
82
83 /* ********************** */
84
85 static void init_render_jit(Render *re)
86 {
87         static float jit[32][2];        /* simple caching */
88         static int lastjit= 0;
89         
90         if(lastjit!=re->r.osa) {
91                 memset(jit, 0, sizeof(jit));
92                 BLI_initjit(jit[0], re->r.osa);
93         }
94         
95         lastjit= re->r.osa;
96         memcpy(re->jit, jit, sizeof(jit));
97 }
98
99
100 /* ****************** MASKS and LUTS **************** */
101
102 static float filt_quadratic(float x)
103 {
104     if (x <  0.0f) x = -x;
105     if (x < 0.5f) return 0.75f-(x*x);
106     if (x < 1.5f) return 0.50f*(x-1.5f)*(x-1.5f);
107     return 0.0f;
108 }
109
110
111 static float filt_cubic(float x)
112 {
113         float x2= x*x;
114         
115     if (x <  0.0f) x = -x;
116         
117     if (x < 1.0f) return 0.5*x*x2 - x2 + 2.0f/3.0f;
118     if (x < 2.0f) return (2.0-x)*(2.0-x)*(2.0-x)/6.0f;
119     return 0.0f;
120 }
121
122
123 static float filt_catrom(float x)
124 {
125         float x2= x*x;
126         
127     if (x <  0.0f) x = -x;
128     if (x < 1.0f) return  1.5f*x2*x - 2.5f*x2  + 1.0f;
129     if (x < 2.0f) return -0.5f*x2*x + 2.5*x2 - 4.0f*x + 2.0f;
130     return 0.0f;
131 }
132
133 static float filt_mitchell(float x)     /* Mitchell & Netravali's two-param cubic */
134 {
135         float b = 1.0f/3.0f, c = 1.0f/3.0f;
136         float p0 = (  6.0 -  2.0*b         ) / 6.0;
137         float p2 = (-18.0 + 12.0*b +  6.0*c) / 6.0;
138         float p3 = ( 12.0 -  9.0*b -  6.0*c) / 6.0;
139         float q0 = (       8.0*b + 24.0*c) / 6.0;
140         float q1 = (      - 12.0*b - 48.0*c) / 6.0;
141         float q2 = (         6.0*b + 30.0*c) / 6.0;
142         float q3 = (       -     b -  6.0*c) / 6.0;
143
144         if (x<-2.0) return 0.0;
145         if (x<-1.0) return (q0-x*(q1-x*(q2-x*q3)));
146         if (x< 0.0) return (p0+x*x*(p2-x*p3));
147         if (x< 1.0) return (p0+x*x*(p2+x*p3));
148         if (x< 2.0) return (q0+x*(q1+x*(q2+x*q3)));
149         return 0.0;
150 }
151
152 /* x ranges from -1 to 1 */
153 float RE_filter_value(int type, float x)
154 {
155         float gaussfac= 1.6f;
156         
157         x= ABS(x);
158         
159         switch(type) {
160                 case R_FILTER_BOX:
161                         if(x>1.0) return 0.0f;
162                         return 1.0;
163                         
164                 case R_FILTER_TENT:
165                         if(x>1.0) return 0.0f;
166                         return 1.0f-x;
167                         
168                 case R_FILTER_GAUSS:
169                         x*= gaussfac;
170                         return (1.0/exp(x*x) - 1.0/exp(gaussfac*gaussfac*2.25));
171                         
172                 case R_FILTER_MITCH:
173                         return filt_mitchell(x*gaussfac);
174                         
175                 case R_FILTER_QUAD:
176                         return filt_quadratic(x*gaussfac);
177                         
178                 case R_FILTER_CUBIC:
179                         return filt_cubic(x*gaussfac);
180                         
181                 case R_FILTER_CATROM:
182                         return filt_catrom(x*gaussfac);
183         }
184         return 0.0f;
185 }
186
187 static float calc_weight(Render *re, float *weight, int i, int j)
188 {
189         float x, y, dist, totw= 0.0;
190         int a;
191
192         for(a=0; a<re->osa; a++) {
193                 x= re->jit[a][0] + i;
194                 y= re->jit[a][1] + j;
195                 dist= sqrt(x*x+y*y);
196
197                 weight[a]= 0.0;
198
199                 /* Weighting choices */
200                 switch(re->r.filtertype) {
201                 case R_FILTER_BOX:
202                         if(i==0 && j==0) weight[a]= 1.0;
203                         break;
204                         
205                 case R_FILTER_TENT:
206                         if(dist < re->r.gauss)
207                                 weight[a]= re->r.gauss - dist;
208                         break;
209                         
210                 case R_FILTER_GAUSS:
211                         x = dist*re->r.gauss;
212                         weight[a]= (1.0/exp(x*x) - 1.0/exp(re->r.gauss*re->r.gauss*2.25));
213                         break;
214                 
215                 case R_FILTER_MITCH:
216                         weight[a]= filt_mitchell(dist*re->r.gauss);
217                         break;
218                 
219                 case R_FILTER_QUAD:
220                         weight[a]= filt_quadratic(dist*re->r.gauss);
221                         break;
222                         
223                 case R_FILTER_CUBIC:
224                         weight[a]= filt_cubic(dist*re->r.gauss);
225                         break;
226                         
227                 case R_FILTER_CATROM:
228                         weight[a]= filt_catrom(dist*re->r.gauss);
229                         break;
230                         
231                 }
232                 
233                 totw+= weight[a];
234
235         }
236         return totw;
237 }
238
239 void free_sample_tables(Render *re)
240 {
241         int a;
242         
243         if(re->samples) {
244                 for(a=0; a<9; a++) {
245                         MEM_freeN(re->samples->fmask1[a]);
246                         MEM_freeN(re->samples->fmask2[a]);
247                 }
248                 
249                 MEM_freeN(re->samples->centmask);
250                 MEM_freeN(re->samples);
251                 re->samples= NULL;
252         }
253 }
254
255 /* based on settings in render, it makes the lookup tables */
256 void make_sample_tables(Render *re)
257 {
258         static int firsttime= 1;
259         SampleTables *st;
260         float flweight[32];
261         float weight[32], totw, val, *fpx1, *fpx2, *fpy1, *fpy2, *m3, *m4;
262         int i, j, a;
263
264         /* optimization tables, only once */
265         if(firsttime) {
266                 firsttime= 0;
267         }
268         
269         free_sample_tables(re);
270         
271         init_render_jit(re);    /* needed for mblur too */
272         
273         if(re->osa==0) {
274                 /* just prevents cpu cycles for larger render and copying */
275                 re->r.filtertype= 0;
276                 return;
277         }
278         
279         st= re->samples= MEM_callocN(sizeof(SampleTables), "sample tables");
280         
281         for(a=0; a<9;a++) {
282                 st->fmask1[a]= MEM_callocN(256*sizeof(float), "initfilt");
283                 st->fmask2[a]= MEM_callocN(256*sizeof(float), "initfilt");
284         }
285         for(a=0; a<256; a++) {
286                 st->cmask[a]= 0;
287                 if(a &   1) st->cmask[a]++;
288                 if(a &   2) st->cmask[a]++;
289                 if(a &   4) st->cmask[a]++;
290                 if(a &   8) st->cmask[a]++;
291                 if(a &  16) st->cmask[a]++;
292                 if(a &  32) st->cmask[a]++;
293                 if(a &  64) st->cmask[a]++;
294                 if(a & 128) st->cmask[a]++;
295         }
296         
297         st->centmask= MEM_mallocN((1<<re->osa), "Initfilt3");
298         
299         for(a=0; a<16; a++) {
300                 st->centLut[a]= -0.45+((float)a)/16.0;
301         }
302
303         /* calculate totw */
304         totw= 0.0;
305         for(j= -1; j<2; j++) {
306                 for(i= -1; i<2; i++) {
307                         totw+= calc_weight(re, weight, i, j);
308                 }
309         }
310
311         for(j= -1; j<2; j++) {
312                 for(i= -1; i<2; i++) {
313                         /* calculate using jit, with offset the weights */
314
315                         memset(weight, 0, sizeof(weight));
316                         calc_weight(re, weight, i, j);
317
318                         for(a=0; a<16; a++) flweight[a]= weight[a]*(1.0/totw);
319
320                         m3= st->fmask1[ 3*(j+1)+i+1 ];
321                         m4= st->fmask2[ 3*(j+1)+i+1 ];
322
323                         for(a=0; a<256; a++) {
324                                 if(a &   1) {
325                                         m3[a]+= flweight[0];
326                                         m4[a]+= flweight[8];
327                                 }
328                                 if(a &   2) {
329                                         m3[a]+= flweight[1];
330                                         m4[a]+= flweight[9];
331                                 }
332                                 if(a &   4) {
333                                         m3[a]+= flweight[2];
334                                         m4[a]+= flweight[10];
335                                 }
336                                 if(a &   8) {
337                                         m3[a]+= flweight[3];
338                                         m4[a]+= flweight[11];
339                                 }
340                                 if(a &  16) {
341                                         m3[a]+= flweight[4];
342                                         m4[a]+= flweight[12];
343                                 }
344                                 if(a &  32) {
345                                         m3[a]+= flweight[5];
346                                         m4[a]+= flweight[13];
347                                 }
348                                 if(a &  64) {
349                                         m3[a]+= flweight[6];
350                                         m4[a]+= flweight[14];
351                                 }
352                                 if(a & 128) {
353                                         m3[a]+= flweight[7];
354                                         m4[a]+= flweight[15];
355                                 }
356                         }
357                 }
358         }
359
360         /* centmask: the correct subpixel offset per mask */
361
362         fpx1= MEM_mallocN(256*sizeof(float), "initgauss4");
363         fpx2= MEM_mallocN(256*sizeof(float), "initgauss4");
364         fpy1= MEM_mallocN(256*sizeof(float), "initgauss4");
365         fpy2= MEM_mallocN(256*sizeof(float), "initgauss4");
366         for(a=0; a<256; a++) {
367                 fpx1[a]= fpx2[a]= 0.0;
368                 fpy1[a]= fpy2[a]= 0.0;
369                 if(a & 1) {
370                         fpx1[a]+= re->jit[0][0];
371                         fpy1[a]+= re->jit[0][1];
372                         fpx2[a]+= re->jit[8][0];
373                         fpy2[a]+= re->jit[8][1];
374                 }
375                 if(a & 2) {
376                         fpx1[a]+= re->jit[1][0];
377                         fpy1[a]+= re->jit[1][1];
378                         fpx2[a]+= re->jit[9][0];
379                         fpy2[a]+= re->jit[9][1];
380                 }
381                 if(a & 4) {
382                         fpx1[a]+= re->jit[2][0];
383                         fpy1[a]+= re->jit[2][1];
384                         fpx2[a]+= re->jit[10][0];
385                         fpy2[a]+= re->jit[10][1];
386                 }
387                 if(a & 8) {
388                         fpx1[a]+= re->jit[3][0];
389                         fpy1[a]+= re->jit[3][1];
390                         fpx2[a]+= re->jit[11][0];
391                         fpy2[a]+= re->jit[11][1];
392                 }
393                 if(a & 16) {
394                         fpx1[a]+= re->jit[4][0];
395                         fpy1[a]+= re->jit[4][1];
396                         fpx2[a]+= re->jit[12][0];
397                         fpy2[a]+= re->jit[12][1];
398                 }
399                 if(a & 32) {
400                         fpx1[a]+= re->jit[5][0];
401                         fpy1[a]+= re->jit[5][1];
402                         fpx2[a]+= re->jit[13][0];
403                         fpy2[a]+= re->jit[13][1];
404                 }
405                 if(a & 64) {
406                         fpx1[a]+= re->jit[6][0];
407                         fpy1[a]+= re->jit[6][1];
408                         fpx2[a]+= re->jit[14][0];
409                         fpy2[a]+= re->jit[14][1];
410                 }
411                 if(a & 128) {
412                         fpx1[a]+= re->jit[7][0];
413                         fpy1[a]+= re->jit[7][1];
414                         fpx2[a]+= re->jit[15][0];
415                         fpy2[a]+= re->jit[15][1];
416                 }
417         }
418
419         for(a= (1<<re->osa)-1; a>0; a--) {
420                 val= st->cmask[a & 255] + st->cmask[a>>8];
421                 i= 8+(15.9*(fpy1[a & 255]+fpy2[a>>8])/val);
422                 CLAMP(i, 0, 15);
423                 j= 8+(15.9*(fpx1[a & 255]+fpx2[a>>8])/val);
424                 CLAMP(j, 0, 15);
425                 i= j + (i<<4);
426                 st->centmask[a]= i;
427         }
428
429         MEM_freeN(fpx1);
430         MEM_freeN(fpx2);
431         MEM_freeN(fpy1);
432         MEM_freeN(fpy2);
433 }
434
435
436 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
437
438 /* call this after InitState() */
439 /* per render, there's one persistant viewplane. Parts will set their own viewplanes */
440 void RE_SetCamera(Render *re, Object *camera)
441 {
442         Camera *cam=NULL;
443         rctf viewplane;
444         float pixsize, clipsta, clipend;
445         float lens, shiftx=0.0, shifty=0.0, winside;
446         
447         /* question mark */
448         re->ycor= ( (float)re->r.yasp)/( (float)re->r.xasp);
449         if(re->r.mode & R_FIELDS)
450                 re->ycor *= 2.0f;
451         
452         if(camera->type==OB_CAMERA) {
453                 cam= camera->data;
454                 
455                 if(cam->type==CAM_ORTHO) re->r.mode |= R_ORTHO;
456                 if(cam->flag & CAM_PANORAMA) re->r.mode |= R_PANORAMA;
457                 
458                 /* solve this too... all time depending stuff is in convertblender.c?
459                  * Need to update the camera early because it's used for projection matrices
460                  * and other stuff BEFORE the animation update loop is done 
461                  * */
462 #if 0 // XXX old animation system
463                 if(cam->ipo) {
464                         calc_ipo(cam->ipo, frame_to_float(re->scene, re->r.cfra));
465                         execute_ipo(&cam->id, cam->ipo);
466                 }
467 #endif // XXX old animation system
468                 lens= cam->lens;
469                 shiftx=cam->shiftx;
470                 shifty=cam->shifty;
471
472                 clipsta= cam->clipsta;
473                 clipend= cam->clipend;
474         }
475         else if(camera->type==OB_LAMP) {
476                 Lamp *la= camera->data;
477                 float fac= cos( M_PI*la->spotsize/360.0 );
478                 float phi= acos(fac);
479                 
480                 lens= 16.0*fac/sin(phi);
481                 if(lens==0.0f)
482                         lens= 35.0;
483                 clipsta= la->clipsta;
484                 clipend= la->clipend;
485         }
486         else {  /* envmap exception... */
487                 lens= re->lens;
488                 if(lens==0.0f)
489                         lens= 16.0;
490                 
491                 clipsta= re->clipsta;
492                 clipend= re->clipend;
493                 if(clipsta==0.0f || clipend==0.0f) {
494                         clipsta= 0.1f;
495                         clipend= 1000.0f;
496                 }
497         }
498
499         /* ortho only with camera available */
500         if(cam && (re->r.mode & R_ORTHO)) {
501                 if( (re->r.xasp*re->winx) >= (re->r.yasp*re->winy) ) {
502                         re->viewfac= re->winx;
503                 }
504                 else {
505                         re->viewfac= re->ycor*re->winy;
506                 }
507                 /* ortho_scale == 1.0 means exact 1 to 1 mapping */
508                 pixsize= cam->ortho_scale/re->viewfac;
509         }
510         else {
511                 if( (re->r.xasp*re->winx) >= (re->r.yasp*re->winy) ) {
512                         re->viewfac= (re->winx*lens)/32.0;
513                 }
514                 else {
515                         re->viewfac= re->ycor*(re->winy*lens)/32.0;
516                 }
517                 
518                 pixsize= clipsta/re->viewfac;
519         }
520         
521         /* viewplane fully centered, zbuffer fills in jittered between -.5 and +.5 */
522         winside= MAX2(re->winx, re->winy);
523         viewplane.xmin= -0.5f*(float)re->winx + shiftx*winside; 
524         viewplane.ymin= -0.5f*re->ycor*(float)re->winy + shifty*winside;
525         viewplane.xmax=  0.5f*(float)re->winx + shiftx*winside; 
526         viewplane.ymax=  0.5f*re->ycor*(float)re->winy + shifty*winside; 
527
528         if(re->flag & R_SEC_FIELD) {
529                 if(re->r.mode & R_ODDFIELD) {
530                         viewplane.ymin-= .5*re->ycor;
531                         viewplane.ymax-= .5*re->ycor;
532                 }
533                 else {
534                         viewplane.ymin+= .5*re->ycor;
535                         viewplane.ymax+= .5*re->ycor;
536                 }
537         }
538         /* the window matrix is used for clipping, and not changed during OSA steps */
539         /* using an offset of +0.5 here would give clip errors on edges */
540         viewplane.xmin= pixsize*(viewplane.xmin);
541         viewplane.xmax= pixsize*(viewplane.xmax);
542         viewplane.ymin= pixsize*(viewplane.ymin);
543         viewplane.ymax= pixsize*(viewplane.ymax);
544         
545         re->viewdx= pixsize;
546         re->viewdy= re->ycor*pixsize;
547
548         if(re->r.mode & R_ORTHO)
549                 RE_SetOrtho(re, &viewplane, clipsta, clipend);
550         else 
551                 RE_SetWindow(re, &viewplane, clipsta, clipend);
552
553 }
554
555 void RE_SetPixelSize(Render *re, float pixsize)
556 {
557         re->viewdx= pixsize;
558         re->viewdy= re->ycor*pixsize;
559 }
560
561 void RE_GetCameraWindow(struct Render *re, struct Object *camera, int frame, float mat[][4])
562 {
563         re->r.cfra= frame;
564         RE_SetCamera(re, camera);
565         Mat4CpyMat4(mat, re->winmat);
566 }
567
568 /* ~~~~~~~~~~~~~~~~ part (tile) calculus ~~~~~~~~~~~~~~~~~~~~~~ */
569
570
571 void freeparts(Render *re)
572 {
573         RenderPart *part= re->parts.first;
574         
575         while(part) {
576                 if(part->rectp) MEM_freeN(part->rectp);
577                 if(part->rectz) MEM_freeN(part->rectz);
578                 part= part->next;
579         }
580         BLI_freelistN(&re->parts);
581 }
582
583 void initparts(Render *re)
584 {
585         int nr, xd, yd, partx, party, xparts, yparts;
586         int xminb, xmaxb, yminb, ymaxb;
587         
588         freeparts(re);
589         
590         /* this is render info for caller, is not reset when parts are freed! */
591         re->i.totpart= 0;
592         re->i.curpart= 0;
593         re->i.partsdone= 0;
594         
595         /* just for readable code.. */
596         xminb= re->disprect.xmin;
597         yminb= re->disprect.ymin;
598         xmaxb= re->disprect.xmax;
599         ymaxb= re->disprect.ymax;
600         
601         xparts= re->r.xparts;
602         yparts= re->r.yparts;
603         
604         /* mininum part size, but for exr tile saving it was checked already */
605         if(!(re->r.scemode & (R_EXR_TILE_FILE|R_FULL_SAMPLE))) {
606                 if(re->r.mode & R_PANORAMA) {
607                         if(ceil(re->rectx/(float)xparts) < 8) 
608                                 xparts= 1 + re->rectx/8;
609                 }
610                 else
611                         if(ceil(re->rectx/(float)xparts) < 64) 
612                                 xparts= 1 + re->rectx/64;
613                 
614                 if(ceil(re->recty/(float)yparts) < 64) 
615                         yparts= 1 + re->recty/64;
616         }
617         
618         /* part size */
619         partx= ceil(re->rectx/(float)xparts);
620         party= ceil(re->recty/(float)yparts);
621         
622         re->xparts= xparts;
623         re->yparts= yparts;
624         re->partx= partx;
625         re->party= party;
626         
627         /* calculate rotation factor of 1 pixel */
628         if(re->r.mode & R_PANORAMA)
629                 re->panophi= panorama_pixel_rot(re);
630         
631         for(nr=0; nr<xparts*yparts; nr++) {
632                 rcti disprect;
633                 int rectx, recty;
634                 
635                 xd= (nr % xparts);
636                 yd= (nr-xd)/xparts;
637                 
638                 disprect.xmin= xminb+ xd*partx;
639                 disprect.ymin= yminb+ yd*party;
640                 
641                 /* ensure we cover the entire picture, so last parts go to end */
642                 if(xd<xparts-1) {
643                         disprect.xmax= disprect.xmin + partx;
644                         if(disprect.xmax > xmaxb)
645                                 disprect.xmax = xmaxb;
646                 }
647                 else disprect.xmax= xmaxb;
648                 
649                 if(yd<yparts-1) {
650                         disprect.ymax= disprect.ymin + party;
651                         if(disprect.ymax > ymaxb)
652                                 disprect.ymax = ymaxb;
653                 }
654                 else disprect.ymax= ymaxb;
655                 
656                 rectx= disprect.xmax - disprect.xmin;
657                 recty= disprect.ymax - disprect.ymin;
658                 
659                 /* so, now can we add this part? */
660                 if(rectx>0 && recty>0) {
661                         RenderPart *pa= MEM_callocN(sizeof(RenderPart), "new part");
662                         
663                         /* Non-box filters need 2 pixels extra to work */
664                         if((re->r.filtertype || (re->r.mode & R_EDGE))) {
665                                 pa->crop= 2;
666                                 disprect.xmin -= pa->crop;
667                                 disprect.ymin -= pa->crop;
668                                 disprect.xmax += pa->crop;
669                                 disprect.ymax += pa->crop;
670                                 rectx+= 2*pa->crop;
671                                 recty+= 2*pa->crop;
672                         }
673                         pa->disprect= disprect;
674                         pa->rectx= rectx;
675                         pa->recty= recty;
676
677                         BLI_addtail(&re->parts, pa);
678                         re->i.totpart++;
679                 }
680         }
681 }
682
683
684