[Bf-blender-cvs] [554afabf758] master: Attribute Node: refactor lookup to remove duplication between engines.

Alexander Gavrilov noreply at git.blender.org
Fri Sep 30 19:10:29 CEST 2022


Commit: 554afabf7580d60cc1990bb9df9d9940cb1fb1c3
Author: Alexander Gavrilov
Date:   Fri Sep 30 18:54:26 2022 +0300
Branches: master
https://developer.blender.org/rB554afabf7580d60cc1990bb9df9d9940cb1fb1c3

Attribute Node: refactor lookup to remove duplication between engines.

Currently lookup of Object and Instancer attributes is completely
duplicated between Cycles, Eevee and Eevee Next. This is bad design,
so this patch aims to deduplicate it by introducing a common API
in blenkernel.

In case of Cycles this requires certain hacks, but according to
Brecht it is planned to be rewritten later for more direct access
to internal Blender data anyway.

Differential Revision: https://developer.blender.org/D16117

===================================================================

M	intern/cycles/blender/CMakeLists.txt
M	intern/cycles/blender/object.cpp
M	source/blender/blenkernel/BKE_duplilist.h
M	source/blender/blenkernel/intern/object_dupli.cc
M	source/blender/draw/intern/draw_instance_data.c
M	source/blender/draw/intern/draw_resource.cc
M	source/blender/draw/intern/draw_shader_shared.h
M	source/blender/gpu/GPU_material.h
M	source/blender/gpu/intern/gpu_node_graph.c
M	source/blender/makesrna/intern/rna_depsgraph.c

===================================================================

