|
Blender
V2.59
|
00001 /* 00002 * $Id: initrender.c 36384 2011-04-30 04:29:36Z campbellbarton $ 00003 * 00004 * ***** BEGIN GPL LICENSE BLOCK ***** 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software Foundation, 00018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 * 00020 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. 00021 * All rights reserved. 00022 * 00023 * Contributors: 2004/2005/2006 Blender Foundation, full recode 00024 * 00025 * ***** END GPL LICENSE BLOCK ***** 00026 */ 00027 00034 /* Global includes */ 00035 00036 #include <math.h> 00037 #include <stdlib.h> 00038 #include <string.h> 00039 #include <stdio.h> 00040 00041 #include "MEM_guardedalloc.h" 00042 00043 #include "PIL_time.h" 00044 00045 #include "BLI_math.h" 00046 #include "BLI_blenlib.h" 00047 #include "BLI_jitter.h" 00048 #include "BLI_utildefines.h" 00049 00050 #include "DNA_camera_types.h" 00051 #include "DNA_group_types.h" 00052 #include "DNA_image_types.h" 00053 #include "DNA_lamp_types.h" 00054 #include "DNA_object_types.h" 00055 #include "DNA_scene_types.h" 00056 00057 00058 #include "BKE_global.h" 00059 #include "BKE_material.h" 00060 #include "BKE_object.h" 00061 #include "BKE_image.h" 00062 #include "BKE_ipo.h" 00063 #include "BKE_key.h" 00064 #include "BKE_action.h" 00065 #include "BKE_writeavi.h" 00066 #include "BKE_scene.h" 00067 00068 #include "IMB_imbuf_types.h" 00069 #include "IMB_imbuf.h" 00070 00071 #ifdef WITH_QUICKTIME 00072 #include "quicktime_export.h" 00073 #endif 00074 00075 /* this module */ 00076 #include "renderpipeline.h" 00077 #include "render_types.h" 00078 00079 #include "rendercore.h" 00080 #include "pixelshading.h" 00081 #include "zbuf.h" 00082 00083 /* Own includes */ 00084 #include "initrender.h" 00085 00086 00087 /* ********************** */ 00088 00089 static void init_render_jit(Render *re) 00090 { 00091 static float jit[32][2]; /* simple caching */ 00092 static float mblur_jit[32][2]; /* simple caching */ 00093 static int lastjit= 0; 00094 static int last_mblur_jit= 0; 00095 00096 if(lastjit!=re->r.osa || last_mblur_jit != re->r.mblur_samples) { 00097 memset(jit, 0, sizeof(jit)); 00098 BLI_initjit(jit[0], re->r.osa); 00099 00100 memset(mblur_jit, 0, sizeof(mblur_jit)); 00101 BLI_initjit(mblur_jit[0], re->r.mblur_samples); 00102 } 00103 00104 lastjit= re->r.osa; 00105 memcpy(re->jit, jit, sizeof(jit)); 00106 00107 last_mblur_jit= re->r.mblur_samples; 00108 memcpy(re->mblur_jit, mblur_jit, sizeof(mblur_jit)); 00109 } 00110 00111 00112 /* ****************** MASKS and LUTS **************** */ 00113 00114 static float filt_quadratic(float x) 00115 { 00116 if (x < 0.0f) x = -x; 00117 if (x < 0.5f) return 0.75f-(x*x); 00118 if (x < 1.5f) return 0.50f*(x-1.5f)*(x-1.5f); 00119 return 0.0f; 00120 } 00121 00122 00123 static float filt_cubic(float x) 00124 { 00125 float x2= x*x; 00126 00127 if (x < 0.0f) x = -x; 00128 00129 if (x < 1.0f) return 0.5*x*x2 - x2 + 2.0f/3.0f; 00130 if (x < 2.0f) return (2.0-x)*(2.0-x)*(2.0-x)/6.0f; 00131 return 0.0f; 00132 } 00133 00134 00135 static float filt_catrom(float x) 00136 { 00137 float x2= x*x; 00138 00139 if (x < 0.0f) x = -x; 00140 if (x < 1.0f) return 1.5f*x2*x - 2.5f*x2 + 1.0f; 00141 if (x < 2.0f) return -0.5f*x2*x + 2.5*x2 - 4.0f*x + 2.0f; 00142 return 0.0f; 00143 } 00144 00145 static float filt_mitchell(float x) /* Mitchell & Netravali's two-param cubic */ 00146 { 00147 float b = 1.0f/3.0f, c = 1.0f/3.0f; 00148 float p0 = ( 6.0 - 2.0*b ) / 6.0; 00149 float p2 = (-18.0 + 12.0*b + 6.0*c) / 6.0; 00150 float p3 = ( 12.0 - 9.0*b - 6.0*c) / 6.0; 00151 float q0 = ( 8.0*b + 24.0*c) / 6.0; 00152 float q1 = ( - 12.0*b - 48.0*c) / 6.0; 00153 float q2 = ( 6.0*b + 30.0*c) / 6.0; 00154 float q3 = ( - b - 6.0*c) / 6.0; 00155 00156 if (x<-2.0) return 0.0; 00157 if (x<-1.0) return (q0-x*(q1-x*(q2-x*q3))); 00158 if (x< 0.0) return (p0+x*x*(p2-x*p3)); 00159 if (x< 1.0) return (p0+x*x*(p2+x*p3)); 00160 if (x< 2.0) return (q0+x*(q1+x*(q2+x*q3))); 00161 return 0.0; 00162 } 00163 00164 /* x ranges from -1 to 1 */ 00165 float RE_filter_value(int type, float x) 00166 { 00167 float gaussfac= 1.6f; 00168 00169 x= ABS(x); 00170 00171 switch(type) { 00172 case R_FILTER_BOX: 00173 if(x>1.0) return 0.0f; 00174 return 1.0; 00175 00176 case R_FILTER_TENT: 00177 if(x>1.0) return 0.0f; 00178 return 1.0f-x; 00179 00180 case R_FILTER_GAUSS: 00181 x*= gaussfac; 00182 return (1.0/exp(x*x) - 1.0/exp(gaussfac*gaussfac*2.25)); 00183 00184 case R_FILTER_MITCH: 00185 return filt_mitchell(x*gaussfac); 00186 00187 case R_FILTER_QUAD: 00188 return filt_quadratic(x*gaussfac); 00189 00190 case R_FILTER_CUBIC: 00191 return filt_cubic(x*gaussfac); 00192 00193 case R_FILTER_CATROM: 00194 return filt_catrom(x*gaussfac); 00195 } 00196 return 0.0f; 00197 } 00198 00199 static float calc_weight(Render *re, float *weight, int i, int j) 00200 { 00201 float x, y, dist, totw= 0.0; 00202 int a; 00203 00204 for(a=0; a<re->osa; a++) { 00205 x= re->jit[a][0] + i; 00206 y= re->jit[a][1] + j; 00207 dist= sqrt(x*x+y*y); 00208 00209 weight[a]= 0.0; 00210 00211 /* Weighting choices */ 00212 switch(re->r.filtertype) { 00213 case R_FILTER_BOX: 00214 if(i==0 && j==0) weight[a]= 1.0; 00215 break; 00216 00217 case R_FILTER_TENT: 00218 if(dist < re->r.gauss) 00219 weight[a]= re->r.gauss - dist; 00220 break; 00221 00222 case R_FILTER_GAUSS: 00223 x = dist*re->r.gauss; 00224 weight[a]= (1.0/exp(x*x) - 1.0/exp(re->r.gauss*re->r.gauss*2.25)); 00225 break; 00226 00227 case R_FILTER_MITCH: 00228 weight[a]= filt_mitchell(dist*re->r.gauss); 00229 break; 00230 00231 case R_FILTER_QUAD: 00232 weight[a]= filt_quadratic(dist*re->r.gauss); 00233 break; 00234 00235 case R_FILTER_CUBIC: 00236 weight[a]= filt_cubic(dist*re->r.gauss); 00237 break; 00238 00239 case R_FILTER_CATROM: 00240 weight[a]= filt_catrom(dist*re->r.gauss); 00241 break; 00242 00243 } 00244 00245 totw+= weight[a]; 00246 00247 } 00248 return totw; 00249 } 00250 00251 void free_sample_tables(Render *re) 00252 { 00253 int a; 00254 00255 if(re->samples) { 00256 for(a=0; a<9; a++) { 00257 MEM_freeN(re->samples->fmask1[a]); 00258 MEM_freeN(re->samples->fmask2[a]); 00259 } 00260 00261 MEM_freeN(re->samples->centmask); 00262 MEM_freeN(re->samples); 00263 re->samples= NULL; 00264 } 00265 } 00266 00267 /* based on settings in render, it makes the lookup tables */ 00268 void make_sample_tables(Render *re) 00269 { 00270 static int firsttime= 1; 00271 SampleTables *st; 00272 float flweight[32]; 00273 float weight[32], totw, val, *fpx1, *fpx2, *fpy1, *fpy2, *m3, *m4; 00274 int i, j, a; 00275 00276 /* optimization tables, only once */ 00277 if(firsttime) { 00278 firsttime= 0; 00279 } 00280 00281 free_sample_tables(re); 00282 00283 init_render_jit(re); /* needed for mblur too */ 00284 00285 if(re->osa==0) { 00286 /* just prevents cpu cycles for larger render and copying */ 00287 re->r.filtertype= 0; 00288 return; 00289 } 00290 00291 st= re->samples= MEM_callocN(sizeof(SampleTables), "sample tables"); 00292 00293 for(a=0; a<9;a++) { 00294 st->fmask1[a]= MEM_callocN(256*sizeof(float), "initfilt"); 00295 st->fmask2[a]= MEM_callocN(256*sizeof(float), "initfilt"); 00296 } 00297 for(a=0; a<256; a++) { 00298 st->cmask[a]= 0; 00299 if(a & 1) st->cmask[a]++; 00300 if(a & 2) st->cmask[a]++; 00301 if(a & 4) st->cmask[a]++; 00302 if(a & 8) st->cmask[a]++; 00303 if(a & 16) st->cmask[a]++; 00304 if(a & 32) st->cmask[a]++; 00305 if(a & 64) st->cmask[a]++; 00306 if(a & 128) st->cmask[a]++; 00307 } 00308 00309 st->centmask= MEM_mallocN((1<<re->osa), "Initfilt3"); 00310 00311 for(a=0; a<16; a++) { 00312 st->centLut[a]= -0.45+((float)a)/16.0; 00313 } 00314 00315 /* calculate totw */ 00316 totw= 0.0; 00317 for(j= -1; j<2; j++) { 00318 for(i= -1; i<2; i++) { 00319 totw+= calc_weight(re, weight, i, j); 00320 } 00321 } 00322 00323 for(j= -1; j<2; j++) { 00324 for(i= -1; i<2; i++) { 00325 /* calculate using jit, with offset the weights */ 00326 00327 memset(weight, 0, sizeof(weight)); 00328 calc_weight(re, weight, i, j); 00329 00330 for(a=0; a<16; a++) flweight[a]= weight[a]*(1.0/totw); 00331 00332 m3= st->fmask1[ 3*(j+1)+i+1 ]; 00333 m4= st->fmask2[ 3*(j+1)+i+1 ]; 00334 00335 for(a=0; a<256; a++) { 00336 if(a & 1) { 00337 m3[a]+= flweight[0]; 00338 m4[a]+= flweight[8]; 00339 } 00340 if(a & 2) { 00341 m3[a]+= flweight[1]; 00342 m4[a]+= flweight[9]; 00343 } 00344 if(a & 4) { 00345 m3[a]+= flweight[2]; 00346 m4[a]+= flweight[10]; 00347 } 00348 if(a & 8) { 00349 m3[a]+= flweight[3]; 00350 m4[a]+= flweight[11]; 00351 } 00352 if(a & 16) { 00353 m3[a]+= flweight[4]; 00354 m4[a]+= flweight[12]; 00355 } 00356 if(a & 32) { 00357 m3[a]+= flweight[5]; 00358 m4[a]+= flweight[13]; 00359 } 00360 if(a & 64) { 00361 m3[a]+= flweight[6]; 00362 m4[a]+= flweight[14]; 00363 } 00364 if(a & 128) { 00365 m3[a]+= flweight[7]; 00366 m4[a]+= flweight[15]; 00367 } 00368 } 00369 } 00370 } 00371 00372 /* centmask: the correct subpixel offset per mask */ 00373 00374 fpx1= MEM_mallocN(256*sizeof(float), "initgauss4"); 00375 fpx2= MEM_mallocN(256*sizeof(float), "initgauss4"); 00376 fpy1= MEM_mallocN(256*sizeof(float), "initgauss4"); 00377 fpy2= MEM_mallocN(256*sizeof(float), "initgauss4"); 00378 for(a=0; a<256; a++) { 00379 fpx1[a]= fpx2[a]= 0.0; 00380 fpy1[a]= fpy2[a]= 0.0; 00381 if(a & 1) { 00382 fpx1[a]+= re->jit[0][0]; 00383 fpy1[a]+= re->jit[0][1]; 00384 fpx2[a]+= re->jit[8][0]; 00385 fpy2[a]+= re->jit[8][1]; 00386 } 00387 if(a & 2) { 00388 fpx1[a]+= re->jit[1][0]; 00389 fpy1[a]+= re->jit[1][1]; 00390 fpx2[a]+= re->jit[9][0]; 00391 fpy2[a]+= re->jit[9][1]; 00392 } 00393 if(a & 4) { 00394 fpx1[a]+= re->jit[2][0]; 00395 fpy1[a]+= re->jit[2][1]; 00396 fpx2[a]+= re->jit[10][0]; 00397 fpy2[a]+= re->jit[10][1]; 00398 } 00399 if(a & 8) { 00400 fpx1[a]+= re->jit[3][0]; 00401 fpy1[a]+= re->jit[3][1]; 00402 fpx2[a]+= re->jit[11][0]; 00403 fpy2[a]+= re->jit[11][1]; 00404 } 00405 if(a & 16) { 00406 fpx1[a]+= re->jit[4][0]; 00407 fpy1[a]+= re->jit[4][1]; 00408 fpx2[a]+= re->jit[12][0]; 00409 fpy2[a]+= re->jit[12][1]; 00410 } 00411 if(a & 32) { 00412 fpx1[a]+= re->jit[5][0]; 00413 fpy1[a]+= re->jit[5][1]; 00414 fpx2[a]+= re->jit[13][0]; 00415 fpy2[a]+= re->jit[13][1]; 00416 } 00417 if(a & 64) { 00418 fpx1[a]+= re->jit[6][0]; 00419 fpy1[a]+= re->jit[6][1]; 00420 fpx2[a]+= re->jit[14][0]; 00421 fpy2[a]+= re->jit[14][1]; 00422 } 00423 if(a & 128) { 00424 fpx1[a]+= re->jit[7][0]; 00425 fpy1[a]+= re->jit[7][1]; 00426 fpx2[a]+= re->jit[15][0]; 00427 fpy2[a]+= re->jit[15][1]; 00428 } 00429 } 00430 00431 for(a= (1<<re->osa)-1; a>0; a--) { 00432 val= st->cmask[a & 255] + st->cmask[a>>8]; 00433 i= 8+(15.9*(fpy1[a & 255]+fpy2[a>>8])/val); 00434 CLAMP(i, 0, 15); 00435 j= 8+(15.9*(fpx1[a & 255]+fpx2[a>>8])/val); 00436 CLAMP(j, 0, 15); 00437 i= j + (i<<4); 00438 st->centmask[a]= i; 00439 } 00440 00441 MEM_freeN(fpx1); 00442 MEM_freeN(fpx2); 00443 MEM_freeN(fpy1); 00444 MEM_freeN(fpy2); 00445 } 00446 00447 00448 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ 00449 00450 struct Object *RE_GetCamera(Render *re) 00451 { 00452 return re->camera_override ? re->camera_override : re->scene->camera; 00453 } 00454 00455 /* call this after InitState() */ 00456 /* per render, there's one persistant viewplane. Parts will set their own viewplanes */ 00457 void RE_SetCamera(Render *re, Object *camera) 00458 { 00459 object_camera_mode(&re->r, camera); 00460 00461 object_camera_matrix(&re->r, camera, re->winx, re->winy, re->flag & R_SEC_FIELD, 00462 re->winmat, &re->viewplane, &re->clipsta, &re->clipend, 00463 &re->lens, &re->ycor, &re->viewdx, &re->viewdy); 00464 } 00465 00466 void RE_SetPixelSize(Render *re, float pixsize) 00467 { 00468 re->viewdx= pixsize; 00469 re->viewdy= re->ycor*pixsize; 00470 } 00471 00472 void RE_GetCameraWindow(struct Render *re, struct Object *camera, int frame, float mat[][4]) 00473 { 00474 re->r.cfra= frame; 00475 RE_SetCamera(re, camera); 00476 copy_m4_m4(mat, re->winmat); 00477 } 00478 00479 /* ~~~~~~~~~~~~~~~~ part (tile) calculus ~~~~~~~~~~~~~~~~~~~~~~ */ 00480 00481 00482 void freeparts(Render *re) 00483 { 00484 RenderPart *part= re->parts.first; 00485 00486 while(part) { 00487 if(part->rectp) MEM_freeN(part->rectp); 00488 if(part->rectz) MEM_freeN(part->rectz); 00489 part= part->next; 00490 } 00491 BLI_freelistN(&re->parts); 00492 } 00493 00494 void initparts(Render *re) 00495 { 00496 int nr, xd, yd, partx, party, xparts, yparts; 00497 int xminb, xmaxb, yminb, ymaxb; 00498 00499 freeparts(re); 00500 00501 /* this is render info for caller, is not reset when parts are freed! */ 00502 re->i.totpart= 0; 00503 re->i.curpart= 0; 00504 re->i.partsdone= 0; 00505 00506 /* just for readable code.. */ 00507 xminb= re->disprect.xmin; 00508 yminb= re->disprect.ymin; 00509 xmaxb= re->disprect.xmax; 00510 ymaxb= re->disprect.ymax; 00511 00512 xparts= re->r.xparts; 00513 yparts= re->r.yparts; 00514 00515 /* mininum part size, but for exr tile saving it was checked already */ 00516 if(!(re->r.scemode & (R_EXR_TILE_FILE|R_FULL_SAMPLE))) { 00517 if(re->r.mode & R_PANORAMA) { 00518 if(ceil(re->rectx/(float)xparts) < 8) 00519 xparts= 1 + re->rectx/8; 00520 } 00521 else 00522 if(ceil(re->rectx/(float)xparts) < 64) 00523 xparts= 1 + re->rectx/64; 00524 00525 if(ceil(re->recty/(float)yparts) < 64) 00526 yparts= 1 + re->recty/64; 00527 } 00528 00529 /* part size */ 00530 partx= ceil(re->rectx/(float)xparts); 00531 party= ceil(re->recty/(float)yparts); 00532 00533 re->xparts= xparts; 00534 re->yparts= yparts; 00535 re->partx= partx; 00536 re->party= party; 00537 00538 /* calculate rotation factor of 1 pixel */ 00539 if(re->r.mode & R_PANORAMA) 00540 re->panophi= panorama_pixel_rot(re); 00541 00542 for(nr=0; nr<xparts*yparts; nr++) { 00543 rcti disprect; 00544 int rectx, recty; 00545 00546 xd= (nr % xparts); 00547 yd= (nr-xd)/xparts; 00548 00549 disprect.xmin= xminb+ xd*partx; 00550 disprect.ymin= yminb+ yd*party; 00551 00552 /* ensure we cover the entire picture, so last parts go to end */ 00553 if(xd<xparts-1) { 00554 disprect.xmax= disprect.xmin + partx; 00555 if(disprect.xmax > xmaxb) 00556 disprect.xmax = xmaxb; 00557 } 00558 else disprect.xmax= xmaxb; 00559 00560 if(yd<yparts-1) { 00561 disprect.ymax= disprect.ymin + party; 00562 if(disprect.ymax > ymaxb) 00563 disprect.ymax = ymaxb; 00564 } 00565 else disprect.ymax= ymaxb; 00566 00567 rectx= disprect.xmax - disprect.xmin; 00568 recty= disprect.ymax - disprect.ymin; 00569 00570 /* so, now can we add this part? */ 00571 if(rectx>0 && recty>0) { 00572 RenderPart *pa= MEM_callocN(sizeof(RenderPart), "new part"); 00573 00574 /* Non-box filters need 2 pixels extra to work */ 00575 if((re->r.filtertype || (re->r.mode & R_EDGE))) { 00576 pa->crop= 2; 00577 disprect.xmin -= pa->crop; 00578 disprect.ymin -= pa->crop; 00579 disprect.xmax += pa->crop; 00580 disprect.ymax += pa->crop; 00581 rectx+= 2*pa->crop; 00582 recty+= 2*pa->crop; 00583 } 00584 pa->disprect= disprect; 00585 pa->rectx= rectx; 00586 pa->recty= recty; 00587 00588 BLI_addtail(&re->parts, pa); 00589 re->i.totpart++; 00590 } 00591 } 00592 } 00593 00594 00595