object.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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. #include "render/camera.h"
  17. #include "device/device.h"
  18. #include "render/light.h"
  19. #include "render/mesh.h"
  20. #include "render/curves.h"
  21. #include "render/object.h"
  22. #include "render/particles.h"
  23. #include "render/scene.h"
  24. #include "util/util_foreach.h"
  25. #include "util/util_logging.h"
  26. #include "util/util_map.h"
  27. #include "util/util_progress.h"
  28. #include "util/util_set.h"
  29. #include "util/util_vector.h"
  30. #include "util/util_murmurhash.h"
  31. #include "subd/subd_patch_table.h"
  32. CCL_NAMESPACE_BEGIN
  33. /* Global state of object transform update. */
  34. struct UpdateObjectTransformState {
  35. /* Global state used by device_update_object_transform().
  36. * Common for both threaded and non-threaded update.
  37. */
  38. /* Type of the motion required by the scene settings. */
  39. Scene::MotionType need_motion;
  40. /* Mapping from particle system to a index in packed particle array.
  41. * Only used for read.
  42. */
  43. map<ParticleSystem *, int> particle_offset;
  44. /* Mesh area.
  45. * Used to avoid calculation of mesh area multiple times. Used for both
  46. * read and write. Acquire surface_area_lock to keep it all thread safe.
  47. */
  48. map<Mesh *, float> surface_area_map;
  49. /* Motion offsets for each object. */
  50. array<uint> motion_offset;
  51. /* Packed object arrays. Those will be filled in. */
  52. uint *object_flag;
  53. KernelObject *objects;
  54. Transform *object_motion_pass;
  55. DecomposedTransform *object_motion;
  56. /* Flags which will be synchronized to Integrator. */
  57. bool have_motion;
  58. bool have_curves;
  59. /* ** Scheduling queue. ** */
  60. Scene *scene;
  61. /* Some locks to keep everything thread-safe. */
  62. thread_spin_lock queue_lock;
  63. thread_spin_lock surface_area_lock;
  64. /* First unused object index in the queue. */
  65. int queue_start_object;
  66. };
  67. /* Object */
  68. NODE_DEFINE(Object)
  69. {
  70. NodeType *type = NodeType::add("object", create);
  71. SOCKET_NODE(mesh, "Mesh", &Mesh::node_type);
  72. SOCKET_TRANSFORM(tfm, "Transform", transform_identity());
  73. SOCKET_UINT(visibility, "Visibility", ~0);
  74. SOCKET_UINT(random_id, "Random ID", 0);
  75. SOCKET_INT(pass_id, "Pass ID", 0);
  76. SOCKET_BOOLEAN(use_holdout, "Use Holdout", false);
  77. SOCKET_BOOLEAN(hide_on_missing_motion, "Hide on Missing Motion", false);
  78. SOCKET_POINT(dupli_generated, "Dupli Generated", make_float3(0.0f, 0.0f, 0.0f));
  79. SOCKET_POINT2(dupli_uv, "Dupli UV", make_float2(0.0f, 0.0f));
  80. SOCKET_TRANSFORM_ARRAY(motion, "Motion", array<Transform>());
  81. SOCKET_BOOLEAN(is_shadow_catcher, "Shadow Catcher", false);
  82. return type;
  83. }
  84. Object::Object() : Node(node_type)
  85. {
  86. particle_system = NULL;
  87. particle_index = 0;
  88. bounds = BoundBox::empty;
  89. }
  90. Object::~Object()
  91. {
  92. }
  93. void Object::update_motion()
  94. {
  95. if (!use_motion()) {
  96. return;
  97. }
  98. bool have_motion = false;
  99. for (size_t i = 0; i < motion.size(); i++) {
  100. if (motion[i] == transform_empty()) {
  101. if (hide_on_missing_motion) {
  102. /* Hide objects that have no valid previous or next
  103. * transform, for example particle that stop existing. It
  104. * would be better to handle this in the kernel and make
  105. * objects invisible outside certain motion steps. */
  106. tfm = transform_empty();
  107. motion.clear();
  108. return;
  109. }
  110. else {
  111. /* Otherwise just copy center motion. */
  112. motion[i] = tfm;
  113. }
  114. }
  115. /* Test if any of the transforms are actually different. */
  116. have_motion = have_motion || motion[i] != tfm;
  117. }
  118. /* Clear motion array if there is no actual motion. */
  119. if (!have_motion) {
  120. motion.clear();
  121. }
  122. }
  123. void Object::compute_bounds(bool motion_blur)
  124. {
  125. BoundBox mbounds = mesh->bounds;
  126. if (motion_blur && use_motion()) {
  127. array<DecomposedTransform> decomp(motion.size());
  128. transform_motion_decompose(decomp.data(), motion.data(), motion.size());
  129. bounds = BoundBox::empty;
  130. /* todo: this is really terrible. according to pbrt there is a better
  131. * way to find this iteratively, but did not find implementation yet
  132. * or try to implement myself */
  133. for (float t = 0.0f; t < 1.0f; t += (1.0f / 128.0f)) {
  134. Transform ttfm;
  135. transform_motion_array_interpolate(&ttfm, decomp.data(), motion.size(), t);
  136. bounds.grow(mbounds.transformed(&ttfm));
  137. }
  138. }
  139. else {
  140. /* No motion blur case. */
  141. if (mesh->transform_applied) {
  142. bounds = mbounds;
  143. }
  144. else {
  145. bounds = mbounds.transformed(&tfm);
  146. }
  147. }
  148. }
  149. void Object::apply_transform(bool apply_to_motion)
  150. {
  151. if (!mesh || tfm == transform_identity())
  152. return;
  153. /* triangles */
  154. if (mesh->verts.size()) {
  155. /* store matrix to transform later. when accessing these as attributes we
  156. * do not want the transform to be applied for consistency between static
  157. * and dynamic BVH, so we do it on packing. */
  158. mesh->transform_normal = transform_transposed_inverse(tfm);
  159. /* apply to mesh vertices */
  160. for (size_t i = 0; i < mesh->verts.size(); i++)
  161. mesh->verts[i] = transform_point(&tfm, mesh->verts[i]);
  162. if (apply_to_motion) {
  163. Attribute *attr = mesh->attributes.find(ATTR_STD_MOTION_VERTEX_POSITION);
  164. if (attr) {
  165. size_t steps_size = mesh->verts.size() * (mesh->motion_steps - 1);
  166. float3 *vert_steps = attr->data_float3();
  167. for (size_t i = 0; i < steps_size; i++)
  168. vert_steps[i] = transform_point(&tfm, vert_steps[i]);
  169. }
  170. Attribute *attr_N = mesh->attributes.find(ATTR_STD_MOTION_VERTEX_NORMAL);
  171. if (attr_N) {
  172. Transform ntfm = mesh->transform_normal;
  173. size_t steps_size = mesh->verts.size() * (mesh->motion_steps - 1);
  174. float3 *normal_steps = attr_N->data_float3();
  175. for (size_t i = 0; i < steps_size; i++)
  176. normal_steps[i] = normalize(transform_direction(&ntfm, normal_steps[i]));
  177. }
  178. }
  179. }
  180. /* curves */
  181. if (mesh->curve_keys.size()) {
  182. /* compute uniform scale */
  183. float3 c0 = transform_get_column(&tfm, 0);
  184. float3 c1 = transform_get_column(&tfm, 1);
  185. float3 c2 = transform_get_column(&tfm, 2);
  186. float scalar = powf(fabsf(dot(cross(c0, c1), c2)), 1.0f / 3.0f);
  187. /* apply transform to curve keys */
  188. for (size_t i = 0; i < mesh->curve_keys.size(); i++) {
  189. float3 co = transform_point(&tfm, mesh->curve_keys[i]);
  190. float radius = mesh->curve_radius[i] * scalar;
  191. /* scale for curve radius is only correct for uniform scale */
  192. mesh->curve_keys[i] = co;
  193. mesh->curve_radius[i] = radius;
  194. }
  195. if (apply_to_motion) {
  196. Attribute *curve_attr = mesh->curve_attributes.find(ATTR_STD_MOTION_VERTEX_POSITION);
  197. if (curve_attr) {
  198. /* apply transform to motion curve keys */
  199. size_t steps_size = mesh->curve_keys.size() * (mesh->motion_steps - 1);
  200. float4 *key_steps = curve_attr->data_float4();
  201. for (size_t i = 0; i < steps_size; i++) {
  202. float3 co = transform_point(&tfm, float4_to_float3(key_steps[i]));
  203. float radius = key_steps[i].w * scalar;
  204. /* scale for curve radius is only correct for uniform scale */
  205. key_steps[i] = float3_to_float4(co);
  206. key_steps[i].w = radius;
  207. }
  208. }
  209. }
  210. }
  211. /* we keep normals pointing in same direction on negative scale, notify
  212. * mesh about this in it (re)calculates normals */
  213. if (transform_negative_scale(tfm))
  214. mesh->transform_negative_scaled = true;
  215. if (bounds.valid()) {
  216. mesh->compute_bounds();
  217. compute_bounds(false);
  218. }
  219. /* tfm is not reset to identity, all code that uses it needs to check the
  220. * transform_applied boolean */
  221. }
  222. void Object::tag_update(Scene *scene)
  223. {
  224. if (mesh) {
  225. if (mesh->transform_applied)
  226. mesh->need_update = true;
  227. foreach (Shader *shader, mesh->used_shaders) {
  228. if (shader->use_mis && shader->has_surface_emission)
  229. scene->light_manager->need_update = true;
  230. }
  231. }
  232. scene->camera->need_flags_update = true;
  233. scene->curve_system_manager->need_update = true;
  234. scene->mesh_manager->need_update = true;
  235. scene->object_manager->need_update = true;
  236. }
  237. bool Object::use_motion() const
  238. {
  239. return (motion.size() > 1);
  240. }
  241. float Object::motion_time(int step) const
  242. {
  243. return (use_motion()) ? 2.0f * step / (motion.size() - 1) - 1.0f : 0.0f;
  244. }
  245. int Object::motion_step(float time) const
  246. {
  247. if (use_motion()) {
  248. for (size_t step = 0; step < motion.size(); step++) {
  249. if (time == motion_time(step)) {
  250. return step;
  251. }
  252. }
  253. }
  254. return -1;
  255. }
  256. bool Object::is_traceable() const
  257. {
  258. /* Mesh itself can be empty,can skip all such objects. */
  259. if (!bounds.valid() || bounds.size() == make_float3(0.0f, 0.0f, 0.0f)) {
  260. return false;
  261. }
  262. /* TODO(sergey): Check for mesh vertices/curves. visibility flags. */
  263. return true;
  264. }
  265. uint Object::visibility_for_tracing() const
  266. {
  267. uint trace_visibility = visibility;
  268. if (is_shadow_catcher) {
  269. trace_visibility &= ~PATH_RAY_SHADOW_NON_CATCHER;
  270. }
  271. else {
  272. trace_visibility &= ~PATH_RAY_SHADOW_CATCHER;
  273. }
  274. return trace_visibility;
  275. }
  276. int Object::get_device_index() const
  277. {
  278. return index;
  279. }
  280. /* Object Manager */
  281. ObjectManager::ObjectManager()
  282. {
  283. need_update = true;
  284. need_flags_update = true;
  285. }
  286. ObjectManager::~ObjectManager()
  287. {
  288. }
  289. void ObjectManager::device_update_object_transform(UpdateObjectTransformState *state, Object *ob)
  290. {
  291. KernelObject &kobject = state->objects[ob->index];
  292. Transform *object_motion_pass = state->object_motion_pass;
  293. Mesh *mesh = ob->mesh;
  294. uint flag = 0;
  295. /* Compute transformations. */
  296. Transform tfm = ob->tfm;
  297. Transform itfm = transform_inverse(tfm);
  298. /* Compute surface area. for uniform scale we can do avoid the many
  299. * transform calls and share computation for instances.
  300. *
  301. * TODO(brecht): Correct for displacement, and move to a better place.
  302. */
  303. float uniform_scale;
  304. float surface_area = 0.0f;
  305. float pass_id = ob->pass_id;
  306. float random_number = (float)ob->random_id * (1.0f / (float)0xFFFFFFFF);
  307. int particle_index = (ob->particle_system) ?
  308. ob->particle_index + state->particle_offset[ob->particle_system] :
  309. 0;
  310. if (transform_uniform_scale(tfm, uniform_scale)) {
  311. map<Mesh *, float>::iterator it;
  312. /* NOTE: This isn't fully optimal and could in theory lead to multiple
  313. * threads calculating area of the same mesh in parallel. However, this
  314. * also prevents suspending all the threads when some mesh's area is
  315. * not yet known.
  316. */
  317. state->surface_area_lock.lock();
  318. it = state->surface_area_map.find(mesh);
  319. state->surface_area_lock.unlock();
  320. if (it == state->surface_area_map.end()) {
  321. size_t num_triangles = mesh->num_triangles();
  322. for (size_t j = 0; j < num_triangles; j++) {
  323. Mesh::Triangle t = mesh->get_triangle(j);
  324. float3 p1 = mesh->verts[t.v[0]];
  325. float3 p2 = mesh->verts[t.v[1]];
  326. float3 p3 = mesh->verts[t.v[2]];
  327. surface_area += triangle_area(p1, p2, p3);
  328. }
  329. state->surface_area_lock.lock();
  330. state->surface_area_map[mesh] = surface_area;
  331. state->surface_area_lock.unlock();
  332. }
  333. else {
  334. surface_area = it->second;
  335. }
  336. surface_area *= uniform_scale;
  337. }
  338. else {
  339. size_t num_triangles = mesh->num_triangles();
  340. for (size_t j = 0; j < num_triangles; j++) {
  341. Mesh::Triangle t = mesh->get_triangle(j);
  342. float3 p1 = transform_point(&tfm, mesh->verts[t.v[0]]);
  343. float3 p2 = transform_point(&tfm, mesh->verts[t.v[1]]);
  344. float3 p3 = transform_point(&tfm, mesh->verts[t.v[2]]);
  345. surface_area += triangle_area(p1, p2, p3);
  346. }
  347. }
  348. kobject.tfm = tfm;
  349. kobject.itfm = itfm;
  350. kobject.surface_area = surface_area;
  351. kobject.pass_id = pass_id;
  352. kobject.random_number = random_number;
  353. kobject.particle_index = particle_index;
  354. kobject.motion_offset = 0;
  355. if (mesh->use_motion_blur) {
  356. state->have_motion = true;
  357. }
  358. if (mesh->attributes.find(ATTR_STD_MOTION_VERTEX_POSITION)) {
  359. flag |= SD_OBJECT_HAS_VERTEX_MOTION;
  360. }
  361. if (state->need_motion == Scene::MOTION_PASS) {
  362. /* Clear motion array if there is no actual motion. */
  363. ob->update_motion();
  364. /* Compute motion transforms. */
  365. Transform tfm_pre, tfm_post;
  366. if (ob->use_motion()) {
  367. tfm_pre = ob->motion[0];
  368. tfm_post = ob->motion[ob->motion.size() - 1];
  369. }
  370. else {
  371. tfm_pre = tfm;
  372. tfm_post = tfm;
  373. }
  374. /* Motion transformations, is world/object space depending if mesh
  375. * comes with deformed position in object space, or if we transform
  376. * the shading point in world space. */
  377. if (!mesh->attributes.find(ATTR_STD_MOTION_VERTEX_POSITION)) {
  378. tfm_pre = tfm_pre * itfm;
  379. tfm_post = tfm_post * itfm;
  380. }
  381. int motion_pass_offset = ob->index * OBJECT_MOTION_PASS_SIZE;
  382. object_motion_pass[motion_pass_offset + 0] = tfm_pre;
  383. object_motion_pass[motion_pass_offset + 1] = tfm_post;
  384. }
  385. else if (state->need_motion == Scene::MOTION_BLUR) {
  386. if (ob->use_motion()) {
  387. kobject.motion_offset = state->motion_offset[ob->index];
  388. /* Decompose transforms for interpolation. */
  389. DecomposedTransform *decomp = state->object_motion + kobject.motion_offset;
  390. transform_motion_decompose(decomp, ob->motion.data(), ob->motion.size());
  391. flag |= SD_OBJECT_MOTION;
  392. state->have_motion = true;
  393. }
  394. }
  395. /* Dupli object coords and motion info. */
  396. kobject.dupli_generated[0] = ob->dupli_generated[0];
  397. kobject.dupli_generated[1] = ob->dupli_generated[1];
  398. kobject.dupli_generated[2] = ob->dupli_generated[2];
  399. kobject.numkeys = mesh->curve_keys.size();
  400. kobject.dupli_uv[0] = ob->dupli_uv[0];
  401. kobject.dupli_uv[1] = ob->dupli_uv[1];
  402. int totalsteps = mesh->motion_steps;
  403. kobject.numsteps = (totalsteps - 1) / 2;
  404. kobject.numverts = mesh->verts.size();
  405. kobject.patch_map_offset = 0;
  406. kobject.attribute_map_offset = 0;
  407. uint32_t hash_name = util_murmur_hash3(ob->name.c_str(), ob->name.length(), 0);
  408. uint32_t hash_asset = util_murmur_hash3(ob->asset_name.c_str(), ob->asset_name.length(), 0);
  409. kobject.cryptomatte_object = util_hash_to_float(hash_name);
  410. kobject.cryptomatte_asset = util_hash_to_float(hash_asset);
  411. /* Object flag. */
  412. if (ob->use_holdout) {
  413. flag |= SD_OBJECT_HOLDOUT_MASK;
  414. }
  415. state->object_flag[ob->index] = flag;
  416. /* Have curves. */
  417. if (mesh->num_curves()) {
  418. state->have_curves = true;
  419. }
  420. }
  421. bool ObjectManager::device_update_object_transform_pop_work(UpdateObjectTransformState *state,
  422. int *start_index,
  423. int *num_objects)
  424. {
  425. /* Tweakable parameter, number of objects per chunk.
  426. * Too small value will cause some extra overhead due to spin lock,
  427. * too big value might not use all threads nicely.
  428. */
  429. static const int OBJECTS_PER_TASK = 32;
  430. bool have_work = false;
  431. state->queue_lock.lock();
  432. int num_scene_objects = state->scene->objects.size();
  433. if (state->queue_start_object < num_scene_objects) {
  434. int count = min(OBJECTS_PER_TASK, num_scene_objects - state->queue_start_object);
  435. *start_index = state->queue_start_object;
  436. *num_objects = count;
  437. state->queue_start_object += count;
  438. have_work = true;
  439. }
  440. state->queue_lock.unlock();
  441. return have_work;
  442. }
  443. void ObjectManager::device_update_object_transform_task(UpdateObjectTransformState *state)
  444. {
  445. int start_index, num_objects;
  446. while (device_update_object_transform_pop_work(state, &start_index, &num_objects)) {
  447. for (int i = 0; i < num_objects; ++i) {
  448. const int object_index = start_index + i;
  449. Object *ob = state->scene->objects[object_index];
  450. device_update_object_transform(state, ob);
  451. }
  452. }
  453. }
  454. void ObjectManager::device_update_transforms(DeviceScene *dscene, Scene *scene, Progress &progress)
  455. {
  456. UpdateObjectTransformState state;
  457. state.need_motion = scene->need_motion();
  458. state.have_motion = false;
  459. state.have_curves = false;
  460. state.scene = scene;
  461. state.queue_start_object = 0;
  462. state.objects = dscene->objects.alloc(scene->objects.size());
  463. state.object_flag = dscene->object_flag.alloc(scene->objects.size());
  464. state.object_motion = NULL;
  465. state.object_motion_pass = NULL;
  466. if (state.need_motion == Scene::MOTION_PASS) {
  467. state.object_motion_pass = dscene->object_motion_pass.alloc(OBJECT_MOTION_PASS_SIZE *
  468. scene->objects.size());
  469. }
  470. else if (state.need_motion == Scene::MOTION_BLUR) {
  471. /* Set object offsets into global object motion array. */
  472. uint *motion_offsets = state.motion_offset.resize(scene->objects.size());
  473. uint motion_offset = 0;
  474. foreach (Object *ob, scene->objects) {
  475. *motion_offsets = motion_offset;
  476. motion_offsets++;
  477. /* Clear motion array if there is no actual motion. */
  478. ob->update_motion();
  479. motion_offset += ob->motion.size();
  480. }
  481. state.object_motion = dscene->object_motion.alloc(motion_offset);
  482. }
  483. /* Particle system device offsets
  484. * 0 is dummy particle, index starts at 1.
  485. */
  486. int numparticles = 1;
  487. foreach (ParticleSystem *psys, scene->particle_systems) {
  488. state.particle_offset[psys] = numparticles;
  489. numparticles += psys->particles.size();
  490. }
  491. /* NOTE: If it's just a handful of objects we deal with them in a single
  492. * thread to avoid threading overhead. However, this threshold is might
  493. * need some tweaks to make mid-complex scenes optimal.
  494. */
  495. if (scene->objects.size() < 64) {
  496. foreach (Object *ob, scene->objects) {
  497. device_update_object_transform(&state, ob);
  498. if (progress.get_cancel()) {
  499. return;
  500. }
  501. }
  502. }
  503. else {
  504. const int num_threads = TaskScheduler::num_threads();
  505. TaskPool pool;
  506. for (int i = 0; i < num_threads; ++i) {
  507. pool.push(function_bind(&ObjectManager::device_update_object_transform_task, this, &state));
  508. }
  509. pool.wait_work();
  510. if (progress.get_cancel()) {
  511. return;
  512. }
  513. }
  514. dscene->objects.copy_to_device();
  515. if (state.need_motion == Scene::MOTION_PASS) {
  516. dscene->object_motion_pass.copy_to_device();
  517. }
  518. else if (state.need_motion == Scene::MOTION_BLUR) {
  519. dscene->object_motion.copy_to_device();
  520. }
  521. dscene->data.bvh.have_motion = state.have_motion;
  522. dscene->data.bvh.have_curves = state.have_curves;
  523. dscene->data.bvh.have_instancing = true;
  524. }
  525. void ObjectManager::device_update(Device *device,
  526. DeviceScene *dscene,
  527. Scene *scene,
  528. Progress &progress)
  529. {
  530. if (!need_update)
  531. return;
  532. VLOG(1) << "Total " << scene->objects.size() << " objects.";
  533. device_free(device, dscene);
  534. if (scene->objects.size() == 0)
  535. return;
  536. /* Assign object IDs. */
  537. int index = 0;
  538. foreach (Object *object, scene->objects) {
  539. object->index = index++;
  540. }
  541. /* set object transform matrices, before applying static transforms */
  542. progress.set_status("Updating Objects", "Copying Transformations to device");
  543. device_update_transforms(dscene, scene, progress);
  544. if (progress.get_cancel())
  545. return;
  546. /* prepare for static BVH building */
  547. /* todo: do before to support getting object level coords? */
  548. if (scene->params.bvh_type == SceneParams::BVH_STATIC) {
  549. progress.set_status("Updating Objects", "Applying Static Transformations");
  550. apply_static_transforms(dscene, scene, progress);
  551. }
  552. }
  553. void ObjectManager::device_update_flags(
  554. Device *, DeviceScene *dscene, Scene *scene, Progress & /*progress*/, bool bounds_valid)
  555. {
  556. if (!need_update && !need_flags_update)
  557. return;
  558. need_update = false;
  559. need_flags_update = false;
  560. if (scene->objects.size() == 0)
  561. return;
  562. /* Object info flag. */
  563. uint *object_flag = dscene->object_flag.data();
  564. /* Object volume intersection. */
  565. vector<Object *> volume_objects;
  566. bool has_volume_objects = false;
  567. foreach (Object *object, scene->objects) {
  568. if (object->mesh->has_volume) {
  569. if (bounds_valid) {
  570. volume_objects.push_back(object);
  571. }
  572. has_volume_objects = true;
  573. }
  574. }
  575. foreach (Object *object, scene->objects) {
  576. if (object->mesh->has_volume) {
  577. object_flag[object->index] |= SD_OBJECT_HAS_VOLUME;
  578. object_flag[object->index] &= ~SD_OBJECT_HAS_VOLUME_ATTRIBUTES;
  579. foreach (Attribute &attr, object->mesh->attributes.attributes) {
  580. if (attr.element == ATTR_ELEMENT_VOXEL) {
  581. object_flag[object->index] |= SD_OBJECT_HAS_VOLUME_ATTRIBUTES;
  582. }
  583. }
  584. }
  585. else {
  586. object_flag[object->index] &= ~(SD_OBJECT_HAS_VOLUME | SD_OBJECT_HAS_VOLUME_ATTRIBUTES);
  587. }
  588. if (object->is_shadow_catcher) {
  589. object_flag[object->index] |= SD_OBJECT_SHADOW_CATCHER;
  590. }
  591. else {
  592. object_flag[object->index] &= ~SD_OBJECT_SHADOW_CATCHER;
  593. }
  594. if (bounds_valid) {
  595. foreach (Object *volume_object, volume_objects) {
  596. if (object == volume_object) {
  597. continue;
  598. }
  599. if (object->bounds.intersects(volume_object->bounds)) {
  600. object_flag[object->index] |= SD_OBJECT_INTERSECTS_VOLUME;
  601. break;
  602. }
  603. }
  604. }
  605. else if (has_volume_objects) {
  606. /* Not really valid, but can't make more reliable in the case
  607. * of bounds not being up to date.
  608. */
  609. object_flag[object->index] |= SD_OBJECT_INTERSECTS_VOLUME;
  610. }
  611. }
  612. /* Copy object flag. */
  613. dscene->object_flag.copy_to_device();
  614. }
  615. void ObjectManager::device_update_mesh_offsets(Device *, DeviceScene *dscene, Scene *scene)
  616. {
  617. if (dscene->objects.size() == 0) {
  618. return;
  619. }
  620. KernelObject *kobjects = dscene->objects.data();
  621. bool update = false;
  622. foreach (Object *object, scene->objects) {
  623. Mesh *mesh = object->mesh;
  624. if (mesh->patch_table) {
  625. uint patch_map_offset = 2 * (mesh->patch_table_offset + mesh->patch_table->total_size() -
  626. mesh->patch_table->num_nodes * PATCH_NODE_SIZE) -
  627. mesh->patch_offset;
  628. if (kobjects[object->index].patch_map_offset != patch_map_offset) {
  629. kobjects[object->index].patch_map_offset = patch_map_offset;
  630. update = true;
  631. }
  632. }
  633. if (kobjects[object->index].attribute_map_offset != mesh->attr_map_offset) {
  634. kobjects[object->index].attribute_map_offset = mesh->attr_map_offset;
  635. update = true;
  636. }
  637. }
  638. if (update) {
  639. dscene->objects.copy_to_device();
  640. }
  641. }
  642. void ObjectManager::device_free(Device *, DeviceScene *dscene)
  643. {
  644. dscene->objects.free();
  645. dscene->object_motion_pass.free();
  646. dscene->object_motion.free();
  647. dscene->object_flag.free();
  648. }
  649. void ObjectManager::apply_static_transforms(DeviceScene *dscene, Scene *scene, Progress &progress)
  650. {
  651. /* todo: normals and displacement should be done before applying transform! */
  652. /* todo: create objects/meshes in right order! */
  653. /* counter mesh users */
  654. map<Mesh *, int> mesh_users;
  655. Scene::MotionType need_motion = scene->need_motion();
  656. bool motion_blur = need_motion == Scene::MOTION_BLUR;
  657. bool apply_to_motion = need_motion != Scene::MOTION_PASS;
  658. int i = 0;
  659. bool have_instancing = false;
  660. foreach (Object *object, scene->objects) {
  661. map<Mesh *, int>::iterator it = mesh_users.find(object->mesh);
  662. if (it == mesh_users.end())
  663. mesh_users[object->mesh] = 1;
  664. else
  665. it->second++;
  666. }
  667. if (progress.get_cancel())
  668. return;
  669. uint *object_flag = dscene->object_flag.data();
  670. /* apply transforms for objects with single user meshes */
  671. foreach (Object *object, scene->objects) {
  672. /* Annoying feedback loop here: we can't use is_instanced() because
  673. * it'll use uninitialized transform_applied flag.
  674. *
  675. * Could be solved by moving reference counter to Mesh.
  676. */
  677. if ((mesh_users[object->mesh] == 1 && !object->mesh->has_surface_bssrdf) &&
  678. !object->mesh->has_true_displacement() &&
  679. object->mesh->subdivision_type == Mesh::SUBDIVISION_NONE) {
  680. if (!(motion_blur && object->use_motion())) {
  681. if (!object->mesh->transform_applied) {
  682. object->apply_transform(apply_to_motion);
  683. object->mesh->transform_applied = true;
  684. if (progress.get_cancel())
  685. return;
  686. }
  687. object_flag[i] |= SD_OBJECT_TRANSFORM_APPLIED;
  688. if (object->mesh->transform_negative_scaled)
  689. object_flag[i] |= SD_OBJECT_NEGATIVE_SCALE_APPLIED;
  690. }
  691. else
  692. have_instancing = true;
  693. }
  694. else
  695. have_instancing = true;
  696. i++;
  697. }
  698. dscene->data.bvh.have_instancing = have_instancing;
  699. }
  700. void ObjectManager::tag_update(Scene *scene)
  701. {
  702. need_update = true;
  703. scene->curve_system_manager->need_update = true;
  704. scene->mesh_manager->need_update = true;
  705. scene->light_manager->need_update = true;
  706. }
  707. string ObjectManager::get_cryptomatte_objects(Scene *scene)
  708. {
  709. string manifest = "{";
  710. unordered_set<ustring, ustringHash> objects;
  711. foreach (Object *object, scene->objects) {
  712. if (objects.count(object->name)) {
  713. continue;
  714. }
  715. objects.insert(object->name);
  716. uint32_t hash_name = util_murmur_hash3(object->name.c_str(), object->name.length(), 0);
  717. manifest += string_printf("\"%s\":\"%08x\",", object->name.c_str(), hash_name);
  718. }
  719. manifest[manifest.size() - 1] = '}';
  720. return manifest;
  721. }
  722. string ObjectManager::get_cryptomatte_assets(Scene *scene)
  723. {
  724. string manifest = "{";
  725. unordered_set<ustring, ustringHash> assets;
  726. foreach (Object *ob, scene->objects) {
  727. if (assets.count(ob->asset_name)) {
  728. continue;
  729. }
  730. assets.insert(ob->asset_name);
  731. uint32_t hash_asset = util_murmur_hash3(ob->asset_name.c_str(), ob->asset_name.length(), 0);
  732. manifest += string_printf("\"%s\":\"%08x\",", ob->asset_name.c_str(), hash_asset);
  733. }
  734. manifest[manifest.size() - 1] = '}';
  735. return manifest;
  736. }
  737. CCL_NAMESPACE_END