Fix T52835: When driven IK influence change, ik animation have 1 frame delay
[blender.git] / source / blender / render / intern / source / initrender.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributors: 2004/2005/2006 Blender Foundation, full recode
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/render/intern/source/initrender.c
27  *  \ingroup render
28  */
29
30 /* Global includes */
31
32 #include <math.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stdio.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "BLI_math.h"
40 #include "BLI_blenlib.h"
41 #include "BLI_jitter.h"
42 #include "BLI_utildefines.h"
43
44 #include "DNA_camera_types.h"
45 #include "DNA_image_types.h"
46 #include "DNA_object_types.h"
47 #include "DNA_scene_types.h"
48
49 #include "BKE_camera.h"
50
51 /* this module */
52 #include "renderpipeline.h"
53 #include "render_types.h"
54
55 /* Own includes */
56 #include "initrender.h"
57
58
59 /* ********************** */
60
61 static void init_render_jit(Render *re)
62 {
63         static float jit[32][2];        /* simple caching */
64         static float mblur_jit[32][2];  /* simple caching */
65         static int lastjit = 0;
66         static int last_mblur_jit = 0;
67         
68         if (lastjit != re->r.osa || last_mblur_jit != re->r.mblur_samples) {
69                 memset(jit, 0, sizeof(jit));
70                 BLI_jitter_init(jit, re->r.osa);
71                 
72                 memset(mblur_jit, 0, sizeof(mblur_jit));
73                 BLI_jitter_init(mblur_jit, re->r.mblur_samples);
74         }
75         
76         lastjit = re->r.osa;
77         memcpy(re->jit, jit, sizeof(jit));
78         
79         last_mblur_jit = re->r.mblur_samples;
80         memcpy(re->mblur_jit, mblur_jit, sizeof(mblur_jit));
81 }
82
83
84 /* ****************** MASKS and LUTS **************** */
85
86 static float filt_quadratic(float x)
87 {
88         if (x <  0.0f) x = -x;
89         if (x < 0.5f) return 0.75f - (x * x);
90         if (x < 1.5f) return 0.50f * (x - 1.5f) * (x - 1.5f);
91         return 0.0f;
92 }
93
94
95 static float filt_cubic(float x)
96 {
97         float x2 = x * x;
98         
99         if (x <  0.0f) x = -x;
100         
101         if (x < 1.0f) return 0.5f * x * x2 - x2 + 2.0f / 3.0f;
102         if (x < 2.0f) return (2.0f - x) * (2.0f - x) * (2.0f - x) / 6.0f;
103         return 0.0f;
104 }
105
106
107 static float filt_catrom(float x)
108 {
109         float x2 = x * x;
110         
111         if (x <  0.0f) x = -x;
112         if (x < 1.0f) return  1.5f * x2 * x - 2.5f * x2 + 1.0f;
113         if (x < 2.0f) return -0.5f * x2 * x + 2.5f * x2 - 4.0f * x + 2.0f;
114         return 0.0f;
115 }
116
117 static float filt_mitchell(float x) /* Mitchell & Netravali's two-param cubic */
118 {
119         float b = 1.0f / 3.0f, c = 1.0f / 3.0f;
120         float p0 = (  6.0f -  2.0f * b)             / 6.0f;
121         float p2 = (-18.0f + 12.0f * b +  6.0f * c) / 6.0f;
122         float p3 = ( 12.0f -  9.0f * b -  6.0f * c) / 6.0f;
123         float q0 = (          8.0f * b + 24.0f * c) / 6.0f;
124         float q1 = (        -12.0f * b - 48.0f * c) / 6.0f;
125         float q2 = (          6.0f * b + 30.0f * c) / 6.0f;
126         float q3 = (                -b -  6.0f * c) / 6.0f;
127
128         if (x < -2.0f) return 0.0f;
129         if (x < -1.0f) return (q0 - x * (q1 - x * (q2 - x * q3)));
130         if (x < 0.0f) return (p0 + x * x * (p2 - x * p3));
131         if (x < 1.0f) return (p0 + x * x * (p2 + x * p3));
132         if (x < 2.0f) return (q0 + x * (q1 + x * (q2 + x * q3)));
133         return 0.0f;
134 }
135
136 /* x ranges from -1 to 1 */
137 float RE_filter_value(int type, float x)
138 {
139         float gaussfac = 1.6f;
140         
141         x = ABS(x);
142         
143         switch (type) {
144                 case R_FILTER_BOX:
145                         if (x > 1.0f) return 0.0f;
146                         return 1.0f;
147                         
148                 case R_FILTER_TENT:
149                         if (x > 1.0f) return 0.0f;
150                         return 1.0f - x;
151                         
152                 case R_FILTER_GAUSS:
153                 {
154                         const float two_gaussfac2 = 2.0f * gaussfac * gaussfac;
155                         x *= 3.0f * gaussfac;
156                         return 1.0f / sqrtf((float)M_PI * two_gaussfac2) * expf(-x*x / two_gaussfac2);
157                 }
158                         
159                 case R_FILTER_MITCH:
160                         return filt_mitchell(x * gaussfac);
161                         
162                 case R_FILTER_QUAD:
163                         return filt_quadratic(x * gaussfac);
164                         
165                 case R_FILTER_CUBIC:
166                         return filt_cubic(x * gaussfac);
167                         
168                 case R_FILTER_CATROM:
169                         return filt_catrom(x * gaussfac);
170         }
171         return 0.0f;
172 }
173
174 static float calc_weight(Render *re, float *weight, int i, int j)
175 {
176         float x, y, dist, totw = 0.0;
177         int a;
178
179         for (a = 0; a < re->osa; a++) {
180                 x = re->jit[a][0] + i;
181                 y = re->jit[a][1] + j;
182                 dist = sqrtf(x * x + y * y);
183
184                 weight[a] = 0.0;
185
186                 /* Weighting choices */
187                 switch (re->r.filtertype) {
188                         case R_FILTER_BOX:
189                                 if (i == 0 && j == 0) weight[a] = 1.0;
190                                 break;
191                         
192                         case R_FILTER_TENT:
193                                 if (dist < re->r.gauss)
194                                         weight[a] = re->r.gauss - dist;
195                                 break;
196                         
197                         case R_FILTER_GAUSS:
198                                 x = dist * re->r.gauss;
199                                 weight[a] = (1.0f / expf(x * x) - 1.0f / expf(re->r.gauss * re->r.gauss * 2.25f));
200                                 break;
201                 
202                         case R_FILTER_MITCH:
203                                 weight[a] = filt_mitchell(dist * re->r.gauss);
204                                 break;
205                 
206                         case R_FILTER_QUAD:
207                                 weight[a] = filt_quadratic(dist * re->r.gauss);
208                                 break;
209                         
210                         case R_FILTER_CUBIC:
211                                 weight[a] = filt_cubic(dist * re->r.gauss);
212                                 break;
213                         
214                         case R_FILTER_CATROM:
215                                 weight[a] = filt_catrom(dist * re->r.gauss);
216                                 break;
217                         
218                 }
219                 
220                 totw += weight[a];
221
222         }
223         return totw;
224 }
225
226 void free_sample_tables(Render *re)
227 {
228         int a;
229         
230         if (re->samples) {
231                 for (a = 0; a < 9; a++) {
232                         MEM_freeN(re->samples->fmask1[a]);
233                         MEM_freeN(re->samples->fmask2[a]);
234                 }
235                 
236                 MEM_freeN(re->samples->centmask);
237                 MEM_freeN(re->samples);
238                 re->samples = NULL;
239         }
240 }
241
242 /* based on settings in render, it makes the lookup tables */
243 void make_sample_tables(Render *re)
244 {
245         static int firsttime = 1;
246         SampleTables *st;
247         float flweight[32];
248         float weight[32], totw, val, *fpx1, *fpx2, *fpy1, *fpy2, *m3, *m4;
249         int i, j, a, centmasksize;
250
251         /* optimization tables, only once */
252         if (firsttime) {
253                 firsttime = 0;
254         }
255         
256         free_sample_tables(re);
257         
258         init_render_jit(re);    /* needed for mblur too */
259         
260         if (re->osa == 0) {
261                 /* just prevents cpu cycles for larger render and copying */
262                 re->r.filtertype = 0;
263                 return;
264         }
265         
266         st = re->samples = MEM_callocN(sizeof(SampleTables), "sample tables");
267         
268         for (a = 0; a < 9; a++) {
269                 st->fmask1[a] = MEM_callocN(256 * sizeof(float), "initfilt");
270                 st->fmask2[a] = MEM_callocN(256 * sizeof(float), "initfilt");
271         }
272         for (a = 0; a < 256; a++) {
273                 st->cmask[a] = 0;
274                 if (a &   1) st->cmask[a]++;
275                 if (a &   2) st->cmask[a]++;
276                 if (a &   4) st->cmask[a]++;
277                 if (a &   8) st->cmask[a]++;
278                 if (a &  16) st->cmask[a]++;
279                 if (a &  32) st->cmask[a]++;
280                 if (a &  64) st->cmask[a]++;
281                 if (a & 128) st->cmask[a]++;
282         }
283         
284         centmasksize = (1 << re->osa);
285         st->centmask = MEM_mallocN(centmasksize, "Initfilt3");
286         
287         for (a = 0; a < 16; a++) {
288                 st->centLut[a] = -0.45f + ((float)a) / 16.0f;
289         }
290
291         /* calculate totw */
292         totw = 0.0;
293         for (j = -1; j < 2; j++) {
294                 for (i = -1; i < 2; i++) {
295                         totw += calc_weight(re, weight, i, j);
296                 }
297         }
298
299         for (j = -1; j < 2; j++) {
300                 for (i = -1; i < 2; i++) {
301                         /* calculate using jit, with offset the weights */
302
303                         memset(weight, 0, sizeof(weight));
304                         calc_weight(re, weight, i, j);
305
306                         for (a = 0; a < 16; a++) flweight[a] = weight[a] * (1.0f / totw);
307
308                         m3 = st->fmask1[3 * (j + 1) + i + 1];
309                         m4 = st->fmask2[3 * (j + 1) + i + 1];
310
311                         for (a = 0; a < 256; a++) {
312                                 if (a &   1) {
313                                         m3[a] += flweight[0];
314                                         m4[a] += flweight[8];
315                                 }
316                                 if (a &   2) {
317                                         m3[a] += flweight[1];
318                                         m4[a] += flweight[9];
319                                 }
320                                 if (a &   4) {
321                                         m3[a] += flweight[2];
322                                         m4[a] += flweight[10];
323                                 }
324                                 if (a &   8) {
325                                         m3[a] += flweight[3];
326                                         m4[a] += flweight[11];
327                                 }
328                                 if (a &  16) {
329                                         m3[a] += flweight[4];
330                                         m4[a] += flweight[12];
331                                 }
332                                 if (a &  32) {
333                                         m3[a] += flweight[5];
334                                         m4[a] += flweight[13];
335                                 }
336                                 if (a &  64) {
337                                         m3[a] += flweight[6];
338                                         m4[a] += flweight[14];
339                                 }
340                                 if (a & 128) {
341                                         m3[a] += flweight[7];
342                                         m4[a] += flweight[15];
343                                 }
344                         }
345                 }
346         }
347
348         /* centmask: the correct subpixel offset per mask */
349
350         fpx1 = MEM_mallocN(256 * sizeof(float), "initgauss4");
351         fpx2 = MEM_mallocN(256 * sizeof(float), "initgauss4");
352         fpy1 = MEM_mallocN(256 * sizeof(float), "initgauss4");
353         fpy2 = MEM_mallocN(256 * sizeof(float), "initgauss4");
354         for (a = 0; a < 256; a++) {
355                 fpx1[a] = fpx2[a] = 0.0;
356                 fpy1[a] = fpy2[a] = 0.0;
357                 if (a & 1) {
358                         fpx1[a] += re->jit[0][0];
359                         fpy1[a] += re->jit[0][1];
360                         fpx2[a] += re->jit[8][0];
361                         fpy2[a] += re->jit[8][1];
362                 }
363                 if (a & 2) {
364                         fpx1[a] += re->jit[1][0];
365                         fpy1[a] += re->jit[1][1];
366                         fpx2[a] += re->jit[9][0];
367                         fpy2[a] += re->jit[9][1];
368                 }
369                 if (a & 4) {
370                         fpx1[a] += re->jit[2][0];
371                         fpy1[a] += re->jit[2][1];
372                         fpx2[a] += re->jit[10][0];
373                         fpy2[a] += re->jit[10][1];
374                 }
375                 if (a & 8) {
376                         fpx1[a] += re->jit[3][0];
377                         fpy1[a] += re->jit[3][1];
378                         fpx2[a] += re->jit[11][0];
379                         fpy2[a] += re->jit[11][1];
380                 }
381                 if (a & 16) {
382                         fpx1[a] += re->jit[4][0];
383                         fpy1[a] += re->jit[4][1];
384                         fpx2[a] += re->jit[12][0];
385                         fpy2[a] += re->jit[12][1];
386                 }
387                 if (a & 32) {
388                         fpx1[a] += re->jit[5][0];
389                         fpy1[a] += re->jit[5][1];
390                         fpx2[a] += re->jit[13][0];
391                         fpy2[a] += re->jit[13][1];
392                 }
393                 if (a & 64) {
394                         fpx1[a] += re->jit[6][0];
395                         fpy1[a] += re->jit[6][1];
396                         fpx2[a] += re->jit[14][0];
397                         fpy2[a] += re->jit[14][1];
398                 }
399                 if (a & 128) {
400                         fpx1[a] += re->jit[7][0];
401                         fpy1[a] += re->jit[7][1];
402                         fpx2[a] += re->jit[15][0];
403                         fpy2[a] += re->jit[15][1];
404                 }
405         }
406
407         for (a = centmasksize - 1; a > 0; a--) {
408                 val = st->cmask[a & 255] + st->cmask[a >> 8];
409                 i = 8 + (15.9f * (fpy1[a & 255] + fpy2[a >> 8]) / val);
410                 CLAMP(i, 0, 15);
411                 j = 8 + (15.9f * (fpx1[a & 255] + fpx2[a >> 8]) / val);
412                 CLAMP(j, 0, 15);
413                 i = j + (i << 4);
414                 st->centmask[a] = i;
415         }
416
417         MEM_freeN(fpx1);
418         MEM_freeN(fpx2);
419         MEM_freeN(fpy1);
420         MEM_freeN(fpy2);
421 }
422
423
424 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
425 struct Object *RE_GetCamera(Render *re)
426 {
427         Object *camera = re->camera_override ? re->camera_override : re->scene->camera;
428         return BKE_camera_multiview_render(re->scene, camera, re->viewname);
429 }
430
431 static void re_camera_params_get(Render *re, CameraParams *params, Object *cam_ob)
432 {
433         copy_m4_m4(re->winmat, params->winmat);
434
435         re->clipsta = params->clipsta;
436         re->clipend = params->clipend;
437
438         re->ycor = params->ycor;
439         re->viewdx = params->viewdx;
440         re->viewdy = params->viewdy;
441         re->viewplane = params->viewplane;
442
443         BKE_camera_object_mode(&re->r, cam_ob);
444 }
445
446 void RE_SetEnvmapCamera(Render *re, Object *cam_ob, float viewscale, float clipsta, float clipend)
447 {
448         CameraParams params;
449
450         /* setup parameters */
451         BKE_camera_params_init(&params);
452         BKE_camera_params_from_object(&params, cam_ob);
453
454         params.lens = 16.0f * viewscale;
455         params.sensor_x = 32.0f;
456         params.sensor_y = 32.0f;
457         params.sensor_fit = CAMERA_SENSOR_FIT_AUTO;
458         params.clipsta = clipsta;
459         params.clipend = clipend;
460         
461         /* compute matrix, viewplane, .. */
462         BKE_camera_params_compute_viewplane(&params, re->winx, re->winy, 1.0f, 1.0f);
463         BKE_camera_params_compute_matrix(&params);
464
465         /* extract results */
466         re_camera_params_get(re, &params, cam_ob);
467 }
468
469 void RE_SetOverrideCamera(Render *re, Object *camera)
470 {
471         re->camera_override = camera;
472 }
473
474 static void re_camera_params_stereo3d(Render *re, CameraParams *params, Object *cam_ob)
475 {
476         BKE_camera_multiview_params(&re->r, params, cam_ob, re->viewname);
477 }
478
479 /* call this after InitState() */
480 /* per render, there's one persistent viewplane. Parts will set their own viewplanes */
481 void RE_SetCamera(Render *re, Object *cam_ob)
482 {
483         CameraParams params;
484
485         /* setup parameters */
486         BKE_camera_params_init(&params);
487         BKE_camera_params_from_object(&params, cam_ob);
488         re_camera_params_stereo3d(re, &params, cam_ob);
489
490         params.use_fields = (re->r.mode & R_FIELDS);
491         params.field_second = (re->flag & R_SEC_FIELD);
492         params.field_odd = (re->r.mode & R_ODDFIELD);
493
494         /* compute matrix, viewplane, .. */
495         BKE_camera_params_compute_viewplane(&params, re->winx, re->winy, re->r.xasp, re->r.yasp);
496         BKE_camera_params_compute_matrix(&params);
497
498         /* extract results */
499         re_camera_params_get(re, &params, cam_ob);
500 }
501
502 void RE_SetPixelSize(Render *re, float pixsize)
503 {
504         re->viewdx = pixsize;
505         re->viewdy = re->ycor * pixsize;
506 }
507
508 void RE_GetCameraWindow(struct Render *re, struct Object *camera, int frame, float mat[4][4])
509 {
510         re->r.cfra = frame;
511         RE_SetCamera(re, camera);
512         copy_m4_m4(mat, re->winmat);
513 }
514
515 void RE_GetCameraModelMatrix(Render *re, struct Object *camera, float r_mat[4][4])
516 {
517         BKE_camera_multiview_model_matrix(&re->r, camera, re->viewname, r_mat);
518 }
519
520 /* ~~~~~~~~~~~~~~~~ part (tile) calculus ~~~~~~~~~~~~~~~~~~~~~~ */
521
522
523 void RE_parts_free(Render *re)
524 {
525         RenderPart *part = re->parts.first;
526         
527         while (part) {
528                 if (part->rectp) MEM_freeN(part->rectp);
529                 if (part->rectz) MEM_freeN(part->rectz);
530                 part = part->next;
531         }
532         BLI_freelistN(&re->parts);
533 }
534
535 void RE_parts_clamp(Render *re)
536 {
537         /* part size */
538         re->partx = max_ii(1, min_ii(re->r.tilex, re->rectx));
539         re->party = max_ii(1, min_ii(re->r.tiley, re->recty));
540 }
541
542 void RE_parts_init(Render *re, bool do_crop)
543 {
544         int nr, xd, yd, partx, party, xparts, yparts;
545         int xminb, xmaxb, yminb, ymaxb;
546         
547         RE_parts_free(re);
548         
549         /* this is render info for caller, is not reset when parts are freed! */
550         re->i.totpart = 0;
551         re->i.curpart = 0;
552         re->i.partsdone = 0;
553         
554         /* just for readable code.. */
555         xminb = re->disprect.xmin;
556         yminb = re->disprect.ymin;
557         xmaxb = re->disprect.xmax;
558         ymaxb = re->disprect.ymax;
559         
560         RE_parts_clamp(re);
561
562         partx = re->partx;
563         party = re->party;
564         /* part count */
565         xparts = (re->rectx + partx - 1) / partx;
566         yparts = (re->recty + party - 1) / party;
567         
568         /* calculate rotation factor of 1 pixel */
569         if (re->r.mode & R_PANORAMA)
570                 re->panophi = panorama_pixel_rot(re);
571         
572         for (nr = 0; nr < xparts * yparts; nr++) {
573                 rcti disprect;
574                 int rectx, recty;
575                 
576                 xd = (nr % xparts);
577                 yd = (nr - xd) / xparts;
578                 
579                 disprect.xmin = xminb + xd * partx;
580                 disprect.ymin = yminb + yd * party;
581                 
582                 /* ensure we cover the entire picture, so last parts go to end */
583                 if (xd < xparts - 1) {
584                         disprect.xmax = disprect.xmin + partx;
585                         if (disprect.xmax > xmaxb)
586                                 disprect.xmax = xmaxb;
587                 }
588                 else disprect.xmax = xmaxb;
589                 
590                 if (yd < yparts - 1) {
591                         disprect.ymax = disprect.ymin + party;
592                         if (disprect.ymax > ymaxb)
593                                 disprect.ymax = ymaxb;
594                 }
595                 else disprect.ymax = ymaxb;
596                 
597                 rectx = BLI_rcti_size_x(&disprect);
598                 recty = BLI_rcti_size_y(&disprect);
599                 
600                 /* so, now can we add this part? */
601                 if (rectx > 0 && recty > 0) {
602                         RenderPart *pa = MEM_callocN(sizeof(RenderPart), "new part");
603                         
604                         /* Non-box filters need 2 pixels extra to work */
605                         if (do_crop && (re->r.filtertype || (re->r.mode & R_EDGE))) {
606                                 pa->crop = 2;
607                                 disprect.xmin -= pa->crop;
608                                 disprect.ymin -= pa->crop;
609                                 disprect.xmax += pa->crop;
610                                 disprect.ymax += pa->crop;
611                                 rectx += 2 * pa->crop;
612                                 recty += 2 * pa->crop;
613                         }
614                         pa->disprect = disprect;
615                         pa->rectx = rectx;
616                         pa->recty = recty;
617
618                         BLI_addtail(&re->parts, pa);
619                         re->i.totpart++;
620                 }
621         }
622 }
623
624
625