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