[Bf-blender-cvs] [155c1de9ad7] functions: improved particle attribute handling

Jacques Lucke noreply at git.blender.org
Mon Jun 17 15:52:52 CEST 2019


Commit: 155c1de9ad71b7d39d6ab374547d2194ccb47c29
Author: Jacques Lucke
Date:   Mon Jun 17 15:52:04 2019 +0200
Branches: functions
https://developer.blender.org/rB155c1de9ad71b7d39d6ab374547d2194ccb47c29

improved particle attribute handling

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

M	source/blender/simulations/CMakeLists.txt
A	source/blender/simulations/bparticles/attributes.cpp
A	source/blender/simulations/bparticles/attributes.hpp
M	source/blender/simulations/bparticles/c_wrapper.cpp
M	source/blender/simulations/bparticles/core.cpp
M	source/blender/simulations/bparticles/core.hpp
M	source/blender/simulations/bparticles/particles_container.cpp
M	source/blender/simulations/bparticles/particles_container.hpp
M	source/blender/simulations/bparticles/playground_solver.cpp

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

diff --git a/source/blender/simulations/CMakeLists.txt b/source/blender/simulations/CMakeLists.txt
index 5dd72a125fc..ba0bbf23d0e 100644
--- a/source/blender/simulations/CMakeLists.txt
+++ b/source/blender/simulations/CMakeLists.txt
@@ -23,6 +23,8 @@ set(SRC
   bparticles/playground_solver.cpp
   bparticles/emitter.hpp
   bparticles/emitter.cpp
+  bparticles/attributes.hpp
+  bparticles/attributes.cpp
   bparticles/c_wrapper.cpp
 )
 
