[Bf-blender-cvs] [e7912dfa195] master: Attributes: Infrastructure for generic 8-bit integer data type

Hans Goudey noreply at git.blender.org
Fri Feb 4 17:29:20 CET 2022


Commit: e7912dfa1959be671f77e4e67eab01de9de86c77
Author: Hans Goudey
Date:   Fri Feb 4 10:29:11 2022 -0600
Branches: master
https://developer.blender.org/rBe7912dfa1959be671f77e4e67eab01de9de86c77

Attributes: Infrastructure for generic 8-bit integer data type

This commit adds infrastructure for 8 bit signed integer attributes.
This can be useful given the discussion in T94193, where we want to
store spline type, Bezier handle type, and other small enums as
attributes.

This is only exposed in the interface in the attribute lists, so it
shouldn't be an option in geometry nodes, at least for now.
I expect that this type won't be used directly very often, it
should mostly be cast to an enum type. However, with support
for 8 bit integers, it also makes sense to add things like mixing
implementations for consistency.

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

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

M	source/blender/blenkernel/BKE_attribute_math.hh
M	source/blender/blenkernel/intern/attribute_access.cc
M	source/blender/blenkernel/intern/attribute_access_intern.hh
M	source/blender/blenkernel/intern/customdata.cc
M	source/blender/blenkernel/intern/mesh.cc
M	source/blender/blenkernel/intern/type_conversions.cc
M	source/blender/draw/intern/draw_cache_impl_mesh.c
M	source/blender/draw/intern/mesh_extractors/extract_mesh_vbo_attributes.cc
M	source/blender/editors/space_spreadsheet/spreadsheet_layout.cc
M	source/blender/functions/intern/cpp_types.cc
M	source/blender/makesdna/DNA_customdata_types.h
M	source/blender/makesdna/DNA_meshdata_types.h
M	source/blender/makesrna/RNA_access.h
M	source/blender/makesrna/intern/rna_attribute.c

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

