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