diff --git a/intern/cycles/blender/CMakeLists.txt b/intern/cycles/blender/CMakeLists.txt
index 666b0077a72..ceb024ba5e7 100644
--- a/intern/cycles/blender/CMakeLists.txt
+++ b/intern/cycles/blender/CMakeLists.txt
@@ -7,6 +7,7 @@ set(INC
   ../../mikktspace
   ../../../source/blender/makesdna
   ../../../source/blender/makesrna
+  ../../../source/blender/blenkernel
   ../../../source/blender/blenlib
   ../../../source/blender/gpu
   ../../../source/blender/render
diff --git a/intern/cycles/blender/object.cpp b/intern/cycles/blender/object.cpp
index 109408c354d..8a3c1136104 100644
--- a/intern/cycles/blender/object.cpp
+++ b/intern/cycles/blender/object.cpp
@@ -23,6 +23,8 @@
 #include "util/log.h"
 #include "util/task.h"
 
+#include "BKE_duplilist.h"
+
 CCL_NAMESPACE_BEGIN
 
 /* Utilities */
@@ -353,79 +355,26 @@ Object *BlenderSync::sync_object(BL::Depsgraph &b_depsgraph,
   return object;
 }
 
-/* This function mirrors drw_uniform_property_lookup in draw_instance_data.cpp */
-static bool lookup_property(BL::ID b_id, const string &name, float4 *r_value)
-{
-  PointerRNA ptr;
-  PropertyRNA *prop;
-
-  if (!RNA_path_resolve(&b_id.ptr, name.c_str(), &ptr, &prop)) {
-    return false;
-  }
-
-  if (prop == NULL) {
-    return false;
-  }
+extern "C" DupliObject *rna_hack_DepsgraphObjectInstance_dupli_object_get(PointerRNA *ptr);
 
-  PropertyType type = RNA_property_type(prop);
-  int arraylen = RNA_property_array_length(&ptr, prop);
-
-  if (arraylen == 0) {
-    float value;
-
-    if (type == PROP_FLOAT)
-      value = RNA_property_float_get(&ptr, prop);
-    else if (type == PROP_INT)
-      value = static_cast<float>(RNA_property_int_get(&ptr, prop));
-    else
-      return false;
-
-    *r_value = make_float4(value, value, value, 1.0f);
-    return true;
-  }
-  else if (type == PROP_FLOAT && arraylen <= 4) {
-    *r_value = make_float4(0.0f, 0.0f, 0.0f, 1.0f);
-    RNA_property_float_get_array(&ptr, prop, &r_value->x);
-    return true;
-  }
-
-  return false;
-}
-
-/* This function mirrors drw_uniform_attribute_lookup in draw_instance_data.cpp */
 static float4 lookup_instance_property(BL::DepsgraphObjectInstance &b_instance,
                                        const string &name,
                                        bool use_instancer)
 {
-  string idprop_name = string_printf("[\"%s\"]", name.c_str());
-  float4 value;
+  ::Object *ob = (::Object *)b_instance.object().ptr.data;
+  ::DupliObject *dupli = nullptr;
+  ::Object *dupli_parent = nullptr;
 
   /* If requesting instance data, check the parent particle system and object. */
   if (use_instancer && b_instance.is_instance()) {
-    BL::ParticleSystem b_psys = b_instance.particle_system();
-
-    if (b_psys) {
-      if (lookup_property(b_psys.settings(), idprop_name, &value) ||
-          lookup_property(b_psys.settings(), name, &value)) {
-        return value;
-      }
-    }
-    if (lookup_property(b_instance.parent(), idprop_name, &value) ||
-        lookup_property(b_instance.parent(), name, &value)) {
-      return value;
-    }
+    dupli = rna_hack_DepsgraphObjectInstance_dupli_object_get(&b_instance.ptr);
+    dupli_parent = (::Object *)b_instance.parent().ptr.data;
   }
 
-  /* Check the object and mesh. */
-  BL::Object b_ob = b_instance.object();
-  BL::ID b_data = b_ob.data();
-
-  if (lookup_property(b_ob, idprop_name, &value) || lookup_property(b_ob, name, &value) ||
-      lookup_property(b_data, idprop_name, &value) || lookup_property(b_data, name, &value)) {
-    return value;
-  }
+  float4 value;
+  BKE_object_dupli_find_rgba_attribute(ob, dupli, dupli_parent, name.c_str(), &value.x);
 
-  return zero_float4();
+  return value;
 }
 
 bool BlenderSync::sync_object_attributes(BL::DepsgraphObjectInstance &b_instance, Object *object)
diff --git a/source/blender/blenkernel/BKE_duplilist.h b/source/blender/blenkernel/BKE_duplilist.h
index cc362495a24..79f37d2edde 100644
--- a/source/blender/blenkernel/BKE_duplilist.h
+++ b/source/blender/blenkernel/BKE_duplilist.h
@@ -64,6 +64,14 @@ typedef struct DupliObject {
   unsigned int random_id;
 } DupliObject;
 
+/** Look up the RGBA value of a uniform shader attribute.
+ *  \return true if the attribute was found; if not, r_value is also set to zero. */
+bool BKE_object_dupli_find_rgba_attribute(struct Object *ob,
+                                          struct DupliObject *dupli,
+                                          struct Object *dupli_parent,
+                                          const char *name,
+                                          float r_value[4]);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/source/blender/blenkernel/intern/object_dupli.cc b/source/blender/blenkernel/intern/object_dupli.cc
index 15a4a0bbb32..b4ccef56ee3 100644
--- a/source/blender/blenkernel/intern/object_dupli.cc
+++ b/source/blender/blenkernel/intern/object_dupli.cc
@@ -58,6 +58,9 @@
 #include "BLI_hash.h"
 
 #include "NOD_geometry_nodes_log.hh"
+#include "RNA_access.h"
+#include "RNA_path.h"
+#include "RNA_types.h"
 
 using blender::Array;
 using blender::float3;
@@ -1711,3 +1714,120 @@ void free_object_duplilist(ListBase *lb)
 }
 
 /** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Uniform attribute lookup
+ * \{ */
+
+/** Lookup an arbitrary RNA property and convert it to RGBA if possible. */
+static bool find_rna_property_rgba(PointerRNA *id_ptr, const char *name, float r_data[4])
+{
+  if (id_ptr->data == nullptr) {
+    return false;
+  }
+
+  /* First, check custom properties. */
+  IDProperty *group = RNA_struct_idprops(id_ptr, false);
+  PropertyRNA *prop = nullptr;
+
+  if (group && group->type == IDP_GROUP) {
+    prop = (PropertyRNA *)IDP_GetPropertyFromGroup(group, name);
+  }
+
+  /* If not found, do full path lookup. */
+  PointerRNA ptr;
+
+  if (prop != nullptr) {
+    ptr = *id_ptr;
+  }
+  else if (!RNA_path_resolve(id_ptr, name, &ptr, &prop)) {
+    return false;
+  }
+
+  if (prop == nullptr) {
+    return false;
+  }
+
+  /* Convert the value to RGBA if possible. */
+  PropertyType type = RNA_property_type(prop);
+  int array_len = RNA_property_array_length(&ptr, prop);
+
+  if (array_len == 0) {
+    float value;
+
+    if (type == PROP_FLOAT) {
+      value = RNA_property_float_get(&ptr, prop);
+    }
+    else if (type == PROP_INT) {
+      value = static_cast<float>(RNA_property_int_get(&ptr, prop));
+    }
+    else if (type == PROP_BOOLEAN) {
+      value = RNA_property_boolean_get(&ptr, prop) ? 1.0f : 0.0f;
+    }
+    else {
+      return false;
+    }
+
+    copy_v4_fl4(r_data, value, value, value, 1);
+    return true;
+  }
+
+  if (type == PROP_FLOAT && array_len <= 4) {
+    copy_v4_fl4(r_data, 0, 0, 0, 1);
+    RNA_property_float_get_array(&ptr, prop, r_data);
+    return true;
+  }
+
+  if (type == PROP_INT && array_len <= 4) {
+    int tmp[4] = {0, 0, 0, 1};
+    RNA_property_int_get_array(&ptr, prop, tmp);
+    for (int i = 0; i < 4; i++) {
+      r_data[i] = static_cast<float>(tmp[i]);
+    }
+    return true;
+  }
+
+  return false;
+}
+
+static bool find_rna_property_rgba(ID *id, const char *name, float r_data[4])
+{
+  PointerRNA ptr;
+  RNA_id_pointer_create(id, &ptr);
+  return find_rna_property_rgba(&ptr, name, r_data);
+}
+
+bool BKE_object_dupli_find_rgba_attribute(
+    Object *ob, DupliObject *dupli, Object *dupli_parent, const char *name, float r_value[4])
+{
+  /* Check the dupli particle system. */
+  if (dupli && dupli->particle_system) {
+    ParticleSettings *settings = dupli->particle_system->part;
+
+    if (find_rna_property_rgba(&settings->id, name, r_value)) {
+      return true;
+    }
+  }
+
+  /* Check the dupli parent object. */
+  if (dupli_parent && find_rna_property_rgba(&dupli_parent->id, name, r_value)) {
+    return true;
+  }
+
+  /* Check the main object. */
+  if (ob) {
+    if (find_rna_property_rgba(&ob->id, name, r_value)) {
+      return true;
+    }
+
+    /* Check the main object data (e.g. mesh). */
+    if (ob->data && find_rna_property_rgba((ID *)ob->data, name, r_value)) {
+      return true;
+    }
+  }
+
+  copy_v4_fl(r_value, 0.0f);
+  return false;
+}
+
+/** \} */
diff --git a/source/blender/draw/intern/draw_instance_data.c b/source/blender/draw/intern/draw_instance_data.c
index 7ed5ca7c983..a56883ce304 100644
--- a/source/blender/draw/intern/draw_instance_data.c
+++ b/source/blender/draw/intern/draw_instance_data.c
@@ -585,87 +585,18 @@ static DRWUniformAttrBuf *drw_uniform_attrs_pool_ensure(GHash *table,
   return (DRWUniformAttrBuf *)*pval;
 }
 
