diff options
5 files changed, 99 insertions, 99 deletions
diff --git a/source/blender/simulations/bparticles/actions.cpp b/source/blender/simulations/bparticles/actions.cpp index abdfdcc77af..3c8a3d17c67 100644 --- a/source/blender/simulations/bparticles/actions.cpp +++ b/source/blender/simulations/bparticles/actions.cpp @@ -15,12 +15,10 @@ void ChangeDirectionAction::execute(ActionInterface &interface) auto position_offsets = interface.attribute_offsets().try_get_float3("Position"); auto velocity_offsets = interface.attribute_offsets().try_get_float3("Velocity"); - auto caller = m_compute_inputs.get_caller(interface); - auto new_directions = caller.add_output<float3>("Direction"); - caller.call(particles.pindices()); + auto inputs = m_compute_inputs.compute(interface); for (uint pindex : particles.pindices()) { - float3 direction = new_directions[pindex]; + float3 direction = inputs.get<float3>("Direction", 0, pindex); velocities[pindex] = direction; @@ -62,14 +60,11 @@ void ExplodeAction::execute(ActionInterface &interface) Vector<float3> new_velocities; Vector<float> new_birth_times; - auto caller = m_compute_inputs.get_caller(interface); - auto parts_amounts = caller.add_output<int>("Amount"); - auto speeds = caller.add_output<float>("Speed"); - caller.call(particles.pindices()); + auto inputs = m_compute_inputs.compute(interface); for (uint pindex : particles.pindices()) { - uint parts_amount = std::max(0, parts_amounts[pindex]); - float speed = speeds[pindex]; + uint parts_amount = std::max(0, inputs.get<int>("Amount", 0, pindex)); + float speed = inputs.get<float>("Speed", 1, pindex); new_positions.append_n_times(positions[pindex], parts_amount); new_birth_times.append_n_times(interface.current_times()[pindex], parts_amount); @@ -93,13 +88,11 @@ void ConditionAction::execute(ActionInterface &interface) { ParticleSet particles = interface.particles(); - auto caller = m_compute_inputs.get_caller(interface); - auto conditions = caller.add_output<bool>("Condition"); - caller.call(particles.pindices()); + auto inputs = m_compute_inputs.compute(interface); Vector<uint> true_pindices, false_pindices; for (uint pindex : particles.pindices()) { - if (conditions[pindex]) { + if (inputs.get<bool>("Condition", 0, pindex)) { true_pindices.append(pindex); } else { diff --git a/source/blender/simulations/bparticles/forces.cpp b/source/blender/simulations/bparticles/forces.cpp index f18c15cfc8a..7c7456e31dd 100644 --- a/source/blender/simulations/bparticles/forces.cpp +++ b/source/blender/simulations/bparticles/forces.cpp @@ -34,13 +34,11 @@ void TurbulenceForce::add_force(ForceInterface &interface) auto positions = block.attributes().get_float3("Position"); - auto caller = m_compute_inputs.get_caller(interface); - auto strengths = caller.add_output<float3>("Strength"); - caller.call(block.active_range().as_array_ref()); + auto inputs = m_compute_inputs.compute(interface); for (uint pindex = 0; pindex < block.active_amount(); pindex++) { float3 pos = positions[pindex]; - float3 strength = strengths[pindex]; + float3 strength = inputs.get<float3>("Strength", 0, pindex); float x = (BLI_gNoise(0.5f, pos.x, pos.y, pos.z + 1000.0f, false, 1) - 0.5f) * strength.x; float y = (BLI_gNoise(0.5f, pos.x, pos.y + 1000.0f, pos.z, false, 1) - 0.5f) * strength.y; float z = (BLI_gNoise(0.5f, pos.x + 1000.0f, pos.y, pos.z, false, 1) - 0.5f) * strength.z; diff --git a/source/blender/simulations/bparticles/offset_handlers.cpp b/source/blender/simulations/bparticles/offset_handlers.cpp index b185cc2a714..325533e21aa 100644 --- a/source/blender/simulations/bparticles/offset_handlers.cpp +++ b/source/blender/simulations/bparticles/offset_handlers.cpp @@ -8,14 +8,17 @@ void CreateTrailHandler::execute(OffsetHandlerInterface &interface) auto positions = particles.attributes().get_float3("Position"); auto position_offsets = interface.offsets().get_float3("Position"); - auto caller = m_compute_inputs.get_caller(interface); - auto rates = caller.add_output<float>("Rate"); - caller.call(particles.pindices()); + auto inputs = m_compute_inputs.compute(interface); Vector<float3> new_positions; Vector<float> new_birth_times; for (uint pindex : particles.pindices()) { - float frequency = 1.0f / rates[pindex]; + float rate = inputs.get<float>("Rate", 0, pindex); + if (rate <= 0.0f) { + continue; + } + + float frequency = 1.0f / rate; float time_factor = interface.time_factors()[pindex]; TimeSpan time_span = interface.time_span(pindex); float current_time = frequency * (std::floor(time_span.start() / frequency) + 1.0f); diff --git a/source/blender/simulations/bparticles/particle_function.cpp b/source/blender/simulations/bparticles/particle_function.cpp index 9656a29cfd6..f480232f5e2 100644 --- a/source/blender/simulations/bparticles/particle_function.cpp +++ b/source/blender/simulations/bparticles/particle_function.cpp @@ -2,35 +2,38 @@ namespace BParticles { -ParticleFunctionCaller ParticleFunction::get_caller(ActionInterface &action_interface) +ParticleFunctionResult ParticleFunction::compute(ActionInterface &action_interface) { - return this->get_caller(action_interface.array_allocator(), - action_interface.particles().attributes(), - &action_interface.context()); + return this->compute(action_interface.array_allocator(), + action_interface.particles().pindices(), + action_interface.particles().attributes(), + &action_interface.context()); } -ParticleFunctionCaller ParticleFunction::get_caller( - OffsetHandlerInterface &offset_handler_interface) +ParticleFunctionResult ParticleFunction::compute(OffsetHandlerInterface &offset_handler_interface) { - return this->get_caller(offset_handler_interface.array_allocator(), - offset_handler_interface.particles().attributes(), - nullptr); + return this->compute(offset_handler_interface.array_allocator(), + offset_handler_interface.particles().pindices(), + offset_handler_interface.particles().attributes(), + nullptr); } -ParticleFunctionCaller ParticleFunction::get_caller(ForceInterface &force_interface) +ParticleFunctionResult ParticleFunction::compute(ForceInterface &force_interface) { - return this->get_caller( - force_interface.array_allocator(), force_interface.block().attributes(), nullptr); + ParticlesBlock &block = force_interface.block(); + return this->compute(force_interface.array_allocator(), + block.active_range().as_array_ref(), + block.attributes(), + nullptr); } -ParticleFunctionCaller ParticleFunction::get_caller(ArrayAllocator &array_allocator, - AttributeArrays attributes, - ActionContext *action_context) +ParticleFunctionResult ParticleFunction::compute(ArrayAllocator &array_allocator, + ArrayRef<uint> pindices, + AttributeArrays attributes, + ActionContext *action_context) { - ParticleFunctionCaller caller; - caller.m_body = m_body; - caller.m_min_buffer_length = attributes.size(); - caller.m_array_allocator = &array_allocator; + Vector<void *> input_buffers; + Vector<uint> input_strides; for (uint i = 0; i < m_fn->input_amount(); i++) { StringRef input_name = m_fn->input_name(i); @@ -53,11 +56,45 @@ ParticleFunctionCaller ParticleFunction::get_caller(ArrayAllocator &array_alloca } BLI_assert(input_buffer != nullptr); - caller.m_input_buffers.append(input_buffer); - caller.m_input_strides.append(input_stride); + input_buffers.append(input_buffer); + input_strides.append(input_stride); } - return caller; + ParticleFunctionResult result; + result.m_fn = m_fn.ptr(); + result.m_array_allocator = &array_allocator; + + for (uint i = 0; i < m_fn->output_amount(); i++) { + CPPTypeInfo *type_info = m_fn->output_type(i)->extension<CPPTypeInfo>(); + BLI_assert(type_info != nullptr); + + uint output_stride = type_info->size_of_type(); + void *output_buffer = array_allocator.allocate(output_stride); + + result.m_buffers.append(output_buffer); + result.m_strides.append(output_stride); + } + + ExecutionStack stack; + ExecutionContext execution_context(stack); + + FN_TUPLE_CALL_ALLOC_TUPLES(m_body, fn_in, fn_out); + + for (uint pindex : pindices) { + for (uint i = 0; i < input_buffers.size(); i++) { + void *ptr = POINTER_OFFSET(input_buffers[i], pindex * input_strides[i]); + fn_in.copy_in__dynamic(i, ptr); + } + + m_body->call(fn_in, fn_out, execution_context); + + for (uint i = 0; i < result.m_buffers.size(); i++) { + void *ptr = POINTER_OFFSET(result.m_buffers[i], pindex * result.m_strides[i]); + fn_out.relocate_out__dynamic(i, ptr); + } + } + + return result; } } // namespace BParticles diff --git a/source/blender/simulations/bparticles/particle_function.hpp b/source/blender/simulations/bparticles/particle_function.hpp index bb5dce8faa7..c84a63b208b 100644 --- a/source/blender/simulations/bparticles/particle_function.hpp +++ b/source/blender/simulations/bparticles/particle_function.hpp @@ -18,66 +18,34 @@ using FN::TupleCallBody; class ParticleFunction; -class ParticleFunctionCaller { +class ParticleFunctionResult { private: - TupleCallBody *m_body; - uint m_min_buffer_length; - Vector<void *> m_input_buffers; - Vector<void *> m_output_buffers; - Vector<uint> m_input_strides; - Vector<uint> m_output_strides; + Vector<void *> m_buffers; + Vector<uint> m_strides; ArrayAllocator *m_array_allocator; + FN::Function *m_fn; friend ParticleFunction; public: - template<typename T> void add_output(ArrayRef<T> array, StringRef expected_name) - { -#ifdef DEBUG - uint index = m_output_buffers.size(); - SharedType &type = m_body->owner()->output_type(index); - uint expected_stride = type->extension<CPPTypeInfo>()->size_of_type(); - uint given_stride = sizeof(T); - BLI_assert(expected_stride == given_stride); - BLI_assert(m_min_buffer_length <= array.size()); - StringRef real_name = m_body->owner()->output_name(index); - BLI_assert(expected_name == real_name); -#endif - - m_output_buffers.append((void *)array.begin()); - m_output_strides.append(sizeof(T)); - } - - template<typename T> ArrayAllocator::Array<T> add_output(StringRef expected_name) + ~ParticleFunctionResult() { - ArrayAllocator::Array<T> array(*m_array_allocator); - this->add_output(array.as_array_ref(), expected_name); - return array; + for (uint i = 0; i < m_buffers.size(); i++) { + m_array_allocator->deallocate(m_buffers[i], m_strides[i]); + } } - void call(ArrayRef<uint> pindices) + template<typename T> T get(StringRef expected_name, uint parameter_index, uint pindex) { - BLI_assert(m_body->owner()->input_amount() == m_input_buffers.size()); - BLI_assert(m_body->owner()->output_amount() == m_output_buffers.size()); - - ExecutionStack stack; - ExecutionContext execution_context(stack); - - FN_TUPLE_CALL_ALLOC_TUPLES(m_body, fn_in, fn_out); - - for (uint pindex : pindices) { - for (uint i = 0; i < m_input_buffers.size(); i++) { - void *ptr = POINTER_OFFSET(m_input_buffers[i], pindex * m_input_strides[i]); - fn_in.copy_in__dynamic(i, ptr); - } - - m_body->call(fn_in, fn_out, execution_context); +#ifdef DEBUG + BLI_assert(sizeof(T) == m_strides[parameter_index]); + StringRefNull real_name = m_fn->output_name(parameter_index); + BLI_assert(real_name == expected_name); +#endif + UNUSED_VARS_NDEBUG(expected_name); - for (uint i = 0; i < m_output_buffers.size(); i++) { - void *ptr = POINTER_OFFSET(m_output_buffers[i], pindex * m_output_strides[i]); - fn_out.relocate_out__dynamic(i, ptr); - } - } + T *buffer = (T *)m_buffers[parameter_index]; + return buffer[pindex]; } }; @@ -92,14 +60,15 @@ class ParticleFunction { BLI_assert(m_body != nullptr); } - ParticleFunctionCaller get_caller(ActionInterface &action_interface); - ParticleFunctionCaller get_caller(OffsetHandlerInterface &offset_handler_interface); - ParticleFunctionCaller get_caller(ForceInterface &force_interface); + ParticleFunctionResult compute(ActionInterface &action_interface); + ParticleFunctionResult compute(OffsetHandlerInterface &offset_handler_interface); + ParticleFunctionResult compute(ForceInterface &force_interface); private: - ParticleFunctionCaller get_caller(ArrayAllocator &array_allocator, - AttributeArrays attributes, - ActionContext *action_context); + ParticleFunctionResult compute(ArrayAllocator &array_allocator, + ArrayRef<uint> pindices, + AttributeArrays attributes, + ActionContext *action_context); }; } // namespace BParticles |