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