1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 /** \file
18  * \ingroup blenloader
19  */
20 
21 /* allow readfile to use deprecated functionality */
22 #define DNA_DEPRECATED_ALLOW
23 
24 #include <float.h>
25 #include <string.h>
26 
27 #include "BLI_listbase.h"
28 #include "BLI_math.h"
29 #include "BLI_string.h"
30 #include "BLI_utildefines.h"
31 
32 #include "DNA_anim_types.h"
33 #include "DNA_camera_types.h"
34 #include "DNA_color_types.h"
35 #include "DNA_light_types.h"
36 #include "DNA_node_types.h"
37 #include "DNA_particle_types.h"
38 
39 #include "BKE_animsys.h"
40 #include "BKE_colortools.h"
41 #include "BKE_idprop.h"
42 #include "BKE_main.h"
43 #include "BKE_node.h"
44 
45 #include "MEM_guardedalloc.h"
46 
47 #include "IMB_colormanagement.h"
48 
49 #include "BLO_readfile.h"
50 #include "readfile.h"
51 
socket_is_used(bNodeSocket * sock)52 static bool socket_is_used(bNodeSocket *sock)
53 {
54   return sock->flag & SOCK_IN_USE;
55 }
56 
cycles_node_socket_float_value(bNodeSocket * socket)57 static float *cycles_node_socket_float_value(bNodeSocket *socket)
58 {
59   bNodeSocketValueFloat *socket_data = socket->default_value;
60   return &socket_data->value;
61 }
62 
cycles_node_socket_rgba_value(bNodeSocket * socket)63 static float *cycles_node_socket_rgba_value(bNodeSocket *socket)
64 {
65   bNodeSocketValueRGBA *socket_data = socket->default_value;
66   return socket_data->value;
67 }
68 
cycles_node_socket_vector_value(bNodeSocket * socket)69 static float *cycles_node_socket_vector_value(bNodeSocket *socket)
70 {
71   bNodeSocketValueVector *socket_data = socket->default_value;
72   return socket_data->value;
73 }
74 
cycles_properties_from_ID(ID * id)75 static IDProperty *cycles_properties_from_ID(ID *id)
76 {
77   IDProperty *idprop = IDP_GetProperties(id, false);
78   return (idprop) ? IDP_GetPropertyTypeFromGroup(idprop, "cycles", IDP_GROUP) : NULL;
79 }
80 
cycles_properties_from_view_layer(ViewLayer * view_layer)81 static IDProperty *cycles_properties_from_view_layer(ViewLayer *view_layer)
82 {
83   IDProperty *idprop = view_layer->id_properties;
84   return (idprop) ? IDP_GetPropertyTypeFromGroup(idprop, "cycles", IDP_GROUP) : NULL;
85 }
86 
cycles_property_float(IDProperty * idprop,const char * name,float default_value)87 static float cycles_property_float(IDProperty *idprop, const char *name, float default_value)
88 {
89   IDProperty *prop = IDP_GetPropertyTypeFromGroup(idprop, name, IDP_FLOAT);
90   return (prop) ? IDP_Float(prop) : default_value;
91 }
92 
cycles_property_int(IDProperty * idprop,const char * name,int default_value)93 static int cycles_property_int(IDProperty *idprop, const char *name, int default_value)
94 {
95   IDProperty *prop = IDP_GetPropertyTypeFromGroup(idprop, name, IDP_INT);
96   return (prop) ? IDP_Int(prop) : default_value;
97 }
98 
cycles_property_int_set(IDProperty * idprop,const char * name,int value)99 static void cycles_property_int_set(IDProperty *idprop, const char *name, int value)
100 {
101   IDProperty *prop = IDP_GetPropertyTypeFromGroup(idprop, name, IDP_INT);
102   if (prop) {
103     IDP_Int(prop) = value;
104   }
105   else {
106     IDPropertyTemplate val = {0};
107     val.i = value;
108     IDP_AddToGroup(idprop, IDP_New(IDP_INT, &val, name));
109   }
110 }
111 
cycles_property_boolean(IDProperty * idprop,const char * name,bool default_value)112 static bool cycles_property_boolean(IDProperty *idprop, const char *name, bool default_value)
113 {
114   return cycles_property_int(idprop, name, default_value);
115 }
116 
cycles_property_boolean_set(IDProperty * idprop,const char * name,bool value)117 static void cycles_property_boolean_set(IDProperty *idprop, const char *name, bool value)
118 {
119   cycles_property_int_set(idprop, name, value);
120 }
121 
displacement_node_insert(bNodeTree * ntree)122 static void displacement_node_insert(bNodeTree *ntree)
123 {
124   bool need_update = false;
125 
126   /* Iterate backwards from end so we don't encounter newly added links. */
127   bNodeLink *prevlink;
128   for (bNodeLink *link = ntree->links.last; link; link = prevlink) {
129     prevlink = link->prev;
130 
131     /* Detect link to replace. */
132     bNode *fromnode = link->fromnode;
133     bNodeSocket *fromsock = link->fromsock;
134     bNode *tonode = link->tonode;
135     bNodeSocket *tosock = link->tosock;
136 
137     if (!(tonode->type == SH_NODE_OUTPUT_MATERIAL && fromnode->type != SH_NODE_DISPLACEMENT &&
138           STREQ(tosock->identifier, "Displacement"))) {
139       continue;
140     }
141 
142     /* Replace link with displacement node. */
143     nodeRemLink(ntree, link);
144 
145     /* Add displacement node. */
146     bNode *node = nodeAddStaticNode(NULL, ntree, SH_NODE_DISPLACEMENT);
147     node->locx = 0.5f * (fromnode->locx + tonode->locx);
148     node->locy = 0.5f * (fromnode->locy + tonode->locy);
149 
150     bNodeSocket *scale_socket = nodeFindSocket(node, SOCK_IN, "Scale");
151     bNodeSocket *midlevel_socket = nodeFindSocket(node, SOCK_IN, "Midlevel");
152     bNodeSocket *height_socket = nodeFindSocket(node, SOCK_IN, "Height");
153     bNodeSocket *displacement_socket = nodeFindSocket(node, SOCK_OUT, "Displacement");
154 
155     /* Set default values for compatibility. */
156     *cycles_node_socket_float_value(scale_socket) = 0.1f;
157     *cycles_node_socket_float_value(midlevel_socket) = 0.0f;
158 
159     /* Link to input and material output node. */
160     nodeAddLink(ntree, fromnode, fromsock, node, height_socket);
161     nodeAddLink(ntree, node, displacement_socket, tonode, tosock);
162 
163     need_update = true;
164   }
165 
166   if (need_update) {
167     ntreeUpdateTree(NULL, ntree);
168   }
169 }
170 
displacement_principled_nodes(bNode * node)171 static void displacement_principled_nodes(bNode *node)
172 {
173   if (node->type == SH_NODE_DISPLACEMENT) {
174     if (node->custom1 != SHD_SPACE_WORLD) {
175       node->custom1 = SHD_SPACE_OBJECT;
176     }
177   }
178   else if (node->type == SH_NODE_BSDF_PRINCIPLED) {
179     if (node->custom2 != SHD_SUBSURFACE_RANDOM_WALK) {
180       node->custom2 = SHD_SUBSURFACE_BURLEY;
181     }
182   }
183 }
184 
node_has_roughness(bNode * node)185 static bool node_has_roughness(bNode *node)
186 {
187   return ELEM(node->type,
188               SH_NODE_BSDF_ANISOTROPIC,
189               SH_NODE_BSDF_GLASS,
190               SH_NODE_BSDF_GLOSSY,
191               SH_NODE_BSDF_REFRACTION);
192 }
193 
square_roughness_node_insert(bNodeTree * ntree)194 static void square_roughness_node_insert(bNodeTree *ntree)
195 {
196   bool need_update = false;
197 
198   /* Update default values */
199   LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
200     if (node_has_roughness(node)) {
201       bNodeSocket *roughness_input = nodeFindSocket(node, SOCK_IN, "Roughness");
202       float *roughness_value = cycles_node_socket_float_value(roughness_input);
203       *roughness_value = sqrtf(max_ff(*roughness_value, 0.0f));
204     }
205   }
206 
207   /* Iterate backwards from end so we don't encounter newly added links. */
208   bNodeLink *prevlink;
209   for (bNodeLink *link = ntree->links.last; link; link = prevlink) {
210     prevlink = link->prev;
211 
212     /* Detect link to replace. */
213     bNode *fromnode = link->fromnode;
214     bNodeSocket *fromsock = link->fromsock;
215     bNode *tonode = link->tonode;
216     bNodeSocket *tosock = link->tosock;
217 
218     if (!(node_has_roughness(tonode) && STREQ(tosock->identifier, "Roughness"))) {
219       continue;
220     }
221 
222     /* Replace links with sqrt node */
223     nodeRemLink(ntree, link);
224 
225     /* Add sqrt node. */
226     bNode *node = nodeAddStaticNode(NULL, ntree, SH_NODE_MATH);
227     node->custom1 = NODE_MATH_POWER;
228     node->locx = 0.5f * (fromnode->locx + tonode->locx);
229     node->locy = 0.5f * (fromnode->locy + tonode->locy);
230 
231     /* Link to input and material output node. */
232     *cycles_node_socket_float_value(node->inputs.last) = 0.5f;
233     nodeAddLink(ntree, fromnode, fromsock, node, node->inputs.first);
234     nodeAddLink(ntree, node, node->outputs.first, tonode, tosock);
235 
236     need_update = true;
237   }
238 
239   if (need_update) {
240     ntreeUpdateTree(NULL, ntree);
241   }
242 }
243 
mapping_node_order_flip(bNode * node)244 static void mapping_node_order_flip(bNode *node)
245 {
246   /* Flip euler order of mapping shader node */
247   if (node->type == SH_NODE_MAPPING && node->storage) {
248     TexMapping *texmap = node->storage;
249 
250     float quat[4];
251     eulO_to_quat(quat, texmap->rot, EULER_ORDER_ZYX);
252     quat_to_eulO(texmap->rot, EULER_ORDER_XYZ, quat);
253   }
254 }
255 
vector_curve_node_remap(bNode * node)256 static void vector_curve_node_remap(bNode *node)
257 {
258   /* Remap values of vector curve node from normalized to absolute values */
259   if (node->type == SH_NODE_CURVE_VEC && node->storage) {
260     CurveMapping *mapping = node->storage;
261     mapping->flag &= ~CUMA_DO_CLIP;
262 
263     for (int curve_index = 0; curve_index < CM_TOT; curve_index++) {
264       CurveMap *cm = &mapping->cm[curve_index];
265       if (cm->curve) {
266         for (int i = 0; i < cm->totpoint; i++) {
267           cm->curve[i].x = (cm->curve[i].x * 2.0f) - 1.0f;
268           cm->curve[i].y = (cm->curve[i].y - 0.5f) * 2.0f;
269         }
270       }
271     }
272 
273     BKE_curvemapping_changed_all(mapping);
274   }
275 }
276 
ambient_occlusion_node_relink(bNodeTree * ntree)277 static void ambient_occlusion_node_relink(bNodeTree *ntree)
278 {
279   bool need_update = false;
280 
281   /* Set default values. */
282   LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
283     if (node->type == SH_NODE_AMBIENT_OCCLUSION) {
284       node->custom1 = 1; /* samples */
285       node->custom2 &= ~SHD_AO_LOCAL;
286 
287       bNodeSocket *distance_socket = nodeFindSocket(node, SOCK_IN, "Distance");
288       *cycles_node_socket_float_value(distance_socket) = 0.0f;
289     }
290   }
291 
292   /* Iterate backwards from end so we don't encounter newly added links. */
293   bNodeLink *prevlink;
294   for (bNodeLink *link = ntree->links.last; link; link = prevlink) {
295     prevlink = link->prev;
296 
297     /* Detect link to replace. */
298     bNode *fromnode = link->fromnode;
299     bNode *tonode = link->tonode;
300     bNodeSocket *tosock = link->tosock;
301 
302     if (!(fromnode->type == SH_NODE_AMBIENT_OCCLUSION)) {
303       continue;
304     }
305 
306     /* Replace links with color socket. */
307     nodeRemLink(ntree, link);
308     bNodeSocket *color_socket = nodeFindSocket(fromnode, SOCK_OUT, "Color");
309     nodeAddLink(ntree, fromnode, color_socket, tonode, tosock);
310 
311     need_update = true;
312   }
313 
314   if (need_update) {
315     ntreeUpdateTree(NULL, ntree);
316   }
317 }
318 
image_node_colorspace(bNode * node)319 static void image_node_colorspace(bNode *node)
320 {
321   if (node->id == NULL) {
322     return;
323   }
324 
325   int color_space;
326   if (node->type == SH_NODE_TEX_IMAGE && node->storage) {
327     NodeTexImage *tex = node->storage;
328     color_space = tex->color_space;
329   }
330   else if (node->type == SH_NODE_TEX_ENVIRONMENT && node->storage) {
331     NodeTexEnvironment *tex = node->storage;
332     color_space = tex->color_space;
333   }
334   else {
335     return;
336   }
337 
338   const int SHD_COLORSPACE_NONE = 0;
339   Image *image = (Image *)node->id;
340   if (color_space == SHD_COLORSPACE_NONE) {
341     STRNCPY(image->colorspace_settings.name,
342             IMB_colormanagement_role_colorspace_name_get(COLOR_ROLE_DATA));
343   }
344 }
345 
light_emission_node_to_energy(Light * light,float * energy,float color[3])346 static void light_emission_node_to_energy(Light *light, float *energy, float color[3])
347 {
348   *energy = 1.0;
349   copy_v3_fl(color, 1.0f);
350 
351   /* If nodetree has animation or drivers, don't try to convert. */
352   bNodeTree *ntree = light->nodetree;
353   if (ntree == NULL || ntree->adt) {
354     return;
355   }
356 
357   /* Find emission node */
358   bNode *output_node = ntreeShaderOutputNode(ntree, SHD_OUTPUT_CYCLES);
359   if (output_node == NULL) {
360     return;
361   }
362 
363   bNode *emission_node = NULL;
364   LISTBASE_FOREACH (bNodeLink *, link, &ntree->links) {
365     if (link->tonode == output_node && link->fromnode->type == SH_NODE_EMISSION) {
366       emission_node = link->fromnode;
367       break;
368     }
369   }
370 
371   if (emission_node == NULL) {
372     return;
373   }
374 
375   /* Don't convert if anything is linked */
376   bNodeSocket *strength_socket = nodeFindSocket(emission_node, SOCK_IN, "Strength");
377   bNodeSocket *color_socket = nodeFindSocket(emission_node, SOCK_IN, "Color");
378 
379   if ((strength_socket->flag & SOCK_IN_USE) || (color_socket->flag & SOCK_IN_USE)) {
380     return;
381   }
382 
383   float *strength_value = cycles_node_socket_float_value(strength_socket);
384   float *color_value = cycles_node_socket_rgba_value(color_socket);
385 
386   *energy = *strength_value;
387   copy_v3_v3(color, color_value);
388 
389   *strength_value = 1.0f;
390   copy_v4_fl(color_value, 1.0f);
391   light->use_nodes = false;
392 }
393 
light_emission_unify(Light * light,const char * engine)394 static void light_emission_unify(Light *light, const char *engine)
395 {
396   if (light->type != LA_SUN) {
397     light->energy *= 100.0f;
398   }
399 
400   /* Attempt to extract constant energy and color from nodes. */
401   bool use_nodes = light->use_nodes;
402   float energy, color[3];
403   light_emission_node_to_energy(light, &energy, color);
404 
405   if (STREQ(engine, "CYCLES")) {
406     if (use_nodes) {
407       /* Energy extracted from nodes */
408       light->energy = energy;
409       copy_v3_v3(&light->r, color);
410     }
411     else {
412       /* Default cycles multipliers if there are no nodes */
413       if (light->type == LA_SUN) {
414         light->energy = 1.0f;
415       }
416       else {
417         light->energy = 100.0f;
418       }
419     }
420   }
421   else {
422     /* Disable nodes if scene was configured for Eevee */
423     light->use_nodes = false;
424   }
425 }
426 
427 /* The B input of the Math node is no longer used for single-operand operators.
428  * Previously, if the B input was linked and the A input was not, the B input
429  * was used as the input of the operator. To correct this, we move the link
430  * from B to A if B is linked and A is not.
431  */
update_math_node_single_operand_operators(bNodeTree * ntree)432 static void update_math_node_single_operand_operators(bNodeTree *ntree)
433 {
434   bool need_update = false;
435 
436   LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
437     if (node->type == SH_NODE_MATH) {
438       if (ELEM(node->custom1,
439                NODE_MATH_SQRT,
440                NODE_MATH_CEIL,
441                NODE_MATH_SINE,
442                NODE_MATH_ROUND,
443                NODE_MATH_FLOOR,
444                NODE_MATH_COSINE,
445                NODE_MATH_ARCSINE,
446                NODE_MATH_TANGENT,
447                NODE_MATH_ABSOLUTE,
448                NODE_MATH_FRACTION,
449                NODE_MATH_ARCCOSINE,
450                NODE_MATH_ARCTANGENT)) {
451         bNodeSocket *sockA = BLI_findlink(&node->inputs, 0);
452         bNodeSocket *sockB = BLI_findlink(&node->inputs, 1);
453         if (!sockA->link && sockB->link) {
454           nodeAddLink(ntree, sockB->link->fromnode, sockB->link->fromsock, node, sockA);
455           nodeRemLink(ntree, sockB->link);
456           need_update = true;
457         }
458       }
459     }
460   }
461 
462   if (need_update) {
463     ntreeUpdateTree(NULL, ntree);
464   }
465 }
466 
467 /* The Value output of the Vector Math node is no longer available in the Add
468  * and Subtract operators. Previously, this Value output was computed from the
469  * Vector output V as follows:
470  *
471  *   Value = (abs(V.x) + abs(V.y) + abs(V.z)) / 3
472  *
473  * Or more compactly using vector operators:
474  *
475  *   Value = dot(abs(V), (1 / 3, 1 / 3, 1 / 3))
476  *
477  * To correct this, if the Value output was used, we are going to compute
478  * it using the second equation by adding an absolute and a dot node, and
479  * then connect them appropriately.
480  */
update_vector_math_node_add_and_subtract_operators(bNodeTree * ntree)481 static void update_vector_math_node_add_and_subtract_operators(bNodeTree *ntree)
482 {
483   bool need_update = false;
484 
485   LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
486     if (node->type == SH_NODE_VECTOR_MATH) {
487       bNodeSocket *sockOutValue = nodeFindSocket(node, SOCK_OUT, "Value");
488       if (socket_is_used(sockOutValue) &&
489           ELEM(node->custom1, NODE_VECTOR_MATH_ADD, NODE_VECTOR_MATH_SUBTRACT)) {
490 
491         bNode *absNode = nodeAddStaticNode(NULL, ntree, SH_NODE_VECTOR_MATH);
492         absNode->custom1 = NODE_VECTOR_MATH_ABSOLUTE;
493         absNode->locx = node->locx + node->width + 20.0f;
494         absNode->locy = node->locy;
495 
496         bNode *dotNode = nodeAddStaticNode(NULL, ntree, SH_NODE_VECTOR_MATH);
497         dotNode->custom1 = NODE_VECTOR_MATH_DOT_PRODUCT;
498         dotNode->locx = absNode->locx + absNode->width + 20.0f;
499         dotNode->locy = absNode->locy;
500         bNodeSocket *sockDotB = BLI_findlink(&dotNode->inputs, 1);
501         bNodeSocket *sockDotOutValue = nodeFindSocket(dotNode, SOCK_OUT, "Value");
502         copy_v3_fl(cycles_node_socket_vector_value(sockDotB), 1 / 3.0f);
503 
504         LISTBASE_FOREACH_BACKWARD_MUTABLE (bNodeLink *, link, &ntree->links) {
505           if (link->fromsock == sockOutValue) {
506             nodeAddLink(ntree, dotNode, sockDotOutValue, link->tonode, link->tosock);
507             nodeRemLink(ntree, link);
508           }
509         }
510 
511         bNodeSocket *sockAbsA = BLI_findlink(&absNode->inputs, 0);
512         bNodeSocket *sockDotA = BLI_findlink(&dotNode->inputs, 0);
513         bNodeSocket *sockOutVector = nodeFindSocket(node, SOCK_OUT, "Vector");
514         bNodeSocket *sockAbsOutVector = nodeFindSocket(absNode, SOCK_OUT, "Vector");
515 
516         nodeAddLink(ntree, node, sockOutVector, absNode, sockAbsA);
517         nodeAddLink(ntree, absNode, sockAbsOutVector, dotNode, sockDotA);
518 
519         need_update = true;
520       }
521     }
522   }
523 
524   if (need_update) {
525     ntreeUpdateTree(NULL, ntree);
526   }
527 }
528 
529 /* The Vector output of the Vector Math node is no longer available in the Dot
530  * Product operator. Previously, this Vector was always zero initialized. To
531  * correct this, we zero out any socket the Vector Output was connected to.
532  */
update_vector_math_node_dot_product_operator(bNodeTree * ntree)533 static void update_vector_math_node_dot_product_operator(bNodeTree *ntree)
534 {
535   bool need_update = false;
536 
537   LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
538     if (node->type == SH_NODE_VECTOR_MATH) {
539       bNodeSocket *sockOutVector = nodeFindSocket(node, SOCK_OUT, "Vector");
540       if (socket_is_used(sockOutVector) && node->custom1 == NODE_VECTOR_MATH_DOT_PRODUCT) {
541         LISTBASE_FOREACH_MUTABLE (bNodeLink *, link, &ntree->links) {
542           if (link->fromsock == sockOutVector) {
543             switch (link->tosock->type) {
544               case SOCK_FLOAT:
545                 *cycles_node_socket_float_value(link->tosock) = 0.0f;
546                 break;
547               case SOCK_VECTOR:
548                 copy_v3_fl(cycles_node_socket_vector_value(link->tosock), 0.0f);
549                 break;
550               case SOCK_RGBA:
551                 copy_v4_fl(cycles_node_socket_rgba_value(link->tosock), 0.0f);
552                 break;
553             }
554             nodeRemLink(ntree, link);
555           }
556         }
557         need_update = true;
558       }
559     }
560   }
561 
562   if (need_update) {
563     ntreeUpdateTree(NULL, ntree);
564   }
565 }
566 
567 /* Previously, the Vector output of the cross product operator was normalized.
568  * To correct this, a Normalize node is added to normalize the output if used.
569  * Moreover, the Value output was removed. This Value was equal to the length
570  * of the cross product. To correct this, a Length node is added if needed.
571  */
update_vector_math_node_cross_product_operator(bNodeTree * ntree)572 static void update_vector_math_node_cross_product_operator(bNodeTree *ntree)
573 {
574   bool need_update = false;
575 
576   LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
577     if (node->type == SH_NODE_VECTOR_MATH) {
578       if (node->custom1 == NODE_VECTOR_MATH_CROSS_PRODUCT) {
579         bNodeSocket *sockOutVector = nodeFindSocket(node, SOCK_OUT, "Vector");
580         if (socket_is_used(sockOutVector)) {
581           bNode *normalizeNode = nodeAddStaticNode(NULL, ntree, SH_NODE_VECTOR_MATH);
582           normalizeNode->custom1 = NODE_VECTOR_MATH_NORMALIZE;
583           normalizeNode->locx = node->locx + node->width + 20.0f;
584           normalizeNode->locy = node->locy;
585           bNodeSocket *sockNormalizeOut = nodeFindSocket(normalizeNode, SOCK_OUT, "Vector");
586 
587           LISTBASE_FOREACH_BACKWARD_MUTABLE (bNodeLink *, link, &ntree->links) {
588             if (link->fromsock == sockOutVector) {
589               nodeAddLink(ntree, normalizeNode, sockNormalizeOut, link->tonode, link->tosock);
590               nodeRemLink(ntree, link);
591             }
592           }
593           bNodeSocket *sockNormalizeA = BLI_findlink(&normalizeNode->inputs, 0);
594           nodeAddLink(ntree, node, sockOutVector, normalizeNode, sockNormalizeA);
595 
596           need_update = true;
597         }
598 
599         bNodeSocket *sockOutValue = nodeFindSocket(node, SOCK_OUT, "Value");
600         if (socket_is_used(sockOutValue)) {
601           bNode *lengthNode = nodeAddStaticNode(NULL, ntree, SH_NODE_VECTOR_MATH);
602           lengthNode->custom1 = NODE_VECTOR_MATH_LENGTH;
603           lengthNode->locx = node->locx + node->width + 20.0f;
604           if (socket_is_used(sockOutVector)) {
605             lengthNode->locy = node->locy - lengthNode->height - 20.0f;
606           }
607           else {
608             lengthNode->locy = node->locy;
609           }
610           bNodeSocket *sockLengthOut = nodeFindSocket(lengthNode, SOCK_OUT, "Value");
611 
612           LISTBASE_FOREACH_BACKWARD_MUTABLE (bNodeLink *, link, &ntree->links) {
613             if (link->fromsock == sockOutValue) {
614               nodeAddLink(ntree, lengthNode, sockLengthOut, link->tonode, link->tosock);
615               nodeRemLink(ntree, link);
616             }
617           }
618           bNodeSocket *sockLengthA = BLI_findlink(&lengthNode->inputs, 0);
619           nodeAddLink(ntree, node, sockOutVector, lengthNode, sockLengthA);
620 
621           need_update = true;
622         }
623       }
624     }
625   }
626 
627   if (need_update) {
628     ntreeUpdateTree(NULL, ntree);
629   }
630 }
631 
632 /* The Value output of the Vector Math node is no longer available in the
633  * Normalize operator. This Value output was equal to the length of the
634  * the input vector A. To correct this, we either add a Length node or
635  * convert the Normalize node into a Length node, depending on if the
636  * Vector output is needed.
637  */
update_vector_math_node_normalize_operator(bNodeTree * ntree)638 static void update_vector_math_node_normalize_operator(bNodeTree *ntree)
639 {
640   bool need_update = false;
641 
642   LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
643     if (node->type == SH_NODE_VECTOR_MATH) {
644       bNodeSocket *sockOutValue = nodeFindSocket(node, SOCK_OUT, "Value");
645       if (node->custom1 == NODE_VECTOR_MATH_NORMALIZE && socket_is_used(sockOutValue)) {
646         bNodeSocket *sockOutVector = nodeFindSocket(node, SOCK_OUT, "Vector");
647         if (socket_is_used(sockOutVector)) {
648           bNode *lengthNode = nodeAddStaticNode(NULL, ntree, SH_NODE_VECTOR_MATH);
649           lengthNode->custom1 = NODE_VECTOR_MATH_LENGTH;
650           lengthNode->locx = node->locx + node->width + 20.0f;
651           lengthNode->locy = node->locy;
652           bNodeSocket *sockLengthValue = nodeFindSocket(lengthNode, SOCK_OUT, "Value");
653 
654           LISTBASE_FOREACH_BACKWARD_MUTABLE (bNodeLink *, link, &ntree->links) {
655             if (link->fromsock == sockOutValue) {
656               nodeAddLink(ntree, lengthNode, sockLengthValue, link->tonode, link->tosock);
657               nodeRemLink(ntree, link);
658             }
659           }
660           bNodeSocket *sockA = BLI_findlink(&node->inputs, 0);
661           bNodeSocket *sockLengthA = BLI_findlink(&lengthNode->inputs, 0);
662           if (sockA->link) {
663             bNodeLink *link = sockA->link;
664             nodeAddLink(ntree, link->fromnode, link->fromsock, lengthNode, sockLengthA);
665           }
666           else {
667             copy_v3_v3(cycles_node_socket_vector_value(sockLengthA),
668                        cycles_node_socket_vector_value(sockA));
669           }
670 
671           need_update = true;
672         }
673         else {
674           node->custom1 = NODE_VECTOR_MATH_LENGTH;
675         }
676       }
677     }
678   }
679   if (need_update) {
680     ntreeUpdateTree(NULL, ntree);
681   }
682 }
683 
684 /* The Vector Math operator types didn't have an enum, but rather, their
685  * values were hard coded into the code. After the enum was created and
686  * after more vector operators were added, the hard coded values needs
687  * to be remapped to their correct enum values. To fix this, we remap
688  * the values according to the following rules:
689  *
690  * Dot Product Operator : 3 -> 7
691  * Normalize Operator   : 5 -> 11
692  *
693  * Additionally, since the Average operator was removed, it is assigned
694  * a value of -1 just to be identified later in the versioning code:
695  *
696  * Average Operator : 2 -> -1
697  *
698  */
update_vector_math_node_operators_enum_mapping(bNodeTree * ntree)699 static void update_vector_math_node_operators_enum_mapping(bNodeTree *ntree)
700 {
701   LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
702     if (node->type == SH_NODE_VECTOR_MATH) {
703       switch (node->custom1) {
704         case 2:
705           node->custom1 = -1;
706           break;
707         case 3:
708           node->custom1 = 7;
709           break;
710         case 5:
711           node->custom1 = 11;
712           break;
713       }
714     }
715   }
716 }
717 
718 /* The Average operator is no longer available in the Vector Math node.
719  * The Vector output was equal to the normalized sum of input vectors while
720  * the Value output was equal to the length of the sum of input vectors.
721  * To correct this, we convert the node into an Add node and add a length
722  * node or a normalize node if needed.
723  */
update_vector_math_node_average_operator(bNodeTree * ntree)724 static void update_vector_math_node_average_operator(bNodeTree *ntree)
725 {
726   bool need_update = false;
727 
728   LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
729     if (node->type == SH_NODE_VECTOR_MATH) {
730       /* See update_vector_math_node_operators_enum_mapping. */
731       if (node->custom1 == -1) {
732         node->custom1 = NODE_VECTOR_MATH_ADD;
733         bNodeSocket *sockOutVector = nodeFindSocket(node, SOCK_OUT, "Vector");
734         if (socket_is_used(sockOutVector)) {
735           bNode *normalizeNode = nodeAddStaticNode(NULL, ntree, SH_NODE_VECTOR_MATH);
736           normalizeNode->custom1 = NODE_VECTOR_MATH_NORMALIZE;
737           normalizeNode->locx = node->locx + node->width + 20.0f;
738           normalizeNode->locy = node->locy;
739           bNodeSocket *sockNormalizeOut = nodeFindSocket(normalizeNode, SOCK_OUT, "Vector");
740 
741           LISTBASE_FOREACH_BACKWARD_MUTABLE (bNodeLink *, link, &ntree->links) {
742             if (link->fromsock == sockOutVector) {
743               nodeAddLink(ntree, normalizeNode, sockNormalizeOut, link->tonode, link->tosock);
744               nodeRemLink(ntree, link);
745             }
746           }
747           bNodeSocket *sockNormalizeA = BLI_findlink(&normalizeNode->inputs, 0);
748           nodeAddLink(ntree, node, sockOutVector, normalizeNode, sockNormalizeA);
749 
750           need_update = true;
751         }
752 
753         bNodeSocket *sockOutValue = nodeFindSocket(node, SOCK_OUT, "Value");
754         if (socket_is_used(sockOutValue)) {
755           bNode *lengthNode = nodeAddStaticNode(NULL, ntree, SH_NODE_VECTOR_MATH);
756           lengthNode->custom1 = NODE_VECTOR_MATH_LENGTH;
757           lengthNode->locx = node->locx + node->width + 20.0f;
758           if (socket_is_used(sockOutVector)) {
759             lengthNode->locy = node->locy - lengthNode->height - 20.0f;
760           }
761           else {
762             lengthNode->locy = node->locy;
763           }
764           bNodeSocket *sockLengthOut = nodeFindSocket(lengthNode, SOCK_OUT, "Value");
765 
766           LISTBASE_FOREACH_BACKWARD_MUTABLE (bNodeLink *, link, &ntree->links) {
767             if (link->fromsock == sockOutValue) {
768               nodeAddLink(ntree, lengthNode, sockLengthOut, link->tonode, link->tosock);
769               nodeRemLink(ntree, link);
770             }
771           }
772           bNodeSocket *sockLengthA = BLI_findlink(&lengthNode->inputs, 0);
773           nodeAddLink(ntree, node, sockOutVector, lengthNode, sockLengthA);
774 
775           need_update = true;
776         }
777       }
778     }
779   }
780 
781   if (need_update) {
782     ntreeUpdateTree(NULL, ntree);
783   }
784 }
785 
786 /* The Noise node now have a dimension property. This property should be
787  * initialized to 3 by default.
788  */
update_noise_node_dimensions(bNodeTree * ntree)789 static void update_noise_node_dimensions(bNodeTree *ntree)
790 {
791   LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
792     if (node->type == SH_NODE_TEX_NOISE && node->storage) {
793       NodeTexNoise *tex = (NodeTexNoise *)node->storage;
794       tex->dimensions = 3;
795     }
796   }
797 }
798 
799 /* This structure is only used to pass data to
800  * update_mapping_node_fcurve_rna_path_callback.
801  */
802 typedef struct {
803   char *nodePath;
804   bNode *minimumNode;
805   bNode *maximumNode;
806 } MappingNodeFCurveCallbackData;
807 
808 /* This callback function is used by update_mapping_node_inputs_and_properties.
809  * It is executed on every fcurve in the nodetree id updating its RNA paths. The
810  * paths needs to be updated because the node properties became inputs.
811  *
812  * nodes["Mapping"].translation --> nodes["Mapping"].inputs[1].default_value
813  * nodes["Mapping"].rotation --> nodes["Mapping"].inputs[2].default_value
814  * nodes["Mapping"].scale --> nodes["Mapping"].inputs[3].default_value
815  * nodes["Mapping"].max --> nodes["Maximum"].inputs[1].default_value
816  * nodes["Mapping"].min --> nodes["Minimum"].inputs[1].default_value
817  *
818  * The fcurve can be that of any node or property in the nodetree, so we only
819  * update if the rna path starts with the rna path of the mapping node and
820  * doesn't end with "default_value", that is, not the Vector input.
821  */
update_mapping_node_fcurve_rna_path_callback(ID * UNUSED (id),FCurve * fcurve,void * _data)822 static void update_mapping_node_fcurve_rna_path_callback(ID *UNUSED(id),
823                                                          FCurve *fcurve,
824                                                          void *_data)
825 {
826   MappingNodeFCurveCallbackData *data = (MappingNodeFCurveCallbackData *)_data;
827   if (!STRPREFIX(fcurve->rna_path, data->nodePath) ||
828       BLI_str_endswith(fcurve->rna_path, "default_value")) {
829     return;
830   }
831   char *old_fcurve_rna_path = fcurve->rna_path;
832 
833   if (BLI_str_endswith(old_fcurve_rna_path, "translation")) {
834     fcurve->rna_path = BLI_sprintfN("%s.%s", data->nodePath, "inputs[1].default_value");
835   }
836   else if (BLI_str_endswith(old_fcurve_rna_path, "rotation")) {
837     fcurve->rna_path = BLI_sprintfN("%s.%s", data->nodePath, "inputs[2].default_value");
838   }
839   else if (BLI_str_endswith(old_fcurve_rna_path, "scale")) {
840     fcurve->rna_path = BLI_sprintfN("%s.%s", data->nodePath, "inputs[3].default_value");
841   }
842   else if (data->minimumNode && BLI_str_endswith(old_fcurve_rna_path, "max")) {
843     fcurve->rna_path = BLI_sprintfN(
844         "nodes[\"%s\"].%s", data->minimumNode->name, "inputs[1].default_value");
845   }
846   else if (data->maximumNode && BLI_str_endswith(old_fcurve_rna_path, "min")) {
847     fcurve->rna_path = BLI_sprintfN(
848         "nodes[\"%s\"].%s", data->maximumNode->name, "inputs[1].default_value");
849   }
850 
851   if (fcurve->rna_path != old_fcurve_rna_path) {
852     MEM_freeN(old_fcurve_rna_path);
853   }
854 }
855 
856 /* The Mapping node has been rewritten to support dynamic inputs. Previously,
857  * the transformation information was stored in a TexMapping struct in the
858  * node->storage member of bNode. Currently, the transformation information
859  * is stored in input sockets. To correct this, we transfer the information
860  * from the TexMapping struct to the input sockets.
861  *
862  * Additionally, the Minimum and Maximum properties are no longer available
863  * in the node. To correct this, a Vector Minimum and/or a Vector Maximum
864  * nodes are added if needed.
865  *
866  * Finally, the TexMapping struct is freed and node->storage is set to NULL.
867  *
868  * Since the RNA paths of the properties changed, we also have to update the
869  * rna_path of the FCurves if they exist. To do that, we loop over FCurves
870  * and check if they control a property of the node, if they do, we update
871  * the path to be that of the corresponding socket in the node or the added
872  * minimum/maximum node.
873  *
874  */
update_mapping_node_inputs_and_properties(bNodeTree * ntree)875 static void update_mapping_node_inputs_and_properties(bNodeTree *ntree)
876 {
877   bool need_update = false;
878 
879   LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
880     /* If node->storage is NULL, then conversion has already taken place.
881      * This can happen if a file with the new mapping node [saved from (2, 81, 8) or newer]
882      * is opened in a blender version prior to (2, 81, 8) and saved from there again. */
883     if (node->type == SH_NODE_MAPPING && node->storage) {
884       TexMapping *mapping = (TexMapping *)node->storage;
885       node->custom1 = mapping->type;
886       node->width = 140.0f;
887 
888       bNodeSocket *sockLocation = nodeFindSocket(node, SOCK_IN, "Location");
889       copy_v3_v3(cycles_node_socket_vector_value(sockLocation), mapping->loc);
890       bNodeSocket *sockRotation = nodeFindSocket(node, SOCK_IN, "Rotation");
891       copy_v3_v3(cycles_node_socket_vector_value(sockRotation), mapping->rot);
892       bNodeSocket *sockScale = nodeFindSocket(node, SOCK_IN, "Scale");
893       copy_v3_v3(cycles_node_socket_vector_value(sockScale), mapping->size);
894 
895       bNode *maximumNode = NULL;
896       if (mapping->flag & TEXMAP_CLIP_MIN) {
897         maximumNode = nodeAddStaticNode(NULL, ntree, SH_NODE_VECTOR_MATH);
898         maximumNode->custom1 = NODE_VECTOR_MATH_MAXIMUM;
899         if (mapping->flag & TEXMAP_CLIP_MAX) {
900           maximumNode->locx = node->locx + (node->width + 20.0f) * 2.0f;
901         }
902         else {
903           maximumNode->locx = node->locx + node->width + 20.0f;
904         }
905         maximumNode->locy = node->locy;
906         bNodeSocket *sockMaximumB = BLI_findlink(&maximumNode->inputs, 1);
907         copy_v3_v3(cycles_node_socket_vector_value(sockMaximumB), mapping->min);
908         bNodeSocket *sockMappingResult = nodeFindSocket(node, SOCK_OUT, "Vector");
909 
910         LISTBASE_FOREACH_BACKWARD_MUTABLE (bNodeLink *, link, &ntree->links) {
911           if (link->fromsock == sockMappingResult) {
912             bNodeSocket *sockMaximumResult = nodeFindSocket(maximumNode, SOCK_OUT, "Vector");
913             nodeAddLink(ntree, maximumNode, sockMaximumResult, link->tonode, link->tosock);
914             nodeRemLink(ntree, link);
915           }
916         }
917         if (!(mapping->flag & TEXMAP_CLIP_MAX)) {
918           bNodeSocket *sockMaximumA = BLI_findlink(&maximumNode->inputs, 0);
919           nodeAddLink(ntree, node, sockMappingResult, maximumNode, sockMaximumA);
920         }
921 
922         need_update = true;
923       }
924 
925       bNode *minimumNode = NULL;
926       if (mapping->flag & TEXMAP_CLIP_MAX) {
927         minimumNode = nodeAddStaticNode(NULL, ntree, SH_NODE_VECTOR_MATH);
928         minimumNode->custom1 = NODE_VECTOR_MATH_MINIMUM;
929         minimumNode->locx = node->locx + node->width + 20.0f;
930         minimumNode->locy = node->locy;
931         bNodeSocket *sockMinimumB = BLI_findlink(&minimumNode->inputs, 1);
932         copy_v3_v3(cycles_node_socket_vector_value(sockMinimumB), mapping->max);
933 
934         bNodeSocket *sockMinimumResult = nodeFindSocket(minimumNode, SOCK_OUT, "Vector");
935         bNodeSocket *sockMappingResult = nodeFindSocket(node, SOCK_OUT, "Vector");
936 
937         if (maximumNode) {
938           bNodeSocket *sockMaximumA = BLI_findlink(&maximumNode->inputs, 0);
939           nodeAddLink(ntree, minimumNode, sockMinimumResult, maximumNode, sockMaximumA);
940         }
941         else {
942           LISTBASE_FOREACH_BACKWARD_MUTABLE (bNodeLink *, link, &ntree->links) {
943             if (link->fromsock == sockMappingResult) {
944               nodeAddLink(ntree, minimumNode, sockMinimumResult, link->tonode, link->tosock);
945               nodeRemLink(ntree, link);
946             }
947           }
948         }
949         bNodeSocket *sockMinimumA = BLI_findlink(&minimumNode->inputs, 0);
950         nodeAddLink(ntree, node, sockMappingResult, minimumNode, sockMinimumA);
951 
952         need_update = true;
953       }
954 
955       MEM_freeN(node->storage);
956       node->storage = NULL;
957 
958       char *nodePath = BLI_sprintfN("nodes[\"%s\"]", node->name);
959       MappingNodeFCurveCallbackData data = {nodePath, minimumNode, maximumNode};
960       BKE_fcurves_id_cb(&ntree->id, update_mapping_node_fcurve_rna_path_callback, &data);
961       MEM_freeN(nodePath);
962     }
963   }
964 
965   if (need_update) {
966     ntreeUpdateTree(NULL, ntree);
967   }
968 }
969 
970 /* The Musgrave node now has a dimension property. This property should
971  * be initialized to 3 by default.
972  */
update_musgrave_node_dimensions(bNodeTree * ntree)973 static void update_musgrave_node_dimensions(bNodeTree *ntree)
974 {
975   LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
976     if (node->type == SH_NODE_TEX_MUSGRAVE && node->storage) {
977       NodeTexMusgrave *tex = (NodeTexMusgrave *)node->storage;
978       tex->dimensions = 3;
979     }
980   }
981 }
982 
983 /* The Color output of the Musgrave node has been removed. Previously, this
984  * output was just equal to the Fac output. To correct this, we move links
985  * from the Color output to the Fac output if they exist.
986  */
update_musgrave_node_color_output(bNodeTree * ntree)987 static void update_musgrave_node_color_output(bNodeTree *ntree)
988 {
989   LISTBASE_FOREACH (bNodeLink *, link, &ntree->links) {
990     if (link->fromnode && link->fromnode->type == SH_NODE_TEX_MUSGRAVE) {
991       if (link->fromsock->type == SOCK_RGBA) {
992         link->fromsock = link->fromsock->next;
993       }
994     }
995   }
996 }
997 
998 /* The Voronoi node now have a dimension property. This property should be
999  * initialized to 3 by default.
1000  */
update_voronoi_node_dimensions(bNodeTree * ntree)1001 static void update_voronoi_node_dimensions(bNodeTree *ntree)
1002 {
1003   LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
1004     if (node->type == SH_NODE_TEX_VORONOI && node->storage) {
1005       NodeTexVoronoi *tex = (NodeTexVoronoi *)node->storage;
1006       tex->dimensions = 3;
1007     }
1008   }
1009 }
1010 
1011 /* The F3 and F4 features of the Voronoi node have been removed.
1012  * To correct this, we set the feature type to be F2 if it is F3
1013  * or F4. The SHD_VORONOI_F3 and SHD_VORONOI_F4 enum values were
1014  * 2 and 3 respectively.
1015  */
update_voronoi_node_f3_and_f4(bNodeTree * ntree)1016 static void update_voronoi_node_f3_and_f4(bNodeTree *ntree)
1017 {
1018   LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
1019     if (node->type == SH_NODE_TEX_VORONOI && node->storage) {
1020       NodeTexVoronoi *tex = (NodeTexVoronoi *)node->storage;
1021       if (ELEM(tex->feature, 2, 3)) {
1022         tex->feature = SHD_VORONOI_F2;
1023       }
1024     }
1025   }
1026 }
1027 
1028 /* The Fac output of the Voronoi node has been removed. Previously, this
1029  * output was the voronoi distance in the Intensity mode and the Cell ID
1030  * in the Cell mode. To correct this, we update the identifier and name
1031  * of the Fac socket such that it gets mapped to the Distance socket.
1032  * This is supposed to work with update_voronoi_node_coloring.
1033  */
update_voronoi_node_fac_output(bNodeTree * ntree)1034 static void update_voronoi_node_fac_output(bNodeTree *ntree)
1035 {
1036   LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
1037     if (node->type == SH_NODE_TEX_VORONOI) {
1038       bNodeSocket *facOutput = BLI_findlink(&node->outputs, 1);
1039       strcpy(facOutput->identifier, "Distance");
1040       strcpy(facOutput->name, "Distance");
1041     }
1042   }
1043 }
1044 
1045 /* The Crackle feature of the Voronoi node has been removed. Previously,
1046  * this feature returned the F2 distance minus the F1 distance. The
1047  * crackle feature had an enum value of 4. To fix this we do the
1048  * following:
1049  *
1050  * 1. The node feature is set to F1.
1051  * 2. A new Voronoi node is added and its feature is set to F2.
1052  * 3. The properties, input values, and connections are copied
1053  *    from the node to the new Voronoi node so that they match
1054  *    exactly.
1055  * 4. A Subtract node is added.
1056  * 5. The outputs of the F1 and F2 voronoi are connected to
1057  *    the inputs of the subtract node.
1058  * 6. The output of the subtract node is connected to the
1059  *    appropriate sockets.
1060  *
1061  */
update_voronoi_node_crackle(bNodeTree * ntree)1062 static void update_voronoi_node_crackle(bNodeTree *ntree)
1063 {
1064   bool need_update = false;
1065 
1066   LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
1067     if (node->type == SH_NODE_TEX_VORONOI && node->storage) {
1068       NodeTexVoronoi *tex = (NodeTexVoronoi *)node->storage;
1069       bNodeSocket *sockDistance = nodeFindSocket(node, SOCK_OUT, "Distance");
1070       bNodeSocket *sockColor = nodeFindSocket(node, SOCK_OUT, "Color");
1071       if (tex->feature == 4 && (socket_is_used(sockDistance) || socket_is_used(sockColor))) {
1072         tex->feature = SHD_VORONOI_F1;
1073 
1074         bNode *voronoiNode = nodeAddStaticNode(NULL, ntree, SH_NODE_TEX_VORONOI);
1075         NodeTexVoronoi *texVoronoi = (NodeTexVoronoi *)voronoiNode->storage;
1076         texVoronoi->feature = SHD_VORONOI_F2;
1077         texVoronoi->distance = tex->distance;
1078         texVoronoi->dimensions = 3;
1079         voronoiNode->locx = node->locx + node->width + 20.0f;
1080         voronoiNode->locy = node->locy;
1081 
1082         bNodeSocket *sockVector = nodeFindSocket(node, SOCK_IN, "Vector");
1083         bNodeSocket *sockScale = nodeFindSocket(node, SOCK_IN, "Scale");
1084         bNodeSocket *sockExponent = nodeFindSocket(node, SOCK_IN, "Exponent");
1085         bNodeSocket *sockVoronoiVector = nodeFindSocket(voronoiNode, SOCK_IN, "Vector");
1086         bNodeSocket *sockVoronoiScale = nodeFindSocket(voronoiNode, SOCK_IN, "Scale");
1087         bNodeSocket *sockVoronoiExponent = nodeFindSocket(voronoiNode, SOCK_IN, "Exponent");
1088         if (sockVector->link) {
1089           nodeAddLink(ntree,
1090                       sockVector->link->fromnode,
1091                       sockVector->link->fromsock,
1092                       voronoiNode,
1093                       sockVoronoiVector);
1094         }
1095         *cycles_node_socket_float_value(sockVoronoiScale) = *cycles_node_socket_float_value(
1096             sockScale);
1097         if (sockScale->link) {
1098           nodeAddLink(ntree,
1099                       sockScale->link->fromnode,
1100                       sockScale->link->fromsock,
1101                       voronoiNode,
1102                       sockVoronoiScale);
1103         }
1104         *cycles_node_socket_float_value(sockVoronoiExponent) = *cycles_node_socket_float_value(
1105             sockExponent);
1106         if (sockExponent->link) {
1107           nodeAddLink(ntree,
1108                       sockExponent->link->fromnode,
1109                       sockExponent->link->fromsock,
1110                       voronoiNode,
1111                       sockVoronoiExponent);
1112         }
1113 
1114         bNode *subtractNode = nodeAddStaticNode(NULL, ntree, SH_NODE_MATH);
1115         subtractNode->custom1 = NODE_MATH_SUBTRACT;
1116         subtractNode->locx = voronoiNode->locx + voronoiNode->width + 20.0f;
1117         subtractNode->locy = voronoiNode->locy;
1118         bNodeSocket *sockSubtractOutValue = nodeFindSocket(subtractNode, SOCK_OUT, "Value");
1119 
1120         LISTBASE_FOREACH_BACKWARD_MUTABLE (bNodeLink *, link, &ntree->links) {
1121           if (link->fromnode == node) {
1122             nodeAddLink(ntree, subtractNode, sockSubtractOutValue, link->tonode, link->tosock);
1123             nodeRemLink(ntree, link);
1124           }
1125         }
1126 
1127         bNodeSocket *sockDistanceF1 = nodeFindSocket(node, SOCK_OUT, "Distance");
1128         bNodeSocket *sockDistanceF2 = nodeFindSocket(voronoiNode, SOCK_OUT, "Distance");
1129         bNodeSocket *sockSubtractA = BLI_findlink(&subtractNode->inputs, 0);
1130         bNodeSocket *sockSubtractB = BLI_findlink(&subtractNode->inputs, 1);
1131 
1132         nodeAddLink(ntree, node, sockDistanceF1, subtractNode, sockSubtractB);
1133         nodeAddLink(ntree, voronoiNode, sockDistanceF2, subtractNode, sockSubtractA);
1134 
1135         need_update = true;
1136       }
1137     }
1138   }
1139 
1140   if (need_update) {
1141     ntreeUpdateTree(NULL, ntree);
1142   }
1143 }
1144 
1145 /**
1146  * The coloring property of the Voronoi node was removed. Previously,
1147  * if the coloring enum was set to Intensity (0), the voronoi distance
1148  * was returned in all outputs, otherwise, the Cell ID was returned.
1149  * Since we remapped the Fac output in update_voronoi_node_fac_output,
1150  * then to fix this, we relink the Color output to the Distance
1151  * output if coloring was set to 0, and the other way around otherwise.
1152  */
update_voronoi_node_coloring(bNodeTree * ntree)1153 static void update_voronoi_node_coloring(bNodeTree *ntree)
1154 {
1155   bool need_update = false;
1156 
1157   LISTBASE_FOREACH_BACKWARD_MUTABLE (bNodeLink *, link, &ntree->links) {
1158     bNode *node = link->fromnode;
1159     if (node && node->type == SH_NODE_TEX_VORONOI && node->storage) {
1160       NodeTexVoronoi *tex = (NodeTexVoronoi *)node->storage;
1161       if (tex->coloring == 0) {
1162         bNodeSocket *sockColor = nodeFindSocket(node, SOCK_OUT, "Color");
1163         if (link->fromsock == sockColor) {
1164           bNodeSocket *sockDistance = nodeFindSocket(node, SOCK_OUT, "Distance");
1165           nodeAddLink(ntree, node, sockDistance, link->tonode, link->tosock);
1166           nodeRemLink(ntree, link);
1167           need_update = true;
1168         }
1169       }
1170       else {
1171         bNodeSocket *sockDistance = nodeFindSocket(node, SOCK_OUT, "Distance");
1172         if (link->fromsock == sockDistance) {
1173           bNodeSocket *sockColor = nodeFindSocket(node, SOCK_OUT, "Color");
1174           nodeAddLink(ntree, node, sockColor, link->tonode, link->tosock);
1175           nodeRemLink(ntree, link);
1176           need_update = true;
1177         }
1178       }
1179     }
1180   }
1181 
1182   if (need_update) {
1183     ntreeUpdateTree(NULL, ntree);
1184   }
1185 }
1186 
1187 /* Previously, the output euclidean distance was actually the squared
1188  * euclidean distance. To fix this, we square the output distance
1189  * socket if the distance metric is set to SHD_VORONOI_EUCLIDEAN.
1190  */
update_voronoi_node_square_distance(bNodeTree * ntree)1191 static void update_voronoi_node_square_distance(bNodeTree *ntree)
1192 {
1193   bool need_update = false;
1194 
1195   LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
1196     if (node->type == SH_NODE_TEX_VORONOI && node->storage) {
1197       NodeTexVoronoi *tex = (NodeTexVoronoi *)node->storage;
1198       bNodeSocket *sockDistance = nodeFindSocket(node, SOCK_OUT, "Distance");
1199       if (tex->distance == SHD_VORONOI_EUCLIDEAN &&
1200           (tex->feature == SHD_VORONOI_F1 || tex->feature == SHD_VORONOI_F2) &&
1201           socket_is_used(sockDistance)) {
1202         bNode *multiplyNode = nodeAddStaticNode(NULL, ntree, SH_NODE_MATH);
1203         multiplyNode->custom1 = NODE_MATH_MULTIPLY;
1204         multiplyNode->locx = node->locx + node->width + 20.0f;
1205         multiplyNode->locy = node->locy;
1206 
1207         bNodeSocket *sockValue = nodeFindSocket(multiplyNode, SOCK_OUT, "Value");
1208         LISTBASE_FOREACH_BACKWARD_MUTABLE (bNodeLink *, link, &ntree->links) {
1209           if (link->fromsock == sockDistance) {
1210             nodeAddLink(ntree, multiplyNode, sockValue, link->tonode, link->tosock);
1211             nodeRemLink(ntree, link);
1212           }
1213         }
1214 
1215         bNodeSocket *sockMultiplyA = BLI_findlink(&multiplyNode->inputs, 0);
1216         bNodeSocket *sockMultiplyB = BLI_findlink(&multiplyNode->inputs, 1);
1217 
1218         nodeAddLink(ntree, node, sockDistance, multiplyNode, sockMultiplyA);
1219         nodeAddLink(ntree, node, sockDistance, multiplyNode, sockMultiplyB);
1220 
1221         need_update = true;
1222       }
1223     }
1224   }
1225 
1226   if (need_update) {
1227     ntreeUpdateTree(NULL, ntree);
1228   }
1229 }
1230 
1231 /* Noise and Wave Texture nodes: Restore previous Distortion range.
1232  * In 2.81 we used noise() for distortion, now we use snoise() which has twice the range.
1233  * To fix this we halve distortion value, directly or by adding multiply node for used sockets.
1234  */
update_noise_and_wave_distortion(bNodeTree * ntree)1235 static void update_noise_and_wave_distortion(bNodeTree *ntree)
1236 {
1237   bool need_update = false;
1238 
1239   LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
1240     if (node->type == SH_NODE_TEX_NOISE || node->type == SH_NODE_TEX_WAVE) {
1241 
1242       bNodeSocket *sockDistortion = nodeFindSocket(node, SOCK_IN, "Distortion");
1243       float *distortion = cycles_node_socket_float_value(sockDistortion);
1244 
1245       if (socket_is_used(sockDistortion) && sockDistortion->link != NULL) {
1246         bNode *distortionInputNode = sockDistortion->link->fromnode;
1247         bNodeSocket *distortionInputSock = sockDistortion->link->fromsock;
1248 
1249         bNode *mulNode = nodeAddStaticNode(NULL, ntree, SH_NODE_MATH);
1250         mulNode->custom1 = NODE_MATH_MULTIPLY;
1251         mulNode->locx = node->locx;
1252         mulNode->locy = node->locy - 240.0f;
1253         mulNode->flag |= NODE_HIDDEN;
1254         bNodeSocket *mulSockA = BLI_findlink(&mulNode->inputs, 0);
1255         bNodeSocket *mulSockB = BLI_findlink(&mulNode->inputs, 1);
1256         *cycles_node_socket_float_value(mulSockB) = 0.5f;
1257         bNodeSocket *mulSockOut = nodeFindSocket(mulNode, SOCK_OUT, "Value");
1258 
1259         nodeRemLink(ntree, sockDistortion->link);
1260         nodeAddLink(ntree, distortionInputNode, distortionInputSock, mulNode, mulSockA);
1261         nodeAddLink(ntree, mulNode, mulSockOut, node, sockDistortion);
1262 
1263         need_update = true;
1264       }
1265       else if (*distortion != 0.0f) {
1266         *distortion = *distortion * 0.5f;
1267       }
1268     }
1269   }
1270 
1271   if (need_update) {
1272     ntreeUpdateTree(NULL, ntree);
1273   }
1274 }
1275 
1276 /**
1277  * Wave Texture node: Restore previous texture directions and offset.
1278  * 1. In 2.81, Wave texture had fixed diagonal direction (Bands) or
1279  *    mapping along distance (Rings). Now, directions are customizable
1280  *    properties, with X axis being new default. To fix this we set new
1281  *    direction options to Diagonal and Spherical.
1282  * 2. Sine profile is now negatively offset by PI/2 to better match
1283  *    other profiles. To fix this we set new Phase Offset input to PI/2
1284  *    in nodes with Sine profile.
1285  */
update_wave_node_directions_and_offset(bNodeTree * ntree)1286 static void update_wave_node_directions_and_offset(bNodeTree *ntree)
1287 {
1288   LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
1289     if (node->type == SH_NODE_TEX_WAVE) {
1290       NodeTexWave *tex = (NodeTexWave *)node->storage;
1291       tex->bands_direction = SHD_WAVE_BANDS_DIRECTION_DIAGONAL;
1292       tex->rings_direction = SHD_WAVE_RINGS_DIRECTION_SPHERICAL;
1293 
1294       if (tex->wave_profile == SHD_WAVE_PROFILE_SIN) {
1295         bNodeSocket *sockPhaseOffset = nodeFindSocket(node, SOCK_IN, "Phase Offset");
1296         *cycles_node_socket_float_value(sockPhaseOffset) = M_PI_2;
1297       }
1298     }
1299   }
1300 }
1301 
blo_do_versions_cycles(FileData * UNUSED (fd),Library * UNUSED (lib),Main * bmain)1302 void blo_do_versions_cycles(FileData *UNUSED(fd), Library *UNUSED(lib), Main *bmain)
1303 {
1304   /* Particle shape shared with Eevee. */
1305   if (!MAIN_VERSION_ATLEAST(bmain, 280, 16)) {
1306     for (ParticleSettings *part = bmain->particles.first; part; part = part->id.next) {
1307       IDProperty *cpart = cycles_properties_from_ID(&part->id);
1308 
1309       if (cpart) {
1310         part->shape = cycles_property_float(cpart, "shape", 0.0);
1311         part->rad_root = cycles_property_float(cpart, "root_width", 1.0);
1312         part->rad_tip = cycles_property_float(cpart, "tip_width", 0.0);
1313         part->rad_scale = cycles_property_float(cpart, "radius_scale", 0.01);
1314         if (cycles_property_boolean(cpart, "use_closetip", true)) {
1315           part->shape_flag |= PART_SHAPE_CLOSE_TIP;
1316         }
1317       }
1318     }
1319   }
1320 
1321   if (!MAIN_VERSION_ATLEAST(bmain, 280, 68)) {
1322     /* Unify Cycles and Eevee film transparency. */
1323     for (Scene *scene = bmain->scenes.first; scene; scene = scene->id.next) {
1324       if (STREQ(scene->r.engine, RE_engine_id_CYCLES)) {
1325         IDProperty *cscene = cycles_properties_from_ID(&scene->id);
1326         if (cscene) {
1327           bool cycles_film_transparency = cycles_property_boolean(
1328               cscene, "film_transparent", false);
1329           scene->r.alphamode = cycles_film_transparency ? R_ALPHAPREMUL : R_ADDSKY;
1330         }
1331       }
1332     }
1333   }
1334 
1335   if (!MAIN_VERSION_ATLEAST(bmain, 281, 3)) {
1336     FOREACH_NODETREE_BEGIN (bmain, ntree, id) {
1337       if (ntree->type == NTREE_SHADER) {
1338         update_vector_math_node_operators_enum_mapping(ntree);
1339       }
1340     }
1341     FOREACH_NODETREE_END;
1342   }
1343 
1344   if (!MAIN_VERSION_ATLEAST(bmain, 281, 10)) {
1345     FOREACH_NODETREE_BEGIN (bmain, ntree, id) {
1346       if (ntree->type == NTREE_SHADER) {
1347         update_musgrave_node_color_output(ntree);
1348       }
1349     }
1350     FOREACH_NODETREE_END;
1351   }
1352 
1353   if (!MAIN_VERSION_ATLEAST(bmain, 281, 11)) {
1354     FOREACH_NODETREE_BEGIN (bmain, ntree, id) {
1355       if (ntree->type == NTREE_SHADER) {
1356         update_voronoi_node_f3_and_f4(ntree);
1357         update_voronoi_node_fac_output(ntree);
1358       }
1359     }
1360     FOREACH_NODETREE_END;
1361   }
1362 }
1363 
do_versions_after_linking_cycles(Main * bmain)1364 void do_versions_after_linking_cycles(Main *bmain)
1365 {
1366   if (!MAIN_VERSION_ATLEAST(bmain, 280, 66)) {
1367     /* Shader node tree changes. After lib linking so we have all the typeinfo
1368      * pointers and updated sockets and we can use the high level node API to
1369      * manipulate nodes. */
1370     FOREACH_NODETREE_BEGIN (bmain, ntree, id) {
1371       if (ntree->type != NTREE_SHADER) {
1372         continue;
1373       }
1374 
1375       if (!MAIN_VERSION_ATLEAST(bmain, 273, 5)) {
1376         /* Euler order was ZYX in previous versions. */
1377         LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
1378           mapping_node_order_flip(node);
1379         }
1380       }
1381 
1382       if (!MAIN_VERSION_ATLEAST(bmain, 276, 6)) {
1383         LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
1384           vector_curve_node_remap(node);
1385         }
1386       }
1387 
1388       if (!MAIN_VERSION_ATLEAST(bmain, 279, 2) ||
1389           (MAIN_VERSION_ATLEAST(bmain, 280, 0) && !MAIN_VERSION_ATLEAST(bmain, 280, 4))) {
1390         displacement_node_insert(ntree);
1391       }
1392 
1393       if (!MAIN_VERSION_ATLEAST(bmain, 279, 3)) {
1394         LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
1395           displacement_principled_nodes(node);
1396         }
1397       }
1398 
1399       if (!MAIN_VERSION_ATLEAST(bmain, 279, 4) ||
1400           (MAIN_VERSION_ATLEAST(bmain, 280, 0) && !MAIN_VERSION_ATLEAST(bmain, 280, 5))) {
1401         /* Switch to squared roughness convention */
1402         square_roughness_node_insert(ntree);
1403       }
1404 
1405       if (!MAIN_VERSION_ATLEAST(bmain, 279, 5)) {
1406         ambient_occlusion_node_relink(ntree);
1407       }
1408 
1409       if (!MAIN_VERSION_ATLEAST(bmain, 280, 66)) {
1410         LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
1411           image_node_colorspace(node);
1412         }
1413       }
1414     }
1415     FOREACH_NODETREE_END;
1416   }
1417 
1418   if (!MAIN_VERSION_ATLEAST(bmain, 280, 64)) {
1419     /* Unfiy Cycles and Eevee settings. */
1420     Scene *scene = bmain->scenes.first;
1421     const char *engine = (scene) ? scene->r.engine : "CYCLES";
1422 
1423     for (Light *light = bmain->lights.first; light; light = light->id.next) {
1424       light_emission_unify(light, engine);
1425     }
1426   }
1427 
1428   if (!MAIN_VERSION_ATLEAST(bmain, 280, 69)) {
1429     /* Unify Cycles and Eevee depth of field. */
1430     Scene *scene = bmain->scenes.first;
1431     const char *engine = (scene) ? scene->r.engine : "CYCLES";
1432 
1433     if (STREQ(engine, RE_engine_id_CYCLES)) {
1434       for (Camera *camera = bmain->cameras.first; camera; camera = camera->id.next) {
1435         IDProperty *ccamera = cycles_properties_from_ID(&camera->id);
1436         if (ccamera) {
1437           const bool is_fstop = cycles_property_int(ccamera, "aperture_type", 0) == 1;
1438 
1439           camera->dof.aperture_fstop = cycles_property_float(ccamera, "aperture_fstop", 5.6f);
1440           camera->dof.aperture_blades = cycles_property_int(ccamera, "aperture_blades", 0);
1441           camera->dof.aperture_rotation = cycles_property_float(ccamera, "aperture_rotation", 0.0);
1442           camera->dof.aperture_ratio = cycles_property_float(ccamera, "aperture_ratio", 1.0f);
1443           camera->dof.flag |= CAM_DOF_ENABLED;
1444 
1445           float aperture_size = cycles_property_float(ccamera, "aperture_size", 0.0f);
1446 
1447           if (is_fstop) {
1448             continue;
1449           }
1450           if (aperture_size > 0.0f) {
1451             if (camera->type == CAM_ORTHO) {
1452               camera->dof.aperture_fstop = 1.0f / (2.0f * aperture_size);
1453             }
1454             else {
1455               camera->dof.aperture_fstop = (camera->lens * 1e-3f) / (2.0f * aperture_size);
1456             }
1457 
1458             continue;
1459           }
1460         }
1461 
1462         /* No depth of field, set default settings. */
1463         camera->dof.aperture_fstop = 2.8f;
1464         camera->dof.aperture_blades = 0;
1465         camera->dof.aperture_rotation = 0.0f;
1466         camera->dof.aperture_ratio = 1.0f;
1467         camera->dof.flag &= ~CAM_DOF_ENABLED;
1468       }
1469     }
1470   }
1471 
1472   if (!MAIN_VERSION_ATLEAST(bmain, 281, 2)) {
1473     FOREACH_NODETREE_BEGIN (bmain, ntree, id) {
1474       if (ntree->type == NTREE_SHADER) {
1475         update_math_node_single_operand_operators(ntree);
1476       }
1477     }
1478     FOREACH_NODETREE_END;
1479   }
1480 
1481   if (!MAIN_VERSION_ATLEAST(bmain, 281, 3)) {
1482     FOREACH_NODETREE_BEGIN (bmain, ntree, id) {
1483       if (ntree->type == NTREE_SHADER) {
1484         update_vector_math_node_add_and_subtract_operators(ntree);
1485         update_vector_math_node_dot_product_operator(ntree);
1486         update_vector_math_node_cross_product_operator(ntree);
1487         update_vector_math_node_normalize_operator(ntree);
1488         update_vector_math_node_average_operator(ntree);
1489       }
1490     }
1491     FOREACH_NODETREE_END;
1492   }
1493 
1494   if (!MAIN_VERSION_ATLEAST(bmain, 281, 7)) {
1495     FOREACH_NODETREE_BEGIN (bmain, ntree, id) {
1496       if (ntree->type == NTREE_SHADER) {
1497         update_noise_node_dimensions(ntree);
1498       }
1499     }
1500     FOREACH_NODETREE_END;
1501   }
1502 
1503   if (!MAIN_VERSION_ATLEAST(bmain, 281, 8)) {
1504     FOREACH_NODETREE_BEGIN (bmain, ntree, id) {
1505       if (ntree->type == NTREE_SHADER) {
1506         update_mapping_node_inputs_and_properties(ntree);
1507       }
1508     }
1509     FOREACH_NODETREE_END;
1510   }
1511 
1512   if (!MAIN_VERSION_ATLEAST(bmain, 281, 10)) {
1513     FOREACH_NODETREE_BEGIN (bmain, ntree, id) {
1514       if (ntree->type == NTREE_SHADER) {
1515         update_musgrave_node_dimensions(ntree);
1516       }
1517     }
1518     FOREACH_NODETREE_END;
1519   }
1520 
1521   if (!MAIN_VERSION_ATLEAST(bmain, 281, 11)) {
1522     FOREACH_NODETREE_BEGIN (bmain, ntree, id) {
1523       if (ntree->type == NTREE_SHADER) {
1524         update_voronoi_node_dimensions(ntree);
1525         update_voronoi_node_crackle(ntree);
1526         update_voronoi_node_coloring(ntree);
1527         update_voronoi_node_square_distance(ntree);
1528       }
1529     }
1530     FOREACH_NODETREE_END;
1531   }
1532 
1533   if (!MAIN_VERSION_ATLEAST(bmain, 282, 4)) {
1534     FOREACH_NODETREE_BEGIN (bmain, ntree, id) {
1535       if (ntree->type == NTREE_SHADER) {
1536         update_noise_and_wave_distortion(ntree);
1537       }
1538     }
1539     FOREACH_NODETREE_END;
1540   }
1541 
1542   if (!MAIN_VERSION_ATLEAST(bmain, 283, 4)) {
1543     FOREACH_NODETREE_BEGIN (bmain, ntree, id) {
1544       if (ntree->type == NTREE_SHADER) {
1545         update_wave_node_directions_and_offset(ntree);
1546       }
1547     }
1548     FOREACH_NODETREE_END;
1549   }
1550 
1551   if (!MAIN_VERSION_ATLEAST(bmain, 290, 5)) {
1552     /* New denoiser settings. */
1553     for (Scene *scene = bmain->scenes.first; scene; scene = scene->id.next) {
1554       IDProperty *cscene = cycles_properties_from_ID(&scene->id);
1555 
1556       /* Check if any view layers had (optix) denoising enabled. */
1557       bool use_optix = false;
1558       bool use_denoising = false;
1559       for (ViewLayer *view_layer = scene->view_layers.first; view_layer;
1560            view_layer = view_layer->next) {
1561         IDProperty *cview_layer = cycles_properties_from_view_layer(view_layer);
1562         if (cview_layer) {
1563           use_denoising = use_denoising ||
1564                           cycles_property_boolean(cview_layer, "use_denoising", false);
1565           use_optix = use_optix ||
1566                       cycles_property_boolean(cview_layer, "use_optix_denoising", false);
1567         }
1568       }
1569 
1570       if (cscene) {
1571         const int DENOISER_AUTO = 0;
1572         const int DENOISER_NLM = 1;
1573         const int DENOISER_OPTIX = 2;
1574 
1575         /* Enable denoiser if it was enabled for one view layer before. */
1576         cycles_property_int_set(cscene, "denoiser", (use_optix) ? DENOISER_OPTIX : DENOISER_NLM);
1577         cycles_property_boolean_set(cscene, "use_denoising", use_denoising);
1578 
1579         /* Migrate Optix denoiser to new settings. */
1580         if (cycles_property_int(cscene, "preview_denoising", 0)) {
1581           cycles_property_boolean_set(cscene, "use_preview_denoising", true);
1582           cycles_property_int_set(cscene, "preview_denoiser", DENOISER_AUTO);
1583         }
1584       }
1585 
1586       /* Enable denoising in all view layer if there was no denoising before,
1587        * so that enabling the scene settings auto enables it for all view layers. */
1588       if (!use_denoising) {
1589         for (ViewLayer *view_layer = scene->view_layers.first; view_layer;
1590              view_layer = view_layer->next) {
1591           IDProperty *cview_layer = cycles_properties_from_view_layer(view_layer);
1592           if (cview_layer) {
1593             cycles_property_boolean_set(cview_layer, "use_denoising", true);
1594           }
1595         }
1596       }
1597     }
1598   }
1599 }
1600