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