diff --git a/source/blender/simulations/bparticles/attributes.cpp b/source/blender/simulations/bparticles/attributes.cpp
new file mode 100644
index 00000000000..6fb572bbb37
--- /dev/null
+++ b/source/blender/simulations/bparticles/attributes.cpp
@@ -0,0 +1,34 @@
+#include "attributes.hpp"
+
+namespace BParticles {
+
+AttributesInfo::AttributesInfo(ArrayRef<std::string> byte_names,
+                               ArrayRef<std::string> float_names,
+                               ArrayRef<std::string> float3_names)
+{
+  m_indices = {};
+  m_indices.add_multiple(byte_names);
+  m_indices.add_multiple(float_names);
+  m_indices.add_multiple(float3_names);
+  BLI_assert(m_indices.size() == byte_names.size() + float_names.size() + float3_names.size());
+
+  m_byte_attributes = Range<uint>(0, byte_names.size());
+  m_float_attributes = m_byte_attributes.after(float_names.size());
+  m_float3_attributes = m_float_attributes.after(float3_names.size());
+
+  m_types = {};
+  m_types.append_n_times(AttributeType::Byte, m_byte_attributes.size());
+  m_types.append_n_times(AttributeType::Float, m_float_attributes.size());
+  m_types.append_n_times(AttributeType::Float3, m_float3_attributes.size());
+}
+
+SmallVector<void *> AttributesInfo::allocate_separate_arrays(uint size) const
+{
+  SmallVector<void *> pointers;
+  for (AttributeType type : m_types) {
+    pointers.append(MEM_malloc_arrayN(size, size_of_attribute_type(type), __func__));
+  }
+  return pointers;
+}
+
+};  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/attributes.hpp b/source/blender/simulations/bparticles/attributes.hpp
new file mode 100644
index 00000000000..05f307e7c70
--- /dev/null
+++ b/source/blender/simulations/bparticles/attributes.hpp
@@ -0,0 +1,248 @@
+#pragma once
+
+#include <string>
+
+#include "BLI_array_ref.hpp"
+#include "BLI_small_vector.hpp"
+#include "BLI_math.hpp"
+#include "BLI_string_ref.hpp"
+#include "BLI_range.hpp"
+#include "BLI_small_set_vector.hpp"
+
+namespace BParticles {
+
+using BLI::ArrayRef;
+using BLI::float3;
+using BLI::Range;
+using BLI::SmallSetVector;
+using BLI::SmallVector;
+using BLI::StringRef;
+using BLI::StringRefNull;
+
+enum AttributeType {
+  Byte,
+  Float,
+  Float3,
+};
+
+inline uint size_of_attribute_type(AttributeType type)
+{
+  switch (type) {
+    case AttributeType::Byte:
+      return sizeof(uint8_t);
+    case AttributeType::Float:
+      return sizeof(float);
+    case AttributeType::Float3:
+      return sizeof(float3);
+    default:
+      BLI_assert(false);
+      return 0;
+  };
+}
+
+class AttributesInfo {
+ private:
+  Range<uint> m_byte_attributes;
+  Range<uint> m_float_attributes;
+  Range<uint> m_float3_attributes;
+  SmallVector<AttributeType> m_types;
+  SmallSetVector<std::string> m_indices;
+
+ public:
+  AttributesInfo() = default;
+  AttributesInfo(ArrayRef<std::string> byte_names,
+                 ArrayRef<std::string> float_names,
+                 ArrayRef<std::string> float3_names);
+
+  uint amount() const
+  {
+    return m_indices.size();
+  }
+
+  StringRefNull name_of(uint index) const
+  {
+    return m_indices.values()[index];
+  }
+
+  AttributeType type_of(uint index) const
+  {
+    return m_types[index];
+  }
+
+  uint attribute_index(StringRef name) const
+  {
+    int index = m_indices.index(name.to_std_string());
+    BLI_assert(index >= 0);
+    return (uint)index;
+  }
+
+  Range<uint> byte_attributes() const
+  {
+    return m_byte_attributes;
+  }
+
+  Range<uint> float_attributes() const
+  {
+    return m_float_attributes;
+  }
+
+  Range<uint> float3_attributes() const
+  {
+    return m_float3_attributes;
+  }
+
+  SmallVector<void *> allocate_separate_arrays(uint size) const;
+};
+
+class AttributeArrays;
+
+class AttributeArraysCore {
+ private:
+  AttributesInfo &m_info;
+  SmallVector<void *> m_arrays;
+  uint m_size;
+
+ public:
+  AttributeArraysCore() = default;
+
+  AttributeArraysCore(AttributesInfo &info, ArrayRef<void *> arrays);
+
+  AttributesInfo &info();
+  void *get_ptr(uint index);
+  AttributeType get_type(uint index);
+  AttributeArrays slice_all();
+  uint size() const;
+  ArrayRef<void *> pointers();
+};
+
+class AttributeArrays {
+ private:
+  AttributeArraysCore &m_core;
+  uint m_start, m_size;
+
+ public:
+  AttributeArrays(AttributeArraysCore &core, uint start, uint size);
+
+  uint size() const;
+  AttributesInfo &info();
+
+  uint attribute_index(StringRef name);
+
+  ArrayRef<uint8_t> get_byte(uint index) const;
+  ArrayRef<uint8_t> get_byte(StringRef name);
+  ArrayRef<float> get_float(uint index) const;
+  ArrayRef<float> get_float(StringRef name);
+  ArrayRef<float3> get_float3(uint index) const;
+  ArrayRef<float3> get_float3(StringRef name);
+
+  AttributeArrays slice(uint start, uint size) const;
+  AttributeArrays take_front(uint n) const;
+};
+
+/* Attribute Arrays Core
+ *****************************************/
+
+inline AttributeArraysCore::AttributeArraysCore(AttributesInfo &info, ArrayRef<void *> arrays)
+    : m_info(info), m_arrays(arrays.to_small_vector())
+{
+}
+
+inline AttributesInfo &AttributeArraysCore::info()
+{
+  return m_info;
+}
+
+inline void *AttributeArraysCore::get_ptr(uint index)
+{
+  return m_arrays[index];
+}
+
+inline AttributeType AttributeArraysCore::get_type(uint index)
+{
+  return m_info.type_of(index);
+}
+
+inline AttributeArrays AttributeArraysCore::slice_all()
+{
+  return AttributeArrays(*this, 0, m_size);
+}
+
+inline uint AttributeArraysCore::size() const
+{
+  return m_size;
+}
+
+inline ArrayRef<void *> AttributeArraysCore::pointers()
+{
+  return m_arrays;
+}
+
+/* Attribute Arrays
+ ******************************************/
+
+inline AttributeArrays::AttributeArrays(AttributeArraysCore &core, uint start, uint size)
+    : m_core(core), m_start(start), m_size(size)
+{
+  BLI_assert(m_start + m_size <= m_core.size());
+}
+
+inline uint AttributeArrays::size() const
+{
+  return m_size;
+}
+
+inline AttributesInfo &AttributeArrays::info()
+{
+  return m_core.info();
+}
+
+inline uint AttributeArrays::attribute_index(StringRef name)
+{
+  return this->info().attribute_index(name);
+}
+
+inline ArrayRef<uint8_t> AttributeArrays::get_byte(uint index) const
+{
+  BLI_assert(m_core.get_type(index) == AttributeType::Byte);
+  return ArrayRef<uint8_t>((uint8_t *)m_core.get_ptr(index) + m_start, m_size);
+}
+
+inline ArrayRef<uint8_t> AttributeArrays::get_byte(StringRef name)
+{
+  return this->get_byte(this->attribute_index(name));
+}
+
+inline ArrayRef<float> AttributeArrays::get_float(uint index) const
+{
+  BLI_assert(m_core.get_type(index) == AttributeType::Float);
+  return ArrayRef<float>((float *)m_core.get_ptr(index) + m_start, m_size);
+}
+
+inline ArrayRef<float> AttributeArrays::get_float(StringRef name)
+{
+  return this->get_float(this->attribute_index(name));
+}
+
+inline ArrayRef<float3> AttributeArrays::get_float3(uint index) const
+{
+  BLI_assert(m_core.get_type(index) == AttributeType::Float3);
+  return ArrayRef<float3>((float3 *)m_core.get_ptr(index) + m_start, m_size);
+}
+
+inline ArrayRef<float3> AttributeArrays::get_float3(StringRef name)
+{
+  return this->get_float3(this->attribute_index(name));
+}
+
+inline AttributeArrays AttributeArrays::slice(uint start, uint size) const
+{
+  return AttributeArrays(m_core, m_start + start, size);
+}
+
+inline AttributeArrays AttributeArrays::take_front(uint n) const
+{
+  BLI_assert(n <= m_size);
+  return AttributeArrays(m_core, m_start, n);
+}
+
+}  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/c_wrapper.cpp b/source/blender/simulations/bparticles/c_wrapper.cpp
index 0eaebf01d92..ea752320f9d 100644
--- a/source/blender/simulations/bparticles/c_wrapper.cpp
+++ b/source/blender/simulations/bparticles/c_wrapper.cpp
@@ -15,10 +15,10 @@
     return (T2)value; \
   }
 
