From f0d247748408bd10f49c54d3a28a925e37683c4b Mon Sep 17 00:00:00 2001 From: Lukas Toenne Date: Fri, 31 Aug 2012 17:27:08 +0000 Subject: Fix for #32184 and redesign of particle storage in Cycles. The particle data used by the Particle Info node was stored in cycles as a list in each object. This is a problem when the particle emitter mesh is hidden: Objects in cycles are only intended as instances of renderable meshes, so when hiding the emitter mesh the particle data doesn't get stored either. Also the particle data can potentially be copied to multiple instances of the same object, which is a waste of texture space. The solution in this patch is to make a completely separate list of particle systems in the Cycles scene data. This way the particle data can be generated even when the emitter object itself is not visible. --- intern/cycles/blender/blender_object.cpp | 9 +-- intern/cycles/blender/blender_particles.cpp | 119 +++++++++++++++++++--------- intern/cycles/blender/blender_sync.cpp | 9 +++ intern/cycles/blender/blender_sync.h | 6 +- intern/cycles/blender/blender_util.h | 11 +++ intern/cycles/render/CMakeLists.txt | 2 + intern/cycles/render/object.cpp | 40 ---------- intern/cycles/render/object.h | 8 -- intern/cycles/render/particles.cpp | 118 +++++++++++++++++++++++++++ intern/cycles/render/particles.h | 69 ++++++++++++++++ intern/cycles/render/scene.cpp | 15 +++- intern/cycles/render/scene.h | 4 + 12 files changed, 314 insertions(+), 96 deletions(-) create mode 100644 intern/cycles/render/particles.cpp create mode 100644 intern/cycles/render/particles.h (limited to 'intern') diff --git a/intern/cycles/blender/blender_object.cpp b/intern/cycles/blender/blender_object.cpp index eb9cc7bc4de..297ce5363ca 100644 --- a/intern/cycles/blender/blender_object.cpp +++ b/intern/cycles/blender/blender_object.cpp @@ -247,11 +247,8 @@ void BlenderSync::sync_object(BL::Object b_parent, int b_index, BL::Object b_ob, scene->object_manager->tag_update(scene); } - /* updated dupli objects require particle sync */ - bool need_particle_update = object_need_particle_update(b_ob); - /* object sync */ - if(object_updated || (object->mesh && object->mesh->need_update) || need_particle_update) { + if(object_updated || (object->mesh && object->mesh->need_update)) { object->name = b_ob.name().c_str(); object->pass_id = b_ob.pass_index(); object->tfm = tfm; @@ -277,10 +274,6 @@ void BlenderSync::sync_object(BL::Object b_parent, int b_index, BL::Object b_ob, object->particle_id = particle_id; - /* particle sync */ - if (need_particle_update) - sync_particles(object, b_ob); - object->tag_update(scene); } } diff --git a/intern/cycles/blender/blender_particles.cpp b/intern/cycles/blender/blender_particles.cpp index e32c80a51b5..337ec36d107 100644 --- a/intern/cycles/blender/blender_particles.cpp +++ b/intern/cycles/blender/blender_particles.cpp @@ -16,9 +16,9 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include "object.h" - #include "mesh.h" +#include "particles.h" + #include "blender_sync.h" #include "blender_util.h" @@ -31,7 +31,7 @@ CCL_NAMESPACE_BEGIN /* Particles Sync */ -bool BlenderSync::object_need_particle_update(BL::Object b_ob) +bool BlenderSync::psys_need_update(BL::ParticleSystem b_psys) { /* Particle data is only needed for * a) Billboard render mode if object's own material uses particle info @@ -41,9 +41,7 @@ bool BlenderSync::object_need_particle_update(BL::Object b_ob) */ bool need_update = false; - BL::Object::particle_systems_iterator b_psys; - for (b_ob.particle_systems.begin(b_psys); b_psys != b_ob.particle_systems.end(); ++b_psys) { - switch (b_psys->settings().render_type()) { + switch (b_psys.settings().render_type()) { /* XXX not implemented yet! * billboards/strands would become part of the mesh data (?), * so the mesh attributes would store whether particle info is required. @@ -61,7 +59,7 @@ bool BlenderSync::object_need_particle_update(BL::Object b_ob) #endif case BL::ParticleSettings::render_type_OBJECT: { - BL::Object b_dupli_ob = b_psys->settings().dupli_object(); + BL::Object b_dupli_ob = b_psys.settings().dupli_object(); if (b_dupli_ob) { BL::ID key = (BKE_object_is_modified(b_dupli_ob))? b_dupli_ob: b_dupli_ob.data(); Mesh *mesh = mesh_map.find(key); @@ -73,7 +71,7 @@ bool BlenderSync::object_need_particle_update(BL::Object b_ob) } case BL::ParticleSettings::render_type_GROUP: { - BL::Group b_dupli_group = b_psys->settings().dupli_group(); + BL::Group b_dupli_group = b_psys.settings().dupli_group(); if (b_dupli_group) { BL::Group::objects_iterator b_gob; for (b_dupli_group.objects.begin(b_gob); b_gob != b_dupli_group.objects.end(); ++b_gob) { @@ -90,7 +88,6 @@ bool BlenderSync::object_need_particle_update(BL::Object b_ob) default: /* avoid compiler warning */ break; - } } return need_update; @@ -117,50 +114,98 @@ static bool use_particle(BL::Particle b_pa) return b_pa.is_exist() && b_pa.is_visible() && b_pa.alive_state()==BL::Particle::alive_state_ALIVE; } +static int psys_count_particles(BL::ParticleSystem b_psys) +{ + int tot = 0; + BL::ParticleSystem::particles_iterator b_pa; + for(b_psys.particles.begin(b_pa); b_pa != b_psys.particles.end(); ++b_pa) { + if(use_particle(*b_pa)) + ++tot; + } + return tot; +} + int BlenderSync::object_count_particles(BL::Object b_ob) { int tot = 0; BL::Object::particle_systems_iterator b_psys; for(b_ob.particle_systems.begin(b_psys); b_psys != b_ob.particle_systems.end(); ++b_psys) { - if (use_particle_system(*b_psys)) { - BL::ParticleSystem::particles_iterator b_pa; - for(b_psys->particles.begin(b_pa); b_pa != b_psys->particles.end(); ++b_pa) { - if(use_particle(*b_pa)) - ++tot; - } - } + if (use_particle_system(*b_psys)) + tot += psys_count_particles(*b_psys); } return tot; } -void BlenderSync::sync_particles(Object *ob, BL::Object b_ob) +void BlenderSync::sync_particles(BL::Object b_ob, BL::ParticleSystem b_psys) { - int tot = object_count_particles(b_ob); + /* depending on settings the psys may not even be rendered */ + if (!use_particle_system(b_psys)) + return; - ob->particles.clear(); - ob->particles.reserve(tot); + /* key to lookup particle system */ + ParticleSystemKey key(b_ob, b_psys); + ParticleSystem *psys; - int index; - BL::Object::particle_systems_iterator b_psys; - for(b_ob.particle_systems.begin(b_psys); b_psys != b_ob.particle_systems.end(); ++b_psys) { - if (use_particle_system(*b_psys)) { - int pa_index = 0; - BL::ParticleSystem::particles_iterator b_pa; - for(b_psys->particles.begin(b_pa), index = 0; b_pa != b_psys->particles.end(); ++b_pa, ++index) { - if(use_particle(*b_pa)) { - Particle pa; - - pa.index = pa_index; - pa.age = b_scene.frame_current() - b_pa->birth_time(); - pa.lifetime = b_pa->lifetime(); - - ob->particles.push_back(pa); - } + /* test if we need to sync */ + bool object_updated = false; + + if(particle_system_map.sync(&psys, b_ob, b_ob, key)) + object_updated = true; + + bool need_update = psys_need_update(b_psys); + + if (object_updated || need_update) { + int tot = psys_count_particles(b_psys); + psys->particles.clear(); + psys->particles.reserve(tot); + + int index = 0; + BL::ParticleSystem::particles_iterator b_pa; + for(b_psys.particles.begin(b_pa); b_pa != b_psys.particles.end(); ++b_pa) { + if(use_particle(*b_pa)) { + Particle pa; + + pa.index = index; + pa.age = b_scene.frame_current() - b_pa->birth_time(); + pa.lifetime = b_pa->lifetime(); - ++pa_index; + psys->particles.push_back(pa); } + + ++index; } } } +void BlenderSync::sync_particle_systems() +{ + /* layer data */ + uint scene_layer = render_layer.scene_layer; + + particle_system_map.pre_sync(); + + /* object loop */ + BL::Scene::objects_iterator b_ob; + BL::Scene b_sce = b_scene; + + for(; b_sce; b_sce = b_sce.background_set()) { + for(b_sce.objects.begin(b_ob); b_ob != b_sce.objects.end(); ++b_ob) { + bool hide = (render_layer.use_viewport_visibility)? b_ob->hide(): b_ob->hide_render(); + uint ob_layer = get_layer(b_ob->layers(), b_ob->layers_local_view(), object_is_light(*b_ob)); + CYCLES_LOCAL_LAYER_HACK(render_layer.use_localview, ob_layer); + hide = hide || !(ob_layer & scene_layer); + + if(!hide) { + BL::Object::particle_systems_iterator b_psys; + for(b_ob->particle_systems.begin(b_psys); b_psys != b_ob->particle_systems.end(); ++b_psys) + sync_particles(*b_ob, *b_psys); + } + } + } + + /* handle removed data and modified pointers */ + if(particle_system_map.post_sync()) + scene->particle_system_manager->tag_update(scene); +} + CCL_NAMESPACE_END diff --git a/intern/cycles/blender/blender_sync.cpp b/intern/cycles/blender/blender_sync.cpp index ae28453a696..e8bd2d59115 100644 --- a/intern/cycles/blender/blender_sync.cpp +++ b/intern/cycles/blender/blender_sync.cpp @@ -46,6 +46,7 @@ BlenderSync::BlenderSync(BL::BlendData b_data_, BL::Scene b_scene_, Scene *scene object_map(&scene_->objects), mesh_map(&scene_->meshes), light_map(&scene_->lights), + particle_system_map(&scene_->particle_systems), world_map(NULL), world_recalc(false), experimental(false) @@ -95,6 +96,12 @@ bool BlenderSync::sync_recalc() if(b_ob->is_updated_data() || b_ob->data().is_updated()) light_map.set_recalc(*b_ob); } + + if(b_ob->is_updated_data() || b_ob->data().is_updated()) { + BL::Object::particle_systems_iterator b_psys; + for (b_ob->particle_systems.begin(b_psys); b_psys != b_ob->particle_systems.end(); ++b_psys) + particle_system_map.set_recalc(*b_ob); + } } BL::BlendData::meshes_iterator b_mesh; @@ -118,6 +125,7 @@ bool BlenderSync::sync_recalc() object_map.has_recalc() || light_map.has_recalc() || mesh_map.has_recalc() || + particle_system_map.has_recalc() || BlendDataObjects_is_updated_get(&b_data.ptr) || world_recalc; @@ -131,6 +139,7 @@ void BlenderSync::sync_data(BL::SpaceView3D b_v3d, BL::Object b_override, const sync_film(); sync_shaders(); sync_objects(b_v3d); + sync_particle_systems(); sync_motion(b_v3d, b_override); } diff --git a/intern/cycles/blender/blender_sync.h b/intern/cycles/blender/blender_sync.h index 6065235a278..f3bd06b3a53 100644 --- a/intern/cycles/blender/blender_sync.h +++ b/intern/cycles/blender/blender_sync.h @@ -77,6 +77,7 @@ private: void sync_world(); void sync_render_layers(BL::SpaceView3D b_v3d, const char *layer); void sync_shaders(); + void sync_particle_systems(); void sync_nodes(Shader *shader, BL::ShaderNodeTree b_ntree); Mesh *sync_mesh(BL::Object b_ob, bool object_updated); @@ -85,14 +86,14 @@ private: void sync_background_light(); void sync_mesh_motion(BL::Object b_ob, Mesh *mesh, int motion); void sync_camera_motion(BL::Object b_ob, int motion); - void sync_particles(Object *ob, BL::Object b_ob); + void sync_particles(BL::Object b_ob, BL::ParticleSystem b_psys); /* util */ void find_shader(BL::ID id, vector& used_shaders, int default_shader); bool BKE_object_is_modified(BL::Object b_ob); bool object_is_mesh(BL::Object b_ob); bool object_is_light(BL::Object b_ob); - bool object_need_particle_update(BL::Object b_ob); + bool psys_need_update(BL::ParticleSystem b_psys); int object_count_particles(BL::Object b_ob); /* variables */ @@ -103,6 +104,7 @@ private: id_map object_map; id_map mesh_map; id_map light_map; + id_map particle_system_map; set mesh_synced; void *world_map; bool world_recalc; diff --git a/intern/cycles/blender/blender_util.h b/intern/cycles/blender/blender_util.h index ebbd4e1221c..b6609377561 100644 --- a/intern/cycles/blender/blender_util.h +++ b/intern/cycles/blender/blender_util.h @@ -389,6 +389,17 @@ struct ObjectKey { { return (parent < k.parent || (parent == k.parent && (index < k.index || (index == k.index && ob < k.ob)))); } }; +struct ParticleSystemKey { + void *ob; + void *psys; + + ParticleSystemKey(void *ob_, void *psys_) + : ob(ob_), psys(psys_) {} + + bool operator<(const ParticleSystemKey& k) const + { return (ob < k.ob && psys < k.psys); } +}; + CCL_NAMESPACE_END #endif /* __BLENDER_UTIL_H__ */ diff --git a/intern/cycles/render/CMakeLists.txt b/intern/cycles/render/CMakeLists.txt index 4d4fbfe6814..85b476e60d9 100644 --- a/intern/cycles/render/CMakeLists.txt +++ b/intern/cycles/render/CMakeLists.txt @@ -27,6 +27,7 @@ set(SRC nodes.cpp object.cpp osl.cpp + particles.cpp scene.cpp session.cpp shader.cpp @@ -51,6 +52,7 @@ set(SRC_HEADERS nodes.h object.h osl.h + particles.h scene.h session.h shader.h diff --git a/intern/cycles/render/object.cpp b/intern/cycles/render/object.cpp index 0fe227fd171..7389b239627 100644 --- a/intern/cycles/render/object.cpp +++ b/intern/cycles/render/object.cpp @@ -249,38 +249,6 @@ void ObjectManager::device_update_transforms(Device *device, DeviceScene *dscene device->tex_alloc("__object_flag", dscene->object_flag); } -void ObjectManager::device_update_particles(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress) -{ - /* count particles. - * adds one dummy particle at the beginning to avoid invalid lookups, - * in case a shader uses particle info without actual particle data. - */ - int num_particles = 1; - foreach(Object *ob, scene->objects) - num_particles += ob->particles.size(); - - float4 *particles = dscene->particles.resize(PARTICLE_SIZE*num_particles); - - /* dummy particle */ - particles[0] = make_float4(0.0f, 0.0f, 0.0f, 0.0f); - - int i = 1; - foreach(Object *ob, scene->objects) { - foreach(Particle &pa, ob->particles) { - /* pack in texture */ - int offset = i*PARTICLE_SIZE; - - particles[offset] = make_float4(pa.index, pa.age, pa.lifetime, 0.0f); - - i++; - - if(progress.get_cancel()) return; - } - } - - device->tex_alloc("__particles", dscene->particles); -} - void ObjectManager::device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress) { if(!need_update) @@ -306,11 +274,6 @@ void ObjectManager::device_update(Device *device, DeviceScene *dscene, Scene *sc if(progress.get_cancel()) return; - progress.set_status("Updating Objects", "Copying Particles to device"); - device_update_particles(device, dscene, scene, progress); - - if(progress.get_cancel()) return; - need_update = false; } @@ -321,9 +284,6 @@ void ObjectManager::device_free(Device *device, DeviceScene *dscene) device->tex_free(dscene->object_flag); dscene->object_flag.clear(); - - device->tex_free(dscene->particles); - dscene->particles.clear(); } void ObjectManager::apply_static_transforms(Scene *scene, Progress& progress) diff --git a/intern/cycles/render/object.h b/intern/cycles/render/object.h index 9b2f5bc8768..88677d79dff 100644 --- a/intern/cycles/render/object.h +++ b/intern/cycles/render/object.h @@ -35,12 +35,6 @@ struct Transform; /* Object */ -struct Particle { - int index; - float age; - float lifetime; -}; - class Object { public: Mesh *mesh; @@ -56,7 +50,6 @@ public: bool use_holdout; int particle_id; - vector particles; Object(); ~Object(); @@ -78,7 +71,6 @@ public: void device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress); void device_update_transforms(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress); - void device_update_particles(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress); void device_free(Device *device, DeviceScene *dscene); void tag_update(Scene *scene); diff --git a/intern/cycles/render/particles.cpp b/intern/cycles/render/particles.cpp new file mode 100644 index 00000000000..539a2333506 --- /dev/null +++ b/intern/cycles/render/particles.cpp @@ -0,0 +1,118 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "device.h" +#include "particles.h" +#include "scene.h" + +#include "util_foreach.h" +#include "util_map.h" +#include "util_progress.h" +#include "util_vector.h" + +CCL_NAMESPACE_BEGIN + +/* Particle System */ + +ParticleSystem::ParticleSystem() +{ +} + +ParticleSystem::~ParticleSystem() +{ +} + +void ParticleSystem::tag_update(Scene *scene) +{ + scene->particle_system_manager->need_update = true; +} + +/* Particle System Manager */ + +ParticleSystemManager::ParticleSystemManager() +{ + need_update = true; +} + +ParticleSystemManager::~ParticleSystemManager() +{ +} + +void ParticleSystemManager::device_update_particles(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress) +{ + /* count particles. + * adds one dummy particle at the beginning to avoid invalid lookups, + * in case a shader uses particle info without actual particle data. + */ + int num_particles = 1; + foreach(ParticleSystem *psys, scene->particle_systems) + num_particles += psys->particles.size(); + + float4 *particles = dscene->particles.resize(PARTICLE_SIZE*num_particles); + + /* dummy particle */ + particles[0] = make_float4(0.0f, 0.0f, 0.0f, 0.0f); + + int i = 1; + foreach(ParticleSystem *psys, scene->particle_systems) { + foreach(Particle &pa, psys->particles) { + /* pack in texture */ + int offset = i*PARTICLE_SIZE; + + particles[offset] = make_float4(pa.index, pa.age, pa.lifetime, 0.0f); + + i++; + + if(progress.get_cancel()) return; + } + } + + device->tex_alloc("__particles", dscene->particles); +} + +void ParticleSystemManager::device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress) +{ + if(!need_update) + return; + + device_free(device, dscene); + + if(scene->particle_systems.size() == 0) + return; + + progress.set_status("Updating Particle Systems", "Copying Particles to device"); + device_update_particles(device, dscene, scene, progress); + + if(progress.get_cancel()) return; + + need_update = false; +} + +void ParticleSystemManager::device_free(Device *device, DeviceScene *dscene) +{ + device->tex_free(dscene->particles); + dscene->particles.clear(); +} + +void ParticleSystemManager::tag_update(Scene *scene) +{ + need_update = true; +} + +CCL_NAMESPACE_END + diff --git a/intern/cycles/render/particles.h b/intern/cycles/render/particles.h new file mode 100644 index 00000000000..e5481ad4c24 --- /dev/null +++ b/intern/cycles/render/particles.h @@ -0,0 +1,69 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef __PARTICLES_H__ +#define __PARTICLES_H__ + +#include "util_types.h" +#include "util_vector.h" + +CCL_NAMESPACE_BEGIN + +class Device; +class DeviceScene; +class Progress; +class Scene; + +/* Particle System */ + +struct Particle { + int index; + float age; + float lifetime; +}; + +class ParticleSystem { +public: + ParticleSystem(); + ~ParticleSystem(); + + void tag_update(Scene *scene); + + vector particles; +}; + +/* ParticleSystem Manager */ + +class ParticleSystemManager { +public: + bool need_update; + + ParticleSystemManager(); + ~ParticleSystemManager(); + + void device_update_particles(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress); + void device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress); + void device_free(Device *device, DeviceScene *dscene); + + void tag_update(Scene *scene); +}; + +CCL_NAMESPACE_END + +#endif /* __PARTICLES_H__ */ + diff --git a/intern/cycles/render/scene.cpp b/intern/cycles/render/scene.cpp index 45c8a05c27d..4f5420dec61 100644 --- a/intern/cycles/render/scene.cpp +++ b/intern/cycles/render/scene.cpp @@ -28,6 +28,7 @@ #include "shader.h" #include "mesh.h" #include "object.h" +#include "particles.h" #include "scene.h" #include "svm.h" #include "osl.h" @@ -53,6 +54,7 @@ Scene::Scene(const SceneParams& params_) integrator = new Integrator(); image_manager = new ImageManager(); shader_manager = ShaderManager::create(this); + particle_system_manager = new ParticleSystemManager(); } Scene::~Scene() @@ -92,9 +94,14 @@ Scene::~Scene() delete o; foreach(Light *l, lights) delete l; + foreach(ParticleSystem *p, particle_systems) + delete p; if(device) image_manager->device_free(device, &dscene); delete image_manager; + + if(device) particle_system_manager->device_free(device, &dscene); + delete particle_system_manager; } void Scene::device_update(Device *device_, Progress& progress) @@ -149,6 +156,11 @@ void Scene::device_update(Device *device_, Progress& progress) if(progress.get_cancel()) return; + progress.set_status("Updating Particle Systems"); + particle_system_manager->device_update(device, &dscene, this, progress); + + if(progress.get_cancel()) return; + progress.set_status("Updating Filter"); filter->device_update(device, &dscene); @@ -210,7 +222,8 @@ bool Scene::need_reset() || light_manager->need_update || filter->need_update || integrator->need_update - || shader_manager->need_update); + || shader_manager->need_update + || particle_system_manager->need_update); } CCL_NAMESPACE_END diff --git a/intern/cycles/render/scene.h b/intern/cycles/render/scene.h index 8b9944cb76e..d9341af08e0 100644 --- a/intern/cycles/render/scene.h +++ b/intern/cycles/render/scene.h @@ -46,6 +46,8 @@ class Mesh; class MeshManager; class Object; class ObjectManager; +class ParticleSystemManager; +class ParticleSystem; class Shader; class ShaderManager; class Progress; @@ -154,6 +156,7 @@ public: vector meshes; vector shaders; vector lights; + vector particle_systems; /* data managers */ ImageManager *image_manager; @@ -161,6 +164,7 @@ public: ShaderManager *shader_manager; MeshManager *mesh_manager; ObjectManager *object_manager; + ParticleSystemManager *particle_system_manager; /* default shaders */ int default_surface; -- cgit v1.2.3