Unravel Engine C++ Reference
Loading...
Searching...
No Matches
particle_system.cpp
Go to the documentation of this file.
1#include "particle_system.h"
2#include "threadpp/thread.h"
7#include <graphics/graphics.h>
8#include <logging/logging.h>
9
10
11#define POOLSTL_STD_SUPPLEMENT 1
12#include <poolstl/poolstl.hpp>
13
14
15namespace unravel
16{
17
19{
20 APPLOG_TRACE("{}::{}", hpp::type_name_str(*this), __func__);
21
22 // Initialize the soa particle system (large emitter pool for editor/scenes).
23 ps_soa::init(4096 * 4);
25
26
27 initialized_ = true;
28
29 return true;
30}
31
33{
34 APPLOG_TRACE("{}::{}", hpp::type_name_str(*this), __func__);
35
36 if(initialized_)
37 {
39 initialized_ = false;
40 }
41
42 return true;
43}
44
46{
47 if(!initialized_)
48 {
49 return;
50 }
51
52 APP_SCOPE_PERF("Particles/System Update");
53
54 auto& registry = *scn.registry;
55
56 // Update all particle emitter components that have both transform and particle emitter components
58
59 // this code should be thread safe as each task works with a whole hierarchy and
60 // there is no interleaving between tasks.
61 std::for_each(poolstl::par,//std::execution::par,
62 view.begin(),
63 view.end(),
64 [&](entt::entity entity)
65 {
66 // This is not needed as we dont cal .get on any assets here
67 // tpp::this_thread::register_this_thread();
68 APP_SCOPE_PERF_THREAD("Particle/Update Emitter","Pool Thread");
69 auto& transform_comp = view.get<transform_component>(entity);
70 auto& emitter_comp = view.get<particle_emitter_component>(entity);
71 // Same grace as model_system: leave newly created as "used" for one frame.
72 if(emitter_comp.is_newly_created())
73 {
74 emitter_comp.set_last_render_frame(uint64_t(gfx::get_render_frame()));
75 }
76 const auto& world_transform = transform_comp.get_transform_global();
77 emitter_comp.update_emitter(world_transform, dt);
78 });
79
80 // Awake GPU emitters only — renderer-based freeze clears pending_pack.
82}
83
84} // namespace unravel
Component that wraps the soa particle system emitter.
auto deinit(rtti::context &ctx) -> bool
Deinitializes the particle system.
auto init(rtti::context &ctx) -> bool
Initializes the particle system.
void on_frame_before_render(scene &scn, delta_t dt)
Updates all particle emitters in the scene.
Component that handles transformations (position, rotation, scale, etc.) in the ACE framework.
std::chrono::duration< float > delta_t
uint16_t view
#define APPLOG_TRACE(...)
Definition logging.h:17
void init(uint16_t max_emitters)
Initialize the soa particle system.
void shutdown()
Shutdown and free all emitters / GPU resources.
void sync_gpu_simulation()
Flush staged GPU spawns and advance resident GPU sim for awake emitters.
void init_gpu(rtti::context &ctx)
Load compute pack program and enable GPU backend when available.
#define APP_SCOPE_PERF(name_literal)
Create a scoped performance timer that records to the timeline profiler. Only accepts string literals...
Definition profiler.h:675
entt::handle entity
Represents a scene in the ACE framework, managing entities and their relationships.
Definition scene.h:70
std::unique_ptr< entt::registry > registry
The registry that manages all entities in the scene.
Definition scene.h:187