Blender V5.0
scene/particles.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "scene/particles.h"
6#include "device/device.h"
7#include "scene/scene.h"
8#include "scene/stats.h"
9
10#include "util/log.h"
11#include "util/progress.h"
12
14
15/* Particle System */
16
18{
19 NodeType *type = NodeType::add("particle_system", create);
20 return type;
21}
22
23ParticleSystem::ParticleSystem() : Node(get_node_type()) {}
24
26
28{
29 scene->particle_system_manager->tag_update(scene);
30}
31
32/* Particle System Manager */
33
35{
36 need_update_ = true;
37}
38
40
42 DeviceScene *dscene,
43 Scene *scene,
44 Progress &progress)
45{
46 /* count particles.
47 * adds one dummy particle at the beginning to avoid invalid lookups,
48 * in case a shader uses particle info without actual particle data. */
49 int num_particles = 1;
50 for (size_t j = 0; j < scene->particle_systems.size(); j++) {
51 num_particles += scene->particle_systems[j]->particles.size();
52 }
53
54 KernelParticle *kparticles = dscene->particles.alloc(num_particles);
55
56 /* dummy particle */
57 *kparticles = KernelParticle{};
58
59 int i = 1;
60 for (size_t j = 0; j < scene->particle_systems.size(); j++) {
61 ParticleSystem *psys = scene->particle_systems[j];
62
63 for (size_t k = 0; k < psys->particles.size(); k++) {
64 /* pack in texture */
65 const Particle &pa = psys->particles[k];
66
67 kparticles[i].index = pa.index;
68 kparticles[i].age = pa.age;
69 kparticles[i].lifetime = pa.lifetime;
70 kparticles[i].size = pa.size;
71 kparticles[i].rotation = pa.rotation;
72 kparticles[i].location = make_float4(pa.location);
73 kparticles[i].velocity = make_float4(pa.velocity);
75
76 i++;
77
78 if (progress.get_cancel()) {
79 return;
80 }
81 }
82 }
83
84 dscene->particles.copy_to_device();
85}
86
88 DeviceScene *dscene,
89 Scene *scene,
90 Progress &progress)
91{
92 if (!need_update()) {
93 return;
94 }
95
96 const scoped_callback_timer timer([scene](double time) {
97 if (scene->update_stats) {
98 scene->update_stats->particles.times.add_entry({"device_update", time});
99 }
100 });
101
102 LOG_INFO << "Total " << scene->particle_systems.size() << " particle systems.";
103
104 device_free(device, dscene);
105
106 progress.set_status("Updating Particle Systems", "Copying Particles to device");
107 device_update_particles(device, dscene, scene, progress);
108
109 if (progress.get_cancel()) {
110 return;
111 }
112
113 need_update_ = false;
114}
115
117{
118 dscene->particles.free();
119}
120
122{
123 need_update_ = true;
124}
125
127{
128 return need_update_;
129}
130
device_vector< KernelParticle > particles
Definition devicescene.h:73
void device_free(Device *device, DeviceScene *dscene)
void tag_update(Scene *scene)
void device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)
void device_update_particles(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)
bool get_cancel() const
Definition progress.h:77
void set_status(const string &status_, const string &substatus_="")
Definition progress.h:248
T * alloc(const size_t width, const size_t height=0)
size_t size() const
#define CCL_NAMESPACE_END
#define LOG_INFO
Definition log.h:106
#define NODE_DEFINE(structname)
Definition node_type.h:152
#define make_float4
float4 angular_velocity
static NodeType * add(const char *name, CreateFunc create, Type type=NONE, const NodeType *base=nullptr)
Node(const NodeType *type, ustring name=ustring())
ParticleData * particles
NODE_DECLARE ParticleSystem()
~ParticleSystem() override
void tag_update(Scene *scene)
float age
Definition particles.h:23
float3 location
Definition particles.h:25
float size
Definition particles.h:27
float4 rotation
Definition particles.h:26
float3 velocity
Definition particles.h:28
float3 angular_velocity
Definition particles.h:29
int index
Definition particles.h:22
unique_ptr< ParticleSystemManager > particle_system_manager
Definition scene.h:151
unique_ptr< SceneUpdateStats > update_stats
Definition scene.h:175
unique_ptr_vector< ParticleSystem > particle_systems
Definition scene.h:139
i
Definition text_draw.cc:230
wmTimer * timer