Code refactor: move more memory allocation logic into device API.
[blender.git] / intern / cycles / render / scene.cpp
index 25f812221ac33151020cfc1ada3144b24f7669b9..260a325206c847dc2fd72cc0bc738a87a41a546b 100644 (file)
 
 #include <stdlib.h>
 
-#include "background.h"
-#include "bake.h"
-#include "camera.h"
-#include "curves.h"
-#include "device.h"
-#include "film.h"
-#include "integrator.h"
-#include "light.h"
-#include "mesh.h"
-#include "object.h"
-#include "osl.h"
-#include "particles.h"
-#include "scene.h"
-#include "shader.h"
-#include "svm.h"
-#include "tables.h"
-
-#include "util_foreach.h"
-#include "util_progress.h"
-
-#ifdef WITH_CYCLES_DEBUG
-#  include "util_guarded_allocator.h"
-#  include "util_logging.h"
-#endif
+#include "render/background.h"
+#include "render/bake.h"
+#include "render/camera.h"
+#include "render/curves.h"
+#include "device/device.h"
+#include "render/film.h"
+#include "render/integrator.h"
+#include "render/light.h"
+#include "render/mesh.h"
+#include "render/object.h"
+#include "render/osl.h"
+#include "render/particles.h"
+#include "render/scene.h"
+#include "render/shader.h"
+#include "render/svm.h"
+#include "render/tables.h"
+
+#include "util/util_foreach.h"
+#include "util/util_guarded_allocator.h"
+#include "util/util_logging.h"
+#include "util/util_progress.h"
 
 CCL_NAMESPACE_BEGIN
 
-Scene::Scene(const SceneParams& params_, const DeviceInfo& device_info_)
-: params(params_)
+DeviceScene::DeviceScene(Device *device)
+: bvh_nodes(device, "__bvh_nodes", MEM_TEXTURE),
+  bvh_leaf_nodes(device, "__bvh_leaf_nodes", MEM_TEXTURE),
+  object_node(device, "__object_node", MEM_TEXTURE),
+  prim_tri_index(device, "__prim_tri_index", MEM_TEXTURE),
+  prim_tri_verts(device, "__prim_tri_verts", MEM_TEXTURE),
+  prim_type(device, "__prim_type", MEM_TEXTURE),
+  prim_visibility(device, "__prim_visibility", MEM_TEXTURE),
+  prim_index(device, "__prim_index", MEM_TEXTURE),
+  prim_object(device, "__prim_object", MEM_TEXTURE),
+  prim_time(device, "__prim_time", MEM_TEXTURE),
+  tri_shader(device, "__tri_shader", MEM_TEXTURE),
+  tri_vnormal(device, "__tri_vnormal", MEM_TEXTURE),
+  tri_vindex(device, "__tri_vindex", MEM_TEXTURE),
+  tri_patch(device, "__tri_patch", MEM_TEXTURE),
+  tri_patch_uv(device, "__tri_patch_uv", MEM_TEXTURE),
+  curves(device, "__curves", MEM_TEXTURE),
+  curve_keys(device, "__curve_keys", MEM_TEXTURE),
+  patches(device, "__patches", MEM_TEXTURE),
+  objects(device, "__objects", MEM_TEXTURE),
+  objects_vector(device, "__objects_vector", MEM_TEXTURE),
+  attributes_map(device, "__attributes_map", MEM_TEXTURE),
+  attributes_float(device, "__attributes_float", MEM_TEXTURE),
+  attributes_float3(device, "__attributes_float3", MEM_TEXTURE),
+  attributes_uchar4(device, "__attributes_uchar4", MEM_TEXTURE),
+  light_distribution(device, "__light_distribution", MEM_TEXTURE),
+  light_data(device, "__light_data", MEM_TEXTURE),
+  light_background_marginal_cdf(device, "__light_background_marginal_cdf", MEM_TEXTURE),
+  light_background_conditional_cdf(device, "__light_background_conditional_cdf", MEM_TEXTURE),
+  particles(device, "__particles", MEM_TEXTURE),
+  svm_nodes(device, "__svm_nodes", MEM_TEXTURE),
+  shader_flag(device, "__shader_flag", MEM_TEXTURE),
+  object_flag(device, "__object_flag", MEM_TEXTURE),
+  lookup_table(device, "__lookup_table", MEM_TEXTURE),
+  sobol_directions(device, "__sobol_directions", MEM_TEXTURE)
+{
+       memset(&data, 0, sizeof(data));
+}
+
+Scene::Scene(const SceneParams& params_, Device *device)
+: device(device), dscene(device), params(params_)
 {
-       device = NULL;
        memset(&dscene.data, 0, sizeof(dscene.data));
 
        camera = new Camera();
@@ -57,19 +92,16 @@ Scene::Scene(const SceneParams& params_, const DeviceInfo& device_info_)
        mesh_manager = new MeshManager();
        object_manager = new ObjectManager();
        integrator = new Integrator();
-       image_manager = new ImageManager();
+       image_manager = new ImageManager(device->info);
        particle_system_manager = new ParticleSystemManager();
        curve_system_manager = new CurveSystemManager();
        bake_manager = new BakeManager();
 
        /* OSL only works on the CPU */
-       if(device_info_.type == DEVICE_CPU)
+       if(device->info.has_osl)
                shader_manager = ShaderManager::create(this, params.shadingsystem);
        else
                shader_manager = ShaderManager::create(this, SHADINGSYSTEM_SVM);
-
-       /* Extended image limits for CPU and GPUs */
-       image_manager->set_extended_image_limits(device_info_);
 }
 
 Scene::~Scene()