+using BParticles::AttributeArrays;
 using BParticles::Description;
 using BParticles::EmitterBuffers;
 using BParticles::EmitterInfoBuilder;
-using BParticles::NamedBuffers;
 using BParticles::ParticlesBlock;
 using BParticles::Solver;
 using BParticles::StateBase;
@@ -42,7 +42,7 @@ class TestForce : public BParticles::Force {
   {
   }
 
-  void add_force(NamedBuffers &UNUSED(buffers), ArrayRef<float3> dst) override
+  void add_force(AttributeArrays &UNUSED(buffers), ArrayRef<float3> dst) override
   {
     for (uint i = 0; i < dst.size(); i++) {
       dst[i].z += m_strength;
@@ -59,7 +59,7 @@ class TurbulenceForce : public BParticles::Force {
   {
   }
 
-  void add_force(NamedBuffers &buffers, ArrayRef<float3> dst) override
+  void add_force(AttributeArrays &buffers, ArrayRef<float3> dst) override
   {
     auto positions = buffers.get_float3("Position");
     for (uint i = 0; i < dst.size(); i++) {
diff --git a/source/blender/simulations/bparticles/core.cpp b/source/blender/simulations/bparticles/core.cpp
index 51cf9d3bee7..44c01dd7499 100644
--- a/source/blender/simulations/bparticles/core.cpp
+++ b/source/blender/simulations/bparticles/core.cpp
@@ -2,10 +2,6 @@
 
 namespace BParticles {
 
-NamedBuffers::~NamedBuffers()
-{
-}
-
 Description::~Description()
 {
   for (Force *force : m_forces) {
diff --git a/source/blender/simulations/bparticles/core.hpp b/source/blender/simulations/bparticles/core.hpp
index 057090a2d9b..148c229d6d0 100644
--- a/source/blender/simulations/bparticles/core.hpp
+++ b/source/blender/simulations/bparticles/core.hpp
@@ -9,6 +9,8 @@
 #include "BLI_utildefines.h"
 #include "BLI_string_ref.hpp"
 
+#include "attributes.hpp"
+
 namespace BParticles {
 class Description;
 class Solver;
@@ -25,35 +27,26 @@ using BLI::SmallVector;
 using BLI::StringRef;
 using std::unique_ptr;
 
-class NamedBuffers {
- public:
-  virtual ~NamedBuffers();
-  virtual uint size() = 0;
-  virtual A

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list