Blender  V3.3
scene/particles.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #include "scene/particles.h"
5 #include "device/device.h"
6 #include "scene/scene.h"
7 #include "scene/stats.h"
8 
9 #include "util/foreach.h"
10 #include "util/hash.h"
11 #include "util/log.h"
12 #include "util/map.h"
13 #include "util/progress.h"
14 #include "util/vector.h"
15 
17 
18 /* Particle System */
19 
21 {
22  NodeType *type = NodeType::add("particle_system", create);
23  return type;
24 }
25 
27 {
28 }
29 
31 {
32 }
33 
35 {
37 }
38 
39 /* Particle System Manager */
40 
42 {
43  need_update_ = true;
44 }
45 
47 {
48 }
49 
51  DeviceScene *dscene,
52  Scene *scene,
53  Progress &progress)
54 {
55  /* count particles.
56  * adds one dummy particle at the beginning to avoid invalid lookups,
57  * in case a shader uses particle info without actual particle data. */
58  int num_particles = 1;
59  for (size_t j = 0; j < scene->particle_systems.size(); j++)
60  num_particles += scene->particle_systems[j]->particles.size();
61 
62  KernelParticle *kparticles = dscene->particles.alloc(num_particles);
63 
64  /* dummy particle */
65  memset(kparticles, 0, sizeof(KernelParticle));
66 
67  int i = 1;
68  for (size_t j = 0; j < scene->particle_systems.size(); j++) {
70 
71  for (size_t k = 0; k < psys->particles.size(); k++) {
72  /* pack in texture */
73  Particle &pa = psys->particles[k];
74 
75  kparticles[i].index = pa.index;
76  kparticles[i].age = pa.age;
77  kparticles[i].lifetime = pa.lifetime;
78  kparticles[i].size = pa.size;
79  kparticles[i].rotation = pa.rotation;
80  kparticles[i].location = float3_to_float4(pa.location);
81  kparticles[i].velocity = float3_to_float4(pa.velocity);
83 
84  i++;
85 
86  if (progress.get_cancel())
87  return;
88  }
89  }
90 
91  dscene->particles.copy_to_device();
92 }
93 
95  DeviceScene *dscene,
96  Scene *scene,
97  Progress &progress)
98 {
99  if (!need_update())
100  return;
101 
102  scoped_callback_timer timer([scene](double time) {
103  if (scene->update_stats) {
104  scene->update_stats->particles.times.add_entry({"device_update", time});
105  }
106  });
107 
108  VLOG_INFO << "Total " << scene->particle_systems.size() << " particle systems.";
109 
110  device_free(device, dscene);
111 
112  progress.set_status("Updating Particle Systems", "Copying Particles to device");
113  device_update_particles(device, dscene, scene, progress);
114 
115  if (progress.get_cancel())
116  return;
117 
118  need_update_ = false;
119 }
120 
122 {
123  dscene->particles.free();
124 }
125 
127 {
128  need_update_ = true;
129 }
130 
132 {
133  return need_update_;
134 }
135 
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
device_vector< KernelParticle > particles
Definition: scene.h:115
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:90
T * alloc(size_t width, size_t height=0, size_t depth=0)
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
double time
Scene scene
#define VLOG_INFO
Definition: log.h:77
std::unique_ptr< IDProperty, IDPropertyDeleter > create(StringRefNull prop_name, int32_t value)
Allocate a new IDProperty of type IDP_INT, set its name and value.
CCL_NAMESPACE_BEGIN NODE_DEFINE(ParticleSystem)
float4 angular_velocity
static NodeType * add(const char *name, CreateFunc create, Type type=NONE, const NodeType *base=NULL)
ParticleData * particles
NODE_DECLARE ParticleSystem()
void tag_update(Scene *scene)
float age
Definition: particles.h:23
float lifetime
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
ParticleSystemManager * particle_system_manager
Definition: scene.h:227
vector< ParticleSystem * > particle_systems
Definition: scene.h:217
SceneUpdateStats * update_stats
Definition: scene.h:249
ccl_device_inline float4 float3_to_float4(const float3 a)
Definition: util/math.h:505