@@ -113,9 +145,9 @@ void Scene::free_memory(bool final)
                bake_manager->device_free(device, &dscene);
 
                if(!params.persistent_data || final)
-                       image_manager->device_free(device, &dscene);
+                       image_manager->device_free(device);
                else
-                       image_manager->device_free_builtin(device, &dscene);
+                       image_manager->device_free_builtin(device);
 
                lookup_tables->device_free(device, &dscene);
        }
@@ -141,7 +173,9 @@ void Scene::device_update(Device *device_, Progress& progress)
 {
        if(!device)
                device = device_;
-       
+
+       bool print_stats = need_data_update();
+
        /* The order of updates is important, because there's dependencies between
         * the different managers, using data computed by previous managers.
         *
@@ -152,8 +186,6 @@ void Scene::device_update(Device *device_, Progress& progress)
         * - Film needs light manager to run for use_light_visibility
         * - Lookup tables are done a second time to handle film tables
         */
-       
-       image_manager->set_pack_images(device->info.pack_images);
 
        progress.set_status("Updating Shaders");
        shader_manager->device_update(device, &dscene, this, progress);
@@ -170,13 +202,13 @@ void Scene::device_update(Device *device_, Progress& progress)
 
        if(progress.get_cancel() || device->have_error()) return;
 
-       progress.set_status("Updating Objects");
-       object_manager->device_update(device, &dscene, this, progress);
+       progress.set_status("Updating Meshes Flags");
+       mesh_manager->device_update_flags(device, &dscene, this, progress);
 
        if(progress.get_cancel() || device->have_error()) return;
 
-       progress.set_status("Updating Meshes Flags");
-       mesh_manager->device_update_flags(device, &dscene, this, progress);
+       progress.set_status("Updating Objects");
+       object_manager->device_update(device, &dscene, this, progress);
 
        if(progress.get_cancel() || device->have_error()) return;
 
@@ -191,7 +223,7 @@ void Scene::device_update(Device *device_, Progress& progress)
        if(progress.get_cancel() || device->have_error()) return;
 
        progress.set_status("Updating Images");
-       image_manager->device_update(device, &dscene, progress);
+       image_manager->device_update(device, this, progress);
 
        if(progress.get_cancel() || device->have_error()) return;
 
@@ -220,13 +252,13 @@ void Scene::device_update(Device *device_, Progress& progress)
 
        if(progress.get_cancel() || device->have_error()) return;
 
-       progress.set_status("Updating Film");
-       film->device_update(device, &dscene, this);
+       progress.set_status("Updating Integrator");
+       integrator->device_update(device, &dscene, this);
 
        if(progress.get_cancel() || device->have_error()) return;
 
-       progress.set_status("Updating Integrator");
-       integrator->device_update(device, &dscene, this);
+       progress.set_status("Updating Film");
+       film->device_update(device, &dscene, this);
 
        if(progress.get_cancel() || device->have_error()) return;
 
@@ -245,11 +277,16 @@ void Scene::device_update(Device *device_, Progress& progress)
                device->const_copy_to("__data", &dscene.data, sizeof(dscene.data));
        }
 
-#ifdef WITH_CYCLES_DEBUG
-       VLOG(1) << "System memory statistics after full device sync:\n"
-               << "  Usage: " << util_guarded_get_mem_used() << "\n"
-               << "  Peak: " << util_guarded_get_mem_peak();
-#endif
+       if(print_stats) {
+               size_t mem_used = util_guarded_get_mem_used();
+               size_t mem_peak = util_guarded_get_mem_peak();
+
+               VLOG(1) << "System memory statistics after full device sync:\n"
+                       << "  Usage: " << string_human_readable_number(mem_used)
+                       << " (" << string_human_readable_size(mem_used) << ")\n"
+                       << "  Peak: " << string_human_readable_number(mem_peak)
+                       << " (" << string_human_readable_size(mem_peak) << ")";
+       }
 }
 
 Scene::MotionType Scene::need_motion(bool advanced_shading)
@@ -262,6 +299,14 @@ Scene::MotionType Scene::need_motion(bool advanced_shading)
                return MOTION_NONE;
 }
 
+float Scene::motion_shutter_time()
+{
+       if(need_motion() == Scene::MOTION_PASS)
+               return 2.0f;
+       else
+               return camera->shuttertime;
+}
+
 bool Scene::need_global_attribute(AttributeStandard std)
 {
        if(std == ATTR_STD_UV)
@@ -286,11 +331,10 @@ bool Scene::need_update()
        return (need_reset() || film->need_update);
 }
 
-bool Scene::need_reset()
+bool Scene::need_data_update()
 {
        return (background->need_update
                || image_manager->need_update
-               || camera->need_update
                || object_manager->need_update
                || mesh_manager->need_update
                || light_manager->need_update
@@ -303,6 +347,11 @@ bool Scene::need_reset()
                || film->need_update);
 }
 
+bool Scene::need_reset()
+{
+       return need_data_update() || camera->need_update;
+}
+
 void Scene::reset()
 {
        shader_manager->reset(this);