Blender  V3.3
blender/shader.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #include "scene/shader.h"
5 #include "scene/background.h"
6 #include "scene/colorspace.h"
7 #include "scene/integrator.h"
8 #include "scene/light.h"
9 #include "scene/osl.h"
10 #include "scene/scene.h"
11 #include "scene/shader_graph.h"
12 #include "scene/shader_nodes.h"
13 
14 #include "blender/image.h"
15 #include "blender/sync.h"
16 #include "blender/texture.h"
17 #include "blender/util.h"
18 
19 #include "util/debug.h"
20 #include "util/foreach.h"
21 #include "util/set.h"
22 #include "util/string.h"
23 #include "util/task.h"
24 
26 
27 typedef map<void *, ShaderInput *> PtrInputMap;
28 typedef map<void *, ShaderOutput *> PtrOutputMap;
29 typedef map<string, ConvertNode *> ProxyMap;
30 
31 /* Find */
32 
33 void BlenderSync::find_shader(BL::ID &id, array<Node *> &used_shaders, Shader *default_shader)
34 {
35  Shader *synced_shader = (id) ? shader_map.find(id) : nullptr;
36  Shader *shader = (synced_shader) ? synced_shader : default_shader;
37 
38  used_shaders.push_back_slow(shader);
39  shader->tag_used(scene);
40 }
41 
42 /* RNA translation utilities */
43 
45 {
46  return (VolumeSampling)get_enum(
47  ptr, "volume_sampling", VOLUME_NUM_SAMPLING, VOLUME_SAMPLING_DISTANCE);
48 }
49 
51 {
53  ptr, "volume_interpolation", VOLUME_NUM_INTERPOLATION, VOLUME_INTERPOLATION_LINEAR);
54 }
55 
57 {
59  ptr, "displacement_method", DISPLACE_NUM_METHODS, DISPLACE_BUMP);
60 }
61 
62 static int validate_enum_value(int value, int num_values, int default_value)
63 {
64  if (value >= num_values) {
65  return default_value;
66  }
67  return value;
68 }
69 
70 template<typename NodeType> static InterpolationType get_image_interpolation(NodeType &b_node)
71 {
72  int value = b_node.interpolation();
75 }
76 
77 template<typename NodeType> static ExtensionType get_image_extension(NodeType &b_node)
78 {
79  int value = b_node.extension();
81 }
82 
84 {
85  int value = b_image.alpha_mode();
87 }
88 
89 /* Attribute name translation utilities */
90 
91 /* Since Eevee needs to know whether the attribute is uniform or varying
92  * at the time it compiles the shader for the material, Blender had to
93  * introduce different namespaces (types) in its attribute node. However,
94  * Cycles already has object attributes that form a uniform namespace with
95  * the more common varying attributes. Without completely reworking the
96  * attribute handling in Cycles to introduce separate namespaces (this could
97  * be especially hard for OSL which directly uses the name string), the
98  * space identifier has to be added to the attribute name as a prefix.
99  *
100  * The prefixes include a control character to ensure the user specified
101  * name can't accidentally include a special prefix.
102  */
103 
104 static const string_view object_attr_prefix("\x01object:");
105 static const string_view instancer_attr_prefix("\x01instancer:");
106 
107 static ustring blender_attribute_name_add_type(const string &name, BlenderAttributeType type)
108 {
109  switch (type) {
110  case BL::ShaderNodeAttribute::attribute_type_OBJECT:
111  return ustring::concat(object_attr_prefix, name);
112  case BL::ShaderNodeAttribute::attribute_type_INSTANCER:
113  return ustring::concat(instancer_attr_prefix, name);
114  default:
115  return ustring(name);
116  }
117 }
118 
119 BlenderAttributeType blender_attribute_name_split_type(ustring name, string *r_real_name)
120 {
121  string_view sname(name);
122 
123  if (sname.substr(0, object_attr_prefix.size()) == object_attr_prefix) {
124  *r_real_name = sname.substr(object_attr_prefix.size());
125  return BL::ShaderNodeAttribute::attribute_type_OBJECT;
126  }
127 
128  if (sname.substr(0, instancer_attr_prefix.size()) == instancer_attr_prefix) {
129  *r_real_name = sname.substr(instancer_attr_prefix.size());
130  return BL::ShaderNodeAttribute::attribute_type_INSTANCER;
131  }
132 
133  return BL::ShaderNodeAttribute::attribute_type_GEOMETRY;
134 }
135 
136 /* Graph */
137 
138 static BL::NodeSocket get_node_output(BL::Node &b_node, const string &name)
139 {
140  for (BL::NodeSocket &b_out : b_node.outputs) {
141  if (b_out.identifier() == name) {
142  return b_out;
143  }
144  }
145  assert(0);
146  return *b_node.outputs.begin();
147 }
148 
149 static float3 get_node_output_rgba(BL::Node &b_node, const string &name)
150 {
151  BL::NodeSocket b_sock = get_node_output(b_node, name);
152  float value[4];
153  RNA_float_get_array(&b_sock.ptr, "default_value", value);
154  return make_float3(value[0], value[1], value[2]);
155 }
156 
157 static float get_node_output_value(BL::Node &b_node, const string &name)
158 {
159  BL::NodeSocket b_sock = get_node_output(b_node, name);
160  return RNA_float_get(&b_sock.ptr, "default_value");
161 }
162 
163 static float3 get_node_output_vector(BL::Node &b_node, const string &name)
164 {
165  BL::NodeSocket b_sock = get_node_output(b_node, name);
166  float value[3];
167  RNA_float_get_array(&b_sock.ptr, "default_value", value);
168  return make_float3(value[0], value[1], value[2]);
169 }
170 
171 static SocketType::Type convert_socket_type(BL::NodeSocket &b_socket)
172 {
173  switch (b_socket.type()) {
174  case BL::NodeSocket::type_VALUE:
175  return SocketType::FLOAT;
176  case BL::NodeSocket::type_INT:
177  return SocketType::INT;
178  case BL::NodeSocket::type_VECTOR:
179  return SocketType::VECTOR;
180  case BL::NodeSocket::type_RGBA:
181  return SocketType::COLOR;
182  case BL::NodeSocket::type_STRING:
183  return SocketType::STRING;
184  case BL::NodeSocket::type_SHADER:
185  return SocketType::CLOSURE;
186 
187  default:
188  return SocketType::UNDEFINED;
189  }
190 }
191 
193  BL::NodeSocket &b_sock,
194  BL::BlendData &b_data,
195  BL::ID &b_id)
196 {
197  Node *node = input->parent;
198  const SocketType &socket = input->socket_type;
199 
200  /* copy values for non linked inputs */
201  switch (input->type()) {
202  case SocketType::FLOAT: {
203  node->set(socket, get_float(b_sock.ptr, "default_value"));
204  break;
205  }
206  case SocketType::INT: {
207  if (b_sock.type() == BL::NodeSocket::type_BOOLEAN) {
208  /* Make sure to call the int overload of set() since this is an integer socket as far as
209  * Cycles is concerned. */
210  node->set(socket, get_boolean(b_sock.ptr, "default_value") ? 1 : 0);
211  }
212  else {
213  node->set(socket, get_int(b_sock.ptr, "default_value"));
214  }
215  break;
216  }
217  case SocketType::COLOR: {
218  node->set(socket, float4_to_float3(get_float4(b_sock.ptr, "default_value")));
219  break;
220  }
221  case SocketType::NORMAL:
222  case SocketType::POINT:
223  case SocketType::VECTOR: {
224  node->set(socket, get_float3(b_sock.ptr, "default_value"));
225  break;
226  }
227  case SocketType::STRING: {
228  node->set(
229  socket,
230  (ustring)blender_absolute_path(b_data, b_id, get_string(b_sock.ptr, "default_value")));
231  break;
232  }
233  default:
234  break;
235  }
236 }
237 
238 static void get_tex_mapping(TextureNode *mapping, BL::TexMapping &b_mapping)
239 {
240  if (!b_mapping)
241  return;
242 
243  mapping->set_tex_mapping_translation(get_float3(b_mapping.translation()));
244  mapping->set_tex_mapping_rotation(get_float3(b_mapping.rotation()));
245  mapping->set_tex_mapping_scale(get_float3(b_mapping.scale()));
246  mapping->set_tex_mapping_type((TextureMapping::Type)b_mapping.vector_type());
247 
248  mapping->set_tex_mapping_x_mapping((TextureMapping::Mapping)b_mapping.mapping_x());
249  mapping->set_tex_mapping_y_mapping((TextureMapping::Mapping)b_mapping.mapping_y());
250  mapping->set_tex_mapping_z_mapping((TextureMapping::Mapping)b_mapping.mapping_z());
251 }
252 
253 static bool is_image_animated(BL::Image::source_enum b_image_source, BL::ImageUser &b_image_user)
254 {
255  return (b_image_source == BL::Image::source_MOVIE ||
256  b_image_source == BL::Image::source_SEQUENCE) &&
257  b_image_user.use_auto_refresh();
258 }
259 
261  BL::RenderEngine &b_engine,
262  BL::BlendData &b_data,
263  BL::Depsgraph &b_depsgraph,
264  BL::Scene &b_scene,
266  BL::ShaderNodeTree &b_ntree,
267  BL::ShaderNode &b_node)
268 {
269  ShaderNode *node = NULL;
270 
271  /* existing blender nodes */
272  if (b_node.is_a(&RNA_ShaderNodeRGBCurve)) {
273  BL::ShaderNodeRGBCurve b_curve_node(b_node);
274  BL::CurveMapping mapping(b_curve_node.mapping());
275  RGBCurvesNode *curves = graph->create_node<RGBCurvesNode>();
276  array<float3> curve_mapping_curves;
277  float min_x, max_x;
278  curvemapping_color_to_array(mapping, curve_mapping_curves, RAMP_TABLE_SIZE, true);
279  curvemapping_minmax(mapping, 4, &min_x, &max_x);
280  curves->set_min_x(min_x);
281  curves->set_max_x(max_x);
282  curves->set_curves(curve_mapping_curves);
283  curves->set_extrapolate(mapping.extend() == mapping.extend_EXTRAPOLATED);
284  node = curves;
285  }
286  if (b_node.is_a(&RNA_ShaderNodeVectorCurve)) {
287  BL::ShaderNodeVectorCurve b_curve_node(b_node);
288  BL::CurveMapping mapping(b_curve_node.mapping());
289  VectorCurvesNode *curves = graph->create_node<VectorCurvesNode>();
290  array<float3> curve_mapping_curves;
291  float min_x, max_x;
292  curvemapping_color_to_array(mapping, curve_mapping_curves, RAMP_TABLE_SIZE, false);
293  curvemapping_minmax(mapping, 3, &min_x, &max_x);
294  curves->set_min_x(min_x);
295  curves->set_max_x(max_x);
296  curves->set_curves(curve_mapping_curves);
297  curves->set_extrapolate(mapping.extend() == mapping.extend_EXTRAPOLATED);
298  node = curves;
299  }
300  else if (b_node.is_a(&RNA_ShaderNodeFloatCurve)) {
301  BL::ShaderNodeFloatCurve b_curve_node(b_node);
302  BL::CurveMapping mapping(b_curve_node.mapping());
303  FloatCurveNode *curve = graph->create_node<FloatCurveNode>();
304  array<float> curve_mapping_curve;
305  float min_x, max_x;
306  curvemapping_float_to_array(mapping, curve_mapping_curve, RAMP_TABLE_SIZE);
307  curvemapping_minmax(mapping, 1, &min_x, &max_x);
308  curve->set_min_x(min_x);
309  curve->set_max_x(max_x);
310  curve->set_curve(curve_mapping_curve);
311  curve->set_extrapolate(mapping.extend() == mapping.extend_EXTRAPOLATED);
312  node = curve;
313  }
314  else if (b_node.is_a(&RNA_ShaderNodeValToRGB)) {
315  RGBRampNode *ramp = graph->create_node<RGBRampNode>();
316  BL::ShaderNodeValToRGB b_ramp_node(b_node);
317  BL::ColorRamp b_color_ramp(b_ramp_node.color_ramp());
318  array<float3> ramp_values;
319  array<float> ramp_alpha;
320  colorramp_to_array(b_color_ramp, ramp_values, ramp_alpha, RAMP_TABLE_SIZE);
321  ramp->set_ramp(ramp_values);
322  ramp->set_ramp_alpha(ramp_alpha);
323  ramp->set_interpolate(b_color_ramp.interpolation() != BL::ColorRamp::interpolation_CONSTANT);
324  node = ramp;
325  }
326  else if (b_node.is_a(&RNA_ShaderNodeRGB)) {
327  ColorNode *color = graph->create_node<ColorNode>();
328  color->set_value(get_node_output_rgba(b_node, "Color"));
329  node = color;
330  }
331  else if (b_node.is_a(&RNA_ShaderNodeValue)) {
332  ValueNode *value = graph->create_node<ValueNode>();
333  value->set_value(get_node_output_value(b_node, "Value"));
334  node = value;
335  }
336  else if (b_node.is_a(&RNA_ShaderNodeCameraData)) {
337  node = graph->create_node<CameraNode>();
338  }
339  else if (b_node.is_a(&RNA_ShaderNodeInvert)) {
340  node = graph->create_node<InvertNode>();
341  }
342  else if (b_node.is_a(&RNA_ShaderNodeGamma)) {
343  node = graph->create_node<GammaNode>();
344  }
345  else if (b_node.is_a(&RNA_ShaderNodeBrightContrast)) {
346  node = graph->create_node<BrightContrastNode>();
347  }
348  else if (b_node.is_a(&RNA_ShaderNodeMixRGB)) {
349  BL::ShaderNodeMixRGB b_mix_node(b_node);
350  MixNode *mix = graph->create_node<MixNode>();
351  mix->set_mix_type((NodeMix)b_mix_node.blend_type());
352  mix->set_use_clamp(b_mix_node.use_clamp());
353  node = mix;
354  }
355  else if (b_node.is_a(&RNA_ShaderNodeSeparateRGB)) {
356  node = graph->create_node<SeparateRGBNode>();
357  }
358  else if (b_node.is_a(&RNA_ShaderNodeCombineRGB)) {
359  node = graph->create_node<CombineRGBNode>();
360  }
361  else if (b_node.is_a(&RNA_ShaderNodeSeparateHSV)) {
362  node = graph->create_node<SeparateHSVNode>();
363  }
364  else if (b_node.is_a(&RNA_ShaderNodeCombineHSV)) {
365  node = graph->create_node<CombineHSVNode>();
366  }
367  else if (b_node.is_a(&RNA_ShaderNodeSeparateColor)) {
368  BL::ShaderNodeSeparateColor b_separate_node(b_node);
369  SeparateColorNode *separate_node = graph->create_node<SeparateColorNode>();
370  separate_node->set_color_type((NodeCombSepColorType)b_separate_node.mode());
371  node = separate_node;
372  }
373  else if (b_node.is_a(&RNA_ShaderNodeCombineColor)) {
374  BL::ShaderNodeCombineColor b_combine_node(b_node);
375  CombineColorNode *combine_node = graph->create_node<CombineColorNode>();
376  combine_node->set_color_type((NodeCombSepColorType)b_combine_node.mode());
377  node = combine_node;
378  }
379  else if (b_node.is_a(&RNA_ShaderNodeSeparateXYZ)) {
380  node = graph->create_node<SeparateXYZNode>();
381  }
382  else if (b_node.is_a(&RNA_ShaderNodeCombineXYZ)) {
383  node = graph->create_node<CombineXYZNode>();
384  }
385  else if (b_node.is_a(&RNA_ShaderNodeHueSaturation)) {
386  node = graph->create_node<HSVNode>();
387  }
388  else if (b_node.is_a(&RNA_ShaderNodeRGBToBW)) {
389  node = graph->create_node<RGBToBWNode>();
390  }
391  else if (b_node.is_a(&RNA_ShaderNodeMapRange)) {
392  BL::ShaderNodeMapRange b_map_range_node(b_node);
393  if (b_map_range_node.data_type() == BL::ShaderNodeMapRange::data_type_FLOAT_VECTOR) {
394  VectorMapRangeNode *vector_map_range_node = graph->create_node<VectorMapRangeNode>();
395  vector_map_range_node->set_use_clamp(b_map_range_node.clamp());
396  vector_map_range_node->set_range_type(
397  (NodeMapRangeType)b_map_range_node.interpolation_type());
398  node = vector_map_range_node;
399  }
400  else {
401  MapRangeNode *map_range_node = graph->create_node<MapRangeNode>();
402  map_range_node->set_clamp(b_map_range_node.clamp());
403  map_range_node->set_range_type((NodeMapRangeType)b_map_range_node.interpolation_type());
404  node = map_range_node;
405  }
406  }
407  else if (b_node.is_a(&RNA_ShaderNodeClamp)) {
408  BL::ShaderNodeClamp b_clamp_node(b_node);
409  ClampNode *clamp_node = graph->create_node<ClampNode>();
410  clamp_node->set_clamp_type((NodeClampType)b_clamp_node.clamp_type());
411  node = clamp_node;
412  }
413  else if (b_node.is_a(&RNA_ShaderNodeMath)) {
414  BL::ShaderNodeMath b_math_node(b_node);
415  MathNode *math_node = graph->create_node<MathNode>();
416  math_node->set_math_type((NodeMathType)b_math_node.operation());
417  math_node->set_use_clamp(b_math_node.use_clamp());
418  node = math_node;
419  }
420  else if (b_node.is_a(&RNA_ShaderNodeVectorMath)) {
421  BL::ShaderNodeVectorMath b_vector_math_node(b_node);
422  VectorMathNode *vector_math_node = graph->create_node<VectorMathNode>();
423  vector_math_node->set_math_type((NodeVectorMathType)b_vector_math_node.operation());
424  node = vector_math_node;
425  }
426  else if (b_node.is_a(&RNA_ShaderNodeVectorRotate)) {
427  BL::ShaderNodeVectorRotate b_vector_rotate_node(b_node);
428  VectorRotateNode *vector_rotate_node = graph->create_node<VectorRotateNode>();
429  vector_rotate_node->set_rotate_type(
430  (NodeVectorRotateType)b_vector_rotate_node.rotation_type());
431  vector_rotate_node->set_invert(b_vector_rotate_node.invert());
432  node = vector_rotate_node;
433  }
434  else if (b_node.is_a(&RNA_ShaderNodeVectorTransform)) {
435  BL::ShaderNodeVectorTransform b_vector_transform_node(b_node);
436  VectorTransformNode *vtransform = graph->create_node<VectorTransformNode>();
437  vtransform->set_transform_type((NodeVectorTransformType)b_vector_transform_node.vector_type());
438  vtransform->set_convert_from(
439  (NodeVectorTransformConvertSpace)b_vector_transform_node.convert_from());
440  vtransform->set_convert_to(
441  (NodeVectorTransformConvertSpace)b_vector_transform_node.convert_to());
442  node = vtransform;
443  }
444  else if (b_node.is_a(&RNA_ShaderNodeNormal)) {
445  BL::Node::outputs_iterator out_it;
446  b_node.outputs.begin(out_it);
447 
448  NormalNode *norm = graph->create_node<NormalNode>();
449  norm->set_direction(get_node_output_vector(b_node, "Normal"));
450  node = norm;
451  }
452  else if (b_node.is_a(&RNA_ShaderNodeMapping)) {
453  BL::ShaderNodeMapping b_mapping_node(b_node);
454  MappingNode *mapping = graph->create_node<MappingNode>();
455  mapping->set_mapping_type((NodeMappingType)b_mapping_node.vector_type());
456  node = mapping;
457  }
458  else if (b_node.is_a(&RNA_ShaderNodeFresnel)) {
459  node = graph->create_node<FresnelNode>();
460  }
461  else if (b_node.is_a(&RNA_ShaderNodeLayerWeight)) {
462  node = graph->create_node<LayerWeightNode>();
463  }
464  else if (b_node.is_a(&RNA_ShaderNodeAddShader)) {
465  node = graph->create_node<AddClosureNode>();
466  }
467  else if (b_node.is_a(&RNA_ShaderNodeMixShader)) {
468  node = graph->create_node<MixClosureNode>();
469  }
470  else if (b_node.is_a(&RNA_ShaderNodeAttribute)) {
471  BL::ShaderNodeAttribute b_attr_node(b_node);
472  AttributeNode *attr = graph->create_node<AttributeNode>();
473  attr->set_attribute(blender_attribute_name_add_type(b_attr_node.attribute_name(),
474  b_attr_node.attribute_type()));
475  node = attr;
476  }
477  else if (b_node.is_a(&RNA_ShaderNodeBackground)) {
478  node = graph->create_node<BackgroundNode>();
479  }
480  else if (b_node.is_a(&RNA_ShaderNodeHoldout)) {
481  node = graph->create_node<HoldoutNode>();
482  }
483  else if (b_node.is_a(&RNA_ShaderNodeBsdfAnisotropic)) {
484  BL::ShaderNodeBsdfAnisotropic b_aniso_node(b_node);
485  AnisotropicBsdfNode *aniso = graph->create_node<AnisotropicBsdfNode>();
486 
487  switch (b_aniso_node.distribution()) {
488  case BL::ShaderNodeBsdfAnisotropic::distribution_BECKMANN:
489  aniso->set_distribution(CLOSURE_BSDF_MICROFACET_BECKMANN_ID);
490  break;
491  case BL::ShaderNodeBsdfAnisotropic::distribution_GGX:
492  aniso->set_distribution(CLOSURE_BSDF_MICROFACET_GGX_ID);
493  break;
494  case BL::ShaderNodeBsdfAnisotropic::distribution_MULTI_GGX:
495  aniso->set_distribution(CLOSURE_BSDF_MICROFACET_MULTI_GGX_ID);
496  break;
497  case BL::ShaderNodeBsdfAnisotropic::distribution_ASHIKHMIN_SHIRLEY:
498  aniso->set_distribution(CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ID);
499  break;
500  }
501 
502  node = aniso;
503  }
504  else if (b_node.is_a(&RNA_ShaderNodeBsdfDiffuse)) {
505  node = graph->create_node<DiffuseBsdfNode>();
506  }
507  else if (b_node.is_a(&RNA_ShaderNodeSubsurfaceScattering)) {
508  BL::ShaderNodeSubsurfaceScattering b_subsurface_node(b_node);
509 
510  SubsurfaceScatteringNode *subsurface = graph->create_node<SubsurfaceScatteringNode>();
511 
512  switch (b_subsurface_node.falloff()) {
513  case BL::ShaderNodeSubsurfaceScattering::falloff_BURLEY:
514  subsurface->set_method(CLOSURE_BSSRDF_BURLEY_ID);
515  break;
516  case BL::ShaderNodeSubsurfaceScattering::falloff_RANDOM_WALK_FIXED_RADIUS:
517  subsurface->set_method(CLOSURE_BSSRDF_RANDOM_WALK_FIXED_RADIUS_ID);
518  break;
519  case BL::ShaderNodeSubsurfaceScattering::falloff_RANDOM_WALK:
520  subsurface->set_method(CLOSURE_BSSRDF_RANDOM_WALK_ID);
521  break;
522  }
523 
524  node = subsurface;
525  }
526  else if (b_node.is_a(&RNA_ShaderNodeBsdfGlossy)) {
527  BL::ShaderNodeBsdfGlossy b_glossy_node(b_node);
528  GlossyBsdfNode *glossy = graph->create_node<GlossyBsdfNode>();
529 
530  switch (b_glossy_node.distribution()) {
531  case BL::ShaderNodeBsdfGlossy::distribution_SHARP:
532  glossy->set_distribution(CLOSURE_BSDF_REFLECTION_ID);
533  break;
534  case BL::ShaderNodeBsdfGlossy::distribution_BECKMANN:
535  glossy->set_distribution(CLOSURE_BSDF_MICROFACET_BECKMANN_ID);
536  break;
537  case BL::ShaderNodeBsdfGlossy::distribution_GGX:
538  glossy->set_distribution(CLOSURE_BSDF_MICROFACET_GGX_ID);
539  break;
540  case BL::ShaderNodeBsdfGlossy::distribution_ASHIKHMIN_SHIRLEY:
541  glossy->set_distribution(CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ID);
542  break;
543  case BL::ShaderNodeBsdfGlossy::distribution_MULTI_GGX:
544  glossy->set_distribution(CLOSURE_BSDF_MICROFACET_MULTI_GGX_ID);
545  break;
546  }
547  node = glossy;
548  }
549  else if (b_node.is_a(&RNA_ShaderNodeBsdfGlass)) {
550  BL::ShaderNodeBsdfGlass b_glass_node(b_node);
551  GlassBsdfNode *glass = graph->create_node<GlassBsdfNode>();
552  switch (b_glass_node.distribution()) {
553  case BL::ShaderNodeBsdfGlass::distribution_SHARP:
554  glass->set_distribution(CLOSURE_BSDF_SHARP_GLASS_ID);
555  break;
556  case BL::ShaderNodeBsdfGlass::distribution_BECKMANN:
557  glass->set_distribution(CLOSURE_BSDF_MICROFACET_BECKMANN_GLASS_ID);
558  break;
559  case BL::ShaderNodeBsdfGlass::distribution_GGX:
560  glass->set_distribution(CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID);
561  break;
562  case BL::ShaderNodeBsdfGlass::distribution_MULTI_GGX:
563  glass->set_distribution(CLOSURE_BSDF_MICROFACET_MULTI_GGX_GLASS_ID);
564  break;
565  }
566  node = glass;
567  }
568  else if (b_node.is_a(&RNA_ShaderNodeBsdfRefraction)) {
569  BL::ShaderNodeBsdfRefraction b_refraction_node(b_node);
570  RefractionBsdfNode *refraction = graph->create_node<RefractionBsdfNode>();
571  switch (b_refraction_node.distribution()) {
572  case BL::ShaderNodeBsdfRefraction::distribution_SHARP:
573  refraction->set_distribution(CLOSURE_BSDF_REFRACTION_ID);
574  break;
575  case BL::ShaderNodeBsdfRefraction::distribution_BECKMANN:
576  refraction->set_distribution(CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID);
577  break;
578  case BL::ShaderNodeBsdfRefraction::distribution_GGX:
579  refraction->set_distribution(CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID);
580  break;
581  }
582  node = refraction;
583  }
584  else if (b_node.is_a(&RNA_ShaderNodeBsdfToon)) {
585  BL::ShaderNodeBsdfToon b_toon_node(b_node);
586  ToonBsdfNode *toon = graph->create_node<ToonBsdfNode>();
587  switch (b_toon_node.component()) {
588  case BL::ShaderNodeBsdfToon::component_DIFFUSE:
589  toon->set_component(CLOSURE_BSDF_DIFFUSE_TOON_ID);
590  break;
591  case BL::ShaderNodeBsdfToon::component_GLOSSY:
592  toon->set_component(CLOSURE_BSDF_GLOSSY_TOON_ID);
593  break;
594  }
595  node = toon;
596  }
597  else if (b_node.is_a(&RNA_ShaderNodeBsdfHair)) {
598  BL::ShaderNodeBsdfHair b_hair_node(b_node);
599  HairBsdfNode *hair = graph->create_node<HairBsdfNode>();
600  switch (b_hair_node.component()) {
601  case BL::ShaderNodeBsdfHair::component_Reflection:
602  hair->set_component(CLOSURE_BSDF_HAIR_REFLECTION_ID);
603  break;
604  case BL::ShaderNodeBsdfHair::component_Transmission:
605  hair->set_component(CLOSURE_BSDF_HAIR_TRANSMISSION_ID);
606  break;
607  }
608  node = hair;
609  }
610  else if (b_node.is_a(&RNA_ShaderNodeBsdfHairPrincipled)) {
611  BL::ShaderNodeBsdfHairPrincipled b_principled_hair_node(b_node);
613  principled_hair->set_parametrization(
614  (NodePrincipledHairParametrization)get_enum(b_principled_hair_node.ptr,
615  "parametrization",
619  }
620  else if (b_node.is_a(&RNA_ShaderNodeBsdfPrincipled)) {
621  BL::ShaderNodeBsdfPrincipled b_principled_node(b_node);
622  PrincipledBsdfNode *principled = graph->create_node<PrincipledBsdfNode>();
623  switch (b_principled_node.distribution()) {
624  case BL::ShaderNodeBsdfPrincipled::distribution_GGX:
625  principled->set_distribution(CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID);
626  break;
627  case BL::ShaderNodeBsdfPrincipled::distribution_MULTI_GGX:
628  principled->set_distribution(CLOSURE_BSDF_MICROFACET_MULTI_GGX_GLASS_ID);
629  break;
630  }
631  switch (b_principled_node.subsurface_method()) {
632  case BL::ShaderNodeBsdfPrincipled::subsurface_method_BURLEY:
633  principled->set_subsurface_method(CLOSURE_BSSRDF_BURLEY_ID);
634  break;
635  case BL::ShaderNodeBsdfPrincipled::subsurface_method_RANDOM_WALK_FIXED_RADIUS:
636  principled->set_subsurface_method(CLOSURE_BSSRDF_RANDOM_WALK_FIXED_RADIUS_ID);
637  break;
638  case BL::ShaderNodeBsdfPrincipled::subsurface_method_RANDOM_WALK:
639  principled->set_subsurface_method(CLOSURE_BSSRDF_RANDOM_WALK_ID);
640  break;
641  }
642  node = principled;
643  }
644  else if (b_node.is_a(&RNA_ShaderNodeBsdfTranslucent)) {
645  node = graph->create_node<TranslucentBsdfNode>();
646  }
647  else if (b_node.is_a(&RNA_ShaderNodeBsdfTransparent)) {
648  node = graph->create_node<TransparentBsdfNode>();
649  }
650  else if (b_node.is_a(&RNA_ShaderNodeBsdfVelvet)) {
651  node = graph->create_node<VelvetBsdfNode>();
652  }
653  else if (b_node.is_a(&RNA_ShaderNodeEmission)) {
654  node = graph->create_node<EmissionNode>();
655  }
656  else if (b_node.is_a(&RNA_ShaderNodeAmbientOcclusion)) {
657  BL::ShaderNodeAmbientOcclusion b_ao_node(b_node);
658  AmbientOcclusionNode *ao = graph->create_node<AmbientOcclusionNode>();
659  ao->set_samples(b_ao_node.samples());
660  ao->set_inside(b_ao_node.inside());
661  ao->set_only_local(b_ao_node.only_local());
662  node = ao;
663  }
664  else if (b_node.is_a(&RNA_ShaderNodeVolumeScatter)) {
665  node = graph->create_node<ScatterVolumeNode>();
666  }
667  else if (b_node.is_a(&RNA_ShaderNodeVolumeAbsorption)) {
668  node = graph->create_node<AbsorptionVolumeNode>();
669  }
670  else if (b_node.is_a(&RNA_ShaderNodeVolumePrincipled)) {
671  PrincipledVolumeNode *principled = graph->create_node<PrincipledVolumeNode>();
672  node = principled;
673  }
674  else if (b_node.is_a(&RNA_ShaderNodeNewGeometry)) {
675  node = graph->create_node<GeometryNode>();
676  }
677  else if (b_node.is_a(&RNA_ShaderNodeWireframe)) {
678  BL::ShaderNodeWireframe b_wireframe_node(b_node);
679  WireframeNode *wire = graph->create_node<WireframeNode>();
680  wire->set_use_pixel_size(b_wireframe_node.use_pixel_size());
681  node = wire;
682  }
683  else if (b_node.is_a(&RNA_ShaderNodeWavelength)) {
684  node = graph->create_node<WavelengthNode>();
685  }
686  else if (b_node.is_a(&RNA_ShaderNodeBlackbody)) {
687  node = graph->create_node<BlackbodyNode>();
688  }
689  else if (b_node.is_a(&RNA_ShaderNodeLightPath)) {
690  node = graph->create_node<LightPathNode>();
691  }
692  else if (b_node.is_a(&RNA_ShaderNodeLightFalloff)) {
693  node = graph->create_node<LightFalloffNode>();
694  }
695  else if (b_node.is_a(&RNA_ShaderNodeObjectInfo)) {
696  node = graph->create_node<ObjectInfoNode>();
697  }
698  else if (b_node.is_a(&RNA_ShaderNodeParticleInfo)) {
699  node = graph->create_node<ParticleInfoNode>();
700  }
701  else if (b_node.is_a(&RNA_ShaderNodeHairInfo)) {
702  node = graph->create_node<HairInfoNode>();
703  }
704  else if (b_node.is_a(&RNA_ShaderNodePointInfo)) {
705  node = graph->create_node<PointInfoNode>();
706  }
707  else if (b_node.is_a(&RNA_ShaderNodeVolumeInfo)) {
708  node = graph->create_node<VolumeInfoNode>();
709  }
710  else if (b_node.is_a(&RNA_ShaderNodeVertexColor)) {
711  BL::ShaderNodeVertexColor b_vertex_color_node(b_node);
712  VertexColorNode *vertex_color_node = graph->create_node<VertexColorNode>();
713  vertex_color_node->set_layer_name(ustring(b_vertex_color_node.layer_name()));
714  node = vertex_color_node;
715  }
716  else if (b_node.is_a(&RNA_ShaderNodeBump)) {
717  BL::ShaderNodeBump b_bump_node(b_node);
718  BumpNode *bump = graph->create_node<BumpNode>();
719  bump->set_invert(b_bump_node.invert());
720  node = bump;
721  }
722  else if (b_node.is_a(&RNA_ShaderNodeScript)) {
723 #ifdef WITH_OSL
724  if (scene->shader_manager->use_osl()) {
725  /* create script node */
726  BL::ShaderNodeScript b_script_node(b_node);
727 
728  ShaderManager *manager = scene->shader_manager;
729  string bytecode_hash = b_script_node.bytecode_hash();
730 
731  if (!bytecode_hash.empty()) {
732  node = OSLShaderManager::osl_node(
733  graph, manager, "", bytecode_hash, b_script_node.bytecode());
734  }
735  else {
736  string absolute_filepath = blender_absolute_path(
737  b_data, b_ntree, b_script_node.filepath());
738  node = OSLShaderManager::osl_node(graph, manager, absolute_filepath, "");
739  }
740  }
741 #else
742  (void)b_data;
743  (void)b_ntree;
744 #endif
745  }
746  else if (b_node.is_a(&RNA_ShaderNodeTexImage)) {
747  BL::ShaderNodeTexImage b_image_node(b_node);
748  BL::Image b_image(b_image_node.image());
749  BL::ImageUser b_image_user(b_image_node.image_user());
750  ImageTextureNode *image = graph->create_node<ImageTextureNode>();
751 
752  image->set_interpolation(get_image_interpolation(b_image_node));
753  image->set_extension(get_image_extension(b_image_node));
754  image->set_projection((NodeImageProjection)b_image_node.projection());
755  image->set_projection_blend(b_image_node.projection_blend());
756  BL::TexMapping b_texture_mapping(b_image_node.texture_mapping());
757  get_tex_mapping(image, b_texture_mapping);
758 
759  if (b_image) {
760  BL::Image::source_enum b_image_source = b_image.source();
761  PointerRNA colorspace_ptr = b_image.colorspace_settings().ptr;
762  image->set_colorspace(ustring(get_enum_identifier(colorspace_ptr, "name")));
763 
764  image->set_animated(is_image_animated(b_image_source, b_image_user));
765  image->set_alpha_type(get_image_alpha_type(b_image));
766 
768  for (BL::UDIMTile &b_tile : b_image.tiles) {
769  tiles.push_back_slow(b_tile.number());
770  }
771  image->set_tiles(tiles);
772 
773  /* builtin images will use callback-based reading because
774  * they could only be loaded correct from blender side
775  */
776  bool is_builtin = b_image.packed_file() || b_image_source == BL::Image::source_GENERATED ||
777  b_image_source == BL::Image::source_MOVIE ||
778  (b_engine.is_preview() && b_image_source != BL::Image::source_SEQUENCE);
779 
780  if (is_builtin) {
781  /* for builtin images we're using image datablock name to find an image to
782  * read pixels from later
783  *
784  * also store frame number as well, so there's no differences in handling
785  * builtin names for packed images and movies
786  */
787  int scene_frame = b_scene.frame_current();
788  int image_frame = image_user_frame_number(b_image_user, b_image, scene_frame);
789  if (b_image_source != BL::Image::source_TILED) {
790  image->handle = scene->image_manager->add_image(
791  new BlenderImageLoader(b_image, image_frame, 0, b_engine.is_preview()),
792  image->image_params());
793  }
794  else {
795  vector<ImageLoader *> loaders;
796  loaders.reserve(image->get_tiles().size());
797  for (int tile_number : image->get_tiles()) {
798  loaders.push_back(
799  new BlenderImageLoader(b_image, image_frame, tile_number, b_engine.is_preview()));
800  }
801 
802  image->handle = scene->image_manager->add_image(loaders, image->image_params());
803  }
804  }
805  else {
806  ustring filename = ustring(
807  image_user_file_path(b_data, b_image_user, b_image, b_scene.frame_current()));
808  image->set_filename(filename);
809  }
810  }
811  node = image;
812  }
813  else if (b_node.is_a(&RNA_ShaderNodeTexEnvironment)) {
814  BL::ShaderNodeTexEnvironment b_env_node(b_node);
815  BL::Image b_image(b_env_node.image());
816  BL::ImageUser b_image_user(b_env_node.image_user());
817  EnvironmentTextureNode *env = graph->create_node<EnvironmentTextureNode>();
818 
819  env->set_interpolation(get_image_interpolation(b_env_node));
820  env->set_projection((NodeEnvironmentProjection)b_env_node.projection());
821  BL::TexMapping b_texture_mapping(b_env_node.texture_mapping());
822  get_tex_mapping(env, b_texture_mapping);
823 
824  if (b_image) {
825  BL::Image::source_enum b_image_source = b_image.source();
826  PointerRNA colorspace_ptr = b_image.colorspace_settings().ptr;
827  env->set_colorspace(ustring(get_enum_identifier(colorspace_ptr, "name")));
828  env->set_animated(is_image_animated(b_image_source, b_image_user));
829  env->set_alpha_type(get_image_alpha_type(b_image));
830 
831  bool is_builtin = b_image.packed_file() || b_image_source == BL::Image::source_GENERATED ||
832  b_image_source == BL::Image::source_MOVIE ||
833  (b_engine.is_preview() && b_image_source != BL::Image::source_SEQUENCE);
834 
835  if (is_builtin) {
836  int scene_frame = b_scene.frame_current();
837  int image_frame = image_user_frame_number(b_image_user, b_image, scene_frame);
839  new BlenderImageLoader(b_image, image_frame, 0, b_engine.is_preview()),
840  env->image_params());
841  }
842  else {
843  env->set_filename(
844  ustring(image_user_file_path(b_data, b_image_user, b_image, b_scene.frame_current())));
845  }
846  }
847  node = env;
848  }
849  else if (b_node.is_a(&RNA_ShaderNodeTexGradient)) {
850  BL::ShaderNodeTexGradient b_gradient_node(b_node);
851  GradientTextureNode *gradient = graph->create_node<GradientTextureNode>();
852  gradient->set_gradient_type((NodeGradientType)b_gradient_node.gradient_type());
853  BL::TexMapping b_texture_mapping(b_gradient_node.texture_mapping());
854  get_tex_mapping(gradient, b_texture_mapping);
855  node = gradient;
856  }
857  else if (b_node.is_a(&RNA_ShaderNodeTexVoronoi)) {
858  BL::ShaderNodeTexVoronoi b_voronoi_node(b_node);
859  VoronoiTextureNode *voronoi = graph->create_node<VoronoiTextureNode>();
860  voronoi->set_dimensions(b_voronoi_node.voronoi_dimensions());
861  voronoi->set_feature((NodeVoronoiFeature)b_voronoi_node.feature());
862  voronoi->set_metric((NodeVoronoiDistanceMetric)b_voronoi_node.distance());
863  BL::TexMapping b_texture_mapping(b_voronoi_node.texture_mapping());
864  get_tex_mapping(voronoi, b_texture_mapping);
865  node = voronoi;
866  }
867  else if (b_node.is_a(&RNA_ShaderNodeTexMagic)) {
868  BL::ShaderNodeTexMagic b_magic_node(b_node);
869  MagicTextureNode *magic = graph->create_node<MagicTextureNode>();
870  magic->set_depth(b_magic_node.turbulence_depth());
871  BL::TexMapping b_texture_mapping(b_magic_node.texture_mapping());
872  get_tex_mapping(magic, b_texture_mapping);
873  node = magic;
874  }
875  else if (b_node.is_a(&RNA_ShaderNodeTexWave)) {
876  BL::ShaderNodeTexWave b_wave_node(b_node);
877  WaveTextureNode *wave = graph->create_node<WaveTextureNode>();
878  wave->set_wave_type((NodeWaveType)b_wave_node.wave_type());
879  wave->set_bands_direction((NodeWaveBandsDirection)b_wave_node.bands_direction());
880  wave->set_rings_direction((NodeWaveRingsDirection)b_wave_node.rings_direction());
881  wave->set_profile((NodeWaveProfile)b_wave_node.wave_profile());
882  BL::TexMapping b_texture_mapping(b_wave_node.texture_mapping());
883  get_tex_mapping(wave, b_texture_mapping);
884  node = wave;
885  }
886  else if (b_node.is_a(&RNA_ShaderNodeTexChecker)) {
887  BL::ShaderNodeTexChecker b_checker_node(b_node);
888  CheckerTextureNode *checker = graph->create_node<CheckerTextureNode>();
889  BL::TexMapping b_texture_mapping(b_checker_node.texture_mapping());
890  get_tex_mapping(checker, b_texture_mapping);
891  node = checker;
892  }
893  else if (b_node.is_a(&RNA_ShaderNodeTexBrick)) {
894  BL::ShaderNodeTexBrick b_brick_node(b_node);
895  BrickTextureNode *brick = graph->create_node<BrickTextureNode>();
896  brick->set_offset(b_brick_node.offset());
897  brick->set_offset_frequency(b_brick_node.offset_frequency());
898  brick->set_squash(b_brick_node.squash());
899  brick->set_squash_frequency(b_brick_node.squash_frequency());
900  BL::TexMapping b_texture_mapping(b_brick_node.texture_mapping());
901  get_tex_mapping(brick, b_texture_mapping);
902  node = brick;
903  }
904  else if (b_node.is_a(&RNA_ShaderNodeTexNoise)) {
905  BL::ShaderNodeTexNoise b_noise_node(b_node);
906  NoiseTextureNode *noise = graph->create_node<NoiseTextureNode>();
907  noise->set_dimensions(b_noise_node.noise_dimensions());
908  BL::TexMapping b_texture_mapping(b_noise_node.texture_mapping());
909  get_tex_mapping(noise, b_texture_mapping);
910  node = noise;
911  }
912  else if (b_node.is_a(&RNA_ShaderNodeTexMusgrave)) {
913  BL::ShaderNodeTexMusgrave b_musgrave_node(b_node);
914  MusgraveTextureNode *musgrave_node = graph->create_node<MusgraveTextureNode>();
915  musgrave_node->set_musgrave_type((NodeMusgraveType)b_musgrave_node.musgrave_type());
916  musgrave_node->set_dimensions(b_musgrave_node.musgrave_dimensions());
917  BL::TexMapping b_texture_mapping(b_musgrave_node.texture_mapping());
918  get_tex_mapping(musgrave_node, b_texture_mapping);
919  node = musgrave_node;
920  }
921  else if (b_node.is_a(&RNA_ShaderNodeTexCoord)) {
922  BL::ShaderNodeTexCoord b_tex_coord_node(b_node);
924  tex_coord->set_from_dupli(b_tex_coord_node.from_instancer());
925  if (b_tex_coord_node.object()) {
926  tex_coord->set_use_transform(true);
927  tex_coord->set_ob_tfm(get_transform(b_tex_coord_node.object().matrix_world()));
928  }
929  node = tex_coord;
930  }
931  else if (b_node.is_a(&RNA_ShaderNodeTexSky)) {
932  BL::ShaderNodeTexSky b_sky_node(b_node);
933  SkyTextureNode *sky = graph->create_node<SkyTextureNode>();
934  sky->set_sky_type((NodeSkyType)b_sky_node.sky_type());
935  sky->set_sun_direction(normalize(get_float3(b_sky_node.sun_direction())));
936  sky->set_turbidity(b_sky_node.turbidity());
937  sky->set_ground_albedo(b_sky_node.ground_albedo());
938  sky->set_sun_disc(b_sky_node.sun_disc());
939  sky->set_sun_size(b_sky_node.sun_size());
940  sky->set_sun_intensity(b_sky_node.sun_intensity());
941  /* Patch sun position to be able to animate daylight cycle while keeping the shading code
942  * simple. */
943  float sun_rotation = b_sky_node.sun_rotation();
944  /* Wrap into [-2PI..2PI] range. */
945  float sun_elevation = fmodf(b_sky_node.sun_elevation(), M_2PI_F);
946  /* Wrap into [-PI..PI] range. */
947  if (fabsf(sun_elevation) >= M_PI_F) {
948  sun_elevation -= copysignf(2.0f, sun_elevation) * M_PI_F;
949  }
950  /* Wrap into [-PI/2..PI/2] range while keeping the same absolute position. */
951  if (sun_elevation >= M_PI_2_F || sun_elevation <= -M_PI_2_F) {
952  sun_elevation = copysignf(M_PI_F, sun_elevation) - sun_elevation;
953  sun_rotation += M_PI_F;
954  }
955  sky->set_sun_elevation(sun_elevation);
956  sky->set_sun_rotation(sun_rotation);
957  sky->set_altitude(b_sky_node.altitude());
958  sky->set_air_density(b_sky_node.air_density());
959  sky->set_dust_density(b_sky_node.dust_density());
960  sky->set_ozone_density(b_sky_node.ozone_density());
961  BL::TexMapping b_texture_mapping(b_sky_node.texture_mapping());
962  get_tex_mapping(sky, b_texture_mapping);
963  node = sky;
964  }
965  else if (b_node.is_a(&RNA_ShaderNodeTexIES)) {
966  BL::ShaderNodeTexIES b_ies_node(b_node);
967  IESLightNode *ies = graph->create_node<IESLightNode>();
968  switch (b_ies_node.mode()) {
969  case BL::ShaderNodeTexIES::mode_EXTERNAL:
970  ies->set_filename(ustring(blender_absolute_path(b_data, b_ntree, b_ies_node.filepath())));
971  break;
972  case BL::ShaderNodeTexIES::mode_INTERNAL:
973  ustring ies_content = ustring(get_text_datablock_content(b_ies_node.ies().ptr));
974  if (ies_content.empty()) {
975  ies_content = "\n";
976  }
977  ies->set_ies(ies_content);
978  break;
979  }
980  node = ies;
981  }
982  else if (b_node.is_a(&RNA_ShaderNodeTexWhiteNoise)) {
983  BL::ShaderNodeTexWhiteNoise b_tex_white_noise_node(b_node);
984  WhiteNoiseTextureNode *white_noise_node = graph->create_node<WhiteNoiseTextureNode>();
985  white_noise_node->set_dimensions(b_tex_white_noise_node.noise_dimensions());
986  node = white_noise_node;
987  }
988  else if (b_node.is_a(&RNA_ShaderNodeNormalMap)) {
989  BL::ShaderNodeNormalMap b_normal_map_node(b_node);
990  NormalMapNode *nmap = graph->create_node<NormalMapNode>();
991  nmap->set_space((NodeNormalMapSpace)b_normal_map_node.space());
992  nmap->set_attribute(ustring(b_normal_map_node.uv_map()));
993  node = nmap;
994  }
995  else if (b_node.is_a(&RNA_ShaderNodeTangent)) {
996  BL::ShaderNodeTangent b_tangent_node(b_node);
997  TangentNode *tangent = graph->create_node<TangentNode>();
998  tangent->set_direction_type((NodeTangentDirectionType)b_tangent_node.direction_type());
999  tangent->set_axis((NodeTangentAxis)b_tangent_node.axis());
1000  tangent->set_attribute(ustring(b_tangent_node.uv_map()));
1001  node = tangent;
1002  }
1003  else if (b_node.is_a(&RNA_ShaderNodeUVMap)) {
1004  BL::ShaderNodeUVMap b_uvmap_node(b_node);
1005  UVMapNode *uvm = graph->create_node<UVMapNode>();
1006  uvm->set_attribute(ustring(b_uvmap_node.uv_map()));
1007  uvm->set_from_dupli(b_uvmap_node.from_instancer());
1008  node = uvm;
1009  }
1010  else if (b_node.is_a(&RNA_ShaderNodeTexPointDensity)) {
1011  BL::ShaderNodeTexPointDensity b_point_density_node(b_node);
1012  PointDensityTextureNode *point_density = graph->create_node<PointDensityTextureNode>();
1013  point_density->set_space((NodeTexVoxelSpace)b_point_density_node.space());
1014  point_density->set_interpolation(get_image_interpolation(b_point_density_node));
1015  point_density->handle = scene->image_manager->add_image(
1016  new BlenderPointDensityLoader(b_depsgraph, b_point_density_node),
1017  point_density->image_params());
1018 
1019  b_point_density_node.cache_point_density(b_depsgraph);
1020  node = point_density;
1021 
1022  /* Transformation form world space to texture space.
1023  *
1024  * NOTE: Do this after the texture is cached, this is because getting
1025  * min/max will need to access this cache.
1026  */
1027  BL::Object b_ob(b_point_density_node.object());
1028  if (b_ob) {
1029  float3 loc, size;
1030  point_density_texture_space(b_depsgraph, b_point_density_node, loc, size);
1031  point_density->set_tfm(transform_translate(-loc) * transform_scale(size) *
1032  transform_inverse(get_transform(b_ob.matrix_world())));
1033  }
1034  }
1035  else if (b_node.is_a(&RNA_ShaderNodeBevel)) {
1036  BL::ShaderNodeBevel b_bevel_node(b_node);
1037  BevelNode *bevel = graph->create_node<BevelNode>();
1038  bevel->set_samples(b_bevel_node.samples());
1039  node = bevel;
1040  }
1041  else if (b_node.is_a(&RNA_ShaderNodeDisplacement)) {
1042  BL::ShaderNodeDisplacement b_disp_node(b_node);
1043  DisplacementNode *disp = graph->create_node<DisplacementNode>();
1044  disp->set_space((NodeNormalMapSpace)b_disp_node.space());
1045  node = disp;
1046  }
1047  else if (b_node.is_a(&RNA_ShaderNodeVectorDisplacement)) {
1048  BL::ShaderNodeVectorDisplacement b_disp_node(b_node);
1049  VectorDisplacementNode *disp = graph->create_node<VectorDisplacementNode>();
1050  disp->set_space((NodeNormalMapSpace)b_disp_node.space());
1051  disp->set_attribute(ustring(""));
1052  node = disp;
1053  }
1054  else if (b_node.is_a(&RNA_ShaderNodeOutputAOV)) {
1055  BL::ShaderNodeOutputAOV b_aov_node(b_node);
1056  OutputAOVNode *aov = graph->create_node<OutputAOVNode>();
1057  aov->set_name(ustring(b_aov_node.name()));
1058  node = aov;
1059  }
1060 
1061  if (node) {
1062  node->name = b_node.name();
1063  graph->add(node);
1064  }
1065 
1066  return node;
1067 }
1068 
1070 {
1071  if (node->special_type == SHADER_SPECIAL_TYPE_OSL)
1072  return false;
1073 
1074  return true;
1075 }
1076 
1077 static ShaderInput *node_find_input_by_name(ShaderNode *node, BL::NodeSocket &b_socket)
1078 {
1079  string name = b_socket.identifier();
1080  ShaderInput *input = node->input(name.c_str());
1081 
1083  /* Different internal name for shader. */
1084  if (string_startswith(name, "Shader")) {
1085  string_replace(name, "Shader", "Closure");
1086  }
1087  input = node->input(name.c_str());
1088 
1089  if (!input) {
1090  /* Different internal numbering of two sockets with same name.
1091  * Note that the Blender convention for unique socket names changed
1092  * from . to _ at some point, so we check both to handle old files. */
1093  if (string_endswith(name, "_001")) {
1094  string_replace(name, "_001", "2");
1095  }
1096  else if (string_endswith(name, ".001")) {
1097  string_replace(name, ".001", "2");
1098  }
1099  else if (string_endswith(name, "_002")) {
1100  string_replace(name, "_002", "3");
1101  }
1102  else if (string_endswith(name, ".002")) {
1103  string_replace(name, ".002", "3");
1104  }
1105  else {
1106  name += "1";
1107  }
1108 
1109  input = node->input(name.c_str());
1110  }
1111  }
1112 
1113  return input;
1114 }
1115 
1116 static ShaderOutput *node_find_output_by_name(ShaderNode *node, BL::NodeSocket &b_socket)
1117 {
1118  string name = b_socket.identifier();
1119  ShaderOutput *output = node->output(name.c_str());
1120 
1122  /* Different internal name for shader. */
1123  if (name == "Shader") {
1124  name = "Closure";
1125  output = node->output(name.c_str());
1126  }
1127  }
1128 
1129  return output;
1130 }
1131 
1132 static void add_nodes(Scene *scene,
1133  BL::RenderEngine &b_engine,
1134  BL::BlendData &b_data,
1135  BL::Depsgraph &b_depsgraph,
1136  BL::Scene &b_scene,
1137  ShaderGraph *graph,
1138  BL::ShaderNodeTree &b_ntree,
1139  const ProxyMap &proxy_input_map,
1140  const ProxyMap &proxy_output_map)
1141 {
1142  /* add nodes */
1143  PtrInputMap input_map;
1144  PtrOutputMap output_map;
1145 
1146  /* find the node to use for output if there are multiple */
1147  BL::ShaderNode output_node = b_ntree.get_output_node(
1148  BL::ShaderNodeOutputMaterial::target_CYCLES);
1149 
1150  /* add nodes */
1151  for (BL::Node &b_node : b_ntree.nodes) {
1152  if (b_node.mute() || b_node.is_a(&RNA_NodeReroute)) {
1153  /* replace muted node with internal links */
1154  for (BL::NodeLink &b_link : b_node.internal_links) {
1155  BL::NodeSocket to_socket(b_link.to_socket());
1156  SocketType::Type to_socket_type = convert_socket_type(to_socket);
1157  if (to_socket_type == SocketType::UNDEFINED) {
1158  continue;
1159  }
1160 
1161  ConvertNode *proxy = graph->create_node<ConvertNode>(to_socket_type, to_socket_type, true);
1162 
1163  input_map[b_link.from_socket().ptr.data] = proxy->inputs[0];
1164  output_map[b_link.to_socket().ptr.data] = proxy->outputs[0];
1165 
1166  graph->add(proxy);
1167  }
1168  }
1169  else if (b_node.is_a(&RNA_ShaderNodeGroup) || b_node.is_a(&RNA_NodeCustomGroup) ||
1170  b_node.is_a(&RNA_ShaderNodeCustomGroup)) {
1171 
1172  BL::ShaderNodeTree b_group_ntree(PointerRNA_NULL);
1173  if (b_node.is_a(&RNA_ShaderNodeGroup))
1174  b_group_ntree = BL::ShaderNodeTree(((BL::NodeGroup)(b_node)).node_tree());
1175  else if (b_node.is_a(&RNA_NodeCustomGroup))
1176  b_group_ntree = BL::ShaderNodeTree(((BL::NodeCustomGroup)(b_node)).node_tree());
1177  else
1178  b_group_ntree = BL::ShaderNodeTree(((BL::ShaderNodeCustomGroup)(b_node)).node_tree());
1179 
1180  ProxyMap group_proxy_input_map, group_proxy_output_map;
1181 
1182  /* Add a proxy node for each socket
1183  * Do this even if the node group has no internal tree,
1184  * so that links have something to connect to and assert won't fail.
1185  */
1186  for (BL::NodeSocket &b_input : b_node.inputs) {
1187  SocketType::Type input_type = convert_socket_type(b_input);
1188  if (input_type == SocketType::UNDEFINED) {
1189  continue;
1190  }
1191 
1192  ConvertNode *proxy = graph->create_node<ConvertNode>(input_type, input_type, true);
1193  graph->add(proxy);
1194 
1195  /* register the proxy node for internal binding */
1196  group_proxy_input_map[b_input.identifier()] = proxy;
1197 
1198  input_map[b_input.ptr.data] = proxy->inputs[0];
1199 
1200  set_default_value(proxy->inputs[0], b_input, b_data, b_ntree);
1201  }
1202  for (BL::NodeSocket &b_output : b_node.outputs) {
1203  SocketType::Type output_type = convert_socket_type(b_output);
1204  if (output_type == SocketType::UNDEFINED) {
1205  continue;
1206  }
1207 
1208  ConvertNode *proxy = graph->create_node<ConvertNode>(output_type, output_type, true);
1209  graph->add(proxy);
1210 
1211  /* register the proxy node for internal binding */
1212  group_proxy_output_map[b_output.identifier()] = proxy;
1213 
1214  output_map[b_output.ptr.data] = proxy->outputs[0];
1215  }
1216 
1217  if (b_group_ntree) {
1218  add_nodes(scene,
1219  b_engine,
1220  b_data,
1221  b_depsgraph,
1222  b_scene,
1223  graph,
1224  b_group_ntree,
1225  group_proxy_input_map,
1226  group_proxy_output_map);
1227  }
1228  }
1229  else if (b_node.is_a(&RNA_NodeGroupInput)) {
1230  /* map each socket to a proxy node */
1231  for (BL::NodeSocket &b_output : b_node.outputs) {
1232  ProxyMap::const_iterator proxy_it = proxy_input_map.find(b_output.identifier());
1233  if (proxy_it != proxy_input_map.end()) {
1234  ConvertNode *proxy = proxy_it->second;
1235 
1236  output_map[b_output.ptr.data] = proxy->outputs[0];
1237  }
1238  }
1239  }
1240  else if (b_node.is_a(&RNA_NodeGroupOutput)) {
1241  BL::NodeGroupOutput b_output_node(b_node);
1242  /* only the active group output is used */
1243  if (b_output_node.is_active_output()) {
1244  /* map each socket to a proxy node */
1245  for (BL::NodeSocket &b_input : b_node.inputs) {
1246  ProxyMap::const_iterator proxy_it = proxy_output_map.find(b_input.identifier());
1247  if (proxy_it != proxy_output_map.end()) {
1248  ConvertNode *proxy = proxy_it->second;
1249 
1250  input_map[b_input.ptr.data] = proxy->inputs[0];
1251 
1252  set_default_value(proxy->inputs[0], b_input, b_data, b_ntree);
1253  }
1254  }
1255  }
1256  }
1257  else {
1258  ShaderNode *node = NULL;
1259 
1260  if (b_node.ptr.data == output_node.ptr.data) {
1261  node = graph->output();
1262  }
1263  else {
1264  BL::ShaderNode b_shader_node(b_node);
1265  node = add_node(
1266  scene, b_engine, b_data, b_depsgraph, b_scene, graph, b_ntree, b_shader_node);
1267  }
1268 
1269  if (node) {
1270  /* map node sockets for linking */
1271  for (BL::NodeSocket &b_input : b_node.inputs) {
1273  if (!input) {
1274  /* XXX should not happen, report error? */
1275  continue;
1276  }
1277  input_map[b_input.ptr.data] = input;
1278 
1279  set_default_value(input, b_input, b_data, b_ntree);
1280  }
1281  for (BL::NodeSocket &b_output : b_node.outputs) {
1283  if (!output) {
1284  /* XXX should not happen, report error? */
1285  continue;
1286  }
1287  output_map[b_output.ptr.data] = output;
1288  }
1289  }
1290  }
1291  }
1292 
1293  /* connect nodes */
1294  for (BL::NodeLink &b_link : b_ntree.links) {
1295  /* Ignore invalid links to avoid unwanted cycles created in graph.
1296  * Also ignore links with unavailable sockets. */
1297  if (!(b_link.is_valid() && b_link.from_socket().enabled() && b_link.to_socket().enabled()) ||
1298  b_link.is_muted()) {
1299  continue;
1300  }
1301  /* get blender link data */
1302  BL::NodeSocket b_from_sock = b_link.from_socket();
1303  BL::NodeSocket b_to_sock = b_link.to_socket();
1304 
1305  ShaderOutput *output = 0;
1306  ShaderInput *input = 0;
1307 
1308  PtrOutputMap::iterator output_it = output_map.find(b_from_sock.ptr.data);
1309  if (output_it != output_map.end())
1310  output = output_it->second;
1311  PtrInputMap::iterator input_it = input_map.find(b_to_sock.ptr.data);
1312  if (input_it != input_map.end())
1313  input = input_it->second;
1314 
1315  /* either node may be NULL when the node was not exported, typically
1316  * because the node type is not supported */
1317  if (output && input)
1318  graph->connect(output, input);
1319  }
1320 }
1321 
1322 static void add_nodes(Scene *scene,
1323  BL::RenderEngine &b_engine,
1324  BL::BlendData &b_data,
1325  BL::Depsgraph &b_depsgraph,
1326  BL::Scene &b_scene,
1327  ShaderGraph *graph,
1328  BL::ShaderNodeTree &b_ntree)
1329 {
1330  static const ProxyMap empty_proxy_map;
1331  add_nodes(scene,
1332  b_engine,
1333  b_data,
1334  b_depsgraph,
1335  b_scene,
1336  graph,
1337  b_ntree,
1338  empty_proxy_map,
1339  empty_proxy_map);
1340 }
1341 
1342 /* Sync Materials */
1343 
1344 void BlenderSync::sync_materials(BL::Depsgraph &b_depsgraph, bool update_all)
1345 {
1346  shader_map.set_default(scene->default_surface);
1347 
1348  TaskPool pool;
1349  set<Shader *> updated_shaders;
1350 
1351  for (BL::ID &b_id : b_depsgraph.ids) {
1352  if (!b_id.is_a(&RNA_Material)) {
1353  continue;
1354  }
1355 
1356  BL::Material b_mat(b_id);
1357  Shader *shader;
1358 
1359  /* test if we need to sync */
1360  if (shader_map.add_or_update(&shader, b_mat) || update_all) {
1361  ShaderGraph *graph = new ShaderGraph();
1362 
1363  shader->name = b_mat.name().c_str();
1364  shader->set_pass_id(b_mat.pass_index());
1365 
1366  /* create nodes */
1367  if (b_mat.use_nodes() && b_mat.node_tree()) {
1368  BL::ShaderNodeTree b_ntree(b_mat.node_tree());
1369 
1370  add_nodes(scene, b_engine, b_data, b_depsgraph, b_scene, graph, b_ntree);
1371  }
1372  else {
1373  DiffuseBsdfNode *diffuse = graph->create_node<DiffuseBsdfNode>();
1374  diffuse->set_color(get_float3(b_mat.diffuse_color()));
1375  graph->add(diffuse);
1376 
1377  ShaderNode *out = graph->output();
1378  graph->connect(diffuse->output("BSDF"), out->input("Surface"));
1379  }
1380 
1381  /* settings */
1382  PointerRNA cmat = RNA_pointer_get(&b_mat.ptr, "cycles");
1383  shader->set_use_mis(get_boolean(cmat, "sample_as_light"));
1384  shader->set_use_transparent_shadow(get_boolean(cmat, "use_transparent_shadow"));
1385  shader->set_heterogeneous_volume(!get_boolean(cmat, "homogeneous_volume"));
1386  shader->set_volume_sampling_method(get_volume_sampling(cmat));
1387  shader->set_volume_interpolation_method(get_volume_interpolation(cmat));
1388  shader->set_volume_step_rate(get_float(cmat, "volume_step_rate"));
1389  shader->set_displacement_method(get_displacement_method(cmat));
1390 
1391  shader->set_graph(graph);
1392 
1393  /* By simplifying the shader graph as soon as possible, some
1394  * redundant shader nodes might be removed which prevents loading
1395  * unnecessary attributes later.
1396  *
1397  * However, since graph simplification also accounts for e.g. mix
1398  * weight, this would cause frequent expensive resyncs in interactive
1399  * sessions, so for those sessions optimization is only performed
1400  * right before compiling.
1401  */
1402  if (!preview) {
1404  /* NOTE: Update shaders out of the threads since those routines
1405  * are accessing and writing to a global context.
1406  */
1407  updated_shaders.insert(shader);
1408  }
1409  else {
1410  /* NOTE: Update tagging can access links which are being
1411  * optimized out.
1412  */
1413  shader->tag_update(scene);
1414  }
1415  }
1416  }
1417 
1418  pool.wait_work();
1419 
1420  foreach (Shader *shader, updated_shaders) {
1421  shader->tag_update(scene);
1422  }
1423 }
1424 
1425 /* Sync World */
1426 
1427 void BlenderSync::sync_world(BL::Depsgraph &b_depsgraph, BL::SpaceView3D &b_v3d, bool update_all)
1428 {
1429  Background *background = scene->background;
1430  Integrator *integrator = scene->integrator;
1431  PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
1432 
1433  BL::World b_world = b_scene.world();
1434 
1435  BlenderViewportParameters new_viewport_parameters(b_v3d, use_developer_ui);
1436 
1437  if (world_recalc || update_all || b_world.ptr.data != world_map ||
1438  viewport_parameters.shader_modified(new_viewport_parameters)) {
1439  Shader *shader = scene->default_background;
1440  ShaderGraph *graph = new ShaderGraph();
1441 
1442  /* create nodes */
1443  if (new_viewport_parameters.use_scene_world && b_world && b_world.use_nodes() &&
1444  b_world.node_tree()) {
1445  BL::ShaderNodeTree b_ntree(b_world.node_tree());
1446 
1447  add_nodes(scene, b_engine, b_data, b_depsgraph, b_scene, graph, b_ntree);
1448 
1449  /* volume */
1450  PointerRNA cworld = RNA_pointer_get(&b_world.ptr, "cycles");
1451  shader->set_heterogeneous_volume(!get_boolean(cworld, "homogeneous_volume"));
1452  shader->set_volume_sampling_method(get_volume_sampling(cworld));
1453  shader->set_volume_interpolation_method(get_volume_interpolation(cworld));
1454  shader->set_volume_step_rate(get_float(cworld, "volume_step_size"));
1455  }
1456  else if (new_viewport_parameters.use_scene_world && b_world) {
1457  BackgroundNode *background = graph->create_node<BackgroundNode>();
1458  background->set_color(get_float3(b_world.color()));
1459  graph->add(background);
1460 
1461  ShaderNode *out = graph->output();
1462  graph->connect(background->output("Background"), out->input("Surface"));
1463  }
1464  else if (!new_viewport_parameters.use_scene_world) {
1465  float3 world_color;
1466  if (b_world) {
1467  world_color = get_float3(b_world.color());
1468  }
1469  else {
1470  world_color = zero_float3();
1471  }
1472 
1473  BackgroundNode *background = graph->create_node<BackgroundNode>();
1474  graph->add(background);
1475 
1476  LightPathNode *light_path = graph->create_node<LightPathNode>();
1477  graph->add(light_path);
1478 
1479  MixNode *mix_scene_with_background = graph->create_node<MixNode>();
1480  mix_scene_with_background->set_color2(world_color);
1481  graph->add(mix_scene_with_background);
1482 
1483  EnvironmentTextureNode *texture_environment = graph->create_node<EnvironmentTextureNode>();
1484  texture_environment->set_tex_mapping_type(TextureMapping::VECTOR);
1485  float3 rotation_z = texture_environment->get_tex_mapping_rotation();
1486  rotation_z[2] = new_viewport_parameters.studiolight_rotate_z;
1487  texture_environment->set_tex_mapping_rotation(rotation_z);
1488  texture_environment->set_filename(new_viewport_parameters.studiolight_path);
1489  graph->add(texture_environment);
1490 
1491  MixNode *mix_intensity = graph->create_node<MixNode>();
1492  mix_intensity->set_mix_type(NODE_MIX_MUL);
1493  mix_intensity->set_fac(1.0f);
1494  mix_intensity->set_color2(make_float3(new_viewport_parameters.studiolight_intensity,
1495  new_viewport_parameters.studiolight_intensity,
1496  new_viewport_parameters.studiolight_intensity));
1497  graph->add(mix_intensity);
1498 
1499  TextureCoordinateNode *texture_coordinate = graph->create_node<TextureCoordinateNode>();
1500  graph->add(texture_coordinate);
1501 
1502  MixNode *mix_background_with_environment = graph->create_node<MixNode>();
1503  mix_background_with_environment->set_fac(
1504  new_viewport_parameters.studiolight_background_alpha);
1505  mix_background_with_environment->set_color1(world_color);
1506  graph->add(mix_background_with_environment);
1507 
1508  ShaderNode *out = graph->output();
1509 
1510  graph->connect(texture_coordinate->output("Generated"),
1511  texture_environment->input("Vector"));
1512  graph->connect(texture_environment->output("Color"), mix_intensity->input("Color1"));
1513  graph->connect(light_path->output("Is Camera Ray"), mix_scene_with_background->input("Fac"));
1514  graph->connect(mix_intensity->output("Color"), mix_scene_with_background->input("Color1"));
1515  graph->connect(mix_intensity->output("Color"),
1516  mix_background_with_environment->input("Color2"));
1517  graph->connect(mix_background_with_environment->output("Color"),
1518  mix_scene_with_background->input("Color2"));
1519  graph->connect(mix_scene_with_background->output("Color"), background->input("Color"));
1520  graph->connect(background->output("Background"), out->input("Surface"));
1521  }
1522 
1523  /* Visibility */
1524  if (b_world) {
1525  PointerRNA cvisibility = RNA_pointer_get(&b_world.ptr, "cycles_visibility");
1526  uint visibility = 0;
1527 
1528  visibility |= get_boolean(cvisibility, "camera") ? PATH_RAY_CAMERA : 0;
1529  visibility |= get_boolean(cvisibility, "diffuse") ? PATH_RAY_DIFFUSE : 0;
1530  visibility |= get_boolean(cvisibility, "glossy") ? PATH_RAY_GLOSSY : 0;
1531  visibility |= get_boolean(cvisibility, "transmission") ? PATH_RAY_TRANSMIT : 0;
1532  visibility |= get_boolean(cvisibility, "scatter") ? PATH_RAY_VOLUME_SCATTER : 0;
1533 
1534  background->set_visibility(visibility);
1535  }
1536 
1537  shader->set_graph(graph);
1538  shader->tag_update(scene);
1539  }
1540 
1541  /* Fast GI */
1542  if (b_world) {
1543  BL::WorldLighting b_light = b_world.light_settings();
1544  enum { FAST_GI_METHOD_REPLACE = 0, FAST_GI_METHOD_ADD = 1, FAST_GI_METHOD_NUM };
1545 
1546  const bool use_fast_gi = get_boolean(cscene, "use_fast_gi");
1547  if (use_fast_gi) {
1548  const int fast_gi_method = get_enum(
1549  cscene, "fast_gi_method", FAST_GI_METHOD_NUM, FAST_GI_METHOD_REPLACE);
1550  integrator->set_ao_factor((fast_gi_method == FAST_GI_METHOD_REPLACE) ? b_light.ao_factor() :
1551  0.0f);
1552  integrator->set_ao_additive_factor(
1553  (fast_gi_method == FAST_GI_METHOD_ADD) ? b_light.ao_factor() : 0.0f);
1554  }
1555  else {
1556  integrator->set_ao_factor(0.0f);
1557  integrator->set_ao_additive_factor(0.0f);
1558  }
1559 
1560  integrator->set_ao_distance(b_light.distance());
1561  }
1562  else {
1563  integrator->set_ao_factor(0.0f);
1564  integrator->set_ao_additive_factor(0.0f);
1565  integrator->set_ao_distance(10.0f);
1566  }
1567 
1568  background->set_transparent(b_scene.render().film_transparent());
1569 
1570  if (background->get_transparent()) {
1571  background->set_transparent_glass(get_boolean(cscene, "film_transparent_glass"));
1572  background->set_transparent_roughness_threshold(
1573  get_float(cscene, "film_transparent_roughness"));
1574  }
1575  else {
1576  background->set_transparent_glass(false);
1577  background->set_transparent_roughness_threshold(0.0f);
1578  }
1579 
1580  background->set_use_shader(view_layer.use_background_shader ||
1581  viewport_parameters.use_custom_shader());
1582 
1583  background->set_lightgroup(ustring(b_world ? b_world.lightgroup() : ""));
1584 
1585  background->tag_update(scene);
1586 }
1587 
1588 /* Sync Lights */
1589 
1590 void BlenderSync::sync_lights(BL::Depsgraph &b_depsgraph, bool update_all)
1591 {
1592  shader_map.set_default(scene->default_light);
1593 
1594  for (BL::ID &b_id : b_depsgraph.ids) {
1595  if (!b_id.is_a(&RNA_Light)) {
1596  continue;
1597  }
1598 
1599  BL::Light b_light(b_id);
1600  Shader *shader;
1601 
1602  /* test if we need to sync */
1603  if (shader_map.add_or_update(&shader, b_light) || update_all) {
1604  ShaderGraph *graph = new ShaderGraph();
1605 
1606  /* create nodes */
1607  if (b_light.use_nodes() && b_light.node_tree()) {
1608  shader->name = b_light.name().c_str();
1609 
1610  BL::ShaderNodeTree b_ntree(b_light.node_tree());
1611 
1612  add_nodes(scene, b_engine, b_data, b_depsgraph, b_scene, graph, b_ntree);
1613  }
1614  else {
1615  EmissionNode *emission = graph->create_node<EmissionNode>();
1616  emission->set_color(one_float3());
1617  emission->set_strength(1.0f);
1618  graph->add(emission);
1619 
1620  ShaderNode *out = graph->output();
1621  graph->connect(emission->output("Emission"), out->input("Surface"));
1622  }
1623 
1624  shader->set_graph(graph);
1625  shader->tag_update(scene);
1626  }
1627  }
1628 }
1629 
1630 void BlenderSync::sync_shaders(BL::Depsgraph &b_depsgraph, BL::SpaceView3D &b_v3d, bool update_all)
1631 {
1632  shader_map.pre_sync();
1633 
1634  sync_world(b_depsgraph, b_v3d, update_all);
1635  sync_lights(b_depsgraph, update_all);
1636  sync_materials(b_depsgraph, update_all);
1637 }
1638 
unsigned int uint
Definition: BLI_sys_types.h:67
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:35
struct ID ID
struct CurveMapping CurveMapping
struct ImageUser ImageUser
struct Image Image
struct Light Light
struct Material Material
struct Object Object
struct Scene Scene
struct TexMapping TexMapping
struct World World
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to curves
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to used to fine tune the interpolation of the input Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp a value between a minimum and a maximum Vector Perform vector math operation Invert a color
struct RenderEngine RenderEngine
static float3 get_node_output_vector(BL::Node &b_node, const string &name)
static DisplacementMethod get_displacement_method(PointerRNA &ptr)
static float3 get_node_output_rgba(BL::Node &b_node, const string &name)
static VolumeSampling get_volume_sampling(PointerRNA &ptr)
static ShaderOutput * node_find_output_by_name(ShaderNode *node, BL::NodeSocket &b_socket)
static BL::NodeSocket get_node_output(BL::Node &b_node, const string &name)
static ImageAlphaType get_image_alpha_type(BL::Image &b_image)
static int validate_enum_value(int value, int num_values, int default_value)
static void add_nodes(Scene *scene, BL::RenderEngine &b_engine, BL::BlendData &b_data, BL::Depsgraph &b_depsgraph, BL::Scene &b_scene, ShaderGraph *graph, BL::ShaderNodeTree &b_ntree, const ProxyMap &proxy_input_map, const ProxyMap &proxy_output_map)
static bool node_use_modified_socket_name(ShaderNode *node)
static ustring blender_attribute_name_add_type(const string &name, BlenderAttributeType type)
static bool is_image_animated(BL::Image::source_enum b_image_source, BL::ImageUser &b_image_user)
BlenderAttributeType blender_attribute_name_split_type(ustring name, string *r_real_name)
static void set_default_value(ShaderInput *input, BL::NodeSocket &b_sock, BL::BlendData &b_data, BL::ID &b_id)
static ShaderInput * node_find_input_by_name(ShaderNode *node, BL::NodeSocket &b_socket)
map< void *, ShaderOutput * > PtrOutputMap
static VolumeInterpolation get_volume_interpolation(PointerRNA &ptr)
static const string_view instancer_attr_prefix("\x01instancer:")
static float get_node_output_value(BL::Node &b_node, const string &name)
static void get_tex_mapping(TextureNode *mapping, BL::TexMapping &b_mapping)
static SocketType::Type convert_socket_type(BL::NodeSocket &b_socket)
static ShaderNode * add_node(Scene *scene, BL::RenderEngine &b_engine, BL::BlendData &b_data, BL::Depsgraph &b_depsgraph, BL::Scene &b_scene, ShaderGraph *graph, BL::ShaderNodeTree &b_ntree, BL::ShaderNode &b_node)
static ExtensionType get_image_extension(NodeType &b_node)
static const string_view object_attr_prefix("\x01object:")
static InterpolationType get_image_interpolation(NodeType &b_node)
CCL_NAMESPACE_BEGIN typedef map< void *, ShaderInput * > PtrInputMap
map< string, ConvertNode * > ProxyMap
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
SIMD_FORCE_INLINE btScalar norm() const
Return the norm (length) of the vector.
Definition: btVector3.h:263
bool shader_modified(const BlenderViewportParameters &other) const
Definition: viewport.cpp:70
bool use_custom_shader() const
Definition: viewport.cpp:89
ImageParams image_params() const
ImageHandle add_image(const string &filename, const ImageParams &params)
ImageHandle handle
Definition: shader_nodes.h:87
ImageParams image_params() const
void simplify(Scene *scene)
virtual bool use_osl()
Definition: scene/shader.h:178
ShaderInput * input(const char *name)
vector< ShaderOutput * > outputs
Definition: shader_graph.h:215
vector< ShaderInput * > inputs
Definition: shader_graph.h:214
ShaderOutput * output(const char *name)
void set_graph(ShaderGraph *graph)
void tag_update(Scene *scene)
void tag_used(Scene *scene)
void push_back_slow(const T &t)
bool add_or_update(T **r_data, const BL::ID &id)
Definition: id_map.h:100
void pre_sync()
Definition: id_map.h:71
T * find(const BL::ID &id)
Definition: id_map.h:41
void set_default(T *data)
Definition: id_map.h:142
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
static string image_user_file_path(BL::BlendData &data, BL::ImageUser &iuser, BL::Image &ima, int cfra)
static float4 get_float4(const BL::Array< float, 4 > &array)
static float get_float(PointerRNA &ptr, const char *name)
static void curvemapping_float_to_array(BL::CurveMapping &cumap, array< float > &data, int size)
static bool get_boolean(PointerRNA &ptr, const char *name)
static int get_int(PointerRNA &ptr, const char *name)
static string get_enum_identifier(PointerRNA &ptr, const char *name)
static string get_text_datablock_content(const PointerRNA &ptr)
static void colorramp_to_array(BL::ColorRamp &ramp, array< float3 > &ramp_color, array< float > &ramp_alpha, int size)
static float3 get_float3(const BL::Array< float, 2 > &array)
static int get_enum(PointerRNA &ptr, const char *name, int num_values=-1, int default_value=-1)
static void curvemapping_color_to_array(BL::CurveMapping &cumap, array< float3 > &data, int size, bool rgb_curve)
static string blender_absolute_path(BL::BlendData &b_data, BL::ID &b_id, const string &path)
static string get_string(PointerRNA &ptr, const char *name)
static Transform get_transform(const BL::Array< float, 16 > &array)
static void curvemapping_minmax(BL::CurveMapping &cumap, int num_curves, float *min_x, float *max_x)
BL::ShaderNodeAttribute::attribute_type_enum BlenderAttributeType
static int image_user_frame_number(BL::ImageUser &iuser, BL::Image &ima, int cfra)
OperationNode * node
Depsgraph * graph
Scene scene
Curve curve
#define function_bind
SyclQueue void void size_t num_bytes void
depth_tx normal_tx diffuse_light_tx specular_light_tx volume_light_tx environment_tx ambient_occlusion_tx aov_value_tx in_weight_img image(1, GPU_R32F, Qualifier::WRITE, ImageType::FLOAT_2D_ARRAY, "out_weight_img") .image(3
uint tex_coord
Definition: gpu_viewport.c:47
#define mix(a, b, c)
Definition: hash.h:17
ccl_device_inline Transform transform_translate(float3 t)
ccl_device_inline Transform transform_inverse(const Transform tfm)
ccl_device_inline Transform transform_scale(float3 s)
ccl_gpu_kernel_postfix ccl_global int ccl_global int int num_values
ccl_gpu_kernel_postfix ccl_global KernelWorkTile * tiles
ccl_global KernelShaderEvalInput ccl_global float * output
ccl_global KernelShaderEvalInput * input
NodeClampType
NodeEnvironmentProjection
NodeMathType
NodeMusgraveType
NodeMapRangeType
NodeWaveBandsDirection
NodeMappingType
NodeVectorTransformConvertSpace
NodeTangentAxis
NodePrincipledHairParametrization
@ NODE_PRINCIPLED_HAIR_NUM
@ NODE_PRINCIPLED_HAIR_REFLECTANCE
NodeVoronoiFeature
NodeWaveType
NodeVoronoiDistanceMetric
NodeSkyType
NodeWaveProfile
NodeTexVoxelSpace
@ NODE_MIX_MUL
@ CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID
@ CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ID
@ CLOSURE_BSSRDF_RANDOM_WALK_FIXED_RADIUS_ID
@ CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID
@ CLOSURE_BSSRDF_BURLEY_ID
@ CLOSURE_BSDF_DIFFUSE_TOON_ID
@ CLOSURE_BSDF_MICROFACET_GGX_ID
@ CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID
@ CLOSURE_BSDF_MICROFACET_MULTI_GGX_ID
@ CLOSURE_BSDF_HAIR_TRANSMISSION_ID
@ CLOSURE_BSDF_SHARP_GLASS_ID
@ CLOSURE_BSDF_MICROFACET_BECKMANN_GLASS_ID
@ CLOSURE_BSSRDF_RANDOM_WALK_ID
@ CLOSURE_BSDF_REFRACTION_ID
@ CLOSURE_BSDF_MICROFACET_BECKMANN_ID
@ CLOSURE_BSDF_MICROFACET_MULTI_GGX_GLASS_ID
@ CLOSURE_BSDF_GLOSSY_TOON_ID
@ CLOSURE_BSDF_HAIR_REFLECTION_ID
@ CLOSURE_BSDF_REFLECTION_ID
NodeVectorRotateType
NodeVectorTransformType
NodeImageProjection
NodeCombSepColorType
NodeGradientType
NodeTangentDirectionType
NodeVectorMathType
NodeWaveRingsDirection
NodeNormalMapSpace
@ PATH_RAY_TRANSMIT
Definition: kernel/types.h:196
@ PATH_RAY_VOLUME_SCATTER
Definition: kernel/types.h:201
@ PATH_RAY_GLOSSY
Definition: kernel/types.h:198
@ PATH_RAY_DIFFUSE
Definition: kernel/types.h:197
@ PATH_RAY_CAMERA
Definition: kernel/types.h:194
#define RAMP_TABLE_SIZE
Definition: kernel/types.h:27
ccl_device_inline float3 one_float3()
Definition: math_float3.h:89
ccl_device_inline float3 zero_float3()
Definition: math_float3.h:80
#define fmodf(x, y)
Definition: metal/compat.h:230
#define copysignf(x, y)
Definition: metal/compat.h:220
#define fabsf(x)
Definition: metal/compat.h:219
#define make_float3(x, y, z)
Definition: metal/compat.h:204
bool is_builtin(const void *UNUSED(owner), const AttributeIDRef &attribute_id)
vec_base< T, Size > normalize(const vec_base< T, Size > &v)
static const pxr::TfToken out("out", pxr::TfToken::Immortal)
static float noise(int n)
struct node_tree node_tree
PointerRNA RNA_pointer_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:5167
const PointerRNA PointerRNA_NULL
Definition: rna_access.c:61
void RNA_float_get_array(PointerRNA *ptr, const char *name, float *values)
Definition: rna_access.c:4980
float RNA_float_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:4957
VolumeInterpolation
Definition: scene/shader.h:45
@ VOLUME_NUM_INTERPOLATION
Definition: scene/shader.h:49
@ VOLUME_INTERPOLATION_LINEAR
Definition: scene/shader.h:46
DisplacementMethod
Definition: scene/shader.h:52
@ DISPLACE_NUM_METHODS
Definition: scene/shader.h:57
@ DISPLACE_BUMP
Definition: scene/shader.h:53
VolumeSampling
Definition: scene/shader.h:37
@ VOLUME_NUM_SAMPLING
Definition: scene/shader.h:42
@ VOLUME_SAMPLING_DISTANCE
Definition: scene/shader.h:38
@ SHADER_SPECIAL_TYPE_OSL
Definition: shader_graph.h:51
closure color principled_hair(normal N, color sigma, float roughnessu, float roughnessv, float coat, float alpha, float eta) BUILTIN
bool string_startswith(const string_view s, const string_view start)
Definition: string.cpp:100
void string_replace(string &haystack, const string &needle, const string &other)
Definition: string.cpp:130
bool string_endswith(const string_view s, const string_view end)
Definition: string.cpp:111
void set_value(const SocketType &input, const Node &other, const SocketType &other_input)
Definition: graph/node.cpp:388
ustring name
Definition: graph/node.h:174
Shader * default_surface
Definition: scene.h:232
ImageManager * image_manager
Definition: scene.h:222
Shader * default_background
Definition: scene.h:235
Background * background
Definition: scene.h:209
ShaderManager * shader_manager
Definition: scene.h:224
Integrator * integrator
Definition: scene.h:210
Shader * default_light
Definition: scene.h:234
void push(TaskRunFunction &&task)
Definition: task.cpp:23
void wait_work(Summary *stats=NULL)
Definition: task.cpp:29
void point_density_texture_space(BL::Depsgraph &b_depsgraph, BL::ShaderNodeTexPointDensity &b_point_density_node, float3 &loc, float3 &size)
Definition: texture.cpp:26
static int magic(const Tex *tex, const float texvec[3], TexResult *texres)
#define M_PI_2_F
Definition: util/math.h:37
#define M_2PI_F
Definition: util/math.h:60
#define M_PI_F
Definition: util/math.h:34
ccl_device_inline float3 float4_to_float3(const float4 a)
Definition: util/math.h:500
ImageAlphaType
Definition: util/texture.h:48
@ IMAGE_ALPHA_NUM_TYPES
Definition: util/texture.h:55
@ IMAGE_ALPHA_AUTO
Definition: util/texture.h:53
InterpolationType
Definition: util/texture.h:19
@ INTERPOLATION_LINEAR
Definition: util/texture.h:21
@ INTERPOLATION_NUM_TYPES
Definition: util/texture.h:26
ExtensionType
Definition: util/texture.h:61
@ EXTENSION_REPEAT
Definition: util/texture.h:63
@ EXTENSION_NUM_TYPES
Definition: util/texture.h:69
PointerRNA * ptr
Definition: wm_files.c:3480