[Bf-blender-cvs] [804ea8fe82a] functions: store and cache size per particle

Jacques Lucke noreply at git.blender.org
Wed Jul 10 12:00:55 CEST 2019


Commit: 804ea8fe82a1b2cb644408cf29bc1306a9856aeb
Author: Jacques Lucke
Date:   Wed Jul 10 10:26:48 2019 +0200
Branches: functions
https://developer.blender.org/rB804ea8fe82a1b2cb644408cf29bc1306a9856aeb

store and cache size per particle

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

M	source/blender/blenloader/intern/readfile.c
M	source/blender/blenloader/intern/writefile.c
M	source/blender/makesdna/DNA_modifier_types.h
M	source/blender/simulations/bparticles/c_wrapper.cpp
M	source/blender/simulations/bparticles/emitters.cpp
M	source/blender/simulations/bparticles/step_description.hpp

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

diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index 61855d8f14e..79ef8a81337 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -5821,6 +5821,16 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
                                             cached_type->particle_amount * 3);
             }
           }
+
+          for (uint i = 0; i < cached_type->num_attributes_float; i++) {
+            BParticlesAttributeCacheFloat *cached_attribute = &cached_type->attributes_float[i];
+            cached_attribute->values = newdataadr(fd, cached_attribute->values);
+
+            if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
+              BLI_endian_switch_float_array(cached_attribute->values,
+                                            cached_type->particle_amount);
+            }
+          }
         }
       }
     }
diff --git a/source/blender/blenloader/intern/writefile.c b/source/blender/blenloader/intern/writefile.c
index e5c6115b9a0..56218d7231b 100644
--- a/source/blender/blenloader/intern/writefile.c
+++ b/source/blender/blenloader/intern/writefile.c
@@ -1785,6 +1785,12 @@ static void write_modifiers(WriteData *wd, ListBase *modbase)
                       sizeof(float) * 3 * cached_type->particle_amount,
                       attribute_cache->values);
           }
+
+          for (uint i = 0; i < cached_type->num_attributes_float; i++) {
+            BParticlesAttributeCacheFloat *attribute_cache = &cached_type->attributes_float[i];
+            writedata(
+                wd, DATA, sizeof(float) * cached_type->particle_amount, attribute_cache->values);
+          }
         }
       }
     }
diff --git a/source/blender/makesdna/DNA_modifier_types.h b/source/blender/makesdna/DNA_modifier_types.h
index 77d93de2ac8..1c2457ad335 100644
--- a/source/blender/makesdna/DNA_modifier_types.h
+++ b/source/blender/makesdna/DNA_modifier_types.h
@@ -1968,11 +1968,21 @@ typedef struct BParticlesAttributeCacheFloat3 {
   float *values;
 } BParticlesAttributeCacheFloat3;
 
