6e70e670ff86a027188fbf400350517287f6b575
[blender.git] / source / blender / render / intern / source / bake.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  * Contributors: 2004/2005/2006 Blender Foundation, full recode
19  * Contributors: Vertex color baking, Copyright 2011 AutoCRC
20  *
21  * ***** END GPL LICENSE BLOCK *****
22  */
23
24 /** \file blender/render/intern/source/bake.c
25  *  \ingroup render
26  */
27
28
29 /* system includes */
30 #include <stdio.h>
31 #include <string.h>
32
33 /* External modules: */
34 #include "MEM_guardedalloc.h"
35
36 #include "BLI_math.h"
37 #include "BLI_blenlib.h"
38 #include "BLI_threads.h"
39 #include "BLI_utildefines.h"
40
41 #include "DNA_image_types.h"
42 #include "DNA_material_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_meshdata_types.h"
45
46 #include "BKE_customdata.h"
47 #include "BKE_global.h"
48 #include "BKE_image.h"
49 #include "BKE_main.h"
50 #include "BKE_node.h"
51 #include "BKE_scene.h"
52 #include "BKE_library.h"
53
54 #include "IMB_imbuf_types.h"
55 #include "IMB_imbuf.h"
56 #include "IMB_colormanagement.h"
57
58 /* local include */
59 #include "rayintersection.h"
60 #include "rayobject.h"
61 #include "render_types.h"
62 #include "renderdatabase.h"
63 #include "shading.h"
64 #include "zbuf.h"
65
66 #include "PIL_time.h"
67
68 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
69 /* defined in pipeline.c, is hardcopy of active dynamic allocated Render */
70 /* only to be used here in this file, it's for speed */
71 extern struct Render R;
72 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
73
74
75 /* ************************* bake ************************ */
76
77
78 typedef struct BakeShade {
79         ShadeSample ssamp;
80         ObjectInstanceRen *obi;
81         VlakRen *vlr;
82         
83         ZSpan *zspan;
84         Image *ima;
85         ImBuf *ibuf;
86         
87         int rectx, recty, quad, type, vdone;
88         bool ready;
89
90         float dir[3];
91         Object *actob;
92
93         /* Output: vertex color or image data. If vcol is not NULL, rect and
94          * rect_float should be NULL. */
95         MPoly *mpoly;
96         MLoop *mloop;
97         MLoopCol *vcol;
98         
99         unsigned int *rect;
100         float *rect_float;
101
102         /* displacement buffer used for normalization with unknown maximal distance */
103         bool use_displacement_buffer;
104         float *displacement_buffer;
105         float displacement_min, displacement_max;
106         
107         bool use_mask;
108         char *rect_mask; /* bake pixel mask */
109
110         float dxco[3], dyco[3];
111
112         short *do_update;
113
114         struct ColorSpace *rect_colorspace;
115 } BakeShade;
116
117 static void bake_set_shade_input(ObjectInstanceRen *obi, VlakRen *vlr, ShadeInput *shi, int quad, int UNUSED(isect), int x, int y, float u, float v)
118 {
119         if (quad)
120                 shade_input_set_triangle_i(shi, obi, vlr, 0, 2, 3);
121         else
122                 shade_input_set_triangle_i(shi, obi, vlr, 0, 1, 2);
123                 
124         /* cache for shadow */
125         shi->samplenr = R.shadowsamplenr[shi->thread]++;
126
127         shi->mask = 0xFFFF; /* all samples */
128
129         shi->u = -u;
130         shi->v = -v;
131         shi->xs = x;
132         shi->ys = y;
133
134         shade_input_set_uv(shi);
135         shade_input_set_normals(shi);
136
137         /* no normal flip */
138         if (shi->flippednor)
139                 shade_input_flip_normals(shi);
140
141         /* set up view vector to look right at the surface (note that the normal
142          * is negated in the renderer so it does not need to be done here) */
143         shi->view[0] = shi->vn[0];
144         shi->view[1] = shi->vn[1];
145         shi->view[2] = shi->vn[2];
146 }
147
148 static void bake_shade(void *handle, Object *ob, ShadeInput *shi, int UNUSED(quad), int x, int y, float UNUSED(u), float UNUSED(v), float *tvn, float *ttang)
149 {
150         BakeShade *bs = handle;
151         ShadeSample *ssamp = &bs->ssamp;
152         ShadeResult shr;
153         VlakRen *vlr = shi->vlr;
154
155         shade_input_init_material(shi);
156
157         if (bs->type == RE_BAKE_AO) {
158                 ambient_occlusion(shi);
159
160                 if (R.r.bake_flag & R_BAKE_NORMALIZE) {
161                         copy_v3_v3(shr.combined, shi->ao);
162                 }
163                 else {
164                         zero_v3(shr.combined);
165                         environment_lighting_apply(shi, &shr);
166                 }
167         }
168         else {
169                 if (bs->type == RE_BAKE_SHADOW) /* Why do shadows set the color anyhow?, ignore material color for baking */
170                         shi->r = shi->g = shi->b = 1.0f;
171         
172                 shade_input_set_shade_texco(shi);
173                 
174                 /* only do AO for a full bake (and obviously AO bakes)
175                  * AO for light bakes is a leftover and might not be needed */
176                 if (ELEM3(bs->type, RE_BAKE_ALL, RE_BAKE_AO, RE_BAKE_LIGHT))
177                         shade_samples_do_AO(ssamp);
178                 
179                 if (shi->mat->nodetree && shi->mat->use_nodes) {
180                         ntreeShaderExecTree(shi->mat->nodetree, shi, &shr);
181                         shi->mat = vlr->mat;  /* shi->mat is being set in nodetree */
182                 }
183                 else
184                         shade_material_loop(shi, &shr);
185
186                 if (bs->type == RE_BAKE_NORMALS) {
187                         float nor[3];
188
189                         copy_v3_v3(nor, shi->vn);
190
191                         if (R.r.bake_normal_space == R_BAKE_SPACE_CAMERA) {
192                                 /* pass */
193                         }
194                         else if (R.r.bake_normal_space == R_BAKE_SPACE_TANGENT) {
195                                 float mat[3][3], imat[3][3];
196
197                                 /* bitangent */
198                                 if (tvn && ttang) {
199                                         copy_v3_v3(mat[0], ttang);
200                                         cross_v3_v3v3(mat[1], tvn, ttang);
201                                         mul_v3_fl(mat[1], ttang[3]);
202                                         copy_v3_v3(mat[2], tvn);
203                                 }
204                                 else {
205                                         copy_v3_v3(mat[0], shi->nmaptang);
206                                         cross_v3_v3v3(mat[1], shi->nmapnorm, shi->nmaptang);
207                                         mul_v3_fl(mat[1], shi->nmaptang[3]);
208                                         copy_v3_v3(mat[2], shi->nmapnorm);
209                                 }
210
211                                 invert_m3_m3(imat, mat);
212                                 mul_m3_v3(imat, nor);
213                         }
214                         else if (R.r.bake_normal_space == R_BAKE_SPACE_OBJECT)
215                                 mul_mat3_m4_v3(ob->imat_ren, nor);  /* ob->imat_ren includes viewinv! */
216                         else if (R.r.bake_normal_space == R_BAKE_SPACE_WORLD)
217                                 mul_mat3_m4_v3(R.viewinv, nor);
218
219                         normalize_v3(nor); /* in case object has scaling */
220
221                         /* The invert of the red channel is to make
222                          * the normal map compliant with the outside world.
223                          * It needs to be done because in Blender
224                          * the normal used in the renderer points inward. It is generated
225                          * this way in calc_vertexnormals(). Should this ever change
226                          * this negate must be removed. */
227                         shr.combined[0] = (-nor[0]) / 2.0f + 0.5f;
228                         shr.combined[1] = nor[1]    / 2.0f + 0.5f;
229                         shr.combined[2] = nor[2]    / 2.0f + 0.5f;
230                 }
231                 else if (bs->type == RE_BAKE_TEXTURE) {
232                         copy_v3_v3(shr.combined, &shi->r);
233                         shr.alpha = shi->alpha;
234                 }
235                 else if (bs->type == RE_BAKE_SHADOW) {
236                         copy_v3_v3(shr.combined, shr.shad);
237                         shr.alpha = shi->alpha;
238                 }
239                 else if (bs->type == RE_BAKE_SPEC_COLOR) {
240                         copy_v3_v3(shr.combined, &shi->specr);
241                         shr.alpha = 1.0f;
242                 }
243                 else if (bs->type == RE_BAKE_SPEC_INTENSITY) {
244                         copy_v3_fl(shr.combined, shi->spec);
245                         shr.alpha = 1.0f;
246                 }
247                 else if (bs->type == RE_BAKE_MIRROR_COLOR) {
248                         copy_v3_v3(shr.combined, &shi->mirr);
249                         shr.alpha = 1.0f;
250                 }
251                 else if (bs->type == RE_BAKE_MIRROR_INTENSITY) {
252                         copy_v3_fl(shr.combined, shi->ray_mirror);
253                         shr.alpha = 1.0f;
254                 }
255                 else if (bs->type == RE_BAKE_ALPHA) {
256                         copy_v3_fl(shr.combined, shi->alpha);
257                         shr.alpha = 1.0f;
258                 }
259                 else if (bs->type == RE_BAKE_EMIT) {
260                         copy_v3_fl(shr.combined, shi->emit);
261                         shr.alpha = 1.0f;
262                 }
263         }
264         
265         if (bs->rect_float && !bs->vcol) {
266                 float *col = bs->rect_float + 4 * (bs->rectx * y + x);
267                 copy_v3_v3(col, shr.combined);
268                 if (bs->type == RE_BAKE_ALL || bs->type == RE_BAKE_TEXTURE) {
269                         col[3] = shr.alpha;
270                 }
271                 else {
272                         col[3] = 1.0;
273                 }
274         }
275         else {
276                 /* Target is char (LDR). */
277                 unsigned char col[4];
278
279                 if (ELEM(bs->type, RE_BAKE_ALL, RE_BAKE_TEXTURE)) {
280                         float rgb[3];
281
282                         copy_v3_v3(rgb, shr.combined);
283                         if (R.scene_color_manage) {
284                                 /* Vertex colors have no way to specify color space, so they
285                                  * default to sRGB. */
286                                 if (!bs->vcol)
287                                         IMB_colormanagement_scene_linear_to_colorspace_v3(rgb, bs->rect_colorspace);
288                                 else
289                                         linearrgb_to_srgb_v3_v3(rgb, rgb);
290                         }
291                         rgb_float_to_uchar(col, rgb);
292                 }
293                 else {
294                         rgb_float_to_uchar(col, shr.combined);
295                 }
296                 
297                 if (ELEM(bs->type, RE_BAKE_ALL, RE_BAKE_TEXTURE)) {
298                         col[3] = FTOCHAR(shr.alpha);
299                 }
300                 else {
301                         col[3] = 255;
302                 }
303
304                 if (bs->vcol) {
305                         /* Vertex color baking. Vcol has no useful alpha channel (it exists
306                          * but is used only for vertex painting). */
307                         bs->vcol->r = col[0];
308                         bs->vcol->g = col[1];
309                         bs->vcol->b = col[2];
310                 }
311                 else {
312                         unsigned char *imcol = (unsigned char *)(bs->rect + bs->rectx * y + x);
313                         copy_v4_v4_char((char *)imcol, (char *)col);
314                 }
315
316         }
317         
318         if (bs->rect_mask) {
319                 bs->rect_mask[bs->rectx * y + x] = FILTER_MASK_USED;
320         }
321 }
322
323 static void bake_displacement(void *handle, ShadeInput *UNUSED(shi), float dist, int x, int y)
324 {
325         BakeShade *bs = handle;
326         float disp;
327
328         if (R.r.bake_flag & R_BAKE_NORMALIZE) {
329                 if (R.r.bake_maxdist)
330                         disp = (dist + R.r.bake_maxdist) / (R.r.bake_maxdist * 2);  /* alter the range from [-bake_maxdist, bake_maxdist] to [0, 1]*/
331                 else
332                         disp = dist;
333         }
334         else {
335                 disp = 0.5f + dist; /* alter the range from [-0.5,0.5] to [0,1]*/
336         }
337
338         if (bs->displacement_buffer) {
339                 float *displacement = bs->displacement_buffer + (bs->rectx * y + x);
340                 *displacement = disp;
341                 bs->displacement_min = min_ff(bs->displacement_min, disp);
342                 bs->displacement_max = max_ff(bs->displacement_max, disp);
343         }
344
345         if (bs->rect_float && !bs->vcol) {
346                 float *col = bs->rect_float + 4 * (bs->rectx * y + x);
347                 col[0] = col[1] = col[2] = disp;
348                 col[3] = 1.0f;
349         }
350         else {
351                 /* Target is char (LDR). */
352                 unsigned char col[4];
353                 col[0] = col[1] = col[2] = FTOCHAR(disp);
354                 col[3] = 255;
355
356                 if (bs->vcol) {
357                         /* Vertex color baking. Vcol has no useful alpha channel (it exists
358                          * but is used only for vertex painting). */
359                         bs->vcol->r = col[0];
360                         bs->vcol->g = col[1];
361                         bs->vcol->b = col[2];
362                 }
363                 else {
364                         char *imcol = (char *)(bs->rect + bs->rectx * y + x);
365                         copy_v4_v4_char((char *)imcol, (char *)col);
366                 }
367         }
368         if (bs->rect_mask) {
369                 bs->rect_mask[bs->rectx * y + x] = FILTER_MASK_USED;
370         }
371 }
372
373 static int bake_intersect_tree(RayObject *raytree, Isect *isect, float *start, float *dir, float sign, float *hitco, float *dist)
374 {
375         float maxdist;
376         int hit;
377
378         /* might be useful to make a user setting for maxsize*/
379         if (R.r.bake_maxdist > 0.0f)
380                 maxdist = R.r.bake_maxdist;
381         else
382                 maxdist = RE_RAYTRACE_MAXDIST + R.r.bake_biasdist;
383
384         /* 'dir' is always normalized */
385         madd_v3_v3v3fl(isect->start, start, dir, -R.r.bake_biasdist);
386
387         mul_v3_v3fl(isect->dir, dir, sign);
388
389         isect->dist = maxdist;
390
391         hit = RE_rayobject_raycast(raytree, isect);
392         if (hit) {
393                 madd_v3_v3v3fl(hitco, isect->start, isect->dir, isect->dist);
394
395                 *dist = isect->dist;
396         }
397
398         return hit;
399 }
400
401 static void bake_set_vlr_dxyco(BakeShade *bs, float *uv1, float *uv2, float *uv3)
402 {
403         VlakRen *vlr = bs->vlr;
404         float A, d1, d2, d3, *v1, *v2, *v3;
405
406         if (bs->quad) {
407                 v1 = vlr->v1->co;
408                 v2 = vlr->v3->co;
409                 v3 = vlr->v4->co;
410         }
411         else {
412                 v1 = vlr->v1->co;
413                 v2 = vlr->v2->co;
414                 v3 = vlr->v3->co;
415         }
416
417         /* formula derived from barycentric coordinates:
418          * (uvArea1*v1 + uvArea2*v2 + uvArea3*v3)/uvArea
419          * then taking u and v partial derivatives to get dxco and dyco */
420         A = (uv2[0] - uv1[0]) * (uv3[1] - uv1[1]) - (uv3[0] - uv1[0]) * (uv2[1] - uv1[1]);
421
422         if (fabsf(A) > FLT_EPSILON) {
423                 A = 0.5f / A;
424
425                 d1 = uv2[1] - uv3[1];
426                 d2 = uv3[1] - uv1[1];
427                 d3 = uv1[1] - uv2[1];
428                 bs->dxco[0] = (v1[0] * d1 + v2[0] * d2 + v3[0] * d3) * A;
429                 bs->dxco[1] = (v1[1] * d1 + v2[1] * d2 + v3[1] * d3) * A;
430                 bs->dxco[2] = (v1[2] * d1 + v2[2] * d2 + v3[2] * d3) * A;
431
432                 d1 = uv3[0] - uv2[0];
433                 d2 = uv1[0] - uv3[0];
434                 d3 = uv2[0] - uv1[0];
435                 bs->dyco[0] = (v1[0] * d1 + v2[0] * d2 + v3[0] * d3) * A;
436                 bs->dyco[1] = (v1[1] * d1 + v2[1] * d2 + v3[1] * d3) * A;
437                 bs->dyco[2] = (v1[2] * d1 + v2[2] * d2 + v3[2] * d3) * A;
438         }
439         else {
440                 bs->dxco[0] = bs->dxco[1] = bs->dxco[2] = 0.0f;
441                 bs->dyco[0] = bs->dyco[1] = bs->dyco[2] = 0.0f;
442         }
443
444         if (bs->obi->flag & R_TRANSFORMED) {
445                 mul_m3_v3(bs->obi->nmat, bs->dxco);
446                 mul_m3_v3(bs->obi->nmat, bs->dyco);
447         }
448 }
449
450 static void do_bake_shade(void *handle, int x, int y, float u, float v)
451 {
452         BakeShade *bs = handle;
453         VlakRen *vlr = bs->vlr;
454         ObjectInstanceRen *obi = bs->obi;
455         Object *ob = obi->obr->ob;
456         float l, *v1, *v2, *v3, tvn[3], ttang[4];
457         int quad;
458         ShadeSample *ssamp = &bs->ssamp;
459         ShadeInput *shi = ssamp->shi;
460
461         /* fast threadsafe break test */
462         if (R.test_break(R.tbh))
463                 return;
464
465         /* setup render coordinates */
466         if (bs->quad) {
467                 v1 = vlr->v1->co;
468                 v2 = vlr->v3->co;
469                 v3 = vlr->v4->co;
470         }
471         else {
472                 v1 = vlr->v1->co;
473                 v2 = vlr->v2->co;
474                 v3 = vlr->v3->co;
475         }
476
477         l = 1.0f - u - v;
478
479         /* shrink barycentric coordinates inwards slightly to avoid some issues
480          * where baking selected to active might just miss the other face at the
481          * near the edge of a face */
482         if (bs->actob) {
483                 const float eps = 1.0f - 1e-4f;
484                 float invsum;
485
486                 u = (u - 0.5f) * eps + 0.5f;
487                 v = (v - 0.5f) * eps + 0.5f;
488                 l = (l - 0.5f) * eps + 0.5f;
489
490                 invsum = 1.0f / (u + v + l);
491
492                 u *= invsum;
493                 v *= invsum;
494                 l *= invsum;
495         }
496
497         /* renderco */
498         shi->co[0] = l * v3[0] + u * v1[0] + v * v2[0];
499         shi->co[1] = l * v3[1] + u * v1[1] + v * v2[1];
500         shi->co[2] = l * v3[2] + u * v1[2] + v * v2[2];
501
502         /* avoid self shadow with vertex bake from adjacent faces [#33729] */
503         if ((bs->vcol != NULL) && (bs->actob == NULL)) {
504                 madd_v3_v3fl(shi->co, vlr->n, 0.0001f);
505         }
506
507         if (obi->flag & R_TRANSFORMED)
508                 mul_m4_v3(obi->mat, shi->co);
509
510         copy_v3_v3(shi->dxco, bs->dxco);
511         copy_v3_v3(shi->dyco, bs->dyco);
512
513         quad = bs->quad;
514         bake_set_shade_input(obi, vlr, shi, quad, 0, x, y, u, v);
515
516         if (bs->type == RE_BAKE_NORMALS && R.r.bake_normal_space == R_BAKE_SPACE_TANGENT) {
517                 shade_input_set_shade_texco(shi);
518                 copy_v3_v3(tvn, shi->nmapnorm);
519                 copy_v4_v4(ttang, shi->nmaptang);
520         }
521
522         /* if we are doing selected to active baking, find point on other face */
523         if (bs->actob) {
524                 Isect isec, minisec;
525                 float co[3], minco[3], dist, mindist = 0.0f;
526                 int hit, sign, dir = 1;
527
528                 /* intersect with ray going forward and backward*/
529                 hit = 0;
530                 memset(&minisec, 0, sizeof(minisec));
531                 minco[0] = minco[1] = minco[2] = 0.0f;
532
533                 copy_v3_v3(bs->dir, shi->vn);
534
535                 for (sign = -1; sign <= 1; sign += 2) {
536                         memset(&isec, 0, sizeof(isec));
537                         isec.mode = RE_RAY_MIRROR;
538
539                         isec.orig.ob   = obi;
540                         isec.orig.face = vlr;
541                         isec.userdata = bs->actob;
542                         isec.check = RE_CHECK_VLR_BAKE;
543                         isec.skip = RE_SKIP_VLR_NEIGHBOUR;
544
545                         if (bake_intersect_tree(R.raytree, &isec, shi->co, shi->vn, sign, co, &dist)) {
546                                 if (!hit || len_squared_v3v3(shi->co, co) < len_squared_v3v3(shi->co, minco)) {
547                                         minisec = isec;
548                                         mindist = dist;
549                                         copy_v3_v3(minco, co);
550                                         hit = 1;
551                                         dir = sign;
552                                 }
553                         }
554                 }
555
556                 if (bs->type == RE_BAKE_DISPLACEMENT) {
557                         if (hit)
558                                 bake_displacement(handle, shi, (dir == -1) ? mindist : -mindist, x, y);
559                         else
560                                 bake_displacement(handle, shi, 0.0f, x, y);
561                         return;
562                 }
563
564                 /* if hit, we shade from the new point, otherwise from point one starting face */
565                 if (hit) {
566                         obi = (ObjectInstanceRen *)minisec.hit.ob;
567                         vlr = (VlakRen *)minisec.hit.face;
568                         quad = (minisec.isect == 2);
569                         copy_v3_v3(shi->co, minco);
570
571                         u = -minisec.u;
572                         v = -minisec.v;
573                         bake_set_shade_input(obi, vlr, shi, quad, 1, x, y, u, v);
574                 }
575         }
576
577         if (bs->type == RE_BAKE_NORMALS && R.r.bake_normal_space == R_BAKE_SPACE_TANGENT)
578                 bake_shade(handle, ob, shi, quad, x, y, u, v, tvn, ttang);
579         else
580                 bake_shade(handle, ob, shi, quad, x, y, u, v, NULL, NULL);
581 }
582
583 static int get_next_bake_face(BakeShade *bs)
584 {
585         ObjectRen *obr;
586         VlakRen *vlr;
587         MTFace *tface;
588         static int v = 0, vdone = false;
589         static ObjectInstanceRen *obi = NULL;
590
591         if (bs == NULL) {
592                 vlr = NULL;
593                 v = vdone = false;
594                 obi = R.instancetable.first;
595                 return 0;
596         }
597         
598         BLI_lock_thread(LOCK_CUSTOM1);
599
600         for (; obi; obi = obi->next, v = 0) {
601                 obr = obi->obr;
602
603                 for (; v < obr->totvlak; v++) {
604                         vlr = RE_findOrAddVlak(obr, v);
605
606                         if ((bs->actob && bs->actob == obr->ob) || (!bs->actob && (obr->ob->flag & SELECT))) {
607                                 if (R.r.bake_flag & R_BAKE_VCOL) {
608                                         /* Gather face data for vertex color bake */
609                                         Mesh *me;
610                                         int *origindex, vcollayer;
611                                         CustomDataLayer *cdl;
612
613                                         if (obr->ob->type != OB_MESH)
614                                                 continue;
615                                         me = obr->ob->data;
616
617                                         origindex = RE_vlakren_get_origindex(obr, vlr, 0);
618                                         if (origindex == NULL)
619                                                 continue;
620                                         if (*origindex >= me->totpoly) {
621                                                 /* Small hack for Array modifier, which gives false
622                                                  * original indices - z0r */
623                                                 continue;
624                                         }
625 #if 0
626                                         /* Only shade selected faces. */
627                                         if ((me->mface[*origindex].flag & ME_FACE_SEL) == 0)
628                                                 continue;
629 #endif
630
631                                         vcollayer = CustomData_get_render_layer_index(&me->ldata, CD_MLOOPCOL);
632                                         if (vcollayer == -1)
633                                                 continue;
634
635                                         cdl = &me->ldata.layers[vcollayer];
636                                         bs->mpoly = me->mpoly + *origindex;
637                                         bs->vcol = ((MLoopCol *)cdl->data) + bs->mpoly->loopstart;
638                                         bs->mloop = me->mloop + bs->mpoly->loopstart;
639
640                                         /* Tag mesh for reevaluation. */
641                                         me->id.flag |= LIB_DOIT;
642                                 }
643                                 else {
644                                         Image *ima = NULL;
645                                         ImBuf *ibuf = NULL;
646                                         const float vec_alpha[4] = {0.0f, 0.0f, 0.0f, 0.0f};
647                                         const float vec_solid[4] = {0.0f, 0.0f, 0.0f, 1.0f};
648                                         const float nor_alpha[4] = {0.5f, 0.5f, 1.0f, 0.0f};
649                                         const float nor_solid[4] = {0.5f, 0.5f, 1.0f, 1.0f};
650                                         const float disp_alpha[4] = {0.5f, 0.5f, 0.5f, 0.0f};
651                                         const float disp_solid[4] = {0.5f, 0.5f, 0.5f, 1.0f};
652
653                                         tface = RE_vlakren_get_tface(obr, vlr, obr->bakemtface, NULL, 0);
654
655                                         if (!tface || !tface->tpage)
656                                                 continue;
657
658                                         ima = tface->tpage;
659                                         ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
660
661                                         if (ibuf == NULL)
662                                                 continue;
663
664                                         if (ibuf->rect == NULL && ibuf->rect_float == NULL) {
665                                                 BKE_image_release_ibuf(ima, ibuf, NULL);
666                                                 continue;
667                                         }
668
669                                         if (ibuf->rect_float && !(ibuf->channels == 0 || ibuf->channels == 4)) {
670                                                 BKE_image_release_ibuf(ima, ibuf, NULL);
671                                                 continue;
672                                         }
673                                         
674                                         if (ima->flag & IMA_USED_FOR_RENDER) {
675                                                 ima->id.flag &= ~LIB_DOIT;
676                                                 BKE_image_release_ibuf(ima, ibuf, NULL);
677                                                 continue;
678                                         }
679                                         
680                                         /* find the image for the first time? */
681                                         if (ima->id.flag & LIB_DOIT) {
682                                                 ima->id.flag &= ~LIB_DOIT;
683                                                 
684                                                 /* we either fill in float or char, this ensures things go fine */
685                                                 if (ibuf->rect_float)
686                                                         imb_freerectImBuf(ibuf);
687                                                 /* clear image */
688                                                 if (R.r.bake_flag & R_BAKE_CLEAR) {
689                                                         if (R.r.bake_mode == RE_BAKE_NORMALS && R.r.bake_normal_space == R_BAKE_SPACE_TANGENT)
690                                                                 IMB_rectfill(ibuf, (ibuf->planes == R_IMF_PLANES_RGBA) ? nor_alpha : nor_solid);
691                                                         else if (R.r.bake_mode == RE_BAKE_DISPLACEMENT)
692                                                                 IMB_rectfill(ibuf, (ibuf->planes == R_IMF_PLANES_RGBA) ? disp_alpha : disp_solid);
693                                                         else
694                                                                 IMB_rectfill(ibuf, (ibuf->planes == R_IMF_PLANES_RGBA) ? vec_alpha : vec_solid);
695                                                 }
696                                                 /* might be read by UI to set active image for display */
697                                                 R.bakebuf = ima;
698                                         }
699
700                                         /* Tag image for redraw. */
701                                         ibuf->userflags |= IB_DISPLAY_BUFFER_INVALID;
702                                         BKE_image_release_ibuf(ima, ibuf, NULL);
703                                 }
704
705                                 bs->obi = obi;
706                                 bs->vlr = vlr;
707                                 bs->vdone++;  /* only for error message if nothing was rendered */
708                                 v++;
709                                 BLI_unlock_thread(LOCK_CUSTOM1);
710                                 return 1;
711                         }
712                 }
713         }
714         
715         BLI_unlock_thread(LOCK_CUSTOM1);
716         return 0;
717 }
718
719 static void bake_single_vertex(BakeShade *bs, VertRen *vert, float u, float v)
720 {
721         int *origindex, i;
722         MLoopCol *basevcol;
723         MLoop *mloop;
724
725         origindex = RE_vertren_get_origindex(bs->obi->obr, vert, 0);
726         if (!origindex || *origindex == ORIGINDEX_NONE)
727                 return;
728
729         /* Search for matching vertex index and apply shading. */
730         for (i = 0; i < bs->mpoly->totloop; i++) {
731                 mloop = bs->mloop + i;
732                 if (mloop->v != *origindex)
733                         continue;
734                 basevcol = bs->vcol;
735                 bs->vcol = basevcol + i;
736                 do_bake_shade(bs, 0, 0, u, v);
737                 bs->vcol = basevcol;
738                 break;
739         }
740 }
741
742 /* Bake all vertices of a face. Actually, this still works on a face-by-face
743  * basis, and each vertex on each face is shaded. Vertex colors are a property
744  * of loops, not vertices. */
745 static void shade_verts(BakeShade *bs)
746 {
747         VlakRen *vlr = bs->vlr;
748
749         /* Disable baking to image; write to vcol instead. vcol pointer is set in
750          * bake_single_vertex. */
751         bs->ima = NULL;
752         bs->rect = NULL;
753         bs->rect_float = NULL;
754         bs->displacement_buffer = NULL;
755         bs->displacement_min = FLT_MAX;
756         bs->displacement_max = -FLT_MAX;
757
758         bs->quad = 0;
759
760         /* No anti-aliasing for vertices. */
761         zero_v3(bs->dxco);
762         zero_v3(bs->dyco);
763
764         /* Shade each vertex of the face. u and v are barycentric coordinates; since
765          * we're only interested in vertices, these will be 0 or 1. */
766         if ((vlr->flag & R_FACE_SPLIT) == 0) {
767                 /* Processing triangle face, whole quad, or first half of split quad. */
768
769                 bake_single_vertex(bs, bs->vlr->v1, 1.0f, 0.0f);
770                 bake_single_vertex(bs, bs->vlr->v2, 0.0f, 1.0f);
771                 bake_single_vertex(bs, bs->vlr->v3, 0.0f, 0.0f);
772
773                 if (vlr->v4) {
774                         bs->quad = 1;
775                         bake_single_vertex(bs, bs->vlr->v4, 0.0f, 0.0f);
776                 }
777         }
778         else {
779                 /* Processing second half of split quad. Only one vertex to go. */
780                 if (vlr->flag & R_DIVIDE_24) {
781                         bake_single_vertex(bs, bs->vlr->v2, 0.0f, 1.0f);
782                 }
783                 else {
784                         bake_single_vertex(bs, bs->vlr->v3, 0.0f, 0.0f);
785                 }
786         }
787 }
788
789 /* already have tested for tface and ima and zspan */
790 static void shade_tface(BakeShade *bs)
791 {
792         VlakRen *vlr = bs->vlr;
793         ObjectInstanceRen *obi = bs->obi;
794         ObjectRen *obr = obi->obr;
795         MTFace *tface = RE_vlakren_get_tface(obr, vlr, obr->bakemtface, NULL, 0);
796         Image *ima = tface->tpage;
797         float vec[4][2];
798         int a, i1, i2, i3;
799         
800         /* check valid zspan */
801         if (ima != bs->ima) {
802                 BKE_image_release_ibuf(bs->ima, bs->ibuf, NULL);
803
804                 bs->ima = ima;
805                 bs->ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
806                 /* note, these calls only free/fill contents of zspan struct, not zspan itself */
807                 zbuf_free_span(bs->zspan);
808                 zbuf_alloc_span(bs->zspan, bs->ibuf->x, bs->ibuf->y, R.clipcrop);
809         }
810
811         bs->rectx = bs->ibuf->x;
812         bs->recty = bs->ibuf->y;
813         bs->rect = bs->ibuf->rect;
814         bs->rect_colorspace = bs->ibuf->rect_colorspace;
815         bs->rect_float = bs->ibuf->rect_float;
816         bs->vcol = NULL;
817         bs->quad = 0;
818         bs->rect_mask = NULL;
819         bs->displacement_buffer = NULL;
820
821         if (bs->use_mask || bs->use_displacement_buffer) {
822                 BakeImBufuserData *userdata = bs->ibuf->userdata;
823                 if (userdata == NULL) {
824                         BLI_lock_thread(LOCK_CUSTOM1);
825                         userdata = bs->ibuf->userdata;
826                         if (userdata == NULL) /* since the thread was locked, its possible another thread alloced the value */
827                                 userdata = MEM_callocN(sizeof(BakeImBufuserData), "BakeImBufuserData");
828
829                         if (bs->use_mask) {
830                                 if (userdata->mask_buffer == NULL) {
831                                         userdata->mask_buffer = MEM_callocN(sizeof(char) * bs->rectx * bs->recty, "BakeMask");
832                                 }
833                         }
834
835                         if (bs->use_displacement_buffer) {
836                                 if (userdata->displacement_buffer == NULL) {
837                                         userdata->displacement_buffer = MEM_callocN(sizeof(float) * bs->rectx * bs->recty, "BakeDisp");
838                                 }
839                         }
840
841                         bs->ibuf->userdata = userdata;
842
843                         BLI_unlock_thread(LOCK_CUSTOM1);
844                 }
845
846                 bs->rect_mask = userdata->mask_buffer;
847                 bs->displacement_buffer = userdata->displacement_buffer;
848         }
849         
850         /* get pixel level vertex coordinates */
851         for (a = 0; a < 4; a++) {
852                 /* Note, workaround for pixel aligned UVs which are common and can screw up our intersection tests
853                  * where a pixel gets in between 2 faces or the middle of a quad,
854                  * camera aligned quads also have this problem but they are less common.
855                  * Add a small offset to the UVs, fixes bug #18685 - Campbell */
856                 vec[a][0] = tface->uv[a][0] * (float)bs->rectx - (0.5f + 0.001f);
857                 vec[a][1] = tface->uv[a][1] * (float)bs->recty - (0.5f + 0.002f);
858         }
859
860         /* UV indices have to be corrected for possible quad->tria splits */
861         i1 = 0; i2 = 1; i3 = 2;
862         vlr_set_uv_indices(vlr, &i1, &i2, &i3);
863         bake_set_vlr_dxyco(bs, vec[i1], vec[i2], vec[i3]);
864         zspan_scanconvert(bs->zspan, bs, vec[i1], vec[i2], vec[i3], do_bake_shade);
865         
866         if (vlr->v4) {
867                 bs->quad = 1;
868                 bake_set_vlr_dxyco(bs, vec[0], vec[2], vec[3]);
869                 zspan_scanconvert(bs->zspan, bs, vec[0], vec[2], vec[3], do_bake_shade);
870         }
871 }
872
873 static void *do_bake_thread(void *bs_v)
874 {
875         BakeShade *bs = bs_v;
876
877         while (get_next_bake_face(bs)) {
878                 if (R.r.bake_flag & R_BAKE_VCOL) {
879                         shade_verts(bs);
880                 }
881                 else {
882                         shade_tface(bs);
883                 }
884                 
885                 /* fast threadsafe break test */
886                 if (R.test_break(R.tbh))
887                         break;
888
889                 /* access is not threadsafe but since its just true/false probably ok
890                  * only used for interactive baking */
891                 if (bs->do_update) {
892                         *bs->do_update = true;
893                 }
894         }
895         bs->ready = true;
896
897         BKE_image_release_ibuf(bs->ima, bs->ibuf, NULL);
898
899         return NULL;
900 }
901
902 void RE_bake_ibuf_filter(ImBuf *ibuf, char *mask, const int filter)
903 {
904         /* must check before filtering */
905         const short is_new_alpha = (ibuf->planes != R_IMF_PLANES_RGBA) && BKE_imbuf_alpha_test(ibuf);
906
907         /* Margin */
908         if (filter) {
909                 IMB_filter_extend(ibuf, mask, filter);
910         }
911
912         /* if the bake results in new alpha then change the image setting */
913         if (is_new_alpha) {
914                 ibuf->planes = R_IMF_PLANES_RGBA;
915         }
916         else {
917                 if (filter && ibuf->planes != R_IMF_PLANES_RGBA) {
918                         /* clear alpha added by filtering */
919                         IMB_rectfill_alpha(ibuf, 1.0f);
920                 }
921         }
922 }
923
924 void RE_bake_ibuf_normalize_displacement(ImBuf *ibuf, float *displacement, char *mask, float displacement_min, float displacement_max)
925 {
926         int i;
927         float *current_displacement = displacement;
928         char *current_mask = mask;
929         float max_distance;
930
931         max_distance = max_ff(fabsf(displacement_min), fabsf(displacement_max));
932
933         for (i = 0; i < ibuf->x * ibuf->y; i++) {
934                 if (*current_mask == FILTER_MASK_USED) {
935                         float normalized_displacement;
936
937                         if (max_distance > 1e-5f)
938                                 normalized_displacement = (*current_displacement + max_distance) / (max_distance * 2);
939                         else
940                                 normalized_displacement = 0.5f;
941
942                         if (ibuf->rect_float) {
943                                 /* currently baking happens to RGBA only */
944                                 float *fp = ibuf->rect_float + i * 4;
945                                 fp[0] = fp[1] = fp[2] = normalized_displacement;
946                                 fp[3] = 1.0f;
947                         }
948
949                         if (ibuf->rect) {
950                                 unsigned char *cp = (unsigned char *) (ibuf->rect + i);
951                                 cp[0] = cp[1] = cp[2] = FTOCHAR(normalized_displacement);
952                                 cp[3] = 255;
953                         }
954                 }
955
956                 current_displacement++;
957                 current_mask++;
958         }
959 }
960
961 /* using object selection tags, the faces with UV maps get baked */
962 /* render should have been setup */
963 /* returns 0 if nothing was handled */
964 int RE_bake_shade_all_selected(Render *re, int type, Object *actob, short *do_update, float *progress)
965 {
966         BakeShade *handles;
967         ListBase threads;
968         Image *ima;
969         int a, vdone = false, result = BAKE_RESULT_OK;
970         bool use_mask = false;
971         bool use_displacement_buffer = false;
972         
973         re->scene_color_manage = BKE_scene_check_color_management_enabled(re->scene);
974         
975         /* initialize render global */
976         R = *re;
977         R.bakebuf = NULL;
978
979         /* initialize static vars */
980         get_next_bake_face(NULL);
981         
982         /* do we need a mask? */
983         if (re->r.bake_filter)
984                 use_mask = true;
985
986         /* do we need buffer to store displacements  */
987         if (type == RE_BAKE_DISPLACEMENT) {
988                 if ((R.r.bake_flag & R_BAKE_NORMALIZE) && R.r.bake_maxdist == 0.0f) {
989                         use_displacement_buffer = true;
990                         use_mask = true;
991                 }
992         }
993
994         /* baker uses this flag to detect if image was initialized */
995         if ((R.r.bake_flag & R_BAKE_VCOL) == 0) {
996                 for (ima = G.main->image.first; ima; ima = ima->id.next) {
997                         ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
998                         ima->id.flag |= LIB_DOIT;
999                         ima->flag &= ~IMA_USED_FOR_RENDER;
1000                         if (ibuf) {
1001                                 ibuf->userdata = NULL; /* use for masking if needed */
1002                         }
1003                         BKE_image_release_ibuf(ima, ibuf, NULL);
1004                 }
1005         }
1006
1007         if (R.r.bake_flag & R_BAKE_VCOL) {
1008                 /* untag all meshes */
1009                 tag_main_lb(&G.main->mesh, false);
1010         }
1011
1012         BLI_init_threads(&threads, do_bake_thread, re->r.threads);
1013
1014         handles = MEM_callocN(sizeof(BakeShade) * re->r.threads, "BakeShade");
1015
1016         /* get the threads running */
1017         for (a = 0; a < re->r.threads; a++) {
1018                 /* set defaults in handles */
1019                 handles[a].ssamp.shi[0].lay = re->lay;
1020
1021                 if (type == RE_BAKE_SHADOW) {
1022                         handles[a].ssamp.shi[0].passflag = SCE_PASS_SHADOW;
1023                 }
1024                 else {
1025                         handles[a].ssamp.shi[0].passflag = SCE_PASS_COMBINED;
1026                 }
1027                 handles[a].ssamp.shi[0].combinedflag = ~(SCE_PASS_SPEC);
1028                 handles[a].ssamp.shi[0].thread = a;
1029                 handles[a].ssamp.tot = 1;
1030
1031                 handles[a].type = type;
1032                 handles[a].actob = actob;
1033                 if (R.r.bake_flag & R_BAKE_VCOL)
1034                         handles[a].zspan = NULL;
1035                 else
1036                         handles[a].zspan = MEM_callocN(sizeof(ZSpan), "zspan for bake");
1037                 
1038                 handles[a].use_mask = use_mask;
1039                 handles[a].use_displacement_buffer = use_displacement_buffer;
1040
1041                 handles[a].do_update = do_update; /* use to tell the view to update */
1042                 
1043                 handles[a].displacement_min = FLT_MAX;
1044                 handles[a].displacement_max = -FLT_MAX;
1045
1046                 BLI_insert_thread(&threads, &handles[a]);
1047         }
1048         
1049         /* wait for everything to be done */
1050         a = 0;
1051         while (a != re->r.threads) {
1052                 PIL_sleep_ms(50);
1053
1054                 /* calculate progress */
1055                 for (vdone = false, a = 0; a < re->r.threads; a++)
1056                         vdone += handles[a].vdone;
1057                 if (progress)
1058                         *progress = (float)(vdone / (float)re->totvlak);
1059
1060                 for (a = 0; a < re->r.threads; a++) {
1061                         if (handles[a].ready == false) {
1062                                 break;
1063                         }
1064                 }
1065         }
1066
1067         /* filter and refresh images */
1068         if ((R.r.bake_flag & R_BAKE_VCOL) == 0) {
1069                 float displacement_min = FLT_MAX, displacement_max = -FLT_MAX;
1070
1071                 if (use_displacement_buffer) {
1072                         for (a = 0; a < re->r.threads; a++) {
1073                                 displacement_min = min_ff(displacement_min, handles[a].displacement_min);
1074                                 displacement_max = max_ff(displacement_max, handles[a].displacement_max);
1075                         }
1076                 }
1077
1078                 for (ima = G.main->image.first; ima; ima = ima->id.next) {
1079                         if ((ima->id.flag & LIB_DOIT) == 0) {
1080                                 ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
1081                                 BakeImBufuserData *userdata;
1082
1083                                 if (ima->flag & IMA_USED_FOR_RENDER)
1084                                         result = BAKE_RESULT_FEEDBACK_LOOP;
1085
1086                                 if (!ibuf)
1087                                         continue;
1088
1089                                 userdata = (BakeImBufuserData *)ibuf->userdata;
1090                                 if (userdata) {
1091                                         if (use_displacement_buffer) {
1092                                                 RE_bake_ibuf_normalize_displacement(ibuf, userdata->displacement_buffer, userdata->mask_buffer,
1093                                                                                     displacement_min, displacement_max);
1094                                         }
1095
1096                                         RE_bake_ibuf_filter(ibuf, userdata->mask_buffer, re->r.bake_filter);
1097                                 }
1098
1099                                 ibuf->userflags |= IB_BITMAPDIRTY;
1100                                 BKE_image_release_ibuf(ima, ibuf, NULL);
1101                         }
1102                 }
1103
1104                 /* calculate return value */
1105                 for (a = 0; a < re->r.threads; a++) {
1106                         zbuf_free_span(handles[a].zspan);
1107                         MEM_freeN(handles[a].zspan);
1108                 }
1109         }
1110
1111         MEM_freeN(handles);
1112         
1113         BLI_end_threads(&threads);
1114
1115         if (vdone == 0) {
1116                 result = BAKE_RESULT_NO_OBJECTS;
1117         }
1118
1119         return result;
1120 }
1121
1122 struct Image *RE_bake_shade_get_image(void)
1123 {
1124         return R.bakebuf;
1125 }
1126