doxygen: prevent GPL license block from being parsed as doxygen comment.
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributors: 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_math.h"
41 #include "BLI_blenlib.h"
42 #include "BLI_jitter.h"
43 #include "BLI_utildefines.h"
44
45 #include "DNA_camera_types.h"
46 #include "DNA_group_types.h"
47 #include "DNA_image_types.h"
48 #include "DNA_lamp_types.h"
49 #include "DNA_object_types.h"
50 #include "DNA_scene_types.h"
51
52
53 #include "BKE_global.h"
54 #include "BKE_material.h"
55 #include "BKE_object.h"
56 #include "BKE_image.h"
57 #include "BKE_ipo.h"
58 #include "BKE_key.h"
59 #include "BKE_action.h"
60 #include "BKE_writeavi.h"
61 #include "BKE_scene.h"
62
63 #include "IMB_imbuf_types.h"
64 #include "IMB_imbuf.h"
65
66 #ifdef WITH_QUICKTIME
67 #include "quicktime_export.h"
68 #endif
69
70 /* this module */
71 #include "renderpipeline.h"
72 #include "render_types.h"
73
74 #include "rendercore.h"
75 #include "pixelshading.h"
76 #include "zbuf.h"
77
78 /* Own includes */
79 #include "initrender.h"
80
81
82 /* ********************** */
83
84 static void init_render_jit(Render *re)
85 {
86         static float jit[32][2];        /* simple caching */
87         static float mblur_jit[32][2];  /* simple caching */
88         static int lastjit= 0;
89         static int last_mblur_jit= 0;
90         
91         if(lastjit!=re->r.osa || last_mblur_jit != re->r.mblur_samples) {
92                 memset(jit, 0, sizeof(jit));
93                 BLI_initjit(jit[0], re->r.osa);
94                 
95                 memset(mblur_jit, 0, sizeof(mblur_jit));
96                 BLI_initjit(mblur_jit[0], re->r.mblur_samples);
97         }
98         
99         lastjit= re->r.osa;
100         memcpy(re->jit, jit, sizeof(jit));
101         
102         last_mblur_jit= re->r.mblur_samples;
103         memcpy(re->mblur_jit, mblur_jit, sizeof(mblur_jit));
104 }
105
106
107 /* ****************** MASKS and LUTS **************** */
108
109 static float filt_quadratic(float x)
110 {
111         if (x <  0.0f) x = -x;
112         if (x < 0.5f) return 0.75f-(x*x);
113         if (x < 1.5f) return 0.50f*(x-1.5f)*(x-1.5f);
114         return 0.0f;
115 }
116
117
118 static float filt_cubic(float x)
119 {
120         float x2= x*x;
121         
122         if (x <  0.0f) x = -x;
123         
124         if (x < 1.0f) return 0.5*x*x2 - x2 + 2.0f/3.0f;
125         if (x < 2.0f) return (2.0-x)*(2.0-x)*(2.0-x)/6.0f;
126         return 0.0f;
127 }
128
129
130 static float filt_catrom(float x)
131 {
132         float x2= x*x;
133         
134         if (x <  0.0f) x = -x;
135         if (x < 1.0f) return  1.5f*x2*x - 2.5f*x2  + 1.0f;
136         if (x < 2.0f) return -0.5f*x2*x + 2.5*x2 - 4.0f*x + 2.0f;
137         return 0.0f;
138 }
139
140 static float filt_mitchell(float x)     /* Mitchell & Netravali's two-param cubic */
141 {
142         float b = 1.0f/3.0f, c = 1.0f/3.0f;
143         float p0 = (  6.0 -  2.0*b         ) / 6.0;
144         float p2 = (-18.0 + 12.0*b +  6.0*c) / 6.0;
145         float p3 = ( 12.0 -  9.0*b -  6.0*c) / 6.0;
146         float q0 = (       8.0*b + 24.0*c) / 6.0;
147         float q1 = (      - 12.0*b - 48.0*c) / 6.0;
148         float q2 = (         6.0*b + 30.0*c) / 6.0;
149         float q3 = (       -     b -  6.0*c) / 6.0;
150
151         if (x<-2.0) return 0.0;
152         if (x<-1.0) return (q0-x*(q1-x*(q2-x*q3)));
153         if (x< 0.0) return (p0+x*x*(p2-x*p3));
154         if (x< 1.0) return (p0+x*x*(p2+x*p3));
155         if (x< 2.0) return (q0+x*(q1+x*(q2+x*q3)));
156         return 0.0;
157 }
158
159 /* x ranges from -1 to 1 */
160 float RE_filter_value(int type, float x)
161 {
162         float gaussfac= 1.6f;
163         
164         x= ABS(x);
165         
166         switch(type) {
167                 case R_FILTER_BOX:
168                         if(x>1.0) return 0.0f;
169                         return 1.0;
170                         
171                 case R_FILTER_TENT:
172                         if(x>1.0) return 0.0f;
173                         return 1.0f-x;
174                         
175                 case R_FILTER_GAUSS:
176                         x*= gaussfac;
177                         return (1.0/exp(x*x) - 1.0/exp(gaussfac*gaussfac*2.25));
178                         
179                 case R_FILTER_MITCH:
180                         return filt_mitchell(x*gaussfac);
181                         
182                 case R_FILTER_QUAD:
183                         return filt_quadratic(x*gaussfac);
184                         
185                 case R_FILTER_CUBIC:
186                         return filt_cubic(x*gaussfac);
187                         
188                 case R_FILTER_CATROM:
189                         return filt_catrom(x*gaussfac);
190         }
191         return 0.0f;
192 }
193
194 static float calc_weight(Render *re, float *weight, int i, int j)
195 {
196         float x, y, dist, totw= 0.0;
197         int a;
198
199         for(a=0; a<re->osa; a++) {
200                 x= re->jit[a][0] + i;
201                 y= re->jit[a][1] + j;
202                 dist= sqrt(x*x+y*y);
203
204                 weight[a]= 0.0;
205
206                 /* Weighting choices */
207                 switch(re->r.filtertype) {
208                 case R_FILTER_BOX:
209                         if(i==0 && j==0) weight[a]= 1.0;
210                         break;
211                         
212                 case R_FILTER_TENT:
213                         if(dist < re->r.gauss)
214                                 weight[a]= re->r.gauss - dist;
215                         break;
216                         
217                 case R_FILTER_GAUSS:
218                         x = dist*re->r.gauss;
219                         weight[a]= (1.0/exp(x*x) - 1.0/exp(re->r.gauss*re->r.gauss*2.25));
220                         break;
221                 
222                 case R_FILTER_MITCH:
223                         weight[a]= filt_mitchell(dist*re->r.gauss);
224                         break;
225                 
226                 case R_FILTER_QUAD:
227                         weight[a]= filt_quadratic(dist*re->r.gauss);
228                         break;
229                         
230                 case R_FILTER_CUBIC:
231                         weight[a]= filt_cubic(dist*re->r.gauss);
232                         break;
233                         
234                 case R_FILTER_CATROM:
235                         weight[a]= filt_catrom(dist*re->r.gauss);
236                         break;
237                         
238                 }
239                 
240                 totw+= weight[a];
241
242         }
243         return totw;
244 }
245
246 void free_sample_tables(Render *re)
247 {
248         int a;
249         
250         if(re->samples) {
251                 for(a=0; a<9; a++) {
252                         MEM_freeN(re->samples->fmask1[a]);
253                         MEM_freeN(re->samples->fmask2[a]);
254                 }
255                 
256                 MEM_freeN(re->samples->centmask);
257                 MEM_freeN(re->samples);
258                 re->samples= NULL;
259         }
260 }
261
262 /* based on settings in render, it makes the lookup tables */
263 void make_sample_tables(Render *re)
264 {
265         static int firsttime= 1;
266         SampleTables *st;
267         float flweight[32];
268         float weight[32], totw, val, *fpx1, *fpx2, *fpy1, *fpy2, *m3, *m4;
269         int i, j, a;
270
271         /* optimization tables, only once */
272         if(firsttime) {
273                 firsttime= 0;
274         }
275         
276         free_sample_tables(re);
277         
278         init_render_jit(re);    /* needed for mblur too */
279         
280         if(re->osa==0) {
281                 /* just prevents cpu cycles for larger render and copying */
282                 re->r.filtertype= 0;
283                 return;
284         }
285         
286         st= re->samples= MEM_callocN(sizeof(SampleTables), "sample tables");
287         
288         for(a=0; a<9;a++) {
289                 st->fmask1[a]= MEM_callocN(256*sizeof(float), "initfilt");
290                 st->fmask2[a]= MEM_callocN(256*sizeof(float), "initfilt");
291         }
292         for(a=0; a<256; a++) {
293                 st->cmask[a]= 0;
294                 if(a &   1) st->cmask[a]++;
295                 if(a &   2) st->cmask[a]++;
296                 if(a &   4) st->cmask[a]++;
297                 if(a &   8) st->cmask[a]++;
298                 if(a &  16) st->cmask[a]++;
299                 if(a &  32) st->cmask[a]++;
300                 if(a &  64) st->cmask[a]++;
301                 if(a & 128) st->cmask[a]++;
302         }
303         
304         st->centmask= MEM_mallocN((1<<re->osa), "Initfilt3");
305         
306         for(a=0; a<16; a++) {
307                 st->centLut[a]= -0.45+((float)a)/16.0;
308         }
309
310         /* calculate totw */
311         totw= 0.0;
312         for(j= -1; j<2; j++) {
313                 for(i= -1; i<2; i++) {
314                         totw+= calc_weight(re, weight, i, j);
315                 }
316         }
317
318         for(j= -1; j<2; j++) {
319                 for(i= -1; i<2; i++) {
320                         /* calculate using jit, with offset the weights */
321
322                         memset(weight, 0, sizeof(weight));
323                         calc_weight(re, weight, i, j);
324
325                         for(a=0; a<16; a++) flweight[a]= weight[a]*(1.0/totw);
326
327                         m3= st->fmask1[ 3*(j+1)+i+1 ];
328                         m4= st->fmask2[ 3*(j+1)+i+1 ];
329
330                         for(a=0; a<256; a++) {
331                                 if(a &   1) {
332                                         m3[a]+= flweight[0];
333                                         m4[a]+= flweight[8];
334                                 }
335                                 if(a &   2) {
336                                         m3[a]+= flweight[1];
337                                         m4[a]+= flweight[9];
338                                 }
339                                 if(a &   4) {
340                                         m3[a]+= flweight[2];
341                                         m4[a]+= flweight[10];
342                                 }
343                                 if(a &   8) {
344                                         m3[a]+= flweight[3];
345                                         m4[a]+= flweight[11];
346                                 }
347                                 if(a &  16) {
348                                         m3[a]+= flweight[4];
349                                         m4[a]+= flweight[12];
350                                 }
351                                 if(a &  32) {
352                                         m3[a]+= flweight[5];
353                                         m4[a]+= flweight[13];
354                                 }
355                                 if(a &  64) {
356                                         m3[a]+= flweight[6];
357                                         m4[a]+= flweight[14];
358                                 }
359                                 if(a & 128) {
360                                         m3[a]+= flweight[7];
361                                         m4[a]+= flweight[15];
362                                 }
363                         }
364                 }
365         }
366
367         /* centmask: the correct subpixel offset per mask */
368
369         fpx1= MEM_mallocN(256*sizeof(float), "initgauss4");
370         fpx2= MEM_mallocN(256*sizeof(float), "initgauss4");
371         fpy1= MEM_mallocN(256*sizeof(float), "initgauss4");
372         fpy2= MEM_mallocN(256*sizeof(float), "initgauss4");
373         for(a=0; a<256; a++) {
374                 fpx1[a]= fpx2[a]= 0.0;
375                 fpy1[a]= fpy2[a]= 0.0;
376                 if(a & 1) {
377                         fpx1[a]+= re->jit[0][0];
378                         fpy1[a]+= re->jit[0][1];
379                         fpx2[a]+= re->jit[8][0];
380                         fpy2[a]+= re->jit[8][1];
381                 }
382                 if(a & 2) {
383                         fpx1[a]+= re->jit[1][0];
384                         fpy1[a]+= re->jit[1][1];
385                         fpx2[a]+= re->jit[9][0];
386                         fpy2[a]+= re->jit[9][1];
387                 }
388                 if(a & 4) {
389                         fpx1[a]+= re->jit[2][0];
390                         fpy1[a]+= re->jit[2][1];
391                         fpx2[a]+= re->jit[10][0];
392                         fpy2[a]+= re->jit[10][1];
393                 }
394                 if(a & 8) {
395                         fpx1[a]+= re->jit[3][0];
396                         fpy1[a]+= re->jit[3][1];
397                         fpx2[a]+= re->jit[11][0];
398                         fpy2[a]+= re->jit[11][1];
399                 }
400                 if(a & 16) {
401                         fpx1[a]+= re->jit[4][0];
402                         fpy1[a]+= re->jit[4][1];
403                         fpx2[a]+= re->jit[12][0];
404                         fpy2[a]+= re->jit[12][1];
405                 }
406                 if(a & 32) {
407                         fpx1[a]+= re->jit[5][0];
408                         fpy1[a]+= re->jit[5][1];
409                         fpx2[a]+= re->jit[13][0];
410                         fpy2[a]+= re->jit[13][1];
411                 }
412                 if(a & 64) {
413                         fpx1[a]+= re->jit[6][0];
414                         fpy1[a]+= re->jit[6][1];
415                         fpx2[a]+= re->jit[14][0];
416                         fpy2[a]+= re->jit[14][1];
417                 }
418                 if(a & 128) {
419                         fpx1[a]+= re->jit[7][0];
420                         fpy1[a]+= re->jit[7][1];
421                         fpx2[a]+= re->jit[15][0];
422                         fpy2[a]+= re->jit[15][1];
423                 }
424         }
425
426         for(a= (1<<re->osa)-1; a>0; a--) {
427                 val= st->cmask[a & 255] + st->cmask[a>>8];
428                 i= 8+(15.9*(fpy1[a & 255]+fpy2[a>>8])/val);
429                 CLAMP(i, 0, 15);
430                 j= 8+(15.9*(fpx1[a & 255]+fpx2[a>>8])/val);
431                 CLAMP(j, 0, 15);
432                 i= j + (i<<4);
433                 st->centmask[a]= i;
434         }
435
436         MEM_freeN(fpx1);
437         MEM_freeN(fpx2);
438         MEM_freeN(fpy1);
439         MEM_freeN(fpy2);
440 }
441
442
443 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
444
445 /* call this after InitState() */
446 /* per render, there's one persistant viewplane. Parts will set their own viewplanes */
447 void RE_SetCamera(Render *re, Object *camera)
448 {
449         object_camera_matrix(&re->r, camera, re->winx, re->winy, re->flag & R_SEC_FIELD,
450                         re->winmat, &re->viewplane, &re->clipsta, &re->clipend,
451                         &re->lens, &re->ycor, &re->viewdx, &re->viewdy);
452 }
453
454 void RE_SetPixelSize(Render *re, float pixsize)
455 {
456         re->viewdx= pixsize;
457         re->viewdy= re->ycor*pixsize;
458 }
459
460 void RE_GetCameraWindow(struct Render *re, struct Object *camera, int frame, float mat[][4])
461 {
462         re->r.cfra= frame;
463         RE_SetCamera(re, camera);
464         copy_m4_m4(mat, re->winmat);
465 }
466
467 /* ~~~~~~~~~~~~~~~~ part (tile) calculus ~~~~~~~~~~~~~~~~~~~~~~ */
468
469
470 void freeparts(Render *re)
471 {
472         RenderPart *part= re->parts.first;
473         
474         while(part) {
475                 if(part->rectp) MEM_freeN(part->rectp);
476                 if(part->rectz) MEM_freeN(part->rectz);
477                 part= part->next;
478         }
479         BLI_freelistN(&re->parts);
480 }
481
482 void initparts(Render *re)
483 {
484         int nr, xd, yd, partx, party, xparts, yparts;
485         int xminb, xmaxb, yminb, ymaxb;
486         
487         freeparts(re);
488         
489         /* this is render info for caller, is not reset when parts are freed! */
490         re->i.totpart= 0;
491         re->i.curpart= 0;
492         re->i.partsdone= 0;
493         
494         /* just for readable code.. */
495         xminb= re->disprect.xmin;
496         yminb= re->disprect.ymin;
497         xmaxb= re->disprect.xmax;
498         ymaxb= re->disprect.ymax;
499         
500         xparts= re->r.xparts;
501         yparts= re->r.yparts;
502         
503         /* mininum part size, but for exr tile saving it was checked already */
504         if(!(re->r.scemode & (R_EXR_TILE_FILE|R_FULL_SAMPLE))) {
505                 if(re->r.mode & R_PANORAMA) {
506                         if(ceil(re->rectx/(float)xparts) < 8) 
507                                 xparts= 1 + re->rectx/8;
508                 }
509                 else
510                         if(ceil(re->rectx/(float)xparts) < 64) 
511                                 xparts= 1 + re->rectx/64;
512                 
513                 if(ceil(re->recty/(float)yparts) < 64) 
514                         yparts= 1 + re->recty/64;
515         }
516         
517         /* part size */
518         partx= ceil(re->rectx/(float)xparts);
519         party= ceil(re->recty/(float)yparts);
520         
521         re->xparts= xparts;
522         re->yparts= yparts;
523         re->partx= partx;
524         re->party= party;
525         
526         /* calculate rotation factor of 1 pixel */
527         if(re->r.mode & R_PANORAMA)
528                 re->panophi= panorama_pixel_rot(re);
529         
530         for(nr=0; nr<xparts*yparts; nr++) {
531                 rcti disprect;
532                 int rectx, recty;
533                 
534                 xd= (nr % xparts);
535                 yd= (nr-xd)/xparts;
536                 
537                 disprect.xmin= xminb+ xd*partx;
538                 disprect.ymin= yminb+ yd*party;
539                 
540                 /* ensure we cover the entire picture, so last parts go to end */
541                 if(xd<xparts-1) {
542                         disprect.xmax= disprect.xmin + partx;
543                         if(disprect.xmax > xmaxb)
544                                 disprect.xmax = xmaxb;
545                 }
546                 else disprect.xmax= xmaxb;
547                 
548                 if(yd<yparts-1) {
549                         disprect.ymax= disprect.ymin + party;
550                         if(disprect.ymax > ymaxb)
551                                 disprect.ymax = ymaxb;
552                 }
553                 else disprect.ymax= ymaxb;
554                 
555                 rectx= disprect.xmax - disprect.xmin;
556                 recty= disprect.ymax - disprect.ymin;
557                 
558                 /* so, now can we add this part? */
559                 if(rectx>0 && recty>0) {
560                         RenderPart *pa= MEM_callocN(sizeof(RenderPart), "new part");
561                         
562                         /* Non-box filters need 2 pixels extra to work */
563                         if((re->r.filtertype || (re->r.mode & R_EDGE))) {
564                                 pa->crop= 2;
565                                 disprect.xmin -= pa->crop;
566                                 disprect.ymin -= pa->crop;
567                                 disprect.xmax += pa->crop;
568                                 disprect.ymax += pa->crop;
569                                 rectx+= 2*pa->crop;
570                                 recty+= 2*pa->crop;
571                         }
572                         pa->disprect= disprect;
573                         pa->rectx= rectx;
574                         pa->recty= recty;
575
576                         BLI_addtail(&re->parts, pa);
577                         re->i.totpart++;
578                 }
579         }
580 }
581
582
583