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 #ifndef __SCENE_H__ 18 #define __SCENE_H__ 19 20 #include "bvh/bvh_params.h" 21 22 #include "render/film.h" 23 #include "render/image.h" 24 #include "render/shader.h" 25 26 #include "device/device.h" 27 #include "device/device_memory.h" 28 29 #include "util/util_param.h" 30 #include "util/util_string.h" 31 #include "util/util_system.h" 32 #include "util/util_texture.h" 33 #include "util/util_thread.h" 34 #include "util/util_types.h" 35 #include "util/util_vector.h" 36 37 CCL_NAMESPACE_BEGIN 38 39 class AttributeRequestSet; 40 class Background; 41 class Camera; 42 class Device; 43 class DeviceInfo; 44 class Film; 45 class Integrator; 46 class Light; 47 class LightManager; 48 class LookupTables; 49 class Geometry; 50 class GeometryManager; 51 class Object; 52 class ObjectManager; 53 class ParticleSystemManager; 54 class ParticleSystem; 55 class CurveSystemManager; 56 class Shader; 57 class ShaderManager; 58 class Progress; 59 class BakeManager; 60 class BakeData; 61 class RenderStats; 62 class SceneUpdateStats; 63 class Volume; 64 65 /* Scene Device Data */ 66 67 class DeviceScene { 68 public: 69 /* BVH */ 70 device_vector<int4> bvh_nodes; 71 device_vector<int4> bvh_leaf_nodes; 72 device_vector<int> object_node; 73 device_vector<uint> prim_tri_index; 74 device_vector<float4> prim_tri_verts; 75 device_vector<int> prim_type; 76 device_vector<uint> prim_visibility; 77 device_vector<int> prim_index; 78 device_vector<int> prim_object; 79 device_vector<float2> prim_time; 80 81 /* mesh */ 82 device_vector<uint> tri_shader; 83 device_vector<float4> tri_vnormal; 84 device_vector<uint4> tri_vindex; 85 device_vector<uint> tri_patch; 86 device_vector<float2> tri_patch_uv; 87 88 device_vector<float4> curves; 89 device_vector<float4> curve_keys; 90 91 device_vector<uint> patches; 92 93 /* objects */ 94 device_vector<KernelObject> objects; 95 device_vector<Transform> object_motion_pass; 96 device_vector<DecomposedTransform> object_motion; 97 device_vector<uint> object_flag; 98 device_vector<float> object_volume_step; 99 100 /* cameras */ 101 device_vector<DecomposedTransform> camera_motion; 102 103 /* attributes */ 104 device_vector<uint4> attributes_map; 105 device_vector<float> attributes_float; 106 device_vector<float2> attributes_float2; 107 device_vector<float4> attributes_float3; 108 device_vector<uchar4> attributes_uchar4; 109 110 /* lights */ 111 device_vector<KernelLightDistribution> light_distribution; 112 device_vector<KernelLight> lights; 113 device_vector<float2> light_background_marginal_cdf; 114 device_vector<float2> light_background_conditional_cdf; 115 116 /* particles */ 117 device_vector<KernelParticle> particles; 118 119 /* shaders */ 120 device_vector<int4> svm_nodes; 121 device_vector<KernelShader> shaders; 122 123 /* lookup tables */ 124 device_vector<float> lookup_table; 125 126 /* integrator */ 127 device_vector<uint> sample_pattern_lut; 128 129 /* ies lights */ 130 device_vector<float> ies_lights; 131 132 KernelData data; 133 134 DeviceScene(Device *device); 135 }; 136 137 /* Scene Parameters */ 138 139 class SceneParams { 140 public: 141 /* Type of BVH, in terms whether it is supported dynamic updates of meshes 142 * or whether modifying geometry requires full BVH rebuild. 143 */ 144 enum BVHType { 145 /* BVH supports dynamic updates of geometry. 146 * 147 * Faster for updating BVH tree when doing modifications in viewport, 148 * but slower for rendering. 149 */ 150 BVH_DYNAMIC = 0, 151 /* BVH tree is calculated for specific scene, updates in geometry 152 * requires full tree rebuild. 153 * 154 * Slower to update BVH tree when modifying objects in viewport, also 155 * slower to build final BVH tree but gives best possible render speed. 156 */ 157 BVH_STATIC = 1, 158 159 BVH_NUM_TYPES, 160 }; 161 162 ShadingSystem shadingsystem; 163 164 /* Requested BVH layout. 165 * 166 * If it's not supported by the device, the widest one from supported ones 167 * will be used, but BVH wider than this one will never be used. 168 */ 169 BVHLayout bvh_layout; 170 171 BVHType bvh_type; 172 bool use_bvh_spatial_split; 173 bool use_bvh_unaligned_nodes; 174 int num_bvh_time_steps; 175 int hair_subdivisions; 176 CurveShapeType hair_shape; 177 bool persistent_data; 178 int texture_limit; 179 180 bool background; 181 SceneParams()182 SceneParams() 183 { 184 shadingsystem = SHADINGSYSTEM_SVM; 185 bvh_layout = BVH_LAYOUT_BVH2; 186 bvh_type = BVH_DYNAMIC; 187 use_bvh_spatial_split = false; 188 use_bvh_unaligned_nodes = true; 189 num_bvh_time_steps = 0; 190 hair_subdivisions = 3; 191 hair_shape = CURVE_RIBBON; 192 persistent_data = false; 193 texture_limit = 0; 194 background = true; 195 } 196 modified(const SceneParams & params)197 bool modified(const SceneParams ¶ms) 198 { 199 return !(shadingsystem == params.shadingsystem && bvh_layout == params.bvh_layout && 200 bvh_type == params.bvh_type && 201 use_bvh_spatial_split == params.use_bvh_spatial_split && 202 use_bvh_unaligned_nodes == params.use_bvh_unaligned_nodes && 203 num_bvh_time_steps == params.num_bvh_time_steps && 204 hair_subdivisions == params.hair_subdivisions && hair_shape == params.hair_shape && 205 persistent_data == params.persistent_data && texture_limit == params.texture_limit); 206 } 207 curve_subdivisions()208 int curve_subdivisions() 209 { 210 /* Matching the tesselation rate limit in Embree. */ 211 return clamp(1 << hair_subdivisions, 1, 16); 212 } 213 }; 214 215 /* Scene */ 216 217 class Scene : public NodeOwner { 218 public: 219 /* Optional name. Is used for logging and reporting. */ 220 string name; 221 222 /* data */ 223 Camera *camera; 224 Camera *dicing_camera; 225 LookupTables *lookup_tables; 226 Film *film; 227 Background *background; 228 Integrator *integrator; 229 230 /* data lists */ 231 vector<Object *> objects; 232 vector<Geometry *> geometry; 233 vector<Shader *> shaders; 234 vector<Light *> lights; 235 vector<ParticleSystem *> particle_systems; 236 vector<Pass> passes; 237 238 /* data managers */ 239 ImageManager *image_manager; 240 LightManager *light_manager; 241 ShaderManager *shader_manager; 242 GeometryManager *geometry_manager; 243 ObjectManager *object_manager; 244 ParticleSystemManager *particle_system_manager; 245 BakeManager *bake_manager; 246 247 /* default shaders */ 248 Shader *default_surface; 249 Shader *default_volume; 250 Shader *default_light; 251 Shader *default_background; 252 Shader *default_empty; 253 254 /* device */ 255 Device *device; 256 DeviceScene dscene; 257 258 /* parameters */ 259 SceneParams params; 260 261 /* mutex must be locked manually by callers */ 262 thread_mutex mutex; 263 264 /* scene update statistics */ 265 SceneUpdateStats *update_stats; 266 267 Scene(const SceneParams ¶ms, Device *device); 268 ~Scene(); 269 270 void device_update(Device *device, Progress &progress); 271 272 bool need_global_attribute(AttributeStandard std); 273 void need_global_attributes(AttributeRequestSet &attributes); 274 275 enum MotionType { MOTION_NONE = 0, MOTION_PASS, MOTION_BLUR }; 276 MotionType need_motion(); 277 float motion_shutter_time(); 278 279 bool need_update(); 280 bool need_reset(); 281 282 void reset(); 283 void device_free(); 284 285 void collect_statistics(RenderStats *stats); 286 287 void enable_update_stats(); 288 289 bool update(Progress &progress, bool &kernel_switch_needed); 290 291 /* This function is used to create a node of a specified type instead of 292 * calling 'new', and sets the scene as the owner of the node. 293 * The function has overloads that will also add the created node to the right 294 * node array (e.g. Scene::geometry for Geometry nodes) and tag the appropriate 295 * manager for an update. 296 */ create_node(Args &&...args)297 template<typename T, typename... Args> T *create_node(Args &&... args) 298 { 299 T *node = new T(args...); 300 node->set_owner(this); 301 return node; 302 } 303 304 /* This function is used to delete a node from the scene instead of calling 'delete' 305 * and manually removing the node from the data array. It also tags the 306 * appropriate manager for an update, if any, and checks that the scene is indeed 307 * the owner of the node. Calling this function on a node not owned by the scene 308 * will likely cause a crash which we want in order to detect such cases. 309 */ delete_node(T * node)310 template<typename T> void delete_node(T *node) 311 { 312 assert(node->get_owner() == this); 313 delete_node_impl(node); 314 } 315 316 /* Same as above, but specify the actual owner. 317 */ delete_node(T * node,const NodeOwner * owner)318 template<typename T> void delete_node(T *node, const NodeOwner *owner) 319 { 320 assert(node->get_owner() == owner); 321 delete_node_impl(node); 322 (void)owner; 323 } 324 325 /* Remove all nodes in the set from the appropriate data arrays, and tag the 326 * specific managers for an update. This assumes that the scene owns the nodes. 327 */ delete_nodes(const set<T * > & nodes)328 template<typename T> void delete_nodes(const set<T *> &nodes) 329 { 330 delete_nodes(nodes, this); 331 } 332 333 /* Same as above, but specify the actual owner of all the nodes in the set. 334 */ 335 template<typename T> void delete_nodes(const set<T *> &nodes, const NodeOwner *owner); 336 337 protected: 338 /* Check if some heavy data worth logging was updated. 339 * Mainly used to suppress extra annoying logging. 340 */ 341 bool need_data_update(); 342 343 void free_memory(bool final); 344 345 bool kernels_loaded; 346 DeviceRequestedFeatures loaded_kernel_features; 347 348 bool load_kernels(Progress &progress, bool lock_scene = true); 349 350 /* ** Split kernel routines ** */ 351 352 DeviceRequestedFeatures get_requested_device_features(); 353 354 /* Maximumnumber of closure during session lifetime. */ 355 int max_closure_global; 356 357 /* Get maximum number of closures to be used in kernel. */ 358 int get_max_closure_count(); 359 delete_node_impl(T * node)360 template<typename T> void delete_node_impl(T *node) 361 { 362 delete node; 363 } 364 }; 365 366 template<> Light *Scene::create_node<Light>(); 367 368 template<> Mesh *Scene::create_node<Mesh>(); 369 370 template<> Object *Scene::create_node<Object>(); 371 372 template<> Hair *Scene::create_node<Hair>(); 373 374 template<> Volume *Scene::create_node<Volume>(); 375 376 template<> ParticleSystem *Scene::create_node<ParticleSystem>(); 377 378 template<> Shader *Scene::create_node<Shader>(); 379 380 template<> void Scene::delete_node_impl(Light *node); 381 382 template<> void Scene::delete_node_impl(Mesh *node); 383 384 template<> void Scene::delete_node_impl(Volume *node); 385 386 template<> void Scene::delete_node_impl(Hair *node); 387 388 template<> void Scene::delete_node_impl(Geometry *node); 389 390 template<> void Scene::delete_node_impl(Object *node); 391 392 template<> void Scene::delete_node_impl(ParticleSystem *node); 393 394 template<> void Scene::delete_node_impl(Shader *node); 395 396 template<> void Scene::delete_nodes(const set<Light *> &nodes, const NodeOwner *owner); 397 398 template<> void Scene::delete_nodes(const set<Geometry *> &nodes, const NodeOwner *owner); 399 400 template<> void Scene::delete_nodes(const set<Object *> &nodes, const NodeOwner *owner); 401 402 template<> void Scene::delete_nodes(const set<ParticleSystem *> &nodes, const NodeOwner *owner); 403 404 template<> void Scene::delete_nodes(const set<Shader *> &nodes, const NodeOwner *owner); 405 406 CCL_NAMESPACE_END 407 408 #endif /* __SCENE_H__ */ 409