diff options
author | Jacques Lucke <mail@jlucke.com> | 2019-12-31 18:48:01 +0300 |
---|---|---|
committer | Jacques Lucke <mail@jlucke.com> | 2019-12-31 18:48:01 +0300 |
commit | 0099355bc6bc1794d04fbcafb7a269bf593e728c (patch) | |
tree | 2995efa6ece94894f6fb483b5867d0630d1c2d9c /source/blender/simulations/bparticles | |
parent | 799fb759a7fb5efc1993b493dc2a49293c4f7f82 (diff) |
continue with ParticleSet
Diffstat (limited to 'source/blender/simulations/bparticles')
9 files changed, 237 insertions, 270 deletions
diff --git a/source/blender/simulations/bparticles/c_wrapper.cpp b/source/blender/simulations/bparticles/c_wrapper.cpp index 139d62cea10..b48ecd90a2d 100644 --- a/source/blender/simulations/bparticles/c_wrapper.cpp +++ b/source/blender/simulations/bparticles/c_wrapper.cpp @@ -74,12 +74,9 @@ void BParticles_simulate_modifier(BParticlesModifierData *bpmd, simulation_state.time().end_update(); auto &containers = simulation_state.particles().particle_containers(); - containers.foreach_key_value_pair( - [](StringRefNull system_name, AttributesBlockContainer *container) { - std::cout << "Particle System: " << system_name << "\n"; - std::cout << " Particles: " << container->count_active() << "\n"; - std::cout << " Blocks: " << container->active_blocks().size() << "\n"; - }); + containers.foreach_key_value_pair([](StringRefNull system_name, ParticleSet *particles) { + std::cout << "Particle System: " << system_name << ": " << particles->size() << "\n"; + }); } static float3 tetrahedon_vertices[4] = { @@ -208,13 +205,13 @@ Mesh *BParticles_modifier_point_mesh_from_state(BParticlesSimulationState state_ { SimulationState &state = *unwrap(state_c); - Vector<float3> positions; - state.particles().particle_containers().foreach_value( - [&positions](AttributesBlockContainer *container) { - positions.extend(container->flatten_attribute<float3>("Position")); - }); + Vector<float3> all_positions; + state.particles().particle_containers().foreach_value([&](ParticleSet *particles) { + ArrayRef<float3> positions = particles->attributes().get<float3>("Position"); + all_positions.extend(positions); + }); - return distribute_points(positions); + return distribute_points(all_positions); } Mesh *BParticles_modifier_mesh_from_state(BParticlesSimulationState state_c) @@ -226,10 +223,11 @@ Mesh *BParticles_modifier_mesh_from_state(BParticlesSimulationState state_c) Vector<rgba_f> colors; state.particles().particle_containers().foreach_value( - [&positions, &colors, &sizes](AttributesBlockContainer *container) { - positions.extend(container->flatten_attribute<float3>("Position")); - colors.extend(container->flatten_attribute<rgba_f>("Color")); - sizes.extend(container->flatten_attribute<float>("Size")); + [&positions, &colors, &sizes](ParticleSet *particles) { + AttributesRef attributes = particles->attributes(); + positions.extend(attributes.get<float3>("Position")); + colors.extend(attributes.get<rgba_f>("Color")); + sizes.extend(attributes.get<float>("Size")); }); Mesh *mesh = distribute_tetrahedons(positions, sizes, colors); @@ -259,17 +257,17 @@ Mesh *BParticles_state_extract_type__tetrahedons(BParticlesSimulationState simul const char *particle_type) { SimulationState &state = *unwrap(simulation_state_c); - ParticlesState &particles = state.particles(); - AttributesBlockContainer **container_ptr = particles.particle_containers().lookup_ptr( - particle_type); - if (container_ptr == nullptr) { + ParticlesState &particles_state = state.particles(); + ParticleSet **particles_ptr = particles_state.particle_containers().lookup_ptr(particle_type); + if (particles_ptr == nullptr) { return BKE_mesh_new_nomain(0, 0, 0, 0, 0); } - AttributesBlockContainer &container = **container_ptr; + ParticleSet &particles = **particles_ptr; - auto positions = container.flatten_attribute<float3>("Position"); - auto sizes = container.flatten_attribute<float>("Size"); - auto colors = container.flatten_attribute<rgba_f>("Color"); + AttributesRef attributes = particles.attributes(); + auto positions = attributes.get<float3>("Position"); + auto sizes = attributes.get<float>("Size"); + auto colors = attributes.get<rgba_f>("Color"); return distribute_tetrahedons(positions, sizes, colors); } @@ -278,15 +276,15 @@ Mesh *BParticles_state_extract_type__points(BParticlesSimulationState simulation const char *particle_type) { SimulationState &state = *unwrap(simulation_state_c); - ParticlesState &particles = state.particles(); - AttributesBlockContainer *container_ptr = particles.particle_containers().lookup_default( - particle_type, nullptr); - if (container_ptr == nullptr) { + ParticlesState &particles_state = state.particles(); + ParticleSet *particles_ptr = particles_state.particle_containers().lookup_default(particle_type, + nullptr); + if (particles_ptr == nullptr) { return BKE_mesh_new_nomain(0, 0, 0, 0, 0); } - AttributesBlockContainer &container = *container_ptr; + ParticleSet &particles = *particles_ptr; - auto positions = container.flatten_attribute<float3>("Position"); + auto positions = particles.attributes().get<float3>("Position"); return distribute_points(positions); } @@ -296,28 +294,28 @@ void BParticles_modifier_cache_state(BParticlesModifierData *bpmd, { SimulationState &state = *unwrap(state_c); - Vector<std::string> container_names; - Vector<AttributesBlockContainer *> containers; + Vector<std::string> system_names; + Vector<ParticleSet *> particle_sets; state.particles().particle_containers().foreach_key_value_pair( - [&container_names, &containers](StringRefNull name, AttributesBlockContainer *container) { - container_names.append(name); - containers.append(container); + [&system_names, &particle_sets](StringRefNull name, ParticleSet *particles) { + system_names.append(name); + particle_sets.append(particles); }); BParticlesFrameCache cached_frame; memset(&cached_frame, 0, sizeof(BParticlesFrameCache)); cached_frame.frame = frame; - cached_frame.num_particle_types = containers.size(); + cached_frame.num_particle_types = particle_sets.size(); cached_frame.particle_types = (BParticlesTypeCache *)MEM_calloc_arrayN( - containers.size(), sizeof(BParticlesTypeCache), __func__); + particle_sets.size(), sizeof(BParticlesTypeCache), __func__); - for (uint i : containers.index_iterator()) { - AttributesBlockContainer &container = *containers[i]; + for (uint i : particle_sets.index_iterator()) { + ParticleSet &particles = *particle_sets[i]; BParticlesTypeCache &cached_type = cached_frame.particle_types[i]; - strncpy(cached_type.name, container_names[i].data(), sizeof(cached_type.name) - 1); - cached_type.particle_amount = container.count_active(); + strncpy(cached_type.name, system_names[i].data(), sizeof(cached_type.name) - 1); + cached_type.particle_amount = particles.size(); cached_type.num_attributes_float = 3; cached_type.attributes_float = (BParticlesAttributeCacheFloat *)MEM_calloc_arrayN( @@ -328,30 +326,27 @@ void BParticles_modifier_cache_state(BParticlesModifierData *bpmd, strncpy(position_attribute.name, "Position", sizeof(position_attribute.name)); position_attribute.values = (float *)MEM_malloc_arrayN( cached_type.particle_amount, sizeof(float3), __func__); - container.flatten_attribute("Position", - FN::GenericMutableArrayRef(FN::CPP_TYPE<float3>(), - position_attribute.values, - cached_type.particle_amount)); + FN::CPP_TYPE<float3>().copy_to_uninitialized_n(particles.attributes().get("Position").buffer(), + position_attribute.values, + cached_type.particle_amount); BParticlesAttributeCacheFloat &size_attribute = cached_type.attributes_float[1]; size_attribute.floats_per_particle = 1; strncpy(size_attribute.name, "Size", sizeof(size_attribute.name)); size_attribute.values = (float *)MEM_malloc_arrayN( cached_type.particle_amount, sizeof(float), __func__); - container.flatten_attribute("Size", - FN::GenericMutableArrayRef(FN::CPP_TYPE<float>(), - size_attribute.values, - cached_type.particle_amount)); + FN::CPP_TYPE<float>().copy_to_uninitialized_n(particles.attributes().get("Size").buffer(), + size_attribute.values, + cached_type.particle_amount); BParticlesAttributeCacheFloat &color_attribute = cached_type.attributes_float[2]; color_attribute.floats_per_particle = 4; strncpy(color_attribute.name, "Color", sizeof(color_attribute.name)); color_attribute.values = (float *)MEM_malloc_arrayN( cached_type.particle_amount, sizeof(rgba_f), __func__); - container.flatten_attribute("Color", - FN::GenericMutableArrayRef(FN::CPP_TYPE<rgba_f>(), - color_attribute.values, - cached_type.particle_amount)); + FN::CPP_TYPE<rgba_f>().copy_to_uninitialized_n(particles.attributes().get("Color").buffer(), + color_attribute.values, + cached_type.particle_amount); } bpmd->cached_frames = (BParticlesFrameCache *)MEM_reallocN( diff --git a/source/blender/simulations/bparticles/node_frontend.cpp b/source/blender/simulations/bparticles/node_frontend.cpp index 0ef45748d26..9b24ad2afa9 100644 --- a/source/blender/simulations/bparticles/node_frontend.cpp +++ b/source/blender/simulations/bparticles/node_frontend.cpp @@ -1146,9 +1146,9 @@ class NodeTreeStepSimulator : public StepSimulator { AttributesInfoBuilder &system_attributes = *influences_collector.m_attributes.lookup(name); /* Keep old attributes. */ - AttributesBlockContainer *container = containers.lookup_default(name, nullptr); - if (container != nullptr) { - system_attributes.add(container->info()); + ParticleSet *particles = containers.lookup_default(name, nullptr); + if (particles != nullptr) { + system_attributes.add(particles->attributes_info()); } this->ensure_particle_container_exist_and_has_attributes( @@ -1177,14 +1177,14 @@ class NodeTreeStepSimulator : public StepSimulator { const AttributesInfoBuilder &attributes_info_builder) { auto &containers = particles_state.particle_containers(); - AttributesBlockContainer *container = containers.lookup_default(name, nullptr); - if (container == nullptr) { - AttributesBlockContainer *container = new AttributesBlockContainer(attributes_info_builder, - 1000); - containers.add_new(name, container); + ParticleSet *particles = containers.lookup_default(name, nullptr); + AttributesInfo *attributes_info = new AttributesInfo(attributes_info_builder); + if (particles == nullptr) { + ParticleSet *new_particle_set = new ParticleSet(*attributes_info, true); + containers.add_new(name, new_particle_set); } else { - container->update_attributes(attributes_info_builder); + particles->update_attributes(attributes_info); } } }; diff --git a/source/blender/simulations/bparticles/particle_allocator.cpp b/source/blender/simulations/bparticles/particle_allocator.cpp index 7a1346524cd..483ed75daaf 100644 --- a/source/blender/simulations/bparticles/particle_allocator.cpp +++ b/source/blender/simulations/bparticles/particle_allocator.cpp @@ -6,40 +6,6 @@ ParticleAllocator::ParticleAllocator(ParticlesState &state) : m_state(state) { } -void ParticleAllocator::allocate_buffer_ranges(AttributesBlockContainer &container, - uint size, - Vector<ArrayRef<void *>> &r_buffers, - Vector<IndexRange> &r_ranges) -{ - std::lock_guard<std::mutex> lock(m_request_mutex); - - uint remaining_size = size; - while (remaining_size > 0) { - AttributesBlock *cached_block = m_non_full_cache.lookup_default(&container, nullptr); - if (cached_block != nullptr) { - uint remaining_in_block = cached_block->unused_capacity(); - BLI_assert(remaining_in_block > 0); - uint size_to_use = std::min(remaining_size, remaining_in_block); - - IndexRange range(cached_block->used_size(), size_to_use); - r_buffers.append(cached_block->buffers()); - r_ranges.append(range); - remaining_size -= size_to_use; - - cached_block->set_used_size(range.one_after_last()); - if (cached_block->unused_capacity() == 0) { - m_non_full_cache.remove(&container); - } - continue; - } - else { - AttributesBlock &new_block = container.new_block(); - m_non_full_cache.add_new(&container, &new_block); - m_allocated_blocks.append(&new_block); - } - } -} - void ParticleAllocator::initialize_new_particles(AttributesRefGroup &attributes_group) { const AttributesInfo &info = attributes_group.info(); @@ -62,13 +28,24 @@ void ParticleAllocator::initialize_new_particles(AttributesRefGroup &attributes_ AttributesRefGroup ParticleAllocator::request(StringRef particle_system_name, uint size) { - AttributesBlockContainer &container = m_state.particle_container(particle_system_name); + ParticleSet &main_set = m_state.particle_container(particle_system_name); + const AttributesInfo &attributes_info = main_set.attributes_info(); + + ParticleSet *particles = new ParticleSet(attributes_info, false); + particles->reserve(size); + particles->increase_size_without_realloc(size); + MutableAttributesRef attributes = particles->attributes(); + + { + std::lock_guard<std::mutex> lock(m_request_mutex); + m_allocated_particles.add(&main_set, particles); + } Vector<ArrayRef<void *>> buffers; Vector<IndexRange> ranges; - this->allocate_buffer_ranges(container, size, buffers, ranges); + buffers.append(attributes.internal_buffers()); + ranges.append(attributes.internal_range()); - const AttributesInfo &attributes_info = container.info(); AttributesRefGroup attributes_group(attributes_info, std::move(buffers), std::move(ranges)); this->initialize_new_particles(attributes_group); diff --git a/source/blender/simulations/bparticles/particle_allocator.hpp b/source/blender/simulations/bparticles/particle_allocator.hpp index f4f2dd1f158..413500cbda0 100644 --- a/source/blender/simulations/bparticles/particle_allocator.hpp +++ b/source/blender/simulations/bparticles/particle_allocator.hpp @@ -1,25 +1,29 @@ #pragma once +#include <mutex> + +#include "BLI_multi_map.h" + #include "particles_state.hpp" namespace BParticles { +using BLI::MultiMap; using FN::AttributesRefGroup; class ParticleAllocator : BLI::NonCopyable, BLI::NonMovable { private: ParticlesState &m_state; - Map<AttributesBlockContainer *, AttributesBlock *> m_non_full_cache; - Vector<AttributesBlock *> m_allocated_blocks; + MultiMap<ParticleSet *, ParticleSet *> m_allocated_particles; std::mutex m_request_mutex; public: ParticleAllocator(ParticlesState &state); /** - * Access all blocks that have been allocated by this allocator. + * Access all particles that have been allocated by this allocator. */ - ArrayRef<AttributesBlock *> allocated_blocks(); + MultiMap<std::string, ParticleSet *> allocated_particles(); /** * Get memory buffers for new particles. @@ -27,30 +31,15 @@ class ParticleAllocator : BLI::NonCopyable, BLI::NonMovable { AttributesRefGroup request(StringRef particle_system_name, uint size); private: - /** - * Return a block that can hold new particles. It might create an entirely new one or use a - * cached block. - */ - AttributesBlock &get_non_full_block(AttributesBlockContainer &container); - - /** - * Allocate space for a given number of new particles. The attribute buffers might be - * distributed over multiple blocks. - */ - void allocate_buffer_ranges(AttributesBlockContainer &container, - uint size, - Vector<ArrayRef<void *>> &r_buffers, - Vector<IndexRange> &r_ranges); - void initialize_new_particles(AttributesRefGroup &attributes_group); }; /* ParticleAllocator inline functions ********************************************/ -inline ArrayRef<AttributesBlock *> ParticleAllocator::allocated_blocks() +inline MultiMap<std::string, ParticleSet *> ParticleAllocator::allocated_particles() { - return m_allocated_blocks; + return m_allocated_particles; } } // namespace BParticles diff --git a/source/blender/simulations/bparticles/particle_set.cpp b/source/blender/simulations/bparticles/particle_set.cpp index ef647cb9959..3ae8d9f01b1 100644 --- a/source/blender/simulations/bparticles/particle_set.cpp +++ b/source/blender/simulations/bparticles/particle_set.cpp @@ -2,19 +2,29 @@ namespace BParticles { -ParticleSet::ParticleSet(const AttributesInfo *attributes_info, uint size) - : m_attributes_info(attributes_info), - m_attribute_buffers(attributes_info->size()), - m_size(size), - m_capacity(size) +ParticleSet::ParticleSet(const AttributesInfo &attributes_info, bool own_attributes_info) + : m_attributes_info(&attributes_info), + m_attribute_buffers(attributes_info.size(), nullptr), + m_size(0), + m_capacity(0), + m_own_attributes_info(own_attributes_info) +{ +} + +ParticleSet::~ParticleSet() { for (uint i : m_attributes_info->indices()) { const CPPType &type = m_attributes_info->type_of(i); - const void *default_value = attributes_info->default_of(i); + void *buffer = m_attribute_buffers[i]; + + if (buffer != nullptr) { + type.destruct_n(m_attribute_buffers[i], m_size); + MEM_freeN(buffer); + } + } - void *buffer = MEM_mallocN_aligned(m_capacity * type.size(), type.alignment(), __func__); - type.fill_uninitialized(default_value, buffer, m_size); - m_attribute_buffers[i] = buffer; + if (m_own_attributes_info) { + delete m_attributes_info; } } @@ -59,13 +69,17 @@ void ParticleSet::realloc_particle_attributes(uint min_size) for (uint index : m_attributes_info->indices()) { const CPPType &type = m_attributes_info->type_of(index); - void *old_buffer = m_attribute_buffers[index]; void *new_buffer = MEM_mallocN_aligned(type.size() * new_capacity, type.alignment(), __func__); - type.relocate_to_uninitialized_n(old_buffer, new_buffer, m_size); - MEM_freeN(old_buffer); + + void *old_buffer = m_attribute_buffers[index]; + if (old_buffer != nullptr) { + type.relocate_to_uninitialized_n(old_buffer, new_buffer, m_size); + MEM_freeN(old_buffer); + } m_attribute_buffers[index] = new_buffer; } + m_capacity = new_capacity; } } // namespace BParticles diff --git a/source/blender/simulations/bparticles/particle_set.hpp b/source/blender/simulations/bparticles/particle_set.hpp index 0e3acba5f31..73128ed6b3e 100644 --- a/source/blender/simulations/bparticles/particle_set.hpp +++ b/source/blender/simulations/bparticles/particle_set.hpp @@ -20,9 +20,10 @@ class ParticleSet : BLI::NonCopyable, BLI::NonMovable { Array<void *> m_attribute_buffers; uint m_size; uint m_capacity; + bool m_own_attributes_info; public: - ParticleSet(const AttributesInfo *attributes_info, uint size); + ParticleSet(const AttributesInfo &attributes_info, bool own_attributes_info); ~ParticleSet(); const AttributesInfo &attributes_info() const @@ -35,11 +36,32 @@ class ParticleSet : BLI::NonCopyable, BLI::NonMovable { return MutableAttributesRef(*m_attributes_info, m_attribute_buffers, m_size); } + MutableAttributesRef attributes_all() + { + return MutableAttributesRef(*m_attributes_info, m_attribute_buffers, m_capacity); + } + uint size() const { return m_size; } + uint remaining_size() const + { + return m_capacity - m_size; + } + + void increase_size_without_realloc(uint amount) + { + BLI_assert(m_size + amount <= m_capacity); + m_size += amount; + } + + void reserve(uint amount) + { + this->realloc_particle_attributes(std::max(amount, m_capacity)); + } + void add_particles(ParticleSet &particles); void update_attributes(const AttributesInfo *new_attributes_info); diff --git a/source/blender/simulations/bparticles/particles_state.cpp b/source/blender/simulations/bparticles/particles_state.cpp index 4533549e825..284cf251db2 100644 --- a/source/blender/simulations/bparticles/particles_state.cpp +++ b/source/blender/simulations/bparticles/particles_state.cpp @@ -4,7 +4,7 @@ namespace BParticles { ParticlesState::~ParticlesState() { - m_container_by_id.foreach_value([](AttributesBlockContainer *container) { delete container; }); + m_container_by_id.foreach_value([](ParticleSet *particles) { delete particles; }); } } // namespace BParticles diff --git a/source/blender/simulations/bparticles/particles_state.hpp b/source/blender/simulations/bparticles/particles_state.hpp index e29737ef73a..b1ba2cdf7c0 100644 --- a/source/blender/simulations/bparticles/particles_state.hpp +++ b/source/blender/simulations/bparticles/particles_state.hpp @@ -2,7 +2,7 @@ #include <atomic> -#include "FN_attributes_block_container.h" +#include "particle_set.hpp" namespace BParticles { @@ -15,15 +15,13 @@ using BLI::StringRef; using BLI::StringRefNull; using BLI::Vector; using BLI::VectorSet; -using FN::AttributesBlock; -using FN::AttributesBlockContainer; using FN::AttributesInfo; using FN::AttributesRef; using FN::MutableAttributesRef; class ParticlesState { private: - StringMap<AttributesBlockContainer *> m_container_by_id; + StringMap<ParticleSet *> m_container_by_id; std::atomic<uint> m_next_id; public: @@ -36,18 +34,18 @@ class ParticlesState { /** * Access the mapping from particle system names to their corresponding containers. */ - StringMap<AttributesBlockContainer *> &particle_containers(); + StringMap<ParticleSet *> &particle_containers(); /** * Get the container corresponding to a particle system name. * Asserts when the container does not exist. */ - AttributesBlockContainer &particle_container(StringRef name); + ParticleSet &particle_container(StringRef name); /** * Get the name of a container in the context of this particle state. */ - StringRefNull particle_container_name(AttributesBlockContainer &container); + StringRefNull particle_container_name(ParticleSet &container); /** * Get range of unique particle ids. @@ -62,17 +60,17 @@ class ParticlesState { /* ParticlesState inline functions ********************************************/ -inline StringMap<AttributesBlockContainer *> &ParticlesState::particle_containers() +inline StringMap<ParticleSet *> &ParticlesState::particle_containers() { return m_container_by_id; } -inline AttributesBlockContainer &ParticlesState::particle_container(StringRef name) +inline ParticleSet &ParticlesState::particle_container(StringRef name) { return *m_container_by_id.lookup(name); } -inline StringRefNull ParticlesState::particle_container_name(AttributesBlockContainer &container) +inline StringRefNull ParticlesState::particle_container_name(ParticleSet &container) { StringRefNull result = m_container_by_id.find_key_for_value(&container); return result; diff --git a/source/blender/simulations/bparticles/simulate.cpp b/source/blender/simulations/bparticles/simulate.cpp index 3a662647cdd..68f6825ff57 100644 --- a/source/blender/simulations/bparticles/simulate.cpp +++ b/source/blender/simulations/bparticles/simulate.cpp @@ -333,9 +333,9 @@ BLI_NOINLINE static void simulate_particle_chunk(SimulationState &simulation_sta } } -BLI_NOINLINE static void delete_tagged_particles_and_reorder(AttributesBlock &block) +BLI_NOINLINE static void delete_tagged_particles_and_reorder(ParticleSet &particles) { - auto kill_states = block.as_ref().get<bool>("Dead"); + auto kill_states = particles.attributes().get<bool>("Dead"); LargeScopedVector<uint> indices_to_delete; for (uint i : kill_states.index_iterator()) { @@ -344,120 +344,104 @@ BLI_NOINLINE static void delete_tagged_particles_and_reorder(AttributesBlock &bl } } - block.destruct_and_reorder(indices_to_delete.as_ref()); + particles.destruct_and_reorder(indices_to_delete.as_ref()); } -BLI_NOINLINE static void simulate_blocks_for_time_span( - ParticleAllocator &particle_allocator, - ArrayRef<AttributesBlock *> blocks, - StringMap<ParticleSystemInfo> &systems_to_simulate, - FloatInterval time_span, - SimulationState &simulation_state) -{ - auto func = [&](uint block_index) { - AttributesBlock &block = *blocks[block_index]; - - StringRef particle_system_name = simulation_state.particles().particle_container_name( - block.owner()); - ParticleSystemInfo &system_info = systems_to_simulate.lookup(particle_system_name); - - LargeScopedArray<float> remaining_durations(block.used_size()); - remaining_durations.fill(time_span.size()); - - simulate_particle_chunk(simulation_state, - particle_allocator, - block.as_ref(), - system_info, - remaining_durations, - time_span.end()); - - delete_tagged_particles_and_reorder(block); - }; - -#ifdef WITH_TBB - tbb::parallel_for((uint)0, blocks.size(), func); -#else - for (uint i : blocks.index_iterator()) { - func(i); - } -#endif -} - -BLI_NOINLINE static void simulate_blocks_from_birth_to_current_time( - ParticleAllocator &particle_allocator, - ArrayRef<AttributesBlock *> blocks, - StringMap<ParticleSystemInfo> &systems_to_simulate, - float end_time, - SimulationState &simulation_state) -{ - auto func = [&](uint block_index) { - AttributesBlock &block = *blocks[block_index]; - - StringRef particle_system_name = simulation_state.particles().particle_container_name( - block.owner()); - ParticleSystemInfo &system_info = systems_to_simulate.lookup(particle_system_name); - - uint active_amount = block.used_size(); - Vector<float> durations(active_amount); - auto birth_times = block.as_ref().get<float>("Birth Time"); - for (uint i = 0; i < active_amount; i++) { - durations[i] = end_time - birth_times[i]; - } - simulate_particle_chunk( - simulation_state, particle_allocator, block.as_ref(), system_info, durations, end_time); - - delete_tagged_particles_and_reorder(block); - }; - -#ifdef WITH_TBB - tbb::parallel_for((uint)0, blocks.size(), func); -#else - for (uint i : blocks.index_iterator()) { - func(i); - } -#endif -} - -BLI_NOINLINE static Vector<AttributesBlock *> get_all_blocks_to_simulate( - ParticlesState &state, StringMap<ParticleSystemInfo> &systems_to_simulate) -{ - Vector<AttributesBlock *> blocks; - systems_to_simulate.foreach_key([&state, &blocks](StringRefNull particle_system_name) { - AttributesBlockContainer &container = state.particle_container(particle_system_name); - blocks.extend(container.active_blocks()); - }); - return blocks; -} - -BLI_NOINLINE static void compress_all_blocks(AttributesBlockContainer &container) -{ - Vector<AttributesBlock *> blocks = container.active_blocks(); - AttributesBlock::Compress(blocks); - - for (AttributesBlock *block : blocks) { - if (block->used_size() == 0) { - container.release_block(*block); - } - } -} - -BLI_NOINLINE static void compress_all_containers(ParticlesState &state) -{ - state.particle_containers().foreach_value( - [](AttributesBlockContainer *container) { compress_all_blocks(*container); }); -} - -BLI_NOINLINE static void simulate_all_existing_blocks( - SimulationState &simulation_state, - StringMap<ParticleSystemInfo> &systems_to_simulate, - ParticleAllocator &particle_allocator, - FloatInterval time_span) -{ - Vector<AttributesBlock *> blocks = get_all_blocks_to_simulate(simulation_state.particles(), - systems_to_simulate); - simulate_blocks_for_time_span( - particle_allocator, blocks, systems_to_simulate, time_span, simulation_state); -} +// BLI_NOINLINE static void simulate_blocks_for_time_span( +// ParticleAllocator &particle_allocator, +// ArrayRef<AttributesBlock *> blocks, +// StringMap<ParticleSystemInfo> &systems_to_simulate, +// FloatInterval time_span, +// SimulationState &simulation_state) +// { +// auto func = [&](uint block_index) { +// AttributesBlock &block = *blocks[block_index]; + +// StringRef particle_system_name = simulation_state.particles().particle_container_name( +// block.owner()); +// ParticleSystemInfo &system_info = systems_to_simulate.lookup(particle_system_name); + +// LargeScopedArray<float> remaining_durations(block.used_size()); +// remaining_durations.fill(time_span.size()); + +// simulate_particle_chunk(simulation_state, +// particle_allocator, +// block.as_ref(), +// system_info, +// remaining_durations, +// time_span.end()); + +// delete_tagged_particles_and_reorder(block); +// }; + +// #ifdef WITH_TBB +// tbb::parallel_for((uint)0, blocks.size(), func); +// #else +// for (uint i : blocks.index_iterator()) { +// func(i); +// } +// #endif +// } + +// BLI_NOINLINE static void simulate_blocks_from_birth_to_current_time( +// ParticleAllocator &particle_allocator, +// ArrayRef<AttributesBlock *> blocks, +// StringMap<ParticleSystemInfo> &systems_to_simulate, +// float end_time, +// SimulationState &simulation_state) +// { +// auto func = [&](uint block_index) { +// AttributesBlock &block = *blocks[block_index]; + +// StringRef particle_system_name = simulation_state.particles().particle_container_name( +// block.owner()); +// ParticleSystemInfo &system_info = systems_to_simulate.lookup(particle_system_name); + +// uint active_amount = block.used_size(); +// Vector<float> durations(active_amount); +// auto birth_times = block.as_ref().get<float>("Birth Time"); +// for (uint i = 0; i < active_amount; i++) { +// durations[i] = end_time - birth_times[i]; +// } +// simulate_particle_chunk( +// simulation_state, particle_allocator, block.as_ref(), system_info, durations, end_time); + +// delete_tagged_particles_and_reorder(block); +// }; + +// #ifdef WITH_TBB +// tbb::parallel_for((uint)0, blocks.size(), func); +// #else +// for (uint i : blocks.index_iterator()) { +// func(i); +// } +// #endif +// } + +// BLI_NOINLINE static Vector<AttributesBlock *> get_all_blocks_to_simulate( +// ParticlesState &state, StringMap<ParticleSystemInfo> &systems_to_simulate) +// { +// Vector<AttributesBlock *> blocks; +// systems_to_simulate.foreach_key([&state, &blocks](StringRefNull particle_system_name) { +// AttributesBlockContainer &container = state.particle_container(particle_system_name); +// blocks.extend(container.active_blocks()); +// }); +// return blocks; +// } + +// BLI_NOINLINE static void simulate_all_existing_blocks( +// SimulationState &simulation_state, +// StringMap<ParticleSystemInfo> &systems_to_simulate, +// ParticleAllocator &particle_allocator, +// FloatInterval time_span) +// { +// simulation_state.particles().particle_containers().foreach_key_value_pair( +// [&](StringRef name, ParticleSet *particles) {}); +// Vector<AttributesBlock *> blocks = get_all_blocks_to_simulate(simulation_state.particles(), +// systems_to_simulate); +// simulate_blocks_for_time_span( +// particle_allocator, blocks, systems_to_simulate, time_span, simulation_state); +// } BLI_NOINLINE static void create_particles_from_emitters(SimulationState &simulation_state, ParticleAllocator &particle_allocator, @@ -482,22 +466,10 @@ void simulate_particles(SimulationState &simulation_state, Vector<AttributesBlock *> newly_created_blocks; { ParticleAllocator particle_allocator(particles_state); -#ifdef WITH_TBB - tbb::parallel_invoke( - [&]() { - simulate_all_existing_blocks( - simulation_state, systems_to_simulate, particle_allocator, simulation_time_span); - }, - [&]() { - create_particles_from_emitters( - simulation_state, particle_allocator, emitters, simulation_time_span); - }); -#else simulate_all_existing_blocks( simulation_state, systems_to_simulate, particle_allocator, simulation_time_span); create_particles_from_emitters( simulation_state, particle_allocator, emitters, simulation_time_span); -#endif newly_created_blocks = particle_allocator.allocated_blocks(); } |