diff --git a/source/blender/blenkernel/BKE_attribute_math.hh b/source/blender/blenkernel/BKE_attribute_math.hh
index 90f349125c9..bf773cd6d75 100644
--- a/source/blender/blenkernel/BKE_attribute_math.hh
+++ b/source/blender/blenkernel/BKE_attribute_math.hh
@@ -50,6 +50,9 @@ inline void convert_to_static_type(const CustomDataType data_type, const Func &f
     case CD_PROP_BOOL:
       func(bool());
       break;
+    case CD_PROP_INT8:
+      func(int8_t());
+      break;
     case CD_PROP_COLOR:
       func(ColorGeometry4f());
       break;
@@ -77,6 +80,9 @@ inline void convert_to_static_type(const fn::CPPType &cpp_type, const Func &func
   else if (cpp_type.is<bool>()) {
     func(bool());
   }
+  else if (cpp_type.is<int8_t>()) {
+    func(int8_t());
+  }
   else if (cpp_type.is<ColorGeometry4f>()) {
     func(ColorGeometry4f());
   }
@@ -93,6 +99,12 @@ inline void convert_to_static_type(const fn::CPPType &cpp_type, const Func &func
 
 template<typename T> T mix3(const float3 &weights, const T &v0, const T &v1, const T &v2);
 
+template<>
+inline int8_t mix3(const float3 &weights, const int8_t &v0, const int8_t &v1, const int8_t &v2)
+{
+  return static_cast<int8_t>(weights.x * v0 + weights.y * v1 + weights.z * v2);
+}
+
 template<> inline bool mix3(const float3 &weights, const bool &v0, const bool &v1, const bool &v2)
 {
   return (weights.x * v0 + weights.y * v1 + weights.z * v2) >= 0.5f;
@@ -147,6 +159,11 @@ template<> inline bool mix2(const float factor, const bool &a, const bool &b)
   return ((1.0f - factor) * a + factor * b) >= 0.5f;
 }
 
+template<> inline int8_t mix2(const float factor, const int8_t &a, const int8_t &b)
+{
+  return static_cast<int8_t>((1.0f - factor) * a + factor * b);
+}
+
 template<> inline int mix2(const float factor, const int &a, const int &b)
 {
   return static_cast<int>((1.0f - factor) * a + factor * b);
@@ -364,6 +381,15 @@ template<> struct DefaultMixerStruct<bool> {
   using type = SimpleMixerWithAccumulationType<bool, float, float_to_bool>;
 };
 
+template<> struct DefaultMixerStruct<int8_t> {
+  static int8_t float_to_int8_t(const float &value)
+  {
+    return static_cast<int8_t>(value);
+  }
+  /* Store interpolated 8 bit integers in a float temporarily to increase accuracy. */
+  using type = SimpleMixerWithAccumulationType<int8_t, float, float_to_int8_t>;
+};
+
 template<typename T> struct DefaultPropatationMixerStruct {
   /* Use void by default. This can be checked for in `if constexpr` statements. */
   using type = typename DefaultMixerStruct<T>::type;
diff --git a/source/blender/blenkernel/intern/attribute_access.cc b/source/blender/blenkernel/intern/attribute_access.cc
index cc43a3e26a8..68ab11a013b 100644
--- a/source/blender/blenkernel/intern/attribute_access.cc
+++ b/source/blender/blenkernel/intern/attribute_access.cc
@@ -83,6 +83,8 @@ const blender::fn::CPPType *custom_data_type_to_cpp_type(const CustomDataType ty
       return &CPPType::get<ColorGeometry4f>();
     case CD_PROP_BOOL:
       return &CPPType::get<bool>();
+    case CD_PROP_INT8:
+      return &CPPType::get<int8_t>();
     default:
       return nullptr;
   }
@@ -109,6 +111,9 @@ CustomDataType cpp_type_to_custom_data_type(const blender::fn::CPPType &type)
   if (type.is<bool>()) {
     return CD_PROP_BOOL;
   }
+  if (type.is<int8_t>()) {
+    return CD_PROP_INT8;
+  }
   return static_cast<CustomDataType>(-1);
 }
 
@@ -117,16 +122,18 @@ static int attribute_data_type_complexity(const CustomDataType data_type)
   switch (data_type) {
     case CD_PROP_BOOL:
       return 0;
-    case CD_PROP_INT32:
+    case CD_PROP_INT8:
       return 1;
-    case CD_PROP_FLOAT:
+    case CD_PROP_INT32:
       return 2;
-    case CD_PROP_FLOAT2:
+    case CD_PROP_FLOAT:
       return 3;
-    case CD_PROP_FLOAT3:
+    case CD_PROP_FLOAT2:
       return 4;
-    case CD_PROP_COLOR:
+    case CD_PROP_FLOAT3:
       return 5;
+    case CD_PROP_COLOR:
+      return 6;
 #if 0 /* These attribute types are not supported yet. */
     case CD_MLOOPCOL:
       return 3;
diff --git a/source/blender/blenkernel/intern/attribute_access_intern.hh b/source/blender/blenkernel/intern/attribute_access_intern.hh
index 2cd128081eb..5341266e182 100644
--- a/source/blender/blenkernel/intern/attribute_access_intern.hh
+++ b/source/blender/blenkernel/intern/attribute_access_intern.hh
@@ -140,7 +140,8 @@ class CustomDataAttributeProvider final : public DynamicAttributesProvider {
  private:
   static constexpr uint64_t supported_types_mask = CD_MASK_PROP_FLOAT | CD_MASK_PROP_FLOAT2 |
                                                    CD_MASK_PROP_FLOAT3 | CD_MASK_PROP_INT32 |
-                                                   CD_MASK_PROP_COLOR | CD_MASK_PROP_BOOL;
+                                                   CD_MASK_PROP_COLOR | CD_MASK_PROP_BOOL |
+                                                   CD_MASK_PROP_INT8;
   const AttributeDomain domain_;
   const CustomDataAccessInfo custom_data_access_;
 
diff --git a/source/blender/blenkernel/intern/customdata.cc b/source/blender/blenkernel/intern/customdata.cc
index db30e223185..36d511422aa 100644
--- a/source/blender/blenkernel/intern/customdata.cc
+++ b/source/blender/blenkernel/intern/customdata.cc
@@ -1793,8 +1793,8 @@ static const LayerTypeInfo LAYERTYPEINFO[CD_NUMTYPES] = {
     {sizeof(float[3]), "vec3f", 1, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr},
     /* 44: CD_RADIUS */
     {sizeof(float), "MFloatProperty", 1, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr},
-    /* 45: CD_HAIRCURVE */ /* UNUSED */
-    {-1, "", 1, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr},
+    /* 45: CD_PROP_INT8 */
+    {sizeof(int8_t), "MInt8Property", 1, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr},
     /* 46: CD_HAIRMAPPING */ /* UNUSED */
     {-1, "", 1, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr},
     /* 47: CD_PROP_COLOR */
@@ -1914,7 +1914,7 @@ static const char *LAYERTYPENAMES[CD_NUMTYPES] = {
     "CDCustomLoopNormal",
     "CDSculptFaceGroups",
     /* 43-46 */ "CDHairPoint",
-    "CDHairCurve",
+    "CDPropInt8",
     "CDHairMapping",
     "CDPoint",
     "CDPropCol",
diff --git a/source/blender/blenkernel/intern/mesh.cc b/source/blender/blenkernel/intern/mesh.cc
index 73fe279552d..c1b1f62a881 100644
--- a/source/blender/blenkernel/intern/mesh.cc
+++ b/source/blender/blenkernel/intern/mesh.cc
@@ -685,6 +685,17 @@ static int customdata_compare(
           }
           break;
         }
+        case CD_PROP_INT8: {
+          const int8_t *l1_data = (int8_t *)l1->data;
+          const int8_t *l2_data = (int8_t *)l2->data;
+
+          for (int i = 0; i < total_length; i++) {
+            if (l1_data[i] != l2_data[i]) {
+              return MESHCMP_ATTRIBUTE_VALUE_MISMATCH;
+            }
+          }
+          break;
+        }
         case CD_PROP_BOOL: {
           const bool *l1_data = (bool *)l1->data;
           const bool *l2_data = (bool *)l2->data;
diff --git a/source/blender/blenkernel/intern/type_conversions.cc b/source/blender/blenkernel/intern/type_conversions.cc
index cb05337ef2a..b2011d2baf7 100644
--- a/source/blender/blenkernel/intern/type_conversions.cc
+++ b/source/blender/blenkernel/intern/type_conversions.cc
@@ -62,6 +62,11 @@ static bool float_to_bool(const float &a)
 {
   return a > 0.0f;
 }
+static int8_t float_to_int8(const float &a)
+{
+  return std::clamp(
+      a, float(std::numeric_limits<int8_t>::min()), float(std::numeric_limits<int8_t>::max()));
+}
 static ColorGeometry4f float_to_color(const float &a)
 {
   return ColorGeometry4f(a, a, a, 1.0f);
@@ -83,6 +88,10 @@ static bool float2_to_bool(const float2 &a)
 {
   return !is_zero_v2(a);
 }
+static int8_t float2_to_int8(const float2 &a)
+{
+  return float_to_int8((a.x + a.y) / 2.0f);
+}
 static ColorGeometry4f float2_to_color(const float2 &a)
 {
   return ColorGeometry4f(a.x, a.y, 0.0f, 1.0f);
@@ -92,6 +101,10 @@ static bool float3_to_bool(const float3 &a)
 {
   return !is_zero_v3(a);
 }
+static int8_t float3_to_int8(const float3 &a)
+{
+  return float_to_int8((a.x + a.y + a.z) / 3.0f);
+}
 static float float3_to_float(const float3 &a)
 {
   return (a.x + a.y + a.z) / 3.0f;
@@ -113,6 +126,11 @@ static bool int_to_bool(const int32_t &a)
 {
   return a > 0;
 }
+static int8_t int_to_int8(const int32_t &a)
+{
+  return std::clamp(
+      a, int(std::numeric_limits<int8_t>::min()), int(std::numeric_limits<int8_t>::max()));
+}
 static float int_to_float(const int32_t &a)
 {
   return (float)a;
@@ -130,10 +148,39 @@ static ColorGeometry4f int_to_color(const int32_t &a)
   return ColorGeometry4f((float)a, (float)a, (float)a, 1.0f);
 }
 
+static bool int8_to_bool(const int8_t &a)
+{
+  return a > 0;
+}
+static int int8_to_int(const int8_t &a)
+{
+  return static_cast<int>(a);
+}
+static float int8_to_float(const int8_t &a)
+{
+  return (float)a;
+}
+static float2 int8_to_float2(const int8_t &a)
+{
+  return float2((float)a);
+}
+static float3 int8_to_float3(const int8_t &a)
+{
+  return float3((float)a);
+}
+static ColorGeometry4f int8_to_color(const int8_t &a)
+{
+  return ColorGeometry4f((float)a, (float)a, (float)a, 1.0f);
+}
+
 static float bool_to_float(const bool &a)
 {
   return (bool)a;
 }
+static int8_t bool_to_int8(const bool &a)
+{
+  return static_cast<int8_t>(a);
+}
 static int32_t bool_to_int(const bool &a)
 {
   return (int32_t)a;
@@ -163,6 +210,10 @@ static int32_t color_to_int(const ColorGeometry4f &a)
 {
   return (int)rgb_to_grayscale(a);
 }
+static int8_t color_to_int8(const ColorGeometry4f &a)
+{
+  return int_to_int8(color_to_int(a));
+}
 static float2 color_to_float2(const ColorGeometry4f &a)
 {
   return float2(a.r, a.g);
@@ -180,33 +231,46 @@ static DataTypeConversions create_implicit_conversions()
   add_implicit_conversion<float, float3, float_to_float3>(conversions);
   add_implicit_conversion<float, int32_t, float_to_int>(conversions);
   add_implicit_conversion<float, bool, float_to_bool>(conversions);
+  add_implicit_conversion<float, int8_t, float_to_int8>(conversions);
   add_implicit_conversion<float, ColorGeometry4f, float_to_color>(conversions);
 
   add_implicit_conversion<float2, float3, float2_to_float3>(conversions);
   add_implicit_conversion<float

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list