+typedef struct BParticlesAttributeCacheFloat {
+  char name[64];
+  float *values;
+} BParticlesAttributeCacheFloat;
+
 typedef struct BParticlesTypeCache {
   char name[64];
-  unsigned int num_attributes_float3;
   unsigned int particle_amount;
+  char _pad[4];
+
+  unsigned int num_attributes_float3;
+  unsigned int num_attributes_float;
+
   BParticlesAttributeCacheFloat3 *attributes_float3;
+  BParticlesAttributeCacheFloat *attributes_float;
 } BParticlesTypeCache;
 
 typedef struct BParticlesFrameCache {
diff --git a/source/blender/simulations/bparticles/c_wrapper.cpp b/source/blender/simulations/bparticles/c_wrapper.cpp
index 27d0bb35634..63fe4899e5f 100644
--- a/source/blender/simulations/bparticles/c_wrapper.cpp
+++ b/source/blender/simulations/bparticles/c_wrapper.cpp
@@ -131,7 +131,7 @@ static uint tetrahedon_edges[6][2] = {{0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2
 static void distribute_tetrahedons_range(Mesh *mesh,
                                          Range<uint> range,
                                          ArrayRef<float3> centers,
-                                         float scale)
+                                         ArrayRef<float> scales)
 {
   for (uint instance : range) {
     uint vertex_offset = instance * ARRAY_SIZE(tetrahedon_vertices);
@@ -141,7 +141,8 @@ static void distribute_tetrahedons_range(Mesh *mesh,
 
     float3 center = centers[instance];
     for (uint i = 0; i < ARRAY_SIZE(tetrahedon_vertices); i++) {
-      copy_v3_v3(mesh->mvert[vertex_offset + i].co, center + tetrahedon_vertices[i] * scale);
+      copy_v3_v3(mesh->mvert[vertex_offset + i].co,
+                 center + tetrahedon_vertices[i] * scales[instance]);
     }
 
     for (uint i = 0; i < ARRAY_SIZE(tetrahedon_loop_starts); i++) {
@@ -160,7 +161,7 @@ static void distribute_tetrahedons_range(Mesh *mesh,
   }
 }
 
-static Mesh *distribute_tetrahedons(ArrayRef<float3> centers, float scale)
+static Mesh *distribute_tetrahedons(ArrayRef<float3> centers, ArrayRef<float> scales)
 {
   uint amount = centers.size();
   Mesh *mesh = BKE_mesh_new_nomain(amount * ARRAY_SIZE(tetrahedon_vertices),
@@ -170,8 +171,8 @@ static Mesh *distribute_tetrahedons(ArrayRef<float3> centers, float scale)
                                    amount * ARRAY_SIZE(tetrahedon_loop_starts));
 
   BLI::Task::parallel_range(
-      Range<uint>(0, amount), 1000, [mesh, centers, scale](Range<uint> range) {
-        distribute_tetrahedons_range(mesh, range, centers, scale);
+      Range<uint>(0, amount), 1000, [mesh, centers, scales](Range<uint> range) {
+        distribute_tetrahedons_range(mesh, range, centers, scales);
       });
 
   return mesh;
@@ -196,6 +197,15 @@ void BParticles_modifier_free_cache(BParticlesModifierData *bpmd)
       if (cached_type.attributes_float3 != nullptr) {
         MEM_freeN(cached_type.attributes_float3);
       }
+      for (auto &cached_attribute :
+           BLI::ref_c_array(cached_type.attributes_float, cached_type.num_attributes_float)) {
+        if (cached_attribute.values != nullptr) {
+          MEM_freeN(cached_attribute.values);
+        }
+      }
+      if (cached_type.attributes_float != nullptr) {
+        MEM_freeN(cached_type.attributes_float);
+      }
     }
     if (cached_frame.particle_types != nullptr) {
       MEM_freeN(cached_frame.particle_types);
@@ -211,6 +221,7 @@ Mesh *BParticles_modifier_mesh_from_cache(BParticlesFrameCache *cached_frame)
   SCOPED_TIMER(__func__);
 
   SmallVector<float3> positions;
+  SmallVector<float> sizes;
   SmallVector<uint> particle_counts;
 
   for (uint i = 0; i < cached_frame->num_particle_types; i++) {
@@ -218,9 +229,10 @@ Mesh *BParticles_modifier_mesh_from_cache(BParticlesFrameCache *cached_frame)
     particle_counts.append(type.particle_amount);
     positions.extend(
         ArrayRef<float3>((float3 *)type.attributes_float3[0].values, type.particle_amount));
+    sizes.extend(ArrayRef<float>(type.attributes_float[0].values, type.particle_amount));
   }
 
-  Mesh *mesh = distribute_tetrahedons(positions, 0.025f);
+  Mesh *mesh = distribute_tetrahedons(positions, sizes);
   if (positions.size() == 0) {
     return mesh;
   }
@@ -267,15 +279,31 @@ void BParticles_modifier_cache_state(BParticlesModifierData *bpmd,
     strncpy(cached_type.name, container_names[i].data(), sizeof(cached_type.name));
     cached_type.particle_amount = container.count_active();
 
-    cached_type.num_attributes_float3 = 1;
-    cached_type.attributes_float3 = (BParticlesAttributeCacheFloat3 *)MEM_calloc_arrayN(
-        cached_type.num_attributes_float3, sizeof(BParticlesAttributeCacheFloat3), __func__);
+    /* Cache Position */
+    {
+      cached_type.num_attributes_float3 = 1;
+      cached_type.attributes_float3 = (BParticlesAttributeCacheFloat3 *)MEM_calloc_arrayN(
+          cached_type.num_attributes_float3, sizeof(BParticlesAttributeCacheFloat3), __func__);
+
+      BParticlesAttributeCacheFloat3 &cached_attribute = cached_type.attributes_float3[0];
+      strncpy(cached_attribute.name, "Position", sizeof(cached_attribute.name));
+      cached_attribute.values = (float *)MEM_malloc_arrayN(
+          cached_type.particle_amount, sizeof(float3), __func__);
+      container.flatten_attribute_data("Position", cached_attribute.values);
+    }
 
-    BParticlesAttributeCacheFloat3 &cached_attribute = cached_type.attributes_float3[0];
-    strncpy(cached_attribute.name, "Position", sizeof(cached_attribute.name));
-    cached_attribute.values = (float *)MEM_malloc_arrayN(
-        cached_type.particle_amount, sizeof(float3), __func__);
-    container.flatten_attribute_data("Position", cached_attribute.values);
+    /* Cache Size */
+    {
+      cached_type.num_attributes_float = 1;
+      cached_type.attributes_float = (BParticlesAttributeCacheFloat *)MEM_calloc_arrayN(
+          cached_type.num_attributes_float, sizeof(BParticlesAttributeCacheFloat), __func__);
+
+      BParticlesAttributeCacheFloat &cached_attribute = cached_type.attributes_float[0];
+      strncpy(cached_attribute.name, "Size", sizeof(cached_attribute.name));
+      cached_attribute.values = (float *)MEM_malloc_arrayN(
+          cached_type.particle_amount, sizeof(float), __func__);
+      container.flatten_attribute_data("Size", cached_attribute.values);
+    }
   }
 
   bpmd->cached_frames = (BParticlesFrameCache *)MEM_reallocN(
diff --git a/source/blender/simulations/bparticles/emitters.cpp b/source/blender/simulations/bparticles/emitters.cpp
index 5351211cc85..df1822c4a90 100644
--- a/source/blender/simulations/bparticles/emitters.cpp
+++ b/source/blender/simulations/bparticles/emitters.cpp
@@ -12,6 +12,11 @@
 
 namespace BParticles {
 
+static float random_float()
+{
+  return (rand() % 4096) / 4096.0f;
+}
+
 class PointEmitter : public Emitter {
  private:
   std::string m_particle_type_name;
@@ -88,11 +93,12 @@ class SurfaceEmitter : public Emitter {
 
     SmallVector<float3> positions;
     SmallVector<float3> velocities;
+    SmallVector<float> sizes;
     SmallVector<float> birth_moments;
 
     for (uint i = 0; i < particles_to_emit; i++) {
       MLoopTri triangle = triangles[rand() % triangle_amount];
-      float birth_moment = (rand() % 1000) / 1000.0f;
+      float birth_moment = random_float();
 
       float3 v1 = verts[loops[triangle.tri[0]].v].co;
       float3 v2 = verts[loops[triangle.tri[1]].v].co;
@@ -119,11 +125,13 @@ class SurfaceEmitter : public Emitter {
       velocities.append(normal_velocity * normal_velocity_factor +
                         emitter_velocity * emitter_velocity_factor);
       birth_moments.append(birth_moment);
+      sizes.append(random_float());
     }
 
     auto &target = interface.request(m_particle_type_name, positions.size());
     target.set_float3("Position", positions);
     target.set_float3("Velocity", velocities);
+    target.set_float("Size", sizes);
     target.set_birth_moments(birth_moments);
   }
 
@@ -134,8 +142,8 @@ class SurfaceEmitter : public Emitter {
     float rand1, rand2;
 
     do {
-      rand1 = (rand() % 1000) / 1000.0f;
-      rand2 = (rand() % 1000) /

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list