scene.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 <stdlib.h>
  17. #include "render/background.h"
  18. #include "render/bake.h"
  19. #include "render/camera.h"
  20. #include "render/curves.h"
  21. #include "device/device.h"
  22. #include "render/film.h"
  23. #include "render/integrator.h"
  24. #include "render/light.h"
  25. #include "render/mesh.h"
  26. #include "render/object.h"
  27. #include "render/osl.h"
  28. #include "render/particles.h"
  29. #include "render/scene.h"
  30. #include "render/shader.h"
  31. #include "render/svm.h"
  32. #include "render/tables.h"
  33. #include "util/util_foreach.h"
  34. #include "util/util_guarded_allocator.h"
  35. #include "util/util_logging.h"
  36. #include "util/util_progress.h"
  37. CCL_NAMESPACE_BEGIN
  38. DeviceScene::DeviceScene(Device *device)
  39. : bvh_nodes(device, "__bvh_nodes", MEM_TEXTURE),
  40. bvh_leaf_nodes(device, "__bvh_leaf_nodes", MEM_TEXTURE),
  41. object_node(device, "__object_node", MEM_TEXTURE),
  42. prim_tri_index(device, "__prim_tri_index", MEM_TEXTURE),
  43. prim_tri_verts(device, "__prim_tri_verts", MEM_TEXTURE),
  44. prim_type(device, "__prim_type", MEM_TEXTURE),
  45. prim_visibility(device, "__prim_visibility", MEM_TEXTURE),
  46. prim_index(device, "__prim_index", MEM_TEXTURE),
  47. prim_object(device, "__prim_object", MEM_TEXTURE),
  48. prim_time(device, "__prim_time", MEM_TEXTURE),
  49. tri_shader(device, "__tri_shader", MEM_TEXTURE),
  50. tri_vnormal(device, "__tri_vnormal", MEM_TEXTURE),
  51. tri_vindex(device, "__tri_vindex", MEM_TEXTURE),
  52. tri_patch(device, "__tri_patch", MEM_TEXTURE),
  53. tri_patch_uv(device, "__tri_patch_uv", MEM_TEXTURE),
  54. curves(device, "__curves", MEM_TEXTURE),
  55. curve_keys(device, "__curve_keys", MEM_TEXTURE),
  56. patches(device, "__patches", MEM_TEXTURE),
  57. objects(device, "__objects", MEM_TEXTURE),
  58. object_motion_pass(device, "__object_motion_pass", MEM_TEXTURE),
  59. object_motion(device, "__object_motion", MEM_TEXTURE),
  60. object_flag(device, "__object_flag", MEM_TEXTURE),
  61. camera_motion(device, "__camera_motion", MEM_TEXTURE),
  62. attributes_map(device, "__attributes_map", MEM_TEXTURE),
  63. attributes_float(device, "__attributes_float", MEM_TEXTURE),
  64. attributes_float2(device, "__attributes_float2", MEM_TEXTURE),
  65. attributes_float3(device, "__attributes_float3", MEM_TEXTURE),
  66. attributes_uchar4(device, "__attributes_uchar4", MEM_TEXTURE),
  67. light_distribution(device, "__light_distribution", MEM_TEXTURE),
  68. lights(device, "__lights", MEM_TEXTURE),
  69. light_background_marginal_cdf(device, "__light_background_marginal_cdf", MEM_TEXTURE),
  70. light_background_conditional_cdf(device, "__light_background_conditional_cdf", MEM_TEXTURE),
  71. particles(device, "__particles", MEM_TEXTURE),
  72. svm_nodes(device, "__svm_nodes", MEM_TEXTURE),
  73. shaders(device, "__shaders", MEM_TEXTURE),
  74. lookup_table(device, "__lookup_table", MEM_TEXTURE),
  75. sobol_directions(device, "__sobol_directions", MEM_TEXTURE),
  76. ies_lights(device, "__ies", MEM_TEXTURE)
  77. {
  78. memset((void *)&data, 0, sizeof(data));
  79. }
  80. Scene::Scene(const SceneParams &params_, Device *device)
  81. : name("Scene"), device(device), dscene(device), params(params_)
  82. {
  83. memset((void *)&dscene.data, 0, sizeof(dscene.data));
  84. camera = new Camera();
  85. dicing_camera = new Camera();
  86. lookup_tables = new LookupTables();
  87. film = new Film();
  88. background = new Background();
  89. light_manager = new LightManager();
  90. mesh_manager = new MeshManager();
  91. object_manager = new ObjectManager();
  92. integrator = new Integrator();
  93. image_manager = new ImageManager(device->info);
  94. particle_system_manager = new ParticleSystemManager();
  95. curve_system_manager = new CurveSystemManager();
  96. bake_manager = new BakeManager();
  97. /* OSL only works on the CPU */
  98. if (device->info.has_osl)
  99. shader_manager = ShaderManager::create(this, params.shadingsystem);
  100. else
  101. shader_manager = ShaderManager::create(this, SHADINGSYSTEM_SVM);
  102. }
  103. Scene::~Scene()
  104. {
  105. free_memory(true);
  106. }
  107. void Scene::free_memory(bool final)
  108. {
  109. foreach (Shader *s, shaders)
  110. delete s;
  111. foreach (Mesh *m, meshes)
  112. delete m;
  113. foreach (Object *o, objects)
  114. delete o;
  115. foreach (Light *l, lights)
  116. delete l;
  117. foreach (ParticleSystem *p, particle_systems)
  118. delete p;
  119. shaders.clear();
  120. meshes.clear();
  121. objects.clear();
  122. lights.clear();
  123. particle_systems.clear();
  124. if (device) {
  125. camera->device_free(device, &dscene, this);
  126. film->device_free(device, &dscene, this);
  127. background->device_free(device, &dscene);
  128. integrator->device_free(device, &dscene);
  129. object_manager->device_free(device, &dscene);
  130. mesh_manager->device_free(device, &dscene);
  131. shader_manager->device_free(device, &dscene, this);
  132. light_manager->device_free(device, &dscene);
  133. particle_system_manager->device_free(device, &dscene);
  134. curve_system_manager->device_free(device, &dscene);
  135. bake_manager->device_free(device, &dscene);
  136. if (!params.persistent_data || final)
  137. image_manager->device_free(device);
  138. else
  139. image_manager->device_free_builtin(device);
  140. lookup_tables->device_free(device, &dscene);
  141. }
  142. if (final) {
  143. delete lookup_tables;
  144. delete camera;
  145. delete dicing_camera;
  146. delete film;
  147. delete background;
  148. delete integrator;
  149. delete object_manager;
  150. delete mesh_manager;
  151. delete shader_manager;
  152. delete light_manager;
  153. delete particle_system_manager;
  154. delete curve_system_manager;
  155. delete image_manager;
  156. delete bake_manager;
  157. }
  158. }
  159. void Scene::device_update(Device *device_, Progress &progress)
  160. {
  161. if (!device)
  162. device = device_;
  163. bool print_stats = need_data_update();
  164. /* The order of updates is important, because there's dependencies between
  165. * the different managers, using data computed by previous managers.
  166. *
  167. * - Image manager uploads images used by shaders.
  168. * - Camera may be used for adaptive subdivision.
  169. * - Displacement shader must have all shader data available.
  170. * - Light manager needs lookup tables and final mesh data to compute emission CDF.
  171. * - Film needs light manager to run for use_light_visibility
  172. * - Lookup tables are done a second time to handle film tables
  173. */
  174. progress.set_status("Updating Shaders");
  175. shader_manager->device_update(device, &dscene, this, progress);
  176. if (progress.get_cancel() || device->have_error())
  177. return;
  178. progress.set_status("Updating Background");
  179. background->device_update(device, &dscene, this);
  180. if (progress.get_cancel() || device->have_error())
  181. return;
  182. progress.set_status("Updating Camera");
  183. camera->device_update(device, &dscene, this);
  184. if (progress.get_cancel() || device->have_error())
  185. return;
  186. mesh_manager->device_update_preprocess(device, this, progress);
  187. if (progress.get_cancel() || device->have_error())
  188. return;
  189. progress.set_status("Updating Objects");
  190. object_manager->device_update(device, &dscene, this, progress);
  191. if (progress.get_cancel() || device->have_error())
  192. return;
  193. progress.set_status("Updating Hair Systems");
  194. curve_system_manager->device_update(device, &dscene, this, progress);
  195. if (progress.get_cancel() || device->have_error())
  196. return;
  197. progress.set_status("Updating Particle Systems");
  198. particle_system_manager->device_update(device, &dscene, this, progress);
  199. if (progress.get_cancel() || device->have_error())
  200. return;
  201. progress.set_status("Updating Meshes");
  202. mesh_manager->device_update(device, &dscene, this, progress);
  203. if (progress.get_cancel() || device->have_error())
  204. return;
  205. progress.set_status("Updating Objects Flags");
  206. object_manager->device_update_flags(device, &dscene, this, progress);
  207. if (progress.get_cancel() || device->have_error())
  208. return;
  209. progress.set_status("Updating Images");
  210. image_manager->device_update(device, this, progress);
  211. if (progress.get_cancel() || device->have_error())
  212. return;
  213. progress.set_status("Updating Camera Volume");
  214. camera->device_update_volume(device, &dscene, this);
  215. if (progress.get_cancel() || device->have_error())
  216. return;
  217. progress.set_status("Updating Lookup Tables");
  218. lookup_tables->device_update(device, &dscene);
  219. if (progress.get_cancel() || device->have_error())
  220. return;
  221. progress.set_status("Updating Lights");
  222. light_manager->device_update(device, &dscene, this, progress);
  223. if (progress.get_cancel() || device->have_error())
  224. return;
  225. progress.set_status("Updating Integrator");
  226. integrator->device_update(device, &dscene, this);
  227. if (progress.get_cancel() || device->have_error())
  228. return;
  229. progress.set_status("Updating Film");
  230. film->device_update(device, &dscene, this);
  231. if (progress.get_cancel() || device->have_error())
  232. return;
  233. progress.set_status("Updating Lookup Tables");
  234. lookup_tables->device_update(device, &dscene);
  235. if (progress.get_cancel() || device->have_error())
  236. return;
  237. progress.set_status("Updating Baking");
  238. bake_manager->device_update(device, &dscene, this, progress);
  239. if (progress.get_cancel() || device->have_error())
  240. return;
  241. if (device->have_error() == false) {
  242. progress.set_status("Updating Device", "Writing constant memory");
  243. device->const_copy_to("__data", &dscene.data, sizeof(dscene.data));
  244. }
  245. if (print_stats) {
  246. size_t mem_used = util_guarded_get_mem_used();
  247. size_t mem_peak = util_guarded_get_mem_peak();
  248. VLOG(1) << "System memory statistics after full device sync:\n"
  249. << " Usage: " << string_human_readable_number(mem_used) << " ("
  250. << string_human_readable_size(mem_used) << ")\n"
  251. << " Peak: " << string_human_readable_number(mem_peak) << " ("
  252. << string_human_readable_size(mem_peak) << ")";
  253. }
  254. }
  255. Scene::MotionType Scene::need_motion()
  256. {
  257. if (integrator->motion_blur)
  258. return MOTION_BLUR;
  259. else if (Pass::contains(film->passes, PASS_MOTION))
  260. return MOTION_PASS;
  261. else
  262. return MOTION_NONE;
  263. }
  264. float Scene::motion_shutter_time()
  265. {
  266. if (need_motion() == Scene::MOTION_PASS)
  267. return 2.0f;
  268. else
  269. return camera->shuttertime;
  270. }
  271. bool Scene::need_global_attribute(AttributeStandard std)
  272. {
  273. if (std == ATTR_STD_UV)
  274. return Pass::contains(film->passes, PASS_UV);
  275. else if (std == ATTR_STD_MOTION_VERTEX_POSITION)
  276. return need_motion() != MOTION_NONE;
  277. else if (std == ATTR_STD_MOTION_VERTEX_NORMAL)
  278. return need_motion() == MOTION_BLUR;
  279. return false;
  280. }
  281. void Scene::need_global_attributes(AttributeRequestSet &attributes)
  282. {
  283. for (int std = ATTR_STD_NONE; std < ATTR_STD_NUM; std++)
  284. if (need_global_attribute((AttributeStandard)std))
  285. attributes.add((AttributeStandard)std);
  286. }
  287. bool Scene::need_update()
  288. {
  289. return (need_reset() || film->need_update);
  290. }
  291. bool Scene::need_data_update()
  292. {
  293. return (background->need_update || image_manager->need_update || object_manager->need_update ||
  294. mesh_manager->need_update || light_manager->need_update || lookup_tables->need_update ||
  295. integrator->need_update || shader_manager->need_update ||
  296. particle_system_manager->need_update || curve_system_manager->need_update ||
  297. bake_manager->need_update || film->need_update);
  298. }
  299. bool Scene::need_reset()
  300. {
  301. return need_data_update() || camera->need_update;
  302. }
  303. void Scene::reset()
  304. {
  305. shader_manager->reset(this);
  306. shader_manager->add_default(this);
  307. /* ensure all objects are updated */
  308. camera->tag_update();
  309. dicing_camera->tag_update();
  310. film->tag_update(this);
  311. background->tag_update(this);
  312. integrator->tag_update(this);
  313. object_manager->tag_update(this);
  314. mesh_manager->tag_update(this);
  315. light_manager->tag_update(this);
  316. particle_system_manager->tag_update(this);
  317. curve_system_manager->tag_update(this);
  318. }
  319. void Scene::device_free()
  320. {
  321. free_memory(false);
  322. }
  323. void Scene::collect_statistics(RenderStats *stats)
  324. {
  325. mesh_manager->collect_statistics(this, stats);
  326. image_manager->collect_statistics(stats);
  327. }
  328. CCL_NAMESPACE_END