Blender  V2.93
particles.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "render/particles.h"
18 #include "device/device.h"
19 #include "render/scene.h"
20 #include "render/stats.h"
21 
22 #include "util/util_foreach.h"
23 #include "util/util_hash.h"
24 #include "util/util_logging.h"
25 #include "util/util_map.h"
26 #include "util/util_progress.h"
27 #include "util/util_vector.h"
28 
30 
31 /* Particle System */
32 
34 {
35  NodeType *type = NodeType::add("particle_system", create);
36  return type;
37 }
38 
40 {
41 }
42 
44 {
45 }
46 
48 {
50 }
51 
52 /* Particle System Manager */
53 
55 {
56  need_update_ = true;
57 }
58 
60 {
61 }
62 
64  DeviceScene *dscene,
65  Scene *scene,
66  Progress &progress)
67 {
68  /* count particles.
69  * adds one dummy particle at the beginning to avoid invalid lookups,
70  * in case a shader uses particle info without actual particle data. */
71  int num_particles = 1;
72  for (size_t j = 0; j < scene->particle_systems.size(); j++)
73  num_particles += scene->particle_systems[j]->particles.size();
74 
75  KernelParticle *kparticles = dscene->particles.alloc(num_particles);
76 
77  /* dummy particle */
78  memset(kparticles, 0, sizeof(KernelParticle));
79 
80  int i = 1;
81  for (size_t j = 0; j < scene->particle_systems.size(); j++) {
83 
84  for (size_t k = 0; k < psys->particles.size(); k++) {
85  /* pack in texture */
86  Particle &pa = psys->particles[k];
87 
88  kparticles[i].index = pa.index;
89  kparticles[i].age = pa.age;
90  kparticles[i].lifetime = pa.lifetime;
91  kparticles[i].size = pa.size;
92  kparticles[i].rotation = pa.rotation;
93  kparticles[i].location = float3_to_float4(pa.location);
94  kparticles[i].velocity = float3_to_float4(pa.velocity);
96 
97  i++;
98 
99  if (progress.get_cancel())
100  return;
101  }
102  }
103 
104  dscene->particles.copy_to_device();
105 }
106 
108  DeviceScene *dscene,
109  Scene *scene,
110  Progress &progress)
111 {
112  if (!need_update())
113  return;
114 
115  scoped_callback_timer timer([scene](double time) {
116  if (scene->update_stats) {
117  scene->update_stats->particles.times.add_entry({"device_update", time});
118  }
119  });
120 
121  VLOG(1) << "Total " << scene->particle_systems.size() << " particle systems.";
122 
123  device_free(device, dscene);
124 
125  progress.set_status("Updating Particle Systems", "Copying Particles to device");
126  device_update_particles(device, dscene, scene, progress);
127 
128  if (progress.get_cancel())
129  return;
130 
131  need_update_ = false;
132 }
133 
135 {
136  dscene->particles.free();
137 }
138 
140 {
141  need_update_ = true;
142 }
143 
145 {
146  return need_update_;
147 }
148 
_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:121
Definition: device.h:293
void device_free(Device *device, DeviceScene *dscene)
Definition: particles.cpp:134
void tag_update(Scene *scene)
Definition: particles.cpp:139
bool need_update() const
Definition: particles.cpp:144
void device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)
Definition: particles.cpp:107
void device_update_particles(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)
Definition: particles.cpp:63
bool get_cancel()
T * alloc(size_t width, size_t height=0, size_t depth=0)
void copy_to_device()
double time
Scene scene
#define CCL_NAMESPACE_END
CCL_NAMESPACE_BEGIN NODE_DEFINE(ParticleSystem)
Definition: particles.cpp:33
float4 angular_velocity
static NodeType * add(const char *name, CreateFunc create, Type type=NONE, const NodeType *base=NULL)
Definition: node.h:98
ParticleData * particles
NODE_DECLARE ParticleSystem()
Definition: particles.cpp:39
void tag_update(Scene *scene)
Definition: particles.cpp:47
float age
Definition: particles.h:36
float lifetime
float3 location
Definition: particles.h:38
float size
Definition: particles.h:40
float4 rotation
Definition: particles.h:39
float3 velocity
Definition: particles.h:41
float3 angular_velocity
Definition: particles.h:42
int index
Definition: particles.h:35
ParticleSystemManager * particle_system_manager
Definition: scene.h:248
vector< ParticleSystem * > particle_systems
Definition: scene.h:238
SceneUpdateStats * update_stats
Definition: scene.h:270
#define VLOG(severity)
Definition: util_logging.h:50
ccl_device_inline float4 float3_to_float4(const float3 a)
Definition: util_math.h:420