-/* This function mirrors lookup_property in cycles/blender/blender_object.cpp */
-static bool drw_uniform_property_lookup(ID *id, const char *name, float r_data[4])
-{
-  PointerRNA ptr, id_ptr;
-  PropertyRNA *prop;
-
-  if (!id) {
-    return false;
-  }
-
-  RNA_id_pointer_create(id, &id_ptr);
-
-  if (!RNA_path_resolve(&id_ptr, name, &ptr, &prop)) {
-    return false;
-  }
-
-  if (prop == NULL) {
-    return false;
-  }
-
-  PropertyType type = RNA_property_type(prop);
-  int arraylen = RNA_property_array_length(&ptr, prop);
-
-  if (arraylen == 0) {
-    float value;
-
-    if (type == PROP_FLOAT) {
-      value = RNA_property_float_get(&ptr, prop);
-    }
-    else if (type == PROP_INT) {
-      value = RNA_property_int_get(&ptr, prop);
-    }
-    else {
-      return false;
-    }
-
-    copy_v4_fl4(r_data, value, value, value, 1);
-    return true;
-  }
-
-  if (type == PROP_FLOAT && arraylen <= 4) {
-    copy_v4_fl4(r_data, 0, 0, 0, 1);
-    RNA_property_float_get_array(&ptr, prop, r_data);
-    return true;
-  }
-
-  return false;
-}
-
-/* This function mirrors lookup_instance_property in cycles/blender/blender_object.cpp */
 static void drw_uniform_attribute_lookup(GPUUniformAttr *attr,
                                          Object *ob,
                                          Object *dupli_parent,
                                          DupliObject *dupli_source,
                                          float r_data[4])
 {
-  copy_v4_fl(r_data, 0);
-
   /* If requesting instance data, check the parent particle system and object. */
   if (attr->use_dupli) {
-    if (dupli_source && dupli_source->particle_system) {
-      P

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list